File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed
Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class Main {
6+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
7+ static BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System . out));
8+ static StringTokenizer st;
9+ static int N ,D ;
10+ static class present {
11+ int price,value;
12+
13+ present (int price , int value ){
14+ this . price = price;
15+ this . value = value;
16+ }
17+ }
18+ static present[] arr;
19+
20+ public static void main (String [] args ) throws IOException {
21+ st = new StringTokenizer (br. readLine());
22+ N = Integer . parseInt(st. nextToken());
23+ D = Integer . parseInt(st. nextToken());
24+ arr = new present [N ];
25+
26+ for (int i = 0 ; i < N ; i++ ) {
27+ st = new StringTokenizer (br. readLine());
28+ arr[i] = new present(Integer . parseInt(st. nextToken()),Integer . parseInt(st. nextToken()));
29+ }
30+ Arrays . sort(arr,(a,b) - > Integer . compare(a. price,b. price));
31+ long cur = 0 ,max = 0 ;
32+ int left = 0 ,right = 0 ;
33+ while (right < N ){
34+ if (arr[right]. price - arr[left]. price < D ){
35+ cur += arr[right++ ]. value;
36+ max = Math . max(max,cur);
37+ }else {
38+ cur -= arr[left++ ]. value;
39+ }
40+ }
41+ bw. write(max+ " " );
42+ bw. close();
43+ }
44+ }
45+ ```
You can’t perform that action at this time.
0 commit comments