codeforces 732 D 二分

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.

题解:

话说这题是昨天晚上柳畅神犇见我闲的慌塞给我的,然后我人生中第一次做了codeforces D题。
回到题目:
这道题的意思就是说给你一个日程表,然后0代表没有考试,其他数字代表这是第几门课,然后给你每门课需要复习的时间,问最少几天才能考完所有科目,其中每门课是有多次机会考试,复习时间可以不连续
很明显,这是让你求最优策略的,那么就很容易想到二分,但我一开始WA了2遍,晚上睡觉的时候突然想起检查答案时忘记判断复习时间只能在这天考试之前完成,然后就跪了,早上想了一会,发现没有思路,然后搜了一下题解,才发现自己是多菜,I good vegetavle a。
不过现在终于明白了,就是每次得到一个答案,然后从这个答案往前推,用贪心的思想,然后记录一个剩余复习天数,然后每次与剩余天数去最小值,同时,每可以复习一门,剩余复习天数减去这门课所需的时间,就可以了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int rc[233333], fx[233333];
bool vis[233333];
int n, m, ls;
bool check(int mm)
{
	memset(vis, 0, sizeof(vis));
	int cur = mm - 1;
	for (int i = mm; i >= 1; i--)
	{
		cur = min(cur, i-1);
		if (rc[i] && !vis[rc[i]] && fx[rc[i]] <= cur)
		{
			vis[rc[i]] = 1;
			cur -= fx[rc[i]] + 1;
		}
	}
	for (int i = 1; i <= m; i++)
		if (!vis[i])
			return 0;
	return 1;
}
int main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		scanf("%d", &rc[i]);
	for (int i = 1; i <= m; i++)
		scanf("%d", &fx[i]);
	int l = 0, r = n+1;
	while (r - l > 1)
	{
		int mid = (l + r) >> 1;
		ls = m;
		if (check(mid))
			r = mid;
		else
			l = mid;
	}
	if (r == n+1)
		r = -1;
	cout << r;
	return 0;
}

 

上一篇
下一篇