Submission #1096040

#TimeUsernameProblemLanguageResultExecution timeMemory
1096040InvMODKnapsack (NOI18_knapsack)C++14
73 / 100
1091 ms1372 KiB
#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); 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)

knapsack.cpp: In function 'int main()':
knapsack.cpp:68:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |         freopen(name".INP","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:69:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |         freopen(name".OUT","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...