가야 할 거리와 현재 가진 연료의 양, 주유소의 위치와 주유소가 가지고 있는 연료의 양을 입력받고 가야 할 거리를 모두 가려면 주유소에 최소한 몇 번 방문해야 하는지 출력하는 문제입니다.
import heapq
import sys
N = int(sys.stdin.readline())
arrive = True
count = 0
heap = []
station = [[0, 0]]
for i in range(N):
a, b = map(int, sys.stdin.readline().split())
station.append([a, b])
station.sort()
L, P = map(int, sys.stdin.readline().split())
for i in range(1, N+2):
if i < N+1:
P -= station[i][0]-station[i-1][0]
else:
P -= L-station[i-1][0]
while P < 0:
if not heap:
arrive = False
break
P -= heapq.heappop(heap)
count += 1
if i < N+1:
heapq.heappush(heap, -station[i][1])
if arrive:
print(count)
else:
print(-1)