PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000193. Số thao tác ít nhất để đưa X về 0 (Minimum Operations to Reduce X to Zero)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

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 a1,…,ana_1,\ldots,a_n and a positive target XX, one operation removes either end element of the current array and subtracts its value from XX. Later operations use the remaining array. Minimize the number of operations needed to make XX exactly zero. Removing the whole array is allowed. Print −1-1 if impossible.

Input

The first line contains n,Xn,X. The second contains nn integers aia_i.

Output

Print the minimum number of operations, or −1-1 if impossible.

Subtasks

  • Subtask 1 (20 points): 1≤n≤5001\le n\le 500; 1≤ai≤1041\le a_i\le10^4; 1≤X≤1091\le X\le10^9.
  • Subtask 2 (30 points): 1≤n≤1000001\le n\le 100000; 1≤ai≤1041\le a_i\le10^4; 1≤X≤1091\le X\le10^9; ai=a1a_i=a_1 for all ii.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 1≤ai≤1041\le a_i\le10^4; 1≤X≤1091\le X\le10^9.

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.