#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define vi vector<int>
#define vl vector<long long>
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace std;
//mt19937 mt;void random(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll rand(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;
ll p = 2137;
ll P[5001];
string s;
ll dp[2500][2500];
ll A,B,C;
ll pref_hash[2501];
ll seg_hash[2500][2500];
ll nxt_same[2500][2500];
vi events[2500];
map<ll,int> pop;
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //random();
    int n;
    cin >> n >> s >> A >> B >> C;
    P[0] = p;
    rep2(i,1,5000)
    {
        P[i] = (P[i-1] * p) % MOD;
    }
    rep2(i,1,n)
    {
        pref_hash[i] = (pref_hash[i-1] + s[i-1]*P[i]) % MOD;
    }
    rep(i,n)
    {
        rep2(j,i,n-1)
        {
            seg_hash[i][j] = (pref_hash[j+1]-pref_hash[i]+MOD)%MOD;
            seg_hash[i][j] *= P[2500-i];
            seg_hash[i][j] %= MOD;
        }
    }
    rep2(d,1,n)
    {
        pop = {};
        rep2(i,d-1,n-1)
        {
            if(i-d-d+1 >= 0)
            {
                pop[seg_hash[i-d-d+1][i-d]] = i-d;
            }
            if(pop.find(seg_hash[i-d+1][i]) != pop.end()) nxt_same[i-d+1][i] = pop[seg_hash[i-d+1][i]];
            else nxt_same[i-d+1][i] = -1;
        }
    }
    unordered_map<ll,ll> hash_cnt;
    rep(r,n)
    {
        hash_cnt = {};
        rep2(d,1,r-1)
        {
            events[r-d+1].pb(d);
        }
        ll cur_min = 1e18;
        for(int l = r; l >= 0; l--)
        {
            dp[l][r] = A*(r-l+1);
            if(l != r) dp[l][r] = min(dp[l][r],dp[l][r-1] + A);
            int later = -1;
            forall(it,events[l])
            {
                if(l + it-1 == r)
                {
                    later = it;
                }
                else
                {
                    hash_cnt[seg_hash[l][l+it-1]]++;
                    cur_min = min(cur_min,(-A*it+C)*hash_cnt[seg_hash[l][l+it-1]]+dp[l][l+it-1]);
                    if(nxt_same[l][l+it-1] != -1) events[nxt_same[l][l+it-1]-it+1].pb(it);
                }
            }
            events[l] = {};
            dp[l][r] = min(dp[l][r],A*(r-l+1) + cur_min+B);
            if(later != -1)
            {
                hash_cnt[seg_hash[l][l+later-1]]++;
                cur_min = min(cur_min,(-A*later+C)*hash_cnt[seg_hash[l][l+later-1]]+dp[l][r]);
                if(nxt_same[l][r] != -1) events[nxt_same[l][r]-later+1].pb(later);
            }
        } 
    }
    cout << dp[0][n-1];
}
| # | 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... |