///////////////////////////////////////////
// //
// CODE BY THE phantomstar3.14 //
// //
///////////////////////////////////////////
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ull unsigned long long
std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
constexpr int V = 1E9;
constexpr int MAXN=1e9;
const int mod=1e9+7;
vector<int> primes;
vector<bool>prime(MAXN+1,true);
void sieve()
{
prime[0] = prime[1] = false;
for (int p = 2; p * p < MAXN; p++)
{
if (prime[p])
{
for (int i = p * p; i < MAXN; i += p)
prime[i] = false;
}
}
for (int p = 2; p < MAXN; p++) {
if (prime[p])
primes.push_back(p);
}
}
void solve() {
int s,n;
cin >> s >> n ;
map<int,vector<pair<int,int>>> v;
vector<vector<int>> dp(n, vector<int>(s+1,0));
for(int i=0; i<n; i++)
{
int val, weight, k;
cin>>val>>weight>>k;
if(i==0)
{
for(int j=weight; j<=min(k*weight,s); j+=weight)
dp[i][j]=dp[i][j-weight]+val;
}
if(weight<=s)
v[weight].push_back({val, k});
}
int curr=1;
for(auto [w, vec] : v)
{
if(curr==n) break;
sort(vec.rbegin(), vec.rend());
for(int j=0; j<=s; j++)
{
dp[curr][j]=max(dp[curr][j], dp[curr-1][j]);
if(j<w)
continue;
int num=1;
int weight=w;
int idx=0;
int total =1;
while(num*weight <= j and idx<vec.size())
{
dp[curr][j]=max(dp[curr][j], dp[curr-1][j-num*weight]+num*vec[idx].first);
num++;
total++;
if(total > vec[idx].second)
{
idx++;
total=1;
}
}
}
curr++;
}
int ans=0;
for(int i=0; i<n; i++)
{
for(int j=0; j<=s; j++)
ans=max(ans, dp[i][j]);
}
cout<<ans<<"\n";
}
int32_t main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
auto begin = std::chrono::high_resolution_clock::now();
//freopen("poetry.in", "r", stdin);
//freopen("poetry.out", "w", stdout);
int t=1;
//cin >> t;
while (t--)
{
solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
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... |