結果

問題 No.4 おもりと天秤
ユーザー 綾地寧々綾地寧々
提出日時 2017-10-02 17:09:28
言語 PHP
(8.2.11)
結果
AC  
実行時間 27 ms / 5,000 ms
コード長 581 bytes
コンパイル時間 421 ms
コンパイル使用メモリ 12,176 KB
実行使用メモリ 12,364 KB
最終ジャッジ日時 2023-09-08 17:34:42
合計ジャッジ時間 1,638 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
12,276 KB
testcase_01 AC 7 ms
12,208 KB
testcase_02 AC 7 ms
12,312 KB
testcase_03 AC 7 ms
12,292 KB
testcase_04 AC 7 ms
12,308 KB
testcase_05 AC 24 ms
12,264 KB
testcase_06 AC 7 ms
12,320 KB
testcase_07 AC 26 ms
12,284 KB
testcase_08 AC 7 ms
12,160 KB
testcase_09 AC 7 ms
12,264 KB
testcase_10 AC 27 ms
12,220 KB
testcase_11 AC 7 ms
12,204 KB
testcase_12 AC 7 ms
12,340 KB
testcase_13 AC 7 ms
12,268 KB
testcase_14 AC 8 ms
12,300 KB
testcase_15 AC 7 ms
12,264 KB
testcase_16 AC 7 ms
12,308 KB
testcase_17 AC 8 ms
12,260 KB
testcase_18 AC 16 ms
12,248 KB
testcase_19 AC 23 ms
12,364 KB
testcase_20 AC 23 ms
12,232 KB
testcase_21 AC 22 ms
12,272 KB
testcase_22 AC 24 ms
12,308 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
No syntax errors detected in Main.php

ソースコード

diff #

<?php
$n = trim(fgets(STDIN));
$w_array = explode(" ",trim(fgets(STDIN)));

// 合計値が偶数でないなら組み合わせはないので即終了
$w_total = array_sum($w_array);
if ( $w_total % 2 ) {
	echo 'impossible'.PHP_EOL;
	return 0;
}

$dp = array(0=>TRUE);
for ( $i = 0; $i < count($w_array); $i++ ) {
	foreach ( $dp as $weight => $value ) {
		if ( $value === TRUE ) {
			$dp[$weight+$w_array[$i]] = TRUE;
		}
	}
}

$w_target = $w_total / 2;

if ( isset($dp[$w_target]) && ($dp[$w_target] === TRUE) ) {
	echo 'possible'.PHP_EOL;
}
else {
	echo 'impossible'.PHP_EOL;
}
0