题目描述

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. linux定时器多次,Spring 定时器执行两次
  2. easyui table 数据表筛选条件
  3. Windows命令行工具实验
  4. Kafka笔记:kafka原理简介以及架构
  5. Apple 预计于内华达州雷诺市再盖一个数据中心
  6. @value 静态变量_springboot项目使用静态变量通过@Value注解获取配置文件内容
  7. 男子支付宝每天莫名进钱 吓得赶紧报警 最后真相哭笑不得...
  8. 公司网络需要内网开发,教你如何使内外网同时访问(windows)
  9. 书柜的尺寸(bzoj 1933)
  10. echarts地图map下钻到镇街、KMZ文件转GeoJson、合成自定义区域
  11. AI头发笔刷_5G大量PS笔刷AI笔刷打包下载(超过1000款笔刷)
  12. 一维 cnn matlab,1D-CNN 一维信号的深度学习算法和例子包括CNN - 下载 - 搜珍网
  13. STM32F107VCTx I2C通信
  14. Jdk8下载安装配置教程
  15. JVM——Java类加载机制总结
  16. 2019腾讯广告算法大赛解析
  17. 视频如何去水印-免费视频水印去除工具
  18. android edittext 取消软键盘,android Edittext输入修改软键盘并关闭软键盘
  19. Appinventor使用
  20. Java获取WiFi ssid_如何在Flutter中获取当前连接的wifi的wifi名称(SSID)

热门文章

  1. java实现微信企业号API服务端调用封装
  2. 私服架设教程-菜鸟篇
  3. 算法设计——荷马史诗(K叉哈夫曼 贪心)
  4. 一键root大师 android,一键Root大师
  5. Java面向对象——自定义异常
  6. c语言某年某月某日的天数,输入某年某月某日,判断这一天是这一年的第几天...
  7. 分享几个找论文参考文献的网站
  8. 电池充电语音警报——隐私政策
  9. java工程源码看不懂_目前从事java开发工作,看不懂源码应该怎么做?
  10. Adobe Flash被禁用和无法加载的官方解决办法