CodeForces - 732 D - Exams 二分贪心

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

Copy

7 2
0 1 0 2 1 0 2
2 1

Output

Copy

5

Input

Copy

10 3
0 0 1 2 3 0 2 0 1 2
1 1 4

Output

Copy

9

Input

Copy

5 1
1 1 1 1 1
5

Output

Copy

-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天里已分配好某一天可考某个科目

m个科目的复习时间分别为.......

输出至少需要多少天能考完全部科目

因为数据量较大,故需要使用二分优化

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 const int maxn = 1e6 + 5;
 5 int d[maxn],a[maxn];//考试安排,第i科目所需的复习时间
 6 int vis[maxn];//i科目是否已安排复习
 7 int n,m;
 8 int check(int x)
 9 {
10     int needtime = 0;//复习完所需要的天数
11     int num = 0;//复习完的科目
12     memset(vis,0,sizeof(vis));
13     for(int i = x;i > 0;i--)
14     {
15         if(!vis[d[i]] && num != m && d[i])//i科目未复习,
16         //复习完的科目没有达到考试的科目,第i天可以考试
17         {
18             needtime += a[d[i]];
19             num ++;
20             vis[d[i]] = 1;
21         }
22         else if(needtime)
23         {
24             needtime--;
25         }
26     }
27     if(needtime <= 0 && num == m)//在x天内复习完了所有科目
28     return 1;
29     return 0;
30 }
31 int main()
32 {
33     while(cin >> n >> m)
34     {
35         for(int i = 1;i <= n;i++)
36         scanf("%d",&d[i]);
37         for(int i = 1;i <= m;i++)
38         scanf("%d",&a[i]);
39         int ans = -1;
40         int l,r,mid;
41         l = 1,r = n;
42         while(l <= r)
43         {
44             mid = (l + r) >> 1;//相当于/2
45             if(check(mid))
46             {
47                 r = mid - 1;
48                 ans = mid;
49             }
50             else
51             l = mid + 1;
52         }
53         printf("%d\n",ans);
54     }
55     return 0;
56 }

猜你喜欢

转载自www.cnblogs.com/lu1nacy/p/9972263.html