Advertisement
Mizuhara_Chizuru

Untitled

May 7th, 2023
769
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         Scanner input = new Scanner(System.in);
  6.         int n = input.nextInt();
  7.         int[] reels = new int[n];
  8.         for (int i = 0; i < n; i++) {
  9.             reels[i] = input.nextInt();
  10.         }
  11.         int editors_count = input.nextInt();
  12.  
  13.         int low = max(reels);
  14.         int high = sum(reels);
  15.         while (low < high) {
  16.             int mid = (low + high) / 2;
  17.             int editors_needed = 1;
  18.             int current_time = 0;
  19.             for (int reel : reels) {
  20.                 if (current_time + reel <= mid) {
  21.                     current_time += reel;
  22.                 } else {
  23.                     editors_needed++;
  24.                     current_time = reel;
  25.                 }
  26.             }
  27.             if (editors_needed <= editors_count) {
  28.                 high = mid;
  29.             } else {
  30.                 low = mid + 1;
  31.             }
  32.         }
  33.  
  34.         System.out.println(low);
  35.     }
  36.  
  37.     private static int max(int[] arr) {
  38.         int max = Integer.MIN_VALUE;
  39.         for (int num : arr) {
  40.             max = Math.max(max, num);
  41.         }
  42.         return max;
  43.     }
  44.  
  45.     private static int sum(int[] arr) {
  46.         int sum = 0;
  47.         for (int num : arr) {
  48.             sum += num;
  49.         }
  50.         return sum;
  51.     }
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement