Submission #480467

#TimeUsernameProblemLanguageResultExecution timeMemory
480467XIIKnapsack (NOI18_knapsack)C++17
73 / 100
1088 ms1484 KiB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

#define fi first
#define se second
#define mp make_pair
#define eb emplace_back
#define ALL(x) (x).begin(), (x).end()

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define FORU(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)

#define IOS cin.tie(0)->sync_with_stdio(false);
#define PROB "NOI18_knapsack"
void Fi(){
    if(fopen(PROB".inp", "r")){
        freopen(PROB".inp", "r", stdin);
        freopen(PROB".out", "w", stdout);
    }
}

const int N = 1e5 + 1;
const int S = 2e3 + 1;

int s, n;
int v[N], w[N], k[N];

void enter(){
    cin >> s >> n;
    FORU(i, 1, n){
        cin >> v[i] >> w[i] >> k[i];
    }
}

ll dp[S][2];
void solve(){
    FORU(i, 1, n) FORU(j, 0, s){
        if(j == 0 || dp[j][0]){
            for(int z = 1; z <= k[i] && j + z * w[i] <= s; ++z){
                dp[j + z * w[i]][1] = max(dp[j + z * w[i]][1], dp[j][0] + z * v[i]);
            }
        }
        dp[j][0] = dp[j][1];
    }
    ll ans = 0;
    FORU(i, 0, s) ans = max(ans, dp[i][1]);
    cout << ans;
}

int main(){
    IOS;
    Fi();
    enter();
    solve();
    return 0;
}

Compilation message (stderr)

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