题解 | 小红闯关

小红闯关

https://www.nowcoder.com/practice/7ce4b75f7a304be481e73bc4dd2705a4

from heapq import heappop, heappush
n, k = map(int, input().strip().split())
costs = list(map(int, input().strip().split()))
ans = 0
heap = []
for i in range(n - 1, -1, -1):
    ans += costs[i]
    heappush(heap, -costs[i])
    if i % k == 0 and i > 0 and heap:
        ans += heappop(heap)
print(ans)


全部评论

相关推荐

评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务