forked from tmwilliamlin168/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathG028-C.java
More file actions
63 lines (59 loc) · 1.26 KB
/
Copy pathG028-C.java
File metadata and controls
63 lines (59 loc) · 1.26 KB
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
import java.io.*;
import java.util.*;
public class Main {
static final Reader in = new Reader();
public static void main(String[] args) {
int n=in.nextInt();
Pair[] p = new Pair[2*n];
long[] a = new long[2];
for(int i=0; i<2*n; ++i) {
p[i] = new Pair(in.nextInt(), i);
a[i&1]+=p[i].a;
}
a[0]=Math.min(a[1], a[0]);
a[1]=0;
Arrays.sort(p, new Comparator<Pair>() {
public int compare(Pair a, Pair b) {
return a.a==b.a?a.b-b.b:a.a-b.a;
}
});
int[] q = new int[2*n];
for(int i=0; i<n; ++i)
a[1]+=p[i].a;
for(int i=0; i<2*n; ++i)
q[p[i].b]=i;
for(int i=0; i<n; ++i) {
long c=a[1];
int j=n;
while(j-(q[2*i]<j?1:0)-(q[2*i+1]<j?1:0)<n)
c+=p[j++].a;
for(int k=2*i; k<2*i+2; ++k)
if(q[k]<j)
c-=p[q[k]].a;
a[0]=Math.min(c, a[0]);
}
System.out.println(a[0]);
}
static class Pair {
int a, b;
Pair(int a, int b) {
this.a=a;
this.b=b;
}
}
static class Reader {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
String next() {
while(st==null||!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch(Exception e) {}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
}
}