Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
import numpy

def countInversions(A, cnt):
    if len(A) == 1:
        return cnt

    maxi = numpy.argmax(A)
    cnt += len(A) - maxi - 1
    del A[maxi]
    return countInversions(A, cnt)

if __name__ == "__main__":
    input()
    A = map(int, raw_input().split())
    print countInversions(A, 0)

Fixed submission:
import sys

cnt = 0

def merge(A, left, mid, right):
    n1 = mid - left
    n2 = right - mid
    L = range(n1 + 1)
    R = range(n2 + 1)

    for i in range(n1):
        L[i] = A[left + i]
    for i in range(n2):
        R[i] = A[mid + i]

    L[n1] = 1000000000
    R[n2] = 1000000000
    i = j = 0
    global cnt

    for k in range(left, right):
        if L[i] <= R[j]:
            A[k] = L[i]
            i += 1
        else:
            A[k] = R[j]
            j += 1
            cnt += n1 - i

def mergeSort(A, left, right):
    if left + 1 < right:
        mid = (left + right) / 2
        mergeSort(A, left, mid)
        mergeSort(A, mid, right)
        merge(A, left, mid, right)

if __name__ == "__main__":
    lines = sys.stdin.readlines()
    S = map(int, lines[1].split())
    mergeSort(S, 0, int(lines[0]))
    print cnt

Buggy python submission (verdict: Runtime Error):
def countInversions(A, cnt):
    if len(A) == 1:
        return cnt

    maxi = argmax(A)
    cnt += len(A) - maxi - 1
    del A[maxi]
    return countInversions(A, cnt)

def argmax(A):
    max = maxi = 0
    for i in range(len(A)):
        if A[i] > max:
            max = A[i]
            maxi = i
    return maxi

if __name__ == "__main__":
    input()
    A = map(int, raw_input().split())
    print countInversions(A, 0)

Fixed submission:
import sys

cnt = 0

def merge(A, left, mid, right):
    n1 = mid - left
    n2 = right - mid
    L = range(n1 + 1)
    R = range(n2 + 1)

    for i in range(n1):
        L[i] = A[left + i]
    for i in range(n2):
        R[i] = A[mid + i]

    L[n1] = 1000000000
    R[n2] = 1000000000
    i = j = 0
    global cnt

    for k in range(left, right):
        if L[i] <= R[j]:
            A[k] = L[i]
            i += 1
        else:
            A[k] = R[j]
            j += 1
            cnt += n1 - i

def mergeSort(A, left, right):
    if left + 1 < right:
        mid = (left + right) / 2
        mergeSort(A, left, mid)
        mergeSort(A, mid, right)
        merge(A, left, mid, right)

if __name__ == "__main__":
    lines = sys.stdin.readlines()
    S = map(int, lines[1].split())
    mergeSort(S, 0, int(lines[0]))
    print cnt
Buggy python submission (verdict: Runtime Error):
import sys

def minimumCostSort(A):
    B = sorted(A)
    V = [False] * len(A)
    T = [0] * 10000
    ans = 0
    s = min(A)

    for i in range(len(A)):
        T[B[i]] = i

    for i in range(len(A)):
        if V[i]:
            continue
        S = an = 0
        j = i
        m = 10000
        while True:
            V[j] = True
            an += 1
            v = A[j]
            m = min(m, v)
            S += v
            j = T[v]
            if V[j]:
                break
        ans += min(S + (an - 2) * m, m + S + (an + 1) * s)
    return ans

if __name__ == "__main__":
    lines = sys.stdin.readlines()
    A = map(int, lines[1].split())
    print minimumCostSort(A)

Fixed submission:
import sys

def minimumCostSort(A):
    B = sorted(A)
    V = [False] * len(A)
    T = [0] * 10001
    ans = 0
    s = min(A)

    for i in range(len(A)):
        T[B[i]] = i

    for i in range(len(A)):
        if V[i]:
            continue
        S = an = 0
        j = i
        m = 10000
        while True:
            V[j] = True
            an += 1
            v = A[j]
            m = min(m, v)
            S += v
            j = T[v]
            if V[j]:
                break
        ans += min(S + (an - 2) * m, m + S + (an + 1) * s)
    return ans

if __name__ == "__main__":
    lines = sys.stdin.readlines()
    A = map(int, lines[1].split())
    print minimumCostSort(A)
