#include <bits/stdc++.h>
#define pb push_back
#define MAX 10006
#define mod 1000000009
#define read freopen("input.txt","r",stdin);
#define base 10
using namespace std;
typedef long long ll;
typedef unsigned long long llu;
int total,w,n;
int cost[1002],weight[1002],ary[1001];
vector<int>c,inde,final_c,final_inde;
void call(int i,int sum)
{
if(i>=n)
{
int ss=0,f=0;
for(int j=0; j<c.size(); j++) ss+=c[j];
for(int j=0; j<final_c.size(); j++) f+=final_c[j];
if(ss>f)
{
final_c=c;
final_inde=inde;
}
return ;
}
if(sum+cost[i]<=total)
{
c.pb(weight[i]);
inde.pb(i);
call(i+1,sum+cost[i]);
c.pop_back();
inde.pop_back();
}
call(i+1,sum);
}
int main()
{
// read;
// freopen("output.txt","w",stdout);
bool flag=true;
while(scanf("%d %d",&total,&w)==2)
{
if(!flag) printf("\n");
flag=false;
scanf("%d",&n);
c.clear();
inde.clear();
final_c.clear();
final_inde.clear();
for(int i=0; i<n; i++) scanf("%d %d",&ary[i],&weight[i]);
for(int i=0; i<n; i++) cost[i]=(ary[i]*w)+(2*(ary[i]*w));
// final_c.pb(0);
call(0,0);
int sum=0;
if(final_inde.size()>0) for(int i=0; i<final_inde.size(); i++)sum+=weight[final_inde[i]];
cout<<sum<<endl;
cout<<final_inde.size()<<endl;
if(final_inde.size()>0)
{
for(int i=0; i<final_inde.size(); i++)
{
cout<<ary[final_inde[i]]<<" "<<weight[final_inde[i]]<<endl;
}
}
else cout<<0<<" "<<0<<endl;
}
return 0;
}
#define pb push_back
#define MAX 10006
#define mod 1000000009
#define read freopen("input.txt","r",stdin);
#define base 10
using namespace std;
typedef long long ll;
typedef unsigned long long llu;
int total,w,n;
int cost[1002],weight[1002],ary[1001];
vector<int>c,inde,final_c,final_inde;
void call(int i,int sum)
{
if(i>=n)
{
int ss=0,f=0;
for(int j=0; j<c.size(); j++) ss+=c[j];
for(int j=0; j<final_c.size(); j++) f+=final_c[j];
if(ss>f)
{
final_c=c;
final_inde=inde;
}
return ;
}
if(sum+cost[i]<=total)
{
c.pb(weight[i]);
inde.pb(i);
call(i+1,sum+cost[i]);
c.pop_back();
inde.pop_back();
}
call(i+1,sum);
}
int main()
{
// read;
// freopen("output.txt","w",stdout);
bool flag=true;
while(scanf("%d %d",&total,&w)==2)
{
if(!flag) printf("\n");
flag=false;
scanf("%d",&n);
c.clear();
inde.clear();
final_c.clear();
final_inde.clear();
for(int i=0; i<n; i++) scanf("%d %d",&ary[i],&weight[i]);
for(int i=0; i<n; i++) cost[i]=(ary[i]*w)+(2*(ary[i]*w));
// final_c.pb(0);
call(0,0);
int sum=0;
if(final_inde.size()>0) for(int i=0; i<final_inde.size(); i++)sum+=weight[final_inde[i]];
cout<<sum<<endl;
cout<<final_inde.size()<<endl;
if(final_inde.size()>0)
{
for(int i=0; i<final_inde.size(); i++)
{
cout<<ary[final_inde[i]]<<" "<<weight[final_inde[i]]<<endl;
}
}
else cout<<0<<" "<<0<<endl;
}
return 0;
}
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন