Can someone explain how to approach solution for this problem(http://codeforces.net/contest/366/problem/C). Any help regarding solving 2d dp problems will be helpful. Any good problems for understanding 2d DP or tutorials will also be helpful. Thanks in advance.
To make the solution simpler:
we notice that the ratio must equal K, that means the sum of tastes must be of the form X.K and the sum
of the calories must equal X, so ratio = X.K / X = K , so if we multiplied all calories values by K
then the ratio must be X.K / X.K which equals 1.
and to get the ratio 1 means the nominator equals the denominator which means nominator-denominator=0.
let dp[i][j] mean the maximum value(of the best subset) of tastes we can get from the fruits
1,2,...i where j is the difference between the nominator and the denominator of ratio of the chosen
subset. and the transitions are :
1- dp[i+1][j] = max(dp[i+1][j] , dp[i][j]) when we don't pick the item (i+1)
2- dp[i+1][j+diff] = max(dp[i+1][j+diff] , dp[i][j] + a[i+1]) where diff = a[i+1] — b[i+1] this transition when we pick the item (i+1).
so the result is dp[n][0], but because j might be negative so we make shift to the whole range of the
second dimension of the array dp by the maximum value |-10000| so the result will be dp[n][10000]