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

#BS0000166. Độ dài đoạn con có tổng không nhỏ hơn ngưỡng (Minimum Size Subarray Sum)

HỌC THEO LỘ TRÌNH

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

2 lộ trình

Độ dài đoạn con có tổng không nhỏ hơn ngưỡng (Minimum Size Subarray Sum)

Minimum Size Subarray Sum

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a sequence a1,a2,…,ana_1,a_2,\ldots,a_n of positive integers and a positive target SS.

A contiguous subarray consists of positions ll through rr, where 1≤l≤r≤n1\le l\le r\le n. Its sum is al+al+1+⋯+ara_l+a_{l+1}+\cdots+a_r, and its length is r−l+1r-l+1.

Find the minimum length of a contiguous subarray whose sum is at least SS. If no such subarray exists, return 00.

Input

  • The first line contains integers n,Sn,S, the number of elements and the target sum.
  • The second line contains exactly nn positive integers a1,a2,…,ana_1,a_2,\ldots,a_n from left to right.

Output

Print one integer: the minimum length of a contiguous subarray with sum at least SS, or 0 if none exists.

Subtasks

  • Subtask 1 (20 points): 1≤n≤401\le n\le40; 1≤S≤10001\le S\le1000; 1≤ai≤1001\le a_i\le100 for every 1≤i≤n1\le i\le n.
  • Subtask 2 (30 points): 1≤n≤30001\le n\le3000; 1≤S≤1061\le S\le10^6; 1≤ai≤10001\le a_i\le1000 for every 1≤i≤n1\le i\le n.
  • Subtask 3 (50 points): 1≤n≤1051\le n\le10^5; 1≤S≤1091\le S\le10^9; 1≤ai≤1041\le a_i\le10^4 for every 1≤i≤n1\le i\le n.

Examples

Input

6 7
2 3 1 2 4 3

Output

2

Explanation

The array contains 2,3,1,2,4,32,3,1,2,4,3 and the target is S=7S=7.

  • Positions 11 through 44 give 2+3+1+2=82+3+1+2=8, with length 44.
  • Positions 33 through 55 give 1+2+4=71+2+4=7, with length 33.
  • Positions 55 through 66 give 4+3=74+3=7, with length 22.

No single element reaches 77 because the maximum array value is 44. The subarray [4,3][4,3] reaches the target with length 22, so the output is 2.