Submission #955807

#TimeUsernameProblemLanguageResultExecution timeMemory
955807AriadnaKnapsack (NOI18_knapsack)C++14
73 / 100
1066 ms84160 KiB
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define int long long
 
using namespace std;
 
vector<pair<pair<int, int>, int>> compression(int n, vector<int>& v, vector<int>& w, vector<int>& k) {
    map<pair<int, int>, int> comp;
    for (int i = 0; i < n; ++i) {
        comp[{v[i], w[i]}] += k[i];
    }
 
    for (auto& x : comp) {
        if (x.second >= 3) {
            comp[{2 * x.first.first, 2*x.first.second}] += (x.second - 1) / 2;
            if (x.second % 2) comp[x.first] = 1;
            else comp[x.first] = 2;
        }
    }
    
    vector<pair<pair<int, int>, int>> products;
    for (auto& x : comp) {
        if (x.second == 2) products.pb(mp(x.first, 1));
        else products.pb(mp(x.first, 0));
    }
    return products;
}
 
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int s, n;
    cin >> s >> n;
    vector<int> v(n), w(n), k(n);
    for (int i = 0; i < n; ++i) {
        cin >> v[i] >> w[i] >> k[i];
    }
 
    vector<pair<pair<int, int>, int>> products = compression(n, v, w, k);
    int N = (int)products.size();
 
    vector<int> prev_dp(s+1, 0), curr_dp(s+1, 0);
    for (int i = 0; i < N; ++i) {
        curr_dp = vector<int>(s+1, 0);
        for (int j = 1; j <= s; ++j) {
            curr_dp[j] = prev_dp[j];
            if (j >= products[i].first.second)
                curr_dp[j] = max(curr_dp[j], prev_dp[j-products[i].first.second] + products[i].first.first);
            if (products[i].second && j >= 2*products[i].first.second) {
                curr_dp[j] = max(curr_dp[j], prev_dp[j-2*products[i].first.second] + 2*products[i].first.first);
            }
        }
        prev_dp = curr_dp;
    }
    cout << curr_dp[s] << '\n';
 
    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...