题目描述

There are n warriors in a row. The power of the i-th warrior is ai. All powers are pairwise distinct.
You have two types of spells which you may cast:
Fireball: you spend x mana and destroy exactly k consecutive warriors;
Berserk: you spend y mana, choose two consecutive warriors and the warrior with greater power destroys another chosen warrior.
For example, let the powers of warriors be [2,3,7,8,11,5,4], and k=3. If you cast Berserk on warriors with powers 8 and 11, the resulting sequence of powers becomes [2,3,7,11,5,4]. Then, for example, if you cast Fireball on consecutive warriors with powers [7,11,5], the resulting sequence of powers becomes [2,3,4].
You want to turn the current sequence of warriors powers a1,a2,…,an into b1,b2,…,bm. Calculate the minimum amount of mana you need to spend on it.

Input

The first line contains two integers n and m (1≤n,m≤2⋅105) — the length of sequence a and the length of sequence b respectively.
The second line contains three integers x,k,y (1≤x,y,≤109;1≤k≤n) — the cost of fireball, the range of fireball and the cost of berserk respectively.
The third line contains n integers a1,a2,…,an (1≤ai≤n). It is guaranteed that all integers ai are pairwise distinct.
The fourth line contains m integers b1,b2,…,bm (1≤bi≤n). It is guaranteed that all integers bi are pairwise distinct.

Output

Print the minimum amount of mana for turning the sequnce a1,a2,…,an into b1,b2,…,bm, or −1 if it is impossible.

Examples

input
5 2
5 2 3
3 1 4 5 2
3 5
output
8
input
4 4
5 1 4
4 3 1 2
2 4 3 1
output
-1
input
4 4
2 1 11
1 3 2 4
1 3 2 4
output
0

题目大意

n个战士排成一排,分别有个武力值a[i]。你有两种法术,一个是火球(花费x个法力,消灭连续k个战士),一个是激怒(花费y个法力,选择相邻两个战士,武力值大的会消灭武力值小的)。求最后留下的战士排列成b[i]需要的最小法力花费

题目分析

我们只需要将a[]中元素和b[]中元素进行一一比较,找出a[]中那些区间要被删除即可。如果a[]只通过删除某些数不能变成b[]或者a[]中某个应该被删除的区间无法通过上述两种法术完全删除,那么输出-1。

利用两种法术删除某个区间中的所有数,且得到最小法力花费的方法:
设该区间为[l,r],首先看看与该区间相邻的两个数a[l-1],a[r+1]是否有一个大于区间内的最大值(判断能否只通过法术(2)来消除区间内的所有数)。
如果区间的长度len<k(此时只能通过法术(2)来消除区间内的数),且上述条件不成立,那么不能完全删除这个区间中的所有数,输出-1。
然后就是找出最小的法力花费了:
我们必须使用法术(2)来消除至少len%k个数(因为法术(1)每次都只能消除连续的k个数)。剩下的len-k个数我们就要通过判断来确定使用那种方法了(注:后面的len都是len-=k后的)。
如果用法术(2)删除k个数需要的法力大于等于x(y*k>=x),那么剩下的数就可以只用法术(1)来删除了。
如果y*k<x且条件1成立,那么就可以用用法术(2)来消除剩下的所有数。
如果y*k<x且条件1不成立,即不能用法术(2)来删除所有数,那么就用法术(2)删除(len-k)个元素,最后只留下k个元素用法术(1)来消除。

代码如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <iomanip>
#define LL long long
#define PII pair<int,int>
using namespace std;
const int N=2e5+5;
LL n,m,x,y,k;       //k*y有可能爆int,所以最后用long long
int a[N],b[N];
bool remove(int l,int r,LL &ans)
{if(l>r) return true;bool st=false;             //判断条件1int max=*max_element(a+l,a+1+r);if(l-1>=0&&a[l-1]>max) st=true;if(r+1<n&&a[r+1]>max) st=true;int len=r-l+1;if(len<k&&!st) return false;int t=len%k;       //法术(2)删除len%k个数ans+=t*y;len-=t;if(k*y>=x) ans+=len/k*x;else if(st) ans+=len*y;else ans+=(len-k)*y+x;return true;
}
int main()
{scanf("%d %d",&n,&m);scanf("%d %d %d",&x,&k,&y);for(int i=0;i<n;i++)scanf("%d",&a[i]);for(int i=0;i<m;i++)scanf("%d",&b[i]);int posa=0,posb=0,s=-1;LL ans=0;       //记录答案while(posb<m){ while(posa<n&&a[posa]!=b[posb]) posa++;       //将a[]和b[]进行一一比对if(posa==n) {puts("-1"); return 0;}         //a[]只通过删除一些元素无法与b[]相等if(!remove(s+1,posa-1,ans)) {puts("-1"); return 0;}    //判断能否把一个区间的数完全删除s=posa;posb++;}if(!remove(s+1,n-1,ans)) {puts("-1"); return 0;}printf("%lld\n",ans); return 0;
}

