Sell All the Cars goes wrong

using namespace std;

int main() {
// your code goes here
int T;
int N;
long int arr[N];
int counter = 0;
long int sum = 0;
for(int i=0; i<N; i++)
//counter initially zero
//counter increses by one for one sell
sum = (arr[i]-counter)%1000000007+sum)%1000000007;
return 0;
//Question link is

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 ==>
This is your corrected code==>
Hope it helps!!

Thank you so much!!!

1 Like