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>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
typedef long long ll;
typedef long double ld;
#define all(a) a.begin(),a.end()
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define mp make_pair
#define F first
#define S second
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define nl "\n"
const int MOD = 1e9+7;
const ld PI = 3.1415926535898;
const ll INF = LLONG_MAX;
const int N = 500;
int main()
{
ios;
int s,n,val,qua,wei;
cin>>s>>n;
vector<pair<int,int>>prods[s+1];
for(int i=1;i<=n;i++)
{
cin>>val>>wei>>qua;
prods[wei].pb({val,qua});
}
vector<pair<int,int>>v;
for(int i=1;i<=s;i++)
{
if(prods[i].size()==0)continue;
sort(all(prods[i]));
int maq=s/i;
for(int j=prods[i].size()-1;j>=0;j--)
{
int temp=min(maq,prods[i][j].S);
maq-=temp;
while(temp--)
{
v.pb({i,prods[i][j].F});
}
if(!maq)break;
}
}
int res[s+1]={0};
for(int i=0;i<v.size();i++)
{
for(int j=s-1;j>=0;j--)
{
if(v[i].F+j<=s)res[v[i].F+j]=max(res[v[i].F+j],res[j]+v[i].S);
}
}
cout<<res[s]<<nl;
}
Compilation message (stderr)
knapsack.cpp:5: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
5 | #pragma GCC optimization("unroll-loops")
|
knapsack.cpp: In function 'int main()':
knapsack.cpp:52:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for(int i=0;i<v.size();i++)
| ~^~~~~~~~~
# | 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... |