def search(A, target):
    if len(A) == 0:
        return -1

    s = 1
    while s < len(A):
        if A[s] != A[0]:
            break
        s += 1

    e = len(A)
    while e > s:
        if A[e-1] != A[0]:
            break
        e -= 1

    r = binary_search(A, lambda x: -1 if x<A[0] else 1, search_range(s,e))

    if target >= A[0]:
        r = binary_search(A, target, search_range(s-1, r[0]))
    elif target < A[0]:
        r = binary_search(A, target, search_range(r[0], e))

    if search_range_size(r) <= 0:
        return -1

    return r[0]
def kth(A, B, k):
    ra = search_range(0, len(A))
    rb = search_range(0, len(B))

    while True:
        if search_range_size(ra) <= 0:
            return B[rb[0] + k]
        if search_range_size(rb) <= 0:
            return A[ra[0] + k]

        ma = search_range_midpoint(ra)
        mb = search_range_midpoint(rb)
        rla = search_range_left(ra, ma)
        rlb = search_range_left(rb, mb)

        if search_range_size(rla) + search_range_size(rlb) < k:
            if A[ma] < B[mb]:
                k -= 1 + search_range_size(rla)
                ra = search_range_right(ra, ma)
            else:
                k -= 1 + search_range_size(rlb)
                rb = search_range_right(rb, mb)
        else:
            if A[ma] > B[mb]:
                ra = rla
            else:
                rb = rlb
def search(A, target):
    if len(A) == 0:
        return -1

    s = 1
    while s < len(A):
        if A[s] != A[0]:
            break
        s += 1

    e = len(A)
    while e > s:
        if A[e - 1] != A[0]:
            break
        e -= 1

    r = binary_search(A, lambda x: -1 if x < A[0] else 1, search_range(s, e))

    if target >= A[0]:
        r = binary_search(A, target, search_range(s - 1, r[0]))
    elif target < A[0]:
        r = binary_search(A, target, search_range(r[0], e))

    if search_range_size(r) <= 0:
        return -1

    return r[0]
def kth(A, B, k):
    ra = search_range(0, len(A))
    rb = search_range(0, len(B))

    while True:
        if search_range_size(ra) <= 0:
            return B[rb[0]+k]
        if search_range_size(rb) <= 0:
            return A[ra[0]+k]

        ma = search_range_midpoint(ra)
        mb = search_range_midpoint(rb)
        rla = search_range_left(ra, ma)
        rlb = search_range_left(rb, mb)

        if search_range_size(rla)+search_range_size(rlb) < k:
            if A[ma] < B[mb]:
                k -= 1+search_range_size(rla)
                ra = search_range_right(ra, ma)
            else:
                k -= 1+search_range_size(rlb)
                rb = search_range_right(rb, mb)
        else:
            if A[ma] > B[mb]:
                ra = rla
            else:
                rb = rlb
def findMin(num):
    s = 1
    while s < len(num):
        if num[s] != num[0]:
            break
        s += 1

    e = len(num)
    while e > s:
        if num[e-1] != num[0]:
            break
        e -= 1

    r = binary_search(num, lambda x: -1 if x<num[0] else 1, search_range(s,e))

    if search_range_size(r) <= 0:
        if r[0] == e:
            return num[0]
        return num[r[0]]

    return num[r[0]]
def findMin(num):
    s = 1
    while s < len(num):
        if num[s] != num[0]:
            break
        s += 1

    e = len(num)
    while e > s:
        if num[e - 1] != num[0]:
            break
        e -= 1

    r = binary_search(num, lambda x: -1
                      if x < num[0] else 1, search_range(s, e))

    if search_range_size(r) <= 0:
        if r[0] == e:
            return num[0]
        return num[r[0]]

    return num[r[0]]