Question Name:ROY & FLOWER FARM

#include <bits/stdc++.h> 
#define int long long
#define fi first
#define se second
#define pb push_back
using namespace std; 
int mod=1e9+7;
int gcd(int a,int b){
	if(b==0)return a;
	return  gcd(b,a%b);
}
 
 
int32_t main(){
	ios_base::sync_with_stdio(false);
	int t;
	cin>>t;
	while(t--)
	{
		int n,p;
		cin>>n>>p;
		vector<pair<int,int> >v;
		v.pb({0,0});
		for(int i=1;i<=n;i++)
		{
		    int x,y;
		    cin>>x>>y;
		    if(x-y>=0)
		    {
		        v.pb({x-y,y});
		    }
		}
        n=v.size()-1;
		int dp[n+1][p+1];
		memset(dp,0,sizeof(dp));
 
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=p;j++)
			{
				if(j>=v[i].se)
				{
					dp[i][j]=max(dp[i-1][j],dp[i-1][j-v[i].se]+v[i].fi);
				}
				else
				    dp[i][j]=dp[i-1][j];
			   	//cout<<dp[i][j]<<" ";
			}//cout<<endl;
		}
		int spend=0,have=p;
		for(int i=0;i<=p;i++)
		{
		    //cout<<dp[n][i]<<" ";
		    if(dp[n][i]+p>have)
		    {
		        have=dp[n][i]+p;
		        spend=i;
		    }
		}//cout<<endl;
		cout<<spend<<" "<<have<<endl;
		//cout<<dp[n][p]<<endl;
		
	}
 
    return 0;
}
  • Problem Description
    Roy is the owner of a flower plant farm and sells flower plants for a living. Now the problem with flower plants is that they wither in a night if not taken care of. So at the end of the day, to make flower plants wither-resistant Roy uses special fertilizers.

    There are N number of plants not sold on one particular day (say today), but Roy can sell them the next day (say tomorrow) if he fertilizes them tonight. 

    Roy sells a flower plant for Rs. X. Cost of fertilizing a plant is Rs. Y. Currently Roy has P Rupees with him.

    Given the selling price X and fertilizing cost Y of N plants, your task is to find minimum amount A (A P) that he should spend in fertilizing plants such that the total money he has after selling plants on the next day is maximized. Assume that all the fertilized plants are sold on the next day.

    Input Format:

    First line contains integer T – number of test cases.
    Second line contains two space separated integers N and P.
    Following N lines each contain two space separated integers X and Y.

    Output Format:
    Print the two space separated integers A and B in a new line for each test case.

    Constraints:
    1<= T<= 10
    1 <= N <= 100
    1 <= X,Y <= 1000 
    1<= P<= 10000
  • Test Case 1
    Input (stdin)2
    2 50
    80 40
    60 20
    5 100
    100 40
    50 40
    40 50
    60 20
    100 50
    Expected Output20 90
    90 210
  • Test Case 2
    Input (stdin)10
    4 56
    38 63
    54 11
    21 31
    55 65
    1 13
    89 67
    4 187
    24 11
    89 87
    97 22
    62 97
    3 100
    82 92
    85 71
    1 28
    4 233
    59 95
    4 66
    24 89
    24 32
    2 61
    15 52
    98 67
    2 97
    75 6
    19 94
    3 57
    3 41
    24 73
    36 17
    2 18
    12 98
    36 51
    2 69
    76 92
    38 95
    Expected Output11 99
    0 13
    120 277
    71 114
    0 233
    0 61
    6 166
    17 76
    0 18
    0 69

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
CHP Adblock Detector Plugin | Codehelppro