|
| 1 | +``` |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | +
|
| 5 | +public class Main { |
| 6 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 8 | + private static int[][] arr; |
| 9 | + private static int N, G, B, D; |
| 10 | + private static long answer; |
| 11 | +
|
| 12 | + public static void main(String[] args) throws IOException { |
| 13 | + init(); |
| 14 | + if (B < arr[0][0]) bw.write(-1 + "\n"); |
| 15 | + else { |
| 16 | + B -= arr[0][0]; |
| 17 | + answer = greedy(); |
| 18 | + } |
| 19 | +
|
| 20 | + bw.write(answer + "\n"); |
| 21 | + bw.flush(); |
| 22 | + bw.close(); |
| 23 | + br.close(); |
| 24 | + } |
| 25 | +
|
| 26 | + private static void init() throws IOException { |
| 27 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 28 | + N = Integer.parseInt(st.nextToken()); |
| 29 | + G = Integer.parseInt(st.nextToken()); |
| 30 | + B = Integer.parseInt(st.nextToken()); |
| 31 | + D = Integer.parseInt(st.nextToken()); |
| 32 | +
|
| 33 | + arr = new int[N][2]; |
| 34 | + for (int i = 0; i < N; i++) { |
| 35 | + st = new StringTokenizer(br.readLine()); |
| 36 | + int X = Integer.parseInt(st.nextToken()); |
| 37 | + int Y = Integer.parseInt(st.nextToken()); |
| 38 | + arr[i][0] = X; |
| 39 | + arr[i][1] = Y; |
| 40 | + } |
| 41 | +
|
| 42 | + Arrays.sort(arr, (o1, o2) -> Integer.compare(o1[0], o2[0])); |
| 43 | + } |
| 44 | +
|
| 45 | + private static long greedy() { |
| 46 | + long result = 0; |
| 47 | + int pos = arr[0][0]; |
| 48 | +
|
| 49 | + int current = 0; |
| 50 | +
|
| 51 | + while (current < N-1) { |
| 52 | + int next = -1; |
| 53 | +
|
| 54 | + for (int i = current+1; i < N; i++) { |
| 55 | + if (arr[i][0] > arr[current][0] + G) break; |
| 56 | +
|
| 57 | + if (arr[i][1] < arr[current][1]) { |
| 58 | + next = i; |
| 59 | + break; |
| 60 | + } |
| 61 | + } |
| 62 | +
|
| 63 | + if (next == -1) { |
| 64 | + long remain = G-B; |
| 65 | + result += arr[current][1]*remain; |
| 66 | + B = G; |
| 67 | + for (int i = current; i < N; i++) { |
| 68 | + if (pos + G >= arr[i][0]) { |
| 69 | + next = i; |
| 70 | + } |
| 71 | + } |
| 72 | + } else { |
| 73 | + long remain = arr[next][0]-pos; |
| 74 | + result += arr[current][1]*remain; |
| 75 | + B += (int)remain; |
| 76 | + } |
| 77 | +
|
| 78 | + int dist = arr[next][0] - pos; |
| 79 | + pos = arr[next][0]; |
| 80 | + B -= dist; |
| 81 | + current = next; |
| 82 | + } |
| 83 | +
|
| 84 | + if (pos + G >= D) { |
| 85 | + result += (long)(D-pos)*arr[current][1]; |
| 86 | + } else { |
| 87 | + result = -1; |
| 88 | + } |
| 89 | +
|
| 90 | + return result; |
| 91 | + } |
| 92 | +} |
| 93 | +``` |
0 commit comments