fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. /*
  6. #pragma GCC target ("avx2")
  7. #pragma GCC optimization ("O3")
  8. #pragma GCC optimization ("unroll-loops")
  9. */
  10.  
  11. #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  12. #define rep(i,l,r) for (int i=l;i<=r;i++)
  13. #define fi first
  14. #define se second
  15. #define pb push_back
  16. #define all(x) (x).begin(), (x).end()
  17. #define rall(x) (x).rbegin(), (x).rend()
  18. #define ll long long
  19. #define ull unsigned long long
  20. #define mod 998244353
  21. #define nul(x) memset((x),0,sizeof((x)))
  22.  
  23. mt19937 mrand(random_device{}());
  24. int rnd(int x) { return mrand() % x;}
  25. ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
  26.  
  27. ll n,m,x,k,y;
  28. ll a[200100],b[200100];
  29. vector<ll>v;
  30.  
  31. bool check(){
  32. ll ind = 1;
  33. v.pb(0);
  34. for (int i=1;i<=n;i++){
  35. if (a[i]==b[ind]){
  36. ind++;
  37. v.pb(i);
  38. }
  39. }
  40. v.pb(n+1);
  41. if (ind==m+1)return true;
  42. else return false;
  43. }
  44.  
  45. int main(){
  46. ios;
  47. memset(a,0,sizeof(a));
  48.  
  49. cin>>n>>m;
  50. cin>>x>>k>>y;
  51. for (int i=1;i<=n;i++){
  52. cin>>a[i];
  53. }
  54. for (int i=1;i<=m;i++){
  55. cin>>b[i];
  56. }
  57.  
  58. if (!check())return cout<<"-1\n",0;
  59.  
  60. ll sum = 0;
  61. for (int i=1;i<v.size();i++){
  62. if (v[i]-v[i-1]==1)continue;
  63.  
  64. ll mx=0;
  65. for (int j=v[i-1]+1;j<v[i];j++){
  66. mx = max(mx,a[j]);
  67. }
  68.  
  69. ll ad = 2e18;
  70. if (max(a[v[i]],a[v[i-1]])>mx){
  71. ad = min(ad, (ll)y * (ll)(v[i]-v[i-1]-1));
  72. }
  73. if ((v[i]-v[i-1]-1) >= k){
  74. ad = min(ad, (ll)x * (ll)((v[i]-v[i-1]-1)/k) + (ll)y * (ll)((v[i]-v[i-1]-1)%k));
  75.  
  76. ad = min(ad, x + (ll)y * (ll)((v[i]-v[i-1]-1) - k));
  77. }
  78.  
  79. if (ad == 2e18)return cout<<"-1\n",0;
  80.  
  81. sum += ad;
  82. }
  83. cout<<sum;
  84. return 0;
  85. }
  86.  
Success #stdin #stdout 0s 5136KB
stdin
Standard input is empty
stdout
Standard output is empty