Kaidul

DATING A GIRL

Feb 22nd, 2014
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long i64;
  6. #define MAX_N 505
  7. #define MAX_W 505
  8. int n;
  9. int dp[MAX_N+1][MAX_W+1], memo[MAX_N+1][MAX_W+1];
  10. int weight[MAX_N+1];
  11. int cost[MAX_N+1];
  12. int CAP;
  13. pair<int, int> func(int i, int w) {
  14.     if(i==n+1) return make_pair(0, 0);
  15.     if(dp[i][w]!=-1) {
  16.         return make_pair(dp[i][w], memo[i][w]);
  17.     }
  18.     int weight1 = 0, weight2 = 0, profit1=0,profit2=0;
  19.     if(w+weight[i]<=CAP) {
  20.         pair<int, int> ret = func(i+1,w+weight[i]);
  21.         profit1= cost[i] + ret.first;
  22.         weight1 = ret.second + weight[i];
  23.     }
  24.  
  25.     pair<int, int> ret2 = func(i+1,w);
  26.     profit2=ret2.first;
  27.     weight2 = ret2.second;
  28.     if(profit1 > profit2) {
  29.         memo[i][w] = weight1;
  30.         dp[i][w]=profit1;
  31.     } else {
  32.         memo[i][w] = weight2;
  33.         dp[i][w]=profit2;
  34.     }
  35.     return make_pair(dp[i][w], memo[i][w]);
  36. }
  37.  
  38. int main() {
  39. #ifndef ONLINE_JUDGE
  40.     freopen("input.txt","r",stdin);
  41. #endif
  42.  
  43.     while(scanf("%d%d",&CAP,&n) == 2) {
  44.         if(CAP == 0 and n == 0) break;
  45.         memset(dp,-1,sizeof(dp));
  46.         memset(memo,0,sizeof(memo));
  47.         for(int i=1; i<=n; i++) {
  48.             scanf("%d%d\n",&weight[i],&cost[i]);
  49.         }
  50.         pair<int, int> ans = func(1,0);
  51.         printf("%d %d\n", ans.second, ans.first);
  52.     }
  53.  
  54.  
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment