现在小红想知道,最终合集最大的优秀度是多少?
第一行输入两个正整数,代表笔记的数量,以及小红准备选择的合集大小。
第二行输入个正整数
,代表每篇笔记的点赞数。
第三行输入个正整数
,代表每篇笔记的评论数。
一个正整数,代表最终最大的优秀度。
4 2 1 2 3 4 3 4 2 1
10
选第二篇和第三篇即可。
//排序,优先队列维护最大点赞数
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt();
List<Integer> a = new ArrayList<>();
List<Integer> b = new ArrayList<>();
for(int i = 0; i < n; i++) {
a.add(in.nextInt());
}
for(int i = 0; i < n; i++) {
b.add(in.nextInt());
}
List<int[]> list = new ArrayList<>();
for(int i = 0; i < n; i++) {
list.add(new int[]{a.get(i), b.get(i)});
}
Collections.sort(list, (o1, o2) -> {
if(o1[1] != o2[1]) {
return Integer.compare(o2[1], o1[1]);
}else {
return Integer.compare(o2[0], o1[0]);
}
});
PriorityQueue<Integer> heap = new PriorityQueue<>();
long sum = 0;
long max = 0;
for(int[] arr : list) {
int ai = arr[0];
int bi = arr[1];
heap.add(ai);
sum += ai;
if(heap.size() > k) {
int remove = heap.poll();
sum -= remove;
}
if(heap.size() == k) {
long current = sum * bi;
if(current > max) {
max = current;
}
}
}
System.out.print(max);
}
}