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: Wrong Answer

Solution code:
n,m,k = map(int,input().split())
a = input().split(" ")
lista = [int(n) for n in a]
b = input().split(" ")
listb = [int(n) for n in b]
time = 0
count = 0
while k>time:
  if m==0 and n==0:
    break
  elif m>0 and n>0 and lista[0]>=listb[0]:
    time+=listb.pop(0)
    m-=1
    if time>k:
      break
    else:
      count+=1  
  elif m>0 and n>0 and listb[0]>=lista[0]:
    time+=lista.pop(0)
    n-=1
    if time>k:
      break
    else:
      count+=1
  elif n>0 and m==0:
    time+=lista.pop(0)
    n-=1
    if time>k:
      break
    else:
      count+=1
  elif m>0 and n==0:
    time+=listb.pop(0)
    m-=1
    if time>k:
      break
    else:
      count+=1  
print(count)

Submission status: Wrong Answer

Solution code:

def main():
    n, m, k = list(map(int, input().split()))
    a_books = list(map(int, input().split()))
    b_books = list(map(int, input().split()))
    a_idx = -1
    b_idx = -1
    sumbooks = sum(a_books) + sum(b_books)
    count = len(a_books) + len(b_books)
    if a_books[0] > k and b_books[0] > k:
        print(0)
        exit()
    elif sumbooks <= k:
        print(len(a_books) + len(b_books))
        exit()

    while sumbooks > k:
        # print(sumbooks, a_idx, b_idx)

        if b_idx >= len(b_books)*-1 and a_idx >= len(a_books)*-1:
            break
        elif b_idx >= len(b_books)*-1 or (a_idx < len(a_books)*-1 and a_books[a_idx] < b_books[b_idx]):
            sumbooks += a_books[a_idx]
            a_idx += -1
        elif a_idx >= len(a_books)*-1 or (b_idx < len(b_books)*-1 and b_books[b_idx] < a_books[a_idx]):
            sumbooks += b_books[b_idx]
            b_idx += -1
        if sumbooks <= k:
            break
        count += 1
    print(count)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, m, k = map(int, input().split())
alis = list(map(int, input().split())) + [10000000000]
blis = list(map(int, input().split())) + [10000000000]
su = 0
a = 0
b = 0
if sum(alis) + sum(blis) - 20000000000 <= k:
  print(len(alis) + len(blis))
  quit()
  
while su <= k:
  if alis[a] > blis[b]:
    su += blis[b]
    b += 1
  else:
    su += alis[a]
    a += 1
    
print(a + b - 1)

Submission status: Wrong Answer

Solution code:
import sys
import math
import itertools as it
def I():return int(sys.stdin.readline().replace("\n",""))
def I2():return map(int,sys.stdin.readline().replace("\n","").split())
def S():return str(sys.stdin.readline().replace("\n",""))
def L():return list(sys.stdin.readline().replace("\n",""))
def Intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def Lx(k):return list(map(lambda x:int(x)*-k,sys.stdin.readline().replace("\n","").split()))

if __name__ == "__main__":
    n,m,k = I2()
    a = Intl()
    b = Intl()
    t = 0
    i = 0
    j = 0
    cnt = 0
    while t <= k:
        #print(i,j,t)
        if i == n and j == m:
            break
        elif i < n and j < m:
            if a[i] <= b[j]:
                t += a[i]
                i += 1
                if t <= k:
                    cnt += 1
            else:
                t += b[j]
                j += 1
                if t <= k:
                    cnt += 1 
        elif i >= n and j < m:
            t += b[j]
            j += 1
            if t <= k:
                cnt += 1 
        elif i < n and j >= m:
            t += a[i]
            i += 1
            if t <= k:
                cnt += 1
    print(cnt)

Submission status: Wrong Answer

Solution code:
n, m, k = map(int, input().split())
ali = [int(i) for i in input().split()]
bli = [int(i) for i in input().split()]
ti = 0
count = 0

