#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
const int NMAX = 2501;
const int inf = 1e9 + 1;
const ll linf = 1e18 + 1;
ll mod[] ={1000000007, 1000000009, 998244353};
ll pr[] = {29, 31, 37};
ll po[NMAX][2];
ll h[NMAX][2];
void hashing(string s){
h[0][0] = 0;
h[0][1] = 0;
h[0][2] = 0;
int n = s.size();
for(int i = 1; i <= n; i++){
ll x = s[i - 1] - 'a' + 1;
for(int j = 0; j < 2; j++){
h[i][j] = h[i - 1][j] * pr[j] + x;
h[i][j] %= mod[j];
}
}
}
int get(int l, int r, int ind){
ll x = h[r][ind] - h[l - 1][ind] * po[r - l + 1][ind];
x %= mod[ind];
if (x < 0)
x += mod[ind];
return x;
}
ll dp[NMAX][NMAX];
short adj[NMAX][NMAX];
map<pii, int> mp;
vector <pii> vec[NMAX];
vector <int> v[NMAX];
int main(){
po[0][0] = po[0][1] = 1;
ios_base::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
for(int k = 0; k < 2; k++){
for(int i = 1; i <=n; i++){
po[i][k] = po[i - 1][k] * pr[k];
po[i][k] %= mod[k];
}
}
for(int i = 0; i <= n; i++){
for(int j = 0; j <= n; j++){
dp[i][j] = linf;
}
}
string s; cin >> s;
ll a, b, c;
cin >> a >> b >> c;
hashing(s);
for(int i = 1; i <= n; i++){
for(pii to : vec[i - 1]) mp[to] = i - 1;
for(int x,x1,v,j = i; j <= n; j++){
x = get(i, j, 0);
x1 = get(i, j, 1);
v = mp[{x, x1}];
if(v != 0)
adj[i][j - i + 1] = v - (j - i);
vec[j].pb({x, x1});
}
}
ll cnt[n + 1];
for(ll r = 1; r <= n; r++){
for(int i = 1; i <= n; i++)v[i].clear();
for(int i = 0; i <= n; i++)cnt[i] = 0;
for(int cur,i = 1; i <= r; i++){
cur = r - i + 1;
while(cur){
v[cur].pb(i);
cur = adj[cur][i];
}
}
ll mn = 0;
dp[r][r] = a;
for(int l = r; l >= 1; l--){
ll val;
for(int to, ind = 0; ind < v[l].size(); ind++){
to = v[l][ind];
cnt[to]++;
val = dp[r - to + 1][r] + b;
val += (c - (ll)to * a) * cnt[to];
mn = min(mn, val);
}
dp[l][r] = mn + (ll)(r - l + 1) * a;
dp[l][r] = min(dp[l][r], dp[l][r - 1] + a);
}
}
cout << dp[1][n];
return 0;
}
Compilation message
copypaste3.cpp: In function 'int get(int, int, int)':
copypaste3.cpp:38:3: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
38 | if (x < 0)
| ^~
copypaste3.cpp:40:5: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
40 | return x;
| ^~~~~~
copypaste3.cpp: In function 'int main()':
copypaste3.cpp:95:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for(int to, ind = 0; ind < v[l].size(); ind++){
| ~~~~^~~~~~~~~~~~~
copypaste3.cpp: In function 'void hashing(std::string)':
copypaste3.cpp:24:11: warning: array subscript 2 is above array bounds of 'long long int [2]' [-Warray-bounds]
24 | h[0][2] = 0;
| ~~~~~~^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
623 ms |
60912 KB |
Output is correct |
4 |
Correct |
735 ms |
69240 KB |
Output is correct |
5 |
Correct |
900 ms |
76856 KB |
Output is correct |
6 |
Correct |
1107 ms |
85832 KB |
Output is correct |
7 |
Correct |
1267 ms |
95624 KB |
Output is correct |
8 |
Correct |
1281 ms |
95684 KB |
Output is correct |
9 |
Correct |
1267 ms |
95824 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
468 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 |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
0 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 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 |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
468 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 |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
596 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 |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 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 |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
468 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 |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
596 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 |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
596 KB |
Output is correct |
40 |
Correct |
3 ms |
1364 KB |
Output is correct |
41 |
Correct |
4 ms |
2644 KB |
Output is correct |
42 |
Correct |
11 ms |
3756 KB |
Output is correct |
43 |
Correct |
11 ms |
3796 KB |
Output is correct |
44 |
Correct |
12 ms |
3752 KB |
Output is correct |
45 |
Correct |
11 ms |
3796 KB |
Output is correct |
46 |
Correct |
10 ms |
3776 KB |
Output is correct |
47 |
Correct |
8 ms |
2900 KB |
Output is correct |
48 |
Correct |
10 ms |
3244 KB |
Output is correct |
49 |
Correct |
10 ms |
3448 KB |
Output is correct |
50 |
Correct |
10 ms |
3500 KB |
Output is correct |
51 |
Correct |
9 ms |
3284 KB |
Output is correct |
52 |
Correct |
12 ms |
3284 KB |
Output is correct |
53 |
Correct |
11 ms |
3692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 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 |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
468 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 |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
596 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 |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
596 KB |
Output is correct |
40 |
Correct |
3 ms |
1364 KB |
Output is correct |
41 |
Correct |
4 ms |
2644 KB |
Output is correct |
42 |
Correct |
11 ms |
3756 KB |
Output is correct |
43 |
Correct |
11 ms |
3796 KB |
Output is correct |
44 |
Correct |
12 ms |
3752 KB |
Output is correct |
45 |
Correct |
11 ms |
3796 KB |
Output is correct |
46 |
Correct |
10 ms |
3776 KB |
Output is correct |
47 |
Correct |
8 ms |
2900 KB |
Output is correct |
48 |
Correct |
10 ms |
3244 KB |
Output is correct |
49 |
Correct |
10 ms |
3448 KB |
Output is correct |
50 |
Correct |
10 ms |
3500 KB |
Output is correct |
51 |
Correct |
9 ms |
3284 KB |
Output is correct |
52 |
Correct |
12 ms |
3284 KB |
Output is correct |
53 |
Correct |
11 ms |
3692 KB |
Output is correct |
54 |
Correct |
72 ms |
12828 KB |
Output is correct |
55 |
Correct |
140 ms |
22400 KB |
Output is correct |
56 |
Correct |
783 ms |
52668 KB |
Output is correct |
57 |
Correct |
777 ms |
52712 KB |
Output is correct |
58 |
Correct |
811 ms |
52900 KB |
Output is correct |
59 |
Correct |
853 ms |
52940 KB |
Output is correct |
60 |
Correct |
815 ms |
52956 KB |
Output is correct |
61 |
Correct |
579 ms |
35660 KB |
Output is correct |
62 |
Correct |
188 ms |
22476 KB |
Output is correct |
63 |
Correct |
698 ms |
45364 KB |
Output is correct |
64 |
Correct |
707 ms |
45508 KB |
Output is correct |
65 |
Correct |
576 ms |
37324 KB |
Output is correct |
66 |
Correct |
590 ms |
37340 KB |
Output is correct |
67 |
Correct |
786 ms |
52952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
623 ms |
60912 KB |
Output is correct |
14 |
Correct |
735 ms |
69240 KB |
Output is correct |
15 |
Correct |
900 ms |
76856 KB |
Output is correct |
16 |
Correct |
1107 ms |
85832 KB |
Output is correct |
17 |
Correct |
1267 ms |
95624 KB |
Output is correct |
18 |
Correct |
1281 ms |
95684 KB |
Output is correct |
19 |
Correct |
1267 ms |
95824 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 |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
0 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
596 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 |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
340 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 |
1 ms |
724 KB |
Output is correct |
41 |
Correct |
1 ms |
724 KB |
Output is correct |
42 |
Correct |
1 ms |
596 KB |
Output is correct |
43 |
Correct |
1 ms |
596 KB |
Output is correct |
44 |
Correct |
1 ms |
596 KB |
Output is correct |
45 |
Correct |
1 ms |
596 KB |
Output is correct |
46 |
Correct |
1 ms |
596 KB |
Output is correct |
47 |
Correct |
1 ms |
724 KB |
Output is correct |
48 |
Correct |
1 ms |
596 KB |
Output is correct |
49 |
Correct |
1 ms |
596 KB |
Output is correct |
50 |
Correct |
1 ms |
596 KB |
Output is correct |
51 |
Correct |
0 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
0 ms |
468 KB |
Output is correct |
54 |
Correct |
1 ms |
468 KB |
Output is correct |
55 |
Correct |
1 ms |
468 KB |
Output is correct |
56 |
Correct |
1 ms |
596 KB |
Output is correct |
57 |
Correct |
3 ms |
1364 KB |
Output is correct |
58 |
Correct |
4 ms |
2644 KB |
Output is correct |
59 |
Correct |
11 ms |
3756 KB |
Output is correct |
60 |
Correct |
11 ms |
3796 KB |
Output is correct |
61 |
Correct |
12 ms |
3752 KB |
Output is correct |
62 |
Correct |
11 ms |
3796 KB |
Output is correct |
63 |
Correct |
10 ms |
3776 KB |
Output is correct |
64 |
Correct |
8 ms |
2900 KB |
Output is correct |
65 |
Correct |
10 ms |
3244 KB |
Output is correct |
66 |
Correct |
10 ms |
3448 KB |
Output is correct |
67 |
Correct |
10 ms |
3500 KB |
Output is correct |
68 |
Correct |
9 ms |
3284 KB |
Output is correct |
69 |
Correct |
12 ms |
3284 KB |
Output is correct |
70 |
Correct |
11 ms |
3692 KB |
Output is correct |
71 |
Correct |
72 ms |
12828 KB |
Output is correct |
72 |
Correct |
140 ms |
22400 KB |
Output is correct |
73 |
Correct |
783 ms |
52668 KB |
Output is correct |
74 |
Correct |
777 ms |
52712 KB |
Output is correct |
75 |
Correct |
811 ms |
52900 KB |
Output is correct |
76 |
Correct |
853 ms |
52940 KB |
Output is correct |
77 |
Correct |
815 ms |
52956 KB |
Output is correct |
78 |
Correct |
579 ms |
35660 KB |
Output is correct |
79 |
Correct |
188 ms |
22476 KB |
Output is correct |
80 |
Correct |
698 ms |
45364 KB |
Output is correct |
81 |
Correct |
707 ms |
45508 KB |
Output is correct |
82 |
Correct |
576 ms |
37324 KB |
Output is correct |
83 |
Correct |
590 ms |
37340 KB |
Output is correct |
84 |
Correct |
786 ms |
52952 KB |
Output is correct |
85 |
Correct |
2510 ms |
124240 KB |
Output is correct |
86 |
Execution timed out |
3091 ms |
220388 KB |
Time limit exceeded |
87 |
Halted |
0 ms |
0 KB |
- |