Skip to content

Instantly share code, notes, and snippets.

@whatalnk
Created April 14, 2019 03:05
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/63c44e33a53f714e5e523a6a20493fbf to your computer and use it in GitHub Desktop.
Save whatalnk/63c44e33a53f714e5e523a6a20493fbf to your computer and use it in GitHub Desktop.
AtCoder ABC #124 D - Handstand
n, k = gets.chomp.split(" ").map(&:to_i)
s = gets.chomp.split("").map(&:to_i)
nums = []
now = 1
cnt = 0
n.times do |i|
if s[i] == now
cnt += 1
else
nums << cnt
now = 1 - now
cnt = 1
end
end
if cnt != 0
nums << cnt
end
if nums.size.even?
nums << 0
end
sums = Array.new(nums.size + 1, 0)
nums.size.times do |i|
sums[i+1] = sums[i] + nums[i]
end
add = 2 * k + 1
ans = 0
(0..(nums.size)).step(2) do |i|
left = i
right = [i + add, nums.size].min
tmp = sums[right] - sums[left]
if tmp > ans
ans = tmp
end
end
puts ans
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment