fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long int
  4. const int M = 1e9 + 7;
  5.  
  6. const int N = 2e5 + 10;
  7.  
  8. int main()
  9. {
  10. ios_base::sync_with_stdio(false);
  11. cin.tie(NULL);
  12. int n;
  13. cin >> n;
  14. vector<int> v(n);
  15. for (int i = 0; i < n; i++)
  16. cin >> v[i];
  17. vector<int> proj(n);
  18. int ma = 0;
  19. for (int i = 0; i < n; i++)
  20. {
  21. ma = max(ma, v[i]);
  22. proj[i] = ma - v[i];
  23. }
  24. int i = 1;
  25. int ans = 0;
  26. while (i < n)
  27. {
  28. while (i < n && proj[i - 1] <= proj[i])
  29. i++;
  30. ans += proj[i - 1];
  31. while (i < n && proj[i - 1] >= proj[i])
  32. i++;
  33. }
  34.  
  35. cout << ans << endl;
  36. return 0;
  37. }
Success #stdin #stdout 0s 5292KB
stdin
6
3 5 2 1 6 2
stdout
8