Submission #1027127

#TimeUsernameProblemLanguageResultExecution timeMemory
1027127lmaobruhKnapsack (NOI18_knapsack)C++14
29 / 100
5 ms4588 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
const int N = 1e6+5, M = 2005, inf = 2e9;

int s, n, v[N], w[N], k[N], dp1[M], dp2[M];

void sol() {
  cin >> s >> n;
  for (int i = 1; i <= n; ++i)
    cin >> v[i] >> w[i] >> k[i];
  for (int i = 1; i <= s; ++i) dp1[i] = -inf;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= s; ++j) dp2[j] = -inf;
    for (int j = 0; j <= s; ++j) {
      if (j + w[i+1] <= s) {
        int cnt = min(k[i+1], (s-j)/w[i+1]);
        chmax(dp2[j+w[i+1]*cnt], dp1[j]+cnt*v[i+1]);
      }
      chmax(dp2[j], dp1[j]);
    }
    if (i + 1 < n) swap(dp1, dp2);
  }
  int ans = 0;
  for (int j = 0; j <= s; ++j) if (dp2[j] < inf)
    chmax(ans, dp2[j]);
  cout << ans;
}

signed main() {
  cin.tie(0) -> sync_with_stdio(0);
  if (fopen("A.inp", "r")) freopen("A.inp", "r", stdin);
  int tc = 1, test = 0; // cin >> tc;
  while (++test <= tc) sol();
}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:35:35: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 |   if (fopen("A.inp", "r")) freopen("A.inp", "r", stdin);
      |                            ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
#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...