您的位置:首页 > 其它

Codeforces 624

2016-05-05 13:07 309 查看
B. Making a String

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
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.

Input
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.

Output
Print a single integer — the maximum length of the string that meets all the requirements.

Examples

input
3
2 5 5


output
11


input
3
1 1 2


output
3


Note
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);
}
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: