Cod sursă (job #521779)

Utilizator avatar gioto Popescu gioto IP ascuns
Problemă Lanterna Compilator cpp | 0,50 kb
Rundă easy_oli1 Status evaluat
Dată 25 ian. 2020 12:37:25 Scor 100
#include <bits/stdc++.h>
using namespace std;

const long long INF = 1e18;

int n;
int a[100005];
long long d[3];

int main(){
	freopen("lanterna.in", "r", stdin);
	freopen("lanterna.out", "w", stdout);
	
	scanf("%d", &n);
	for(int i = 1; i <= n ; ++i) scanf("%d", &a[i]);
	sort(a + 1, a + n + 1);
	
	for(int i = n; i >= 3 ; --i){
		d[i % 3] = d[(i + 1) % 3] + a[i] + a[1];
		if(i < n){
			d[i % 3] = min(d[i % 3], d[(i + 2) % 3] + a[i + 1] + a[1] + 2 * a[2]);
		}
	}
	
	printf("%lld", d[0] + a[2]);
	
	return 0;
}