-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA.cpp
53 lines (52 loc) · 835 Bytes
/
A.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
#include "bits/stdc++.h"
using namespace std;
int cnt[3];
int p[400010];
const int maxn = 400000;
int main(int argc, char const *argv[])
{
for(int i = 2; i * i <= maxn; i++) {
if(p[i] == 1) continue;
for(int j = i * i; j <= maxn; j += i) {
p[j] = 1;
}
}
int n;
scanf("%d", &n);
int sum = 0;
for(int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
cnt[x] += 1;
sum += x;
}
vector <int> v;
for(int i = 2; i <= sum; i++) {
if(p[i] == 0) {
v.emplace_back(i);
}
}
int pt = 0;
int cur = 0;
while(pt < v.size()) {
while(v[pt] - cur >= 2 && cnt[2] > 0) {
printf("2 ");
cur += 2;
--cnt[2];
}
while(v[pt] - cur >= 1 && cnt[1] > 0) {
printf("1 ");
cur += 1;
--cnt[1];
}
++pt;
}
while(cnt[2]--) {
printf("2 ");
}
while(cnt[1]--) {
printf("1 ");
}
printf("\n");
return 0;
}