#include<iostream>
using namespace std;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
typedef struct node
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int price;
int wei;
}Node;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Node a[3000];
int dp[3000][3000];
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int main()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int text;
cin>>text;
while(text--)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int n,m;
cin>>n>>m;
int num;
int i,j;
int k = 1;
for(i = 0;i < m;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cin>>a[k].price;
cin>>a[k].wei;
cin>>num;
k++;
for(j = 1;j < num;j++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
a[k].price = a[k-1].price;
a[k].wei = a[k-1].wei;
k++;
}
}
for(j = 0;j < a[1].price;j++)
dp[1][j] = 0;
for(j = a[1].price;j <= n;j++)
dp[1][j] = a[1].wei;
for(i = 2;i < k;i++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(j = 0;j <= n;j++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(a[i].price > j)
dp[i][j] = dp[i-1][j];
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(dp[i-1][j] > dp[i-1][j-a[i].price]+a[i].wei)
dp[i][j] = dp[i-1][j];
else
dp[i][j] = dp[i-1][j-a[i].price]+a[i].wei;
}
}
}
cout<<dp[k-1][n]<<endl;
}
return 0;
}