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

#Union Find
 
#xの根を求める
def find(x):
    if par[x] < 0:
        return x
    else:
        par[x] = find(par[x])
        return par[x]
#xとyの属する集合の併合
def unite(x,y):
    x = find(x)
    y = find(y)
    
    if x == y:
        return False
    else:
        #sizeの大きいほうがx
        if par[x] > par[y]:
            x,y = y,x
        par[x] += par[y]
        par[y] = x
        return True

#xとyが同じ集合に属するかの判定
def same(x,y):
    return find2(x) == find2(y)

def find2(x):
  if pa[x] < 0:
      return x
  else:
      pa[x] = find2(pa[x])
      return pa[x]

#根なら-size,子なら親の頂点
n,k,l = [int(i) for i in input().split()]
par = [-1]*n
pa = [-1]*n
ans = [0]*n

for i in range(k):
  p,q = [int(i) for i in input().split()]
  unite(p-1,q-1)

def unite2(x,y):
    x = find2(x)
    y = find2(y)
    if x == y:
        return False
    else:
        if pa[x] > pa[y]:
            x,y = y,x
        pa[x] += pa[y]
        pa[y] = x
        return True
  
for i in range(l):
  r,s = [int(i) for i in input().split()]
  unite2(r-1,s-1)

#print(par,pa)
chk = []
for i in range(n):
  chk_append = chk.append
  chk_append((find(i),find2(i)))
  
import collections
for i in range(n):
  c = collections.Counter(chk)
  ans[i] += c[chk[i]]
  
ans = list(map(str,ans))
print(' '.join(ans))

### FIXED CODE ###

def main():  
  #Union Find

  #xの根を求める
  def find(x):
      if par[x] < 0:
          return x
      else:
          par[x] = find(par[x])
          return par[x]
  #xとyの属する集合の併合
  def unite(x,y):
      x = find(x)
      y = find(y)

      if x == y:
          return False
      else:
          #sizeの大きいほうがx
          if par[x] > par[y]:
              x,y = y,x
          par[x] += par[y]
          par[y] = x
          return True

  def find2(x):
    if pa[x] < 0:
        return x
    else:
        pa[x] = find2(pa[x])
        return pa[x]

  import sys
  input = sys.stdin.readline

  n,k,l = [int(i) for i in input().split()]
  par = [-1]*n
  pa = [-1]*n
  ans = [0]*n

  for i in range(k):
    p,q = [int(i) for i in input().split()]
    unite(p-1,q-1)

  def unite2(x,y):
      x = find2(x)
      y = find2(y)
      if x == y:
          return False
      else:
          if pa[x] > pa[y]:
              x,y = y,x
          pa[x] += pa[y]
          pa[y] = x
          return True

  for i in range(l):
    r,s = [int(i) for i in input().split()]
    unite2(r-1,s-1)

  dic = {}
  chk = []
  for i in range(n):
      p1 = find(i)
      p2 = find2(i)
      chk.append((p1,p2))
      if (p1,p2) in dic:
          dic[(p1,p2)] += 1
      else: 
          dic[(p1,p2)] = 1
  print(' '.join([str(dic[chk[i]]) for i in range(n)]))
  
if __name__ == '__main__':
    main()
### BUGGY CODE ###

n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
ans = -float('inf')

for i in range(n):
    start, count = i, 1
    val = c[start]
    while p[start]-1 != i:
        start = p[start]-1
        count += 1
        val += c[start]

    start = i
    if val > 0:
        a = (k//count-1)*val
        if a > ans:
            ans = a
        for j in range(k%count+count):
            start = p[start]-1
            a += c[start]
            if a > ans:
                ans = a
    else:
        a = 0
        for j in range(min(k, count)):
            start = p[start]-1
            a += c[start]
            if a > ans:
                ans = a
                
print(ans)



### FIXED CODE ###

from numba import jit
@jit
def main():
    n, k = map(int, input().split())
    p = list(map(int, input().split()))
    c = list(map(int, input().split()))
    ans = -10**10
    
    for i in range(n):
        start, count = i, 1
        val = c[start]
        while p[start]-1 != i:
            start = p[start]-1
            count += 1
            val += c[start]

        start = i
        if val > 0:
            a = (k//count-1)*val
            ans = max(a, ans)
            num = k%count+count
        else:
            a = 0
            num = min(k, count)

        for _ in range(num):
            start = p[start]-1
            a += c[start]
            ans = max(a, ans)
    print(int(ans))

main()
### BUGGY CODE ###

n=int(input())
a=list(map(int,input().split()))
b={}
for i in range(n):
    if a[i] in b:
        b[a[i]]+=1
    else:
        b[a[i]]=1

for i in range(n):
    s = 0
    b[a[i]]-=1
    for key,val in b.items():
        if val>1:
            s+=val*(val-1)//2
    b[a[i]]+=1
    print(s)


### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
b={}
p=0
for i in range(n):
    if a[i] in b :
        b[a[i]]+=1
    else:
        b[a[i]]=1
for key,val in b.items():
    p+=val*(val-1)//2



for i in range(n):
    t=b[a[i]]
    s=t*(t-1)//2
    r=(t-1)*(t-2)//2

    print(p-s+r)
### BUGGY CODE ###

def resolve():
    N = int(input())
    A = [int(input()) - 1 for _ in range(N)]

    pressed = []
    i = 0
    t = 0
    while i != 1:
        i = A[i]
        if i in pressed:
            t = -1
            break
        pressed.append(i)
        t += 1

    print(t)
    
resolve()

### FIXED CODE ###

def resolve():
    N = int(input())
    A = [int(input()) - 1 for _ in range(N)]

    pressed = 0
    i = 0
    t = 0
    while i != 1:
        i = A[i]
        if pressed > N:
            t = -1
            break
        pressed += 1
        t += 1

    print(t)
    
resolve()
### BUGGY CODE ###

N, K = map(int,input().split())
H = list(map(int,input().split()))

#DP[i] i番目までの足場を見たときの足場iにたどり着く最小値

DP = [float('inf') for _ in range(N + 1)] 
DP[0] = 0
for i in range(N):
    for k in range(1,K + 1):
        if 0 <= i - k and i - k < N:
            cost_min = float('inf')
            cost_min = min(cost_min, DP[i - k] +  abs(H[i] - H[i - k]))
            DP[i] = min(DP[i],cost_min)
print(DP[N - 1])



### FIXED CODE ###

N, K  = map(int,input().split())
L = list(map(int,input().split()))
dp = [10**10 for _ in range(N+2)]
dp[0] = L[0]
for i in range(N-1):
    for k in range(1,1+K):
        try:
            dp[i+k] = min(dp[i]+abs(L[i]-L[i+k]),dp[i+k])
        except:
            pass
print(dp[N-1]-dp[0])
### BUGGY CODE ###

def main():
    n, k = map(int, input().split())
    p = list(map(int, input().split()))
    e = []
    for j in range(1, n-k+2):
        e.append((k + (sum(p[j-1:j+k-1]))) / 2.)
    ans = max(e)
    print(ans)

if __name__ == "__main__":
    main()

### FIXED CODE ###

def main():
    n, k = map(int, input().split())
    p = list(map(int, input().split()))
    e = [(p[j-1]+1)/2.for j in range(1, n+1)]
    s = [sum(e[:k])]
    for i in range(1, n-k+1):
        s.append(s[-1]-e[i-1]+e[k+i-1])
    ans = max(s)
    print(ans)

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

n = int(input())
S = input()
ans = []
for i in range(10):
  for j in range(10):
    for k in range(10):
      for x in range(n-2):
        if int(S[x]) == i:
          for y in range(x+1,n):
            if int(S[y]) == j:
              for z in range(y+1,n):
                if int(S[z]) == k:
                  ans.append(100*i+10*j+k)
print(len(set(ans)))

### FIXED CODE ###

n = int(input())
S = input()
ans = 0
for i in range(10):
  x = S.find(str(i))
  if x == -1:
    continue
  for j in range(10):
    y = S.find(str(j),x+1)
    if y == -1:
      continue
    for k in range(10):
      z = S.find(str(k),y+1)
      if z != -1:
        ans += 1
print(ans)
### BUGGY CODE ###

import itertools
from functools import lru_cache


def resolve():
    MOD = 10**9 + 7
    N, K = map(int, input().split())
    A = map(int, input().split())

    c_list = itertools.combinations(A, K)

    @lru_cache(maxsize=None)
    def f(x):
        return (max(x) % MOD - min(x) % MOD) % MOD

    fx = map(f, c_list)
    total = sum(fx)
    total = total % MOD
    print(total)

if __name__ == "__main__":
    resolve()

### FIXED CODE ###

import math
from functools import lru_cache
from operator import mul
from functools import reduce


def resolve():
    import sys
    input = sys.stdin.readline
    MOD = 10**9 + 7
    N, K = map(int, input().split())
    A = list(map(int, input().split()))

    A.sort()
    B = A[::-1]

    @lru_cache(maxsize=None)
    def combinations_count__(n, r):
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

    @lru_cache(maxsize=None)
    def combinations_count____(n, r):
        r = min(r, n - r)
        numer = reduce(mul, range(n, n - r, -1), 1)
        denom = reduce(mul, range(1, r + 1), 1)
        return numer // denom

    def combinations_count_(n, r):
        if n - r < r: r = n - r
        if r == 0: return 1
        if r == 1: return n

        numerator = [n - r + k + 1 for k in range(r)]
        denominator = [k + 1 for k in range(r)]

        for p in range(2,r+1):
            pivot = denominator[p - 1]
            if pivot > 1:
                offset = (n - r) % p
                for k in range(p-1,r,p):
                    numerator[k - offset] /= pivot
                    denominator[k] /= pivot

        result = 1
        for k in range(r):
            if numerator[k] > 1:
                result *= int(numerator[k])

        return result

    def modinv(a, mod=10**9+7):
        return pow(a, mod-2, mod)

    def combinations_count(n, r, mod=10**9+7):
        if (r < 0) or (n < r):
            return 0
        r = min(r, n-r)
        return fact[n] * factinv[r] * factinv[n-r] % mod
    
    fact = [1, 1]
    factinv = [1, 1]
    inv = [0, 1]
    for i in range(2, N + 1):
        fact.append((fact[-1] * i) % MOD)
        inv.append((-inv[MOD % i] * (MOD // i)) % MOD)
        factinv.append((factinv[-1] * inv[-1]) % MOD)

    sum = 0
    for i in range(K-1, N):
        sum += combinations_count(i, K-1) * A[i] % MOD - combinations_count(i, K-1) * B[i] % MOD

    print(sum % MOD)


if __name__ == "__main__":
    resolve()
### BUGGY CODE ###

def C():
    nk=list(map(int,input().split()))
    
    abs_val=abs(nk[0]-nk[1])
    
    while True:
        if abs_val>nk[1]:
            abs_val=abs(abs_val-nk[1])
        elif abs_val<nk[1]:
            abs_val=min(abs_val,abs(abs_val-nk[1]))
            break
        else :
            # 0
            abs_val=abs_val-nk[1]
            break
    
    print(min(abs_val,nk[0]))
    
    
    
if __name__=='__main__':
    C()


### FIXED CODE ###

def C():
    nk=list(map(int,input().split()))
    
    abs_val=abs(nk[0]-nk[1])
    
    while True:
        if abs_val>nk[1]:
            abs_val=abs_val%nk[1]
        elif abs_val<nk[1]:
            abs_val=min(abs_val,abs(abs_val-nk[1]))
            break
        else :
            # 0
            abs_val=abs_val-nk[1]
            break
    
    print(min(abs_val,nk[0]))
    
    
    
if __name__=='__main__':
    C()
### BUGGY CODE ###

S=list(input())
l = len(S)
i = 0
j = -1
ans = 0
while i<l:
    s = S[i]
    if j>=0:
        if s=="A":
            i+=1
        elif i+1<l and s == "B" and S[i+1] == "C":
            S[i] = "A"
            S[i+1] = "A"
            S[j] = "B"
            S[j+1] = "C"
            ans += i-j
            i = j
            j = -1
        else:
            j = -1
            i += 1
    elif s == "A":
        j=i
        i+=1
    else:
        i+=1
print(ans)

### FIXED CODE ###

S=input().replace("BC","D").replace("B","C").split("C")
ans = 0
for s in S:
    a = 0
    for c in s:
        if c == "A":a+=1
        else:ans += a
print(ans)
### BUGGY CODE ###

def gcd(a, b):
  if a%b == 0:
    return b
  else:
    return gcd(b, a%b)
def multi_gcd(l):
  l = sorted(l)
  if len(l) == 1:
    return l[0]
  else:
    new_l = []
    if len(l)%2 == 0:
      for i in range(len(l)//2):
        temp_gcd = gcd(l[2*i], l[2*i+1])
        if temp_gcd == 1:
          return 1
        else:
          if temp_gcd not in new_l:
            new_l.append(temp_gcd)
      return multi_gcd(new_l)
    else:
      for i in range(len(l)//2):
        temp_gcd = gcd(l[2*i], l[2*i+1])
        if temp_gcd == 1:
          return 1
        else:
          if temp_gcd not in new_l:
            new_l.append(temp_gcd)
      new_l.append(l[-1])
      return multi_gcd(new_l)
     
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(n):
  a_gcd = multi_gcd(a[:i]+a[i+1:])
  if ans < a_gcd:
    ans = a_gcd
print(ans)

### FIXED CODE ###

def gcd(a, b):
  if a%b == 0:
    return b
  else:
    return gcd(b, a%b)
def mgcd(l):
  if len(l) == 1:
    return l[0]
  else:
    new_l = []
    for i in range(-(-len(l)//2)):
      temp = gcd(l[i], l[-i-1])
      if temp == 1:
        return 1
      else:
        new_l.append(temp)
    return mgcd(new_l)

n = int(input())
a = list(map(int, input().split()))
a.sort()
ans1 = mgcd(a[1:])
min_gcd = 1001001001
min_idx = 0
for i in range(1,n):
  temp = gcd(a[0], a[i])
  if temp < min_gcd:
    min_gcd = temp
    min_idx = i
a.pop(min_idx)
ans2 = mgcd(a)
print(max(ans1, ans2))
### BUGGY CODE ###

n = int(input())
s = input()
r = []
g = []
b = []
for i in range(n):
	if s[i] == "R":
		r.append(i)
	if s[i] == "G":
		g.append(i)
	if s[i] == "B":
		b.append(i)
count = len(r)*len(g)*len(b)
for i in r:
	for j in g:
		u = abs(i-j)
		if (max(i,j)+u) in b:
			count -= 1
		if (min(i,j)-u) in b:
			count -= 1
		if u%2 == 0:
			if (i+j)//2 in b:
				count -= 1
print(count)

### FIXED CODE ###

from collections import Counter
# これは, u749194f さんの改造です。
# オリジナルではございません。
# https://atcoder.jp/contests/abc162/submissions/11825963
n = int(input())
s = input()
flag = True
if len(set(s))<=2:
    print(0)
    flag = False
if flag:
    r = [i+1 for i in range(n) if s[i]=='R']
    b = [i+1 for i in range(n) if s[i]=='B']
    g = [i+1 for i in range(n) if s[i]=='G']
    minus = 0
    def cnt_cmb(a, b, c):
        cnt = 0
        set_c = set(c)
        for i in a:
            for j in b:
                if (i+j)/2 in set_c:
                    cnt += 1
        return cnt
    minus += cnt_cmb(r, b, g)
    minus += cnt_cmb(b, g, r)
    minus += cnt_cmb(g, r, b)
    a = Counter(s)
    rslt = 1
    for value in a.values():
        rslt *= value
    print(len(r)*len(b)*len(g)-minus)
### BUGGY CODE ###

#import numpy as np
import sys
from collections import defaultdict
import math
import itertools
import bisect
read = sys.stdin.buffer.read
readline = sys.stdin.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10000)

N = int(readline())
tmp = list(map(int,input().split()))
A = []  
[bisect.insort_right(A, x) for x in tmp]
tmp = list(map(int,readline().rstrip().split()))
B = []
[bisect.insort_right(B,x) for x in tmp ]
tmp = list(map(int,readline().rstrip().split()))
C = []
[bisect.insort_right(C,x) for x in tmp]

ans = 0
for b in B :
    indA = bisect.bisect_left(A,b)
    indC = bisect.bisect_right(C,b)
    ans += indA * (N-indC)

print(ans)

### FIXED CODE ###

#import numpy as np
import sys
from collections import defaultdict
import math
import itertools
import bisect
read = sys.stdin.buffer.read
readline = sys.stdin.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10000)

N = int(readline())
A = list(map(int,input().split()))
#A = []  
#[bisect.insort_right(A, x) for x in tmp]
B= list(map(int,readline().rstrip().split()))
#B = []
#[bisect.insort_right(B,x) for x in tmp ]
C = list(map(int,readline().rstrip().split()))
#C = []
#[bisect.insort_right(C,x) for x in tmp]

A.sort()
C.sort()

ans = 0
for b in B :
    indA = bisect.bisect_left(A,b)
    indC = bisect.bisect_right(C,b)
    ans += indA * (N-indC)

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

import math
tmp = input().split()
X = int(tmp[0])
Y = int(tmp[1])

mod = 10 ** 9 + 7

if (X + Y) % 3 != 0:
    print(0)
else:
    n = (2 * Y - X) / 3
    m = Y - 2 * n
    if n < 0 or m < 0:
        print(0)
        exit()
    n %= mod
    m %= mod
    total = math.factorial(n+m)
    n = math.factorial(n)
    m = math.factorial(m)

    total //= n*m
    print(total%mod)

### FIXED CODE ###

import math
tmp = input().split()
X = int(tmp[0])
Y = int(tmp[1])

mod = 10 ** 9 + 7

if (X + Y) % 3 != 0:
    print(0)
else:
    n = int((2 * Y - X) / 3)
    m = int(Y - 2 * n)
    if n < 0 or m < 0:
        print(0)
        exit()
    n %= mod
    m %= mod

    r = n
    n = n+m
    # if n - r < r:
    #     r = n - r
    # if r == 0:
    #     return 1
    # if r == 1:
    #     return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2, r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    # return result
    print(result % mod)
### BUGGY CODE ###

import itertools
N,A = map(int, input().split())
X = map(int,input().split())

ans = 0
X = sorted(X)
for n in range(1,N+1):
    tmp = []
    tmp = list(itertools.combinations(X,n))
    tmp_sum = []

    for i in range(len(tmp)):
        tmp_sum.append(sum(tmp[i]))
    ans += tmp_sum.count(A*n)

print(ans)


### FIXED CODE ###

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

max_x = max(X)
Y = []
for x in X:
    Y.append(x - A)

dp=[[0 for t in range(60*60*2)] for j in range(60)]
#dp[j番目までの変数から0枚以上選んでy_iの合計を][t-NXにする] = 総数

#dp[0][N*max_x] = 1
for j in range(N+1):
    for t in range(2*N*max_x+1):
        if j == 0 and t == N*max_x:
            dp[j][t] = 1
        else:
            if j>=1 and (t-Y[j-1] < 0 or t - Y[j-1] > 2*N*max_x):
                dp[j][t] = dp[j-1][t]
            elif j>=1 and 0<= t-Y[j-1] <=2*N*max_x:
                dp[j][t] = dp[j-1][t] + dp[j-1][t-Y[j-1]]
            else:
                dp[j][t] = 0
print(dp[N][N*max_x]-1)



# dp=[[[0 for s in range(60*60)] for k in range(60)] for j in range(60)]
# for s in range(60*60):
#     dp[0][0][s]=0
# #dp[j番目までの変数から][k個選んで][合計がs] = になる総数
# for j in range(N+1):
#     for k in range(N+1):
#         for s in range(N*k+1):
#             if j == 0 and k == 0 and s == 0:
#                 dp[j][k][s] = 1
#             else:
#                 if j >= 1 and s < X[j-1]:
#                     dp[j][k][s] = dp[j-1][k][s]
#                 elif j >= 1 and  k >= 1 and s>=X[j-1]:
#                     dp[j][k][s] = dp[j-1][k][s] + dp[j-1][k-1][s-X[j-1]]
#                 else:
#                     dp[j][k][s] = 0

# print(dp[0][0][6])
# ans = 0
# for k in range(N+1):
#     ans += dp[N][k][k*A]
# print('ans = ',ans)

# import itertools
# N,A = map(int, input().split())
# X = map(int,input().split())

# ans = 0
# X = sorted(X)
# for n in range(1,N+1):
#     tmp = []
#     tmp = list(itertools.combinations(X,n))
#     tmp_sum = []

#     for i in range(len(tmp)):
#         tmp_sum.append(sum(tmp[i]))
#     ans += tmp_sum.count(A*n)
#     if tmp_sum[0] > A*n:
#         break

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

S_A, S_B, S_C = [list(input()) for i in range(3)]
S = S_A[0]
while True:
    if S == 'a':
        if len(S_A) == 0:
            break
        else:
            S == S_A.pop(0)
    elif S == 'b':
        if len(S_B) == 0:
            break
        else:
            S = S_B.pop(0)
    elif S == 'c':
        if len(S_C) == 0:
            break
        else:
            S == S_C.pop(0)
if S == 'a':
    print('A')
elif S == 'b':
    print('B')
elif S == 'c':
    print('C')

### FIXED CODE ###

a, b, h = [int(input()) for i in range(3)]
S = (a + b) * h / 2
S = int(S)
print(S)
### BUGGY CODE ###

N = int(input())
A = []
for _ in range(N):
    A.append(input()) 
B = set(A)
M = len(B)
ans = 0
for i in B:
  if A.count(i)%2 ==1:
    ans += 1
print(ans)

### FIXED CODE ###

import collections
n=int(input())
arr=[]
for i in range(n):
  a=int(input())
  arr.append(a)
c = collections.Counter(arr)
ans=0
for j in c.values():
    if j % 2 != 0:
        ans += 1
print(ans)
### BUGGY CODE ###

MOD = 10**9+7
l = input()
n = len(l)
dp = [[0 for _ in range (n + 1) ] for _ in range(n + 1)]
dp[0][0]  = 1
#dp[桁数][条件を満たすことが確定しているフラグ]
# 00 10 01

for i in range(1,n +1):
    # (dp[i][flag]のflag,dp[i-1][flag]のflag,i桁目の数字) で繊維は2**3の8通り
    dp[i][1] += dp[i - 1][1] * 3 # i桁目が (0,1) flag が 1 1
    #flag 0→1はない

    if l[i-1] == "1":
        dp[i][0] += dp[i -1][0] * 2 #1,0  or 0,1のときに相変わらず未定
        dp[i][1] += dp[i -1][0] # 0,0のときだけ確定させられる e.g. 101 10 のときに次に0が来れば確定 なので0,0
    else:
        dp[i][0] +=dp[i-1][0] # 0,0 の場合のみ
        # dp[i][1] への遷移は無し
ans = dp[-1][0] + dp[-1][1]
print(ans % MOD)


### FIXED CODE ###

MOD = 10**9+7
l = input()
n = len(l)
dp = [    [0,0] for _ in range(n + 1)]
dp[0][0]  = 1
#dp[桁数][条件を満たすことが確定しているフラグ]
# 00 10 01

for i in range(1,n +1):
    # (dp[i][flag]のflag,dp[i-1][flag]のflag,i桁目の数字) で繊維は2**3の8通り
    dp[i][1] += dp[i - 1][1] * 3 # i桁目が (0,1) flag が 1 1
    #flag 0→1はない

    if l[i-1] == "1":
        dp[i][0] += dp[i -1][0] * 2 #1,0  or 0,1のときに相変わらず未定
        dp[i][1] += dp[i -1][0] # 0,0のときだけ確定させられる e.g. 101 10 のときに次に0が来れば確定 なので0,0
    else:
        dp[i][0] +=dp[i-1][0] # 0,0 の場合のみ
        # dp[i][1] への遷移は無し
    dp[i][0] %= MOD
    dp[i][1] %= MOD
ans = dp[-1][0] + dp[-1][1]
print(ans % MOD)
### BUGGY CODE ###

import math
import copy
import sys
import fractions
import numpy as np
from functools import reduce
import statistics
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

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


treeList = [[] for _ in range(n)]
for i in range(n-1):
  a,b = map(int, input().split())
  a -= 1
  b -= 1
  treeList[a].append(b) # a<bとは限らないため、双方向にする
  treeList[b].append(a)


data3 = getSomeInputListInt(q)

ans = np.zeros(n)

for i in range(len(data3)):
    ans[data3[i][0]-1] += data3[i][1]


def treeTrace(crnt, prnt):
    for chld in treeList[crnt]:
        if prnt == chld:
            continue
        ans[chld] += ans[crnt]

        treeTrace(chld, crnt)

treeTrace(0, -1)

for s in ans:
    print(int(s), end =" ")


### FIXED CODE ###

import sys
input = sys.stdin.readline #for input speed
sys.setrecursionlimit(10**6) #for deep recursion
n, q = map(int, input().split())

tree_index = [[] for _ in range(n)]
for i in range(n-1):
  a,b = map(int, input().split())
  a -= 1
  b -= 1
  tree_index[a].append(b) # a<bとは限らないため、双方向にする
  tree_index[b].append(a)
ans = [0] * n
for i in range(q):
  p, x = map(int, input().split())
  ans[p-1] += x
def dfs(cur, par):
    for chl in tree_index[cur]:
      if chl == par: #parentとchildが等しい時はスキップ
        continue
      ans[chl] += ans[cur] 
      dfs(chl, cur)
dfs(0, -1)
print(*ans)