#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct flower
{
int cost;
int be;
int fr;
};
int n, q;
flower a[510];
int dp[510][510];
int book[510];
int c, f;
//价格 美丽度 新鲜程度
int dfs(int x, int y, int z, int step)
{
// for(int i = 1; i <= step; i++)
// {
// cout << " ";
// }
// cout << x << " " << y << " " << z << endl;
if(dp[x][z] != -1)
{
return dp[x][z];
}
int ans = -1;
for(int i = 1; i <= n; i++)
{
if(a[i].cost <= x and book[i] == 0)
{
book[i] = 1;
ans = max(ans, dfs(x - a[i].cost, y + a[i].be, z + a[i].fr, step + 1));
book[i] = 0;
}
}
if(ans == -1)
{
if(z >= f)
{
return y;
}
return -1;
}
dp[x][z] = ans;
return ans;
}
int main()
{
// freopen("a.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
memset(dp, -1, sizeof(dp));
cin >> n >> q;
for(int i = 1; i <= n; i++)
{
int cost, be, fr;
cin >> cost >> fr >> be;
a[i].cost = cost;
a[i].fr = fr;
a[i].be = be;
}
for(int i = 1; i <= q; i++)
{
cin >> c >> f;
cout << dfs(c, 0, 0, 0) << endl;
// for(int j = 1; j <= c; j++)
// {
// for(int k = 1; k <= f; k++)
// {
// cout << dp[j][k] << " ";
// }
// cout << endl;
// }
}
return 0;
}
过样例
一半WA一半T