Cod sursă (job #803236)

Utilizator avatar penguinvanilic Perciun Cristiano penguinvanilic IP ascuns
Problemă Lanterna Compilator cpp-32 | 0,77 kb
Rundă lasm_09_01_2025_clasa11 Status evaluat
Dată 9 ian. 2025 15:45:35 Scor 80
#include <bits/stdc++.h>
using namespace std;
 
int main() {
    ifstream fin("lanterna.in");
    ofstream fout("lanterna.out");
 
    int N;
    fin >> N;
 
    vector<long long> t(N);
    for(int i = 0; i < N; i++){
        fin >> t[i];
    }
 
    sort(t.begin(), t.end());
 
    long long totalTime = 0;    
    int n = N; 
    while(n > 3) {
        long long costA = t[1] + t[0] + t[n-1] + t[1];
        long long costB = t[n-1] + t[0] + t[n-2] + t[0];
        totalTime += min(costA, costB);
        n -= 2; 
    }
 
    if(n == 3) {
        totalTime += (t[0] + t[1] + t[2]);
    } 
    else if(n == 2) {
        totalTime += t[1];
    } 
    else if(n == 1) {
        totalTime += t[0];
    }
 
    fout << totalTime << "\n";
    return 0;
}