time limit per test1 second

memory limit per test256 megabytes

inputstandard input

outputstandard 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.

【题解】



二分枚举需要的天数。

每次枚举的时候从最后一天往前处理。

第一次遇到的可以用来考试的天数。如果没考。则一定要用来考试。

然后再往前去找哪些天要用来复习。(即转换成给每个考试找复习时间);

如果最后有一些考试不能完成(没找到足够的复习天数);

则不行。

那么再把天数往右扩大;(l = m+1);

如果可行就减少天数(r = m-1);

#include <cstdio>
#include <queue> using namespace std; const int MAXN = 1e5 + 100; int n, m;
int d[MAXN], a[MAXN],days = 0,temp[MAXN];
bool flag[MAXN];
queue <int> dl; int main()
{
//freopen("F:\\rush.txt", "r", stdin);
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
scanf("%d", &d[i]);
for (int i = 1; i <= m; i++)
scanf("%d", &a[i]),temp[i] = a[i];
int l = 1, r = n;
int best = -1;
while (l <= r)
{
int tempm = m;
for (int i = 1; i <= m; i++)
flag[i] = false;
for (int i = 1; i <= n; i++)
a[i] = temp[i];
while (!dl.empty())
dl.pop();
int m = (l + r) >> 1;
for (int i = m; i >= 1; i--)
if (!flag[d[i]] && d[i] != 0)
{
flag[d[i]] = true;
dl.push(d[i]);
}
else
{
if (!dl.empty())
{
int x = dl.front();
a[x]--;
if (!a[x])
{
dl.pop();
tempm--;
}
}
}
if (tempm!=0)//不行->所有考试都完了才算通过。
{
l = m + 1;
}
else//行,尝试更优
{
r = m - 1;
best = m;
}
}
if (best == -1)
puts("-1");
else
printf("%d\n", best);
return 0;
}

【37.50%】【codeforces 732D】Exams的更多相关文章

  1. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  2. 【35.37%】【codeforces 556C】Case of Matryoshkas

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【50.00%】【codeforces 602C】The Two Routes

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【81.37%】【codeforces 734B】Anton and Digits

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. 【21.37%】【codeforces 579D】"Or" Game

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. 【50.88%】【Codeforces round 382B】Urbanization

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. 【50.00%】【codeforces 747C】Servers

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. 【codeforces 509C】Sums of Digits

    [题目链接]:http://codeforces.com/contest/509/problem/C [题意] 给你一个数组b[i] 要求一个严格升序的数组a[i]; 使得a[i]是b[i]各个位上的 ...

  9. 【codeforces 760D】Travel Card

    [题目链接]:http://codeforces.com/contest/760/problem/D [题意] 去旅行,有3种类型的乘车票; 第一种:只能旅行一次20元 第二种:按时间计算,90分钟内 ...

随机推荐

  1. python 字符串大小写转换(不能使用swapcase()方法)

    python 3字符串大小写转换 要求不能使用swapcase()方法 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wa ...

  2. java调用C++的过程

    转自https://blog.csdn.net/yjhdxflqm/article/details/50503551 jni是java和C.C++通信的桥梁. java适合写上层的应用,C.C++适合 ...

  3. [D3] Margin Convention with D3 v4

    You can’t add axes to a chart if you don’t make room for them. To that end, the D3 community has ado ...

  4. js进阶 12-16 jquery如何实现通过点击按钮和按下组合键两种方式提交留言

    js进阶 12-16 jquery如何实现通过点击按钮和按下组合键两种方式提交留言 一.总结 一句话总结:实现按下组合键提交留言是通过给input加keydown事件,判断按键的键码来实现的. 1.如 ...

  5. 每日技术总结:filter(),Bscroll

    前言: 这是一个vue的电商项目,使用express后端提供数据. 1.filter()函数. 事情是这样的.我从数据库拿到了所有分类数据. 分类有三个等级.父类,子类,孙类这样.但它们都在同一张表里 ...

  6. Oracle以系统管理员的方式登录失败

    解决方法: 因为SYS是在数据库之外的超级管理员,所以我们在登录的时候输入sys后在输入命令:password as sysdba 就可以!例如:输入口令: m1234 as sysdba 参考文章 ...

  7. IOS自动化测试 UIAutomation

    一.通过Xcode工具编写运行测试脚本 说明:如果是在IOS模拟器上运行测试用例,需要有被测试应用的源代码才有权限把应用安装到模拟器中,当前示例中使用了自己编写的一个简单Iphone应用,大家也可以直 ...

  8. Android学习笔记之GridView的使用具体解释

    (1)创建布局代码例如以下: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android&quo ...

  9. jQuery和CSS3炫酷GOOGLE样式的用户登录界面

    这是一款使用jQuery和CSS3打造的GOOGLE样式的用户登录界面特效.该登录界面特效中,右上角的小问号和错误提示小图标使用SVG来制作.username和password输入框採用浮动标签特效. ...

  10. swift项目第五天:swift中storyBoard Reference搭建主界面

    一:StoryBoard Reference的介绍 StoryBoard Reference是Xcode7,iOS9出现的新功能 目的是让我们可以更好的使用storyboard来开发项目 在之前的开发 ...