peterverebics/CodeNet_Python_428 huggingface dataset:
# Judge status: Accepted
wmax,hmax,n = map(int,input().split())
wmin = hmin = 0
for i in range(n):
  x,y,a = map(int,input().split())
  if a == 1:
    if wmin < x:
      wmin = x
  elif a == 2:
    if wmax > x:
      wmax = x
  elif a == 3:
    if hmin < y:
      hmin = y
  else:
    if hmax > y:
      hmax = y

if wmax-wmin < 0 or hmax-hmin < 0:
  print(0)
else:
  print((wmax-wmin) * (hmax-hmin))
# Judge status: Accepted
K,P=map(int,input().split())
A=list(map(int,input().split()))

x=dict()
x[0]=0
x[1]=0
for i in range(K):
    if A[i]%2==0:
        x[0]+=1
    else:
        x[1]+=1
if x[1]==0:
    if P==0:
        print(2**K)
    else:
        print(0)
    exit()
print(2**(K-1))
# Judge status: Accepted
import sys, math
from functools import lru_cache
from collections import deque
sys.setrecursionlimit(500000)
MOD = 10**9+7

def input():
    return sys.stdin.readline()[:-1]

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]

def main():
    A, B, C = mi()
    if A < C < B or A > C > B:
        print('Yes')
    else:
        print('No')


if __name__ == '__main__':
    main()
# Judge status: Accepted
S = input()
T = input()
s, t = len(S), len(T)
L = []
for i in range(s-t+1):
  if all(S[i+j]==T[j] or S[i+j]=="?" for j in range(t)):
    L.append(S[:i]+T+S[i+t:])
L = [l.replace("?", "a") for l in L]
print(min(L) if len(L)>0 else "UNRESTORABLE")
# Judge status: Accepted
N = int(input())
A = list(map(int, input().split()))
Alice = 0
Bob = 0
A = sorted(A, reverse = True)
for i, a in enumerate(A):
    if i %2 == 0:
        Alice += a
    else:
        Bob += a

print(Alice - Bob)
# Judge status: Wrong Answer
n,m=map(int,input().split())
gakusei=[]
for i in range(n):
  gakusei.append(list(map(int,input().split())))
check=[]
for j in range(m):
  check.append(list(map(int,input().split())))

for i in range(0,n):
  minkyori=10*100
  keep=0
  for j in range(0,m):
    kyori=abs(gakusei[i][0]-check[j][0])+abs(gakusei[i][1]-check[j][1])
    if kyori<minkyori:
      minkyori=kyori
      keep=j+1
  print(keep)
# Judge status: Wrong Answer
import collections as c

n = int(input())

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

q = int(input())

s = sum(a)

cnt = c.Counter(a)

for i in range(q):
    b,c = map(int,input().split())
    s += (c-b)*cnt[b-1]
    cnt[c-1] += cnt[b-1]
    cnt[b-1] = 0
    print(s)
# Judge status: Accepted
a, b, c = map(int, input().split())

print("Yes" if c - b - a > 0 and (c - b - a) ** 2 > 4 * a * b else "No")
# Judge status: Accepted
N = int(input())
d = list(map(int, input().split()))
d.sort()

if(d[N//2-1] == d[N//2]):
    print(0)
else:
    print(d[N//2]-d[N//2-1])
# Judge status: Accepted
from operator import itemgetter

def main():
    N,M = map(int,input().split())
    PY = [list(map(int,input().split())) + [index] for index  in range(M)]
    PY.sort(key = itemgetter(1))
    PY.sort(key = itemgetter(0))
    Ans = [""]*M
    Ken = [0]*(N + 1)
    for i  in range(M):
        p,y,index = PY[i]
        Ken[p] += 1
        Ans[index] = str(p).zfill(6) + str(Ken[p]).zfill(6)
    print(*Ans,sep="\n")
    return 

if __name__ == "__main__":
    main()
# Judge status: Accepted
N = int(input())


Nlist = list(str(N))

keep = list(map(int,Nlist))


if sum(keep) % 9 == 0:
    print("Yes")
else:
    print("No")
# Judge status: Accepted
N, M = map(int, input().split())

A = list(map(int, input().split()))

if N >= sum(A):
    print(N - sum(A))
else:
    print(-1)
# Judge status: Accepted
n, k = map(int, input().split())
X = []
for i in range(n):
    a, b = map(int, input().split())
    X.append((a, b))

X.sort()
cnt = 0
for i in range(n):
    a, b = X[i]
    cnt += b
    if cnt >= k:
        print(a)
        exit()
# Judge status: Accepted
s = input()
L = s.find('A')
R = s.rfind('Z')
print(R-L+1)
# Judge status: Accepted
import math
N, X, T = map(int, input().split())
ans = int(math.ceil(N / X)) * T
print(ans)
# Judge status: Accepted
n=int(input())
ab=[list(map(int,input().split())) for _ in range(n)]
ab.sort(key=lambda x:x[1])
jikoku=0
for i in range(n):
  jikoku+=ab[i][0]
  if jikoku>ab[i][1]:
    print('No')
    exit()
print('Yes')
# Judge status: Accepted
A, B = map(int, input().split())
if A > B:
    A, B = B, A
diff = B - A
if diff % 2:
    print("IMPOSSIBLE")
else:
    print(A + diff // 2)
# Judge status: Accepted
def main():
    q, h, s, d = map(int, input().split())
    target = int(input())
    h = min(2 * q, h)
    s = min(2 * h, s)
    d = min(2 * s, d)
    answer = target // 2 * d + (target % 2) * s
    print(answer)


if __name__ == '__main__':
    main()
# Judge status: Accepted
import math
s = int(input())
x = (10**9-(s%10**9))%(10**9)
y = (s+x)//(10**9)
print (0,0,10**9,1,x,y)
# Judge status: Accepted
import math
A,B,N=map(float,input().split())
if N >= B:
    N = B-1
ans = math.floor(A*N/B) - A*math.floor(N/B)
print(int(ans))
# Judge status: Accepted
def main():
    S = input()
    N = len(S)
    a = S.count("a")
    b = S.count("b")
    c = S.count("c")
    ma = max(a, b, c)
    mi = min(a, b, c)
    if ma - mi > 1:
        print("NO")
    else:
        print("YES")


if __name__ == '__main__':
    main()
# Judge status: Accepted
S = input()
T = input()

ns = len(S)
nt = len(T)
idx = []

for i in range(ns - nt + 1):
    for j in range(nt):
        if S[i+j] != T[j] and S[i+j] != '?':
            break
    else:
        idx.append(i)

if idx != []:
    ans = []
    for i in idx:
        s = S[:i] + T + S[i+nt:]
        ans.append(s.replace('?', 'a'))
    ans.sort()
    print(ans[0])
else:
    print('UNRESTORABLE')
# Judge status: Accepted
import numpy as np
n,m = [int(i) for i in input().split()]

h = [int(i) for i in input().split()]

l = [[int(i)-1 for i in input().split()] for _ in range(m)]

ans = np.array([1 for _ in range(n)])

for x,y in l:
    if h[x]>h[y]:
        ans[y] = 0
    elif h[x] < h[y]:
        ans[x] = 0
    else:
        ans[x] = 0
        ans[y] = 0
        
print(np.count_nonzero(ans))
# Judge status: Accepted
s = input()
N = len(s)
c1 = 0
c2 = 0
for i in range(N):
    if i % 2 == 0:
        if s[i] != '0':
            c1 += 1
        else:
            c2 += 1
    else:
        if s[i] != '1':
            c1 += 1
        else:
            c2 += 1
print(min(c1, c2))
# Judge status: Accepted
N = int(input())

x = 0
for i in range(1, N+1):
    x += i
    if x >= N:
        j = i
        break

y = x-N

for i in range(1, j+1):
    if i == y:
        continue
    print(i)
# Judge status: Accepted
a = input()
if ord('a') <= ord(a) <= ord('z'):
  print('a')
else:
  print('A')
# Judge status: Wrong Answer
from collections import Counter
n = input()
l = input().split()
lt = [int(i) for i in l]
f = Counter(l)
for i in lt:
    print(f[str(i+1)])
# Judge status: Accepted
W,a,b = map(int,input().split())

if a+W >= b and a <= b:
  print(0)
elif a >= b+W and a > b:
  print(a-(b+W))
else:
  print(b-(a+W))
# Judge status: Accepted
#!/usr/bin/env python3
import numpy as np
import bisect
X = int(input())
A = np.cumsum(range(1, 10 ** 5))
if np.any(A == X):
    print(A.tolist().index(X) + 1)
else:
    idx = bisect.bisect_left(A, X)
    print(idx + 1)
# Judge status: Accepted
"""n,m,q = (int(i) for i in input().split())
a = []
ans = 0

for i in range(q):
	a.append(list(map(int,input().split())))

for i in range(q):
	if
		ans+=a[i][3]

print(ans)
"""

a,b,n = (int(i) for i in input().split())
x = 0
if b<=n:
	x = b-1
else:
	x = n
c = a*x
print(c//b)
# Judge status: Accepted
N,K=map(int, input().split())
L=[0]*(10**5+1)

for i in range(N):
    a,b=map(int, input().split())
    L[a]+=b


res=K
for i,l in enumerate(L):
    if l>0:
        res-=l
        if res<=0:
            print(i)
            break
#print(L)
# Judge status: Accepted
n=int(input())
x=0
b=[]
s=input()
for S in s:
    if S=='I':
        x += 1
        b.append(x)
    else:
        x -= 1
        b.append(x)
print(max(max(b),0))
# Judge status: Wrong Answer
import string
from collections import Counter

ans = ""
dic = {}

for a in string.ascii_lowercase:
    dic[a] = 51

N = int(input())
for _ in range(N):
    S = list(input())
    S = Counter(S)
    
    for l, c in dic.items():
        if dic[l] > S[l]:
            dic[l] = S[l]

for k, v in dic.items():
    if v != 0:
        ans += k * v

print(ans)
# Judge status: Accepted
k = int(input())
a, b = map(int,input().split())
if a%k == 0 or a//k != b//k:
  print("OK")
else:
  print("NG")
# Judge status: Wrong Answer
S = input("")
if len(S)<6:
    print("If S is coffee-like, print Yes; otherwise, print No.")
else:
    if ((S[2]) == (S[3])):
        if ((S[4]) == (S[5])):
            print ("Yes")
    else:
        print("No")
# Judge status: Accepted
def main():
    k, x = map(int, input().split())
    anslis = [x for x in range(x-k+1, x+k)]
    print(*anslis)

    
if __name__ == "__main__":
    main()
# Judge status: Accepted
N = int(input())
S = str(input())

if len(S) % 2 == 0:
  S1 = S[:N // 2]
  S2 = S[N // 2:]
  if S1 == S2:
    print('Yes')
    exit()
  else:
    print('No')
    exit()
else:
  print('No')
# Judge status: Accepted
a,b,c = map(list,input().split())
if a == b and b == c:
    print('1')
elif a == b or a == c or b == c:
    print('2')
else:
    print('3')
# Judge status: Wrong Answer
n, a, b = map(int, input().split())

MOD = 10**9 + 7
N = pow(2,n,MOD)

def com(n,r):
    s = 1
    for i in range(n-r+1, n+1):
        s *= i
        s %= MOD

    t = 1
    for j in range(1,r+1):
        t *= j
        t %= MOD


    return s * pow(t,MOD-2, MOD) % MOD

print((N - com(n, a) - com(n, b)) % (MOD))
# Judge status: Wrong Answer
import math
A,B=map(int,input().split())
C=max(100*A/8,10*B)
D=min(100*(A+1)/8,10*(B+1))
if math.ceil(C)>math.floor(D):
    print("-1")
else:
    print(math.ceil(C))
# Judge status: Accepted
def main():
    length = int(input())
    word = input()
    count = {}
    for i in range(length):
        if word[i] in count:
            count[word[i]] += 1
        else:
            count[word[i]] = 2
    mod = 10 ** 9 + 7
    answer = 1
    for c in count.values():
        answer = (answer * c) % mod
    print(answer - 1)


if __name__ == '__main__':
    main()
# Judge status: Accepted
n = int(input())
if n == 0:
    print(0)
    exit()
i = 1
ans = []
while n != 0:
    if n % (i*2) != 0:
        n -= i
        ans.append(1)
    else:
        ans.append(0)
    i *= -2
print("".join(map(str,ans[::-1])))
# Judge status: Wrong Answer
import numpy as np
n = int(input())
a = list(map(int, input().split()))
p = 10**9 + 7
num_a = np.array([0]*60)
str_a = []
for i in a:
  a_str = list(map(int, format(i, 'b').zfill(60)))
  num_a = num_a + np.array(a_str)
num_a =reversed(num_a)
ans = 0
for i, j in enumerate(num_a):
  ans =  (ans + j*(n-j)*2**i)%p

print(ans)
# Judge status: Wrong Answer

MOD = 1000000007

def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a%b)

def lcm(a, b):
    return a//gcd(a,b)*a

n = int(input())
a = list(map(int,input().split()))
all_lcm = 1
for i in range(n):
    all_lcm = lcm(all_lcm, a[i])
ans = 0
for i in range(n):
    ans += all_lcm//a[i]
print(ans%MOD)
# Judge status: Accepted
# if required XOR, re-solve later.
# just copy and paste from https://atcoder.jp/contests/abc147/submissions/14707076
for i in range(pow(10,6)):
    pass
# added due to avoid duplicated search list wtih the same solver.
n = int(input())
a = list(map(int,input().split()))
d = 0
m = 10**9+7
for i in range(60):
    b = 1<<i
    l = len([1for j in a if j&b])
    d += l*(n-l)*b%m
    d %= m
print(d)
# Judge status: Accepted
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
for _ in range(k):
    c = [0] * n
    for i in range(n):
        c[max(0, i-a[i])] += 1
        if a[i] + i + 1 < n:
            c[a[i] + i + 1] -= 1
    j = 0
    for i in range(n):
        j += c[i]
        a[i] = j

    if sum(a) == n * n:
        print(*([n] * n))
        break
else:
    print(*a)
# Judge status: Accepted
W,H,x,y = map(int,input().split())
print((H*W)/2, int(W/2 == x and H/2 == y))
# Judge status: Wrong Answer
A = list(map(int,input().split()))
A = sorted(A)
cnt = 0

if A[1]-A[0]>=2:
  cnt += (A[1]-A[0])//2
  A[0]+= cnt*2

if A[0]==A[1]==A[2]:
  print(cnt)
  exit()
  
while(True):
  cnt+=1
  A = sorted(A)
  A[0]+=1
  A[1]+=1
  print(A)
  if A[0]==A[1]==A[2]:
    break

print(cnt)
# Judge status: Accepted
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

A = lr()
A.sort()
answer = sum(A) + 9 * max(A)
print(answer)
# Judge status: Accepted
N = int(input())
X_list = list(map(int, input().split()))

min_n = min(X_list)
max_n = max(X_list)
min_cost = 10**16

for n in range(min_n,max_n+1):
    cost = 0
    for x in X_list:
        cost = cost + (x - n)**2
    min_cost = min(min_cost, cost)

print(min_cost)
# Judge status: Wrong Answer
from sys import stdin

def main():
    n,w = map(int, input().split())
    ary = [list(map(int, stdin.readline().split())) for _ in range(n)]
    dp = [[0 for _ in range(w+1)] for _ in range(n)]
    for i in range(n-1):
        for y in range(w+1):
            dp[i+1][y] = dp[i][y-ary[i+1][0]] + ary[i+1][1] if ary[i+1][0] <= y else dp[i][y]
    print(dp[n-1][w])

if __name__ == '__main__':
    main()
# Judge status: Accepted
W, H, N = map(int, input().split())

border = [0,W,0,H]

for i in range(N):
  x, y, a = map(int, input().split())
  if a == 1:
    border[a-1] = max(border[a-1], x)
  elif a == 2:
    border[a-1] = min(border[a-1], x)
  elif a == 3:
    border[a-1] = max(border[a-1], y)
  elif a== 4:
    border[a-1] = min(border[a-1], y)
if border[1] - border[0] < 0 and border[3] - border[2] < 0:
  print(0)
  exit()
    
print(max((border[1] - border[0]) * (border[3] - border[2]), 0))
# Judge status: Accepted
n = int(input())
s = str(n)
ans1 =0
for i in range(len(s)):
    ans1 += int(s[i])
ans2 = (len(s)-1)*9+int(s[0])-1
print(max(ans1, ans2))
# Judge status: Accepted
n,q=map(int,input().split())
s=list(input())
x=[[] for i in range(q)]
for i in range(q):
    l,r=map(int,input().split())
    x[i].append(l)
    x[i].append(r)
ans=[0 for i in range(n+1)]
if s[0]=="A" and s[1]=="C":
    ans[2]=1
for i in range(3,n+1):
    if s[i-1]=="C" and s[i-2]=="A":
        ans[i]=ans[i-1]+1
    else:
        ans[i]=ans[i-1]
for i in range(q):
    print(ans[x[i][1]]-ans[x[i][0]])
# Judge status: Wrong Answer
a = str(input())
if a == "AAA"or"BBB":
  print("No")
else:
  print("Yes")
# Judge status: Accepted
k, a, b = map(int, input().split())
if b - a <= 2:
    print(k + 1)
else:
    print((k - (a - 1)) // 2 * (b - a) + (k - (a - 1)) % 2 + a)
# Judge status: Wrong Answer
n, m = map(int, input().split())
a = [set(list(map(int, input().split()))[1:]) for i in range(n)]
s = {}
for i in a:
  if a.index(i) == 0:
    s = i
  else:
    s = s & i
print(len(s))
# Judge status: Wrong Answer
S = input()
list1 = []
cnt = 0
for i in range(len(S)-1):
  if S[i] == S[i+1]:
    if len(S) ==2:
      cnt =  2
    else:
      cnt += 1
  else:
    if cnt > 2: 
      list1.append(cnt)
      cnt = 0

if list1 == []:
  print(len(S) - cnt + 1)
else:
  for x in list1:
    cnt += x - 1
  print(len(S) - cnt)
# Judge status: Accepted
w = input()

import collections as c

counts = c.Counter(w)
if all(elem % 2 == 0 for elem in counts.values()):
  print('Yes')
else:
  print('No')
# Judge status: Accepted
n = int(input())
v = list(map(int,input().split()))

v.sort()

value = v[0]

for i in range(1,n):
    value = (value+v[i])/2

print(value)
# Judge status: Wrong Answer
L = int(input())
print(L/3**3)
# Judge status: Accepted
N,M=map(int,input().split())
print((min(2*N,M)+M)//4)
# Judge status: Wrong Answer
def solve(ls, n, w):
	dp = [0] * (w + 1)
	for i in range(1, w + 1):
		for j in range(n):
			if i - ls[j][0] >= 0:
				dp[i] = max(dp[w - ls[j][0]] + ls[j][1], dp[i])
			else:
				dp[i] = dp[i - 1]
	return dp[w]


n, w = map(int, input().split())
ls = []
for i in range(n):
	ls.append(list(map(int, input().split())))
print(solve(ls, n, w))
# Judge status: Wrong Answer
h,n=map(int,input().split())
a_=0
magic=[]
for i in range(n):
  a,b=map(int,input().split())
  a_=max(a,a_)
  c=[a,b]
  magic.append(c)
dp=[10**7+1]*(h+a_+1)
dp[0]=0
for i in range(n):
  a,b=magic[i]
  for j in range(len(dp)):
    u=1
    while j+u*a<=h+a_:
      dp[j+u*a]=min(dp[j+u*a],dp[j]+u*b)
      u+=1
print(min(dp[h:]))
# Judge status: Accepted
L, R = map(int, input().split())

A = []
Lm = L % 2019
Rm = R % 2019
for _ in range(min(R - L + 1, 2019 * 2)):
    A.append(Lm)
    Lm = (Lm + 1) % 2019

A.sort()
ans = 2018
for i in range(len(A)):
    for j in range(i + 1, len(A)):
        ans = min(ans, (A[i] * A[j]) % 2019)
        if ans == 0:
            break

print(ans)
# Judge status: Accepted
from collections import Counter

def resolve():
    n = int(input())
    a = list(map(int, input().split()))
    acc = sum(a)
    counter = Counter(a)
    q = int(input())
    for _ in range(q):
        b, c = map(int, input().split())
        if b in counter:
            if c not in counter:
                counter[c] = 0
            acc += counter[b] * c - counter[b] * b
            counter[c] += counter[b]
            counter[b] = 0
        print(acc)

if __name__ == "__main__":
    resolve()
# Judge status: Wrong Answer
n=int(input())

ans = 1

i = 2

while i**2 <= n:
    u = 2
    while i**u <= n:
        ans = i**u
        u += 1
    i += 1
print(ans)
# Judge status: Accepted
a,b = map(int,input().split())
ans = a-1
if a<=b:
    ans += 1
print(ans)
# Judge status: Wrong Answer
import math

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

print(math.sqrt(A * A + B * B - 2 * A * B * math.cos(abs(30 * H - 6 * M) * math.pi * 2 / 360)))
# Judge status: Accepted
N=int(input())
N = N+ (N%2)
N/=2
N=int(N)
print(N)
# Judge status: Wrong Answer
import collections
import math


def main():
    x, y = map(int, input().split())

    if y % x == 0:
        print(-1)
        return
    print(x)


if __name__ == '__main__':
    main()
# Judge status: Accepted
n = int(input())
lst = list(map(int, input().split()))
all = 0
for i in range(n):
    all ^= lst[i]
for i in range(n-1):
    print(all ^ lst[i], end=' ')
print(all ^ lst[n-1])
# Judge status: Accepted
n, l = map(int, input().split())
S = [str(input()) for _ in range(n)]
S.sort()
print(''.join(S))
# Judge status: Accepted
from collections import deque
def main():
    n=int(input())
    x=deque()
    r=False
    for i in map(int,input().split()):
        if r:
            x.appendleft(i)
        else:
            x.append(i)
        r = not r
    if r:
        x.reverse()
    print(*x)

if __name__ == "__main__":
    main()
# Judge status: Accepted
import math
n, h = map(int, input().split())
a = []
b = []
ans = 0
for i in range(n):
    x, y=map(int, input().split())
    a.append(x)
    b.append(y)

b.sort(reverse=True)
a.sort(reverse = True)

for i in range(n):
    if b[i] >= a[0]:
        h -= b[i]
        ans += 1
    if h <= 0:
        break
if h > 0:
    ans += math.ceil(h / a[0])
print(ans)
# Judge status: Accepted
N = int(input())
A = int(input())

cnt = N//500
if(N-cnt*500<=A):
    print("Yes")
else:
    print("No")
# Judge status: Wrong Answer
N,Q=map(int,input().split())
S=input()
L=[]
R=[]
for i in range(Q):
  l,r=map(int,input().split())
  L.append(l)
  R.append(r)
for j in range(Q):
  print(S.count("AC",L[j],R[j]+1))
# Judge status: Accepted
N = int(input())
A = list(map(int, input().split()))

#Ai+i = j-Aj(j > i)

dic = {}
for i in range(N):
    if dic.get(A[i]+i) == None:
        dic[A[i]+i] = 1
    else:
        dic[A[i]+i] += 1
        
ans = 0
for j in range(N):
    if dic.get(j-A[j]) != None:
        ans += dic.get(j-A[j])
print(ans)
# Judge status: Accepted
n,k = map(int,input().split())
n = n % k
if n > k/2 :
    print(k-n)
else:
    print(n)
# Judge status: Accepted
import sys
sys.setrecursionlimit(300000)

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI0(): return map(lambda s: int(s) - 1, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
def LMI0(): return list(map(lambda s: int(s) - 1, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


S = input()
print(S[:3])
# Judge status: Accepted
n,x=map(int,input().split())
a=list(map(int,input().split()))
ans=0
if a[0]>=x:
    ans+=a[0]-x
    a[0]=x
for i in range(n-1):
    if a[i]+a[i+1]>=x:
        ans+=a[i]+a[i+1]-x
        a[i+1]=x-a[i]
    # print(a)
print(ans)
# Judge status: Accepted
import sys
import math
from collections import deque
import heapq
import itertools
MAX_INT = int(10e25)
MIN_INT = -MAX_INT
mod = 998244353
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N = I()
a = IL()

num = sum(a)
ans = 0
ko = 1
for i in range(N):
  ans += ko
  ko -= a[i]
  num -= a[i]
  ko = min(ko*2, num)

  if ko <= 0:
    print(-1)
    break
else:
  ans += ko
  ko -= a[-1]
  if ko < 0:
    print(-1)
  else:
    print(ans)
# Judge status: Wrong Answer
a = list(map(int, input().split()))

tmp = 0
if(len(a) != 4):
    exit()
if(a[0] > a[1]):
    tmp = a[1]
    a[1] = a[0]
    a[0] = tmp

if(a[2] > a[3]):
    tmp = a[3]
    a[3] = a[2]
    a[2] = tmp
a_len = list(range((a[0]), a[1] + 1))
b_len = list(range((a[2]), a[3] + 1))

max = -100000
print(a_len, b_len)
for str1 in a_len:
    for str2 in b_len:
        if(str1 * str2 > max):
            max = str1 * str2
print(max)
# Judge status: Wrong Answer
N, M = map(int, input().split())

d = 1
ds = []
while d ** 2 <= M:
    if M % d == 0:
        ds.append(d)
    d += 1
ds.sort(reverse=True)
for d in ds:
    if N <= M // d:
        print(d)
        break
# Judge status: Accepted
c = []
for _ in range(3):
    c.append([int(x) for x in input().split()])

a = [0]
b = [c[0][i] for i in range(3)]

for j in range(1, 3):
    a.append(c[j][j] - b[j])
ans = 'Yes'
for k in range(3):
    for m in range(3):
        if c[k][m] != a[k] + b[m]:
            ans = 'No'
            break
print(ans)
# Judge status: Wrong Answer
N = int(input())

def digit_sum(a, b):
    ab_sum = 0
    for i in range(1, len(str(a))+1):
        ab_sum += a % (10*i)
        a = a // (10*i)
    
    for i in range(1, len(str(b))+1):
        ab_sum += b % (10*i)
        b = b // (10*i)
    
    return ab_sum

max_sum = float("inf")
for i in range(1, N//2+1):
    a = i
    b = N - i
    if digit_sum(a, b) < max_sum:
        max_sum = digit_sum(a, b)

print(max_sum)
# Judge status: Accepted
A, B, N = map(int,input().split())

def floor(x):
  return (A * x // B) - A * (x // B)

print(floor(min(B - 1, N)))
# Judge status: Accepted
n = int(input())
l, l2 = [], []
for i in range(n):
    x, y = map(int, input().split())
    l.append(x + y)
    l2.append(x - y)
print(max(max(l) - min(l), max(l2) - min(l2)))
# Judge status: Accepted
s=input()
if s == "a" or s == "i" or s == "u" or s =="e" or s == "o":
    print("vowel")
else:
    print("consonant")
# Judge status: Accepted
K = int(input())
S = input()
if len(S)<=K:
    print(S)
else:
    print(S[0:K] + "...")
# Judge status: Accepted
a=list(input())
b=list(input())
c=list(input())
next=a.pop(0)
while True:
  if next=="a":
    if not a:
      print("A")
      break
    else:
      next=a.pop(0)
  elif next=="b":
    if not b:
      print("B")
      break
    else:
      next=b.pop(0)
  else :
    if not c:
      print("C")
      break
    else:
      next=c.pop(0)
# Judge status: Wrong Answer
import bisect

N, X = map(int, input().split())
L = list(map(int, input().split()))

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

print(bisect.bisect_right(L, X))
# Judge status: Accepted
def solve():
  A, B, C = map(int, input().split())
  K = int(input())
  ans = A+B+C+max([A,B,C])*(pow(2,K)-1)
  return ans
print(solve())
# Judge status: Accepted
W, H, x, y = map(int, input().split())
        
if (W / 2 == x) and (H / 2 == y):
    print(W * H / 2, 1)
else:
    print(W * H / 2, 0)