 # What is the difference between two codes? Can anyone please explain me?

In yesterday’s weekend contest at coding ninja’s platform, in the first problem named Minimize Bill when I tried to submit the first code it showed me the Partially Accepted but when I submited the editorialist code after the contest it showed me Accepted. I think the logic of two solutions are almost similar. Can anyone please tell me why my code fails?

// My solution

long long int totalBill(int n, vector<int> &a)
{
sort(a.begin(), a.end());

int maxx = a[n - 1];
long long int sum = 0;

for (long long int i = 0; i < n - 1; i++)
{
sum = sum + a[i];
}

long long int ans = sum - maxx;

return ans;
}

// Editorialist Solution

long long int totalBill(int n, vector<int> &a)
{
int maxx = a;
long long int sum = 0;

for(long long int i = 0; i < n; i++)
{
maxx = max(maxx, a[i]);
sum = sum + a[i];
}

long long int ans = sum - (2 * maxx);

return ans;
}


What is the difference between the logic of this two codes?

What are the constraints for N and arr[i]?

##### Constraints -
1 <= 'T' <= 10
1 <= 'N' <= 10^5
-10^6 <= 'A[i]' <= 10^6
Sum of 'N' over all test cases doesn’t exceed 10^5.

Time Limit: 1 sec


I see a message saying \red{\text{Time limit exceeded}} after submitting your code. An O(N\log{N}) solution should pass in the given time limit for the given constraints.

The version of g++ (g++ 5.4) could be the reason for \red{\text{TLE}}. I don’t see any other flaw.

Edit: The same logic gives \green{\text{AC}} when implemented in Python 3.5.

Screenshot

Thanks for such a good clarification. Thank You.