标签:alt 地址 amp pre ios src highlight += http
#include<cstdio> #include<iostream> #include<cstring> #define int long long using namespace std; const int MAXN=4e3,INF=2e9; char str[MAXN]; int w[MAXN]; int a[MAXN][MAXN];//l r int n; int getValue(int l,int r){ if(l>r)return 0; if((r-l+1)%2!=0)return INF; int rest=0; if(str[l-1]!=str[r-1])rest=min(w[l],w[r]); return getValue(l+1,r-1)+rest; } void init(){ memset(a,0,sizeof(a)); for(int l=1;l<=n;l++){ for(int r=l+1;r<=n;r++){ if((l%2)==0)continue; a[l][r]=getValue(l,r); //printf("a[%lld][%lld]=%lld\n",l,r,a[l][r]); } } } int f[MAXN]; void test(){ init(); memset(f,0x3f,sizeof(f)); f[2]=a[1][2]; f[0]=0; for(int i=2;i<=n;i+=2){ for(int j=0;j<=i-2;j+=2){ f[i]=min(f[i],f[j]+a[j+1][i]); //printf("f[%d]=%d\n",i,f[i]); } } cout<<f[n]<<endl; } signed main(){ scanf("%lld",&n); cin>>str; for(int i=1;i<=n;i++){ cin>>w[i]; } test(); return 0; }
标签:alt 地址 amp pre ios src highlight += http
原文地址:https://www.cnblogs.com/zbsy-wwx/p/11791330.html