#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 ; ++ i ) {
for ( int j = i ; j <= n ; ++ j ) {
class_list[ eq_class[ i ][ j ] ].push_back ( { i , j } ) ;
dp[ i ][ j ] = 1e15 ;
}
}
for ( int i = 1 ; i <= n ; ++ 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 ;
if ( i + 1 <= n ) {
dp[ i ][ j ] = min ( dp[ i ][ j ] , dp[ i + 1 ][ j ] + costA ) ;
}
if ( j - 1 >= 1 ) {
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 ;
}
# |
Verdict |
Execution time |
Memory |
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 |
404 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
559 ms |
171796 KB |
Output is correct |
4 |
Correct |
656 ms |
197332 KB |
Output is correct |
5 |
Correct |
806 ms |
225000 KB |
Output is correct |
6 |
Correct |
970 ms |
257396 KB |
Output is correct |
7 |
Correct |
1190 ms |
293080 KB |
Output is correct |
8 |
Correct |
1142 ms |
293084 KB |
Output is correct |
9 |
Correct |
1132 ms |
292948 KB |
Output is correct |
10 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
404 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 |
528 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 |
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 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
404 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 |
528 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 |
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 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
404 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 |
528 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 |
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 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
404 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 |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
559 ms |
171796 KB |
Output is correct |
14 |
Correct |
656 ms |
197332 KB |
Output is correct |
15 |
Correct |
806 ms |
225000 KB |
Output is correct |
16 |
Correct |
970 ms |
257396 KB |
Output is correct |
17 |
Correct |
1190 ms |
293080 KB |
Output is correct |
18 |
Correct |
1142 ms |
293084 KB |
Output is correct |
19 |
Correct |
1132 ms |
292948 KB |
Output is correct |
20 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |