fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define pb emplace_back
  5.  
  6. signed main()
  7. {
  8.  
  9. int n, i, x, ans = 0;
  10.  
  11. cin >> n;
  12. vector<int> a(n, 0);
  13. priority_queue<int> pq;
  14.  
  15. for (i = 0; i < n; i++)
  16. {
  17. cin >> a[i];
  18. pq.push(a[i]);
  19.  
  20. x = pq.top() - a[i];
  21.  
  22. ans += x;
  23. pq.pop();
  24. pq.push(a[i]);
  25. }
  26. cout << ans << endl;
  27. }
Success #stdin #stdout 0s 5640KB
stdin
5
3 8 5 6 5
stdout
4