#!ruby n,k,*a=gets(p).split.map(&:to_i) i=0 d=n/k m=a[k-1] 2.upto(d) do u = (1..d).map{|i|a[i*k-1,k].min}.inject{|x,y|x+y} m = [m,u].min end p m