# frozen_string_literal: true

n, @k = gets.split(' ').map(&:to_i)
@weights = n.times.map { gets.to_i }

def loadable?(cap)
  truck = 0
  loadage = 0
  @weights.each do |weight|
    if loadage + weight <= cap
      loadage += weight
    else
      truck += 1
      return false if truck >= @k

      loadage = weight
    end
  end
  true
end

cap_right = @weights.sum
cap_left = @weights.max

def search_bin(left, right)
  return left if left == right

  if left + 1 == right
    return loadable?(left) ? left : right
  end

  mid = (left + right) / 2
  if loadable?(mid)
    search_bin(left, mid)
  else
    search_bin(mid + 1, right)
  end
end

puts search_bin(cap_left, cap_right)

