結果

問題 No.2591 安上がりな括弧列
ユーザー t98slidert98slider
提出日時 2023-12-19 02:13:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 798 bytes
コンパイル時間 2,060 ms
コンパイル使用メモリ 206,896 KB
実行使用メモリ 34,688 KB
最終ジャッジ日時 2023-12-19 02:13:45
合計ジャッジ時間 3,472 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 29 ms
34,688 KB
testcase_12 AC 28 ms
34,688 KB
testcase_13 AC 28 ms
34,688 KB
testcase_14 AC 28 ms
34,688 KB
testcase_15 AC 30 ms
34,688 KB
testcase_16 AC 28 ms
34,176 KB
testcase_17 AC 9 ms
11,264 KB
testcase_18 AC 15 ms
18,176 KB
testcase_19 AC 11 ms
14,464 KB
testcase_20 AC 28 ms
34,688 KB
testcase_21 AC 29 ms
34,688 KB
testcase_22 AC 27 ms
34,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

template<class T> istream& operator >> (istream& is, vector<T>& vec) {
    for(T& x : vec) is >> x;
    return is;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    string s;
    cin >> n >> s;
    n *= 2;
    vector<ll> a(n);
    cin >> a;
    vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, 1ll << 60));
    dp[0][0] = 0;
    for(int i = 0; i < n; i++){
        dp[i + 1][1] = min(dp[i + 1][1], dp[i][0] + (s[i] == ')' ? a[i] : 0));
        for(int j = 1; j <= i; j++){
            dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + (s[i] == ')' ? a[i] : 0));
            dp[i + 1][j - 1] = min(dp[i + 1][j - 1], dp[i][j] + (s[i] == '(' ? a[i] : 0));
        }
    }
    cout << dp[n][0] << '\n';
}
0