Submission #968544

# Submission time Handle Problem Language Result Execution time Memory
968544 2024-04-23T15:14:49 Z vjudge1 Knapsack (NOI18_knapsack) C++17
0 / 100
128 ms 262148 KB
#include<bits/stdc++.h>
using namespace std;

const int MAXS = 2000;
const int MAXITEM = 20000;
const long long INF = 1e15;

int S, N;
vector<pair<int, int> > A[MAXS + 5];
vector<pair<int, int> > items;
long long DP[MAXITEM + 5][MAXS + 5];

long long f(int idx, int cap) {
    if (cap < 0) return -INF;
    if (idx == items.size()) return 0;
    long long &ret = DP[idx][cap];
    if (ret != -1) return ret;
    auto [w, v] = items[idx];
    return ret = max(f(idx + 1, cap), f(idx + 1, cap - w) + v);
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> S >> N;
    for (int i = 0; i < N; i++) {
        int v, w, k;
        cin >> v >> w >> k;
        A[w].push_back({v, k});
    }
    for (int w = 1; w <= MAXS; w++) {
        sort(A[w].begin(), A[w].end());
        int used = MAXS / w + 1;
        while (used != 0 && !A[w].empty()) {
            auto &[v, k] = A[w].back();
            items.push_back({w, v});
            k--;
            if (k == 0) A[w].pop_back();
        }
    }
    memset(DP, -1, sizeof DP);
    cout << f(0, S) << endl;
    return 0;
}

Compilation message

knapsack.cpp: In function 'long long int f(int, int)':
knapsack.cpp:15:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   15 |     if (idx == items.size()) return 0;
      |         ~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 128 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 39 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 39 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 128 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 128 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -