제출 #480471

#제출 시각아이디문제언어결과실행 시간메모리
480471XIIKnapsack (NOI18_knapsack)C++17
100 / 100
89 ms4108 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], p[N];

vector<int> a[S];

void enter(){
    cin >> s >> n;
    FORU(i, 1, n){
        cin >> v[i] >> w[i] >> k[i];
        p[i] = i;
    }
    sort(p + 1, p + n + 1, [&](const int &i, const int &j){
            return v[i] > v[j];
        }
    );
    FORU(i, 1, n){
        for(int j = 1; j <= k[p[i]] && a[w[p[i]]].size() <= (s / w[p[i]]); ++j){
            a[w[p[i]]].eb(v[p[i]]);
        }
    }
}

int dp[S][2];
void solve(){
    FORU(i, 1, s) FORU(j, 0, s){
        if(j == 0 || dp[j][0]){
            int z = j, w = 0;
            for(int x: a[i]) if((z += i) <= s){
                dp[z][1] = max(dp[z][1], dp[j][0] + (w += x));
            }
        }
        dp[j][0] = dp[j][1];
    }
    int ans = 0;
    FORU(i, 0, s) ans = max(ans, dp[i][1]);
    cout << ans;
}

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

컴파일 시 표준 에러 (stderr) 메시지

knapsack.cpp: In function 'void enter()':
knapsack.cpp:44:58: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   44 |         for(int j = 1; j <= k[p[i]] && a[w[p[i]]].size() <= (s / w[p[i]]); ++j){
      |                                        ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
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...