Submission #911652

#TimeUsernameProblemLanguageResultExecution timeMemory
911652anastasiskolioKnapsack (NOI18_knapsack)C++14
37 / 100
1027 ms19544 KiB
#include <bits/stdc++.h> using namespace std; int main() { int S, N; scanf("%d%d", &S, &N); int V[N + 1]; int W[N + 1]; int K[N + 1]; int M = 0; for (int i = 1; i <= N; i++) { scanf("%d%d%d", &V[i], &W[i], &K[i]); M += K[i]; } int Weight[M + 1]; int Value[M + 1]; int m = 0; for (int i = 1; i <= N; i++) { int j = K[i]; while (j > 0) { m++; Weight[m] = W[i]; Value[m] = V[i]; j--; } } /*int dp[2][S + 1]; dp[1][0] = 0; for (int s = 0; s <= S; s++) dp[0][s] = 0; for (int i = 1; i <= M; i++) { for (int s = 1; s <= S; s++) { if (s - Weight[i] < 0) dp[i%2][s] = dp[(i - 1)%2][s]; else dp[i%2][s] = max(dp[(i - 1)%2][s], dp[(i - 1)%2][s - Weight[i]] + Value[i]); } } printf("%d\n", dp[M%2][S]);*/ int dp[2][S + 1]; dp[1][0] = 0; for (int s = 0; s <= S; s++) dp[0][s] = 0; for (int i = 1; i <= M; i++) { for (int s = 1; s <= S; s++) { dp[i%2][s] = dp[(i - 1)%2][s]; if (s - Weight[i] >= 0) dp[i%2][s] = max(dp[i%2][s], dp[(i - 1)%2][s - Weight[i]] + Value[i]); } } printf("%d\n", dp[M%2][S]); return 0; }

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:7:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    7 |   scanf("%d%d", &S, &N);
      |   ~~~~~^~~~~~~~~~~~~~~~
knapsack.cpp:14:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 |     scanf("%d%d%d", &V[i], &W[i], &K[i]);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...