PROBLEM LINK:Author: Kanstantsin Sokal DIFFICULTY:EasyMedium PREREQUISITES:dynamic programming, greedy PROBLEM:There are $N$ castles that can be conquered by George. He has decided to conquer exactly $K$ castles during the next $K$ days, an aim he plans to achieve by conquering one castle every day for the next $K$ days. As you are the king's trusted advisor, you can choose a set of $K$ castles, that the king should conquer. After you provide a set of castles, George will choose the order in which he will conquer them. You can assume that George always chooses the order which minimizes the total number of knights used during all the conquests. Your task is to find the maximum possible total number of knights that can be used during the campaign and reached by delegating a set of $K$ castles to conquer. Also, you are not sure about the value of $K$, so you should find the optimal answer for each $K = 1, 2, ... , N$. QUICK EXPLANATION:====================== EXPLANATION:================ Now, he has to pay $A_i$ for each castle $i$ independent of which day he conqueres it on. So, the ordering depends only on $B_i$. If we think of a greedy approach we should first conquer those castles with highest $B_i$ since, as the day number increases $(j1)*B_i$ will also increase. So, this is the the strategy of king: Conquer all $K$ castles in nonincreasing order of $B_i$. Now, moving onto solving the problem. Let's solve for a particular $K$. So, we have to choose $K$ castles out of $N$ such that using king's approach maximum number of knights are used. Now, a greedy approach which selects only of basis of $A_i$ or $B_i$ would be certainly wrong. We should, by now, be thinking on terms of dynamic programming. Now, if you remember subset sum DP, we kept our states as $(i, j)$ denoting that we are solving for first $i$ elements and we need to form a sum $j$. If we try to think on similar terms here, we should keep our states as $(i, j)$ denoting that we are solving for first $i$ elements and we need to select $j$ castles to maximise knights required according to king's strategy. Now, we have to make a decision that either we select $i^{th}$ castle or not. If we select it, do we know what its going to contribute i.e. how many knights are going to be required? For that we should know on which day king will conquer this castle. Now, here is the interesting part: if we sort the initial arrays in decreasing order of $B_i$, then we'll be sure that according to king's strategy $i^{th}$ castle will be conquered on last day because all other castles that will be selected will have $B_j$ less than $B_i$. So, we first sort arrays $A$ and $B$ in decreasing order of $B$ while preserving the mutual connectedness of $A_i$ and $B_i$. After that, we define $f(i, j)$ as maximum number of knights required for king to conquer a subset of $j$ castles from first $i$ castles. Recurrences can be define easily as:
Realising base cases is very easy. So, we calculate $f(N1, i)$ for all $i = 1, 2, ..., N$ and print them. COMPLEXITY:================ PROBLEMS TO SOLVE:================ AUTHOR'S, TESTER'S SOLUTIONS:
This question is marked "community wiki".
asked 19 Sep '15, 20:15

Links for the solution not working. Please check. answered 22 Sep '15, 18:25

Cant view the tester and setters solution, please fix it. answered 21 Sep '15, 15:07

For the second case : K = 1 : 4, 6, 4, 4 3 So for K = 2, how can answer be 17. I think the answer is 19 because in day 1, select 2nd castle and in day 2, select 1st castle .So 6 + 13 = 19). Can any one explain this. answered 22 Sep '15, 17:40
