//Challenge: Accepted
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 2505
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
const ll inf = 1LL<<60;
int nxt[maxn][maxn];
ll dp[maxn][maxn], g[maxn][maxn];
int main() {
io
int n;
cin >> n;
string s;
cin >> s;
ll A, B, C;
cin >> A >> B >> C;
for (int i = 0;i < n;i++) {
for (int j = 0;j < n;j++) {
dp[i][j] = inf, nxt[i][j] = 1<<30;
g[i][j] = inf;
}
}
for (int i = 0;i < n;i++) {
string t = s.substr(i, n - i);
int siz = n - i;
vector<int> z(siz, 0), fi(siz, 1<<30);
int lef = 0, rig = 0;
for (int j = 1;j < siz;j++) {
if (j > rig) lef = j, rig = j;
else z[j] = min(z[j - lef], rig - j + 1);
while (j + z[j] < siz && t[j + z[j]] == t[z[j]]) z[j]++;
int v = min(j, z[j]);
fi[v] = min(fi[v], j);
if (j + z[j] - 1 > rig) lef = j, rig = j + z[j] - 1;
}
for (int j = siz - 2;j >= 0;j--) fi[j] = min(fi[j], fi[j+1]);
for (int j = 1;j < siz;j++) {
nxt[i][j-1] = i + fi[j];
if (nxt[i][j-1] < n) debug(i, j-1, i + fi[j]);
}
}
for (int x = 0;x < n;x++) {
for (int i = 0;i < n - x;i++) {
int j = i + x;
dp[i][j] = min(dp[i][j], A * (x + 1) + B);
g[i][j] = min(g[i][j], dp[i][j] + C + B);
if (i) {
dp[i-1][j] = min(dp[i-1][j], g[i][j] + A);
g[i-1][j] = min(g[i-1][j], g[i][j] + A);
}
if (j < n - 1) dp[i][j+1] = min(dp[i][j+1], dp[i][j] + A);
int cur = nxt[i][x], cnt = 2;
//debug(i, j);
while (cur < n) {
//debug("to", cur, cur + x, dp[i][j] + C * (cnt) + (cur + x - i + 1 - ((x+1) * cnt)) * A + B);
dp[i][cur + x] = min(dp[i][cur + x], dp[i][j] + C * (cnt) + (cur + x - i + 1 - ((x+1) * cnt)) * A + B);
g[i][cur + x] = min(g[i][cur + x], dp[i][cur + x]);
cur = nxt[cur][x];
cnt++;
}
//debug(i, j, dp[i][j]);
}
//debug();
}
cout << dp[0][n-1] - B << endl;
}
Compilation message
copypaste3.cpp: In function 'int main()':
copypaste3.cpp:12:20: warning: statement has no effect [-Wunused-value]
12 | #define debug(...) 0
| ^
copypaste3.cpp:57:25: note: in expansion of macro 'debug'
57 | if (nxt[i][j-1] < n) debug(i, j-1, i + fi[j]);
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
456 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
367 ms |
85240 KB |
Output is correct |
4 |
Correct |
434 ms |
96708 KB |
Output is correct |
5 |
Correct |
537 ms |
105200 KB |
Output is correct |
6 |
Correct |
668 ms |
113792 KB |
Output is correct |
7 |
Correct |
784 ms |
122928 KB |
Output is correct |
8 |
Correct |
775 ms |
122928 KB |
Output is correct |
9 |
Correct |
810 ms |
122936 KB |
Output is correct |
10 |
Correct |
1 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 |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
456 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
712 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
724 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 |
724 KB |
Output is correct |
33 |
Correct |
1 ms |
712 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
328 KB |
Output is correct |
36 |
Correct |
1 ms |
324 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
712 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
724 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 |
724 KB |
Output is correct |
33 |
Correct |
1 ms |
712 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
328 KB |
Output is correct |
36 |
Correct |
1 ms |
324 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
716 KB |
Output is correct |
40 |
Correct |
1 ms |
1480 KB |
Output is correct |
41 |
Correct |
3 ms |
3540 KB |
Output is correct |
42 |
Correct |
2 ms |
3540 KB |
Output is correct |
43 |
Correct |
2 ms |
3540 KB |
Output is correct |
44 |
Correct |
3 ms |
3540 KB |
Output is correct |
45 |
Correct |
2 ms |
3540 KB |
Output is correct |
46 |
Correct |
2 ms |
3540 KB |
Output is correct |
47 |
Correct |
3 ms |
3540 KB |
Output is correct |
48 |
Correct |
2 ms |
3500 KB |
Output is correct |
49 |
Correct |
3 ms |
3540 KB |
Output is correct |
50 |
Correct |
3 ms |
3524 KB |
Output is correct |
51 |
Correct |
3 ms |
3540 KB |
Output is correct |
52 |
Correct |
3 ms |
3540 KB |
Output is correct |
53 |
Correct |
2 ms |
3540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
712 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
724 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 |
724 KB |
Output is correct |
33 |
Correct |
1 ms |
712 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
328 KB |
Output is correct |
36 |
Correct |
1 ms |
324 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
716 KB |
Output is correct |
40 |
Correct |
1 ms |
1480 KB |
Output is correct |
41 |
Correct |
3 ms |
3540 KB |
Output is correct |
42 |
Correct |
2 ms |
3540 KB |
Output is correct |
43 |
Correct |
2 ms |
3540 KB |
Output is correct |
44 |
Correct |
3 ms |
3540 KB |
Output is correct |
45 |
Correct |
2 ms |
3540 KB |
Output is correct |
46 |
Correct |
2 ms |
3540 KB |
Output is correct |
47 |
Correct |
3 ms |
3540 KB |
Output is correct |
48 |
Correct |
2 ms |
3500 KB |
Output is correct |
49 |
Correct |
3 ms |
3540 KB |
Output is correct |
50 |
Correct |
3 ms |
3524 KB |
Output is correct |
51 |
Correct |
3 ms |
3540 KB |
Output is correct |
52 |
Correct |
3 ms |
3540 KB |
Output is correct |
53 |
Correct |
2 ms |
3540 KB |
Output is correct |
54 |
Correct |
8 ms |
9700 KB |
Output is correct |
55 |
Correct |
79 ms |
31936 KB |
Output is correct |
56 |
Correct |
41 ms |
31984 KB |
Output is correct |
57 |
Correct |
38 ms |
31956 KB |
Output is correct |
58 |
Correct |
36 ms |
32004 KB |
Output is correct |
59 |
Correct |
38 ms |
31916 KB |
Output is correct |
60 |
Correct |
30 ms |
31956 KB |
Output is correct |
61 |
Correct |
40 ms |
31948 KB |
Output is correct |
62 |
Correct |
75 ms |
31984 KB |
Output is correct |
63 |
Correct |
36 ms |
31956 KB |
Output is correct |
64 |
Correct |
40 ms |
31956 KB |
Output is correct |
65 |
Correct |
62 ms |
31948 KB |
Output is correct |
66 |
Correct |
55 ms |
31984 KB |
Output is correct |
67 |
Correct |
30 ms |
31956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
456 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
367 ms |
85240 KB |
Output is correct |
14 |
Correct |
434 ms |
96708 KB |
Output is correct |
15 |
Correct |
537 ms |
105200 KB |
Output is correct |
16 |
Correct |
668 ms |
113792 KB |
Output is correct |
17 |
Correct |
784 ms |
122928 KB |
Output is correct |
18 |
Correct |
775 ms |
122928 KB |
Output is correct |
19 |
Correct |
810 ms |
122936 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
456 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
324 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
468 KB |
Output is correct |
30 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
712 KB |
Output is correct |
39 |
Correct |
1 ms |
596 KB |
Output is correct |
40 |
Correct |
1 ms |
596 KB |
Output is correct |
41 |
Correct |
1 ms |
724 KB |
Output is correct |
42 |
Correct |
1 ms |
724 KB |
Output is correct |
43 |
Correct |
1 ms |
724 KB |
Output is correct |
44 |
Correct |
1 ms |
596 KB |
Output is correct |
45 |
Correct |
1 ms |
596 KB |
Output is correct |
46 |
Correct |
1 ms |
724 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 |
724 KB |
Output is correct |
50 |
Correct |
1 ms |
712 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
328 KB |
Output is correct |
53 |
Correct |
1 ms |
324 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
468 KB |
Output is correct |
56 |
Correct |
1 ms |
716 KB |
Output is correct |
57 |
Correct |
1 ms |
1480 KB |
Output is correct |
58 |
Correct |
3 ms |
3540 KB |
Output is correct |
59 |
Correct |
2 ms |
3540 KB |
Output is correct |
60 |
Correct |
2 ms |
3540 KB |
Output is correct |
61 |
Correct |
3 ms |
3540 KB |
Output is correct |
62 |
Correct |
2 ms |
3540 KB |
Output is correct |
63 |
Correct |
2 ms |
3540 KB |
Output is correct |
64 |
Correct |
3 ms |
3540 KB |
Output is correct |
65 |
Correct |
2 ms |
3500 KB |
Output is correct |
66 |
Correct |
3 ms |
3540 KB |
Output is correct |
67 |
Correct |
3 ms |
3524 KB |
Output is correct |
68 |
Correct |
3 ms |
3540 KB |
Output is correct |
69 |
Correct |
3 ms |
3540 KB |
Output is correct |
70 |
Correct |
2 ms |
3540 KB |
Output is correct |
71 |
Correct |
8 ms |
9700 KB |
Output is correct |
72 |
Correct |
79 ms |
31936 KB |
Output is correct |
73 |
Correct |
41 ms |
31984 KB |
Output is correct |
74 |
Correct |
38 ms |
31956 KB |
Output is correct |
75 |
Correct |
36 ms |
32004 KB |
Output is correct |
76 |
Correct |
38 ms |
31916 KB |
Output is correct |
77 |
Correct |
30 ms |
31956 KB |
Output is correct |
78 |
Correct |
40 ms |
31948 KB |
Output is correct |
79 |
Correct |
75 ms |
31984 KB |
Output is correct |
80 |
Correct |
36 ms |
31956 KB |
Output is correct |
81 |
Correct |
40 ms |
31956 KB |
Output is correct |
82 |
Correct |
62 ms |
31948 KB |
Output is correct |
83 |
Correct |
55 ms |
31984 KB |
Output is correct |
84 |
Correct |
30 ms |
31956 KB |
Output is correct |
85 |
Correct |
99 ms |
67636 KB |
Output is correct |
86 |
Correct |
242 ms |
122928 KB |
Output is correct |
87 |
Correct |
242 ms |
122932 KB |
Output is correct |
88 |
Correct |
177 ms |
122928 KB |
Output is correct |
89 |
Correct |
178 ms |
122956 KB |
Output is correct |
90 |
Correct |
176 ms |
122932 KB |
Output is correct |
91 |
Correct |
450 ms |
123056 KB |
Output is correct |
92 |
Correct |
581 ms |
122952 KB |
Output is correct |
93 |
Correct |
241 ms |
123032 KB |
Output is correct |
94 |
Correct |
263 ms |
122936 KB |
Output is correct |
95 |
Correct |
483 ms |
122932 KB |
Output is correct |
96 |
Correct |
478 ms |
122940 KB |
Output is correct |
97 |
Correct |
167 ms |
122844 KB |
Output is correct |