제출 #499807

#제출 시각아이디문제언어결과실행 시간메모리
499807mewnianKnapsack (NOI18_knapsack)C++14
73 / 100
1097 ms3912 KiB
//#pragma GCC optimize("Ofast") //#pragma GCC optimize("unroll-loops") //#pragma GCC target("avx,avx2,fma") #include <bits/stdc++.h> #define sze(x) (ll) x.size() #define idx(x, a) get<x>(a) #define LID(x) (x << 1LL) #define RID(x) (x << 1LL) + 1LL #define ID(x) (x + MAXN) #define CONV(x) (x - MAXN) #define countbit(x) __builtin_popcountll(x) #define pb push_back #define mp make_pair #define x first #define y second using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<ll, ll> pi; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rng); } const ll MAXN = 100'003; const ll MAXS = 2003; const ll INF = 1e18; const ld EPS = 1e-9; ll n, s, v[MAXN], w[MAXN], k[MAXN]; ll dp0[MAXS], dp1[MAXS]; void solve() { fill(dp0, dp0 + s + 1, 0); for (ll i = 1; i <= n; ++i) { vector< deque<ll> > q(w[i]); for (ll sn = 0; sn <= s; ++sn) { dp0[sn] -= (sn / w[i]) * v[i]; ll grp = sn % w[i], bnd = sn - k[i] * w[i]; while (!q[grp].empty() && q[grp].front() < bnd) q[grp].pop_front(); while (!q[grp].empty() && dp0[q[grp].back()] < dp0[sn]) q[grp].pop_back(); ll lev = (sn / w[i]) * v[i]; if (!q[grp].empty()) dp1[sn] = lev + dp0[q[grp].front()]; // cerr << dp1[sn] << " "; q[grp].push_back(sn); } for (ll sn = 0; sn <= s; ++sn) dp0[sn] = dp1[sn]; // cerr << endl; } cout << dp1[s]; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); #ifdef OFFLINE freopen("input.inp", "r", stdin); #endif cin >> s >> n; for (ll i = 1; i <= n; ++i) cin >> v[i] >> w[i] >> k[i]; solve(); }
#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...