def sol a, h if a.size < 3 return 0 end r = a.pop l = a.shift h[l] -= 1 h[r] -= 1 tmp = 0 idx = 10000 a.size.times{|i| if [l,a[i],r].uniq.size == 3 && tmp < h[a[i]] tmp = h[a[i]] idx = i end } if idx == 10000 return 0 end a.delete_at(idx) h[idx] -= 1 return sol(a, h) + 1 end gets.to_i.times{ n = gets.to_i a = gets.split.map(&:to_i).sort h = Hash.new(0) a.each{|x| h[x] += 1 } p sol(a, h) }