File tree Expand file tree Collapse file tree 1 file changed +55
-0
lines changed
Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.util.* ;
3+ import java.io.* ;
4+
5+ class Solution
6+ {
7+ static Work [] works;
8+
9+ public static void main (String [] args ) throws Exception {
10+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
11+ StringTokenizer st = new StringTokenizer (br. readLine());
12+
13+ int N = Integer . parseInt(st. nextToken());
14+ works = new Work [N ];
15+ for (int i = 0 ; i < N ; i++ ) {
16+ st = new StringTokenizer (br. readLine());
17+ works[i] = new Work (Integer . parseInt(st. nextToken()), Integer . parseInt(st. nextToken()));
18+ }
19+
20+ Arrays . sort(works, (o1, o2) - > {
21+ if (o1. s != o2. s) return o1. s- o2. s;
22+ return o2. t- o1. t;
23+ });
24+
25+ int answer = works[0 ]. s - works[0 ]. t;
26+ while (answer >= 0 ) {
27+ if (check(answer)) {
28+ System . out. println(answer);
29+ return ;
30+ }
31+ answer -= 1 ;
32+ }
33+ System . out. println(- 1 );
34+ }
35+
36+ static boolean check (int start ) {
37+ int curr = start;
38+ for (Work w : works) {
39+ if (curr + w. t > w. s) return false ;
40+ curr += w. t;
41+ }
42+ return true ;
43+ }
44+
45+ static class Work {
46+ int t;
47+ int s;
48+ public Work (int t , int s ) {
49+ this . t = t;
50+ this . s = s;
51+ }
52+ }
53+ }
54+
55+ ```
You can’t perform that action at this time.
0 commit comments