//水题 //分组背包,比背包01多了一个条件:一些宝物不能同时共存 #include <iostream> #include <cstdio> #include <cstring> using namespace std; int t,p,n,q; int s[101],d[101],e[101],m[101]; int dp[1001]; int main() { //freopen("1.txt","r",stdin); scanf("%d",&t); while( t-- ) { memset(s,0,sizeof(s)); memset(dp,0,sizeof(dp)); scanf("%d%d",&p,&n); q = 0;s[q]++; scanf("%d%d%d",d,e,m); for( int i = 1;i < n;i++ ) { scanf("%d%d%d",d+i,e+i,m+i); if( d[i] == d[i-1] ) s[q]++; else s[++q]++; } int j = 0; for( int i = 0;i <= q;i++ ) { for( int l = p;l >= 0;l-- ) { int k = j; int _mx = dp[l]; for( int z = 0;z < s[i];z++ ) { if( l-e[k] >= 0 && dp[l-e[k]]+m[k] > _mx ) _mx = dp[l-e[k]]+m[k]; //k写成j贡献6次WA k++; } dp[l] = _mx; } j += s[i]; } int _mx = 0; for( int i = 0;i <= p;i++ ) if( dp[i] > _mx ) _mx = dp[i]; cout<<_mx<<endl; } return 0; }