Safe Passage

A group of friends snuck away from their school campus, but now they must return from the main campus gate to their dorm while remaining undetected by the many teachers who patrol the campus. Fortunately, they have an invisibility cloak, but it is only large enough to cover two people at a time. They will take turns as individuals or pairs traveling across campus under the cloak (and by necessity, returning the cloak to the gate if others remain). Each student has a maximum pace at which he or she is able to travel, yet if a pair of students are walking under the cloak together, they will have to travel at the pace of the slower of the two. Their goal is to have everyone back at the dorm as quickly as possible.

As an example, assume that there are four people in the group, with person AA able to make the trip in 11 minute, person BB able to travel in 22 minutes, person CC able to travel in 77 minutes, and person DD able to travel in 1010 minutes. It is possible to get everyone to the dorm in 1717 minutes with the following plan:

 

\begin{array}{l}\text{– A and B go from the gate to the dorm together} & \text{(taking 2 minutes)}\\\text{– A returns with the cloak to the gate} & \text{(taking 1 minute)}\\\text{– C and D go from the gate to the dorm together} & \text{(taking 10 minutes)}\\\text{– B returns with the cloak to the gate} & \text{(taking 2 minutes)}\\\text{– A and B go from the gate to the dorm together} & \text{(taking 2 minutes)}\end{array}– A and B go from the gate to the dorm together– A returns with the cloak to the gate– C and D go from the gate to the dorm together– B returns with the cloak to the gate– A and B go from the gate to the dorm together(taking 2 minutes)(taking 1 minute)(taking 10 minutes)(taking 2 minutes)(taking 2 minutes)

 

Input

The input is a single line beginning with an integer, 2 \leq N \leq 152N15. Following that are NN positive integers that respectively represent the minimum time in which each person is able to cross the campus if alone; these times are measured in minutes, with each being at most 50005000. (It is a very large campus!)

Output

Output the minimum possible time it takes to get the entire group from the gate to the dorm.

输出时每行末尾的多余空格,不影响答案正确性

样例输入1

2 15 5

样例输出1

15

样例输入2

4 1 2 7 10

样例输出2

17

样例输入3

5 12 1 3 8 6

样例输出3

29
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>
//#include <xfunctional>
#define ll              long long
#define PII             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
#define pb              push_back
#define mk              make_pair
using namespace std;
int dir1[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,1 },{ -1,1 } };
int dir2[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,-1 },{ -1,-1 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979;
const int mod = 1e9 + 7;
const int N = 100;
//if(x<0 || x>=r || y<0 || y>=c)

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
int TravelBridge(vector<int> times)
{
    // 假设时间数组已经排序
    size_t length = times.size();
    if (length <= 2)
        return times[length - 1];
    else if (length == 3)
    {
        return times[0] + times[1] + times[2];
    }
    else
    {
        int totaltime = 0;
        int a = times[0];
        int b = times[1];
        int z = times[length - 1];
        int y = times[length - 2];
        if (b * 2 < a + y)
        {
            times.erase(times.end() - 1);
            times.erase(times.end() - 1);
            totaltime += b + a + z + b + TravelBridge(times);
        }
        else
        {
            times.erase(times.end() - 1);
            totaltime += z + a + TravelBridge(times);
        }
        return totaltime;
    }
}
int main()
{
    int n;
    cin >> n;
    vector<int> a(n), b;
    for (int i = 0; i < n; i++)
        cin >> a[i];
    sort(a.begin(), a.end());
    int ans;
    ans = TravelBridge(a);
    cout << ans << endl;
    return 0;
}

 

posted @ 2020-04-19 22:52  DeaL57  阅读(182)  评论(0编辑  收藏  举报