# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1277047 | vibeduck | Knapsack (NOI18_knapsack) | C++20 | 263 ms | 40292 KiB |
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define int long long
#define all(x) (x).begin(), (x).end()
typedef long double ld;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<ll>> vvll;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef map<int, int> mii;
typedef unordered_map<int, int> umii;
const int mod = 1e9 + 7;
const int inf = INTMAX_MAX;
const bool tc = false;
int s, n;
const int mxn = 101, mxs = 2002;
// int v[mxn], w[mxn], k[mxn];
int dp[mxs][mxs]; //mii tmp[mxn];
mii tmp[mxs]; vector<pii> item[mxs];
inline void solve() {
cin >> s >> n;
for (int i = 0; i < n; i++) {
int v, w, k;
cin >> v >> w >> k;
k = min(k, (int)2000);
tmp[w][-v] += k;
}
for (int i = 1; i <= s; i++) {
for (auto &x : tmp[i]) item[i].pb({-x.fi, x.se});
}
// for (int i = 1; i <= s; i++) {
// if (!item[i].size()) continue;
// cout << i << ": \n";
// for (auto &x : item[i]) cout << x.fi << " " << x.se << '\n';
// cout << '\n';
// }
if (item[1].size()) {
int tkn = 0; int vl = 0; int ptr = 0;
for (int i = 1; i <= 2000; i++) {
if (item[1][ptr].se == 0) {
if (ptr == item[1].size() - 1) break;
ptr++;
}
tkn += 1;
vl += item[1][ptr].fi;
item[1][ptr].se--;
if (tkn > s) break;
dp[1][s - tkn] = vl;
}
}
// for (int i = 1; i <= s; i++) {
// if (!item[i].size()) continue;
// cout << i << ": \n";
// for (auto &x : item[i]) cout << x.fi << " " << x.se << '\n';
// cout << '\n';
// }
for (int i = 2; i <= 2000; i++) {
// i is current weight type
for (int j = 0; j <= s; j++) {
// j is how much is left afterwards in knapsack
int tkn = 0, vl = 0, ptr = 0;
vector<pii> og = item[i];
dp[i][j] = dp[i - 1][j];
if (item[i].size()) {
// cout << i << " weight with " << j << " left: ";
for (int nm = 0; nm <= 2000; nm++) {
// nm is how many we took of current
// cout << item[i][ptr].fi << " " << item[i][ptr].se << " aa: ";
if (item[i][ptr].se == 0) {
if (ptr == item[i].size() - 1) break;
ptr++;
}
tkn += i;
vl += item[i][ptr].fi;
item[i][ptr].se--;
if (j + tkn > s) break;
//cout << j + tkn << " aa\n";
dp[i][j] = max(dp[i][j], dp[i - 1][j + tkn] + vl);
}
// cout << dp[i][j] << "\n";
}
item[i] = og;
}
}
// for (int i = 1; i <= s; i++) {
// cout << i << ": ";
// for (int j = 0; j <= s; j++) cout << dp[i][j] << " ";
// cout << '\n';
// }
int ans = 0;
for (int i = 0; i <= s; i++) ans = max(ans, dp[2000][i]);
cout << ans << '\n';
}
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
signed main() {
ios::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
//setIO();
int t = 1;
if (tc) {
cin >> t;
}
while (t--) {
solve();
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |