#BS0000126. Dãy con dài nhất với giới hạn tổng (Longest Subsequence With Limited Sum)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Dãy con dài nhất với giới hạn tổng (Longest Subsequence With Limited Sum)
Longest Subsequence With Limited Sum
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
Given positive integers and queries, for each limit choose a subsequence (delete some entries, retaining the relative order of the remaining entries) with total sum at most . Find the maximum possible subsequence length for each query. The empty subsequence of length zero is allowed.
Input
The first line contains . The second line contains values . The third line contains the query limits .
Output
Print space-separated integers, the maximum subsequence length for each query.
Subtasks
- Subtask 1 (20 points): ; for , .
- Subtask 2 (30 points): ; for , .
- Subtask 3 (50 points): ; for , .
Examples
Input
4 3
4 5 2 1
3 10 21
Output
2 3 4
Explanation
For , the subsequence has sum , while the three smallest values already sum to . For , has sum ; all four sum to . For , all four values fit, with total . Therefore print 2 3 4.