您的位置:首页 > 其它

CodeForces-732D-Exams(二分 模拟 贪心)

2016-11-23 13:06 447 查看
D. Exams

time limit per test1 second

memory limit per test256 megabytes

inputstandard input

outputstandard output

Vasiliy has an exam period which will continue for n days. He has to pass exams on m subjects. Subjects are numbered from 1 to m.

About every day we know exam for which one of m subjects can be passed on that day. Perhaps, some day you can’t pass any exam. It is not allowed to pass more than one exam on any day.

On each day Vasiliy can either pass the exam of that day (it takes the whole day) or prepare all day for some exam or have a rest.

About each subject Vasiliy know a number ai — the number of days he should prepare to pass the exam number i. Vasiliy can switch subjects while preparing for exams, it is not necessary to prepare continuously during ai days for the exam number i. He can mix the order of preparation for exams in any way.

Your task is to determine the minimum number of days in which Vasiliy can pass all exams, or determine that it is impossible. Each exam should be passed exactly one time.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 105) — the number of days in the exam period and the number of subjects.

The second line contains n integers d1, d2, …, dn (0 ≤ di ≤ m), where di is the number of subject, the exam of which can be passed on the day number i. If di equals 0, it is not allowed to pass any exams on the day number i.

The third line contains m positive integers a1, a2, …, am (1 ≤ ai ≤ 105), where ai is the number of days that are needed to prepare before passing the exam on the subject i.

Output

Print one integer — the minimum number of days in which Vasiliy can pass all exams. If it is impossible, print -1.

Examples

input

7 2

0 1 0 2 1 0 2

2 1

output

5

input

10 3

0 0 1 2 3 0 2 0 1 2

1 1 4

output

9

input

5 1

1 1 1 1 1

5

output

-1

Note

In the first example Vasiliy can behave as follows. On the first and the second day he can prepare for the exam number 1 and pass it on the fifth day, prepare for the exam number 2 on the third day and pass it on the fourth day.

In the second example Vasiliy should prepare for the exam number 3 during the first four days and pass it on the fifth day. Then on the sixth day he should prepare for the exam number 2 and then pass it on the seventh day. After that he needs to prepare for the exam number 1 on the eighth day and pass it on the ninth day.

In the third example Vasiliy can’t pass the only exam because he hasn’t anough time to prepare for it.

题意:第一行输入N和M,代表考试期有N天,考试科目编号1~M,接着一行输入N个di,代表第i天可以考第di门科目,接着一行M个ai,代表第i门科目需要预习ai天。对于任意一门科目必须预习足够的天数并在允许考试的日期参加考试,预习某门课的日期不必是连续的,求出最小通过所有科目的天数,如果不存在则输出-1

思路:二分枚举天数,对于每一个枚举到的天数mid,倒序判断在mid天内是否能完成所有科目,时间复杂度O(N*logN)

代码

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
const int maxn=100005;
int num[maxn];//第i天能通过num[i]科目
int day[maxn];//第i门课需要学习day[i]天
bool flag[maxn];//标记第i门课是否已经过了
int N,M;
int sum_day=0;//最少所需天数
bool Judge(int mid)//mid天能否完成全部作业
{
for(int i=1; i<=M; i++)
flag[i]=false;//假设M门课都没过
int res=sum_day+M;//还需要的天数
for(int i=mid; i>0; i--)
{
if(num[i]==0)
continue;
if(flag[num[i]]==false)//如果第i天能考没考过的科目num[i]
{
if(res>i)//预判剩余天数不足
return false;
flag[num[i]]=true;
res-=day[num[i]]+1;
}
}
for(int i=1; i<=M; i++)
if(flag[i]==false)
return false;
return true;
}
int main()
{
scanf("%d%d",&N,&M);
for(int i=1; i<=N; i++)
scanf("%d",&num[i]);
int left=M,right=N;
for(int i=1; i<=M; i++)
{
scanf("%d",&day[i]);
sum_day+=day[i];
left+=day[i];//更新最少天数
}
if(sum_day+M>N)
{
printf("-1\n");
return 0;
}
int result=-1;
while(left<=right)
{
int mid=(left+right)>>1;
if(Judge(mid)==true)
{
right=mid-1;
result=mid;
}
else
left=mid+1;
}
printf("%d\n",result);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: