Submission #484677

#TimeUsernameProblemLanguageResultExecution timeMemory
484677QkakeKnapsack (NOI18_knapsack)C++17
73 / 100
145 ms262148 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pi; typedef pair<ll, int> pli; typedef pair<int, ll> pil; typedef pair<ll,ll> pl; typedef pair<ld,ld> pd; typedef vector<int> vi; typedef vector<bool> vb; typedef vector<ld> vd; typedef vector<ll> vl; typedef vector<pi> vpi; typedef vector<pl> vpl; typedef vector<pli> vli; typedef vector<pil> vil; #define mp make_pair #define pb push_back #define f first #define s second #define lb lower_bound #define ub upper_bound #define sz(x) (int)x.size() #define all(x) begin(x), end(x) const int MOD = 1000000007; // 998244353 const ll INF = 1e18; const int oo = 1000000007; const int MX = 100001; const ld PI = 4 * atan((ld)1); int n, s; vi v, w, t; bool avai; vpi val[MX]; set<int> p; void solve() { cin >> s >> n; v.resize(n + 1); w.resize(n + 1); t.resize(n + 1); avai = false; for (int i = 1; i <= n; i++) { cin >> v[i] >> w[i] >> t[i]; if (t[i] != 1) avai = true; val[w[i]].pb({v[i], t[i]}); p.insert(w[i]); } if (!avai) { vector<vl> dp(n + 1, vl(s + 1, 0)); ll res = 0; for (int i = 0; i < n; i++) for (int j = 0; j <= s; j++) { dp[i + 1][j] = dp[i][j]; if (j - w[i + 1] >= 0) dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i + 1]] + v[i + 1]); } for (int j = 0; j <= s; j++) res = max(res, dp[n][j]); cout << res; return; } vector<vl> dp(sz(p) + 1, vl(s + 1, 0)); int i = 0; for (int wi = 0; wi <= s; wi++) if (sz(val[wi])) { vpi sta = val[wi]; sort(all(sta), greater<pi>()); for (int j = 0; j <= s; j++) { dp[i + 1][j] = dp[i][j]; int times = 1, cnt = 1, used = 0; ll value = 0; while (j - times * wi >= 0 && used < sz(sta)) { value += sta[used].f; dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - times * wi] + value); if (cnt == sta[used].s) { used++; cnt = 0; } times++; cnt++; } } i++; } ll res = 0; for (int j = 0; j <= s; j++) res = max(res, dp[sz(p)][j]); cout << res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); solve(); 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...