Problem:


Score : 300 points

Problem Statement

We have two desks: A and B. Desk A has a vertical stack of N books on it, and Desk B similarly has M books on it.

It takes us A_i minutes to read the i-th book from the top on Desk A (1 \leq i \leq N), and B_i minutes to read the i-th book from the top on Desk B (1 \leq i \leq M).

Consider the following action:

Choose a desk with a book remaining, read the topmost book on that desk, and remove it from the desk.

How many books can we read at most by repeating this action so that it takes us at most K minutes in total? We ignore the time it takes to do anything other than reading.

Constraints

1 \leq N, M \leq 200000

1 \leq K \leq 10^9

1 \leq A_i, B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

Output

Print an integer representing the maximum number of books that can be read.

Sample Input 1

3 4 240
60 90 120
80 150 80 150

Sample Output 1

3

In this case, it takes us 60, 90, 120 minutes to read the 1-st, 2-nd, 3-rd books from the top on Desk A, and 80, 150, 80, 150 minutes to read the 1-st, 2-nd, 3-rd, 4-th books from the top on Desk B, respectively.

We can read three books in 230 minutes, as shown below, and this is the maximum number of books we can read within 240 minutes.

Read the topmost book on Desk A in 60 minutes, and remove that book from the desk.

Read the topmost book on Desk B in 80 minutes, and remove that book from the desk.

Read the topmost book on Desk A in 90 minutes, and remove that book from the desk.

Sample Input 2

3 4 730
60 90 120
80 150 80 150

Sample Output 2

7

Sample Input 3

5 4 1
1000000000 1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000

Sample Output 3

0

Watch out for integer overflows.



Sample input:
3 4 240
60 90 120
80 150 80 150


Sample output:
3

Submission status: Runtime Error

Solution code:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

ans = 0
left_time = K
while True:
    if len(A) != 0 and A[0] <= B[0]:
        val = A.pop(0)
    elif len(B) != 0:
        B.pop(0)
    else:
        break

    left_time -= val
    if left_time < 0:
        break
    else:
        ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
from collections import deque
n,m,k = iim()
qa = deque(iil())
qb = deque(iil())

elp = 0
cnt = 0
while elp<k:
    if qa and qb:
        if qa[0] < qb[0]:
            elp += qa.popleft()
        elif qa[0] > qb[0]:
            elp += qb.popleft()
        else:
            if qa[1] < qb[1]:
                elp += qa.popleft()
            else:
                elp += qb.popleft()
    else:
        if qa:
            elp += qa.popleft()
        elif qb:
            elp += qb.popleft()
        else:
            break
    if elp <= k:
        cnt += 1
print(cnt)

Submission status: Runtime Error

Solution code:
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
at,bt=0,0
p=0
while p<k:
    if n!=at and a[at]<b[bt]:
        p+=a[at]
        at+=1
    else:
        p+=b[bt]
        bt+=1
    if n==at and m==bt:
        print(at+bt)
        break
    #print(p)
else:
    print(at+bt-1)

Submission status: Runtime Error

Solution code:
n,m,k = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

s = 0
cnt = 0
idx1 = 0
idx2 = 0
while s < k:
  if idx1 == n and idx2 == m:
    break
  elif idx1 == n:
    s += B[idx2]
    idx2 += 1
  elif idx2 == n:
    s += A[idx1]
    idx1 += 1
  else:
    if A[idx1] > B[idx2]:
      s += B[idx2]
      idx2 += 1
    else:
      s += A[idx1]
      idx1 += 1
  if s <= k:
    cnt += 1
  else:
    break
print(cnt)

Submission status: Runtime Error

Solution code:


N,M,K = map(int, input().split())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))

ai = 0
bi = 0
cnt = 0
while True:
    if ai == N and bi == M:
        break
    elif ai == N:
        if B_list[bi] <= K:
            cnt += 1
            K -= B_list[bi]
            bi += 1
        else:
            break
    elif bi == N:
        if A_list[ai] <= K:
            cnt += 1
            K -= A_list[ai]
            ai += 1
        else:
            break

    elif A_list[ai] < B_list[bi]:
        if A_list[ai] <= K:
            cnt += 1
            K -= A_list[ai]
            ai += 1
        else:
            break
    else:
        if B_list[bi] <= K:
            cnt += 1
            K -= B_list[bi]
            bi += 1
        else:
            break
print(cnt)

Submission status: Runtime Error

Solution code:
n, m, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

cnt_a = 0
cnt_b = 0
time = 0
ans = 0

while time <= k:
    if cnt_a == n and cnt_b == m:
        break
    elif cnt_a == n:
        if time + B[cnt_a] > k:
            break
        time += B[cnt_b]
        cnt_b += 1
        ans += 1
    elif cnt_b == m:
        if time + A[cnt_a] > k:
            break
        time += A[cnt_a]
        cnt_a += 1
        ans += 1
    else:
        if A[cnt_a] < B[cnt_b]:
            if time + A[cnt_a] > k:
                break
            time += A[cnt_a]
            cnt_a += 1
            ans += 1
        else:
            if time + B[cnt_a] > k:
                break
            time += B[cnt_b]
            cnt_b += 1
            ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
from typing import List

dp = {}

def solve(N: int, M: int, K: int, A: "List[int]", B: "List[int]"):
    r = _solve(0, K, A, B, 0, 0)
    print(r)
    return


def _solve(current:int, K: int, A: "List[int]", B: "List[int]", i, j):
    key = f'{i}_{j}'
    if key in dp: return dp[key]

    res = [current]
    if i < len(A) and A[i] <= K:
        res.append(_solve(current + 1, K - A[i], A, B, i + 1, j))

    if j < len(B) and B[j] <= K:
        res.append(_solve(current + 1, K - B[j], A, B, i, j + 1))

    dp[key] = max(res)
    return dp[key]


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    B = [int(next(tokens)) for _ in range(M)]  # type: "List[int]"
    solve(N, M, K, A, B)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

a = [0]*N
for i in range(N):
    a[i] = A[i]+a[i-1]
b = [0]*M
for i in range(M):
    b[i] = B[i]+b[i-1]
a = [0]+a
b = [0]+b
ans = 0
for i in range(N+1):
    c = a[i]
    if K-c>=0:
        j = bisect(b,K-c)
        ans = max(ans,i+j-1)
print(ans)

Submission status: Runtime Error

Solution code:
import math  # noqa: F401
from collections import deque
from typing import Dict, List, Optional, Tuple, Union  # noqa: F401


def main(N: int, M: int, K: int, A, B):
    A = deque(A)
    B = deque(B)

    t = 0
    ans = 0
    while t <= K:
        if A and B:
            if A[0] < B[0]:
                a = A.popleft()
                t += a
            else:
                b = B.popleft()
                t += b
            ans += 1
        elif A and not B:
            A.popleft()
            t += a
            ans += 1
        elif not A and B:
            B.popleft()
            t += b
            ans += 1
        else:
            break

    if t > K:
        ans -= 1

    print(ans)


if __name__ == "__main__":
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    main(N, M, K, A, B)

Submission status: Runtime Error

Solution code:
n,m,k = map(int, raw_input().split(' '))
ais = map(int,raw_input().split(' '))
bis = map(int,raw_input().split(' '))

mem = {}
def f(i,j,k,ais,bis):
	if (i,j,k) in mem:
		return mem[(i,j,k)]
	m = 0
	if i < len(ais) and ais[i] <= k:
		m = max(m , 1+ f(i + 1,j, k - ais[i],ais,bis ))
	if j < len(bis) and bis[j] <= k:
		m = max(m , 1+ f(i,j+1, k - bis[i],ais,bis ))
	mem[(i,j,k)] = m
	return m

print f(0,0, k, ais, bis)

Submission status: Runtime Error

Solution code:
n, m, k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
while k > 0:
    if a[0] < b[0] and a[0] <= k:
        k -= a[0]
        a.pop(0)
        ans += 1
    elif b[0] <= k:
        k -= b[0]
        b.pop(0)
        ans += 1
    else:
        break
print(ans)

Submission status: Runtime Error

Solution code:
n, m, k = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))

books = 0

while True:
	if A[0] < B[0]:
		if A[0] <= k:
			books += 1
			k -= A.pop(0)
		else:
			break
	else:
		if B[0] <= k:
			books += 1
			k -= B.pop(0)
		else:
			break
print(books)

Submission status: Runtime Error

Solution code:
N, M, K = map(int, input().split())
A = [int(a) for a in input().split()]
B = [int(a) for a in input().split()]
s = 0
for i, a in enumerate(A):
    if s + a > K:
        break
    s += a
