2026-02-10 22:47:47 +09:00

34 lines
535 B
Python

import sys
import heapq
input = sys.stdin.readline
jewels = []
bags = []
N, K = map(int, input().split())
for _ in range(N) :
m, v = map(int, input().split())
jewels.append((-v, m))
for _ in range(K) :
bags.append(int(input()))
jewels.sort(key = lambda x: (x[1], x[0]))
bags.sort()
pq = []
ans = 0
idx = 0
for bag in bags :
while idx < N and jewels[idx][1] <= bag :
heapq.heappush(pq, jewels[idx][0])
idx += 1
if pq :
cost = heapq.heappop(pq)
ans -= cost
print(ans)