Submission #849086

#TimeUsernameProblemLanguageResultExecution timeMemory
849086AbdalrhmanMohammadKnapsack (NOI18_knapsack)C++14
73 / 100
561 ms41132 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 pr(v) v.begin(), v.end()
#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];
vector<vector<ll>> v;
vector<vector<ll>> mem;
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 (ll j = 1; j <= min(2000LL, v[i][2]); j++)
    res = max(res, fun(i + 1, rem - j * v[i][1]) + v[i][0] * j);
  return res;
}
int main() {
  FAST

  cin >> s >> n;
  mem = vector<vector<ll>>(n + 1, vector<ll>(s + 1));
  map<int, vector<pair<int, int>>> mp;
  for (int i = 0; i < n; i++) {
    cin >> V[i] >> W[i] >> K[i];
    mp[W[i]].push_back({V[i], K[i]});
  }
  for (pair<int, vector<pair<int, int>>> p : mp) {
    vector<pair<int, int>> &tmp = p.second;
    int wei = p.first;
    sort(pr(tmp));
    reverse(pr(tmp));
    int sum = 0;
    for (int i = 0; i < tmp.size(); i++) {
      if (i > 0) {
        if (v.back()[0] == tmp[i].first) {
          v.back()[2] += max(0, min(tmp[i].second, 2000 - sum));
          sum += tmp[i].second;
        } else {
          v.push_back(
              {tmp[i].first, wei, max(0, min(tmp[i].second, 2000 - sum))});
          sum += tmp[i].second;
        }
      } else {
        v.push_back(
            {tmp[i].first, wei, max(0, min(tmp[i].second, 2000 - sum))});
        sum += tmp[i].second;
      }
    }
  }
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= s; j++)
      mem[i][j] = -1;
  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 'int main()':
knapsack.cpp:53:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |     for (int i = 0; i < tmp.size(); 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...