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$.

Difficulty: interview

Starter code:

Solutions:

# returns answer to the subproblem with interval range [start, end],
# but with a total of "extra" additional stuff on the end
# that must be deleted last.
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]
    
    # test all possible "cut points".
    # "cut" is the earliest index to die in the same deletion as "end".
    out = 0
    for cut in range(end, start-1, -2):
        if cut == end:
            # in this case, we're deleting the last interval right away.
            out_curr = rewards[dat[cut] + extra]
            out_curr += f(dat, rewards, start, cut-1, 0)
        else:
            # split into 2 pieces:
            # 1) slots [start, cut] plus [end + extra]
            # 2) slots [cut+1, end-1] (with no extra, this needs to get deleted first).
            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):
    # break into intervals.
    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
    
    # compute the highest-value way to remove a run of size k.
    # (google translated from C++ thinking)
    rewards = [0, rewards_orig[0]]
    for k in range(2, len(rewards_orig) + 1):
        # print(
        #     "{}: {}".format(
        #         k,
        #         [
        #             rewards[k-j] + rewards_orig[j-1]
        #             for j in range(1, k+1)
        #         ]
        #     )
        # )
        rewards.append(
            max(
                rewards[k-j] + rewards_orig[j-1]
                for j in range(1, k+1)
            )
        )
    
    # print("dat: {}".format(dat))
    # print("rewards: {}".format(rewards))
    
    return f(dat, rewards, 0, len(dat)-1, 0)

# get the integer
int_dummy = input()
# get the string
dat_str = input().strip()
# get the array
rewards_input = input().strip().split()
rewards_ints = [int(x) for x in rewards_input]

# print(dat_str)
# print(rewards_ints)

print((
    solve(
        dat_str,
        rewards_ints,
    )
))


# dat_test = "10101"
# rewards_test = [3, 10, 15, 15, 15]
# print(solve(dat_test, rewards_test))


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()
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))

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))

res = {}

@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))

# 545 ms
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))

from sys import stdin
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))


from sys import stdin
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))


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)

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))

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 #1-index

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))
    #print(i,j,best)
    cache[(i,j,k)]=best
    return best


ans=solve(0,n-1,1)
print(ans)
      


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))