117222024-11-07 17:13:57hunzombiBob Baba Zárójelsorozatacpp17Accepted 100/100116ms3468 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;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted1ms320 KiB
2Accepted1ms320 KiB
3Accepted1ms320 KiB
subtask220/20
4Accepted1ms320 KiB
5Accepted1ms320 KiB
6Accepted2ms320 KiB
subtask330/30
7Accepted2ms396 KiB
8Accepted3ms320 KiB
9Accepted3ms424 KiB
10Accepted3ms428 KiB
11Accepted2ms320 KiB
subtask450/50
12Accepted65ms3188 KiB
13Accepted115ms3432 KiB
14Accepted116ms3468 KiB
15Accepted115ms3404 KiB
16Accepted64ms3228 KiB
17Accepted65ms3264 KiB
18Accepted34ms1840 KiB
19Accepted83ms2032 KiB
20Accepted83ms2004 KiB
21Accepted83ms2104 KiB
22Accepted32ms1700 KiB
23Accepted34ms1848 KiB
24Accepted14ms832 KiB
25Accepted64ms1136 KiB
26Accepted64ms1084 KiB
27Accepted14ms824 KiB
28Accepted13ms856 KiB
29Accepted14ms824 KiB
30Accepted8ms748 KiB
31Accepted57ms812 KiB
32Accepted54ms840 KiB
33Accepted7ms560 KiB
34Accepted7ms600 KiB
35Accepted7ms580 KiB
36Accepted57ms784 KiB
37Accepted54ms680 KiB
38Accepted57ms824 KiB
39Accepted61ms952 KiB
40Accepted63ms1052 KiB
41Accepted64ms1072 KiB
42Accepted64ms1016 KiB
43Accepted64ms1080 KiB
44Accepted68ms1164 KiB
45Accepted68ms1328 KiB
46Accepted75ms1592 KiB
47Accepted75ms1592 KiB
48Accepted76ms1592 KiB
49Accepted78ms1840 KiB
50Accepted81ms2008 KiB
51Accepted90ms2444 KiB
52Accepted92ms2336 KiB
53Accepted93ms2420 KiB
54Accepted101ms2872 KiB
55Accepted112ms3384 KiB
56Accepted103ms2828 KiB
57Accepted90ms2360 KiB
58Accepted92ms2400 KiB