11722 | 2024-11-07 17:13:57 | hunzombi | Bob Baba Zárójelsorozata | cpp17 | Accepted 100/100 | 116ms | 3468 KiB |
#include <bits/stdc++.h>
using namespace std;
int N;
int x;
int values[501];
bitset<50001> dp[502];
int main()
{
//ifstream cin("input.txt");
//ofstream cout("output.txt");
//ios::sync_with_stdio(false);
//cin.tie(nullptr);
cin >> N;
cin >> x;
dp[1][x] = 1;
values[1] = x;
for (int i=2; i <= N; i++) {
cin >> x;
values[i] = x;
for (int j=0; j < 50001; j++){
if (dp[i - 1][j] == 1){
if (j - x >= 0){
dp[i][j - x] = 1;
}
if (j + x <= 50000){
dp[i][j + x] = 1;
}
}
}
}
if (dp[N][0] == 0){
cout << -1 << '\n';
return 0;
}
string res = "";
int j = 0;
for (int i = N; i >= 1; i--){
if (((j - values[i] >= 0 && dp[i - 1][j - values[i]] == 1) || (i == 1)) && i != N){
j = j - values[i];
for (int k=0; k < values[i]; k++){
res = "(" + res;
}
} else {
j = j + values[i];
for (int k=0; k < values[i]; k++){
res = ")" + res;
}
}
}
cout << res << '\n';
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 1ms | 320 KiB | ||||
2 | Accepted | 1ms | 320 KiB | ||||
3 | Accepted | 1ms | 320 KiB | ||||
subtask2 | 20/20 | ||||||
4 | Accepted | 1ms | 320 KiB | ||||
5 | Accepted | 1ms | 320 KiB | ||||
6 | Accepted | 2ms | 320 KiB | ||||
subtask3 | 30/30 | ||||||
7 | Accepted | 2ms | 396 KiB | ||||
8 | Accepted | 3ms | 320 KiB | ||||
9 | Accepted | 3ms | 424 KiB | ||||
10 | Accepted | 3ms | 428 KiB | ||||
11 | Accepted | 2ms | 320 KiB | ||||
subtask4 | 50/50 | ||||||
12 | Accepted | 65ms | 3188 KiB | ||||
13 | Accepted | 115ms | 3432 KiB | ||||
14 | Accepted | 116ms | 3468 KiB | ||||
15 | Accepted | 115ms | 3404 KiB | ||||
16 | Accepted | 64ms | 3228 KiB | ||||
17 | Accepted | 65ms | 3264 KiB | ||||
18 | Accepted | 34ms | 1840 KiB | ||||
19 | Accepted | 83ms | 2032 KiB | ||||
20 | Accepted | 83ms | 2004 KiB | ||||
21 | Accepted | 83ms | 2104 KiB | ||||
22 | Accepted | 32ms | 1700 KiB | ||||
23 | Accepted | 34ms | 1848 KiB | ||||
24 | Accepted | 14ms | 832 KiB | ||||
25 | Accepted | 64ms | 1136 KiB | ||||
26 | Accepted | 64ms | 1084 KiB | ||||
27 | Accepted | 14ms | 824 KiB | ||||
28 | Accepted | 13ms | 856 KiB | ||||
29 | Accepted | 14ms | 824 KiB | ||||
30 | Accepted | 8ms | 748 KiB | ||||
31 | Accepted | 57ms | 812 KiB | ||||
32 | Accepted | 54ms | 840 KiB | ||||
33 | Accepted | 7ms | 560 KiB | ||||
34 | Accepted | 7ms | 600 KiB | ||||
35 | Accepted | 7ms | 580 KiB | ||||
36 | Accepted | 57ms | 784 KiB | ||||
37 | Accepted | 54ms | 680 KiB | ||||
38 | Accepted | 57ms | 824 KiB | ||||
39 | Accepted | 61ms | 952 KiB | ||||
40 | Accepted | 63ms | 1052 KiB | ||||
41 | Accepted | 64ms | 1072 KiB | ||||
42 | Accepted | 64ms | 1016 KiB | ||||
43 | Accepted | 64ms | 1080 KiB | ||||
44 | Accepted | 68ms | 1164 KiB | ||||
45 | Accepted | 68ms | 1328 KiB | ||||
46 | Accepted | 75ms | 1592 KiB | ||||
47 | Accepted | 75ms | 1592 KiB | ||||
48 | Accepted | 76ms | 1592 KiB | ||||
49 | Accepted | 78ms | 1840 KiB | ||||
50 | Accepted | 81ms | 2008 KiB | ||||
51 | Accepted | 90ms | 2444 KiB | ||||
52 | Accepted | 92ms | 2336 KiB | ||||
53 | Accepted | 93ms | 2420 KiB | ||||
54 | Accepted | 101ms | 2872 KiB | ||||
55 | Accepted | 112ms | 3384 KiB | ||||
56 | Accepted | 103ms | 2828 KiB | ||||
57 | Accepted | 90ms | 2360 KiB | ||||
58 | Accepted | 92ms | 2400 KiB |