Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- int main() {
- freopen("in", "r", stdin);
- ll l, n;
- cin >> l >> n;
- priority_queue<ll, vector<ll>, greater<ll> > pq;
- for (int i = 0; i < n; i++) {
- ll x; cin >> x;
- pq.push(x);
- l -= x;
- }
- if (l != 0) pq.push(l);
- ll ans = 0;
- while (pq.size() > 1) {
- ll a = pq.top(); pq.pop();
- ll b = pq.top(); pq.pop();
- ll s = a + b;
- ans += s;
- pq.push(s);
- }
- cout << ans << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment