#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;
const int N = 2550;
int pre[N][N];
vector<pii> wright[N][N];
ll dp[N][N];
ll A, B, C;
char s[N];
int n;
// TODO: use kmp, this is probably O(n^3) (but with glibc's blazingly fast mem* ans str*)
void make_pre()
{
memset(pre, -1, sizeof(pre));
Loop (r2,1,n+1) {
int len = 1;
LoopR (r1,len,r2) {
if (r2-r1 < len)
continue;
if (memcmp(&s[r2-len], &s[r1-len], len) == 0) {
pre[r2-len][r2] = r1-len;
++len;
++r1;
}
}
}
}
void make_wright()
{
Loop (l,0,n) Loop (r,l+1,n+1) {
int k = 1;
int p = l;
do {
wright[p][r].push_back({r-l, k});
p = pre[p][p+r-l];
++k;
} while (p != -1);
}
}
void calc_dp()
{
Loop (r,1,n+1) LoopR (l,0,r) {
dp[l][r] = min(dp[l][r-1], dp[l+1][r]) + A;
for (auto [k, cnt] : wright[l][r])
dp[l][r] = min(dp[l][r], dp[l][l+k] + B + C * cnt + A * (r-l - cnt*k));
}
}
int main()
{
cin.tie(0) -> sync_with_stdio(false);
cin >> n >> s >> A >> B >> C;
make_pre();
make_wright();
calc_dp();
cout << dp[0][n] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
81 ms |
178428 KB |
Output is correct |
2 |
Correct |
79 ms |
178368 KB |
Output is correct |
3 |
Correct |
81 ms |
178468 KB |
Output is correct |
4 |
Correct |
79 ms |
178380 KB |
Output is correct |
5 |
Correct |
78 ms |
178468 KB |
Output is correct |
6 |
Correct |
78 ms |
178432 KB |
Output is correct |
7 |
Correct |
81 ms |
178436 KB |
Output is correct |
8 |
Correct |
79 ms |
178476 KB |
Output is correct |
9 |
Correct |
79 ms |
178364 KB |
Output is correct |
10 |
Correct |
83 ms |
178476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
178508 KB |
Output is correct |
2 |
Correct |
82 ms |
178444 KB |
Output is correct |
3 |
Correct |
1550 ms |
342956 KB |
Output is correct |
4 |
Correct |
1945 ms |
371680 KB |
Output is correct |
5 |
Correct |
2336 ms |
405348 KB |
Output is correct |
6 |
Correct |
2814 ms |
444640 KB |
Output is correct |
7 |
Execution timed out |
3109 ms |
491060 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
81 ms |
178428 KB |
Output is correct |
2 |
Correct |
79 ms |
178368 KB |
Output is correct |
3 |
Correct |
81 ms |
178468 KB |
Output is correct |
4 |
Correct |
79 ms |
178380 KB |
Output is correct |
5 |
Correct |
78 ms |
178468 KB |
Output is correct |
6 |
Correct |
78 ms |
178432 KB |
Output is correct |
7 |
Correct |
81 ms |
178436 KB |
Output is correct |
8 |
Correct |
79 ms |
178476 KB |
Output is correct |
9 |
Correct |
79 ms |
178364 KB |
Output is correct |
10 |
Correct |
83 ms |
178476 KB |
Output is correct |
11 |
Correct |
82 ms |
178516 KB |
Output is correct |
12 |
Correct |
81 ms |
178436 KB |
Output is correct |
13 |
Correct |
82 ms |
178504 KB |
Output is correct |
14 |
Correct |
80 ms |
178468 KB |
Output is correct |
15 |
Correct |
81 ms |
178548 KB |
Output is correct |
16 |
Correct |
82 ms |
178448 KB |
Output is correct |
17 |
Correct |
82 ms |
178380 KB |
Output is correct |
18 |
Correct |
81 ms |
178448 KB |
Output is correct |
19 |
Correct |
81 ms |
178604 KB |
Output is correct |
20 |
Correct |
81 ms |
178428 KB |
Output is correct |
21 |
Correct |
81 ms |
178592 KB |
Output is correct |
22 |
Correct |
81 ms |
178580 KB |
Output is correct |
23 |
Correct |
82 ms |
178508 KB |
Output is correct |
24 |
Correct |
81 ms |
178556 KB |
Output is correct |
25 |
Correct |
81 ms |
178600 KB |
Output is correct |
26 |
Correct |
83 ms |
178612 KB |
Output is correct |
27 |
Correct |
82 ms |
178532 KB |
Output is correct |
28 |
Correct |
84 ms |
178548 KB |
Output is correct |
29 |
Correct |
82 ms |
178612 KB |
Output is correct |
30 |
Correct |
87 ms |
178516 KB |
Output is correct |
31 |
Correct |
82 ms |
178516 KB |
Output is correct |
32 |
Correct |
82 ms |
178608 KB |
Output is correct |
33 |
Correct |
82 ms |
178520 KB |
Output is correct |
34 |
Correct |
89 ms |
178468 KB |
Output is correct |
35 |
Correct |
81 ms |
178380 KB |
Output is correct |
36 |
Correct |
85 ms |
178400 KB |
Output is correct |
37 |
Correct |
81 ms |
178436 KB |
Output is correct |
38 |
Correct |
82 ms |
178508 KB |
Output is correct |
39 |
Correct |
81 ms |
178620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
81 ms |
178428 KB |
Output is correct |
2 |
Correct |
79 ms |
178368 KB |
Output is correct |
3 |
Correct |
81 ms |
178468 KB |
Output is correct |
4 |
Correct |
79 ms |
178380 KB |
Output is correct |
5 |
Correct |
78 ms |
178468 KB |
Output is correct |
6 |
Correct |
78 ms |
178432 KB |
Output is correct |
7 |
Correct |
81 ms |
178436 KB |
Output is correct |
8 |
Correct |
79 ms |
178476 KB |
Output is correct |
9 |
Correct |
79 ms |
178364 KB |
Output is correct |
10 |
Correct |
83 ms |
178476 KB |
Output is correct |
11 |
Correct |
82 ms |
178516 KB |
Output is correct |
12 |
Correct |
81 ms |
178436 KB |
Output is correct |
13 |
Correct |
82 ms |
178504 KB |
Output is correct |
14 |
Correct |
80 ms |
178468 KB |
Output is correct |
15 |
Correct |
81 ms |
178548 KB |
Output is correct |
16 |
Correct |
82 ms |
178448 KB |
Output is correct |
17 |
Correct |
82 ms |
178380 KB |
Output is correct |
18 |
Correct |
81 ms |
178448 KB |
Output is correct |
19 |
Correct |
81 ms |
178604 KB |
Output is correct |
20 |
Correct |
81 ms |
178428 KB |
Output is correct |
21 |
Correct |
81 ms |
178592 KB |
Output is correct |
22 |
Correct |
81 ms |
178580 KB |
Output is correct |
23 |
Correct |
82 ms |
178508 KB |
Output is correct |
24 |
Correct |
81 ms |
178556 KB |
Output is correct |
25 |
Correct |
81 ms |
178600 KB |
Output is correct |
26 |
Correct |
83 ms |
178612 KB |
Output is correct |
27 |
Correct |
82 ms |
178532 KB |
Output is correct |
28 |
Correct |
84 ms |
178548 KB |
Output is correct |
29 |
Correct |
82 ms |
178612 KB |
Output is correct |
30 |
Correct |
87 ms |
178516 KB |
Output is correct |
31 |
Correct |
82 ms |
178516 KB |
Output is correct |
32 |
Correct |
82 ms |
178608 KB |
Output is correct |
33 |
Correct |
82 ms |
178520 KB |
Output is correct |
34 |
Correct |
89 ms |
178468 KB |
Output is correct |
35 |
Correct |
81 ms |
178380 KB |
Output is correct |
36 |
Correct |
85 ms |
178400 KB |
Output is correct |
37 |
Correct |
81 ms |
178436 KB |
Output is correct |
38 |
Correct |
82 ms |
178508 KB |
Output is correct |
39 |
Correct |
81 ms |
178620 KB |
Output is correct |
40 |
Correct |
81 ms |
178968 KB |
Output is correct |
41 |
Correct |
87 ms |
180716 KB |
Output is correct |
42 |
Correct |
84 ms |
180048 KB |
Output is correct |
43 |
Correct |
83 ms |
179996 KB |
Output is correct |
44 |
Correct |
94 ms |
180080 KB |
Output is correct |
45 |
Correct |
83 ms |
179968 KB |
Output is correct |
46 |
Correct |
83 ms |
180052 KB |
Output is correct |
47 |
Correct |
84 ms |
180092 KB |
Output is correct |
48 |
Correct |
83 ms |
180032 KB |
Output is correct |
49 |
Correct |
84 ms |
180100 KB |
Output is correct |
50 |
Correct |
85 ms |
179996 KB |
Output is correct |
51 |
Correct |
86 ms |
180404 KB |
Output is correct |
52 |
Correct |
85 ms |
180256 KB |
Output is correct |
53 |
Correct |
84 ms |
180084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
81 ms |
178428 KB |
Output is correct |
2 |
Correct |
79 ms |
178368 KB |
Output is correct |
3 |
Correct |
81 ms |
178468 KB |
Output is correct |
4 |
Correct |
79 ms |
178380 KB |
Output is correct |
5 |
Correct |
78 ms |
178468 KB |
Output is correct |
6 |
Correct |
78 ms |
178432 KB |
Output is correct |
7 |
Correct |
81 ms |
178436 KB |
Output is correct |
8 |
Correct |
79 ms |
178476 KB |
Output is correct |
9 |
Correct |
79 ms |
178364 KB |
Output is correct |
10 |
Correct |
83 ms |
178476 KB |
Output is correct |
11 |
Correct |
82 ms |
178516 KB |
Output is correct |
12 |
Correct |
81 ms |
178436 KB |
Output is correct |
13 |
Correct |
82 ms |
178504 KB |
Output is correct |
14 |
Correct |
80 ms |
178468 KB |
Output is correct |
15 |
Correct |
81 ms |
178548 KB |
Output is correct |
16 |
Correct |
82 ms |
178448 KB |
Output is correct |
17 |
Correct |
82 ms |
178380 KB |
Output is correct |
18 |
Correct |
81 ms |
178448 KB |
Output is correct |
19 |
Correct |
81 ms |
178604 KB |
Output is correct |
20 |
Correct |
81 ms |
178428 KB |
Output is correct |
21 |
Correct |
81 ms |
178592 KB |
Output is correct |
22 |
Correct |
81 ms |
178580 KB |
Output is correct |
23 |
Correct |
82 ms |
178508 KB |
Output is correct |
24 |
Correct |
81 ms |
178556 KB |
Output is correct |
25 |
Correct |
81 ms |
178600 KB |
Output is correct |
26 |
Correct |
83 ms |
178612 KB |
Output is correct |
27 |
Correct |
82 ms |
178532 KB |
Output is correct |
28 |
Correct |
84 ms |
178548 KB |
Output is correct |
29 |
Correct |
82 ms |
178612 KB |
Output is correct |
30 |
Correct |
87 ms |
178516 KB |
Output is correct |
31 |
Correct |
82 ms |
178516 KB |
Output is correct |
32 |
Correct |
82 ms |
178608 KB |
Output is correct |
33 |
Correct |
82 ms |
178520 KB |
Output is correct |
34 |
Correct |
89 ms |
178468 KB |
Output is correct |
35 |
Correct |
81 ms |
178380 KB |
Output is correct |
36 |
Correct |
85 ms |
178400 KB |
Output is correct |
37 |
Correct |
81 ms |
178436 KB |
Output is correct |
38 |
Correct |
82 ms |
178508 KB |
Output is correct |
39 |
Correct |
81 ms |
178620 KB |
Output is correct |
40 |
Correct |
81 ms |
178968 KB |
Output is correct |
41 |
Correct |
87 ms |
180716 KB |
Output is correct |
42 |
Correct |
84 ms |
180048 KB |
Output is correct |
43 |
Correct |
83 ms |
179996 KB |
Output is correct |
44 |
Correct |
94 ms |
180080 KB |
Output is correct |
45 |
Correct |
83 ms |
179968 KB |
Output is correct |
46 |
Correct |
83 ms |
180052 KB |
Output is correct |
47 |
Correct |
84 ms |
180092 KB |
Output is correct |
48 |
Correct |
83 ms |
180032 KB |
Output is correct |
49 |
Correct |
84 ms |
180100 KB |
Output is correct |
50 |
Correct |
85 ms |
179996 KB |
Output is correct |
51 |
Correct |
86 ms |
180404 KB |
Output is correct |
52 |
Correct |
85 ms |
180256 KB |
Output is correct |
53 |
Correct |
84 ms |
180084 KB |
Output is correct |
54 |
Correct |
92 ms |
184524 KB |
Output is correct |
55 |
Correct |
410 ms |
226568 KB |
Output is correct |
56 |
Correct |
163 ms |
203832 KB |
Output is correct |
57 |
Correct |
146 ms |
202360 KB |
Output is correct |
58 |
Correct |
131 ms |
202120 KB |
Output is correct |
59 |
Correct |
138 ms |
202084 KB |
Output is correct |
60 |
Correct |
126 ms |
202052 KB |
Output is correct |
61 |
Correct |
140 ms |
202892 KB |
Output is correct |
62 |
Correct |
330 ms |
221008 KB |
Output is correct |
63 |
Correct |
135 ms |
202512 KB |
Output is correct |
64 |
Correct |
145 ms |
203428 KB |
Output is correct |
65 |
Correct |
214 ms |
212272 KB |
Output is correct |
66 |
Correct |
207 ms |
212004 KB |
Output is correct |
67 |
Correct |
124 ms |
202080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
81 ms |
178428 KB |
Output is correct |
2 |
Correct |
79 ms |
178368 KB |
Output is correct |
3 |
Correct |
81 ms |
178468 KB |
Output is correct |
4 |
Correct |
79 ms |
178380 KB |
Output is correct |
5 |
Correct |
78 ms |
178468 KB |
Output is correct |
6 |
Correct |
78 ms |
178432 KB |
Output is correct |
7 |
Correct |
81 ms |
178436 KB |
Output is correct |
8 |
Correct |
79 ms |
178476 KB |
Output is correct |
9 |
Correct |
79 ms |
178364 KB |
Output is correct |
10 |
Correct |
83 ms |
178476 KB |
Output is correct |
11 |
Correct |
79 ms |
178508 KB |
Output is correct |
12 |
Correct |
82 ms |
178444 KB |
Output is correct |
13 |
Correct |
1550 ms |
342956 KB |
Output is correct |
14 |
Correct |
1945 ms |
371680 KB |
Output is correct |
15 |
Correct |
2336 ms |
405348 KB |
Output is correct |
16 |
Correct |
2814 ms |
444640 KB |
Output is correct |
17 |
Execution timed out |
3109 ms |
491060 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |