結果

問題 No.4 おもりと天秤
ユーザー ねずねず
提出日時 2018-01-28 11:05:11
言語 PHP
(8.3.4)
結果
AC  
実行時間 60 ms / 5,000 ms
コード長 507 bytes
コンパイル時間 702 ms
コンパイル使用メモリ 11,956 KB
実行使用メモリ 40,848 KB
最終ジャッジ日時 2023-09-08 17:38:23
合計ジャッジ時間 1,940 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
12,256 KB
testcase_01 AC 7 ms
12,264 KB
testcase_02 AC 8 ms
12,252 KB
testcase_03 AC 7 ms
12,308 KB
testcase_04 AC 8 ms
12,284 KB
testcase_05 AC 54 ms
24,024 KB
testcase_06 AC 7 ms
12,260 KB
testcase_07 AC 56 ms
24,028 KB
testcase_08 AC 8 ms
12,252 KB
testcase_09 AC 38 ms
40,848 KB
testcase_10 AC 60 ms
24,084 KB
testcase_11 AC 7 ms
12,260 KB
testcase_12 AC 7 ms
12,272 KB
testcase_13 AC 7 ms
12,260 KB
testcase_14 AC 7 ms
12,252 KB
testcase_15 AC 7 ms
12,216 KB
testcase_16 AC 8 ms
12,272 KB
testcase_17 AC 7 ms
12,272 KB
testcase_18 AC 37 ms
24,120 KB
testcase_19 AC 50 ms
24,020 KB
testcase_20 AC 50 ms
24,004 KB
testcase_21 AC 47 ms
24,012 KB
testcase_22 AC 49 ms
24,044 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
No syntax errors detected in Main.php

ソースコード

diff #

<?php
$n = trim(fgets(STDIN));
$weights = explode(" ", trim(fgets(STDIN)));
$sum = array_sum($weights);
$dp = array_fill(0, $n, array_fill(0, $sum + 1, false));
$dp[0][$weights[0]] = true;
for ($i = 1; $i < $n; $i++) {
	$w = $weights[$i];
	for ($j = 0; $j <= $sum; $j++) {
		if (!$dp[$i - 1][$j]) continue;
		if ($j + $w <= $sum) $dp[$i][$j + $w] = true;
		if ($j - $w >= 0)    $dp[$i][$j - $w] = true;
		if ($w - $j >= 0)    $dp[$i][$w - $j] = true;
	}
}
echo $dp[$n - 1][0] ? "possible" : "impossible";
?>
0