PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#BS0000310. Đưa mảng về toàn số 0 II (Zero Array Transformation II)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Đưa mảng về toàn số 0 II (Zero Array Transformation II)
Zero Array Transformation II
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
Given a nonnegative array of length n and q operations , each operation permits independently decreasing each value in the inclusive zero-based interval by any integer from 0 to v_j. Find the shortest prefix of operations that can make the entire array zero, or report failure.
Input
First line: n,q. Second line: n integers . Next q lines contain using zero-based indices.
Output
Print the smallest k, or -1 if impossible.
Subtasks
- Subtask 1 (20 points): ; ; ; .
- Subtask 2 (30 points): ; ; ; ; .
- Subtask 3 (50 points): ; ; ; .
Examples
Input 1
3 3
2 0 2
0 2 1
0 2 1
1 1 3
Output 1
2
Explanation
Decrease both endpoints by one in each of the first two operations. After only the first operation, both endpoints still need one.
Input 2
2 1
0 0
0 1 5
Output 2
0
Explanation
The array is already zero.