#PS0000193. Số thao tác ít nhất để đưa X về 0 (Minimum Operations to Reduce X to Zero)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Số thao tác ít nhất để đưa X về 0 (Minimum Operations to Reduce X to Zero)
Minimum Operations to Reduce X to Zero
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
Given positive integers and a positive target , one operation removes either end element of the current array and subtracts its value from . Later operations use the remaining array. Minimize the number of operations needed to make exactly zero. Removing the whole array is allowed. Print if impossible.
Input
The first line contains . The second contains integers .
Output
Print the minimum number of operations, or if impossible.
Subtasks
- Subtask 1 (20 points): ; ; .
- Subtask 2 (30 points): ; ; ; for all .
- Subtask 3 (50 points): ; ; .
Example 1
Input
5 5
1 1 4 2 3
Output
2
Explanation
Remove 3 then 2 from the right. Neither end alone equals 5.
Example 2
Input
5 4
5 6 7 8 9
Output
-1
Explanation
Both ends exceed 4, so zero cannot be reached.
Example 3
Input
6 10
3 2 20 1 1 3
Output
5
Explanation
Remove 3 and 2 from the left and 3,1,1 from the right. Only the single 20 can be kept with sum 20.
Example 4
Input
2 5
2 3
Output
2
Explanation
Remove the entire array, whose sum is 5.