cf1107E(区间DP)

题意:给定01串,若删去连续i个0/1的子串,可得到a[i]分数,能获得分数的最大值

总是不会暴力。。其实不难

设d[x][y][k]为区间[x,y]内且与第i个字符形成的连续k个0/1子串的最大值

那么有2个方面的转移

一个是枚举i,使得[x,i]贡献一个0/1,[i,y]贡献k-1个0/1

一个是枚举i,使得[x,i]贡献k个0/1,[i,y]贡献他所能贡献的最大值

 

 

/**
 *          ┏┓    ┏┓
 *          ┏┛┗━━━━━━━┛┗━━━┓
 *          ┃       ┃  
 *          ┃   ━    ┃
 *          ┃ >   < ┃
 *          ┃       ┃
 *          ┃... ⌒ ...  ┃
 *          ┃              ┃
 *          ┗━┓          ┏━┛
 *          ┃          ┃ Code is far away from bug with the animal protecting          
 *          ┃          ┃   神兽保佑,代码无bug
 *          ┃          ┃           
 *          ┃          ┃        
 *          ┃          ┃
 *          ┃          ┃           
 *          ┃          ┗━━━┓
 *          ┃              ┣┓
 *          ┃              ┏┛
 *          ┗┓┓┏━━━━━━━━┳┓┏┛
 *           ┃┫┫       ┃┫┫
 *           ┗┻┛       ┗┻┛
 */ 
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<set>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 105
#define nm 20005
#define pi 3.1415926535897931
const ll inf=1e16;
using namespace std;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}
 
 



int n,a[NM];
ll b[NM];
ll d[NM][NM][NM],g[NM][NM];
bool v[NM][NM];

void dfs(int x,int y){
    if(v[x][y])return;
    v[x][y]++;
    inc(k,1,n)d[x][y][k]=-inf;g[x][y]=-inf;
    if(x>y)return;
    if(x==y){g[x][y]=b[1];d[x][y][1]=0;return;}
    inc(i,x+1,y){
	dfs(x,i-1);dfs(i,y);
	if(a[x]==a[i])
	inc(k,2,n)
	    d[x][y][k]=max(d[x][y][k],d[x][i-1][1]+d[i][y][k-1]);
	inc(k,1,n)
	    d[x][y][k]=max(d[x][y][k],d[x][i-1][k]+g[i][y]);
    }
    inc(k,1,n)g[x][y]=max(g[x][y],d[x][y][k]+b[k]);
}


int main(){
    n=read();inc(i,1,n)scanf("%1d",a+i);
    inc(i,1,n)b[i]=read();
    dfs(1,n);
    return 0*printf("%lld\n",g[1][n]);
}

 

 

E. Vasya and Binary String

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Vasya has a string ?

of length ? consisting only of digits 0 and 1. Also he has an array ? of length ?

.

Vasya performs the following operation until the string becomes empty: choose some consecutive substring of equal characters, erase it from the string and glue together the remaining parts (any of them can be empty). For example, if he erases substring 111 from string 111110 he will get the string 110. Vasya gets ??

points for erasing substring of length ?

.

Vasya wants to maximize his total points, so help him with this!

Input

The first line contains one integer ?

(1≤?≤100) — the length of string ?

.

The second line contains string ?

, consisting only of digits 0 and 1.

The third line contains ?

integers ?1,?2,…?? (1≤??≤109), where ?? is the number of points for erasing the substring of length ?

.

Output

Print one integer — the maximum total points Vasya can get.

Examples

Input

Copy

7
1101001
3 4 9 100 1 2 3

Output

Copy

109

Input

Copy

5
10101
3 10 15 15 15

Output

Copy

23

Note

In the first example the optimal sequence of erasings is: 1101001 →

111001 → 11101 → 1111 → ∅

.

In the second example the optimal sequence of erasings is: 10101 →

1001 → 11 → ∅

.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值