Buggy python submission (verdict: Time Limit Exceeded):
F_list = [1,1] + [None]*43

def fibo(n):
  F=F_list[n]
  if n==0 or n==1: return F
  F = fibo(n-1)+fibo(n-2)
  F_list[n]=F
  return F


n = int(input())

print(fibo(n))

Fixed submission:
F =[1,1] + [0 for i in range(44)]

n = int(input())

for i in range(2,n+1):
  F[i]=F[i-1]+F[i-2]

print(F[n])
Buggy python submission (verdict: Runtime Error):
n = int(input())
v = [[0] * n for _ in range(n)] 
for i in range(0,n):
  l = list(map(int,input().split()))
  u = l[0]
  k = l[1]
  for j in l[2:]:
    v[i-1][j-1]=1

for i in range(n):
  for j in range(n):
    print(v[i][j]+" ")

Fixed submission:
n = int(input())
v = [[0]*n  for _ in range(n)]
for i in range(n):
  l = list(map(int,input().split()))
  u = l[0]
  k = l[1]
  c = l[2:]
  for j in c:
    v[u-1][j-1]=1
for m in v:
    print(' '.join(map(str, m)))
Buggy python submission (verdict: Runtime Error):
def BFS(G):
    ans = [0] + [[i+1,-1] for i in range(len(G))]
    def main(G,i,d):
        ans[i] = [i, d]
        for j in G[i-1][2:]: main(G,j,d+1)
    main(G,1,0)
    return ans[1:]


if __name__=='__main__':
    n = int(input())
    G = [list(map(int,input().split())) for _ in range(n)]
    for out in BFS(G): print(*out)

Fixed submission:
def BFS(G):
    ans = [0] + [[i+1,-1] for i in range(len(G))]
    def main(G,i,d):
        if -1 < ans[i][1] <= d: return
        ans[i] = [i, d]
        for j in G[i-1][2:]: main(G,j,d+1)
    main(G,1,0)
    return ans[1:]


if __name__=='__main__':
    n = int(input())
    G = [list(map(int,input().split())) for _ in range(n)]
    for out in BFS(G): print(*out)
Buggy python submission (verdict: Time Limit Exceeded):
def judge_prime(x):
    A = list(range(2,x+1))
    while A[-1]==x:
        if A[0]==x: return True
        A = list(filter(lambda y: y%A[0]!=0, A))
    return False

if __name__=='__main__':
    N=int(input())
    cnt=0
    for _ in range(N):
        if judge_prime(int(input())): cnt+=1
    print(cnt)

Fixed submission:
def is_prime(x):
    if x == 2:
        return True
    if x < 2 or x%2==0:
        return False
    i = 3
    while i <= x**(0.5):
        if x%i==0:
            return False
        i+=2
    return True

if __name__=='__main__':
    N=int(input())
    cnt=0
    for _ in range(N):
        if is_prime(int(input())): cnt+=1
    print(cnt)
Buggy python submission (verdict: Runtime Error):
from collections import deque
n = int(input())
dl = deque()
for _ in range(n):
    cmd = input()
    if ' ' in cmd:
        op,v = cmd.split()
    else:
        op = cmd
    if   op == 'insert'     : dl.appendleft(v) 
    elif op == 'delete'     : dl.remove(v) 
    elif op == 'deleteFirst': dl.popleft() 
    elif op == 'deleteLast' : dl.pop() 
print(*dl)

Fixed submission:
from collections import deque
n = int(input())
dl = deque()
for _ in range(n):
    op = input().split()
    if   op[0] == 'insert'     : dl.appendleft(op[1]) 
    elif op[0] == 'delete'     :
        if op[1] in dl: dl.remove(op[1])
    elif op[0] == 'deleteFirst': dl.popleft() 
    elif op[0] == 'deleteLast' : dl.pop() 
print(*dl)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
D = input()
que = deque()
p = 0
for c in D:
    que.append(p)
    if   c == "\\" : p += -1
    elif c == "/"  : p +=  1
que.append(p)

cnt = 0
A = 0
L = []
Li = 0
ps = que.popleft()
pp = ps
while len(que) > 0:
    p = que.popleft()
    if Li == 0 and (p > ps or pp not in que):
            ps = p
    elif p == ps and p > pp:
        Li += 0.5
        cnt += 1
        A += int(Li)
        L.append(int(Li))
        Li = 0
    elif p < ps:
        if   p == pp: Li += ps - p
        elif p <  pp: Li += ps - p - 0.5
        else        : Li += ps - p + 0.5
    pp = p
        
