At first 2 cases were not passing and after sorting the array error is coming

#include
#define int long long //added
using namespace std;
#include
#include

bool poss(int a[],int n,int s,int minpg){
int st=1;
int pgs=0;
for(int i=0;i<n;i++){
if(pgs+a[i]>minpg){
st++;
pgs=a[i];
if(st>s){
return false;
}
}
else{
pgs+=a[i];
}
}
return true;
}

int32_t main () {
int n,k;
cin>>k>>n;
int a[n];

int t=0;
for(int i=0;i<n;i++){
	cin>>a[i];
	t+=a[i];
}
sort(a,a+n)
int s=a[n-1];
int e=t;
int ans=t;
while(s<=e){
	int mid=(s+e)/2;
	if(poss(a,n,k,mid)){
		ans=mid;
		e=mid-1;
	}
	else{
		s=mid+1;
	}

}
cout<<ans<<endl;
return 0;

}

hello @mansi25

check now->

a) order matters so dont sort
b) s = max in array and becuase array is not sorted u need to explicilty find it out

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.