//http://codeforces.com/problemset/problem/732/D
#include<bits/stdc++.h>
using namespace std;
const int MAX=1e5+5;
int N,M,D[MAX],A[MAX],F[MAX];
bool Used[MAX];
bool Check(int x)
{
memset(Used,false,sizeof(Used));
memset(F,0,sizeof(F));
for (int i=x; i>=1; i--)
if (D[i]!=0&&!Used[D[i]])
{
F[i]=D[i];
Used[D[i]]=true;
}
int pre=0;
for (int i=1; i<=x; i++)
{
if (!F[i])
pre++;
else
{
if (A[F[i]]>pre)
return false;
else
pre-=A[F[i]];
}
}
for (int i=1; i<=M; i++)
if (!Used[i])
return false;
return true;
}
int main()
{
ios::sync_with_stdio(false);
cin>>N>>M;
for (int i=1; i<=N; i++)
cin>>D[i];
for (int i=1; i<=M; i++)
cin>>A[i];
int l=1,r=N,mid;
while (r-l>1)
{
mid=(l+r)>>1;
if (Check(mid))
r=mid;
else
l=mid;
}
if (Check(l))
cout<<l;
else if (Check(r))
cout<<r;
else
cout<<-1;
return 0;
}
CodeForces 732D Exams 二分+贪心
最新推荐文章于 2019-08-28 17:47:27 发布