-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathPARTY-Party_Schedule(SPOJ).cpp
More file actions
57 lines (53 loc) · 907 Bytes
/
PARTY-Party_Schedule(SPOJ).cpp
File metadata and controls
57 lines (53 loc) · 907 Bytes
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
#include <bits/stdc++.h>
#define lli long long int
#define endl "\n"
#define MAX 1000005
#define MOD 1000000007
using namespace std;
void knapsack(int cost[], int fun[], int p, int n)
{
int dp[n+1][p+1];
for(int i=0; i<n+1; i++)
dp[i][0] = 0;
for(int i=0; i<p+1; i++)
dp[0][i] = 0;
for(int i =1; i<n+1; i++)
{
for(int j =1; j<p+1; j++)
{
if(cost[i-1] <= j)
dp[i][j] = max((fun[i-1] + dp[i-1][j - cost[i-1]]), dp[i-1][j]);
else
dp[i][j] = dp[i-1][j];
}
}
int max_ans = dp[n][p];
int idx = INT_MAX;
for(int i =0; i<n+1; i++)
{
for(int j =0; j<p+1; j++)
{
if(dp[i][j] == max_ans)
{
if(idx > j)
idx = j;
}
}
}
cout<<idx<<" "<<max_ans<<endl;
}
int main()
{
while(1)
{
int p, n;
cin>>p>>n;
if((p == 0) && (n==0))
break;
int cost[n], fun[n];
for(int i =0; i<n; i++)
cin>>cost[i]>>fun[i];
knapsack(cost, fun, p, n);
}
return 0;
}