print(A)
print(cnt, *L)

Fixed submission:
DOWN = []
EDGE = []
POOL = []

for i, c in enumerate(input()):
    if c == '\\':
        DOWN.append(i)
    elif c == '/':
        if DOWN:
            e = DOWN.pop()
            Li = i - e # Calcurate 1 line.  \______/ 
            while EDGE: # Merge connected pools.
                if EDGE[-1] > e:
                    EDGE.pop()
                    Li += POOL.pop()
                else:
                    break
            EDGE.append(e)
            POOL.append(Li)

print(sum(POOL))
print(len(POOL), *POOL)
Buggy python submission (verdict: Runtime Error):
ef binarySearch(S,t):
    if len(S) == 1:
        return S[0] == t
    m = len(S)//2
    if S[m] == t:
        return True
    if S[m] > t:
        return binarySearch(S[:m],t)
    if len(S) > 2:
        return binarySearch(S[m+1:],t)
    return False

if __name__=='__main__':
    n=int(input())
    S=list(map(int,input().split()))
    q=int(input())
    T=list(map(int,input().split()))
    cnt = 0
    for t in T: cnt += binarySearch(S,t)
    print(cnt)

Fixed submission:
def binarySearch(A, key):
    left = 0
    right = len(A)
    while left < right:
        mid = (left + right) // 2
        if A[mid] == key:
            return mid
        elif key < A[mid]:
            right = mid
        else:
            left = mid + 1
    return -1

if __name__=='__main__':
    n=int(input())
    S=list(map(int,input().split()))
    q=int(input())
    T=list(map(int,input().split()))
    cnt = 0
    print(sum(map(lambda t: binarySearch(S,t) >= 0, T)))

Buggy python submission (verdict: Time Limit Exceeded):
def binarySearch(S,t):
    if len(S) == 1:
        return S[0] == t
    m = len(S)//2
    if S[m] == t:
        return True
    if S[m] > t:
        return binarySearch(S[:m],t)
    if len(S) > 2:
        return binarySearch(S[m+1:],t)
    return False

if __name__=='__main__':
    n=int(input())
    S=list(map(int,input().split()))
    q=int(input())
    T=list(map(int,input().split()))
    cnt = 0
    for t in T: cnt += binarySearch(S,t)
    print(cnt)

Fixed submission:
def binarySearch(A, key):
    left = 0
    right = len(A)
    while left < right:
        mid = (left + right) // 2
        if A[mid] == key:
            return mid
        elif key < A[mid]:
            right = mid
        else:
            left = mid + 1
    return -1

if __name__=='__main__':
    n=int(input())
    S=list(map(int,input().split()))
    q=int(input())
    T=list(map(int,input().split()))
    cnt = 0
    print(sum(map(lambda t: binarySearch(S,t) >= 0, T)))
Buggy python submission (verdict: Time Limit Exceeded):
from itertools import combinations
n,k = list(map(int, input().split()))
k = min(n,k)
W = list(map(int,[input() for _ in range(n)]))
if k > 1: 
    P = 10001
    for b in combinations(range(1,n),k-1):
        s = 0
        Pi = 0
        for i in b:
            Pi = max(Pi, sum(W[s:i]))
            s = i
        P = min(P, max(Pi, sum(W[s:])))
    print(P)
else:
    print(sum(W))

Fixed submission:
def checkCapacity(W,k,P):
    ws = 0
    cnt = 1
    for wi in W:
        ws += wi
        if ws > P:
            cnt += 1
            ws = wi
        if cnt > k: return False
    return True

if __name__=='__main__':
    n,k = list(map(int, input().split()))
    W = list(map(int,[input() for _ in range(n)]))
    Pmax = sum(W)
    Pmin = max(W)
    while Pmin < Pmax:
        P = (Pmax + Pmin) // 2
        if checkCapacity(W,k,P):
            Pmax = P
        else:
            Pmin = P + 1
    print(Pmax)
Buggy python submission (verdict: Time Limit Exceeded):
def solve(m, A):
    if A[0] == m: return True
    if len(A)==1: return False
    if m < A[0] : return solve(m, A[1:])
    if solve(m-A[0], A[1:]):
        return True
    return solve(m, A[1:])

