题解 | 小红闯关
小红闯关
https://www.nowcoder.com/practice/7ce4b75f7a304be481e73bc4dd2705a4
import heapq#不用堆队列也可以做,但是会超时
n, k = map(int,input().split())
a = list(map(int,input().split()))
total_time = sum(a)
pq, saved_time = [], 0
for i in range(n-1,-1,-1):
if (i+1)%k==0 and pq:
max_cost = -heapq.heappop(pq)
saved_time += max_cost
heapq.heappush(pq, -a[i])
print(total_time-saved_time)
查看7道真题和解析
