LearnPy Hub
← Arena
AdvancedRating 1800

At Most k Trades

With prices on n days, make at most k buy-then-sell trades (finish one before starting the next). Print the largest total profit.

Input

n and k, then n prices.

Output

One integer.

Sample 1

Input
3 2
2 4 1
Output
2

Sample 2

Input
6 2
3 2 6 5 0 3
Output
7
solution.py
Output appears here
At Most k Trades · Python Arena problem · LearnPy Hub