if __name__=='__main__':
    n=int(input()) 
    A=list(map(int,input().split()))
    q=int(input()) 
    Q=list(map(int,input().split()))
    for m in Q:
        if solve(m,A): print('yes') 
        else         : print('no') 

Fixed submission:
def solve(m, A):
    if A[0] == m: return True
    if len(A)==1: return False
    if m < A[0] : return solve(m, A[1:])
    if solve(m-A[0], A[1:]):
        return True
    return solve(m, A[1:])

if __name__=='__main__':
    n=int(input()) 
    A=list(map(int,input().split()))
    q=int(input()) 
    Q=list(map(int,input().split()))
    for m in Q:
        if   m > sum(A): print('no')
        elif solve(m,A): print('yes') 
        else           : print('no')
Buggy python submission (verdict: Runtime Error):
def invNum(A):
    if len(A)==1: return 0
    cnt = 0
    for i in range(1,len(A)):
        if A[0] > A[i]: cnt += 1
    return cnt + invNum(A[1:])


if __name__=='__main__':
    n = int(input()) 
    print(invNum(list(map(int,input().split()))))

Fixed submission:
cnt = 0
def merge(L,R):
    global cnt
    n = len(L)+len(R)
    lenL = len(L)
    A = []
    i = j = 0
    L.append(-1)
    R.append(-1)
    for _ in range(n):
        if L[i] > R[j]:
            A.append(L[i])
            i += 1
        else:
            A.append(R[j])
            j += 1
            cnt += lenL-i
    return A

def mergeSort(A):
    if len(A)==1: return A
    m = len(A)//2
    return merge(mergeSort(A[:m]),mergeSort(A[m:])) 

if __name__=='__main__':
    n = int(input()) 
    A = list(map(int,input().split()))
    mergeSort(A[::-1])
    print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
def invNum(d,A):
    if d < 0 or len(A)==0: return 0
    cnt = 0
    L = []
    R = []
    for ai in A:
        if ai < 2**d:
            L.append(ai)
            cnt += len(R)
        else:
            R.append(ai-2**d)
    return cnt + invNum(d-1,L) + invNum(d-1,R)
 
if __name__=='__main__':
    n = int(input()) 
    print(invNum(29,list(map(int,input().split()))))

Fixed submission:
cnt = 0
def merge(L,R):
    global cnt
    n = len(L)+len(R)
    lenL = len(L)
    A = []
    i = j = 0
    L.append(-1)
    R.append(-1)
    for _ in range(n):
        if L[i] > R[j]:
            A.append(L[i])
            i += 1
        else:
            A.append(R[j])
            j += 1
            cnt += lenL-i
    return A

def mergeSort(A):
    if len(A)==1: return A
    m = len(A)//2
    return merge(mergeSort(A[:m]),mergeSort(A[m:])) 

if __name__=='__main__':
    n = int(input()) 
    A = list(map(int,input().split()))
    mergeSort(A[::-1])
    print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
def invNum(d,A):
    if d < 0 or not A: return 0
    cnt = 0
    L = []
    R = []
    for ai in A:
        if ai < 2**d:
            L.append(ai)
            cnt += len(R)
        else:
            R.append(ai-2**d)
    return cnt + invNum(d-1,L) + invNum(d-1,R)
 
if __name__=='__main__':
    n = int(input()) 
    print(invNum(29,list(map(int,input().split()))))

Fixed submission:
cnt = 0
def merge(L,R):
    global cnt
    n = len(L)+len(R)
    lenL = len(L)
    A = []
    i = j = 0
    L.append(-1)
    R.append(-1)
    for _ in range(n):
        if L[i] > R[j]:
            A.append(L[i])
            i += 1
        else:
            A.append(R[j])
            j += 1
            cnt += lenL-i
    return A

def mergeSort(A):
    if len(A)==1: return A
    m = len(A)//2
    return merge(mergeSort(A[:m]),mergeSort(A[m:])) 

if __name__=='__main__':
    n = int(input()) 
    A = list(map(int,input().split()))
    mergeSort(A[::-1])
    print(cnt)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import namedtuple
Card = namedtuple('Card', 'suit value')

