In stock market , a person buys a stock and sells it on some future date. Given the stock prices of N days in an form of an array A[ ] and a positive integer K, find out the maximum profit a person can make in atmost K transactions. A transaction is equivalent to (buying + selling) of a stock and new transaction can start only when the previous transaction has been completed.
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. The first line of each test case contains a positve integer K, denoting the number of transactions.The second line of each test case contains a positve integer N, denoting the length of the array A[ ].The third line of each test case contains a N space separated positive integers, denoting the prices of each day in the array A[ ].
Print out the maximum profit earned by the person.No profit will be equivalent to 0.
1 <= T <= 100
0 < K <= 10
2 <= N <=30
0 <= A[ ] <= 1000
10 22 5 75 65 80
20 580 420 900
100 90 80 50 25
Output 1: Trader earns 87 as sum of 12 and 75 i.e. Buy at price 10, sell at 22, buy at 5 and sell at 80
Output 2: Trader earns 1040 as sum of 560 and 480 i.e. Buy at price 20, sell at 580, buy at 420 and sell at 900
Output 3: Trader cannot make any profit as selling price is decreasing day by day.Hence, it is not possible to earn anything.
If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.