Submission #666547

#TimeUsernameProblemLanguageResultExecution timeMemory
666547I_love_Chou_GiangKnapsack (NOI18_knapsack)C++14
100 / 100
144 ms82828 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

#define vt vector
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()

#define R_EP(i, a, b, s) for (int i = (a); (s) > 0 ? i < (b) : i > (b); i += (s))
#define R_EP1(e) R_EP(i, 0, e, 1)
#define R_EP2(i, e) R_EP(i, 0, e, 1)
#define R_EP3(i, b, e) R_EP(i, b, e, 1)
#define R_EP4(i, b, e, s) R_EP(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define R_EPC(...) GET5(__VA_ARGS__, R_EP4, R_EP3, R_EP2, R_EP1)
#define rep(...) R_EPC(__VA_ARGS__)(__VA_ARGS__)
#define trav(x, a) for (auto x : a) 

template<typename T> inline bool umax(T&x, const T& y) { if (x >= y) return false; x = y; return true; }
template<typename T> inline bool umin(T&x, const T& y) { if (x <= y) return false; x = y; return true; }

const int MOD = int(1e9) + 7, naxm = 1000000;

ll dp[2005][2005], prefix[2005][2005];
int S, N, V[naxm + 5], W[naxm + 5], K[naxm + 5];
vt<int> opt[2005];

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

  cin >> S >> N;
  rep(i, 1, N + 1) cin >> V[i] >> W[i] >> K[i];

  vt<int> p(N, 0);
  iota(all(p), 1);
  sort(all(p), [&](const int& x, const int &y) {
    if (W[x] == W[y]) {
      return V[x] > V[y];
    }
    return W[x] < W[y];
  });

  trav(i, p) {
    while (sz(opt[W[i]]) < 2000 && K[i]--) {
      opt[W[i]].pb(V[i]);
    }
  } 

  rep(i, 1, S + 1) sort(opt[i].rbegin(), opt[i].rend());
  rep(i, 1, S + 1) {
    // cout << i << " " << sz(opt[i]) << "\n";
    rep(j, 1, sz(opt[i]) + 1) prefix[i][j] = prefix[i][j - 1] + opt[i][j - 1];
    // rep(j, sz(opt[i]) + 1) cout << prefix[i][j] << " \n"[j == sz(opt[i])];
  }

  ll ans = 0;
  rep(i, 1, S + 1) {
    rep(j, 0, min(S / i, sz(opt[i])) + 1) {
      rep(k, S, i * j - 1, -1) {
        umax(dp[i][k], dp[i - 1][k - i * j] + prefix[i][j]);
        // if (dp[i][k] == 19) cout << i << " " << j << "\n";
        umax(ans, dp[i][k]);
      }
    }
    // cout << ans << "\n";
  }

  // cout << dp[15][17] << "\n";

  cout << ans << "\n";

  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...