-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlower.java
More file actions
103 lines (90 loc) · 3.31 KB
/
Flower.java
File metadata and controls
103 lines (90 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
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 B 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 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.
Sample Input :
5 100
100 40
50 40
40 50
60 20
100 50
Sample Output :
90 210
Explanation :
Profit for each of the plants is 60, 10, -10, 40, 50 respectively. He has Rs. 100 with him.
The optimal selection would be fertilize first and last plant.
So Minimum Amount to spend is Rs. 90 and
total money he has after selling the plants is 100 - 40 - 50 + 100 + 100 =210
*/
import java.util.*;
class Flower{
int x,y;
Flower(int x,int y){
this.x = x;
this.y = y;
}
@Override
public String toString(){
return "[ "+this.x +" "+this.y+" ]";
}
}
class Flower{
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), P = sc.nextInt(),n1=n,i=0,k=0;
Flower arr[] = new Flower[n];
while(i<n){
int x = sc.nextInt(),y = sc.nextInt();
if(x <= y){ n1--; }
else arr[k++] = new Flower(x,y);
i++;
}
getMax(arr,P,n1);
}
static void getMax(Flower arr[],int P,int n){
int k=0;
Flower dp1[] = new Flower[P+1];
int max=Integer.MIN_VALUE,min = Integer.MAX_VALUE;
for(int i=0;i<=P;i++) dp1[i] = new Flower(0,0);
while(k<n){
Flower dp2[] = new Flower[P+1];
for(int i=0;i<=P;i++){
if(arr[k].y > i) dp2[i] = dp1[i];
else{
int takecost = arr[k].y + dp1[i - arr[k].y].y, dtakecost = dp1[i].y;
int take = arr[k].x + dp1[i - arr[k].y].x, dtake = dp1[i].x;
if(take >= dtake ){
dp2[i] = new Flower(take, take == dtake ? Math.min(takecost,dtakecost): takecost);
if(max < dp2[i].x + P - takecost){
max = dp2[i].x + P - takecost;
min = dp2[i].y;
}
else if(max == take + P - takecost) min = Math.min(min,dp2[i].y);
}
else dp2[i] = dp1[i];
}
}
/*for(int l=10;l<=P;l=l+10)
System.out.print(dp2[l]+" ");
System.out.println();*/
dp1 = dp2;
k++;
}
System.out.println(min+ " "+max);
}
}