Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- long long 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 < 101; i++) {
- for(int j = 0; j < 100001; j++) {
- dp[i][j] = -1e18;
- }
- }
- dp[0][W] = 0;
- for(int i = 0; i < n; i++) {
- for(int w = W; w >= 0; w--) {
- if(w - weight[i] >= 0) {
- dp[i + 1][w - weight[i]] = max(dp[i + 1][w - weight[i]], dp[i][w] + value[i]);
- }
- dp[i + 1][w] = max(dp[i + 1][w], dp[i][w]);
- }
- }
- long long result = -1e18;
- for(int i = 0; i <= W; i++) {
- result = max(result, dp[n][i]);
- }
- cout << result << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement