Vasya has a string $s$ of length $n$ consisting only of digits 0 and 1. Also he has an array $a$ of length $n$. 

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 $a_x$ points for erasing substring of length $x$.

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


-----Input-----

The first line contains one integer $n$ ($1 \le n \le 100$) — the length of string $s$.

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

The third line contains $n$ integers $a_1, a_2, \dots a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the number of points for erasing the substring of length $i$.


-----Output-----

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


-----Examples-----
Input
7
1101001
3 4 9 100 1 2 3

Output
109

Input
5
10101
3 10 15 15 15

Output
23



-----Note-----

In the first example the optimal sequence of erasings is: 1101001 $\rightarrow$ 111001 $\rightarrow$ 11101 $\rightarrow$ 1111 $\rightarrow$ $\varnothing$.

In the second example the optimal sequence of erasings is: 10101 $\rightarrow$ 1001 $\rightarrow$ 11 $\rightarrow$ $\varnothing$.

Source: codeforces | Difficulty: VERY_HARD
Tags: Dynamic programming


Solutions:
memo = {}

def f(dat, rewards, start, end, extra):
	curr = (start, end, extra)
	if curr in memo:
		return memo[curr]
	if start > end:
		return 0
	if start == end:
		memo[curr] = rewards[dat[start] + extra]
		return memo[curr]
	out = 0
	for cut in range(end, start - 1, -2):
		if cut == end:
			out_curr = rewards[dat[cut] + extra]
			out_curr += f(dat, rewards, start, cut - 1, 0)
		else:
			out_curr = f(dat, rewards, start, cut, extra + dat[end])
			out_curr += f(dat, rewards, cut + 1, end - 1, 0)
		out = max(out, out_curr)
	memo[curr] = out
	return memo[curr]

def solve(dat_str, rewards_orig):
	dat = []
	pos = 0
	while pos < len(dat_str):
		end = pos
		while end < len(dat_str) and dat_str[pos] == dat_str[end]:
			end += 1
		dat.append(end - pos)
		pos = end
	rewards = [0, rewards_orig[0]]
	for k in range(2, len(rewards_orig) + 1):
		rewards.append(max((rewards[k - j] + rewards_orig[j - 1] for j in range(1, k + 1))))
	return f(dat, rewards, 0, len(dat) - 1, 0)
int_dummy = input()
dat_str = input().strip()
rewards_input = input().strip().split()
rewards_ints = [int(x) for x in rewards_input]
print(solve(dat_str, rewards_ints))


import sys
sys.setrecursionlimit(10 ** 6)
n = int(input())
s = input().strip()
a = [0] + list(map(int, input().split()))
MX = 105
dp = [0] * MX ** 3

def f(i, j, k):
	if i == j:
		return 0
	idx = i * MX * MX + j * MX + k
	if not dp[idx]:
		dp[idx] = f(i + 1, j, 1) + a[k]
		for m in range(i + 1, j):
			if s[i] == s[m]:
				dp[idx] = max(dp[idx], f(i + 1, m, 1) + f(m, j, k + 1))
	return dp[idx]
print(f(0, n, 1))


n = int(input())
s = input().strip()
a = [0] + list(map(int, input().split()))
MX = 105
dp = [0] * MX ** 3
idx = lambda i, j, k: i * MX * MX + j * MX + k
for i in range(n + 1):
	for k in range(1, n + 1):
		dp[idx(i, i, k)] = 0
for gap in range(1, n + 1):
	for i in range(n):
		j = i + gap
		if j > n:
			break
		for k in range(1, n + 1):
			cur = idx(i, j, k)
			dp[cur] = dp[idx(i + 1, j, 1)] + a[k]
			for m in range(i + 1, j):
				if s[i] == s[m]:
					dp[cur] = max(dp[cur], dp[idx(i + 1, m, 1)] + dp[idx(m, j, k + 1)])
print(dp[idx(0, n, 1)])


from functools import lru_cache
n = int(input())
s = input()
A = [0] + list(map(int, input().split()))
dp = [0] * (n + 1)
for i in range(1, n + 1):
	dp[i] = max((dp[j] + A[i - j] for j in range(0, i)))

@lru_cache(None)
def score(s):
	length = len(s)
	if length == 0:
		return 0
	ans = 0
	start = 0
	i = 1
	while i < length and s[i] == s[start]:
		i += 1
	ans = dp[i] + score(s[i:])
	for j in range(i, length):
		if s[j] == s[start]:
			ans = max(ans, score(s[i:j]) + score(s[:i] + s[j:]))
	return ans
print(score(s))


n = int(input())
s = input()
P = [int(a) for a in input().split()]
X = [[[0] * n for _ in range(n)] for __ in range(n)]

def dp(l, r, k):
	if X[l][r][k]:
		return X[l][r][k]
	if l == r:
		return P[k]
	ma = 0
	for i in range(l, r):
		ma = max(ma, dp(l, i, 0) + dp(i + 1, r, k))
		if s[i] == s[r]:
			ma = max(ma, dp(i + 1, r - 1, 0) + dp(l, i, k + 1))
	X[l][r][k] = ma
	return ma
print(dp(0, n - 1, 0))


import sys
from math import *
from fractions import gcd
readints = lambda : list(map(int, input().strip('\n').split()))
n = int(input())
s = input()
a = list(readints())
a = [0] + a
cache = {}

