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

using namespace std;


int ile;
int tab[1000000];
long long sum = 0;
int treesize = 1;
vector<pair<int, int> > el;
vector<int> treeL;
vector<int> treeR;
void treeLeft(int pos, int val){
    while(pos >= 1){
        treeL[pos] += val;
        pos /= 2;
    }
}
void treeRight(int pos, int val){
    while(pos >= 1){
        treeR[pos] += val;
        pos /= 2;
    }
}

int rightT(int a){
    int score = 0;
    for(int i = a; i < treesize * 2; i++){
        if(treeR[i] != 0){
            return treeR[i];
        }
        
    }
    //podaj sume od a do konca
    return score;
}
int leftT(int a){
    int score = 0;
    for(int i  = a; i >= treesize; i--){
        if(treeL[i] != 0){
            return treeL[i];
        }
        //score += treeL[i];
    }
    return score;
    //podaj suma od 0 do a
}

int main() {
    
    cin.tie(0);cout.tie(0);
    ios_base::sync_with_stdio(0);

    cin >> ile;
    for(int i = 0; i < ile; i++){
        cin >> tab[i];
        sum += tab[i];
        if(tab[i] != 0){
            el.push_back(make_pair(tab[i], i));
        }

    }
    //impossible
    if(sum < 0){
        cout << "-1\n";
        return 0;
    }

    int dist = el.back().second - el[0].second;
 //   cout << "Distance: " << dist << "\n";


    //how many possible
    int len = 0;
    vector < pair< pair<int, int>, pair<int, int> > > kol;
    int beg = 0;
    for(int i = 0; i < el.size() - 1; i++){
        beg += el[i].first;
        if(beg >= 0 && sum - beg >= 0){
            //{ {dist_gained, position}, {val_beg, val_end} }
            len ++;
            kol.push_back(make_pair(make_pair(el[i + 1].second - el[i].second, len), make_pair(beg, sum - beg)));
            
        }
    }
    sort(kol.begin(), kol.end());
    /*
    for(auto a : kol){
        cout << a.first.second << "\n";
    }
    */

    //cout << len << "\n\n";
    //cout << kol.size() << "\n\n";
    //TODO: drzewo przedzialowe
    //treesize - dlugosc drewa
    //len - zakres mozliwych do usuniecia przedzialow
    

    while(treesize <= len){
        treesize *= 2;
    }
    //tworzy drzewo 
    treeL.resize(2 * treesize + 5, 0);
    treeR.resize(2 * treesize + 5, 0);
    

    //treeLeft 
    //treeRight -- dodaje do drawa w punkcie x
    //checkL sprawdza sume na przedziale a - b
    //sprawdza max na przedziale a b

    while(kol.size() != 0){
        auto temp = kol.back();
        if(temp.second.first - leftT(temp.first.second + treesize - 1) >= 0 && temp.second.second - rightT(temp.first.second + treesize - 1) >= 0){
            //TODO: czy jest mozliwe usuniecie
            dist -= temp.first.first;
            //dodaj do drzew ze sa usuniete
            treeLeft(temp.first.second + treesize - 1, temp.second.first);
            treeRight(temp.first.second + treesize - 1, temp.second.second);
          //  cout << "removed " << temp.first.second << "\n";
        }
        else{//TOREMOVE
          /*  cout << "Not removed " << temp.first.second << "\n";
            cout << "condition first: " << temp.second.first << " - " << leftT(temp.first.second + treesize - 1) << "\n";
            cout << "condition second: " << temp.second.second << " - " << rightT(temp.first.second + treesize - 1) << "\n";
            */
        }
        
        //sprawdz czy mozliwe jest usuniecie 
        // tak? usun i update wynik + dodaj do drzewa
        // usun z kolejki 
        kol.pop_back();
    }
    cout << dist << "\n";
    return 0;


    
    return 0;
}