B. Making a String

题目连接:

http://codeforces.com/contest/624/problem/B

Description

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 Input

3

2 5 5

Sample Output

11

Hint

题意

有n个字母,每个字母最多出现ai次,但是每个字母的出现次数都要求不一样

问你这个字符串最长多长

题解:

显然从大到小排序,然后贪心就好了,能选就选,不能选就--就行了

注意小心被减到负数了

代码

#include<bits/stdc++.h>
using namespace std; int a[30];
map<int,int> H;
int main()
{
int n;scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+1+n);
int flag = 0;
long long ans = 0;
for(int i=n;i>=1;i--)
{
int tmp = a[i];
while(H[tmp])tmp--;
if(tmp)
{
H[tmp]=1;
ans+=tmp;
}
}
cout<<ans<<endl;
}

AIM Tech Round (Div. 2) B. Making a String 贪心的更多相关文章

  1. AIM Tech Round (Div. 2) C. Graph and String 二分图染色

    C. Graph and String 题目连接: http://codeforces.com/contest/624/problem/C Description One day student Va ...

  2. AIM Tech Round (Div. 2) C. Graph and String

    C. Graph and String time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. AIM Tech Round (Div. 1) D. Birthday 数学 暴力

    D. Birthday 题目连接: http://www.codeforces.com/contest/623/problem/D Description A MIPT student named M ...

  4. AIM Tech Round (Div. 2) D. Array GCD dp

    D. Array GCD 题目连接: http://codeforces.com/contest/624/problem/D Description You are given array ai of ...

  5. AIM Tech Round (Div. 2) A. Save Luke 水题

    A. Save Luke 题目连接: http://codeforces.com/contest/624/problem/A Description Luke Skywalker got locked ...

  6. Codeforces AIM Tech Round (Div. 2)

    这是我第一次完整地参加codeforces的比赛! 成绩 news standings中第50. 我觉这个成绩不太好.我前半小时就过了前三题,但后面的两题不难,却乱搞了1.5h都没有什么结果,然后在等 ...

  7. AIM Tech Round (Div. 2) B

    B. Making a String time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  8. AIM Tech Round (Div. 2) A

    A. Save Luke time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  9. AIM Tech Round (Div. 1) C. Electric Charges 二分

    C. Electric Charges 题目连接: http://www.codeforces.com/contest/623/problem/C Description Programmer Sas ...

随机推荐

  1. 实现两个N*N矩阵的乘法,矩阵由一维数组表示

    实现两个N*N矩阵的乘法,矩阵由一维数组表示. 先介绍一下矩阵的加法: void Add(int rows, int cols) { ;i<rows;i++) { ;j<cols;j++) ...

  2. 12、NFC技术:读写NFC标签中的Uri数据

    功能实现,如下代码所示: 读写NFC标签的Uri 主Activity import cn.read.write.uri.library.UriRecord; import android.app.Ac ...

  3. location.hash来保持页面状态

    /*本例是为了在客户端页面返回时保存状态,采用hash值记录的模式,为了使用方便所写的存取hash值的库,时间仓促,望指出错误.*/var pageStateHash = { hashArray: [ ...

  4. 【转】Linux mount/unmount命令

    转自:http://www.cnblogs.com/xd502djj/p/3809375.html 格式:mount [-参数] [设备名称] [挂载点] 其中常用的参数有:-a 安装在/etc/fs ...

  5. 大数据时代的数据存储,非关系型数据库MongoDB

    在过去的很长一段时间中,关系型数据库(Relational Database Management System)一直是最主流的数据库解决方案,他运用真实世界中事物与关系来解释数据库中抽象的数据架构. ...

  6. pthread_cond_timedwait时间设置

    最近工作中需要在ACodec中起一个pthread,并每间隔100ms统计一次buffer的状态,在程序中使用pthread_cond_timedwait来设置时间间隔,但在使用中发现当超时时间设置成 ...

  7. leetcode@ [146] LRU Cache (TreeMap)

    https://leetcode.com/problems/lru-cache/ Design and implement a data structure for Least Recently Us ...

  8. 去掉StringBuilder或String 最后一个项逗号

    一. sb.Length = sb.Length - 1; 二. stringBuilder.Remove(stringBuilder.ToString().LastIndexOf(','), 1); ...

  9. Dependency Injection学习笔记

    component把需要依赖者(CoffeeMaker)和供应提供者(Heater, Pump)联系起来 使用 区别:上的的依赖是内部创建的,下面的依赖是外面传进来的 注入方式

  10. 微软IOC容器Unity简单代码示例3-基于约定的自动注册机制

    @(编程) [TOC] Unity在3.0之后,支持基于约定的自动注册机制Registration By Convention,本文简单介绍如何配置. 1. 通过Nuget下载Unity 版本号如下: ...