The startup"Booble" has shown explosive growth and now it needs a new datacenter with the capacity of m petabytes. Booble canbuy servers, there are n servers available forpurchase: they have equal price but different capacities. The i-th servercan store ai petabytes of data. Also they have different energy consumption —some servers are low voltage and other servers are not.
Booble wants to buy theminimum number of servers with the total capacity of at least m petabytes.If there are many ways to do it Booble wants to choose a way to maximize thenumber of low voltage servers. Booble doesn't care about exacttotal capacity, the only requirement is to make it at least m petabytes.
Input
The first line contains twointeger numbers n and m (1 ≤ n ≤ 2·105, 1 ≤ m ≤ 2·1015) — the number of serversand the required total capacity.
The following n linesdescribe the servers, one server per line. The i-th line contains twointegers ai, li (1 ≤ ai ≤ 1010, 0 ≤ li ≤ 1), where ai is the capacity, li = 1 ifserver is low voltage and li = 0 in the opposite case.
It is guaranteed that thesum of all ai is at least m.
Output
Print two integers r and w onthe first line — the minimum number of servers needed to satisfy the capacityrequirement and maximum number of low voltage servers that canbe bought in an optimal r servers set.
Print on the second line r distinctintegers between 1 and n — the indices ofservers to buy. You may print the indices in any order. If there are manysolutions, print any of them.
Sample test(s)
input
4 10
3 1
7 0
5 1
4 1
output
2 1
4 2
input
3 13
6 1
6 1
6 1
output
3 3
1 2 3
Note
In the first example anypair of servers which includes the server 2 is a correct output.
思路:
先确定最少需要多少台机器,最后如果1的多出来,则用多出来的1换已选中的0
程序:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include <cstdio>
#include <stack>
#include <cstdlib>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 200005;
struct node
{
__int64 c;
int id;
}a1[N], a2[N];
int l1, l2;
bool cmp(node x, node y)
{
return x.c>y.c;
}
void pr(int x, int y)
{
printf("%d %d\n", x + y, x);
int i;
if (x>0)
{
printf("%d", a1[0].id);
for (i = 1; i<x; i++)
{
printf(" %d", a1[i].id);
}
if (y>0)
printf(" ");
}
if (y>0)
{
printf("%d", a2[0].id);
for (i = 1; i<y; i++)
{
printf(" %d", a2[i].id);
}
}
printf("\n");
}
int min(int x, int y)
{
if (x<y)
return x;
else
return y;
}
int main()
{
__int64 n, m, c, l;
scanf("%I64d %I64d", &n, &m);
int i;
l1 = l2 = 0;
for (i = 1; i <= n; i++)
{
scanf("%I64d %I64d", &c, &l);
if (l == 1)
{
a1[l1].c = c;
a1[l1].id = i;
l1++;
}
else
{
a2[l2].c = c;
a2[l2].id = i;
l2++;
}
}
sort(a1, a1 + l1, cmp);
sort(a2, a2 + l2, cmp);
__int64 cost = 0, m2 = m;
int j;
i = j = 0;
while (i<l1 && j<l2)
{
if (a1[i].c >= a2[j].c)
{
cost += a1[i].c;
m -= a1[i].c;
i++;
}
else
{
cost += a2[j].c;
m -= a2[j].c;
j++;
}
if (m <= 0)
{
break;
}
}
if (m>0)
{
while (i<l1)
{
cost += a1[i].c;
m -= a1[i].c;
i++;
if (m <= 0)
break;
}
while (j<l2)
{
cost += a2[j].c;
m -= a2[j].c;
j++;
if (m <= 0)
break;
}
}
__int64 cut = cost - m2;
int count = min(j, l1 - i);
__int64 sum1 = 0, sum2 = 0;
int add = 0;
int o;
for (o = 0; o<count; o++)
{
sum1 += a1[i + o].c;
sum2 += a2[j - o - 1].c;
if (sum2 - sum1 <= cut)
{
add = o + 1;
}
}
pr(i + add, j - add);
return 0;
}