結果

問題 No.4 おもりと天秤
ユーザー nishimanishima
提出日時 2015-11-07 22:41:58
言語 Ruby
(3.3.0)
結果
AC  
実行時間 111 ms / 5,000 ms
コード長 520 bytes
コンパイル時間 182 ms
コンパイル使用メモリ 11,572 KB
実行使用メモリ 16,684 KB
最終ジャッジ日時 2023-09-08 16:28:18
合計ジャッジ時間 3,261 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
15,188 KB
testcase_01 AC 79 ms
15,028 KB
testcase_02 AC 80 ms
15,044 KB
testcase_03 AC 79 ms
15,064 KB
testcase_04 AC 79 ms
15,276 KB
testcase_05 AC 104 ms
16,344 KB
testcase_06 AC 80 ms
15,264 KB
testcase_07 AC 104 ms
16,476 KB
testcase_08 AC 81 ms
15,176 KB
testcase_09 AC 80 ms
15,028 KB
testcase_10 AC 108 ms
16,620 KB
testcase_11 AC 79 ms
15,132 KB
testcase_12 AC 79 ms
15,028 KB
testcase_13 AC 80 ms
15,252 KB
testcase_14 AC 81 ms
15,132 KB
testcase_15 AC 80 ms
15,152 KB
testcase_16 AC 79 ms
15,172 KB
testcase_17 AC 78 ms
15,156 KB
testcase_18 AC 111 ms
16,684 KB
testcase_19 AC 101 ms
16,392 KB
testcase_20 AC 100 ms
16,180 KB
testcase_21 AC 99 ms
16,088 KB
testcase_22 AC 100 ms
16,180 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

# http://yukicoder.me/problems/19

n = $stdin.gets.chomp.to_i
w = $stdin.gets.chomp.split(/ /).map(&:to_i).sort
dp = Array.new( n ){ Array.new() }
sum = w.inject( :+ )

if sum % 2 == 1
	puts "impossible"
	exit
else
	sum2 = sum / 2	

	0.upto( n - 1 ){ |i|
		val = w[ i ]

		dp[ i ] = dp[ i - 1].dup
		dp[ i ] << val
		dp[ i - 1 ].each{ |e|
			dp[ i ] << ( e + val )
		}

		dp[ i ].uniq! if dp[ i ].uniq
		dp[ i ].sort!

		dp[ i ].each{ |e|
			if e == sum2
				puts "possible"
				exit
			end
		}
	}
	puts "impossible"
end
0