CF732D.Exams

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 a__i — 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 a__i 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.

瓦西里有一段为期 nn 天的考试期。他需要通过 mm 门科目的考试,各科目编号为 11 到 mm。

对于每一天,我们知道当天可参加哪一门(共 mm 门中的一门)科目的考试;也可能某天无法参加任何考试。任意一天最多只能参加一场考试。

每天,瓦西里有三种选择:

  • 参加当天安排的考试(这将占用一整天);
  • 全天复习某一门科目的考试;
  • 休息。

对于每门科目 ii,瓦西里知道一个整数 aia_i —— 即他为通过第 ii 门考试所需提前复习的天数。瓦西里在复习过程中可以随时切换复习科目,不要求连续 aia_i 天专门复习第 ii 门考试;他可以以任意顺序混合安排各科目的复习。

你的任务是:确定瓦西里通过全部考试所需的最少天数;若不可能全部通过,则判定为不可能。每门考试必须且仅能通过一次。

输入格式

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 _d_1, _d_2, ..., d__n (0 ≤ d__i ≤ m), where d__i is the number of subject, the exam of which can be passed on the day number i. If d__i equals 0, it is not allowed to pass any exams on the day number i.

The third line contains m positive integers _a_1, _a_2, ..., a__m (1 ≤ a__i ≤ 105), where a__i is the number of days that are needed to prepare before passing the exam on the subject i.

第一行包含两个整数 nn 和 mm(1≤n,m≤1051 \leq n, m \leq 10^5)—— 分别表示考试期的天数和科目的数量。

第二行包含 nn 个整数 d1,d2,…,dnd_1, d_2, \dots, d_n(0≤di≤m0 \leq d_i \leq m),其中 did_i 表示第 ii 天可以参加考试的科目编号。若 di=0d_i = 0,则第 ii 天不允许参加任何科目的考试。

第三行包含 mm 个正整数 a1,a2,…,ama_1, a_2, \dots, a_m(1≤ai≤1051 \leq a_i \leq 10^5),其中 aia_i 表示在参加第 ii 科目的考试前所需准备的天数。

输出格式

Print one integer — the minimum number of days in which Vasiliy can pass all exams. If it is impossible, print -1.

输出一个整数——Vasiliy 通过所有考试所需的最少天数。如果不可能,输出 −1-1。

输入输出样例

  • 输入#1

    7 2
    0 1 0 2 1 0 2
    2 1

    输出#1

    5
  • 输入#2

    10 3
    0 0 1 2 3 0 2 0 1 2
    1 1 4

    输出#2

    9
  • 输入#3

    5 1
    1 1 1 1 1
    5

    输出#3

    -1

说明/提示

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.

在第一个例子中,瓦西里可以按如下方式行动:在第一天和第二天准备第 1 门考试,并于第五天通过该考试;在第三天准备第 2 门考试,并于第四天通过该考试。

在第二个例子中,瓦西里应在前四天准备第 3 门考试,并于第五天通过该考试;然后在第六天准备第 2 门考试,并于第七天通过该考试;之后,他需在第八天准备第 1 门考试,并于第九天通过该考试。

在第三个例子中,瓦西里无法通过唯一的考试,因为他没有足够的时间来准备该考试。

输入解题思路,AI测评打分。不知道怎么写?

首页