#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
const ll nx=2505, mod=1e9+9, mul=41;
ll n, a, b, c, s[nx], dp[nx][nx], pw[nx];
string stmp;
struct update
{
ll l, r, cnta, cntc;
update(ll l=0, ll r=0, ll cnta=0, ll cntc=0): l(l), r(r), cnta(cnta), cntc(cntc){}
};
vector<update> calc[nx][nx];
int32_t main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>stmp>>a>>b>>c;
pw[0]=1;
for (int i=1; i<=n; i++) s[i]=stmp[i-1]-'a'+1, pw[i]=(pw[i-1]*mul)%mod;
for (int sz=1; sz<=n; sz++)
{
map<ll, vector<ll>> mp;
ll sm=0;
for (int i=1; i<=sz; i++) sm=(sm+pw[sz-i]*s[i])%mod;
mp[sm].push_back(1);
for (int i=sz+1; i<=n; i++) sm=(sm*mul)%mod, sm=((sm-s[i-sz]*pw[sz])%mod+mod)%mod, sm=(sm+s[i])%mod, mp[sm].push_back(i-sz+1);
for (auto &[_, v]:mp)
{
int csz=v.size(), pts=v.size();
vector<int> pa(csz);
for (int i=csz-1; i>=0; i--)
{
while (pts!=0&&v[i]+sz<=v[pts-1]) pts--;
if (pts!=v.size()) pa[i]=pts;
else pa[i]=-1;
}
/*
cout<<"string update ";
for (int i=v[0]; i<v[0]+sz; i++) cout<<(char)(s[i]-1+'a');
cout<<'\n';
for (int i=0; i<csz; i++) cout<<pa[i]<<' ';
cout<<'\n';*/
for (int i=0; i<csz; i++)
{
if (pa[i]==-1) continue;
ll tmp=pa[i], cnt=2;
while (tmp!=-1)
{
calc[v[0]][v[0]+sz-1].push_back(update(v[i], v[tmp]+sz-1, v[tmp]+sz-v[i]-cnt*sz, cnt));
//cout<<"upd "<<v[i]<<' '<<v[tmp]+sz-1<<' '<<v[tmp]+sz-v[i]-cnt*sz<<' '<<cnt<<'\n';
cnt++;
tmp=pa[tmp];
}
}
}
}
for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) dp[i][j]=1e18;
for (int i=1; i<=n; i++) dp[i][i]=a;
for (int sz=0; sz<=n; sz++)
{
for (int l=1; l+sz<=n; l++)
{
int r=l+sz;
if (l!=r) dp[l][r]=min({dp[l][r], dp[l+1][r]+a, dp[l][r-1]+a});
for (auto upd:calc[l][r]) dp[upd.l][upd.r]=min(dp[upd.l][upd.r], dp[l][r]+c*upd.cntc+a*upd.cnta+b);
//cout<<"dp "<<l<<' '<<r<<' '<<dp[l][r]<<'\n';
}
}
cout<<dp[1][n];
}
Compilation message
copypaste3.cpp: In function 'int32_t main()':
copypaste3.cpp:40:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | if (pts!=v.size()) pa[i]=pts;
| ~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
148160 KB |
Output is correct |
2 |
Correct |
39 ms |
148308 KB |
Output is correct |
3 |
Correct |
41 ms |
148268 KB |
Output is correct |
4 |
Correct |
37 ms |
148308 KB |
Output is correct |
5 |
Correct |
36 ms |
148304 KB |
Output is correct |
6 |
Correct |
40 ms |
148264 KB |
Output is correct |
7 |
Correct |
40 ms |
148316 KB |
Output is correct |
8 |
Correct |
39 ms |
148312 KB |
Output is correct |
9 |
Correct |
40 ms |
148564 KB |
Output is correct |
10 |
Correct |
40 ms |
148332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
148308 KB |
Output is correct |
2 |
Correct |
44 ms |
148308 KB |
Output is correct |
3 |
Correct |
400 ms |
508704 KB |
Output is correct |
4 |
Correct |
467 ms |
573152 KB |
Output is correct |
5 |
Correct |
586 ms |
648776 KB |
Output is correct |
6 |
Correct |
698 ms |
733652 KB |
Output is correct |
7 |
Correct |
799 ms |
836396 KB |
Output is correct |
8 |
Correct |
840 ms |
836352 KB |
Output is correct |
9 |
Correct |
926 ms |
836492 KB |
Output is correct |
10 |
Correct |
46 ms |
148160 KB |
Output is correct |
11 |
Correct |
41 ms |
148360 KB |
Output is correct |
12 |
Correct |
39 ms |
148316 KB |
Output is correct |
13 |
Correct |
37 ms |
148136 KB |
Output is correct |
14 |
Correct |
37 ms |
148316 KB |
Output is correct |
15 |
Correct |
45 ms |
148376 KB |
Output is correct |
16 |
Correct |
43 ms |
148256 KB |
Output is correct |
17 |
Correct |
49 ms |
148320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
148160 KB |
Output is correct |
2 |
Correct |
39 ms |
148308 KB |
Output is correct |
3 |
Correct |
41 ms |
148268 KB |
Output is correct |
4 |
Correct |
37 ms |
148308 KB |
Output is correct |
5 |
Correct |
36 ms |
148304 KB |
Output is correct |
6 |
Correct |
40 ms |
148264 KB |
Output is correct |
7 |
Correct |
40 ms |
148316 KB |
Output is correct |
8 |
Correct |
39 ms |
148312 KB |
Output is correct |
9 |
Correct |
40 ms |
148564 KB |
Output is correct |
10 |
Correct |
40 ms |
148332 KB |
Output is correct |
11 |
Correct |
47 ms |
148144 KB |
Output is correct |
12 |
Correct |
42 ms |
148316 KB |
Output is correct |
13 |
Correct |
58 ms |
148264 KB |
Output is correct |
14 |
Correct |
55 ms |
148268 KB |
Output is correct |
15 |
Correct |
41 ms |
148304 KB |
Output is correct |
16 |
Correct |
41 ms |
148304 KB |
Output is correct |
17 |
Correct |
39 ms |
148228 KB |
Output is correct |
18 |
Correct |
54 ms |
148312 KB |
Output is correct |
19 |
Correct |
46 ms |
148316 KB |
Output is correct |
20 |
Correct |
47 ms |
148144 KB |
Output is correct |
21 |
Correct |
44 ms |
148180 KB |
Output is correct |
22 |
Correct |
42 ms |
148400 KB |
Output is correct |
23 |
Correct |
48 ms |
148308 KB |
Output is correct |
24 |
Correct |
38 ms |
148304 KB |
Output is correct |
25 |
Correct |
43 ms |
148304 KB |
Output is correct |
26 |
Correct |
54 ms |
148308 KB |
Output is correct |
27 |
Correct |
44 ms |
148292 KB |
Output is correct |
28 |
Correct |
52 ms |
150356 KB |
Output is correct |
29 |
Correct |
55 ms |
148384 KB |
Output is correct |
30 |
Correct |
46 ms |
148152 KB |
Output is correct |
31 |
Correct |
41 ms |
148260 KB |
Output is correct |
32 |
Correct |
69 ms |
148308 KB |
Output is correct |
33 |
Correct |
64 ms |
148380 KB |
Output is correct |
34 |
Correct |
67 ms |
148120 KB |
Output is correct |
35 |
Correct |
47 ms |
148288 KB |
Output is correct |
36 |
Correct |
44 ms |
148356 KB |
Output is correct |
37 |
Correct |
48 ms |
148200 KB |
Output is correct |
38 |
Correct |
42 ms |
148316 KB |
Output is correct |
39 |
Correct |
43 ms |
148376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
148160 KB |
Output is correct |
2 |
Correct |
39 ms |
148308 KB |
Output is correct |
3 |
Correct |
41 ms |
148268 KB |
Output is correct |
4 |
Correct |
37 ms |
148308 KB |
Output is correct |
5 |
Correct |
36 ms |
148304 KB |
Output is correct |
6 |
Correct |
40 ms |
148264 KB |
Output is correct |
7 |
Correct |
40 ms |
148316 KB |
Output is correct |
8 |
Correct |
39 ms |
148312 KB |
Output is correct |
9 |
Correct |
40 ms |
148564 KB |
Output is correct |
10 |
Correct |
40 ms |
148332 KB |
Output is correct |
11 |
Correct |
47 ms |
148144 KB |
Output is correct |
12 |
Correct |
42 ms |
148316 KB |
Output is correct |
13 |
Correct |
58 ms |
148264 KB |
Output is correct |
14 |
Correct |
55 ms |
148268 KB |
Output is correct |
15 |
Correct |
41 ms |
148304 KB |
Output is correct |
16 |
Correct |
41 ms |
148304 KB |
Output is correct |
17 |
Correct |
39 ms |
148228 KB |
Output is correct |
18 |
Correct |
54 ms |
148312 KB |
Output is correct |
19 |
Correct |
46 ms |
148316 KB |
Output is correct |
20 |
Correct |
47 ms |
148144 KB |
Output is correct |
21 |
Correct |
44 ms |
148180 KB |
Output is correct |
22 |
Correct |
42 ms |
148400 KB |
Output is correct |
23 |
Correct |
48 ms |
148308 KB |
Output is correct |
24 |
Correct |
38 ms |
148304 KB |
Output is correct |
25 |
Correct |
43 ms |
148304 KB |
Output is correct |
26 |
Correct |
54 ms |
148308 KB |
Output is correct |
27 |
Correct |
44 ms |
148292 KB |
Output is correct |
28 |
Correct |
52 ms |
150356 KB |
Output is correct |
29 |
Correct |
55 ms |
148384 KB |
Output is correct |
30 |
Correct |
46 ms |
148152 KB |
Output is correct |
31 |
Correct |
41 ms |
148260 KB |
Output is correct |
32 |
Correct |
69 ms |
148308 KB |
Output is correct |
33 |
Correct |
64 ms |
148380 KB |
Output is correct |
34 |
Correct |
67 ms |
148120 KB |
Output is correct |
35 |
Correct |
47 ms |
148288 KB |
Output is correct |
36 |
Correct |
44 ms |
148356 KB |
Output is correct |
37 |
Correct |
48 ms |
148200 KB |
Output is correct |
38 |
Correct |
42 ms |
148316 KB |
Output is correct |
39 |
Correct |
43 ms |
148376 KB |
Output is correct |
40 |
Correct |
46 ms |
148560 KB |
Output is correct |
41 |
Correct |
43 ms |
152020 KB |
Output is correct |
42 |
Correct |
42 ms |
149892 KB |
Output is correct |
43 |
Correct |
42 ms |
149588 KB |
Output is correct |
44 |
Correct |
47 ms |
149328 KB |
Output is correct |
45 |
Correct |
45 ms |
149328 KB |
Output is correct |
46 |
Correct |
43 ms |
149340 KB |
Output is correct |
47 |
Correct |
41 ms |
150108 KB |
Output is correct |
48 |
Correct |
44 ms |
149588 KB |
Output is correct |
49 |
Correct |
43 ms |
149720 KB |
Output is correct |
50 |
Correct |
44 ms |
149584 KB |
Output is correct |
51 |
Correct |
48 ms |
150612 KB |
Output is correct |
52 |
Correct |
43 ms |
150612 KB |
Output is correct |
53 |
Correct |
42 ms |
149328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
148160 KB |
Output is correct |
2 |
Correct |
39 ms |
148308 KB |
Output is correct |
3 |
Correct |
41 ms |
148268 KB |
Output is correct |
4 |
Correct |
37 ms |
148308 KB |
Output is correct |
5 |
Correct |
36 ms |
148304 KB |
Output is correct |
6 |
Correct |
40 ms |
148264 KB |
Output is correct |
7 |
Correct |
40 ms |
148316 KB |
Output is correct |
8 |
Correct |
39 ms |
148312 KB |
Output is correct |
9 |
Correct |
40 ms |
148564 KB |
Output is correct |
10 |
Correct |
40 ms |
148332 KB |
Output is correct |
11 |
Correct |
47 ms |
148144 KB |
Output is correct |
12 |
Correct |
42 ms |
148316 KB |
Output is correct |
13 |
Correct |
58 ms |
148264 KB |
Output is correct |
14 |
Correct |
55 ms |
148268 KB |
Output is correct |
15 |
Correct |
41 ms |
148304 KB |
Output is correct |
16 |
Correct |
41 ms |
148304 KB |
Output is correct |
17 |
Correct |
39 ms |
148228 KB |
Output is correct |
18 |
Correct |
54 ms |
148312 KB |
Output is correct |
19 |
Correct |
46 ms |
148316 KB |
Output is correct |
20 |
Correct |
47 ms |
148144 KB |
Output is correct |
21 |
Correct |
44 ms |
148180 KB |
Output is correct |
22 |
Correct |
42 ms |
148400 KB |
Output is correct |
23 |
Correct |
48 ms |
148308 KB |
Output is correct |
24 |
Correct |
38 ms |
148304 KB |
Output is correct |
25 |
Correct |
43 ms |
148304 KB |
Output is correct |
26 |
Correct |
54 ms |
148308 KB |
Output is correct |
27 |
Correct |
44 ms |
148292 KB |
Output is correct |
28 |
Correct |
52 ms |
150356 KB |
Output is correct |
29 |
Correct |
55 ms |
148384 KB |
Output is correct |
30 |
Correct |
46 ms |
148152 KB |
Output is correct |
31 |
Correct |
41 ms |
148260 KB |
Output is correct |
32 |
Correct |
69 ms |
148308 KB |
Output is correct |
33 |
Correct |
64 ms |
148380 KB |
Output is correct |
34 |
Correct |
67 ms |
148120 KB |
Output is correct |
35 |
Correct |
47 ms |
148288 KB |
Output is correct |
36 |
Correct |
44 ms |
148356 KB |
Output is correct |
37 |
Correct |
48 ms |
148200 KB |
Output is correct |
38 |
Correct |
42 ms |
148316 KB |
Output is correct |
39 |
Correct |
43 ms |
148376 KB |
Output is correct |
40 |
Correct |
46 ms |
148560 KB |
Output is correct |
41 |
Correct |
43 ms |
152020 KB |
Output is correct |
42 |
Correct |
42 ms |
149892 KB |
Output is correct |
43 |
Correct |
42 ms |
149588 KB |
Output is correct |
44 |
Correct |
47 ms |
149328 KB |
Output is correct |
45 |
Correct |
45 ms |
149328 KB |
Output is correct |
46 |
Correct |
43 ms |
149340 KB |
Output is correct |
47 |
Correct |
41 ms |
150108 KB |
Output is correct |
48 |
Correct |
44 ms |
149588 KB |
Output is correct |
49 |
Correct |
43 ms |
149720 KB |
Output is correct |
50 |
Correct |
44 ms |
149584 KB |
Output is correct |
51 |
Correct |
48 ms |
150612 KB |
Output is correct |
52 |
Correct |
43 ms |
150612 KB |
Output is correct |
53 |
Correct |
42 ms |
149328 KB |
Output is correct |
54 |
Correct |
60 ms |
154448 KB |
Output is correct |
55 |
Correct |
135 ms |
248992 KB |
Output is correct |
56 |
Correct |
134 ms |
174896 KB |
Output is correct |
57 |
Correct |
129 ms |
168132 KB |
Output is correct |
58 |
Correct |
127 ms |
163288 KB |
Output is correct |
59 |
Correct |
124 ms |
161440 KB |
Output is correct |
60 |
Correct |
121 ms |
160596 KB |
Output is correct |
61 |
Correct |
130 ms |
175216 KB |
Output is correct |
62 |
Correct |
131 ms |
233376 KB |
Output is correct |
63 |
Correct |
132 ms |
171088 KB |
Output is correct |
64 |
Correct |
139 ms |
177492 KB |
Output is correct |
65 |
Correct |
153 ms |
206544 KB |
Output is correct |
66 |
Correct |
154 ms |
206148 KB |
Output is correct |
67 |
Correct |
122 ms |
160848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
148160 KB |
Output is correct |
2 |
Correct |
39 ms |
148308 KB |
Output is correct |
3 |
Correct |
41 ms |
148268 KB |
Output is correct |
4 |
Correct |
37 ms |
148308 KB |
Output is correct |
5 |
Correct |
36 ms |
148304 KB |
Output is correct |
6 |
Correct |
40 ms |
148264 KB |
Output is correct |
7 |
Correct |
40 ms |
148316 KB |
Output is correct |
8 |
Correct |
39 ms |
148312 KB |
Output is correct |
9 |
Correct |
40 ms |
148564 KB |
Output is correct |
10 |
Correct |
40 ms |
148332 KB |
Output is correct |
11 |
Correct |
44 ms |
148308 KB |
Output is correct |
12 |
Correct |
44 ms |
148308 KB |
Output is correct |
13 |
Correct |
400 ms |
508704 KB |
Output is correct |
14 |
Correct |
467 ms |
573152 KB |
Output is correct |
15 |
Correct |
586 ms |
648776 KB |
Output is correct |
16 |
Correct |
698 ms |
733652 KB |
Output is correct |
17 |
Correct |
799 ms |
836396 KB |
Output is correct |
18 |
Correct |
840 ms |
836352 KB |
Output is correct |
19 |
Correct |
926 ms |
836492 KB |
Output is correct |
20 |
Correct |
46 ms |
148160 KB |
Output is correct |
21 |
Correct |
41 ms |
148360 KB |
Output is correct |
22 |
Correct |
39 ms |
148316 KB |
Output is correct |
23 |
Correct |
37 ms |
148136 KB |
Output is correct |
24 |
Correct |
37 ms |
148316 KB |
Output is correct |
25 |
Correct |
45 ms |
148376 KB |
Output is correct |
26 |
Correct |
43 ms |
148256 KB |
Output is correct |
27 |
Correct |
49 ms |
148320 KB |
Output is correct |
28 |
Correct |
47 ms |
148144 KB |
Output is correct |
29 |
Correct |
42 ms |
148316 KB |
Output is correct |
30 |
Correct |
58 ms |
148264 KB |
Output is correct |
31 |
Correct |
55 ms |
148268 KB |
Output is correct |
32 |
Correct |
41 ms |
148304 KB |
Output is correct |
33 |
Correct |
41 ms |
148304 KB |
Output is correct |
34 |
Correct |
39 ms |
148228 KB |
Output is correct |
35 |
Correct |
54 ms |
148312 KB |
Output is correct |
36 |
Correct |
46 ms |
148316 KB |
Output is correct |
37 |
Correct |
47 ms |
148144 KB |
Output is correct |
38 |
Correct |
44 ms |
148180 KB |
Output is correct |
39 |
Correct |
42 ms |
148400 KB |
Output is correct |
40 |
Correct |
48 ms |
148308 KB |
Output is correct |
41 |
Correct |
38 ms |
148304 KB |
Output is correct |
42 |
Correct |
43 ms |
148304 KB |
Output is correct |
43 |
Correct |
54 ms |
148308 KB |
Output is correct |
44 |
Correct |
44 ms |
148292 KB |
Output is correct |
45 |
Correct |
52 ms |
150356 KB |
Output is correct |
46 |
Correct |
55 ms |
148384 KB |
Output is correct |
47 |
Correct |
46 ms |
148152 KB |
Output is correct |
48 |
Correct |
41 ms |
148260 KB |
Output is correct |
49 |
Correct |
69 ms |
148308 KB |
Output is correct |
50 |
Correct |
64 ms |
148380 KB |
Output is correct |
51 |
Correct |
67 ms |
148120 KB |
Output is correct |
52 |
Correct |
47 ms |
148288 KB |
Output is correct |
53 |
Correct |
44 ms |
148356 KB |
Output is correct |
54 |
Correct |
48 ms |
148200 KB |
Output is correct |
55 |
Correct |
42 ms |
148316 KB |
Output is correct |
56 |
Correct |
43 ms |
148376 KB |
Output is correct |
57 |
Correct |
46 ms |
148560 KB |
Output is correct |
58 |
Correct |
43 ms |
152020 KB |
Output is correct |
59 |
Correct |
42 ms |
149892 KB |
Output is correct |
60 |
Correct |
42 ms |
149588 KB |
Output is correct |
61 |
Correct |
47 ms |
149328 KB |
Output is correct |
62 |
Correct |
45 ms |
149328 KB |
Output is correct |
63 |
Correct |
43 ms |
149340 KB |
Output is correct |
64 |
Correct |
41 ms |
150108 KB |
Output is correct |
65 |
Correct |
44 ms |
149588 KB |
Output is correct |
66 |
Correct |
43 ms |
149720 KB |
Output is correct |
67 |
Correct |
44 ms |
149584 KB |
Output is correct |
68 |
Correct |
48 ms |
150612 KB |
Output is correct |
69 |
Correct |
43 ms |
150612 KB |
Output is correct |
70 |
Correct |
42 ms |
149328 KB |
Output is correct |
71 |
Correct |
60 ms |
154448 KB |
Output is correct |
72 |
Correct |
135 ms |
248992 KB |
Output is correct |
73 |
Correct |
134 ms |
174896 KB |
Output is correct |
74 |
Correct |
129 ms |
168132 KB |
Output is correct |
75 |
Correct |
127 ms |
163288 KB |
Output is correct |
76 |
Correct |
124 ms |
161440 KB |
Output is correct |
77 |
Correct |
121 ms |
160596 KB |
Output is correct |
78 |
Correct |
130 ms |
175216 KB |
Output is correct |
79 |
Correct |
131 ms |
233376 KB |
Output is correct |
80 |
Correct |
132 ms |
171088 KB |
Output is correct |
81 |
Correct |
139 ms |
177492 KB |
Output is correct |
82 |
Correct |
153 ms |
206544 KB |
Output is correct |
83 |
Correct |
154 ms |
206148 KB |
Output is correct |
84 |
Correct |
122 ms |
160848 KB |
Output is correct |
85 |
Correct |
301 ms |
213540 KB |
Output is correct |
86 |
Correct |
709 ms |
291268 KB |
Output is correct |
87 |
Correct |
686 ms |
247400 KB |
Output is correct |
88 |
Correct |
652 ms |
216956 KB |
Output is correct |
89 |
Incorrect |
657 ms |
205500 KB |
Output isn't correct |
90 |
Halted |
0 ms |
0 KB |
- |