fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void solve() {
  5. int n;
  6. cin >> n;
  7. vector<int> h(n);
  8. for (int i = 0; i < n; i++) {
  9. cin >> h[i];
  10. }
  11.  
  12. int operations = 0;
  13. int target = h[n - 1]; // Start from the last pillar
  14.  
  15. for (int i = n - 2; i >= 0; i--) {
  16. if (h[i] > target) {
  17. operations += (h[i] - target);
  18. } else {
  19. target = h[i];
  20. }
  21. }
  22.  
  23. cout << operations << endl;
  24. }
  25.  
  26. int main() {
  27. ios::sync_with_stdio(false);
  28. cin.tie(nullptr);
  29.  
  30. int t;
  31. cin >> t;
  32. while (t--) {
  33. solve();
  34. }
  35.  
  36. return 0;
  37. }
Success #stdin #stdout 0.01s 5284KB
stdin
3
5
5 4 3 2 1
3
2 2 1
1
1
stdout
10
2
0