CF 377 D. Exams

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.


总结:寻找最优位置的题目,联想用二分查找。枚举最优位置,判断即可,主要想到用二分就好做了!每次判断某个位置是否可行时,从后往前进行贪心的进行;遇到新的未通过的考试,则进入一个队列等待,遇到空的日期就把它用于当前队列最前位置的考试,直到满足m个考试。

#include<bits/stdc++.h>
#include<string.h>
#include<exception>
#include<sstream>
using namespace std;
#define LL long long int
#define rep(i,a,n) for(int i=a;i<n;++i)
#define per(i,a,n) for(int i=n-1;i>=a;--i)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define PI pair<int,int>
#define VI vector<int>
#define mem(a,t) memset(a,t,sizeof(a))
#define all(v) v.begin(),v.end()
#define sz(v) (int)sizeof(v)
const int inf=1e8;
#define N 100005
int a[N],d[N];
int num[N];
int mark[N];

bool judge(int x,int m){ //判断该位置是否可行
    mem(num,0);
    mem(mark,0);
    int cnt=0;
    queue<int>q;
    per(i,1,x+1){
        if(d[i]){
            if(!mark[d[i]]){  //
                mark[d[i]]=1;
                q.push(d[i]);
            }
            else{
                if(!q.empty()){
                    ++num[q.front()];
                    if(num[q.front()]==a[q.front()]){
                        ++cnt;
                        q.pop();
                    }
                }
            }
        }
        else{
            if(!q.empty()){
                ++num[q.front()];
                if(num[q.front()]==a[q.front()]){
                    ++cnt;
                    q.pop();
                }
            }
        }
        if(cnt==m){
            return true;
        }
    }
    return false;
}
int main()
{
    int n,m,ans;
    cin>>n>>m;
    rep(i,1,n+1) {
        scanf("%d",&d[i]);
    }
    rep(i,1,m+1) scanf("%d",&a[i]);
    int l=1,r=n,mid;
    ans=inf;
    while(l<=r){
        mid=(l+r)>>1;
        if(judge(mid,m)){
            r=mid-1;
            ans=mid;
        }
        else{
            l=mid+1;
        }
    }
    if(ans==inf) ans=-1;
    cout<<ans<<endl;
    return 0;
}


猜你喜欢

转载自blog.csdn.net/u011721440/article/details/52985989