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
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
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 positive integers . For each nonempty contiguous subarray , define
There are such subarrays. Collect the sums with multiplicity, sort them in non-decreasing order, and output the -th smallest value, using 1-based positions.
Distinct subarrays with equal sums are counted separately.
Input
- Line 1 contains and . Line 2 contains positive integers .
Output
- Print the -th smallest subarray sum.
Subtasks
- Subtask 1 (20 points): ; ; .
- Subtask 2 (30 points): ; ; .
- Subtask 3 (50 points): ; ; .
Examples
Input
3 4
2 1 3
Output
3
Explanation
For , the subarray sums are . After sorting they become . The fourth is 3; equal sums from distinct subarrays are retained.