#include<bits/stdc++.h>
#define ii __int128_t
#define ll long long
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ld long double
using namespace std;
const ll maxn=2505;
const long long p1=47, p2=101, p3=197, Mod1=998244353, Mod2=1e9+21, Mod3=1e9+7;
vector <ll> pos[maxn*maxn];
long long Hash1[maxn][maxn], Hash2[maxn][maxn], Hash3[maxn][maxn], check[maxn*maxn];
ll pi[maxn][maxn], idx[maxn][maxn], cnt[maxn][maxn], R[maxn][maxn];
inline ll calc(ll l, ll r, ll q)
{
ll id=idx[l][r], len=r-l+1;
while (1)
{
ll p=upper_bound(pos[id].begin(), pos[id].end(), R[l][r])-pos[id].begin();
if (p>=pos[id].size() || pos[id][p]+len-1>q) break;
cnt[l][r]++, R[l][r]=pos[id][p]+len-1;
}
return cnt[l][r];
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll n; cin >> n;
string s; cin >> s; s='$'+s;
long long A, B, C; cin >> A >> B >> C;
vector <pair<pair<long long, long long>, long long>> num;
for (ll i=1; i<=n; i++)
{
for (ll j=i+1; j<=n; j++)
{
ll k=pi[i][j-1];
while (k && s[i+k]!=s[j])
k=pi[i][k-1];
pi[i][j]=k+(s[i+k]==s[j]);
}
for (ll j=i; j<=n; j++)
{
Hash1[i][j]=(Hash1[i][j-1]*p1+s[j])%Mod1;
Hash2[i][j]=(Hash2[i][j-1]*p2+s[j])%Mod2;
Hash3[i][j]=(Hash3[i][j-1]*p3+s[j])%Mod3;
num.pb({{Hash1[i][j], Hash2[i][j]}, Hash3[i][j]}), R[i][j]=j, cnt[i][j]=1;
}
}
sort(num.begin(), num.end());
num.resize(unique(num.begin(), num.end())-num.begin());
for (ll i=1; i<=n; i++)
for (ll j=i; j<=n; j++)
{
idx[i][j]=lower_bound(num.begin(), num.end(), mp(mp(Hash1[i][j], Hash2[i][j]), Hash3[i][j]))-num.begin();
pos[idx[i][j]].pb(i);
}
for (ll len=1; len<=n; len++)
{
for (ll i=1; i+len<=n+1; i++)
{
ll j=i+len-1, crr=pi[i][j];
if (check[idx[i][j]])
continue;
check[idx[i][j]]=A*len;
if (i<j) check[idx[i][j]]=min({check[idx[i][j]], check[idx[i+1][j]]+A, check[idx[i][j-1]]+A});
for (; crr; crr=pi[i][i+crr-1])
if (crr*2<=len)
{
ll c=calc(i, i+crr-1, j);
long long val=check[idx[i][i+crr-1]];
check[idx[i][j]]=min(check[idx[i][j]], val+B+C*c+A*(len-crr*c));
}
}
}
cout << check[idx[1][n]];
}
Compilation message
copypaste3.cpp: In function 'long long int calc(long long int, long long int, long long int)':
copypaste3.cpp:24:14: 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]
24 | if (p>=pos[id].size() || pos[id][p]+len-1>q) break;
| ~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
152660 KB |
Output is correct |
2 |
Correct |
49 ms |
152912 KB |
Output is correct |
3 |
Correct |
47 ms |
152656 KB |
Output is correct |
4 |
Correct |
48 ms |
152656 KB |
Output is correct |
5 |
Correct |
46 ms |
152828 KB |
Output is correct |
6 |
Correct |
45 ms |
152832 KB |
Output is correct |
7 |
Correct |
47 ms |
152660 KB |
Output is correct |
8 |
Correct |
47 ms |
152664 KB |
Output is correct |
9 |
Correct |
47 ms |
152656 KB |
Output is correct |
10 |
Correct |
46 ms |
152832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
157172 KB |
Output is correct |
2 |
Correct |
48 ms |
152832 KB |
Output is correct |
3 |
Correct |
419 ms |
421504 KB |
Output is correct |
4 |
Correct |
493 ms |
448504 KB |
Output is correct |
5 |
Correct |
585 ms |
485276 KB |
Output is correct |
6 |
Correct |
665 ms |
518664 KB |
Output is correct |
7 |
Correct |
761 ms |
553872 KB |
Output is correct |
8 |
Correct |
768 ms |
553272 KB |
Output is correct |
9 |
Correct |
795 ms |
552020 KB |
Output is correct |
10 |
Correct |
47 ms |
152912 KB |
Output is correct |
11 |
Correct |
47 ms |
152656 KB |
Output is correct |
12 |
Correct |
48 ms |
152724 KB |
Output is correct |
13 |
Correct |
47 ms |
152688 KB |
Output is correct |
14 |
Correct |
46 ms |
152924 KB |
Output is correct |
15 |
Correct |
46 ms |
154972 KB |
Output is correct |
16 |
Correct |
51 ms |
159484 KB |
Output is correct |
17 |
Correct |
47 ms |
152656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
152660 KB |
Output is correct |
2 |
Correct |
49 ms |
152912 KB |
Output is correct |
3 |
Correct |
47 ms |
152656 KB |
Output is correct |
4 |
Correct |
48 ms |
152656 KB |
Output is correct |
5 |
Correct |
46 ms |
152828 KB |
Output is correct |
6 |
Correct |
45 ms |
152832 KB |
Output is correct |
7 |
Correct |
47 ms |
152660 KB |
Output is correct |
8 |
Correct |
47 ms |
152664 KB |
Output is correct |
9 |
Correct |
47 ms |
152656 KB |
Output is correct |
10 |
Correct |
46 ms |
152832 KB |
Output is correct |
11 |
Correct |
47 ms |
154964 KB |
Output is correct |
12 |
Correct |
49 ms |
157020 KB |
Output is correct |
13 |
Correct |
49 ms |
157040 KB |
Output is correct |
14 |
Correct |
48 ms |
152924 KB |
Output is correct |
15 |
Correct |
48 ms |
157004 KB |
Output is correct |
16 |
Correct |
47 ms |
154824 KB |
Output is correct |
17 |
Correct |
50 ms |
152660 KB |
Output is correct |
18 |
Correct |
49 ms |
152656 KB |
Output is correct |
19 |
Correct |
52 ms |
154964 KB |
Output is correct |
20 |
Correct |
47 ms |
152664 KB |
Output is correct |
21 |
Correct |
50 ms |
159368 KB |
Output is correct |
22 |
Correct |
49 ms |
159488 KB |
Output is correct |
23 |
Correct |
50 ms |
159320 KB |
Output is correct |
24 |
Correct |
49 ms |
159412 KB |
Output is correct |
25 |
Correct |
49 ms |
159312 KB |
Output is correct |
26 |
Correct |
48 ms |
159312 KB |
Output is correct |
27 |
Correct |
52 ms |
159528 KB |
Output is correct |
28 |
Correct |
48 ms |
159416 KB |
Output is correct |
29 |
Correct |
52 ms |
159480 KB |
Output is correct |
30 |
Correct |
50 ms |
159320 KB |
Output is correct |
31 |
Correct |
48 ms |
159420 KB |
Output is correct |
32 |
Correct |
50 ms |
159324 KB |
Output is correct |
33 |
Correct |
48 ms |
159312 KB |
Output is correct |
34 |
Correct |
48 ms |
152660 KB |
Output is correct |
35 |
Correct |
50 ms |
152744 KB |
Output is correct |
36 |
Correct |
50 ms |
152656 KB |
Output is correct |
37 |
Correct |
49 ms |
152864 KB |
Output is correct |
38 |
Correct |
49 ms |
154972 KB |
Output is correct |
39 |
Correct |
54 ms |
159352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
152660 KB |
Output is correct |
2 |
Correct |
49 ms |
152912 KB |
Output is correct |
3 |
Correct |
47 ms |
152656 KB |
Output is correct |
4 |
Correct |
48 ms |
152656 KB |
Output is correct |
5 |
Correct |
46 ms |
152828 KB |
Output is correct |
6 |
Correct |
45 ms |
152832 KB |
Output is correct |
7 |
Correct |
47 ms |
152660 KB |
Output is correct |
8 |
Correct |
47 ms |
152664 KB |
Output is correct |
9 |
Correct |
47 ms |
152656 KB |
Output is correct |
10 |
Correct |
46 ms |
152832 KB |
Output is correct |
11 |
Correct |
47 ms |
154964 KB |
Output is correct |
12 |
Correct |
49 ms |
157020 KB |
Output is correct |
13 |
Correct |
49 ms |
157040 KB |
Output is correct |
14 |
Correct |
48 ms |
152924 KB |
Output is correct |
15 |
Correct |
48 ms |
157004 KB |
Output is correct |
16 |
Correct |
47 ms |
154824 KB |
Output is correct |
17 |
Correct |
50 ms |
152660 KB |
Output is correct |
18 |
Correct |
49 ms |
152656 KB |
Output is correct |
19 |
Correct |
52 ms |
154964 KB |
Output is correct |
20 |
Correct |
47 ms |
152664 KB |
Output is correct |
21 |
Correct |
50 ms |
159368 KB |
Output is correct |
22 |
Correct |
49 ms |
159488 KB |
Output is correct |
23 |
Correct |
50 ms |
159320 KB |
Output is correct |
24 |
Correct |
49 ms |
159412 KB |
Output is correct |
25 |
Correct |
49 ms |
159312 KB |
Output is correct |
26 |
Correct |
48 ms |
159312 KB |
Output is correct |
27 |
Correct |
52 ms |
159528 KB |
Output is correct |
28 |
Correct |
48 ms |
159416 KB |
Output is correct |
29 |
Correct |
52 ms |
159480 KB |
Output is correct |
30 |
Correct |
50 ms |
159320 KB |
Output is correct |
31 |
Correct |
48 ms |
159420 KB |
Output is correct |
32 |
Correct |
50 ms |
159324 KB |
Output is correct |
33 |
Correct |
48 ms |
159312 KB |
Output is correct |
34 |
Correct |
48 ms |
152660 KB |
Output is correct |
35 |
Correct |
50 ms |
152744 KB |
Output is correct |
36 |
Correct |
50 ms |
152656 KB |
Output is correct |
37 |
Correct |
49 ms |
152864 KB |
Output is correct |
38 |
Correct |
49 ms |
154972 KB |
Output is correct |
39 |
Correct |
54 ms |
159352 KB |
Output is correct |
40 |
Correct |
52 ms |
160620 KB |
Output is correct |
41 |
Correct |
56 ms |
171216 KB |
Output is correct |
42 |
Correct |
59 ms |
171728 KB |
Output is correct |
43 |
Correct |
61 ms |
171588 KB |
Output is correct |
44 |
Correct |
62 ms |
171732 KB |
Output is correct |
45 |
Correct |
60 ms |
171728 KB |
Output is correct |
46 |
Correct |
58 ms |
171696 KB |
Output is correct |
47 |
Correct |
58 ms |
171212 KB |
Output is correct |
48 |
Correct |
60 ms |
171296 KB |
Output is correct |
49 |
Correct |
60 ms |
171484 KB |
Output is correct |
50 |
Correct |
59 ms |
171384 KB |
Output is correct |
51 |
Correct |
59 ms |
171292 KB |
Output is correct |
52 |
Correct |
61 ms |
171348 KB |
Output is correct |
53 |
Correct |
59 ms |
171736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
152660 KB |
Output is correct |
2 |
Correct |
49 ms |
152912 KB |
Output is correct |
3 |
Correct |
47 ms |
152656 KB |
Output is correct |
4 |
Correct |
48 ms |
152656 KB |
Output is correct |
5 |
Correct |
46 ms |
152828 KB |
Output is correct |
6 |
Correct |
45 ms |
152832 KB |
Output is correct |
7 |
Correct |
47 ms |
152660 KB |
Output is correct |
8 |
Correct |
47 ms |
152664 KB |
Output is correct |
9 |
Correct |
47 ms |
152656 KB |
Output is correct |
10 |
Correct |
46 ms |
152832 KB |
Output is correct |
11 |
Correct |
47 ms |
154964 KB |
Output is correct |
12 |
Correct |
49 ms |
157020 KB |
Output is correct |
13 |
Correct |
49 ms |
157040 KB |
Output is correct |
14 |
Correct |
48 ms |
152924 KB |
Output is correct |
15 |
Correct |
48 ms |
157004 KB |
Output is correct |
16 |
Correct |
47 ms |
154824 KB |
Output is correct |
17 |
Correct |
50 ms |
152660 KB |
Output is correct |
18 |
Correct |
49 ms |
152656 KB |
Output is correct |
19 |
Correct |
52 ms |
154964 KB |
Output is correct |
20 |
Correct |
47 ms |
152664 KB |
Output is correct |
21 |
Correct |
50 ms |
159368 KB |
Output is correct |
22 |
Correct |
49 ms |
159488 KB |
Output is correct |
23 |
Correct |
50 ms |
159320 KB |
Output is correct |
24 |
Correct |
49 ms |
159412 KB |
Output is correct |
25 |
Correct |
49 ms |
159312 KB |
Output is correct |
26 |
Correct |
48 ms |
159312 KB |
Output is correct |
27 |
Correct |
52 ms |
159528 KB |
Output is correct |
28 |
Correct |
48 ms |
159416 KB |
Output is correct |
29 |
Correct |
52 ms |
159480 KB |
Output is correct |
30 |
Correct |
50 ms |
159320 KB |
Output is correct |
31 |
Correct |
48 ms |
159420 KB |
Output is correct |
32 |
Correct |
50 ms |
159324 KB |
Output is correct |
33 |
Correct |
48 ms |
159312 KB |
Output is correct |
34 |
Correct |
48 ms |
152660 KB |
Output is correct |
35 |
Correct |
50 ms |
152744 KB |
Output is correct |
36 |
Correct |
50 ms |
152656 KB |
Output is correct |
37 |
Correct |
49 ms |
152864 KB |
Output is correct |
38 |
Correct |
49 ms |
154972 KB |
Output is correct |
39 |
Correct |
54 ms |
159352 KB |
Output is correct |
40 |
Correct |
52 ms |
160620 KB |
Output is correct |
41 |
Correct |
56 ms |
171216 KB |
Output is correct |
42 |
Correct |
59 ms |
171728 KB |
Output is correct |
43 |
Correct |
61 ms |
171588 KB |
Output is correct |
44 |
Correct |
62 ms |
171732 KB |
Output is correct |
45 |
Correct |
60 ms |
171728 KB |
Output is correct |
46 |
Correct |
58 ms |
171696 KB |
Output is correct |
47 |
Correct |
58 ms |
171212 KB |
Output is correct |
48 |
Correct |
60 ms |
171296 KB |
Output is correct |
49 |
Correct |
60 ms |
171484 KB |
Output is correct |
50 |
Correct |
59 ms |
171384 KB |
Output is correct |
51 |
Correct |
59 ms |
171292 KB |
Output is correct |
52 |
Correct |
61 ms |
171348 KB |
Output is correct |
53 |
Correct |
59 ms |
171736 KB |
Output is correct |
54 |
Correct |
108 ms |
202692 KB |
Output is correct |
55 |
Correct |
160 ms |
274356 KB |
Output is correct |
56 |
Correct |
543 ms |
288428 KB |
Output is correct |
57 |
Correct |
497 ms |
288540 KB |
Output is correct |
58 |
Correct |
457 ms |
288592 KB |
Output is correct |
59 |
Correct |
465 ms |
288640 KB |
Output is correct |
60 |
Correct |
432 ms |
288960 KB |
Output is correct |
61 |
Correct |
340 ms |
278960 KB |
Output is correct |
62 |
Correct |
176 ms |
275012 KB |
Output is correct |
63 |
Correct |
434 ms |
284460 KB |
Output is correct |
64 |
Correct |
391 ms |
284832 KB |
Output is correct |
65 |
Incorrect |
367 ms |
280492 KB |
Output isn't correct |
66 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
152660 KB |
Output is correct |
2 |
Correct |
49 ms |
152912 KB |
Output is correct |
3 |
Correct |
47 ms |
152656 KB |
Output is correct |
4 |
Correct |
48 ms |
152656 KB |
Output is correct |
5 |
Correct |
46 ms |
152828 KB |
Output is correct |
6 |
Correct |
45 ms |
152832 KB |
Output is correct |
7 |
Correct |
47 ms |
152660 KB |
Output is correct |
8 |
Correct |
47 ms |
152664 KB |
Output is correct |
9 |
Correct |
47 ms |
152656 KB |
Output is correct |
10 |
Correct |
46 ms |
152832 KB |
Output is correct |
11 |
Correct |
48 ms |
157172 KB |
Output is correct |
12 |
Correct |
48 ms |
152832 KB |
Output is correct |
13 |
Correct |
419 ms |
421504 KB |
Output is correct |
14 |
Correct |
493 ms |
448504 KB |
Output is correct |
15 |
Correct |
585 ms |
485276 KB |
Output is correct |
16 |
Correct |
665 ms |
518664 KB |
Output is correct |
17 |
Correct |
761 ms |
553872 KB |
Output is correct |
18 |
Correct |
768 ms |
553272 KB |
Output is correct |
19 |
Correct |
795 ms |
552020 KB |
Output is correct |
20 |
Correct |
47 ms |
152912 KB |
Output is correct |
21 |
Correct |
47 ms |
152656 KB |
Output is correct |
22 |
Correct |
48 ms |
152724 KB |
Output is correct |
23 |
Correct |
47 ms |
152688 KB |
Output is correct |
24 |
Correct |
46 ms |
152924 KB |
Output is correct |
25 |
Correct |
46 ms |
154972 KB |
Output is correct |
26 |
Correct |
51 ms |
159484 KB |
Output is correct |
27 |
Correct |
47 ms |
152656 KB |
Output is correct |
28 |
Correct |
47 ms |
154964 KB |
Output is correct |
29 |
Correct |
49 ms |
157020 KB |
Output is correct |
30 |
Correct |
49 ms |
157040 KB |
Output is correct |
31 |
Correct |
48 ms |
152924 KB |
Output is correct |
32 |
Correct |
48 ms |
157004 KB |
Output is correct |
33 |
Correct |
47 ms |
154824 KB |
Output is correct |
34 |
Correct |
50 ms |
152660 KB |
Output is correct |
35 |
Correct |
49 ms |
152656 KB |
Output is correct |
36 |
Correct |
52 ms |
154964 KB |
Output is correct |
37 |
Correct |
47 ms |
152664 KB |
Output is correct |
38 |
Correct |
50 ms |
159368 KB |
Output is correct |
39 |
Correct |
49 ms |
159488 KB |
Output is correct |
40 |
Correct |
50 ms |
159320 KB |
Output is correct |
41 |
Correct |
49 ms |
159412 KB |
Output is correct |
42 |
Correct |
49 ms |
159312 KB |
Output is correct |
43 |
Correct |
48 ms |
159312 KB |
Output is correct |
44 |
Correct |
52 ms |
159528 KB |
Output is correct |
45 |
Correct |
48 ms |
159416 KB |
Output is correct |
46 |
Correct |
52 ms |
159480 KB |
Output is correct |
47 |
Correct |
50 ms |
159320 KB |
Output is correct |
48 |
Correct |
48 ms |
159420 KB |
Output is correct |
49 |
Correct |
50 ms |
159324 KB |
Output is correct |
50 |
Correct |
48 ms |
159312 KB |
Output is correct |
51 |
Correct |
48 ms |
152660 KB |
Output is correct |
52 |
Correct |
50 ms |
152744 KB |
Output is correct |
53 |
Correct |
50 ms |
152656 KB |
Output is correct |
54 |
Correct |
49 ms |
152864 KB |
Output is correct |
55 |
Correct |
49 ms |
154972 KB |
Output is correct |
56 |
Correct |
54 ms |
159352 KB |
Output is correct |
57 |
Correct |
52 ms |
160620 KB |
Output is correct |
58 |
Correct |
56 ms |
171216 KB |
Output is correct |
59 |
Correct |
59 ms |
171728 KB |
Output is correct |
60 |
Correct |
61 ms |
171588 KB |
Output is correct |
61 |
Correct |
62 ms |
171732 KB |
Output is correct |
62 |
Correct |
60 ms |
171728 KB |
Output is correct |
63 |
Correct |
58 ms |
171696 KB |
Output is correct |
64 |
Correct |
58 ms |
171212 KB |
Output is correct |
65 |
Correct |
60 ms |
171296 KB |
Output is correct |
66 |
Correct |
60 ms |
171484 KB |
Output is correct |
67 |
Correct |
59 ms |
171384 KB |
Output is correct |
68 |
Correct |
59 ms |
171292 KB |
Output is correct |
69 |
Correct |
61 ms |
171348 KB |
Output is correct |
70 |
Correct |
59 ms |
171736 KB |
Output is correct |
71 |
Correct |
108 ms |
202692 KB |
Output is correct |
72 |
Correct |
160 ms |
274356 KB |
Output is correct |
73 |
Correct |
543 ms |
288428 KB |
Output is correct |
74 |
Correct |
497 ms |
288540 KB |
Output is correct |
75 |
Correct |
457 ms |
288592 KB |
Output is correct |
76 |
Correct |
465 ms |
288640 KB |
Output is correct |
77 |
Correct |
432 ms |
288960 KB |
Output is correct |
78 |
Correct |
340 ms |
278960 KB |
Output is correct |
79 |
Correct |
176 ms |
275012 KB |
Output is correct |
80 |
Correct |
434 ms |
284460 KB |
Output is correct |
81 |
Correct |
391 ms |
284832 KB |
Output is correct |
82 |
Incorrect |
367 ms |
280492 KB |
Output isn't correct |
83 |
Halted |
0 ms |
0 KB |
- |