fork download
  1. import java.util.*;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. int n = sc.nextInt();
  7. int k = sc.nextInt();
  8. int[] a = new int[n];
  9. for(int i = 0; i < n; i++) {
  10. a[i] = sc.nextInt();
  11. }
  12.  
  13. // 初始等待时间是 a[1] 到 a[n-1] 的和(不包括第1首歌)
  14. long totalWait = 0;
  15. for(int i = 1; i < n; i++) {
  16. totalWait += a[i];
  17. }
  18.  
  19. // 提取第2首歌之后的 n-1 首,排序后去掉最大的 k 个
  20. Integer[] rest = new Integer[n - 1];
  21. for(int i = 1; i < n; i++) {
  22. rest[i - 1] = a[i];
  23. }
  24. Arrays.sort(rest, Collections.reverseOrder()); // 降序
  25.  
  26. for(int i = 0; i < k; i++) {
  27. totalWait -= rest[i]; // 移除最长的 k 首
  28. }
  29.  
  30. System.out.println(totalWait);
  31. }
  32. }
  33.  
Success #stdin #stdout 0.11s 56632KB
stdin
3 1
1 2 3
stdout
2