#include <bits/stdc++.h>
#define int ll
using namespace std;
#define ll long long
#define f first
#define s second
#define pii pair<ll, ll>
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#define pb push_back
#define MX(a,b) a = max(a,b)
#define MN(a,b) a = min(a,b)
#define FOR(i,a,b) for (int i = a; i<b; ++i)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n+1)
#define RREP(i,n) for (int i = n-1; i>=0; --i)
#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<"- "<<#__VA_ARGS__<<": ", _do(__VA_ARGS__)
template<typename T> void _do(T && x) {cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T && x, S && ...y) {cerr<<x<<", "; _do(y...);}
#else
#define bug(...)
#define endl '\n'
#endif // BALBIT
const int maxn = 2500+5;
int comlen[maxn][maxn];
string s;
int A,B,C; // single letter, cut, paste
int dp[maxn][maxn];
vector<int> todo[maxn];
int nxt[maxn][maxn]; // starting at i, with length j
signed main(){
ios::sync_with_stdio(0), cin.tie(0);
int n; cin>>n;
cin>>s;
cin>>A>>B>>C;
// build comlen
for (int j = n-1; j>=0; --j) {
for (int i = j; i>=0; --i) {
if (s[i] != s[j]) {comlen[i][j] = comlen[j][i] = 0;}
else{
comlen[i][j] = comlen[j][i] = 1 + comlen[i+1][j+1];
}
}
}
REP(i,n) REP(j,i) bug(i,j,comlen[i][j]);
memset(nxt, -1, sizeof nxt);
REP(i,n) {
set<int> have;
vector<pii> yom;
for (int j = i+1; j<n; ++j) {
yom.pb({comlen[i][j], j});
}
sort(ALL(yom), greater<pii>());
int at = 0;
for (int len = n; len>=1; --len) {
if (i + len*2 <=n) {
while (at < SZ(yom) && yom[at].f >= len) {
have.insert(yom[at].s); ++at;
}
auto it = have.lower_bound(i+len);
nxt[i][len] = it==have.end()?-1 : *it;
bug(nxt[i][len]);
}
}
}
RREP(i,n) {
REP(j,n) todo[j].clear();
for (int len = 1; len*2+i<=n; ++len) {
int at = nxt[i][len];
while(at != -1) {
todo[at + len-1].pb(len);
at = nxt[at][len];
}
}
vector<int> howgood(n+1);
int best = 0;
for (int j = i; j<n; ++j) {
int len = j-i+1;
dp[i][j] = A + dp[i+1][j];
for (int t : todo[j]) {
howgood[t] += -t * A + C;
MN(best, howgood[t]);
}
bug(best);
MN(dp[i][j], len * A + best);
howgood[len] = dp[i][j] + B - (len) * A + C;
MN(best, howgood[len]);
}
}
cout<<dp[0][n-1]<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
49480 KB |
Output is correct |
2 |
Correct |
24 ms |
49484 KB |
Output is correct |
3 |
Correct |
21 ms |
49516 KB |
Output is correct |
4 |
Correct |
22 ms |
49388 KB |
Output is correct |
5 |
Correct |
23 ms |
49408 KB |
Output is correct |
6 |
Correct |
23 ms |
49496 KB |
Output is correct |
7 |
Correct |
24 ms |
49456 KB |
Output is correct |
8 |
Correct |
21 ms |
49492 KB |
Output is correct |
9 |
Correct |
23 ms |
49492 KB |
Output is correct |
10 |
Correct |
20 ms |
49452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
49620 KB |
Output is correct |
2 |
Correct |
22 ms |
49400 KB |
Output is correct |
3 |
Correct |
633 ms |
103944 KB |
Output is correct |
4 |
Correct |
741 ms |
111832 KB |
Output is correct |
5 |
Correct |
904 ms |
118556 KB |
Output is correct |
6 |
Correct |
1129 ms |
125384 KB |
Output is correct |
7 |
Correct |
1547 ms |
132876 KB |
Output is correct |
8 |
Correct |
1404 ms |
132984 KB |
Output is correct |
9 |
Correct |
1500 ms |
133008 KB |
Output is correct |
10 |
Correct |
20 ms |
49492 KB |
Output is correct |
11 |
Correct |
22 ms |
49428 KB |
Output is correct |
12 |
Correct |
22 ms |
49480 KB |
Output is correct |
13 |
Correct |
26 ms |
49436 KB |
Output is correct |
14 |
Correct |
23 ms |
49516 KB |
Output is correct |
15 |
Correct |
23 ms |
49492 KB |
Output is correct |
16 |
Correct |
24 ms |
49680 KB |
Output is correct |
17 |
Correct |
22 ms |
49460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
49480 KB |
Output is correct |
2 |
Correct |
24 ms |
49484 KB |
Output is correct |
3 |
Correct |
21 ms |
49516 KB |
Output is correct |
4 |
Correct |
22 ms |
49388 KB |
Output is correct |
5 |
Correct |
23 ms |
49408 KB |
Output is correct |
6 |
Correct |
23 ms |
49496 KB |
Output is correct |
7 |
Correct |
24 ms |
49456 KB |
Output is correct |
8 |
Correct |
21 ms |
49492 KB |
Output is correct |
9 |
Correct |
23 ms |
49492 KB |
Output is correct |
10 |
Correct |
20 ms |
49452 KB |
Output is correct |
11 |
Correct |
22 ms |
49496 KB |
Output is correct |
12 |
Correct |
21 ms |
49628 KB |
Output is correct |
13 |
Correct |
23 ms |
49640 KB |
Output is correct |
14 |
Correct |
23 ms |
49568 KB |
Output is correct |
15 |
Correct |
24 ms |
49592 KB |
Output is correct |
16 |
Correct |
22 ms |
49532 KB |
Output is correct |
17 |
Correct |
23 ms |
49484 KB |
Output is correct |
18 |
Correct |
22 ms |
49428 KB |
Output is correct |
19 |
Correct |
25 ms |
49620 KB |
Output is correct |
20 |
Correct |
21 ms |
49492 KB |
Output is correct |
21 |
Correct |
23 ms |
49724 KB |
Output is correct |
22 |
Correct |
23 ms |
49804 KB |
Output is correct |
23 |
Correct |
23 ms |
49748 KB |
Output is correct |
24 |
Correct |
25 ms |
49760 KB |
Output is correct |
25 |
Correct |
22 ms |
49680 KB |
Output is correct |
26 |
Correct |
23 ms |
49736 KB |
Output is correct |
27 |
Correct |
24 ms |
49748 KB |
Output is correct |
28 |
Correct |
22 ms |
49636 KB |
Output is correct |
29 |
Correct |
29 ms |
49736 KB |
Output is correct |
30 |
Correct |
25 ms |
49740 KB |
Output is correct |
31 |
Correct |
30 ms |
49756 KB |
Output is correct |
32 |
Correct |
23 ms |
49748 KB |
Output is correct |
33 |
Correct |
21 ms |
49740 KB |
Output is correct |
34 |
Correct |
22 ms |
49492 KB |
Output is correct |
35 |
Correct |
21 ms |
49452 KB |
Output is correct |
36 |
Correct |
22 ms |
49448 KB |
Output is correct |
37 |
Correct |
26 ms |
49464 KB |
Output is correct |
38 |
Correct |
27 ms |
49544 KB |
Output is correct |
39 |
Correct |
28 ms |
49704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
49480 KB |
Output is correct |
2 |
Correct |
24 ms |
49484 KB |
Output is correct |
3 |
Correct |
21 ms |
49516 KB |
Output is correct |
4 |
Correct |
22 ms |
49388 KB |
Output is correct |
5 |
Correct |
23 ms |
49408 KB |
Output is correct |
6 |
Correct |
23 ms |
49496 KB |
Output is correct |
7 |
Correct |
24 ms |
49456 KB |
Output is correct |
8 |
Correct |
21 ms |
49492 KB |
Output is correct |
9 |
Correct |
23 ms |
49492 KB |
Output is correct |
10 |
Correct |
20 ms |
49452 KB |
Output is correct |
11 |
Correct |
22 ms |
49496 KB |
Output is correct |
12 |
Correct |
21 ms |
49628 KB |
Output is correct |
13 |
Correct |
23 ms |
49640 KB |
Output is correct |
14 |
Correct |
23 ms |
49568 KB |
Output is correct |
15 |
Correct |
24 ms |
49592 KB |
Output is correct |
16 |
Correct |
22 ms |
49532 KB |
Output is correct |
17 |
Correct |
23 ms |
49484 KB |
Output is correct |
18 |
Correct |
22 ms |
49428 KB |
Output is correct |
19 |
Correct |
25 ms |
49620 KB |
Output is correct |
20 |
Correct |
21 ms |
49492 KB |
Output is correct |
21 |
Correct |
23 ms |
49724 KB |
Output is correct |
22 |
Correct |
23 ms |
49804 KB |
Output is correct |
23 |
Correct |
23 ms |
49748 KB |
Output is correct |
24 |
Correct |
25 ms |
49760 KB |
Output is correct |
25 |
Correct |
22 ms |
49680 KB |
Output is correct |
26 |
Correct |
23 ms |
49736 KB |
Output is correct |
27 |
Correct |
24 ms |
49748 KB |
Output is correct |
28 |
Correct |
22 ms |
49636 KB |
Output is correct |
29 |
Correct |
29 ms |
49736 KB |
Output is correct |
30 |
Correct |
25 ms |
49740 KB |
Output is correct |
31 |
Correct |
30 ms |
49756 KB |
Output is correct |
32 |
Correct |
23 ms |
49748 KB |
Output is correct |
33 |
Correct |
21 ms |
49740 KB |
Output is correct |
34 |
Correct |
22 ms |
49492 KB |
Output is correct |
35 |
Correct |
21 ms |
49452 KB |
Output is correct |
36 |
Correct |
22 ms |
49448 KB |
Output is correct |
37 |
Correct |
26 ms |
49464 KB |
Output is correct |
38 |
Correct |
27 ms |
49544 KB |
Output is correct |
39 |
Correct |
28 ms |
49704 KB |
Output is correct |
40 |
Correct |
22 ms |
50192 KB |
Output is correct |
41 |
Correct |
26 ms |
51600 KB |
Output is correct |
42 |
Correct |
26 ms |
51532 KB |
Output is correct |
43 |
Correct |
26 ms |
51608 KB |
Output is correct |
44 |
Correct |
24 ms |
51500 KB |
Output is correct |
45 |
Correct |
25 ms |
51524 KB |
Output is correct |
46 |
Correct |
23 ms |
51516 KB |
Output is correct |
47 |
Correct |
27 ms |
51512 KB |
Output is correct |
48 |
Correct |
23 ms |
51528 KB |
Output is correct |
49 |
Correct |
25 ms |
51568 KB |
Output is correct |
50 |
Correct |
26 ms |
51532 KB |
Output is correct |
51 |
Correct |
25 ms |
51544 KB |
Output is correct |
52 |
Correct |
25 ms |
51500 KB |
Output is correct |
53 |
Correct |
23 ms |
51484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
49480 KB |
Output is correct |
2 |
Correct |
24 ms |
49484 KB |
Output is correct |
3 |
Correct |
21 ms |
49516 KB |
Output is correct |
4 |
Correct |
22 ms |
49388 KB |
Output is correct |
5 |
Correct |
23 ms |
49408 KB |
Output is correct |
6 |
Correct |
23 ms |
49496 KB |
Output is correct |
7 |
Correct |
24 ms |
49456 KB |
Output is correct |
8 |
Correct |
21 ms |
49492 KB |
Output is correct |
9 |
Correct |
23 ms |
49492 KB |
Output is correct |
10 |
Correct |
20 ms |
49452 KB |
Output is correct |
11 |
Correct |
22 ms |
49496 KB |
Output is correct |
12 |
Correct |
21 ms |
49628 KB |
Output is correct |
13 |
Correct |
23 ms |
49640 KB |
Output is correct |
14 |
Correct |
23 ms |
49568 KB |
Output is correct |
15 |
Correct |
24 ms |
49592 KB |
Output is correct |
16 |
Correct |
22 ms |
49532 KB |
Output is correct |
17 |
Correct |
23 ms |
49484 KB |
Output is correct |
18 |
Correct |
22 ms |
49428 KB |
Output is correct |
19 |
Correct |
25 ms |
49620 KB |
Output is correct |
20 |
Correct |
21 ms |
49492 KB |
Output is correct |
21 |
Correct |
23 ms |
49724 KB |
Output is correct |
22 |
Correct |
23 ms |
49804 KB |
Output is correct |
23 |
Correct |
23 ms |
49748 KB |
Output is correct |
24 |
Correct |
25 ms |
49760 KB |
Output is correct |
25 |
Correct |
22 ms |
49680 KB |
Output is correct |
26 |
Correct |
23 ms |
49736 KB |
Output is correct |
27 |
Correct |
24 ms |
49748 KB |
Output is correct |
28 |
Correct |
22 ms |
49636 KB |
Output is correct |
29 |
Correct |
29 ms |
49736 KB |
Output is correct |
30 |
Correct |
25 ms |
49740 KB |
Output is correct |
31 |
Correct |
30 ms |
49756 KB |
Output is correct |
32 |
Correct |
23 ms |
49748 KB |
Output is correct |
33 |
Correct |
21 ms |
49740 KB |
Output is correct |
34 |
Correct |
22 ms |
49492 KB |
Output is correct |
35 |
Correct |
21 ms |
49452 KB |
Output is correct |
36 |
Correct |
22 ms |
49448 KB |
Output is correct |
37 |
Correct |
26 ms |
49464 KB |
Output is correct |
38 |
Correct |
27 ms |
49544 KB |
Output is correct |
39 |
Correct |
28 ms |
49704 KB |
Output is correct |
40 |
Correct |
22 ms |
50192 KB |
Output is correct |
41 |
Correct |
26 ms |
51600 KB |
Output is correct |
42 |
Correct |
26 ms |
51532 KB |
Output is correct |
43 |
Correct |
26 ms |
51608 KB |
Output is correct |
44 |
Correct |
24 ms |
51500 KB |
Output is correct |
45 |
Correct |
25 ms |
51524 KB |
Output is correct |
46 |
Correct |
23 ms |
51516 KB |
Output is correct |
47 |
Correct |
27 ms |
51512 KB |
Output is correct |
48 |
Correct |
23 ms |
51528 KB |
Output is correct |
49 |
Correct |
25 ms |
51568 KB |
Output is correct |
50 |
Correct |
26 ms |
51532 KB |
Output is correct |
51 |
Correct |
25 ms |
51544 KB |
Output is correct |
52 |
Correct |
25 ms |
51500 KB |
Output is correct |
53 |
Correct |
23 ms |
51484 KB |
Output is correct |
54 |
Correct |
42 ms |
55448 KB |
Output is correct |
55 |
Correct |
167 ms |
69500 KB |
Output is correct |
56 |
Correct |
112 ms |
69312 KB |
Output is correct |
57 |
Correct |
98 ms |
69196 KB |
Output is correct |
58 |
Correct |
71 ms |
69196 KB |
Output is correct |
59 |
Correct |
62 ms |
69308 KB |
Output is correct |
60 |
Correct |
56 ms |
69284 KB |
Output is correct |
61 |
Correct |
65 ms |
69308 KB |
Output is correct |
62 |
Correct |
120 ms |
69460 KB |
Output is correct |
63 |
Correct |
66 ms |
69308 KB |
Output is correct |
64 |
Correct |
61 ms |
69216 KB |
Output is correct |
65 |
Correct |
83 ms |
69308 KB |
Output is correct |
66 |
Correct |
101 ms |
69324 KB |
Output is correct |
67 |
Correct |
58 ms |
69248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
49480 KB |
Output is correct |
2 |
Correct |
24 ms |
49484 KB |
Output is correct |
3 |
Correct |
21 ms |
49516 KB |
Output is correct |
4 |
Correct |
22 ms |
49388 KB |
Output is correct |
5 |
Correct |
23 ms |
49408 KB |
Output is correct |
6 |
Correct |
23 ms |
49496 KB |
Output is correct |
7 |
Correct |
24 ms |
49456 KB |
Output is correct |
8 |
Correct |
21 ms |
49492 KB |
Output is correct |
9 |
Correct |
23 ms |
49492 KB |
Output is correct |
10 |
Correct |
20 ms |
49452 KB |
Output is correct |
11 |
Correct |
22 ms |
49620 KB |
Output is correct |
12 |
Correct |
22 ms |
49400 KB |
Output is correct |
13 |
Correct |
633 ms |
103944 KB |
Output is correct |
14 |
Correct |
741 ms |
111832 KB |
Output is correct |
15 |
Correct |
904 ms |
118556 KB |
Output is correct |
16 |
Correct |
1129 ms |
125384 KB |
Output is correct |
17 |
Correct |
1547 ms |
132876 KB |
Output is correct |
18 |
Correct |
1404 ms |
132984 KB |
Output is correct |
19 |
Correct |
1500 ms |
133008 KB |
Output is correct |
20 |
Correct |
20 ms |
49492 KB |
Output is correct |
21 |
Correct |
22 ms |
49428 KB |
Output is correct |
22 |
Correct |
22 ms |
49480 KB |
Output is correct |
23 |
Correct |
26 ms |
49436 KB |
Output is correct |
24 |
Correct |
23 ms |
49516 KB |
Output is correct |
25 |
Correct |
23 ms |
49492 KB |
Output is correct |
26 |
Correct |
24 ms |
49680 KB |
Output is correct |
27 |
Correct |
22 ms |
49460 KB |
Output is correct |
28 |
Correct |
22 ms |
49496 KB |
Output is correct |
29 |
Correct |
21 ms |
49628 KB |
Output is correct |
30 |
Correct |
23 ms |
49640 KB |
Output is correct |
31 |
Correct |
23 ms |
49568 KB |
Output is correct |
32 |
Correct |
24 ms |
49592 KB |
Output is correct |
33 |
Correct |
22 ms |
49532 KB |
Output is correct |
34 |
Correct |
23 ms |
49484 KB |
Output is correct |
35 |
Correct |
22 ms |
49428 KB |
Output is correct |
36 |
Correct |
25 ms |
49620 KB |
Output is correct |
37 |
Correct |
21 ms |
49492 KB |
Output is correct |
38 |
Correct |
23 ms |
49724 KB |
Output is correct |
39 |
Correct |
23 ms |
49804 KB |
Output is correct |
40 |
Correct |
23 ms |
49748 KB |
Output is correct |
41 |
Correct |
25 ms |
49760 KB |
Output is correct |
42 |
Correct |
22 ms |
49680 KB |
Output is correct |
43 |
Correct |
23 ms |
49736 KB |
Output is correct |
44 |
Correct |
24 ms |
49748 KB |
Output is correct |
45 |
Correct |
22 ms |
49636 KB |
Output is correct |
46 |
Correct |
29 ms |
49736 KB |
Output is correct |
47 |
Correct |
25 ms |
49740 KB |
Output is correct |
48 |
Correct |
30 ms |
49756 KB |
Output is correct |
49 |
Correct |
23 ms |
49748 KB |
Output is correct |
50 |
Correct |
21 ms |
49740 KB |
Output is correct |
51 |
Correct |
22 ms |
49492 KB |
Output is correct |
52 |
Correct |
21 ms |
49452 KB |
Output is correct |
53 |
Correct |
22 ms |
49448 KB |
Output is correct |
54 |
Correct |
26 ms |
49464 KB |
Output is correct |
55 |
Correct |
27 ms |
49544 KB |
Output is correct |
56 |
Correct |
28 ms |
49704 KB |
Output is correct |
57 |
Correct |
22 ms |
50192 KB |
Output is correct |
58 |
Correct |
26 ms |
51600 KB |
Output is correct |
59 |
Correct |
26 ms |
51532 KB |
Output is correct |
60 |
Correct |
26 ms |
51608 KB |
Output is correct |
61 |
Correct |
24 ms |
51500 KB |
Output is correct |
62 |
Correct |
25 ms |
51524 KB |
Output is correct |
63 |
Correct |
23 ms |
51516 KB |
Output is correct |
64 |
Correct |
27 ms |
51512 KB |
Output is correct |
65 |
Correct |
23 ms |
51528 KB |
Output is correct |
66 |
Correct |
25 ms |
51568 KB |
Output is correct |
67 |
Correct |
26 ms |
51532 KB |
Output is correct |
68 |
Correct |
25 ms |
51544 KB |
Output is correct |
69 |
Correct |
25 ms |
51500 KB |
Output is correct |
70 |
Correct |
23 ms |
51484 KB |
Output is correct |
71 |
Correct |
42 ms |
55448 KB |
Output is correct |
72 |
Correct |
167 ms |
69500 KB |
Output is correct |
73 |
Correct |
112 ms |
69312 KB |
Output is correct |
74 |
Correct |
98 ms |
69196 KB |
Output is correct |
75 |
Correct |
71 ms |
69196 KB |
Output is correct |
76 |
Correct |
62 ms |
69308 KB |
Output is correct |
77 |
Correct |
56 ms |
69284 KB |
Output is correct |
78 |
Correct |
65 ms |
69308 KB |
Output is correct |
79 |
Correct |
120 ms |
69460 KB |
Output is correct |
80 |
Correct |
66 ms |
69308 KB |
Output is correct |
81 |
Correct |
61 ms |
69216 KB |
Output is correct |
82 |
Correct |
83 ms |
69308 KB |
Output is correct |
83 |
Correct |
101 ms |
69324 KB |
Output is correct |
84 |
Correct |
58 ms |
69248 KB |
Output is correct |
85 |
Correct |
238 ms |
91604 KB |
Output is correct |
86 |
Correct |
589 ms |
132072 KB |
Output is correct |
87 |
Correct |
484 ms |
132056 KB |
Output is correct |
88 |
Correct |
355 ms |
132040 KB |
Output is correct |
89 |
Correct |
278 ms |
132044 KB |
Output is correct |
90 |
Correct |
243 ms |
132036 KB |
Output is correct |
91 |
Correct |
390 ms |
132296 KB |
Output is correct |
92 |
Correct |
529 ms |
132360 KB |
Output is correct |
93 |
Correct |
257 ms |
132136 KB |
Output is correct |
94 |
Correct |
273 ms |
132096 KB |
Output is correct |
95 |
Correct |
469 ms |
132184 KB |
Output is correct |
96 |
Correct |
486 ms |
132292 KB |
Output is correct |
97 |
Correct |
244 ms |
132116 KB |
Output is correct |