package arrays;
import java.util.Scanner;
public class bookallocationproblem {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
for (int i = 0; i <= t; i++) {
long n = scn.nextLong();
long m = scn.nextLong();
long[] books = new long[100005];
for (int j = 0; j <n; j++) {
books[j] = scn.nextLong();
}
System.out.println(binarysearch(books,n,m));
}
}
public static long binarysearch(long[] books, long n, long m) {
long totalpages = 0;
long s = 0, e = 0;
for (int i = 0; i < n; i++) {
totalpages = totalpages + books[i];
s = Math.max(s,books[i]);
}
e = totalpages;
long finalans = s;
while (s <= e) {
long mid = s + e / 2;
if (isitvalid(books, n, m, mid)) {
finalans = mid;
e = mid - 1;
} else {
s = mid + 1;
}
}
return finalans;
}
public static boolean isitvalid(long[] books, long n, long m, long mid) {
int students = 1;
long currentpages = 0;
for (int i = 0; i < n; i++) {
if (currentpages + books[i] > mid) {
currentpages=books[i];
students++;
if (students>m) {
return false;
}
}
else {
currentpages =currentpages+ books[i];
}
}
return true;
}
}
what is wrong in my code???