题解 | 小红闯关

小红闯关

https://www.nowcoder.com/practice/7ce4b75f7a304be481e73bc4dd2705a4

import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] a = new int[n];
        for(int i = 0;i < n;i ++ ) a[i] = sc.nextInt();
        long sum=0;
        PriorityQueue<Integer>pq=new PriorityQueue<>(Comparator.reverseOrder());
        for(int i=n-1;i>=0;i--){
            sum+=a[i];
            if(i>=k){
                pq.offer(a[i]);
                if(i%k==0)sum-=pq.poll();
            }
        }
        System.out.println(sum);
    }
}

全部评论

相关推荐

02-04 13:52
已编辑
山西农业大学 Java
点赞 评论 收藏
分享
对空六翼:你真幸运,碰见这么好的人,不像我,秋招的时候被室友骗进cx了
实习好累,可以辞职全力准...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务