fork download
  1. <?php
  2. $line = trim(fgets(STDIN));
  3. $tokens = explode(" ",$line);
  4. $price_list_count = (int)$tokens[0];
  5. $price_list = array();
  6. for($i = 0; $i < $price_list_count; $i++)
  7. {
  8. $line = trim(fgets(STDIN));
  9. array_push($price_list,(int)$line);
  10. }
  11. sort($price_list);
  12.  
  13. $campain_count = (int)$tokens[1];
  14. $campain_list = array();
  15. for($i = 0; $i < $campain_count; $i++)
  16. {
  17. $line = trim(fgets(STDIN));
  18. array_push($campain_list,(int)$line);
  19. }
  20.  
  21. $list = NULL;
  22. foreach($campain_list as $campain)
  23. {
  24. $list = knaposack($list,$campain,$price_list);
  25. show_total_price($list,$campain);
  26. }
  27.  
  28. function knaposack($list,$campain,$price_list)
  29. {
  30. $max = $campain + 1;
  31.  
  32. if($list == NULL)
  33. $list = array_fill(0,$max,0);
  34.  
  35. foreach($price_list as $price)
  36. {
  37. for($i = $price; $i <= $max; $i++)
  38. {
  39. if(!isset($list[$i - $price]))
  40. $list[$i - $price] = 0;
  41. if(!isset($list[$i]))
  42. $list[$i] = 0;
  43. $old_price = $list[$i - $price];
  44. $new_price = $old_price + $price;
  45. if($new_price > $list[$i])
  46. {
  47. $list[$i] = $new_price;
  48. }
  49. }
  50. }
  51. return $list;
  52. }
  53.  
  54. function show_total_price($list,$campain_price)
  55. {
  56. echo $list[$campain_price] . "\n";
  57. }
  58. ?>
  59.  
Success #stdin #stdout #stderr 0.01s 20520KB
stdin
Standard input is empty
stdout
// your code goes here
stderr
PHP Notice:  Undefined offset: 1 in /home/xN67FN/prog.php on line 13