To fit the best stuff in a sack with limits on what you can pack recursion will do if memoized too but DP puts less on the stack.
You are given a classic knapsack problem: a set of elements (w_1,v_1),…,(w_n,v_n) and a capacity W. Solve
max_S⊆\[n]∑_i∈Sw_i≤W∑_i∈Sv_i.
Here, \[n] is the list of integers from 1 through n.
You are guaranteed that 0≤n≤500, 0≤w_i≤W≤1017, and 0≤v_i≤1016. Furthermore, you are guaranteed that the w_i have been "smoothed" as described in the next paragraph.
A problem instance complying with the above bounds is constructed. Then a randomizing filter is applied to the problem instance as follows: Let B be the weight of the element of maximum weight. Now the following update is applied to each weight:
w_i←min(w_i+\mboxrand(⌊.05B⌋),W)
Here \mboxrand(A) is a function that generates a uniform random integer in \[0,A−1]. This process is applied only to the w_i, not to W or any of the other values.
The first line contains the two space-separated integers n and W. Each of the next n lines contains two space-separated integers w_i and v_i. The element weights were generated as described above. First the items are chosen in compliance with all the bounds. Then the smoothing algorithm is applied. The result is what your program will receive as input.
Output one line with the value of the given knapsack problem instance.