#include <bits/stdc++.h>
using namespace std;
class Object
{
public:
int p;
int w;
int id;
};
int n, c, p, cw, bestp, total_w, total_p;
int x[100], best_x[100];
Object O[100];
void Input()
{
cout<<"请输入物品个数和背包容量:";
cin>>n>>c;
cout<<"请输入每个物品的价值和重量:"<<endl;
for(int i = 1; i <= n; ++i)
{
cin>>O[i].p>>O[i].w;
O[i].id = i;
}
}
bool cmp(const Object &a, const Object &b)
{
return a.p / a.w > b.p / b.w;
}
void Initilize()
{
cp = cw = total_w = total_p = bestp = 0;
for(int i = 1; i <= n; ++i)
{
total_p += O[i].p;
total_w += O[i].w;
}
sort(O + 1, O + n + 1, cmp);
}
int Bound(int i)
{
int temp_cw = c - cw;
int temp_cp = cp;
while(i <= n && temp_cw >= O[i].w)
{
temp_cw -= O[i].w;
temp_cp += O[i].p;
++i;
}
if(i <= n)
temp_cp += O[i].p / O[i].w * temp_cw;
return temp_cp;
}
void Backtrack(int i)
{
if(i > n)
{
bestp = cp;
for(int i = 1; i <= n; ++i)
best_x[i] = x[i];
return;
}
if(cw + O[i].w <= c)
{
x[O[i].id] = 1;
cw += O[i].w;
cp += O[i].p;
Backtrack(i + 1);
cp -= O[i].p;
cw -= O[i].w;
x[O[i].id] = 0;
}
if(Bound(i + 1) > bestp)
{
x[O[i].id] = 0;
Backtrack(i + 1);
}
}
void OutPut()
{
cout<<"最大价值为 = "<<bestp<<endl;;
cout<<"选取的物品为 : ";
for(int i = 1; i <= n; ++i)
{
if(best_x[i] == 1)
cout << i << " ";
}
}
int main()
{
Input();
Initilize();
Backtrack(1);
OutPut();
}