def partition(A,p,r):
    x = A[r]
    i = p
    for j in range(p,r):
        if A[j].value <= x.value:
            A[i],A[j] = A[j],A[i]
            i += 1
    A[i],A[r] = A[r],A[i]
    return i

def quickSort(A,p,r):
    if p < r:
        q = partition(A,p,r)
        quickSort(A,p,q-1)
        quickSort(A,q+1,r)

def checkStable(C0, C1):
    result = True
    num = {}
    for c in C0: num[c.value] = c.value
    for i in num.keys():
        f0 = [x.suit for x in list(filter(lambda c: c.value==i,C0))]
        f1 = [x.suit for x in list(filter(lambda c: c.value==i,C1))]
        if f0!=f1:
            result = False
    return result

if __name__=='__main__':
    n = int(input()) 
    Co = list(map(lambda X: Card(X[0],int(X[1])), [input().split() for _ in range(n)]))
    Cs = Co[:]
    quickSort(Cs,0,n-1)
    print("Stable" if checkStable(Co,Cs) else "Not stable")
    for c in Cs: print(c.suit,c.value)

Fixed submission:
from collections import namedtuple
Card = namedtuple('Card', 'suit value')

def partition(A,p,r):
    x = A[r]
    i = p
    for j in range(p,r):
        if A[j].value <= x.value:
            A[i],A[j] = A[j],A[i]
            i += 1
    A[i],A[r] = A[r],A[i]
    return i

def quickSort(A,p,r):
    if p < r:
        q = partition(A,p,r)
        quickSort(A,p,q-1)
        quickSort(A,q+1,r)

def isStable(Co,Cs):
    C = Cs[:]
    for ci in Co:
        j = C.index(ci)
        if j > 0:
            if Cs[j-1].value == ci.value:
                return False
        del C[j]
    return True

if __name__=='__main__':
    n = int(input()) 
    Co = list(map(lambda X: Card(X[0],int(X[1])), [input().split() for _ in range(n)]))
    Cs = Co[:]
    quickSort(Cs,0,n-1)
    print("Stable" if isStable(Co,Cs) else "Not stable")
    for c in Cs: print(c.suit,c.value)
Buggy python submission (verdict: Time Limit Exceeded):
class my_tree:
    def __init__(self):
        self.t = {}
    def addNode(self, i, c=[]):
        self.t[i] = {'parent':-1, 'type': 'root', 'c':c}
        ti = self.t[i]
        for j in self.t.keys():
            tj = self.t[j]
            if j in ti['c'] :
                tj['parent'] = i
                if tj['c']:
                    tj['type'] = 'internal node'
                else:
                    tj['type'] = 'leaf'
            elif i in tj['c']:
                ti['parent'] = j
                if ti['c']:
                    ti['type'] = 'internal node'
                else:
                    ti['type'] = 'leaf'
    def getDepth(self, i):
        p_id = self.t[i]['parent']
        if p_id==-1: return 0
        return self.getDepth(p_id) + 1
    def getParentId(self, i): return self.t[i]['parent']
    def getNodeType(self, i): return self.t[i]['type']
    def getChildren(self, i): return self.t[i]['c']
    def getIdList(self)     : return sorted(self.t.keys())

if __name__=='__main__':
    n = int(input())
    tree = my_tree()
    for _ in range(n):
        node = list(map(int,input().split()))
        i = node[0]
        c = node[2:] if node[1] > 0 else []
        tree.addNode(i,c)
    
    for i in tree.getIdList():        
        print("node {0}: parent = {1}, depth = {2}, {3}, {4}".format(\
                i, tree.getParentId(i), tree.getDepth(i), tree.getNodeType(i), tree.getChildren(i)))

Fixed submission:
from collections import namedtuple
Node = namedtuple('Node', ['parent', 'children'])

class my_tree:
    def __init__(self, N=[]):
        self.T = {}
        for ni in N: self.addNode(ni)

    def addNode(self, n):
        p = self.T.get(n[0],Node(-1,[None])).parent
        self.T[n[0]] = Node(p,n[2:])
        for c in n[2:]:
            cc = self.T.get(c,Node(-1,[None])).children
            self.T[c] = Node(n[0],cc)

    def getParent(self, i): return self.T[i].parent

    def getChildren(self, i): return self.T[i].children

    def getDepth(self, i):
        if self.T[i].parent == -1: return 0
        return self.getDepth(self.T[i].parent) + 1

    def getNodeType(self, i):
        if self.T[i].parent == -1: return 'root'
        if self.T[i].children    : return 'internal node'
        return 'leaf'

    def getIdList(self): return sorted(self.T.keys())

 
