PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#BS0000346. Hoàn thành kỳ thi sớm nhất (Exams)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Hoàn thành kỳ thi sớm nhất (Exams)
Exams
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
An exam period has n days and m subjects. On day i an exam in subject is available if ; zero means no exam. A day can be used for one exam, preparation for one subject, or rest. Subject j requires preparation days before its exam; they need not be consecutive. Pass each subject exactly once. Find the earliest day by which all subjects can be passed, or -1.
Input
First line: . Second line: integers . Third line: integers .
Output
Print the earliest completion day, or -1.
Subtasks
- Subtask 1 (20 points): ; ; ; .
- Subtask 2 (30 points): ; ; ; each positive subject occurs at most once in d.
- Subtask 3 (50 points): ; ; .
Examples
Input 1
7 2
0 1 0 2 1 0 2
2 1
Output 1
5
Explanation
Study subject 1 on days 1 and 2, subject 2 on day 3, take exams 2 and 1 on days 4 and 5. At least five days are necessary.
Input 2
5 1
1 1 1 1 1
5
Output 2
-1
Explanation
Five study days and one exam day are necessary.