Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<ctime>
- #include<map>
- #include<vector>
- #include<queue>
- using namespace std;
- typedef long long ll;
- ll dp[105][100005];
- int main()
- {
- int n, W;
- cin >> n >> W;
- vector<int> weight(n), value(n);
- for(int i = 0; i < n; i++) {
- cin >> weight[i] >> value[i];
- }
- for(int i = 0; i <= n; i++) {
- for(int j = 0; j <= W; j++) {
- dp[i][j] = -1e18;
- }
- }
- dp[0][W] = 0;
- for(int i = 0; i < n; i++) {
- for(int j = W; j >= 0; j--) {
- dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
- if(j - weight[i] >= 0) {
- dp[i + 1][j - weight[i]] = max(dp[i + 1][j - weight[i]], dp[i][j] + value[i]);
- }
- }
- }
- ll result = 0;
- for(int j = 0; j <= W; j++) {
- result = max(result, dp[n][j]);
- }
- cout << result << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement