영어 원문입니다. 원문의 수식·표를 글자로 옮겨 모양이 다를 수 있습니다. 정확한 내용은 원문 페이지에서 확인하세요. 원문 페이지 열기 (새 창)
Problem
Lieutenant Slotto is currently garrisoning N castles. Each castle contains riches worth l_i gold coins and is defended by g_i soldiers.
You plan to attack Slotto's castles and have a total of T soldiers at your disposal. Each soldier can be sent to attack exactly one castle. If you attack castle i with at least g_i soldiers, the attack succeeds and you can plunder its riches. If you send fewer soldiers, the attack fails.
If you distribute your soldiers optimally, how many gold coins' worth of riches can you obtain?
Input
The first line of input contains the integers N and T (1 ≤ N ≤ 2 · 10^5, 1 ≤ T ≤ 10^6), the number of castles and the number of soldiers you have.
The following N lines each describe a castle. Each of these lines contains the integers l_i and g_i (1 ≤ l_i ≤ 5, 1 ≤ g_i ≤ 10^6), the value of the riches in castle i and the number of guards defending castle i.
Output
Print an integer: the maximum total amount of riches you can seize if you distribute your soldiers optimally.
Scoring
Your solution will be tested on a set of test groups. To earn points for a group, you must pass all test cases in that group.
| Group | Points | Constraints |
|---|---|---|
| 1 | 7 | l_i=1 |
| 2 | 12 | N, T ≤ 500 |
| 3 | 24 | N ≤ 5000 |
| 4 | 17 | l_i ≤ 2 |
| 5 | 40 | No additional constraints. |
Explanation of Samples
In sample 1, an optimal solution is to send 5 troops to castle 2 (the one with l_i = g_i = 4) and 2 troops to castle 4 (the one with l_i = g_i = 2), yielding a total of 6 gold coins' worth of riches.
In sample 2, an optimal solution is to send all troops to castle 1.
In both cases, there are other allocations that give the same amount of riches.
예제
예제 1
5 7 5 6 4 4 3 5 2 2 1 3
6
예제 2
3 10 5 9 3 6 3 6
5