Solmi @ 2019-08-16 11:30:44
#include<bits/stdc++.h>
using namespace std;
int n,m,l;
int f[100][32010],fv[100][3],fw[100][3];
int cnt[100];
int main()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=m;i++){
int v,p,q;
scanf("%d%d%d",&v,&p,&q);
if (q == 0){
l++;
fv[l][0]=v;
fw[l][0]=p*v;
}else{
fv[q][++cnt[q]]=v;
fw[q][cnt[q]]=p*v;
}
}
int ans=0;
for (int i=1;i<=l;i++){
for (int j=n;j>=1;j--){
if (j-fv[i][0] >= 0)
f[i][j]=max(f[i][j],f[i-1][j-fv[i][0]]+fw[i][0]);
if (j-fv[i][0]-fv[i][1]>=0)
f[i][j]=max(f[i][j],f[i-1][j-fv[i][0]-fv[i][1]]+fw[i][0]+fw[i][1]);
if (j-fv[i][0]-fv[i][2]>=0)
f[i][j]=max(f[i][j],f[i-1][j-fv[i][0]-fv[i][2]]+fw[i][0]+fw[i][2]);
if (j-fv[i][0]-fv[i][2]-fv[i][1]>=0)
f[i][j]=max(f[i][j],f[i-1][j-fv[i][0]-fv[i][2]-fv[i][1]]+fw[i][0]+fw[i][2]+fw[i][1]);
ans=max(ans,f[i][j]);
}
}
printf("%d",ans);
return 0;
}