#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
const int MAXN = 2507 ;
const int BASE = 27 ;
ll MOD[ 2 ] = { 1000000007 , 998244353 } ;
int n ;
string a ;
ll costA , costB , costC ;
struct hsh {
int l , r ;
ll val[ 2 ] ;
void advance ( int nw ) {
for ( auto i : { 0 , 1 } ) {
val[ i ] = ( val[ i ] * BASE + nw ) % MOD[ i ] ;
}
}
};
hsh aux[ MAXN ][ MAXN ] ;
pair < int , int > nxt[ MAXN ][ MAXN ] ;
ll dp[ MAXN ][ MAXN ] ;
map < pair < ll , ll > , int > w ;
set < int > s[ MAXN ] ;
void input ( ) {
cin >> n >> a ;
cin >> costA >> costB >> costC ;
for ( int i = 1 ; i <= n ; ++ i ) {
for ( int j = i ; j <= n ; ++ j ) {
aux[ i ][ j ] = aux[ i ][ j - 1 ] ;
aux[ i ][ j ].l = i , aux[ i ][ j ].r = j ;
aux[ i ][ j ].advance ( a[ j - 1 ] - 'a' + 1 ) ;
}
}
for ( int i = 1 ; i <= n + 2 ; ++ i ) {
for ( int j = i ; j <= n + 2 ; ++ j ) {
dp[ i ][ j ] = 1e15 ;
}
}
for ( int i = 1 ; i <= n + 2 ; ++ i ) {
for ( int j = 0 ; j < i ; ++ j ) {
dp[ i ][ j ] = costB ;
}
}
for ( int len = 0 ; len < n ; ++ len ) {
w.clear ( ) ;
for ( int i = 1 ; i + len <= n ; ++ i ) {
int j = i + len ;
pair < ll , ll > hh = { aux[ i ][ j ].val[ 0 ] , aux[ i ][ j ].val[ 1 ] } ;
if ( w[ hh ] == 0 ) { w[ hh ] = i ; }
s[ w[ hh ] ].insert ( i ) ;
}
for ( int i = 1 ; i + len <= n ; ++ i ) {
int j = i + len ;
pair < ll , ll > hh = { aux[ i ][ j ].val[ 0 ] , aux[ i ][ j ].val[ 1 ] } ;
if ( w[ hh ] != i ) { continue ; }
int id = w[ hh ] ;
auto pos = s[ id ].begin ( ) ;
for ( auto x : s[ id ] ) {
while ( pos != s[ id ].end ( ) && x + len >= *pos ) {
++ pos ;
}
if ( pos != s[ id ].end ( ) ) {
nxt[ x ][ x + len ] = { *pos , *pos + len } ;
}
}
s[ id ].clear ( ) ;
}
}
}
void solve ( ) {
for ( int len = 0 ; len < n ; ++ len ) {
for ( int i = 1 ; i + len <= n ; ++ i ) {
int j = i + len ;
dp[ i ][ j ] = min ( dp[ i ][ j ] , dp[ i + 1 ][ j ] + costA ) ;
dp[ i ][ j ] = min ( dp[ i ][ j ] , dp[ i ][ j - 1 ] + costA ) ;
pair < int , int > act ;
act = { i , j } ;
int coef = 1 ;
while ( 1 ) {
act = nxt[ act.first ][ act.second ] ;
if ( act.first <= 0 ) { break ; }
++ coef ;
int lft = ( act.second - i + 1 ) - coef * ( j - i + 1 ) ;
dp[ i ][ act.second ] = min ( dp[ i ][ act.second ] , dp[ i ][ j ] + coef * costC + lft * costA + costB ) ;
}
}
}
cout << dp[ 1 ][ n ] - costB << "\n" ;
}
int main ( ) {
ios_base :: sync_with_stdio ( false ) ;
cin.tie ( NULL ) ;
int t ;
/// cin >> t ;
t = 1 ;
while ( t -- ) {
input ( ) ;
solve ( ) ;
}
return 0 ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
444 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
448 KB |
Output is correct |
3 |
Correct |
503 ms |
94640 KB |
Output is correct |
4 |
Correct |
576 ms |
108696 KB |
Output is correct |
5 |
Correct |
697 ms |
122444 KB |
Output is correct |
6 |
Correct |
869 ms |
137736 KB |
Output is correct |
7 |
Correct |
1010 ms |
155076 KB |
Output is correct |
8 |
Correct |
1010 ms |
155216 KB |
Output is correct |
9 |
Correct |
1018 ms |
155116 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
444 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
576 KB |
Output is correct |
12 |
Correct |
1 ms |
568 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
444 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
724 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 |
828 KB |
Output is correct |
25 |
Correct |
1 ms |
724 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 |
828 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 |
0 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
596 KB |
Output is correct |
39 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
444 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
576 KB |
Output is correct |
12 |
Correct |
1 ms |
568 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
444 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
724 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 |
828 KB |
Output is correct |
25 |
Correct |
1 ms |
724 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 |
828 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 |
0 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
596 KB |
Output is correct |
39 |
Correct |
1 ms |
724 KB |
Output is correct |
40 |
Correct |
2 ms |
1620 KB |
Output is correct |
41 |
Correct |
5 ms |
3668 KB |
Output is correct |
42 |
Correct |
8 ms |
3668 KB |
Output is correct |
43 |
Correct |
8 ms |
3708 KB |
Output is correct |
44 |
Correct |
8 ms |
3668 KB |
Output is correct |
45 |
Correct |
8 ms |
3668 KB |
Output is correct |
46 |
Correct |
8 ms |
3540 KB |
Output is correct |
47 |
Correct |
6 ms |
3668 KB |
Output is correct |
48 |
Correct |
8 ms |
3540 KB |
Output is correct |
49 |
Correct |
7 ms |
3668 KB |
Output is correct |
50 |
Correct |
8 ms |
3540 KB |
Output is correct |
51 |
Correct |
7 ms |
3640 KB |
Output is correct |
52 |
Correct |
7 ms |
3668 KB |
Output is correct |
53 |
Correct |
8 ms |
3540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
444 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
576 KB |
Output is correct |
12 |
Correct |
1 ms |
568 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
444 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
724 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 |
828 KB |
Output is correct |
25 |
Correct |
1 ms |
724 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 |
828 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 |
0 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
596 KB |
Output is correct |
39 |
Correct |
1 ms |
724 KB |
Output is correct |
40 |
Correct |
2 ms |
1620 KB |
Output is correct |
41 |
Correct |
5 ms |
3668 KB |
Output is correct |
42 |
Correct |
8 ms |
3668 KB |
Output is correct |
43 |
Correct |
8 ms |
3708 KB |
Output is correct |
44 |
Correct |
8 ms |
3668 KB |
Output is correct |
45 |
Correct |
8 ms |
3668 KB |
Output is correct |
46 |
Correct |
8 ms |
3540 KB |
Output is correct |
47 |
Correct |
6 ms |
3668 KB |
Output is correct |
48 |
Correct |
8 ms |
3540 KB |
Output is correct |
49 |
Correct |
7 ms |
3668 KB |
Output is correct |
50 |
Correct |
8 ms |
3540 KB |
Output is correct |
51 |
Correct |
7 ms |
3640 KB |
Output is correct |
52 |
Correct |
7 ms |
3668 KB |
Output is correct |
53 |
Correct |
8 ms |
3540 KB |
Output is correct |
54 |
Correct |
41 ms |
9812 KB |
Output is correct |
55 |
Correct |
114 ms |
34068 KB |
Output is correct |
56 |
Correct |
233 ms |
32372 KB |
Output is correct |
57 |
Correct |
232 ms |
32328 KB |
Output is correct |
58 |
Correct |
228 ms |
32332 KB |
Output is correct |
59 |
Correct |
229 ms |
32256 KB |
Output is correct |
60 |
Correct |
228 ms |
32212 KB |
Output is correct |
61 |
Correct |
171 ms |
33636 KB |
Output is correct |
62 |
Correct |
109 ms |
34104 KB |
Output is correct |
63 |
Correct |
205 ms |
33224 KB |
Output is correct |
64 |
Correct |
208 ms |
33132 KB |
Output is correct |
65 |
Correct |
181 ms |
33872 KB |
Output is correct |
66 |
Correct |
182 ms |
33868 KB |
Output is correct |
67 |
Correct |
233 ms |
32200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
444 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
448 KB |
Output is correct |
13 |
Correct |
503 ms |
94640 KB |
Output is correct |
14 |
Correct |
576 ms |
108696 KB |
Output is correct |
15 |
Correct |
697 ms |
122444 KB |
Output is correct |
16 |
Correct |
869 ms |
137736 KB |
Output is correct |
17 |
Correct |
1010 ms |
155076 KB |
Output is correct |
18 |
Correct |
1010 ms |
155216 KB |
Output is correct |
19 |
Correct |
1018 ms |
155116 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
0 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
576 KB |
Output is correct |
29 |
Correct |
1 ms |
568 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
444 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
724 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 |
828 KB |
Output is correct |
42 |
Correct |
1 ms |
724 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 |
828 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 |
0 ms |
468 KB |
Output is correct |
52 |
Correct |
1 ms |
468 KB |
Output is correct |
53 |
Correct |
1 ms |
468 KB |
Output is correct |
54 |
Correct |
1 ms |
468 KB |
Output is correct |
55 |
Correct |
1 ms |
596 KB |
Output is correct |
56 |
Correct |
1 ms |
724 KB |
Output is correct |
57 |
Correct |
2 ms |
1620 KB |
Output is correct |
58 |
Correct |
5 ms |
3668 KB |
Output is correct |
59 |
Correct |
8 ms |
3668 KB |
Output is correct |
60 |
Correct |
8 ms |
3708 KB |
Output is correct |
61 |
Correct |
8 ms |
3668 KB |
Output is correct |
62 |
Correct |
8 ms |
3668 KB |
Output is correct |
63 |
Correct |
8 ms |
3540 KB |
Output is correct |
64 |
Correct |
6 ms |
3668 KB |
Output is correct |
65 |
Correct |
8 ms |
3540 KB |
Output is correct |
66 |
Correct |
7 ms |
3668 KB |
Output is correct |
67 |
Correct |
8 ms |
3540 KB |
Output is correct |
68 |
Correct |
7 ms |
3640 KB |
Output is correct |
69 |
Correct |
7 ms |
3668 KB |
Output is correct |
70 |
Correct |
8 ms |
3540 KB |
Output is correct |
71 |
Correct |
41 ms |
9812 KB |
Output is correct |
72 |
Correct |
114 ms |
34068 KB |
Output is correct |
73 |
Correct |
233 ms |
32372 KB |
Output is correct |
74 |
Correct |
232 ms |
32328 KB |
Output is correct |
75 |
Correct |
228 ms |
32332 KB |
Output is correct |
76 |
Correct |
229 ms |
32256 KB |
Output is correct |
77 |
Correct |
228 ms |
32212 KB |
Output is correct |
78 |
Correct |
171 ms |
33636 KB |
Output is correct |
79 |
Correct |
109 ms |
34104 KB |
Output is correct |
80 |
Correct |
205 ms |
33224 KB |
Output is correct |
81 |
Correct |
208 ms |
33132 KB |
Output is correct |
82 |
Correct |
181 ms |
33872 KB |
Output is correct |
83 |
Correct |
182 ms |
33868 KB |
Output is correct |
84 |
Correct |
233 ms |
32200 KB |
Output is correct |
85 |
Correct |
631 ms |
68872 KB |
Output is correct |
86 |
Correct |
1732 ms |
143240 KB |
Output is correct |
87 |
Correct |
1728 ms |
143160 KB |
Output is correct |
88 |
Correct |
1692 ms |
143124 KB |
Output is correct |
89 |
Correct |
1689 ms |
143176 KB |
Output is correct |
90 |
Correct |
1704 ms |
143052 KB |
Output is correct |
91 |
Correct |
888 ms |
154724 KB |
Output is correct |
92 |
Correct |
886 ms |
155084 KB |
Output is correct |
93 |
Correct |
1486 ms |
150328 KB |
Output is correct |
94 |
Correct |
1520 ms |
148940 KB |
Output is correct |
95 |
Correct |
1423 ms |
153708 KB |
Output is correct |
96 |
Correct |
1423 ms |
153580 KB |
Output is correct |
97 |
Correct |
1689 ms |
143088 KB |
Output is correct |