11596 | 2024-10-29 19:27:07 | Dávid | Bob Baba Zárójelsorozata | cpp17 | Accepted 100/100 | 140ms | 6700 KiB |
#include <algorithm>
#include <bits/stdc++.h>
#include <vector>
using namespace std;
#define ll long long
int main() {
int n;
cin >> n;
ll osszeg = 0;
vector<int> szamok(n + 1);
for(int i = 1; i <= n; i++) {
cin >> szamok[i];
osszeg += szamok[i];
}
vector<vector<bool>> dp(n + 1, vector<bool>(osszeg + 1));
dp[0][0] = true;
vector<vector<bool>> vissza(n + 1, vector<bool>(osszeg + 1));
for(int i = 1; i <= n; i++) {
for(int j = 0; j < osszeg; j++) {
if(j - szamok[i] >= 0 && dp[i - 1][j - szamok[i]]) {
dp[i][j] = true;
vissza[i][j] = 0;
}
if(j + szamok[i] <= osszeg && dp[i - 1][j + szamok[i]]) {
dp[i][j] = true;
vissza[i][j] = 1;
}
}
}
if(!dp[n][0]) {
cout << -1 << endl;
return 0;
}
vector<char> eredmeny;
int j = 0;
for(int i = n; i > 0; i--) {
if(!vissza[i][j]) {
for(int k = 0; k < szamok[i]; k++) eredmeny.push_back('(');
j -= szamok[i];
} else {
for(int k = 0; k < szamok[i]; k++) eredmeny.push_back(')');
j += szamok[i];
}
}
reverse(eredmeny.begin(), eredmeny.end());
for(char i : eredmeny) {
cout << i;
}
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 | 1ms | 508 KiB | ||||
subtask3 | 30/30 | ||||||
7 | Accepted | 1ms | 320 KiB | ||||
8 | Accepted | 1ms | 500 KiB | ||||
9 | Accepted | 1ms | 320 KiB | ||||
10 | Accepted | 1ms | 320 KiB | ||||
11 | Accepted | 1ms | 320 KiB | ||||
subtask4 | 50/50 | ||||||
12 | Accepted | 136ms | 6580 KiB | ||||
13 | Accepted | 136ms | 6584 KiB | ||||
14 | Accepted | 140ms | 6596 KiB | ||||
15 | Accepted | 138ms | 6700 KiB | ||||
16 | Accepted | 136ms | 6456 KiB | ||||
17 | Accepted | 136ms | 6580 KiB | ||||
18 | Accepted | 68ms | 3384 KiB | ||||
19 | Accepted | 71ms | 3508 KiB | ||||
20 | Accepted | 71ms | 3640 KiB | ||||
21 | Accepted | 70ms | 3640 KiB | ||||
22 | Accepted | 67ms | 3384 KiB | ||||
23 | Accepted | 68ms | 3384 KiB | ||||
24 | Accepted | 28ms | 1772 KiB | ||||
25 | Accepted | 28ms | 1648 KiB | ||||
26 | Accepted | 29ms | 1656 KiB | ||||
27 | Accepted | 28ms | 1644 KiB | ||||
28 | Accepted | 27ms | 1592 KiB | ||||
29 | Accepted | 27ms | 1592 KiB | ||||
30 | Accepted | 14ms | 1052 KiB | ||||
31 | Accepted | 14ms | 1080 KiB | ||||
32 | Accepted | 14ms | 1024 KiB | ||||
33 | Accepted | 14ms | 824 KiB | ||||
34 | Accepted | 14ms | 1020 KiB | ||||
35 | Accepted | 14ms | 824 KiB | ||||
36 | Accepted | 14ms | 1092 KiB | ||||
37 | Accepted | 8ms | 824 KiB | ||||
38 | Accepted | 14ms | 1080 KiB | ||||
39 | Accepted | 21ms | 1336 KiB | ||||
40 | Accepted | 27ms | 1404 KiB | ||||
41 | Accepted | 27ms | 1460 KiB | ||||
42 | Accepted | 28ms | 1592 KiB | ||||
43 | Accepted | 28ms | 1592 KiB | ||||
44 | Accepted | 37ms | 1928 KiB | ||||
45 | Accepted | 39ms | 2292 KiB | ||||
46 | Accepted | 52ms | 2872 KiB | ||||
47 | Accepted | 52ms | 2908 KiB | ||||
48 | Accepted | 54ms | 2908 KiB | ||||
49 | Accepted | 59ms | 3128 KiB | ||||
50 | Accepted | 65ms | 3440 KiB | ||||
51 | Accepted | 85ms | 4332 KiB | ||||
52 | Accepted | 89ms | 4920 KiB | ||||
53 | Accepted | 93ms | 5432 KiB | ||||
54 | Accepted | 109ms | 6072 KiB | ||||
55 | Accepted | 131ms | 6436 KiB | ||||
56 | Accepted | 109ms | 5316 KiB | ||||
57 | Accepted | 82ms | 4152 KiB | ||||
58 | Accepted | 86ms | 4408 KiB |