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

using namespace std;

pair<int, int> findMinCluster(vector<int> &points, vector<long long> &balance, vector<int>::iterator &consumerCity) {
    int leftBound = *consumerCity;
    int minCost = 1000000000;
    int rightBound = *consumerCity;

    auto lit = points.begin();
    while (lit != (consumerCity + 1)) {
        auto rit = consumerCity;
        while (rit != points.end()) {
            if (balance[*rit] - balance[*lit - 1] >= 0) {
                int cost = *rit - *lit;
                if (cost < minCost) {
                    minCost = cost;
                    leftBound = *lit;
                    rightBound = *rit;
                }
            }
            rit += 1;
        }
        lit += 1;
    }

    return pair<int, int>(leftBound, rightBound);
}

class Range {
public:

    int l;
    int r;

    Range(const int l, const int r) {
        this->l = l;
        this->r = r;
    }

    bool overlaps(const Range &that) const {
        return (l <= that.l && that.l <= r) || (that.l <= l && l <= that.r);
    }

    void join(const Range &range) {
        this->l = std::min(l, range.l);
        this->r = std::max(r, range.r);
    }

    bool compare(const Range &that) const {
        return l < that.l;
    }
};

bool compare(Range &r1, Range &r2) {
    return r1.compare(r2);
}

class Ranges {
    std::vector<Range> *ranges;
public:
    Ranges() {
        this->ranges = new std::vector<Range>();
    }

    void add(Range r) {
        ranges->push_back(r);
    }

    Ranges *compact() {
        if (!ranges->empty()) {
            std::sort(ranges->begin(), ranges->end(), compare);
            auto *newVector = new std::vector<Range>();
            newVector->push_back(ranges->front());
            auto iter = newVector->begin();
            for (auto it = ranges->begin() + 1; it != ranges->end(); ++it) {
                Range &currentOld = *it;
                if (iter->overlaps(currentOld)) {
                    iter->join(currentOld);
                } else {
                    newVector->push_back(currentOld);
                    iter = newVector->end() - 1;
                }
            }
            delete ranges;
            ranges = newVector;
        }
        return this;
    }

    int count() {
        int ret = 0;
        for (auto &range : *ranges) {
            ret += range.r - range.l;
        }
        return ret;
    }

};


int main() {
    int n;
    cin >> n;
    vector<long long> cities = vector<long long>(n + 1);
    vector<long long> balance = vector<long long>(n + 1);
    balance[0] = 0;
    vector<int> points = vector<int>();
    for (int i = 1; i <= n; ++i) {
        cin >> cities[i];
        balance[i] = balance[i - 1] + cities[i];
        if (cities[i] != 0) {
            points.push_back(i);
        }
    }

    if (balance[n] < 0) {
        cout << -1 << endl;
    } else {
        Ranges ranges = Ranges();
        auto lastCluster = pair<int, int>(0, 0);
        for (auto city = points.begin(); city != points.end(); city++) {
            if (cities[*city] < 0) {
                if (*city < lastCluster.second) {
                    // already calculated skip
                } else {


                    pair<int, int> cluster = findMinCluster(points, balance, city);

                    ranges.add(Range(cluster.first, cluster.second));
                    lastCluster = cluster;
                }
            }
        }
        ranges.compact();
        cout << ranges.count() << endl;
    }

    return 0;
}