Cod sursă (job #803007)

Utilizator avatar nexuss eselte nexuss IP ascuns
Problemă Lanterna Compilator cpp-32 | 1,03 kb
Rundă Arhiva de probleme Status evaluat
Dată 9 ian. 2025 12:22:54 Scor 50
#include <bits/stdc++.h>
using namespace std;


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

    int n;
    fin >> n;

    int A[10010];
    for (int i = 0; i <=n-1; ++i) {
        fin >> A[i];
    }
    bool u= true; 
    int k=n-1;
    while (u) {
        u=false; 
        int l=k;
        for (int j=0; j<= l -1; j++) {
            if (A[j+1]<A[j]) {swap(A[j+1], A[j]); k=j; u=true;}
                               }}


    int total = 0;
    int i = n - 1;

    while (i >= 0) {
        if (i == 0) {
            total += A[0];
            break;
        } else if (i == 1) {
            total += A[1];
            break;
        } else if (i == 2) {
            total += A[0] + A[1] + A[2];
            break;
        } else {
            int strategy1 = A[0] + A[1] + A[i] + A[1];
            int strategy2 = A[0] + A[i] + A[0] + A[i - 1];
            total += min(strategy1, strategy2);
            i -= 2;
        }
    }

    fout << total;

    return 0;
}