Teamwork (基础dp)

Teamwork (基础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.

 

题意非常费解,比赛读了一个小时也没读明白......

题意:【例题6】在一个夜黑风高的晚上,有n(n <= 50)个小朋友在桥的这边,现在他们需要过桥,但是由于桥很窄,每次只允许不大于两人通过,他们只有一个手电筒,所以每次过桥的两个人需要把手电筒带回来,i号小朋友过桥的时间为T[i],两个人过桥的总时间为二者中时间长者。问所有小朋友过桥的总时间最短是多少。

思路:我们先将所有人按花费时间递增进行排序,假设前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] }

 

代码:

#include <bits/stdc++.h>

using namespace std;

long long a[200005];
long long dp[200005];

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

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值