zoukankan      html  css  js  c++  java
  • codeforces624b Making a String

     
    Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
    Submit Status

    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

    Input
    3
    2 5 5
    Output
    11
    Input
    3
    1 1 2
    Output
    3

    Hint

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

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int a[26];
    bool cmp(int a,int b){
        return a>b;
    }
    int main(){
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
        }
        int tmp=0;
        long long ans=0;
        sort(a,a+n,cmp);
        ans+=a[0];
        tmp=a[0];
        for(int i=1;i<n;i++){
            if(a[i]>=tmp) tmp=tmp-1;
            else tmp=a[i];
            if(tmp<=0) break;
            ans+=tmp;
        }
        printf("%I64d
    ",ans);
        return 0;
    }
    View Code
  • 相关阅读:
    首次使用随便写点哦
    js中call、apply和bind的区别
    前端的事件流以及事件处理程序
    javascript中数组的深拷贝的方法
    我的第一篇博客
    圆盘转动按钮-react native
    鼠标拖拽删除
    js基础 -----鼠标事件(按下 拖拽)
    清除浮动的几种常用方法
    VUE常见问题解决
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/5686207.html
Copyright © 2011-2022 走看看