Cod sursă (job #654402)

Utilizator avatar TheAndrei Enache Andrei Alexandru TheAndrei IP ascuns
Problemă Lanterna Compilator cpp-32 | 0,80 kb
Rundă Arhiva de probleme Status evaluat
Dată 5 iul. 2022 19:21:54 Scor 50
#include <iostream>
#include <fstream>

using namespace std;

ifstream fin("lanterna.in");
ofstream fout("lanterna.out");
int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}

int main()
{
    int n, i, min1=1000001, min2=1000002, max1=0, max2=0, s=0, s1, s2;
    fin>>n;
    int t[n+1];
    for(i=1;i<=n;i++){
        fin>>t[i];
    }
    t[0] = 0;
    qsort(t, n + 1, sizeof(int), compare);

    for(i=n;i>=4;i-=2){
        s1 = 2 * t[2] + t[1] + t[i];
        s2 = t[i] + t[i-1] + 2 * t[1];
//        if(s1 < s2) cout << t[2] << " " << t[1] << " " << t[i] << " "  << t[2];
//        else cout << t[i] << " " << t[1] << " " << t[i-1] << " " << t[1];
//        cout << " ";
        s += s1 < s2 ? s1 : s2;
    }
    s += t[2];
    fout<<s;
    return 0;
}