#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#define dbg(...) printf(__VA_ARGS__);
#define getchar_unlocked getchar
#else
#define dbg(...)
#endif
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789
#define LINF 1023456789123456789
#define all(x) x.begin(), x.end()
#define disc(x) sort(all(x));x.resize(unique(all(x))-x.begin());
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<pll> vll;
mt19937 rng(time(0));
#define mod 1000000007
#define mod2 1000000009
inline int add(int a,int b){
int r=a+b;
while(r>=mod)r-=mod;
while(r<0)r+=mod;
return r;
}
inline int mult(int a,int b){
return (int)(((ll)(a*b))%mod);
}
inline int rd(){
int x=0;
char ch=getchar_unlocked();
while(!(ch&16))ch=getchar();//keep reading while current character is whitespace
while(ch&16){//this will break when ‘\n’ or ‘ ‘ is encountered
x=(x<<3)+(x<<1)+(ch&15);
ch=getchar_unlocked();
}
return x;
}
int n,a,b,c,cnt;
char s[2505];
int hh[2][2505][2505],x[2]={29,31};
int nx[2505][2505];
ll dp[2505][2505];
map<ll,int> mp;
int main(){
sf("%d",&n);
sf(" %s",&s);
sf("%d%d%d",&a,&b,&c);
for(int z=0;z<2;++z){
for(int i=0;i<n;++i){
hh[z][i][i]=s[i]-'a'+1;
for(int j=i+1;j<n;++j){
hh[z][i][j]=((ll)hh[z][i][j-1]*x[z]+s[j]-'a'+1)%mod;
}
}
}
for(int l=1;l<=n;++l){
mp.clear();
for(int i=n-1;i>=0;--i){
int j=i+l;
if(j+l-1<=n-1){
int k=j+l-1;
ll x=(ll)hh[0][j][k]*mod+hh[1][j][k];
mp[x]=j;
}
ll x=(ll)hh[0][i][i+l-1]*mod+hh[1][i][i+l-1];
if(mp.find(x)!=mp.end())nx[i][i+l-1]=mp[x];
else nx[i][i+l-1]=n;
}
}
/*
for(int i=0;i<n;++i){
for(int j=i;j<n;++j){
pf("nx ");
for(int k=i;k<=j;++k)pf("%c",s[k]);
pf(": %d\n",nx[i][j]);
}
}
*/
for(int i=n-1;i>=0;--i){
for(int j=i;j<n;++j){
dp[i][j]=dp[i+1][j]+a;
}
for(int j=i;j<n;++j){
int l=j-i+1;
mnto(dp[i][j],dp[i][j-1]+a);
int u=i,cnt=1;
while(u!=n){
u=nx[u][u+l-1];
++cnt;
int tot=u+l-i;
ll cost=dp[i][j]+b+(ll)cnt*c+(ll)(tot-cnt*l)*a;
mnto(dp[i][u+l-1],cost);
}
}
}
pf("%lld\n",dp[0][n-1]);
}
Compilation message
copypaste3.cpp: In function 'int main()':
copypaste3.cpp:67:8: warning: format '%s' expects argument of type 'char*', but argument 2 has type 'char (*)[2505]' [-Wformat=]
67 | sf(" %s",&s);
| ~^ ~~
| | |
| | char (*)[2505]
| char*
copypaste3.cpp:66:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | sf("%d",&n);
| ^
copypaste3.cpp:67:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | sf(" %s",&s);
| ^
copypaste3.cpp:68:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | sf("%d%d%d",&a,&b,&c);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
1 ms |
312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
392 ms |
73480 KB |
Output is correct |
4 |
Correct |
441 ms |
80872 KB |
Output is correct |
5 |
Correct |
532 ms |
88716 KB |
Output is correct |
6 |
Correct |
657 ms |
97196 KB |
Output is correct |
7 |
Correct |
783 ms |
106656 KB |
Output is correct |
8 |
Correct |
779 ms |
106524 KB |
Output is correct |
9 |
Correct |
800 ms |
106408 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
444 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
828 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
1 ms |
312 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
440 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
832 KB |
Output is correct |
22 |
Correct |
1 ms |
724 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
824 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
724 KB |
Output is correct |
32 |
Correct |
1 ms |
724 KB |
Output is correct |
33 |
Correct |
1 ms |
724 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
312 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
1 ms |
312 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
440 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
832 KB |
Output is correct |
22 |
Correct |
1 ms |
724 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
824 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
724 KB |
Output is correct |
32 |
Correct |
1 ms |
724 KB |
Output is correct |
33 |
Correct |
1 ms |
724 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
312 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
724 KB |
Output is correct |
40 |
Correct |
2 ms |
1728 KB |
Output is correct |
41 |
Correct |
3 ms |
4052 KB |
Output is correct |
42 |
Correct |
4 ms |
4052 KB |
Output is correct |
43 |
Correct |
5 ms |
4052 KB |
Output is correct |
44 |
Correct |
5 ms |
4052 KB |
Output is correct |
45 |
Correct |
5 ms |
4152 KB |
Output is correct |
46 |
Correct |
4 ms |
4052 KB |
Output is correct |
47 |
Correct |
3 ms |
4052 KB |
Output is correct |
48 |
Correct |
4 ms |
4052 KB |
Output is correct |
49 |
Correct |
4 ms |
4052 KB |
Output is correct |
50 |
Correct |
4 ms |
4052 KB |
Output is correct |
51 |
Correct |
4 ms |
4124 KB |
Output is correct |
52 |
Correct |
4 ms |
4052 KB |
Output is correct |
53 |
Correct |
4 ms |
4112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
1 ms |
312 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
440 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
832 KB |
Output is correct |
22 |
Correct |
1 ms |
724 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
824 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
724 KB |
Output is correct |
32 |
Correct |
1 ms |
724 KB |
Output is correct |
33 |
Correct |
1 ms |
724 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
312 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
724 KB |
Output is correct |
40 |
Correct |
2 ms |
1728 KB |
Output is correct |
41 |
Correct |
3 ms |
4052 KB |
Output is correct |
42 |
Correct |
4 ms |
4052 KB |
Output is correct |
43 |
Correct |
5 ms |
4052 KB |
Output is correct |
44 |
Correct |
5 ms |
4052 KB |
Output is correct |
45 |
Correct |
5 ms |
4152 KB |
Output is correct |
46 |
Correct |
4 ms |
4052 KB |
Output is correct |
47 |
Correct |
3 ms |
4052 KB |
Output is correct |
48 |
Correct |
4 ms |
4052 KB |
Output is correct |
49 |
Correct |
4 ms |
4052 KB |
Output is correct |
50 |
Correct |
4 ms |
4052 KB |
Output is correct |
51 |
Correct |
4 ms |
4124 KB |
Output is correct |
52 |
Correct |
4 ms |
4052 KB |
Output is correct |
53 |
Correct |
4 ms |
4112 KB |
Output is correct |
54 |
Correct |
18 ms |
10580 KB |
Output is correct |
55 |
Correct |
93 ms |
31888 KB |
Output is correct |
56 |
Correct |
106 ms |
31944 KB |
Output is correct |
57 |
Correct |
113 ms |
32044 KB |
Output is correct |
58 |
Correct |
116 ms |
31976 KB |
Output is correct |
59 |
Correct |
100 ms |
32048 KB |
Output is correct |
60 |
Correct |
95 ms |
31936 KB |
Output is correct |
61 |
Correct |
97 ms |
31980 KB |
Output is correct |
62 |
Correct |
76 ms |
31936 KB |
Output is correct |
63 |
Correct |
108 ms |
31976 KB |
Output is correct |
64 |
Correct |
100 ms |
32016 KB |
Output is correct |
65 |
Correct |
98 ms |
31976 KB |
Output is correct |
66 |
Correct |
99 ms |
31988 KB |
Output is correct |
67 |
Correct |
119 ms |
31992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
1 ms |
312 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
392 ms |
73480 KB |
Output is correct |
14 |
Correct |
441 ms |
80872 KB |
Output is correct |
15 |
Correct |
532 ms |
88716 KB |
Output is correct |
16 |
Correct |
657 ms |
97196 KB |
Output is correct |
17 |
Correct |
783 ms |
106656 KB |
Output is correct |
18 |
Correct |
779 ms |
106524 KB |
Output is correct |
19 |
Correct |
800 ms |
106408 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
444 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
828 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
440 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
832 KB |
Output is correct |
39 |
Correct |
1 ms |
724 KB |
Output is correct |
40 |
Correct |
1 ms |
724 KB |
Output is correct |
41 |
Correct |
1 ms |
724 KB |
Output is correct |
42 |
Correct |
1 ms |
824 KB |
Output is correct |
43 |
Correct |
1 ms |
724 KB |
Output is correct |
44 |
Correct |
1 ms |
724 KB |
Output is correct |
45 |
Correct |
1 ms |
724 KB |
Output is correct |
46 |
Correct |
1 ms |
724 KB |
Output is correct |
47 |
Correct |
1 ms |
724 KB |
Output is correct |
48 |
Correct |
1 ms |
724 KB |
Output is correct |
49 |
Correct |
1 ms |
724 KB |
Output is correct |
50 |
Correct |
1 ms |
724 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
312 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
468 KB |
Output is correct |
56 |
Correct |
1 ms |
724 KB |
Output is correct |
57 |
Correct |
2 ms |
1728 KB |
Output is correct |
58 |
Correct |
3 ms |
4052 KB |
Output is correct |
59 |
Correct |
4 ms |
4052 KB |
Output is correct |
60 |
Correct |
5 ms |
4052 KB |
Output is correct |
61 |
Correct |
5 ms |
4052 KB |
Output is correct |
62 |
Correct |
5 ms |
4152 KB |
Output is correct |
63 |
Correct |
4 ms |
4052 KB |
Output is correct |
64 |
Correct |
3 ms |
4052 KB |
Output is correct |
65 |
Correct |
4 ms |
4052 KB |
Output is correct |
66 |
Correct |
4 ms |
4052 KB |
Output is correct |
67 |
Correct |
4 ms |
4052 KB |
Output is correct |
68 |
Correct |
4 ms |
4124 KB |
Output is correct |
69 |
Correct |
4 ms |
4052 KB |
Output is correct |
70 |
Correct |
4 ms |
4112 KB |
Output is correct |
71 |
Correct |
18 ms |
10580 KB |
Output is correct |
72 |
Correct |
93 ms |
31888 KB |
Output is correct |
73 |
Correct |
106 ms |
31944 KB |
Output is correct |
74 |
Correct |
113 ms |
32044 KB |
Output is correct |
75 |
Correct |
116 ms |
31976 KB |
Output is correct |
76 |
Correct |
100 ms |
32048 KB |
Output is correct |
77 |
Correct |
95 ms |
31936 KB |
Output is correct |
78 |
Correct |
97 ms |
31980 KB |
Output is correct |
79 |
Correct |
76 ms |
31936 KB |
Output is correct |
80 |
Correct |
108 ms |
31976 KB |
Output is correct |
81 |
Correct |
100 ms |
32016 KB |
Output is correct |
82 |
Correct |
98 ms |
31976 KB |
Output is correct |
83 |
Correct |
99 ms |
31988 KB |
Output is correct |
84 |
Correct |
119 ms |
31992 KB |
Output is correct |
85 |
Correct |
272 ms |
61548 KB |
Output is correct |
86 |
Correct |
712 ms |
106700 KB |
Output is correct |
87 |
Correct |
714 ms |
106616 KB |
Output is correct |
88 |
Correct |
702 ms |
106764 KB |
Output is correct |
89 |
Correct |
707 ms |
106564 KB |
Output is correct |
90 |
Correct |
670 ms |
106724 KB |
Output is correct |
91 |
Correct |
451 ms |
106424 KB |
Output is correct |
92 |
Correct |
457 ms |
106480 KB |
Output is correct |
93 |
Correct |
671 ms |
106696 KB |
Output is correct |
94 |
Correct |
640 ms |
106572 KB |
Output is correct |
95 |
Correct |
668 ms |
106468 KB |
Output is correct |
96 |
Correct |
666 ms |
106708 KB |
Output is correct |
97 |
Correct |
689 ms |
106640 KB |
Output is correct |