#! 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 z = [true] * N ans = W.inject(:+) if N == M puts ans exit end if N - M == 1 puts (ans - W.rotate(-1).zip(W).map{|x,y|x+y}.min) exit end if M == 2 puts [W.max, 0].max exit end