File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed
Expand file tree Collapse file tree 1 file changed +52
-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+ public class Main {
6+ public static void main (String [] args ) throws Exception {
7+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
8+ int N = Integer . parseInt(br. readLine());
9+ Pair [] pairs = new Pair [N ];
10+
11+ for (int i = 0 ; i < N ; i++ ) {
12+ StringTokenizer st = new StringTokenizer (br. readLine());
13+ int t = Integer . parseInt(st. nextToken());
14+ int s = Integer . parseInt(st. nextToken());
15+ pairs[i] = new Pair (t, s);
16+ }
17+
18+ Arrays . sort(pairs, (x, y) - > x. s - y. s);
19+
20+
21+ int nowTime = 0 ;
22+ Integer ans = null ;
23+ for (Pair p : pairs) {
24+ nowTime += p. t;
25+
26+ if (nowTime > p. s) {
27+ System . out. println(- 1 );
28+ return ;
29+ }
30+
31+ if (ans == null ) {
32+ ans = p. s - nowTime;
33+ } else {
34+ ans = Math . min (ans, p. s - nowTime);
35+ }
36+ }
37+
38+ System . out. println(ans);
39+ }
40+
41+ static class Pair {
42+ int t;
43+ int s;
44+
45+ public Pair (int t , int s ) {
46+ this . t = t;
47+ this . s = s;
48+ }
49+ }
50+ }
51+
52+ ```
You can’t perform that action at this time.
0 commit comments