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
#define _CRT_SECURE_NO_WARNINGS 1
#define _WINSOCK_DEPRECATED_NO_WARNINGS 1

#include <iostream>
#include <string>
#include <vector>
using namespace std;



long city[500001];



int main()
{
    int n;
    
    scanf("%d", &n);
   
      
    for (int i = 0; i < n; ++i)
    {
        int c;
        scanf("%d", &c);
        city[i] = c;
        
    }

    vector<vector<pair<long long, long long>>> cities(n);

    pair<long long, long long> tuple = make_pair(city[0], 0);
    cities[0].push_back(tuple);

    for (int i = 1; i < n; ++i)
    {
        long long minRoads = 999999;
        int d = (cities[i - 1]).size();
        for (int j = 0; j < cities[i - 1].size(); ++j)
        {
            long long power = cities[i - 1][j].first;
            long long roads = cities[i - 1][j].second;
            if (power >= 0)
            {
                minRoads = min(minRoads, roads);
            }
            pair<long long, long long> tuple = make_pair(power + city[i], roads + 1);
            cities[i].push_back(tuple);
        }
        if (minRoads != 999999)
        {
            pair<long long, long long> tuple = make_pair(city[i], minRoads);
            cities[i].push_back(tuple);
        }
    }


    long long minRoads = 999999;
    for (int j = 0; j < cities[n - 1].size(); ++j)
    {
        long long power = cities[n - 1][j].first;
        long long roads = cities[n - 1][j].second;
        if (power >= 0)
        {
            minRoads = min(minRoads, roads);
        }
    }
    if (minRoads != 999999)
    {
        printf("%lld", minRoads);
        return 0;
    }

    printf("-1");
}