您的位置:首页 > 其它

CodeForces 732D Exams (思维+set乱搞)

2017-08-23 23:07 330 查看
题目链接:http://codeforces.com/problemset/problem/732/D点击打开链接

D. Exams

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard 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.

考试 一天只能考一科 每一科都需要时间准备 问能通过所有科目的最短时间

我们只需要记录 能够完成通过所有科目 这一情况即可

我们直接看满足题意的情况而不去想中间怎么考试的过程 

而满足这一情况必须具备两个

1.已经过了足够多的天数将所有科目都复习完了 (注意需要加上考试时间 考试也占一天)

2.所有的可行考试科目都出现过

这样 我们在所有考试科目都出现过之后 再记录那些有考试的天数

计算复习完所有科目需要的天数 然后二分找最近的考试天数 就能找到通过所有考试的那一天

但是这样思考有一个漏洞 

如果出现的考试时间 根本没办法复习完 就不能算这个考试时间出现过

因此我们还需记录数据所给的每一天的信息 当给完每一科需要复习的天数后 再判断哪些考试时间是可行的 

依照上面的条件判断二分查找答案

#include <bits/stdc++.h>
using namespace std;
set<long long int > s;
set<long long int > day;
set<long long int > ::iterator it;
long long int num[111111];
long long int numm[111111];
int main()
{
long long int n,m;
long long int sum=0;
scanf("%lld%lld",&n,&m);
for(long long int i=1;i<=n;i++)
{
long long int mid;
scanf("%lld",&mid);
num[i]=mid;
}
for(long long int i=1;i<=m;i++)
{
long long int mid;
scanf("%lld",&mid);
numm[i]=mid;
sum+=mid;
}
for(long long int i=1;i<=n;i++)
{
long long int mid=num[i];
if(mid==0||i<=numm[mid])
continue;
s.insert(mid);
if(s.size()==m)
day.insert(i);
}
sum+=(m);
it=lower_bound(day.begin(),day.end(),sum);
if(it==day.end())
printf("-1");
else
printf("%lld",*(it));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: