fork(1) download
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<cstring>
  4.  
  5. #define max1 10000
  6.  
  7. using namespace std;
  8.  
  9. struct node
  10. {
  11. int fee, fun;
  12. };
  13.  
  14.  
  15. int max(int x, int y)
  16. {
  17. return x>y?x:y;
  18. }
  19.  
  20.  
  21. int a[2][max1];
  22. node dp[max1];
  23. int p, n;
  24.  
  25.  
  26. node solve(int pos, int cur)
  27. {
  28. if(pos == n)
  29. {
  30. return (node){cur, 0};
  31. }
  32.  
  33. if(cur+a[0][pos]<=p)
  34. {
  35. node l = solve(pos+1, cur+a[0][pos]);
  36. node r = solve(pos+1, cur);
  37.  
  38. if(l.fun + a[1][pos] > r.fun)
  39. {
  40. return (node){l.fee, l.fun + a[1][pos]};
  41. }
  42.  
  43. else
  44. {
  45. return (node){r.fee, r.fun};
  46. }
  47. }
  48.  
  49. if(cur+a[0][pos]>p)
  50. {
  51. return solve(pos+1, cur);
  52. }
  53.  
  54. //else
  55. return (node){cur, 0};
  56. }
  57.  
  58.  
  59. int main()
  60. {
  61. //freopen("C:\\Users\\Shraeyas\\Documents\\pg\\pr_ag\\prag_PARTY.txt", "r", stdin);
  62.  
  63. while(1)
  64. {
  65. scanf("%d %d", &p, &n);
  66.  
  67. if(p==0 && n==0)
  68. break;
  69.  
  70. for(int i=0; i<n; i++)
  71. {
  72. scanf("%d %d", &a[0][i], &a[1][i]);
  73. }
  74.  
  75. node ans = solve(0, 0);
  76.  
  77. printf("%d %d\n", ans.fee, ans.fun);
  78. }
  79. }
Success #stdin #stdout 0s 15392KB
stdin
50 10
12 3
15 8
16 9
16 6
10 2
21 9
18 4
12 4
17 8
18 9 

50 10
13 8
19 10
16 8
12 9
10 2
12 8
13 5
15 5
11 7
16 2

0 0
stdout
49 26
48 32