結果

問題 No.2591 安上がりな括弧列
ユーザー ldsybldsyb
提出日時 2023-12-24 19:50:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 1,282 bytes
コンパイル時間 4,600 ms
コンパイル使用メモリ 266,684 KB
実行使用メモリ 34,688 KB
最終ジャッジ日時 2023-12-24 19:50:59
合計ジャッジ時間 6,350 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 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 39 ms
34,688 KB
testcase_12 AC 40 ms
34,688 KB
testcase_13 AC 40 ms
34,688 KB
testcase_14 AC 40 ms
34,688 KB
testcase_15 AC 39 ms
34,688 KB
testcase_16 AC 39 ms
34,048 KB
testcase_17 AC 12 ms
11,264 KB
testcase_18 AC 20 ms
18,176 KB
testcase_19 AC 16 ms
14,464 KB
testcase_20 AC 40 ms
34,688 KB
testcase_21 AC 39 ms
34,688 KB
testcase_22 AC 39 ms
34,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif

int main()
{
    int64_t n;
    string s;
    cin >> n >> s;
    vector<int64_t> as(2 * n);
    for (auto &&a : as)
    {
        cin >> a;
    }

    int64_t inf = (1LL << 60);

    vector dp(2 * n + 1, vector(2 * n + 1, inf));

    dp[0][0] = 0;

    for (int64_t i = 0; i < 2 * n; i++)
    {
        for (int64_t j = 0; j <= 2 * n; j++)
        {
            if (s[i] == '(')
            {
                if (j + 1 <= 2 * n)
                {
                    dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j]);
                }

                if (0 <= j - 1)
                {
                    dp[i + 1][j - 1] = min(dp[i + 1][j - 1], dp[i][j] + as[i]);
                }
            }
            else if (s[i] == ')')
            {
                if (j + 1 <= 2 * n)
                {
                    dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + as[i]);
                }

                if (0 <= j - 1)
                {
                    dp[i + 1][j - 1] = min(dp[i + 1][j - 1], dp[i][j]);
                }
            }
        }
    }

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

    return 0;
}
0