-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolve.cpp
More file actions
39 lines (32 loc) · 844 Bytes
/
Copy pathsolve.cpp
File metadata and controls
39 lines (32 loc) · 844 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
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main () {
while(true){
int m,n;cin>>m>>n;
if(m==0&&n==0)break;
vector<int> cost(n+1),fun(n+1);
for(int i=1;i<=n;i++){
cin>>cost[i]>>fun[i];
}
vector<int> dp(m+5, 0);
for(int i=1;i<=n;i++){
for(int j=m;j>=cost[i];j--){
int candidate = dp[j-cost[i]] + fun[i];
if(candidate > dp[j]){
dp[j] = candidate;
// dp[j].second = dp[j].second+cost[i];
}
}
}
int mxfun=dp[m];
int mncost=0;
for(int j=0;j<=m;j++){
if(dp[j]==mxfun){
mncost=j;
break;
}
}
cout<<mncost<<" "<<dp[m]<<endl;
}
}