Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- //#include <bits/stdc++.h>
- using namespace std;
- int main()
- {
- ios_base::sync_with_stdio(true);
- int n,W;
- cin>>n>>W;
- long long dp[n+1][W+1];
- for(int i=0;i<=n;i++)
- {
- for(int j=0;j<=W;j++)
- {
- dp[i][j]=0;
- }
- }
- int x[n][2];
- for(int i=0;i<n;i++)
- {
- for(int j=1;j>=0;j--)cin>>x[i][j];
- }
- 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 w=W;w>=0;w--)
- {
- dp[i + 1][w] = max(dp[i + 1][w], dp[i][w]);
- if(w - x[i][1] >= 0) {
- dp[i + 1][w - x[i][1]] = max(dp[i + 1][w - x[i][1]], dp[i][w] + x[i][0]);
- }
- }
- }
- long long ans=0;
- for(int j=0;j<=W;j++)
- {
- ans=max(dp[n][j],ans);
- }
- cout<<ans<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement