Description After winning two coupons for the largest shopping mart in your city, you can't wait inviting your girlfriend for gift hunting. Having inspected hundreds of kinds of souvenirs, toys and cosmetics, you finally narrowed down the

Description

After winning two coupons for the largest shopping mart in your city, you can't wait inviting your girlfriend for gift hunting. Having inspected hundreds of kinds of souvenirs, toys and cosmetics, you finally narrowed down the candidate list to only n gifts, numbered 1 to n. Each gift has a happiness value that measures how happy your girlfriend would be, if you get this gift for her. Some of them are special - you must get it for your girlfriend (note that whether a gift is special has nothing to do with its happiness value).

Coupon 1 can be used to buy gifts with total price not greater than V1 (RMB). Like most other coupons, you can’t get any money back if the total price is strictly smaller than V1. Coupon 2 is almost the same, except that it’s worth V2. Coupons should be used separately. That means you cannot combine them into a super-coupon that’s worth V1+ V2. You have to divide the gifts you choose into two part, one uses coupon 1, the other uses coupon 2.

It is your girlfriend's birthday today. According to the rules of the mart, she can take one (only one) gift for FREE! Here comes your challenge: how to make your girlfriend as happy as possible?

Input

There will be at most 20 test cases. Each case begins with 3 integers V1, V2 and n (1 <= V1 <= 500, 1 <= V2 <= 50, 1 <= n <= 300), the values of coupon 1 and coupon 2 respectively, and the number of candidate gifts. Each of the following n lines describes a gift with 3 integers: P, H and S, where P is the price, H is the happiness (1 <= P,H <= 1000), S=1 if and only if this is a special gift - you must buy it (or get it for free). Otherwise S=0. The last test case is followed by V1 = V2 = n = 0, which should not be processed.

Output

For each test case, print the case number and the maximal total happiness of your girlfriend. If you can't finish the task, i.e. you are not able to buy all special gifts even with the 1-FREE bonus, the happiness is -1 (negative happiness means she's unhappy). Print a blank line after the output of each test case.

Sample Input

```3 2 4
3 10 1
2 10 0
5 100 0
5 80 0
3 2 4
3 10 1
2 10 0
5 100 0
5 80 1
0 0 0 ```

Sample Output

```Case 1: 120

Case 2: 100 ```

```#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int cost1,hap1,cost0,hap0,dp,n,v1,v2;
int main()
{
// freopen("cin.txt","r",stdin);
//freopen("out.txt","w",stdout);
int cas=1;
while(~scanf("%d%d%d",&v1,&v2,&n))
{
if(v1==0&&v2==0&&n==0)break;
memset(dp,0,sizeof(dp));
int l0=0,l1=0,maxh=0,maxn;
for(int i=0;i<n;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(c==1)
{
cost1[l1]=a;
hap1[l1++]=b;
maxh+=b;
}
if(c==0)
{
cost0[l0]=a;
hap0[l0++]=b;
}
}
for(int i=0;i<l1;i++)
{
for(int j=v1;j>=0;j--)
{
for(int k=v2;k>=0;k--)
{
dp[j][k]=max(dp[j][k],dp[j][k]+hap1[i]);
if(j>=cost1[i])
dp[j][k]=max(dp[j][k],dp[j-cost1[i]][k]+hap1[i]),
dp[j][k]=max(dp[j][k],dp[j-cost1[i]][k]+hap1[i]);
if(k>=cost1[i])
dp[j][k]=max(dp[j][k],dp[j][k-cost1[i]]+hap1[i]),
dp[j][k]=max(dp[j][k],dp[j][k-cost1[i]]+hap1[i]);
//     printf("i=%d,j=%d,k=%d,dp0=%d,dp1=%d\n",i,j,k,dp[j][k],dp[j][k]);
}
}
}
if(dp[v1][v2]<maxh)maxn=-1;
else
{
for(int i=0;i<l0;i++)
{
for(int j=v1;j>=0;j--)
{
for(int k=v2;k>=0;k--)
{
if(dp[j][k]>=maxh)
dp[j][k]=max(dp[j][k],dp[j][k]+hap0[i]);
if(j>=cost0[i]&&dp[j-cost0[i]][k]>=maxh)
dp[j][k]=max(dp[j][k],dp[j-cost0[i]][k]+hap0[i]);
if(j>=cost0[i]&&dp[j-cost0[i]][k]>=maxh)
dp[j][k]=max(dp[j][k],dp[j-cost0[i]][k]+hap0[i]);
if(k>=cost0[i]&&dp[j][k-cost0[i]]>=maxh)
dp[j][k]=max(dp[j][k],dp[j][k-cost0[i]]+hap0[i]);
if(k>=cost0[i]&&dp[j][k-cost0[i]]>=maxh)
dp[j][k]=max(dp[j][k],dp[j][k-cost0[i]]+hap0[i]);
//    printf("i=%d,j=%d,k=%d,dp0=%d,dp1=%d\n",i,j,k,dp[j][k],dp[j][k]);
}
}
}
maxn=dp[v1][v2];
}
printf("Case %d: %d\n\n",cas++,maxn);
}
return 0;
}```

<