while ti <= k:
  if ali == [] and bli ==[]:
    break
  else:
    if ali == []:
      if ti + bli[0] > k:
        break
      else:
        ti += bli[0]
        del bli[0]
        count += 1
    elif bli == []:
      if ti + ali[0] > k:
        break
      else:
        ti += ali[0]
        del ali[0]
        count += 1
    else:
      if ti + ali[0] > k and ti + bli[0] > k:
        break
      elif ali[0] >= bli[0] and ti + bli[0] <= k:
        ti += bli[0]
        del bli[0]
        count += 1
      elif ali[0] < bli[0] and ti + ali[0] <= k:
        ti += ali[0]
        del ali[0]
        count += 1
      
print(count)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
from collections import deque

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

da = deque(a)
db = deque(b)
jikan = 0
satu = 0

while jikan <= k:
    if len(da) == 0 and len(db) == 0:
        break
    elif len(da) == 0:
         jikan = jikan + db.popleft()
    elif len(db) == 0:
        jikan = jikan + da.popleft()
    elif da[0] < db[0]:
        jikan = jikan + da.popleft()
    else:
        jikan = jikan + db.popleft()

    if jikan <= k:
        satu = satu + 1
print(satu)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from bisect import bisect_left
input = stdin.readline

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

for i in range(1, N):
    A[i] += A[i - 1]

for i in range(1, M):
    B[i] += B[i - 1]

ans = 0

for i, a in enumerate(A):
    time = K - a
    if time < 0:
        continue
    cnt = i + 1
    tmp = bisect_left(B, time)
    cnt += tmp
    if tmp < M and time == B[tmp]:
        cnt += 1
    ans = max(ans, cnt)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
N, M, K = [int(x) for x in input().split()]
A = [float(x) for x in input().split()]
B = [float(x) for x in input().split()]

AB = list()
a = 0
b = 0
for i in range(N+M):
  if (((a+1) <= N) and ((b+1) <= M)):
    if(A[a] < B[b]):
      AB.append(A[a])
      a += 1
    else:
      AB.append(B[b])
      b += 1
  elif((a) >= N):
    for j in range(M-b):
      AB.append(B[b])
      b += 1
    break
  elif((b) >= M):
    for j in range(N-a):
      AB.append(A[a])
      a += 1
    break

ans = 0
while K >= 0:
  K -= AB[ans]
  ans += 1
  if(ans == (N+M)):
    print(N+M)
    sys.exit()
  
print(ans-1)

Submission status: Wrong Answer

Solution code:
N, M, K = map(int, input().split())
A_in = list(map(int, input().split()) )
B_in = list(map(int, input().split()) )
from collections import deque

A = deque(A_in)
B = deque(B_in)

T = 0
N_book = 0
for i in range(len(A_in) + len(B_in)):
    if len(A)==0:
        if T+B[0] > K:
            break
        else:
            N_book+=1
            T+=B.popleft()
    elif len(B)==0:
        if T+A[0] > K:
            break
        else:
            N_book+=1
            T+=A.popleft()
    else:
        a = A[0]
        b = B[0]
        if T+min(a, b) > K:
            break
        elif a >= b:
            N_book+=1
            T+=B.popleft()
        else:
            N_book+=1
            T+=A.popleft()
print(N_book)

Submission status: Wrong Answer

Solution code:
# C

import itertools
import bisect

n, m, k = map(int, input().split())
a_l = [int(x) for x in input().split()]
b_l = [int(x) for x in input().split()]

cumsuma_l = list(itertools.accumulate(a_l)) 
cumsumb_l = list(itertools.accumulate(b_l))

ans = 0
for i in range(n):
    _a = cumsuma_l[i]
    if _a > k:
        break
    _tmp = k - _a
    _idx = bisect.bisect_left(cumsumb_l, _tmp)
    ans = max(ans, i + _idx + 2)
        
print(ans)

Submission status: Wrong Answer

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

count = 0
timecount = 0

if (sum(A) + sum(B)) <= K:
    count = (len(A) + len(B))
