Submission #598603

#TimeUsernameProblemLanguageResultExecution timeMemory
598603bojackduyKnapsack (NOI18_knapsack)C++14
73 / 100
1077 ms3928 KiB
#include<iostream> #include<queue> #include<stack> #include<algorithm> #include<string.h> #define int long long #define size() size() * 1ll #define all(x) (x).begin(), (x).end() #define allr(x, sz) (x) + 1, (x) + 1 + sz #define pb push_back #define pii pair<int,int> #define fi first #define se second #define MASK(x) (1LL<<(x)) #define BIT(x,i) (((x)>>(i))&1) #define numbit(x) __builtin_popcountll(x) using namespace std; const int N = 1e5 + 1; const int M = 2e3 + 1; const long long mod = 1e9 + 7; const long long oo = 1e18 + 7; typedef vector<int> vi; typedef vector<pii> vii; typedef long long ll; template<class t> bool mini(t &x,t y) { if (y < x) { x = y; return 1; } return 0; } template<class t> bool maxi(t &x,t y) { if (x < y) { x = y; return 1; } return 0; } void file() { ios_base::sync_with_stdio(0); cin.tie(0); // freopen(task".inp", "r", stdin); // freopen(task".out", "w", stdout); return ; } int n, m; int v[N], w[N], k[N]; int dp[2][M]; void DP(int i) { vi f(m + 1, 0); for (int j = w[i]; j <= m; j++) { for (int it = 1; it <= min(j / w[i], k[i]); it++) { maxi(f[j], dp[i & 1][j - w[i] * it] + v[i] * it); } } for (int j = w[i]; j <= m; j++) maxi(dp[i & 1][j], f[j]); } void solve(int test = -1) { cin >> m >> n; for (int i = 1; i <= n; i++) { cin >> v[i] >> w[i] >> k[i]; } for (int i = 1; i <= n; i++) { for (int j = 0; j <= m; j++) { maxi(dp[i & 1][j], dp[(i - 1) & 1][j]); } DP(i); } cout << dp[n&1][m]; } int32_t main() { file(); int T = 1; // cin >> T; for (int i = 1; i <= T; i++) { solve(i); } return 0; } /* 20 3 5000 15 1 100 1 3 50 1 4 WRONG 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5000 5000 5000 5000 5000 5000 0 100 200 200 200 200 200 200 200 200 200 200 200 200 200 5000 5000 5200 5200 5200 5200 0 100 200 200 300 350 350 350 350 350 350 350 350 350 350 5000 5000 5200 5200 5300 5350 RIGHT 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5000 5000 5000 5000 5000 5000 0 100 200 300 300 300 300 300 300 300 300 300 300 300 300 5000 5100 5200 5300 5300 5300 0 100 200 300 350 400 450 500 500 500 500 500 500 500 500 5000 5100 5200 5300 5350 5400 */
#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...