-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcowCrossing.java
42 lines (34 loc) · 1.4 KB
/
cowCrossing.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.StringTokenizer;
public class cowCrossing {
static ArrayList<Integer> time = new ArrayList<>();
static HashMap<Integer, Integer> timeAndStay = new HashMap<>();
public static void main(String[] args) throws IOException{
BufferedReader bf = new BufferedReader(new FileReader("cowqueue.in"));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("cowqueue.out")));
StringTokenizer st = new StringTokenizer(bf.readLine());
int n = Integer.parseInt(st.nextToken());
for(int i = 0; i < n; i++){
StringTokenizer token = new StringTokenizer(bf.readLine());
int timeArrived = Integer.parseInt(token.nextToken());
int timeStayed2 = Integer.parseInt(token.nextToken());
time.add(timeArrived);
timeAndStay.put(timeArrived, timeStayed2);
}
Collections.sort(time);
int totalTime = timeAndStay.get(time.get(0)) + time.get(0);
for(int i = 1; i < n; i++){
if(totalTime >= time.get(i)){
totalTime = totalTime + timeAndStay.get(time.get(i));
} else {
totalTime = time.get(i) + timeAndStay.get(time.get(i));
}
}
pw.println(totalTime);
pw.close();
bf.close();
}
}