Skip to content

Instantly share code, notes, and snippets.

@whatalnk
Created April 18, 2019 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whatalnk/a6d71fefa409def2486e066bbc0ba167 to your computer and use it in GitHub Desktop.
Save whatalnk/a6d71fefa409def2486e066bbc0ba167 to your computer and use it in GitHub Desktop.
AtCoder ABC #117 C - Streamline
n, m = gets.chomp.split(" ").map(&:to_i)
x = gets.chomp.split(" ").map(&:to_i).sort
y = Array.new(m - 1, 0)
(m - 1).times do |i|
y[i] = x[i + 1] - x[i]
end
y.sort!
xmax = x.last
xmin = x.first
ymaxs = y.last(n - 1)
if ymaxs.empty?
ysum = 0
else
ysum = ymaxs.inject(:+)
end
puts xmax - xmin - ysum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment