|
| 1 | +import java.io.OutputStream; |
| 2 | +import java.io.IOException; |
| 3 | +import java.io.InputStream; |
| 4 | +import java.io.OutputStream; |
| 5 | +import java.io.PrintWriter; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.io.BufferedWriter; |
| 8 | +import java.io.Writer; |
| 9 | +import java.io.OutputStreamWriter; |
| 10 | +import java.util.InputMismatchException; |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.InputStream; |
| 13 | + |
| 14 | +/** |
| 15 | + * Built using CHelper plug-in |
| 16 | + * Actual solution is at the top |
| 17 | + * |
| 18 | + * @author Rishabhdeep Singh |
| 19 | + */ |
| 20 | +public class Main { |
| 21 | + public static void main(String[] args) { |
| 22 | + InputStream inputStream = System.in; |
| 23 | + OutputStream outputStream = System.out; |
| 24 | + InputReader in = new InputReader(inputStream); |
| 25 | + OutputWriter out = new OutputWriter(outputStream); |
| 26 | + MultipleTestcases solver = new MultipleTestcases(); |
| 27 | + int testCount = Integer.parseInt(in.next()); |
| 28 | + for (int i = 1; i <= testCount; i++) |
| 29 | + solver.solve(i, in, out); |
| 30 | + out.close(); |
| 31 | + } |
| 32 | + |
| 33 | + static class MultipleTestcases { |
| 34 | + public void solve(int testNumber, InputReader in, OutputWriter out) { |
| 35 | + int n = in.nextInt(); |
| 36 | + int m = in.nextInt(); |
| 37 | + --n; |
| 38 | + --m; |
| 39 | + int[] x = in.nextIntArray(n); |
| 40 | + int[] y = in.nextIntArray(m); |
| 41 | + int s1 = 0, s2 = 0; |
| 42 | + for (int i = 0; i < n; i++) { |
| 43 | + s1 += x[i]; |
| 44 | + } |
| 45 | + for (int i = 0; i < m; i++) { |
| 46 | + s2 += y[i]; |
| 47 | + } |
| 48 | + Arrays.sort(x); |
| 49 | + Arrays.sort(y); |
| 50 | + int ans = s1 + s2; |
| 51 | + for (int i = n - 1, j = m - 1; i >= 0 && j >= 0; ) { |
| 52 | + if (y[j] >= x[i]) { |
| 53 | + ans += s1; |
| 54 | + s2 -= y[j]; |
| 55 | + --j; |
| 56 | + } else { |
| 57 | + ans += s2; |
| 58 | + s1 -= x[i]; |
| 59 | + --i; |
| 60 | + } |
| 61 | + } |
| 62 | + out.println(ans); |
| 63 | + } |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + static class OutputWriter { |
| 68 | + private final PrintWriter writer; |
| 69 | + |
| 70 | + public OutputWriter(OutputStream outputStream) { |
| 71 | + writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); |
| 72 | + } |
| 73 | + |
| 74 | + public OutputWriter(Writer writer) { |
| 75 | + this.writer = new PrintWriter(writer); |
| 76 | + } |
| 77 | + |
| 78 | + public void close() { |
| 79 | + writer.close(); |
| 80 | + } |
| 81 | + |
| 82 | + public void println(int i) { |
| 83 | + writer.println(i); |
| 84 | + } |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + static class InputReader { |
| 89 | + private InputStream stream; |
| 90 | + private byte[] buf = new byte[1024]; |
| 91 | + private int curChar; |
| 92 | + private int numChars; |
| 93 | + private InputReader.SpaceCharFilter filter; |
| 94 | + |
| 95 | + public InputReader(InputStream stream) { |
| 96 | + this.stream = stream; |
| 97 | + } |
| 98 | + |
| 99 | + public int read() { |
| 100 | + if (numChars == -1) { |
| 101 | + throw new InputMismatchException(); |
| 102 | + } |
| 103 | + if (curChar >= numChars) { |
| 104 | + curChar = 0; |
| 105 | + try { |
| 106 | + numChars = stream.read(buf); |
| 107 | + } catch (IOException e) { |
| 108 | + throw new InputMismatchException(); |
| 109 | + } |
| 110 | + if (numChars <= 0) { |
| 111 | + return -1; |
| 112 | + } |
| 113 | + } |
| 114 | + return buf[curChar++]; |
| 115 | + } |
| 116 | + |
| 117 | + public int nextInt() { |
| 118 | + int c = read(); |
| 119 | + while (isSpaceChar(c)) { |
| 120 | + c = read(); |
| 121 | + } |
| 122 | + int sgn = 1; |
| 123 | + if (c == '-') { |
| 124 | + sgn = -1; |
| 125 | + c = read(); |
| 126 | + } |
| 127 | + int res = 0; |
| 128 | + do { |
| 129 | + if (c < '0' || c > '9') { |
| 130 | + throw new InputMismatchException(); |
| 131 | + } |
| 132 | + res *= 10; |
| 133 | + res += c - '0'; |
| 134 | + c = read(); |
| 135 | + } while (!isSpaceChar(c)); |
| 136 | + return res * sgn; |
| 137 | + } |
| 138 | + |
| 139 | + public String nextString() { |
| 140 | + int c = read(); |
| 141 | + while (isSpaceChar(c)) { |
| 142 | + c = read(); |
| 143 | + } |
| 144 | + StringBuilder res = new StringBuilder(); |
| 145 | + do { |
| 146 | + if (Character.isValidCodePoint(c)) { |
| 147 | + res.appendCodePoint(c); |
| 148 | + } |
| 149 | + c = read(); |
| 150 | + } while (!isSpaceChar(c)); |
| 151 | + return res.toString(); |
| 152 | + } |
| 153 | + |
| 154 | + public boolean isSpaceChar(int c) { |
| 155 | + if (filter != null) { |
| 156 | + return filter.isSpaceChar(c); |
| 157 | + } |
| 158 | + return isWhitespace(c); |
| 159 | + } |
| 160 | + |
| 161 | + public static boolean isWhitespace(int c) { |
| 162 | + return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; |
| 163 | + } |
| 164 | + |
| 165 | + public String next() { |
| 166 | + return nextString(); |
| 167 | + } |
| 168 | + |
| 169 | + public int[] nextIntArray(int n) { |
| 170 | + int[] array = new int[n]; |
| 171 | + for (int i = 0; i < n; ++i) array[i] = nextInt(); |
| 172 | + return array; |
| 173 | + } |
| 174 | + |
| 175 | + public interface SpaceCharFilter { |
| 176 | + public boolean isSpaceChar(int ch); |
| 177 | + |
| 178 | + } |
| 179 | + |
| 180 | + } |
| 181 | +} |
| 182 | + |
0 commit comments