Skip to content

Instantly share code, notes, and snippets.

@whatalnk whatalnk/abc124-d.rb
Created Apr 14, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.