結果

問題 No.2591 安上がりな括弧列
ユーザー coindarwcoindarw
提出日時 2023-12-19 00:24:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,828 ms / 2,000 ms
コード長 2,073 bytes
コンパイル時間 2,209 ms
コンパイル使用メモリ 207,292 KB
実行使用メモリ 34,688 KB
最終ジャッジ日時 2023-12-19 00:24:46
合計ジャッジ時間 19,005 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 1 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 1 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 1,828 ms
34,688 KB
testcase_12 AC 1,813 ms
34,688 KB
testcase_13 AC 1,816 ms
34,688 KB
testcase_14 AC 1,796 ms
34,688 KB
testcase_15 AC 1,816 ms
34,688 KB
testcase_16 AC 1,159 ms
34,048 KB
testcase_17 AC 97 ms
11,264 KB
testcase_18 AC 433 ms
18,176 KB
testcase_19 AC 197 ms
14,464 KB
testcase_20 AC 1,626 ms
34,688 KB
testcase_21 AC 1,806 ms
34,688 KB
testcase_22 AC 1,223 ms
34,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using ll = long long;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; --i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define repc2(i, s, n) for (int i = (s); i <= (int)(n); i++)
constexpr int inf = 2000'000'000;
constexpr ll linf = 4000000000000000000ll;
constexpr ll M7 = 1000000007ll;
constexpr ll M09 = 1000000009ll;
constexpr ll M9 = 998244353ll;
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
using namespace std;
template <typename T>
inline ostream& operator<<(ostream& os, const vector<T>& v) {
    for (auto itr = v.begin(); itr != v.end(); ++itr) {
        os << *itr;
        if (itr != v.end() - 1) {
            os << " ";
        }
    }
    return os;
}
template <typename T, typename U>
std::ostream& operator<<(std::ostream& os, const std::pair<T, U>& p) noexcept {
    return os << "(" << p.first << ", " << p.second << ")";
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    string s;
    cin >> n >> s;
    vector<ll> a(2 * n);
    rep(i, 2 * n) cin >> a[i];

    vector<vector<ll>> dp = vector<vector<ll>>(2 * n, vector<ll>(2 * n, linf));
    for (int len = 2; len <= 2 * n; len += 2) {
        for (int l = 0; l < 2 * n; l++) {
            int r = l + len - 1;
            if (r >= 2 * n) break;
            if (len == 2) {
                ll x = 0;
                if (s[l] == ')') x += a[l];
                if (s[r] == '(') x += a[r];
                dp[l][r] = x;
            } else {
                for (int i = l + 1; i < r; i += 2) dp[l][r] = min(dp[l][r], dp[l][i] + dp[i + 1][r]);
                ll x = 0;
                if (s[l] == ')') x += a[l];
                if (s[r] == '(') x += a[r];
                dp[l][r] = min(dp[l][r], x + dp[l + 1][r - 1]);
            }
        }
    }
    cout << dp[0][2 * n - 1] << endl;

    return 0;
}
0