# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
673719 | asteilind | Knapsack (NOI18_knapsack) | C++17 | 134 ms | 5032 KiB |
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 <iostream>
#include <vector>
#include <queue>
#include <string>
#include <stack>
#include <unordered_map>
#include <map>
#include <unordered_set>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <set>
#include <numeric>
#include <bitset>
#include <climits>
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define ll long long
#define MOD 1000000007
using namespace std;
void setIO(string name = "") { // name is nonempty for USACO file I/O
ios_base::sync_with_stdio(0); cin.tie(0); // see Fast Input & Output
if(name.length()){
freopen((name+".in").c_str(), "r", stdin); // see Input & Output
freopen((name+".out").c_str(), "w", stdout);
}
}
void solve()
{
ll s,n; cin >> s >> n;
map<ll,vector<pair<ll,ll>>> map;
forn(i,n)
{
ll p,w,c; cin >> p >> w >> c;
map[w].push_back({p,c});
}
vector<ll> dp(s+1,-1);
dp[0] = 0;
ll res = 0;
for (auto &[w,items]: map)
{
sort(items.begin(),items.end(),greater<pair<ll,ll>>());
ll ct2 = 0;
ll maxv = s/w;
for (ll j=0 ; items.size()>j ; j++)
{
if (ct2 == maxv) break;
for (ll i = 0; items[j].second>i && (ct2 != maxv) ; i++)
{
ct2++;
for (ll a=s; a>=0 ; a--)
{
if (a+w > s || dp[a] == -1) continue;
dp[a+w] = max(dp[a+w],(dp[a]+items[j].first));
res = max(res,dp[a+w]);
}
}
}
}
cout << res << endl;
}
int main()
{
ios_base::sync_with_stdio(0);
setIO();
solve();
}
Compilation message (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... |