#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, one = 0; int d = 0; for (int i = 0; i < n; i++) { if (b[i] == 0) { if (p > 0) p--, res += i - d; else c++; }else if (b[i] == 1) { one++; } else { if (c > 0) { res += c*(c+1)/2 + one; b[i] -= c; c = 0; one = 0; } p = b[i] - 1; b[i] = 1; if (p > 0) d = i; } } cout << res << endl; return 0; }