def leftcalc(arr, start) res = 0 (start - 2).downto(0) do |i| if arr[i] >= 2 res += arr[i] elsif arr[i] == 1 res += arr[i] break elsif arr[i] == 0 break end end return res end def rightcalc(arr, start) res = 0 start.upto(arr.size - 1) do |i| if arr[i] >= 2 res += arr[i] elsif arr[i] == 1 res += arr[i] break elsif arr[i] == 0 break end end return res end N, K = gets.split.map(&:to_i) arr = gets.split.map(&:to_i) if K > 1 && K < N leftsum = leftcalc(arr, K) rightsum = rightcalc(arr, K) elsif K == 1 leftsum = 0 rightsum = rightcalc(arr, K) elsif K == N leftsum = leftcalc(arr, K) rightsum = 0 end if arr[K - 1] >= 2 puts arr[K - 1] + leftsum + rightsum elsif arr[K - 1] == 1 puts arr[K - 1] + leftsum >= rightsum ? leftsum : rightsum elsif arr[K - 1] == 0 puts 0 end