結果

問題 No.2591 安上がりな括弧列
ユーザー eve__fuyukieve__fuyuki
提出日時 2024-01-04 14:24:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 144 ms / 2,000 ms
コード長 621 bytes
コンパイル時間 348 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 91,124 KB
最終ジャッジ日時 2024-01-04 14:24:47
合計ジャッジ時間 3,142 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,460 KB
testcase_01 AC 36 ms
53,460 KB
testcase_02 AC 36 ms
53,460 KB
testcase_03 AC 36 ms
53,460 KB
testcase_04 AC 36 ms
53,460 KB
testcase_05 AC 36 ms
53,460 KB
testcase_06 AC 35 ms
53,460 KB
testcase_07 AC 36 ms
53,460 KB
testcase_08 AC 41 ms
59,720 KB
testcase_09 AC 45 ms
61,860 KB
testcase_10 AC 43 ms
59,628 KB
testcase_11 AC 144 ms
90,996 KB
testcase_12 AC 111 ms
90,868 KB
testcase_13 AC 117 ms
90,996 KB
testcase_14 AC 113 ms
90,996 KB
testcase_15 AC 114 ms
90,996 KB
testcase_16 AC 116 ms
90,996 KB
testcase_17 AC 62 ms
72,512 KB
testcase_18 AC 92 ms
90,624 KB
testcase_19 AC 84 ms
86,792 KB
testcase_20 AC 114 ms
91,124 KB
testcase_21 AC 142 ms
90,996 KB
testcase_22 AC 119 ms
90,996 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
s = input()
a = list(map(int, input().split()))

INF = 10 ** 18
dp = [[INF] * (2 * n + 1) for _ in range(2 * n + 1)]

dp[0][0] = 0

for i in range(2 * n):
    for j in range(2 * n + 1):
        if dp[i][j] == INF:
            continue
        if s[i] == '(':
            dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j])
            if j:
                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:
                dp[i + 1][j - 1] = min(dp[i + 1][j - 1], dp[i][j])

print(dp[2 * n][0])
0