Submission #2557743
Source Code Expand
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { int n; int[] as; public static void main(String args[]) { new Main().run(); } void run() { FastReader sc = new FastReader(); n = sc.nextInt(); as = new int[3 * n]; for (int i = 0; i < 3 * n; i++) { as[i] = sc.nextInt(); } solve(); } void solve() { if (n == 1) { long ans = 0; if (Math.abs(as[0] - as[1]) < Math.abs(as[1] - as[2])) { ans = as[0] - Math.max(as[1], as[2]); } else { ans = Math.max(as[0], as[1]) - as[2]; } System.out.println(ans); return; } int length = 3 * n; boolean[] picked = new boolean[length]; SortedMap<Integer, List<Integer>> former = new TreeMap<>(); SortedMap<Integer, List<Integer>> latter = new TreeMap<>(Collections.reverseOrder()); for (int i = 0; i < n; i++) { if (!former.containsKey(as[i])) { List<Integer> temp = new LinkedList<>(); temp.add(i); former.put(as[i], temp); } else { List<Integer> temp = former.get(as[i]); temp.add(i); former.put(as[i], temp); } int index = length - i - 1; if (!latter.containsKey(as[index])) { List<Integer> temp = new LinkedList<>(); temp.add(index); latter.put(as[index], temp); } else { List<Integer> temp = latter.get(as[index]); temp.add(index); latter.put(as[index], temp); } } int l = n; int r = length - n - 1; for (int i = 0; i < n; i++) { Iterator<Integer> formerIt = former.keySet().iterator(); int formerMin = formerIt.next(); int formerSecondMin = formerIt.next(); int formerDiff = formerSecondMin - formerMin; if (formerMin >= as[l]) { formerDiff = formerMin - as[l]; } else if (formerSecondMin >= as[l]) { formerDiff = as[l] - formerMin; } Iterator<Integer> latterIt = latter.keySet().iterator(); int latterMax = latterIt.next(); int latterSecondMax = latterIt.next(); int latterDiff = latterMax - latterSecondMax; if (as[r] >= latterMax) { latterDiff = as[r] - latterMax; } else if (as[r] >= latterSecondMax) { latterDiff = latterMax - as[r]; } if (formerDiff >= latterDiff) { if (formerMin >= as[l]) { picked[l] = true; } else { if (!former.containsKey(as[l])) { List<Integer> temp = new LinkedList<>(); temp.add(l); former.put(as[l], temp); } else { List<Integer> temp = former.get(as[l]); temp.add(l); former.put(as[l], temp); } picked[former.get(formerMin).get(0)] = true; if (former.get(formerMin).size() == 1) { former.remove(formerMin); } else { List<Integer> temp = former.get(formerMin); temp.remove(0); former.put(formerMin, temp); } } l++; } else { if (latterMax <= as[r]) { picked[r] = true; } else { if (!latter.containsKey(as[r])) { List<Integer> temp = new LinkedList<>(); temp.add(r); latter.put(as[r], temp); } else { List<Integer> temp = latter.get(as[r]); temp.add(r); latter.put(as[r], temp); } picked[latter.get(latterMax).get(0)] = true; if (latter.get(latterMax).size() == 1) { latter.remove(latterMax); } else { List<Integer> temp = latter.get(latterMax); temp.remove(0); latter.put(latterMax, temp); } } r--; } } long ans = 0; int j = 0; int k = 0; for (int i = 0; i < n; i++) { while(picked[i + j]) { j++; } ans += (long)as[i + j]; while(picked[length - i - k - 1]) { k++; } ans -= (long)as[length - i - k - 1]; } System.out.println(ans); } class Pair implements Comparable<Pair> { int value; int index; Pair(int value, int index) { this.value = value; this.index = index; } public int compareTo(Pair p) { return this.value - p.value; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Submission Info
Submission Time | |
---|---|
Task | D - 3N Numbers |
User | ynish |
Language | Java8 (OpenJDK 1.8.0) |
Score | 0 |
Code Size | 6769 Byte |
Status | RE |
Exec Time | 1043 ms |
Memory | 106096 KB |
Judge Result
Set Name | Sample | Subtask | All | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 0 / 300 | 0 / 200 | ||||||||||||||
Status |
|
|
|
Set Name | Test Cases |
---|---|
Sample | 0_00.txt, 0_01.txt, 0_02.txt |
Subtask | 0_00.txt, 0_01.txt, 0_02.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt |
All | 0_00.txt, 0_01.txt, 0_02.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 2_00.txt, 2_01.txt, 2_02.txt, 2_03.txt, 2_04.txt, 2_05.txt, 2_06.txt, 2_07.txt, 2_08.txt, 2_09.txt, 2_10.txt, 2_11.txt, 2_12.txt, 2_13.txt, 2_14.txt, 2_15.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
0_00.txt | AC | 76 ms | 19668 KB |
0_01.txt | AC | 68 ms | 23252 KB |
0_02.txt | AC | 71 ms | 21076 KB |
1_00.txt | AC | 71 ms | 19156 KB |
1_01.txt | AC | 69 ms | 19284 KB |
1_02.txt | WA | 67 ms | 19284 KB |
1_03.txt | AC | 68 ms | 18516 KB |
1_04.txt | WA | 69 ms | 17876 KB |
1_05.txt | WA | 70 ms | 18260 KB |
1_06.txt | WA | 68 ms | 15828 KB |
1_07.txt | AC | 70 ms | 18388 KB |
1_08.txt | RE | 90 ms | 19284 KB |
1_09.txt | RE | 105 ms | 18900 KB |
1_10.txt | RE | 94 ms | 21460 KB |
1_11.txt | RE | 105 ms | 19028 KB |
1_12.txt | RE | 97 ms | 19412 KB |
1_13.txt | RE | 102 ms | 18900 KB |
1_14.txt | RE | 96 ms | 22100 KB |
1_15.txt | RE | 100 ms | 21844 KB |
1_16.txt | WA | 95 ms | 19412 KB |
1_17.txt | RE | 105 ms | 19028 KB |
1_18.txt | RE | 106 ms | 18516 KB |
1_19.txt | RE | 106 ms | 21716 KB |
1_20.txt | WA | 119 ms | 24916 KB |
1_21.txt | WA | 114 ms | 20564 KB |
1_22.txt | WA | 120 ms | 20052 KB |
1_23.txt | WA | 122 ms | 24784 KB |
2_00.txt | RE | 275 ms | 43748 KB |
2_01.txt | RE | 348 ms | 68468 KB |
2_02.txt | RE | 292 ms | 58184 KB |
2_03.txt | RE | 291 ms | 54840 KB |
2_04.txt | RE | 333 ms | 56924 KB |
2_05.txt | RE | 311 ms | 54976 KB |
2_06.txt | RE | 322 ms | 58256 KB |
2_07.txt | RE | 300 ms | 56284 KB |
2_08.txt | WA | 472 ms | 52568 KB |
2_09.txt | RE | 458 ms | 53980 KB |
2_10.txt | RE | 469 ms | 53788 KB |
2_11.txt | RE | 482 ms | 54956 KB |
2_12.txt | WA | 857 ms | 102652 KB |
2_13.txt | WA | 1043 ms | 106096 KB |
2_14.txt | WA | 853 ms | 102260 KB |
2_15.txt | WA | 856 ms | 102008 KB |