Submission #791224

#TimeUsernameProblemLanguageResultExecution timeMemory
791224AmrTKnapsack (NOI18_knapsack)C++14
73 / 100
704 ms262144 KiB
#include <bits/stdc++.h>
#define lop(i,a,b) for(ll i = a; i < b; i++)
#define alop(i,v) for(auto &i: v)
#define in(v) for(auto &i: v) cin >> i;
#define ll long long
#define endl "\n"
#define pb push_back
#define all(v) v.begin(),v.end()
#define mem(dp, x) memset(dp, x, sizeof(dp))
#define F first
#define S second
using namespace std;
ll mod = 2019;
const ll N = 1e5;

bool cmp(pair<ll, ll> a, pair<ll, ll> b) {
    if(a.first == b.first) return a.second < b.second;
    return a.first > b.first;
}

signed main()
{
    int n, s; cin >> s >> n;
    vector<array<ll, 3>> v(n); // {value, weight, copies}
    vector<pair<ll, ll>> arr(1);
    alop(i, v){
        in(i);
        ll temp = (s / i[1]) + 1, p = 1;
        while(i[2] > 0 && temp > 0){
            arr.pb({i[0] * p, i[1] * p});
            i[2] -= p, temp -= p;

            if(i[2] - (p + 1) >= 0 && temp - (p + 1) >= 0) p++;
            else p = min(i[2], temp);
        }
    }

    sort(arr.begin() + 1, arr.end(), cmp);

    n = arr.size() - 1;
    ll dp[n + 1][s + 1] = {};

    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= s; j++){
            dp[i][j] = dp[i - 1][j];
            if(j - arr[i].second >= 0) dp[i][j] = max(dp[i][j], dp[i - 1][j - arr[i].second] + arr[i].first);
        }
    }
    cout << dp[n][s];

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...