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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// tested by Hightail: https://github.com/dj3500/hightail
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define INF 1001001001
#define FOR(i,n) for(int i=0;(i)<(n);++(i))
#define FORI(i,n) for(int i=1;(i)<=(n);++(i))
#define mp make_pair
#define pii pair<int,int>
#define ll long long
#define vi vector<int>
#define SZ(x) ((int)((x).size()))
#define fi first
#define se second
#define wez(n) int n; scanf("%d",&(n));
#define wez2(n,m) int n,m; scanf("%d %d",&(n),&(m));
#define wez3(n,m,k) int n,m,k; scanf("%d %d %d",&(n),&(m),&(k));
inline void pisz(int n) { printf("%d\n",n); }
template<typename T,typename TT> ostream& operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";}
template<typename T> ostream& operator<<(ostream &s,vector<T> t){FOR(i,SZ(t))s<<t[i]<<" ";return s; }
#define DBG(vari) cout<<"["<<__LINE__<<"] "<<#vari<<" = "<<(vari)<<endl;
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (__typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define TESTS wez(testow)while(testow--)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define REMAX(a,b) (a)=max((a),(b));
#define REMIN(a,b) (a)=min((a),(b));
#define IOS ios_base::sync_with_stdio(0);


// FastIO od Gawrego
#define MAX_BUF 1024
 
char buf_in[MAX_BUF],*next_in,buf_out[MAX_BUF],*next_out;
 
void read_buf() {
  buf_in[fread(buf_in,1,MAX_BUF-1,stdin)]=0;
  next_in=buf_in;
}
 
void read_char() {
  next_in++;
  if (!*next_in)
    read_buf();
}
 
bool read_eof() {
  return !*next_in;
}
 
int read_int() {
  int x=0;
  bool neg=false;
 
  while (*next_in!='-'&&(*next_in<'0'||*next_in>'9'))
    read_char();
  if(*next_in=='-')neg=true,read_char();
  while (*next_in>='0' && *next_in<='9') {
    x=10*x+*next_in-'0';
    read_char();
  }
 
  return neg?-x:x;
}
 
double read_double() {
  double x=0.0,p=1.0;
 
  while ((*next_in<'0' || *next_in>'9') && *next_in!='-')
    read_char();
  if (*next_in=='-')
    return -read_double();
  while (*next_in>='0' && *next_in<='9') {
    x=10*x+*next_in-'0';
    read_char();
  }
  if (*next_in=='.') {
    read_char();
    while (*next_in>='0' && *next_in<='9') {
      x=10*x+*next_in-'0';
      p*=10.0;
      read_char();
    }
  }
 
  return x/p;
}
 
void write_buf() {
  fwrite(buf_out,1,next_out-buf_out,stdout);
  next_out=buf_out;
}
 
void write_char(char c) {
  *next_out++=c;
  if (next_out-buf_out==MAX_BUF)
    write_buf();
}
 
void _write_int(int x) {
  if (x) {
    _write_int(x/10);
    write_char('0'+x%10);
  }
}
 
void write_int(int x) {
  if (x<0) {
    write_char('-');
    x=-x;
  }
  if (!x)
    write_char('0');
  else
    _write_int(x);
}
 
void write_string(const char *c) {
  while (*c)
    write_char(*c++);
}
 
void init() {
  read_buf();
  next_out=buf_out;
}
 
void deinit() {
  write_buf();
}




struct S {
   int xp;
   ll yp;
   multiset<pair<ll,int>> st;
   
   S () : xp(0), yp(0) {}
   
   // add new pair (k,s)
   void insert (int k, ll s) {
      k -= xp;
      s -= yp;
      multiset<pair<ll,int>>::iterator it = st.insert(mp(s,-k));
      
      if (next(it) != st.end() && next(it)->se >= it->se) {
         // it is useless
         st.erase(it);
         return;
      }
      while (it != st.begin() && prev(it)->se <= it->se) {
         st.erase(prev(it));
      }
   }
   
   // add (+1,+y) to everyone
   void add (ll y) {
      xp += 1;
      yp += y;
   }
   
   // find smallest k such that there is (k,s) with s >= 0
   // (INF if none)
   int binsearch () {
      multiset<pair<ll,int>>::iterator it = st.lower_bound(mp(-yp,-INF));
      if (it == st.end()) return INF;
      else return -it->se + xp;
   }
   
   void dbg () {
      for (auto it : st) {
         printf("(%d,%d) ", -it.se + xp, int(it.fi + yp));
      }
   }
};

int main () {
   init();
   int n = read_int();
   S s;
   s.insert(0,0);
   FORI(i,n) {
      int xi = read_int();
      int k0 = s.binsearch();
      s.add(xi);
      if (k0 < INF) {
         s.insert(k0, xi);
      }
      /*DBG(i)
      DBG(xi)
      s.dbg();
      cout<<endl;
      cout<<endl;*/
   }
   int k0 = s.binsearch();
   if (k0 >= INF) k0 = -1;
   pisz(k0);
}