# [整体二分] Codechef January Challenge 2018 #MONSTER Killing Monsters

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<vector>
#define pb push_back
using namespace std;
typedef long long ll;

inline char nc(){
static char buf[100000],*p1=buf,*p2=buf;
}
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()) if (c=='-') b=-1;
}
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()) if (c=='-') b=-1;
}

const int N=500005;

int n,m;
ll h[N];
int x[N],y[N];

int tim[N],cc[N];

const int S=12;
const int ss=(1<<S)-1;
ll a[N];
int x0=x>>S,x1=x&ss;
for (int i=x0;;i=(i-1)&x0){
a[(i<<S)|x1]+=y;
if (i==0) break;
}
}
inline void Dec(int x,int y){
int x0=x>>S,x1=x&ss;
for (int i=x0;;i=(i-1)&x0){
a[(i<<S)|x1]-=y;
if (i==0) break;
}
}
inline bool Query(int x,ll y){
int x0=x>>S,x1=x&ss;
for (int i=x1^ss;;i=(i-1)&(x1^ss)){
y-=a[x0<<S|(i^ss)];
if (y<=0) return 0;
if (i==0) break;
}
return 1;
}

inline void Solve(int l,int r,vector<int> &v){
if (l==r){
for (int i:v) tim[i]=l;
return;
}
vector<int> lv,rv;
int mid=(l+r)>>1;
for (int i=l;i<=mid;i++)
for (int i:v)
if (Query(i,h[i]))
rv.pb(i);
else
lv.pb(i);
Solve(mid+1,r,rv);
for (int i=l;i<=mid;i++)
Dec(x[i],y[i]);
Solve(l,mid,lv);
}

int main(){
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);