CSES - Shared codeLink to this code: https://cses.fi/paste/cbc2ace1b3ce631f827c22/
#include <bits/stdc++.h>
using namespace std;
#define nl "\n"
#define nf endl
#define ll long long
#define pb push_back
#define _ << ' ' <<
#define INF (ll)1e18
#define mod 998244353
#define maxn 110
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
#if !ONLINE_JUDGE && !EVAL
ifstream cin("input.txt");
ofstream cout("output.txt");
#endif
ll n; cin >> n;
vector<ll> a(n + 1, 0);
ll start = 0;
for (ll i = 1; i <= n; i++) cin >> a[i], start += a[i];
priority_queue<ll> pq;
for (ll i = 1; i <= n; i++) {
pq.push(a[i]); pq.push(a[i]); pq.pop();
}
pq.push(0);
ll bonus = 0, curr_slope = 0, curr_pos = INF;
while (!pq.empty()) {
ll x = pq.top(); pq.pop();
bonus += (curr_slope * (curr_pos - x));
// cout << curr_slope _ curr_pos _ x << nl;
curr_pos = x; curr_slope++;
}
ll ans = start - bonus;
cout << ans << nl;
return 0;
}