#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2500 + 2,MOD = (int)1e9 + 7;
struct H{
int p,mod;
vector<ll> h,inv;
ll binpow(ll a,ll b) {
ll ret = 1,k = a;
while(b) {
if(b & 1) {
ret *= k;
ret %= mod;
}
k *= k;
k%=mod;
b /= 2;
}
return ret;
}
void init(string s) {
int n = (int)s.size();
h.resize((int)s.size());
inv.resize((int)s.size());
ll x = 1;
for(int i = 0;i < n;i++) {
h[i] = x * 1ll * (s[i] - 'a' + 1)%mod;
if(i) h[i] += h[i - 1];
if(h[i] >= mod) h[i] -= mod;
inv[i] = binpow(x,mod-2);
x = x * 1ll * p%mod;
}
}
int get(int l,int r) {
ll val = h[r];
if(l) {
val -= h[l - 1];
}
if(val < 0) val += mod;
val = val * inv[l]%mod;
return val;
}
}xx,yy;
string s;
ll a,b,c;
int n;
ll dp[N][N];
int nx[N][N];
vector<pair<pair<int,int>,pair<int,int>>> f;
vector<array<int,3>> x;
void test() {
cin >> n >> s >> a >> b >> c;
yy.p = 37;yy.mod = 998244353;yy.init(s);
xx.p = 167;xx.mod = (int)1e9 + 7;xx.init(s);
bool sub = true;
for(int i = 0;i < n;i++) {
if(s[i] != 'a') {
sub = false;
}
}
ll res = n * 1ll * a;
for(int i = 0;i < n;i++) {
for(int j = i;j < n;j++) {
int ff = xx.get(i,j),tt = yy.get(i,j);
f.push_back({{ff,tt},{i,j}});
}
}
sort(f.begin(),f.end());
for(int i = 0;i < (int)f.size();) {
vector<pair<int,int>> k;
int e = i;
while(e < (int)f.size() && f[e].first == f[i].first) {
k.push_back(f[e].second);
e++;
}
i = e;
int it = (int)k.size() - 1;
for(int j = (int)k.size() - 1;j >= 0;j--) {
while(it >= 0 && k[it].first > k[j].second) {
it--;
}
if(it == (int)k.size() - 1) {
nx[k[j].first][k[j].second] = -1;
} else {
nx[k[j].first][k[j].second] = k[it + 1].first;
}
}
}
for(int l = 0;l < n;l++) {
for(int r = 0;r < n;r++) {
dp[l][r] = 1e18;
}
}
for(int i = 0;i < n;i++) {
dp[i][i] = a;
}
for(int s = 1;s <= n;s++) {
for(int l = 0;l + s - 1 < n;l++) {
x.push_back({s,l,l + s - 1});
}
}
for(auto [s,l,r]:x) {
dp[l][r] = min({dp[l][r],dp[l + 1][r] + a,dp[l][r - 1] + a});
int l_ = l,r_ = r;
int occ = 1;
int len = (r - l + 1);
// cout << l << ' ' << r << ' ' << dp[l][r] << '\n';
while(l_ != -1) {
// cout << l_ << "x\n";
int sz = (r_ - l + 1);
ll val = (sz - occ * 1ll * len) * 1ll * a + b + occ * c + dp[l][r];
dp[l][r_] = min(dp[l][r_],val);
occ++;
l_ = nx[l_][r_];
r_ = l_ + len - 1;
}
}
cout << dp[0][n-1] << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);cin.tie(0);
int t = 1;
// cin >> t;
while(t--) {
test();
}
}
Compilation message
copypaste3.cpp: In function 'void test()':
copypaste3.cpp:58:10: warning: variable 'sub' set but not used [-Wunused-but-set-variable]
58 | bool sub = true;
| ^~~
copypaste3.cpp:64:8: warning: unused variable 'res' [-Wunused-variable]
64 | ll res = n * 1ll * a;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
307 ms |
130044 KB |
Output is correct |
4 |
Correct |
353 ms |
143416 KB |
Output is correct |
5 |
Correct |
429 ms |
179608 KB |
Output is correct |
6 |
Correct |
500 ms |
188564 KB |
Output is correct |
7 |
Correct |
619 ms |
197908 KB |
Output is correct |
8 |
Correct |
595 ms |
197960 KB |
Output is correct |
9 |
Correct |
594 ms |
197516 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2392 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6488 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6488 KB |
Output is correct |
31 |
Correct |
1 ms |
6488 KB |
Output is correct |
32 |
Correct |
1 ms |
6492 KB |
Output is correct |
33 |
Correct |
1 ms |
6488 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2392 KB |
Output is correct |
36 |
Correct |
0 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6488 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6488 KB |
Output is correct |
31 |
Correct |
1 ms |
6488 KB |
Output is correct |
32 |
Correct |
1 ms |
6492 KB |
Output is correct |
33 |
Correct |
1 ms |
6488 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2392 KB |
Output is correct |
36 |
Correct |
0 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
1 ms |
6492 KB |
Output is correct |
40 |
Correct |
1 ms |
6744 KB |
Output is correct |
41 |
Correct |
4 ms |
9684 KB |
Output is correct |
42 |
Correct |
3 ms |
9688 KB |
Output is correct |
43 |
Correct |
4 ms |
9716 KB |
Output is correct |
44 |
Correct |
3 ms |
9688 KB |
Output is correct |
45 |
Correct |
3 ms |
9464 KB |
Output is correct |
46 |
Correct |
3 ms |
9688 KB |
Output is correct |
47 |
Correct |
4 ms |
9628 KB |
Output is correct |
48 |
Correct |
3 ms |
9688 KB |
Output is correct |
49 |
Correct |
3 ms |
9688 KB |
Output is correct |
50 |
Correct |
4 ms |
9460 KB |
Output is correct |
51 |
Correct |
4 ms |
9684 KB |
Output is correct |
52 |
Correct |
4 ms |
9688 KB |
Output is correct |
53 |
Correct |
3 ms |
9624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6488 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6488 KB |
Output is correct |
31 |
Correct |
1 ms |
6488 KB |
Output is correct |
32 |
Correct |
1 ms |
6492 KB |
Output is correct |
33 |
Correct |
1 ms |
6488 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2392 KB |
Output is correct |
36 |
Correct |
0 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
1 ms |
6492 KB |
Output is correct |
40 |
Correct |
1 ms |
6744 KB |
Output is correct |
41 |
Correct |
4 ms |
9684 KB |
Output is correct |
42 |
Correct |
3 ms |
9688 KB |
Output is correct |
43 |
Correct |
4 ms |
9716 KB |
Output is correct |
44 |
Correct |
3 ms |
9688 KB |
Output is correct |
45 |
Correct |
3 ms |
9464 KB |
Output is correct |
46 |
Correct |
3 ms |
9688 KB |
Output is correct |
47 |
Correct |
4 ms |
9628 KB |
Output is correct |
48 |
Correct |
3 ms |
9688 KB |
Output is correct |
49 |
Correct |
3 ms |
9688 KB |
Output is correct |
50 |
Correct |
4 ms |
9460 KB |
Output is correct |
51 |
Correct |
4 ms |
9684 KB |
Output is correct |
52 |
Correct |
4 ms |
9688 KB |
Output is correct |
53 |
Correct |
3 ms |
9624 KB |
Output is correct |
54 |
Correct |
15 ms |
23184 KB |
Output is correct |
55 |
Correct |
90 ms |
53172 KB |
Output is correct |
56 |
Correct |
71 ms |
53436 KB |
Output is correct |
57 |
Correct |
68 ms |
53432 KB |
Output is correct |
58 |
Correct |
68 ms |
53172 KB |
Output is correct |
59 |
Correct |
68 ms |
53436 KB |
Output is correct |
60 |
Correct |
67 ms |
53176 KB |
Output is correct |
61 |
Correct |
74 ms |
53172 KB |
Output is correct |
62 |
Correct |
84 ms |
53176 KB |
Output is correct |
63 |
Correct |
72 ms |
53172 KB |
Output is correct |
64 |
Correct |
72 ms |
53176 KB |
Output is correct |
65 |
Correct |
83 ms |
53432 KB |
Output is correct |
66 |
Correct |
81 ms |
53248 KB |
Output is correct |
67 |
Correct |
68 ms |
53176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
307 ms |
130044 KB |
Output is correct |
14 |
Correct |
353 ms |
143416 KB |
Output is correct |
15 |
Correct |
429 ms |
179608 KB |
Output is correct |
16 |
Correct |
500 ms |
188564 KB |
Output is correct |
17 |
Correct |
619 ms |
197908 KB |
Output is correct |
18 |
Correct |
595 ms |
197960 KB |
Output is correct |
19 |
Correct |
594 ms |
197516 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
0 ms |
2396 KB |
Output is correct |
22 |
Correct |
0 ms |
2396 KB |
Output is correct |
23 |
Correct |
0 ms |
2392 KB |
Output is correct |
24 |
Correct |
0 ms |
2396 KB |
Output is correct |
25 |
Correct |
0 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
0 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
6492 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
0 ms |
2396 KB |
Output is correct |
36 |
Correct |
0 ms |
2396 KB |
Output is correct |
37 |
Correct |
0 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
6492 KB |
Output is correct |
39 |
Correct |
1 ms |
6492 KB |
Output is correct |
40 |
Correct |
1 ms |
6492 KB |
Output is correct |
41 |
Correct |
1 ms |
6488 KB |
Output is correct |
42 |
Correct |
1 ms |
6492 KB |
Output is correct |
43 |
Correct |
1 ms |
6492 KB |
Output is correct |
44 |
Correct |
1 ms |
6492 KB |
Output is correct |
45 |
Correct |
1 ms |
6492 KB |
Output is correct |
46 |
Correct |
1 ms |
6492 KB |
Output is correct |
47 |
Correct |
1 ms |
6488 KB |
Output is correct |
48 |
Correct |
1 ms |
6488 KB |
Output is correct |
49 |
Correct |
1 ms |
6492 KB |
Output is correct |
50 |
Correct |
1 ms |
6488 KB |
Output is correct |
51 |
Correct |
1 ms |
2396 KB |
Output is correct |
52 |
Correct |
1 ms |
2392 KB |
Output is correct |
53 |
Correct |
0 ms |
2396 KB |
Output is correct |
54 |
Correct |
1 ms |
2396 KB |
Output is correct |
55 |
Correct |
1 ms |
2396 KB |
Output is correct |
56 |
Correct |
1 ms |
6492 KB |
Output is correct |
57 |
Correct |
1 ms |
6744 KB |
Output is correct |
58 |
Correct |
4 ms |
9684 KB |
Output is correct |
59 |
Correct |
3 ms |
9688 KB |
Output is correct |
60 |
Correct |
4 ms |
9716 KB |
Output is correct |
61 |
Correct |
3 ms |
9688 KB |
Output is correct |
62 |
Correct |
3 ms |
9464 KB |
Output is correct |
63 |
Correct |
3 ms |
9688 KB |
Output is correct |
64 |
Correct |
4 ms |
9628 KB |
Output is correct |
65 |
Correct |
3 ms |
9688 KB |
Output is correct |
66 |
Correct |
3 ms |
9688 KB |
Output is correct |
67 |
Correct |
4 ms |
9460 KB |
Output is correct |
68 |
Correct |
4 ms |
9684 KB |
Output is correct |
69 |
Correct |
4 ms |
9688 KB |
Output is correct |
70 |
Correct |
3 ms |
9624 KB |
Output is correct |
71 |
Correct |
15 ms |
23184 KB |
Output is correct |
72 |
Correct |
90 ms |
53172 KB |
Output is correct |
73 |
Correct |
71 ms |
53436 KB |
Output is correct |
74 |
Correct |
68 ms |
53432 KB |
Output is correct |
75 |
Correct |
68 ms |
53172 KB |
Output is correct |
76 |
Correct |
68 ms |
53436 KB |
Output is correct |
77 |
Correct |
67 ms |
53176 KB |
Output is correct |
78 |
Correct |
74 ms |
53172 KB |
Output is correct |
79 |
Correct |
84 ms |
53176 KB |
Output is correct |
80 |
Correct |
72 ms |
53172 KB |
Output is correct |
81 |
Correct |
72 ms |
53176 KB |
Output is correct |
82 |
Correct |
83 ms |
53432 KB |
Output is correct |
83 |
Correct |
81 ms |
53248 KB |
Output is correct |
84 |
Correct |
68 ms |
53176 KB |
Output is correct |
85 |
Correct |
191 ms |
111264 KB |
Output is correct |
86 |
Correct |
461 ms |
196868 KB |
Output is correct |
87 |
Correct |
463 ms |
198040 KB |
Output is correct |
88 |
Correct |
474 ms |
198292 KB |
Output is correct |
89 |
Correct |
443 ms |
197776 KB |
Output is correct |
90 |
Correct |
449 ms |
197576 KB |
Output is correct |
91 |
Correct |
559 ms |
197776 KB |
Output is correct |
92 |
Correct |
641 ms |
198324 KB |
Output is correct |
93 |
Correct |
524 ms |
197524 KB |
Output is correct |
94 |
Correct |
507 ms |
197960 KB |
Output is correct |
95 |
Correct |
617 ms |
197272 KB |
Output is correct |
96 |
Correct |
648 ms |
197304 KB |
Output is correct |
97 |
Correct |
537 ms |
198184 KB |
Output is correct |