2 test cases are giving wrong answer

this is the code
#include <bits/stdc++.h>
using namespace std;

bool check(int *a, int time, int k, int n) {
int quantity = 0;
int painter = 1;
for(int i = 0; i < n; i++) {
if(quantity + a[i] > time) {
quantity = a[i];
painter++;
if(painter > k) {
return false;
}
}
else {
quantity += a[i];
}
}
return true;
}

int main() {
int k, n;
cin >> k >> n;

int a[n], sum = 0;

for(int i = 0; i < n; i++) {
    cin >> a[i];
    sum = sum + a[i];
}

int s = a[n-1];
int e = sum;
int ans = INT_MAX;
while(s <= e) {
    int time = (s + e)/2;
    if(check(a, time, k, n)) {
        ans = min(ans, time);
        e = time - 1;
    }
    else {
        s = time + 1;
    }
}

cout << ans << endl;

}

@alter
use long long int instead of int.
also for calculation mid use :- mid = start + (end-start)/2 (beacuse there may be chance that after addition it may got overflow from datatype)

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.