#include

using namespace std;

int main() {

// your code goes here

int T;

cin>>T;

while(T–)

{

int N;

cin>>N;

long int arr[N];

int counter = 0;

long int sum = 0;

for(int i=0; i<N; i++)

{

cin>>arr[i];

//counter initially zero

//counter increses by one for one sell

if(arr[i]>counter)

{

sum = (arr[i]-counter)%1000000007+sum)%1000000007;

counter++;

}

}

cout<<sum%1000000007<<endl;

}

return 0;

}

//Question link is https://www.codechef.com/problems/CARSELL

Actually your approach is not all correct.

Suppose test case ->1

N=5 A[i]'s => 1 2 3 4 5

According to your code -> you will get answer=5 but this is not the correct answer as maximum profit can be 9 in this test case.

What you need to do is sort your array(in decreasing order) initially so that your if condition runs maximum time hence maximizing profit.

Just try to figure it out by yourself.

Here is my neat and simple code ==> https://www.codechef.com/viewsolution/31053496

This is your corrected code==>https://www.codechef.com/viewsolution/32020818

Hope it helps!!