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;
}