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.

题意:每天可以准备任意一个科目或者通过一个科目,最少在第几天完成,全部科目;

思路:二分答案,o(n)check(我的zz mlog(m));

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define esp 0.00000000001
const int N=1e5+,M=1e6+,inf=1e9;
int d[N];
int a[N];
int n,m;
vector<int>v[N];
int deep[N];
int flag[N];
int check(int x)
{
for(int i=;i<=m;i++)
{
if(v[i].size()==)
return ;
int pos=upper_bound(v[i].begin(),v[i].end(),x)-v[i].begin();
if(pos==)
return ;
deep[i]=v[i][pos-];
}
for(int i=;i<=m;i++)
flag[deep[i]]=;
int st=;
for(int i=;i<=x;i++)
{
if(flag[i])
{
st-=a[d[i]];
if(st<)
return ;
}
else
st++;
}
return ;
}
int main() {
scanf("%d%d",&n,&m);
for(int i = ; i <= n; ++i) scanf("%d",&d[i]),v[d[i]].push_back(i);
for(int i = ; i <= m; ++i) scanf("%d",&a[i]);
int st=;
int en=n;
if(check(n)==)
return puts("-1");
for(int i=;i<=m;i++)
flag[deep[i]]=;
int ans=-;
while(st<=en)
{
int mid=(st+en)>>;
if(check(mid)) en=mid-,ans=mid;
else st=mid+;
for(int i=;i<=m;i++)
flag[deep[i]]=;
}
printf("%d\n",ans);
return ;
}

Codeforces Round #377 (Div. 2) D. Exams 二分的更多相关文章

  1. Codeforces Round #377 (Div. 2) D. Exams(二分答案)

    D. Exams Problem Description: Vasiliy has an exam period which will continue for n days. He has to p ...

  2. Codeforces Round #377 (Div. 2) D. Exams

    Codeforces Round #377 (Div. 2) D. Exams    题意:给你n个考试科目编号1~n以及他们所需要的复习时间ai;(复习时间不一定要连续的,可以分开,只要复习够ai天 ...

  3. Codeforces Round #377 (Div. 2) D. Exams 贪心 + 简单模拟

    http://codeforces.com/contest/732/problem/D 这题我发现很多人用二分答案,但是是不用的. 我们统计一个数值all表示要准备考试的所有日子和.+m(这些时间用来 ...

  4. Codeforces Round #377 (Div. 2)D(二分)

    题目链接:http://codeforces.com/contest/732/problem/D 题意: 在m天中要考k个课程, 数组a中有m个元素,表示第a[i]表示第i天可以进行哪门考试,若a[i ...

  5. Codeforces Round #377 (Div. 2)A,B,C,D【二分】

    PS:这一场真的是上分场,只要手速快就行.然而在自己做的时候不用翻译软件,看题非常吃力非常慢,还有给队友讲D题如何判断的时候又犯了一个毛病,一定要心平气和,比赛也要保证,不要用翻译软件做题: Code ...

  6. Codeforces Round #377 (Div. 2) A B C D 水/贪心/贪心/二分

    A. Buy a Shovel time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  7. Codeforces Round #274 (Div. 1) A. Exams 贪心

    A. Exams Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/problem/A Des ...

  8. Codeforces Round #543 (Div. 2) F dp + 二分 + 字符串哈希

    https://codeforces.com/contest/1121/problem/F 题意 给你一个有n(<=5000)个字符的串,有两种压缩字符的方法: 1. 压缩单一字符,代价为a 2 ...

  9. Codeforces Round #377 (Div. 2) E. Sockets

    http://codeforces.com/contest/732/problem/E 题目说得很清楚,每个电脑去插一个插座,然后要刚好的,电脑的power和sockets的值相同才行. 如果不同,还 ...

随机推荐

  1. 所谓完整的linux系统包括哪些部分呢?【转】

    本文转载自:http://www.eeskill.com/article/index/id/1358.html 简介:三部分:bootloader.linux kernel(linux内核).root ...

  2. TM1680的I2C的51例程

    搞到一个例程,虽然是51的, 但是我的ST版本也是用的模拟I2C, 分析一下吧: unsigned char i=0;TM1680start();  //I2C起始信号 TM1680SendByte( ...

  3. asp显示记录条数

    Sql = "select * from xin126 where ID=" & id Rs.Open Sql,Conn,1,1 %> 共有<strong st ...

  4. CSS修改方法

    1.在IE中,大部分情况下默认margin = 0px padding = 0px,但在Chrome中需要写明 在css.css文件开头加上(要加在最上面) html,body,ul,ol,li,ta ...

  5. Binary Tree Level Order Traversal II

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  6. 【css】多行文字图片混排容器内垂直居中解决方案

    css:   .box-wrap{display:table;width:200px;height:200px;*position:relative;}/*最外边的容器,需固定宽高*/ .box-ha ...

  7. PHP高级架构技术年度大盘点

    2015年1月的最后一个周末,上海的冬季虽不如北方的雪窖冰天,但腊月的寒风也足以让人猫 在家中不愿出门.可是,在华美达酒店的一个会议室中,却人声鼎沸.春意融融,第三期商派技术沙龙正在火热进行,本期沙龙 ...

  8. 百度地图瓦片原理 | 百度map使用教程

    百度地图瓦片原理: http://blog.csdn.net/mygisforum/article/details/22997879 百度map使用教程: http://www.myexception ...

  9. [ios]离屏渲染优化

    原文链接:https://mp.weixin.qq.com/s?__biz=MjM5NTIyNTUyMQ==&mid=2709544818&idx=1&sn=62d0d2e9a ...

  10. [Unity3D]unity3d5.0简单的调用摄像头

    Unity3D中新建一个工程,加一个Plane,新建一个C# 脚本,将这个脚本添加到Plane上,调用摄像头.(如果显示的图片居然是翻转的,Plane的Rotation 值就可以了) 以下是脚本内容: ...