-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDP-Party Problem-Coding Ninjas CP Course
68 lines (61 loc) · 2.55 KB
/
DP-Party Problem-Coding Ninjas CP Course
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
PARTY - Problem
Send Feedback
You just received another bill which you cannot pay because you lack the money.
Unfortunately, this is not the first time to happen, and now you decide to investigate the cause of your constant monetary shortness. The reason is quite obvious: the lion's share of your money routinely disappears at the entrance of party localities.
You make up your mind to solve the problem where it arises, namely at the parties themselves. You introduce a limit for your party budget and try to have the most possible fun with regard to this limit.
You inquire beforehand about the entrance fee to each party and estimate how much fun you might have there. The list is readily compiled, but how do you actually pick the parties that give you the most fun and do not exceed your budget?
Write a program which finds this optimal set of parties that offer the most fun. Keep in mind that your budget need not necessarily be reached exactly. Achieve the highest possible fun level, and do not spend more money than is absolutely necessary.
Input Format:
First line of input will contain an integer N (number of parties).
Next line of input will contain N space-separated integers denoting the entry fee of Ith party.
Next line will contain N space-separated integers denoting the amount of fun Ith party provide.
Last line of input will contain an integer W party budget.
Output Format:
For each test case your program must output the sum of the entrance fees and the sum of all fun values of an optimal solution. Both numbers must be separated by a single space.
Note: In case of multiple cost provides the maximum fun output the minimum total cost.
Sample Input:
5
1 7 9 7 2
5 5 2 4 7
12
Sample Output:
10 17
#include<bits/stdc++.h>
using namespace std;
int main(){
// write your code here
int n;cin>>n;
int fee[n],fun[n];
for(int i=0;i<n;i++)
cin>>fee[i];
for(int i=0;i<n;i++)
cin>>fun[i];
int w;
cin>>w;
int **dp=new int*[n+1];
for(int i=0;i<n+1;i++){
dp[i]=new int[w+1];
}
for(int i=0;i<n+1;i++){
dp[i][0]=0;
}
for(int i=0;i<w+1;i++){
dp[0][i]=0;
}
int maxfun=0;
int minweight=0;
for(int i=1;i<n+1;i++){
for(int j=1;j<w+1;j++){
dp[i][j]=dp[i-1][j];
if(fee[i-1]<=j){
dp[i][j]=max(dp[i][j],(fun[i-1]+dp[i-1][j-fee[i-1]]));
if(dp[i][j]>maxfun){
maxfun=dp[i][j];
minweight=j;
}
}
}
}
cout<<minweight<<" "<<maxfun;
return 0;
}