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

struct ConnectedComponent
{
    unsigned start;
    unsigned end;
    int weight;
};

inline int getConnectedComponentSize(ConnectedComponent connected_component) {return connected_component.end - connected_component.start;}
//inline int getDistanceBetweenComponents(ConnectedComponent *a, ConnectedComponent *b) {return b->start - a->end;}

int main()
{
    //std::ios_base::sync_with_stdio(0);
    unsigned n;
    std::cin >> n;
    std::list<ConnectedComponent> connected_components;
    for (unsigned i = 0; i < n; i++)
    {
        int weight;
        std::cin >> weight;
        if (weight != 0)
        connected_components.push_back({i, i, weight});
    }
    for (std::list<ConnectedComponent>::iterator i = connected_components.begin(); i != connected_components.end(); i++)
    {
        //std::cout << i->weight;
        if (i->weight < 0)
        {
            int weight = i->weight;
            std::list<ConnectedComponent>::iterator j = i;
            std::list<ConnectedComponent>::iterator k = i;
            std::list<ConnectedComponent>::iterator behind_begin = connected_components.begin();
            behind_begin--;
            j--;
            k++;
            while (weight <= 0)
            {
                if (j != behind_begin && k != connected_components.end())
                {
                    if (i->start - j->end < k->start - i->end)
                    {
                        weight += j->weight;
                        j--;
                    }
                    else
                    {
                        weight += k->weight;
                        k++;
                    }
                }
                else if (j != behind_begin)
                {
                    weight += j->weight;
                    j--;
                }
                else if (k != connected_components.end())
                {
                    weight += k->weight;
                    k++;
                }
                else
                {
                    std::cout << "-1";
                    return 0;
                }
            }
            j++;
            k--;
            while (true)
            {
                if (weight - j->weight >= 0 && j != i)
                {
                    weight -= j->weight;
                    j++;
                }
                else if (weight - k->weight >= 0 && k != i)
                {
                    weight -= k->weight;
                    k--;
                }
                else
                {
                    break;
                }
            }
            //std::list<ConnectedComponent>::iterator j_base = j.base();
            ConnectedComponent res = {j->start, k->end, weight};
            k++;
            connected_components.insert(k, res);
            k--;
            //std::cout << k->start << k->end << std::endl;
            connected_components.erase(j, k);
            //k--;
            i = k;
        }
    }
    unsigned res = 0;
    for (ConnectedComponent connected_component : connected_components)
        res += connected_component.end - connected_component.start;
    std::cout << res;
    return 0;
}