#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;
const ll base = 29, mod = 1234567891;
int N;
char c[2505];
ll A, B, C, dp[2505][2505], h[2505][2505], nxt[2505][2505];
signed main()
{
cin >> N;
for(int i = 1; i <= N; i++)
cin >> c[i];
cin >> A >> B >> C;
for(int i = 1; i <= N; i++)
for(int j = i; j <= N; j++)
{
dp[i][j] = A * (j - i + 1);
h[i][j] = (h[i][j - 1] * base + c[j] - 'a') % mod;
}
for(int l = 1; l <= N; l++)
{
map<int, int> mp;
for(int i = N - l + 1; i >= 1; i--)
if(i + 2 * l - 1 <= N)
{
mp[h[i + l][i + 2 * l - 1]] = i + l;
nxt[i][i + l - 1] = mp[h[i][i + l - 1]];
// cout << "[" << i << ", " << i + l - 1 << "] -> [" << nxt[i][i + l - 1] << "]\n";
}
}
for(int i = N; i >= 1; i--)
for(int k = i; k <= N; k++)
{
dp[i][k] = min({dp[i][k], dp[i + 1][k] + A, dp[i][k - 1] + A});
ll base = dp[i][k] + B, j = nxt[i][k], t = 2;
for(; j != 0; j = nxt[j][j + k - i])
{
dp[i][j + k - i] = min(dp[i][j + k - i], base + t * C + (j + k - i - i + 1 - t * (k - i + 1)) * A);
t++;
}
}
/* for(int i = 1; i <= N; i++)
for(int j = 1; j <= N; j++)
if(j < i) cout << "- ";
else cout << dp[i][j] << " \n"[N == j];
*/
cout << dp[1][N] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 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 |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 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 |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
206 ms |
55052 KB |
Output is correct |
4 |
Correct |
268 ms |
62264 KB |
Output is correct |
5 |
Correct |
304 ms |
70600 KB |
Output is correct |
6 |
Correct |
396 ms |
79648 KB |
Output is correct |
7 |
Correct |
464 ms |
89548 KB |
Output is correct |
8 |
Correct |
488 ms |
89544 KB |
Output is correct |
9 |
Correct |
459 ms |
89540 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 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 |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
696 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 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 |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
592 KB |
Output is correct |
13 |
Correct |
0 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
0 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
596 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 |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 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 |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
592 KB |
Output is correct |
13 |
Correct |
0 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
0 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
596 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 |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
596 KB |
Output is correct |
40 |
Correct |
1 ms |
1364 KB |
Output is correct |
41 |
Correct |
2 ms |
3028 KB |
Output is correct |
42 |
Correct |
5 ms |
3028 KB |
Output is correct |
43 |
Correct |
4 ms |
3028 KB |
Output is correct |
44 |
Correct |
4 ms |
3028 KB |
Output is correct |
45 |
Correct |
4 ms |
3028 KB |
Output is correct |
46 |
Correct |
5 ms |
3064 KB |
Output is correct |
47 |
Correct |
3 ms |
3028 KB |
Output is correct |
48 |
Correct |
3 ms |
3028 KB |
Output is correct |
49 |
Correct |
3 ms |
3028 KB |
Output is correct |
50 |
Correct |
3 ms |
3028 KB |
Output is correct |
51 |
Correct |
3 ms |
3028 KB |
Output is correct |
52 |
Correct |
3 ms |
3028 KB |
Output is correct |
53 |
Correct |
4 ms |
3028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 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 |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
592 KB |
Output is correct |
13 |
Correct |
0 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
0 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
596 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 |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
596 KB |
Output is correct |
40 |
Correct |
1 ms |
1364 KB |
Output is correct |
41 |
Correct |
2 ms |
3028 KB |
Output is correct |
42 |
Correct |
5 ms |
3028 KB |
Output is correct |
43 |
Correct |
4 ms |
3028 KB |
Output is correct |
44 |
Correct |
4 ms |
3028 KB |
Output is correct |
45 |
Correct |
4 ms |
3028 KB |
Output is correct |
46 |
Correct |
5 ms |
3064 KB |
Output is correct |
47 |
Correct |
3 ms |
3028 KB |
Output is correct |
48 |
Correct |
3 ms |
3028 KB |
Output is correct |
49 |
Correct |
3 ms |
3028 KB |
Output is correct |
50 |
Correct |
3 ms |
3028 KB |
Output is correct |
51 |
Correct |
3 ms |
3028 KB |
Output is correct |
52 |
Correct |
3 ms |
3028 KB |
Output is correct |
53 |
Correct |
4 ms |
3028 KB |
Output is correct |
54 |
Correct |
15 ms |
7644 KB |
Output is correct |
55 |
Correct |
48 ms |
22096 KB |
Output is correct |
56 |
Correct |
74 ms |
22160 KB |
Output is correct |
57 |
Correct |
73 ms |
22184 KB |
Output is correct |
58 |
Correct |
73 ms |
22088 KB |
Output is correct |
59 |
Correct |
74 ms |
22192 KB |
Output is correct |
60 |
Correct |
78 ms |
22112 KB |
Output is correct |
61 |
Correct |
56 ms |
22096 KB |
Output is correct |
62 |
Correct |
34 ms |
22100 KB |
Output is correct |
63 |
Correct |
61 ms |
22180 KB |
Output is correct |
64 |
Correct |
62 ms |
22112 KB |
Output is correct |
65 |
Correct |
53 ms |
22284 KB |
Output is correct |
66 |
Correct |
50 ms |
22164 KB |
Output is correct |
67 |
Correct |
72 ms |
22096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 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 |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 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 |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
206 ms |
55052 KB |
Output is correct |
14 |
Correct |
268 ms |
62264 KB |
Output is correct |
15 |
Correct |
304 ms |
70600 KB |
Output is correct |
16 |
Correct |
396 ms |
79648 KB |
Output is correct |
17 |
Correct |
464 ms |
89548 KB |
Output is correct |
18 |
Correct |
488 ms |
89544 KB |
Output is correct |
19 |
Correct |
459 ms |
89540 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 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 |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
696 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 |
592 KB |
Output is correct |
30 |
Correct |
0 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
468 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
596 KB |
Output is correct |
39 |
Correct |
0 ms |
596 KB |
Output is correct |
40 |
Correct |
1 ms |
596 KB |
Output is correct |
41 |
Correct |
1 ms |
596 KB |
Output is correct |
42 |
Correct |
1 ms |
596 KB |
Output is correct |
43 |
Correct |
1 ms |
596 KB |
Output is correct |
44 |
Correct |
1 ms |
596 KB |
Output is correct |
45 |
Correct |
1 ms |
724 KB |
Output is correct |
46 |
Correct |
1 ms |
596 KB |
Output is correct |
47 |
Correct |
1 ms |
596 KB |
Output is correct |
48 |
Correct |
1 ms |
596 KB |
Output is correct |
49 |
Correct |
1 ms |
596 KB |
Output is correct |
50 |
Correct |
1 ms |
596 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 |
468 KB |
Output is correct |
56 |
Correct |
1 ms |
596 KB |
Output is correct |
57 |
Correct |
1 ms |
1364 KB |
Output is correct |
58 |
Correct |
2 ms |
3028 KB |
Output is correct |
59 |
Correct |
5 ms |
3028 KB |
Output is correct |
60 |
Correct |
4 ms |
3028 KB |
Output is correct |
61 |
Correct |
4 ms |
3028 KB |
Output is correct |
62 |
Correct |
4 ms |
3028 KB |
Output is correct |
63 |
Correct |
5 ms |
3064 KB |
Output is correct |
64 |
Correct |
3 ms |
3028 KB |
Output is correct |
65 |
Correct |
3 ms |
3028 KB |
Output is correct |
66 |
Correct |
3 ms |
3028 KB |
Output is correct |
67 |
Correct |
3 ms |
3028 KB |
Output is correct |
68 |
Correct |
3 ms |
3028 KB |
Output is correct |
69 |
Correct |
3 ms |
3028 KB |
Output is correct |
70 |
Correct |
4 ms |
3028 KB |
Output is correct |
71 |
Correct |
15 ms |
7644 KB |
Output is correct |
72 |
Correct |
48 ms |
22096 KB |
Output is correct |
73 |
Correct |
74 ms |
22160 KB |
Output is correct |
74 |
Correct |
73 ms |
22184 KB |
Output is correct |
75 |
Correct |
73 ms |
22088 KB |
Output is correct |
76 |
Correct |
74 ms |
22192 KB |
Output is correct |
77 |
Correct |
78 ms |
22112 KB |
Output is correct |
78 |
Correct |
56 ms |
22096 KB |
Output is correct |
79 |
Correct |
34 ms |
22100 KB |
Output is correct |
80 |
Correct |
61 ms |
22180 KB |
Output is correct |
81 |
Correct |
62 ms |
22112 KB |
Output is correct |
82 |
Correct |
53 ms |
22284 KB |
Output is correct |
83 |
Correct |
50 ms |
22164 KB |
Output is correct |
84 |
Correct |
72 ms |
22096 KB |
Output is correct |
85 |
Correct |
196 ms |
43768 KB |
Output is correct |
86 |
Correct |
507 ms |
89676 KB |
Output is correct |
87 |
Correct |
509 ms |
89684 KB |
Output is correct |
88 |
Correct |
502 ms |
89612 KB |
Output is correct |
89 |
Correct |
523 ms |
89656 KB |
Output is correct |
90 |
Incorrect |
502 ms |
89576 KB |
Output isn't correct |
91 |
Halted |
0 ms |
0 KB |
- |