Cod sursă (job #567719)

Utilizator avatar Alex_tz307 Lorintz Alexandru Alex_tz307 IP ascuns
Problemă Lanterna Compilator cpp | 0,70 kb
Rundă Arhiva de probleme Status evaluat
Dată 19 oct. 2020 18:46:21 Scor 90
#include<bits/stdc++.h>

using namespace std;

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

typedef unsigned long long ull;

inline void min_self(ull& a, ull b) {
    a = min(a, b);
}

int main() {
    fin.sync_with_stdio(false);
    fout.sync_with_stdio(false);
    fin.tie(nullptr);
    fout.tie(nullptr);
    int N;
    fin >> N;
    vector < int > a(N);
    for(int& x : a)
        fin >> x;
    sort(a.begin(), a.end());
    ull cost = a[0] * (N - 2);
    for(int i = 1; i < N; ++i)
        cost += a[i];
    ull ans = cost;
    for(int k = N - 2; k > 1; k -= 2) {
        cost -= a[k] + a[0] - 2 * a[1];
        min_self(ans, cost);
    }
    fout << ans;
}