This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* اقْرَأْ بِاسْمِ رَبِّكَ الَّذِي خَلَقَ
* خَلَقَ الْإِنْسَانَ مِنْ عَلَقٍ
**/
#include<algorithm>
#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<bitset>
#include<climits>
#include<functional>
using namespace std;
using ll = long long;
using ld = long double;
#define nl "\n"
#define vc vector
#define vi vc<int>
#define f first
#define s second
#define pb push_back
#define pi pair<int,int>
#define sz(x) (int)x.size()
#define all(x) (x).begin(),(x).end()
#define rall(v) v.rbegin(),v.rend()
#define ms0(X) memset((X), 0, sizeof((X)))
#define rep(i,j, k) for(int i = j; i < (k); ++i)
#define getunique(v) {sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end());}
#define sum(a) ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())
void setIO(string name = "") {
cin.tie(0)->sync_with_stdio(0);
if (sz(name)) {
freopen((name+".in").c_str(), "r", stdin);
freopen((name+".out").c_str(), "w", stdout);
}
}
template<typename T, typename U> void amin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void amax(T& a, U b){if (a < b) a = b;}
//<-------------------------------debug----------------------------------->//
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(const T &t,const V &... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
//<-------------------------------debug----------------------------------->//
const int N=2e5+10;
const int dx[]={0,0,-1,1},dy[]={-1,1,0,0};
#define int ll
void solve(){
int s,n; cin>>s>>n;
vc<pi> v[2001];
rep(i,0,n){
int val,w,k; cin>>val>>w>>k;
v[w].pb({val,k});
}
vc<int> dp(2001);
for(int i=0;i<=s;++i){
if(sz(v[i])==0) continue;
sort(rall(v[i]));
int id=0;
for(int j=0;j*i<s;++j){
if(id>=sz(v[i])) break;
for(int k=s;k>=i;--k){
amax(dp[k],dp[k-i]+v[i][j].f);
}
v[i][j].s--;
if(v[i][j].s==0) id++;
}
}
cout<<dp[s]<<nl;
}
signed main(){
setIO();
int t=1;
//cin >> t;
while(t--) solve();
return 0;
}
/*
*/
Compilation message (stderr)
knapsack.cpp: In function 'void setIO(std::string)':
knapsack.cpp:44:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | freopen((name+".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:45:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | freopen((name+".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |