Cod sursă (job #785111)

Utilizator avatar andreitrica Andrei Trica andreitrica IP ascuns
Problemă Lanterna Compilator cpp-32 | 1,18 kb
Rundă Arhiva de probleme Status evaluat
Dată 31 aug. 2024 17:33:36 Scor 80
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

#define int long long int

int minimum_crossing_time(vector<int>& times) {
    int n = times.size();

    if (n == 1) return times[0];
    if (n == 2) return max(times[0], times[1]);

    // Sort times in ascending order
    sort(times.begin(), times.end());

    int total_time = 0;
    int i = n - 1;

    while (i >= 3) {
        int option1 = times[i] + times[0] + times[i-1] + times[0]; // Two slowest go, fastest returns twice
        int option2 = times[1] + times[0] + times[i] + times[1]; // Fastest return after each cross

        total_time += min(option1, option2);
        i -= 2;
    }

    if (i == 2) {
        total_time += times[2] + times[0] + times[1];
    } else if (i == 1) {
        total_time += times[1];
    }

    return total_time;
}

#undef int
int main() {
    #define int long long int
    int n;

    ifstream cin ("lanterna.in");
    ofstream cout("lanterna.out");

    cin >> n;

    vector<int> times(n);
    for (int i = 0; i < n; i++) {
        cin >> times[i];
    }

    cout << minimum_crossing_time(times) << endl;

    return 0;
}