package algorithms.minimumtimerequired;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class Solution {

  private static final Scanner scanner = new Scanner(System.in);

  static long minTime(long[] machineItemsPerDays, long goal) {
    long itemsProduced = 0;
    long daysTakenToReachTheGoal = 0;
    long[] machineProgress = Arrays.copyOf(machineItemsPerDays, machineItemsPerDays.length);

    while (itemsProduced < goal) {
      for (int i = 0; i < machineProgress.length; i++) {
        if (machineProgress[i] > 0) {
          machineProgress[i]--;
        }
        if (machineProgress[i] == 0) {
          itemsProduced++;
          machineProgress[i] = machineItemsPerDays[i];
        }
      }

      daysTakenToReachTheGoal++;
    }

    return daysTakenToReachTheGoal;
  }

  public static void main(String[] args) throws IOException {
    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

    String[] nGoal = scanner.nextLine().split(" ");

    int n = Integer.parseInt(nGoal[0]);

    long goal = Long.parseLong(nGoal[1]);

    long[] machines = new long[n];

    String[] machinesItems = scanner.nextLine().split(" ");
    scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

    for (int i = 0; i < n; i++) {
      long machinesItem = Long.parseLong(machinesItems[i]);
      machines[i] = machinesItem;
    }

    long ans = minTime(machines, goal);

    bufferedWriter.write(String.valueOf(ans));
    bufferedWriter.newLine();

    bufferedWriter.close();

    scanner.close();
  }
}

@@ -299,27 +299,16 @@
  machine
-ItemsPerDay
 s, long 
@@ -323,328 +323,316 @@
     
-long itemsProduced = 0;%0A    long daysTakenToReachTheGoal = 0;%0A    long%5B%5D machineProgress = Arrays.copyOf(machineItemsPer
+Arrays.sort(machines);%0A    long max = machines%5Bmachines.length - 1%5D;%0A    long minDays = 0;%0A    long max
 Days
-, machineItemsPerDays.length);%0A%0A    while (itemsProduced %3C goal) %7B%0A      for (int i = 0; i %3C machineProgress.length; i++) %7B%0A        if (machineProgress%5Bi%5D %3E 0) %7B%0A          machineProgress%5Bi%5D--;%0A  
+ = max * goal;%0A    long result = 0;%0A%0A    while (minDays %3C maxDays) %7B%0A      long mid = (minDays + maxDays) / 2;%0A      long unit = 0;%0A      for (long machine : machines) %7B%0A        unit += mid / machine;%0A
     
@@ -645,182 +645,128 @@
     
-  
 if (
-machineProgress%5Bi%5D == 0) %7B%0A          itemsProduced++;%0A          machineProgress%5Bi%5D = machineItemsPerDays%5Bi%5D;%0A        %7D%0A      %7D%0A%0A      daysTakenToReachTheGoal++;
+unit %3C goal) %7B%0A        minDays = mid + 1;%0A      %7D else %7B%0A        result = mid;%0A        maxDays = mid;%0A      %7D
 %0A    %7D%0A
-%0A
     
@@ -776,31 +776,14 @@
 urn 
-daysTakenToReachTheGoal
+result
 ;%0A  

use binary search to fit in the time constraints