Problem:
問題 B : Evacuation Route
問題文
日本では防災研究が盛んに行われており，近年その重要性がますます増している．
避難経路の評価も重要な研究のひとつである．
今回は直線状の通路の安全評価を行う．
通路は
W
個のユニットに分けられており，一方の端のユニットからもう一方の端のユニットまで
0,  1,  2,  … ,  W-1
の番号がつけられている．
通路内の各ユニットには，入口の扉，出口の扉，防火扉のいずれか1つが存在する．
入り口の扉，出口の扉，防火扉はそれぞれ通路内に複数個存在しうる．
この問題では時刻
t=0
で火災が発生したと想定する．
それにより，通路の外部にいて避難しようとしている人々が入口の扉を通じて通路へ入り，より安全な場所へ出るために出口の扉へ脱出しようとするものとする．
避難中のそれぞれの人は単位時刻ごとに 1 つのユニットを移動するか，今のユニットに留まることができる．
すなわち，時刻
t
にある人がユニット
i
にいたとするとき，その人は時刻
t+1
ではユニット
i-1,  i,   i+1
の3つの数字のうち
0
以上
W-1
以下であるものを選択し，その番号のユニットへ移動することができる．
防火扉があるユニットは，ある一定時刻以降になると完全に遮断されてしまい，避難中の人々はそのユニットに立ち入りできなくなる．また，そのユニット内に居た人々もそこから他のユニットに移動できなくなってしまう．
この問題における目的は，それぞれの扉の情報が与えられるので，避難中の人々が最適に行動した時に最大で何人が出口の扉へたどり着けるか計算することである．
通路の情報が
W
個の整数
a_i
で与えられる．
a_i = 0
のとき，
i
番目のユニットが出口の扉であることをあらわす．
a_i < 0
のとき，
i
番目のユニットが防火扉により時間
|a_i|
以降出入りできなくなることを表す．
a_i > 0
のとき，時刻
t=0, 1, 2, … , a_{i}-1
のそれぞれにおいて，ちょうど一人の人が
i
番目のユニットに現れる．時刻
t
に現れた人は，時刻
t+1
以降から移動を開始する．
なお，1つのユニットに複数の人々が存在してもかまわない．
出口の扉へたどり着ける最大の人数を求めよ．
入力形式
入力は以下の形式で与えられる
W
a_0
a_1
...
a_{W-1}
出力形式
最大人数を1行で出力せよ．
制約
1 ≤ W ≤ 10^5
|a_i|   ≤ 10^4
入力値はすべて整数である．
入出力例
入力例 1
7
2 0 -2 3 2 -2 0
出力例 1
4
0,   3,   5
番目のユニットに入り口の扉があり，
1,   6
番目のユニットに出口の扉がある．
0
番目のユニットからは，
1
番目のユニットへ２人出ることができる．
3
番目のユニットからは，
1
番目のユニットへ１人出ることができる．
5
番目のユニットからは，
6
番目のユニットへ１人出ることができる．
よって合わせて４人が出口へとたどり着ける．
入力例 2
4
1 1 1 1
出力例 2
0
出口がないので誰も脱出できない．
入力例 3
9
10 -10 10 -10 10 -10 10 -10 0
出力例 3
24

Submitted solutions:

code_snippet: #include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
#define rep(i, m, n) for (ll i = (ll)m; i < (ll)n; i++)
#define drep(i, m, n) for (ll i = m - 1; i >= n; i--)
#define Endl endl
#define all(a) a.begin(), a.end()
#define pr(i, j) make_pair(i, j)
#define isin(x, l, r) (l <= x && x < r)
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
#define srt(ar) sort(ar.begin(), ar.end())
#define rev(ar) reverse(ar.begin(), ar.end())
#define jge(f, s, t) cout << (f ? s : t) << endl
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#define printar(ar)             \
    do                          \
    {                           \
        for (auto dbg : ar)     \
        {                       \
            cout << dbg << " "; \
        }                       \
        cout << endl;           \
    } while (0)
const ll inf = 1e18;
const ld pi = 3.14159265358979;
const ld eps = 1e-9;
template <class T, ll n, ll idx = 0>
auto make_vec(const ll (&d)[n], const T &init) noexcept
{
    if constexpr (idx < n)
        return std::vector(d[idx], make_vec<T, n, idx + 1>(d, init));
    else
        return init;
}

template <class T, ll n>
auto make_vec(const ll (&d)[n]) noexcept
{
    return make_vec(d, T{});
}
//////////////// 以下を貼る ////////////////
template <class T>
size_t HashCombine(const size_t seed, const T &v)
{
    return seed ^ (std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
}
/* pair用 */
template <class T, class S>
struct std::hash<std::pair<T, S>>
{
    size_t operator()(const std::pair<T, S> &keyval) const noexcept
    {
        return HashCombine(std::hash<T>()(keyval.first), keyval.second);
    }
};
template <typename T>
struct SparseTable
{
    vector<vector<T>> st;
    vector<int> lookup;

    void init(vector<T> v)
    {
        int b = 0;
        while ((1 << b) <= v.size())
            ++b;
        st.assign(b, vector<T>(1 << b));
        for (int i = 0; i < v.size(); i++)
        {
            st[0][i] = v[i];
        }
        for (int i = 1; i < b; i++)
        {
            for (int j = 0; j + (1 << i) <= (1 << b); j++)
            {
                st[i][j] = min(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
            }
        }
        lookup.resize(v.size() + 1);
        for (int i = 2; i < lookup.size(); i++)
        {
            lookup[i] = lookup[i >> 1] + 1;
        }
    }

    inline T rmq(int l, int r)
    {
        int b = lookup[r - l];
        return min(st[b][l], st[b][r - (1 << b)]);
    }
};

////////////////////////////////////////////
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll w;
    cin >> w;
    vector<ll> a(w);
    vector<ll> in(w);
    vector<ll> goal;
    vector<ll> limit(w, inf);
    vector<ll> limit2(w, inf);
    rep(i, 0, w)
    {
        cin >> a[i];
        if (a[i] > 0)
        {
            in[i] = a[i];
        }
        else if (a[i] == 0)
        {
            goal.push_back(i);
        }
        else
        {
            limit[i] = -i - a[i];
            limit2[i] = i - a[i];
        }
    }
    SparseTable<ll> RMQ, RMQ2;
    RMQ.init(limit);
    RMQ2.init(limit2);
    ll ans = 0;
    rep(i, 0, w)
    {
        ll s1 = 0;
        ll s2 = 0;
        auto itr = lower_bound(all(goal), i);
        if (itr != goal.end())
        {
            ll G = *itr;
            ll m = RMQ.rmq(i, G);
            ll t = max(m + i, 0LL);
            s1 = min(t, in[i]);
        }
        if (itr != goal.begin())
        {
            ll G = *prev(itr);
            ll m = RMQ2.rmq(G, i);
            ll t = max(m - i, 0LL);
            s2 = min(t, in[i]);
        }
        ans += max(s1, s2);
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 44344
final_rank: 18

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

#define int long long

signed main() {
	int n;
	cin >> n;
	vector<int> A(n);
	for (int i = 0; i < n; i++) {
		cin >> A[i];
	}
	vector<int> ans(n, -1);
	int cnt = -1;
	for (int i = 0; i < n; i++) {
		if (A[i] == 0) {
			cnt = 1e9;
		}
		else if (A[i] > 0) {
			ans[i] = max(ans[i], cnt);
		}
		else {
			cnt = min(cnt, -A[i] - 1);
		}
		cnt--;
	}
	cnt = -1;
	for (int i = n - 1; i >= 0; i--) {
		if (A[i] == 0) {
			cnt = 1e9;
		}
		else if (A[i] > 0) {
			ans[i] = max(ans[i], cnt);
		}
		else {
			cnt = min(cnt, -A[i] - 1);
		}
		cnt--;
	}
	int sum = 0;
	for (int i = 0; i < n; i++) {
		if (A[i] > 0) {
			sum += min(A[i], ans[i] + 1);
		}
	}
	cout << sum << '\n';
}
accuracy: 1
time_ms: 20
memory_kb: 4440
final_rank: 14

code_snippet: #include <iostream>
#include <vector>

using namespace std;

#define int long long

signed main() {
	int n;
	cin >> n;
	vector<int> A(n);
	for (int i = 0; i < n; i++) {
		cin >> A[i];
	}
	vector<int> ans(n, -1);
	int cnt = -1;
	for (int i = 0; i < n; i++) {
		if (A[i] == 0) {
			cnt = 1e9;
		}
		else if (A[i] > 0) {
			ans[i] = max(ans[i], cnt);
		}
		else {
			cnt = min(cnt, -A[i] - 1);
		}
		cnt--;
	}
	cnt = -1;
	for (int i = n - 1; i >= 0; i--) {
		if (A[i] == 0) {
			cnt = 1e9;
		}
		else if (A[i] > 0) {
			ans[i] = max(ans[i], cnt);
		}
		else {
			cnt = min(cnt, -A[i] - 1);
		}
		cnt--;
	}
	int sum = 0;
	for (int i = 0; i < n; i++) {
		if (A[i] > 0) {
			sum += min(A[i], ans[i] + 1);
		}
	}
	cout << sum << '\n';
}
accuracy: 1
time_ms: 10
memory_kb: 4692
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define all(x) x.begin(),x.end()
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }
template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }

int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i, n) cin >> a[i];
    auto get = [](vector<int> a) {
        int n = a.size();
        const int INF = 1e9;
        int cur = 0;
        vector<int> ans(n, INF);
        rep(i, n) {
            if (a[i] == 0) {
                cur = INF;
            } else if (a[i] < 0) {
                chmin(cur, -a[i]);
            } else {
                ans[i] = cur;
            }
            cur--;
        }
        return ans;
    };
    auto lhs = get(a);
    reverse(all(a));
    auto rhs = get(a);
    reverse(all(a));
    reverse(all(rhs));
    int ans = 0;
    rep(i, n) {
        if (a[i] > 0) {
            ans += min(max({lhs[i], rhs[i], 0}), a[i]);
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 4556
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;


int main() {

    int W;
    cin>>W;
    vector<int> A(W);
    vector<int> L(W,-1),R(W,-1);
    for(int &w:A)cin>>w;
    int tm=-1;
    for(int i=0;i<W;i++){
        if(A[i]==0)tm=1e9;
        else if(A[i]<0){
            tm=min(tm,-A[i]);
        }
        L[i]=tm;
        tm--;
    }
    tm=-1;
    for(int i=W-1;i>=0;i--){
        if(A[i]==0)tm=1e9;
        else if(A[i]<0){
            tm=min(tm,-A[i]);
        }
        R[i]=max(tm,0);
        tm--;
    }
    int an=0;
    for(int i=0;i<W;i++){
        if(A[i]>0){
            an+=min(A[i],max(L[i],R[i]));
        }
    }
    cout<<an<<endl;
}
accuracy: 1
time_ms: 10
memory_kb: 4196
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;

int solve(int N, const vector<int>& A) {
	const int INF = 1012345678;
	vector<int> v(N, 0);
	int cur = 0;
	for (int i = 0; i < N; i++) {
		if (A[i] < 0) {
			cur = min(cur, -A[i]);
		}
		if (A[i] == 0) {
			cur = INF;
		}
		v[i] = max(v[i], cur);
		cur = max(cur - 1, 0);
	}
	for (int i = N - 1; i >= 0; i--) {
		if (A[i] < 0) {
			cur = min(cur, -A[i]);
		}
		if (A[i] == 0) {
			cur = INF;
		}
		v[i] = max(v[i], cur);
		cur = max(cur - 1, 0);
	}
	int ans = 0;
	for (int i = 0; i < N; i++) {
		if (A[i] > 0) {
			ans += min(A[i], v[i]);
		}
	}
	return ans;
}

int main() {
	int N;
	cin >> N;
	vector<int> A(N);
	for (int i = 0; i < N; i++) {
		cin >> A[i];
	}
	int ans = solve(N, A);
	cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3852
final_rank: 1

code_snippet: #include<bits/stdc++.h>
using namespace std;
const int INF = (1<<30);

int W,a[1<<17],S[1<<17],S_rev[1<<17];

int main()
{
    cin>>W;
    for(int i=0; i<W; i++)cin>>a[i];
    int now=0;
    for(int i=0; i<W; i++)
    {
        if(a[i]<0)now=min(now,-a[i]);
        else if(a[i]==0)now=INF;
        else S[i]=min(a[i],now);
        now=max(now-1,0);
    }
    reverse(a,a+W);
    now=0;
    for(int i=0; i<W; i++)
    {
        if(a[i]<0)now=min(now,-a[i]);
        else if(a[i]==0)now=INF;
        else S_rev[i]=min(a[i],now);
        now=max(now-1,0);
    }
    reverse(S_rev,S_rev+W);
    reverse(a,a+W);
    int ans=0;
    for(int i=0; i<W; i++)
    {
        if(a[i]>0)ans+=max(S[i],S_rev[i]);
        // cout << i << ' ' << S[i] << ' ' << S_rev[i] << endl;
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 4596
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
	int N;
	cin >> N;
	constexpr long long t_inf = 1LL<<25;
	int s = N, t = -1;
	vector<long long> a(N), max_evac(N);
	for( int i = 0; i < N; i++ ) {
		cin >> a[i];
		if( a[i] == 0 ) s = min(s, i), t = max(t, i);
	}
	long long rem = t_inf;
	for( int i = s; i < N; i++, rem-- ) {
		if( a[i] > 0 ) max_evac[i] = max(max_evac[i], min(a[i], rem));
		if( a[i] < 0 ) rem = min(rem, -a[i]);
		if( a[i] == 0 ) rem = t_inf;
	}
	rem = t_inf;
	for( int i = t; i >= 0; i--, rem-- ) {
		if( a[i] > 0 ) max_evac[i] = max(max_evac[i], min(a[i], rem));
		if( a[i] < 0 ) rem = min(rem, -a[i]);
		if( a[i] == 0 ) rem = t_inf;
	}
	cout << accumulate(max_evac.begin(), max_evac.end(), 0LL) << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 4676
final_rank: 8

code_snippet: // 予想: 20mins
// 開始: 14:35

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
    const ll INF = 1e18;
    int N; cin >> N;
    vector<ll> A(N);
    for(int i = 0; i < N; i++) cin >> A[i];

    vector<ll> L(N), R(N);

    L[0] = (A[0] == 0 ? -INF : 0); 
    for(int i = 1; i < N; i++){
        if(A[i] == 0) L[i] = -INF;
        else if(A[i] < 0){
            L[i] = max(A[i], L[i - 1] + 1);
        }
        else L[i] = L[i - 1] + 1;
    }

    R[N - 1] = (A[N - 1] == 0 ? -INF : 0);
    for(int i = N - 2; i >= 0; i--){
        if(A[i] == 0) R[i] = -INF;
        else if(A[i] < 0){
            R[i] = max(A[i], R[i + 1] + 1);
        }
        else R[i] = R[i + 1] + 1;
    }

    ll ans = 0;
    for(int i = 0; i < N; i++){
        //cout << L[i] << " " << R[i] << endl;
        if(A[i] > 0) ans += min(A[i], max(max(-L[i], -R[i]), 0ll));
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 5488
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
const ll N=1<<17;
ll w,a[N],l[N],r[N];
int main(void){
    cin>>w;
    for(ll i=0;i<w;i++){
        cin>>a[i];
    }
    l[0]=(a[0]==0 ? 1e9 : 0);
    for(ll i=1;i<w;i++){
        l[i]=(a[i]==0 ? 1e9 : (a[i]<0 ? min(-a[i],max(l[i-1]-1,0LL)) : max(l[i-1]-1,0LL)));
    }
    r[w-1]=(a[w-1]==0 ? 1e9 : 0);
    for(ll i=w-1;i--;){
        r[i]=(a[i]==0 ? 1e9 : (a[i]<0 ? min(-a[i],max(r[i+1]-1,0LL)) : max(r[i+1]-1,0LL)));
    }
    ll ans=0;
    for(ll i=0;i<w;i++){
        if(a[i]>0){
            ans+=min(a[i],max(l[i],r[i]));
        }
    }
    cout<<ans<<endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 5772
final_rank: 12

code_snippet: #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb(...) emplace_back(__VA_ARGS__)
#define mp(a, b) make_pair(a, b)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define REP(i, l, r) for (int i = l; i < (r); i++)
#define REP2(i, l, r) for (int i = (int)r - 1; i >= (l); i--)
#define siz(x) (ll) x.size()
template <class T>
using rque = priority_queue<T, vector<T>, greater<T>>;

template <class T>
bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}

template <class T>
bool chmax(T &a, const T &b) {
    if (b > a) {
        a = b;
        return 1;
    }
    return 0;
}

template <class T>
void print(vector<T> a) {
    if (a.empty())
        cout << '\n';
    else {
        for (int i = 0; i < a.size(); i++)
            cout << a[i] << (i + 1 == a.size() ? '\n' : ' ');
    }
}

// __int128_t gcd(__int128_t a, __int128_t b) {
//     if (a == 0)
//         return b;
//     if (b == 0)
//         return a;
//     __int128_t cnt = a % b;
//     while (cnt != 0) {
//         a = b;
//         b = cnt;
//         cnt = a % b;
//     }
//     return b;
// }

long long extGCD(long long a, long long b, long long &x, long long &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    long long d = extGCD(b, a % b, y, x);
    y -= a / b * x;
    return d;
}

struct UnionFind {
    vector<int> data;
    int num;

    UnionFind(int sz) {
        data.assign(sz, -1);
        num = sz;
    }

    bool unite(int x, int y) {
        x = find(x), y = find(y);
        if (x == y)
            return (false);
        if (data[x] > data[y])
            swap(x, y);
        data[x] += data[y];
        data[y] = x;
        num--;
        return (true);
    }

    int find(int k) {
        if (data[k] < 0)
            return (k);
        return (data[k] = find(data[k]));
    }

    int size(int k) {
        return (-data[find(k)]);
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }

    int operator[](int k) {
        return find(k);
    }
};

template <int mod>
struct Mod_Int {
    int x;

    Mod_Int() : x(0) {
    }

    Mod_Int(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {
    }

    static int get_mod() {
        return mod;
    }

    Mod_Int &operator+=(const Mod_Int &p) {
        if ((x += p.x) >= mod)
            x -= mod;
        return *this;
    }

    Mod_Int &operator-=(const Mod_Int &p) {
        if ((x += mod - p.x) >= mod)
            x -= mod;
        return *this;
    }

    Mod_Int &operator*=(const Mod_Int &p) {
        x = (int)(1LL * x * p.x % mod);
        return *this;
    }

    Mod_Int &operator/=(const Mod_Int &p) {
        *this *= p.inverse();
        return *this;
    }

    Mod_Int &operator++() {
        return *this += Mod_Int(1);
    }

    Mod_Int operator++(int) {
        Mod_Int tmp = *this;
        ++*this;
        return tmp;
    }

    Mod_Int &operator--() {
        return *this -= Mod_Int(1);
    }

    Mod_Int operator--(int) {
        Mod_Int tmp = *this;
        --*this;
        return tmp;
    }

    Mod_Int operator-() const {
        return Mod_Int(-x);
    }

    Mod_Int operator+(const Mod_Int &p) const {
        return Mod_Int(*this) += p;
    }

    Mod_Int operator-(const Mod_Int &p) const {
        return Mod_Int(*this) -= p;
    }

    Mod_Int operator*(const Mod_Int &p) const {
        return Mod_Int(*this) *= p;
    }

    Mod_Int operator/(const Mod_Int &p) const {
        return Mod_Int(*this) /= p;
    }

    bool operator==(const Mod_Int &p) const {
        return x == p.x;
    }

    bool operator!=(const Mod_Int &p) const {
        return x != p.x;
    }

    Mod_Int inverse() const {
        assert(*this != Mod_Int(0));
        return pow(mod - 2);
    }

    Mod_Int pow(long long k) const {
        Mod_Int now = *this, ret = 1;
        for (; k > 0; k >>= 1, now *= now) {
            if (k & 1)
                ret *= now;
        }
        return ret;
    }

    friend ostream &operator<<(ostream &os, const Mod_Int &p) {
        return os << p.x;
    }

    friend istream &operator>>(istream &is, Mod_Int &p) {
        long long a;
        is >> a;
        p = Mod_Int<mod>(a);
        return is;
    }
};

ll mpow2(ll x, ll n, ll mod) {
    ll ans = 1;
    x %= mod;
    while (n != 0) {
        if (n & 1)
            ans = ans * x % mod;
        x = x * x % mod;
        n = n >> 1;
    }
    ans %= mod;
    return ans;
}

ll modinv2(ll a, ll mod) {
    ll b = mod, u = 1, v = 0;
    while (b) {
        ll t = a / b;
        a -= t * b;
        swap(a, b);
        u -= t * v;
        swap(u, v);
    }
    u %= mod;
    if (u < 0)
        u += mod;
    return u;
}

ll divide_int(ll a, ll b) {
    return (a >= 0 ? a / b : (a - b + 1) / b);
}

constexpr int mod = 1000000007;
// constexpr int mod = 998244353;
// constexpr int mod = 31607;
using mint = Mod_Int<mod>;

mint mpow(mint x, ll n) {
    bool rev = n < 0;
    n = abs(n);
    mint ans = 1;
    while (n != 0) {
        if (n & 1)
            ans *= x;
        x *= x;
        n = n >> 1;
    }
    return (rev ? ans.inverse() : ans);
}

// ----- library -------
// ----- library -------

int main() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int w;
    cin >> w;
    vector<int> a(w);
    rep(i, w) cin >> a[i];
    vector<int> d(w, -1);
    priority_queue<pair<int, int>> que;
    rep(i, w) if (!a[i]) que.push({1e9, i});
    while (!que.empty()) {
        auto [nd, idx] = que.top();
        que.pop();
        if (chmax(d[idx], nd)) {
            int ned = nd;
            if (a[idx] < 0)
                chmin(ned, -a[idx]);
            if (idx > 0)
                que.push({ned - 1, idx - 1});
            if (idx + 1 < w)
                que.push({ned - 1, idx + 1});
        }
    }
    int ans = 0;
    rep(i, w) if (a[i] > 0) ans += max(0, min(a[i], d[i]));
    cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 5828
final_rank: 15

code_snippet: #line 1 "a.cpp"
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif  // LOCAL

#include <bits/stdc++.h>

#line 1 "SPJ-Library/atcoder/segtree.hpp"



#line 7 "SPJ-Library/atcoder/segtree.hpp"

#line 1 "SPJ-Library/atcoder/internal_bit.hpp"



#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

// @param n `0 <= n`
// @return minimum non-negative `x` s.t. `n <= 2**x`
int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}

// @param n `1 <= n`
// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
constexpr int bsf_constexpr(unsigned int n) {
    int x = 0;
    while (!(n & (1 << x))) x++;
    return x;
}

// @param n `1 <= n`
// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
int bsf(unsigned int n) {
#ifdef _MSC_VER
    unsigned long index;
    _BitScanForward(&index, n);
    return index;
#else
    return __builtin_ctz(n);
#endif
}

}  // namespace internal

}  // namespace atcoder


#line 9 "SPJ-Library/atcoder/segtree.hpp"

namespace atcoder {

template <class S, S (*op)(S, S), S (*e)()> struct segtree {
  public:
    segtree() : segtree(0) {}
    explicit segtree(int n) : segtree(std::vector<S>(n, e())) {}
    explicit segtree(const std::vector<S>& v) : _n(int(v.size())) {
        log = internal::ceil_pow2(_n);
        size = 1 << log;
        d = std::vector<S>(2 * size, e());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    void set(int p, S x) {
        assert(0 <= p && p < _n);
        p += size;
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S get(int p) const {
        assert(0 <= p && p < _n);
        return d[p + size];
    }

    S prod(int l, int r) const {
        assert(0 <= l && l <= r && r <= _n);
        S sml = e(), smr = e();
        l += size;
        r += size;

        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return op(sml, smr);
    }

    S all_prod() const { return d[1]; }

    template <bool (*f)(S)> int max_right(int l) const {
        return max_right(l, [](S x) { return f(x); });
    }
    template <class F> int max_right(int l, F f) const {
        assert(0 <= l && l <= _n);
        assert(f(e()));
        if (l == _n) return _n;
        l += size;
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!f(op(sm, d[l]))) {
                while (l < size) {
                    l = (2 * l);
                    if (f(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    template <bool (*f)(S)> int min_left(int r) const {
        return min_left(r, [](S x) { return f(x); });
    }
    template <class F> int min_left(int r, F f) const {
        assert(0 <= r && r <= _n);
        assert(f(e()));
        if (r == 0) return 0;
        r += size;
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(op(d[r], sm))) {
                while (r < size) {
                    r = (2 * r + 1);
                    if (f(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

  private:
    int _n, size, log;
    std::vector<S> d;

    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};

}  // namespace atcoder


#line 8 "a.cpp"

#pragma region debug

#pragma region output
template <typename T1, typename T2>
std::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {
    os << p.first << " " << p.second;
    return os;
}
template <class T>
std::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {
    for (int i = 0; i < (int)v.size(); i++) {
        if (i) os << " ";
        os << v[i];
    }
    return os;
}
#pragma endregion

#pragma region view

namespace viewer {
template <typename T>
void view(const T e) {
    std::cerr << e;
}

template <typename T, typename U>
void view(const std::pair<T, U> &p) {
    std::cerr << "(";
    view(p.first);
    std::cerr << ", ";
    view(p.second);
    std::cerr << ")";
}

template <typename T>
void view(const std::set<T> &s) {
    if (s.empty()) {
        std::cerr << "{ }";
        return;
    }
    std::cerr << "{ ";
    for (auto &t : s) {
        view(t);
        std::cerr << ", ";
    }
    std::cerr << "\b\b }";
}

template <typename T>
void view(const std::unordered_set<T> &s) {
    if (s.empty()) {
        std::cerr << "{ }";
        return;
    }
    std::cerr << "{ ";
    for (auto &t : s) {
        view(t);
        std::cerr << ", ";
    }
    std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<T> &v) {
    if (v.empty()) {
        std::cerr << "{ }";
        return;
    }
    std::cerr << "{ ";
    for (const auto &e : v) {
        view(e);
        std::cerr << ", ";
    }
    std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<std::vector<T>> &vv) {
    std::cerr << "{\n";
    for (const auto &v : vv) {
        std::cerr << "\t";
        view(v);
        std::cerr << '\n';
    }
    std::cerr << "}";
}

template <typename T, typename U>
void view(const std::vector<std::pair<T, U>> &v) {
    std::cerr << "{\n";
    for (const auto &c : v) {
        std::cerr << "\t(";
        view(c.first);
        std::cerr << ", ";
        view(c.second);
        std::cerr << ")\n";
    }
    std::cerr << "}";
}

template <typename T, typename U>
void view(const std::map<T, U> &m) {
    std::cerr << "{\n";
    for (const auto &t : m) {
        std::cerr << "\t[";
        view(t.first);
        std::cerr << "] : ";
        view(t.second);
        std::cerr << '\n';
    }
    std::cerr << "}";
}

template <typename T, typename U>
void view(const std::unordered_map<T, U> &m) {
    std::cerr << "{\n";
    for (const auto &t : m) {
        std::cerr << "\t[";
        view(t.first);
        std::cerr << "] : ";
        view(t.second);
        std::cerr << '\n';
    }
    std::cerr << "}";
}
}  // namespace viewer

#pragma endregion

// when debugging : g++ foo.cpp -DLOCAL
#ifdef LOCAL
void debug_out() {}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    viewer::view(H);
    std::cerr << ", ";
    debug_out(T...);
}
#define debug(...)                                                \
    do {                                                          \
        std::cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
        debug_out(__VA_ARGS__);                                   \
        std::cerr << "\b\b]\n";                                   \
    } while (0)
#define dump(x)                                      \
    do {                                             \
        std::cerr << __LINE__ << " " << #x << " : "; \
        viewer::view(x);                             \
        std::cerr << '\n';                           \
    } while (0)

#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

#pragma endregion

using namespace std;
using namespace atcoder;

using ll = long long;

const int INF = 1001001001;

int op(int a, int b) { return min(a, b); }

int e() { return INF; }

int main() {
    int W;
    cin >> W;
    vector<int> As(W);
    for (int i = 0; i < W; i++) {
        cin >> As[i];
    }

    W += 4;
    As.insert(As.begin(), {0, -1});
    As.insert(As.end(), {-1, 0});

    vector<int> left(W, INF), right(W, INF);
    for (int i = 0; i < W; i++) {
        if (As[i] < 0) {
            left[i] = abs(As[i]) + i;
            right[i] = abs(As[i]) - i;
        }
    }

    segtree<int, op, e> seg_left(left), seg_right(right);

    set<int> s;
    for (int i = 0; i < W; i++) {
        if (As[i] == 0) {
            s.insert(i);
        }
    }

    debug(s);

    int answer = 0;
    for (int i = 2; i < W - 2; i++) {
        auto r = *(s.lower_bound(i));
        auto l = *(--(s.lower_bound(i)));
        debug(l, i, r);
        if (As[i] > 0) {
            int tmp = As[i];
            // debug(seg_left.prod(l, i + 1), seg_right.prod(i, r + 1));
            int left_close = seg_left.prod(l, i + 1) - i;
            int right_close = seg_right.prod(i, r + 1) + i;
            debug(left_close, right_close);
            int close = max(left_close, right_close);
            tmp = min(tmp, close);
            tmp = max(tmp, 0);
            debug(i, tmp);
            answer += tmp;
        }
    }

    cout << answer << endl;

    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 10356
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define INF 1020304050

class LazySegTree {
  vector<long long> dat,lazy;
  vector<bool> change;

public:
  int n2 = 1;
  LazySegTree(int m) {
    while (n2 < m) n2 *= 2;
    for (int i=0;i<n2*2-1;i++) {
      dat.emplace_back(0);
      lazy.emplace_back(0);
      change.emplace_back(false);
    }
  }

  void eval(int k, int l, int r) {
    if (change[k]) {
      dat[k] += lazy[k]/(r-l);
      if (r-l > 1) {
        lazy[k*2+1] += lazy[k]/2;
        change[k*2+1] = true;
        lazy[k*2+2] += lazy[k]/2;
        change[k*2+2] = true;
      }
      change[k] = false;
      lazy[k] = 0;
    }
  }

  void update(int a, int b, long long v, int k, int l, int r) {
    eval(k,l,r);
    if (b <= l || r <= a) return;
    if (a <= l && r <= b) {
      change[k] = true;
      lazy[k] += (r-l)*v;
      eval(k,l,r);
    }
    else {
      update(a,b,v,k*2+1,l,(l+r)/2);
      update(a,b,v,k*2+2,(l+r)/2,r);
      dat[k] = min(dat[k*2+1],dat[k*2+2]);
    }
  }

  // update values of [a,b)
  void put(int a, int b, int v) {
    update(a,b,v,0,0,n2);
  }

  long long query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l) return INF;
    eval(k,l,r);
    if (a <= l && r <= b) return dat[k];
    long long vl = query(a,b,k*2+1,l,(l+r)/2);
    long long vr = query(a,b,k*2+2,(l+r)/2,r);
    return min(vl,vr);
  }

  // min of [a,b)
  long long get(int a, int b) {
    return query(a,b,0,0,n2);
  }

};

int main() {
  
  ios::sync_with_stdio(false);
  cin.tie(0);

	int w;
	cin >> w;
	vector<int> a(w);
	for (int i=0;i<w;i++) {
		cin >> a[i];
	}
	LazySegTree lseg(w);
	set<int> pos0;
	for (int i=0;i<w;i++) {
		if (a[i] == 0) pos0.insert(i);
	}
	pos0.insert(-INF);
	pos0.insert(INF);
	for (int i=0;i<w;i++) {
		if (a[i] >= 0) {
			lseg.put(i,i+1,INF-i);
		}
		else {
			lseg.put(i,i+1,abs(a[i])-i);
		}
	}
	int ans = 0;
	for (int i=0;i<w;i++) {
		if (a[i] <= 0) {
			lseg.put(0,i+1,-1);
			lseg.put(i+1,w,1);
			continue;
		}
		auto it = pos0.lower_bound(i);
		it--;
		int vl = *it;
		it++;
		int vr = *it;
		int lans = 0;
		if (vl > -INF) {
			int kans = a[i];
			int shut = lseg.get(vl+1,i);
			lans = min(kans,shut);
		}
		int rans = 0;
		if (vr < INF) {
			int kans = a[i];
			int shut = lseg.get(i+1,vr);
			rans = min(kans,shut);
		}
		ans += max({0,lans,rans});
		lseg.put(0,i+1,-1);
		lseg.put(i+1,w,1);
	}
	cout << ans << endl;
}
accuracy: 1
time_ms: 110
memory_kb: 11760
final_rank: 19

code_snippet: #include <stdio.h>
#include <queue>
#define INF 1012345678
using namespace std;

priority_queue<pair<int, int>> pq;
int a[103000], cap[103000], vis[103000];

int main(void) {
    int n, x, c, cc, r = 0;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
        if (a[i] == 0) {
            pq.push(pair<int, int>(INF, i));
            cap[i] = INF;
        }
    }
    while (pq.size()) {
        c = pq.top().first;
        x = pq.top().second;
        pq.pop();
        if (vis[x]) continue;
        else vis[x] = 1;

        if (x - 1 >= 0 && !vis[x - 1]) {
            cc = c - 1;
            if (a[x - 1] < 0 && -a[x - 1] < cc) cc = -a[x - 1];
            if (cap[x - 1] < cc) {
                pq.push(pair<int, int>(cc, x - 1));
                cap[x - 1] = cc;
            }
        }
        if (x + 1 < n && !vis[x + 1]) {
            cc = c - 1;
            if (a[x + 1] < 0 && -a[x + 1] < cc) cc = -a[x + 1];
            if (cap[x + 1] < cc) {
                pq.push(pair<int, int>(cc, x + 1));
                cap[x + 1] = cc;
            }
        }
    }

    for (int i = 0; i < n; i++) {
        if (a[i] > 0) {
            c = a[i];
            if (cap[i] < a[i]) c = cap[i];
            r += c;
        }
    }
    printf("%d\n", r);
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4644
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;

ll INF = 1ll << 60;

int main() {
    ll W;
    cin >> W;
    vector<ll> a(W);
    for (int i = 0; i < W; i++) cin >> a[i];
    vector<ll> dp1(W, INF);
    ll c = 0;
    for (int i = W - 1; i >= 0; i--) {
        if (a[i] == 0) c = INF;
        if (a[i] < 0) c = min(-a[i], c);
        dp1[i] = c--;
    }
    vector<ll> dp2(W, INF);
    c = 0;
    for (int i = 0; i < W; i++) {
        if (a[i] == 0) c = INF;
        if (a[i] < 0) c = min(-a[i], c);
        dp2[i] = c--;
    }
    ll ans = 0;
    for (int i = 0; i < W; i++) {
        ans += max(0ll, min(a[i], max(dp1[i], dp2[i])));
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 5476
final_rank: 10

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define cinf(n,x) for(int i=0;i<(n);i++)cin>>x[i];
#define ft first
#define sc second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(),(v).end()
#define LB(a,x) lb(all(a),x)-a.begin()
#define UB(a,x) ub(all(a),x)-a.begin()
#define mod 1000000007
//#define mod 998244353
#define FS fixed<<setprecision(15)
using namespace std;
typedef long long ll;
const double pi=3.141592653589793;
template<class T> using V=vector<T>;
using P=pair<ll,ll>;
typedef unsigned long long ull;
typedef long double ldouble;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline void out(T a){ cout << a << '\n'; }
void YN(bool ok){if(ok) cout << "Yes" << endl; else cout << "No" << endl;}
//void YN(bool ok){if(ok) cout << "YES" << endl; else cout << "NO" << endl;}


const ll INF=1e18;
const int mx=200005;

//Segtree template
class Segtree{
public:
    ll N=1;
    V<ll> dat;
    explicit Segtree(ll sz){
        while(N<sz) N*=2;
        dat.reserve(2*N+3);
        rep(i,2*N+3)dat[i]=0;
    }
 
    ll get(int i){
        return dat[i+N];
    }
    // Range Minimum Query -------------------------
 
    void min_init(){
        for(int i=0;i<N;i++)min_update(i,INF);
    }
 
    void min_update(ll k,ll a){
        k+=N;
        dat[k]=a;
        while(k>1){
            k=k/2;
            dat[k]=min(dat[2*k],dat[2*k+1]);
        }
    }
    ll get_min(int l,int r){
        ll res=INF;
        l=l+N;
        r=r+N;
        while(l<r){
            if(l&1) res=min(res,dat[l++]);
            if(r&1) res=min(res,dat[--r]);
            l >>= 1;
            r >>= 1;
        }
        return res;
    }

    //Range Maximum Query ---------------------------

    void max_init(){
        for(int i=0;i<N;i++)max_update(i,-INF);
    }

    void max_update(ll k,ll a){
        k+=N;
        dat[k]=a;
        while(k>1){
            k/=2;
            dat[k]=max(dat[2*k],dat[2*k+1]);
        }
    }

    ll get_max(ll l,ll r){
        ll res=-INF;
        l=l+N;
        r=r+N;
        while(l<r){
            if(l&1) res=max(res,dat[l++]);
            if(r&1) res=max(res,dat[--r]);
            l >>= 1;
            r >>= 1;
        }
        return res;
    }

    //Range Sum Query -----------------------------

    void sum_update(ll k,ll a){
        k+=N;
        dat[k]+=a;
        while(k>1){
            k/=2;
            dat[k]=(dat[2*k]+dat[2*k+1]);
        }
    }
    
    ll get_sum(ll l,ll r){
        ll res=0;
        l+=N;
        r+=N;
        while(l<r){
            if(l&1) res=(res+dat[l++]);
            if(r&1) res=(res+dat[--r]);
            l >>= 1;
            r >>= 1;
        }
        return res;
    }

    ll kth(ll k){
        ll cur=1;
        ll S=0;
        while(cur*2<=2*N-1){
            if(S+dat[2*cur]>=k){
                cur=2*cur;
            }else{
                S+=dat[2*cur];
                cur=2*cur+1;
            }
        }
        cur-=N;
        return cur;
    }

    //その他----------------------------------------
    /*
    ll unit(){
        return ;//単位元
    }
    ll calc(ll a,ll b){
        return ;//演算
    }
    void update(ll k,ll a){
        k+=N;
        dat[k]=a;
        while(k>1){
            k/=2;
            dat[k]=calc(dat[2*k],dat[2*k+1]);
        }
    }

    ll get_val(ll l,ll r){
        ll res=unit();
        l=l+N;
        r=r+N;
        while(l<r){
            if(l&1) res=calc(res,dat[l++]);
            if(r&1) res=calc(res,dat[--r]);
            l >>= 1;
            r >>= 1;
        }
        return res;
    }
    */
};



int main(){
    //オーバーフローは大丈夫ですか？？
    cin.tie(0);ios::sync_with_stdio(false);
    int n;
    cin>>n;
    V<ll> a(n);
    cinf(n,a);
    Segtree ma(n),mi(n);
    V<int> zero;
    ma.min_init();
    mi.min_init();
    rep(i,n){
        if(a[i]==0) zero.pb(i);
        if(a[i]<0){
            ma.min_update(i,-a[i]+i);
            mi.min_update(i,-a[i]-i);
        }
    }
    zero.pb(n);
    ll ans=0;
    rep(i,n){
        if(a[i]>0){

            int R=*lb(all(zero),i);
            int L=lb(all(zero),i)-zero.begin();
            L--;
            int neg=0;
            if(L<0){
                L=0;
                neg=1;
            }
            else L=zero[L];
            ll tmp1=mi.get_min(i,R)+i;
            ll tmp2=ma.get_min(L,i)-i;
            if(R==n) tmp1=-1;
            if(neg) tmp2=-1;
            ll tmp3=max(tmp1,tmp2);
            
            if(tmp3>0){
                ans+=min(a[i],tmp3);
            }
        }
    }
    out(ans);
}
accuracy: 1
time_ms: 20
memory_kb: 8536
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
    int W;
    cin >> W;
    vector<int>a(W);
    for(int i = 0; i < W; i++) {
        cin >> a[i];
    }
    int res = -1,inf = 1001001001;
    vector<int>b(W);
    for(int i = 0; i < W; i++) {
        if(a[i] == 0) {
            res = inf;
        }
        if(a[i] > 0) {
            b[i] = max(b[i],res);
        }
        if(a[i] < 0) {
            res = min(res,-a[i]);
        }
        res--;
    }
    res = -1,inf = 1001001001;
    for(int i = W-1; i >= 0; i--) {
        if(a[i] == 0) {
            res = inf;
        }
        if(a[i] > 0) {
            b[i] = max(b[i],res);
        }
        if(a[i] < 0) {
            res = min(res,-a[i]);
        }
        res--;
    }
    int ans = 0;
    for(int i = 0; i < W; i++) {
        if(a[i] > 0) {
            ans += min(a[i],max(0,b[i]));
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3864
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;

void solve(){
  int N;
  cin >> N;
  vector<int> vec(N);
  for(int i=0; i<N; i++){
    cin >> vec[i];
  }
  vector<int> ans(N);
  int ok = 0;
  for(int i=0; i<N; i++){
    if(vec[i] > 0){
      ans[i] = max(ans[i],min(vec[i],ok));
    }
    else if(vec[i] == 0){
      ok = 1e9;
    }
    else{
      ok = min(ok,abs(vec[i]));
    }
    ok--;
  }
  ok = 0;
  for(int i=N-1; i>=0; i--){
    if(vec[i] > 0){
      ans[i] = max(ans[i],min(vec[i],ok));
    }
    else if(vec[i] == 0){
      ok = 1e9;
    }
    else{
      ok = min(ok,abs(vec[i]));
    }
    ok--;
  }
  int A = 0;
  for(int i=0; i<N; i++){
    A += ans[i];
  }
  cout << A << endl;
}

int main(){
  while(true){
    solve();
    break;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3888
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;

void solve(){
  int N;
  cin >> N;
  vector<int> vec(N);
  for(int i=0; i<N; i++){
    cin >> vec[i];
  }
  vector<int> ans(N);
  int ok = 0;
  for(int i=0; i<N; i++){
    if(vec[i] > 0){
      ans[i] = max(ans[i],ok);
    }
    else if(vec[i] == 0){
      ok = 1e9;
    }
    else{
      ok = min(ok,abs(vec[i]));
    }
    ok--;
  }
  ok = 0;
  for(int i=N-1; i>=0; i--){
    if(vec[i] > 0){
      ans[i] = max(ans[i],min(vec[i],ok));
    }
    else if(vec[i] == 0){
      ok = 1e9;
    }
    else{
      ok = min(ok,abs(vec[i]));
    }
    ok--;
  }
  int A = 0;
  for(int i=0; i<N; i++){
    A += ans[i];
  }
  cout << A << endl;
}

int main(){
  while(true){
    solve();
    break;
  }
}
accuracy: 0.08695652173913043
time_ms: 10
memory_kb: 3808
final_rank: 20

code_snippet: // #include "atcoder/all"
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <iomanip> // setprecision
#include <complex> // complex
#include <math.h>
#include <functional>
#include <cassert>
using namespace std;
// using namespace atcoder;
using ll = long long;
using P = pair<ll,ll>;
constexpr ll INF = 1e15;
constexpr ll LLMAX = 9223372036854775807;
constexpr int inf = 1e9;
// constexpr ll mod = 1000000007;
constexpr ll mod = 998244353;
// 右下左上
const int dx[8] = {1, 0, -1, 0,1,1,-1,-1};
const int dy[8] = {0, 1, 0, -1,1,-1,1,-1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
#define eol "\n"
// ---------------------------------------------------------------------------

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int W;
    cin >> W;
    vector<ll> A(W);
    for(int i=0; i<W; i++){
        cin >> A[i];
    }
    vector<ll> dist(W,-inf);
    priority_queue<P> que;
    for(int i=0; i<W; i++){
        if(A[i] == 0){
            que.emplace(inf,i);
            dist[i] = inf;
        }
    }
    while(que.size()){
        int v = que.top().second;
        que.pop();
        {
            if(v-1 >= 0){
                if(dist[v-1] == -inf){
                    if(A[v-1] < 0){
                        dist[v-1] = min(dist[v]-1,abs(A[v-1])-1);
                        que.emplace(dist[v-1],v-1);
                    }else{
                        dist[v-1] = dist[v]-1;
                        que.emplace(dist[v-1],v-1);
                    }
                }
            }
        }
        {
            if(v+1 <= W-1){
                if(dist[v+1] == -inf){
                    if(A[v+1] < 0){
                        dist[v+1] = min(dist[v]-1,abs(A[v+1])-1);
                        que.emplace(dist[v+1],v+1);
                    }else{
                        dist[v+1] = dist[v] - 1;
                        que.emplace(dist[v+1],v+1);
                    }
                }
            }
        }
    }
    ll ans = 0;
    for(int i=0; i<W; i++){
        if(A[i] > 0){
            if(dist[i] < 0) continue;
            ans += min(A[i],dist[i]+1);
        }
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 6720
final_rank: 13