Gadgets for dollars and pounds CodeForces - 609D ()

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.

Example
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



#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
#include <cstdio>
using namespace std;
typedef long long ll;
struct node{
	ll q;
	int id;
};
int cmp(node a,node b)
{
	return a.q < b.q;
}
node c[3][500006];
ll a[500006];
ll b[500006];
vector <int> vc[2];
int ii[3];
int n,m,k,s; 
int f(int x)
{
	ll sum = s;
	int kk = 0;
	int i = 0,j = 0;
	vc[0].clear();
	vc[1].clear();
	while(1)
	{
		if(j >= ii[2] || ii[1] > i && c[1][i].q*a[x] <= b[x]*c[2][j].q)
		{
		    kk++;
			sum -= a[x]*c[1][i].q;
			vc[0].push_back(c[1][i].id);
			i++;	
		}
		else
		{
			kk++;
			sum -= b[x]*c[2][j].q;
			vc[1].push_back(c[2][j].id);
			j++;
		}
		if(sum < 0) return 0;
		if(kk >= k) return 1;
	}	
}

int main()
{
	scanf("%d%d%d%d",&n,&m,&k,&s);
	for(int i = 1; i <= n; i++)
	scanf("%d",&a[i]);
	for(int i = 1; i <= n; i++)
	scanf("%d",&b[i]);
	
	for(int i = 2; i <= n; i++)
	{
		a[i] = min(a[i],a[i-1]);
		b[i] = min(b[i],b[i-1]);
	}
	
    for(int i = 0; i < m; i++)
    {
    	int x,y;
    	scanf("%d%d",&x,&y);
    	node nd;
    	nd.q = y;
    	nd.id = i+1;
    	c[x][ii[x]++] = nd;
    }
    sort(c[1],c[1]+ii[1],cmp);
    sort(c[2],c[2]+ii[2],cmp);
    
    int l = 1,r = n;
    int ans = -1;;
    while(l <= r)
    {
    	int mid = (l+r)/2;
    	if(f(mid))
    	{
    		ans = mid;
    		r = mid - 1;
    	}
    	else
    	{
    	    l = mid + 1; 	
    	}
    }
    if(ans == -1)
    {
    	cout << ans <<endl;
    	return 0;
    }
    f(ans);
    cout << ans <<endl;
    for(int i = 1; i <= ans;i++)
    {
    	if(a[ans] == a[i])
    	{
    		for(int j = 0; j < vc[0].size(); j++)
    		{
    			printf("%d %d\n",vc[0][j],i);
    		}
    		vc[0].clear();
    	}
    	if(b[ans] == b[i])
    	{
    		for(int j = 0; j < vc[1].size(); j++)
    		{
    			printf("%d %d\n",vc[1][j],i);
    		}
    		vc[1].clear();
    	}
    }
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值