Created
January 5, 2019 13:12
-
-
Save tomcha/b4bbac20d11d0b2e09e91aa0fd34a848 to your computer and use it in GitHub Desktop.
部分和
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
n = gets.chomp.to_i | |
a = gets.chomp.split(/ /).map(&:to_i) | |
ans = gets.chomp.to_i | |
dp= [] | |
sums = [] | |
(0..(n - 1)).each do |i| | |
if(dp[ans - a[i]] && (ans - a[i]) > 0) | |
puts 'YES' | |
exit | |
else | |
dp[a[i]] = 1 | |
newsums = sums.dup | |
sums.each do |j| | |
dp[a[i] + j] = 1 | |
newsums << a[i] + j | |
end | |
sums = newsums | |
end | |
end | |
puts "NO" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment