Skip to content

Instantly share code, notes, and snippets.

@tomcha
Created January 5, 2019 13:12
Show Gist options
  • Save tomcha/b4bbac20d11d0b2e09e91aa0fd34a848 to your computer and use it in GitHub Desktop.
Save tomcha/b4bbac20d11d0b2e09e91aa0fd34a848 to your computer and use it in GitHub Desktop.
部分和
#!/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