I cant find wrong in my solution, why is it not working for some test cases, please help me resolve it, I am pasting my code here:
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int b = sc.nextInt();
long[] boards = new long[b];
long sum = 0;
for(int i=0;i<b;i++){
boards[i] = Math.abs(sc.nextInt());
sum += boards[i];
}
Arrays.sort(boards);
System.out.print(binarySearch(boards[b-1],sum,n,boards));
}
public static long binarySearch(long lo,long hi,long n, long[] arr){
long result = -1;
long mid = 0;
while(lo<hi){
mid = (lo+hi)/2;
if(isPossible(mid,n,arr)){
result = mid;
hi = mid;
}else{
lo=mid+1;
}
}
return result;
}
public static boolean isPossible(long m,long n,long[] arr){
long painters = 1;
long pbc = 0;
for(int i=0;i<arr.length;i++){
if(pbc+arr[i]<=m){
pbc+=arr[i];
}else{
painters++;
if(painters>n){
return false;
}
pbc = arr[i];
}
}
return true;
}
}