This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// I stand with PALESTINE
//#pragma GCC optimize("Ofast,O3")
//#pragma GCC target("avx,avx2")
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const ll inf = 1e18;
const int mod = 1007173513, coeff = 995269721;
void solve() {
int n;
ll A, B, C;
string s;
cin >> n >> s >> A >> B >> C;
vector dp(n, vector(n, inf));
vector hash(n, vector(n, 0ll));
map<ll, vector<int>> pos;
for (int i = 0; i < n; i++) {
ll val = 0, pw = 1;
for (int j = i; j < n; j++) {
val = (val + (s[j] - 'a' + 1) * pw) % mod;
pw = pw * coeff % mod;
hash[i][j] = val;
pos[val].emplace_back(i);
}
}
// set<ll> st;
// for (int ln = 1; ln <= n; ln++) {
// for (int i = 0, j = ln - 1; j < n; i++, j++) assert(!st.count(hash[i][j]));
// for (int i = 0, j = ln - 1; j < n; i++, j++) st.insert(hash[i][j]);
// }
for (int i = n - 1; i >= 0; i--) {
for (int j = i; j < n; j++) {
dp[i][j] = min(dp[i][j], (j - i + 1) * A);
if (i != j) {
dp[i][j] = min(dp[i][j], dp[i + 1][j] + A);
dp[i][j] = min(dp[i][j], dp[i][j - 1] + A);
}
auto &it = pos[hash[i][j]];
int h = upper_bound(it.begin(), it.end(), j) - it.begin(), cnt = 1;
int ln = j - i + 1;
// cout << i << ' ' << j << ' ' << dp[i][j] << endl;
while (h != it.size()) {
h = it[h] + ln - 1;
cnt++;
dp[i][h] = min(dp[i][h], dp[i][j] + B + cnt * C + (h - i + 1 - ln * cnt) * A);
// cout << '\t' << i << ' ' << h << ' ' << dp[i][h] << endl;
h = upper_bound(it.begin(), it.end(), h) - it.begin();
}
}
}
cout << dp[0][n - 1];
}
int main() {
cin.tie(0)->sync_with_stdio(false);
#ifdef sunnatov
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int q = 1;
// cin >> q;
while (q--) {
solve();
cout << '\n';
}
}
Compilation message (stderr)
copypaste3.cpp: In function 'void solve()':
copypaste3.cpp:48:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | while (h != it.size()) {
| ~~^~~~~~~~~~~~
# | 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... |