-
Notifications
You must be signed in to change notification settings - Fork 0
/
Diving for gold.cpp
70 lines (68 loc) · 1.26 KB
/
Diving for gold.cpp
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
#include<bits/stdc++.h>
using namespace std;
#define M 1001
int t,n,d[31],v[31],x,temp[31];
int dp[31][M],dir[31][M];
int knap(int i,int w)
{
if(i==n)
return 0;
if(dp[i][w]!=-1)
return dp[i][w];
int pro1=0,pro2=0;
if(w+d[i]<=t)
pro1=v[i]+knap(i+1,w+d[i]);
pro2=knap(i+1,w);
if(pro1>pro2)
{
dp[i][w]=pro1;
dir[i][w]=1;
}
else
{
dp[i][w]=pro2;
dir[i][w]=2;
}
return dp[i][w];
}
int main()
{
bool notfirst=false;
int i;
bool taken[31];
while(cin>>t>>x)
{
if(notfirst)
cout<<endl;
notfirst=true;
cin>>n;
x=3*x;
for(i=0;i<n;i++)
{
cin>>d[i]>>v[i];
temp[i]=d[i];
d[i]=d[i]*x;
taken[i]=false;
}
memset(dp,-1,sizeof(dp));
memset(dir,-1,sizeof(dir));
cout<<knap(0,0)<<endl;
int c=0,j=0;
for(i=0;i<n;i++)
{
if(dir[i][j]==1)
{
c++;
j+=d[i];
taken[i]=true;
}
}
cout<<c<<endl;
for(i=0;i<n;i++)
{
if(taken[i])
cout<<temp[i]<<' '<<v[i]<<endl;
}
}
return 0;
}