#include <bits/stdc++.h>
using namespace std;
struct hashCode {
int n, nrHashes;
vector<int> mod;
vector<vector<int>> p, hashPref;
void init( string s, vector<int> vals, vector<int> _mod ) {
n = s.size() - 1;
nrHashes = vals.size();
mod = _mod;
p.resize( nrHashes );
for ( int h = 0; h < nrHashes; h++ ) {
p[h].resize( n + 1 );
p[h][0] = 1;
for ( int i = 1; i <= n; i++ ) {
p[h][i] = (long long)p[h][i - 1] * vals[h] % mod[h];
}
}
hashPref.resize( nrHashes );
for ( int h = 0; h < nrHashes; h++ ) {
hashPref[h].resize( n + 1 );
hashPref[h][0] = 1;
for ( int i = 1; i <= n; i++ )
hashPref[h][i] = (hashPref[h][i - 1] + (long long)p[h][i - 1] * (s[i] - 'a')) % mod[h];
}
}
vector<int> query( int l, int r ) {
vector<int> ans;
for ( int h = 0; h < nrHashes; h++ )
ans.push_back( (long long)(hashPref[h][r] - hashPref[h][l - 1] + mod[h]) % mod[h] * p[h][n - l] % mod[h] );
return ans;
}
} sir;
const int MAX_N = 2500;
long long dp[MAX_N][MAX_N];
int main() {
int n, a, b, c;
string s;
cin >> n >> s >> a >> b >> c;
s = "#" + s;
for ( int l = 1; l <= n; l++ ) {
for ( int r = l; r <= n; r++ )
dp[l][r] = (long long)a * (r - l + 1) + b;
}
int mod1 = (1e9 + 7), mod2 = (1e9 + 9);
sir.init( s, { 29, 31 }, { mod1, mod2 } );
for ( int len = 1; len <= n; len++ ) {
for ( int l = 1; l <= n - len + 1; l++ ) {
int r = l + len - 1;
dp[l - 1][r] = min( dp[l - 1][r], dp[l][r] + a );
dp[l][r + 1] = min( dp[l][r + 1], dp[l][r] + a );
long long cost = dp[l][r] + b + c;
int k = r + len;
while ( k <= n ) {
if ( sir.query( l, r ) == sir.query( k - len + 1, k ) ) {
cost += c;
dp[l][k] = min( dp[l][k], cost );
k += len;
} else {
cost += a;
k++;
}
}
}
}
cout << dp[1][n] - b << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
440 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
990 ms |
37532 KB |
Output is correct |
4 |
Correct |
1106 ms |
39592 KB |
Output is correct |
5 |
Correct |
1393 ms |
43704 KB |
Output is correct |
6 |
Correct |
1521 ms |
45764 KB |
Output is correct |
7 |
Runtime error |
56 ms |
97064 KB |
Execution killed with signal 11 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
440 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
2492 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2392 KB |
Output is correct |
27 |
Correct |
1 ms |
2588 KB |
Output is correct |
28 |
Correct |
1 ms |
2488 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2500 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
2 ms |
2396 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
2488 KB |
Output is correct |
39 |
Correct |
1 ms |
2488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
440 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
2492 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2392 KB |
Output is correct |
27 |
Correct |
1 ms |
2588 KB |
Output is correct |
28 |
Correct |
1 ms |
2488 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2500 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
2 ms |
2396 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
2488 KB |
Output is correct |
39 |
Correct |
1 ms |
2488 KB |
Output is correct |
40 |
Correct |
5 ms |
2396 KB |
Output is correct |
41 |
Correct |
8 ms |
4444 KB |
Output is correct |
42 |
Correct |
62 ms |
4444 KB |
Output is correct |
43 |
Correct |
70 ms |
4572 KB |
Output is correct |
44 |
Correct |
62 ms |
4568 KB |
Output is correct |
45 |
Correct |
66 ms |
4440 KB |
Output is correct |
46 |
Correct |
62 ms |
4568 KB |
Output is correct |
47 |
Correct |
32 ms |
4444 KB |
Output is correct |
48 |
Correct |
47 ms |
4572 KB |
Output is correct |
49 |
Correct |
52 ms |
4444 KB |
Output is correct |
50 |
Correct |
50 ms |
4444 KB |
Output is correct |
51 |
Correct |
34 ms |
4444 KB |
Output is correct |
52 |
Correct |
35 ms |
4444 KB |
Output is correct |
53 |
Correct |
63 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
440 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
2492 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2392 KB |
Output is correct |
27 |
Correct |
1 ms |
2588 KB |
Output is correct |
28 |
Correct |
1 ms |
2488 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2500 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
2 ms |
2396 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
2488 KB |
Output is correct |
39 |
Correct |
1 ms |
2488 KB |
Output is correct |
40 |
Correct |
5 ms |
2396 KB |
Output is correct |
41 |
Correct |
8 ms |
4444 KB |
Output is correct |
42 |
Correct |
62 ms |
4444 KB |
Output is correct |
43 |
Correct |
70 ms |
4572 KB |
Output is correct |
44 |
Correct |
62 ms |
4568 KB |
Output is correct |
45 |
Correct |
66 ms |
4440 KB |
Output is correct |
46 |
Correct |
62 ms |
4568 KB |
Output is correct |
47 |
Correct |
32 ms |
4444 KB |
Output is correct |
48 |
Correct |
47 ms |
4572 KB |
Output is correct |
49 |
Correct |
52 ms |
4444 KB |
Output is correct |
50 |
Correct |
50 ms |
4444 KB |
Output is correct |
51 |
Correct |
34 ms |
4444 KB |
Output is correct |
52 |
Correct |
35 ms |
4444 KB |
Output is correct |
53 |
Correct |
63 ms |
4444 KB |
Output is correct |
54 |
Correct |
688 ms |
10756 KB |
Output is correct |
55 |
Correct |
278 ms |
21052 KB |
Output is correct |
56 |
Execution timed out |
3069 ms |
20828 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
440 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
990 ms |
37532 KB |
Output is correct |
14 |
Correct |
1106 ms |
39592 KB |
Output is correct |
15 |
Correct |
1393 ms |
43704 KB |
Output is correct |
16 |
Correct |
1521 ms |
45764 KB |
Output is correct |
17 |
Runtime error |
56 ms |
97064 KB |
Execution killed with signal 11 |
18 |
Halted |
0 ms |
0 KB |
- |