fork(3) download
  1. #include <algorithm>
  2. #include <cstdio>
  3. using namespace std;
  4. int fun[110], cost[110];
  5. int n, budgt;
  6. int main() {
  7. while (scanf("%d%d", &budgt, &n) && budgt + n) {
  8. int dpfun[110][510], dpcost[110][510];
  9. for (int i = 0; i < n; ++i) scanf("%d%d", &cost[i], &fun[i]);
  10. for (int i = 0; i < n; ++i)
  11. for (int j = 0; j <= budgt; ++j) {
  12. if (j < cost[i]) {
  13. dpfun[i + 1][j] = dpfun[i][j];
  14. dpcost[i + 1][j] = dpcost[i][j];
  15. } else {
  16. dpfun[i + 1][j] = max(dpfun[i][j], dpfun[i][j - cost[i]] + fun[i]);
  17. if (dpfun[i][j] < dpfun[i][j - cost[i]] + fun[i]) {
  18. dpcost[i + 1][j] = dpcost[i][j - cost[i]] + cost[i];
  19. } else {
  20. dpcost[i + 1][j] =
  21. min(dpcost[i][j - cost[i]] + cost[i], dpcost[i][j]);
  22. }
  23. }
  24. }
  25. printf("%d %d\n", dpcost[n][budgt], dpfun[n][budgt]);
  26. }
  27. return 0;
  28. }#include <algorithm>
  29. #include <cstdio>
  30. using namespace std;
  31. int fun[110], cost[110];
  32. int n, budgt;
  33. int main() {
  34. while (scanf("%d%d", &budgt, &n) && budgt + n) {
  35. int dpfun[110][510], dpcost[110][510];
  36. for (int i = 0; i < n; ++i) scanf("%d%d", &cost[i], &fun[i]);
  37. for (int i = 0; i < n; ++i)
  38. for (int j = 0; j <= budgt; ++j) {
  39. if (j < cost[i]) {
  40. dpfun[i + 1][j] = dpfun[i][j];
  41. dpcost[i + 1][j] = dpcost[i][j];
  42. } else {
  43. dpfun[i + 1][j] = max(dpfun[i][j], dpfun[i][j - cost[i]] + fun[i]);
  44. if (dpfun[i][j] < dpfun[i][j - cost[i]] + fun[i]) {
  45. dpcost[i + 1][j] = dpcost[i][j - cost[i]] + cost[i];
  46. } else {
  47. dpcost[i + 1][j] =
  48. min(dpcost[i][j - cost[i]] + cost[i], dpcost[i][j]);
  49. }
  50. }
  51. }
  52. printf("%d %d\n", dpcost[n][budgt], dpfun[n][budgt]);
  53. }
  54. return 0;
  55. }
Compilation error #stdin compilation error #stdout 0s 3792KB
stdin
20 4
2 3
8 2
9 5
10 50

0 0
20 4
2 3
8 2
9 5
10 50

0 0
20 4
2 3
8 2
9 5
10 50

0 0
20 4
2 3
8 2
9 5
10 50

0 0
compilation info
prog.cpp:28:2: error: stray '#' in program
 }#include <algorithm>
  ^
prog.cpp:28:3: error: 'include' does not name a type
 }#include <algorithm>
   ^
prog.cpp:31:12: error: redefinition of 'int fun [110]'
 int fun[110], cost[110];
            ^
prog.cpp:4:5: note: 'int fun [110]' previously declared here
 int fun[110], cost[110];
     ^
prog.cpp:31:23: error: redefinition of 'int cost [110]'
 int fun[110], cost[110];
                       ^
prog.cpp:4:15: note: 'int cost [110]' previously declared here
 int fun[110], cost[110];
               ^
prog.cpp:32:5: error: redefinition of 'int n'
 int n, budgt;
     ^
prog.cpp:5:5: note: 'int n' previously declared here
 int n, budgt;
     ^
prog.cpp:32:8: error: redefinition of 'int budgt'
 int n, budgt;
        ^
prog.cpp:5:8: note: 'int budgt' previously declared here
 int n, budgt;
        ^
prog.cpp: In function 'int main()':
prog.cpp:33:5: error: redefinition of 'int main()'
 int main() {
     ^
prog.cpp:6:5: note: 'int main()' previously defined here
 int main() {
     ^
stdout
Standard output is empty