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

题意:你可以用n个字母来构成一个字符串,然后给你n个值ai代表分别对应的第i个字符使用次数不得超过ai且所有字母出现的次数都不得相同,求最长的字符串长度

题解:用一个数组记录使用过的字母出现的次数,重复的次数不可使用

#include<stdio.h>
#include<string.h>
#include<string>
#include<math.h>
#include<algorithm>
#define LL long long
#define PI atan(1.0)*4
#define DD double
#define MAX 100100
#define mod 100
#define dian 1.000000011
#define INF 0x3f3f3f
using namespace std;
LL s[MAX];
LL vis[30];//记录使用过的字母的使用次数
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
int n,j,i,t,k,l;
LL m;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
scanf("%lld",&s[i]);
LL sum=0;k=0;
for(i=0;i<n;i++)
{
m=s[i];
sort(vis,vis+k,cmp);
for(j=0;j<k;j++)
{
if(m==0)
break;
if(m==vis[j])//判断当前字母可出现的次数是否被使用过
m--;
}
vis[k++]=m;//更新使用过的次数
sum+=m;
}
printf("%lld\n",sum);
}
return 0;
}

  

codeforces 624B Making a String的更多相关文章

  1. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  2. 【动态规划】【最短路】Codeforces 710E Generate a String

    题目链接: http://codeforces.com/problemset/problem/710/E 题目大意: 问写N个字符的最小花费,写一个字符或者删除一个字符花费A,将当前的字符数量翻倍花费 ...

  3. codeforces 632C The Smallest String Concatenation

    The Smallest String Concatenation 题目链接:http://codeforces.com/problemset/problem/632/C ——每天在线,欢迎留言谈论. ...

  4. 【Codeforces 1120C】Compress String

    Codeforces 1120 C 题意:给一个串\(S\),将这个串分成\(t_1..t_m\),如果\(t_i\)在\(t_1..t_{i-1}\)中作为子串出现过,那么这个的代价是\(b\),否 ...

  5. 【codeforces 797C】Minimal string

    [题目链接]:http://codeforces.com/contest/797/problem/C [题意] 一开始,给你一个字符串s:两个空字符串t和u; 你有两种合法操作; 1.将s的开头字符加 ...

  6. Codeforces 1144 E. Median String

    原题链接:https://codeforces.com/problemset/problem/1144/E tag:字符串模拟,大整数. 题意:给定两个字符串,求字典序中间串. 思路:可以把这个题当做 ...

  7. CodeForces 710E Generate a String (DP)

    题意:给定 n,x,y,表示你要建立一个长度为 n的字符串,如果你加一个字符要花费 x时间,如果你复制前面的字符要花费y时间,问你最小时间. 析:这个题,很明显的DP,dp[i]表示长度为 i 的字符 ...

  8. Codeforces 56D Changing a String

    http://codeforces.com/contest/56/problem/D 题目大意: 一个字符串变为目标字符串,可以执行插入,置换和删除3种操作,求最少操作数. 思路:dp[i][j]代表 ...

  9. codeforces 632C. The Smallest String Concatenation 排序

    题目链接 给出n个字符串, 将他们连在一起, 求连玩之后字典序最小的那种情况. 按a+b<b+a排序.... #include <iostream> #include <vec ...

随机推荐

  1. C++STL之整理算法

    这里主要介绍颠倒.旋转.随机排列和分类4中常见的整理算法 1.颠倒(反转) void reverse(_BidIt _First, _BidIt _Last) _OutIt reverse_copy( ...

  2. gulp browser-sync自动刷新插件

    很久没弄gulp了,都快忘了,今天又来温习下browser-sync 自动刷新插件,在安装的时候出现以下提示: $ npm install browser-sync --save-dev> ws ...

  3. bzoj1877: [SDOI2009]晨跑

    挺裸的最小费用最大流... #include<cstdio> #include<queue> #include<cstring> #include<iostr ...

  4. 四种途径将HTML5 web应用变成android应用

    作为下一代的网页语言,HTML5拥有很多让人期待已久的新特性.HTML5的优势之一在于能够实现跨平台游戏编码移植,现在已经有很多公司在移动 设备上使用HTML5技术.随着HTML5跨平台支持的不断增强 ...

  5. linq xml读取

    <?xml version="1.0" encoding="UTF-8" ?> <cache> <chatOld> < ...

  6. Java知识点:内部类

    内部类class文件命名规则 普通内部类.静态内部类:<Outer>\$<Inner>.class,其中<Outer>为外部类类名,<Inner>为内部 ...

  7. tomcat启动中提示 - consider increasing the maximum size of the cache

    tomcat启动过程中提示: org.apache.catalina.webresources.Cache.getResource Unable to add the resource at xxx ...

  8. noip2008提高组题解

    第一题:笨小猴 模拟   第二题:火柴棒等式 搜索 深搜不用说,确定出两个加数然后判断能否拼出等式. 枚举确实不太好搞,因为枚举范围不确定,太大了容易超时,太小了容易漏解.不过这题的数据貌似很温和,我 ...

  9. LeetCode: divideInteger

    Title: Divide two integers without using multiplication, division and mod operator. If it is overflo ...

  10. RAID0_RAID1_RAID10_RAID5各需几块盘才可组建

    RAID 0 RAID 0即Data Stripping(数据分条技术).整个逻辑盘的数据是被分条(stripped)分布在多个物理磁盘上,可以并行读/写,提供最快的速度,但没有冗余能力.要求至少两个 ...