#! ruby # yukicoder My Practice # author: Leonardone @ NEETSDKASU def gs() gets.chomp end def gi() gets.to_i end def gss() gets.chomp.split end def gis() gss.map(&:to_i) end def nmapf(n,f) n.times.map{ __send__ f } end def arr2d(h,w,v=0) h.times.map{[v] * w} end def ngs(n) nmapf n,:gs end def ngi(n) nmapf n,:gi end def ngss(n) nmapf n,:gss end def ngis(n) nmapf n,:gis end def for2p(hr,wr,&pr) hr.each{|i|wr.each{|j|pr.call(i,j)}} end $N, $M = gis $W = gis if $M < 2 puts 0 exit end $minselln = $M * 2 > $N ? $M * 2 - $N : 0 $maxselln = $M - 1 $ans = $M * 2 > $N ? $W.take($M * 2 - $N).inject(:+) : 0 $ar = [true] * $N $v = 0 $ln = 0 $vt = 0 def f(s) if $vt > $M return end if $vt == $M $ans = [$ans, $v].max end (s...$N).each do |i| if $ar[i] $ar[i] = false avt = 2 c1 = false if !$ar[i - 1] avt -= 1 elsif !$ar[i - 2] c1 = true $ar[i - 1] = false $v += $W[i - 1] end c2 = false if !$ar[(i + 1) % $N] avt -= 1 elsif !$ar[(i + 2) % $N] c2 = true $ar[(i + 1) % $N] = false $v += $W[(i + 1) % $N] end $v += $W[i] $vt += avt f(i + 1) $vt -= avt if c1 $v -= $W[i - 1] $ar[i - 1] = true end if c2 $v -= $W[(i + 1) % $N] $ar[(i + 1) % $N] = true end $v -= $W[i] $ar[i] = true end end end f(0) puts $ans