Cod sursă (job #60776)

Utilizator avatar horatiu11 Ilie Ovidiu Horatiu horatiu11 IP ascuns
Problemă Lanterna Compilator cpp | 0,68 kb
Rundă Arhiva de probleme Status evaluat
Dată 5 mar. 2014 22:10:48 Scor 100
//horatiu11
# include <cstdio>
# include <algorithm>
# define nmax 100001
using namespace std;
int n,a[nmax];
long long s;
int main()
{
    int i;
    freopen("lanterna.in","r",stdin);
    freopen("lanterna.out","w",stdout);
    scanf("%d",&n);
    for(i=1;i<=n;++i)
        scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    while(n)
    {
        if(n>3)
        {
            s+=min(a[1]+2*a[2]+a[n],2*a[1]+a[n-1]+a[n]);
            n-=2;
        }
        else if(n==3)
        {
            s+=a[1]+a[2]+a[3];
            n-=3;
        }
        else if(n==2)
        {
            s+=a[2];
            n-=2;
        }
    }
    printf("%lld\n",s);
    return 0;
}