-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path10037.cpp
More file actions
165 lines (159 loc) · 2.65 KB
/
10037.cpp
File metadata and controls
165 lines (159 loc) · 2.65 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<string>
#include<stdlib.h>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<set>
using namespace std;
// Define Some Variables
#define eps 1e-14
#define si 1010
#define pi acos(-1.0)
#define inf (1<<30)-1
//Define Some Functions
#define even(a) ((a)%2==0)
#define odd(a) ((a)%2==1)
#define max(a,b) (a>b ?a:b)
#define min(a,b) (a<b ?a:b)
#define sqr(a)((a)*(a))
#define area(x1,y1,x2,y2,x3,y3) (x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2)) //Area of a triangle
#define distance(x1,y1,x2,y2) (sqr(x1-x2)+sqr(y1-y2)) //Distance between two points
#define FOR1(i,a,b) for(i=a;i<=b;i++)
#define FOR0(i,a,b) for(i=a;i<b;i++)
#define REV(i,a,b) for(i=a;i>=b;i--)
//typedef __int64 i64;
int i,j,l,n,x=1,sm,cnt,fg,a[si],res[si],cnt1,cnt2,chk1,chk2;
char ch[si];
int gcd(int a,int b)
{
while(b>0)
{
a%=b;
a^=b;
b^=a;
a^=b;
}
return a;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
sm=cnt=0;
fg=1;
FOR0(i,0,n)
{
scanf("%d",&a[i]);
res[i]=0;
}
if(n<=2)
{
sm=a[n-1];
printf("%d\n",sm);
FOR0(i,0,n)
{
if(!i)
printf("%d",a[0]);
else
printf(" %d",a[i]);
}
puts("");
goto end;
}
sort(a,a+n);
cnt1=a[0]+2*a[1];
cnt2=2*a[0];
if(n%2==0)
{
for(i=n-1;i>=2;i-=2)
{
chk1=(cnt1+a[i]);
chk2=(cnt2+a[i]+a[i-1]);
if(chk1<chk2)
{
res[i]=1;
sm+=chk1;
}
else
{
res[i]=2;
sm+=chk2;
}
}
sm+=a[1];
printf("%d\n",sm);
for(i=n-1;i>=2;i-=2)
{
if(res[i]==1)
{
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]);
printf("%d\n",a[0]);
printf("%d %d\n",a[0],a[i-1]);
printf("%d\n",a[0]);
}
}
printf("%d %d\n",a[0],a[1]);
}
else
{
for(i=n-1;i>=3;i-=2)
{
chk1=cnt1+a[i];
chk2=(cnt2+a[i]+a[i-1]);
if(chk1<chk2)
{
res[i]=1;
sm+=chk1;
}
else
{
res[i]=2;
sm+=chk2;
}
}
sm+=(cnt1+a[2]);
sm-=a[1];
printf("%d\n",sm);
for(i=n-1;i>=3;i-=2)
{
if(res[i]==1)
{
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]);
printf("%d\n",a[0]);
printf("%d %d\n",a[0],a[i-1]);
printf("%d\n",a[0]);
}
}
printf("%d %d\n",a[0],a[1]);
printf("%d\n",a[0]);
printf("%d %d\n",a[0],a[2]);
}
end:
if(t)
puts("");
}
return 0;
}