N=gets.to_i D=gets.split.map &:to_i H={[]=>100} def dfs d,h H[d]||=d.map{|i|(t=dfs d-[i],D[i]<0?h-1:h)<1?0:[t+D[i],h*100].min}.max end p dfs [*0...N],D.count{|i|i<0}+1