-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP10037.cpp
95 lines (90 loc) · 2.16 KB
/
P10037.cpp
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
#include <iostream>
#include <algorithm>
#include <assert.h>
#include <vector>
int main() {
int NUMBER_OF_CASES;
std::cin >> NUMBER_OF_CASES;
int people[1000];
for(int ignore = 0; ignore < NUMBER_OF_CASES; ++ignore) {
if(ignore != 0)
std::cout << std::endl;
// Parse input:
int n;
std::cin >> n;
for(int i = 0; i < n; ++i) {
std::cin >> people[i];
}
std::sort(&people[0], &people[n]);
if(n == 0) {
// Error!
assert(false); int *die = NULL; die[0] = 666;
return 1;
}
if(n == 1) {
std::cout << people[0] << std::endl; // time
std::cout << people[0] << std::endl;
continue;
}
if(n == 2) {
std::cout << people[1] << std::endl; // time
std::cout << people[0] << " " << people[1] << std::endl;
continue;
}
std::vector<int> walks;
int time = 0;
while(true) {
if(n == 2) {
// Remaining two:
walks.push_back(people[0]);
walks.push_back(people[1]);
time += people[1];
break;
}
if(n == 3) {
// Push fattest:
walks.push_back(people[0]);
walks.push_back(people[2]);
walks.push_back(people[0]);
time += people[0] + people[2];
// Remaining two:
walks.push_back(people[0]);
walks.push_back(people[1]);
time += people[1];
break;
}
if(people[1]+people[1] > people[n-2]+people[0]) {
// Push last:
walks.push_back(people[0]);
walks.push_back(people[n-1]);
walks.push_back(people[0]);
time += people[0] + people[n-1];
--n;
continue;
}
// Push returner:
walks.push_back(people[0]);
walks.push_back(people[1]);
walks.push_back(people[0]);
time += people[0] + people[1];
// Push two fattest:
walks.push_back(people[n-1]);
walks.push_back(people[n-2]);
walks.push_back(people[1]);
time += people[n-1] + people[1];
n-=2;
}
// output:
std::cout << time << std::endl;
bool twoWalk = true;
for(unsigned int i = 0; i < walks.size(); i+=twoWalk?2:1, twoWalk = !twoWalk) {
if(twoWalk) {
std::cout << walks[i] << " " << walks[i+1] << std::endl;
}
else {
std::cout << walks[i] << std::endl;
}
}
}
return 0;
}