A Heavy Rainy Day
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 476 Accepted Submission(s) : 124
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
As you know, Hangzhou always rains heavily, especially in Xiaoheshan area, which does annoy ACMers for a longer time. One day, N hardworking ACMers were playing League of Legends in the lab, of course not include me, while it was raining cats and dogs. Everyone wanted to come back to the department where they were living together, but no one wanted to be soaked without umbrella. Unfortunately, they only had one umbrella, which is too small to take more than two individuals. The time that everyone needs to spent to go to the department is known. If two guys came back together, it will take the longer time of this two guys. The question is the earliest time they come back to the department.
Input
The first line contains an integer T(1<=T<=20),which is the number of test cases.
For each testcase:
The first line contains one integer N ,1<=N<=1000, present N ACMers want to come back,
The second line contains N integers Si, present the time the ith ACMers need.
For each testcase:
The first line contains one integer N ,1<=N<=1000, present N ACMers want to come back,
The second line contains N integers Si, present the time the ith ACMers need.
Output
The shortest time they need to come to the department.
Sample Input
1 4 1 2 5 10
Sample Output
17
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int s[10010];
int main()
{
int n,T;
scanf("%d",&T);
while(T--)
{
<span style="white-space:pre"> </span>scanf("%d",&n);
for(int i=0; i<n; i++)
{
scanf("%d",&s[i]);
}
sort(s,s+n);
int sum=0;
while(n)
{
if(n==1)
{
sum+=s[0];
n-=1;
}
else if(n==2)
{
sum+=s[1];
n-=2;
}
else if(n==3)
{
sum+=s[0]+s[1]+s[2];
n-=3;
}
else
{
int q,p;
q=s[0]+s[1]*2+s[n-1];
p=s[0]*2+s[n-2]+s[n-1];
if(q<p)
sum+=q;
else
sum+=p;
n-=2;
}
}
printf("%d\n",sum);
}
return 0;
}