11628 | 2024-11-01 13:11:41 | MagyarKendeSZLG | Bob Baba Zárójelsorozata | cpp17 | Accepted 100/100 | 165ms | 3396 KiB |
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
const int MAXM = 50005;
int main() {
cin.tie(0), ios::sync_with_stdio(0);
int N;
cin >> N;
vector<int> a(N);
for (int& x : a) cin >> x;
vector dp(N, vector<bool>(MAXM));
dp[0][a[0]] = 1;
for (int i = 1; i < N; i++) {
for (int j = 0; j < MAXM; j++) {
dp[i][j] =
(j + a[i] < MAXM && dp[i - 1][j + a[i]]) ||
(j - a[i] >= 0 && dp[i - 1][j - a[i]]);
}
}
if (dp[N - 1][0]) {
vector<int> result(N);
for (int i = N - 1, x = 0; i > 0; i--) {
if (x + a[i] < MAXM && dp[i - 1][x + a[i]]) {
x += a[i];
result[i] = 1;
} else {
x -= a[i];
}
}
for (int i = 0; i < N; i++) {
cout << string(a[i], "()"[result[i]]);
}
cout << "\n";
} else {
cout << "-1\n";
}
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 2ms | 320 KiB | ||||
2 | Accepted | 2ms | 320 KiB | ||||
3 | Accepted | 1ms | 320 KiB | ||||
subtask2 | 20/20 | ||||||
4 | Accepted | 1ms | 320 KiB | ||||
5 | Accepted | 1ms | 348 KiB | ||||
6 | Accepted | 1ms | 320 KiB | ||||
subtask3 | 30/30 | ||||||
7 | Accepted | 4ms | 320 KiB | ||||
8 | Accepted | 7ms | 320 KiB | ||||
9 | Accepted | 7ms | 556 KiB | ||||
10 | Accepted | 7ms | 552 KiB | ||||
11 | Accepted | 6ms | 320 KiB | ||||
subtask4 | 50/50 | ||||||
12 | Accepted | 163ms | 3384 KiB | ||||
13 | Accepted | 165ms | 3384 KiB | ||||
14 | Accepted | 165ms | 3384 KiB | ||||
15 | Accepted | 164ms | 3396 KiB | ||||
16 | Accepted | 165ms | 3384 KiB | ||||
17 | Accepted | 165ms | 3384 KiB | ||||
18 | Accepted | 82ms | 1972 KiB | ||||
19 | Accepted | 83ms | 1848 KiB | ||||
20 | Accepted | 82ms | 1848 KiB | ||||
21 | Accepted | 82ms | 1968 KiB | ||||
22 | Accepted | 82ms | 1848 KiB | ||||
23 | Accepted | 82ms | 1848 KiB | ||||
24 | Accepted | 34ms | 824 KiB | ||||
25 | Accepted | 34ms | 1080 KiB | ||||
26 | Accepted | 34ms | 1044 KiB | ||||
27 | Accepted | 34ms | 824 KiB | ||||
28 | Accepted | 34ms | 824 KiB | ||||
29 | Accepted | 34ms | 904 KiB | ||||
30 | Accepted | 17ms | 568 KiB | ||||
31 | Accepted | 17ms | 568 KiB | ||||
32 | Accepted | 17ms | 752 KiB | ||||
33 | Accepted | 17ms | 568 KiB | ||||
34 | Accepted | 17ms | 756 KiB | ||||
35 | Accepted | 17ms | 756 KiB | ||||
36 | Accepted | 14ms | 676 KiB | ||||
37 | Accepted | 9ms | 756 KiB | ||||
38 | Accepted | 14ms | 672 KiB | ||||
39 | Accepted | 23ms | 820 KiB | ||||
40 | Accepted | 28ms | 920 KiB | ||||
41 | Accepted | 28ms | 952 KiB | ||||
42 | Accepted | 30ms | 828 KiB | ||||
43 | Accepted | 29ms | 952 KiB | ||||
44 | Accepted | 41ms | 1172 KiB | ||||
45 | Accepted | 45ms | 1268 KiB | ||||
46 | Accepted | 61ms | 1592 KiB | ||||
47 | Accepted | 63ms | 1608 KiB | ||||
48 | Accepted | 65ms | 1592 KiB | ||||
49 | Accepted | 71ms | 1768 KiB | ||||
50 | Accepted | 79ms | 1940 KiB | ||||
51 | Accepted | 104ms | 2360 KiB | ||||
52 | Accepted | 119ms | 2684 KiB | ||||
53 | Accepted | 127ms | 2864 KiB | ||||
54 | Accepted | 146ms | 3188 KiB | ||||
55 | Accepted | 157ms | 3388 KiB | ||||
56 | Accepted | 130ms | 2884 KiB | ||||
57 | Accepted | 98ms | 2280 KiB | ||||
58 | Accepted | 104ms | 2372 KiB |