結果

問題 No.2591 安上がりな括弧列
ユーザー lloyzlloyz
提出日時 2024-01-04 00:42:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 140 ms / 2,000 ms
コード長 667 bytes
コンパイル時間 256 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 107,140 KB
最終ジャッジ日時 2024-01-04 00:42:41
合計ジャッジ時間 3,807 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
53,332 KB
testcase_01 AC 35 ms
53,332 KB
testcase_02 AC 34 ms
53,332 KB
testcase_03 AC 35 ms
53,332 KB
testcase_04 AC 35 ms
53,332 KB
testcase_05 AC 34 ms
53,332 KB
testcase_06 AC 35 ms
53,332 KB
testcase_07 AC 34 ms
53,332 KB
testcase_08 AC 41 ms
59,764 KB
testcase_09 AC 44 ms
61,852 KB
testcase_10 AC 43 ms
61,852 KB
testcase_11 AC 137 ms
107,140 KB
testcase_12 AC 138 ms
107,012 KB
testcase_13 AC 139 ms
107,140 KB
testcase_14 AC 134 ms
107,012 KB
testcase_15 AC 134 ms
107,012 KB
testcase_16 AC 135 ms
106,372 KB
testcase_17 AC 65 ms
72,364 KB
testcase_18 AC 95 ms
88,580 KB
testcase_19 AC 87 ms
86,792 KB
testcase_20 AC 136 ms
106,884 KB
testcase_21 AC 140 ms
107,012 KB
testcase_22 AC 137 ms
106,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
S = input()
A = list(map(int, input().split()))

INF = 10**18
DP = [[INF for _ in range(2 * n + 1)] for _ in range(2 * n + 1)]
DP[0][0] = 0
for i in range(2 * n):
    s = S[i]
    for j in range(2 * n + 1):
        if DP[i][j] != INF:
            if s == '(':
                DP[i + 1][j + 1] = min(DP[i + 1][j + 1], DP[i][j])
                if j - 1 >= 0:
                    DP[i + 1][j - 1] = min(DP[i + 1][j - 1], DP[i][j] + A[i])
            else:
                DP[i + 1][j + 1] = min(DP[i + 1][j + 1], DP[i][j] + A[i])
                if j - 1 >= 0:
                    DP[i + 1][j - 1] = min(DP[i + 1][j - 1], DP[i][j])
print(DP[2 * n][0])
0