@x=[];a=[];@t=[] n,k = gets.chomp.split(" ").map(&:to_i) @x+=gets.chomp.split(" ").map(&:to_i) a+=gets.chomp.split(" ").map(&:to_i) @hash = Hash[*([@x,a].transpose.flatten)] p=@x[k-1] update = true @t=[[nil,0]] xxx = @x.length def aaa(elm) if @t.any? {|w,y| w == elm } return true else if @t[0][0]==nil @t=[[elm,0]] else @t.push([elm,0]) end return false end end while(update) if @t[0][0]==nil b = @hash[p] zzz = p+b zzz1 = p+(-1*b) for i in 0...xxx do if zzz1 > zzz if @x[i] >=zzz and zzz1 >= @x[i] aaa(@x[i]) end else if zzz >= @x[i] and @x[i] >= zzz1 aaa(@x[i]) end end break if i == xxx-1 end else cnt = 0 @t.each do |elm, elm2| next if elm2==1 b = @hash[elm] zzz = elm+b zzz1 = elm+(-1*b) @t[cnt][1] = 1 for i in 0...xxx do update = false if i == xxx-1 if zzz1 > zzz if @x[i] >=zzz and zzz1 >= @x[i] aaa(@x[i]) end else if zzz >= @x[i] and @x[i] >= zzz1 aaa(@x[i]) end end end cnt+=1 end end end if @t==nil p "1" else p @t.length end