description
Beginning with n H nonnegative integer i , the next operation will be performed m times, the operation i th a given number of c [i], requires you to select c [i] a number greater than zero and subtracting them 1.
After the maximum number Q wheel operation can not operate (i.e., not c [i] a number greater than zero)
analysis
Obviously every time minus the maximum of those few, you can not cut a \ (GG \)
Then the first \ (a \) sorted, considering how time and time again to lose \ (a \) several foremost
However, the order will directly reduce the foremost mess, such as \ (5,5,5,4,4,4,3,2,1 \) minus the maximum of four
Then consider the separation of two position in the last paragraph, and then subtract the last paragraph from back to front
I.e. two separated \ (3 \) position, the \ (5 \) full minus one, \ (4 \) from a forwardly after Save \ (4 \) , becomes \ (4,4,4, 4,4,3,3,2,1 \)
Obviously you can use Fenwick tree \ (+ \) the difference easily maintain the array
code
#pragma GCC optimize("O3")
#pragma G++ optimize("O3")
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#define MAXN 1000005
#define ll long long
#define reg register ll
#define fo(i,a,b) for (reg i=a;i<=b;++i)
#define fd(i,a,b) for (reg i=a;i>=b;--i)
using namespace std;
ll tr[MAXN];
ll a[MAXN];
ll n,m;
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while (ch<'0' || '9'<ch){if (ch=='-')f=-1;ch=getchar();}
while ('0'<=ch && ch<='9')x=x*10+ch-'0',ch=getchar();
return x*f;
}
inline bool cmp(ll x,ll y){return x>y;}
inline void modify(ll x,ll y){while (x<=n+1)tr[x]+=y,x+=x&(-x);}
inline ll query(ll x){ll y=0;while (x)y+=tr[x],x-=x&(-x);return y;}//query(x)表示第x位的值
inline ll binary(ll x){ll l=1,r=n+1,mid;while (l<r)mid=(l+r)>>1,query(mid)<=x?r=mid:l=mid+1;return l;}
int main()
{
freopen("sequence.in","r",stdin);
freopen("sequence.out","w",stdout);
n=read(),m=read();fo(i,1,n)a[i]=read();sort(a+1,a+n+1,cmp);
fo(i,1,n)modify(i,a[i]),modify(i+1,-a[i]);
fo(i,1,m)
{
ll x=read(),tmp=query(x);
if (!tmp){printf("%lld\n",i-1);return 0;}
ll xx=binary(tmp),yy=binary(tmp-1);
modify(1,-1),modify(xx,1);
if (yy-(x-(xx-1))>0)modify(yy-(x-(xx-1)),-1),modify(yy,1);
}
printf("%lld\n",m);
return 0;
}