Cod sursă (job #372871)

Utilizator avatar ezioconnor Vlad - Gabriel Iftimescu ezioconnor IP ascuns
Problemă Lanterna Compilator cpp | 0.53 kb
Rundă Arhiva de probleme Status evaluat
Dată 29 mar. 2018 10:58:40 Scor 100
#include <fstream>
#include <algorithm>

using namespace std;

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

int n, a, b, v[100001], i;
long long s = 0;

int main()
{
    in >> n;
    for (i = 1; i <= n; ++i)
        in>>v[i];
    sort(v + 1, v + n + 1);
    while (n >= 4)
    {   a = v[1] + v[2] * 2 + v[n];
        b = v[1] * 2 + v[n - 1] + v[n];
        s += min(a, b);
        n -= 2;
    }
    if (n == 3)
        s += v[1] + v[2] + v[3];
    else
        s += v[2];
    out << s;
    return 0;
}