AcWing 3627. 最大差值
原题链接
中等
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0) {
int n = sc.nextInt();
int k = sc.nextInt();
long[] a = new long[n];
for(int i = 0; i < n; i++) {
a[i] = sc.nextLong();
}
Arrays.sort(a);
if (n == 1) {
System.out.println(0);
continue;
}
long s = 0;
for(int i = n - 1, j = 0; i >= 0; i--) {
a[i - 1] += a[i];
s = a[i - 1];
j++;
if (j == k) {
break;
}
}
System.out.println(s);
}
}
}