Submission #1256988

#TimeUsernameProblemLanguageResultExecution timeMemory
1256988cjspd_olyKnapsack (NOI18_knapsack)C++17
100 / 100
61 ms33096 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using vi = vector<int>; using vvi = vector<vi>; using pii = pair<int, int>; using pll = pair<ll, ll>; using vii = vector<pii>; using vll = vector<ll>; using vvll = vector<vll>; using vllll = vector<pll>; #define pb push_back #define eb emplace_back #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) (int)(x).size() void setIO(string name = "") { ios_base::sync_with_stdio(0); cin.tie(0); if (!name.empty()) { (void)freopen((name + ".in").c_str(), "r", stdin); (void)freopen((name + ".out").c_str(), "w", stdout); } } const ld pi = 3.14159265358979323846; const ll LINF = 1e18; const ll INF = 1e9; const ll MOD = 1e9 + 7; // const ll MOD = 998244353; const ll MAXN = 2e5 + 5; void solve() { int S, N; cin >> S >> N; map<int, vii> by_weight; for (int i = 0; i < N; i++) { int value, weight, qty; cin >> value >> weight >> qty; if (weight <= S && qty > 0) by_weight[weight].pb({value, qty}); } vvll best(by_weight.size() + 1, vll(S + 1, -1)); best[0][0] = 0; int at = 1; // best[?][]; at represent `?` which group of weight, like a coordinate compression; 1-based indexing for (auto &[w, items] : by_weight) { sort(all(items), greater<pii>()); for (int i = 0; i <= S; ++i) { best[at][i] = best[at - 1][i]; int copies = 0, type_at = 0, curr_used = 0; ll profit = 0; while ((copies + 1) * w <= i && type_at < items.size()) { ++copies; profit += items[type_at].first; // if (best[at - 1][i - copies * w] != -1) best[at][i] = max(best[at][i], best[at - 1][i - copies * w] + profit); ++curr_used; if (curr_used == items[type_at].second) curr_used = 0, ++type_at; } } ++at; } cout << *std::max_element(all(best.back())); } int main() { setIO(""); int t = 1; // cin >> t; while (t--) solve(); return 0; }

Compilation message (stderr)

knapsack.cpp: In function 'void setIO(std::string)':
knapsack.cpp:28:22: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |         (void)freopen((name + ".in").c_str(), "r", stdin);
      |               ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:29:22: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   29 |         (void)freopen((name + ".out").c_str(), "w", stdout);
      |               ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...