#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Function to return the minimum cost of connecting the ropes.
int minCost(vector<int>& arr) {
int totalCost = 0;
while (arr.size() > 1) {
sort(arr.begin(), arr.end());
int first = arr[0];
int second = arr[1];
arr.erase(arr.begin());
arr.erase(arr.begin());
int cost = first + second;
totalCost += cost;
arr.push_back(cost);
}
return totalCost;
}
int main() {
vector<int> ropes = {4, 6,8, 1, 56, 43, 10};
cout << minCost(ropes) << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8YWxnb3JpdGhtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKLy8gRnVuY3Rpb24gdG8gcmV0dXJuIHRoZSBtaW5pbXVtIGNvc3Qgb2YgY29ubmVjdGluZyB0aGUgcm9wZXMuCmludCBtaW5Db3N0KHZlY3RvcjxpbnQ+JiBhcnIpIHsKICAgIGludCB0b3RhbENvc3QgPSAwOwogICAgd2hpbGUgKGFyci5zaXplKCkgPiAxKSB7CiAgICAgICAgc29ydChhcnIuYmVnaW4oKSwgYXJyLmVuZCgpKTsKICAgICAgICBpbnQgZmlyc3QgPSBhcnJbMF07CiAgICAgICAgaW50IHNlY29uZCA9IGFyclsxXTsKICAgICAgICBhcnIuZXJhc2UoYXJyLmJlZ2luKCkpOwogICAgICAgIGFyci5lcmFzZShhcnIuYmVnaW4oKSk7CiAgICAgICAgaW50IGNvc3QgPSBmaXJzdCArIHNlY29uZDsKICAgICAgICB0b3RhbENvc3QgKz0gY29zdDsKICAgICAgICBhcnIucHVzaF9iYWNrKGNvc3QpOwogICAgfQogICAgcmV0dXJuIHRvdGFsQ29zdDsKfQoKaW50IG1haW4oKSB7CiAgICB2ZWN0b3I8aW50PiByb3BlcyA9IHs0LCA2LDgsIDEsIDU2LCA0MywgMTB9OwogICAgY291dCA8PCBtaW5Db3N0KHJvcGVzKSA8PCBlbmRsOwogICAgcmV0dXJuIDA7Cn0=