#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long
using i64 = long long;
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
const int inf = 1e15;
const int Mod = 998244353;
struct _hash{
vector <int> pw, p;
_hash() : pw(1, 1) {}
void modify(string s, int P){
int n = s.size();
while ( pw.size() <= n ){
pw.pb(pw.back() * 1LL * P % Mod);
}
p.resize(n + 1);
for ( int i = 0; i < n; i++ ){
p[i + 1] = (p[i] * 1LL * P + s[i] - 'a' + 1) % Mod;
}
}
int get(int l, int r){
return (p[r + 1] - p[l] * 1LL * pw[r - l + 1] % Mod + Mod) % Mod;
}
};
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
string s; cin >> s;
int A, B, C; cin >> A >> B >> C;
_hash h1, h2;
h1.modify(s, 31);
h2.modify(s, 47);
vector <vector<int>> nxt(n + 1, vector <int> (n));
for ( int l = 1; l <= n; l++ ){
map <pair<int,int>,int> lst;
for ( int i = n - l; i >= 0; i-- ){
int p = i, q = i + l - 1;
if ( q + l < n ){
lst[{h1.get(q + 1, q + l), h2.get(q + 1, q + l)}] = q + 1;
}
if ( lst.count({h1.get(p, q), h2.get(p, q)}) ){
nxt[l][i] = lst[{h1.get(p, q), h2.get(p, q)}];
} else{
nxt[l][i] = -1;
}
}
}
vector <vector<int>> dp(n, vector <int> (n, inf));
for ( int l = 1; l <= n; l++ ){
for ( int i = 0; i + l <= n; i++ ){
int p = i, q = i + l - 1;
chmin(dp[p][q], l * A);
if ( p + 1 <= q ){
chmin(dp[p][q], min(dp[p][q - 1], dp[p + 1][q]) + A);
}
int j = i, cnt = 1;
while ( (j = nxt[l][j]) != -1 ){
++cnt;
int r = j + l - 1;
chmin(dp[i][r], dp[p][q] + (r - i + 1 - l * cnt) * A + B + cnt * C);
}
}
}
cout << dp[0][n - 1];
cout << '\n';
}
Compilation message
copypaste3.cpp: In member function 'void _hash::modify(std::string, long long int)':
copypaste3.cpp:43:27: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
43 | while ( pw.size() <= n ){
| ~~~~~~~~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
460 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
81 ms |
52996 KB |
Output is correct |
4 |
Correct |
96 ms |
61776 KB |
Output is correct |
5 |
Correct |
121 ms |
72216 KB |
Output is correct |
6 |
Correct |
133 ms |
84188 KB |
Output is correct |
7 |
Correct |
171 ms |
98388 KB |
Output is correct |
8 |
Correct |
181 ms |
98388 KB |
Output is correct |
9 |
Correct |
177 ms |
98508 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
464 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
460 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
600 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
352 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
456 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
460 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
600 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
352 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
456 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
344 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
860 KB |
Output is correct |
42 |
Correct |
3 ms |
928 KB |
Output is correct |
43 |
Correct |
2 ms |
1116 KB |
Output is correct |
44 |
Correct |
2 ms |
1064 KB |
Output is correct |
45 |
Correct |
2 ms |
968 KB |
Output is correct |
46 |
Correct |
2 ms |
1064 KB |
Output is correct |
47 |
Correct |
1 ms |
864 KB |
Output is correct |
48 |
Correct |
2 ms |
932 KB |
Output is correct |
49 |
Correct |
2 ms |
1120 KB |
Output is correct |
50 |
Correct |
2 ms |
860 KB |
Output is correct |
51 |
Correct |
2 ms |
968 KB |
Output is correct |
52 |
Correct |
2 ms |
860 KB |
Output is correct |
53 |
Correct |
2 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
460 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
600 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
352 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
456 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
344 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
860 KB |
Output is correct |
42 |
Correct |
3 ms |
928 KB |
Output is correct |
43 |
Correct |
2 ms |
1116 KB |
Output is correct |
44 |
Correct |
2 ms |
1064 KB |
Output is correct |
45 |
Correct |
2 ms |
968 KB |
Output is correct |
46 |
Correct |
2 ms |
1064 KB |
Output is correct |
47 |
Correct |
1 ms |
864 KB |
Output is correct |
48 |
Correct |
2 ms |
932 KB |
Output is correct |
49 |
Correct |
2 ms |
1120 KB |
Output is correct |
50 |
Correct |
2 ms |
860 KB |
Output is correct |
51 |
Correct |
2 ms |
968 KB |
Output is correct |
52 |
Correct |
2 ms |
860 KB |
Output is correct |
53 |
Correct |
2 ms |
1116 KB |
Output is correct |
54 |
Correct |
10 ms |
3420 KB |
Output is correct |
55 |
Correct |
20 ms |
16244 KB |
Output is correct |
56 |
Correct |
53 ms |
16096 KB |
Output is correct |
57 |
Correct |
52 ms |
16212 KB |
Output is correct |
58 |
Correct |
50 ms |
16220 KB |
Output is correct |
59 |
Correct |
57 ms |
16208 KB |
Output is correct |
60 |
Correct |
50 ms |
16096 KB |
Output is correct |
61 |
Correct |
42 ms |
16208 KB |
Output is correct |
62 |
Correct |
19 ms |
16248 KB |
Output is correct |
63 |
Correct |
48 ms |
16056 KB |
Output is correct |
64 |
Correct |
47 ms |
16072 KB |
Output is correct |
65 |
Correct |
38 ms |
16072 KB |
Output is correct |
66 |
Correct |
41 ms |
16212 KB |
Output is correct |
67 |
Correct |
50 ms |
16212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
460 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
81 ms |
52996 KB |
Output is correct |
14 |
Correct |
96 ms |
61776 KB |
Output is correct |
15 |
Correct |
121 ms |
72216 KB |
Output is correct |
16 |
Correct |
133 ms |
84188 KB |
Output is correct |
17 |
Correct |
171 ms |
98388 KB |
Output is correct |
18 |
Correct |
181 ms |
98388 KB |
Output is correct |
19 |
Correct |
177 ms |
98508 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
464 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
600 KB |
Output is correct |
30 |
Correct |
0 ms |
344 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
352 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
456 KB |
Output is correct |
55 |
Correct |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
0 ms |
344 KB |
Output is correct |
57 |
Correct |
1 ms |
348 KB |
Output is correct |
58 |
Correct |
1 ms |
860 KB |
Output is correct |
59 |
Correct |
3 ms |
928 KB |
Output is correct |
60 |
Correct |
2 ms |
1116 KB |
Output is correct |
61 |
Correct |
2 ms |
1064 KB |
Output is correct |
62 |
Correct |
2 ms |
968 KB |
Output is correct |
63 |
Correct |
2 ms |
1064 KB |
Output is correct |
64 |
Correct |
1 ms |
864 KB |
Output is correct |
65 |
Correct |
2 ms |
932 KB |
Output is correct |
66 |
Correct |
2 ms |
1120 KB |
Output is correct |
67 |
Correct |
2 ms |
860 KB |
Output is correct |
68 |
Correct |
2 ms |
968 KB |
Output is correct |
69 |
Correct |
2 ms |
860 KB |
Output is correct |
70 |
Correct |
2 ms |
1116 KB |
Output is correct |
71 |
Correct |
10 ms |
3420 KB |
Output is correct |
72 |
Correct |
20 ms |
16244 KB |
Output is correct |
73 |
Correct |
53 ms |
16096 KB |
Output is correct |
74 |
Correct |
52 ms |
16212 KB |
Output is correct |
75 |
Correct |
50 ms |
16220 KB |
Output is correct |
76 |
Correct |
57 ms |
16208 KB |
Output is correct |
77 |
Correct |
50 ms |
16096 KB |
Output is correct |
78 |
Correct |
42 ms |
16208 KB |
Output is correct |
79 |
Correct |
19 ms |
16248 KB |
Output is correct |
80 |
Correct |
48 ms |
16056 KB |
Output is correct |
81 |
Correct |
47 ms |
16072 KB |
Output is correct |
82 |
Correct |
38 ms |
16072 KB |
Output is correct |
83 |
Correct |
41 ms |
16212 KB |
Output is correct |
84 |
Correct |
50 ms |
16212 KB |
Output is correct |
85 |
Correct |
141 ms |
39764 KB |
Output is correct |
86 |
Correct |
390 ms |
98644 KB |
Output is correct |
87 |
Correct |
369 ms |
98636 KB |
Output is correct |
88 |
Correct |
349 ms |
98620 KB |
Output is correct |
89 |
Correct |
349 ms |
98696 KB |
Output is correct |
90 |
Correct |
350 ms |
98620 KB |
Output is correct |
91 |
Correct |
180 ms |
98516 KB |
Output is correct |
92 |
Correct |
151 ms |
98392 KB |
Output is correct |
93 |
Correct |
360 ms |
98640 KB |
Output is correct |
94 |
Correct |
343 ms |
98512 KB |
Output is correct |
95 |
Correct |
325 ms |
98644 KB |
Output is correct |
96 |
Correct |
301 ms |
98644 KB |
Output is correct |
97 |
Correct |
348 ms |
98616 KB |
Output is correct |