fork download
  1. import java.util.*;
  2.  
  3. class MinK {
  4.  
  5. static boolean check(int k, ArrayList<Integer> b, int d) {
  6. int n = b.size();
  7. int sum_moves = 0;
  8. for(int i = 0; i < n; i++) {
  9. if(b.get(i) % k == 0) {
  10. sum_moves += b.get(i) / k;
  11. } else {
  12. sum_moves += (b.get(i) / k) + 1;
  13. }
  14. }
  15. return sum_moves <= d;
  16. }
  17.  
  18. public static void main(String[] args) {
  19. Scanner scanner = new Scanner(System.in);
  20. int n = scanner.nextInt();
  21. int d = scanner.nextInt();
  22.  
  23. ArrayList<Integer> b = new ArrayList<>();
  24. for (int i = 0; i < n; i++) {
  25. b.add(scanner.nextInt());
  26. }
  27.  
  28. int left = 1; // Minimum possible value of k
  29. int right = Collections.max(b); // Maximum possible value of k
  30. int answer = right; // Initialize answer to maximum
  31.  
  32. while (left <= right) {
  33. int mid = (left + right) / 2;
  34. if (check(mid, b, d)) {
  35. answer = mid; // Update answer if valid
  36. right = mid - 1; // Try for a smaller k
  37. } else {
  38. left = mid + 1; // Increase k
  39. }
  40. }
  41.  
  42. System.out.println(answer);
  43. }
  44. }
  45.  
Success #stdin #stdout 0.18s 54540KB
stdin
4 5 
2 3 4 5
stdout
4