fork(6) download
  1. #include <bits/stdc++.h>
  2.  
  3. #define INF 1000000000
  4. #define MOD 1000000007
  5. #define MAXN 1000005
  6. #define ins insert
  7. #define pb push_back
  8. #define mp make_pair
  9. #define sz size
  10. #define rep(i, n) for(int i = 0; i < n; ++i)
  11. #define sd(n) scanf("%d",&n)
  12. #define sll(n) scanf("%I64d",&n)
  13. #define pdn(n) printf("%d\n",n)
  14. #define plln(n) printf("%I64d\n",n)
  15. #define pd(n) printf("%d ",n)
  16. #define nl() printf("\n")
  17. using namespace std;
  18.  
  19. typedef long long ll;
  20. typedef unsigned long long ull;
  21. typedef vector<ll> vi;
  22. typedef vector<vi> vii;
  23. typedef pair<int, int> pii;
  24.  
  25. int budget, parties;
  26.  
  27. int fun[101], fee[101];
  28.  
  29. int dp[101][501];
  30.  
  31. int solve(int idx, int rembudget) {
  32. if(rembudget == 0)
  33. return 0;
  34. if(rembudget < 0)
  35. -INF;
  36. if(idx == 0) {
  37. if(rembudget >= 0)
  38. return 0;
  39. return -INF;
  40. }
  41. if(dp[idx][rembudget] != -1)
  42. return dp[idx][rembudget];
  43. return dp[idx][rembudget] = max(solve(idx-1, rembudget), fun[idx]+solve(idx-1, rembudget-fee[idx]));
  44. }
  45.  
  46. int main() {
  47. while((cin >> budget >> parties) && (budget) && (parties)) {
  48. for(int i = 0; i < 101; ++i)
  49. for(int j = 0; j < 501; ++j)
  50. dp[i][j] = -1;
  51. for(int i = 0; i < parties; ++i) {
  52. cin >> fee[i] >> fun[i];
  53. }
  54. for(int i = 0; i < 501; ++i)
  55. solve(parties-1, i);
  56. int ans = solve(parties-1, budget);
  57. int currmin = INF;
  58. for(int i = 0; i < 501; ++i) {
  59. if(dp[parties-1][i] == ans) {
  60. currmin = i;
  61. break;
  62. }
  63. }
  64. cout << currmin << " " << ans << endl;
  65. }
  66. return 0;
  67. }
Success #stdin #stdout 0s 3340KB
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 29