#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define debug(x) cout << "Line " << __LINE__ << ", " << #x << " is " << x << endl
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned long long
#define pll pair<ll, ll>
#define pii pair<ll, ll>
#define ld long double
#define nl '\n'
#define tb '\t'
#define sp ' '
#define sqr(x) (x)*(x)
#define arr3 array<ll, 3>
using namespace std;
const ll MX=2502, MOD1=1000000000000027, MOD2=1000000123, BLOCK=160, INF=1e9+7, LG=62, key1=177013, key2=353707;
const ll INFF=1000000000000000004;
const ld ERR=1e-6, pi=3.14159265358979323846;
ll A, B, C, N, hsh[MX][MX], DP[MX][MX];
string S;
int main(){
fastio;
cin >> N >> S >> A >> B >> C;
if(N==1){
cout << A << nl;
return 0;
}
for(ll i=0; i<=N; i++){
for(ll j=0; j<=N; j++)
DP[i][j]=INFF;
}
for(ll i=0; i<N; i++){
for(ll j=i; j<N; j++){
(hsh[i][j]=hsh[i][j-1]*key1+S[j])%=MOD1;
}
DP[i][i]=A;
}
for(ll len=0; len<N; len++){
vector<pll> sub;
for(ll le=0, ri=le+len; ri<N; le++, ri++){
DP[le][ri]=min({DP[le][ri], DP[le+1][ri]+A, DP[le][ri-1]+A});
}
for(ll i=0; i+len<N; i++){
sub.pb({hsh[i][i+len], i});
}
sort(all(sub));
vector<ll> nxt(N, -1);
for(ll i=0; i+len<N; i++){
ll loc=lower_bound(all(sub), mp(hsh[i][i+len], i+len+1))-sub.begin();
if(loc<sub.size() && sub[loc].fi==hsh[i][i+len])
nxt[i]=sub[loc].se;
}
for(ll le=0; le+len<N; le++){
ll idx=nxt[le], cnt=2, ri;
while(idx!=-1){
ri=idx+len;
DP[le][ri]=min(DP[le][ri], DP[le][le+len]+B+C*cnt+A*(ri-le+1-cnt*(len+1)));
cnt++;
idx=nxt[idx];
}
}
}
cout << DP[0][N-1] << nl;
}
Compilation message
copypaste3.cpp: In function 'int main()':
copypaste3.cpp:57:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | if(loc<sub.size() && sub[loc].fi==hsh[i][i+len])
| ~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
260 ms |
54292 KB |
Output is correct |
4 |
Correct |
278 ms |
62144 KB |
Output is correct |
5 |
Correct |
324 ms |
68716 KB |
Output is correct |
6 |
Correct |
398 ms |
75488 KB |
Output is correct |
7 |
Correct |
455 ms |
82884 KB |
Output is correct |
8 |
Correct |
482 ms |
82812 KB |
Output is correct |
9 |
Correct |
474 ms |
82880 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
324 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 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 |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
496 KB |
Output is correct |
25 |
Correct |
1 ms |
580 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
584 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
324 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 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 |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
496 KB |
Output is correct |
25 |
Correct |
1 ms |
580 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
584 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
324 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
1108 KB |
Output is correct |
41 |
Correct |
3 ms |
2388 KB |
Output is correct |
42 |
Correct |
4 ms |
2376 KB |
Output is correct |
43 |
Correct |
4 ms |
2372 KB |
Output is correct |
44 |
Correct |
4 ms |
2388 KB |
Output is correct |
45 |
Correct |
5 ms |
2376 KB |
Output is correct |
46 |
Correct |
4 ms |
2388 KB |
Output is correct |
47 |
Correct |
4 ms |
2388 KB |
Output is correct |
48 |
Correct |
4 ms |
2388 KB |
Output is correct |
49 |
Correct |
4 ms |
2376 KB |
Output is correct |
50 |
Correct |
4 ms |
2388 KB |
Output is correct |
51 |
Correct |
4 ms |
2388 KB |
Output is correct |
52 |
Correct |
4 ms |
2376 KB |
Output is correct |
53 |
Correct |
4 ms |
2380 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 |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
496 KB |
Output is correct |
25 |
Correct |
1 ms |
580 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
584 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
324 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
1108 KB |
Output is correct |
41 |
Correct |
3 ms |
2388 KB |
Output is correct |
42 |
Correct |
4 ms |
2376 KB |
Output is correct |
43 |
Correct |
4 ms |
2372 KB |
Output is correct |
44 |
Correct |
4 ms |
2388 KB |
Output is correct |
45 |
Correct |
5 ms |
2376 KB |
Output is correct |
46 |
Correct |
4 ms |
2388 KB |
Output is correct |
47 |
Correct |
4 ms |
2388 KB |
Output is correct |
48 |
Correct |
4 ms |
2388 KB |
Output is correct |
49 |
Correct |
4 ms |
2376 KB |
Output is correct |
50 |
Correct |
4 ms |
2388 KB |
Output is correct |
51 |
Correct |
4 ms |
2388 KB |
Output is correct |
52 |
Correct |
4 ms |
2376 KB |
Output is correct |
53 |
Correct |
4 ms |
2380 KB |
Output is correct |
54 |
Correct |
16 ms |
6228 KB |
Output is correct |
55 |
Correct |
80 ms |
20036 KB |
Output is correct |
56 |
Correct |
85 ms |
20052 KB |
Output is correct |
57 |
Correct |
78 ms |
20148 KB |
Output is correct |
58 |
Correct |
75 ms |
20136 KB |
Output is correct |
59 |
Correct |
79 ms |
20136 KB |
Output is correct |
60 |
Correct |
84 ms |
20148 KB |
Output is correct |
61 |
Correct |
109 ms |
20024 KB |
Output is correct |
62 |
Correct |
81 ms |
20132 KB |
Output is correct |
63 |
Correct |
89 ms |
20132 KB |
Output is correct |
64 |
Correct |
84 ms |
20052 KB |
Output is correct |
65 |
Correct |
99 ms |
20140 KB |
Output is correct |
66 |
Correct |
101 ms |
20036 KB |
Output is correct |
67 |
Correct |
77 ms |
20172 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 |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
260 ms |
54292 KB |
Output is correct |
14 |
Correct |
278 ms |
62144 KB |
Output is correct |
15 |
Correct |
324 ms |
68716 KB |
Output is correct |
16 |
Correct |
398 ms |
75488 KB |
Output is correct |
17 |
Correct |
455 ms |
82884 KB |
Output is correct |
18 |
Correct |
482 ms |
82812 KB |
Output is correct |
19 |
Correct |
474 ms |
82880 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
324 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
340 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 |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
496 KB |
Output is correct |
42 |
Correct |
1 ms |
580 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
584 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
1 ms |
468 KB |
Output is correct |
49 |
Correct |
1 ms |
468 KB |
Output is correct |
50 |
Correct |
1 ms |
468 KB |
Output is correct |
51 |
Correct |
1 ms |
324 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
1 ms |
1108 KB |
Output is correct |
58 |
Correct |
3 ms |
2388 KB |
Output is correct |
59 |
Correct |
4 ms |
2376 KB |
Output is correct |
60 |
Correct |
4 ms |
2372 KB |
Output is correct |
61 |
Correct |
4 ms |
2388 KB |
Output is correct |
62 |
Correct |
5 ms |
2376 KB |
Output is correct |
63 |
Correct |
4 ms |
2388 KB |
Output is correct |
64 |
Correct |
4 ms |
2388 KB |
Output is correct |
65 |
Correct |
4 ms |
2388 KB |
Output is correct |
66 |
Correct |
4 ms |
2376 KB |
Output is correct |
67 |
Correct |
4 ms |
2388 KB |
Output is correct |
68 |
Correct |
4 ms |
2388 KB |
Output is correct |
69 |
Correct |
4 ms |
2376 KB |
Output is correct |
70 |
Correct |
4 ms |
2380 KB |
Output is correct |
71 |
Correct |
16 ms |
6228 KB |
Output is correct |
72 |
Correct |
80 ms |
20036 KB |
Output is correct |
73 |
Correct |
85 ms |
20052 KB |
Output is correct |
74 |
Correct |
78 ms |
20148 KB |
Output is correct |
75 |
Correct |
75 ms |
20136 KB |
Output is correct |
76 |
Correct |
79 ms |
20136 KB |
Output is correct |
77 |
Correct |
84 ms |
20148 KB |
Output is correct |
78 |
Correct |
109 ms |
20024 KB |
Output is correct |
79 |
Correct |
81 ms |
20132 KB |
Output is correct |
80 |
Correct |
89 ms |
20132 KB |
Output is correct |
81 |
Correct |
84 ms |
20052 KB |
Output is correct |
82 |
Correct |
99 ms |
20140 KB |
Output is correct |
83 |
Correct |
101 ms |
20036 KB |
Output is correct |
84 |
Correct |
77 ms |
20172 KB |
Output is correct |
85 |
Correct |
204 ms |
42432 KB |
Output is correct |
86 |
Correct |
581 ms |
82764 KB |
Output is correct |
87 |
Correct |
520 ms |
82884 KB |
Output is correct |
88 |
Correct |
534 ms |
82888 KB |
Output is correct |
89 |
Correct |
586 ms |
82884 KB |
Output is correct |
90 |
Correct |
506 ms |
82884 KB |
Output is correct |
91 |
Correct |
617 ms |
82884 KB |
Output is correct |
92 |
Correct |
641 ms |
82900 KB |
Output is correct |
93 |
Correct |
591 ms |
82888 KB |
Output is correct |
94 |
Correct |
578 ms |
82884 KB |
Output is correct |
95 |
Correct |
734 ms |
82884 KB |
Output is correct |
96 |
Correct |
698 ms |
82880 KB |
Output is correct |
97 |
Correct |
558 ms |
82884 KB |
Output is correct |