else:
    i += 1
for j, b in enumerate(B):
    if s + b > K:
        break
    s += b
else:
    j += 1

ans = i + j
while i:
    i -= 1
    s -= A[i]
    while j < M and s + B[j] <= K:
        j += 1
        s += B[j]
    ans = max(ans, i + j)

print(ans)

Submission status: Runtime Error

Solution code:
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

cntA = 0
cntB = 0
time = 0
ans = 0
while time <= K:
    if ans >= N+M:
        break
    if cntA <= N-1 and A[cntA] <= B[cntB]:
        time += A[cntA]
        cntA += 1
    else:
        time += B[cntB]
        cntB += 1
    if time <= K:
        ans+=1
print(ans)

Submission status: Runtime Error

Solution code:
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ans = 0
while True:
    if len(a) < 1 and len(b) < 1:
        break
    elif len(a) < 1:
        k -= b[0]
        del b[0]
        ans += 1
    elif len(b) < 1:
        k -= a[0]
        del a[0]
        ans += 1
    else:
        if a[0] > b[0]:
            k -= b[0]
            del b[0]
            ans += 1
        elif a[0] < b[0]:
            k -= a[0]
            del a[0]
            ans += 1
        else:
            if a[1] > b[1]:
                k -= b[0]
                del b[0]
                ans += 1
            else:
                k -= a[0]
                del a[0]
                ans += 1
    if k < 0:
        break
if k < 0:
    print(ans-1)
else:
    print(ans)

Submission status: Runtime Error

Solution code:
n, m, k = map(int, input().split())

a = list(map(int, input().split()))
b = list(map(int, input().split()))

time = 0
count = 0
for _ in range(N * M):
    if a[-1] < b[-1]:
        time += a[-1]
        a = a[:-1]
    else:
        time += b[-i-1]
        b = b[:-1]
    if time > k:
        print(count)
        break
    else:
        count += 1

Submission status: Runtime Error

Solution code:
from  collections import deque

N, M, K =map(int, input().split())
A  = list(map(int, input().split()))
B  = list(map(int, input().split()))

A = deque(A)
B = deque(B)

ans = 0
while True:
  a = A[0]
  b = B[0]
  if min(a, b) > K:
    break
  else:
    if a <  b:
      a = A.popleft()
      K -= a
      ans += 1
    else:
      b = B.popleft()
      K -= b
      ans += 1
  #print(ans,  K)
      
      
print(ans)

Submission status: Runtime Error

Solution code:
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
a = 0
b = 0
ans = 0
if sum(A) + sum(B) <= K:
    print(N+M)
    exit()
while min(A[a],B[b]) <= K:
    if A[a] == min(A[a],B[b]):
        K -= A[a]
        a += 1
    else:
        K -= B[b]
        b += 1
    ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
n , m , k = map(int, input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
su = 0
at=0
bt=0
ans=0
while su <= k:
    if a[at]<=b[bt]:
        su+=a[at]
        ans+=1
        at+=1
    else:
        su+=b[bt]
        ans+=1
        bt+=1
print(ans-1)

Submission status: Runtime Error

Solution code:
from collections import deque

(n, m, k), aa, bb = [deque(map(int, l.split())) for l in open(0)]

i = 0

while k - min(aa[0], bb[0])>0:
    if aa[0]>bb[0]:
        k -= bb.popleft()
        i+=1
    else:
        k -= aa.popleft()
        i+=1

print(i)

Submission status: Runtime Error

Solution code:
# coding: utf-8
#import numpy as np
import re
import math
from collections import defaultdict,deque,Counter
from fractions import Fraction
import bisect
import itertools
from itertools import accumulate
from copy import deepcopy
import heapq
import random
import time
import os
import sys
from functools import lru_cache,reduce
readline=sys.stdin.readline
sys.setrecursionlimit(100000)
alp="abcdefghijklmnopqrstuvwxyz"
numbers="0123456789"
mod=int(10**9+7)
inf=int(10**19)
class Set():
    def __init__(self,A=[]):
        self.heap=[]
        self.dic=defaultdict(bool)
        self.size=0
        for a in A:self.add(a)
    
    def __str__(self):
        return str(self.heap)
    
    def add(self,x):
        if not self.find(x):
            heapq.heappush(self.heap,x)
            self.dic[x]=True
            self.size+=1

    def remove(self,x):
        if self.find(x):
            self.dic[x]=False
            self.size-=1

    def find(self,x):
        return self.dic[x]
    
    def top(self):
        while self.heap and self.dic[self.heap[0]]==False:
            heapq.heappop(self.heap)
        if self.heap:return self.heap[0]
        else:return None

    def pop(self):
        ret=None
        if self.heap:
            ret=self.top()
            self.remove(ret)
        return ret
class multiset():
    def __init__(self,A=[]):
        self.heap=[]
        self.dic=defaultdict(int)
        self.size=0
        for a in A:self.add(a)
    
    def __str__(self):
        return str(self.heap)
    
    def add(self,x):
        heapq.heappush(self.heap,x)
        self.dic[x]+=1
        self.size+=1

    def remove(self,x):
        if self.dic[x]>0:
            self.dic[x]-=1
            self.size-=1
    
    def count(self,x):
        return self.dic[x]

    def find(self,x):
        return self.dic[x]>0
    
    def top(self):
        while self.heap and self.dic[self.heap[0]]==0:
            heapq.heappop(self.heap)
        if self.heap:return self.heap[0]
        else:return None

    def pop(self):
        ret=None
        if self.heap:
            ret=self.top()
            self.remove(ret)
        return ret
class Edge():
    def __init__(self,x,y,val=1):
        self.x=x
        self.y=y
        self.val=val
class unionfind():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(n)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):
            self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def yn(b):print("yes" if b else "no")
def Yn(b):print("Yes" if b else "No")
def YN(b):print("YES" if b else "NO")
def ispow(a,b):
    now=b
    while now<a:
        now*=b
    return now==a
def getbin(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def getfacs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod_>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def nextcomb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:return num
def getprimes(n):
    if n==0:return []
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    ret=[]
    for a in range(n+1):
        if(A[a]):ret.append(a)
    return ret
def isprime(num):
    if(num<=1):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(b,t,f):return t if b else f
def factorize(n):
    b = 2
    ret=defaultdict(int)
    while b * b <= n:
        while n % b == 0:
            n //= b
            ret[b]+=1
        b+=1
    if n > 1:ret[n]+=1
    return ret
def inputint(row=1): 
    if row==1:
        ret=list(map(int,input().split()))
        if len(ret)==1:ret=ret[0]
    else:
        ret=[None]*row
        for a in range(row):
            ret[a]=int(input())
    return ret
def inputfloat(row=1): 
    if row==1:
        ret=list(map(float,input().split()))
        if len(ret)==1:ret=ret[0]
    else:
        ret=[None]*row
        for a in range(row):
            ret[a]=float(input())
    return ret
def istanchouzouka(A):
    f=True
    prev=min(A)-1
    for a in A:
        if prev>=a:
            f=False
        prev=a
    return f
def istanchouhigensyou(A):
    f=True
    prev=min(A)-1
    for a in A:
        if prev>a:
            f=False
        prev=a
    return f
def getallsum(A):
    s=sum(A)
    dp=defaultdict(int)
    dp[0]=1
    for a in range(0,len(A)):
        for b in range(s,-1,-1):
            if b-A[a]>=0:
                dp[b]+=dp[b-A[a]]
    return dp
def yuukoutomukou(edges):
    ret=deepcopy(edges)
    for edge in edges:
        if not edge.x==edge.y:
            ret.append(Edge(edge.y,edge.x,edge.val))
    return ret
def dijkstra(edges,V,start):
    mincost=[inf]*V
    G=[[] for a in range(V)]
    for edge in edges:
        G[edge.x].append([edge.val,edge.y])
    Q=PriorityQueue()
    Q.put([0,start])#[cost,x]
    while not Q.empty():
        nowcost,nowx=Q.get()
        if mincost[nowx]==inf:
            mincost[nowx]=nowcost
            for cost,y in G[nowx]:
                newcost=nowcost+cost
                Q.put([newcost,y])
    return mincost
def warshallfloyd(edges,V):
    mincost=[[inf for b in range(V)] for a in range(V)]
    for a in range(V):mincost[a][a]=0
    for edge in edges:
        mincost[edge.x][edge.y]=min(mincost[edge.x][edge.y],edge.val)#x→yが複数ある場合のためにmin()する
    for k in range(V):
        for s in range(V):
            for t in range(V):
                if mincost[s][k]==inf or mincost[k][t]==inf:continue
                mincost[s][t]=min(mincost[s][t],mincost[s][k]+mincost[k][t])
    return mincost
def bellemanford(edges,V,start):
    mincost=[inf]*V
    mincost[start]=0
    for _ in range(V):
        for edge in edges:
            if mincost[edge.x]==inf:continue
            mincost[edge.y]=min(mincost[edge.y],mincost[edge.x]+edge.val)
    return mincost
def getmd(x1,y1,x2,y2):return abs(x1-x2)+abs(y1-y2)
def geted(x1,y1,x2,y2):return math.sqrt((x1-x2)**2+(y1-y2)**2)
class fordfulkerson():
    def __init__(self,edges,V,s,t):
        self.V=V
        self.used=[False]*V
        self.G=[[] for a in range(V)]
        self.s=s
        self.t=t
        for edge in edges:
            self.G[edge.x].append({"x":edge.x,"y":edge.y,"cap":edge.val,"rev":len(self.G[edge.y])})
            self.G[edge.y].append({"x":edge.y,"y":edge.x,"cap":0,"rev":len(self.G[edge.x])-1})
    def dfs(self,v,t,f=inf):
        if v==t:return f
        self.used[v]=True
        for a in range(len(self.G[v])):
            x=self.G[v][a]["x"]
            y=self.G[v][a]["y"]
            cap=self.G[v][a]["cap"]
            rev=self.G[y][self.G[x][a]["rev"]]
            if self.used[y] or cap==0:continue
            f2=self.dfs(y,t,min(f,cap))
            if f2>0:
                self.G[v][a]["cap"]-=f2
                rev["cap"]+=f2
                return f2
        return 0
    def maxflow(self):
        flow=0
        while True:
            self.used=[False]*V
            zouka=self.dfs(self.s,self.t)
            if zouka==0:break
            flow+=zouka
        return flow
def zipsort(*args):
    ziplist=sorted(zip(*args))
    for a in range(len(args)):
        for b in range(len(ziplist)):
            args[a][b]=ziplist[b][a]
def zipreverse(*args):
    ziplist=list(zip(*args))
    rev=[]
    for a in range(len(ziplist)-1,-1,-1):
        rev.append(ziplist[a])
    for a in range(len(args)):
        for b in range(len(rev)):
            args[a][b]=rev[b][a]

######################################################################################################
N,M,K=inputint()
A=inputint()
B=inputint()
for a in range(10000):
    A.append(inf)
    B.append(inf)
A.reverse()
B.reverse()
ans=0
while K>=0:
    if A[-1]<B[-1]:
        K-=A.pop()
    else:
        K-=B.pop()
    if K>=0:
        ans+=1
print(ans)

Submission status: Runtime Error

Solution code:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

time = 0
A_cn = 0
B_cn = 0
while time <= K:
    if A[A_cn] <= B[B_cn]:
        time += A[A_cn]
        A_cn += 1
    else:
        time += B[B_cn]
        B_cn += 1
print(A_cn + B_cn)

Submission status: Runtime Error

Solution code:
N, M, K = list(map(int, input().split()))
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))

count = 0
while K >= min(As[0], Bs[0]):
  if As[0] <= Bs[0]:
    t = As.pop(0)
  else:
    t = Bs.pop(0)
  K -= t
  count += 1
  
print(count)

Submission status: Runtime Error

Solution code:
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
l=0
while len(a)>0 or len(b)>0:
    if len(a)>0 and a[0]<b[0]:
        p=a[0]
        a=a[1:]
    else:
        p=b[0]
        b=b[1:]
    if p<=k:
        l+=1
        k-=p
    else:
        break
print(l)

Submission status: Runtime Error

Solution code:
def resolve():
    N, M, K = [int(i) for i in input().split()]
    A = [int(i) for i in input().split()]
    B = [int(i) for i in input().split()]
    sumA = 0
    ai, bi = 0, 0
    while sumA <= K:
        if ai < N and A[ai] < B[bi] and sumA + A[ai] <= K:
            sumA += A[ai]
            ai += 1
        elif bi < M and sumA + B[bi] <= K:
            sumA += B[bi]
            bi += 1
        else:
            break
    print(ai + bi)


resolve()

Submission status: Runtime Error

Solution code:
a = int(input())

print(a+a**2+a**3)