Submission #1086674

#TimeUsernameProblemLanguageResultExecution timeMemory
1086674quangminh412Knapsack (NOI18_knapsack)C++14
17 / 100
135 ms262144 KiB
#include<bits/stdc++.h> using namespace std; #define faster() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define ll long long const int maxn = 1e5 + 9; ll s; int n; ll v[maxn], w[maxn], k[maxn]; int main() { if (fopen("BALO.INP", "r")) { freopen("BALO.INP", "r", stdin); freopen("BALO.OUT", "w", stdout); } faster(); cin >> s >> n; bool sub2 = true; for (int i = 1; i <= n; i++) { cin >> v[i] >> w[i] >> k[i]; if (k[i] != 1) sub2 = false; } if (n == 1) { ll num = s / w[1]; cout << num * v[1] << '\n'; return 0; } else if (sub2 == true) { vector<vector<ll>> dp(n + 5, vector<ll>(s + 5, 0)); ll ans = 0; for (int i = 1; i <= n; i++) for (int j = 0; j <= s; j++) { dp[i][j] = dp[i - 1][j]; if (j - w[i] >= 0) dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i]] + v[i]); if (i == n) ans = max(ans, dp[i][j]); } cout << ans << '\n'; return 0; } ll dp[n + 5][s + 5][s + 5]; ll mx[n + 5][s + 5]; memset(dp, sizeof(dp), 0); memset(mx, sizeof(mx), 0); ll ans = 0; for (int i = 1; i <= n; i++) for (int num = 0; num <= s; num++) for (int j = s; j >= 0; j--) { dp[i][num][j] = mx[i - 1][j]; if (num > k[i]) continue; if (j - num * w[i] >= 0) dp[i][num][j] = max(dp[i][num][j], mx[i - 1][j - num * w[i]] + num * v[i]); mx[i][j] = max(mx[i][j], dp[i][num][j]); if (i == n) ans = max(ans, dp[i][num][j]); } cout << ans << '\n'; return 0; }

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:56:27: warning: 'memset' used with constant zero length parameter; this could be due to transposed parameters [-Wmemset-transposed-args]
   56 |   memset(dp, sizeof(dp), 0);
      |                           ^
knapsack.cpp:57:27: warning: 'memset' used with constant zero length parameter; this could be due to transposed parameters [-Wmemset-transposed-args]
   57 |   memset(mx, sizeof(mx), 0);
      |                           ^
knapsack.cpp:18:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |   freopen("BALO.INP", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:19:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |   freopen("BALO.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...