package chelper; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.SortedMap; import java.util.TreeMap; import io.InputReader; import io.OutputWriter; public class TaskD { class Team implements Comparable { public final long score; public final long weight; public final long leeway; private final int index; public Team(int index, long score, long weight) { this.index = index; this.score = score; this.weight = weight; leeway = weight - score + 1; } @Override public int compareTo(Team o) { return Long.compare(leeway, o.leeway); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Team team = (Team) o; if (score != team.score) return false; if (weight != team.weight) return false; if (leeway != team.leeway) return false; return index == team.index; } @Override public int hashCode() { int result = (int) (score ^ score >>> 32); result = 31 * result + (int) (weight ^ weight >>> 32); result = 31 * result + (int) (leeway ^ leeway >>> 32); result = 31 * result + index; return result; } } public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt() - 1; long ourScore = in.nextLong(); long ourWeight = in.nextLong(); List teams = new ArrayList<>(); for (int i = 0; i < n; i++) { Team team = new Team(i, in.nextLong(), in.nextLong()); teams.add(team); } Collections.sort(teams); List left = new ArrayList<>(); List right = new ArrayList<>(); for (Team team : teams) { if (team.score > ourScore) { left.add(team); } else { right.add(team); } } Collections.sort(right, new Comparator() { @Override public int compare(Team o1, Team o2) { return -Long.compare(o1.score, o2.score); } }); long ans = left.size() + 1; SortedMap leftMap = new TreeMap<>(); for (Team team : left) { if (!leftMap.containsKey(team.leeway)) { leftMap.put(team.leeway, 0L); } leftMap.put(team.leeway, leftMap.get(team.leeway) + 1); } int rightPos = 0; int ourPlace = left.size() + 1; while (!leftMap.isEmpty()) { long minLeeway = leftMap.firstKey(); ourScore -= minLeeway; if (ourScore < 0) { break; } long minVal = leftMap.get(minLeeway) - 1; if (minVal == 0) { leftMap.remove(minLeeway); } else { leftMap.put(minLeeway, minVal); } ourPlace--; while (rightPos < right.size()) { Team team = right.get(rightPos); if (team.score > ourScore) { if (!leftMap.containsKey(team.leeway)) { leftMap.put(team.leeway, 0L); } leftMap.put(team.leeway, leftMap.get(team.leeway) + 1); rightPos++; ourPlace++; } else { break; } } ans = Math.min(ans, ourPlace); } out.println(ans); } }