Doubt in question

What does the overlapping tries to say. Let i, j be the index so can I use any one of i or j again or I can not use either of i or j again. And my code is not passing all test cases. I have assumed that i can pick either of i or j again but not both together.

#include <bits/stdc++.h>
using namespace std;

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);

int t;	cin>>t;
while(t--){
    // your code goes here
    int k; cin>>k;
	int n;cin>>n;
	int arr[n];
	for(int i=0;i<n;i++)cin>>arr[i];
	vector <int> profit;
	for(int i=0;i<n;i++){
		for(int j=i;j<n;j++){
			int temp=arr[j]-arr[i];
			if(temp>=0)profit.push_back(temp);
		}
	}
	sort(profit.begin(),profit.end(),greater<int>());
	int ans =0;
	for(int i=0;i<k;i++)ans+=profit[i];
	cout<<ans<<"\n";
 
}
return 0;

}

@tilakparth123 this is not the intended solution for this problem you have to use stacks and you have to solve this question in 0(n) time. Your solution is getting wrong here because you are not considering buying as a transaction. You have already considered that you have stocks and you are just finding max value.

As there can be no overlaps between transactions, so we need something to know that whether any previous transaction is pending and, if yes, what was its starting point. So we a state prev
State prev: tells the previous index where we bought a stock, (=-1 if no ongoing transaction),
range={-1,[1,n]}

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.