LogiQuest

257. Stock Trading with K Transactions

HardDynamic Programming

Print the maximum profit with at most k buy–sell transactions (you must sell before buying again).

Input format

Line 1: n k Line 2: n prices

Output format

The maximum profit.

Example 1

Input

3 2
2 4 1

Output

2

Example 2

Input

6 2
3 2 6 5 0 3

Output

7

Constraints

1 ≤ n ≤ 1000 1 ≤ k ≤ 100

Read input from standard input and print the answer to standard output. Your code is judged against 4 test cases, most of them hidden.

Loading the MyLoginn IDE…