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 <bits/stdc++.h>
#define sz(x) int(x.size())
#define all(x) x.begin(),x.end()
#define reset(x) memset(x, 0,sizeof(x))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define N 100005
#define remain(x) if (x > MOD) x -= MOD
#define ii pair<int, int>
#define iiii pair< ii , ii >
#define viiii vector< iiii >
#define vi vector<int>
#define vii vector< ii >
#define bit(x, i) (((x) >> (i)) & 1)
#define Task "test"
#define int long long
using namespace std;
typedef long double ld;
const int inf = 1e10;
const int minf = -1e10;
int s, n;
int v[N], w[N], k[N];
void readfile()
{
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
if (fopen(Task".inp","r"))
{
freopen(Task".inp","r",stdin);
//freopen(Task".out","w",stdout);
}
cin >> s >> n;
for(int i=1; i<=n; i++) cin >> v[i] >> w[i] >> k[i];
}
void proc()
{
vector<int> dp(s+1,0);
vector<ii> obj[s+1];
for(int i=1; i<=n; i++) obj[w[i]].pb(ii(v[i],k[i]));
for(int i=0; i<=s; i++){
if (obj[i].size()==0) continue;
sort(obj[i].begin(),obj[i].end(),greater<ii>());
int id = 0;
for(int j=0; j*i < s; j++){
if (id >= obj[i].size()) break;
for(int k=s; k>=i; k--){
dp[k] = max(dp[k], dp[k-i] + obj[i][id].fi);
}
obj[i][id].se--;
if (obj[i][id].se==0) id++;
}
}
cout << dp[s];
}
signed main()
{
readfile();
proc();
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'void proc()':
knapsack.cpp:52:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | if (id >= obj[i].size()) break;
| ~~~^~~~~~~~~~~~~~~~
knapsack.cpp: In function 'void readfile()':
knapsack.cpp:35:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | freopen(Task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |