Cod sursă (job #74928)

Utilizator avatar rpdstrike Puscasu Felix rpdstrike IP ascuns
Problemă Lanterna Compilator cpp | 0,55 kb
Rundă Arhiva de probleme Status evaluat
Dată 6 sept. 2014 08:57:30 Scor 100
#include <fstream>
#include <algorithm>

using namespace std;

ifstream in("lanterna.in");
ofstream out("lanterna.out");

const int NMAX = 100001;

int v[NMAX+1];
long long N, x, y, S= 0;
int main()
{

    in >> N;
    for( int i=1;  i<=N;  i++ )  in >> v[i];
    sort( v+1, v+1+N );

    while( N > 3 ) {
        x= v[1]+2*v[2]+v[N];
        y= v[N]+2*v[1]+v[N-1];
        if( x < y )  S+= x;
        else S+= y;
        N-= 2;
    }

    if( N == 3 )  S+= v[1]+v[2]+v[3];
    else S+= v[2];

    out << S << '\n';

    return 0;
}