您的位置:首页 > 其它

Codeforces 624B Making a String 【水题】

2016-02-13 18:19 387 查看
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.

Sample test(s)

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

 

题意:给你n个字母('a', 'a'+n)以及每个字母最多出现次数a[i],要求你组成一个最长的串使得串中任意两个字母出现次数不能相等。

 

AC代码:

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#define INF 1000000
#define eps 1e-8
#define MAXN (10000+10)
#define MAXM (2000000+10)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while((a)--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
#pragma comment(linker, "/STACK:102400000,102400000")
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
map<int, bool> fp;
int a[30];
int main()
{
int n; Ri(n); LL ans = 0; fp.clear();
for(int i = 1; i <= n; i++) Ri(a[i]);
sort(a+1, a+n+1);
for(int i = n; i >= 1; i--)
{
int temp = a[i];
while(temp > 0 && fp[temp]){
temp--;
}
if(temp > 0)
{
ans += 1LL*temp;
fp[temp] = true;
}
}
Pl(ans);
return 0;
}


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