Please whats wrong in my code, please help

import java.util.Scanner;

public class BookParition {

public static void main(String[] args) {

	Scanner sc = new Scanner(System.in);

	int t = sc.nextInt();

	while (t > 0) {
		t--;
		int n = sc.nextInt();
		int m = sc.nextInt();

		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = sc.nextInt();
		}

		int ans = BookSearch(a, m);

		System.out.println(ans);
	}

}

public static int sum(int[] a, int n) {

	int sum = 0;

	for (int i = 0; i < a.length; i++) {
		sum += a[i];
	}
	return sum;
}

public static boolean numberofStudent(int[] a, int n, int mid, int m) {

	int student = 1;
	long res = 0;

	for (int i = 0; i < n; i++) {
		res = res+a[i];
		
	

		if (res > mid) {
			student++;
			res = a[i];
			
			if (student > m) {
				return false;
			}
		} else {
			res += a[i];
		}
	}
	return true;

}

public static int BookSearch(int[] a, int m) {

	int n = a.length - 1;
	int left = 0;
	int right = sum(a, n);
	int rs = Integer.MAX_VALUE;

	while (left <= right) {

		int mid =  (left + right / 2);
		if (numberofStudent(a, n, mid, m))
		{
			rs = Math.min(rs, mid);
			right = mid - 1;

		} else {

			left = mid + 1;
		}
	}
	return rs;
}

}