pdd第三题,购物券问题,思路是先让价格最小的商品划掉能减的最大的券,这个思路有问题吗?样例能过,但提交zero
# -*- coding=utf-8 -*-
import heapq
def get_min_money(prices, heap_max):
res = 0
while prices:
goods = heapq.heappop(prices) # 价格最小的商品
i = 0
if goods < heap_max[i][0]:
res += goods
else:
max_dis = [0, 0]
max_dis[-1] = -heap_max[i][-1]
i += 1
# 找到价格最小的商品能用的最大的券
while i <= len(heap_max):
if goods >= heap_max[i][0]:
dis = -heap_max[i][-1]
if dis > max_dis:
max_dis = [i, dis]
i += 1
else:
break
heap_max.pop(max_dis[0])
heapq.heapify(heap_max)
res = res + goods - max_dis[-1]
return res
if __name__ == "__main__":
n, m = map(int, input().split())
prices = list(map(int, input().split()))
heap_max = []
for i in range(m):
temp = list(map(int, input().split()))
temp[-1] = -temp[-1]
heap_max.append(temp)
heapq.heapify(heap_max)
heapq.heapify(prices)
print(get_min_money(prices, heap_max))
#拼多多#