Educational Codeforces Round 91 D. Berserk And Fireball相关推荐

  1. Educational Codeforces Round 90 (Rated for Div. 2)(A, B, C, D, E)

    Educational Codeforces Round 90 (Rated for Div. 2) Donut Shops 思路 分三种情况: a==c/ba == c / ba==c/b这个时候两 ...

  2. Educational Codeforces Round 95题解

    Educational Codeforces Round 95题解 题目链接 代码链接 A. Buying Torches 题目大意: 你手上现在有一个木棍.有以下两种交换方式: 1.用一个木棍交换x ...

  3. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

  4. Educational Codeforces Round 114 (Rated for Div. 2) (A ~ F)全题解

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Educational Codeforces Round 114 (Rated for Div. 2) ...

  5. Educational Codeforces Round 106 (Rated for Div. 2)(A ~ E)题解(每日训练 Day.16 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 Educational Codeforces Round 106 (Rated for Div. ...

  6. Educational Codeforces Round 32

    http://codeforces.com/contest/888 A Local Extrema[水] [题意]:计算极值点个数 [分析]:除了第一个最后一个外,遇到极值点ans++,包括极大和极小 ...

  7. Educational Codeforces Round 37 (Rated for Div. 2) 1

    Educational Codeforces Round 37 (Rated for Div. 2) A.Water The Garden 题意:Max想给花园浇水.花园可被视为长度为n的花园床,花园 ...

  8. Educational Codeforces Round 89 (Rated for Div. 2)(A, B, C, D)

    Educational Codeforces Round 89 (Rated for Div. 2) A. Shovels and Swords 思路 题意非常简单,就是得到最多的物品嘛,我们假定a, ...

  9. Educational Codeforces Round 114 (Rated for Div. 2) D. The Strongest Build 暴力 + bfs

    传送门 文章目录 题意: 思路: 题意: 你有nnn个装备槽,每个槽里面有cic_ici​个力量加成,对于每个槽只能选一个力量加成,现在给你mmm个力量组合[b1,b2,...,bn][b_1,b_2 ...

  10. Educational Codeforces Round 17 E. Radio stations cdq分治 + 树状数组

    传送门 文章目录 题意 思路: 题意 有nnn个电台,对于每个电台iii有三个参数xi,ri,fix_i,r_i,f_ixi​,ri​,fi​,分别指他们的坐标.作用半径.频率.如果两个电台频率差值在 ...

最新文章

  1. Android通过for循环批量发送短信
  2. boost::intrusive::offset_ptr用法的测试程序
  3. 【js】vue 2.5.1 源码学习(二) 策略合并
  4. java 反射与泛型_Java基础系列 - 泛型和反射机制
  5. vue 拷贝 数组_vue 使用lodash实现对象数组深拷贝操作
  6. 分布式面试 - 如何基于 dubbo 进行服务治理、服务降级、失败重试以及超时重试?
  7. 模式识别中的特征向量和矩阵的特征向量有什么关系
  8. 网易云音乐java爬虫_用Java实现网易云音乐爬虫
  9. pc端和移动端抓包工具
  10. 数据库:简述对数据库的认识
  11. Mac 如何免费支持NTFS 格式移动硬盘读写
  12. 怀旧--这些游戏你都玩过么?还记得么?
  13. Vue实战篇三十五:实现滑动拼图验证登录
  14. 论文笔记:Auto-Encoding Scene Graphs for Image Captioning
  15. 软件工程课程设计项目总结与项目报告
  16. pytorch中torch.mul、torch.mm/torch.bmm、torch.matmul的区别
  17. python和access哪个好过计算机二级_大一考计算机二级,那考office、C语言、VB、Java、Access还是Python好呢?...
  18. 【老鸟进阶】deepfacelab合成参数详解
  19. qq不显示我的android手机号码,QQ手机通讯录怎么设置不显示推荐联系人?
  20. 【行人重识别】计算机视觉进阶系列 第一课 基础知识

热门文章

  1. C盘用户名更改后一些注意事项
  2. html在线表情聊天功能,HTML5高仿微信聊天、微信聊天表情|对话框|编辑器功能
  3. js实现-别踩白块儿-类
  4. 手机H5-调用百度地图导航
  5. 【web前端特效源码】使用HTML5+CSS3制作一个3D视频旋转立方体动画效果~~适合初学者~超简单~ |前端开发|IT软件
  6. 【51单片机】OOK无线通讯在无线门磁报警中的应用
  7. Vue Router4路由
  8. 2022 年十大绩效考核技巧
  9. 【商业模式学习感悟】趣步App——新型商业模式,还是新型传销?
  10. 公开下载 |《2021技术人的百宝黑皮书》来了!