-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpairup.java
83 lines (69 loc) · 2.07 KB
/
pairup.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class pairup {
public static int largest(int[] dingo){
int current = dingo[0];
for (int j=0; j<dingo.length - 1; j++){
if (dingo[j] > current){
current = dingo[j];
}
}
return current;
}
public static void main(String[] args) throws FileNotFoundException {
Scanner fagr = new Scanner(new File("pairup.in"));
int milkOutputs = fagr.nextInt();
pair[] stuff = new pair[milkOutputs];
for (int i=0; i<milkOutputs; i++){
int times = fagr.nextInt();
int output = fagr.nextInt();
pair x = new pair();
x.count = times;
x.time = output;
stuff[i] = x;
}
Arrays.sort(stuff);
int x = 0;
int y = milkOutputs-1;
int max = 0;
while(x<y){
if(stuff[x].count>stuff[y].count){
stuff[x].count-=stuff[y].count;
max = Math.max(max,stuff[x].time+stuff[y].time);
y--;
}
else if(stuff[y].count>stuff[x].count){
stuff[y].count-=stuff[x].count;
max = Math.max(max,stuff[x].time+stuff[y].time);
x++;
}
else{
if(x==y){
max = Math.max(max,2*stuff[x].time);
break;
}
else {
max = Math.max(max,stuff[x].time+stuff[y].time);
x++;
y--;
}
}
}
PrintWriter writer = new PrintWriter("pairup.out");
writer.println(max);
writer.close();
}
static class pair implements Comparable<pair>{
int count;
int time;
@Override
public int compareTo(pair pair) {
return this.time-pair.time;
}
}
}