def evaluate(a, i) if a[i] != a[i+2] and (a[i] < a[i+1] and a[i+1] > a[i+2]) or (a[i] > a[i+1] and a[i+1] < a[i+2]) a[i] else 0 end end T = gets.to_i T.times do n = gets.to_i a = gets.split.map(&:to_i) a << a[0] << a[1] dp = [0, 0, 0] n.times do |i| value = evaluate(a, i) dp[i + 3] = [dp[i] + value, dp[i + 1], dp[i + 2]].max end puts dp[-1] end