#include using namespace std; #define rep(i,n) for(int i = 0; i < (n);i++) #define sz(x) int(x.size()) typedef long long ll; typedef pair P; int main(){ int n; cin >> n; vector b(n); rep(i,n) cin >> b[i]; ll res = 0; ll c = 0, p = 0; for (int i = 0; i < n; i++) { if (b[i] == 0) { if (p) p--, b[i]++; else c++; } else if (b[i] > 1) { while (c && (b[i] > 1)) c--, b[i]--; if (b[i] > 1) p += b[i]-1; b[i] = 1; } res += c; res += p; } cout << res << endl; return 0; }