#include <vector>
#include <iostream>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <string>
using namespace std;
#define v vector
#define ll long long
#define pii pair<int,int>
#define fi first
#define se second
int main(){
int N;
cin>>N;
string S;
cin>>S;
ll A,B,C;
cin>>A>>B>>C;
//non disjoint next occurence
v<v<v<int>>> immediateNext(N+1,v<v<int>>(N+1,v<int>(20,N)));
for(int l=0; l<N; l++){
for(int i=l+1; i<N; i++){
if(S[l]==S[i]){
immediateNext[l][1][0]=i;
break;
}
}
}
for(int s=2; s<=N; s++){
for(int l=0; l<=N-s; l++){
int nex=immediateNext[l][s-1][0];
while(nex<=N-s){
if(S[l+s-1]==S[nex+s-1]){
immediateNext[l][s][0]=nex;
break;
}
nex=immediateNext[nex][s-1][0];
}
}
}
//make jump pointers:
for(int i=1; i<20; i++){
for(int s=2; s<=N; s++){
for(int l=0; l<=N-s; l++){
immediateNext[l][s][i]=immediateNext[immediateNext[l][s][i-1]][s][i-1];
}
}
}
//make next occurence disjoint using jump pointers
v<v<int>> nextOccur(N, v<int>(N+1, N));
for(int l=0; l<N; l++){
for(int s=1; s<=N-l; s++){
int nex=l;
for(int i=19; i>=0; i--){
if(immediateNext[nex][s][i]<l+s){
nex=immediateNext[nex][s][i];
}
}
nextOccur[l][s] = immediateNext[nex][s][0];
}
}
v<v<ll>> dp(N, v<ll>(N,1e17));
for(int s=1; s<=N; s++){
for(int l=N-1; l>=0; l--){
dp[l][l]=A;
if(s!=1 && l+s-1<N){
dp[l][l+s-1] = min(dp[l][l+s-1], A+dp[l+1][l+s-1]);
dp[l][l+s-1] = min(dp[l][l+s-1], dp[l][l+s-1-1]+A);
}
int num=2, nex=nextOccur[l][s];
while(nex<N){
int r=nex+s-1;
dp[l][r] = min(dp[l][r], dp[l][l+s-1]+B+C*num+(r-l+1 - num*s)*A);
dp[l][r] = min(dp[l][r], A+dp[l+1][r]);
dp[l][r] = min(dp[l][r], dp[l][r-1]+A);
nex=nextOccur[nex][s];
num++;
}
}
}
cout<<dp[0][N-1]<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2147 ms |
432508 KB |
Output is correct |
4 |
Correct |
2694 ms |
505808 KB |
Output is correct |
5 |
Execution timed out |
3120 ms |
591468 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
2 ms |
1236 KB |
Output is correct |
41 |
Correct |
11 ms |
5456 KB |
Output is correct |
42 |
Correct |
9 ms |
5460 KB |
Output is correct |
43 |
Correct |
10 ms |
5536 KB |
Output is correct |
44 |
Correct |
9 ms |
5452 KB |
Output is correct |
45 |
Correct |
8 ms |
5460 KB |
Output is correct |
46 |
Correct |
9 ms |
5452 KB |
Output is correct |
47 |
Correct |
10 ms |
5460 KB |
Output is correct |
48 |
Correct |
9 ms |
5460 KB |
Output is correct |
49 |
Correct |
10 ms |
5452 KB |
Output is correct |
50 |
Correct |
10 ms |
5580 KB |
Output is correct |
51 |
Correct |
11 ms |
5460 KB |
Output is correct |
52 |
Correct |
10 ms |
5460 KB |
Output is correct |
53 |
Correct |
9 ms |
5460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
2 ms |
1236 KB |
Output is correct |
41 |
Correct |
11 ms |
5456 KB |
Output is correct |
42 |
Correct |
9 ms |
5460 KB |
Output is correct |
43 |
Correct |
10 ms |
5536 KB |
Output is correct |
44 |
Correct |
9 ms |
5452 KB |
Output is correct |
45 |
Correct |
8 ms |
5460 KB |
Output is correct |
46 |
Correct |
9 ms |
5452 KB |
Output is correct |
47 |
Correct |
10 ms |
5460 KB |
Output is correct |
48 |
Correct |
9 ms |
5460 KB |
Output is correct |
49 |
Correct |
10 ms |
5452 KB |
Output is correct |
50 |
Correct |
10 ms |
5580 KB |
Output is correct |
51 |
Correct |
11 ms |
5460 KB |
Output is correct |
52 |
Correct |
10 ms |
5460 KB |
Output is correct |
53 |
Correct |
9 ms |
5460 KB |
Output is correct |
54 |
Correct |
80 ms |
26228 KB |
Output is correct |
55 |
Correct |
567 ms |
129796 KB |
Output is correct |
56 |
Correct |
489 ms |
129748 KB |
Output is correct |
57 |
Correct |
495 ms |
129792 KB |
Output is correct |
58 |
Correct |
491 ms |
129728 KB |
Output is correct |
59 |
Correct |
489 ms |
129728 KB |
Output is correct |
60 |
Correct |
487 ms |
129796 KB |
Output is correct |
61 |
Correct |
537 ms |
129800 KB |
Output is correct |
62 |
Correct |
526 ms |
129732 KB |
Output is correct |
63 |
Correct |
484 ms |
129732 KB |
Output is correct |
64 |
Correct |
498 ms |
129728 KB |
Output is correct |
65 |
Correct |
512 ms |
129792 KB |
Output is correct |
66 |
Correct |
491 ms |
129740 KB |
Output is correct |
67 |
Correct |
487 ms |
129768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
2147 ms |
432508 KB |
Output is correct |
14 |
Correct |
2694 ms |
505808 KB |
Output is correct |
15 |
Execution timed out |
3120 ms |
591468 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |