CodeForces - 732D Exams

Vasiliy has an exam period which will continue for n days. He has to pass exams on msubjects. 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.

Example

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.

题解

我们对于每门课程,只贪心地选择该门课程的最后一次考试。这样我们便有最大限度的时间去准备每一门考试。

但是这样我们只知道能不能通过。

怎么把能不能通过考试转换成最少需要多少天通过呢?

二分答案

我们检测最晚第mid天有没有通过

如果其可以通过,那么最晚第1 – mid-1天可能会通过

如果最晚第mid天通不过,那么

最晚第1-mid-1 天也一样不可能通过

二分答案处理即可

 

坑点:

如果这一天考试,那么这一天就不能用去复习~

代码:

 1 #include<cstdio>
 2 #include<cstring> 
 3 using namespace std;
 4 #include<iostream>
 5 #define cfread(x) scanf("%I64d",&x)
 6 #define cfout(x) printf("%I64d",&x)
 7 #define mian main
 8 #define min(x,y) (x<y?x:y)
 9 #define max(x,y) (x<y?y:x)
10 #define f(i,p,q,t) for(i=p;i<q;i+=t)
11 #define inf 0x3f3f3f3f
12 #define mem(x,t) memset(x,t,sizeof(x));
13 #define T true
14 #define F false
15 #define def -1*inf
16 typedef long long ll; 
17 ll n,m;
18 const int MAXN = 110000;
19 ll a[MAXN];//复习功课i需要a[i]天 
20 ll d[MAXN];//第i天可以考第d[i]门课 
21 ll t[MAXN];//复习课的时序表,肯定是复习最后考试期限最先的 
22 ll sum[MAXN];//复习时间 
23 bool v[MAXN];
24 bool dv[MAXN];
25 bool judge(int x)//前x天可不可以通过全部的课程,复杂度O(n) 
26 {
27     int num = 0;
28     memset(v,0,sizeof(v)); 
29     memset(dv,0,sizeof(dv));
30     for(int i=x;i>=1;i--){
31         if(d[i]==0||v[d[i]])//如果当天没有考试或如果已经知道第i门课的最后考试期限 
32             continue; //无视本次考试 
33         dv[i]=1;
34         t[m-num]=i;//将第i天的考试加入时序表课程加入时序表 
35         v[d[i]]=1;//标记该课程已访问 
36         num++;//课程数+1 
37         if(num==m)
38             break;
39     } 
40     if(num<m)//并不够m门课的考试
41         return false;//GG 
42     sum[0] = 0;
43     for(int i=1;i<=x;i++){
44         if(d[i]==0||dv[i]==0)
45             sum[i] = sum[i-1]+1;
46         else
47             sum[i] = sum[i-1];
48     }
49     ll ans = 0;//记录需要的时间 
50     ll day = 0;//复习到哪一天 
51     /*接下来贪心地去复习*/
52     for(int i=1;i<=m;i++){
53         day += a[d[t[i]]];//将复习本门课程所需时间加入总时间
54         if(day>sum[t[i]]){//到了考试那天本门课程还没有复习完
55             return false;//GG 
56         } 
57     } 
58     return true; 
59 } 
60 int main(){
61     cfread(n);
62     cfread(m);
63     for(int i=1;i<=n;i++)
64         cfread(d[i]);
65     for(int i=1;i<=m;i++)
66         cfread(a[i]);
67     int l = 0, r = n;
68     int ans = -1;
69     while(l<=r){//二分答案,O(log(n)) ,总复杂度 O(nlog(n))
70         int mid = (l+r)/2;
71         if(judge(mid)){
72             ans = mid;
73             r = mid-1; 
74         }
75         else{
76             l = mid+1;
77         }
78     } 
79     printf("%d",ans);
80     return 0;
81 }

 

posted @ 2018-03-09 10:18  晓风微微  阅读(375)  评论(0编辑  收藏  举报