Pentru această operație este nevoie să te autentifici.

Cod sursă (job #666754)

Utilizator avatar Dragono63 Stanciu Rares Stefan Dragono63 IP ascuns
Problemă Lanterna Compilator cpp-32 | 1,30 kb
Rundă Arhiva de probleme Status evaluat
Dată 4 oct. 2022 20:29:10 Scor 100
#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int, int>
using ll = long long;

using namespace std;

/*******************************/
// INPUT / OUTPUT

ifstream f("lanterna.in");
ofstream g("lanterna.out");
/*******************************/
/// GLOBAL DECLARATIONS

int N;
ll ans;
vector <int> v;
/*******************************/
/// FUNCTIONS

void ReadInput();
void Solution();
void Output();
/*******************************/
///-------------------------------------
inline void ReadInput()
{
    f >> N;

    int num;
    for (int i = 0 ; i < N ; ++ i)
    {
        f >> num;
        v.push_back(num);
    }
}
///-------------------------------------
inline void Solution()
{
    sort(v.begin(), v.end());

    ll a, b, c, d;

    a = v[0], b = v[1];
    int i = N - 1;

    while (i >= 3)
    {
        c = v[i - 1], d = v[i];
        ans += min((2 * a + c + d), (a + 2 * b + d));
        i -= 2;
    }

    if (i == 2) ans += 1LL * v[0] + 1LL * v[1] + 1LL * v[2];
    if (i == 1) ans += 1LL * v[1];
    if (i == 0) ans += 1LL * v[0];
}
///-------------------------------------
inline void Output()
{
    g << ans;
}
///-------------------------------------
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ReadInput();
    Solution();
    Output();
    return 0;
}