Cod sursă (job #546610)

Utilizator avatar racovcenl Racovcen Laurentiu racovcenl IP ascuns
Problemă Lanterna Compilator cpp | 0,86 kb
Rundă lasm_19_03_2020_10 Status evaluat
Dată 19 mar. 2020 16:59:09 Scor 100
#include<cstdio>
#include<algorithm>
using namespace std;
const int LIM = 100010;
int v[LIM], l1=0, l2;
int main(){
    FILE *in=fopen("lanterna.in","r"), *out=fopen("lanterna.out","w");
    long long scenario1, scenario2, cost=0; int n;
    fscanf(in, "%d",&n);
    for(int i=0; i<n; i++)
        fscanf(in, "%d", &v[i]);
    sort(v, v+n);
    l2=n-1;
    while(l2-l1+1>3){
        scenario1=2*v[l1+1]+v[l1]+v[l2];
        scenario2=v[l2]+v[l2-1]+2*v[l1];
        if(scenario1<scenario2){
            cost+=scenario1;
        }else{
            cost+=scenario2;
        }
        l2-=2;
    }
    if(l2-l1+1==3){
        cost+=v[l1]+v[l2]+v[l2-1];
    }else if(l2-l1+1==2){
        if(v[l2]>v[l1])
            cost+=v[l2]; else cost+=v[l1];
    }else if(l2-l1+1==1){
        cost+= v[l1];
    }
    fprintf(out, "%lld\n", cost);
    return 0;
}