Find the mistake

#include<bits/stdc++.h>
#define ll long long int
using namespace std;

bool karpayega(ll mid , ll a[] , ll n , ll painter){
ll s = 0, p = 1;

for(int i = 0; i < n ; i++){
	if((s + a[i]) > mid){
		p++;
		
		s = a[i];
		if(p > painter){
			return false;
		}
	}
	else{
		s+=a[i];
	}
}
return true;

}
int main() {
ll n;
cin>>n;
ll a[n];
ll painter;
cin>>painter;
ll time;
cin>>time;
ll sum = 0;
for(int i = 0 ; i< n ; i++){
cin>>a[i];
sum+=a[i];
}
sort(a,a+n);
ll s = a[n-1] , e = sum , ans = INT_MAX;
while(s<=e){
ll mid = (s+e)/2;

	if(karpayega(mid , a, n, painter)){
		ans = mid;
		
		e = mid - 1;
	}
	else{
		s = mid + 1;
	}
}
cout<<(ans%10000003)*time<<endl;
return 0;

}

can you tell why this code is giving wrong o/p

Your approach is not correct.
You cannot sort the array as it will change the order and make a new array.

Refer this video and try again

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.