제출 #747216

#제출 시각아이디문제언어결과실행 시간메모리
747216vjudge1Knapsack (NOI18_knapsack)C++17
100 / 100
588 ms19368 KiB
/*
Free at last, Free at last, Thank God Almighty, We are free at last.
*/

#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#define ll long long
#define nl cout << '\n'
#define pri(n) cout<<fixed<<setprecision(n)
#define d5l_arr(arr, n) for(int i = 0 ; i < n ; i ++)cin >> arr[i]

void m4_htgeb_tle_kda_ya3ny() {
    cin.tie(0);
    cin.sync_with_stdio(0);
    cout.tie(0);
    cout.sync_with_stdio(0);
}

const double eps = -1e-9;
//const long double pi = 3.14159265358979323846;
const ll MOD = 1e9 + 7;
const ll inf = 1e16 + 1;
const int N = 2e5 + 3;

const int W = 2001;
vector<pair<int, int>> cost[W];


void solveCase() {
    int n, s;
    cin >> s >> n;
    for (int i = 0; i < n; ++i) {
        int v, w, k;
        cin >> v >> w >> k;
        cost[w].emplace_back(v, k);
    }
    for (auto &i: cost)std::sort(i.rbegin(), i.rend());
    int dp[s + 1][s + 1];
    memset(dp, 0, sizeof dp);


    for (int rem = 0; rem <= s; ++rem) {
        for (int w = 1; w <= rem; ++w) {
            int taken = 0, val = 0;
            dp[rem][w] = dp[rem][w - 1];
            for (pair<int, int> item: cost[w]) {
                for (int ct = 0; ct < item.second && (taken + 1) * w <= rem; ++ct) {
                    taken++, val += item.first;
                    dp[rem][w] = max(dp[rem][w], dp[rem - taken * w][min(w - 1, rem - taken * w)] + val);

                }
            }
        }
    }

    cout << dp[s][s];
}


int main() {
    m4_htgeb_tle_kda_ya3ny();
    int test = 1;
//     freopen("feast.in", "r", stdin);
//     freopen("feast.out", "w", stdout);
//    cin >> test;
    for (int i = 1; i <= test; ++i) {
        solveCase();
        nl;
    }
    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...