# include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define f first
#define s second
#define int long long
#define pii pair <int, int>
const int N = 3005, mod = 1e9 + 7;
int t,n,a,b,c,nxt[N], dp[N][N],pw[N],p;
vector < pii > v;
int hs[N][N];
string s;
int get(char ch) {
return (ch - 'a' + 1);
}
void go(vector <int> vv, int sz) {
int cur = -1;
for (int i = (int)vv.size() - 1; i >= 0; i--) {
while(true) {
if (vv.back() >= vv[i] + sz) {
cur = vv.back();
vv.pop_back();
} else break;
}
nxt[vv[i]] = cur;
}
}
main() {
p = 37;
cin>>n>>s>>a>>b>>c;
s = "@" + s;
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
dp[i][j] = (j - i + 1)*a;
}
}
pw[0] = 1;
for (int i = 1; i <= n; i++) {
pw[i] = (pw[i - 1] * p) % mod;
}
for (int i = 1; i <= n; i++) {
int curhs = 0;
for (int j = i; j <= n; j++) {
curhs = (curhs * p + get(s[j])) % mod;
hs[i][j] = curhs;
}
}
for (int sz = 1; sz <= n; sz++) {
v.clear();
for (int j = 1; j + sz - 1 <= n; j++) {
v.pb({hs[j][j + sz - 1], j});
}
sort(v.begin(),v.end());
v.pb({-1, 0});
int le = 0;
for (int i = 1; i < v.size(); i++) {
if (v[i].f != v[i - 1].f) {
int ri = i - 1;
vector <int> vv;
for (int j = le; j <= ri; j++) {
vv.pb(v[j].s);
}
go(vv,sz);
le = i;
}
}
for (int l = 1; l + sz - 1 <= n; l++) {
if (sz > 1) dp[l][l + sz - 1] = min(dp[l][l + sz - 1], min(dp[l + 1][l + sz - 1] + a, dp[l][l + sz - 2] + a));
}
for (int l = 1; l + sz - 1 <= n; l++) {
int curl = l;
int cnt = 1;
while (nxt[curl] != -1) {
cnt++;
int ri = nxt[curl] + sz - 1;
dp[l][nxt[curl] + sz - 1] = min(dp[l][nxt[curl] + sz - 1], dp[l][l + sz - 1] + b + cnt * c + (ri - l + 1 - sz * cnt)*a);
curl = nxt[curl];
}
}
}
cout<<dp[1][n]<<"\n";
}
Compilation message
copypaste3.cpp:28:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
28 | main() {
| ^~~~
copypaste3.cpp: In function 'int main()':
copypaste3.cpp:57:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for (int i = 1; i < v.size(); i++) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
191 ms |
41292 KB |
Output is correct |
4 |
Correct |
208 ms |
46924 KB |
Output is correct |
5 |
Correct |
246 ms |
53332 KB |
Output is correct |
6 |
Correct |
306 ms |
60856 KB |
Output is correct |
7 |
Correct |
352 ms |
69516 KB |
Output is correct |
8 |
Correct |
348 ms |
69468 KB |
Output is correct |
9 |
Correct |
388 ms |
69472 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 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 |
0 ms |
468 KB |
Output is correct |
26 |
Correct |
0 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 |
0 ms |
468 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 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 |
0 ms |
468 KB |
Output is correct |
26 |
Correct |
0 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 |
0 ms |
468 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
980 KB |
Output is correct |
41 |
Correct |
3 ms |
2260 KB |
Output is correct |
42 |
Correct |
3 ms |
2260 KB |
Output is correct |
43 |
Correct |
3 ms |
2260 KB |
Output is correct |
44 |
Correct |
3 ms |
2260 KB |
Output is correct |
45 |
Correct |
3 ms |
2260 KB |
Output is correct |
46 |
Correct |
3 ms |
2260 KB |
Output is correct |
47 |
Correct |
3 ms |
2260 KB |
Output is correct |
48 |
Correct |
3 ms |
2260 KB |
Output is correct |
49 |
Correct |
3 ms |
2260 KB |
Output is correct |
50 |
Correct |
3 ms |
2260 KB |
Output is correct |
51 |
Correct |
3 ms |
2260 KB |
Output is correct |
52 |
Correct |
3 ms |
2260 KB |
Output is correct |
53 |
Correct |
3 ms |
2260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 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 |
0 ms |
468 KB |
Output is correct |
26 |
Correct |
0 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 |
0 ms |
468 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
980 KB |
Output is correct |
41 |
Correct |
3 ms |
2260 KB |
Output is correct |
42 |
Correct |
3 ms |
2260 KB |
Output is correct |
43 |
Correct |
3 ms |
2260 KB |
Output is correct |
44 |
Correct |
3 ms |
2260 KB |
Output is correct |
45 |
Correct |
3 ms |
2260 KB |
Output is correct |
46 |
Correct |
3 ms |
2260 KB |
Output is correct |
47 |
Correct |
3 ms |
2260 KB |
Output is correct |
48 |
Correct |
3 ms |
2260 KB |
Output is correct |
49 |
Correct |
3 ms |
2260 KB |
Output is correct |
50 |
Correct |
3 ms |
2260 KB |
Output is correct |
51 |
Correct |
3 ms |
2260 KB |
Output is correct |
52 |
Correct |
3 ms |
2260 KB |
Output is correct |
53 |
Correct |
3 ms |
2260 KB |
Output is correct |
54 |
Correct |
12 ms |
5524 KB |
Output is correct |
55 |
Correct |
36 ms |
16268 KB |
Output is correct |
56 |
Correct |
62 ms |
16244 KB |
Output is correct |
57 |
Correct |
61 ms |
16240 KB |
Output is correct |
58 |
Correct |
61 ms |
16244 KB |
Output is correct |
59 |
Correct |
65 ms |
16212 KB |
Output is correct |
60 |
Correct |
60 ms |
16248 KB |
Output is correct |
61 |
Correct |
63 ms |
16208 KB |
Output is correct |
62 |
Correct |
56 ms |
16252 KB |
Output is correct |
63 |
Correct |
63 ms |
16212 KB |
Output is correct |
64 |
Correct |
65 ms |
16240 KB |
Output is correct |
65 |
Correct |
66 ms |
16152 KB |
Output is correct |
66 |
Correct |
65 ms |
16240 KB |
Output is correct |
67 |
Correct |
60 ms |
16208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
191 ms |
41292 KB |
Output is correct |
14 |
Correct |
208 ms |
46924 KB |
Output is correct |
15 |
Correct |
246 ms |
53332 KB |
Output is correct |
16 |
Correct |
306 ms |
60856 KB |
Output is correct |
17 |
Correct |
352 ms |
69516 KB |
Output is correct |
18 |
Correct |
348 ms |
69468 KB |
Output is correct |
19 |
Correct |
388 ms |
69472 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
468 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
0 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 |
0 ms |
468 KB |
Output is correct |
43 |
Correct |
0 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 |
0 ms |
468 KB |
Output is correct |
51 |
Correct |
0 ms |
212 KB |
Output is correct |
52 |
Correct |
0 ms |
340 KB |
Output is correct |
53 |
Correct |
0 ms |
340 KB |
Output is correct |
54 |
Correct |
0 ms |
340 KB |
Output is correct |
55 |
Correct |
0 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
1 ms |
980 KB |
Output is correct |
58 |
Correct |
3 ms |
2260 KB |
Output is correct |
59 |
Correct |
3 ms |
2260 KB |
Output is correct |
60 |
Correct |
3 ms |
2260 KB |
Output is correct |
61 |
Correct |
3 ms |
2260 KB |
Output is correct |
62 |
Correct |
3 ms |
2260 KB |
Output is correct |
63 |
Correct |
3 ms |
2260 KB |
Output is correct |
64 |
Correct |
3 ms |
2260 KB |
Output is correct |
65 |
Correct |
3 ms |
2260 KB |
Output is correct |
66 |
Correct |
3 ms |
2260 KB |
Output is correct |
67 |
Correct |
3 ms |
2260 KB |
Output is correct |
68 |
Correct |
3 ms |
2260 KB |
Output is correct |
69 |
Correct |
3 ms |
2260 KB |
Output is correct |
70 |
Correct |
3 ms |
2260 KB |
Output is correct |
71 |
Correct |
12 ms |
5524 KB |
Output is correct |
72 |
Correct |
36 ms |
16268 KB |
Output is correct |
73 |
Correct |
62 ms |
16244 KB |
Output is correct |
74 |
Correct |
61 ms |
16240 KB |
Output is correct |
75 |
Correct |
61 ms |
16244 KB |
Output is correct |
76 |
Correct |
65 ms |
16212 KB |
Output is correct |
77 |
Correct |
60 ms |
16248 KB |
Output is correct |
78 |
Correct |
63 ms |
16208 KB |
Output is correct |
79 |
Correct |
56 ms |
16252 KB |
Output is correct |
80 |
Correct |
63 ms |
16212 KB |
Output is correct |
81 |
Correct |
65 ms |
16240 KB |
Output is correct |
82 |
Correct |
66 ms |
16152 KB |
Output is correct |
83 |
Correct |
65 ms |
16240 KB |
Output is correct |
84 |
Correct |
60 ms |
16208 KB |
Output is correct |
85 |
Correct |
163 ms |
32696 KB |
Output is correct |
86 |
Correct |
400 ms |
69480 KB |
Output is correct |
87 |
Correct |
383 ms |
69472 KB |
Output is correct |
88 |
Correct |
384 ms |
69472 KB |
Output is correct |
89 |
Correct |
383 ms |
69476 KB |
Output is correct |
90 |
Correct |
380 ms |
69476 KB |
Output is correct |
91 |
Correct |
412 ms |
69580 KB |
Output is correct |
92 |
Correct |
430 ms |
69488 KB |
Output is correct |
93 |
Correct |
405 ms |
69476 KB |
Output is correct |
94 |
Correct |
413 ms |
69580 KB |
Output is correct |
95 |
Correct |
477 ms |
69420 KB |
Output is correct |
96 |
Correct |
473 ms |
69580 KB |
Output is correct |
97 |
Correct |
377 ms |
69432 KB |
Output is correct |