题目描述
有一个长度为 n 的数组(n 是 10 的倍数),每个数 ai 都是区间 [0, 9] 中的整数。小明发现数组里每种数出现的次数不太平均,而更改第 i 个数的代价为bi,他想更改若干个数的值使得这 10 种数出现的次数相等(都等于n/10),请问代价和最少为多少。
输入格式
输入的第一行包含一个正整数 n 。
接下来 n 行,第 i 行包含两个整数 ai , bi ,用一个空格分隔。
输出格式
输出一行包含一个正整数表示答案。
样例输入
复制
10 1 1 1 2 1 3 2 4 2 5 2 6 3 7 3 8 3 9 4 10
样例输出
复制
27
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] arr = new int[n][2];
int a = n / 10;
long num = 0;
for (int i = 0; i < n; i++) {
arr[i][0] = sc.nextInt();
arr[i][1] = sc.nextInt();
}
Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if (o1[0] == o2[0]) {
return o2[1] - o1[1];
}
return o1[0] - o2[0];
}
});
for (int i = 1; i < n; i++) {
int x = 1;
while (i < n) {
if (arr[i][0] != arr[i - 1][0]) break;
x++;
if (x > a) num += arr[i][1];
i++;
}
}
System.out.println(num);
}
}