#include<bits/stdc++.h>
#define pb push_back
#define int long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define vc vector<char>
#define vb vector<bool>
#define mii map<int,int>
#define f0r(i,n) for(int i=0;i<n;i++)
#define FOR(i,k,n) for(int i=k;i<n;i++)
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define in(a) int a; cin>>a
#define in2(a,b) int a,b; cin>>a>>b
#define in3(a,b,c) int a,b,c; cin>>a>>b>>c
#define in4(a,b,c,d) int a,b,c,d; cin>>a>>b>>c>>d
#define vin(v,n); vi v(n); f0r(i,n){cin>>v[i];}
#define out(a) cout<<a<<'\n'
#define out2(a,b) cout<<a<<' '<<b<<'\n'
#define out3(a,b,c) cout<<a<<' '<<b<<' '<<c<<'\n'
#define out4(a,b,c,d) cout<<a<<' '<<b<<' '<<c<<' '<<d<<'\n'
#define vout(v) cout<<#v<<' '; for(auto u : v){cout<<u<<' ';} cout<<'\n'
#define dout(a) cout<<a<<' '<<#a<<'\n'
#define dout2(a,b) cout<<a<<' '<<#a<<' '<<b<<' '<<#b<<'\n'
#define yn(x); if(x){cout<<"YES"<<'\n';}else{cout<<"NO"<<'\n';}
const int leg = 1e9 + 7;
const int mod = 998244353;
using namespace std;
const int A = 1e9 + 1911;
int minu(int a, int b){
int ret = (a - b) % mod;
if(ret < 0)ret += mod;
return ret;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
//ifstream cin(".in");
//ofstream cout(".out");
in(n);
string s;
cin>>s;
vi h(n);
h[0] = s[0];
FOR(i, 1, n)h[i] = (h[i-1] * A + s[i]) % mod;
vi p(n);
p[0] = 1;
FOR(i, 1, n)p[i] = (p[i-1] * A) % mod;
//vout(h);
//vout(p);
in3(a,b,c);
vvi dp(n, vi(n));
f0r(i,n){
FOR(j, i, n){
dp[i][j] = (j - i + 1) * a;
}
}
vvi hash(n, vi(n));
f0r(i,n){
hash[i][i] = s[i];
FOR(j, i+1, n){
hash[i][j] = (hash[i][j-1] * A + s[j]) % mod;
}
}
f0r(i,n){
//vout(hash[i]);
}
vvi nxt(n+1, vi(n, -1));
FOR(i, 1, n+1){
mii prev;
f0r(j,n-i+1){
if(prev.count(hash[j][j + i - 1]) == 0){
prev[hash[j][j + i - 1]] = j;
}
else{
int p = prev[hash[j][j + i - 1]];
if(p + i - 1 < j){
nxt[i][p] = j;
prev[hash[j][j + i - 1]] = j;
}
}
}
}
FOR(i, 1, n+1){
//vout(nxt[i]);
}
vector<vector<vpii>>t(n, vector<vpii>(n));
FOR(sz, 1, n+1){
f0r(i, n - sz + 1){
if(sz != 1)dp[i][i + sz - 1] = min(dp[i][i+sz-1], min(dp[i][i+sz-2], dp[i+1][i+sz-1]) + a);
//else dp[i][i] = a;
int cur = i;
int count = 1;
while(cur != -1){
cur = nxt[sz][cur];
if(cur != -1){
//out2(i, cur + sz - 1);
//out4(i, i+sz-1, count, (count + 1) * c + (cur + sz - 1 - i + 1 - (count + 1) * sz) * a + b + dp[i][i + sz - 1]);
dp[i][cur + sz - 1] = min(dp[i][cur + sz - 1], (count + 1) * c + (cur + sz - 1 - i + 1 - (count + 1) * sz) * a + b + dp[i][i + sz - 1]);
}
count++;
}
}
}
f0r(i,n){
//vout(dp[i]);
}
out(dp[0][n-1]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
504 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
592 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
139 ms |
157704 KB |
Output is correct |
4 |
Correct |
210 ms |
184136 KB |
Output is correct |
5 |
Correct |
212 ms |
215368 KB |
Output is correct |
6 |
Correct |
244 ms |
251464 KB |
Output is correct |
7 |
Correct |
308 ms |
294728 KB |
Output is correct |
8 |
Correct |
286 ms |
294476 KB |
Output is correct |
9 |
Correct |
286 ms |
294472 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
504 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
508 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
504 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
592 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
1 ms |
504 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
504 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
2 ms |
336 KB |
Output is correct |
38 |
Correct |
1 ms |
336 KB |
Output is correct |
39 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
504 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
592 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
1 ms |
504 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
504 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
2 ms |
336 KB |
Output is correct |
38 |
Correct |
1 ms |
336 KB |
Output is correct |
39 |
Correct |
1 ms |
336 KB |
Output is correct |
40 |
Correct |
2 ms |
592 KB |
Output is correct |
41 |
Correct |
3 ms |
2252 KB |
Output is correct |
42 |
Correct |
4 ms |
2384 KB |
Output is correct |
43 |
Correct |
4 ms |
2384 KB |
Output is correct |
44 |
Correct |
6 ms |
2384 KB |
Output is correct |
45 |
Correct |
4 ms |
2384 KB |
Output is correct |
46 |
Correct |
5 ms |
2384 KB |
Output is correct |
47 |
Correct |
3 ms |
2128 KB |
Output is correct |
48 |
Correct |
5 ms |
2268 KB |
Output is correct |
49 |
Correct |
4 ms |
2128 KB |
Output is correct |
50 |
Correct |
4 ms |
2384 KB |
Output is correct |
51 |
Correct |
4 ms |
2128 KB |
Output is correct |
52 |
Correct |
4 ms |
2128 KB |
Output is correct |
53 |
Correct |
5 ms |
2252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
504 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
592 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
1 ms |
504 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
504 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
2 ms |
336 KB |
Output is correct |
38 |
Correct |
1 ms |
336 KB |
Output is correct |
39 |
Correct |
1 ms |
336 KB |
Output is correct |
40 |
Correct |
2 ms |
592 KB |
Output is correct |
41 |
Correct |
3 ms |
2252 KB |
Output is correct |
42 |
Correct |
4 ms |
2384 KB |
Output is correct |
43 |
Correct |
4 ms |
2384 KB |
Output is correct |
44 |
Correct |
6 ms |
2384 KB |
Output is correct |
45 |
Correct |
4 ms |
2384 KB |
Output is correct |
46 |
Correct |
5 ms |
2384 KB |
Output is correct |
47 |
Correct |
3 ms |
2128 KB |
Output is correct |
48 |
Correct |
5 ms |
2268 KB |
Output is correct |
49 |
Correct |
4 ms |
2128 KB |
Output is correct |
50 |
Correct |
4 ms |
2384 KB |
Output is correct |
51 |
Correct |
4 ms |
2128 KB |
Output is correct |
52 |
Correct |
4 ms |
2128 KB |
Output is correct |
53 |
Correct |
5 ms |
2252 KB |
Output is correct |
54 |
Correct |
25 ms |
9916 KB |
Output is correct |
55 |
Correct |
43 ms |
47584 KB |
Output is correct |
56 |
Correct |
97 ms |
47432 KB |
Output is correct |
57 |
Correct |
103 ms |
47428 KB |
Output is correct |
58 |
Correct |
98 ms |
47432 KB |
Output is correct |
59 |
Correct |
97 ms |
47436 KB |
Output is correct |
60 |
Incorrect |
97 ms |
47424 KB |
Output isn't correct |
61 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
504 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
592 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
139 ms |
157704 KB |
Output is correct |
14 |
Correct |
210 ms |
184136 KB |
Output is correct |
15 |
Correct |
212 ms |
215368 KB |
Output is correct |
16 |
Correct |
244 ms |
251464 KB |
Output is correct |
17 |
Correct |
308 ms |
294728 KB |
Output is correct |
18 |
Correct |
286 ms |
294476 KB |
Output is correct |
19 |
Correct |
286 ms |
294472 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
504 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
508 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
592 KB |
Output is correct |
29 |
Correct |
1 ms |
504 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
1 ms |
336 KB |
Output is correct |
38 |
Correct |
1 ms |
336 KB |
Output is correct |
39 |
Correct |
1 ms |
336 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
1 ms |
336 KB |
Output is correct |
42 |
Correct |
1 ms |
336 KB |
Output is correct |
43 |
Correct |
1 ms |
336 KB |
Output is correct |
44 |
Correct |
1 ms |
336 KB |
Output is correct |
45 |
Correct |
1 ms |
336 KB |
Output is correct |
46 |
Correct |
1 ms |
336 KB |
Output is correct |
47 |
Correct |
1 ms |
336 KB |
Output is correct |
48 |
Correct |
1 ms |
504 KB |
Output is correct |
49 |
Correct |
1 ms |
336 KB |
Output is correct |
50 |
Correct |
1 ms |
336 KB |
Output is correct |
51 |
Correct |
1 ms |
336 KB |
Output is correct |
52 |
Correct |
1 ms |
336 KB |
Output is correct |
53 |
Correct |
1 ms |
336 KB |
Output is correct |
54 |
Correct |
2 ms |
336 KB |
Output is correct |
55 |
Correct |
1 ms |
336 KB |
Output is correct |
56 |
Correct |
1 ms |
336 KB |
Output is correct |
57 |
Correct |
2 ms |
592 KB |
Output is correct |
58 |
Correct |
3 ms |
2252 KB |
Output is correct |
59 |
Correct |
4 ms |
2384 KB |
Output is correct |
60 |
Correct |
4 ms |
2384 KB |
Output is correct |
61 |
Correct |
6 ms |
2384 KB |
Output is correct |
62 |
Correct |
4 ms |
2384 KB |
Output is correct |
63 |
Correct |
5 ms |
2384 KB |
Output is correct |
64 |
Correct |
3 ms |
2128 KB |
Output is correct |
65 |
Correct |
5 ms |
2268 KB |
Output is correct |
66 |
Correct |
4 ms |
2128 KB |
Output is correct |
67 |
Correct |
4 ms |
2384 KB |
Output is correct |
68 |
Correct |
4 ms |
2128 KB |
Output is correct |
69 |
Correct |
4 ms |
2128 KB |
Output is correct |
70 |
Correct |
5 ms |
2252 KB |
Output is correct |
71 |
Correct |
25 ms |
9916 KB |
Output is correct |
72 |
Correct |
43 ms |
47584 KB |
Output is correct |
73 |
Correct |
97 ms |
47432 KB |
Output is correct |
74 |
Correct |
103 ms |
47428 KB |
Output is correct |
75 |
Correct |
98 ms |
47432 KB |
Output is correct |
76 |
Correct |
97 ms |
47436 KB |
Output is correct |
77 |
Incorrect |
97 ms |
47424 KB |
Output isn't correct |
78 |
Halted |
0 ms |
0 KB |
- |