#include <bits/stdc++.h>
using namespace std;
#define rep(i,l,r) for(int i = (l); i < (r); i++)
#define per(i,r,l) for(int i = (r); i >= (l); i--)
#define all(x) begin(x), end(x)
#define sz(x) (int)size(x)
#define pb push_back
#define ff first
#define ss second
typedef long long ll;
typedef pair<int, int> pp;
void dbg(){
cerr << endl;
}
template<typename H, typename... T> void dbg(H h, T... t){
cerr << h << ", ";
dbg(t...);
}
void IOS(){
cin.tie(0) -> sync_with_stdio(0);
#ifndef ONLINE_JUDGE
// freopen("inp.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#define er(...) cerr << __LINE__ << " <" << #__VA_ARGS__ << ">: ", dbg(__VA_ARGS__)
#else
#define er(...) 0
#endif
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 1e9 + 7, mod2 = 1e9 + 9, maxn = 25e2 + 5, lg = 21, inf = ll(1e18) + 5, p = 9973;
ll dp[maxn][maxn];
ll pw[2][maxn], hsh[2][maxn];
void buildHsh(string s){
hsh[0][0] = hsh[1][0] = s[0];
rep(i,1,sz(s)){
hsh[0][i] = (hsh[0][i-1]*p%mod + s[i])%mod;
hsh[1][i] = (hsh[1][i-1]*p%mod2 + s[i])%mod2;
}
pw[0][0] = pw[1][0] = 1;
rep(i,1,sz(s)+1) {
pw[0][i] = pw[0][i-1]*p%mod;
pw[1][i] = pw[1][i-1]*p%mod2;
}
}
pp get(int l, int r){
return {
(hsh[0][r] - (l? hsh[0][l-1]*pw[0][r-l+1]%mod: 0) + mod)%mod,
(hsh[1][r] - (l? hsh[1][l-1]*pw[1][r-l+1]%mod2: 0) + mod2)%mod2
};
}
int main(){ IOS();
int n; cin >> n;
string s; cin >> s;
ll A, C, P; cin >> A >> C >> P;
rep(i,0,n){
rep(j,i,n) dp[i][j] = inf;
}
buildHsh(s);
rep(l,1,n+1){
map<pp, int> mp;
vector<int> prv(n, -1);
rep(i,0,n-l+1){
int j = i + l - 1;
if(i < j) dp[i][j] = min({dp[i][j], dp[i+1][j] + A, dp[i][j-1] + A});
else dp[i][j] = A;
if(i >= l) mp[get(i-l, i-1)] = i-l;
pp cr = get(i, j);
if(mp.count(cr)) prv[i] = mp[cr];
int x = prv[i];
int cnt = 2;
while(x + 1){
dp[x][j] = min(dp[x][j], P*cnt + dp[i][j] + C + (j - x + 1 - l*cnt)*A);
x = prv[x];
cnt++;
}
}
}
cout << dp[0][n-1] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
198 ms |
20816 KB |
Output is correct |
4 |
Correct |
322 ms |
23636 KB |
Output is correct |
5 |
Correct |
395 ms |
26804 KB |
Output is correct |
6 |
Correct |
470 ms |
30164 KB |
Output is correct |
7 |
Correct |
388 ms |
33996 KB |
Output is correct |
8 |
Correct |
401 ms |
33816 KB |
Output is correct |
9 |
Correct |
384 ms |
33896 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
460 KB |
Output is correct |
40 |
Correct |
1 ms |
720 KB |
Output is correct |
41 |
Correct |
2 ms |
1236 KB |
Output is correct |
42 |
Correct |
3 ms |
1236 KB |
Output is correct |
43 |
Correct |
3 ms |
1236 KB |
Output is correct |
44 |
Correct |
3 ms |
1236 KB |
Output is correct |
45 |
Correct |
3 ms |
1236 KB |
Output is correct |
46 |
Correct |
3 ms |
1236 KB |
Output is correct |
47 |
Correct |
2 ms |
1236 KB |
Output is correct |
48 |
Correct |
3 ms |
1236 KB |
Output is correct |
49 |
Correct |
3 ms |
1236 KB |
Output is correct |
50 |
Correct |
3 ms |
1236 KB |
Output is correct |
51 |
Correct |
3 ms |
1236 KB |
Output is correct |
52 |
Correct |
2 ms |
1236 KB |
Output is correct |
53 |
Correct |
3 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
460 KB |
Output is correct |
40 |
Correct |
1 ms |
720 KB |
Output is correct |
41 |
Correct |
2 ms |
1236 KB |
Output is correct |
42 |
Correct |
3 ms |
1236 KB |
Output is correct |
43 |
Correct |
3 ms |
1236 KB |
Output is correct |
44 |
Correct |
3 ms |
1236 KB |
Output is correct |
45 |
Correct |
3 ms |
1236 KB |
Output is correct |
46 |
Correct |
3 ms |
1236 KB |
Output is correct |
47 |
Correct |
2 ms |
1236 KB |
Output is correct |
48 |
Correct |
3 ms |
1236 KB |
Output is correct |
49 |
Correct |
3 ms |
1236 KB |
Output is correct |
50 |
Correct |
3 ms |
1236 KB |
Output is correct |
51 |
Correct |
3 ms |
1236 KB |
Output is correct |
52 |
Correct |
2 ms |
1236 KB |
Output is correct |
53 |
Correct |
3 ms |
1236 KB |
Output is correct |
54 |
Correct |
12 ms |
2900 KB |
Output is correct |
55 |
Correct |
27 ms |
8276 KB |
Output is correct |
56 |
Correct |
69 ms |
8276 KB |
Output is correct |
57 |
Correct |
66 ms |
8396 KB |
Output is correct |
58 |
Correct |
65 ms |
8364 KB |
Output is correct |
59 |
Correct |
65 ms |
8276 KB |
Output is correct |
60 |
Correct |
67 ms |
8276 KB |
Output is correct |
61 |
Correct |
62 ms |
8276 KB |
Output is correct |
62 |
Correct |
25 ms |
8276 KB |
Output is correct |
63 |
Correct |
62 ms |
8456 KB |
Output is correct |
64 |
Correct |
61 ms |
8336 KB |
Output is correct |
65 |
Correct |
55 ms |
8324 KB |
Output is correct |
66 |
Correct |
56 ms |
8324 KB |
Output is correct |
67 |
Correct |
66 ms |
8364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
198 ms |
20816 KB |
Output is correct |
14 |
Correct |
322 ms |
23636 KB |
Output is correct |
15 |
Correct |
395 ms |
26804 KB |
Output is correct |
16 |
Correct |
470 ms |
30164 KB |
Output is correct |
17 |
Correct |
388 ms |
33996 KB |
Output is correct |
18 |
Correct |
401 ms |
33816 KB |
Output is correct |
19 |
Correct |
384 ms |
33896 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
1 ms |
468 KB |
Output is correct |
49 |
Correct |
1 ms |
468 KB |
Output is correct |
50 |
Correct |
1 ms |
468 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
0 ms |
336 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
460 KB |
Output is correct |
57 |
Correct |
1 ms |
720 KB |
Output is correct |
58 |
Correct |
2 ms |
1236 KB |
Output is correct |
59 |
Correct |
3 ms |
1236 KB |
Output is correct |
60 |
Correct |
3 ms |
1236 KB |
Output is correct |
61 |
Correct |
3 ms |
1236 KB |
Output is correct |
62 |
Correct |
3 ms |
1236 KB |
Output is correct |
63 |
Correct |
3 ms |
1236 KB |
Output is correct |
64 |
Correct |
2 ms |
1236 KB |
Output is correct |
65 |
Correct |
3 ms |
1236 KB |
Output is correct |
66 |
Correct |
3 ms |
1236 KB |
Output is correct |
67 |
Correct |
3 ms |
1236 KB |
Output is correct |
68 |
Correct |
3 ms |
1236 KB |
Output is correct |
69 |
Correct |
2 ms |
1236 KB |
Output is correct |
70 |
Correct |
3 ms |
1236 KB |
Output is correct |
71 |
Correct |
12 ms |
2900 KB |
Output is correct |
72 |
Correct |
27 ms |
8276 KB |
Output is correct |
73 |
Correct |
69 ms |
8276 KB |
Output is correct |
74 |
Correct |
66 ms |
8396 KB |
Output is correct |
75 |
Correct |
65 ms |
8364 KB |
Output is correct |
76 |
Correct |
65 ms |
8276 KB |
Output is correct |
77 |
Correct |
67 ms |
8276 KB |
Output is correct |
78 |
Correct |
62 ms |
8276 KB |
Output is correct |
79 |
Correct |
25 ms |
8276 KB |
Output is correct |
80 |
Correct |
62 ms |
8456 KB |
Output is correct |
81 |
Correct |
61 ms |
8336 KB |
Output is correct |
82 |
Correct |
55 ms |
8324 KB |
Output is correct |
83 |
Correct |
56 ms |
8324 KB |
Output is correct |
84 |
Correct |
66 ms |
8364 KB |
Output is correct |
85 |
Correct |
173 ms |
16544 KB |
Output is correct |
86 |
Correct |
460 ms |
34048 KB |
Output is correct |
87 |
Correct |
443 ms |
34032 KB |
Output is correct |
88 |
Correct |
459 ms |
34044 KB |
Output is correct |
89 |
Correct |
437 ms |
34004 KB |
Output is correct |
90 |
Correct |
442 ms |
34048 KB |
Output is correct |
91 |
Correct |
302 ms |
33892 KB |
Output is correct |
92 |
Correct |
313 ms |
33900 KB |
Output is correct |
93 |
Correct |
440 ms |
33916 KB |
Output is correct |
94 |
Correct |
419 ms |
33960 KB |
Output is correct |
95 |
Correct |
460 ms |
33904 KB |
Output is correct |
96 |
Correct |
517 ms |
33928 KB |
Output is correct |
97 |
Correct |
441 ms |
34124 KB |
Output is correct |