fork download
  1. import java.util.*;
  2.  
  3. class Solution {
  4.  
  5. public static int chef(int num, int c, int[][] arr) {
  6. boolean[] val = new boolean[num];
  7. int count = 0;
  8.  
  9. while (true) {
  10. int index = -1;
  11. int p = Integer.MAX_VALUE;
  12.  
  13. for (int i = 0; i < num; i++) {
  14. if (!val[i] && arr[i][0] >= c) {
  15. if (arr[i][1] < p) {
  16. p = arr[i][1];
  17. index = i;
  18. }
  19. }
  20. }
  21.  
  22. if (index == -1) break; // No eligible chef found
  23.  
  24. val[index] = true;
  25. c += (arr[index][1] - arr[index][0]); // update complexity
  26. count++;
  27. }
  28.  
  29. return count;
  30. }
  31.  
  32. public static void main(String[] args) {
  33. Scanner sc = new Scanner(System.in);
  34.  
  35. int num = sc.nextInt();
  36. int c = sc.nextInt();
  37. int[][] arr = new int[num][2];
  38. for (int i = 0; i < num; i++) {
  39. arr[i][0] = sc.nextInt(); // Expertise
  40. arr[i][1] = sc.nextInt(); // Messiness
  41. }
  42.  
  43. int result = chef(num, c, arr);
  44. System.out.println(result);
  45. }
  46. }
  47.  
Success #stdin #stdout 0.18s 56640KB
stdin
3
5
3 7
3 6
6 8
stdout
1