#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 = 1e9 + 1911;
using namespace std;
const int A = 1e9 + 1234;
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 |
336 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 |
336 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 |
130 ms |
157532 KB |
Output is correct |
4 |
Correct |
165 ms |
184136 KB |
Output is correct |
5 |
Correct |
186 ms |
215368 KB |
Output is correct |
6 |
Correct |
230 ms |
251696 KB |
Output is correct |
7 |
Correct |
278 ms |
294524 KB |
Output is correct |
8 |
Correct |
275 ms |
294472 KB |
Output is correct |
9 |
Correct |
292 ms |
294388 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 |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
556 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 |
336 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 |
336 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 |
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 |
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 |
400 KB |
Output is correct |
24 |
Correct |
1 ms |
504 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
504 KB |
Output is correct |
27 |
Correct |
1 ms |
348 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 |
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 |
# |
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 |
336 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 |
336 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 |
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 |
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 |
400 KB |
Output is correct |
24 |
Correct |
1 ms |
504 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
504 KB |
Output is correct |
27 |
Correct |
1 ms |
348 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 |
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 |
592 KB |
Output is correct |
41 |
Correct |
2 ms |
2128 KB |
Output is correct |
42 |
Correct |
4 ms |
2384 KB |
Output is correct |
43 |
Correct |
4 ms |
2128 KB |
Output is correct |
44 |
Correct |
4 ms |
2384 KB |
Output is correct |
45 |
Correct |
5 ms |
2384 KB |
Output is correct |
46 |
Correct |
4 ms |
2128 KB |
Output is correct |
47 |
Correct |
3 ms |
2128 KB |
Output is correct |
48 |
Correct |
4 ms |
2140 KB |
Output is correct |
49 |
Correct |
4 ms |
2128 KB |
Output is correct |
50 |
Correct |
4 ms |
2128 KB |
Output is correct |
51 |
Correct |
4 ms |
2260 KB |
Output is correct |
52 |
Correct |
4 ms |
2196 KB |
Output is correct |
53 |
Correct |
4 ms |
2128 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 |
336 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 |
336 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 |
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 |
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 |
400 KB |
Output is correct |
24 |
Correct |
1 ms |
504 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
504 KB |
Output is correct |
27 |
Correct |
1 ms |
348 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 |
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 |
592 KB |
Output is correct |
41 |
Correct |
2 ms |
2128 KB |
Output is correct |
42 |
Correct |
4 ms |
2384 KB |
Output is correct |
43 |
Correct |
4 ms |
2128 KB |
Output is correct |
44 |
Correct |
4 ms |
2384 KB |
Output is correct |
45 |
Correct |
5 ms |
2384 KB |
Output is correct |
46 |
Correct |
4 ms |
2128 KB |
Output is correct |
47 |
Correct |
3 ms |
2128 KB |
Output is correct |
48 |
Correct |
4 ms |
2140 KB |
Output is correct |
49 |
Correct |
4 ms |
2128 KB |
Output is correct |
50 |
Correct |
4 ms |
2128 KB |
Output is correct |
51 |
Correct |
4 ms |
2260 KB |
Output is correct |
52 |
Correct |
4 ms |
2196 KB |
Output is correct |
53 |
Correct |
4 ms |
2128 KB |
Output is correct |
54 |
Correct |
21 ms |
9808 KB |
Output is correct |
55 |
Correct |
41 ms |
47396 KB |
Output is correct |
56 |
Correct |
105 ms |
47552 KB |
Output is correct |
57 |
Correct |
107 ms |
47436 KB |
Output is correct |
58 |
Correct |
112 ms |
47432 KB |
Output is correct |
59 |
Correct |
105 ms |
47432 KB |
Output is correct |
60 |
Correct |
109 ms |
47432 KB |
Output is correct |
61 |
Correct |
92 ms |
47436 KB |
Output is correct |
62 |
Correct |
45 ms |
47384 KB |
Output is correct |
63 |
Correct |
113 ms |
47404 KB |
Output is correct |
64 |
Correct |
110 ms |
47432 KB |
Output is correct |
65 |
Correct |
88 ms |
47636 KB |
Output is correct |
66 |
Correct |
90 ms |
47444 KB |
Output is correct |
67 |
Correct |
98 ms |
47436 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 |
336 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 |
336 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 |
130 ms |
157532 KB |
Output is correct |
14 |
Correct |
165 ms |
184136 KB |
Output is correct |
15 |
Correct |
186 ms |
215368 KB |
Output is correct |
16 |
Correct |
230 ms |
251696 KB |
Output is correct |
17 |
Correct |
278 ms |
294524 KB |
Output is correct |
18 |
Correct |
275 ms |
294472 KB |
Output is correct |
19 |
Correct |
292 ms |
294388 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 |
556 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 |
508 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 |
400 KB |
Output is correct |
41 |
Correct |
1 ms |
504 KB |
Output is correct |
42 |
Correct |
1 ms |
336 KB |
Output is correct |
43 |
Correct |
1 ms |
504 KB |
Output is correct |
44 |
Correct |
1 ms |
348 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 |
336 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 |
1 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 |
1 ms |
592 KB |
Output is correct |
58 |
Correct |
2 ms |
2128 KB |
Output is correct |
59 |
Correct |
4 ms |
2384 KB |
Output is correct |
60 |
Correct |
4 ms |
2128 KB |
Output is correct |
61 |
Correct |
4 ms |
2384 KB |
Output is correct |
62 |
Correct |
5 ms |
2384 KB |
Output is correct |
63 |
Correct |
4 ms |
2128 KB |
Output is correct |
64 |
Correct |
3 ms |
2128 KB |
Output is correct |
65 |
Correct |
4 ms |
2140 KB |
Output is correct |
66 |
Correct |
4 ms |
2128 KB |
Output is correct |
67 |
Correct |
4 ms |
2128 KB |
Output is correct |
68 |
Correct |
4 ms |
2260 KB |
Output is correct |
69 |
Correct |
4 ms |
2196 KB |
Output is correct |
70 |
Correct |
4 ms |
2128 KB |
Output is correct |
71 |
Correct |
21 ms |
9808 KB |
Output is correct |
72 |
Correct |
41 ms |
47396 KB |
Output is correct |
73 |
Correct |
105 ms |
47552 KB |
Output is correct |
74 |
Correct |
107 ms |
47436 KB |
Output is correct |
75 |
Correct |
112 ms |
47432 KB |
Output is correct |
76 |
Correct |
105 ms |
47432 KB |
Output is correct |
77 |
Correct |
109 ms |
47432 KB |
Output is correct |
78 |
Correct |
92 ms |
47436 KB |
Output is correct |
79 |
Correct |
45 ms |
47384 KB |
Output is correct |
80 |
Correct |
113 ms |
47404 KB |
Output is correct |
81 |
Correct |
110 ms |
47432 KB |
Output is correct |
82 |
Correct |
88 ms |
47636 KB |
Output is correct |
83 |
Correct |
90 ms |
47444 KB |
Output is correct |
84 |
Correct |
98 ms |
47436 KB |
Output is correct |
85 |
Correct |
259 ms |
118088 KB |
Output is correct |
86 |
Correct |
695 ms |
294472 KB |
Output is correct |
87 |
Incorrect |
689 ms |
294472 KB |
Output isn't correct |
88 |
Halted |
0 ms |
0 KB |
- |