This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
char s[202];
int qs[202][202], cnt[202][202][202];
ll dp[202][202];
ll A, B, C;
int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int n;
    cin >> n;
    for (int i = 1;i <= n;i++) {
        cin >> s[i];
    }
    for (int i = 1;i <= n;i++) {
        for (int j = 1;i + j <= n;j++) {
            qs[i][j] = s[i] == s[i + j];
        }
        for (int j = 1;j <= n;j++) {
            qs[i][j] += qs[i - 1][j];
        }
    }
    for (int i = 1;i <= n;i++) {
        for (int j = i;j <= n;j++) {
            int r = j - i + 1;
            cnt[i][j][j] = 1;
            for (int k = j + 1;k <= n;k++) {
                cnt[i][j][k] = cnt[i][j][k - 1];
                // x = k - j
                int x = k - j;
                if (qs[j][x] - qs[i - 1][x] == r)
                    cnt[i][j][k] = max(cnt[i][j][k], cnt[i][j][k - r] + 1);
            }
        }
    }
    cin >> A >> B >> C;
    for (int i = 1;i <= n;i++) dp[i][i] = A;
    for (int k = 1;k < n;k++) {
        for (int i = 1;i <= n - k;i++) {
            int j = i + k;
            dp[i][j] = (k + 1) * A;
            dp[i][j] = min(dp[i][j], A + dp[i + 1][j]);
            dp[i][j] = min(dp[i][j], dp[i][j - 1] + A);
            for (int l = 0;l < k;l++) {
                int c = cnt[i][i + l][j];
                ll val = A * (k + 1 - c * (l + 1));
                val += dp[i][i + l] + B;
                val += C * c;
                dp[i][j] = min(dp[i][j], val);
            }
        }
    }
    // calculate cost to make X = s[l...r]
    cout << dp[1][n] << '\n';
    return 0;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |