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

using namespace std;

struct Node
{
public:
    long long Energy = 0;
    int Distance = 0;
};

inline bool operator< (const Node& l, const Node& r) {
    return l.Energy < r.Energy;
}

vector<Node> inputNodes;
set<Node> dynamicNodes;

long long energyOffset = 0;
int distanceOffset = 0;

void getData() {
    int n;
    cin >> n;
    int i = 0;
    Node node;
    for (;i < n;i++) {
        int t;
        cin >> t;
        if (t != 0) {
            i++;
            node.Energy = t;
            break;
        }
    }

    inputNodes.push_back(node);
    node = Node();

    if (i == n)
        return;

    for (;i < n;i++) {
        int t;
        cin >> t;
        node.Distance++;
        if (t != 0) {
            node.Energy = t;
            inputNodes.push_back(node);
            node = Node();
        }
    }
}

bool isImposible() {
    long long acc = 0;
    for (int i = 0;i < inputNodes.size();i++) {
        acc += inputNodes[i].Energy;
    }

    return acc < 0;
}

void updateDynamic(vector<Node>::iterator& it) {
    Node tmp;
    tmp.Energy = -energyOffset;
    set<Node>::iterator it_zero = dynamicNodes.lower_bound(tmp);
    energyOffset += it->Energy;
    distanceOffset += it->Distance;
    if (it_zero == dynamicNodes.end())
        return;
    tmp.Distance = it_zero->Distance - it->Distance;
    tmp.Energy = it->Energy - energyOffset;
    
    set<Node>::iterator it_arleady = dynamicNodes.lower_bound(tmp);
    if (it_arleady->Energy == tmp.Energy) {
        if (it_arleady->Distance > tmp.Distance) {
            dynamicNodes.erase(it_arleady);
        }
        else {
            return;
        }
    }

    dynamicNodes.insert(tmp);
    set<Node>::iterator it_insert = dynamicNodes.find(tmp);
    set<Node>::iterator it_erase = it_insert;
    while (it_erase != dynamicNodes.begin()) {
        it_erase--;
        if (it_erase->Distance < tmp.Distance) {
            it_erase++;
            break;
        }
    }

    dynamicNodes.erase(it_erase, it_insert);
}

void printDynamic() {
    for (set<Node>::iterator it = dynamicNodes.begin(); it != dynamicNodes.end(); it++) {
        printf("%d -> %d\n", it->Energy + energyOffset, it->Distance + distanceOffset);
    }
    printf("\n");
}

int main()
{
    getData();
    if (inputNodes.size() == 0) {
        cout << 0;
        return 0;
    }

    if (isImposible()) {
        cout << -1;
        return 0;
    }

    dynamicNodes.insert(inputNodes.front());

    vector<Node>::iterator it = inputNodes.begin();
    it++;
    while (it != inputNodes.end()) {
        updateDynamic(it);
        //printDynamic();
        it++;
    }

    Node tmp;
    tmp.Energy = -energyOffset;
    set<Node>::iterator it_zero = dynamicNodes.lower_bound(tmp);

    cout << it_zero->Distance + distanceOffset;

    return 0;
}