Xidianoj 1176 ship

Source: Internet
Author: User

Title Description

The members of XDU-ACM group went camp this summer holiday. They came across a river one day. There is a ship which only can carry at the most of the people at the same time. The ship would move has only if there are at least one person in the ship to drive it. Everyone had different cost of time to pass the river, and the time of the pass the river by ship depended on the longer time of the passengers. You should tell them the minimum total time, all of the members should spend to arrive the next band.

Input

The input contains multiple test cases.
The first line of all case contains one integer n (1≤n≤100000). Then next n lines contains n positive integers a[i] (1≤a[i]≤10000)-the ith person spend a[i] time to pass the river.

Output

For each case, print the minimum total time they should spend on the only line.

--Text

First by the length of time row, then a[1] is the fastest, a[2] times fast, a[n] slowest, a[n-1] times slow

if (2*a[2]+a[1]+a[n] > 2*a[1]+a[n-1]+a[n])

A[1],a[n], a[1] come back and a[n-1] to sit, a[1] come back

Otherwise, each time a[1],a[2] boat, a[1] back, a[n-1] and a[n] sit, a[2] come back

So that you lose 2 people at a time, until n<4.

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;intN;inta[100001];Long Longres =0;voidSolveintN) {    if(N <=3){        if(n = =3) {res+ = a[1] + a[2] + a[3]; return; }        if(n = =2) {res+ = a[2]; return; }        if(n = =1) {res+ = a[1]; return; }} Res+ = min (2*a[2]+a[1]+a[n],2*a[1]+a[n-1]+A[n]); Solve (n-2);}intMain () { while(SCANF ("%d", &n)! =EOF) {        inti; Res=0;  for(i=1; i<=n;i++) {scanf ("%d",&A[i]); } sort (A+1, A +1+n);//For (i=1;i<=n;i++) {//printf ("%d", A[i]);//        }solve (n); printf ("%lld\n", RES); }} 

Xidianoj 1176 ship

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.