1 #include<bits/stdc++.h>
2 #define N 100005
3 #define ll long long
4 #define node pair<int,int>
5 using namespace std;
6 ll n,a,b,c,d;
7 int id,L,R,l,r;
8 int w[N][‘z‘+1];
9 int h[N];
10 ll di[N],dp[N];
11 char s[N];
12 priority_queue<node,vector<node>,greater<node> > q;
13 bool calc(int i,int j,int k){
14 return di[i]*2*a*(di[j]-di[k])>=dp[j]-dp[k]+a*di[j]*di[j]-a*di[k]*di[k];
15 }
16 bool cal(int i,int j,int k){
17 return (di[j]-di[k])*(dp[i]-dp[j]+a*di[i]*di[i]-a*di[j]*di[j])<=(di[i]-di[j])*(dp[j]-dp[k]+a*di[j]*di[j]-a*di[k]*di[k]);
18 }
19 bool ok(int id,int i){
20 int s=-1e8;
21 for (int j=‘a‘;j<=‘z‘;j++)
22 s=max(s,w[i][j]-w[id][j]);
23 if (s>=L&&s<=R) return true;
24 return false;
25 }
26 int main(){
27 scanf("%lld%lld%lld%lld%lld%d%d",&n,&a,&b,&c,&d,&L,&R);
28 scanf("%s",s+1);
29 for (int i=1;i<=n;i++){
30 for (int j=‘a‘;j<=‘z‘;j++) w[i][j]=w[i-1][j];
31 w[i][s[i]]++;
32 }
33 for (int i=1;i<=n;i++)
34 scanf("%lld",&di[i]),di[i]+=di[i-1];
35 l=0; r=0; id=0;
36 for (int i=1;i<=n;i++){
37 while (l<r&&calc(i,h[l+1],h[l])) l++;
38 dp[i]=dp[h[l]]+a*(di[i]-di[h[l]])*(di[i]-di[h[l]])+b;
39 while (id<=i)
40 if (ok(id,i)){
41 q.push(make_pair(dp[id]-c*di[id],id));
42 id++;
43 } else break;
44 while (!q.empty()){
45 int f=q.top().second;
46 if (ok(f,i)){
47 dp[i]=min(dp[i],dp[f]+c*(di[i]-di[f])+d);
48 break;
49 } else{ q.pop(); continue; }
50 }
51 printf("%lld\n",dp[i]);
52 while (l<r&&cal(i,h[r],h[r-1])) r--;
53 h[++r]=i;
54 }
55 return 0;
56 }