天天看点

E - E Gym - 101492E (dp)

Teamwork is highly valued in the ACM ICPC. Since the beginning, the ICPC has distinguished itself from other programming contests in that teamwork is a key factor.

The University of Beijing, host of next year's World Finals, is planning recreational activities for the participants. They want to prepare games that show the importance of teamwork and enable new acquaintances among the contestants from all over the world. One of the staff members has been thinking about the following game.

In a court we have several N-person teams. The goal of each team is roughly as follows: starting from a corner, each team must take all of its members to the other corner of the court. The winning team is the one that finishes this in the shortest time.

Now we explain the game more formally. Consider the team that starts at corner A and must take all of its members to corner B. The following procedure is repeated:

  1. While there are team members at corner A,
    • If there is only one team member, he is the only one that goes to corner B.
    • Otherwise, two team members must tie one leg of each with a rope and go to B.
  2. Once arriving at B, they untie their legs, if applicable.
  3. If there are still team members at corner A, some team member at B must return to A with the rope.

The organization wants to form the teams so that no team has an advantage. They entrusted you with the following task. Given the time in seconds that the members of a team take to go from a corner to the other, find the minimum time it takes for the team to take all its members from corner A to corner B. When two team members cross the court with their legs tied, the time for the pair to cross is the maximum between the times of the two members.

Input

The first line of input contains an integer N, the number of team members. The next line contains N integers ti, the time in seconds that the i-th team member takes to go from one corner to the other.

  • 1 ≤ N ≤ 105
  • 1 ≤ ti ≤ 109

Output

Print a single integer, the minimum time required for the whole team to reach corner B.

Examples

Input

3
30 40 50
      

Output

120
      

Input

4
10 20 50 100
      

Output

170
      

Note

In the first example, the process can be completed in 120 seconds as follows:

  1. The first and second members go from A to B in 40 seconds.
  2. The first member returns to corner A in 30 seconds.
  3. The first and third team members go from A to B in 50 seconds.

This is not the only way to complete the process in this total time, but no other way does it in strictly less than 120 seconds.

题解:

我们先将所有人按花费时间递增进行排序,假设前i个人过河花费的最少时间为opt[i],那么考虑前i-1个人过河的情况,即河这边还有1个人,河那边有i-1个人,并且这时候手电筒肯定在对岸,所以opt[i] = opt[i-1] + a[1] + a[i]        (让花费时间最少的人把手电筒送过来,然后和第i个人一起过河)

如果河这边还有两个人,一个是第i号,另外一个无所谓,河那边有i-2个人,并且手电筒肯定在对岸,所以opt[i] = opt[i-2] + a[1] + a[i] + 2*a[2]    (让花费时间最少的人把电筒送过来,然后第i个人和另外一个人一起过河,由于花费时间最少的人在这边,所以下一次送手电筒过来的一定是花费次少的,送过来后花费最少的和花费次少的一起过河,解决问题)

所以 opt[i] = min{opt[i-1] + a[1] + a[i] , opt[i-2] + a[1] + a[i] + 2*a[2] ;

代码:

//Full of love and hope for life

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <queue>
#include <vector>
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
//https://paste.ubuntu.com/
//https://www.cnblogs.com/zzrturist/    //博客园
//https://blog.csdn.net/qq_44134712     //csdn

using namespace std;
const int N=5e3+10;
typedef long long ll;
const int mod=1e9+7;

ll n[100010],dp[100010];

int main(){
    ll a;
    cin >> a;
    for(ll i=1;i<=a;i++){
        cin >> n[i];
    }
    sort(n+1,n+a+1);
    dp[1]=n[1];
    dp[2]=n[2];
    for(ll i=3;i<=a;i++){
        dp[i]=min(dp[i-1]+n[1]+n[i],dp[i-2]+n[1]+n[i]+2*n[2]);
    }
    cout << dp[a];
    return 0;
}