結果

問題 No.2591 安上がりな括弧列
ユーザー daiotadaiota
提出日時 2023-12-19 03:13:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 884 bytes
コンパイル時間 1,628 ms
コンパイル使用メモリ 168,932 KB
実行使用メモリ 34,972 KB
最終ジャッジ日時 2023-12-19 03:13:26
合計ジャッジ時間 2,624 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 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 1 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 26 ms
34,972 KB
testcase_12 AC 25 ms
34,972 KB
testcase_13 AC 26 ms
34,972 KB
testcase_14 AC 25 ms
34,972 KB
testcase_15 AC 24 ms
34,972 KB
testcase_16 AC 24 ms
34,972 KB
testcase_17 AC 9 ms
20,456 KB
testcase_18 AC 15 ms
26,728 KB
testcase_19 AC 12 ms
22,572 KB
testcase_20 AC 25 ms
34,972 KB
testcase_21 AC 26 ms
34,972 KB
testcase_22 AC 23 ms
34,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll,ll> P;
#define REP(i,n) for(ll i=0;i<ll(n);i++)


ll a[2010],c[2010];
ll dp[2010][2010];


int main(void){
	cin.tie(nullptr);  ios_base::sync_with_stdio(false);
	ll i,j;

	ll N;
	string S;
	cin >> N >> S;
	for(i=1;i<=2*N;i++) cin >> a[i];

	REP(i,2*N){
		if(S[i]=='(') c[i+1]=1;
		else c[i+1]=-1;
	}

	REP(i,2*N+1) REP(j,2*N+1) dp[i][j]=1e13;

	dp[0][0]=0;
	for(i=1;i<=2*N;i++){
		for(j=0;j<=2*N;j++){
			if(c[i]==1){
				if(j-1>=0 && dp[i-1][j-1]!=1e13) dp[i][j]=min(dp[i][j],dp[i-1][j-1]);
				if(j+1<=2*N && dp[i-1][j+1]!=1e13) dp[i][j]=min(dp[i][j],dp[i-1][j+1]+a[i]);

			}else{
				if(j-1>=0 && dp[i-1][j-1]!=1e13) dp[i][j]=min(dp[i][j],dp[i-1][j-1]+a[i]);
				if(j+1<=2*N && dp[i-1][j+1]!=1e13) dp[i][j]=min(dp[i][j],dp[i-1][j+1]);

			}
		}
	}


	cout << dp[2*N][0] << endl;


	return 0;
}
0