Harryxun/stf_run huggingface dataset:
import bisect
import time

start = time.time()

D = int(input())
c = [0] + list(map(int, input().split()))
s = [0] + [[0] + list(map(int, input().split())) for _ in range(D)]
sch, held = [0], [[0] for _ in range(27)]

for d in range(1, D+1):
    SLvar = [s[d][i] + c[i] * (d - held[i][-1]) for i in range(1, 27)]
    t = SLvar.index(max(SLvar)) + 1
    sch.append(t)
    held[t].append(d)

for i in range(1, 27):
    held[i].append(D+1)

s_sort = [0] + [[0] + sorted([[s[d][i], i] for i in range(1, 27)], key=lambda x: x[0], reverse=True) for d in range(1, D+1)]

i, j = 1, 1
while time.time() - start < 1.8 and i <= D and j < 27:
    d, q = i, s_sort[i][j][1]

    if sch[d] != q:
        SLvar = s[d][q] - s[d][sch[d]]

        ind = bisect.bisect_left(held[sch[d]], d)
        k = d - held[sch[d]][ind-1]
        l = held[sch[d]][ind+1] - d
        SLvar -= c[sch[d]] * k * l

        ind = bisect.bisect_left(held[q], d)
        k = d - held[q][ind-1]
        l = held[q][ind] - d
        SLvar += c[q] * k * l

        if SLvar > 0:
            del held[sch[d]][ind]
            held[q].insert(ind, d)
            sch[d] = q

    i += 1
    if i > D:
        i = 1
        j += 1

for i in range(1, D+1):
    print(sch[i])

### FIXED CODE ###

D = int(input())
c = [0] + list(map(int, input().split()))
s = [0] + [[0] + list(map(int, input().split())) for _ in range(D)]
last = [0]*27

for d in range(1, D+1):
    SLvar = [s[d][i] + c[i] * (d - last[i]) for i in range(1, 27)]

    t = SLvar.index(max(SLvar)) + 1
    print(t)
    last[t] = d
input_from = input()
strings = input_from.split(' ')
print(f'{strings[1]}{strings[0]}')

### FIXED CODE ###

input_from = input()
strings = input_from.split(' ')
print("{0}{1}".format(strings[1], strings[0]))
from collections import defaultdict

n, m = [int(i) for i in raw_input().split()]

parents = defaultdict(list)

i = 0
while i < m:
    u, v = [int(x) for x in raw_input().split()]
    parents[v-1].append(u-1)
    i += 1

def calc_f(parents, f, i):
    if f[i] != -1:
        return f[i]

    f[i] = float("-inf")
    for v in parents[i]:
        f[i] = max(f[i], calc_f(parents, f, v)+1)

    return f[i]

f = [-1] * n
j = 0
while j < n:
    calc_f(parents, f, j)
    j += 1

print max(f)
s

### FIXED CODE ###

from collections import defaultdict
import sys
sys.setrecursionlimit(1000000)

n, m = [int(i) for i in raw_input().split()]
parents = defaultdict(list)

i = 0
while i < m:
    u, v = [int(x) for x in raw_input().split()]
    parents[v-1].append(u-1)
    i += 1

def calc_f(parents, f, i):
    if i < 0 or i >= len(f):
        return 0

    if f[i] != -1:
        return f[i]

    f[i] = 0
    for v in parents[i]:
        f[i] = max(f[i], calc_f(parents, f, v)+1)

    return f[i]

f = [-1] * n
j = 0
while j < n:
    calc_f(parents, f, j)
    j += 1

if len(f) == 0:
    print 0
else:
    print max(f)
import sys
import math
from collections import deque
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def S(): return sys.stdin.readline()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h,w): return [[i for j in range(w)] for k in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**6+10)
input = sys.stdin.readline

N = I()
l = LI()
array = [i for i in range(N)]
happy = 0
while True:
    for i in range(N-1):
        left = array[i]
        right = array[i+1]
        left_p = 1 if i-left>=0 else -1
        right_p = 1 if i+1-right<=0 else -1
        if left_p*l[left]+right_p*l[right]>0:
            happy += left_p*l[left]+right_p*l[right]
            array[i],array[i+1] = array[i+1],array[i]
            break
    else:
        break
print(happy)


### FIXED CODE ###

import sys
import math
from collections import deque
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def S(): return sys.stdin.readline()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h,w): return [[i for j in range(w)] for k in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**6+10)
input = sys.stdin.readline

R = I()
print(2*math.pi*R)
x,n=map(int,input().split())
Ps=list(map(int,input().split()))
numlist=[]
for i in range(101):
    numlist.append(i)
for p in Ps:
    numlist.remove(p)
import bisect
a=bisect.bisect_left(numlist,x)
b=a-1
if abs(numlist[a]-x)>=abs(numlist[b]-x):
    print(numlist[b])

else:
    print(numlist[a])

### FIXED CODE ###

x,n=map(int,input().split())
Ps=list(map(int,input().split()))
numlist=[]
for i in range(102):
    numlist.append(i)
for p in Ps:
    numlist.remove(p)
import bisect
a=bisect.bisect_left(numlist,x)
b=a-1
if abs(numlist[a]-x)>=abs(numlist[b]-x):
    print(numlist[b])
else:
    print(numlist[a])
input()
st=input().split()
a=""
for s,t in zip(st[0],st[1]):
 a+=(s+t)
print(a)

### FIXED CODE ###

input()
st=raw_input().split()
a=""
for s,t in zip(st[0],st[1]):
 a+=(s+t)
print(a)
x, y, a, b, c = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
p.sort()
q.sort()
r.sort()
red = []
green = []
less = []
for i in range(x):
    red.append(p[-i-1])
for i in range(y):
    green.append(q[-i-1])
# print(red, green)
for i in range(c):
    if red[-1] < green[-1]:
        if red[-1] <= r[-i-1]:
            del red[-1]
            less.append(r[-i-1])
    else:
        if green[-1] <= r[-i-1]:
            del green[-1]
            less.append(r[-i-1])
ans = sum(red) + sum(green) + sum(less)
# print(red, green, less)
print(ans)

### FIXED CODE ###

x, y, a, b, c = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
p.sort()
q.sort()
r.sort()
red = []
green = []
less = []
for i in range(x):
    red.append(p[-i-1])
for i in range(y):
    green.append(q[-i-1])
# print(red, green)
for i in range(c):
    if len(red) > 0 and len(green) > 0:
        if red[-1] < green[-1]:
            if red[-1] < r[-i-1]:
                del red[-1]
                less.append(r[-i-1])
        else:
            if green[-1] <= r[-i-1]:
                del green[-1]
                less.append(r[-i-1])
    elif len(red) > 0:
        if red[-1] < r[-i-1]:
            del red[-1]
            less.append(r[-i-1])
    elif len(green) > 0:
        if green[-1] <= r[-i-1]:
            del green[-1]
            less.append(r[-i-1])
ans = sum(red) + sum(green) + sum(less)
# print(red, green, less)
print(ans)
AB = list(map(float, input().split()))
A = int(AB[0])
B = AB[1] * 100

result = int((A * B) / 100)
print(results)

### FIXED CODE ###

from decimal import Decimal


AB = list(map(Decimal, input().split()))

result = int((AB[0] * AB[1]))
print(result)
for number in b:
    for i in range(3):
        if number in a[i]:
            a[i][a[i].index(number)] = 0
if a[0][0] == a[1][1] == a[2][2] == 0:
    print('Yes')
else:
    for i in range(3):
        if a[i][0] == a[i][1] == a[i][2] == 0:
            print('Yes')
            break
        elif a[0][i] == a[1][i] == a[2][i] == 0:
            print('Yes')
            break
    else:
        print('No')

### FIXED CODE ###

a = [list(map(int,input().split())) for i in range(3)]
n = int(input())
b = [int(input()) for i in range(n)]
for number in b:
    for i in range(3):
        if number in a[i]:
            a[i][a[i].index(number)] = 0
if a[0][0] == a[1][1] == a[2][2] == 0 or a[2][0] == a[1][1] == a[0][2] == 0:
    print('Yes')
else:
    for i in range(3):
        if a[i][0] == a[i][1] == a[i][2] == 0:
            print('Yes')
            break
        elif a[0][i] == a[1][i] == a[2][i] == 0:
            print('Yes')
            break
    else:
        print('No')
import numpy as np
n,k = map(int,input().split())

a = np.array(input().split(),np.int)

F = np.array(input().split(),np.int)

A.sort()
F.sort()

F_ = F[::-1]

left = -1
right = (A * F_).sum()

while (right - left) > 1:
    s = (left + right) // 2
    q = np.maximum(0, A - s // F_)
    r = (q.sum() <= k)
    if r:
        right = s 
    else:
        left = s 

print(right)



### FIXED CODE ###

import numpy as np
n,k = map(int,input().split())

A = np.array(input().split(),np.int)

F = np.array(input().split(),np.int)


A.sort()
F.sort()

F_ = F[::-1]

left = -1
right = (A * F_).sum()

while (right - left) > 1:
    s = (left + right) // 2
    r = np.maximum(0, A - s // F_).sum() <= k
    if r:
        right = s 
    else:
        left = s 

print(right)
N,W=map(int,input().split())
weight=[0]*N
values=[0]*N
for i in range(N):
  weight[i],values[i]=map(int,input().split())

dp=[[0 for i in range(N+100)] for j in range(W+100)]

for i in range(N):
  for w in range(W+1):
    if w>=weight[i]:
      dp[i+1][w]=max(dp[i][w],dp[i][w-weight[i]]+values[i])
    else:
      dp[i+1][w]=dp[i][w]
      
print(dp[N][W])

### FIXED CODE ###

def calc():
  N,W=map(int,input().split())
  dp=[-1]*(W+1)
  dp[0]=0
  for i in range(N):
    w,v=map(int,input().split())
    for j in range(W-w,-1,-1):
      if dp[j]!=-1:
        if dp[j+w]<dp[j]+v:
          dp[j+w]=dp[j]+v
  print(max(dp))
  
calc()
import sys
msg = str(input())

# 逆から、大きい文字数から、見ていけば重複は生じない
len_l1 = 0
while len(msg) != len_l1:
    len_l1 = len(msg)
    print(msg)
    
    # dreamer
    if msg[-7:] == "dreamer":
        msg = msg[:-7]
    # eraser
    if msg[-6:] == "eraser":
        msg = msg[:-6]
        
    # dreame
    if msg[-5:] == "dream":
        msg = msg[:-5]
    # erase
    if msg[-5:] == "erase":
        msg = msg[:-5]
        
    if len(msg) == 0:
        print('YES')
        sys.exit()    
    
print('NO')

### FIXED CODE ###

import sys
msg = str(input())

# 逆から、大きい文字数から、見ていけば重複は生じない
len_l1 = 0
while len(msg) != len_l1:
    len_l1 = len(msg)
    
    # dreamer
    if msg[-7:] == "dreamer":
        msg = msg[:-7]
    # eraser
    if msg[-6:] == "eraser":
        msg = msg[:-6]
        
    # dreame
    if msg[-5:] == "dream":
        msg = msg[:-5]
    # erase
    if msg[-5:] == "erase":
        msg = msg[:-5]
        
    if len(msg) == 0:
        print('YES')
        sys.exit()    
    
print('NO')
L, R = map(int, input().split())

res = 1000000007

if R - L > 2019:
    print(0)
else:
    for i in range(L, R + 1):
    a = i % 2019
    for j in range(i + 1, R + 1):
        b = j % 2019
        res = min(res, a * b % 2019)

print(res)

### FIXED CODE ###

L, R = map(int, input().split())

res = 1000000007

if R - L + 1 > 2019:
    print(0)
else:
    amari = [i % 2019 for i in range(L, R + 1)]
    l = len(amari)
    for i in range(l):
        a = amari[i]
        for j in range(i + 1, l):
            b = amari[j]
            res = min(res, a * b % 2019)

    print(res)
N = int(input())
num = 0
for i in range(1,N):
    if i*i> N:
        break
    else:
        num = i*i
print(num)

### FIXED CODE ###

# -*- coding: utf-8 -*-


def main():
    c1 = input()
    c2 = input()
    if c1 == c2[::-1] and c2 == c1[::-1]:
        print('YES')
    else:
        print('NO')


if __name__ == '__main__':
    main()
n = input()
S = set(input().split())
q = input()
T = set(input().split())

#print(len(S&T))
#set:???????????????????????????

cnt = 0

for i in range(T):
    left = 0
    right = n-1
    while left < right:
        mid = (left+right) // 2
        if S[mid] == i:
            cnt += 1
            break
        elif S[mid] < i:
            left = mid+1
        else:
            right = mid-1
print(cnt)

### FIXED CODE ###

n = int(input())
S = input().split()
q = int(input())
T = input().split()

#print(len(S&T))
#set:???????????????????????????

cnt = 0

for i in range(len(T)):
    left = 0
    right = n
    while left < right:
        mid = (left+right) // 2
        if int(S[mid]) == int(T[i]):
            cnt += 1
            break
        elif int(S[mid]) < int(T[i]):
            left = mid+1
        else:
            right = mid
print(cnt)
def main():
    _ = input()
    lst = list(map(int, input().split()))
    ans = 1

    if 0 in lst:
        print(0)
        exit()
    for i in map(int, input().split()):
        ans *= i
        if ans > 10 ** 18:
            print(-1)
            exit()
    print(ans)


if __name__ == "__main__":
    main()


### FIXED CODE ###

def main():
    _ = input()
    lst = list(map(int, input().split()))
    ans = 1

    if 0 in lst:
        print(0)
        exit()
    for i in lst:
        ans *= i
        if ans > 10 ** 18:
            print(-1)
            exit()
    print(ans)


if __name__ == "__main__":
    main()
N = int(input())
direction = list(input())
change = [0]*N
change[0] = direction.count("E")
 
for i in range(1, N):
    f_n = 0
    f_p = 0
    if direction[i] == "E":
        f_n = 1
    if direction[i1] == "W":
        f_p = 1
    change[i] = change[i-1] - f_n + f_p
print(min(change))

### FIXED CODE ###

N = int(input())
direction = list(input())
change = [0]*(N+1)
# print(direction)
change[0] = direction.count("E")
# print(change)
for i in range(N):
    f_n = 0
    f_p = 0
    if direction[i] == "E":
        f_n = 1
    else:
        f_p = 1
    change[i+1] = change[i] - f_n + f_p
# print(change)
print(min(change))
s = input()

def func(num, lst):
  if num == 0:
    return len(lst[num])
  else:
    return func(num - 1, lst) + len(lst[num])

s = s.lstrip("W")
s = s.rstrip("B")
if 'W' in s:
  lst = s.split('W')
  out = 0
  for i in range(0, len(lst)):
    if not lst[i] == '':
      out += func(i, lst) 
  print(out)
else:
  print(0)

### FIXED CODE ###

cnt = ans = 0
for c in input():
  if c is 'W':
    ans += cnt
  else:
    cnt += 1
print(ans)
def fib(n):
  if F[n] != 0:
    return F[n]
  elif n==0 or n==1:
    F[n] = 1
  else:
    F[n] = fib(n-1) + fib(n-2)

  return F[n]



n = input()
F = [45]
print fib(n)

### FIXED CODE ###

def fib(n):
  if F[n] != 0:
    return F[n]
  elif n==0 or n==1:
    F[n] = 1
  else:
    F[n] = fib(n-1) + fib(n-2)

  return F[n]



n = input()
F = [0 for row in range(n+1)]
print fib(n)
str_S = raw_input()

list_S = list(str_S)

list_str = []
list_number = []

for char in list_S:
    number = 0
    if char == 'A' or char == 'C' or char == 'T' or char == 'G':
        index = list_S.index(char) + 1
        number += 1
        for n in list_S[index:]:
            if n == 'A' or n == 'C' or n == 'T' or n == 'G':
                char += n
                number += 1
            else:
                list_str.append(char)
                list_number.append(number)
                break


print int(max(list_number))


### FIXED CODE ###

# coding: utf-8
str_S = raw_input()

list_S = list(str_S)
list = list(str_S)

list_number = []

while list != []:
    number = 0
    for char in list:
        if char == 'A' or char == 'C' or char == 'G' or char == 'T':
            number += 1
        else:
            break
    list_number.append(number)

    for n in range(number + 1):
        if list != []:
            list.pop(0)

print max(list_number)
N=int(input())
S=input()
K=int(input())
cnt=0
for i in range(N):
    if S[i]!=S[K]:
        cnt+=1
print(cnt)

### FIXED CODE ###

N=int(input())
S=list(input())
K=int(input())
for i in range(N):
    if S[i]!=S[K-1]:
        S[i]='*'
print(''.join(S))
from collections import Counter
n=int(input())
c=input()
d=Counter(c)
MIN=min(Counter(c)["W"],Counter(c)["R"])
ans=[]
for i in range(MIN):
  if Counter(c[:i])["W"]<=Counter(c[i:])["R"]:
    ans.append(Counter(c[i:])["R"])
  else:
    ans.append(Counter(c[i:])["W"])
    
print(min(ans))

### FIXED CODE ###

from collections import Counter
n=int(input())
c=input()
d=Counter(c)
w,r=Counter(c)["W"], Counter(c)["R"]
ans=0
for i in range(r):
  if c[i]=="W":
    ans+=1

print(ans)
N = int(input())
H, W, N = [int(input()) for _ in range(N)]
print(min((N+H-1)//H, (W+W-1)//W))


### FIXED CODE ###

H, W, N = [int(input()) for _ in range(3)]
print(min((N+H-1)//H, (N+W-1)//W))
H, W = map(int, input().split())
C = []
for i in range(H):
    C[i] = input()
for i in range(H):
    print(C[i])
    print(C[i])

### FIXED CODE ###

H, W = map(int, input().split())
C = []
for i in range(H):
    C.append(input())
for i in range(H):
    print(C[i])
    print(C[i])
a=[int(input()) for i in range(5)]
minn=0
for i in a:
  minn=min(minn,i%10)
  summ+=(i+9)//10
print(summ+minn)

### FIXED CODE ###

a=[int(input()) for i in range(5)]
minn,summ=10,0
for i in a:
  minn=min(minn,(i-1)%10)
  summ+=(i+9)//10
print(summ*10+minn-9)
N, K = map(int, input().split())
h = [0]*N
for i in range(N):
    h[i] = int(input())
h.sort()
print(min(h[i+K+1] - h[i] for i in range(N-K+1)))

### FIXED CODE ###

N, K = map(int, input().split())
h = [0] * N
for i in range(N):
    h[i] = int(input())
h.sort()
print(min(h[i+K-1]-h[i] for i in range(N-K+1)))
a=input()
b=input()
square=int(a)*int(b)
length=int(a)*2+int(b)*2
#print(square),
#print(length)
print(square,length)

### FIXED CODE ###

InputNum = input().split()
tate = int(InputNum[0])
yoko = int(InputNum[1])
square = tate * yoko
length = 2* (tate + yoko)
print(square,length)
import sys
import itertools
import numpy as np
import networkx as nx

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, M = map(int, readline().split())
S = np.frombuffer(read(), 'S1').reshape(N, -1)[:, :M].astype('U1')

A = []
B = []
E = []

for i, j in itertools.product(range(N), range(M)):
    if S[i, j] == '#':
        continue
    elif (i + j) & 1:
        A.append((i, j))
    else:
        B.append((i, j))

for i, j in A:
    for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):
        i1, j1 = i + dx, j + dy
        if 0 <= i1 < N and 0 <= j1 < M and S[i1, j1] != '#':
            E.append(((i, j), (i1, j1)))

G = nx.Graph()
G.add_nodes_from(A, bipartite=0)
G.add_nodes_from(B, bipartite=1)
G.add_edges_from(E)

M = nx.bipartite.maximum_matching(G)
for key, item in M.items():
    i, j = key
    i1, j1 = item
    if i1 == i + 1:
        a, b = 'v', '^'
    elif i1 == i - 1:
        a, b = '^', 'v'
    elif j1 == j + 1:
        a, b = '>', '<'
    else:
        a, b = '<', '>'
    S[i, j], S[i1, j1] = a, b

print(len(M) // 2)
for row in S:
    print(''.join(row))

### FIXED CODE ###

import sys
import itertools
import numpy as np
import networkx as nx

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, M = map(int, readline().split())
S = np.frombuffer(read(), 'S1').reshape(N, -1)[:, :M].astype('U1')

G = nx.DiGraph()
G.add_nodes_from(['S', 'T'])
for i, j in itertools.product(range(N), range(M)):
    if S[i, j] == '#':
        continue
    if (i + j) % 2 == 0:
        G.add_edge('S', (i, j), capacity=1)
    else:
        G.add_edge((i, j), 'T', capacity=1)
    if (i + j) % 2 == 0:
        for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):
            i1, j1 = i + dx, j + dy
            if 0 <= i1 < N and 0 <= j1 < M and S[i1, j1] != '#':
                G.add_edge((i, j), (i1, j1), capacity=1)

flow_value, flow = nx.maximum_flow(G, 'S', 'T')
for i, j in itertools.product(range(N), range(M)):
    if (i, j) in flow:
        for v, f in flow[(i, j)].items():
            if v == 'T' or f == 0:
                continue
            i1, j1 = v
            if i1 == i + 1:
                a, b = 'v', '^'
            elif i1 == i - 1:
                a, b = '^', 'v'
            elif j1 == j + 1:
                a, b = '>', '<'
            else:
                a, b = '<', '>'
            S[i, j], S[i1, j1] = a, b

print(flow_value)
for row in S:
    print(''.join(row))
import sys
N, M = map(int, input().split())
sys.setrecursionlimit(N)
E = [[] for _ in range(N)]
for i in range(M):
    x, y = map(int, input().split())
    E[x-1].append(y-1)

dp = [-1]*N


def rec(v):
    if dp[v] != -1:
        return dp[v]
    if len(E[v]) == 0:
        dp[v] = 0
        return dp[v]

    for to in E[v]:
        dp[v] = max(dp[v], rec(to)+1)
    return dp[v]


for i in range(N):
    dp[i] = rec(i)
print(max(dp))

### FIXED CODE ###

import sys
N, M = map(int, input().split())
sys.setrecursionlimit(10**5)
E = [[] for _ in range(N)]
for i in range(M):
    x, y = map(int, input().split())
    E[x-1].append(y-1)

dp = [-1]*N


def rec(v):
    if dp[v] != -1:
        return dp[v]
    if len(E[v]) == 0:
        dp[v] = 0
        return dp[v]

    for to in E[v]:
        dp[v] = max(dp[v], rec(to)+1)
    return dp[v]


for i in range(N):
    dp[i] = rec(i)
print(max(dp))
n = int(input())
edges = [[] for _ in range(n)]
for i in range(n-1):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    edges[a].append((b, c))
    edges[b].append((a, c))

q, k = map(int, input().split())
k -= 1

visited = [0] * n
d = [0] * n
def dfs(v, c):
    for u, c2 in edges[v]:
        if visited[u] == 0:
            d[u] = c + c2
            visited[u] = 1
            dfs(u, c + c2)

visited[k] = 1
dfs(k, 0)

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


### FIXED CODE ###

import sys
sys.setrecursionlimit(200000)
n = int(input())
edges = [[] for _ in range(n)]
for i in range(n-1):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    edges[a].append((b, c))
    edges[b].append((a, c))

q, k = map(int, input().split())
k -= 1

visited = [0] * n
d = [0] * n
def dfs(v, c):
    for u, c2 in edges[v]:
        if visited[u] == 0:
            d[u] = c + c2
            visited[u] = 1
            dfs(u, c + c2)

visited[k] = 1
dfs(k, 0)

for i in range(q):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    print(d[x] + d[y])
N, K = list(map(int,input().split()))
A = list(map(int,input().split()))
P = [0] + [abs(i) for i in A if i> 0 ]
M = [abs(i) for i in A if i< 0 ] +[0]
M = sorted(M,reverse =False)

cnt = 10000000

if len(P) == len(M) == 1:
    cnt = 0
elif len(P) == 1:
    cnt = M[K]
elif len(M) == 1:
    cnt = P[K]
else:
    #正の部分、負の部分の長さが0の場合エラーが起こる
    #正の方向でi個ろうそくを通って、負の方向にK-i個ろうそくを通る
    for i in range(1,K):
        cnt =min(cnt,P[i]*2 + M[K-i])
        cnt =min(cnt,M[i]*2 + P[K-i])
        #print(cnt)

print(cnt)

### FIXED CODE ###

N, K = list(map(int,input().split()))
A = list(map(int,input().split()))
P = [0] + [abs(i) for i in A if i> 0 ]
M = [abs(i) for i in A if i< 0 ] +[0]
M = sorted(M,reverse =False)


cnt = 10000000000

if len(P) == len(M) == 1:
    cnt = 0
elif len(P) == 1:
    cnt = M[K]
elif len(M) == 1:
    cnt = P[K]
else:
    #リストがK以下の場合不具合が起きる
    #for i in range(1,K):   
    #len(P)=4 len(M)=3 K=5の場合、IndexErrorとなる。rangeをうまく設定する必要があるが実装できない。
    #正の方向でi個ろうそくを通って、負の方向にK-i個ろうそくを通る
    s = max(0,K-len(M)+1)
    e = min(K+1, len(P))
    for i in range(s, e):
        cnt =min(cnt,P[i]*2 + M[K-i])
    #負の方向でi個ろうそくを通って、正の方向にK-i個ろうそくを通る
    s1 = max(0,K-len(P)+1)
    e1 = min(K+1, len(M))
    for i in range(s1, e1):
        cnt =min(cnt,M[i]*2 + P[K-i])

print(cnt)
a = list(int, input().split())
a.sort()
print(a[0], a[1], a[2])

### FIXED CODE ###

a = list(map(int, input().split()))
a.sort()
print(a[0], a[1], a[2])
N, M = map(int, input().split())
Fm = [0]*M
KA = [list(map(int, input().split())) for _ in range(N)]
for n in range(N):
  for i in range(KA[n][0]):
    Fm[KA[n][i+1]] += 1
ans = 0
for m in range(M):
  if Fm[m] == N:
    ans += 1
print(ans)

### FIXED CODE ###

N, M = map(int, input().split())
Fm = [0]*M
KA = [list(map(int, input().split())) for _ in range(N)]
for n in range(N):
  #KA = list(map(int, input().split()))
  for i in range(KA[n][0]):
    Fm[KA[n][i+1]-1] += 1
ans = 0
for m in range(M):
  if Fm[m] == N:
    ans += 1
print(ans)