1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| #include <cstdio> #include <cstring> #include <algorithm> using namespace std;
const int maxn = 1010; int a[maxn];
int main() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); } sort(a, a + n); if (n <= 2) { printf("%d\n", a[n - 1]); for (int i = 0; i < n; ++i) { printf("%d%c", a[i], i == n - 1 ? '\n' : ' '); } } else { int ans = n & 1 ? a[0] + a[1] + a[2] : a[1]; for (int i = n - 1; i > 2; i -= 2) { ans += min(a[1] << 1, a[i - 1] + a[0]) + a[0] + a[i]; } printf("%d\n", ans); for (int i = n - 1; i > 2; i -= 2) { if (a[1] << 1 < a[i - 1] + a[0]) { printf("%d %d\n", a[0], a[1]); printf("%d\n", a[0]); printf("%d %d\n", a[i - 1], a[i]); printf("%d\n", a[1]); } else { printf("%d %d\n", a[0], a[i - 1]); printf("%d\n", a[0]); printf("%d %d\n", a[0], a[i]); printf("%d\n", a[0]); } } if (n & 1) { printf("%d %d\n", a[0], a[2]); printf("%d\n", a[0]); } printf("%d %d\n", a[0], a[1]); } if (t) { puts(""); } } return 0; }
|