Cod sursă (job #374442)

Utilizator avatar Vodka Yushkov Arseniy Vladislavovich Vodka IP ascuns
Problemă Lanterna Compilator cpp | 0,57 kb
Rundă Arhiva de probleme Status evaluat
Dată 1 apr. 2018 10:17:51 Scor 90
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
const int N=100000;
int n,v[N+5];
long long cost=0,c1,c2;
int main(){
    freopen("lanterna.in","r",stdin);
    freopen("lanterna.out","w",stdout);
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&v[i]);
    sort(v+1,v+n+1);
    for(int i=n;i>=3;i-=2){
        c1=v[2]+v[1]+v[2]+v[i];
        c2=v[i]+v[1]+v[i-1]+v[1];
        cost+=min(c1,c2);
    }
    if(n%2==1)
        cost+=v[1];
    else
        cost+=v[2];
    printf("%lld",cost);
    return 0;
}