METOXES
A stock exchange company wants to evaluate a trading practice known as the -hits strategy.
We are given the prices , , , of a stock for a period of days. The company intends to buy and sell this stock at most times during this period. Let's assume that there will be transactions, and in the -th transaction, the company will buy the stock on day and sell it on day . The time intervals [, ] do not overlap, meaning: . The total profit of the company from all transactions is obviously:
The company wants to find out how many transactions it should make and when, in order to maximize the profit . In other words, it wants to determine the value of and the boundaries of the intervals [, ].
Input
The first line of input will contain two natural numbers and : the number of days and the maximum number of transactions. The second line of input will contain integers , separated in pairs by a single space: the stock prices.
Output
The output should consist of a single line containing exactly one natural number: the maximum profit that can be achieved with at most transactions.
Constraints
Execution time limit: 1 sec.
Memory limit: 64 MB.
Examples of Input - Output Files:
1st
STDIN (metoxes.in)
10 3
12 12 7 10 15 8 3 4 8 8
STDOUT (metoxes.out)
13
Explanation of first example: In the first example, the company can achieve the maximum profit by making only transactions:
- Buying on the rd day at the price of and selling on the th day at the price of .
- Buying on the th day at the price of and selling on the th day (or the th day) at the price of .
Thus, its profit is . This is the maximum possible.
2nd
STDIN (metoxes.in)
5 2
10 9 8 7 6
STDOUT (metoxes.out)
0
Explanation of the second example: In the second example, the stock price decreases every day, so the maximum possible profit is zero if no transactions are made.
Comments