Submission #1329996

#TimeUsernameProblemLanguageResultExecution timeMemory
1329996asu123Knapsack (NOI18_knapsack)C++20
73 / 100
1095 ms432 KiB
#include <bits/stdc++.h>
#define int long long

using namespace std;

template<typename T, typename... Args>
void print(T t) {cout << t << '\n';}
template<typename T, typename... Args>
void print(T t, Args... args) {cout << t << " "; print(args...) ;}

const int N = 105 , W = 1e5+3 , MX = 1e18;

int n , w , dp[W] , ans = -MX;

void file_input() {
    if (fopen("inp.txt" , "r")) {
        freopen("inp.txt" , "r" , stdin);
        freopen("out.txt" , "w" , stdout);
    }
}

void update(int u , int v) {
    for (int i = w; i-u >= 0; i--) {
        if (dp[i-u] == -MX) {continue;}
        dp[i] = max(dp[i-u]+v,dp[i]);
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    file_input();
    cin >> w >> n;
    for (int i = 0; i <= w; i++) {dp[w] = -MX;}
    dp[0] = 0;
    while (n--) {
        int u , v , k , sm;
        cin >> v >> u >> k;
        sm = k;
        for (int i = 0; (1<<(i+1)) <= k; i++) {
            update(u<<i , v<<i) , sm -= 1<<i;
        }
        update(u*sm , v*sm);
    }
    for (int i = 0; i <= w; i++) {ans = max(ans , dp[i]);}
    cout << ans;
    return 0;
}

Compilation message (stderr)

knapsack.cpp: In function 'void file_input()':
knapsack.cpp:17:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |         freopen("inp.txt" , "r" , stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:18:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |         freopen("out.txt" , "w" , stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#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...