Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int dp[100][100];
- int main()
- {
- //freopen("in.txt","r",stdin);
- //freopen("out.txt","w",stdout);
- int weight[100],price[100],t,i,j,k,capacity,e,n;
- cin>>t;
- while(t--)
- {
- cin>>e;
- cin>>capacity;
- cin>>n;
- capacity-=e;
- for(i=1; i<=n; i++)
- {
- cin>>price[i];
- cin>>weight[i];
- }
- for(i=0; i<100; i++)
- {
- for(j=0; j<100; j++)
- dp[i][j]=100000000;
- }
- dp[0][0]=0;
- for(i=1; i<=n; i++)
- {
- for(j=0; j<=capacity; j++)
- {
- if(j<weight[i])
- {
- dp[i][j]=dp[i-1][j];
- }
- else
- {
- dp[i][j]=min(dp[i][j-weight[i]]+price[i],dp[i][j]);
- }
- }
- }
- for(i=0;i<=n;i++)
- {
- for(j=0;j<=capacity;j++)
- cout<<dp[i][j]<<" ";
- cout<<endl;
- }
- if(dp[n][capacity]==100000000)
- cout<<"This is impossible."<<endl;
- else
- cout<<"The minimum amount of money in the piggy-bank is "<<dp[n][capacity]<<"."<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement