|
| 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 | +class SegTree { |
| 51 | + long[] tree; |
| 52 | + SegTree(int size) { |
| 53 | + tree = new long[size*4]; |
| 54 | + } |
| 55 | + |
| 56 | + void update(int s, int e, int i, long v, int n) { |
| 57 | + if(s == e) { |
| 58 | + tree[n] = v; |
| 59 | + return; |
| 60 | + } |
| 61 | + int m = (s+e)>>1; |
| 62 | + if(i <= m) update(s,m,i,v,n*2); |
| 63 | + else update(m+1,e,i,v,n*2+1); |
| 64 | + tree[n] = Math.max(tree[n*2], tree[n*2+1]); |
| 65 | + } |
| 66 | + |
| 67 | + long find(int s, int e, int l, int r, int n) { |
| 68 | + if(l>r || l>e || r<s) return 0; |
| 69 | + if(l<=s && e<=r) return tree[n]; |
| 70 | + int m = (s+e)>>1; |
| 71 | + return Math.max(find(s,m,l,r,n*2), find(m+1,e,l,r,n*2+1)); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +public class Main { |
| 76 | + |
| 77 | + static IOController io; |
| 78 | + |
| 79 | + // |
| 80 | + |
| 81 | + static int N; |
| 82 | + static long[][] infos; |
| 83 | + static SegTree seg; |
| 84 | + |
| 85 | + public static void main(String[] args) throws Exception { |
| 86 | + |
| 87 | + io = new IOController(); |
| 88 | + |
| 89 | + init(); |
| 90 | + solve(); |
| 91 | + |
| 92 | + io.close(); |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + static void init() throws Exception { |
| 97 | + |
| 98 | + N = io.nextInt(); |
| 99 | + infos = new long[N][]; |
| 100 | + for(int i=0;i<N;i++) infos[i] = new long[]{io.nextLong(),io.nextLong(),io.nextLong(),i+1}; |
| 101 | + seg = new SegTree(N); |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + static void solve() throws Exception { |
| 106 | + |
| 107 | + Arrays.sort(infos, (a,b) -> { |
| 108 | + if(a[1]-a[0] == b[1]-b[0]) { |
| 109 | + return Long.compare(a[0],b[0]); |
| 110 | + } |
| 111 | + return Long.compare(a[1]-a[0],b[1]-b[0]); |
| 112 | + }); |
| 113 | + |
| 114 | + for(int i=0;i<N;i++) { |
| 115 | + long[] info = infos[i]; |
| 116 | + int idx = (int)info[3]; |
| 117 | + long c = info[2]; |
| 118 | + seg.update(1,N,idx,seg.find(1,N,1,idx-1,1) + c,1); |
| 119 | + } |
| 120 | + io.write(seg.find(1,N,1,N,1) + "\n"); |
| 121 | + |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | +} |
| 126 | +``` |
0 commit comments