Buggy python submission (verdict: Time Limit Exceeded):
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
 
cost = {}#[[float('inf')]*(m+1) for i in range(n+1)]
#Aからi冊、Bからｊ冊読むときにかかる時間がcost[i][j]
              
cost[(0,0)] = 0
max_count = 0
 
i = 0
while i<=n:
  j = 0
  while j<m:
    cost[(i,j+1)] = cost[(i,j)] + b[j]
    j += 1
    if cost[(i,j)]<=k:
      max_count = max(max_count,i+j)
    else:
      break
  if i == n:
    break
  cost[(i+1,0)] = cost[(i,0)] + a[i]
  i += 1
  if cost[(i,0)]<=k:
    max_count = max(max_count,i)
  else:
    break
print(max_count)

Fixed submission:
def judge(num):
  if num>n+m:
    return False
  for i in range(max(0,num-m),min(n,num)+1):
    if a_cumsum[i] + b_cumsum[num-i]<=k:
      return True
  return False
 
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
 
a_cumsum = [0]
b_cumsum = [0]
for i in a:
  a_cumsum.append(a_cumsum[-1]+i)
for i in b:
  b_cumsum.append(b_cumsum[-1]+i)
  
left = 0
right = k+1
 
while right-left>1:
  mid = (right+left)//2
  
  if judge(mid):
    left = mid
  else:
    right = mid
 
print(left)

Buggy python submission (verdict: Time Limit Exceeded):
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))

cost = {}#[[float('inf')]*(m+1) for i in range(n+1)]
#Aからi冊、Bからｊ冊読むときにかかる時間がcost[i][j]
              
cost[(0,0)] = 0
max_count = 0

i = 0
while i<=n:
  j = 0
  while j<m:
    cost[(i,j+1)] = cost[(i,j)] + b[j]
    j += 1
    if cost[(i,j)]<=k:
      max_count = max(max_count,i+j)
    else:
      break
  if i == n:
    break
  cost[(i+1,0)] = cost[(i,0)] + a[i]
  i += 1
  if cost[(i,0)]<=k:
    max_count = max(max_count,i)
  else:
    break
print(max_count)

Fixed submission:
def judge(num):
  if num>n+m:
    return False
  for i in range(max(0,num-m),min(n,num)+1):
    if a_cumsum[i] + b_cumsum[num-i]<=k:
      return True
  return False
 
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
 
a_cumsum = [0]
b_cumsum = [0]
for i in a:
  a_cumsum.append(a_cumsum[-1]+i)
for i in b:
  b_cumsum.append(b_cumsum[-1]+i)
  
left = 0
right = k+1
 
while right-left>1:
  mid = (right+left)//2
  
  if judge(mid):
    left = mid
  else:
    right = mid
 
print(left)

Buggy python submission (verdict: Time Limit Exceeded):
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))

cost = [[float('inf')]*(m+1) for i in range(n+1)]
#Aからi冊、Bからｊ冊読むときにかかる時間がcost[i][j]
              
cost[0][0] = 0
max_count = 0

i = 0
while i<=n:
  j = 0
  while j<m:
    cost[i][j+1] = cost[i][j] + b[j]
    j += 1
    if cost[i][j]<=k:
      max_count = max(max_count,i+j)
    else:
      break
  if i == n:
    break
  cost[i+1][0] = cost[i][0] + a[i]
  i += 1
  if cost[i][0]<=k:
    max_count = max(max_count,i)
  else:
    break
print(max_count)

Fixed submission:
def judge(num):
  if num>n+m:
    return False
  for i in range(max(0,num-m),min(n,num)+1):
    if a_cumsum[i] + b_cumsum[num-i]<=k:
      return True
  return False
 
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
 
a_cumsum = [0]
b_cumsum = [0]
for i in a:
  a_cumsum.append(a_cumsum[-1]+i)
for i in b:
  b_cumsum.append(b_cumsum[-1]+i)
  
left = 0
right = k+1
 
while right-left>1:
  mid = (right+left)//2
  
  if judge(mid):
    left = mid
  else:
    right = mid
 
print(left)