Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6.  
  7. const ll maxn = 1e5 + 10;
  8. const ll inf = 2e18;
  9.  
  10. ll n, a[maxn], mx = -1, ind, ans = 0, was[maxn], k[maxn];
  11.  
  12. int main() {
  13.     ios_base::sync_with_stdio(0);
  14.     cin >> n;
  15.     for (int i = 0; i < n; ++i) {
  16.         cin >> a[i];
  17.     }
  18.     for (int i = 0; i < n; ++i) {
  19.         if (mx < a[i]) {
  20.             ll need = a[i] - mx;
  21.             mx = a[i];
  22.             for (ll j = i; j >= 0; --j) {
  23.                 if (was[j] == 0) {
  24.                     was[j] = 1;
  25.                     need--;
  26.                 }
  27.                 if (need == 0) {
  28.                     break;
  29.                 }
  30.             }
  31.         }
  32.     }
  33.     ll now = 0;
  34.     for (ll i = 0; i < n; ++i) {
  35.         if (was[i]) now++;
  36.         ans += now - a[i] - 1;
  37.     }
  38.     cout << ans;
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement