SHARE
TWEET

Untitled

a guest Apr 24th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. n, k = gets.chomp.split(" ").map(&:to_i)
  2. s = gets.chomp.split("").map(&:to_i)
  3. nums = []
  4. now = 1
  5. cnt = 0
  6. n.times do |i|
  7.   if s[i] == now
  8.     cnt += 1
  9.   else
  10.     nums << cnt
  11.     now = 1 - now
  12.     cnt = 1
  13.   end
  14. end
  15. if cnt != 0
  16.   nums << cnt
  17. end
  18. if nums.size.even?
  19.   nums << 0
  20. end
  21.  
  22. sums = Array.new(nums.size + 1, 0)
  23. nums.size.times do |i|
  24.   sums[i+1] = sums[i] + nums[i]
  25. end
  26.  
  27. add = 2 * k + 1
  28.  
  29. ans = 0
  30.  
  31. (0..(nums.size)).step(2) do |i|
  32.   left = i
  33.   right = [i + add, nums.size].min
  34.   tmp = sums[right] - sums[left]
  35.   if tmp > ans
  36.     ans = tmp
  37.   end
  38. end
  39. puts ans
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top