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

using namespace std;
#define PB push_back
#define LL long long
#define int LL
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORD(i,a,b) for (int i = (a); i >= (b); i--)
#define REP(i,n) FOR(i,0,(int)(n)-1)
#define st first
#define nd second
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define VI vector<int>
#define PII pair<int,int>
#define LD long double

template<class C> void mini(C& a4, C b4) { a4 = min(a4, b4); }
template<class C> void maxi(C& a4, C b4) { a4 = max(a4, b4); }

template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;
  cerr<<"="<<h<<","; _dbg(sdbg+1, a...);
}

template<class T> ostream &operator<<(ostream &os, vector<T> V){
  os<<"[";for(auto vv:V)os<<vv<<",";return os<<"]";
} 

template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {
  return os << "(" << P.st << "," << P.nd << ")";
}


#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif

struct Dyn{
    map<int,int> balance2cost;
    int cost_offset = 0;
    int balance_offset = 0;
    Dyn(){
        balance2cost.insert({0,0});
    }
    void add(int balance){
        optional<PII> to_add;
        //szukamy 0
        auto it = balance2cost.lower_bound(-balance_offset);
        if(it != balance2cost.end()){
            to_add = {balance, it->nd + cost_offset};
        }
        cost_offset++;
        balance_offset += balance;
        if(to_add){
            insert(*to_add);
            // sprawdź czy wypukły z poprzednim
        }
    }
    void insert(PII to_add){
        to_add.st -= balance_offset;
        to_add.nd -= cost_offset;
        auto it = balance2cost.lower_bound(to_add.st);
        if(it != balance2cost.end() && redundant(*it, to_add)){
            return;
        }
        while(it != balance2cost.begin()){
            it--;
            if(redundant(to_add, *it)){
                balance2cost.erase(it++);
            } else{
                break;
            }
        }
        balance2cost[to_add.st] = to_add.nd;
    }
    bool redundant(PII p1, PII p2){
        // is p2 redundant?
        return p2.nd >= p1.nd && p2.st <= p1.st;
    }
};

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(11);
  cerr << fixed << setprecision(6);
  int n;
  cin >> n;
  Dyn dyn;
  REP(i, n){
    int t;
    cin >> t;
    dyn.add(t);
  }
  auto it = dyn.balance2cost.lower_bound(-dyn.balance_offset);
  if(it == dyn.balance2cost.end()){
    cout << -1 <<"\n";
  } else{
    cout << it->nd + dyn.cost_offset << "\n";
  }
}