Description
In order to make the girlfriend happy,CZY decided to send flowers to CM every week. CZY has some coins, a total of N (1 <= N <= 20) different denominations. Every denomination can divide all the denominations that are bigger than it. He wants to use this set of coins to buy flowers per week. The cost of the flowers is at least C(1 <= C <= 100000000), and the shop won't give change. Please help him figure out how many weeks he can send flowers to his girlfriend.
Input
There are multiple sets of input and output,Each set contains:
Line 1: two integers separated by space: N and C
second to line N+1: each row has two integers representing a denomination of coins: coin denomination V (1 <= V <= 100000000) and the number of coins of that denomination that CZY has, B (1 <= B <= 1000000).
Output
A single integer, means that CZY can send flowers at most weeks.
Hint
CZY can pay a 10 cent coin in a week. Then he pays two 5 cent coins every week for the next 10 weeks. In the last 100 weeks, he pays a 1 cent coin and a 5 cent coin.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef struct{int l,r;}node;
node a[25];
int n,c,v,b,cnt,num,vis[25];
long long ans;
bool cmp(node a,node b)
{
return a.l>b.l;
}
void dfs(int now,int sum)
{
if(now>=cnt)
{
if(sum<c)
{
int i;
for(i=cnt-1;i>=0;i--)
if(a[i].r>vis[i])
{
vis[i]++;
break;
}
if(i<0)
return;
}
int tp=0x3f3f3f3f;
for(int i=0;i<cnt;i++)
if(vis[i])
tp=min(tp,a[i].r/vis[i]);
for(int i=0;i<cnt;i++)
a[i].r-=tp*vis[i];
ans+=tp;
return;
}
int tp=min((c-sum)/a[now].l,a[now].r);
vis[now]=tp;
dfs(now+1,sum+tp*a[now].l);
}
int main()
{
while(scanf("%d%d",&n,&c)!=EOF)
{
ans=0;
cnt=0;
for(int i=0;i<n;i++)
{
scanf("%d%d",&v,&b);
if(v>=c)
ans+=b;
else
{
a[cnt].l=v;
a[cnt++].r=b;
}
}
sort(a,a+cnt,cmp);
for(int i=0;i<2*cnt;i++)
{
num=0;
dfs(0,0);
}
printf("%lld\n",ans);
}
return 0;
}