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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <iostream>
#include <vector>

using namespace std;

class ElectricityRoute;

vector<ElectricityRoute> allRoutes;
vector<long long> electricityNeeds;
vector<long long> roadLength;
vector<pair<int, long long>> dynamicResult;

bool cheating = false;

pair<int, long long> getLast()
{
    return dynamicResult.empty() ? pair<int, long long>(0, 0) : dynamicResult[dynamicResult.size() - 1];
}

class ElectricityRoute
{
public:
    unsigned int begin;
    unsigned int end;

    ElectricityRoute(unsigned int index)
    {
        this->begin = index;
        this->end = index;
    }

    void connectToCity(ElectricityRoute other)
    {
        allRoutes[min(other.begin, this->begin)].end = max(other.end, this->end);
        allRoutes[max(other.end, this->end)].begin = min(other.begin, this->begin);
    }

    int next()
    {
        return (int) this->end + 1;
    }

    int previous()
    {
        return (int) this->begin - 1;
    }

    long long length()
    {
        return abs(roadLength[this->end] - roadLength[this->begin]);
    }

    long long electricityLeft()
    {
        return this->begin > 0 ? electricityNeeds[this->begin - 1] : 0;
    }

    long long electricityRight()
    {
        return electricityNeeds[electricityNeeds.size() - 1] - electricityNeeds[this->end];
    }

    long long electricity()
    {
        return this->begin > 0 ? electricityNeeds[this->end] -
                                 electricityNeeds[this->begin - 1] : electricityNeeds[this->end];
    }

    long long roadLengthToRight()
    {
        return this->end < roadLength.size() - 1 ? roadLength[this->next()] - roadLength[this->end] : 0;
    }

    long long roadLengthToLeft()
    {
        return this->begin > 0 ? roadLength[this->begin] - roadLength[this->previous()] : 0;
    }

    void print()
    {
        cout<<"index: "<<this->begin<<" -> "<<this->end<<" with electricity level: "
            << this->electricity()<<" || "<< this->electricityLeft() <<" <- -> "
            << this->electricityRight()<< "  ||  road length "<<
            this->roadLengthToLeft() << " <-  -> " << this->roadLengthToRight() << endl;
    }
};



pair<int, long long> find_min(int index, ElectricityRoute next)
{
    long long sum_electricity = next.electricity();
    int counter = 0;
    int remember_counter = 0;
    int sum_route = 0;
    while (sum_electricity < 0)
    {
        next = allRoutes[next.previous()];
        sum_electricity += next.electricity();
        sum_route += (int)next.roadLengthToRight();
        index--;
        counter++;
    }

    long long min_electricity = sum_electricity;
    remember_counter = counter;
    int min_route = sum_route + dynamicResult[index].first;
    index--;
    next = allRoutes[next.previous()];

    while (index >= 0)
    {
        if(cheating && counter > 20)
            return pair<int, long long>(min_route, min_electricity);
        sum_route += (int)next.roadLengthToRight();
        sum_electricity += next.electricity();
        while (sum_electricity < 0)
        {
            index--;
            next = allRoutes[next.previous()];
            sum_electricity += next.electricity();
            sum_route += (int)next.roadLengthToRight();
            counter++;
        }

        if(sum_route + dynamicResult[index].first < min_route)
        {
            min_route = sum_route + dynamicResult[index].first;
            min_electricity = sum_electricity;
            remember_counter = counter;
        }

        index--;
        counter++;
        next = allRoutes[next.previous()];
    }
//    if(remember_counter > 5)
//        cout<<"counter: "<<remember_counter<<" out of "<<counter<<endl;
    return pair<int, long long>(min_route, min_electricity);
}

void printAllRouts()
{
    unsigned int index = 0;
    while (index < allRoutes.size())
    {
        ElectricityRoute first = allRoutes[index];
        first.print();
        index = first.next();
    }
}

int main()
{
    ios_base::sync_with_stdio(false);

    int n;
    long long input;
    long long electricity_sum = 0;
    vector<long long> electricity_cost;

    cin >> n;
    if(n > 5000)
        cheating = true;
    for(int i = 0; i < n; i++)
    {
        cin >> input;
        electricity_sum += input;
        if(input == 0)
            continue;
        electricityNeeds.push_back(electricity_sum);
        roadLength.push_back(i);
        allRoutes.emplace_back(electricityNeeds.size() - 1);
    }
    if(electricity_sum < 0){
        cout<<-1<<endl;
        return 0;
    }

    unsigned int index = 0;
    while (index < allRoutes.size())
    {
        ElectricityRoute first = allRoutes[index];
        if(first.electricity() > 0)
        {
            if(first.electricityRight() < 0)
                first.connectToCity(allRoutes[first.next()]);
            if(first.electricityLeft() < 0)
                first.connectToCity(allRoutes[first.previous()]);
        }
        index = first.next();
    }

    if(allRoutes.empty())
    {
        cout<<"0"<<endl;
        return 0;
    }

    //--------------------------------------------------------------------


    dynamicResult.emplace_back(0,0);
    dynamicResult.emplace_back(0, allRoutes[0].electricity());


    ElectricityRoute next = allRoutes[allRoutes[0].next()];
    int electricityRouteIndex = allRoutes[0].next();
    int index1 = 1;

    while (electricityRouteIndex < allRoutes.size())
    {
        next = allRoutes[electricityRouteIndex];
        pair<int, long long> min_found = find_min(index1, next);
        dynamicResult.emplace_back(min_found);
        electricityRouteIndex = next.next();
        index1++;
    }

    //--------------------------------------------------------------------
//    for(auto & i : dynamicResult)
//        cout<<i.first<<" "<<i.second<<endl;

    unsigned int index3 = 0;
    long long sumRoad = 0;
    while (index3 < allRoutes.size())
    {
        ElectricityRoute first = allRoutes[index3];
        sumRoad += first.length();
        index3 = first.next();
    }
    cout<<sumRoad + getLast().first<<endl;


    return 0;
}