Pick elements from an array so that their sum lies within an interval

Revision en6, by sensibilityness, 2019-10-25 19:16:35

Update: My coach got back to me and sent me the constraints! $$$w_1, w_2, \ldots$$$ are the elements.

Subtask 1: $$$n \leq 20; w_i \leq 10^9; l, r \leq 10^{15}$$$

Subtask 2: $$$n \leq 40; w_i \leq 10^9; l, r \leq 10^{15}$$$

Subtask 3: $$$n \leq 80; w_i, l, r \leq 10^5$$$

Subtask 4: $$$n \leq 200000; w_1 = w_2 = \ldots = w_n \leq 10^9; l, r \leq 10^{15}$$$

Subtask 5: $$$n \leq 200000; w_i = i; l, r \leq 10^{15}$$$

Subtask 6: $$$n \leq 200000; w_i, l, r \leq 10^{15}; r - l \geq \max w_i - \min w_i$$$

Update: A helpful user has sent me an online judge link. I can start researching from here. Thanks a lot! Unfortunately, the trollish attitude of downvoters is something I have to put up with. It's a permanent aspect of Codeforces.

Edit: The constraints weren't included in the original problem statement! My coach and I failed to solve this problem. I have two RAR archives of test cases for you to judge the validity of your solution. Any suggestions are welcome.

Given an array of $$$n$$$ positive integers and an interval $$$[l, r]$$$, you need to pick some elements from this array so that their sum lies within the interval $$$[l, r]$$$.

Input:
The first line contains three positive integers $$$n$$$, $$$l$$$ and $$$r$$$. The second line contains the elements of the array.

Output:
Print one line containing an integer denoting the number of elements chosen and another line containing a space-delimited list of elements sorted in ascending order.

Note: If there are multiple solutions, print any of them.

Sample test case:

Input:

7 80 100
10 20 30 40 50 60 70

Output:

3
20 30 40

Edit (to address this comment): This output is valid as well:

2
40 60
Tags #dynamic programing

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en6 English sensibilityness 2019-10-25 19:16:35 2 Tiny change: '_i, l, r \leq 10^{15}' -> '_i, l, r \geq 10^{15}'
en5 English sensibilityness 2019-10-25 19:09:24 525 Tiny change: 'ts!** $w_i$s are the e' -> 'ts!** $w_i\text{s}$ are the e'
en4 English sensibilityness 2019-10-25 18:40:03 285 I am feeling really happy.
en3 English sensibilityness 2019-10-25 18:15:36 147 Tiny change: '20 30 40\n' -> '20 30 40\n\nThis output is valid as well:\n\n 2\n 40 60'
en2 English sensibilityness 2019-10-25 17:40:23 241
en1 English sensibilityness 2019-10-25 16:36:08 743 Initial revision (published)