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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const ll inf = 1'000'000'000'000'000'000ll;

class tree {
  struct node {
    ll buf;
    ll max_value;
  };
  int size{0}, lg{0};
  vector<node> nodes;

public:
  tree(int n) {
    for (size = 1; size < n; size *= 2) lg++;
    nodes.assign(2 * size, {0, -inf});
    val.assign(n, 0);
  }

  void add(int b, int e, ll v) {
    _add(1, 0, size, b, e, v);
  }

  ll get(int pos) {
    pos += size;
    for (int i = lg; i >= 0; i--) {
      flush(pos >> i);
    }
    update(pos);
    ll res = nodes[pos].max_value;
    while (pos) {
      update(pos);
      pos /= 2;
    }
    return res;
  }
  
  int get_last_zero(int len) {
    int pos = 1;
    while (pos < size) {
      flush(pos);
      if (nodes[2 * pos + 1].max_value + nodes[2*pos + 1].buf >= 0) {
        pos = 2 * pos + 1;
      } else {
        pos = 2 * pos;
      }
    }
    update(pos);
    int res = nodes[pos].max_value >= 0 ? pos - size : - 1;
    while (pos) {
      update(pos);
      pos /= 2;
    }
    return res < len ? res : -1;
  }

private:

  void flush(int pos) {
    if (pos < size) {
      nodes[2*pos].buf += nodes[pos].buf;
      nodes[2*pos+1].buf += nodes[pos].buf;
      nodes[pos].buf = 0;
    }
  }

  void update(int pos) {
    if (pos < size) {
      // assert(nodes[pos].buf == 0);
      nodes[pos].max_value = nodes[2*pos].max_value + nodes[2*pos].buf;
    } else {
      nodes[pos].max_value += nodes[pos].buf;
      nodes[pos].buf = 0;
    }
  }

  vector<ll> val;
  void add_brute(int b, int e, ll v) {
    while (b != e) val[b++] += v;
  }
  ll get_brute(int pos) {
    return val[pos];
  }
  int get_last_zero_brute(int len) {
    for (int i = len - 1; i >= 0; i--) {
      if (!val[i]) {
        return i;
      }
    }
    return -1;
  }

  void _add(int pos, int p, int q, int b, int e, ll v) {
    flush(pos);
    if (b <= p && q <= e) {
      nodes[pos].buf += v;
      flush(pos);
      update(pos);
      return;
    }
    if (e <= p || b >= q) {
      update(pos);
      return;
    }
    int mid = (p + q) / 2;
    _add(2 * pos, p, mid, b, e, v);
    _add(2 * pos + 1, mid, q, b, e, v);
    update(pos);
  }

};

int main() {
  int n;
  scanf("%d", &n);
  tree T(n);
  int a;
  scanf("%d", &a);
  T.add(0, 1, a - T.get(0));
  // for (int j = 0; j < 1; j++) printf("%lld\n", T.get(j)); printf("\n");
  int len = 1;
  for (int i = 1; i < n; i++) {
    scanf("%d", &a);
    auto last_zero_pos = T.get_last_zero(len);
    auto last_val = T.get(len - 1);
    // printf("=== %d\n", a);
    T.add(0, len, a);
    auto cur_val = T.get(last_zero_pos + 1);
    // printf("last zero %d last val %lld cur val %lld\n", last_zero_pos, last_val, cur_val);
    if (last_zero_pos != -1 && cur_val <= a) {
      T.add(last_zero_pos + 1, last_zero_pos + 2, a - cur_val);
      if (last_zero_pos == len - 1) {
        len++;
      }
    } else if (last_val > 0) {
      T.add(len, len + 1, a - T.get(len));
      len++;
    }
    // for (int j = 0; j < len; j++) printf("%lld ", T.get(j)); printf("\n");
  }
  int res = 0;
  while (res < len && T.get(res) >= 0) {
    res++;
  }
  printf("%d\n", res ? n - res : -1);
}