from typing import List


def dot_product2(a: List[int], b: List[int]) -> int:
    """
    >>> dot_product2([1, 1, 1, 2, 1, 3], [3, 1])
    6
    >>> dot_product2(
    ...         [50_000, 1, 50_000, 2],
    ...         [10_000, 1, 50_000, 2, 40_000, 3])
    370000
    >>> dot_product2([1, 1, 1, 1], [1, 2, 1, 3])
    5
    >>> dot_product2([1, 1], [1, -1])
    -1
    >>> dot_product2(
    ...    [1, -1, 2, 2, 3, -3, 4, 4, 5, -5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 999945, -1],
    ...    [999945, 2, 10, 10, 9, 9, 8, -8, 7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1])
    -1999407
    """
    n = len(a)
    m = len(b)
    i = 0
    j = 0
    count1 = 0
    value1 = 0
    count2 = 0
    value2 = 0
    r = 0
    while i < n or j < m:
        if count1 == 0:
            count1 = a[i]
            value1 = a[i + 1]
            i += 2

        if count2 == 0:
            count2 = b[j]
            value2 = b[j + 1]
            j += 2

        common_count = min(count1, count2)
        r += common_count * value1 * value2
        count1 -= common_count
        count2 -= common_count

    return r