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
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

#ifdef USE_CERR_LOG
#define LOG if (true) cerr
const bool LogEnabled = true;
#else
#define LOG if (false) cerr
const bool LogEnabled = false;
#endif

bool LogBigEnabled = true;

#ifdef USE_FILE_CIN
ifstream fin("ele0.in");
#define cin fin
#endif

typedef unsigned uint;
typedef long long ll;
typedef unsigned long long ull;

const int InfCost = 999'999;

vector<ll> powers;
vector<int> distances;


void readData() {
    ios_base::sync_with_stdio(false);
    ll power;
    int currentDistance = 0;
    int n;
    
    cin >> n;
    
    for (int i = 0; i < n; i++) {
        cin >> power;
        currentDistance ++;
        
        if (power != 0) {
            if (!powers.empty()) {
                distances.push_back(currentDistance);
            }
            
            powers.push_back(power);
            currentDistance = 0;
        }
    }
 
    #ifdef USE_CERR_LOG
        for (ll power : powers) LOG << power << ", ";
        LOG << endl;
        for (int dist : distances) LOG << dist << ", ";
        LOG << endl;
    #endif
}


int solveTrivial() {
   if (powers.empty()) {
       return 0;
   }
   
   ll sum = 0;
   bool allPositive = true;
   
   for (auto power : powers) {
       sum += power;
       allPositive = allPositive && power > 0;
   }
   
   if (allPositive) {
       return 0;
   }
   
   if (sum < 0) {
       return -1;
   }
   
   if (powers.size() == 2) {
       return distances[0];
   }
   
   return -2;
}

struct Line {
    int cost;
    ll power;
};

int minPlusCost(const vector<Line>& lines) {
    int result = InfCost;
    
    for (const Line& line : lines) {
        if (line.power >= 0) {
            result = min(result, line.cost);
        }
    }
    
    return result;
}

int solve() {
    vector<Line> prevLines;
    vector<Line> currLines;
    
    prevLines.push_back({0, powers[0]});
    
    for (uint i = 0; i < distances.size(); i++) {
        ll currentPower = powers[i + 1];
        int currentDist = distances[i];
        
        int minCostForZero = minPlusCost(prevLines);
        if (minCostForZero < InfCost) {
            currLines.push_back({minCostForZero, currentPower});
        }
        
        for (const Line& prevLine : prevLines) {
            currLines.push_back({prevLine.cost + currentDist, prevLine.power + currentPower});
        }
        
        swap(prevLines, currLines);
        currLines.clear();
        
        #ifdef USE_CERR_LOG
            for (Line l : prevLines) LOG << "(" << l.cost << " " << l.power << "), ";
            LOG << endl << endl;
        #endif
    }
    
    return minPlusCost(prevLines);
}


int main() {
    readData();
    
    int result = solveTrivial();
    if (result == -2) {
        result = solve();
    }
    
    cout << result;
    
    return 0;
}