#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
const ll nx=2505, mod=1e12+9, mul=37;
mt19937 rng(time(0));
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:42: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]
42 | if (pts!=v.size()) pa[i]=pts;
| ~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
148300 KB |
Output is correct |
2 |
Correct |
41 ms |
148308 KB |
Output is correct |
3 |
Correct |
45 ms |
148308 KB |
Output is correct |
4 |
Correct |
41 ms |
148304 KB |
Output is correct |
5 |
Correct |
44 ms |
148304 KB |
Output is correct |
6 |
Correct |
41 ms |
148316 KB |
Output is correct |
7 |
Correct |
44 ms |
148216 KB |
Output is correct |
8 |
Correct |
40 ms |
148308 KB |
Output is correct |
9 |
Correct |
44 ms |
148304 KB |
Output is correct |
10 |
Correct |
42 ms |
148304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
148312 KB |
Output is correct |
2 |
Correct |
45 ms |
148356 KB |
Output is correct |
3 |
Correct |
395 ms |
508640 KB |
Output is correct |
4 |
Correct |
445 ms |
572896 KB |
Output is correct |
5 |
Correct |
579 ms |
647500 KB |
Output is correct |
6 |
Correct |
682 ms |
733832 KB |
Output is correct |
7 |
Correct |
758 ms |
836484 KB |
Output is correct |
8 |
Correct |
752 ms |
836336 KB |
Output is correct |
9 |
Correct |
747 ms |
836484 KB |
Output is correct |
10 |
Correct |
41 ms |
148304 KB |
Output is correct |
11 |
Correct |
41 ms |
148316 KB |
Output is correct |
12 |
Correct |
40 ms |
148236 KB |
Output is correct |
13 |
Correct |
42 ms |
148304 KB |
Output is correct |
14 |
Correct |
42 ms |
148312 KB |
Output is correct |
15 |
Correct |
42 ms |
148304 KB |
Output is correct |
16 |
Correct |
40 ms |
148308 KB |
Output is correct |
17 |
Correct |
44 ms |
148148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
148300 KB |
Output is correct |
2 |
Correct |
41 ms |
148308 KB |
Output is correct |
3 |
Correct |
45 ms |
148308 KB |
Output is correct |
4 |
Correct |
41 ms |
148304 KB |
Output is correct |
5 |
Correct |
44 ms |
148304 KB |
Output is correct |
6 |
Correct |
41 ms |
148316 KB |
Output is correct |
7 |
Correct |
44 ms |
148216 KB |
Output is correct |
8 |
Correct |
40 ms |
148308 KB |
Output is correct |
9 |
Correct |
44 ms |
148304 KB |
Output is correct |
10 |
Correct |
42 ms |
148304 KB |
Output is correct |
11 |
Correct |
44 ms |
148304 KB |
Output is correct |
12 |
Correct |
40 ms |
148316 KB |
Output is correct |
13 |
Correct |
43 ms |
148312 KB |
Output is correct |
14 |
Correct |
47 ms |
148304 KB |
Output is correct |
15 |
Correct |
50 ms |
148304 KB |
Output is correct |
16 |
Correct |
44 ms |
148312 KB |
Output is correct |
17 |
Correct |
43 ms |
148340 KB |
Output is correct |
18 |
Correct |
43 ms |
148308 KB |
Output is correct |
19 |
Correct |
40 ms |
148316 KB |
Output is correct |
20 |
Correct |
42 ms |
148308 KB |
Output is correct |
21 |
Correct |
48 ms |
148304 KB |
Output is correct |
22 |
Correct |
40 ms |
148308 KB |
Output is correct |
23 |
Correct |
45 ms |
148308 KB |
Output is correct |
24 |
Correct |
46 ms |
148384 KB |
Output is correct |
25 |
Correct |
41 ms |
148308 KB |
Output is correct |
26 |
Correct |
45 ms |
148168 KB |
Output is correct |
27 |
Correct |
48 ms |
148304 KB |
Output is correct |
28 |
Correct |
45 ms |
148316 KB |
Output is correct |
29 |
Correct |
45 ms |
148304 KB |
Output is correct |
30 |
Correct |
42 ms |
148356 KB |
Output is correct |
31 |
Correct |
41 ms |
148392 KB |
Output is correct |
32 |
Correct |
42 ms |
148316 KB |
Output is correct |
33 |
Correct |
45 ms |
148308 KB |
Output is correct |
34 |
Correct |
50 ms |
148304 KB |
Output is correct |
35 |
Correct |
43 ms |
148304 KB |
Output is correct |
36 |
Correct |
44 ms |
148308 KB |
Output is correct |
37 |
Correct |
46 ms |
148316 KB |
Output is correct |
38 |
Correct |
42 ms |
148216 KB |
Output is correct |
39 |
Correct |
47 ms |
148308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
148300 KB |
Output is correct |
2 |
Correct |
41 ms |
148308 KB |
Output is correct |
3 |
Correct |
45 ms |
148308 KB |
Output is correct |
4 |
Correct |
41 ms |
148304 KB |
Output is correct |
5 |
Correct |
44 ms |
148304 KB |
Output is correct |
6 |
Correct |
41 ms |
148316 KB |
Output is correct |
7 |
Correct |
44 ms |
148216 KB |
Output is correct |
8 |
Correct |
40 ms |
148308 KB |
Output is correct |
9 |
Correct |
44 ms |
148304 KB |
Output is correct |
10 |
Correct |
42 ms |
148304 KB |
Output is correct |
11 |
Correct |
44 ms |
148304 KB |
Output is correct |
12 |
Correct |
40 ms |
148316 KB |
Output is correct |
13 |
Correct |
43 ms |
148312 KB |
Output is correct |
14 |
Correct |
47 ms |
148304 KB |
Output is correct |
15 |
Correct |
50 ms |
148304 KB |
Output is correct |
16 |
Correct |
44 ms |
148312 KB |
Output is correct |
17 |
Correct |
43 ms |
148340 KB |
Output is correct |
18 |
Correct |
43 ms |
148308 KB |
Output is correct |
19 |
Correct |
40 ms |
148316 KB |
Output is correct |
20 |
Correct |
42 ms |
148308 KB |
Output is correct |
21 |
Correct |
48 ms |
148304 KB |
Output is correct |
22 |
Correct |
40 ms |
148308 KB |
Output is correct |
23 |
Correct |
45 ms |
148308 KB |
Output is correct |
24 |
Correct |
46 ms |
148384 KB |
Output is correct |
25 |
Correct |
41 ms |
148308 KB |
Output is correct |
26 |
Correct |
45 ms |
148168 KB |
Output is correct |
27 |
Correct |
48 ms |
148304 KB |
Output is correct |
28 |
Correct |
45 ms |
148316 KB |
Output is correct |
29 |
Correct |
45 ms |
148304 KB |
Output is correct |
30 |
Correct |
42 ms |
148356 KB |
Output is correct |
31 |
Correct |
41 ms |
148392 KB |
Output is correct |
32 |
Correct |
42 ms |
148316 KB |
Output is correct |
33 |
Correct |
45 ms |
148308 KB |
Output is correct |
34 |
Correct |
50 ms |
148304 KB |
Output is correct |
35 |
Correct |
43 ms |
148304 KB |
Output is correct |
36 |
Correct |
44 ms |
148308 KB |
Output is correct |
37 |
Correct |
46 ms |
148316 KB |
Output is correct |
38 |
Correct |
42 ms |
148216 KB |
Output is correct |
39 |
Correct |
47 ms |
148308 KB |
Output is correct |
40 |
Correct |
45 ms |
148564 KB |
Output is correct |
41 |
Correct |
47 ms |
152016 KB |
Output is correct |
42 |
Correct |
48 ms |
149840 KB |
Output is correct |
43 |
Correct |
46 ms |
149584 KB |
Output is correct |
44 |
Correct |
47 ms |
149328 KB |
Output is correct |
45 |
Correct |
48 ms |
149328 KB |
Output is correct |
46 |
Correct |
50 ms |
149380 KB |
Output is correct |
47 |
Correct |
47 ms |
150096 KB |
Output is correct |
48 |
Correct |
46 ms |
149588 KB |
Output is correct |
49 |
Correct |
49 ms |
149584 KB |
Output is correct |
50 |
Correct |
50 ms |
149544 KB |
Output is correct |
51 |
Correct |
46 ms |
150612 KB |
Output is correct |
52 |
Correct |
48 ms |
150612 KB |
Output is correct |
53 |
Correct |
49 ms |
149332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
148300 KB |
Output is correct |
2 |
Correct |
41 ms |
148308 KB |
Output is correct |
3 |
Correct |
45 ms |
148308 KB |
Output is correct |
4 |
Correct |
41 ms |
148304 KB |
Output is correct |
5 |
Correct |
44 ms |
148304 KB |
Output is correct |
6 |
Correct |
41 ms |
148316 KB |
Output is correct |
7 |
Correct |
44 ms |
148216 KB |
Output is correct |
8 |
Correct |
40 ms |
148308 KB |
Output is correct |
9 |
Correct |
44 ms |
148304 KB |
Output is correct |
10 |
Correct |
42 ms |
148304 KB |
Output is correct |
11 |
Correct |
44 ms |
148304 KB |
Output is correct |
12 |
Correct |
40 ms |
148316 KB |
Output is correct |
13 |
Correct |
43 ms |
148312 KB |
Output is correct |
14 |
Correct |
47 ms |
148304 KB |
Output is correct |
15 |
Correct |
50 ms |
148304 KB |
Output is correct |
16 |
Correct |
44 ms |
148312 KB |
Output is correct |
17 |
Correct |
43 ms |
148340 KB |
Output is correct |
18 |
Correct |
43 ms |
148308 KB |
Output is correct |
19 |
Correct |
40 ms |
148316 KB |
Output is correct |
20 |
Correct |
42 ms |
148308 KB |
Output is correct |
21 |
Correct |
48 ms |
148304 KB |
Output is correct |
22 |
Correct |
40 ms |
148308 KB |
Output is correct |
23 |
Correct |
45 ms |
148308 KB |
Output is correct |
24 |
Correct |
46 ms |
148384 KB |
Output is correct |
25 |
Correct |
41 ms |
148308 KB |
Output is correct |
26 |
Correct |
45 ms |
148168 KB |
Output is correct |
27 |
Correct |
48 ms |
148304 KB |
Output is correct |
28 |
Correct |
45 ms |
148316 KB |
Output is correct |
29 |
Correct |
45 ms |
148304 KB |
Output is correct |
30 |
Correct |
42 ms |
148356 KB |
Output is correct |
31 |
Correct |
41 ms |
148392 KB |
Output is correct |
32 |
Correct |
42 ms |
148316 KB |
Output is correct |
33 |
Correct |
45 ms |
148308 KB |
Output is correct |
34 |
Correct |
50 ms |
148304 KB |
Output is correct |
35 |
Correct |
43 ms |
148304 KB |
Output is correct |
36 |
Correct |
44 ms |
148308 KB |
Output is correct |
37 |
Correct |
46 ms |
148316 KB |
Output is correct |
38 |
Correct |
42 ms |
148216 KB |
Output is correct |
39 |
Correct |
47 ms |
148308 KB |
Output is correct |
40 |
Correct |
45 ms |
148564 KB |
Output is correct |
41 |
Correct |
47 ms |
152016 KB |
Output is correct |
42 |
Correct |
48 ms |
149840 KB |
Output is correct |
43 |
Correct |
46 ms |
149584 KB |
Output is correct |
44 |
Correct |
47 ms |
149328 KB |
Output is correct |
45 |
Correct |
48 ms |
149328 KB |
Output is correct |
46 |
Correct |
50 ms |
149380 KB |
Output is correct |
47 |
Correct |
47 ms |
150096 KB |
Output is correct |
48 |
Correct |
46 ms |
149588 KB |
Output is correct |
49 |
Correct |
49 ms |
149584 KB |
Output is correct |
50 |
Correct |
50 ms |
149544 KB |
Output is correct |
51 |
Correct |
46 ms |
150612 KB |
Output is correct |
52 |
Correct |
48 ms |
150612 KB |
Output is correct |
53 |
Correct |
49 ms |
149332 KB |
Output is correct |
54 |
Correct |
61 ms |
154448 KB |
Output is correct |
55 |
Correct |
135 ms |
248964 KB |
Output is correct |
56 |
Correct |
143 ms |
174904 KB |
Output is correct |
57 |
Correct |
152 ms |
167884 KB |
Output is correct |
58 |
Correct |
149 ms |
163284 KB |
Output is correct |
59 |
Correct |
126 ms |
161232 KB |
Output is correct |
60 |
Correct |
129 ms |
160736 KB |
Output is correct |
61 |
Correct |
128 ms |
175184 KB |
Output is correct |
62 |
Correct |
118 ms |
233496 KB |
Output is correct |
63 |
Correct |
132 ms |
171088 KB |
Output is correct |
64 |
Correct |
152 ms |
177488 KB |
Output is correct |
65 |
Correct |
156 ms |
206544 KB |
Output is correct |
66 |
Correct |
151 ms |
206092 KB |
Output is correct |
67 |
Correct |
132 ms |
160536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
148300 KB |
Output is correct |
2 |
Correct |
41 ms |
148308 KB |
Output is correct |
3 |
Correct |
45 ms |
148308 KB |
Output is correct |
4 |
Correct |
41 ms |
148304 KB |
Output is correct |
5 |
Correct |
44 ms |
148304 KB |
Output is correct |
6 |
Correct |
41 ms |
148316 KB |
Output is correct |
7 |
Correct |
44 ms |
148216 KB |
Output is correct |
8 |
Correct |
40 ms |
148308 KB |
Output is correct |
9 |
Correct |
44 ms |
148304 KB |
Output is correct |
10 |
Correct |
42 ms |
148304 KB |
Output is correct |
11 |
Correct |
45 ms |
148312 KB |
Output is correct |
12 |
Correct |
45 ms |
148356 KB |
Output is correct |
13 |
Correct |
395 ms |
508640 KB |
Output is correct |
14 |
Correct |
445 ms |
572896 KB |
Output is correct |
15 |
Correct |
579 ms |
647500 KB |
Output is correct |
16 |
Correct |
682 ms |
733832 KB |
Output is correct |
17 |
Correct |
758 ms |
836484 KB |
Output is correct |
18 |
Correct |
752 ms |
836336 KB |
Output is correct |
19 |
Correct |
747 ms |
836484 KB |
Output is correct |
20 |
Correct |
41 ms |
148304 KB |
Output is correct |
21 |
Correct |
41 ms |
148316 KB |
Output is correct |
22 |
Correct |
40 ms |
148236 KB |
Output is correct |
23 |
Correct |
42 ms |
148304 KB |
Output is correct |
24 |
Correct |
42 ms |
148312 KB |
Output is correct |
25 |
Correct |
42 ms |
148304 KB |
Output is correct |
26 |
Correct |
40 ms |
148308 KB |
Output is correct |
27 |
Correct |
44 ms |
148148 KB |
Output is correct |
28 |
Correct |
44 ms |
148304 KB |
Output is correct |
29 |
Correct |
40 ms |
148316 KB |
Output is correct |
30 |
Correct |
43 ms |
148312 KB |
Output is correct |
31 |
Correct |
47 ms |
148304 KB |
Output is correct |
32 |
Correct |
50 ms |
148304 KB |
Output is correct |
33 |
Correct |
44 ms |
148312 KB |
Output is correct |
34 |
Correct |
43 ms |
148340 KB |
Output is correct |
35 |
Correct |
43 ms |
148308 KB |
Output is correct |
36 |
Correct |
40 ms |
148316 KB |
Output is correct |
37 |
Correct |
42 ms |
148308 KB |
Output is correct |
38 |
Correct |
48 ms |
148304 KB |
Output is correct |
39 |
Correct |
40 ms |
148308 KB |
Output is correct |
40 |
Correct |
45 ms |
148308 KB |
Output is correct |
41 |
Correct |
46 ms |
148384 KB |
Output is correct |
42 |
Correct |
41 ms |
148308 KB |
Output is correct |
43 |
Correct |
45 ms |
148168 KB |
Output is correct |
44 |
Correct |
48 ms |
148304 KB |
Output is correct |
45 |
Correct |
45 ms |
148316 KB |
Output is correct |
46 |
Correct |
45 ms |
148304 KB |
Output is correct |
47 |
Correct |
42 ms |
148356 KB |
Output is correct |
48 |
Correct |
41 ms |
148392 KB |
Output is correct |
49 |
Correct |
42 ms |
148316 KB |
Output is correct |
50 |
Correct |
45 ms |
148308 KB |
Output is correct |
51 |
Correct |
50 ms |
148304 KB |
Output is correct |
52 |
Correct |
43 ms |
148304 KB |
Output is correct |
53 |
Correct |
44 ms |
148308 KB |
Output is correct |
54 |
Correct |
46 ms |
148316 KB |
Output is correct |
55 |
Correct |
42 ms |
148216 KB |
Output is correct |
56 |
Correct |
47 ms |
148308 KB |
Output is correct |
57 |
Correct |
45 ms |
148564 KB |
Output is correct |
58 |
Correct |
47 ms |
152016 KB |
Output is correct |
59 |
Correct |
48 ms |
149840 KB |
Output is correct |
60 |
Correct |
46 ms |
149584 KB |
Output is correct |
61 |
Correct |
47 ms |
149328 KB |
Output is correct |
62 |
Correct |
48 ms |
149328 KB |
Output is correct |
63 |
Correct |
50 ms |
149380 KB |
Output is correct |
64 |
Correct |
47 ms |
150096 KB |
Output is correct |
65 |
Correct |
46 ms |
149588 KB |
Output is correct |
66 |
Correct |
49 ms |
149584 KB |
Output is correct |
67 |
Correct |
50 ms |
149544 KB |
Output is correct |
68 |
Correct |
46 ms |
150612 KB |
Output is correct |
69 |
Correct |
48 ms |
150612 KB |
Output is correct |
70 |
Correct |
49 ms |
149332 KB |
Output is correct |
71 |
Correct |
61 ms |
154448 KB |
Output is correct |
72 |
Correct |
135 ms |
248964 KB |
Output is correct |
73 |
Correct |
143 ms |
174904 KB |
Output is correct |
74 |
Correct |
152 ms |
167884 KB |
Output is correct |
75 |
Correct |
149 ms |
163284 KB |
Output is correct |
76 |
Correct |
126 ms |
161232 KB |
Output is correct |
77 |
Correct |
129 ms |
160736 KB |
Output is correct |
78 |
Correct |
128 ms |
175184 KB |
Output is correct |
79 |
Correct |
118 ms |
233496 KB |
Output is correct |
80 |
Correct |
132 ms |
171088 KB |
Output is correct |
81 |
Correct |
152 ms |
177488 KB |
Output is correct |
82 |
Correct |
156 ms |
206544 KB |
Output is correct |
83 |
Correct |
151 ms |
206092 KB |
Output is correct |
84 |
Correct |
132 ms |
160536 KB |
Output is correct |
85 |
Correct |
308 ms |
213200 KB |
Output is correct |
86 |
Correct |
717 ms |
291268 KB |
Output is correct |
87 |
Correct |
673 ms |
247400 KB |
Output is correct |
88 |
Correct |
634 ms |
217124 KB |
Output is correct |
89 |
Correct |
626 ms |
205380 KB |
Output is correct |
90 |
Correct |
625 ms |
201288 KB |
Output is correct |
91 |
Correct |
499 ms |
514240 KB |
Output is correct |
92 |
Correct |
524 ms |
619836 KB |
Output is correct |
93 |
Correct |
727 ms |
269620 KB |
Output is correct |
94 |
Correct |
749 ms |
299076 KB |
Output is correct |
95 |
Correct |
857 ms |
557996 KB |
Output is correct |
96 |
Correct |
854 ms |
556956 KB |
Output is correct |
97 |
Correct |
649 ms |
201384 KB |
Output is correct |