P25459 [AtCoder] Sushi That Only Wants to Be Eaten for the Toppings
提高
题目描述

Problem Description

Takashi, who loves sushi, has come to the rotating sushi chain store called "Nico Nico Sushi". Takashi can gain happiness by eating sushi. However, Takashi is currently on a low-carbohydrate diet, and he gains more happiness by leaving the rice and eating only the toppings than by eating the sushi normally.

When Takashi sits down, $ N $ plates of sushi will come by in order, and he can take as many of his favorite sushi plates as he wants. However, he can only take the sushi in the order they come, and he can only take the next sushi after finishing the previous one. Here, finishing a sushi means either eating it normally or leaving the rice and eating only the toppings. If Takashi eats the $i\ (1≦i≦N)$th sushi by leaving the rice, he gains happiness $ X_i $, and if he eats it normally, he gains happiness $ Y_i $.

The table has space for up to $ M $ plates when arranged side by side, and Takashi places the plates here each time he finishes eating a sushi. He can stack any number of plates on top of the plates already placed. However, since the plates with only the toppings eaten still have rice on them, he cannot stack another plate on top of these. Also, he cannot place plates under the already placed plates. Once there is no more space to place plates, he cannot eat any more sushi.

Please answer the maximum total happiness Takashi can gain.

Input Format

The input is given from the standard input in the following format:

$ N $ $ M $ $ X_1 $ $ Y_1 $ $ X_2 $ $ Y_2 $ $ : $ $ X_N $ $ Y_N $

Output Format

Output the maximum total happiness Takashi can gain in one line. Make sure to output a newline at the end.

Sample Input and Output

Sample Input #1

4 1
5 2
5 3
100 50
5 1

Sample Output #1

105

Sample Input #2

5 2
5 2
100 50
5 3
5 1
100 3

Sample Output #2

206

Sample Input #3

4 5
280 101
456 404
789 707
1000 999

Sample Output #3

2525

Notes/Hints

Constraints

  • $ 1≦N≦10^5 $
  • $ 1≦M≦10^5 $
  • $1≦Y_i\ \lt\ X_i≦1,000$

Sample Explanation 1

Eating the 1st and 2nd sushi normally and only the toppings of the 3rd sushi results in a happiness of $ 105 $, which is the maximum. Note that it is okay to not take the 4th sushi, as shown in this case.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
4 1 5 2 5 3 100 50 5 1
样例输出
105