Submission #1137156

#TimeUsernameProblemLanguageResultExecution timeMemory
1137156hoa208Knapsack (NOI18_knapsack)C++20
73 / 100
1095 ms16800 KiB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORD(i, b, a) for (ll i = (b), _a = (a); i >= _a; i--)
#define pa pair<ll, ll>
#define fi first
#define se second
#define bit(mask, j) ((mask >> j) & 1)
#define t_test int t;cin >> t;while(t--)
const ll mod = 1e9 + 7;
const ll INF = 1e17;
const ll N = 1e2 + 10;
const ll W = 2e3 + 10;
ll S, n, m;
vector<pa> a;
ll dp[W];
void hbmt() {
    // inp
    cin >> S >> n;
    a.push_back({0, 0});
    FOR(i, 1, n) {
        ll v, w, k;
        cin >> v >> w >> k;
        ll heso = 1, sum = 1;
        a.push_back({v, w});
        while(1) {
            heso *= 2;
            if(sum + heso > k) break;
            if(heso > S / w) break;
            sum += heso;
            a.push_back({heso * v, heso * w});
        }        
        if(sum < k) {
            heso = k - sum;
            a.push_back({heso * v, heso * w});
        }
    }
    FOR(i, 1, a.size() - 1) {
        ll v = a[i].fi, w = a[i].se;
        FORD(j, S, w) {
            dp[j] = max(dp[j - w] + v, dp[j]);
        }
    }
    cout << dp[S];
    // slove
   // return subX::slove();
}

int main() {
    
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);

    if(fopen("hbmt.inp", "r")) {
        freopen("hbmt.inp", "r", stdin);
        freopen("hbmt.out", "w", stdout);
    }
    
    // t_test 
    hbmt();
    return 0;
}

Compilation message (stderr)

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