-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArt.java
More file actions
92 lines (77 loc) · 2.67 KB
/
Copy pathArt.java
File metadata and controls
92 lines (77 loc) · 2.67 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import java.io.*;
import java.util.*;
public class Art {
public static void main(String[] args) {
FastScanner sc = new FastScanner();
int t = sc.nextInt();
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
int n = sc.nextInt();
int m = sc.nextInt();
long totalSum = 0;
ArrayList<Long> O = new ArrayList<>();
ArrayList<Long> E = new ArrayList<>();
for (int i = 0; i < n; i++) {
long val = sc.nextLong();
totalSum += val;
if ((i + 1) % 2 != 0) O.add(val);
else E.add(val);
}
int co = 0;
int ce = 0;
for (int i = 0; i < m; i++) {
int x = sc.nextInt();
if (x % 2 != 0) co++;
else ce++;
}
Collections.sort(O, Collections.reverseOrder());
Collections.sort(E, Collections.reverseOrder());
long markedSum = 0;
if (co > 0 && !O.isEmpty()) {
markedSum += O.get(0);
int limit = Math.min(O.size(), co);
for (int i = 1; i < limit; i++) {
if (O.get(i) > 0)
markedSum += O.get(i);
else
break;
}
}
if (ce > 0 && !E.isEmpty()) {
markedSum += E.get(0);
int limit = Math.min(E.size(), ce);
for (int i = 1; i < limit; i++) {
if (E.get(i) > 0) markedSum += E.get(i);
else break;
}
}
sb.append(totalSum - markedSum).append("\n");
}
System.out.print(sb);
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
String line = br.readLine();
if (line == null) return null;
st = new StringTokenizer(line);
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
}