結果

問題 No.4 おもりと天秤
ユーザー nishima
提出日時 2015-11-07 22:41:58
言語 Ruby
(3.4.1)
結果
AC  
実行時間 124 ms / 5,000 ms
コード長 520 bytes
コンパイル時間 250 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 20,224 KB
最終ジャッジ日時 2024-06-26 09:20:43
合計ジャッジ時間 3,315 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます
コンパイルメッセージ
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