<pre name="code" class="cpp">/*problem: 多重背包*/
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 105;
const int inf = 0x7fffffff;
int cost[maxn], value[maxn], num[maxn], f[maxn];
int v;
void ZeroOnePack(int cost, int value)
{
for(int i = v; i >= cost; i--)
f[i] = max(f[i], f[i - cost] + value);
}
void CompletePack(int cost, int value)
{
for(int i = cost; i <= v; i++)
f[i] = max(f[i], f[i - cost] + value);
}
void MultiPack(int cost, int value, int amount)
{
if(v <= cost * amount)
{
CompletePack(cost, value);
return;
}
else
{
int k = 1;
while(k < amount)
{
ZeroOnePack(k*cost, k*value);
amount -= k;
k *= 2;
}
ZeroOnePack(amount*cost, amount*value);
}
}
int main()
{
int cas;
cin >> cas;
int kindn;
while(cas--)
{
cin >> v >> kindn;
for(int i = 0;i < kindn;i++)
{
cin >> cost[i] >> value[i] >> num[i];
}
for(int i = 0;i <= v; i++)
f[i] = -1*inf;
f[0] = 0;
for(int i = 0; i < kindn; i++)
MultiPack(cost[i], value[i], num[i]);
cout << f[v] << endl;
}
return 0;
}