1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back 

const int N = 5e5 + 7;

pair<int, int> tab[N];
vector<int> fabryki;

int n;


int main()
{
    // ios_base::sync_with_stdio(0);
    // cin.tie(0); cout.tie(0);
    cin >> n;
    int it = 1;
    for(int i = 1; i <= n; i++)
    {
        int temp;
        cin >> temp;
        if(temp != 0)
        {
            tab[it].first = temp;
            tab[it].second = i;
            if(temp < 0)
                fabryki.pb(it);
            it++;
        }
    }


    int nN = it;
    ll ans = 0;
    for(int i = 0; i < fabryki.size(); i++)
    {
        // int it = tab[fabryki[i]].second;
        int it = fabryki[i];
        ll potrzeba = -tab[fabryki[i]].first;

        // cout << it << " " << potrzeba << "\n";
        //idziemy w lewo
        ll lE = 0, lK = 0, lI = 0, lastKL = 0;
        
        for(int l = it - 1; l > 0; l--)
        {
            if(tab[l].first == 0 || tab[l].first < 0)
                break;
            lI = l;
            lE += tab[l].first;
            lK = tab[it].second - tab[l].second;
            lastKL = tab[l + 1].second - tab[l].second;
            if(lE >= potrzeba)
                break;
        }


        ll rE = 0, rK = 0, rI = 0, lastKR = 0;
        for(int r = it + 1; r < nN; r++)
        {

            if(tab[r].first == 0 || tab[r].first < 0)
                break;
            rI = r;
            rE += tab[r].first;
            rK = tab[r].second - tab[it].second;
            lastKR = tab[r].second - tab[r - 1].second;
            if(rE >= potrzeba)
                break;
        }

        // cout << lI << " " << lE << " " << lK << " " << lastKL << "\n";
        // cout << rI << " " << rE << " " << rK << " " << lastKR << "\n";
        // cout << "DUASDSAD\n";

        // if się nie da, to czy nie chcemy dodawać kolejnej fabryce czy coś w tym stylu

        ll suma = rE + lE;

        while(true)
        {
            if(lastKL == 0 && lastKR == 0)
                break;
            // cout << lastKL << " " << lastKR << "\n";
            // cout << lK << " " << rK << "\n";
            // cout << "SUMA: " << suma << "\n";

            if(lastKR != 0 && lastKL != 0)
            {
                if(lastKR > lastKL)
                {
                    // cout << "TUTAJ KR\n";
                    if(suma - tab[rI].first >= potrzeba)
                    {
                        suma -= tab[rI].first;
                        
                        rK -= lastKR;
                        if(tab[rI - 1].second == tab[it].second)
                            lastKR = 0;
                        else
                            lastKR = tab[rI].second - tab[rI - 1].second;
                        rI--;
                    }
                    else
                    {
                        lastKR = 0;
                    }

                }
                else
                {
                    if(suma - tab[lI].first >= potrzeba)
                    {
                        suma -= tab[lI].first;
                        
                        lK -= lastKL;
                        
                        if(tab[lI + 1].second == tab[it].second)
                            lastKL = 0;
                        else
                            lastKL = tab[lI + 1].second - tab[lI].second;
                        lI++;
                    }
                    else
                    {
                        lastKL = 0;
                    }
                }
            }
            else if(lastKR == 0 && lastKL != 0)
            {
                if(suma - tab[lI].first >= potrzeba)
                    {
                        suma -= tab[lI].first;
                        
                        lK -= lastKL;
                        
                        if(tab[lI + 1].second == tab[it].second)
                            lastKL = 0;
                        else
                            lastKL = tab[lI + 1].second - tab[lI].second;
                        lI++;
                    }
                    else
                    {
                        lastKL = 0;
                    }
            }
            else if(lastKR != 0 && lastKL == 0)
            {
                if(suma - tab[rI].first >= potrzeba)
                    {
                        suma -= tab[rI].first;
                        
                        rK -= lastKR;
                        if(tab[rI - 1].second == tab[it].second)
                            lastKR = 0;
                        else
                            lastKR = tab[rI].second - tab[rI - 1].second;
                        rI--;
                    }
                    else
                    {
                        lastKR = 0;
                    }
            }
            
        }
        // cout << "WYN: " << lK + rK << "\n";


        ans += lK + rK;

        // break;
        //idziemy w prawo
    }
    cout << ans << "\n";
}