제출 #475633

#제출 시각아이디문제언어결과실행 시간메모리
475633vishnu_sujithKnapsack (NOI18_knapsack)C++17
49 / 100
1065 ms9140 KiB
#include <bits/stdc++.h>

using namespace std;

#ifdef DEBUG
#define LOG(...) cerr << "[" << #__VA_ARGS__ << "]: " << repr(__VA_ARGS__) << endl;
#define MSG(args) cerr << args << "\n";
#define debug(x) x
#else
#define LOG(...)
#define MSG(args)
#define debug(x)
#endif

#define mp make_pair
#define pb push_back
#define sz(x) (int((x).size()))
#define ms(x, v) memset((x), v, sizeof(x))
#define all(x) (x).begin(), (x).end()
#define endl '\n'

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<pii>;
using vvi = vector<vi>;

const int mxN = int(1e5) + 10;

int n, S;
int v[mxN], w[mxN], k[mxN];

struct chash {
  size_t operator()(const pii &x) const {
    return hash<ll>()(ll(x.first)^(ll(x.second)<<32));
  }
};

unordered_map<pii, int, chash> memo;

int solve(int i, int s) {
  auto key = mp(i, s);
  if (i >= n || s <= 0) return 0LL;
  else if (memo.find(key) != memo.end()) return memo[key];

  int res = 0;
  for (int j = 0; j <= k[i]; ++j) {
    if (s - (1LL * j * w[i]) >= 0)
      res = max(res, solve(i + 1, s - (1LL * j * w[i])) + v[i] * j);
    else
      break;
  }
  return memo[key] = res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> S >> n;
  for (int i = 0; i < n; ++i)
    cin >> v[i] >> w[i] >> k[i];
  cout << solve(0, S) << endl;

  return 0;
}
#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...