#include<bits/stdc++.h>
#pragma GCC optimize ("Ofast")
using namespace std;
//\\ PRINCIPAL \\//
#define oo 1e18
#define endl "\n"
#define int long long
#define mod 1000000007
#define tle while(1){;}
#define ios ios_base::sync_with_stdio(0); cin.tie(0),cout.tie(0)
//\\ BITS \\//
#define bits_1 __builtin_popcount
//\\ STL \\//
#define fr first
#define sc second
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define lb(x,y) lower_bound(x.begin(),x.end(),y)-x.begin()
#define ub(x,y) upper_bound(x.begin(),x.end(),y)-x.begin()
void solve() {
int k,n;
cin >> k >> n;
vector<pair<int,pair<int,int> > >v(n + 1,{oo,{oo,oo}});
for(int i = 1; i <= n; i++)
{
cin >> v[i].fr >> v[i].sc.fr >> v[i].sc.sc;
}
sort(all(v));
reverse(all(v));
map<int,int>mapp;
vector<pair<int,int> >v2;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= min(k / v[i].sc.fr - mapp[v[i].sc.fr],v[i].sc.sc); j++)
{
v2.pb({v[i].sc.fr,v[i].fr});
}
mapp[v[i].sc.fr] = min(k / v[i].sc.fr,mapp[v[i].sc.fr] + v[i].sc.sc);
}
vector<int>dp(k + 1,-oo);
dp[0] = 0;
for(int i = 0; i < v2.size(); i++)
{
for(int j = k; j >= v2[i].fr; j--)
{
if(dp[j - v2[i].fr] == -oo)continue;
dp[j] = max(dp[j],dp[j - v2[i].fr] + v2[i].sc);
}
}
int maxx = 0;
for(int i = 1; i <= k; i++)
{
maxx = max(maxx,dp[i]);
}
cout << maxx << endl;
}
int32_t main() {
ios;
//freopen("taming.in", "r", stdin);
//freopen("taming.out", "w", stdout);
int t = 1;
while(t--)solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |