Submission #869875

#TimeUsernameProblemLanguageResultExecution timeMemory
869875chuongdanKnapsack (NOI18_knapsack)C++17
0 / 100
2 ms348 KiB
#include<bits/stdc++.h>
#define N 100000
#define S 2000
#define pb push_back
#define int long long
#define mp make_pair
#define fi first
#define se second
#define rep(i, l, r) for(int i = l; i <= r; i++)

using namespace std;

typedef pair<int,int> ii;

int n, s;
int dp[S+5];
vector<int> val[S+5];
vector<ii> ares;
struct mondo {
    int v, w, k;
} a[N+5];

bool cmp(mondo x, mondo y){
    if (x.w == y.w) return x.v > y.v;
    return x.w < y.w;
}

void solve(){
    cin >> s >> n;
    rep(i, 1, n) {
        cin >> a[i].v >> a[i].w >> a[i].k;
    }
    sort(a+1, a+1+n, cmp);
    rep(i, 1, n) {
//        cout << a[i].v << " " << a[i].w << " " << a[i].k << endl;
    }
    rep(i, 1, n){
        int maxsize = s / a[i].w;
        if (a[i].w > s) continue;
        while (a[i].k && val[a[i].w].size() < maxsize){
            a[i].k--;
            val[a[i].w].pb(a[i].v);
        }
    }
    rep(i, 1, s){
        for(auto x: val[i]){
            ares.pb(mp(i, x));
        }
    }
    int res = 0;
    for(auto x: ares) {
        int w = x.fi, v = x.se;
        for(int i = s; i >= w; i--){
            dp[i] = max(dp[i], dp[i-w] + v);
        }
    }
    rep(i, 0, s) res = max(res, dp[i]);
    cout << res;
}

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
        freopen("bt.inp", "r", stdin);
        freopen("bt.out", "w", stdout);
    #endif
    solve();
}

Compilation message (stderr)

knapsack.cpp: In function 'void solve()':
knapsack.cpp:40:45: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   40 |         while (a[i].k && val[a[i].w].size() < maxsize){
      |                          ~~~~~~~~~~~~~~~~~~~^~~~~~~~~
knapsack.cpp: In function 'int main()':
knapsack.cpp:65:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |         freopen("bt.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:66:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |         freopen("bt.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...