$i; $j--) { if ($data[$j-1] > $data[$j]) { $temp = $data[$j-1]; $data[$j-1] = $data[$j]; $data[$j] = $temp; } } } foreach( $data as $w ){ //あふれたら終わり if( ( $fill += $w ) > $box ){ break; } $idx++; } printf( "%d\n", $idx );