#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<vector<ll>,ll> pii;
int n;
string s;
ll dp[2505][2505],nxt[2505],a,b,c,qs[4][2505],mod=1e9+7,hs[4],inv[4],mul[4][2505];
ll power(ll a, ll n) {
ll ans=1;
while (n) {
if (n%2==1) ans=(ans*a)%mod;
a=(a*a)%mod;
n/=2;
}
return ans;
}
vector<ll> hsh(int l, int r) {
vector<ll> res;
for (int x=0; x<3; ++x) res.push_back(((qs[x][r]-qs[x][l-1]+mod)%mod * power(inv[x],l-1))%mod);
return res;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
for (int i=1; i<=2500; ++i) for (int j=1; j<=2500; ++j) dp[i][j]=1e18;
cin>>n>>s>>a>>b>>c;
s=" "+s;
hs[0]=101;
hs[1]=5e6+11;
hs[2]=911;
for (int x=0; x<3; ++x) {
mul[x][0]=1;
inv[x]=power(hs[x],mod-2);
for (int i=1; i<n; ++i) mul[x][i]=(mul[x][i-1]*hs[x])%mod;
for (int i=1; i<=n; ++i) qs[x][i]=(qs[x][i-1]+mul[x][i-1]*(s[i]-'a'))%mod;
}
for (ll siz=1; siz<=n; ++siz) {
//find next
map<vector<ll>,int> bef;
queue<pii> q;
memset(nxt,0,sizeof(nxt));
for (int l=n-siz+1; l>=1; --l) {
int r=l+siz-1;
while (q.size()>=siz) {
bef[q.front().first]=q.front().second;
q.pop();
}
nxt[l]=bef[hsh(l,r)];
q.push(pii(hsh(l,r),l));
}
//find dp
for (ll l=1; l+siz-1<=n; ++l) {
ll r=l+siz-1;
if (siz==1) dp[l][r]=a;
else dp[l][r]=min({dp[l][r],dp[l][r-1]+a,dp[l+1][r]+a});
ll nl=l,nr=r,cnt=0;
while (nl) {
++cnt;
dp[l][nr]=min(dp[l][nr],dp[l][r]+ ((nr-l+1)-siz*cnt)*a+b+c*cnt);
nl=nxt[nl];
nr=nl+siz-1;
}
}
}
cout<<dp[1][n];
}
Compilation message
copypaste3.cpp: In function 'int main()':
copypaste3.cpp:52:28: warning: comparison of integer expressions of different signedness: 'std::queue<std::pair<std::vector<long long int>, long long int> >::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
52 | while (q.size()>=siz) {
| ~~~~~~~~^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
49364 KB |
Output is correct |
2 |
Correct |
21 ms |
49256 KB |
Output is correct |
3 |
Correct |
20 ms |
49344 KB |
Output is correct |
4 |
Correct |
22 ms |
49292 KB |
Output is correct |
5 |
Correct |
25 ms |
49328 KB |
Output is correct |
6 |
Correct |
27 ms |
49312 KB |
Output is correct |
7 |
Correct |
21 ms |
49348 KB |
Output is correct |
8 |
Correct |
21 ms |
49276 KB |
Output is correct |
9 |
Correct |
23 ms |
49336 KB |
Output is correct |
10 |
Correct |
24 ms |
49304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
49324 KB |
Output is correct |
2 |
Correct |
22 ms |
49352 KB |
Output is correct |
3 |
Correct |
1479 ms |
49756 KB |
Output is correct |
4 |
Correct |
1763 ms |
49612 KB |
Output is correct |
5 |
Correct |
2065 ms |
49452 KB |
Output is correct |
6 |
Correct |
2467 ms |
49572 KB |
Output is correct |
7 |
Correct |
2951 ms |
49580 KB |
Output is correct |
8 |
Correct |
2991 ms |
49580 KB |
Output is correct |
9 |
Correct |
2915 ms |
49684 KB |
Output is correct |
10 |
Correct |
22 ms |
49364 KB |
Output is correct |
11 |
Correct |
21 ms |
49336 KB |
Output is correct |
12 |
Correct |
24 ms |
49364 KB |
Output is correct |
13 |
Correct |
22 ms |
49328 KB |
Output is correct |
14 |
Correct |
26 ms |
49352 KB |
Output is correct |
15 |
Correct |
21 ms |
49364 KB |
Output is correct |
16 |
Correct |
21 ms |
49292 KB |
Output is correct |
17 |
Correct |
21 ms |
49348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
49364 KB |
Output is correct |
2 |
Correct |
21 ms |
49256 KB |
Output is correct |
3 |
Correct |
20 ms |
49344 KB |
Output is correct |
4 |
Correct |
22 ms |
49292 KB |
Output is correct |
5 |
Correct |
25 ms |
49328 KB |
Output is correct |
6 |
Correct |
27 ms |
49312 KB |
Output is correct |
7 |
Correct |
21 ms |
49348 KB |
Output is correct |
8 |
Correct |
21 ms |
49276 KB |
Output is correct |
9 |
Correct |
23 ms |
49336 KB |
Output is correct |
10 |
Correct |
24 ms |
49304 KB |
Output is correct |
11 |
Correct |
21 ms |
49352 KB |
Output is correct |
12 |
Correct |
21 ms |
49268 KB |
Output is correct |
13 |
Correct |
21 ms |
49264 KB |
Output is correct |
14 |
Correct |
22 ms |
49340 KB |
Output is correct |
15 |
Correct |
22 ms |
49364 KB |
Output is correct |
16 |
Correct |
24 ms |
49364 KB |
Output is correct |
17 |
Correct |
23 ms |
49308 KB |
Output is correct |
18 |
Correct |
25 ms |
49396 KB |
Output is correct |
19 |
Correct |
21 ms |
49368 KB |
Output is correct |
20 |
Correct |
22 ms |
49364 KB |
Output is correct |
21 |
Correct |
24 ms |
49388 KB |
Output is correct |
22 |
Correct |
22 ms |
49356 KB |
Output is correct |
23 |
Correct |
24 ms |
49372 KB |
Output is correct |
24 |
Correct |
21 ms |
49304 KB |
Output is correct |
25 |
Correct |
25 ms |
49364 KB |
Output is correct |
26 |
Correct |
22 ms |
49356 KB |
Output is correct |
27 |
Correct |
21 ms |
49364 KB |
Output is correct |
28 |
Correct |
25 ms |
49548 KB |
Output is correct |
29 |
Correct |
22 ms |
49340 KB |
Output is correct |
30 |
Correct |
22 ms |
49300 KB |
Output is correct |
31 |
Correct |
21 ms |
49288 KB |
Output is correct |
32 |
Correct |
22 ms |
49352 KB |
Output is correct |
33 |
Correct |
22 ms |
49316 KB |
Output is correct |
34 |
Correct |
22 ms |
49364 KB |
Output is correct |
35 |
Correct |
26 ms |
49364 KB |
Output is correct |
36 |
Correct |
22 ms |
49348 KB |
Output is correct |
37 |
Correct |
22 ms |
49352 KB |
Output is correct |
38 |
Correct |
22 ms |
49332 KB |
Output is correct |
39 |
Correct |
26 ms |
49468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
49364 KB |
Output is correct |
2 |
Correct |
21 ms |
49256 KB |
Output is correct |
3 |
Correct |
20 ms |
49344 KB |
Output is correct |
4 |
Correct |
22 ms |
49292 KB |
Output is correct |
5 |
Correct |
25 ms |
49328 KB |
Output is correct |
6 |
Correct |
27 ms |
49312 KB |
Output is correct |
7 |
Correct |
21 ms |
49348 KB |
Output is correct |
8 |
Correct |
21 ms |
49276 KB |
Output is correct |
9 |
Correct |
23 ms |
49336 KB |
Output is correct |
10 |
Correct |
24 ms |
49304 KB |
Output is correct |
11 |
Correct |
21 ms |
49352 KB |
Output is correct |
12 |
Correct |
21 ms |
49268 KB |
Output is correct |
13 |
Correct |
21 ms |
49264 KB |
Output is correct |
14 |
Correct |
22 ms |
49340 KB |
Output is correct |
15 |
Correct |
22 ms |
49364 KB |
Output is correct |
16 |
Correct |
24 ms |
49364 KB |
Output is correct |
17 |
Correct |
23 ms |
49308 KB |
Output is correct |
18 |
Correct |
25 ms |
49396 KB |
Output is correct |
19 |
Correct |
21 ms |
49368 KB |
Output is correct |
20 |
Correct |
22 ms |
49364 KB |
Output is correct |
21 |
Correct |
24 ms |
49388 KB |
Output is correct |
22 |
Correct |
22 ms |
49356 KB |
Output is correct |
23 |
Correct |
24 ms |
49372 KB |
Output is correct |
24 |
Correct |
21 ms |
49304 KB |
Output is correct |
25 |
Correct |
25 ms |
49364 KB |
Output is correct |
26 |
Correct |
22 ms |
49356 KB |
Output is correct |
27 |
Correct |
21 ms |
49364 KB |
Output is correct |
28 |
Correct |
25 ms |
49548 KB |
Output is correct |
29 |
Correct |
22 ms |
49340 KB |
Output is correct |
30 |
Correct |
22 ms |
49300 KB |
Output is correct |
31 |
Correct |
21 ms |
49288 KB |
Output is correct |
32 |
Correct |
22 ms |
49352 KB |
Output is correct |
33 |
Correct |
22 ms |
49316 KB |
Output is correct |
34 |
Correct |
22 ms |
49364 KB |
Output is correct |
35 |
Correct |
26 ms |
49364 KB |
Output is correct |
36 |
Correct |
22 ms |
49348 KB |
Output is correct |
37 |
Correct |
22 ms |
49352 KB |
Output is correct |
38 |
Correct |
22 ms |
49332 KB |
Output is correct |
39 |
Correct |
26 ms |
49468 KB |
Output is correct |
40 |
Correct |
26 ms |
49392 KB |
Output is correct |
41 |
Correct |
34 ms |
49364 KB |
Output is correct |
42 |
Correct |
36 ms |
49320 KB |
Output is correct |
43 |
Correct |
36 ms |
49416 KB |
Output is correct |
44 |
Correct |
36 ms |
49416 KB |
Output is correct |
45 |
Correct |
36 ms |
49328 KB |
Output is correct |
46 |
Correct |
37 ms |
49356 KB |
Output is correct |
47 |
Correct |
35 ms |
49304 KB |
Output is correct |
48 |
Correct |
36 ms |
49364 KB |
Output is correct |
49 |
Correct |
36 ms |
49364 KB |
Output is correct |
50 |
Correct |
38 ms |
49408 KB |
Output is correct |
51 |
Correct |
34 ms |
49420 KB |
Output is correct |
52 |
Correct |
35 ms |
49300 KB |
Output is correct |
53 |
Correct |
36 ms |
49412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
49364 KB |
Output is correct |
2 |
Correct |
21 ms |
49256 KB |
Output is correct |
3 |
Correct |
20 ms |
49344 KB |
Output is correct |
4 |
Correct |
22 ms |
49292 KB |
Output is correct |
5 |
Correct |
25 ms |
49328 KB |
Output is correct |
6 |
Correct |
27 ms |
49312 KB |
Output is correct |
7 |
Correct |
21 ms |
49348 KB |
Output is correct |
8 |
Correct |
21 ms |
49276 KB |
Output is correct |
9 |
Correct |
23 ms |
49336 KB |
Output is correct |
10 |
Correct |
24 ms |
49304 KB |
Output is correct |
11 |
Correct |
21 ms |
49352 KB |
Output is correct |
12 |
Correct |
21 ms |
49268 KB |
Output is correct |
13 |
Correct |
21 ms |
49264 KB |
Output is correct |
14 |
Correct |
22 ms |
49340 KB |
Output is correct |
15 |
Correct |
22 ms |
49364 KB |
Output is correct |
16 |
Correct |
24 ms |
49364 KB |
Output is correct |
17 |
Correct |
23 ms |
49308 KB |
Output is correct |
18 |
Correct |
25 ms |
49396 KB |
Output is correct |
19 |
Correct |
21 ms |
49368 KB |
Output is correct |
20 |
Correct |
22 ms |
49364 KB |
Output is correct |
21 |
Correct |
24 ms |
49388 KB |
Output is correct |
22 |
Correct |
22 ms |
49356 KB |
Output is correct |
23 |
Correct |
24 ms |
49372 KB |
Output is correct |
24 |
Correct |
21 ms |
49304 KB |
Output is correct |
25 |
Correct |
25 ms |
49364 KB |
Output is correct |
26 |
Correct |
22 ms |
49356 KB |
Output is correct |
27 |
Correct |
21 ms |
49364 KB |
Output is correct |
28 |
Correct |
25 ms |
49548 KB |
Output is correct |
29 |
Correct |
22 ms |
49340 KB |
Output is correct |
30 |
Correct |
22 ms |
49300 KB |
Output is correct |
31 |
Correct |
21 ms |
49288 KB |
Output is correct |
32 |
Correct |
22 ms |
49352 KB |
Output is correct |
33 |
Correct |
22 ms |
49316 KB |
Output is correct |
34 |
Correct |
22 ms |
49364 KB |
Output is correct |
35 |
Correct |
26 ms |
49364 KB |
Output is correct |
36 |
Correct |
22 ms |
49348 KB |
Output is correct |
37 |
Correct |
22 ms |
49352 KB |
Output is correct |
38 |
Correct |
22 ms |
49332 KB |
Output is correct |
39 |
Correct |
26 ms |
49468 KB |
Output is correct |
40 |
Correct |
26 ms |
49392 KB |
Output is correct |
41 |
Correct |
34 ms |
49364 KB |
Output is correct |
42 |
Correct |
36 ms |
49320 KB |
Output is correct |
43 |
Correct |
36 ms |
49416 KB |
Output is correct |
44 |
Correct |
36 ms |
49416 KB |
Output is correct |
45 |
Correct |
36 ms |
49328 KB |
Output is correct |
46 |
Correct |
37 ms |
49356 KB |
Output is correct |
47 |
Correct |
35 ms |
49304 KB |
Output is correct |
48 |
Correct |
36 ms |
49364 KB |
Output is correct |
49 |
Correct |
36 ms |
49364 KB |
Output is correct |
50 |
Correct |
38 ms |
49408 KB |
Output is correct |
51 |
Correct |
34 ms |
49420 KB |
Output is correct |
52 |
Correct |
35 ms |
49300 KB |
Output is correct |
53 |
Correct |
36 ms |
49412 KB |
Output is correct |
54 |
Correct |
113 ms |
49456 KB |
Output is correct |
55 |
Correct |
425 ms |
49464 KB |
Output is correct |
56 |
Correct |
491 ms |
49556 KB |
Output is correct |
57 |
Correct |
493 ms |
49556 KB |
Output is correct |
58 |
Correct |
490 ms |
49564 KB |
Output is correct |
59 |
Correct |
496 ms |
49556 KB |
Output is correct |
60 |
Correct |
505 ms |
49656 KB |
Output is correct |
61 |
Correct |
468 ms |
49580 KB |
Output is correct |
62 |
Correct |
441 ms |
49580 KB |
Output is correct |
63 |
Correct |
484 ms |
49524 KB |
Output is correct |
64 |
Correct |
485 ms |
49488 KB |
Output is correct |
65 |
Correct |
468 ms |
49484 KB |
Output is correct |
66 |
Correct |
469 ms |
49516 KB |
Output is correct |
67 |
Correct |
517 ms |
49560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
49364 KB |
Output is correct |
2 |
Correct |
21 ms |
49256 KB |
Output is correct |
3 |
Correct |
20 ms |
49344 KB |
Output is correct |
4 |
Correct |
22 ms |
49292 KB |
Output is correct |
5 |
Correct |
25 ms |
49328 KB |
Output is correct |
6 |
Correct |
27 ms |
49312 KB |
Output is correct |
7 |
Correct |
21 ms |
49348 KB |
Output is correct |
8 |
Correct |
21 ms |
49276 KB |
Output is correct |
9 |
Correct |
23 ms |
49336 KB |
Output is correct |
10 |
Correct |
24 ms |
49304 KB |
Output is correct |
11 |
Correct |
25 ms |
49324 KB |
Output is correct |
12 |
Correct |
22 ms |
49352 KB |
Output is correct |
13 |
Correct |
1479 ms |
49756 KB |
Output is correct |
14 |
Correct |
1763 ms |
49612 KB |
Output is correct |
15 |
Correct |
2065 ms |
49452 KB |
Output is correct |
16 |
Correct |
2467 ms |
49572 KB |
Output is correct |
17 |
Correct |
2951 ms |
49580 KB |
Output is correct |
18 |
Correct |
2991 ms |
49580 KB |
Output is correct |
19 |
Correct |
2915 ms |
49684 KB |
Output is correct |
20 |
Correct |
22 ms |
49364 KB |
Output is correct |
21 |
Correct |
21 ms |
49336 KB |
Output is correct |
22 |
Correct |
24 ms |
49364 KB |
Output is correct |
23 |
Correct |
22 ms |
49328 KB |
Output is correct |
24 |
Correct |
26 ms |
49352 KB |
Output is correct |
25 |
Correct |
21 ms |
49364 KB |
Output is correct |
26 |
Correct |
21 ms |
49292 KB |
Output is correct |
27 |
Correct |
21 ms |
49348 KB |
Output is correct |
28 |
Correct |
21 ms |
49352 KB |
Output is correct |
29 |
Correct |
21 ms |
49268 KB |
Output is correct |
30 |
Correct |
21 ms |
49264 KB |
Output is correct |
31 |
Correct |
22 ms |
49340 KB |
Output is correct |
32 |
Correct |
22 ms |
49364 KB |
Output is correct |
33 |
Correct |
24 ms |
49364 KB |
Output is correct |
34 |
Correct |
23 ms |
49308 KB |
Output is correct |
35 |
Correct |
25 ms |
49396 KB |
Output is correct |
36 |
Correct |
21 ms |
49368 KB |
Output is correct |
37 |
Correct |
22 ms |
49364 KB |
Output is correct |
38 |
Correct |
24 ms |
49388 KB |
Output is correct |
39 |
Correct |
22 ms |
49356 KB |
Output is correct |
40 |
Correct |
24 ms |
49372 KB |
Output is correct |
41 |
Correct |
21 ms |
49304 KB |
Output is correct |
42 |
Correct |
25 ms |
49364 KB |
Output is correct |
43 |
Correct |
22 ms |
49356 KB |
Output is correct |
44 |
Correct |
21 ms |
49364 KB |
Output is correct |
45 |
Correct |
25 ms |
49548 KB |
Output is correct |
46 |
Correct |
22 ms |
49340 KB |
Output is correct |
47 |
Correct |
22 ms |
49300 KB |
Output is correct |
48 |
Correct |
21 ms |
49288 KB |
Output is correct |
49 |
Correct |
22 ms |
49352 KB |
Output is correct |
50 |
Correct |
22 ms |
49316 KB |
Output is correct |
51 |
Correct |
22 ms |
49364 KB |
Output is correct |
52 |
Correct |
26 ms |
49364 KB |
Output is correct |
53 |
Correct |
22 ms |
49348 KB |
Output is correct |
54 |
Correct |
22 ms |
49352 KB |
Output is correct |
55 |
Correct |
22 ms |
49332 KB |
Output is correct |
56 |
Correct |
26 ms |
49468 KB |
Output is correct |
57 |
Correct |
26 ms |
49392 KB |
Output is correct |
58 |
Correct |
34 ms |
49364 KB |
Output is correct |
59 |
Correct |
36 ms |
49320 KB |
Output is correct |
60 |
Correct |
36 ms |
49416 KB |
Output is correct |
61 |
Correct |
36 ms |
49416 KB |
Output is correct |
62 |
Correct |
36 ms |
49328 KB |
Output is correct |
63 |
Correct |
37 ms |
49356 KB |
Output is correct |
64 |
Correct |
35 ms |
49304 KB |
Output is correct |
65 |
Correct |
36 ms |
49364 KB |
Output is correct |
66 |
Correct |
36 ms |
49364 KB |
Output is correct |
67 |
Correct |
38 ms |
49408 KB |
Output is correct |
68 |
Correct |
34 ms |
49420 KB |
Output is correct |
69 |
Correct |
35 ms |
49300 KB |
Output is correct |
70 |
Correct |
36 ms |
49412 KB |
Output is correct |
71 |
Correct |
113 ms |
49456 KB |
Output is correct |
72 |
Correct |
425 ms |
49464 KB |
Output is correct |
73 |
Correct |
491 ms |
49556 KB |
Output is correct |
74 |
Correct |
493 ms |
49556 KB |
Output is correct |
75 |
Correct |
490 ms |
49564 KB |
Output is correct |
76 |
Correct |
496 ms |
49556 KB |
Output is correct |
77 |
Correct |
505 ms |
49656 KB |
Output is correct |
78 |
Correct |
468 ms |
49580 KB |
Output is correct |
79 |
Correct |
441 ms |
49580 KB |
Output is correct |
80 |
Correct |
484 ms |
49524 KB |
Output is correct |
81 |
Correct |
485 ms |
49488 KB |
Output is correct |
82 |
Correct |
468 ms |
49484 KB |
Output is correct |
83 |
Correct |
469 ms |
49516 KB |
Output is correct |
84 |
Correct |
517 ms |
49560 KB |
Output is correct |
85 |
Correct |
1273 ms |
49672 KB |
Output is correct |
86 |
Execution timed out |
3038 ms |
49812 KB |
Time limit exceeded |
87 |
Halted |
0 ms |
0 KB |
- |