結果
問題 | No.2591 安上がりな括弧列 |
ユーザー | ldsyb |
提出日時 | 2023-12-24 19:50:52 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 40 ms / 2,000 ms |
コード長 | 1,282 bytes |
コンパイル時間 | 4,773 ms |
コンパイル使用メモリ | 267,264 KB |
実行使用メモリ | 34,688 KB |
最終ジャッジ日時 | 2024-09-27 13:47:01 |
合計ジャッジ時間 | 6,066 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 23 |
ソースコード
#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; }