#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<unordered_map>
#include<cstdio>
using namespace std;
#ifndef LOCAL
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx2,popcnt")
#endif
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define pb push_back
#define ppb pop_back
#define mp make_pair
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif
void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}
void _print(pair<int,int> t) {cerr << t.first << ' '<<t.second;}
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T> void _print(multiset <T> v);
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(unordered_map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
bool comp(pair<int,int>& a, pair<int,int>& b)
{
if(a.first!=b.first)
return a.first > b.first;
return a.second > b.second;
}
int main()
{
fastio();
#ifndef ONLINE_JUDGE
freopen("Error.txt", "w", stderr);
#endif
int x,n;
cin>>x>>n;
unordered_map<int, vector<pair<int,int>>> mp_;
for(int i=0;i<n;i++)
{
int wt, val, k;
cin>>val>>wt>>k;
mp_[wt].pb({val,k});
}
for(auto it=mp_.begin();it!=mp_.end();it++)
{
sort(it->second.begin(), it->second.end(), comp);
}
vector<int> dp(x+1,0);
for(auto it=mp_.begin();it!=mp_.end();it++)
{
int wt = it->first;
vector<pair<int,int>> items = it->second;
int id = 0;
for(int l=1;l<=x/wt;l++)
{
for(int s=x;s>=wt;s--)
{
dp[s] = max(dp[s-wt] + items[id].first, dp[s]);
}
items[id].second--;
if(items[id].second == 0)
{
id++;
}
}
}
cout<<dp[x]<<'\n';
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'int main()':
knapsack.cpp:63:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | freopen("Error.txt", "w", stderr);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |