Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class TestClass {
- public static void main(String args[] ) throws Exception {
- Scanner sc = new Scanner(System.in);
- int n = sc.nextInt();
- int k = sc.nextInt();
- int[] a = new int[n];
- for (int i = 0; i < n; i++)
- a[i] = sc.nextInt();
- long l = 1, r = (long)1e15, m;
- while (l < r) {
- m = (l + r) / 2;
- if (numberOfSubarrays(a, m) <= k)
- r = m;
- else
- l = m + 1;
- }
- System.out.println(r);
- }
- static int numberOfSubarrays(int[] a, long sum) {
- int ans = 0;
- long curr = 0;
- for (int x : a) {
- if (x > sum)
- return Integer.MAX_VALUE; // Not possible
- if (curr + x > sum) {
- ans++;
- curr = 0;
- }
- curr += x;
- }
- return ans + 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement