emo_male_god @ 2023-03-28 21:13:00
#include <iostream>
#pragma GCC optimize(2)
#define ll long long
using namespace std;
const int N = 100, M = 200010;
ll n, m, v[N], w[N], p[N], a[N][5], f[M], ans;
int main()
{
scanf("%lld%lld", &n, &m);
for (int i = 1; i <= m; i ++ )
{
scanf("%lld%lld%lld", &v[i], &w[i], &p[i]);
if (p[i] > 0)
{
if (a[i][1] == 0) a[i][1] = i;
else a[i][2] = i;
}
}
for (ll i = 1; i <= m; i ++ )
{
f[0] = f[1] = -2147483648;
for (ll j = n; j >= v[i]; j -- )
{
if (p[i] == 0)
{
f[j] = max(f[j], f[j - v[i]] + w[i] * v[i]);
if (j >= v[i] + v[a[i][1]])
{
f[j] = max(f[j], f[j - v[i] - v[a[i][1]]] + v[i] * w[i] + v[a[i][1]] * w[a[i][1]]);
}
if (j >= v[i] + v[a[i][2]])
{
f[j] = max(f[j], f[j - v[i] - v[a[i][2]]] + v[i] * w[i] + v[a[i][2]] * w[a[i][2]]);
}
if (j >= v[i] + v[a[i][1]] + v[a[i][2]])
{
f[j] = max(f[j], f[j - v[i] - v[a[i][1]] - v[a[i][2]]] + v[i] * w[i] + v[a[i][1]] * w[a[i][1]] + v[a[i][2]] * w[a[i][2]]);
}
}
}
}
printf("%lld\n", f[n]);
return 0;
}