結果
| 問題 | No.2591 安上がりな括弧列 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2023-12-19 03:13:23 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 28 ms / 2,000 ms | 
| コード長 | 884 bytes | 
| コンパイル時間 | 1,580 ms | 
| コンパイル使用メモリ | 169,080 KB | 
| 実行使用メモリ | 34,964 KB | 
| 最終ジャッジ日時 | 2024-09-27 08:36:25 | 
| 合計ジャッジ時間 | 2,720 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 23 | 
ソースコード
#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;
}
            
            
            
        