Problem p02623 (python)

Fixed submission:
"""everything has a cost!"""

from math import *
import random, functools, collections, sys

__author__ = 'p7x'


# sys.stdin = open('in.txt', 'r')
# sys.stdout = open('out.txt', 'w')
# sys.stderr = open('err.txt', 'w')


if __name__ == '__main__':
    n,m,k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))

    for i in range(1,len(a)):
        a[i] += a[i-1]
    for i in range(1,len(b)):
        b[i] += b[i-1]

    j = len(b)-1
    while j > -1 and b[j] > k:
        j -= 1
    ans = j+1
    i = len(a)-1
    while i > -1 and a[i] > k:
        i -= 1
    ans = max(i+1,ans)

    for inx in range(i+1):
        while j > -1 and a[inx] + b[j] > k:
            j -= 1
        ans = max(inx+j+2,ans)
    print(ans)

Buggy submission (verdict: Runtime Error):
"""everything has a cost!"""

from math import *
import random
import functools
import collections
import sys

__author__ = 'p7x'


# sys.stdin = open('in.txt', 'r')
# sys.stdout = open('out.txt', 'w')
# sys.stderr = open('err.txt', 'w')
    
i, j = 0, 0
n, m = 0, 0
a = []
b = []


# @functools.lru_cache(maxsize=None, typed=False)
def read(ti: int, tj: int) -> int:
    global n, m, i, j, a, b
    if (ti < n and (tj == m or a[ti] < b[tj])):
        return 1
    elif (tj < m and (ti == n or b[tj] < a[ti])):
        return 2
    elif (ti < n and tj < m and a[ti] == b[tj]):
        return read(ti+1, tj+1)
    else:
        if (ti == i and tj == j):
            return 3
        elif (ti == i):
            return 2
        else:
            return 1


if __name__ == '__main__':
    # global n, m, i, j, a, b
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))

    mins = 0
    while (mins < k):
        v = read(i, j)
        if (v == 1):
            mins += a[i]
            i += 1
        elif (v == 2):
            mins += b[j]
            j += 1
        else:
            break
    if mins > k:
        print(i+j-1)
    else:
        print(i+j)

Buggy submission (verdict: Runtime Error):
"""everything has a cost!"""

from math import *
import random
import functools
import collections
import sys

__author__ = 'p7x'


# sys.stdin = open('in.txt', 'r')
# sys.stdout = open('out.txt', 'w')
# sys.stderr = open('err.txt', 'w')

n, m = 0, 0


# @functools.lru_cache(maxsize=None, typed=False)
def read(ti: int, tj: int, i: int, j: int) -> int:
    if (ti < n and (tj == m or a[ti] < b[tj])):
        return 1
    elif (tj < m and (ti == n or b[tj] < a[ti])):
        return 2
    elif (ti < n and tj < m):
        return read(ti+1, tj+1, i, j)
    else:
        if (ti == i and tj == j):
            return 3
        elif (ti == i):
            return 2
        else:
            return 1


if __name__ == '__main__':
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))

    mins = 0
    i, j = 0, 0
    while (mins < k):
        v = read(i, j, i, j)
        # print(i, j, v)
        if (v == 1):
            mins += a[i]
            i += 1
        elif (v == 2):
            mins += b[j]
            j += 1
        else:
            break
    if mins > k:
        print(i+j-1)
    else:
        print(i+j)

Buggy submission (verdict: Runtime Error):
"""everything has a cost!"""

from math import *
import random
import functools
import collections
import sys

__author__ = 'p7x'


# sys.stdin = open('in.txt', 'r')
# sys.stdout = open('out.txt', 'w')
# sys.stderr = open('err.txt', 'w')

n, m = 0, 0


@functools.lru_cache(maxsize=None, typed=False)
def read(ti: int, tj: int, i: int, j: int) -> int:
    if (ti < n and (tj == m or a[ti] < b[tj])):
        return 1
    elif (tj < m and (ti == n or b[tj] < a[ti])):
        return 2
    elif (ti < n and tj < m):
        return read(ti+1, tj+1, i, j)
    else:
        if (ti == i):
            return 3
        else:
            return 1


if __name__ == '__main__':
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))

    mins = 0
    i, j = 0, 0
    while (mins < k):
        v = read(i, j, i, j)
        # print(i, j, v)
        if (v == 1):
            mins += a[i]
            i += 1
        elif (v == 2):
            mins += b[j]
            j += 1
        else:
            break
    if mins > k:
        print(i+j-1)
    else:
        print(i+j)

Buggy submission (verdict: Runtime Error):
"""everything has a cost!"""

from math import *
import random
import functools
import collections
import sys

__author__ = 'p7x'



n, m = 0, 0


@functools.lru_cache(maxsize=None, typed=False)
def read(ti: int, tj: int, i: int, j: int) -> int:
    if (ti < n and (tj == m or a[ti] < b[tj])):
        return 1
    elif (tj < m and (ti == n or b[tj] < a[ti])):
        return 2
    elif (ti < n and tj < m):
        return read(ti+1, tj+1, i, j)
    else:
        if (ti == i):
            return 3
        else:
            return 1


if __name__ == '__main__':
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))

    mins = 0
    i, j = 0, 0
    while (mins < k):
        v = read(i, j, i, j)
        print(i, j, v)
        if (v == 1):
            mins += a[i]
            i += 1
        elif (v == 2):
            mins += b[j]
            j += 1
        else:
            break
    if mins > k:
        print(i+j-1)
    else:
        print(i+j)

Buggy submission (verdict: Runtime Error):
"""everything has a cost!"""

from math import *
import random
import functools
import collections
import sys

__author__ = 'p7x'


# sys.stdin = open('in.txt', 'r')
# sys.stdout = open('out.txt', 'w')
# sys.stderr = open('err.txt', 'w')

i, j = 0, 0
n, m = 0, 0


@functools.lru_cache(maxsize=None, typed=False)
def read(ti: int, tj: int) -> int:
    if (ti < n and (tj == m or a[ti] < b[tj])):
        return 1
    elif (tj < m and (ti == n or b[tj] < a[ti])):
        return 2
    elif (ti < n and tj < m and a[ti] == b[tj]):
        return read(ti+1, tj+1)
    else:
        if (ti == i):
            return 3
        else:
            return 1


if __name__ == '__main__':
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))

    mins = 0
    while (mins < k):
        v = read(i, j)
        if (v == 1):
            mins += a[i]
            i += 1
        elif (v == 2):
            mins += b[j]
            j += 1
        else:
            break
    if mins > k:
        print(i+j-1)
    else:
        print(i+j)

Buggy submission (verdict: Runtime Error):
"""everything has a cost!"""

from math import *
import random, functools, collections, sys

__author__ = 'p7x'


# sys.stdin = open('in.txt', 'r')
# sys.stdout = open('out.txt', 'w')
# sys.stderr = open('err.txt', 'w')

i, j = 0, 0
n , m = 0, 0

def read(ti, tj) -> int:
    if (ti < n and (tj == m or a[ti] < b[tj])):
            return 1
    elif (tj < m and (ti == n or b[tj] < a[ti])):
        return 2
    elif (ti < n and tj < m):
        return read(ti+1, tj+1)
    else:
        if (ti == i): return 3
        else: return 1


if __name__ == '__main__':
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    
    mins = 0
    while (mins < k):
        v = read(i, j)
        if (v == 1):
            mins += a[i]
            i += 1
        elif (v == 2):
            mins += b[j]
            j += 1
        else:
            break
    if mins > k:
        print(i+j-1)
    else:
        print(i+j)