Maximum Cost Deletion Codeforces Solution

Maximum Cost Deletion Codeforces Solution

You are given a string ss of length nn 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 ll, you get a⋅l+ba⋅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 tt (1≤t≤20001≤t≤2000) — the number of testcases.

The first line of each testcase contains three integers nn, aa and bb (1≤n≤100;−100≤a,b≤1001≤n≤100;−100≤a,b≤100) — the length of the string ss and the parameters aa and bb.

The second line contains the string ss. The string ss 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.

Example

input

3
3 2 0
000
5 -2 5
11001
6 1 -4
100111

output

6
15
-2

Note

In the first example, it is enough to delete the entire string, then we will get 2⋅3+0=62⋅3+0=6 points.

In the second example, if we delete characters one by one, then for each deleted character we will get (−2)⋅1+5=3(−2)⋅1+5=3 points, i. e. 1515 points in total.

In the third example, we can delete the substring 00 from the string 100111, we get 1⋅2+(−4)=−21⋅2+(−4)=−2 points, and the string will be equal to 1111, removing it entirely we get 1⋅4+(−4)=01⋅4+(−4)=0 points. In total, we got −2−2 points for 22 operations.

Solution

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

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    cin >> t;
    while(t--) {
        int n, a, b;
        string s;
        cin >> n >> a >> b >> s;
        if(b >= 0) {
            cout << n*(a+b) << endl;
            continue;
        }
        int ct[2] = {};
        ct[s[0]-48]++;
        for(int i = 1; i < n; i++) {
            if(s[i] != s[i-1]) {
                ct[s[i]-48]++;
            }
        }
        cout << n*a+(min(ct[0], ct[1])+1)*b << endl;
    }
    return 0;
}

Educational Codeforces Round 111 (Rated for Div. 2)

Leave a Comment

Please Click on 1 or 2 Ads to help us run this site.
+