别人写的题解:https://blog.csdn.net/qq_42505741/article/details/84852078
做这道题应该要有个感觉,是有个迭代或规律的。没思路时可以枚举一下,应该枚举到n=4就可以得出个思路了。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<stack>
#include<queue>
using namespace std;
const int maxn=1e3+6;
typedef long long ll;
ll t[maxn];
void count(int n)
{
ll sum = 0;
while(n >= 4)
{
sum += min(t[1] + 2*t[2] + t[n], 2*t[1] + t[n-1] + t[n]);
n -= 2;
}
if(n == 1)
sum += t[n];
else if(n == 2)
sum += t[n];
else if(n == 3)
sum += t[n-2]+t[n-1]+t[n];
printf("%lld\n", sum);
}
void print(int n)
{
while(n >= 4)
{
if(t[1] + 2*t[2] + t[n] < 2*t[1] + t[n-1] + t[n])
{
printf("%d %d\n", t[1], t[2]);
printf("%d\n", t[1]);
printf("%d %d\n", t[n-1], t[n]);
printf("%d\n", t[2]);
}
else
{
printf("%d %d\n", t[1], t[n]);
printf("%d\n", t[1]);
printf("%d %d\n", t[1], t[n-1]);
printf("%d\n", t[1]);
}
n -= 2;
}
if(n == 1)
printf("%d\n", t[n]);
else if(n == 2)
printf("%d %d\n", t[n-1], t[n]);
else if(n == 3)
{
printf("%d %d\n", t[n-2], t[n-1]);
printf("%d\n", t[n-2]);
printf("%d %d\n", t[n-2], t[n]);
}
}
int main()
{
int n, kase;
scanf("%d", &kase);
while(kase--)
{
scanf("%d", &n);
for(int i=1; i<=n; ++i)
scanf("%lld", &t[i]);
sort(t+1, t+1+n);
count(n);
print(n);
if(kase) putchar('\n');
}
return 0;
}