제출 #1192507

#제출 시각아이디문제언어결과실행 시간메모리
1192507dprtoKnapsack (NOI18_knapsack)C++20
0 / 100
1 ms328 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define x first
#define y second
#define MASK(i) (1 << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define f(i, l, r) for(int i = l; i <= r; ++i)
const int mod = 1e9 + 7;
const int ars = 2e3 + 5;
const ll infll = 1e18;


int s, n, v[ars], w[ars], k[ars], dp[ars];
vector<pair<int, int>> adj[ars];
signed main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    freopen("Templinp.txt", "r", stdin);
    freopen("Templout.txt", "w", stdout);
    cin >> s >> n;
    
    for(int i = 1; i <= n; ++i){
        int v, w, k;
        cin >> v >> w >> k;
        if (w > s) continue;
        adj[w].push_back({v, k});
    }

    for(int i = 0; i <= s; ++i) dp[i] = -1;
    dp[0] = 0;

    for(int w = 1; w <= s; ++w){
        if(adj[w].empty()) continue;
        sort(adj[w].begin(), adj[w].end(), greater<pair<int, int>>());

        vector<int> items;
        for(auto [v, k] : adj[w]){
            while (k--) items.push_back(v);
        }

        for(int v : items){
            for(int i = s - w; i >= 0; --i){
                if(dp[i] != -1){
                    dp[i + w] = max(dp[i + w], dp[i] + v);
                }
            }
        }
    }

    int res = 0;
    for(int i = 0; i <= s; ++i){
        res = max(res, dp[i]);
    }
    cout << res << "\n";


    return 0;
}

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

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