#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 ] ;
vector < hsh > srt ;
int eq_class[ MAXN ][ MAXN ] ;
vector < pair < int , int > > class_list[ MAXN ] ;
pair < int , int > nxt[ MAXN ][ MAXN ] ;
ll dp[ MAXN ][ 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 ) ;
srt.push_back ( aux[ i ][ j ] ) ;
}
}
auto cmp = [ & ] ( hsh p1 , hsh p2 ) {
for ( auto i : { 0 , 1 } ) {
if ( p1.val[ i ] != p2.val[ i ] ) {
return ( p1.val[ i ] < p2.val[ i ] ) ;
}
}
return false ;
};
sort ( srt.begin ( ) , srt.end ( ) , cmp ) ;
int sz = srt.size ( ) ;
eq_class[ srt[ 0 ].l ][ srt[ 0 ].r ] = 1 ;
int tp = 1 ;
for ( int i = 1 ; i < sz ; ++ i ) {
if ( srt[ i ].val[ 0 ] == srt[ i - 1 ].val[ 0 ] && srt[ i ].val[ 1 ] == srt[ i - 1 ].val[ 1 ] ) {
eq_class[ srt[ i ].l ][ srt[ i ].r ] = tp ;
}
else {
eq_class[ srt[ i ].l ][ srt[ i ].r ] = ++ tp ;
}
}
for ( int i = 1 ; i <= n + 2 ; ++ i ) {
for ( int j = i ; j <= n + 2 ; ++ j ) {
class_list[ eq_class[ i ][ j ] ].push_back ( { i , 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 wh = 1 ; wh <= tp ; ++ wh ) {
int sz = class_list[ wh ].size ( ) ;
int pos = 0 ;
for ( int i = 0 ; i < sz ; ++ i ) {
while ( pos < sz && class_list[ wh ][ i ].second >= class_list[ wh ][ pos ].first ) {
++ pos ;
}
if ( pos < sz ) {
nxt[ class_list[ wh ][ i ].first ][ class_list[ wh ][ i ].second ] = class_list[ wh ][ pos ] ;
}
else {
nxt[ class_list[ wh ][ i ].first ][ class_list[ wh ][ i ].second ] = { -1 , -1 } ;
}
}
}
}
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 |
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 |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
400 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
597 ms |
171436 KB |
Output is correct |
4 |
Correct |
672 ms |
198004 KB |
Output is correct |
5 |
Correct |
831 ms |
225676 KB |
Output is correct |
6 |
Correct |
983 ms |
257468 KB |
Output is correct |
7 |
Correct |
1141 ms |
292956 KB |
Output is correct |
8 |
Correct |
1212 ms |
292956 KB |
Output is correct |
9 |
Correct |
1168 ms |
292912 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 |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
536 KB |
Output is correct |
15 |
Correct |
1 ms |
528 KB |
Output is correct |
16 |
Correct |
1 ms |
912 KB |
Output is correct |
17 |
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 |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
400 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
596 KB |
Output is correct |
12 |
Correct |
0 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
852 KB |
Output is correct |
22 |
Correct |
1 ms |
920 KB |
Output is correct |
23 |
Correct |
1 ms |
852 KB |
Output is correct |
24 |
Correct |
1 ms |
852 KB |
Output is correct |
25 |
Correct |
1 ms |
916 KB |
Output is correct |
26 |
Correct |
1 ms |
852 KB |
Output is correct |
27 |
Correct |
1 ms |
852 KB |
Output is correct |
28 |
Correct |
1 ms |
852 KB |
Output is correct |
29 |
Correct |
1 ms |
852 KB |
Output is correct |
30 |
Correct |
1 ms |
912 KB |
Output is correct |
31 |
Correct |
1 ms |
916 KB |
Output is correct |
32 |
Correct |
1 ms |
852 KB |
Output is correct |
33 |
Correct |
1 ms |
852 KB |
Output is correct |
34 |
Correct |
1 ms |
340 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 |
540 KB |
Output is correct |
38 |
Correct |
1 ms |
532 KB |
Output is correct |
39 |
Correct |
1 ms |
912 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 |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
400 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
596 KB |
Output is correct |
12 |
Correct |
0 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
852 KB |
Output is correct |
22 |
Correct |
1 ms |
920 KB |
Output is correct |
23 |
Correct |
1 ms |
852 KB |
Output is correct |
24 |
Correct |
1 ms |
852 KB |
Output is correct |
25 |
Correct |
1 ms |
916 KB |
Output is correct |
26 |
Correct |
1 ms |
852 KB |
Output is correct |
27 |
Correct |
1 ms |
852 KB |
Output is correct |
28 |
Correct |
1 ms |
852 KB |
Output is correct |
29 |
Correct |
1 ms |
852 KB |
Output is correct |
30 |
Correct |
1 ms |
912 KB |
Output is correct |
31 |
Correct |
1 ms |
916 KB |
Output is correct |
32 |
Correct |
1 ms |
852 KB |
Output is correct |
33 |
Correct |
1 ms |
852 KB |
Output is correct |
34 |
Correct |
1 ms |
340 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 |
540 KB |
Output is correct |
38 |
Correct |
1 ms |
532 KB |
Output is correct |
39 |
Correct |
1 ms |
912 KB |
Output is correct |
40 |
Runtime error |
8 ms |
2516 KB |
Execution killed with signal 11 |
41 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
400 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
596 KB |
Output is correct |
12 |
Correct |
0 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
852 KB |
Output is correct |
22 |
Correct |
1 ms |
920 KB |
Output is correct |
23 |
Correct |
1 ms |
852 KB |
Output is correct |
24 |
Correct |
1 ms |
852 KB |
Output is correct |
25 |
Correct |
1 ms |
916 KB |
Output is correct |
26 |
Correct |
1 ms |
852 KB |
Output is correct |
27 |
Correct |
1 ms |
852 KB |
Output is correct |
28 |
Correct |
1 ms |
852 KB |
Output is correct |
29 |
Correct |
1 ms |
852 KB |
Output is correct |
30 |
Correct |
1 ms |
912 KB |
Output is correct |
31 |
Correct |
1 ms |
916 KB |
Output is correct |
32 |
Correct |
1 ms |
852 KB |
Output is correct |
33 |
Correct |
1 ms |
852 KB |
Output is correct |
34 |
Correct |
1 ms |
340 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 |
540 KB |
Output is correct |
38 |
Correct |
1 ms |
532 KB |
Output is correct |
39 |
Correct |
1 ms |
912 KB |
Output is correct |
40 |
Runtime error |
8 ms |
2516 KB |
Execution killed with signal 11 |
41 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
400 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
597 ms |
171436 KB |
Output is correct |
14 |
Correct |
672 ms |
198004 KB |
Output is correct |
15 |
Correct |
831 ms |
225676 KB |
Output is correct |
16 |
Correct |
983 ms |
257468 KB |
Output is correct |
17 |
Correct |
1141 ms |
292956 KB |
Output is correct |
18 |
Correct |
1212 ms |
292956 KB |
Output is correct |
19 |
Correct |
1168 ms |
292912 KB |
Output is correct |
20 |
Correct |
1 ms |
340 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 |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
536 KB |
Output is correct |
25 |
Correct |
1 ms |
528 KB |
Output is correct |
26 |
Correct |
1 ms |
912 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
596 KB |
Output is correct |
29 |
Correct |
0 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
724 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
596 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
852 KB |
Output is correct |
39 |
Correct |
1 ms |
920 KB |
Output is correct |
40 |
Correct |
1 ms |
852 KB |
Output is correct |
41 |
Correct |
1 ms |
852 KB |
Output is correct |
42 |
Correct |
1 ms |
916 KB |
Output is correct |
43 |
Correct |
1 ms |
852 KB |
Output is correct |
44 |
Correct |
1 ms |
852 KB |
Output is correct |
45 |
Correct |
1 ms |
852 KB |
Output is correct |
46 |
Correct |
1 ms |
852 KB |
Output is correct |
47 |
Correct |
1 ms |
912 KB |
Output is correct |
48 |
Correct |
1 ms |
916 KB |
Output is correct |
49 |
Correct |
1 ms |
852 KB |
Output is correct |
50 |
Correct |
1 ms |
852 KB |
Output is correct |
51 |
Correct |
1 ms |
340 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 |
540 KB |
Output is correct |
55 |
Correct |
1 ms |
532 KB |
Output is correct |
56 |
Correct |
1 ms |
912 KB |
Output is correct |
57 |
Runtime error |
8 ms |
2516 KB |
Execution killed with signal 11 |
58 |
Halted |
0 ms |
0 KB |
- |