n, m = read_line.split.map(&.to_i) a = read_line.split.map(&.to_i).reverse dp_max = Array.new(n + 1) { Array.new(m + 1, -(1i64 << 58)) } dp_min = Array.new(n + 1) { Array.new(m + 1, (1i64 << 58)) } use_max = Array.new(n + 1) { Array.new(m + 1, false) } use_min = Array.new(n + 1) { Array.new(m + 1, false) } dp_max[0][0] = 0 n.times do |i| 0.upto(m) do |j| if dp_max[i + 1][j] < dp_max[i][j] + a[i] dp_max[i + 1][j] = dp_max[i][j] + a[i] use_max[i + 1][j] = false end if j < m && dp_min[i + 1][j + 1] > -dp_max[i][j] - a[i] dp_min[i + 1][j + 1] = -dp_max[i][j] - a[i] use_min[i + 1][j + 1] = true end if j < m && dp_max[i + 1][j + 1] < -dp_min[i][j] - a[i] dp_max[i + 1][j + 1] = -dp_min[i][j] - a[i] use_max[i + 1][j + 1] = true end if dp_min[i + 1][j] > dp_min[i][j] + a[i] dp_min[i + 1][j] = dp_min[i][j] + a[i] use_min[i + 1][j] = false end end end mi = 0 mv = 0i64 0.upto(m) do |i| if dp_max[n][i] > mv mi = i mv = dp_max[n][i] end end ans = [] of Int32 cur_max = true n.downto(1) do |i| use = cur_max ? use_max : use_min if use[i][mi] ans << n - i mi -= 1 cur_max = !cur_max end end while ans.size < m ans << n end puts ans.join("\n")