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()))

cnt = 0
cnt_max = 0

t = 0

#aの本を全部読む場合を考える
#bを1冊ずつ増やしたときに最大になるかどうかを判定する

c = []

for i in a:
  t += i
  if t <= k:
    c.append(i)
    cnt += 1
  else:
    t -= i
    break

#print(c)
cnt_max = cnt
tmp_cnt = cnt

t_max = t

for i in range(len(c) + 1):
  if i == 0:
    pass
  else:
    t_max -= c[i-1]
    tmp_cnt -= 1
    
  #print(i, t_max, tmp_cnt)
  
  t = t_max
  cnt = tmp_cnt
  
  for j in b:
    t += j
    #print('j:', j, t)
    if t <= k:
      cnt += 1
      if cnt > cnt_max:
        cnt_max = cnt
        #print('cnt', cnt, 'max', cnt_max)
    else:
      break
      
print(cnt_max)
        


Fixed submission:
from collections import deque

n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = deque(list(map(int, input().split())))

cnt = 0
cnt_max = 0

t = 0

#aの本を全部読む場合を考える
#bを1冊ずつ増やしたときに最大になるかどうかを判定する

c = []

for i in a:
  t += i
  if t <= k:
    c.append(i)
    cnt += 1
  else:
    t -= i
    break

#print(c)
cnt_max = cnt
tmp_cnt = cnt

t_max = t

c.reverse()
for i in range(len(c) + 1):
  d = []
  
  if i == 0:
    pass
  else:
    t_max -= c[i-1]
    tmp_cnt -= 1
    
  #print(i, t_max, tmp_cnt, c[i-1])
  
  t = t_max
  cnt = tmp_cnt
  
  for j in b:
    t += j
    #print('j:', j, t)
    if t <= k:
      cnt += 1
      d.append(j)
      t_max = t
      if cnt > cnt_max:
        cnt_max = cnt
        #print('cnt', cnt, 'max', cnt_max)
    else:
      break
      
  for i in range(len(d)):
    b.popleft()
    tmp_cnt += 1
      
print(cnt_max)

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()))

cnt = 0
cnt_max = 0

t = 0

#aの本を全部読む場合を考える
#bを1冊ずつ増やしたときに最大になるかどうかを判定する

c = []

for i in a:
  t += i
  if t <= k:
    c.append(i)
    cnt += 1
  else:
    t -= i
    break

#print(c)
cnt_max = cnt
tmp_cnt = cnt

t_max = t

for i in range(len(c) + 1):
  if i == 0:
    pass
  else:
    t_max -= c[i-1]
    tmp_cnt -= 1
    
  #print(i, t_max, tmp_cnt)
  
  t = t_max
  cnt = tmp_cnt
  
  for j in b:
    t += j
    #print('j:', j, t)
    if t <= k:
      cnt += 1
      if cnt > cnt_max:
        cnt_max = cnt
        #print('cnt', cnt, 'max', cnt_max)
    else:
      break
      
print(cnt_max)
        


Fixed submission:
from collections import deque

n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = deque(list(map(int, input().split())))

cnt = 0
cnt_max = 0

t = 0

#aの本を全部読む場合を考える
#bを1冊ずつ増やしたときに最大になるかどうかを判定する

c = []

for i in a:
  t += i
  if t <= k:
    c.append(i)
    cnt += 1
  else:
    t -= i
    break

#print(c)
cnt_max = cnt
tmp_cnt = cnt

t_max = t

c.reverse()
for i in range(len(c) + 1):
  d = []
  
  if i == 0:
    pass
  else:
    t_max -= c[i-1]
    tmp_cnt -= 1
    
  #print(i, t_max, tmp_cnt, c[i-1])
  
  t = t_max
  cnt = tmp_cnt
  
  for j in b:
    t += j
    #print('j:', j, t)
    if t <= k:
      cnt += 1
      d.append(j)
      t_max = t
      if cnt > cnt_max:
        cnt_max = cnt
        #print('cnt', cnt, 'max', cnt_max)
    else:
      break
      
  for i in range(len(d)):
    b.popleft()
    tmp_cnt += 1
      
print(cnt_max)