1 second
256 megabytes
standard input
standard output
You are given an alphabet consisting of n letters, your task is to make a string of the maximum possible length so that the following conditions are satisfied:
- the i-th letter occurs in the string no more than ai times;
- the number of occurrences of each letter in the string must be distinct for all the letters that occurred in the string at least once.
The first line of the input contains a single integer n (2 ≤ n ≤ 26) — the number of letters in the alphabet.
The next line contains n integers ai (1 ≤ ai ≤ 109) — i-th of these integers gives the limitation on the number of occurrences of the i-th character in the string.
Print a single integer — the maximum length of the string that meets all the requirements.
3
2 5 5
11
3
1 1 2
3
For convenience let's consider an alphabet consisting of three letters: "a", "b", "c". In the first sample, some of the optimal strings are: "cccaabbccbb", "aabcbcbcbcb". In the second sample some of the optimal strings are: "acc", "cbc".
题目的意思就是,给定你每个字母可以出现的次数,然后根据题目中的两个条件,计算最后字符串可能的最大长度。
条件一,每个字母最多出现给定的 ai 次;条件二,不能有两个字母出现的次数相同。ok,我用了一个set。
package Sunny; import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner; /**
* Created by lenovo on 2016-05-05.
*/
public class CF624B {
public static void main(String[] args){
Scanner scanner = new Scanner(new InputStreamReader(System.in));
int n = scanner.nextInt();
long[] arr = new long[26];
for(int i = 0; i < n; ++i){
arr[i] = scanner.nextLong();
} Arrays.sort(arr);
long num = 0; long ans = 0;
HashSet<Long> set = new HashSet<Long>();
for(int i = 25; i >= 0; --i){
while(set.contains(arr[i])){
arr[i]--;
}
set.add(arr[i]);
}
long tmp;
Iterator<Long> iterator = set.iterator();
while(iterator.hasNext()){
tmp = iterator.next();
if(tmp > 0)
ans += tmp;
}
System.out.println(ans);
}
}