tuki2501

chiadoan.cpp

Sep 3rd, 2022
820
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.46 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5.  
  6. int main() {
  7.     freopen("in", "r", stdin);
  8.     ll l, n;
  9.     cin >> l >> n;
  10.     priority_queue<ll, vector<ll>, greater<ll> > pq;
  11.     for (int i = 0; i < n; i++) {
  12.         ll x; cin >> x;
  13.         pq.push(x);
  14.         l -= x;
  15.     }
  16.     if (l != 0) pq.push(l);
  17.     ll ans = 0;
  18.     while (pq.size() > 1) {
  19.         ll a = pq.top(); pq.pop();
  20.         ll b = pq.top(); pq.pop();
  21.         ll s = a + b;
  22.         ans += s;
  23.         pq.push(s);
  24.     }
  25.     cout << ans << '\n';
  26. }
Advertisement
Add Comment
Please, Sign In to add comment