Cod sursă (job #803120)

Utilizator avatar deniscorman Corman Denis deniscorman IP ascuns
Problemă Lanterna Compilator cpp-32 | 1,08 kb
Rundă lasm_09_01_2025_clasa11 Status evaluat
Dată 9 ian. 2025 14:37:30 Scor 0
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    freopen("lanterna.in", "r", stdin);
    freopen("lanterna.out", "w", stdout);
    int N;
    cin >> N;
    vector<int> times(N);
    
    for (int i = 0; i < N; i++) {
        cin >> times[i];
    }
    
    // Sort the times in ascending order
    sort(times.begin(), times.end());
    
    int total_time = 0;

    while (N > 3) {
        // Two strategies:
        // 1. Send the two slowest across and return the fastest
        int strategyA = times[1] + times[0] + times[N-1] + times[1];
        
        // 2. Send the two fastest across and return the fastest
        int strategyB = times[1] + times[0] + times[N-1] + times[2];
        
        // Add the minimal of both strategies
        total_time += min(strategyA, strategyB);
        
        // Reduce the number of people by 2 since they are now across the bridge
        N--;
    }
    
    // For the last three people
    if (N == 3) {
        total_time += times[2] + times[1];
    }
    
    cout << total_time << endl;
    return 0;
}