fork download
  1. // LonggVu.
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. // Noob C++
  5. #define LonggVu() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  6. #define use(x) freopen(x".inp", "r", stdin); freopen(x".out", "w", stdout);
  7. #define fix(x) fixed << setprecision(x)
  8. #define all(x) x.begin(), x.end()
  9. #define mod 1000000007
  10. #define el '\n'
  11. #define fi first
  12. #define se second
  13. #define pb push_back
  14. #define is insert
  15. using ll = long long int;
  16. using ld = long double;
  17. using str = string;
  18.  
  19. int n;
  20. ll a[10005], res;
  21. void run(int pos){
  22. if(pos > n) return;
  23. int vt = pos;
  24. for(int i=pos+1; i<=n; i++){
  25. if(a[i] >= a[vt]){
  26. vt = i;
  27. }
  28. }
  29. for(int i=pos; i<vt; i++){
  30. res += a[vt] - a[i];
  31. }run(vt + 1);
  32. }
  33.  
  34. int main(){
  35. LonggVu();
  36.  
  37. int t; cin >> t;
  38. while(t--){
  39. cin >> n;
  40. for(int i=1; i<=n; i++){
  41. cin >> a[i];
  42. }res = 0;
  43. run(1);
  44. cout << res << el;
  45. }
  46. }
Success #stdin #stdout 0.01s 5308KB
stdin
3
10
1 2 4 9 15 5 8 15 7 9
3
1 2 100
4
1 3 1 2
stdout
63
197
3