1. 程式人生 > >CodeForces - 732 D - Exams 二分貪心

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 }