Maximum Cost Deletion (Codeforces 1550B)
Tags: Math, greedy

You are given a string $$$s$$$ of length $$$n$$$ consisting only of the characters 0 and 1.You perform the following operation until the string becomes empty: choose some consecutive substring of equal characters, erase it from the string and glue the remaining two parts together (any of them can be empty) in the same order. For example, if you erase the substring 111 from the string 111110, you will get the string 110. When you delete a substring of length $$$l$$$, you get $$$a \cdot l + b$$$ points.Your task is to calculate the maximum number of points that you can score in total, if you have to make the given string empty.

Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of testcases.The first line of each testcase contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$1 \le n \le 100; -100 \le a, b \le 100$$$) — the length of the string $$$s$$$ and the parameters $$$a$$$ and $$$b$$$.The second line contains the string $$$s$$$. The string $$$s$$$ consists only of the characters 0 and 1.

Output: For each testcase, print a single integer — the maximum number of points that you can score.

Note: In the first example, it is enough to delete the entire string, then we will get $$$2 \cdot 3 + 0 = 6$$$ points.In the second example, if we delete characters one by one, then for each deleted character we will get $$$(-2) \cdot 1 + 5 = 3$$$ points, i. e. $$$15$$$ points in total.In the third example, we can delete the substring 00 from the string 100111, we get $$$1 \cdot 2 + (-4) = -2$$$ points, and the string will be equal to 1111, removing it entirely we get $$$1 \cdot 4 + (-4) = 0$$$ points. In total, we got $$$-2$$$ points for $$$2$$$ operations.

Sample Inputs:
3
3 2 0
000
5 -2 5
11001
6 1 -4
100111

Sample Outputs:
6
15
-2

Solution (Python):
for i in range(int(input())):
    (n, a, b) = map(int, input().split())
    s = input()
    if b >= 0:
        print(n * a + n * b)
    else:
        zero_and_one = max(s.count('01'), s.count('10')) + 1
        print(n * a + b * zero_and_one)

Solution (Python):
from math import ceil

class Solution:

    def __init__(self):
        pass

    def solve(self):
        t = int(input())
        result = []
        for i in range(t):
            (n, a, b) = map(int, input().split())
            s = input()
            point = 0
            if b >= 0:
                point = (a + b) * len(s)
            else:
                count = 1
                prev = s[0]
                for char in s[1:]:
                    if char != prev:
                        count += 1
                        prev = char
                if count == 1 or count == 2:
                    point = a * len(s) + b * count
                elif count % 2 == 0:
                    point = a * len(s) + b * (count // 2 + 1)
                else:
                    point = a * len(s) + b * ceil(count / 2)
            result.append(point)
        for x in result:
            print(x)
if __name__ == '__main__':
    sol = Solution()
    sol.solve()

Solution (Python):
""""""

def maximum_cost_deletion():
    t = int(input())
    for _ in range(t):
        (n, a, b) = map(int, input().split(' '))
        s = input()
        if b >= 0:
            print(n * (a + b))
        else:
            print(n * a + b * (max(s.count('01'), s.count('10')) + 1))
if __name__ == '__main__':
    maximum_cost_deletion()