n, m = gets.split.map(&:to_i) (puts 0; exit) if m <= 1 ws = gets.split.map(&:to_i) memo = Array.new(4){ Array.new(m+1, -Float::INFINITY) } memo[0][m] = 0 memo[3][m-1] = 0 0.upto(n-2){|i| [0,m-i-2].max.upto([m,n-i-2].min){|j| memo[0][j] = memo[0][j] > memo[1][j] ? memo[0][j] : memo[1][j] memo[1][j] = memo[0][j+1] > memo[1][j+1] + ws[i] ? memo[0][j+1] : memo[1][j+1] + ws[i] if j < m memo[2][j] = memo[2][j] > memo[3][j] ? memo[2][j] : memo[3][j] memo[3][j] = memo[2][j+1] > memo[3][j+1] + ws[i] ? memo[2][j+1] : memo[3][j+1] + ws[i] if j < m } } puts [memo[0][0], memo[1][0], memo[2][0], memo[3][0]+ws.last].max