fork(2) download
  1. #include<stdio.h>
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. #define ll long long
  5. #define f(i,x,n) for (int i = x;i < n;++i)
  6.  
  7. map<int, int> mp;
  8.  
  9. int main() {
  10. int n, mx = 0;
  11. scanf("%d", &n);
  12. f(i, 0, n) {
  13. int t;
  14. scanf("%d", &t);
  15. if (t >= mx)mx = t;
  16. else --mp[mx], ++mp[t];
  17. }
  18. int an = 0, k = 0, b = 0;
  19. for (map<int, int> ::iterator it = mp.begin(); it != mp.end(); ++it) {
  20. if (k)an += it->first - b;
  21. k += it->second;
  22. b = it->first;
  23. }
  24. printf("%d", an);
  25. }
Success #stdin #stdout 0s 3472KB
stdin
5
1 9 9 2 2
stdout
7