这个题由于可以自由的加括号,那么负数也可以变成加,想加就加想减就减
1.特殊情况:m=0,直接输出和
2.一般情况:把所有数排个序,最大的拿出来,放首项,把最小的数拿出来,给他一个减号,再套一个括号
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pw = new PrintWriter(System.out);
static int N = 200010, n, m;
static int a[] = new int[N];
public static void main(String[] args) throws IOException {
String s[] = br.readLine().split(" ");
n = Integer.parseInt(s[0]);
m = Integer.parseInt(s[1]);
int k = n + m + 1;
s = br.readLine().split(" ");
for (int i = 0; i < k; i++) a[i] = Integer.parseInt(s[i]);
long res = 0;
if (m == 0) {
for (int i = 0; i < k; i++) res += a[i];
pw.print(res);
} else {
Arrays.sort(a, 0, k);
res = a[--k] - a[0];
for (int i = 1; i < k; i++) res += Math.abs(a[i]);
pw.print(res);
}
pw.flush();
pw.close();
br.close();
}
}