Created
August 25, 2018 14:59
-
-
Save whatalnk/3150bfa73f5ed6ac2ea7e867e9d01b55 to your computer and use it in GitHub Desktop.
AtCoder ABC #107 C
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
n, k = gets.chomp.split(" ").map(&:to_i) | |
x = gets.chomp.split(" ").map(&:to_i) | |
neg = [] | |
pos = [] | |
x.each do |e| | |
if e < 0 | |
neg << e.abs | |
else | |
pos << e | |
end | |
end | |
neg.reverse! | |
if neg.empty? | |
puts pos[k - 1] | |
elsif pos.empty? | |
puts neg[k - 1] | |
else | |
ans = [] | |
0.upto(k) do |i| | |
next if i > neg.length || k - i > pos.length | |
tm = 0 | |
left = neg[i - 1] | |
right = pos[k - i - 1] | |
tm = left + right + [left, right].min | |
ans << tm | |
end | |
puts ans.min | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment