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
#include<cstdio>
#include<unordered_map>

const int INF = 1000000;

int n;
long long power[500500];
long long sum[500500];

std::unordered_map<long long, int> mem;

int solve(int begin, int end)
{
    int best_res;
    if (mem.find(begin + INF * end) == mem.end())
    {
        long long total = sum[end] - sum[begin - 1];
        if (total < 0LL)
        {
            mem[begin + INF * end] = -1;
            return -1;
        }
        best_res = end - begin;
        for (int i = begin; i < end; ++i)
        {
            int res1 = solve(begin, i);
            int res2 = solve(i + 1, end);
            if (-1 == res1 || -1 == res2) continue;
            if (res1 + res2 < best_res) best_res = res1 + res2;
        }
        mem[begin + INF * end] = best_res;
    }
    return mem[begin + INF * end];
}

int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
    {
        scanf("%lld", &power[i]);
        sum[i + 1] = sum[i] + power[i];
    }
    int ans = solve(0, n);
    printf("%d\n", ans);
    return 0;
}