|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +class IOController { |
| 6 | + BufferedReader br; |
| 7 | + BufferedWriter bw; |
| 8 | + StringTokenizer st; |
| 9 | + |
| 10 | + public IOController() { |
| 11 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 12 | + bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 13 | + st = new StringTokenizer(""); |
| 14 | + } |
| 15 | + |
| 16 | + String nextLine() throws Exception { |
| 17 | + String line = br.readLine(); |
| 18 | + st = new StringTokenizer(line); |
| 19 | + return line; |
| 20 | + } |
| 21 | + |
| 22 | + String nextToken() throws Exception { |
| 23 | + while (!st.hasMoreTokens()) nextLine(); |
| 24 | + return st.nextToken(); |
| 25 | + } |
| 26 | + |
| 27 | + int nextInt() throws Exception { |
| 28 | + return Integer.parseInt(nextToken()); |
| 29 | + } |
| 30 | + |
| 31 | + long nextLong() throws Exception { |
| 32 | + return Long.parseLong(nextToken()); |
| 33 | + } |
| 34 | + |
| 35 | + double nextDouble() throws Exception { |
| 36 | + return Double.parseDouble(nextToken()); |
| 37 | + } |
| 38 | + |
| 39 | + void close() throws Exception { |
| 40 | + bw.flush(); |
| 41 | + bw.close(); |
| 42 | + } |
| 43 | + |
| 44 | + void write(String content) throws Exception { |
| 45 | + bw.write(content); |
| 46 | + } |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | +public class Main { |
| 51 | + |
| 52 | + static IOController io; |
| 53 | + |
| 54 | + // |
| 55 | + |
| 56 | + static int N, L; |
| 57 | + static int[][] infos; |
| 58 | + static TreeMap<Integer, Long> h, l; |
| 59 | + |
| 60 | + public static void main(String[] args) throws Exception { |
| 61 | + |
| 62 | + io = new IOController(); |
| 63 | + |
| 64 | + init(); |
| 65 | + solve(); |
| 66 | + |
| 67 | + io.close(); |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + static void init() throws Exception { |
| 72 | + |
| 73 | + N = io.nextInt(); |
| 74 | + L = io.nextInt(); |
| 75 | + infos = new int[N][2]; |
| 76 | + for(int i=0;i<N;i++) for(int j=0;j<2;j++) infos[i][j] = io.nextInt(); |
| 77 | + h = new TreeMap<>(); |
| 78 | + l = new TreeMap<>(); |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + static void solve() throws Exception { |
| 83 | + |
| 84 | + Arrays.sort(infos, (a,b) -> a[1]==b[1] ? a[0]-b[0] : a[1]-b[1]); |
| 85 | + long ans = 0; |
| 86 | + for(int[] info : infos) { |
| 87 | + int t = info[0], d = info[1]; |
| 88 | + long hValue = h.getOrDefault(t, 0L); |
| 89 | + long hRes = Math.max(hValue, l.getOrDefault(d,0L) + Math.abs(t-d) + L); |
| 90 | + long lValue = l.getOrDefault(d, 0L); |
| 91 | + long lRes = Math.max(lValue, Math.max(lValue, h.getOrDefault(t, 0L) + Math.abs(t-d) + L)); |
| 92 | + h.put(t, hRes); |
| 93 | + l.put(d, lRes); |
| 94 | + ans = Math.max(ans, Math.max(hRes, lRes)); |
| 95 | + } |
| 96 | + io.write(ans + "\n"); |
| 97 | + |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | +} |
| 102 | +``` |
0 commit comments