Cod sursă (job #393627)

Utilizator avatar Mars312 Vlad Tronciu Mars312 IP ascuns
Problemă Lanterna Compilator cpp | 0,70 kb
Rundă Arhiva de probleme Status evaluat
Dată 12 oct. 2018 17:31:33 Scor 100
#include <fstream>
#include <algorithm>

using namespace std;
const int NMAX= 100000;
int t[NMAX+5];

int main()
{
    ifstream in("lanterna.in");
    ofstream out("lanterna.out");

    int x, n, i;
    in >> n;
    for(i=1; i<=n; i++)
    {
        in >> t[i];
    }
    sort(&t[1], &t[n+1]);

    long long tp;
    tp = 0;
    while(n>=4)
    {
        if((t[n]+t[n-1]+2*t[1])<(t[n]+2*t[2]+t[1]))
        {
            tp+=(t[n]+t[n-1]+2*t[1]);
        }
        else
        {
            tp+=(t[n]+2*t[2]+t[1]);
        }
        n-=2;
    }
    if (n==2)
    {
        tp+=t[2];
    }
    else
    {
        tp+=(t[1]+t[2]+t[3]);
    }
    out << tp;
    return 0;
}