Skip to content

Instantly share code, notes, and snippets.

@budnik
Created January 10, 2016 20:29
Show Gist options
  • Save budnik/81e9a8ff70ead484287e to your computer and use it in GitHub Desktop.
Save budnik/81e9a8ff70ead484287e to your computer and use it in GitHub Desktop.
def solution(a)
sum = a[0]
sum += a[-1] if a[-1] < 0
negative = []
a[1..-1].each_with_index do |el,i|
if el >= 0
sum+=el
elsif i==a.size-2 || a[i+2]>=0
chunk = (i+1).downto(1).take_while{|j| a[j]<0}
negative << a[chunk.last..chunk.first] if chunk.size>=6
end
end
negative.each do |chunk|
sum+=chunk.each_slice(6).inject{|s,m| 6.times{|j| s[j]= s.max+m[j].to_i}; s}.max
end
sum
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment