Gug is lazy to get out of his house, so he has tasked his AI to go grocery shopping for him. At the store, the AI has found a row of N mushrooms lined up in a row, numbered 0 to N-1 from left to right. The ith mushroom has deliciousness Di, and weight Wi grams. Some mushrooms can be rotten, and so can have negative deliciousness.
Gug wishes to buy a consecutive segment of mushrooms (or no mushrooms at all). Unlike Gug, his AI is weak, and so can only carry a maximum of X grams of mushrooms before it collapses and dies. Help Gug choose a consecutive segment of mushrooms to buy such that the sum of their deliciousness is maximised.
The first line of input will contain two integers, N and X.
The second line of input will contain N integers, representing the array D.
The third line of input will contain N integers, representing the array W.
The output should contain one line with exactly one integer, the maximum sum of deliciousness of any valid segment of mushrooms the AI can buy.
For all subtasks: 0 ≤ X ≤ sum of Wi, |Di| ≤ 106, 1 ≤ Wi ≤ 106.
Subtask 1 (8%): 1 ≤ N ≤ 300.
Subtask 2 (24%): 1 ≤ N ≤ 2000.
Subtask 3 (17%): 1 ≤ N ≤ 200000, X = sum of Wi.
Subtask 4 (19%): 1 ≤ N ≤ 200000, Di ≥ 0.
Subtask 5 (18%): 1 ≤ N ≤ 200000.
Subtask 6 (14%): 1 ≤ N ≤ 3000000.
Subtask 7 (0%): Sample Testcases.
6 20 5 -1 2 -4 6 7 3 1 4 1 9 15
8
2 4 -1 -2 1 3
0
Subtask | Score |
---|---|
1 | 8 |
2 | 24 |
3 | 17 |
4 | 19 |
5 | 18 |
6 | 14 |
7 | 0 |