def solve(i, j, k):
	if i > j:
		return 0
	if i == j:
		return a[k]
	if (i, j, k) in cache:
		return cache[i, j, k]
	best = a[k] + solve(i + 1, j, 1)
	for x in range(i + 1, j + 1):
		if s[i] == s[x]:
			best = max(best, solve(i + 1, x - 1, 1) + solve(x, j, k + 1))
	cache[i, j, k] = best
	return best
ans = solve(0, n - 1, 1)
print(ans)


n = int(input())
s = input()
A = [0] + list(map(int, input().split()))
dp = [0] * (n + 1)
for i in range(1, n + 1):
	dp[i] = max((dp[j] + A[i - j] for j in range(0, i)))
res = {}

def score(s):
	length = len(s)
	if length == 0:
		return 0
	if s not in res.keys():
		ans = 0
		start = 0
		i = 1
		while i < length and s[i] == s[start]:
			i += 1
		ans = dp[i] + score(s[i:])
		for j in range(i, length):
			if s[j] == s[start]:
				ans = max(ans, score(s[i:j]) + score(s[:i] + s[j:]))
		res[s] = ans
	return res[s]
print(score(s))


n = int(input())
s = input()
a = [int(x) for x in input().split()]
dp = [[[False for i in range(101)] for j in range(101)] for k in range(101)]

def f(i, j, k):
	if not dp[i][j][k]:
		if i == j:
			dp[i][j][k] = 0
		else:
			dp[i][j][k] = f(i + 1, j, 0) + a[k]
			for m in range(i + 1, j):
				if s[i] == s[m]:
					dp[i][j][k] = max(dp[i][j][k], f(i + 1, m, 0) + f(m, j, k + 1))
	return dp[i][j][k]
answer = f(0, n, 0)
print(answer)


import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
n = int(input())
s = input()
a = list(map(int, input().split()))
N = len(a)
dp = [[[-1] * N for i in range(N)] for j in range(N)]

def calc(l, r, k):
	if dp[l][r][k] != -1:
		return dp[l][r][k]
	if l == r:
		return a[k]
	tmp = 0
	for i in range(l, r):
		tmp = max(tmp, calc(l, i, 0) + calc(i + 1, r, k))
		if s[i] == s[r]:
			tmp = max(tmp, calc(i + 1, r - 1, 0) + calc(l, i, k + 1))
	dp[l][r][k] = tmp
	return tmp
print(calc(0, n - 1, 0))


n = int(input())
s = input()
ai = [0] + list(map(int, input().split()))
ar = [ai[i] for i in range(n + 1)]
for i in range(1, n):
	num = 0
	for j in range(0, (i + 1) // 2 + 1):
		ar[i] = max(ar[i], ar[j] + ar[i - j])
ar2 = []
num = 1
for i in range(1, n):
	if s[i] == s[i - 1]:
		num += 1
	else:
		ar2 += [num]
		num = 1
ar2 += [num]
n2 = len(ar2)
ar = ar
ar3 = [[-1] * (n2 + 5) for i in range(n2 + 5)]
ar4 = [[[-1] * 102 for i in range(102)] for i in range(102)]
num5 = 0

def fun(i, n3, num3):
	if i > n3 - 1:
		return 0
	ans = 0
	num = num3
	num2 = 0
	for j in range(i, n3, 2):
		num += ar2[j]
		if ar3[j + 1][n3] == -1:
			ar3[j + 1][n3] = fun(j + 1, n3, 0)
		ans = max(ans, ar3[j + 1][n3] + ar[num] + num2)
		for z in range(j + 3, n3, 2):
			if ar3[j + 1][z + 1] == -1:
				ar3[j + 1][z + 1] = fun(j + 1, z + 1, 0)
			if ar4[z + 1][n3][num] == -1:
				ar4[z + 1][n3][num] = fun(z + 1, n3, num)
			ans = max(ans, ar3[j + 1][z + 1] + ar4[z + 1][n3][num] + num2)
		if j < n3 - 1:
			num2 += ar[ar2[j + 1]]
	return ans
print(fun(0, n2, 0))


from sys import stdin
uu = 0
n = int(stdin.readline().strip())
s1 = stdin.readline().strip()
s = list(map(int, stdin.readline().strip().split()))
dp = [-1 for i in range(n + 1)]

def sol1(x):
	if dp[x] != -1:
		return dp[x]
	if x == 0:
		return 0
	ans = 0
	for i in range(x):
		ans = max(ans, s[i] + sol1(x - i - 1))
	dp[x] = ans
for i in range(n):
	sol1(i + 1)
dp[0] = 0
s2 = []
x = 1
for i in range(1, len(s)):
	if s1[i] != s1[i - 1]:
		s2.append(x)
		x = 1
	else:
		x += 1
s2.append(x)
dp1 = [[[-1 for i in range(n + 1)] for j in range(n + 1)] for k in range(n + 1)]
n = len(s2)
s = s2.copy()

def sol(l, r, k):
	if l == r:
		return dp[s[l] + k]
	if l > r:
		return 0
	if dp1[l][r][k] != -1:
		return dp1[l][r][k]
	ans = 0
	for i in range(l, r + 1, 2):
		if i != l:
			ans = max(ans, sol(l + 1, i - 1, 0) + sol(i, r, s[l] + k))
		else:
			ans = max(ans, sol(i + 1, r, 0) + dp[s[l] + k])
	dp1[l][r][k] = ans
	return ans
print(sol(0, len(s) - 1, 0))