Submission #1323996

#TimeUsernameProblemLanguageResultExecution timeMemory
1323996sh_qaxxorov_571Knapsack (NOI18_knapsack)C++20
37 / 100
1 ms332 KiB
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

/**
 * Bounded Knapsack masalasi - NOI Singapore 2018
 * Vaqt murakkabligi: O(S * N * log(K))
 */

int main() {
    // Tezkor kiritish-chiqarish
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int S, N;
    cin >> S >> N;

    // dp[w] - w og'irlikda olinishi mumkin bo'lgan maksimal qiymat
    vector<long long> dp(S + 1, 0);

    for (int i = 0; i < N; i++) {
        int v, w, k;
        cin >> v >> w >> k;

        // Ikkilik ajratish (Binary Splitting)
        // k ta elementni 1, 2, 4, 8... kabi guruhlarga bo'lamiz
        for (int j = 1; k > 0; j <<= 1) {
            int num = min(j, k);
            long long current_v = (long long)num * v;
            int current_w = num * w;

            // 0/1 Knapsack usulida DP ni yangilaymiz (teskari tartibda)
            for (int weight = S; weight >= current_w; weight--) {
                dp[weight] = max(dp[weight], dp[weight - current_w] + current_v);
            }
            k -= num;
        }
    }

    // Maksimal qiymatni chiqaramiz
    cout << dp[S] << endl;

    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...