#BS0000347. Thời điểm sớm nhất đánh dấu mọi chỉ số I (Earliest Second to Mark Indices I)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Thời điểm sớm nhất đánh dấu mọi chỉ số I (Earliest Second to Mark Indices I)
Earliest Second to Mark Indices I
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
Two integer arrays and are given. No index of is marked initially. There are seconds, numbered from through .
At second , choose exactly one of these actions:
- Select an index () and subtract from .
- If , mark index .
- Leave the state unchanged.
Find the earliest second by which every index is marked. If this cannot be achieved within seconds, the answer is -1.
Input
The first line contains two integers , the array lengths.
The second line contains integers , the initial values.
The third line contains integers , where is the index associated with second .
Output
Print one integer: the earliest second by which every index can be marked, or -1 if impossible.
Subtasks
- Subtask 1 (20 points): ; ; for all ; for all .
- Subtask 2 (30 points): ; ; for all ; for all .
- Subtask 3 (50 points): ; ; for all ; for all .
Examples
Input 1
3 8
2 2 0
2 2 2 2 3 2 2 1
Output 1
8
Explanation
During the first four seconds, decrement each of the first two entries twice to obtain . Mark index 3 at second 5, index 2 at second 6, wait at second 7 and mark index 1 at second 8. Index 1 is unavailable earlier, proving optimality.
Input 2
2 3
0 1
2 2 2
Output 2
-1
Explanation
Index 1 is zero initially but never occurs in , so no marking opportunity exists for it.