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
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define lll __int128
#define ull unsigned long long
#define fi first
#define se second
#define db double
#define ld long double
#define lld __float128

/// order_of_key, find_by_order

template<class T, class COMP>
using custom_set = tree<T, null_type, COMP, rb_tree_tag, tree_order_statistics_node_update>;

template<class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template<class T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

template<class T, class U>
using ordered_map = tree<T, U, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rnd_64(chrono::steady_clock::now().time_since_epoch().count());

const int MAXN = 500005;

ll dat[MAXN], prf[MAXN];

int main()
{
    int n; cin >> n;
    for(int i = 0; i < n; i++)
    {
        cin >> dat[i];
    }
    prf[0] = 0;
    for(int i = 1; i <= n; i++)
    {
        prf[i] = prf[i-1] + dat[i-1];
    }
    vector<ll> dp;
    dp.push_back(0);
    for(int i = 1; i < n; i++)
    {
        if(prf[i] > prf[n])
            continue;
        if(prf[i] < 0)
            continue;
        if(prf[i] >= dp.back())
            dp.push_back(prf[i]);
        else
        {
            auto j = lower_bound(dp.begin(), dp.end(), prf[i]);
            *j = prf[i];
        }
    }
    if(prf[n] < 0)
        cout << -1;
    else
    cout << n-dp.size();
}






































/// shche ne vmerla Ykraina