题解 | 小红拿石子1.0

小红拿石子1.0

https://www.nowcoder.com/practice/537ba6fa73d84d26b03196caed197772

#include <iostream>
#include <algorithm>

using namespace std;

typedef long long LL;
const int N = 2e5 + 10;
int a[N];


int main() {
    int n;
    cin>>n;
    for(int i = 1;i <= n;i++){
        cin>>a[i];
    }
    sort(a + 1,a + n + 1);
    long long res = 0;
    int j = n + 1;
    int cnt = 1;
    while(j >= 2){
        j--;
        if(a[j] > 0 && j >= 1) res += a[j];
        else break;
        a[j - 1] = a[j - 1] - cnt;
        cnt++;
    } 
    cout<<res;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务