http://codeforces.com/contest/1107/problem/E

Vasya has a string ss of length nn consisting only of digits 0 and 1. Also he has an array aa of length nn .

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 axax points for erasing substring of length xx .

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

Input

The first line contains one integer nn (1≤n≤1001≤n≤100 ) — the length of string ss .

The second line contains string ss , consisting only of digits 0 and 1.

The third line contains nn integers a1,a2,…ana1,a2,…an (1≤ai≤1091≤ai≤109 ), where aiai is the number of points for erasing the substring of length ii .

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 →→ ∅ .

 

 

设f(l,r,pre):字符串[l,r]区间,在l及其之前与l处字符相同的字符数,对应的最大价值。

则f[l][r][pre]=max{a[pre]+f(l+1,r,1),f(l+1,i-1,1)+f(i,r,pre+1)}   ,a[pre]表示pre个相同字符的价值,s[l]==s[i]。

也就是说,当前状态f(l,r,pre)

要么单独拿出第一个字符与前缀一块,从第一和二个字符处断开,其余的分治作为另一子任务递归解决,

要么第一个字符与后续某个相同的字符合并,中间的和后面的递归解决。

这样子一定能不重不漏地考虑到所有的情况。

用出题人的话讲:

We set up a DP on (start index, end index, number of consecutive digits matching our start index). In other words, the current string we are solving is the substring from start index to end index, plus some number of additional digits (all equal to S[start]) added as a prefix to our substring.

 

这张图是我输出的状态依赖表示:

以第一组样例

7
1101001
3 4 9 100 1 2 3

为例:

 

#include<bits/stdc++.h>
using namespace std;

int n,a[200];
char s[200];
long long f[200][200][200];

long long solve(int l,int r,int pre)
{  
	if(l>r)return 0;
	if(l==r)return a[pre];
	long long& ans=f[l][r][pre];
	if(ans)return ans;
	ans=a[pre]+solve(l+1,r,1);
	for(int i=l+1;i<=r;i++)if(s[l]==s[i])
		ans=max(ans,solve(l+1,i-1,1)+solve(i,r,pre+1));
	//printf("f(%d,%d,%d) use:",l,r,pre);
	//printf("a[%d]+f(%d,%d,%d) ",pre,l+1,r,1);
	//for(int i=l+1;i<=r;i++)if(s[l]==s[i])printf("f(%d,%d,%d)+f(%d,%d,%d) ",l+1,i-1,1,i,r,pre+1);
	//putchar('\n');
	return ans;
}

int main()
{
	//freopen("input.in","r",stdin);
	cin>>n;
	cin>>s+1;
	for(int i=1;i<=n;i++)cin>>a[i];
	cout<<solve(1,n,1)<<endl;
	return 0;
}