#define NDEBUG
#ifdef NDEBUG
#define dbg(TXTMSG) if constexpr (false) cerr << "lol"
#define dbgv(VARN) ((void)0)
#define dbgfor(COND) if constexpr (false) for (COND)
#else
#define _GLIBCXX_DEBUG 1
#define _GLIBCXX_DEBUG_PEDANTIC 1
#pragma GCC optimize("trapv")
#define dbg(TXTMSG) cerr << "\n" << TXTMSG
#define dbgv(VARN) cerr << "\n" << #VARN << " = "<< VARN << ", line " << __LINE__ << "\n"
#define dbgfor(COND) for (COND)
#endif
#include <bits/stdc++.h>
using namespace std;
using ll = long long; 
using pll = pair<ll,ll>;
#define e0 first
#define e1 second
constexpr ll INFTY = 2e18;
constexpr ll mod = 1e9+7;
ll powmod(ll b, ll e)
{
	ll o = 1;
	while (e>0)
	{
		if (e%2==1)
		{
			o*=b;
			o%=mod;
		}
		b*=b;
		b%=mod;
		e/=2;
	}
	return o;
}
int main()
{
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	dbgv("Started");
	ll N,A,B,C;
	string S;
	cin >> N >> S >> A >> B >> C;
	ll base = 31;
	vector<ll> hashsum(N+1);
	hashsum[0] = S[0]-'a'+1;
	for (ll i = 0; i < N; ++i)
	{
		hashsum[i+1]=hashsum[i] + (S[i]-'a'+1)*powmod(base,i);
		hashsum[i+1]%=mod;
	}
	auto gethash = [&](ll l, ll r)
	{
		return (((hashsum[r+1]-hashsum[l]+mod)%mod)*powmod(base,mod-1-l))%mod;
	};
	dbgv("hasdone");
	vector<vector<ll>> nextcpy(N+1,vector<ll>());
	for (ll l = 0; l <= N; ++l)
	{
		nextcpy[l].assign(N-l+1, INFTY);
		map<ll,ll> seenhashidx;
		for (ll i = N-2*l; i >= 0; --i)
		{
			seenhashidx[gethash(i+l,i+2*l-1)] = i+l;
			auto it = seenhashidx.find(gethash(i,i+l-1));
			if (it==seenhashidx.end())
			{
				nextcpy[l][i] = INFTY;
			}
			else
			{
				nextcpy[l][i] = it->e1;
			}
		}
	}
	//dbgv("ncpy done");
	for (ll i = 0; i < N; ++i)
	{
		dbgv(gethash(i,i));
		dbgv(nextcpy[1][i]);
	}
	vector<vector<ll>> cost(N+1, vector<ll>());
	for (ll l = 1; l <= N; ++l)
	{
		cost[l].assign(N-l+1, INFTY);
	}
	cost[0].assign(N+1,0);
	
	for (ll l = 1; l <= N; ++l)
	{
		for (ll i = 0; i <= N-l; ++i)
		{
			cost[l][i] = min({cost[l][i], cost[l-1][i]+A, A+cost[l-1][i+1]});
			ll eidx = nextcpy[l][i];
			dbgv(eidx);
			ll npaste = 2;
			while (eidx<INFTY)
			{
				cost[eidx-i+l][i] = min(cost[eidx-i+l][i], cost[l][i] + B + C*npaste + A*(eidx-i+l - l*npaste));
				npaste++;
				eidx = nextcpy[l][eidx];
			}
		}
	}
	for (ll i = 0; i < N-1; ++i)
	{
		//dbgv(gethash(i,i));
		dbgv(cost[2][i]);
	}
	cout << cost[N][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... |