AlgoUniversity
  • Go Back
Discussion
Stonks :

Author

Ayush Gangwani

Difficulty Level : Hard

Submissions : 162

Asked In : Morgan-Stanley

Marks :20

: 1 | : 0

You are given an integer array $$$prices$$$ where $$$prices[i]$$$ is the price of a given stock on the $$$i^{th}$$$ day for subsequent $$$n$$$ days, and an integer $$$k$$$.

Find the maximum profit you can achieve. You may complete at most $$$k$$$ transactions.

Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).

Input

The first line of input contains an integer $$$t$$$ $$$(1 \le t \le 100)$$$ — the number of testcases.

The first line of each testcase contains two space separated integers $$$n$$$ and $$$k$$$ $$$(1 \le k \le n \le 1000)$$$ — the number of days and the maximum number of transactions that you can perform.

The second line of each testcase contains $$$n$$$ space separated integers $$$prices[1], prices[2]... prices[n]$$$ $$$(0 \le prices[i] \le 10^{15})$$$ — the elements of the $$$prices$$$ array respectively.

It is guaranteed that the individual sums of $$$n$$$ and $$$k$$$ over all testcases does not exceed $$$1000$$$.

Output

For each test case, print a single integer — the maximum profit you can obtain in a single new line.

Example

Input
3
3 2
2 4 1
6 2
3 2 6 5 0 3
6 2
5 20 10 30 50 70
Output
2
7
75

Note

In sample test case 1, Buy on day $$$1$$$ $$$(price = 2)$$$ and sell on day $$$2$$$ $$$(price = 4)$$$, profit $$$= 4-2 = 2$$$.

In sample test case 2, Buy on day $$$2$$$ $$$(price = 2)$$$ and sell on day $$$3$$$ $$$(price = 6)$$$, profit $$$= 6-2 = 4.$$$ Then buy on day $$$5$$$ $$$(price = 0)$$$ and sell on day $$$6$$$ $$$(price = 3)$$$, profit $$$= 3-0 = 3$$$. Hence total profit $$$ = 4 + 3 = 7.$$$

You need to login to view your submissions.

You need to login to view all submissions.

Loading...

Result : Executed

Loading...

Feel something is wrong with the test cases?

Result : Accepted

Test Cases :

You need to Log In
We're glad that you want to attempt this problem!

But to Run or Submit the Problem, you need to Log In.

Continue to Log In
Challenge Submitted!

Your challenge has been submitted successfully.

You will get a response soon via WhatsApp or Email.

Challenge
Facing issue while trying to solve the problem! Don't worry, we got you covered!

Do let us know your issue.

Looks good!
Please enter your issue / feedback.

How do we get in touch with you?
Looks good!
Please enter your phone no.
Looks good!
Please enter your email address.