def kadomatz(a, b, c) p a != c && ((a < b && b > c) || (a > b && b < c)) end def solve_partial(aa) n = aa.size dp = [0]*(n+1) # note: heavy implemenatation! (n-2).times do |i| dp[i+3] = dp[i] + aa[i] if kadomatz(*aa[i..(i+2)]) dp[i+1] = [dp[i], dp[i+1]].max end dp.max end def solve(aa) [ solve_partial(aa), solve_partial(aa[1..-1]+aa[0..0]), solve_partial(aa[2..-1]+aa[0..1]) ].max end linet = gets.chomp! ntest = linet.to_i abort 'linet' unless linet =~ /^\d+$/ abort 'T' unless 1 <= ntest && ntest <= 20000 totaln = 0 ntest.times do |t| linen = gets.chomp! linea = gets.chomp! n = linen.to_i aa =linea.split.map(&:to_i) totaln += n abort "linen[#{t}]" unless linen =~ /^\d+$/ abort "linea[#{t}]" unless linea =~ /^\d+( \d+)*$/ abort "n[#{t}]" unless 3 <= n && n <= 200000 abort "a.size[#{t}]" unless aa.size == n abort "a[#{t}]" unless aa.all?{|e| 1 <= e && 200000 } p solve(aa) end abort 'totaln' unless totaln <= 200000 abort 'too many lines' if gets