(CodeForces

来源:互联网 发布:剑三正太脸型数据网盘 编辑:程序博客网 时间:2024/06/05 20:26

(CodeForces - 609D)Gadgets for dollars and pounds

time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

Nura wants to buy k gadgets. She has only s burles for that. She can buy each gadget for dollars or for pounds. So each gadget is selling only for some type of currency. The type of currency and the cost in that currency are not changing.

Nura can buy gadgets for n days. For each day you know the exchange rates of dollar and pound, so you know the cost of conversion burles to dollars or to pounds.

Each day (from 1 to n) Nura can buy some gadgets by current exchange rate. Each day she can buy any gadgets she wants, but each gadget can be bought no more than once during n days.

Help Nura to find the minimum day index when she will have k gadgets. Nura always pays with burles, which are converted according to the exchange rate of the purchase day. Nura can’t buy dollars or pounds, she always stores only burles. Gadgets are numbered with integers from 1 to m in order of their appearing in input.

Input

First line contains four integers n, m, k, s (1 ≤ n ≤ 2·105, 1 ≤ k ≤ m ≤ 2·105, 1 ≤ s ≤ 109) — number of days, total number and required number of gadgets, number of burles Nura has.

Second line contains n integers ai (1 ≤ ai ≤ 106) — the cost of one dollar in burles on i-th day.

Third line contains n integers bi (1 ≤ bi ≤ 106) — the cost of one pound in burles on i-th day.

Each of the next m lines contains two integers ti, ci (1 ≤ ti ≤ 2, 1 ≤ ci ≤ 106) — type of the gadget and it’s cost. For the gadgets of the first type cost is specified in dollars. For the gadgets of the second type cost is specified in pounds.

Output

If Nura can’t buy k gadgets print the only line with the number -1.

Otherwise the first line should contain integer d — the minimum day index, when Nura will have k gadgets. On each of the next k lines print two integers qi, di — the number of gadget and the day gadget should be bought. All values qi should be different, but the values di can coincide (so Nura can buy several gadgets at one day). The days are numbered from 1 to n.

In case there are multiple possible solutions, print any of them.

Examples

Input

5 4 2 2
1 2 3 2 1
3 2 1 2 3
1 1
2 1
1 2
2 2

Output

3
1 1
2 3

Input

4 3 2 200
69 70 71 72
104 105 106 107
1 1
2 2
1 2

Output

-1

Input

4 3 1 1000000000
900000 910000 940000 990000
990000 999000 999900 999990
1 87654
2 76543
1 65432

Output

-1

题目大意:一共有n天在m件商品中购买k件。现在一共有s个卢比,对于每一天可以用a[i]个卢比换一个美元,用b[i]个卢比换一块英镑,对于每一件商品只能用它规定的货币购买,1代表美元,2代表英镑,问至少需要多少天才能买到k件商品,如果不能购买到k件就输出-1.

思路:如果x天能买到k件商品,那么x+1天也可以,这里就有一个单调性,所以我们可以二分购买的天数。至于怎么判断这个天数是否合理:我们可以处理出1 - x天里面,只能用美元购买的商品所需最少的卢比的那天和只能用英镑购买的商品所需最少的卢比的那天,用这两天来买这k件商品,看所用的卢比是否超过s。

#include<cstdio>#include<algorithm>using namespace std;typedef long long LL;const int INF=0x3f3f3f3f;const int maxn=200005;int n,m,k,s;LL a[maxn],b[maxn];struct node1{    int id,type;    LL pay,cost;//pay为支付的外币价格,cost为实际的卢比价格 }c[maxn];bool cmp(node1 a,node1 b){    return a.cost<b.cost;}struct node2{    int id,day;}ans[maxn];bool check(int x){    int daya,dayb;    LL mina=INF,minb=INF;    for(int i=1;i<=x;i++)    {        if(a[i]<mina)         {            mina=a[i];            daya=i;        }        if(b[i]<minb)        {            minb=b[i];            dayb=i;        }    }    for(int i=1;i<=m;i++)    {        if(c[i].type==1) c[i].cost=c[i].pay*mina;        else c[i].cost=c[i].pay*minb;    }    sort(c+1,c+1+m,cmp);    LL sum=0;    for(int i=1;i<=k;i++) sum+=c[i].cost;    if(sum<=s)    {        for(int i=1;i<=k;i++)        {            ans[i].id=c[i].id;            if(c[i].type==1) ans[i].day=daya;            else ans[i].day=dayb;        }        return true;    }    return false;}int main(){    while(~scanf("%d%d%d%d",&n,&m,&k,&s))    {        for(int i=1;i<=n;i++) scanf("%lld",a+i);        for(int i=1;i<=n;i++) scanf("%lld",b+i);        for(int i=1;i<=m;i++)         {            scanf("%d%lld",&c[i].type,&c[i].pay);            c[i].id=i;        }        int lo=1,hi=n,mid;        int sumday=-1;        while(lo<=hi)        {            mid=(lo+hi)>>1;            if(check(mid))            {                sumday=mid;                hi=mid-1;            }            else lo=mid+1;        }        printf("%d\n",sumday);        if(sumday>0)        {            for(int i=1;i<=k;i++)                printf("%d %d\n",ans[i].id,ans[i].day);        }    }}
原创粉丝点击