• + 0 comments

    Java:

    public static int luckBalance(int k, List<List<Integer>> contests) {
      int balance = 0;
      Collections.sort(contests, new Comparator<List<Integer>>() {
        @Override
        public int compare(List<Integer> o1, List<Integer> o2) {
          return o1.get(0).compareTo(o2.get(0));
        }
      });
      int n = contests.size();
      for (int i = n - 1; i >= 0; i--) {
        int current = contests.get(i).get(0);
        if (contests.get(i).get(1) == 1) {
          if (k > 0) {
            balance += current;
            k--;
          } else {
            balance -= current;
          }
    
        } else {
          balance += current;
        }
      }
    
      return balance;
    }