https://www.acmicpc.net/problem/1826
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Main {
static int N;
static PriorityQueue<Station> stations;
static int endPosition, fuel;
static void input() {
Reader scanner = new Reader();
N = scanner.nextInt();
stations = new PriorityQueue<>();
for(int idx = 0; idx < N; idx++) {
int distance = scanner.nextInt(), fuel = scanner.nextInt();
stations.offer(new Station(distance, fuel));
}
endPosition = scanner.nextInt();
fuel = scanner.nextInt();
}
static void solution() {
PriorityQueue<Integer> queue = new PriorityQueue<>(Comparator.reverseOrder());
int answer = 0;
while(fuel < endPosition) {
while(!stations.isEmpty() && stations.peek().distance <= fuel) {
queue.offer(stations.poll().fuel);
}
if(queue.isEmpty()) {
System.out.println(-1);
return;
}
answer++;
fuel += queue.poll();
}
System.out.println(answer);
}
static class Station implements Comparable<Station> {
int distance, fuel;
public Station(int distance, int fuel) {
this.distance = distance;
this.fuel = fuel;
}
@Override
public int compareTo(Station s) {
if(distance != s.distance) return distance - s.distance;
return s.fuel - fuel;
}
}
public static void main(String[] args) {
input();
solution();
}
static class Reader {
BufferedReader br;
StringTokenizer st;
public Reader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
}
}