#1038:01 Backpack time limit:20000msSingle Point time limit:1000msMemory Limit:256MB
Describe
Say then last week's story, Small hi and small ho struggled to finally get the vast number of lottery tickets! And now, it's time for Little Ho to pick up the reward!
Small Ho now has Mticket, and the prize area has n prizes, respectively labeled 1 to N, of which the first prize of the need (i) to redeem the lottery, but also can only redeem once, in order to make the hardwon lottery not wasted, small ho to each of the prizes are assessed points, The score value of the prize for the Iitem is value (i), which indicates his preference for the prize. Now he wants to know which prizes can be changed by virtue of the lotteries in his hand, so that the sum of the prizes ' preferences can be maximized.
Tip One: Reasonable abstract problem, define state is the most critical step in dynamic planning
Tip One: Reasonable abstract problem, define state is the most critical step in dynamic planning
Little Hi thought, and asked: "What are you going to do?" ”
"Enumerating the possible options for 2^n, first calculating the sum of the lottery tickets they need, and calculating the sum of their preference values in sums not exceeding m, and counting an optimal scheme, the maximum value of value!" "The naïve little Ho gave an equally naïve plan.
"The white teach you to dynamically plan ..." Little hi can't help but think about how to use dynamic programming to solve this problem! ”
"Good!" Let me recall ... Dynamic planning requires two kinds of properties: duplicate subproblem and noeffectbut ... How can I not see this question how to set on these two properties ah, what kind of a subproblem? Little Ho thought, said.
"Don't worry, you want to know what the subproblem is?" So first of all, we have to find a way to abstract the problem we are facing now! "
Little Ho bowed his head and thought for a minute, and said, "Well, I think, if we use Best (x) to indicate the highest value that I can get when I have an Xticket in hand, then our question is what is better (M)?" ”
"You define this, there is no way to ' solve Best (M) ' Such a problem to break down into different subproblems of the OH ~" Little Hi laughed: "Whether or not, beginners are often difficult to figure out how to good abstract problems, this time let me tell you ~
"Just know suspense ~ say!" ”
Small hi smiled, continue to say: "This problembest (M) of the solution, in fact, asked is each prize whether the choice is it?" So when you go through this 2^n option, do you determine whether each prize is selected in order? ”
Yes ”
Small hi continued: "Then we may as you traverse the situation, but make a small change, with Best (i, X) indicates whether the first I item is selected, the currently selected items of the sum of the required lottery number is not more than X, can obtain the highest preference value and. "
"It does sound like a search, and when you search, you decide whether each item is selected by the number from small to large, and maintain the sum of the required lotteries for a currently selected item." "Suddenly little Ho seems to think of something:" Eh, so this best (I, X) is actually the same as the previous experience of the digital triangle maze problem in the memory search is very similar? ”
"Yes, the memory search is really very similar to dynamic programming, or it can be said that the principle they use to solve the problem is the same ." "Little Hi replied."
"So I think ... So the final answer is actually best (N, M), right? Little Ho came to the conclusion.
Yes This time we can call the best (N, M) solution for our problem! "Little hi Happy Way."
"Then what about the subproblem?" ”
Little hi waved little ho not to worry: "Subproblems usually take the problem into a number of parts, sometimes evenly divided, and sometimes only on the scale of the reduction of one." For example, here we might consider the last decision on the question of Best (n, M)whether or not the nth prize is selected: First, if you choose the Nth Prize, of course, first of all, to ensure that the number of tickets required for the nth item does not exceed M, we can know that the best benefit of this scheme is N1, M Need (n)) + value (n). ”
"Secondly, if we do not select the nth prize, we can know that the best benefit for this scheme is the preferred one (N1, M)." "Little Hi Dayton," continued the way: "Because the nth prize only chooses and does not choose two kinds of possibilities, we can then know best (n, M) = Max{best (N1, Mneed (n)) + value (n), Best (N1, M)}! ”
Yes "Little Ho Way:" The same principle, for any i>1, J, we can know best (i, J) =max{best (I1, Jneed (i)) + value (i), Best (I1, J)}! "
"Good place to stay!" So do you want to check to see if the definition of this problem has two properties that are required for dynamic planning? ”
Small Ho thought, decided one article: "First look at the duplicate subproblemThis is the reason why dynamic planning is more efficient than search, if the last four prizes for the required lottery is 1, the preference value is 1, the required lottery is 2, the value of 2, the required lottery is 3, the value of 3, the required lottery is 4, Preference value is 4 of the four prizes, then whether the choice of 1, 4 or 2, 3, will require the best (N4, M5) Such a subproblem, and this subproblem only need to solve once to be able to calculate, so the nature of repeating subproblem is satisfied. ”
"Yes," said the next. ”
Second, see no validity ... Similarly, if there is a need for a lottery ticket of 1 per cent, the preferred value is 1, the required lottery is 2, the preferred value is 2, the required lottery is 3, the preferred value is 3, the required lottery is 4, 4 of the prizes are preferred four, then whether it is selected 1th and 4th, or 2nd and 3rd, they have the required lottery number is 5, The sum of the preference values is 5. So I just need to know best (4, 5) =5 is enough, why is it equal to 5 for me no difference, no impact on subsequent decisions. This is no effect, so it is satisfying to think about it.
"The next thing to consider is how to use Best (i, J) =max{best (I1, Jneed (i)) + value (i), Best (I1, J)} to solve each of the best (I, J) ~" Little Hi Way: " This Part I'll tell you directly, we define a problem a depends on another problem B when and only when solving a process need to know the value of B beforehand, then we can easily find that best (i, j) is dependent on best (i1, Jneed (i)) and Best (I1, J) Two questions , which means that the two problems are solved before best (i, J) ~ "
"So we just have to follow the order of I from small to large, in such a way to calculate, on it!" Little Ho interposed.
"You're stealing my lines again!"
Hint two: said to reduce the time consumption, let's see how to reduce the space consumption
Hint two: said to reduce the time consumption, let's see how to reduce the space consumption
Say then small ho figure out the calculation method, is buried bitter write code, in the side to see him write code of small hi is in see not go down, decided to point pointing little ho: "Little ho Ah!" ”
"What's the matter?" "Small ho eyes staring at the screen, Wangdu did not look at a small hi."
"Do you now need to open a twodimensional array of n * M size best to record the best value of the solution?" ”
Little Ho finally had a little reaction and looked up and said, "Yes, what's wrong?" “
"I have a way not to open so much space oh ~" Small hi smiled: "But I just don't tell you!" ”
"Eh, don't do that, I'll take you to ice cream!" Small Ho heard on the urgent, hurriedly made a reward.
"A joke ~" Little hi is also casually amused the child did not continue: "You think, in I is already more than 10, best (5, j) Such a value is also useful?" ”
"It's no use ... You mean, I don't have to save all the best (I, J) in memory, and no value can be saved ... That is, in fact, just open an array of 2*m size, and then the way to calculate the back and forth, is it possible? ”
"Yes!" But can be less oh ~ Let me write this program, I just need to open a onedimensional msize array can be "small hi confidently said:" You think, if I follow J from M to 1 in the order, that is, in the reverse order to calculate. In addition, according to our state transfer equation, it can be clearly concluded that if the state (IA, JA) depends on the state (IB, JB), then there must be IA = ib+1, ja>=jb. So it's not hard to come to a conclusion: when I was calculating best (I, j), because the J+1..M states had already been computed, it meant that it would be i1, K, and K=j. M these values are uselessall the values that depend on them have been calculated. So that their original storage space can be used to store other things, so I do not imitate the best (I, J) the value of Best (I1, J) in the original position, like this. ”
"The original is still possible!" Such a processing, not only the space complexity is a lot less, code is also very goodlooking! "Little Ho Happy Road."
"Then you're not going to write?" ”
Input
Each test point (input file) has and has only one set of test data.
The first act of each set of test data is two positive integers n and m, indicating the number of prizes, and the number of lotteries in small Ho's hands.
The next n lines describe each line describing a prize, where I behaves two integers need (i) and value (i), meaning as described earlier.
Test Data Assurance
For 100% of data, the value of n does not exceed the value of 500,m no more than 10^5
For 100% of data, need (i) does not exceed 2*10^5, value (i) does not exceed 10^3
Output
For each set of test data, output an integer ans that represents the total preference value that small ho can get.


Sample input


5 1000144 990487 436210 673567 581056 897


Sample output


2099