루미 알고리즘

#1652플래티넘Lieutenant Slotto

PO 2026 온라인 예선코딩출제 Joshua Andersson영어 원문이 문제가 어렵다면 — 배우는 순서 보기

영어 원문입니다. 원문의 수식·표를 글자로 옮겨 모양이 다를 수 있습니다. 정확한 내용은 원문 페이지에서 확인하세요. 원문 페이지 열기 (새 창)

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.

GroupPointsConstraints
17l_i=1
212N, T ≤ 500
324N ≤ 5000
417l_i ≤ 2
540No 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

출처 표기

출처: Programmeringsolympiaden 2026년 온라인 예선 · 원문 페이지 · 이용 조건 CC BY-SA · LaTeX 원문을 화면용 글로 옮겼습니다(수식·표의 모양이 다를 수 있습니다). 내용은 고치지 않았습니다.

크리에이티브 커먼즈 저작자표시-동일조건변경허락.

문제 출처: 한국정보올림피아드(KOI) (CC BY-NC-SA 4.0) · International Olympiad in Informatics (IOI) (CC BY) · Centre for Education in Mathematics and Computing (CEMC), University of Waterloo (CC BY-NC 4.0) · Programmeringsolympiaden (CC BY-SA) · CSES Problem Set (Antti Laaksonen) (CC BY-NC-SA 4.0). 루미 알고리즘은 위 단체들과 관계없는 별도의 서비스이며, 자료는 원문 그대로 싣습니다. 문제마다 출처와 이용 조건을 따로 표기합니다.