#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1LL << 60;
int dp[2501][2501];
int chmin(int& x, int y){
return x = min(x, y);
}
const int a1 = 32913, m1 = 1e9 + 7, a2 = 5289, m2 = 998244353;
int pw1[2501], h1[2501], pw2[2501], h2[2501];
int ha(int l, int len){
return ((((h1[l + len] - h1[l] * pw1[len]) % m1 + m1) % m1) << 32) + (((h2[l + len] - h2[l] * pw2[len]) % m2 + m2) % m2);
}
int H[2501][2501];
int32_t main(){
int n; string s; cin >> n >> s;
int A, B, C; cin >> A >> B >> C;
pw1[0] = pw2[0] = 1;
for(int i = 0; i < n; i++)
pw1[i + 1] = pw1[i] * a1 % m1, h1[i + 1] = (h1[i] * a1 + s[i]) % m1,
pw2[i + 1] = pw2[i] * a2 % m2, h2[i + 1] = (h2[i] * a2 + s[i]) % m2;
for(int l = 0; l < n; l++){
for(int r = l; r < n; r++){
H[l][r] = ha(l, r - l + 1);
}
}
for(int sz = 2; sz <= n; sz++){
for(int l = 0; l <= n - sz; l++){
dp[l][l + sz - 1] = INF;
}
}
for(int sz = 1; sz <= n; sz++){
if(sz != 1){
for(int l = 0; l <= n - sz; l++){
int r = l + sz - 1;
dp[l][r] = min(dp[l][r], min(dp[l][r - 1], dp[l + 1][r]) + A);
}
}else{
for(int l = 0; l < n; l++) dp[l][l] = A;
}
unordered_map<int, vector<int>> M;
for(int l = 0; l <= n - sz; l++){
M[ha(l, sz)].push_back(l);
}
for(auto& [h, v] : M){
int k = v.size();
for(int l = 0; l < k; l++){
int _acc = 0, _max = -1;
for(int r = l; r < k; r++){
if(v[r] > _max){
_acc++; _max = v[r] + sz - 1;
dp[v[l]][_max] = min(dp[v[l]][_max], dp[v[l]][v[l] + sz - 1] + (_max - v[l] + 1 - _acc * sz) * A + B + _acc * C);
}
}
}
}
}
cout << dp[0][n - 1] << endl;
}
Compilation message
copypaste3.cpp: In function 'int32_t main()':
copypaste3.cpp:55:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
55 | for(auto& [h, v] : M){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
980 ms |
41304 KB |
Output is correct |
4 |
Correct |
1223 ms |
46864 KB |
Output is correct |
5 |
Correct |
1545 ms |
53204 KB |
Output is correct |
6 |
Correct |
1975 ms |
59972 KB |
Output is correct |
7 |
Correct |
2434 ms |
67404 KB |
Output is correct |
8 |
Correct |
2486 ms |
67368 KB |
Output is correct |
9 |
Execution timed out |
3061 ms |
67372 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 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 |
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 |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 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 |
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 |
0 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 |
0 ms |
212 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 |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 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 |
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 |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 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 |
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 |
0 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 |
0 ms |
212 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 |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
2 ms |
980 KB |
Output is correct |
41 |
Correct |
5 ms |
2260 KB |
Output is correct |
42 |
Correct |
4 ms |
2260 KB |
Output is correct |
43 |
Correct |
18 ms |
2280 KB |
Output is correct |
44 |
Correct |
6 ms |
2260 KB |
Output is correct |
45 |
Correct |
6 ms |
2260 KB |
Output is correct |
46 |
Correct |
5 ms |
2260 KB |
Output is correct |
47 |
Correct |
3 ms |
2260 KB |
Output is correct |
48 |
Correct |
4 ms |
2264 KB |
Output is correct |
49 |
Correct |
4 ms |
2260 KB |
Output is correct |
50 |
Correct |
4 ms |
2228 KB |
Output is correct |
51 |
Correct |
3 ms |
2260 KB |
Output is correct |
52 |
Correct |
3 ms |
2260 KB |
Output is correct |
53 |
Correct |
5 ms |
2260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 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 |
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 |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 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 |
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 |
0 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 |
0 ms |
212 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 |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
2 ms |
980 KB |
Output is correct |
41 |
Correct |
5 ms |
2260 KB |
Output is correct |
42 |
Correct |
4 ms |
2260 KB |
Output is correct |
43 |
Correct |
18 ms |
2280 KB |
Output is correct |
44 |
Correct |
6 ms |
2260 KB |
Output is correct |
45 |
Correct |
6 ms |
2260 KB |
Output is correct |
46 |
Correct |
5 ms |
2260 KB |
Output is correct |
47 |
Correct |
3 ms |
2260 KB |
Output is correct |
48 |
Correct |
4 ms |
2264 KB |
Output is correct |
49 |
Correct |
4 ms |
2260 KB |
Output is correct |
50 |
Correct |
4 ms |
2228 KB |
Output is correct |
51 |
Correct |
3 ms |
2260 KB |
Output is correct |
52 |
Correct |
3 ms |
2260 KB |
Output is correct |
53 |
Correct |
5 ms |
2260 KB |
Output is correct |
54 |
Correct |
23 ms |
5460 KB |
Output is correct |
55 |
Correct |
232 ms |
16220 KB |
Output is correct |
56 |
Correct |
119 ms |
16320 KB |
Output is correct |
57 |
Correct |
124 ms |
16320 KB |
Output is correct |
58 |
Correct |
116 ms |
16336 KB |
Output is correct |
59 |
Correct |
107 ms |
16332 KB |
Output is correct |
60 |
Correct |
104 ms |
16316 KB |
Output is correct |
61 |
Correct |
80 ms |
16264 KB |
Output is correct |
62 |
Correct |
105 ms |
16236 KB |
Output is correct |
63 |
Correct |
106 ms |
16284 KB |
Output is correct |
64 |
Correct |
128 ms |
16280 KB |
Output is correct |
65 |
Correct |
76 ms |
16264 KB |
Output is correct |
66 |
Correct |
91 ms |
16264 KB |
Output is correct |
67 |
Correct |
99 ms |
16324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 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 |
980 ms |
41304 KB |
Output is correct |
14 |
Correct |
1223 ms |
46864 KB |
Output is correct |
15 |
Correct |
1545 ms |
53204 KB |
Output is correct |
16 |
Correct |
1975 ms |
59972 KB |
Output is correct |
17 |
Correct |
2434 ms |
67404 KB |
Output is correct |
18 |
Correct |
2486 ms |
67368 KB |
Output is correct |
19 |
Execution timed out |
3061 ms |
67372 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |