結果

問題 No.2591 安上がりな括弧列
ユーザー 遭難者遭難者
提出日時 2023-12-19 10:08:20
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 1,796 bytes
コンパイル時間 7,886 ms
コンパイル使用メモリ 347,344 KB
実行使用メモリ 11,264 KB
最終ジャッジ日時 2023-12-19 10:08:29
合計ジャッジ時間 9,087 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 10 ms
11,264 KB
testcase_12 AC 9 ms
11,264 KB
testcase_13 AC 10 ms
11,264 KB
testcase_14 AC 10 ms
11,264 KB
testcase_15 AC 10 ms
11,264 KB
testcase_16 AC 10 ms
11,136 KB
testcase_17 AC 4 ms
6,676 KB
testcase_18 AC 6 ms
7,040 KB
testcase_19 AC 5 ms
6,676 KB
testcase_20 AC 10 ms
11,264 KB
testcase_21 AC 10 ms
11,264 KB
testcase_22 AC 11 ms
11,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#else
#pragma GCC target("avx2")
#pragma GCC optimize("Ofast,unroll-loops")
#endif
#include <bits/stdc++.h>
#include <atcoder/all>
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define ALL(a) a.begin(), a.end()
#undef long
#define long long long
#define ll long
using namespace std;
using mint = atcoder::modint998244353;
ostream& operator<<(ostream& os, mint& a) {
	return os << a.val();
}
template<typename T>
ostream& operator<<(ostream& os, vector<T>& a) {
	const int n = a.size();
	rep(i, n) os << a[i] << " \n"[i + 1 == n];
	return os;
}
template<typename T, size_t n>
ostream& operator<<(ostream& os, array<T, n>& a) {
	rep(i, n) os << a[i] << " \n"[i + 1 == n];
	return os;
}
template<typename T>
istream& operator>>(istream& is, vector<T>& a) {
	for (T& i : a) is >> i;
	return is;
}
template<typename T>
bool chmin(T& x, T y) {
	if (x > y) { x = y; return true; }
	return false;
}
template<typename T>
bool chmax(T& x, T y) {
	if (x < y) { x = y; return true; }
	return false;
}
void solve() {
	int n; string s; cin >> n >> s;
	const int nn = 2 * n;
	vector<int> a(nn); cin >> a;
	constexpr long inf = 1e18;
	vector<vector<long>> d(n + 1, vector<long>(n + 1, inf));
	d[0][0] = 0;
	rep(i, n + 1) rep(j, n + 1) {
		if (i < j) continue;
		const int ij = i + j;
		if (ij == nn) continue;
		if (s[ij] == '(') {
			if (i != n) chmin(d[i + 1][j], d[i][j]);
			if (j != n) chmin(d[i][j + 1], d[i][j] + a[ij]);
		}
		else {
			if (j != n) chmin(d[i][j + 1], d[i][j]);
			if (i != n) chmin(d[i + 1][j], d[i][j] + a[ij]);
		}
	}
	cout << d[n][n] << endl;
}
int main() {
	// srand((unsigned)time(NULL));
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(40);
	solve();
	return 0;
}
0