Vasya and Binary String
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! 
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$$$.
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$$$.
Print one integer — the maximum total points Vasya can get.
n = int(input())
s = input()
cost = list(map(int, input().split()))
groups = []
cl = 1
for i in range(1, n):
    if s[i] == s[i - 1]:
        cl += 1
    else:
        groups.append(cl)
        cl = 1
groups.append(cl)
rod = [0] * (n + 1)
rod[1] = cost[0]
for i in range(1, n):
    for j in range(1, i + 1):
        rod[i] = max(rod[i], cost[j - 1] + rod[i - j])
dp = [[[-1] * 102 for _ in range(102)] for _ in range(102)]

def solve(groups, i, j, extras, dp):
    if i > j:
        return 0
    if dp[i][j][extras] != -1:
        return dp[i][j][extras]
    ans = rod[groups[i] + extras] + solve(groups, i + 1, j, 0, dp)
    for k in range(i + 2, j + 1, 2):
        ans = max(ans, solve(groups, i + 1, k - 1, 0, dp) + solve(groups, k, j, extras + groups[i], dp))
    dp[i][j][extras] = ans
    return ans
print(solve(groups, 0, len(groups) - 1, 0, dp))
import itertools
from functools import lru_cache

class Solution:

    def maxPoints(self, s, points):
        bit_counts = [(bit, len(list(group))) for (bit, group) in itertools.groupby(s)]

        @lru_cache(None)
        def max_points_for_substr(L):
            if L <= 0:
                return 0
            ans = 0
            for k in range(1, L + 1):
                ans = max(ans, points[k - 1] + max_points_for_substr(L - k))
            return ans

        @lru_cache(None)
        def collapse(i, j, past=0):
            if i > j:
                return 0
            ans = max_points_for_substr(bit_counts[i][1] + past) + collapse(i + 1, j)
            for k in range(i + 1, j + 1):
                if bit_counts[k][0] == bit_counts[i][0]:
                    ans = max(ans, collapse(i + 1, k - 1) + collapse(k, j, past + bit_counts[i][1]))
            return ans
        return collapse(0, len(bit_counts) - 1)

def read_input():
    n = int(input())
    s = input()
    points = list(map(int, input().split()))
    return [n, s, points]

def main():
    (n, s, points) = read_input()
    solution = Solution()
    print(solution.maxPoints(s, points))
if __name__ == '__main__':
    main()
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))