fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int MaxN = 1e6 + 5;
  4. long long n, a[MaxN];
  5. int main()
  6. {
  7. ios_base::sync_with_stdio(0);
  8. cin.tie(0);
  9. freopen("file.inp", "r", stdin);
  10. freopen("file.out", "w", stdout);
  11. cin >> n;
  12.  
  13. long long mx = 0;
  14. for (int i = 1; i <= n; i++)
  15. {
  16. cin >> a[i];
  17. mx = max(mx, a[i]);
  18. }
  19.  
  20. long long res = mx - a[1];
  21. for (int i = 2; i <= n; i++)
  22. {
  23. res += max(0LL, a[i - 1] - a[i]);
  24. }
  25.  
  26. cout << res << "\n";
  27.  
  28. return 0;
  29. }
  30.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty