結果

問題 No.2591 安上がりな括弧列
ユーザー fdironiafdironia
提出日時 2023-12-19 00:07:03
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 777 bytes
コンパイル時間 805 ms
コンパイル使用メモリ 86,900 KB
実行使用メモリ 19,064 KB
最終ジャッジ日時 2023-12-19 00:07:06
合計ジャッジ時間 1,817 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 1 ms
6,676 KB
testcase_04 AC 1 ms
6,676 KB
testcase_05 AC 1 ms
6,676 KB
testcase_06 AC 1 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 15 ms
19,064 KB
testcase_12 AC 12 ms
19,064 KB
testcase_13 AC 12 ms
19,064 KB
testcase_14 AC 12 ms
19,064 KB
testcase_15 AC 13 ms
19,064 KB
testcase_16 AC 12 ms
18,756 KB
testcase_17 AC 6 ms
7,424 KB
testcase_18 AC 8 ms
10,804 KB
testcase_19 AC 7 ms
8,936 KB
testcase_20 AC 14 ms
19,036 KB
testcase_21 AC 12 ms
19,064 KB
testcase_22 AC 12 ms
18,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>

using namespace std;

using ll = long long;

constexpr ll INF = 2.1e12;

int main() {
    int n;
    cin >> n;
    char s[2*n+1];
    for (int i = 1; i <= 2 * n; i++) {
        cin >> s[i];
    }
    int a[2*n+1];
    for (int i = 1; i <= 2 * n; i++) {
        cin >> a[i];
    }

    ll dp[2*n+1][n+1];
    fill(dp[0], dp[2*n+1], INF);
    dp[0][0] = 0;
    for (int i = 1; i <= 2 * n; i++) {
        for (int j = 0; j <= n; j++) {
            if (j > 0) {
                dp[i][j] = min(dp[i][j], dp[i-1][j-1] + a[i] * (s[i] != '('));
            }
            if (j < n) {
                dp[i][j] = min(dp[i][j], dp[i-1][j+1] + a[i] * (s[i] != ')'));
            }
        }
    }

    cout << dp[2*n][0] << endl;

    return 0;
}
0