# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1096037 | InvMOD | Knapsack (NOI18_knapsack) | C++14 | 1066 ms | 1372 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define gcd __gcd
#define siz(v) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
///#define int long long
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}
const int N = 2e3+1;
int S, n;
ll dp[N], answer;
void process(ll v, ll w){
if(w > S) return;
for(int i = S; i >= w; i--){
ckmx(dp[i], dp[i-w] + v);
ckmx(answer, dp[i]);
}
return;
}
void solve()
{
cin >> S >> n;
for(int i = 1; i <= n; i++){
ll v,w,k; cin >> v >> w >> k;
k = min(k, S/w+1);
for(int j = 0; j < (32 - __builtin_clz(k)); j++){
if(k >= (1<<j)){
k -= (1<<j);
if(w * (1ll<<j) > S) break;
process(v * (1ll<<j), w * (1ll<<j));
}
else break;
}
if(k){
if(w * k > S) continue;
process(v * k, w * k);
}
}
cout << answer <<"\n";
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP","r",stdin);
freopen(name".OUT","w",stdout);
}
int t = 1; //cin >> t;
while(t--) solve();
return 0;
}
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... |