Submission #964387

#TimeUsernameProblemLanguageResultExecution timeMemory
964387KasymKKnapsack (NOI18_knapsack)C++17
49 / 100
255 ms262144 KiB
#include "bits/stdc++.h"
using namespace std;

int main(){
    int S, n;
    scanf("%d%d", &S, &n);
    if(n == 1){
        int v, w, k;
        scanf("%d%d%d", &v, &w, &k);
        int mn = min(S/w, k);
        printf("%lld\n", mn*1LL*v);
        return 0;
    }
    vector<int> baha, agram;
    while(n--){
        int v, w, k;
        scanf("%d%d%d", &v, &w, &k);
        while(k--){
            baha.push_back(v);
            agram.push_back(w);
        }
    }

    vector<long long> dp(S + 1);
    for(int i = 0; i < baha.size(); ++i){
        int v = baha[i], w = agram[i];
        for(int j = S - w; j >= 0; --j)
            dp[j + w] = max(dp[j + w], dp[j] + v);
    }

    long long ans = *max_element(dp.begin(), dp.end());

    printf("%lld\n", ans);

    return 0;
}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:25:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |     for(int i = 0; i < baha.size(); ++i){
      |                    ~~^~~~~~~~~~~~~
knapsack.cpp:6:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    6 |     scanf("%d%d", &S, &n);
      |     ~~~~~^~~~~~~~~~~~~~~~
knapsack.cpp:9:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 |         scanf("%d%d%d", &v, &w, &k);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:17:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |         scanf("%d%d%d", &v, &w, &k);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
#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...