if __name__=='__main__':
    n = int(input())
    N = [None]*n
    for i in range(n): N[i] = list(map(int,input().split()))
    tree = my_tree(N)
     
    for i in tree.getIdList():        
        print("node {0}: parent = {1}, depth = {2}, {3}, {4}".format(\
                i, tree.getParent(i), tree.getDepth(i), tree.getNodeType(i), tree.getChildren(i)))
Buggy python submission (verdict: Runtime Error):
import math
x1,y1,x2,y2 = map(int,input().split())
print("{0:.8f}".format(math.sqrt((x1-x2)**2+(y1-y2)**2)))

Fixed submission:
import math
x1,y1,x2,y2 = map(float,input().split())
print("{0:.8f}".format(math.sqrt((x1-x2)**2+(y1-y2)**2)))
Buggy python submission (verdict: Runtime Error):
class dice:
    def __init__(self,label):
        self.d = label
    def roll(self,op):
        d = self.d
        if   op=='N': self.d = [d[1],d[5],d[2],d[3],d[0],d[4]]
        elif op=='E': self.d = [d[3],d[1],d[0],d[5],d[4],d[2]]
        elif op=='W': self.d = [d[2],d[1],d[5],d[0],d[4],d[3]]
        elif op=='S': self.d = [d[4],d[0],d[2],d[3],d[5],d[1]]
    def print(self):
        print(self.d[0])
    def print_r_side(self,t,f):
        d = self.d
        if   (t,f)==(d[0],d[1]): print(d[2])
        elif (t,f)==(d[0],d[2]): print(d[4])
        elif (t,f)==(d[0],d[3]): print(d[1])
        elif (t,f)==(d[0],d[4]): print(d[3])
        elif (t,f)==(d[1],d[0]): print(d[3])
        elif (t,f)==(d[1],d[2]): print(d[0])
        elif (t,f)==(d[1],d[3]): print(d[5])
        elif (t,f)==(d[1],d[5]): print(d[2])
        elif (t,f)==(d[2],d[0]): print(d[1])
        elif (t,f)==(d[2],d[1]): print(d[5])
        elif (t,f)==(d[2],d[4]): print(d[0])
        elif (t,f)==(d[2],d[5]): print(d[4])
        elif (t,f)==(d[3],d[0]): print(d[4])
        elif (t,f)==(d[3],d[1]): print(d[0])
        elif (t,f)==(d[3],d[4]): print(d[5])
        elif (t,f)==(d[3],d[5]): print(d[1])
        elif (t,f)==(d[4],d[0]): print(d[2])
        elif (t,f)==(d[4],d[2]): print(d[5])
        elif (t,f)==(d[4],d[3]): print(d[0])
        elif (t,f)==(d[4],d[5]): print(d[3])
        elif (t,f)==(d[5],d[1]): print(d[3])
        elif (t,f)==(d[5],d[2]): print(d[1])
        elif (t,f)==(d[5],d[3]): print(d[4])
        elif (t,f)==(d[5],d[4]): print(d[2])

label = list(map(int,input().split()))
q = int(input())
d0 = dice(label)
for _ in range(q):
    t,f = list(map(int,input().split()))
    d0.print_r_side(t,f)

Fixed submission:
class dice:
    def __init__(self,label):
        self.top, self.front, self.right, self.left, self.rear, self.bottom \
                = label
    def roll(self,op):
        if   op=='N': self.top, self.front, self.bottom, self.rear = \
                                self.front, self.bottom, self.rear , self.top
        elif op=='E': self.top, self.left , self.bottom, self.right = \
                                self.left , self.bottom, self.right, self.top
        elif op=='W': self.top, self.right, self.bottom, self.left = \
                                self.right, self.bottom, self.left , self.top
        elif op=='S': self.top, self.rear , self.bottom, self.front = \
                                self.rear , self.bottom, self.front, self.top
    def print_top(self): print(self.top)
 
label = list(map(int,input().split()))
op_list = list(input())
d = dice(label)
for op in op_list:
    d.roll(op)
d.print_top()