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

#PS0000162. Đoạn ngắn nhất có tổng đúng K (Shortest Subarray With Sum K)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đoạn ngắn nhất có tổng đúng K (Shortest Subarray With Sum K)

Shortest Subarray With Sum K

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

Given an array of nn integers a1,a2,…,ana_1,a_2,\ldots,a_n, numbered from 1. A nonempty contiguous interval [l,r][l,r] contains all elements from ll through rr and has length r−l+1r-l+1. Find the minimum length of an interval whose sum is exactly KK. Print -1 if none exists.

Input

The first line contains two integers n,Kn,K. The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n.

Output

Print the minimum qualifying length, or -1 if none exists.

Subtasks

  • Subtask 1 (20 points): 1≤n≤5001\le n\le 500; −109≤aj≤109-10^9\le a_j\le10^9 for all 1≤j≤n1\le j\le n; −109≤K≤109-10^9\le K\le10^9.
  • Subtask 2 (30 points): 1≤n≤2000001\le n\le 200000; 0≤aj≤1090\le a_j\le10^9 for all 1≤j≤n1\le j\le n; −109≤K≤109-10^9\le K\le10^9.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; −109≤aj≤109-10^9\le a_j\le10^9 for all 1≤j≤n1\le j\le n; −109≤K≤109-10^9\le K\le10^9.

Example 1

Input

4 3
2 -1 2 3

Output

1

Explanation

Interval [4,4] sums to 3 and has length 1, the smallest possible nonempty length.

Example 2

Input

3 0
1 -1 2

Output

2

Explanation

No element is zero. Interval [1,2] sums to zero, so the shortest length is 2.