codeforces 461A Appleman and Toastman

来源:互联网 发布:php 日期格式 编辑:程序博客网 时间:2024/05/16 08:20

题意:

思路:

贪心。既然希望尽量达到最大,排好序,每次都把最小的那个去掉,这样比它大的数就可以多加几次。

AC代码:

#include <cstring>#include <cstdlib>#include <algorithm>#include <iostream>#include <cstdio>typedef long long ll;using namespace std;const int MAXN = 3*100005;long long sum[MAXN], a[MAXN];int main(){    int n;        scanf("%d", &n);        for(int i = 1;i <= n; i++)                scanf("%I64d", &a[i]);    //special    if(n == 1)  {cout<<a[1]<<endl; return 0;}    if(n == 2)  {cout<<(a[1]+a[2])*2<<endl; return 0;}    //normal    sort(a+1, a+n+1);    for(int i = 1;i <= n; i++)        sum[i] = sum[i-1] + a[i];    long long res = 0;    res += sum[n];    for(int i = n, j = 1;j <= n; j++)    {        if(i - j == 1)        {            res += sum[i] - sum[j-1];            break;        }        res += a[j];        res += sum[i] - sum[j];    }    cout<<res<<endl;    return 0;}


0 0