Pagini recente »
Rating Stoiean Tudor (tudor00)
|
2017-11-16-clasa-5-tema-15
|
2021-12-15-clasa-5-tema-20
|
Istoria paginii runda/2018-04-26-clasa-5-tema-38
|
Cod sursă (job #785111)
Cod sursă (job
#785111)
#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;
}