elif A[0] > K and B[0] > K:
    count = 0
else:
    while timecount <= K:
        if A == [] and B == []:
            break
        if A and B:
            timecount += min(A[0], B[0])
            if timecount > K:
                break
            if min(A[0], B[0]) == A[0]:
                A.pop(0)
            else:
                B.pop(0)
            count += 1
        elif A == []:
            timecount += B[0]
            B.pop(0)
            if timecount > K:
                break
            count += 1
        elif B == []:
            timecount += A[0]
            A.pop(0)
            if timecount > K:
                break
            count += 1

print(count)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

for _ in range(n+m):
    if len(a) + len(b) == 0:
        break
    if len(a) == 0:
        if b[0] > k:
            break
        k -= b.popleft()
    elif len(b) == 0:
        if a[0] > k:
            break
        k -= a.popleft()
    elif a[0] <= b[0]:
        if a[0] > k:
            break
        k -= a.popleft()
    else:
        if b[0] > k:
            break
        k -= b.popleft()

print(n + m - (len(a) + len(b)))

Submission status: Wrong Answer

Solution code:
n, m, k = (int(x) for x in input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a_sum=[0]
b_sum=[]
su = a + b
ans = 0
if sum(su)<=k:
    print(n+m)
else:
    for i in range(n):
        a_sum.append(a_sum[i]+a[i])
    for i in range(m):
        b_sum.append(b[i])
    for i in range(n+1):
        if a_sum[i]>k:
            break
        ta = k
        ta-=a_sum[i]
        for j in range(m):
            if ta<b_sum[j]:
                if i+j+1>ans:
                    ans = i+j
                break
print(ans)

Submission status: Wrong Answer

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

i, j = 0, 0
len_A, len_B = len(A), len(B)
time, count = 0, 0
while time < k:
    if i == len_A and j == len_B:
        break

    if i < len_A and j < len_B:
        if A[i] < B[j]:
            time += A[i]
            i += 1
        else:
            time += B[j]
            j += 1
    else:
        if i == len_A:
            time += B[j]
            j += 1
        else:
            time += A[i]
            i += 1

    if time > k:
        break

    count += 1

print(count)

Submission status: Wrong Answer

Solution code:
import sys
N, M, K = map(int, input().split())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
count = 0
while True:
    if (not A) and (not B):
        print(count)
        sys.exit()
    elif not A:
        K -= B[0]
        if K >= 0:
            count += 1
            del B[0]
        else:
            print(count)
            sys.exit()
    elif not B:
        K -= A[0]
        if K >= 0:
            count += 1
            del A[0]
        else:
            print(count)
            sys.exit()
    else:
        if A[0] <= B[0]:
            K -= A[0]
            if K >= 0:
                count += 1
                del A[0]
            else:
                print(count)
                sys.exit()
        else:
            K -= B[0]
            if K >= 0:
                count += 1
                del B[0]
            else:
                print(count)
                sys.exit()

Submission status: Wrong Answer

Solution code:
x = input().split()
y = input().split()
z = input().split()
s = 0

for nn in range(400000):
    if int(x[2]) < 0:
        break
    if int(y[0]) <= int(z[0]) :
        x[2] = int(x[2]) - int(y[0])
        del y[0]  
        y.append(1000000001) 
        s += 1
    else:
        x[2] = int(x[2]) - int(z[0])
        del z[0]
        z.append(1000000001)
        s += 1
print(s-1)

Submission status: Wrong Answer

Solution code:
n, m, k = map(int, input().split(' '))
a = list(map(int, input().strip().split(' ')))
b = list(map(int, input().strip().split(' ')))


timeLeft = k
count = 0;

i = 0
j = 0

while i < n and j < m:
    if (a[i] < b[j] and a[i] <= timeLeft):
        timeLeft -= a[i]
        i += 1
    elif(a[i] > b[j] and b[j] <= timeLeft):
        timeLeft -= b[j]
        j += 1
    else:
        break

while i < n :
    if (a[i] <= timeLeft):
        timeLeft -= a[i]
        i += 1
    else:
        break

while j < m :
    if (b[j] <= timeLeft):
        timeLeft -= b[j]
        j += 1
    else:
        break


print(i+j)

Submission status: Wrong Answer

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

a = 0
b = 0
La = len(A)
Lb = len(B)
ans = 0
S = 0
while a <= N or b <= B:
  if a == N:
    va = 100000000000000
  else:
    va = A[a]
  if b == M:
    vb = 100000000000000
  else:
    vb = B[b]
  
  Sa = S + va
  Sb = S + vb
  if Sa > K and Sb > K:
    break
  if va < vb:
    S += va
    a += 1
  else:
    S   += vb
    b += 1
 
  
print(a+b)

Submission status: Wrong Answer

Solution code:
M,N,K = [int(hoge) for hoge in input().split()]
A = [int(hoge) for hoge in input().split()]
B = [int(hoge) for hoge in input().split()]
S = 0
Abook = 0
Bbook = 0
curbook = 0
for a in A:
    if S + a <= K:
        Abook += 1
        curbook += 1
        S += a
nextB = 0
for n,b in enumerate(B):
    if S + b <= K:
        Bbook += 1
        curbook += 1
        S += b
    else:
        nextB = n
        break
if Abook == len(A) and Bbook == len(B):
    print(len(A) + len(B))
    exit()

ans = curbook
for i in range(Abook):
    Abook -= 1
    curbook -= 1
    S -= A[Abook]
    for b_index in range(nextB,len(B)):
        if S + B[b_index] <= K:
            Bbook += 1
            curbook += 1
            S += B[b_index]
            nextB += 1
        else:
            nextB = b_index
            break
    ans = max(ans,curbook)
print(ans)

Submission status: Wrong Answer

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

time = 0
count = -1
while time <= K:
    count += 1
    if len(A) == 0 and len(B) == 0:
        break
    elif len(A) == 0:
        time += B.pop(0)

    elif len(B) == 0:
        time += A.pop(0)

    else:
        if A[0] <= B[0]:
            time += A.pop(0)

        else:
            time += B.pop(0)
print(count)

Submission status: Wrong Answer

Solution code:
import sys
data = sys.stdin

conversion_data = []
for l in data:
    conversion_data.append([int(i) for i in l.split()])

table_a = conversion_data[1]
table_b = conversion_data[2]

N = conversion_data[0][0]
M = conversion_data[0][1]
limit = conversion_data[0][2]
max = max([N, M])

total_time = 0
position_a = 0
position_b = 0

for i in range(max):
  if position_a <= N - 1:
      a = table_a[position_a]
      if (a + total_time) <= limit:
          total_time += a
          position_a += 1
  
  if position_b <= M - 1:
    b = table_b[position_b]
    if (b + total_time) <= limit:
      total_time += b
      position_b += 1
  
print(position_a + position_b)

Submission status: Wrong Answer

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

que = list()

flag = True
val = 0

while flag:
    if len(A) == 0 and len(B) == 0:
        flag = False
        break
    elif len(A) > 0 and len(B) > 0:
        if A[0] == B[0]:
            if sum(A) < sum (B):
                val = A.pop(0)
            else:
                val = B.pop(0)
        elif A[0] > B[0]:
            val = B.pop(0)
        elif A[0] < B[0]:
            val = A.pop(0)
    elif len(A) == 0 and len(B) > 0:
        val = B.pop(0)
    elif len(B) == 0 and len(A) > 0:
        val = A.pop(0)

    if sum(que) + val <= K:
        que.append(val)
    else:
        flag = False
        break


print(len(que))

Submission status: Wrong Answer

Solution code:
from collections import deque
def resolve():
    N, M, K = list(map(int, input().split()))
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    read = deque()
    w = 0
    while w <= K:        
        if len(A) == 0 and len(B) == 0:
            break

        if len(A) == 0:
            if w + B[0] <= K:
                read.append(B[0])
                w += B[0]
            else:
                break
            B.pop(0)
            continue

        if len(B) == 0:
            if w + A[0] <= K:
                read.append(A[0])
                w += A[0]
            else:
                break
            A.pop(0)
            continue

        if A[0] < B[0]:
            if w + A[0] <= K:
                read.append(A[0])
                w += A[0]
            A.pop(0)
        else:
            if w + B[0] <= K:
                read.append(B[0])
                w += B[0]
            B.pop(0)

    print(len(read))


resolve()

Submission status: Wrong Answer

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

i = 0
j = 0
ans = 0

while k > 0:
    if i <= n-1 and j <= m-1:
        k -= min(a_lsit[i], b_list[j])
        if a_lsit[i] < b_list[j]:
            i += 1
        else:
            j += 1
    elif i > n-1:
        k -= b_list[j]
        j += 1
    elif j > m-1:
        k -= a_lsit[i]
        i += 1

    if k < 0:
        break

    ans += 1
    
print(ans)

Submission status: Wrong Answer

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

ans = 0
num_n = 0
num_m = 0
time = 0
while ans <= n+m:
  if num_n == n and num_m == m:
    break
  if num_n == n:
    tmp = b[num_m]
    num_m += 1
  elif num_m == m:
    tmp = a[num_n]
    num_n += 1
  else:
    if a[num_n] >= b[num_m]:
      tmp = b[num_m]
      num_m += 1
    else:
      tmp = a[num_n]
      num_n += 1
      
  if time + tmp <= k:
    time += tmp
    ans += 1
  else:
    break

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
import math

n,m,k = map(int,input().split())
li_a = deque(list(map(int,input().split())))
li_b = deque(list(map(int,input().split())))
ans,cnt = 0,0

for i in range(n+m):
    if n != 0:
        a_num = li_a[0]
    else:
        a_num = math.inf

    if m != 0:
        b_num = li_b[0]
    else:
        b_num = math.inf

    if a_num <= b_num:
        temp = a_num
        li_a.popleft()
        n -= 1
    else:
        temp = b_num
        li_b.popleft()
        m -= 1

    if cnt + temp > k:
        break
    else:
        cnt += temp
        ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
n,m,k=map(int,input().split())
l1=list(map(int,input().split()))
l2=list(map(int,input().split()))
a,b=l1[0],l2[0];c=0;x,y=0,0
while(k>=0):
  if x<len(l1) and y<len(l2) and k-min(l1[x],l2[y])>=0:
    k=k-min(l1[x],l2[y]);
    if l1[x]<l2[y]:
      x+=1;
    else:
      y+=1;
  elif x<len(l1) and k-l1[x]>=0:
    k=k-l1[x];
    x=x+1;
  elif y<len(l2) and k-l2[y]>=0:
    k=k-l2[y];
    y=y+1;
  else:
    break;
  c=c+1;
print(c)

Submission status: Wrong Answer

Solution code:
n, m, k = map(int, input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]

# print(n, m, k)
# print(a)
# print(b)

ans = set()
ans.add(0)
time_sum_a = 0
for i_n in range(0, n+1):
    if time_sum_a > k:
        break

    if i_n != 0:
        time_sum_a += a[i_n-1]

    start_idx = max(list(ans)[-1] - i_n, 0)
    time_sum_b = sum(b[:start_idx])
    # print('start_idx', start_idx)
    for i_m in range(start_idx+1, m+1):
        if time_sum_a + time_sum_b > k:
            break

        if i_m != 0:
            time_sum_b += b[i_m-1]

        time_sum = time_sum_a + time_sum_b
        # print(i_n, a[:i_n], i_m, b[:i_m], time_sum, k)
        if time_sum <= k:
            # print(i_n, a[:i_n], i_m, sum(b[:i_m]), k)
            ans.add(i_n + i_m)

# print(ans)

print(list(ans)[-1])

Submission status: Wrong Answer

Solution code:
from collections import deque

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

    sumA = 0
    sumB = 0

    tmp = 0

    dA = deque(A)
    dB = deque(B)

    dsA = deque()
    dsB = deque()

    while dA:
        tmp = dA.popleft()
        if sumA + tmp <= K:
            dsA.append(tmp)
            sumA += tmp
        else:
            break
    
    while dB:
        tmp = dB.popleft()
        if sumB + tmp <= K:
            dsB.append(tmp)
            sumB += tmp
        else:
            break

    sumall = sumA + sumB
    lenall = len(dsA) + len(dsB)
    Atmp = 0
    Btmp = 0

    while sumall > K:
        if Atmp == 0:
            Atmp = dsA.pop()
        if Btmp == 0:
            Btmp = dsB.pop()
        if Atmp >= Btmp:
            sumall -= Atmp
            lenall -= 1
            Atmp = 0
        else:
            sumall -= Btmp
            lenall -= 1
            Btmp = 0

    print(lenall)
                 
solve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
import os
import operator
from collections import defaultdict
import sys
from io import BytesIO, IOBase
import bisect

# def power(x, p):
#     res = 1
#     while p:
#         if p & 1:
#             res = res * x % 1000000007
#         x = x * x % 1000000007
#         p >>= 1
#     return res;

def main():
    # for _ in range(int(input())):
    #     n=int(input())
    #     n,k=map(int,input().split())
    #     arr=[int(k) for k in input().split()]
    #n=int(input())
    # n,k=map(int,input().split())
    # arr=[int(k) for k in input().split()]
    n,m,k=map(int,input().split())
    arr1=[int(el)for el in input().split()]
    arr2 = [int(el) for el in input().split()]
    stack1=[]
    stack2=[]
    for i in range(n-1,-1,-1):
        stack1.append(arr1[i])
    for i in range(m-1,-1,-1):
        stack2.append(arr2[i])
    count1=0
    ks=k
    stac1=stack1.copy()
    stac2=stack2.copy()

    while True:
        if len(stack1)>0 and len(stack2)>0:
            el1=stack1.pop()
            el2=stack2.pop()
            if el1<el2:
                if el1<=k:
                    k-=el1
                    count1+=1
                    stack2.append(el2)
                else:
                    break
            else:
                if el2<=k:
                    k-=el2
                    count1+=1
                    stack1.append(el1)
                else:
                    break
        elif len(stack1)==0 and len(stack2)>0:
            el2=stack2.pop()
            if el2<=k:
                k-=el2
                count1+=1
            else:
                break
        elif len(stack2)==0 and len(stack1)>0:
            el1=stack1.pop()
            if el1<=k:
                k-=el1
                count1+=1
            else:
                break
        else:
            break
    count2=0
    while True:
        if len(stac1)>0 and len(stac2)>0:
            el1=stac1.pop()
            el2=stac2.pop()
            if el1<=el2:
                if el1<=ks:
                    ks-=el1
                    count2+=1
                    stac2.append(el2)
                else:
                    break
            else:
                if el2<=ks:
                    ks-=el2
                    count2+=1
                    stac1.append(el1)
                else:
                    break
        elif len(stac1)==0 and len(stac2)>0:
            el2=stac2.pop()
            if el2<=ks:
                ks-=el2
                count2+=1
            else:
                break
        elif len(stac2)==0 and len(stac1)>0:
            el1=stac1.pop()
            if el1<=ks:
                ks-=el1
                count2+=1
            else:
                break
        else:
            break
    print(max(count1,count2))































BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import math
import sys
from collections import deque

N, M , K =1,1,1000
A = deque([1,1,1,1,1,1,1,1])
B = deque([1,1,1,1,1,1,1])


count = 0
time = 0
#もし先にどっちかの玉が尽きた時の問題。
for i in range(400001):
    #本が尽きていない。
    if len(A) != 0 and len(B) != 0:

        if A[0] < B[0]:
            time = time + A[0]
            if time <= K:
                count = count + 1
                A.popleft()

        elif A[0] > B[0]:
            time = time + B[0]
            if time <= K:
                count = count + 1
                B.popleft()
        else:
            time = time + A[0]
            if time <= K:
                count = count + 1
                A.popleft()
    else:
        #どっかの本が尽きている。
        if len(A) == 0 and len(B) != 0:
            time = time + B[0]
            if time <= K:
                count = count + 1
                B.popleft()
        elif len(A) != 0 and len(B) == 0:
            time = time +A[0]
            if time <= K:
                count = count + 1
                A.popleft()
        elif len(A) == 0 and len(B) == 0:
            print(count)
            sys.exit()
               


print(count)

Submission status: Wrong Answer

Solution code:
def totalCheck(A, K):
    total = 0
    counter = 0
    tem = 0
    for i in A:
        tem += i
        if tem <= K:
            total += i
            counter += 1
        else:
            del A[counter:]
    return A


def timeCheck(A, B):
  max = 0
  time = 0
  for i in range(len(A)):
    total = i + 1
    time += A[i]
    timec = time
    for j in range(len(B)):
      timec += B[j]
      if timec <= K:
        total += 1
      else:
        break
    if total > max:
      max = total
  print(max)

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

A = totalCheck(A, K)
B = totalCheck(B, K)

if len(A) >= len(B):
  timeCheck(B,A)
else:
  timeCheck(A,B)

Submission status: Wrong Answer

Solution code:
n,m,k = map(int,input().split())
A = [0]*n
B = [0]*m
for i,a in enumerate(input().split()):
    A[n-1-i] = int(a)
for i,b in enumerate(input().split()):
    B[m-1-i] = int(b)
counter = -1
time_left = k
while time_left >= 0:
    counter += 1
    if A != [] and B !=[]:
        a = A[len(A)-1]
        b = B[len(B)-1]
        if a >= b:
            time_left -= b
            B.pop()
        else:
            time_left -= a
            A.pop()
    elif A != []:
        a = A[len(A)-1]
        time_left -= a
        A.pop()
    elif B != []:
        b = B[len(B)-1]
        time_left -= b
        B.pop()
    else:
        break
print(counter)

Submission status: Wrong Answer

Solution code:
from decimal import *

n,m,k = map(Decimal,input().split())

a = list(map(Decimal,input().split()))[::-1]
b = list(map(Decimal,input().split()))[::-1]

#print(a)
#print(b)
ans = 0
while 1:
    #print(k, a,b)
    if b == [] and a == []:
        break
    elif b == [] and a != []:
        if a[-1] <= k:
            k -= a.pop()
            ans += 1
        else:
            break
    elif a == [] and b != []:
        if b[-1] <= k:
            k -= b.pop()
            ans += 1
        else:
            break
    else:
        if a[-1] > b[-1]:
            if b[-1] <= k:
                k -= b.pop()
                ans += 1
            else:
                break
        else:
            if a[-1] <= k:
                k -= a.pop()
                ans += 1
            else:
                break



print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A += [1000000001] * M
B += [1000000001] * N

count = 0
time = 0

tmp_A = A.pop(0)
tmp_B = B.pop(0)

for i in range(N+M):
    if tmp_A <= tmp_B:
        time += tmp_A
        tmp_A = A.pop(0)
    else:
        time += tmp_B 
        tmp_B = B.pop(0)
    if time > K:
        break
    count += 1
    
print(count)

Submission status: Wrong Answer

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

count = 0
time = 0
a = 0
b = 0
while time < k:
    if a == n:
        if b == m:
            print(n + m)
            sys.exit()
        else:
            time += B[b]
            b += 1
    elif b == m:
        time += A[a]
        a += 1
    elif A[a] < B[b]:
        time += A[a]
        a += 1
    else:
        time += B[b]
        b += 1
    
    count += 1
    # print(count, time)
    if time == k:
        print(count)
        sys.exit()
    
print(count-1)