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.

题意:在满足复习够天数的条件下每天可以通过指定的一科,要么选择通过一科,要么选择复习某一科,问最少几天能全部通过。(题目中不能看出每天只能复习一科,在NOTE中才能看出每天只能复习一科)

看CF上2400多人做出来,以为可以轻松做出来,结果硬是没想出怎么做,看了题解说是二分贪心。

对1—n天进行二分check,若check(mid)==1,l=mid+1;若check(mid)==0,r=mid-1。若能通过,则最终将接近最早通过的天数。check的思路是,从mid这天开始往前,让每一科在能通过的最晚的一天通过,再从头求每科复习的天数。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define N 100005 int day[N];
int cost[N];
int n,k;
bool check(int x)
{
int dt[N];
memset(dt,,sizeof(dt));
for(int i=x;i>;i--)
if(!dt[day[i]])
dt[day[i]]=i;
int pre=;
for(int i=;i<=x;i++)
{
if(day[i]==)
pre++;
else if(dt[day[i]]!=i)
pre++;
else
{
if(cost[day[i]]>pre)
return ;
else
pre-=cost[day[i]];
}
}
for(int i=;i<=k;i++)
if(dt[i]==)
return ;
return ;
} int main()
{
while(scanf("%d%d",&n,&k)!=EOF)
{
for(int i=; i<=n; i++)
scanf("%d",&day[i]);
for(int i=; i<=k; i++)
scanf("%d",&cost[i]);
int l=,r=n,mid,ans=-;
while(l<=r)
{
mid=(l+r)>>;
if(check(mid))
{
ans=mid;
r=mid-;
}
else
l=mid+;
}
printf("%d\n",ans);
}
return ;
}

最新文章

  1. node.js中使用https请求报CERT_UNTRUSTED的问题解决
  2. VS2013,asp.net网站转换为web应用程序
  3. phpstorm编辑器智能提示框架代码
  4. uva 11324
  5. struts1标签-&lt;html:radio&gt; 使用注意事项
  6. Qt之读取配置文件
  7. ZOJ 3905 Cake(贪心+dp)
  8. jQuery官网一个关于菜单的例子
  9. ListView与CheckBox组合实现单选
  10. ORACLE 计算节假日
  11. Penalty
  12. web开发路径问题
  13. 《程序员修炼之道:从小工到专家》【PDF】下载
  14. jquery.amaran jquery提示类使用
  15. Spring教程笔记(3) Bean
  16. native和html5的通信方案
  17. Deferred Shading 延迟着色(翻译)
  18. oracel 复制A列的内容到列
  19. 4~20mA电流输出芯片XTR111完整电路
  20. GTK安装

热门文章

  1. Ubuntu 16.04安装Gufw防火墙(转)
  2. internalsviewer
  3. mysql limit具体用法
  4. NetCore发布WebApi项目到IIS服务器中
  5. 一个经典的消费者和生产者的实现(linux )
  6. [Algorithm] Determine if two strings are an anagram
  7. Java泛型解析(01):认识泛型
  8. hdu 2255奔小康赚大钱 KM算法模板
  9. 通达OA 小飞鱼老师OA工作流设计课程教学网络公开课之HTML基础(一)
  10. 如何用css约束一个层不可见