結果
| 問題 |
No.2591 安上がりな括弧列
|
| コンテスト | |
| ユーザー |
ldsyb
|
| 提出日時 | 2023-12-24 19:50:52 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 42 ms / 2,000 ms |
| コード長 | 1,282 bytes |
| コンパイル時間 | 4,002 ms |
| コンパイル使用メモリ | 253,708 KB |
| 最終ジャッジ日時 | 2025-02-18 14:15:49 |
|
ジャッジサーバーID (参考情報) |
judge4 / 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;
}
ldsyb