Harryxun/stf_run huggingface dataset:
a,b,c,d = map(int,input().split())
import math
gcdcd = c*d//int(math.gcd(c,d))
e = a-1
print( b - b//c - b//d + b//gcdcd - e + e//c + e//d - e//gcdcd)


### FIXED CODE ###

a,b,c,d = (map(int,input().split()))
import fractions
gcdcd = c*d//int(fractions.gcd(c,d))
e = a-1
print( b - b//c - b//d + b//gcdcd - e + e//c + e//d - e//gcdcd)
import bisect
s=input()
t=input()
dic={}
for v in s:
    dic[v]=True

for u in s:
    if not dic[u]:
        print(-1)
        exit()

nex={}
for i, v in enumerate(s+s):
    if not v in nex:
        nex[v]=[i]
    else:
        nex[v].append(i)
#print(s)
#print(t)
#print(nex)
sl=len(s)
tl=len(t)
def func(st, tn, su):
    #print(st, tn, su)
    if tn == tl:
        return su 
    ind=bisect.bisect_left(nex[t[tn]] ,st)
    nm=(nex[t[tn]][ind]-st)
    return func((st+nm)%sl, tn+1, su+nm)


res=func(-1, 0, 0)

print(res)

### FIXED CODE ###

import bisect
import sys

sys.setrecursionlimit(1000000)

s=input()
t=input()
dic={}
for v in s:
    dic[v]=True

for u in t:
    if not u in dic:
        print(-1)
        exit()

nex={}
for i, v in enumerate(s+s):
    if not v in nex:
        nex[v]=[i]
    else:
        nex[v].append(i)
#print(s)
#print(t)
#print(nex)
sl=len(s)
tl=len(t)
def func(st, tn, su):
    if tn == tl:
        return su
    #print(st, tn, su, t[tn])
    ind=bisect.bisect_left(nex[t[tn]] ,st+1)
    nm=(nex[t[tn]][ind]-st)
    return func((st+nm)%sl, tn+1, su+nm)


res=func(-1, 0, 0)

print(res)
x,y = map(int,input().split())
mn = 10**10
if x<=y: ans = min(ans,y-x)
if -x<=y: ans = min(ans,1+y-(-x))
if x<=-y: ans = min(ans,-y-x+1)
if -x<=-y: ans = min(ans,-y-(-x)+1)
print(ans)


### FIXED CODE ###

x,y = map(int,input().split())
ans = 10**10
if x<=y: ans = min(ans,y-x)
if -x<=y: ans = min(ans,1+y-(-x))
if x<=-y: ans = min(ans,-y-x+1)
if -x<=-y: ans = min(ans,-y-(-x)+2)
print(ans)
while True:
    l = map(int, raw_input().split())
    if l[0]==0 and l[1]==0 and l[2]==0: break
    n = l[0]+l[1]+l[2]
    s = [] #????¨????
    a = set(range(n)) #??????
    b = set() #?£????
    c = set() #??£???
    for i in range(input()):
        x, y, z, r = map(int, raw_input().split())
        x-=1;y-=1;z-=1;
        if r==0: s.append(set([x,y,z]))
        else:
            c.add(x);c.add(y);c.add(z);
    while True:
        f = True
        for lst in s:
            if len(lst & c) >=2:
                s.remove(lst)
                lst = lst - c
                b.add(lst.pop())
                f = False
        if f : break
    a = a - b; a = a - c;
    for i in range(n):
        if i in a: print 2
        elif i in b: print 0
        else: print 1

### FIXED CODE ###

while True:
    i,j,k = map(int, raw_input().split())
    if i==0 and j==0 and k==0: break
    n = i+j+k
    s = []
    a = set(range(n))
    b = set()
    c = set()
    for i in range(input()):
        x, y, z, r = map(int, raw_input().split())
        x-=1;y-=1;z-=1;
        if r==0: s.append(set([x,y,z]))
        else:
            c.add(x);c.add(y);c.add(z);
    while True:
        f = True
        for lst in s:
            if len(lst & c) >=2:
                s.remove(lst)
                lst = lst - c
                if len(lst)>=1: b.add(lst.pop())
                f = False
        if f : break
    a = a - b; a = a - c;
    for i in range(n):
        if i in a: print 2
        elif i in b: print 0
        else: print 1
N, K = map(int, input().split())
p = list(map(int, input().split()))
fac = [0] * 3010
fac[0] = 1
if (N >= 1):
    for i in range(1, 1010): fac[i] = i + 1 + fac[i - 1]
# 期待値
#print(p, fac[:max(p)])
exp = [0] * 3010
for i in range(N):
    exp[i] = fac[p[i] - 1] / p[i]
# print(exp)

Ans = 0
ans = [0] * 3000
ans[0] = sum(exp[:K])
for i in range(1, N - K + 1):
    if(i + K == N + 1): break
    ans[i] = ans[i - 1] - exp[i - 1] + exp[i + K - 1]
    # print(ans[i])
    #print(exp[i: i + K])
print(max(ans))


### FIXED CODE ###

N, K = map(int, input().split())
p = list(map(int, input().split()))
fac = [0] * 735477
fac[0] = 1
if (N >= 1):
    for i in range(1, 1010): fac[i] = i + 1 + fac[i - 1]
# 期待値
#print(p, fac[:max(p)])
exp = [0] * 735477
for i in range(N):
    exp[i] = fac[p[i] - 1] / p[i]
# print(exp)

Ans = 0
ans = [0] * 735477
ans[0] = sum(exp[:K])
for i in range(1, N - K + 1):
    if(i + K == N + 1): break
    ans[i] = ans[i - 1] - exp[i - 1] + exp[i + K - 1]
    # print(ans[i])
    #print(exp[i: i + K])
print(max(ans))
s=input()
a=s[:len(s)//2]
b=s[(len(s)//2)+2:]
if a==a.reverse() and b==b.reverse():
  print('Yes')
else:
  print('No')

### FIXED CODE ###

s=input()
ans='No'
if s==s[::-1]:
  t=s[:len(s)//2]
  if t==t[::-1]:
    u=s[len(s)//2+1:]
    if u==u[::-1]:
      ans='Yes'
print(ans)
class Dice:

    def __init__(self, faces):
        self.faces = tuple(faces)

    def roll_north(self):
        self.faces = (self.faces[1], self.faces[5], self.faces[2],
                      self.faces[3], self.faces[0], self.faces[4])

    def roll_south(self):
        self.faces = (self.faces[4], self.faces[0], self.faces[2],
                      self.faces[3], self.faces[5], self.faces[1])

    def roll_west(self):
        self.faces = (self.faces[2], self.faces[1], self.faces[5],
                      self.faces[0], self.faces[4], self.faces[3])

    def roll_east(self):
        self.faces = (self.faces[3], self.faces[1], self.faces[0],
                      self.faces[5], self.faces[4], self.faces[2])

    def number(self, face_id):
        return self.faces[face_id - 1]

    def issame(self, other_dice):
        for i in range(4):
            for j in range(4):
                if self.faces == other_dice.faces:
                    return True
                self.rotate()
            self.roll_north()
        for i in range(4):
            for j in range(4):
                if self.faces == other_dice.faces:
                    return True
                self.rotate()
            self.roll_east()
        return False

dice1 = Dice(list(map(int, input().split())))
dice2 = Dice(list(map(int, input().split())))

print("Yes" if dice1.issame(dice2) else "No")

### FIXED CODE ###

class Dice:

    def __init__(self, faces):
        self.faces = tuple(faces)

    def roll_north(self):
        self.faces = (self.faces[1], self.faces[5], self.faces[2],
                      self.faces[3], self.faces[0], self.faces[4])

    def roll_south(self):
        self.faces = (self.faces[4], self.faces[0], self.faces[2],
                      self.faces[3], self.faces[5], self.faces[1])

    def roll_west(self):
        self.faces = (self.faces[2], self.faces[1], self.faces[5],
                      self.faces[0], self.faces[4], self.faces[3])

    def roll_east(self):
        self.faces = (self.faces[3], self.faces[1], self.faces[0],
                      self.faces[5], self.faces[4], self.faces[2])

    def rotate(self):
        self.faces = (self.faces[0], self.faces[2], self.faces[4],
                      self.faces[1], self.faces[3], self.faces[5])

    def issame(self, other_dice):
        for i in range(4):
            if self.faces[0] == other_dice.faces[0]:
                for j in range(4):
                    if self.faces == other_dice.faces:
                        return True
                    self.rotate()
            self.roll_north()

        for i in range(4):
            if self.faces[0] == other_dice.faces[0]:
                for j in range(4):
                    if self.faces == other_dice.faces:
                        return True
                    self.rotate()
            self.roll_east()
        return False

dice1 = Dice(list(map(int, input().split())))
dice2 = Dice(list(map(int, input().split())))

print("Yes" if dice1.issame(dice2) else "No")
/* 
 * Created by solzard on 2020-03-05.
 * 
 */
#include <iostream>
#include <vector>
#include <unordered_set>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

bool is_bingo(int player) {
  std::unordered_set<int> winnable_conditions
      {0b111000000, 0b000111000, 0b000000111, 0b100100100, 0b010010010, 0b001001001, 0b100010001, 0b001010100};
  for (const auto &condition : winnable_conditions) {
    if ((player & condition) == condition) return true;
  }
  return false;
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);

  std::vector<int> sheet;
  std::unordered_set<int> bingo;
  int a, n, b;
  rep(_, 9) {
    std::cin >> a;
    sheet.emplace_back(a);
  }
  std::cin >> n;
  rep(_, n) {
    std::cin >> b;
    bingo.emplace(b);
  }
  int player = 0;
  rep(i, 9) {
    if (bingo.find(sheet[i]) != bingo.end()) {
      player |= 1 << i;
    }
  }
  std::cout << (is_bingo(player) ? "Yes" : "No") << "\n";
}


### FIXED CODE ###

# B - Bingo
def is_winnable(player: int) -> bool:
    winnable_conditions = \
        {0b111000000, 0b000111000, 0b000000111, 0b100100100, 0b010010010, 0b001001001, 0b100010001, 0b001010100}
    return any(player & condition == condition for condition in winnable_conditions)


def main():
    *ANB, = map(int, open(0).read().split())
    A, B = ANB[:9], set(ANB[10:])
    player = 0b000000000
    for i, a in enumerate(A):
        if a in B:
            player |= 1 << i
    print("Yes" if is_winnable(player) else "No")


if __name__ == "__main__":
    main()
import itertools
n,m,R = map(int,input().split())
r = list(map(int,input().split()))
cost = [[-1]*n for i in range(n)]
for i in range(m):
    a,b,c = map(int,input().split())
    cost[a-1][b-1] = c
    cost[b-1][a-1] = c

for k in range(n):
    for i in range(n):
        for j in range(n):
            if cost[i][k] == -1 or cost[k][j] == -1:
                continue
            if cost[i][j] == -1:
                cost = cost[i][k]+cost[k][j]
            else:
                cost[i][j] = min(cost[i][j], cost[i][k]+cost[k][j])

ans = -1
for p in itertools.permutations(r):
    c = 0
    for i in range(R-1):
        c += cost[p[i]-1][p[i+1]-1]
    if ans == -1:
        ans = c
    else:
        ans = min(c, ans)
print(ans)

### FIXED CODE ###

import itertools
n,m,R = map(int,input().split())
r = list(map(int,input().split()))
cost = [[-1]*n for i in range(n)]
for i in range(m):
    a,b,c = map(int,input().split())
    cost[a-1][b-1] = c
    cost[b-1][a-1] = c

for k in range(n):
    for i in range(n):
        for j in range(n):
            if k == i or k == j or cost[i][k] == -1 or cost[k][j] == -1:
                continue
            if cost[i][j] == -1:
                cost[i][j] = cost[i][k]+cost[k][j]
            else:
                cost[i][j] = min(cost[i][j], cost[i][k]+cost[k][j])

ans = -1
for p in itertools.permutations(r):
    c = 0
    for i in range(R-1):
        c += cost[p[i]-1][p[i+1]-1]
    if ans == -1:
        ans = c
    else:
        ans = min(c, ans)
print(ans)
n = int(input())
a = [int(i) for i in input().split()]
out = [] * n
for i in range(n):
    out[i] = (str(a.index(n-i)+1))
print(' '.join(out[::-1]))


### FIXED CODE ###

n = int(input())
a = [int(i) for i in input().split()]
out = [0] * n
for i in range(len(a)): out[a[i] - 1] = i + 1
print(' '.join([str(i) for i in out]))
kingaku, n = input().split()
no_num = input().split()
result = []
ok_num = []
kuriage_num = [-1, 0]

def kuriage(l):
    for i in ok_num:
        if result[l] < i:
            kuriage_num = [l, i]
            return
    if l <= 0:
        return
    return kuriage(l-1)

#使える数字のリストを作成
for i in range(10):
    if not str(i) in no_num:
        ok_num.append(str(i))

set_flg = False
for i in kingaku:
    set_flg = True
    for j in ok_num:
        if i == j:
            set_flg = False
            result.append(j)
            break
        elif i < j:
            result.append(j)
            print("".join(result).ljust(len(kingaku), ok_num[0]))
            exit()
            break
    
    if set_flg:
        ok_num.remove('0')
        print(ok_num[0].ljust(len(kingaku)+1, '0'))
        exit()

print(''.join(result))

### FIXED CODE ###

kingaku, n = input().split()
no_num = input().split()
result = []
ok_num = []
kuriage_num = [-1, 0]

def kuriage(l):
    for i in ok_num:
        if result[l] < i:
            kuriage_num = [l, i]
            return
    if l <= 0:
        return
    return kuriage(l-1)

#使える数字のリストを作成
for i in range(10):
    if not str(i) in no_num:
        ok_num.append(str(i))

set_flg = False
for i in kingaku:
    set_flg = True
    for j in ok_num:
        if i == j:
            set_flg = False
            result.append(j)
            break
        elif i < j:
            result.append(j)
            print("".join(result).ljust(len(kingaku), ok_num[0]))
            exit()
            break
    
    if set_flg:
        n = 0
        if ok_num[0] == '0':
            n = 1
        print(ok_num[n].ljust(len(kingaku)+1, ok_num[0]))
        exit()

print(''.join(result))
import bisect
N = int(input())
A = list(map(int, input().split()))

L = sorted(A[:2*N])
sumL = sum(L[N:])
R = sorted(A[N:])
sumR = sum(R[:N])

AL = [sumL]
p = N
for a in reversed(A[N:2*N]):  # sumLはだんだん減る
    ind = bisect.bisect_left(L, a)
    if ind >= p:
        sumL -= L[ind]
        p -= 1
        while L[p] == False:
            p -= 1
        sumL += L[p]
    AL.append(sumL)
    L[ind] = False
AR = [sumR]
p = N-1
for a in A[N:2*N]:  # sumRはだんだん増える
    ind = bisect.bisect_left(R, a)
    if ind <= p:
        sumR -= R[ind]
        p += 1
        while R[p] == False:
            p += 1
        sumR += R[p]
    AR.append(sumR)
    R[ind] = False
AL.reverse()
C = [AL[i] - AR[i] for i in range(N+1)]
print(max(C))


### FIXED CODE ###

import bisect
N = int(input())
A = list(map(int, input().split()))
L = sorted(A[:2*N])
sumL = sum(L[N:])
R = sorted(A[N:])
sumR = sum(R[:N])
AL = [sumL]
p = N
BL = [False] * (2*N)
for a in reversed(A[N:2*N]):  # sumLはだんだん減る
    ind = bisect.bisect_left(L, a)
    if BL[ind] != False:
        BL[ind] += 1
        ind += BL[ind] - 1
    if ind >= p:
        sumL -= L[ind]
        p -= 1
        while BL[p] != False:
            p -= 1
        sumL += L[p]
    AL.append(sumL)
    BL[ind] = 1
AR = [sumR]
p = N-1
BR = [False] * (2*N)
for a in A[N:2*N]:  # sumRはだんだん増える
    ind = bisect.bisect_left(R, a)
    if BR[ind] != False:
        BR[ind] += 1
        ind += BR[ind] - 1
    if ind <= p:
        sumR -= R[ind]
        p += 1
        while BR[p] != False:
            p += 1
        sumR += R[p]
    AR.append(sumR)
    BR[ind] = 1
AL.reverse()
C = [AL[i] - AR[i] for i in range(N+1)]
print(max(C))
n = int(input())
num_list = list(map(int, input().split()))

ans = 'Yes'

for i in range(n - 1):
  
  if i == 0:
    if num_list[i] > num_list[i + 1]:
    num_list[i] -= 1
  
  if num_list[i] > num_list[i + 1]:
    ans = 'No'
    break
  
  if num_list[i] < num_list[i + 1]:
    num_list[i + 1] -= 1
    
print(ans)

### FIXED CODE ###

n = int(input())
num_list = [int(i) for i in input().split()]
 
answer = "Yes"
 
for i in range(n-1):
  if num_list[i] - num_list[i+1] >= 2:
    answer = "No"
    break
  elif num_list[i] - num_list[i+1] >= 1:
    num_list[i+1] += 1
    
print(answer)
import math
a, b, c, d = map(int, input().split())
cn = b//c + (-a//c) + 1
dn = b//d + (-a//d) + 1
cd = (c*d)//math.gcd(c, d)
cdn = b//cd + (-a//cd) + 1
print(b-a+1-(cn+dn-cdn))

### FIXED CODE ###

import fractions
a, b, c, d = map(int, input().split())
cn = b//c + (-a//c) + 1
dn = b//d + (-a//d) + 1
cd = (c*d)//fractions.gcd(c, d)
cdn = b//cd + (-a//cd) + 1
print(b-a+1-(cn+dn-cdn))
from sys import stdin
a,b,c = map(int,input().split())
print("{} {} {}".format(c,b,a)

### FIXED CODE ###

from sys import stdin
a,b,c = map(int,input().split())
print("{} {} {}".format(c,a,b))
import numpy as np
N = int(input())
xyh = [list(map(int,input().split(' '))) for i in [0]*N]


ls = [[0]*101 for i in [0]*101]

cor = [[i,j] for i in range(101) for j in range(101)]

x,y,h = xyh[0]
for cx,cy in cor[:]:
	hx = abs(cx-x)
	hy = abs(cy-y)
	H = h+hx+hy
	if H <= 0:
		cor.remove([cx,cy])
	else:
		ls[cx][cy] = H

for x,y,h in xyh[1:]:
	for cx,cy in cor[:]:
		hx = abs(cx-x)
		hy = abs(cy-y)
		H = h + hx + hy
		if not ls[cx][cy] == H:
			cor.remove([cx,cy])

cx,cy = cor[0]
print(cx,cy,ls[cx][cy])







### FIXED CODE ###

N = int(input())
xyh = [list(map(int,input().split(' '))) for i in [0]*N]


ls = [[0]*101 for i in [0]*101]

cor = [[i,j] for i in range(101) for j in range(101)]


for cx,cy in cor:
	H = 0
	for x,y,h in xyh:
		if h != 0:
			hx = abs(cx-x)
			hy = abs(cy-y)
			H = h + hx + hy
			break
	ok = True 
	for x,y,h in xyh:
		hx = abs(cx-x)
		hy = abs(cy-y)
		if h != max(H-hx-hy,0):
			ok = False
			break
	if ok:
		break

print(cx,cy,H)
N = long(raw_input())
def dig(A,B):
        if A > B:
                return A
        else:
                return B
A = 0
B = 0 
X = []
i = long(1)
print N
while N > i:
        if N%i == 0:
                A = long(i)
                B = long(N/i)
                X.append(dig(A,B))
        if A in X:
                break
        i+=1
print len(str(min(X)))

### FIXED CODE ###

N = long(raw_input())
def dig(A,B):
        if A > B:
                return A
        else:
                return B
A = 0
B = 0 
X = []
i = long(1)
while N >= A*A:
        A = long(i)
        if N%i == 0:
                B = long(N/i)
                X.append(dig(A,B))
        if A in X:
                break
        i+=1
print len(str(min(X)))
a,b=input().split()
if (a<b):
  print("<")
elsif (a>b):
  print(">")
else:
  print("=")

### FIXED CODE ###

a, b = input().split()

if a < b:
  print('<')
elif a>b:
  print('>')
else:
  print('=')
n,p=map(int,input().split())


ans = 1


for i in range(2,min(int(p**(1./n))+1,int(sqrt(p))+1)):
	while(p%(i**n)==0):
		ans*=i
		p//=i**n
print(ans)


### FIXED CODE ###

n,p=map(int,input().split())


ans = 1
if(n==1):print(p)
else:

	for i in range(2,int(p**(1./n))+1):
		while(p%(i**n)==0):
			ans*=i
			p//=i**n
	print(ans)
n, q = map(int, input().split())
edge = [tuple(map(int, input().split())) for _ in range(n - 1)]
query = [tuple(map(int, input().split())) for _ in range(q)]

tree = [[] for _ in range(n)]
for a, b in edge:
    tree[a - 1].append(b - 1)
p = [0] * n
for x, y in query:
    p[x - 1] += y

counter = [0] * n

def solve(i=0, c=0):
    c += p[i]
    counter[i] = c
    for j in tree[i]:
        solve(j, c)
    return

solve()
for c in counter:
    print(c, end=' ')
print()

### FIXED CODE ###

import collections

n, q = map(int, input().split())
edge = [tuple(map(int, input().split())) for _ in range(n - 1)]
counter = [0] * n
for _ in range(q):
    p, x = map(int, input().split())
    counter[p - 1] += x

tree = [[] for _ in range(n)]
edge = collections.deque(edge)
while len(edge) != 0:
    a, b = edge.popleft()
    tree[a - 1].append(b - 1)
    tree[b - 1].append(a - 1)

que = collections.deque([0])
flag = [False] * n
while len(que) != 0:
    i = que.popleft()
    flag[i] = True
    for j in tree[i]:
        if flag[j]: continue
        counter[j] += counter[i]
        que.append(j)

print(' '.join([str(c) for c in counter]))
N,X=map(int,input().split())
L=list(map(int,input().split()))
D=[]
D[0]=0
for i in range(1,N+1):
  D.append(D[i-1]+L[i-1])
s=0
for j in range(0,N+1):
  if D[j]<=X:
    s+=1
print(s)

### FIXED CODE ###

N,X=map(int,input().split())
L=list(map(int,input().split()))
D=[]
D.append(0)
for i in range(1,N+1):
  D.append(D[i-1]+L[i-1])
s=0
for j in range(0,N+1):
  if D[j]<=X:
    s+=1
print(s)
from numpy.core.numeric import indices
from scipy.sparse.csgraph import bellman_ford, NegativeCycleError
import numpy as np

def dfs(s):
    for i in to[s]:
        if not toflag[i]:
            toflag[i]=True
            dfs(i)


def rdfs(s):
    for i in ot[s]:
        if not otflag[i]:
            otflag[i]=True
            rdfs(i)


def main():

    for i in range(m):
        a,b,c=map(lambda x:int(x)-1,input().split())
        g[a][b]=min(g[a][b], -(c+1-p))
        to[a].append(b)
        ot[b].append(a)

    toflag[0]=True
    otflag[n-1]=True
    dfs(0) # check for 0 to i path
    rdfs(n-1) # check for n-1 to i path
 
    for i in range(n):
        for j in range(n):
            if toflag[i] and otflag[i] and toflag[j] and otflag[j]: continue;
            g[i][j]=0

    try:
        d=bellman_ford(g, indices=[0]).astype(int)
        print(max(-d[0][-1], 0))
    except NegativeCycleError:
        print(-1)



if __name__=='__main__':
    n,m,p=map(int,input().split())
    g=np.array([[0]*n for i in range(n)])
    to=[[] for i in range(n)]
    ot=[[] for i in range(n)]
    toflag=[False]*n
    otflag=[False]*n
    main()

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**5)


def dfs(s):
    for i in to[s]:
        if not toflag[i]:
            toflag[i]=True
            dfs(i)


def rdfs(s):
    for i in ot[s]:
        if not otflag[i]:
            otflag[i]=True
            rdfs(i)


def bellman_ford():
    for i in range(n):
        upd=False
        for a,b,c in edges:
            if d[b]>d[a]+c:
                upd=True
                d[b]=d[a]+c     
        if not upd:
            break
        if i == n-1:
            print(-1)
            exit()


def main():

    for i in range(m):
        a,b,c=map(lambda x:int(x)-1,input().split())
        edges.append([a,b,-(c+1-p)])
        to[a].append(b)
        ot[b].append(a)

    toflag[0]=True
    otflag[n-1]=True
    dfs(0) # check for 0 to i path
    rdfs(n-1) # check for n-1 to i path
 
    for i in range(m):
        a,b,c=edges[i]
        if toflag[a] and otflag[a] and toflag[b] and otflag[b]: continue;
        edges[i]=[0,0,0]

    d[0]=0
    bellman_ford()
    print(max(0,-d[-1]))



if __name__=='__main__':
    n,m,p=map(int,input().split())
    edges=[]
    to=[[] for i in range(n)]
    ot=[[] for i in range(n)]
    toflag=[False]*n
    otflag=[False]*n
    d=[10**9]*n
    main()
dice = input().split()
direction = list(input())
dice2 = []
for i in range(len(direction)):
    dice2 = dice
    print(dice2)
    if direction[i] == 'E':
        dice = [dice2[3],dice2[1],dice2[0],dice2[5],dice2[4],dice2[2]]
        print(dice)
    elif direction[i] == 'N':
        dice = [dice2[1],dice2[5],dice2[2],dice2[3],dice2[0],dice2[4]]
        print(dice)
    elif direction[i] == 'S':
        dice = [dice2[4],dice2[0],dice2[2],dice2[3],dice2[5],dice2[1]]
        print(dice)
    else:
        dice = [dice2[2],dice2[1],dice2[5],dice2[0],dice2[4],dice2[3]]
        print(dice)
print('{0}'.format(int(dice[0)])) 


### FIXED CODE ###

dice = input().split()
direction = list(input())
dice2 = []
for i in range(len(direction)):
    dice2 = dice
    if direction[i] == 'E':
        dice = [dice2[3],dice2[1],dice2[0],dice2[5],dice2[4],dice2[2]]
    elif direction[i] == 'N':
        dice = [dice2[1],dice2[5],dice2[2],dice2[3],dice2[0],dice2[4]]
    elif direction[i] == 'S':
        dice = [dice2[4],dice2[0],dice2[2],dice2[3],dice2[5],dice2[1]]
    else:
        dice = [dice2[2],dice2[1],dice2[5],dice2[0],dice2[4],dice2[3]]
print('{0}'.format(int(dice[0])))
N = int(input())
arr = list(map(int, input().split()))

for i in range(min(N,25)):
    if arr[i] > i*2:
        print(-1)
        exit()
sum_arr = [0]*N
sum_arr[0] = arr[-1]
for i in range(1,N):
    sum_arr[i] = sum_arr[i-1]+arr[N-i]
sum_arr = sum_arr[::-1]
ans = [0]*(N+1)
ans[0] = 1
for i in range(1,N+1):
    if (ans[i-1]-arr[i-1])*2 <= sum_arr[i-1]:
        tmp = (ans[i-1]-arr[i-1])*2
    else:
        tmp = sum_arr[i-1]
    ans[i] = tmp
print(sum(ans))

### FIXED CODE ###

N = int(input())
arr = list(map(int, input().split()))
if N == 0:
    if arr[0] == 1:
        print(1)
    else:
        print(-1)
    exit()
else:
    if arr[0] != 0:
        print(-1)
        exit()
sum_arr = [0]*N
sum_arr[0] = arr[-1]
for i in range(1,N):
    sum_arr[i] = sum_arr[i-1]+arr[N-i]
sum_arr = sum_arr[::-1]
ans = [0]*(N+1)
ans[0] = 1
for i in range(1,N+1):
    if ans[i-1]-arr[i-1] < 0:
        print(-1)
        exit()
    tmp = min((ans[i-1]-arr[i-1])*2,sum_arr[i-1])
    ans[i] = tmp
for i in range(min(25,N+1)):
    if ans[i] > 2**i:
        print(-1)
        exit()
if ans[N] != arr[-1]:
    print(-1)
    exit()
print(sum(ans))