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

#PS0000154. Chọn nhiều nhất các đoạn không giao nhau có tổng cho trước (Maximum Number of Non-Overlapping Subarrays With Sum Equals Target)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Chọn nhiều nhất các đoạn không giao nhau có tổng cho trước (Maximum Number of Non-Overlapping Subarrays With Sum Equals Target)

Maximum Number of Non-Overlapping Subarrays With Sum Equals Target

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Given integers a1,…,ana_1,\ldots,a_n and KK, choose as many nonempty contiguous intervals of sum KK as possible. No two chosen intervals may share an index. Some elements may remain unused. Find the maximum number of chosen intervals.

Input

  • Line 1: integers nn and KK, the array size and target sum.
  • Line 2: nn integers a1,…,ana_1,\ldots,a_n in order.

Output

Print one integer: the maximum number of intervals.

Subtasks

  • Subtask 1 (20 points): 1≤n≤5001\le n\le 500; −10000≤aj≤10000-10000\le a_j\le 10000 for every 1≤j≤n1\le j\le n; 0≤K≤1060\le K\le 10^6.
  • Subtask 2 (30 points): 1≤n≤1000001\le n\le 100000; 0≤aj≤100000\le a_j\le 10000 for every 1≤j≤n1\le j\le n; 0≤K≤1060\le K\le 10^6.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; −10000≤aj≤10000-10000\le a_j\le 10000 for every 1≤j≤n1\le j\le n; 0≤K≤1060\le K\le 10^6.

Example 1

Input

5 2
1 1 1 1 1

Output

2

Explanation

Choose [1,2] and [3,4], each of sum 2. Every qualifying interval needs two elements, so five elements cannot supply three intervals.

Example 2

Input

7 6
-1 3 5 1 4 2 -9

Output

2

Explanation

Intervals [3,4] and [5,6] each sum to 6. The only other qualifying interval is [2,7], which overlaps both. Three disjoint intervals are impossible.