
Code: Select all
/* Q11003 Boxes: by yatsen */
#include <stdio.h>
#include <string.h>
#define MaxN 1001
#define MaxLoad 3001
struct
{
int weight;
int load;
}box[MaxN];
int best[MaxN][MaxLoad];
int smaller(int x,int y)
{
return (x<y?x:y);
}
int bigger(int x,int y)
{
return (x>y?x:y);
}
void main()
{
int i,j,k,n,ans,maxload;
/*freopen("in11003.txt","r",stdin);*/
while(scanf("%d",&n)==1)
{
if(n==0) break;
for(i=1,maxload=0; i<=n; i++)
{
scanf("%d %d",&box[i].weight,&box[i].load);
if(box[i].load>maxload) maxload=box[i].load;
}
memset(best,0,sizeof(best));
for(i=1; i<=n; i++) best[i][box[i].load]=1;
for(i=2; i<=n; i++) /* i: # of boxes stacked up so far */
{
for(j=maxload; j>=0; j--) /* j: max load */
{
if(best[i-1][j] > 0)
{
if(j-box[i].weight>=0)
{
k=smaller(j-box[i].weight,box[i].load);
best[i][k]=bigger(best[i][k],best[i-1][j]+1);
}
else
best[i][j]=bigger(best[i][j],best[i-1][j]);
}
}
}
for(j=1,ans=best[n][0]; j<=maxload; j++)
if(best[n][j]>ans) ans=best[n][j];
printf("%d\n",ans);
}
}