#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;
map<pii, ll> pairToHash;
map<ll, set<int>> hashToStarts;
ll m=1e9+9, p=31;
for(int l=0; l<N; l++){
string sub="";
ll hash=0, pow=1;
for(int r=l; r<N; r++){
hash = (hash + (S[r]-'a'+1) * pow) % m;
pow = (pow * p) % m;
pairToHash[{l,r-l+1}] = hash;
hashToStarts[pairToHash[{l,r-l+1}]].insert(l);
}
}
v<v<int>> nextOccur(N,v<int>(N+1,N));
for(int i=N-1; i>=0; i--){
for(int s=1; s<=N-i; s++){
int ind=pairToHash[{i,s}];
auto next = hashToStarts[ind].lower_bound(i+s);
if(next!=hashToStarts[ind].end()) nextOccur[i][s]=(*next);
}
}
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
1 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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2847 ms |
223048 KB |
Output is correct |
4 |
Execution timed out |
3088 ms |
260736 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
1 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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 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 |
1 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 |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
1 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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 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 |
1 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 |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
4 ms |
980 KB |
Output is correct |
41 |
Correct |
16 ms |
2988 KB |
Output is correct |
42 |
Correct |
24 ms |
4764 KB |
Output is correct |
43 |
Correct |
21 ms |
4740 KB |
Output is correct |
44 |
Correct |
27 ms |
4728 KB |
Output is correct |
45 |
Correct |
20 ms |
4828 KB |
Output is correct |
46 |
Correct |
18 ms |
4776 KB |
Output is correct |
47 |
Correct |
16 ms |
3412 KB |
Output is correct |
48 |
Correct |
17 ms |
4052 KB |
Output is correct |
49 |
Correct |
17 ms |
4316 KB |
Output is correct |
50 |
Correct |
17 ms |
4308 KB |
Output is correct |
51 |
Correct |
17 ms |
3924 KB |
Output is correct |
52 |
Correct |
16 ms |
3864 KB |
Output is correct |
53 |
Correct |
17 ms |
4808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
1 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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 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 |
1 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 |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
4 ms |
980 KB |
Output is correct |
41 |
Correct |
16 ms |
2988 KB |
Output is correct |
42 |
Correct |
24 ms |
4764 KB |
Output is correct |
43 |
Correct |
21 ms |
4740 KB |
Output is correct |
44 |
Correct |
27 ms |
4728 KB |
Output is correct |
45 |
Correct |
20 ms |
4828 KB |
Output is correct |
46 |
Correct |
18 ms |
4776 KB |
Output is correct |
47 |
Correct |
16 ms |
3412 KB |
Output is correct |
48 |
Correct |
17 ms |
4052 KB |
Output is correct |
49 |
Correct |
17 ms |
4316 KB |
Output is correct |
50 |
Correct |
17 ms |
4308 KB |
Output is correct |
51 |
Correct |
17 ms |
3924 KB |
Output is correct |
52 |
Correct |
16 ms |
3864 KB |
Output is correct |
53 |
Correct |
17 ms |
4808 KB |
Output is correct |
54 |
Correct |
149 ms |
22676 KB |
Output is correct |
55 |
Correct |
657 ms |
66984 KB |
Output is correct |
56 |
Correct |
1260 ms |
113188 KB |
Output is correct |
57 |
Incorrect |
1237 ms |
113388 KB |
Output isn't correct |
58 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
1 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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
2847 ms |
223048 KB |
Output is correct |
14 |
Execution timed out |
3088 ms |
260736 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |