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

S = input()[::-1]
c = ['maerd', 'remaerd', 'esare', 'resare']

def dfs(s):
    while 1:
        l = len(s)
        if l==0:
            return True
        if l<5:
            return False
        if s[:5] in c:
            s = s[5:]
        elif l>=6 and s[:6]=='resare':
            s = s[6:]
        elif l>=7 and s[:7]=='remaerd':
            s = s[7:]

if dfs(S):
    print('YES')
else:
    print('NO')
        

### FIXED CODE ###

S = input()[::-1]
import sys
sys.setrecursionlimit(1000)
c = ['maerd', 'remaerd', 'esare', 'resare']

def dfs(s):
    L = len(s)
    while 1:
        if L==0:
            return True
        if L<5:
            return False
        if s[:5] in c:
            s = s[5:]
            L -= 5
        elif L>=6 and s[:6]=='resare':
            s = s[6:]
            L -= 6
        elif L>=7 and s[:7]=='remaerd':
            s = s[7:]
            L -= 7
        else:
            return False

if dfs(S):
    print('YES')
else:
    print('NO')
### BUGGY CODE ###

K,S = map(int,(input().split()))

n = 0

for x in range(S+1) :
        if (S - x <= 2 * K):
                for y in range(S+1) :
                        if (K >= x) and (K >= y) and (S >= x + y) and (S-x-y <= K):
                                n = n + 1

                
print(n)

### FIXED CODE ###

K,S = map(int,(input().split()))

n = 0

for x in range(K+1) :
        if (S - x <= 2 * K):
                for y in range(K+1) :
                        if 0 <=S-x-y <= K:
                                n += 1

                
print(n)
### BUGGY CODE ###

d,g = map(int,input().split())
li = [list(map(int,input().split())) for i in range(d)]

for i in range(d):
  li[i].append(li[i][0]*100*(i+1) + li[i][1])
tmp = 10 ** 9
def func(x,lis,cnt):
  global tmp
  if x <= 0:
    return cnt
  else:
    for i in range(d):
      if i not in lis:
        if x <= 100*(i+1)*(li[i][0]-1):
          tmp = min(tmp,cnt + (x-1)//(100*(i+1)) + 1)
          #print("{} {} {}".format(i,lis,tmp))
        elif x <= li[i][2]:
          tmp = min(tmp,cnt + li[i][0])
          #print("{} {} {}".format(i,lis,tmp))
        else:
          tmp = min(tmp,func(x-li[i][2],lis+[i],cnt+li[i][0]))
          #print("{} {} {}".format(i,lis,tmp))
    return tmp
print(func(g,[],0))

### FIXED CODE ###

d,g = map(int,input().split())
li = [list(map(int,input().split())) for i in range(d)]

ans = 10 ** 9
for i in range(2**d):
  ch = bin(i)[2:]
  ch = ch.zfill(d)
  tmpsum = 0
  cnt = 0
  for j in range(d):
    if ch[j] == "1":
      tmpsum += li[j][0]*100*(j+1) + li[j][1]
      cnt += li[j][0]
  if tmpsum >= g:
    ans = min(ans,cnt)
    #print("{} {}".format(bin(i),cnt))
  else:
    ind = ch.rfind("0")
    if tmpsum + (li[ind][0]-1)*100*(ind+1) >= g:
      ans = min(ans,cnt+(g-tmpsum-1)//(100*(ind+1))+1)
      #print("{} {} {}".format(bin(i),cnt,ans))
print(ans)
### BUGGY CODE ###

#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush,heapify
from bisect import bisect_left

def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    heapify(A)
    for i in range(M):
        B, C = map(int, input().split())
        count = 0
        while count < B:
            q = heappop(A)
            if q < C:
                heappush(A,C)
                count += 1
            else:
                heappush(A,q)
                break
    ans = 0
    while A:
        ans += heappop(A)
    print(ans)

if __name__ == "__main__":
    main()


### FIXED CODE ###

#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush,heapify
from bisect import bisect_left

def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    A.sort()
    q = []
    for i in range(M):
        B, C = map(int, input().split())
        heappush(q,(-C,B))
    
    ans = 0
    index = 0
    # print(A)
    while q:
        c,b = heappop(q)
        c *= -1
        # print(c,b)
        count = 0
        for i in range(b):
            if i + index == N:
                break
            if c > A[index+i]:
                ans += c
                count += 1
            else:
                break

        # print(ans,index)
        if count < b:
            index += count
            break
        else:
            index += count
            
    print(ans + sum(A[index:]))

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

def find_gcd(x, y):

    while(y):
        x, y = y, x%y

    return x

nums = int(input())
result = 0

for i in  range(1, nums+1):
    for j in range(1, nums+1):
        for l in range(1, nums+1):
            gcd = find_gcd(i, j)
            result += find_gcd(gcd, l)

print(result)

### FIXED CODE ###

from math import gcd

nums = int(input())
result = 0

gcd_dict = {}

for i in  range(1, nums+1):
    for j in range(1, nums+1):
        for l in range(1, nums+1):
            result += gcd(gcd(i, j), l)

print(result)
### BUGGY CODE ###

N,M=map(int,input().split())
a=list(map(int,input().split()))
dp=[[0 for i in range(N+1)] for j in range(N+1)]
count=0
for i in range(N):
    dp[i+1][i+1]=a[i]
    if a[i]%M==0:
        count+=1
    if N==1:
        break
    else:
        for j in range(i+2,N+1):
            dp[i+1][j]=dp[i+1][j-1]+a[j-1]
            if dp[i+1][j]%M==0:
                count+=1
print(count)

### FIXED CODE ###

from collections import Counter
N,M=map(int,input().split())
a=list(map(int,input().split()))
s=[a[0]]
if N==1:
    if a[0]%M==0:
        print(1)
    else:
        print(0)
else:
    for i in range(N-1):
        s.append(s[i]+a[i+1])
    for i in range(N):
        s[i]%=M
    count=0
    b=Counter(s)
    c=b.most_common()
    def calc(n):
        ans=0
        for i in range(1,n+1):
            ans+=i
        return ans
    for i in range(len(c)):
        if c[i][1]!=1:
            count+=calc(c[i][1]-1)
            if c[i][0]==0:
                count+=c[i][1]
        else:
            break
    print(count)
### BUGGY CODE ###

# coding: utf-8
#!/usr/bin/env python3

"""
Python の queue モジュールは遅いので使わない
deque を使おう!
"""

from collections import deque

h, w = map(int, input().split())
a = [ list(input()) for _ in range(h) ]

# direction vector
dx, dy = [1, 0, -1, 0], [0, 1, 0, -1]

# 訪れたかどうかの情報を持つ bool 値
is_visited = [ [False] * w for _ in range(h) ]

q = deque()

for x in range(w):
    for y in range(h):
        if a[y][x] == "#":
            q.append( (x, y, 0) )
            is_visited[y][x] = True

ans = 0

while q:
    # queue の先頭を取り出す
    x, y, dist = q.popleft()
    is_visited[y][x] = True

    ans = max(ans, dist)
    # 4方向にループ
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]

        if nx < 0 or w <= nx or ny < 0 or h <= ny:
            continue
        # is_visited == True <=> そこは黒マス
        if is_visited[ny][nx]:
            continue

        q.append( (nx, ny, dist+1) )

print(ans)

### FIXED CODE ###

# coding: utf-8
#!/usr/bin/env python3

from collections import deque

# input
h, w = map(int, input().split())
a = [ list(input()) for _ in range(h) ]

# direction vector
dx, dy = [1, 0, -1, 0], [0, 1, 0, -1]

# 訪れたかどうかの情報を持つ bool 値
is_visited = [ [False for _ in range(w)] for __ in range(h) ]

q = deque()

for x in range(w):
    for y in range(h):
        if a[y][x] == "#":
            q.append( (x, y, 0) )
            is_visited[y][x] = True

while q:
    # queue の先頭を取り出す
    x, y, cnt = q.popleft()
    # 4方向にループ
    for i in range(4):
        nx, ny = x + dx[i], y + dy[i]

        if nx < 0 or w <= nx or ny < 0 or h <= ny or is_visited[ny][nx]:
            continue

        q.append( (nx, ny, cnt+1) )
        is_visited[ny][nx] = True

print(cnt)
### BUGGY CODE ###

#TLE
K,Q = map(int,input().split())
D = list(map(int,input().split()))
NXM = [tuple(map(int,input().split())) for i in range(Q)]

for N,X,M in NXM:
    ds = [d%M for d in D]
    a,b = divmod(N-1,K)
    s = 0
    z = 0
    for i,d in enumerate(ds):
        if i==b:
            sb = s
            zb = z
        if d==0:
            z += 1
        s += d
    x = (X + s*a + sb)//M - X//M
    y = z*a + zb
    print(N-1-x-y)

### FIXED CODE ###

K,Q = map(int,input().split())
D = list(map(int,input().split()))
NXM = [tuple(map(int,input().split())) for i in range(Q)]

for N,X,M in NXM:
    ds = [d%M for d in D]
    a,b = divmod(N-1,K)
    s = 0
    z = 0
    for i,d in enumerate(ds):
        if i==b:
            sb = s
            zb = z
        if d==0:
            z += 1
        s += d
    x = (X + s*a + sb)//M - X//M
    y = z*a + zb
    print(N-1-x-y)
### BUGGY CODE ###

def StrConv(S, T, F, C):
  ans = ''
  if T == '1':
    ans = S[::-1]
  else:
    if F == '1':
      ans = C + S
    else:
      ans = S + C
  return ans
  
#パラメータの取得
S = input()
Q = int(input())

#処理実体処理
for i in range(Q):
  tmp = str(input())
  #Q各行の値を、T, F, Cに基づいて処理
  if tmp[0] == '1':
    T = tmp[0]
    F = '-'
    C = '-'
  else:
    T, F, C = tmp.split(' ')
  
  #変換処理本体
  S = StrConv(S, T, F, C)
  
print(S)

### FIXED CODE ###

#パラメータの取得
S = input()
Q = int(input())
cnt = 0   #反転回数カウンタ
head = ''
tail = ''

#処理実体処理
for i in range(Q):
  tmp = str(input())
  #Q各行の値を、T, F, Cに基づいて処理
  if tmp[0] == '1':
    cnt += 1
  else:
    T, F, C = tmp.split(' ')
    if cnt % 2 == 1:
      if F == '1':
        tail = tail + C
      else:
        head = C + head
    else:
      if F == '1':
        head = C + head
      else:
        tail = tail + C
    #cnt = 0
  #print('head=',head)
  #print('tail=',tail)

#print('cnt=',cnt)
if cnt % 2 == 0:
  ans = head + S + tail
else:
  ans = tail[::-1] + S[::-1] + head[::-1]
print(ans)
### BUGGY CODE ###

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

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


for i in range(m):
    j = a.index(max(a))
    a[j] = int(max(a)/2)

print(sum(a))


### FIXED CODE ###

import heapq
n,m = map(int,input().split())

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


for i in range(n):
    a[i] = (-1) * a[i]

heapq.heapify(a)

for i in range(m):
    d = heapq.heappop(a)
    heapq.heappush(a,d/2)

ans = 0
for i in range(n):
    ans += int(-a[i])

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

import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

debug = True
debug = False

def dprint(*objects):
    if debug == True:
        print(*objects)

def main():
    s = SI()
    t = SI()

    d = {}
    for i, c in enumerate(s):
        if c not in d.keys():
            d[c] = [i]
        else:
            d[c].append(i)

    def init_didx():
        didx = {}
        for k in d.keys():
            didx[k] = 0
        return didx

    didx = init_didx()
    for c in set(t):
        if c not in d.keys():
            print(-1)
            return

    dprint(d)
    dprint(didx)

    import bisect

    i_loop = 0
    currentidx = -1
    for c in t:
        # 次の文字を読む
        poslist = d[c]

        if len(poslist) <= didx[c]:
            # 文字を取り切っている場合は次のループに
            i_loop += 1
            didx = init_didx()
            currentidx = poslist[0]
            didx[c] += 1
        else:
            # とり切っていない場合
            posidx = bisect.bisect_right(poslist[didx[c]:], currentidx)

            if posidx >= len(poslist[didx[c]:]):
                # とれないなら次のループでとったことにする
                i_loop += 1
                didx = init_didx()
                currentidx = poslist[0]
                didx[c] += 1
            else:
                # 同じループでとれるなら取って、現在位置を更新
                pos = poslist[didx[c]:][posidx]
                currentidx = pos
                didx[c] = posidx + 1

        dprint(c, i_loop, pos, currentidx)
        dprint(didx)

    print(len(s)*i_loop + currentidx + 1)

main()

### FIXED CODE ###

import sys
def SI(): return sys.stdin.readline().strip()

def main():
    s = SI()
    t = SI()

    import collections
    d = collections.defaultdict(list)
    for i, c in enumerate(s):
        d[c].append(i)

    keys = d.keys()
    for c in set(t):
        if c not in keys:
            print(-1)
            return

    import bisect

    i_loop = 0
    currentidx = -1
    for c in t:
        # 次の文字を読む
        poslist = d[c]

        # とり切っていない場合
        posidx = bisect.bisect_right(poslist, currentidx)

        if posidx >= len(poslist):
            # とれないなら次のループでとったことにする
            i_loop += 1
            currentidx = poslist[0]
        else:
            # 同じループでとれるなら取って、現在位置を更新
            pos = poslist[posidx]
            currentidx = pos

    print(len(s)*i_loop + currentidx + 1)

main()
### BUGGY CODE ###

from itertools import accumulate


def dfs(l, r):
    if memo[l][r]:
        return dp[l][r]
    for i in range(l, r):
        dp[l][r] = min(max(0, dp[l][r]), dfs(l, i) + dfs(i + 1, r) + acc_A[r] - acc_A[l - 1])
    return dp[l][r]


n = int(input())
A = list(map(int, input().split()))
acc_A = [0] + list(accumulate(A))

INF = 10 ** 18
dp = [[INF] * (n + 1) for _ in range(n + 1)]
for i in range(1, n + 1):
    dp[i][i] = 0
memo = [[False] * (n + 1) for _ in range(n + 1)]


print(dfs(1, n))


### FIXED CODE ###

from itertools import accumulate


def dfs(l, r):
    if memo[l][r]:
        return dp[l][r]
    for i in range(l, r):
        dp[l][r] = min(max(0, dp[l][r]), dfs(l, i) + dfs(i + 1, r) + acc_A[r] - acc_A[l - 1])
    memo[l][r] = True
    return dp[l][r]


n = int(input())
A = list(map(int, input().split()))
acc_A = [0] + list(accumulate(A))

INF = 10 ** 18
dp = [[INF] * (n + 1) for _ in range(n + 1)]
memo = [[False] * (n + 1) for _ in range(n + 1)]
for i in range(1, n + 1):
    dp[i][i] = 0
    memo[i][i] = True


print(dfs(1, n))
### BUGGY CODE ###

n = int(input())
s = [input() for i in range(n)]
t = set(s)
ma = 0
tmp = []
for i in t:
    if ma < s.count(i):
        ma = s.count(i)
for i in t:
    if ma == s.count(i):
        tmp.append(i)
tmp.sort()
#print()
for i in tmp:
    print(i)


### FIXED CODE ###

n = int(input())
s = [input() for i in range(n)]
s.sort()
max = 0
count = 1
for i in range(1,n):
    if s[i] == s[i-1]:
        count+=1
    else:
        if max < count:
            max = count
            count=1
        else:
            count=1
if max < count:
    max = count
count = 1
ans = []
for i in range(1,n):
    if s[i] == s[i-1]:
        count+=1
    else:
        if max == count:
            ans.append(s[i-1])
            count = 1
        else:
            count = 1
if max == count:
    ans.append(s[n-1])
for i in range(len(ans)):
    print(ans[i])
### BUGGY CODE ###

N, A, B, C = [int(a) for a in input().split(" ")]
ls = [int(input()) for i in range(N)]
tab = [([float("inf")]*N, ref) for ref in (A, B, C)]
INF = float("inf")
from heapq import heappush, heappop, heapreplace
maxtarg = max(A, B, C)

# O(n)
def solve(ls):
    n = len(ls)
    qs = [[(-INF, -1)]*3 for i in range(3)]
    # 3*n times
    for ref, q in zip((A, B, C), qs):
        for i in range(n):
            val = -abs(ref-ls[i])
            if q[0][0] < val:
                heapreplace(q, (val, i))
    ans = INF
    # 9 times
    for a, i in qs[0]:
        for b, j in qs[1]:
            for c, k in qs[2]:
                if i!=j and j!=k and k!=i:
                    ans = min(ans, -(a+b+c))
    return ans

def dfs(ls):
    n = len(ls)
    if n < 3: return INF
    ans = solve(ls)
    for i in range(n-1):
        for j in range(i+1,n):
            tmp = ls[i]+ls[j]
            if min(maxtarg-ls[i], maxtarg-ls[j]) < 10:
                continue
            ls2 = ls[:i]+ls[i+1:]
            ls2[j-1] = tmp
            ans = min(ans, 10+dfs(ls2))
    return ans

print(dfs(ls))


### FIXED CODE ###

N, A, B, C = [int(a) for a in input().split(" ")]
ls = [int(input()) for i in range(N)]
tab = [([float("inf")]*N, ref) for ref in (A, B, C)]
INF = float("inf")
from heapq import heappush, heappop, heapreplace
maxtarg = max(A, B, C)

# O(n)
def solve(ls):
    n = len(ls)
    qs = [[(-INF, -1)]*3 for i in range(3)]
    # 3*n times
    for ref, q in zip((A, B, C), qs):
        for i in range(n):
            val = -abs(ref-ls[i])
            if q[0][0] < val:
                heapreplace(q, (val, i))
    ans = INF
    # 9 times
    for a, i in qs[0]:
        for b, j in qs[1]:
            for c, k in qs[2]:
                if i!=j and j!=k and k!=i:
                    ans = min(ans, -(a+b+c))
    return ans
dic = {}
def dfs(ls):
    key = tuple(sorted(ls))
    if key in dic:
        return dic[key]
    n = len(ls)
    if n < 3: return INF
    ans = solve(ls)
    for i in range(n-1):
        for j in range(i+1,n):
            tmp = ls[i]+ls[j]
            if min(maxtarg-ls[i], maxtarg-ls[j]) < 10:
                continue
            ls2 = ls[:i]+ls[i+1:]
            ls2[j-1] = tmp
            ans = min(ans, 10+dfs(ls2))
    dic[key] = ans
    return ans

print(dfs(ls))
### BUGGY CODE ###

N=int(input())
S=[str(i) for i in input()]
l=[]
for i in range(10):
  for j in range(10):
    for k in range(10):
      l.append(str(i)+str(j)+str(k))

ans=0
for i in range(len(l)):
  now=l[i]
  j=0
  for s in range(N):
    if S[s]==now[j]:
      j+=1

    if j==3:
      ans+=1
      break
print(ans)

### FIXED CODE ###

N=int(input())
S=input()
ans=0

for i in range(10):
  x=S.find(str(i),0)
  if x!=-1:
    for j in range(10):
      y=S.find(str(j),x+1)
      if y!=-1:
        for k in range(10):
          z=S.find(str(k),y+1)
          if z!=-1:
            ans+=1

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

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

def gcd(a, b):
    while b:
        a, b = b, a%b
    return a

def gcd_of_arr(arr):
    cd = arr[0]
    for i in range(1,len(arr)):
        if cd==1:
            return cd
        cd = gcd(arr[i], cd)
    return cd

ans = 0
for i in range(N):
    tmp = gcd_of_arr(A[0:i] + A[i+1:N])
    ans = max(ans, tmp)

print(ans)


### FIXED CODE ###

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

def gcd(a, b):
    while b:
        a, b = b, a%b
    return a

L, R = [0]*(N+1), [0]*(N+1)
for i in range(1,N+1):
    L[i] = gcd(L[i-1], A[i-1])
    R[N-i] = gcd(R[N+1-i], A[N-i])

ans = 0
for i in range(N):
    ans = max(ans, gcd(L[i], R[i+1]))

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

n = int(input())
now = (0,0)

for _ in range(n):
    t,a = map(int,input().split())

    i=1
    while True:
        if t*i>=now[0] and a*i>=now[1]:
            now = (t*i,a*i)
            break
        i +=1

print(sum(now))

### FIXED CODE ###

n = int(input())
nt  =0
na = 0
for _ in range(n):
    t,a = map(int,input().split())
    if nt==0 and na==0:
        nt =t
        na =a
        continue
    if na <= nt*(a/t):
        mod = nt%t
        if not mod==0:
            mod = t - mod
            nt = nt + mod
            na = a*nt//t
        else:
           nt = nt
           na = a*nt//t
    else:
        mod = na%a
        if not mod==0:
            mod = a - mod
            na = na + mod
            nt = t*na//a
        else:
           na = na
           nt = na*t//a 
        

print(na+nt)
### BUGGY CODE ###

S = input()
Q = int(input())
T = [input() for i in range(Q)]

left = ""
right = ""

for i in range(Q):
  s = T[i]
  if(int(s[0]) == 2):
    if(int(s[2]) == 1):
      left = s[4] + left
    else:
      right = right + s[4]
  else:
    S = left + S + right
    S = S[::-1]
    left = ""
    right = ""
S = left + S + right

print(S)

### FIXED CODE ###

from collections import deque
S = input()
Q = int(input())
T = [input() for i in range(Q)]

d =deque(S)

count = True

for i in range(Q):
  t = T[i]
  if(int(t[0]) == 1):
    count = not count
  else:
    if(int(t[2]) == 1):
      if(count):
        d.appendleft(t[4])
      else:
        d.append(t[4])
    else:
      if(count):
        d.append(t[4])
      else:
        d.appendleft(t[4])

ans = list(d)

if(count):
  print("".join(ans))
else:
  print("".join(ans[::-1]))
### BUGGY CODE ###

n, m, l = map(int, input().split())
mw =[[float('inf')] * n for _ in range(n)]
for _ in range(m):
    a, b, c = map(int, input().split())
    mw[a-1][b-1] = c
    mw[b-1][a-1] = c

def warshall_floyd(d): # d: 隣接行列
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j], d[i][k] + d[k][j])
    return d

d = warshall_floyd(mw)
mw =[[float('inf')] * n for _ in range(n)]
for i in range(n):
    for j in range(n):
        if d[i][j] <= l:
            mw[i][j] = 1
dd = warshall_floyd(mw)

q = int(input())
for _ in range(q):
    s, t = map(int, input().split())
    ans = dd[s-1][t-1]-1
    if ans > 10 ** 10:
        print(-1)
    else:
        print(ans)

### FIXED CODE ###

import sys
input = sys.stdin.readline

n, m, l = map(int, input().split())
mw =[[float('inf')] * n for _ in range(n)]
for _ in range(m):
    a, b, c = map(int, input().split())
    mw[a-1][b-1] = c
    mw[b-1][a-1] = c

def warshall_floyd(d): # d: 隣接行列
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j], d[i][k] + d[k][j])
    return d

d = warshall_floyd(mw)
mw =[[float('inf')] * n for _ in range(n)]
for i in range(n):
    for j in range(n):
        if d[i][j] <= l:
            mw[i][j] = 1
dd = warshall_floyd(mw)

q = int(input())
for _ in range(q):
    s, t = map(int, input().split())
    ans = dd[s-1][t-1]-1
    if ans > 10 ** 10:
        print(-1)
    else:
        print(ans)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**5)
input = lambda: sys.stdin.readline().strip()

def m():
    n, q = map(int, input().split())
    tree = [[] for _ in range(n + 1)]
    for i in range(1, n):
        a, b = map(int, input().split())
        tree[a].append(b)
        tree[b].append(a)
    counter = [0 for _ in range(n + 1)]
    for _ in range(q):
        p, x = map(int, input().split())
        counter[p] += x

    ans = [0] * (n + 1)
    visited = []

    def dfs(root, point):
        point += counter[root]
        ans[root] = point
        visited.append(root)
        for j in tree[root]:
            if j in visited:
                continue
            dfs(j, point)
        return

    dfs(1, 0)
    for i in range(1, n + 1):
        print(ans[i], '', end='')
    return ans


m()


### FIXED CODE ###

import queue
import sys
def input(): return sys.stdin.readline().rstrip()

def m():
    n, q = map(int, input().split())
    tree = [[] for _ in range(n + 1)]
    for i in range(1, n):
        a, b = map(int, input().split())
        tree[a].append(b)
        tree[b].append(a)
    counter = [0] * (n + 1)
    for _ in range(q):
        p, x = map(int, input().split())
        counter[p] += x

    que = queue.Queue()
    visited = [False] * (n+1)

    def bfs(root, base):
        base += counter[root]
        visited[root] = True
        for e in tree[root]:
            if visited[e]:
                continue
            que.put(e)
            counter[e] += base

        while not que.empty():
            x = que.get()
            point = counter[x]
            visited[x] = True
            for e in tree[x]:
                if visited[e]:
                    continue
                que.put(e)
                counter[e] += point

    bfs(1, 0)
    print(*counter[1:])
    return


m()