-
Notifications
You must be signed in to change notification settings - Fork 222
/
game-of-two-stacks.py
52 lines (36 loc) · 1.04 KB
/
game-of-two-stacks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/python3
import sys
g = int(input().strip())
for a0 in range(g):
n,m,x = input().strip().split(' ')
n,m,x = [int(n),int(m),int(x)]
a = list(map(int, input().strip().split(' ')))
b = list(map(int, input().strip().split(' ')))
a = a[::-1]
b = b[::-1]
handy_stack = []
el_count = 0
total = 0
while len(a) > 0:
if total + a[-1] <= x:
temp = a.pop()
handy_stack.append(temp)
total += temp
el_count += 1
else:
break
total_b = 0
while len(b) > 0:
if total_b + b[-1] <= x:
if total + b[-1] <= x:
temp = b.pop()
total += temp
total_b += temp
el_count += 1
else:
temp = b.pop()
total = total - handy_stack.pop() + temp
total_b += temp
else:
break
print(el_count)