Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)

INF = 10**11


def main(input, print):
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    at = [0] * (n+1)
    bt = [0] * (m+1)
    for i in range(1, n+1):
        at[i] = at[i-1]+a[i-1]
    for i in range(1, m+1):
        bt[i] = bt[i-1]+b[i-1]

    ans = 0
    for i in range(n+1):
        if at[i] > k:
            break
        for j in reversed(range(m+1)):
            if k >= at[i] + bt[j]:
                break
        ans = max(ans, i+j)

    print(ans)


if __name__ == '__main__':
    main(sys.stdin.readline, print)


Fixed submission:
import sys
sys.setrecursionlimit(10**6)

INF = 10**11


def main(input, print):
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    at = [0] * (n+1)
    bt = [0] * (m+1)
    for i in range(1, n+1):
        at[i] = at[i-1]+a[i-1]
    for i in range(1, m+1):
        bt[i] = bt[i-1]+b[i-1]

    ans = 0
    j = m
    for i in range(n+1):
        if at[i] > k:
            break
        while k < at[i] + bt[j]:
            j -= 1
        ans = max(ans, i+j)

    print(ans)


if __name__ == '__main__':
    main(sys.stdin.readline, print)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)


def main(input, print):
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    a.insert(0, 0)
    b.insert(0, 0)
    ans = 0
    at = 0
    for i in range(n+1):
        at += a[i]
        if at > k:
            break
        bt = at
        for j in range(m+1):
            if bt + b[j] > k:
                break
            bt += b[j]
            if ans < i+j:
                ans = i+j
    print(ans)


if __name__ == '__main__':
    main(sys.stdin.readline, print)


Fixed submission:
import sys
sys.setrecursionlimit(10**6)

INF = 10**11


def main(input, print):
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    at = [0] * (n+1)
    bt = [0] * (m+1)
    for i in range(1, n+1):
        at[i] = at[i-1]+a[i-1]
    for i in range(1, m+1):
        bt[i] = bt[i-1]+b[i-1]

    ans = 0
    j = m
    for i in range(n+1):
        if at[i] > k:
            break
        while k < at[i] + bt[j]:
            j -= 1
        ans = max(ans, i+j)

    print(ans)


if __name__ == '__main__':
    main(sys.stdin.readline, print)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)


def main(input, print):
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    a.insert(0, 0)
    b.insert(0, 0)
    ans = 0
    at = 0
    for i in range(n+1):
        at += a[i]
        bt = at
        for j in range(m+1):
            if bt + b[j] > k:
                break
            bt += b[j]
            if ans < i+j:
                ans = i+j
    print(ans)


if __name__ == '__main__':
    main(sys.stdin.readline, print)


Fixed submission:
import sys
sys.setrecursionlimit(10**6)

INF = 10**11


def main(input, print):
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    at = [0] * (n+1)
    bt = [0] * (m+1)
    for i in range(1, n+1):
        at[i] = at[i-1]+a[i-1]
    for i in range(1, m+1):
        bt[i] = bt[i-1]+b[i-1]

    ans = 0
    j = m
    for i in range(n+1):
        if at[i] > k:
            break
        while k < at[i] + bt[j]:
            j -= 1
        ans = max(ans, i+j)

    print(ans)


if __name__ == '__main__':
    main(sys.stdin.readline, print)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)


def main(input, print):
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    a.insert(0, 0)
    b.insert(0, 0)
    ans = 0
    at = 0
    for i in range(n+1):
        at += a[i]
        bt = at
        for j in range(m+1):
            if bt + b[j] > k:
                break
            bt += b[j]
            ans = max(ans, i+j)
    print(ans)


if __name__ == '__main__':
    main(sys.stdin.readline, print)


Fixed submission:
import sys
sys.setrecursionlimit(10**6)

INF = 10**11


def main(input, print):
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    at = [0] * (n+1)
    bt = [0] * (m+1)
    for i in range(1, n+1):
        at[i] = at[i-1]+a[i-1]
    for i in range(1, m+1):
        bt[i] = bt[i-1]+b[i-1]

    ans = 0
    j = m
    for i in range(n+1):
        if at[i] > k:
            break
        while k < at[i] + bt[j]:
            j -= 1
        ans = max(ans, i+j)

    print(ans)


if __name__ == '__main__':
    main(sys.stdin.readline, print)