C. Anton and Making Potions
time limit per test4 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Anton is playing a very interesting computer game, but now he is stuck at one of the levels. To pass to the next level he has to prepare n potions.
Anton has a special kettle, that can prepare one potions in x seconds. Also, he knows spells of two types that can faster the process of preparing potions.
Spells of this type speed up the preparation time of one potion. There are m spells of this type, the i-th of them costs bi manapoints and changes the preparation time of each potion to ai instead of x.
Spells of this type immediately prepare some number of potions. There are k such spells, the i-th of them costs di manapoints and instantly create ci potions.
Anton can use no more than one spell of the first type and no more than one spell of the second type, and the total number of manapoints spent should not exceed s. Consider that all spells are used instantly and right before Anton starts to prepare potions.
Anton wants to get to the next level as fast as possible, so he is interested in the minimum number of time he needs to spent in order to prepare at least n potions.
Input
The first line of the input contains three integers n, m, k (1 ≤ n ≤ 2·109, 1 ≤ m, k ≤ 2·105) — the number of potions, Anton has to make, the number of spells of the first type and the number of spells of the second type.
The second line of the input contains two integers x and s (2 ≤ x ≤ 2·109, 1 ≤ s ≤ 2·109) — the initial number of seconds required to prepare one potion and the number of manapoints Anton can use.
The third line contains m integers ai (1 ≤ ai < x) — the number of seconds it will take to prepare one potion if the i-th spell of the first type is used.
The fourth line contains m integers bi (1 ≤ bi ≤ 2·109) — the number of manapoints to use the i-th spell of the first type.
There are k integers ci (1 ≤ ci ≤ n) in the fifth line — the number of potions that will be immediately created if the i-th spell of the second type is used. It’s guaranteed that ci are not decreasing, i.e. ci ≤ cj if i < j.
The sixth line contains k integers di (1 ≤ di ≤ 2·109) — the number of manapoints required to use the i-th spell of the second type. It’s guaranteed that di are not decreasing, i.e. di ≤ dj if i < j.
Output
Print one integer — the minimum time one has to spent in order to prepare n potions.
Examples
input
20 3 2
10 99
2 4 3
20 10 40
4 15
10 80
output
20
input
20 3 2
10 99
2 4 3
200 100 400
4 15
100 800
output
200
Note
In the first sample, the optimum answer is to use the second spell of the first type that costs 10 manapoints. Thus, the preparation time of each potion changes to 4 seconds. Also, Anton should use the second spell of the second type to instantly prepare 15 potions spending 80 manapoints. The total number of manapoints used is 10 + 80 = 90, and the preparation time is 4·5 = 20 seconds (15 potions were prepared instantly, and the remaining 5 will take 4 seconds each).
In the second sample, Anton can’t use any of the spells, so he just prepares 20 potions, spending 10 seconds on each of them and the answer is 20·10 = 200.
题意:前两行给出五个数字N,M,K,X,S,代表需要制作N个药水,M种操作一,K种操作二,初始时制作每个药水需要X秒,初始法力值为S,接着第三行给出M个ai,第四行给出M个bi,代表如果把制作时间更改为ai需要消耗bi的法力值,接着第五行给出ci,第六行给出di,代表如果立即完成ci个药水需要花费di的法力值,并保证ci和di都是严格非递减的序列。操作一和操作二至多可分别各用一次,使用的前提是有足够的法力值供消耗。求出制作药水的最短时间。
思路:枚举操作一,对于每一个枚举的操作一再根据剩余法力值二分枚举操作二即可。
在两种操作中分别增加ai=X,bi=0,ci=0,di=0的情况,可以在枚举时遍历到不使用其中某种操作的情况,时间复杂度O(MlogK)
坑点:注意ci和di序列是严格非递减的,也就是说可能会相等,那么二分的时候就需要有特殊判断了,具体看代码。
代码
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const long long int maxn=200005;
const long long int INF=0x3f3f3f3f;
long long int ai[maxn];
long long int bi[maxn];
long long int ci[maxn];
long long int di[maxn];
int main()
{
long long int N,M,K,X,S;
scanf("%I64d%I64d%I64d%I64d%I64d",&N,&M,&K,&X,&S);
ai[0]=X;
for(long long int i=1; i<=M; i++)
scanf("%I64d",&ai[i]);
bi[0]=0;
for(long long int i=1; i<=M; i++)
scanf("%I64d",&bi[i]);
ci[0]=0;
for(long long int i=1; i<=K; i++)
scanf("%I64d",&ci[i]);
di[0]=0;
for(long long int i=1; i<=K; i++)
scanf("%I64d",&di[i]);
long long int result=N*X;
for(long long int i=0; i<=M; i++)
{
if(S>=bi[i])
{
long long int flag_S=S-bi[i];//剩余法力值
long long int left=0,right=K;
while(left<right)
{
long long int mid=(left+right)/2;
if(di[mid]<=flag_S)
{
if(di[mid+1]>flag_S)
{
left=mid;
break;
}
else
left=mid+1;
}
else
right=mid;
}
result=min(result,ai[i]*(N-ci[left]));
}
}
printf("%I64d\n",result);
return 0;
}