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

#BS0000197. Tổng đoạn con nhỏ thứ K (Kth Smallest Subarray Sum)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tổng đoạn con nhỏ thứ K (Kth Smallest Subarray Sum)

Kth Smallest Subarray Sum

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

You are given nn positive integers A1,…,AnA_1,\ldots,A_n. For each nonempty contiguous subarray [L,R][L,R], define

S(L,R)=∑i=LRAi.S(L,R)=\sum_{i=L}^{R}A_i.

There are n(n+1)/2n(n+1)/2 such subarrays. Collect the sums with multiplicity, sort them in non-decreasing order, and output the kk-th smallest value, using 1-based positions.

Distinct subarrays with equal sums are counted separately.

Input

  • Line 1 contains nn and kk. Line 2 contains nn positive integers A1,…,AnA_1,\ldots,A_n.

Output

  • Print the kk-th smallest subarray sum.

Subtasks

  • Subtask 1 (20 points): 1≤n≤501\le n\le50; 1≤Ai≤1001\le A_i\le100; 1≤k≤n(n+1)/21\le k\le n(n+1)/2.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le2000; 1≤Ai≤50001\le A_i\le5000; 1≤k≤n(n+1)/21\le k\le n(n+1)/2.
  • Subtask 3 (50 points): 1≤n≤200001\le n\le20000; 1≤Ai≤500001\le A_i\le50000; 1≤k≤n(n+1)/21\le k\le n(n+1)/2.

Examples

Input

3 4
2 1 3

Output

3

Explanation

For A=[2,1,3]A=[2,1,3], the subarray sums are 2,1,3,3,4,62,1,3,3,4,6. After sorting they become [1,2,3,3,4,6][1,2,3,3,4,6]. The fourth is 3; equal sums from distinct subarrays are retained.