#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,a,b,c;
string s;
cin >> n >> s >> a >> b >> c;
s.insert(s.begin(),'$');
vector<vector<int>> DP(n+1,vector<int>(n+1));
for(int l=1;l<=n;l++)for(int r=l;r<=n;r++)DP[l][r]=a*(r-l+1ll);
// Do the hashing stuff here
map<pair<int,char>,int> hasher;
vector<vector<int>> occurences = {{}};
int alloc = 0;
for(int l=1;l<=n;l++){
int currhash = 0;
for(int r=l;r<=n;r++){
if(!hasher.count({currhash,s[r]})){
hasher[{currhash,s[r]}]=++alloc;
occurences.emplace_back();
}
currhash = hasher[{currhash,s[r]}];
occurences[currhash].emplace_back(r);
}
}
for(int l=n;l;l--){
int currhash = 0;
for(int r=l;r<=n;r++){
currhash = hasher[{currhash,s[r]}];
int len = r-l+1;
if(l!=r){
DP[l][r] = min(DP[l][r],DP[l+1][r]+a);
DP[l][r] = min(DP[l][r],DP[l][r-1]+a);
}
auto iter = lower_bound(occurences[currhash].begin(),occurences[currhash].end(),r+len);
int cnt = 1;
while(iter!=occurences[currhash].end()){
int x = *iter;
cnt++;
DP[l][x] = min(DP[l][x],DP[l][r]+b+a*(x-l+1ll - cnt*(len))+cnt*c);
iter = lower_bound(occurences[currhash].begin(),occurences[currhash].end(),x+len);
}
}
}
cout << DP[1][n] << '\n';
}
# |
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 |
0 ms |
348 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 |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
3 |
Correct |
766 ms |
44536 KB |
Output is correct |
4 |
Correct |
950 ms |
51280 KB |
Output is correct |
5 |
Correct |
1137 ms |
60496 KB |
Output is correct |
6 |
Correct |
1328 ms |
71248 KB |
Output is correct |
7 |
Correct |
1603 ms |
83792 KB |
Output is correct |
8 |
Correct |
1558 ms |
83796 KB |
Output is correct |
9 |
Correct |
1588 ms |
84040 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 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 |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 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 |
1 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 |
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 |
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 |
1 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 |
# |
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 |
0 ms |
348 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 |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 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 |
1 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 |
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 |
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 |
1 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 |
1 ms |
860 KB |
Output is correct |
41 |
Correct |
3 ms |
860 KB |
Output is correct |
42 |
Correct |
7 ms |
2968 KB |
Output is correct |
43 |
Correct |
6 ms |
2964 KB |
Output is correct |
44 |
Correct |
9 ms |
3092 KB |
Output is correct |
45 |
Correct |
6 ms |
3092 KB |
Output is correct |
46 |
Correct |
6 ms |
3092 KB |
Output is correct |
47 |
Correct |
5 ms |
1384 KB |
Output is correct |
48 |
Correct |
7 ms |
2452 KB |
Output is correct |
49 |
Correct |
6 ms |
2456 KB |
Output is correct |
50 |
Correct |
5 ms |
2456 KB |
Output is correct |
51 |
Correct |
5 ms |
1944 KB |
Output is correct |
52 |
Correct |
5 ms |
1944 KB |
Output is correct |
53 |
Correct |
6 ms |
3012 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 |
0 ms |
348 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 |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 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 |
1 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 |
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 |
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 |
1 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 |
1 ms |
860 KB |
Output is correct |
41 |
Correct |
3 ms |
860 KB |
Output is correct |
42 |
Correct |
7 ms |
2968 KB |
Output is correct |
43 |
Correct |
6 ms |
2964 KB |
Output is correct |
44 |
Correct |
9 ms |
3092 KB |
Output is correct |
45 |
Correct |
6 ms |
3092 KB |
Output is correct |
46 |
Correct |
6 ms |
3092 KB |
Output is correct |
47 |
Correct |
5 ms |
1384 KB |
Output is correct |
48 |
Correct |
7 ms |
2452 KB |
Output is correct |
49 |
Correct |
6 ms |
2456 KB |
Output is correct |
50 |
Correct |
5 ms |
2456 KB |
Output is correct |
51 |
Correct |
5 ms |
1944 KB |
Output is correct |
52 |
Correct |
5 ms |
1944 KB |
Output is correct |
53 |
Correct |
6 ms |
3012 KB |
Output is correct |
54 |
Correct |
38 ms |
13316 KB |
Output is correct |
55 |
Correct |
179 ms |
13788 KB |
Output is correct |
56 |
Correct |
175 ms |
65976 KB |
Output is correct |
57 |
Correct |
162 ms |
66500 KB |
Output is correct |
58 |
Correct |
167 ms |
66536 KB |
Output is correct |
59 |
Correct |
160 ms |
66788 KB |
Output is correct |
60 |
Correct |
164 ms |
66796 KB |
Output is correct |
61 |
Correct |
159 ms |
37716 KB |
Output is correct |
62 |
Correct |
124 ms |
14656 KB |
Output is correct |
63 |
Correct |
149 ms |
53480 KB |
Output is correct |
64 |
Correct |
153 ms |
53348 KB |
Output is correct |
65 |
Correct |
172 ms |
39160 KB |
Output is correct |
66 |
Correct |
151 ms |
39272 KB |
Output is correct |
67 |
Correct |
159 ms |
66792 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 |
0 ms |
348 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 |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
13 |
Correct |
766 ms |
44536 KB |
Output is correct |
14 |
Correct |
950 ms |
51280 KB |
Output is correct |
15 |
Correct |
1137 ms |
60496 KB |
Output is correct |
16 |
Correct |
1328 ms |
71248 KB |
Output is correct |
17 |
Correct |
1603 ms |
83792 KB |
Output is correct |
18 |
Correct |
1558 ms |
83796 KB |
Output is correct |
19 |
Correct |
1588 ms |
84040 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 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 |
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 |
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 |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 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 |
0 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 |
1 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
860 KB |
Output is correct |
58 |
Correct |
3 ms |
860 KB |
Output is correct |
59 |
Correct |
7 ms |
2968 KB |
Output is correct |
60 |
Correct |
6 ms |
2964 KB |
Output is correct |
61 |
Correct |
9 ms |
3092 KB |
Output is correct |
62 |
Correct |
6 ms |
3092 KB |
Output is correct |
63 |
Correct |
6 ms |
3092 KB |
Output is correct |
64 |
Correct |
5 ms |
1384 KB |
Output is correct |
65 |
Correct |
7 ms |
2452 KB |
Output is correct |
66 |
Correct |
6 ms |
2456 KB |
Output is correct |
67 |
Correct |
5 ms |
2456 KB |
Output is correct |
68 |
Correct |
5 ms |
1944 KB |
Output is correct |
69 |
Correct |
5 ms |
1944 KB |
Output is correct |
70 |
Correct |
6 ms |
3012 KB |
Output is correct |
71 |
Correct |
38 ms |
13316 KB |
Output is correct |
72 |
Correct |
179 ms |
13788 KB |
Output is correct |
73 |
Correct |
175 ms |
65976 KB |
Output is correct |
74 |
Correct |
162 ms |
66500 KB |
Output is correct |
75 |
Correct |
167 ms |
66536 KB |
Output is correct |
76 |
Correct |
160 ms |
66788 KB |
Output is correct |
77 |
Correct |
164 ms |
66796 KB |
Output is correct |
78 |
Correct |
159 ms |
37716 KB |
Output is correct |
79 |
Correct |
124 ms |
14656 KB |
Output is correct |
80 |
Correct |
149 ms |
53480 KB |
Output is correct |
81 |
Correct |
153 ms |
53348 KB |
Output is correct |
82 |
Correct |
172 ms |
39160 KB |
Output is correct |
83 |
Correct |
151 ms |
39272 KB |
Output is correct |
84 |
Correct |
159 ms |
66792 KB |
Output is correct |
85 |
Correct |
480 ms |
167888 KB |
Output is correct |
86 |
Correct |
1188 ms |
413784 KB |
Output is correct |
87 |
Correct |
1124 ms |
414816 KB |
Output is correct |
88 |
Correct |
1204 ms |
415512 KB |
Output is correct |
89 |
Correct |
1050 ms |
415784 KB |
Output is correct |
90 |
Correct |
1157 ms |
415940 KB |
Output is correct |
91 |
Correct |
760 ms |
107068 KB |
Output is correct |
92 |
Correct |
806 ms |
90372 KB |
Output is correct |
93 |
Correct |
973 ms |
344756 KB |
Output is correct |
94 |
Correct |
1038 ms |
346796 KB |
Output is correct |
95 |
Correct |
967 ms |
241452 KB |
Output is correct |
96 |
Correct |
1049 ms |
241592 KB |
Output is correct |
97 |
Correct |
1146 ms |
415920 KB |
Output is correct |