C - Ahmad and Spells Gym - 101502C 尺取+思维

C. Ahmad and Spells

time limit per test

1.0 s

memory limit per test

256 MB

input

standard input

output

standard output

Ahmad has recently started playing a new game called DotA (Defense of the Agents), which is a very interesting computer game, and currently he is learning a new strategy for winning with his special hero "Chimpanzee King". In order to perform the strategy, the hero must execute n spells.

Initially, each spell needs x seconds of channeling to be executed, but Ahmad knows two ways that can reduce the channeling time, which are:

  1. There are m talents (the hero can learn no more than one talent), the ith of them costs bi mana-points, and changes the channeling time of all spells to ai instead of x.
  2. He can ask for help from other heroes (no more than one) to immediately execute spells. There are k such heroes, and the ith of them costs di mana-points, and instantly executes ci spells.

The total number of mana-points that Ahmad spends should not exceed s.

Ahmad wants to use this strategy at its best, so he is interested in the minimum time he needs to spend in order to execute n spells.

Input

The first line of the input contains an integer T, where T is the number of test cases.

The first line of each test case contains three integers nm, and k (1 ≤ n ≤ 2 × 109, 1 ≤ m, k ≤ 105), where n is the number of spells Ahmad has to execute, m is the number of talents, and k is the number of heroes.

The second line of each test case contains two integers x and s (2 ≤ x ≤ 2 × 109, 1 ≤ s ≤ 2 × 109), where x is the initial number of seconds required to channel one spell, and s is the number of mana-points Ahmad can use.

The third line of each test case contains m integers a1, a2, ..., am (1 ≤ ai ≤ x), where ai is the number of seconds it will take to prepare one potion of the ith spell from the first type.

The fourth line of each test case contains m integers b1, b2, ..., bm (1 ≤ bi ≤ 2 × 109), where bi is the number of required mana-points to use the ith spell from the first type.

The fifth line of each test case contains k integers c1, c2, ..., ck (1 ≤ ci ≤ n), where ci is the number of potions that will be immediately created if the ith spell from the second type was used.

The sixth line of each test case contains k integers d1, d2, ..., dk (1 ≤ di ≤ 2 × 109), where di is the number of mana-points required to use the ith spell from the second type.

Output

For each test case, print a single line containing the minimum time Ahmad has to spend in order to prepare n potions.

Example

input

2
20 3 2
10 99
2 4 3
20 10 40
4 15
10 80
20 3 2
10 99
2 4 3
200 100 400
4 15
100 800

output

20
200

Note

As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printfinstead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.


题意:

       本题题意有点长..请耐心观看。开始时输入5个变量,n,m,k,x,s,主人公有个英雄,要施展n次魔法,初始情况下每次使用魔法要花费x秒,但现在他有s点魔力,他可以用魔力做两种操作。第一种是最多使用一次法术,他有m种法术,每种法术都会有一个魔力消耗值bi和一个值ai,在使用了bi的魔力后,每次施展魔法的时间会全变成ai。第二种是求助最多一个英雄,他有k个求助对象,求助时要消耗di点魔力,可以不用时间就施展ci次魔法。问主人公施展n次魔法需要的最少的时间是多少。

做法:

       先考虑不用法术只求助英雄的情况,枚举一次即可。再考虑一定要用一个法术的情况,把法术和英雄按照魔力消耗值排序,法术从小到大枚举,英雄从大的消耗开始,如果加起来大于s则英雄往小的消耗枚举。

        小技巧:在排序完之后先进行遍历,法术如果魔力多的ai还大,则变成ai小的,英雄如果魔力多的ci还小则变成大的(因为既然小的可以做到更优,就当作选了小的)。


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pii;
const int maxn=100005;
pii hero[maxn],mag[maxn];
ll n,m,k,s,x;
void read(){
    scanf("%lld%lld%lld%lld%lld",&n,&m,&k,&x,&s);
    for(int i=1;i<=m;i++)
        scanf("%lld",&mag[i].second);
    for(int i=1;i<=m;i++)
        scanf("%lld",&mag[i].first);
    for(int i=1;i<=k;i++)
        scanf("%lld",&hero[i].second);
    for(int i=1;i<=k;i++)
        scanf("%lld",&hero[i].first);
}
int main(){
    int t;
    cin>>t;
    while(t--){
        read();
        sort(mag+1,mag+1+m);
        sort(hero+1,hero+1+k);
        for(int i=2;i<=m;i++)
            mag[i].second=min(mag[i].second,mag[i-1].second);//两次优化,敲重点
        for(int i=2;i<=k;i++)
            hero[i].second=max(hero[i].second,hero[i-1].second);
        ll ans=n*x;
        for(int i=1;i<=k;i++){//考虑不取法术
            if(hero[i].first>s) continue;
            ans=min(ans,(n-hero[i].second)*x);
        }
        for(int i=1,j=k;mag[i].first<=s&&i<=m;i++){//尺取,注意条件
            while(j>=1&&mag[i].first+hero[j].first>s) j--;
            if(j>=1){
                ans=min(ans,(n-hero[j].second)*mag[i].second);
            }
            else {
                ans=min(ans,n*mag[i].second);
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41955236/article/details/81624129