a253774060 @ 2018-10-17 20:25:04
#include<cstdio>
#include<algorithm>
struct Item{
int cost,val,belong;
bool is_primary;
bool operator<(const Item &other){
return belong==other.belong? is_primary:(belong<other.belong);
}
}item[62];
int f[32001];
inline int max(const int &a,const int &b){
return a>b? a:b;
}
inline void update(int cost,int val,int k){
if(k>=cost) f[k]=max(f[k],f[k-cost]+val);
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
int v,p,q;
for(int i=1;i<=m;++i){
scanf("%d%d%d",&v,&p,&q);
item[i].cost=v;
item[i].val=v*p;
if(q==0){
item[i].belong=i;
item[i].is_primary=false;
}else{
item[i].belong=q;
}
}
std::sort(item+1,item+m+1);
int idx=1,primary_idx;
do{
primary_idx=idx;
while(item[idx].belong==item[primary_idx].belong) ++idx;
for(int k=n;k>=0;--k){
update(item[primary_idx].cost,
item[primary_idx].val,k);
for(int i=primary_idx+1;i<idx;++i){
update(item[primary_idx].cost+item[i].cost,
item[primary_idx].val+item[i].val,k);
}
for(int i=primary_idx+1;i<idx;++i){
for(int j=i+1;j<idx;++j){
update(item[primary_idx].cost+item[i].cost+item[j].cost,
item[primary_idx].val+item[i].val+item[j].val,k);
}
}
}
}while(idx<=m);
printf("%d",f[n]);
}