이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
import sys
c = int(2E9)
raw_input = sys.stdin.readline
range = xrange
N, K = map(int, raw_input().split())
T = [0 for i in range(N) ]
for i in range(N):
T[i] = int(raw_input())
''' O(NK) code
DP = [ [c]*K for i in xrange(N) ]
DIFF = [0]*N
for i in range(1, N):
DIFF[i] = T[i] - T[i-1]
for i in range(K):
DP[0][i] = 1
for i in range(1, N):
DP[i][0] = T[i] + 1 - T[0]
for j in range(1, K):
DP[i][j] = min(DP[i-1][j] + DIFF[i],
DP[i-1][j-1] + 1)
print min(DP[N-1])
'''
#### GREEDY APPROACH
#### If we turn-on stove k times,
#### Then, it is equivalent to turn-off stove on k-1 intervals
M = []
for i in range(N-1):
M.append(T[i] - T[i+1])
M.sort()
ans = T[-1] - T[0] + K
for i in range(K-1):
ans += M[i]
print ans
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |