Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

s = input()
n = len(s)
S=[0 for i in range(n)]
for i in range(n):
    if s[i]=="R":
        S[i]=1


ANS =[0 for i in range(n)]
tame =[0,0]
k=0
for i in range(n):
    if S[i] == 1:
        tame[k]+=1
        k = (k+1)%2
    elif tame[0]>0:
        ANS[i-1] += tame[0]
        ANS[i] += tame[1]
        tame=[0,0]
        k=0
tame =[0,0]
k=0
for i in range(n-1,-1,-1):
    if S[i] == 0:
        tame[k]+=1
        k = (k+1)%2
    elif tame[0]>0:
        ANS[i+1] += tame[0]
        ANS[i] += tame[1]
        tame=[0,0]
        k=0
#print(ANS)
ans=""
for i in range(n):
    ans += str(ANS[i])
    ans += " "
print(ans)
#print(n)

### FIXED CODE ###

s = input()
n = len(s)
S=[0 for i in range(n)]
for i in range(n):
    if s[i]=="R":
        S[i]=1


ANS =[0 for i in range(n)]
tame =[0,0]
k=0
for i in range(n):
    if S[i] == 1:
        tame[k]+=1
        k = (k+1)%2
    elif tame[0]>0:
        ANS[i-1] += tame[0]
        ANS[i] += tame[1]
        tame=[0,0]
        k=0
tame =[0,0]
k=0
for i in range(n-1,-1,-1):
    if S[i] == 0:
        tame[k]+=1
        k = (k+1)%2
    elif tame[0]>0:
        ANS[i+1] += tame[0]
        ANS[i] += tame[1]
        tame=[0,0]
        k=0
#print(ANS)
ans=""
for i in range(n):
    ans += str(ANS[i])
    ans += " "
print(ans)
#print(n)
### BUGGY CODE ###

X = int(input())
List = [(A,B) for A in range(X//2+1) for B in range(-X//2,X//2) if A**5 - B**5 == X]

print(*List[0])

### FIXED CODE ###

X = int(input())
List = [(A,B) for A in range(-120,120) for B in range(-120,120) if A**5 - B**5 == X]

print(*List[0])
### BUGGY CODE ###

import sys
from math import factorial


def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod



mod = 10**9 + 7
x, y = map(int, sys.stdin.readline().split())

if (x + y)%3 != 0:
    print(0)
    sys.exit()
if y < x/2 and y > 2*x:
    print(0)
    sys.exit()

count = 0
while(True):
    if y == 1/2*x:
        break
    else:
        x -= 1
        y -= 2
        count += 1
count2 = y
sum = count + count2

mod = 10**9+7 #出力の制限
N = 10**4
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, sum + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

a = cmb(sum,count,mod)

print(a)


### FIXED CODE ###

import sys
from math import factorial


def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9 + 7
x, y = map(int, sys.stdin.readline().split())

if (x + y)%3 != 0:
    print(0)
    sys.exit()
if y < x/2 and y > 2*x:
    print(0)
    sys.exit()

count1 = int((y-2*x)/(-3))
count2 = int((x-2*y)/(-3))

sum = count1 + count2

N = 10**4
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, sum + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

a = cmb(sum,count1,mod)

print(a)
### BUGGY CODE ###

n,q = map(int,input().split())
s = input()
for _ in range(q):
    l,r = map(int,input().split())
    print (s[l-1:r].count("AC"))


### FIXED CODE ###

n,q = map(int,input().split())
s = input()

t = [0] * (n+1)
for i in range(n):
    t[i+1] = t[i] + (1 if s[i:i+2] == 'AC' else 0)

#print (t)

for _ in range(q):
    l,r = map(int,input().split())
    print (t[r-1]-t[l-1])
### BUGGY CODE ###

N=int(input())
c=list(input())
ans=0

if "R" in c and "W" in c:
    while True:
        c1=c[::-1]
        w_index=int(c.index("W"))
        r_index=int(N-1-c1.index("R"))
        if w_index-r_index==1:
             break
        ans+=1
        c[w_index]="R"
        c[r_index]="W"

print(ans)

### FIXED CODE ###

N=int(input())
c=list(input())
c1=c[::-1]
ans=0

w_index=[i for i, x in enumerate(c) if x=="W"]
r_index=[i for i, x in enumerate(c1) if x=="R"]

for i in range(len(r_index)):
    r_index[i]=N-1-r_index[i]


if "R" in c and "W" in c:
    for i in range(min(len(r_index),len(w_index))):
        w=w_index[ans]
        r=r_index[ans]
        if w-r>0:
             break
        ans+=1

print(ans)
### BUGGY CODE ###

H,N=map(int,input().split())
AB=[]
for i in range(N):
  AB.append(list(map(int,input().split())))
dp=[0]*(10**8)
dp[0]=0
i=0

while True:
  i+=1
  for A,B in AB:
    if i-B>=0:
      dp[i]=max(dp[i-B]+A,dp[i])
  if dp[i]>=H:
    break
print(i)


### FIXED CODE ###

H,N=map(int,input().split())
AB=[]
for i in range(N):
  AB.append(list(map(int,input().split())))
dp=[10**8]*(H+1)
dp[0]=0
i=0

for i in range(H+1):
  for A,B in AB:
    if i-A>=0:
      dp[i]=min(dp[i-A]+B,dp[i])
  for A,B in AB:
    if i+A>H:
      dp[-1]=min(dp[-1],dp[i]+B)
print(dp[-1])
### BUGGY CODE ###

MOD = 10 ** 9 + 7

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

res = 0

for k in range(60):
    cnt = [0, 0]
    for i in range(N):
        cnt[(A[i] >> k) & 1] += 1
    res = (res + cnt[0] * cnt[1] * pow(2, k)) % MOD

print(res)


### FIXED CODE ###

MOD = 10 ** 9 + 7

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

res = 0

for k in range(60):
    cnt = [0, 0]
    for i in range(N):
        cnt[(A[i] >> k) & 1] += 1
    res = (res + cnt[0] * cnt[1] * pow(2, k)) % MOD

print(res)
### BUGGY CODE ###

import sys,collections as cl,bisect as bs , heapq as hq
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

def search(a,n):
    se = dict()
    data = []
    for i in range(0,len(a)-n+1):
        kkk = a[i:i+n]
        kkk = "".join(kkk)
        if In(kkk,data):
            for j in range(len(se[kkk])):
                if se[kkk][j] <= i-n:
                    return True
            else:
                se[kkk].append(i)
        else:
            bs.insort(data,kkk)
            se[kkk] = [i]
    return False








n = onem()

s = list(input())
l = 0
r = n//2
for i in range(n):
    mid = -(-(l+r)//2)
    ans = search(s,mid)
    if l == mid:
        break
    if ans:
        l = mid
    else:
        r = mid-1
print(l)









### FIXED CODE ###

import sys,collections as cl,bisect as bs,heapq as hq
input = sys.stdin.readline
sys.setrecursionlimit(100000)
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

def search(a,n):
    se = dict()
    data = []
    for i in range(0,len(a)-n+1):
        kkk = a[i:i+n]
        kkk = "".join(kkk)
        if In(kkk,data):
            if se[kkk] <= i-n:
                    return True
        else:
            bs.insort(data,kkk)
            se[kkk] = i
    return False
def solver():
    n = onem()

    s = list(input())
    l = 0
    r = n//2
    for i in range(n):
        mid = -(-(l+r)//2)
        ans = search(s,mid)
        if l == mid:
            break
        if ans:
            l = mid
        else:
            r = mid-1
    print(l)



if __name__ == '__main__':
    solver()
### BUGGY CODE ###

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(1000000000)

from collections import Counter

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

dp_memo = [[[0]*(N+1) for _ in range(N+1)] for _ in range(N+1)]

def dp(i,j,k):
    if i==0 and j==0 and k==0:
        return 0
    elif i<0 or j<0 or k<0:
        return 0
    elif dp_memo[i][j][k] > 0:
        return dp_memo[i][j][k]
    else:
        tmp = (N/(i+j+k)) * (dp(i,j+1,k-1)*(k/N) + dp(i+1,j-1,k)*(j/N) + dp(i-1,j,k)*(i/N) + 1)
        dp_memo[i][j][k] = tmp
        return tmp

c = Counter(a)
print(dp(c[1], c[2], c[3]))

### FIXED CODE ###

def main():
    from collections import Counter

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

    dp = [[[0]*(N+1) for _ in range(N+1)] for _ in range(N+1)]
    c = Counter(a)

    # メモ化再帰ではpypy3でもTLEするのでforループを使う
    # j==k==0の場合を計算 → k==0の場合を計算 → 一般の場合を計算
    for k in range(c[3]+1):
        for j in range(c[3]+c[2]-k+1):
            for i in range(N-j-k+1):
                if i+j+k == 0:
                    continue
                else:
                    tmp = N
                    if i > 0: # IndexError防止
                        tmp += dp[i-1][j][k]*i
                    if j > 0:
                        tmp += dp[i+1][j-1][k]*j
                    if k > 0:
                        tmp += dp[i][j+1][k-1]*k
                    dp[i][j][k] = tmp/(i+j+k) # 割り算をまとめないとTLE

    print(dp[c[1]][c[2]][c[3]])

if __name__ == '__main__':
    main()
### BUGGY CODE ###

import sys
input = sys.stdin.readline
import functools

def multiplication2(n,a):
    acc = functools.reduce(lambda x,y:x*y, a)
    if 10**18 < acc:
        acc = -1

    return acc

if __name__ == "__main__":
    n = int(input())
    a = list(map(int,input().split()))
    print(multiplication2(n,a))


### FIXED CODE ###

import sys
input = sys.stdin.readline
import functools

def multiplication2(n,a):
    #acc = functools.reduce(lambda x,y:x*y, a)
    acc = 1
    a_sorted = sorted(a)
    if a_sorted[0] == 0:
        return 0
    
    for s in a_sorted:

        acc = acc*s
        if 10**18 < acc:
            acc = -1
            break
    return acc

if __name__ == "__main__":
    n = int(input())
    a = list(map(int,input().split()))
    print(multiplication2(n,a))
### BUGGY CODE ###

import itertools

while 1:
    n=int(input())
    if n==0:break
    l=[int(input()) for _ in range(n)]
    print(sum([sum(sorted(l)[:len(l)-1][:i]) for i in range(1,len(l))]))



### FIXED CODE ###

import itertools

while 1:
    n=int(input())
    if n==0:break
    l=[int(input()) for _ in range(n)]
    ll=len(l)
    m=sorted(l)[:ll-1]
    print(sum([sum(m[:i]) for i in range(1,ll)]))
### BUGGY CODE ###

if __name__=="__main__":
    count = 0
    idx = int(input())
    n = list(map(int,input().split(" ")))
    while True:
        for i in range(idx):
            if n[i]%2==1:
                print(count)
                break
        count+=1

### FIXED CODE ###

if __name__=="__main__":
    count = 0
    idx = int(input())
    n = list(map(int,input().split(" ")))
    while True:
        for i in range(idx):
            if n[i]|1==n[i]:
                print(count)
                exit()
            else:
                n[i]=n[i]>>1
        count+=1
### BUGGY CODE ###

N = int(input())
S = input()
ans = N
for i in range(N):
    cnt = S[:i].count("W")+S[i+1:].count("E")
    ans = min(ans, cnt)
print(ans)

### FIXED CODE ###

N = int(input())
S = input()
e = S.count("E")
cnt = e
for i in S:
    if i == "E":
        cnt -= 1
    else:
        cnt += 1
    e = min(e, cnt)
print(e)
### BUGGY CODE ###

n = int(input())
a = [int(i) for i in input().split()]
prd = 1
for i in range(n):
  prd*=a[i]
if prd > 10**18:
  print(-1)
else:
  print(prd)

### FIXED CODE ###

import sys

n = int(input())
a = [int(i) for i in input().split()]
prd = 1
if 0 in a:
  print(0)
  sys.exit()
for i in range(n):
  prd*=a[i]
  if prd > 10**18:
    print(-1)
    sys.exit()
print(prd)
### BUGGY CODE ###

N = int(input())
A = list(map(int,input().split()))
ans = 0
for i in range(N - 1):
    for j in range(N - i - 1):
        ans += A[i] * A[i + j + 1]
print(ans % (pow(10,9) + 7))

### FIXED CODE ###

N = int(input())
A = list(map(int,input().split()))
s = [0]
for i in range(N):
    s.append(s[i] + A[i])
ans = 0
for i in range(N):
    ans += A[i] * (s[N] - s[i + 1])
print(ans % (10 ** 9 + 7))
### BUGGY CODE ###

import copy
n= int(input())
a = [int(input()) for i in range(n)]

for i in range(n):
    a_n = copy.copy(a)
    a_n.pop(i)
    print(max(a_n))

### FIXED CODE ###


n= int(input())
a = [int(input()) for i in range(n)]
a_s = sorted(a,reverse=True)

for i in range(n):
    ans = a_s[0]
    if a[i]==a_s[0]:
        ans = a_s[1]
    
    print(ans)
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
for i in range(1,n+1):
    print(a.count(i))

### FIXED CODE ###

n = int(input())
a = list(map(int,input().split()))
ans = [0]*(n)
for i in a:
    ans[i-1] += 1
for j in ans:
    print(j)
### BUGGY CODE ###

n = int(input())
ans = []
for _ in range(n):
    tmp = input()
    if tmp not in ans:
        ans.append(tmp)
print(len(ans))

### FIXED CODE ###

n = int(input())
from collections import defaultdict
dic = defaultdict(int)
for _ in range(n):
    tmp = input()
    dic[tmp] += 1
print(len(dic))
### BUGGY CODE ###

from collections import deque
n = int(input())
p = list(map(int, input().split()))
for i in range(len(p)):
    p[i] -= 1

to_outsides = [[0 for _ in range(n)] for _ in range(n)]
not_exist = [["■" for _ in range(n)] for _ in range(n)]


for i in range(n):
    for j in range(n):
        to_outside = [i,n-1-i,j,n-1-j]
        to_outsides[i][j] = min(to_outside)

directions = [[-1,0],[1,0],[0,-1],[0,1]]


all_loss = 0
answer = 0
for i, person in enumerate(p):
    
    exited = [person//n, person%n]
    answer += to_outsides[exited[0]][exited[1]]
    not_exist[exited[0]][exited[1]] = "□"
    seen = [[False for _ in range(n)] for _ in range(n)]
    seen[exited[0]][exited[1]]=True
    todo = deque([exited])
    renews = []
    while len(todo)>0:
        now = todo.pop()
        renews.append(now)
        for direction in directions:
            try:
                checking = [now[0]+direction[0],now[1]+direction[1]]
                to_outsides[checking[0]][checking[1]]
            except:
                continue
            if seen[checking[0]][checking[1]] == False and\
            ((not_exist[checking[0]][checking[1]] == "□" and  to_outsides[checking[0]][checking[1]]==to_outsides[now[0]][now[1]])or\
            to_outsides[checking[0]][checking[1]]>to_outsides[now[0]][now[1]]):
                todo.append([checking[0],checking[1]])
                seen[checking[0]][checking[1]] = True
    to_outsides_pre = to_outsides.copy()
    for renew in renews:
        if to_outsides[renew[0]][renew[1]] != 0:
            to_outsides[renew[0]][renew[1]]-=1
print(answer)

### FIXED CODE ###

N = int(input())
P = list(map(int, input().split()))

dp = [[min(i, N - i - 1, j, N - j - 1) for j in range(N)] for i in range(N)]

presence = [[1 for j in range(N)] for i in range(N)]

ans = 0
for i in range(len(P)):
    P[i] -= 1
    x, y = divmod(P[i], N)
    ans += dp[x][y]
    presence[x][y] = 0

    Q = [(x, y)]
    #%%
    while Q:
        x, y = Q.pop()
        new_value = dp[x][y] + presence[x][y]
        if 1 <= x and dp[x - 1][y] > new_value:
            dp[x - 1][y] = new_value
            Q.append((x - 1, y))
        if x < N - 1 and dp[x + 1][y] > new_value:
            dp[x + 1][y] = new_value
            Q.append((x + 1, y))
        if 1 <= y and dp[x][y - 1] > new_value:
            dp[x][y - 1] = new_value
            Q.append((x, y - 1))
        if y < N - 1 and dp[x][y + 1] > new_value:
            dp[x][y + 1] = new_value
            Q.append((x, y + 1))

print(ans)
### BUGGY CODE ###

import sys
input = sys.stdin.readline


def read():
    N = int(input().strip())
    S = input().strip()
    return N, S


def solve(N, S):
    ans = 0
    l0 = 0
    r0 = 1
    while l0 < N:
        for l1 in range(l0+1, N):
            # find and compare
            if S[l0] == S[l1]:
                k = 1
                while l0 + k < l1 and l1+k < N and S[l0+k] == S[l1+k]:
                    k += 1
                ans = max(ans, k)
                l1 += k
        l0 += 1
    return ans

if __name__ == "__main__":
    inputs = read()
    print("%s" % solve(*inputs))


### FIXED CODE ###

import random
from collections import defaultdict


class RollingHash():

    def __init__(self, S, b=3491, m=999999937):
        """任意の基数と法でハッシュを生成する"""
        n = len(S)
        self.prefix = prefix = [0] * (n+1)
        self.power = power = [1] * (n+1)
        self.b = b
        self.m = m
        for i in range(n):
            c = ord(S[i])
            prefix[i+1] = (prefix[i] * b + c) % m
            power[i+1] = (power[i] * b) % m
    
    def get(self, l, r):
        """S[l, r) のハッシュを求める"""
        return (self.prefix[r] - self.power[r-l] * self.prefix[l]) % self.m
    
    def concat(self, h1, h2, l2):
        """S1+S2 のハッシュを、それぞれのハッシュから求める"""
        return (self.power[l2] * h1 + h2) % self.m
    
    def lcp(self, l1, r1, l2, r2):
        """S[l1, r1) とS[l2, r2) の最大共通接頭辞を求める"""
        low = 0
        high = min(r1-l1, r2-l2) + 1
        while high - low > 1:
            mid = (high + low) // 2
            if self.get(l1, l1 + mid) == self.get(l2, l2 + mid):
                low = mid
            else:
                high = mid
        return low


def read():
    N = int(input().strip())
    S = input().strip()
    return N, S


def solve(N, S):
    ans = []
    for m in [999999937, 10**9+7]:
        b = random.randint(10000, m-1)
        rh = RollingHash(S, b=b, m=m)
        low = 0
        high = (N+1) // 2 + 1
        dmax = defaultdict(int)
        dmin = defaultdict(int)
        while high - low > 1:
            dmax.clear()
            dmin.clear()
            mid = (high + low) // 2
            for i in range(0, N-mid+1):
                h = rh.get(i, i+mid)
                dmax[h] = max(dmax[h], i+1)
                dmin[h] = max(dmin[h], N+1-i)
            is_match = False
            for h in dmax.keys():
                if dmax[h] - (N+2 - dmin[h]) >= mid:
                    is_match = True
                    break
            if is_match:
                low = mid
            else:
                high = mid
        ans.append(low)
    return min(ans)


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))
### BUGGY CODE ###

import math
from collections import defaultdict


class UnionFindTree:
    def __init__(self):
        self.p = {}

    def make(self, x):
        self.p[x] = x

    def find(self, x):
        if x in self.p:
            if self.p[x] != x:
                self.p[x] = self.find(self.p[x])
            return self.p[x]
        return None

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def unite(self, x, y):
        return self._unite(self.find(x), self.find(y))

    def _unite(self, x, y):
        if x >= y:
            self.p[x] = y
            return y
        else:
            self.p[y] = x
            return x


def ncr(n, r):
    if n < r:
        return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


N, M = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(M)]
ab.reverse()

utf = UnionFindTree()
for n in range(1, N + 1):
    utf.make(n)

h = {}
ans = []
total = ncr(N, 2)
now = 0
for i in range(M):
    ans.append(total)

    a, b = ab[i]
    x, y = utf.find(a), utf.find(b)
    a = utf.unite(x, y)
    if x != y:
        n = h.get(x, 1) + h.get(y, 1)
        total += ncr(h.get(x, 1), 2) + ncr(h.get(y, 1), 2)
        total -= ncr(n, 2)

        if x in h:
            del h[x]
        if y in h:
            del h[y]
        h[a] = n

for v in reversed(ans):
    print(v)

### FIXED CODE ###

from collections import defaultdict
from collections import deque


class UnionFindTree:
    def __init__(self):
        self.p = {}
        self.rank = {}

    def make(self, x):
        self.p[x] = x
        self.rank[x] = 0

    def find(self, x):
        if x in self.p:
            if self.p[x] != x:
                # 経路圧縮
                self.p[x] = self.find(self.p[x])
            return self.p[x]
        return None

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def unite(self, x, y):
        return self._unite(self.find(x), self.find(y))

    def _unite(self, x, y):
        if self.rank[x] < self.rank[y]:
            self.p[x] = y
            return y
        else:
            self.p[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            return x


def nc2(n):
    return (n * (n - 1)) >> 1


N, M = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(M)]

utf = UnionFindTree()
for n in range(1, N + 1):
    utf.make(n)

h = {}
ans = deque([])
total = nc2(N)
now = 0
for i in range(M):
    ans.appendleft(total)

    a, b = ab[M - i - 1]
    x, y = utf.find(a), utf.find(b)
    a = utf.unite(x, y)
    if x != y:
        nx, ny = h.get(x, 1), h.get(y, 1)
        n = nx + ny
        total += nc2(nx) + nc2(ny)
        total -= nc2(n)

        h[x] = 1
        h[y] = 1
        h[a] = n

for v in ans:
    print(v)
### BUGGY CODE ###

N = int(raw_input())
G = [[0]*N for i in range(N)]
for i in range(N-1):
  a, b, c = map(int, raw_input().split())
  G[a-1][b-1] = G[b-1][a-1] = c
Q, K = map(int, raw_input().split())
K -= 1

d = [0]*N
p = [K]
while len(p)>0:
  v = p.pop()
  for n in range(N):
    if n!=K and d[n]==0 and G[v][n]>0:
      d[n] = d[v]+G[v][n]
      p.append(n)

for i in range(Q):
  x, y = map(int, raw_input().split())
  print d[x-1]+d[y-1]

### FIXED CODE ###

N = int(raw_input())
G = {}
for i in range(N):
  G[i] = {}
for i in range(N-1):
  a, b, c = map(int, raw_input().split())
  G[a-1][b-1] = G[b-1][a-1] = c
Q, K = map(int, raw_input().split())
K -= 1

d = [0]*N
p = [K]
while len(p)>0:
  v = p.pop()
  for n in G[v].keys():
    if n!=K and d[n]==0:
      d[n] = d[v]+G[v][n]
      p.append(n)

for i in range(Q):
  x, y = map(int, raw_input().split())
  print d[x-1]+d[y-1]
### BUGGY CODE ###

import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(10000)

# Debug output


def chkprint(*args):
    names = {id(v): k for k, v in inspect.currentframe().f_back.f_locals.items()}
    print(', '.join(names.get(id(arg), '???')+' = '+repr(arg) for arg in args))

# Binary converter


def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in input().split()]

# --------------------------------------------


dp = None


def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


def main():
    N = int(input())
    A = li_input()

    A.sort(reverse=True)
    ans_c = -1

    for i in range(100):
        n = A[i]

        if n == min(A):
            break

        half = n / 2
        diff = 10 ** 100

        A_ = A[:]
        A_.remove(n)

        for a in A_:
            if abs(a - half) < diff:
                r = a
                diff = abs(a - half)

        tmp_c = combinations_count(n, r)
        if tmp_c > ans_c:
            ans_c = tmp_c
            ans_n = n
            ans_r = r

    print(ans_n, ans_r)


main()


### FIXED CODE ###

import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(10000)

# Debug output


def chkprint(*args):
    names = {id(v): k for k, v in inspect.currentframe().f_back.f_locals.items()}
    print(', '.join(names.get(id(arg), '???')+' = '+repr(arg) for arg in args))

# Binary converter


def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in input().split()]

# --------------------------------------------


dp = None


def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


def main():
    N = int(input())
    A = li_input()

    A.sort(reverse=True)
    ans_c = -1

    n = A[0]

    half = n / 2
    diff = 10 ** 100

    A.remove(n)

    for a in A:
        if abs(a - half) < diff:
            r = a
            diff = abs(a - half)

    print(n, r)


main()