#include <bits/stdc++.h>
#define int long long
using namespace std;
const int inf=(1LL<<60);
int n,a,b,c; string s;
vector<int> z(string s) {
int n = (int) s.length();
vector<int> z(n);
for (int i = 1, l = 0, r = 0; i < n; ++i) {
if (i <= r)
z[i] = min (r - i + 1, z[i - l]);
while (i + z[i] < n && s[z[i]] == s[i + z[i]])
++z[i];
if (i + z[i] - 1 > r)
l = i, r = i + z[i] - 1;
}
return z;
}
int solve(string s){
int n=s.size();
vector<int> p=z(s),dp(n,inf);
vector<vector<int>> A(n+1);
set<int> cur;
for(int i=1;i<n;i++){
if(p[i]==0) continue;
A[p[i]].push_back(i);
cur.insert(i);
}
//for(int&x:p) cout << x << " " ; cout << endl << s << endl ;
// return 0 ;
dp[0]=a;
for(int i=0;i<n;i++){
if(i) dp[i]=min(dp[i],dp[i-1]+a);
int mx=1,last=i;
while(1){
auto it = cur.lower_bound(last+1);
if(it==cur.end()) break;
last=*it+i;
mx++;
dp[last]=min(dp[last],dp[i]+b+c*mx+a*(last-i-(mx-1)*(i+1)));
}
for(int x:A[i+1])
cur.erase(x);
}
return dp.back();
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
cin>>n>>s>>a>>b>>c;
reverse(s.begin(),s.end());
int answer=inf;
for(int i=0;i<n;i++){
string ns;
for(int j=i;j<n;j++)ns+=s[j];
answer=min(answer,solve(ns)+a*i+(i?b+c:0));
}
cout << answer;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
743 ms |
516 KB |
Output is correct |
4 |
Correct |
893 ms |
540 KB |
Output is correct |
5 |
Correct |
1063 ms |
700 KB |
Output is correct |
6 |
Correct |
1237 ms |
640 KB |
Output is correct |
7 |
Correct |
1486 ms |
972 KB |
Output is correct |
8 |
Correct |
1476 ms |
736 KB |
Output is correct |
9 |
Correct |
1470 ms |
604 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 |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |