#include #define rep(i,n) for(int i=0;i> n; int64_t empty = 0; int64_t wait = 0; int64_t ans = 0; for(int i = 0; i < n; ++i) { int a; cin >> a; if(a == 0) { if(wait > 0) { wait--; } else { empty++; } } else if(a != 1) { a--; wait += a; if(wait >= empty) { wait -= empty; empty = 0; } else { empty -= wait; wait = 0; } } ans += (wait + empty); } cout << ans << endl; return 0; }