Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- int n = input.nextInt();
- int[] reels = new int[n];
- for (int i = 0; i < n; i++) {
- reels[i] = input.nextInt();
- }
- int editors_count = input.nextInt();
- int low = max(reels);
- int high = sum(reels);
- while (low < high) {
- int mid = (low + high) / 2;
- int editors_needed = 1;
- int current_time = 0;
- for (int reel : reels) {
- if (current_time + reel <= mid) {
- current_time += reel;
- } else {
- editors_needed++;
- current_time = reel;
- }
- }
- if (editors_needed <= editors_count) {
- high = mid;
- } else {
- low = mid + 1;
- }
- }
- System.out.println(low);
- }
- private static int max(int[] arr) {
- int max = Integer.MIN_VALUE;
- for (int num : arr) {
- max = Math.max(max, num);
- }
- return max;
- }
- private static int sum(int[] arr) {
- int sum = 0;
- for (int num : arr) {
- sum += num;
- }
- return sum;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement