Submission #997529

#TimeUsernameProblemLanguageResultExecution timeMemory
997529vjudge1Knapsack (NOI18_knapsack)C++17
100 / 100
91 ms19540 KiB
#include <bits/stdc++.h> using namespace std; signed main() { int S, N, W, v, n, ans = -1e18; cin >> S >> N; vector<vector<pair<int, int>>> w(S + 1); for (int i = 0; i < N; i++) { cin >> v >> W >> n; w[W].push_back({-v, n}); } for (auto& i : w) { if (!i.size()) continue; sort(i.begin(), i.end()); } vector<vector<int>> dp(S + 1, vector<int>(S + 1, -1e18)); dp[0][0] = 0; for (int i = 1; i <= S; i++) { if (!w[i].size()) { dp[i] = dp[i - 1]; continue; } for (int j = 0; j <= S; j++) { dp[i][j] = dp[i - 1][j]; int a = 0; int ind = 0; int used = 0; int val = 0; while ((a + 1) * i <= j && ind < (int)w[i].size()) { a++; val += -w[i][ind].first; if (dp[i - 1][j - a * i] != -1e18) { dp[i][j] = max(dp[i][j], dp[i - 1][j - a * i] + val); } ans = max(ans, dp[i][j]); used++; if (used == w[i][ind].second) { used = 0; ind++; } } } } if (ans < 0) ans = 0; cout << ans; return 0; }

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:4:30: warning: overflow in conversion from 'double' to 'int' changes value from '-1.0e+18' to '-2147483648' [-Woverflow]
    4 |     int S, N, W, v, n, ans = -1e18;
      |                              ^~~~~
knapsack.cpp:15:54: warning: overflow in conversion from 'double' to 'std::vector<int>::value_type' {aka 'int'} changes value from '-1.0e+18' to '-2147483648' [-Woverflow]
   15 |     vector<vector<int>> dp(S + 1, vector<int>(S + 1, -1e18));
      |                                                      ^~~~~
#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...