백준 13164번: 행복 유치원 #Python

ColorlessDia·2025년 6월 18일

algorithm/baekjoon

목록 보기
577/836
from heapq import heappush, heappop

N, K = map(int, input().split())
height_list = list(map(int, input().split()))

total_cost = 0
cost_max_heap = []

for i in range(N - 1):
    difference = height_list[i + 1] - height_list[i]

    total_cost += difference

    heappush(cost_max_heap, -difference)

for j in range(K - 1):
    max_cost = -heappop(cost_max_heap)

    total_cost -= max_cost

print(total_cost)

0개의 댓글