解题思路:先分析n与m相等的情况。然后对于b数组两个相邻的,找到他们在a数组中相隔几个。假设为len个,可以通过假设有p个k,然后剩下的用y来解决,计算出最小值。还有一种情况就是在这两个数之间的最大数小于这两个数的其中一个,那么就还有一种情况len*k。求出它们的最小值。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5+10;
const int inf = 0x7f7f7f7f;
const ll INF = 1e18;
int random(int n){
return (int)(ll)rand()*rand()%n;
}
int a[N],b[N];
void solve(){
int n,m,x,k,y;
scanf("%d%d%d%d%d",&n,&m,&x,&k,&y);
for(int i = 1;i <= n;i++){
scanf("%d",a+i);
}
for(int i = 1;i <= m;i++){
scanf("%d",b+i);
}
if(n == m){
for(int i = 1;i <= n;i++){
if(a[i] != b[i]){
puts("-1");
return;
}
}
}
ll ans = 0;
for(int i = 1,j = 1;i <= m+1;i++,j++){
if(a[j] == b[i]) continue;
ll len = 0;
int M = 0;
while(j <= n+1 && a[j] != b[i]){
M = max(M,a[j]);
len++;j++;
}
if(i <= m){
if(j == n+1){puts("-1");return;}
}
ll ret = INF;
for(ll p = 1;p*k <= len;p++){
ret = min(ret,p*x+(len-p*k)*y);
}
if(M < b[i] || M < b[i-1]&&i-1>=1){
ret = min(ret,len*y);
}
if(ret == INF){
puts("-1");return;
}
ans += ret;
}
printf("%lld\n",ans);
}
int main(){
srand((unsigned)time(NULL));
solve();
return 0;
}