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
#define _CRT_SECURE_NO_WARNINGS
#define ONLINE_JUDGE
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cassert>
using namespace std;

typedef vector<int> VI;

VI readEnergyProduction()
{
    int elements, temp;
    scanf("%d", &elements);

    VI result(elements);
    for (int i = 0; i < elements; ++i)
    {
        scanf("%d", &temp);
        result[i] = temp;
    }

    return result;
}

const int IhaveNoIdea = -1;
const short UnknownMinCost = -1;
const short Infinity = 10000;

void myassert(bool condition)
{
    if (condition == false)
        throw "ups";
}

class Solver
{
public:
    int solve(const VI& energyProduction)
    {
        this->energyProduction = energyProduction;
        if (energyProduction.size() > 5000)
            return IhaveNoIdea;

        computeNonNegativeEnergyProduction();
        minCost = vector<vector<short>>(energyProduction.size(), vector<short>(energyProduction.size(), UnknownMinCost));
        short answer = getMinCost(0, energyProduction.size() - 1);

        return answer == Infinity ? -1 : answer;
    }
private:
    void computeNonNegativeEnergyProduction()
    {
        isNonNegativeEnergyProduction = vector<vector<bool>>(energyProduction.size(),
            vector<bool>(energyProduction.size()));

        vector<long long> sumStartingFrom(energyProduction.size() + 1, 0);
        sumStartingFrom[energyProduction.size() - 1] = energyProduction[energyProduction.size() - 1];
        for (int i = energyProduction.size() - 2; i >= 0; --i)
        {
            sumStartingFrom[i] = sumStartingFrom[i + 1] + energyProduction[i];
        }

        for (int beginning = 0; beginning < energyProduction.size(); ++beginning)
        {
            for (int end = 0; end < energyProduction.size(); ++end)
            {
                const long long sumFromBeginnningToEnd = sumStartingFrom[beginning] - sumStartingFrom[end + 1];
                isNonNegativeEnergyProduction[beginning][end] = sumFromBeginnningToEnd >= 0;
            }
        }
    }

    short getMinCost(int beginning, int end)
    {
        if (minCost[beginning][end] != UnknownMinCost)
            return minCost[beginning][end];

        if (!isNonNegativeEnergyProduction[beginning][end])
            return minCost[beginning][end] = Infinity;

        short result = end - beginning;

        for (int middle = beginning + 1; middle <= end; ++middle)
        {
            if (!isNonNegativeEnergyProduction[beginning][middle - 1] || !isNonNegativeEnergyProduction[middle][end])
                continue;

            const short firstMinCost = getMinCost(beginning, middle - 1);
            if (firstMinCost >= result)
                continue;
            const short currentCandidate =  firstMinCost + getMinCost(middle, end);
            result = min(result, currentCandidate);
        }

        return minCost[beginning][end] = result;
    }

    vector<vector<bool>> isNonNegativeEnergyProduction;
    vector<vector<short>> minCost;
    VI energyProduction;
};

int main()
{
    Solver solver;
#ifndef ONLINE_JUDGE
    {
        VI in{ 2, -5, 0, 2, 0, 0, 0, 4, 0, 0, -1, 4, 0, 0, 0, 0, -3 };
        assert(solver.solve(in) == 12);
    }
    {
        VI in{ 0 };
        assert(solver.solve(in) == 0);
    }
    {
        VI in{ 1 };
        assert(solver.solve(in) == 0);
    }
    {
        VI in{ 1, 1 };
        assert(solver.solve(in) == 0);
    }
    {
        VI in{ 1, 2 };
        assert(solver.solve(in) == 0);
    }
    {
        VI in{ -1 };
        assert(solver.solve(in) == -1);
    }
    {
        VI in{ 1, -1 };
        assert(solver.solve(in) == 1);
    }
    {
        VI in{ 1, -1, 1, -1, 1, -1 };
        assert(solver.solve(in) == 3);
    }
    {
        VI in{ 1, -1, 1, -1, -1, -1 };
        assert(solver.solve(in) == -1);
    }
#endif
    VI energyProduction = readEnergyProduction();
    printf("%d\n", solver.solve(energyProduction));
#ifndef ONLINE_JUDGE
    system("pause");
#endif
    return 0;
}