#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 = 998244353, maxn = 25e2 + 5, lg = 21, inf = ll(1e18) + 5, p = 9973;
ll dp[maxn][maxn];
ll pw[maxn], hsh[maxn];
void buildHsh(string s){
hsh[0] = s[0];
rep(i,1,sz(s)){
hsh[i] = (hsh[i-1]*p%mod + s[i])%mod;
}
pw[0] = 1;
rep(i,1,sz(s)) pw[i] = pw[i-1]*p%mod;
}
int get(int l, int r){
return (hsh[r] - (l? hsh[l-1]*pw[r-l+1]%mod: 0) + mod)%mod;
}
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<int, 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;
int 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 |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
328 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 |
340 KB |
Output is correct |
10 |
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 |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
155 ms |
20812 KB |
Output is correct |
4 |
Correct |
194 ms |
23596 KB |
Output is correct |
5 |
Correct |
240 ms |
26784 KB |
Output is correct |
6 |
Correct |
309 ms |
30156 KB |
Output is correct |
7 |
Correct |
351 ms |
33748 KB |
Output is correct |
8 |
Correct |
372 ms |
33832 KB |
Output is correct |
9 |
Correct |
364 ms |
33860 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
328 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
328 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 |
340 KB |
Output is correct |
10 |
Correct |
1 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 |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
460 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
460 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 |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
340 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 |
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 |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
328 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 |
340 KB |
Output is correct |
10 |
Correct |
1 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 |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
460 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
460 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 |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
716 KB |
Output is correct |
41 |
Correct |
1 ms |
1236 KB |
Output is correct |
42 |
Correct |
2 ms |
1224 KB |
Output is correct |
43 |
Correct |
3 ms |
1236 KB |
Output is correct |
44 |
Correct |
2 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 |
2 ms |
1236 KB |
Output is correct |
50 |
Correct |
3 ms |
1236 KB |
Output is correct |
51 |
Correct |
2 ms |
1236 KB |
Output is correct |
52 |
Correct |
2 ms |
1236 KB |
Output is correct |
53 |
Correct |
2 ms |
1228 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 |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
328 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 |
340 KB |
Output is correct |
10 |
Correct |
1 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 |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
460 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
460 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 |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
716 KB |
Output is correct |
41 |
Correct |
1 ms |
1236 KB |
Output is correct |
42 |
Correct |
2 ms |
1224 KB |
Output is correct |
43 |
Correct |
3 ms |
1236 KB |
Output is correct |
44 |
Correct |
2 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 |
2 ms |
1236 KB |
Output is correct |
50 |
Correct |
3 ms |
1236 KB |
Output is correct |
51 |
Correct |
2 ms |
1236 KB |
Output is correct |
52 |
Correct |
2 ms |
1236 KB |
Output is correct |
53 |
Correct |
2 ms |
1228 KB |
Output is correct |
54 |
Correct |
12 ms |
2940 KB |
Output is correct |
55 |
Correct |
22 ms |
8296 KB |
Output is correct |
56 |
Correct |
64 ms |
8264 KB |
Output is correct |
57 |
Correct |
61 ms |
8276 KB |
Output is correct |
58 |
Correct |
61 ms |
8336 KB |
Output is correct |
59 |
Correct |
62 ms |
8276 KB |
Output is correct |
60 |
Correct |
59 ms |
8276 KB |
Output is correct |
61 |
Correct |
55 ms |
8276 KB |
Output is correct |
62 |
Correct |
23 ms |
8276 KB |
Output is correct |
63 |
Correct |
56 ms |
8300 KB |
Output is correct |
64 |
Correct |
55 ms |
8276 KB |
Output is correct |
65 |
Correct |
51 ms |
8276 KB |
Output is correct |
66 |
Correct |
53 ms |
8300 KB |
Output is correct |
67 |
Correct |
60 ms |
8320 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 |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
328 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 |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
155 ms |
20812 KB |
Output is correct |
14 |
Correct |
194 ms |
23596 KB |
Output is correct |
15 |
Correct |
240 ms |
26784 KB |
Output is correct |
16 |
Correct |
309 ms |
30156 KB |
Output is correct |
17 |
Correct |
351 ms |
33748 KB |
Output is correct |
18 |
Correct |
372 ms |
33832 KB |
Output is correct |
19 |
Correct |
364 ms |
33860 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
328 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
332 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 |
332 KB |
Output is correct |
31 |
Correct |
0 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
0 ms |
340 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 |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
460 KB |
Output is correct |
42 |
Correct |
1 ms |
464 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
460 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
212 KB |
Output is correct |
52 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
716 KB |
Output is correct |
58 |
Correct |
1 ms |
1236 KB |
Output is correct |
59 |
Correct |
2 ms |
1224 KB |
Output is correct |
60 |
Correct |
3 ms |
1236 KB |
Output is correct |
61 |
Correct |
2 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 |
2 ms |
1236 KB |
Output is correct |
67 |
Correct |
3 ms |
1236 KB |
Output is correct |
68 |
Correct |
2 ms |
1236 KB |
Output is correct |
69 |
Correct |
2 ms |
1236 KB |
Output is correct |
70 |
Correct |
2 ms |
1228 KB |
Output is correct |
71 |
Correct |
12 ms |
2940 KB |
Output is correct |
72 |
Correct |
22 ms |
8296 KB |
Output is correct |
73 |
Correct |
64 ms |
8264 KB |
Output is correct |
74 |
Correct |
61 ms |
8276 KB |
Output is correct |
75 |
Correct |
61 ms |
8336 KB |
Output is correct |
76 |
Correct |
62 ms |
8276 KB |
Output is correct |
77 |
Correct |
59 ms |
8276 KB |
Output is correct |
78 |
Correct |
55 ms |
8276 KB |
Output is correct |
79 |
Correct |
23 ms |
8276 KB |
Output is correct |
80 |
Correct |
56 ms |
8300 KB |
Output is correct |
81 |
Correct |
55 ms |
8276 KB |
Output is correct |
82 |
Correct |
51 ms |
8276 KB |
Output is correct |
83 |
Correct |
53 ms |
8300 KB |
Output is correct |
84 |
Correct |
60 ms |
8320 KB |
Output is correct |
85 |
Correct |
161 ms |
16468 KB |
Output is correct |
86 |
Correct |
435 ms |
33972 KB |
Output is correct |
87 |
Correct |
413 ms |
33972 KB |
Output is correct |
88 |
Correct |
408 ms |
33988 KB |
Output is correct |
89 |
Incorrect |
411 ms |
33876 KB |
Output isn't correct |
90 |
Halted |
0 ms |
0 KB |
- |