#include <bits/stdc++.h>
#define int long long
using namespace std;
int pre[2504][2504];
long long dp[2504][2504];
int pw(int x, int y, int mod){
if(!y) return 1;
int gop = 1;
if(y % 2){
gop = x;
y -= 1;
}
int val = pw(x, y / 2, mod);
return (long long)val * val % mod * gop % mod;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
memset(pre, -1, sizeof(pre));
int n; cin >> n;
string s; cin >> s;
int a, b, c; cin >> a >> b >> c;
int p = 998244353, mod = (int)1e9 + 7;
int p2 = 37, mod2 = 998244353;
int invp = pw(p, mod - 2, mod);
int invp2 = pw(p2, mod2 - 2, mod2);
for(int i = 1; i <= n; ++i){
int pi = 1, p2i = 1;
for(int j = 0; j < i; ++j){
pi = (long long)pi * p % mod;
p2i = (long long)p2i * p2 % mod2;
}
int now = 0, now2 = 0;
for(int j = i - 1; j >= 0; --j){
now = (long long)now * p % mod;
now = ((long long)now + s[j] - 'a') % mod;
now2 = (long long)now2 * p2 % mod;
now2 = ((long long)now2 + s[j] - 'a') % mod2;
}
map<pair<int, int>, deque<int>> pos;
for(int j = 0; j + i - 1 < n; ++j){
auto&nowp = pos[make_pair(now, now2)];
while((int)nowp.size() >= 2 && nowp[1] + i <= j){
nowp.pop_front();
}
if((int)nowp.size() && nowp[0] + i <= j){
pre[j][i] = nowp[0];
}
nowp.push_back(j);
if(j + i < n){
now = ((long long)now + (long long)pi * (s[j + i] - 'a') % mod) % mod;
now = ((long long)now - s[j] + 'a' + mod) % mod;
now = (long long)now * invp % mod;
now2 = ((long long)now2 + (long long)p2i * (s[j + i] - 'a') % mod2) % mod2;
now2 = ((long long)now2 - s[j] + 'a' + mod2) % mod2;
now2 = (long long)now2 * invp2 % mod2;
}
}
}
for(int j = 0; j < n; ++j){
long long mnval = (long long)1e18;
vector<long long> mnadd(n, (long long)1e18);
for(int i = j; i >= 0; --i){
if(i == j){
dp[i][j] = a;
}
else{
dp[i][j] = dp[i][j - 1] + a;
}
mnval = min(mnval, mnadd[i]);
dp[i][j] = min(dp[i][j], mnval - (long long)a * i);
long long cnt = 0;
for(int k = i; k != -1; k = pre[k][j - i + 1]){
++cnt;
mnadd[k] = min(mnadd[k], dp[i][j] + b + cnt * c + a * (j - k + 1 - cnt * (j - i + 1)) + (long long)k * a);
}
}
}
cout << dp[0][n - 1] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
49364 KB |
Output is correct |
2 |
Correct |
20 ms |
49312 KB |
Output is correct |
3 |
Correct |
23 ms |
49344 KB |
Output is correct |
4 |
Correct |
21 ms |
49368 KB |
Output is correct |
5 |
Correct |
22 ms |
49368 KB |
Output is correct |
6 |
Correct |
20 ms |
49312 KB |
Output is correct |
7 |
Correct |
19 ms |
49304 KB |
Output is correct |
8 |
Correct |
20 ms |
49352 KB |
Output is correct |
9 |
Correct |
19 ms |
49404 KB |
Output is correct |
10 |
Correct |
19 ms |
49308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
49360 KB |
Output is correct |
2 |
Correct |
23 ms |
49364 KB |
Output is correct |
3 |
Correct |
297 ms |
69872 KB |
Output is correct |
4 |
Correct |
424 ms |
72700 KB |
Output is correct |
5 |
Correct |
435 ms |
75784 KB |
Output is correct |
6 |
Correct |
563 ms |
79224 KB |
Output is correct |
7 |
Correct |
705 ms |
82864 KB |
Output is correct |
8 |
Correct |
686 ms |
82940 KB |
Output is correct |
9 |
Correct |
751 ms |
82836 KB |
Output is correct |
10 |
Correct |
18 ms |
49364 KB |
Output is correct |
11 |
Correct |
19 ms |
49364 KB |
Output is correct |
12 |
Correct |
19 ms |
49364 KB |
Output is correct |
13 |
Correct |
24 ms |
49360 KB |
Output is correct |
14 |
Correct |
23 ms |
49344 KB |
Output is correct |
15 |
Correct |
21 ms |
49316 KB |
Output is correct |
16 |
Correct |
25 ms |
49424 KB |
Output is correct |
17 |
Correct |
19 ms |
49412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
49364 KB |
Output is correct |
2 |
Correct |
20 ms |
49312 KB |
Output is correct |
3 |
Correct |
23 ms |
49344 KB |
Output is correct |
4 |
Correct |
21 ms |
49368 KB |
Output is correct |
5 |
Correct |
22 ms |
49368 KB |
Output is correct |
6 |
Correct |
20 ms |
49312 KB |
Output is correct |
7 |
Correct |
19 ms |
49304 KB |
Output is correct |
8 |
Correct |
20 ms |
49352 KB |
Output is correct |
9 |
Correct |
19 ms |
49404 KB |
Output is correct |
10 |
Correct |
19 ms |
49308 KB |
Output is correct |
11 |
Correct |
21 ms |
49348 KB |
Output is correct |
12 |
Correct |
18 ms |
49420 KB |
Output is correct |
13 |
Correct |
19 ms |
49360 KB |
Output is correct |
14 |
Correct |
19 ms |
49416 KB |
Output is correct |
15 |
Correct |
23 ms |
49492 KB |
Output is correct |
16 |
Correct |
19 ms |
49364 KB |
Output is correct |
17 |
Correct |
19 ms |
49376 KB |
Output is correct |
18 |
Correct |
19 ms |
49388 KB |
Output is correct |
19 |
Correct |
19 ms |
49356 KB |
Output is correct |
20 |
Correct |
18 ms |
49320 KB |
Output is correct |
21 |
Correct |
19 ms |
49500 KB |
Output is correct |
22 |
Correct |
21 ms |
49492 KB |
Output is correct |
23 |
Correct |
22 ms |
49448 KB |
Output is correct |
24 |
Correct |
19 ms |
49492 KB |
Output is correct |
25 |
Correct |
21 ms |
49428 KB |
Output is correct |
26 |
Correct |
20 ms |
49492 KB |
Output is correct |
27 |
Correct |
20 ms |
49492 KB |
Output is correct |
28 |
Incorrect |
20 ms |
49492 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
49364 KB |
Output is correct |
2 |
Correct |
20 ms |
49312 KB |
Output is correct |
3 |
Correct |
23 ms |
49344 KB |
Output is correct |
4 |
Correct |
21 ms |
49368 KB |
Output is correct |
5 |
Correct |
22 ms |
49368 KB |
Output is correct |
6 |
Correct |
20 ms |
49312 KB |
Output is correct |
7 |
Correct |
19 ms |
49304 KB |
Output is correct |
8 |
Correct |
20 ms |
49352 KB |
Output is correct |
9 |
Correct |
19 ms |
49404 KB |
Output is correct |
10 |
Correct |
19 ms |
49308 KB |
Output is correct |
11 |
Correct |
21 ms |
49348 KB |
Output is correct |
12 |
Correct |
18 ms |
49420 KB |
Output is correct |
13 |
Correct |
19 ms |
49360 KB |
Output is correct |
14 |
Correct |
19 ms |
49416 KB |
Output is correct |
15 |
Correct |
23 ms |
49492 KB |
Output is correct |
16 |
Correct |
19 ms |
49364 KB |
Output is correct |
17 |
Correct |
19 ms |
49376 KB |
Output is correct |
18 |
Correct |
19 ms |
49388 KB |
Output is correct |
19 |
Correct |
19 ms |
49356 KB |
Output is correct |
20 |
Correct |
18 ms |
49320 KB |
Output is correct |
21 |
Correct |
19 ms |
49500 KB |
Output is correct |
22 |
Correct |
21 ms |
49492 KB |
Output is correct |
23 |
Correct |
22 ms |
49448 KB |
Output is correct |
24 |
Correct |
19 ms |
49492 KB |
Output is correct |
25 |
Correct |
21 ms |
49428 KB |
Output is correct |
26 |
Correct |
20 ms |
49492 KB |
Output is correct |
27 |
Correct |
20 ms |
49492 KB |
Output is correct |
28 |
Incorrect |
20 ms |
49492 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
49364 KB |
Output is correct |
2 |
Correct |
20 ms |
49312 KB |
Output is correct |
3 |
Correct |
23 ms |
49344 KB |
Output is correct |
4 |
Correct |
21 ms |
49368 KB |
Output is correct |
5 |
Correct |
22 ms |
49368 KB |
Output is correct |
6 |
Correct |
20 ms |
49312 KB |
Output is correct |
7 |
Correct |
19 ms |
49304 KB |
Output is correct |
8 |
Correct |
20 ms |
49352 KB |
Output is correct |
9 |
Correct |
19 ms |
49404 KB |
Output is correct |
10 |
Correct |
19 ms |
49308 KB |
Output is correct |
11 |
Correct |
21 ms |
49348 KB |
Output is correct |
12 |
Correct |
18 ms |
49420 KB |
Output is correct |
13 |
Correct |
19 ms |
49360 KB |
Output is correct |
14 |
Correct |
19 ms |
49416 KB |
Output is correct |
15 |
Correct |
23 ms |
49492 KB |
Output is correct |
16 |
Correct |
19 ms |
49364 KB |
Output is correct |
17 |
Correct |
19 ms |
49376 KB |
Output is correct |
18 |
Correct |
19 ms |
49388 KB |
Output is correct |
19 |
Correct |
19 ms |
49356 KB |
Output is correct |
20 |
Correct |
18 ms |
49320 KB |
Output is correct |
21 |
Correct |
19 ms |
49500 KB |
Output is correct |
22 |
Correct |
21 ms |
49492 KB |
Output is correct |
23 |
Correct |
22 ms |
49448 KB |
Output is correct |
24 |
Correct |
19 ms |
49492 KB |
Output is correct |
25 |
Correct |
21 ms |
49428 KB |
Output is correct |
26 |
Correct |
20 ms |
49492 KB |
Output is correct |
27 |
Correct |
20 ms |
49492 KB |
Output is correct |
28 |
Incorrect |
20 ms |
49492 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
49364 KB |
Output is correct |
2 |
Correct |
20 ms |
49312 KB |
Output is correct |
3 |
Correct |
23 ms |
49344 KB |
Output is correct |
4 |
Correct |
21 ms |
49368 KB |
Output is correct |
5 |
Correct |
22 ms |
49368 KB |
Output is correct |
6 |
Correct |
20 ms |
49312 KB |
Output is correct |
7 |
Correct |
19 ms |
49304 KB |
Output is correct |
8 |
Correct |
20 ms |
49352 KB |
Output is correct |
9 |
Correct |
19 ms |
49404 KB |
Output is correct |
10 |
Correct |
19 ms |
49308 KB |
Output is correct |
11 |
Correct |
20 ms |
49360 KB |
Output is correct |
12 |
Correct |
23 ms |
49364 KB |
Output is correct |
13 |
Correct |
297 ms |
69872 KB |
Output is correct |
14 |
Correct |
424 ms |
72700 KB |
Output is correct |
15 |
Correct |
435 ms |
75784 KB |
Output is correct |
16 |
Correct |
563 ms |
79224 KB |
Output is correct |
17 |
Correct |
705 ms |
82864 KB |
Output is correct |
18 |
Correct |
686 ms |
82940 KB |
Output is correct |
19 |
Correct |
751 ms |
82836 KB |
Output is correct |
20 |
Correct |
18 ms |
49364 KB |
Output is correct |
21 |
Correct |
19 ms |
49364 KB |
Output is correct |
22 |
Correct |
19 ms |
49364 KB |
Output is correct |
23 |
Correct |
24 ms |
49360 KB |
Output is correct |
24 |
Correct |
23 ms |
49344 KB |
Output is correct |
25 |
Correct |
21 ms |
49316 KB |
Output is correct |
26 |
Correct |
25 ms |
49424 KB |
Output is correct |
27 |
Correct |
19 ms |
49412 KB |
Output is correct |
28 |
Correct |
21 ms |
49348 KB |
Output is correct |
29 |
Correct |
18 ms |
49420 KB |
Output is correct |
30 |
Correct |
19 ms |
49360 KB |
Output is correct |
31 |
Correct |
19 ms |
49416 KB |
Output is correct |
32 |
Correct |
23 ms |
49492 KB |
Output is correct |
33 |
Correct |
19 ms |
49364 KB |
Output is correct |
34 |
Correct |
19 ms |
49376 KB |
Output is correct |
35 |
Correct |
19 ms |
49388 KB |
Output is correct |
36 |
Correct |
19 ms |
49356 KB |
Output is correct |
37 |
Correct |
18 ms |
49320 KB |
Output is correct |
38 |
Correct |
19 ms |
49500 KB |
Output is correct |
39 |
Correct |
21 ms |
49492 KB |
Output is correct |
40 |
Correct |
22 ms |
49448 KB |
Output is correct |
41 |
Correct |
19 ms |
49492 KB |
Output is correct |
42 |
Correct |
21 ms |
49428 KB |
Output is correct |
43 |
Correct |
20 ms |
49492 KB |
Output is correct |
44 |
Correct |
20 ms |
49492 KB |
Output is correct |
45 |
Incorrect |
20 ms |
49492 KB |
Output isn't correct |
46 |
Halted |
0 ms |
0 KB |
- |