dp (river issues)

http://codeforces.com/gym/101492/problem/E

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  t i  ≤ 10 9
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.

Ideas:

After crossing the river by the time sorting

There are two optimal strategy:

1. The heaviest past with the lightest,

2. The two lightest with two heaviest in the past.

Each judge, compare two strategies which better.

Sample 1: No. 1, 2, and 3 to 1 first past, a back, and then past 1 and 2, the time is 50 + 30 + 40 = 120

Sample 2: numbered 1,2,3,4, 1 and 2 firstly to the past, a back, and then the last 3 and 4, back 2, 1 and 2 over the last time is 20 + 10 + 100 + 20 + 20 = 170

Topic Analysis: This question may seem dp topic, but look at memory and time, two cycle time is not enough. But a little analysis, in fact, can be found, there are two cases:

Suppose numbered 1,2,3,4,4 individuals go B in A;

The first case: 2 + 1 to the last, and then for the past 3 + 4; 2 + 1 is the specific process in the past, a back past 3 + 4, 2 back in this time is t1 = num [2] + num [ 1] + num [4] + num [2] = num [2] * 2 + num [1] + num [4];

The second case: 1 + 4 past, 1 back, 1 + 3 in the past, 1 back; that is, through a back and forth, one by one in the past. Time t2 = num [4] + num [1] + num [3] + num [1] = num [1] * 2 + num [3] + num [4];

On the both cases, the choice of which summed over method, can compare the time t1 and t2 take a small, i.e. see num [2] + num [2]> num [1] + num [3] t1?: t2

#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdio.h>
#include <set>
#include <stack>
#include <string.h>
#include <vector>
#include <queue>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF  0x3f3f3f3f
using namespace std;
long long a[100009];

int main()
{
    int n ;
    while(scanf("%d" , &n) != EOF)
    {
        for(int i = 0 ; i < n ; i++)
        {
            scanf("%lld" , &a[i]);
        }
        sort(a , a + n);
        long long ans = 0 ;
        if(n == 1)
        {
            ans = a[0];
        }
        else
        {
            if(n % 2 == 1)
            {
                ans += a[0] + a[1] + a[2];
                for(int i = 4 ; i < n ; i += 2)
                {
                    ans += min(2 *a[0] + a[i-1] + a[i] , a[0] + 2*a[1] + a[i]);
                }
            }
            else
            {
                ans += a[1];
                for(int i = 3 ; i < n ; i += 2 )
                {
                    ans += min(2*a[0] + a[i-1] + a[i] , a[0] + 2*a[1] + a[i]);
                }
            }
        }
        printf("%lld\n" , ans);

    }



    return 0 ;
}

 

Guess you like

Origin www.cnblogs.com/nonames/p/11329547.html