01bit @ 2021-02-27 17:20:28
#include<cstdio>
#include<cstring>
using namespace std;
int max(int a,int b){
return a>b?a:b;
}
int n,m;
int v[61],w[61],q[61];
int f[32001][61];
int b[61];
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)scanf("%d%d%d",v+i,w+i,q+i);
for(int i=1;i<=n;i++){
memset(b,0,sizeof(b));
for(int j=1;j<=m;j++){
if(b[j])continue;
if(i>=v[j]+v[q[j]]){
int buy=f[i-v[j]-v[q[j]]][j-1]+v[j]*w[j]+v[q[j]]*w[q[j]];
if(buy>f[i][j-1]){
f[i][j]=buy;
b[j]=1;b[q[j]]=1;
}else f[i][j]=f[i][j-1];
}else f[i][j]=f[i][j-1];
}
}
printf("%d",f[n][m]);
return 0;
}