#include<stdio.h>
#include<stdlib.h>
int sort(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
}
int min(int a, int b)
{
if (a < b)
return a;
else
return b;
}
int main()
{
int m, n, i, j;
scanf("%d",&m);
while (m--)
{
scanf("%d",&n);
int a[1000] = {0}, num = 0;
for (i = 0; i < n; i++)
scanf("%d",&a[i]);
qsort(a, n, sizeof(int), sort);
while (n > 3)
{
num += min((2 * a[1] + a[0] + a[n - 1]), (a[n - 1] + 2 * a[0] + a[n - 2]));
n-=2;
}
if (n == 3)
num += a[0] + a[1] + a[2];
else if (n == 2)
num += a[1];
else
num += a[0];
printf("%d\n",num);
}
return 0;
}
过河问题
最新推荐文章于 2022-01-17 16:29:50 发布