Submission #849055

#TimeUsernameProblemLanguageResultExecution timeMemory
849055AbdalrhmanMohammadKnapsack (NOI18_knapsack)C++14
0 / 100
45 ms262144 KiB

#include <bits/stdc++.h>
using namespace std;

#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr);                                                            \
  cout.tie(nullptr);

#define ll long long
#define yn(tf) cout << (tf ? "YES\n" : "NO\n")
#define Endl endl
#define ld long double

const ld PI = 3.14159265359;
const ll MOD = 998244353;
const ll N = 1e5 + 7;

int s, n;
ll v[N], w[N], k[N];
ll mem[N][2000];
ll fun(int i, int rem) {
  if (rem < 0)
    return -1e15;
  if (i == n || rem == 0)
    return 0;
  ll &res = mem[i][rem];
  if (res != -1)
    return res;
  res = fun(i + 1, rem);
  for (int j = 1; j <= (rem / w[i], k[i]); j++)
    res = max(res, fun(i + 1, rem - j * w[i]) + v[i] * j);
  return res;
}
int main() {
  FAST
#ifndef ONLINE_JUDGE
      freopen("in.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
  cin >> s >> n;

  for (int i = 0; i < n; i++)
    cin >> v[i] >> w[i] >> k[i];

  memset(mem, -1, sizeof mem);
  cout << fun(0, s);
  return 0;
}

/*

*/

// BEFORE coding are you sure you understood the statement correctly?
// PLEASE do not forget to read the sample explanation carefully.
// WATCH out for overflows & RTs in general.
// TEST your idea or code on the corner cases.
// ANALYZE each idea you have thoroughly.

Compilation message (stderr)

knapsack.cpp: In function 'long long int fun(int, int)':
knapsack.cpp:31:29: warning: left operand of comma operator has no effect [-Wunused-value]
   31 |   for (int j = 1; j <= (rem / w[i], k[i]); j++)
      |                         ~~~~^~~~~~
knapsack.cpp: In function 'int main()':
knapsack.cpp:38:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |       freopen("in.txt", "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...