EPS/29-01-24/improve_it.py
2024-01-30 15:04:05 +01:00

20 lines
575 B
Python

def main():
N, M = [int(i) for i in input().split()]
values = [[int(i) for i in input().split()] for _ in range(N)]
values.append([0, 0])
# best buy without sell at month 0
bests = {0: values[0][0]}
for i in range(len(values)):
for j in range(i-M, i):
if j in bests.keys():
if i not in bests.keys():
bests[i] = bests[j] - values[j][i-j] + values[i][0]
else:
bests[i] = min(bests[j] - values[j][i-j]+ values[i][0], bests[i])
print(bests[N])
main()