1. 程式人生 > >01揹包整理 - 2: 模板

01揹包整理 - 2: 模板

#include<iostream>
#include<algorithm>
#include<cstring>
#include<stack>
#include<set>
#include<queue>
#include<deque>
#include<vector>
#include<cmath>
#include<bits/stdc++.h>

#define ll long long
#define debug(____x) cout << ____x << endl
#define
readfrom(____s) freopen(____s, "r", stdin) #define wrtto(____s) freopen(____s, "w", stdout) using namespace std; const int desinf=0x3f3f3f3f; const int inf=2147483647; const int maxn=1234; int n; //物品個數 int v; //揹包總容量 int c[maxn]; int w[maxn]; int dp[maxn]; int main () { cin >> n >> v;
for (int i = 0; i < n; i ++) cin >> c[i] >> w[i]; //c: 佔用位置, w: 錢數 for (int i = 0; i < n; i ++) for (int j = v; j > 0; j --) if(j - c[i] >= 0) dp[j] = max (dp[j], dp[j - c[i]] + w[i]); cout << dp[v] << endl; }