from heapq import heappop, heappush
import sys
input = sys.stdin.readline
N, M, K = map(int, input().split())
beers = []
for _ in range(K):
v, c = map(int, input().split())
beers.append([v, c])
beers.sort(key=lambda x: x[1])
def solution():
picked = []
preference = 0
for b in beers:
heappush(picked, b)
preference += b[0]
if len(picked) >= N:
if preference >= M:
return b[1]
else:
preference -= heappop(picked)[0]
return -1
print(solution())
'Algorithm > Python' 카테고리의 다른 글
[Algorithm|Python] 백준 19598번 / 99클럽 20일차 TIL (0) | 2025.02.15 |
---|---|
[Algorithm|Python] 백준 1946번 / 99클럽 19일차 TIL (0) | 2025.02.14 |
[Algorithm|Python] 백준 11399번 / 99클럽 17일차 TIL (0) | 2025.02.12 |
[Algorithm|Python] 백준 27961번 / 99클럽 16일차 TIL (0) | 2025.02.11 |
[Algorithm|Python] 백준 15686번 / 99클럽 15일차 TIL (0) | 2025.02.08 |