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
#include <bits/stdc++.h>

using namespace std;

const int oo = INT_MAX/3;

template<typename T>
struct max_fenwick_tree
{
    size_t n;
    vector<T> F;

    max_fenwick_tree(size_t m, const T& v) : n(m), F(n+1, v) {}

    static constexpr size_t lsb(size_t x) { return x & -x; }

    T get_prefix(size_t p)
    {
        int r = F[0];
        while(p)
            r = max(r, F[p]), p -= lsb(p);
        return r;
    }

    void maximize(size_t p, const T& v)
    {
        p++;
        while(p <= n)
            F[p] = max(F[p], v), p += lsb(p);
    }
};

int main()
{
    ios::sync_with_stdio(false); cin.tie(nullptr);

    size_t n;
    cin >> n;

    int64_t s = 0;
    vector<pair<int64_t, size_t>> P = {{0, 0}};
    for(size_t i = 0; i < n; i++)
    {
        int a; cin >> a;
        P.emplace_back(s += a, i+1);
    }

    max_fenwick_tree<int> tree(n+1, -oo);
    sort(P.rbegin(), P.rend());

    for(auto [_, i] : P)
    {
        auto r = i == n ? 0 : tree.get_prefix(n - i) + 1;
        if(i == 0)
        {
            cout << (r >= 0 ? (int)n - r : -1) << '\n';
            break;
        }
        else if(r >= 0)
            tree.maximize(n - i, r);
    }
}