#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
#define f first
#define s second
#define LOGN 21
const ll MAXN = 2550;
const ll P = 37;
const ll P2 = 31;
const ll MOD1 = 1e9 + 9;
const ll MOD2 = 998244353;
ll N, A, B, C;
string s;
ll hashes1[MAXN], hashes2[MAXN], to[MAXN][MAXN], dp[MAXN][MAXN], powP1[MAXN], powP2[MAXN];
pair<ll, ll> f(int l, int r) {
ll h = (hashes1[r] - powP1[r-l+1] * hashes1[l - 1] % MOD1) % MOD1;
ll h2 = (hashes2[r] - powP2[r-l+1] * hashes2[l - 1] % MOD2) % MOD2;
if (h < 0)
h += MOD1;
if (h2 < 0)
h2 += MOD2;
return {h, h2};
}
vector<int> occ[MAXN];
map<pair<ll,ll>, deque<int>> mp;
int main() {
fast
cin >> N >> s >> A >> B >> C;
s = "#" + s;
powP1[0] = powP2[0] = 1;
for (int i = 1; i <= N; i++)
powP1[i] = (powP1[i-1] * P) % MOD1;
for (int i = 1; i <= N; i++)
powP2[i] = (powP2[i-1] * P2) % MOD2;
for (int i = 1; i <= N; i++)
hashes1[i] = (hashes1[i-1] * P + (s[i] - 'a' + 1)) % MOD1;
for (int i = 1; i <= N; i++)
hashes2[i] = (hashes2[i-1] * P2 + (s[i] - 'a' + 1)) % MOD2;
for (int len = 1; len <= N; len++) {
mp.clear();
for (int j = N-len+1; j >= 1; j--) {
pair<ll, ll> h = f(j, j + len - 1);
while (mp[h].size() >= 2 && mp[h][1] > j + len - 1)
mp[h].pop_front();
to[j][len] = (mp[h].size() == 0 ? 0 : mp[h].front());
if (to[j][len] <= j + len - 1)
to[j][len] = 0;
if (to[j][len])
occ[j].push_back(len);
mp[h].push_back(j);
}
}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++)
dp[i][j] = 1e14;
}
for (int l = N; l >= 1; l--) {
dp[l][l] = A;
for (int r = l+1; r <= N; r++)
dp[l][r] = min(dp[l][r], min(dp[l+1][r] + A, dp[l][r-1] + A));
for (auto len : occ[l]) {
int cnt = 1;
int now = to[l][len];
dp[l][l+len-1] = min(dp[l][l+len-1], min(dp[l][l+len-2] + A, dp[l+1][l+len-1] + A));
ll base = dp[l][l+len-1];
while (now != 0) {
int total = now - l - cnt * len;
dp[l][now + len - 1] = min(dp[l][now + len - 1], total * A + B + C * (cnt + 1) + base);
cnt++;
now = to[now][len];
}
}
for (int r = l+1; r <= N; r++)
dp[l][r] = min(dp[l][r], min(dp[l+1][r] + A, dp[l][r-1] + A));
}
cout << dp[1][N] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2648 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
171 ms |
81096 KB |
Output is correct |
4 |
Correct |
199 ms |
89936 KB |
Output is correct |
5 |
Correct |
238 ms |
95056 KB |
Output is correct |
6 |
Correct |
285 ms |
104764 KB |
Output is correct |
7 |
Correct |
332 ms |
110264 KB |
Output is correct |
8 |
Correct |
338 ms |
110256 KB |
Output is correct |
9 |
Correct |
339 ms |
110420 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
4636 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
6688 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2648 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
4696 KB |
Output is correct |
12 |
Correct |
1 ms |
4440 KB |
Output is correct |
13 |
Correct |
1 ms |
4440 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
6744 KB |
Output is correct |
22 |
Correct |
2 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
2 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
1 ms |
6492 KB |
Output is correct |
32 |
Correct |
1 ms |
6492 KB |
Output is correct |
33 |
Correct |
1 ms |
6492 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
4444 KB |
Output is correct |
37 |
Correct |
1 ms |
4444 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2648 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
4696 KB |
Output is correct |
12 |
Correct |
1 ms |
4440 KB |
Output is correct |
13 |
Correct |
1 ms |
4440 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
6744 KB |
Output is correct |
22 |
Correct |
2 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
2 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
1 ms |
6492 KB |
Output is correct |
32 |
Correct |
1 ms |
6492 KB |
Output is correct |
33 |
Correct |
1 ms |
6492 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
4444 KB |
Output is correct |
37 |
Correct |
1 ms |
4444 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
1 ms |
6492 KB |
Output is correct |
40 |
Correct |
2 ms |
6744 KB |
Output is correct |
41 |
Correct |
3 ms |
10844 KB |
Output is correct |
42 |
Correct |
7 ms |
10844 KB |
Output is correct |
43 |
Correct |
7 ms |
10948 KB |
Output is correct |
44 |
Correct |
7 ms |
10844 KB |
Output is correct |
45 |
Correct |
7 ms |
10844 KB |
Output is correct |
46 |
Correct |
7 ms |
11096 KB |
Output is correct |
47 |
Correct |
4 ms |
10844 KB |
Output is correct |
48 |
Correct |
6 ms |
10844 KB |
Output is correct |
49 |
Correct |
8 ms |
10844 KB |
Output is correct |
50 |
Correct |
6 ms |
10920 KB |
Output is correct |
51 |
Correct |
6 ms |
10844 KB |
Output is correct |
52 |
Correct |
6 ms |
10916 KB |
Output is correct |
53 |
Correct |
7 ms |
10844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2648 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
4696 KB |
Output is correct |
12 |
Correct |
1 ms |
4440 KB |
Output is correct |
13 |
Correct |
1 ms |
4440 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
6744 KB |
Output is correct |
22 |
Correct |
2 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
2 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
1 ms |
6492 KB |
Output is correct |
32 |
Correct |
1 ms |
6492 KB |
Output is correct |
33 |
Correct |
1 ms |
6492 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
4444 KB |
Output is correct |
37 |
Correct |
1 ms |
4444 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
1 ms |
6492 KB |
Output is correct |
40 |
Correct |
2 ms |
6744 KB |
Output is correct |
41 |
Correct |
3 ms |
10844 KB |
Output is correct |
42 |
Correct |
7 ms |
10844 KB |
Output is correct |
43 |
Correct |
7 ms |
10948 KB |
Output is correct |
44 |
Correct |
7 ms |
10844 KB |
Output is correct |
45 |
Correct |
7 ms |
10844 KB |
Output is correct |
46 |
Correct |
7 ms |
11096 KB |
Output is correct |
47 |
Correct |
4 ms |
10844 KB |
Output is correct |
48 |
Correct |
6 ms |
10844 KB |
Output is correct |
49 |
Correct |
8 ms |
10844 KB |
Output is correct |
50 |
Correct |
6 ms |
10920 KB |
Output is correct |
51 |
Correct |
6 ms |
10844 KB |
Output is correct |
52 |
Correct |
6 ms |
10916 KB |
Output is correct |
53 |
Correct |
7 ms |
10844 KB |
Output is correct |
54 |
Correct |
32 ms |
23380 KB |
Output is correct |
55 |
Correct |
51 ms |
45044 KB |
Output is correct |
56 |
Correct |
173 ms |
44404 KB |
Output is correct |
57 |
Correct |
174 ms |
44380 KB |
Output is correct |
58 |
Correct |
175 ms |
44368 KB |
Output is correct |
59 |
Correct |
173 ms |
44368 KB |
Output is correct |
60 |
Correct |
171 ms |
44380 KB |
Output is correct |
61 |
Correct |
139 ms |
44628 KB |
Output is correct |
62 |
Correct |
41 ms |
44884 KB |
Output is correct |
63 |
Correct |
167 ms |
44844 KB |
Output is correct |
64 |
Correct |
158 ms |
44748 KB |
Output is correct |
65 |
Correct |
137 ms |
45136 KB |
Output is correct |
66 |
Correct |
137 ms |
45060 KB |
Output is correct |
67 |
Correct |
173 ms |
44368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2648 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
171 ms |
81096 KB |
Output is correct |
14 |
Correct |
199 ms |
89936 KB |
Output is correct |
15 |
Correct |
238 ms |
95056 KB |
Output is correct |
16 |
Correct |
285 ms |
104764 KB |
Output is correct |
17 |
Correct |
332 ms |
110264 KB |
Output is correct |
18 |
Correct |
338 ms |
110256 KB |
Output is correct |
19 |
Correct |
339 ms |
110420 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
0 ms |
2396 KB |
Output is correct |
22 |
Correct |
0 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
4636 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
6688 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
4696 KB |
Output is correct |
29 |
Correct |
1 ms |
4440 KB |
Output is correct |
30 |
Correct |
1 ms |
4440 KB |
Output is correct |
31 |
Correct |
1 ms |
4444 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 KB |
Output is correct |
33 |
Correct |
1 ms |
4444 KB |
Output is correct |
34 |
Correct |
1 ms |
4444 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
4444 KB |
Output is correct |
37 |
Correct |
1 ms |
4444 KB |
Output is correct |
38 |
Correct |
1 ms |
6744 KB |
Output is correct |
39 |
Correct |
2 ms |
6492 KB |
Output is correct |
40 |
Correct |
1 ms |
6492 KB |
Output is correct |
41 |
Correct |
1 ms |
6492 KB |
Output is correct |
42 |
Correct |
1 ms |
6492 KB |
Output is correct |
43 |
Correct |
1 ms |
6492 KB |
Output is correct |
44 |
Correct |
2 ms |
6492 KB |
Output is correct |
45 |
Correct |
1 ms |
6492 KB |
Output is correct |
46 |
Correct |
1 ms |
6492 KB |
Output is correct |
47 |
Correct |
1 ms |
6492 KB |
Output is correct |
48 |
Correct |
1 ms |
6492 KB |
Output is correct |
49 |
Correct |
1 ms |
6492 KB |
Output is correct |
50 |
Correct |
1 ms |
6492 KB |
Output is correct |
51 |
Correct |
1 ms |
2396 KB |
Output is correct |
52 |
Correct |
1 ms |
2396 KB |
Output is correct |
53 |
Correct |
1 ms |
4444 KB |
Output is correct |
54 |
Correct |
1 ms |
4444 KB |
Output is correct |
55 |
Correct |
1 ms |
4444 KB |
Output is correct |
56 |
Correct |
1 ms |
6492 KB |
Output is correct |
57 |
Correct |
2 ms |
6744 KB |
Output is correct |
58 |
Correct |
3 ms |
10844 KB |
Output is correct |
59 |
Correct |
7 ms |
10844 KB |
Output is correct |
60 |
Correct |
7 ms |
10948 KB |
Output is correct |
61 |
Correct |
7 ms |
10844 KB |
Output is correct |
62 |
Correct |
7 ms |
10844 KB |
Output is correct |
63 |
Correct |
7 ms |
11096 KB |
Output is correct |
64 |
Correct |
4 ms |
10844 KB |
Output is correct |
65 |
Correct |
6 ms |
10844 KB |
Output is correct |
66 |
Correct |
8 ms |
10844 KB |
Output is correct |
67 |
Correct |
6 ms |
10920 KB |
Output is correct |
68 |
Correct |
6 ms |
10844 KB |
Output is correct |
69 |
Correct |
6 ms |
10916 KB |
Output is correct |
70 |
Correct |
7 ms |
10844 KB |
Output is correct |
71 |
Correct |
32 ms |
23380 KB |
Output is correct |
72 |
Correct |
51 ms |
45044 KB |
Output is correct |
73 |
Correct |
173 ms |
44404 KB |
Output is correct |
74 |
Correct |
174 ms |
44380 KB |
Output is correct |
75 |
Correct |
175 ms |
44368 KB |
Output is correct |
76 |
Correct |
173 ms |
44368 KB |
Output is correct |
77 |
Correct |
171 ms |
44380 KB |
Output is correct |
78 |
Correct |
139 ms |
44628 KB |
Output is correct |
79 |
Correct |
41 ms |
44884 KB |
Output is correct |
80 |
Correct |
167 ms |
44844 KB |
Output is correct |
81 |
Correct |
158 ms |
44748 KB |
Output is correct |
82 |
Correct |
137 ms |
45136 KB |
Output is correct |
83 |
Correct |
137 ms |
45060 KB |
Output is correct |
84 |
Correct |
173 ms |
44368 KB |
Output is correct |
85 |
Correct |
453 ms |
69464 KB |
Output is correct |
86 |
Correct |
1235 ms |
103100 KB |
Output is correct |
87 |
Correct |
1301 ms |
103044 KB |
Output is correct |
88 |
Correct |
1273 ms |
103012 KB |
Output is correct |
89 |
Correct |
1230 ms |
103000 KB |
Output is correct |
90 |
Correct |
1224 ms |
102996 KB |
Output is correct |
91 |
Correct |
414 ms |
109880 KB |
Output is correct |
92 |
Correct |
295 ms |
110316 KB |
Output is correct |
93 |
Correct |
1123 ms |
107664 KB |
Output is correct |
94 |
Correct |
1154 ms |
106704 KB |
Output is correct |
95 |
Correct |
1025 ms |
108888 KB |
Output is correct |
96 |
Correct |
1008 ms |
108792 KB |
Output is correct |
97 |
Correct |
1237 ms |
102996 KB |
Output is correct |