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

H, W = map(int, input().split())
S = [0]*(H+2)
S[0] = '*'*(W+2)
S[H+1] = '*'*(W+2)
for h in range(1,H+1):
  S[h] = '*' + input() + '*'

import sys
sys.setrecursionlimit(10**8)

bw = ['#','.']

visited = [[False]*(W+2) for h in range(H+2)]
ans = 0
dh = [0,1,0,-1]
dw = [1,0,-1,0]

def dfs(h,w,c):
  #print(h,w,c, bw_count)
  for i in range(4):
    h0 = h+dh[i]
    w0 = w+dw[i]
    if S[h0][w0]==bw[c^1] and visited[h0][w0]==False:
      visited[h0][w0]=True
      bw_count[c^1]+=1
      dfs(h0,w0,c^1)
  
for h in range(H+2):
  for w in range(W+2):
    if S[h][w]=='#' and visited[h][w]==False:
      bw_count = [1,0]
      visited[h][w]=True
      dfs(h,w,0)
      ans += bw_count[0]*bw_count[1]

print(ans)   

#print(*ans, sep='\n')


### FIXED CODE ###

H, W = map(int, input().split())
S = [0]*(H+2)
S[0] = '*'*(W+2)
S[H+1] = '*'*(W+2)
for h in range(1,H+1):
  S[h] = '*' + input() + '*'

import sys
sys.setrecursionlimit(10**8)

bw = ['#','.']

visited = [[False]*(W+2) for h in range(H+2)]
ans = 0
dh = [0,1,0,-1]
dw = [1,0,-1,0]

def dfs(h,w,c):
  #print(h,w,c, bw_count)
  for i in range(4):
    h0 = h+dh[i]
    w0 = w+dw[i]
    if S[h0][w0]==bw[c^1] and visited[h0][w0]==False:
      visited[h0][w0]=True
      bw_count[c^1]+=1
      dfs(h0,w0,c^1)
  
for h in range(H+2):
  for w in range(W+2):
    if S[h][w]=='#' and visited[h][w]==False:
      bw_count = [1,0]
      visited[h][w]=True
      dfs(h,w,0)
      ans += bw_count[0]*bw_count[1]

print(ans)   

#print(*ans, sep='\n')
### BUGGY CODE ###

n,m=map(int,input().split())
s=input()
a=0
for i in range(n+1):
  if s[i]=="1":
    a=a+1
    if a==m:
      print(-1)
      break
  else:
    a=0
if a==0:
  b=[]
  d=n
  while d>0:
    if d<=m:
      b.insert(0,d)
      d=0
    else:
      i=m
      while i>0:
        if s[d-i]=="1":
          i=i-1
        else:
          b.insert(0,str(i))
          d=d-i
          break
  for i in range(len(b)):
    print(b[i],end=" ")

### FIXED CODE ###

from collections import deque
n,m=map(int,input().split())
s=deque(input())
a=0
for i in range(n+1):
  if s[i]=="1":
    a=a+1
    if a==m:
      print(-1)
      break
  else:
    a=0
if a==0:
  b=deque([])
  d=n
  while d>0:
    if d<=m:
      b.appendleft(d)
      d=0
    else:
      i=m
      while i>0:
        if s[d-i]=="1":
          i=i-1
        else:
          b.appendleft(i)
          d=d-i
          for j in range(i):
            s.pop()
          break
  for i in range(len(b)):
    print(b[i],end=" ")
### BUGGY CODE ###

n = int(input())
A = [int(i) for i in input().split()]
out = [[sum(A[j:j + k]) for k in range(1,n-j+1,1)].count(0) for j in range(n)]
print(sum(out))

### FIXED CODE ###

from collections import Counter
n = int(input())
A = [int(i) for i in input().split()]
S = [0]*(n+1)
for i in range(n):
    S[i+1] = S[i] + A[i]
def hello(data):
    dt = Counter()
    for i in data:
        dt[str(i)] += 1
    return dt
num = hello(S)
out = [i*(i-1)//2 for i in num.values() if i>1]
print(sum(out))
### BUGGY CODE ###

a,b,m = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = [tuple(map(int,input().split())) for i in range(m)]
p = []
x = 0
for ka,ai in enumerate(a):
    for kb,bi in enumerate(b):
        for t in c:
            if t[0]==ka+1 and t[1]==kb+1:
                x = t[2]
            p.append(ai+bi-x)
print(min(p))

### FIXED CODE ###

a,b,m = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = [tuple(map(int,input().split())) for i in range(m)]
p = min(a)+min(b)
for t in c:
    p = min(p,a[t[0]-1]+b[t[1]-1]-t[2])
print(p)
### BUGGY CODE ###

val = list(map(int, input().split()))
N = int(input())
price = [v / (2 ** i)  for i, v in enumerate(val)]
sortedIndex = []
sortedPrice = sorted(price)
price2 = price.copy()
for p in sortedPrice:
    sortedIndex.append(price2.index(p))
    price2[price2.index(p)] = -1

temp = 0
ans = 0
for i in sortedIndex:
    while N > temp:
        if(N < temp + 0.25 * (2 ** i)):
            break
        temp += 0.25 * (2 ** i)
        ans += val[i]
    
print (ans)


### FIXED CODE ###

val = list(map(int, input().split()))
N = int(input())
price = [v / (2 ** i)  for i, v in enumerate(val)]
sortedIndex = []
sortedPrice = sorted(price)
price2 = price.copy()
for p in sortedPrice:
    sortedIndex.append(price2.index(p))
    price2[price2.index(p)] = -1

temp = 0
ans = 0
for i in sortedIndex:
    size  = (0.25 * (2 ** i))
    times = int(N / size)
    # print (size, times)
    N -= size * times
    ans += val[i] * times
# for i in sortedIndex:
#     while N > temp:
#         if(N < temp + 0.25 * (2 ** i)):
#             break
#         temp += 0.25 * (2 ** i)
#         ans += val[i]

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

import sys
input = sys.stdin.readline

def main():
    n = int(input())
    s = input()
    q = int(input())
    k = list(map(int,input().split()))
    k = [[x,i] for i,x in enumerate(k)]
    k.sort()

    ans = [[0,-1] for i in range(q)]
    for i in range(q):
        ans[i][1] = k[i][1]

    def search(x):
        ind = x
        m = 0
        c = 0
        count = 0
        for num,i in enumerate(k):
            while ind+count < n and count < i[0]:
                if s[ind+count] == "M":
                    m += 1
                elif s[ind+count] == "C":
                    c += 1
                    ans[num][0]+=m
                count += 1
        
    for i in range(n):
        if s[i] == "D":
            search(i)

    for i in range(1,q):
        ans[i][0] += ans[i-1][0]
    ans.sort(key=lambda x: x[1])
    for i,j in ans:
        print(i)

if __name__ == "__main__":
    main()

### FIXED CODE ###

from collections import deque
n = int(input())
s = input()
q = int(input())
k = list(map(int,input().split()))

def search(k):
    d = 0
    m = 0
    count = 0
    ans = 0
    q = deque([])
    for i in range(n):
        if s[i] == "D":
            d += 1
        elif s[i] == "M":
            m += 1
            count += d
            q.append(d)
        elif s[i] == "C":
            ans += count
        if i>= k-1:
            if s[i-k+1] == "D":
                d -= 1
                count -= m
            elif s[i-k+1] == "M":
                m -= 1
                
        
    return ans
for i in k:
    print(search(i))
### BUGGY CODE ###

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

def substract(min):
    tmp = min
    if N % K == 0:
        return 0
    while(1):
        tmp = abs(tmp - K)
        if tmp > min:
            break
        min = tmp
    return min

if abs(N-K) < N or abs(N-K) < K:
    print(substract(abs(N-K)))
elif N < K:
    print(N)
else:
    print(K)


### FIXED CODE ###

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

def substract(min):
    if N % K == 0:
        return 0
    tmp = N % K
    if abs(tmp - K) < tmp:
        return abs(tmp-K)
    return tmp
    #while(1):
    #    tmp = abs(tmp - K)
    #    if tmp > min:
    #        break
    #    min = tmp
    #return min

if abs(N-K) < N or abs(N-K) < K:
    print(substract(abs(N-K)))
elif N < K:
    print(N)
else:
    print(K)
### BUGGY CODE ###

values = [int(input()) for i in range(int(input()))]

maxv = values[1] - values[0]

for c, i in enumerate(values):
    for j in values[c + 1:]:
        maxv = max(maxv, j - i)

print(maxv)


### FIXED CODE ###

values = [int(input()) for _ in range(int(input()))]
maxv = values[1] - values[0]
minv = values[0]

for val in values[1:]:
    maxv = max(maxv, val - minv)
    minv = min(minv, val)         # 今までの最小値を保持(自分と自分を比較しない様に、最大値の評価後に計算)
print(maxv)
### BUGGY CODE ###

n = int(input())
c = str(input())
cn = [False]*n
for i in range(n):
    if c[i]=="R":
        cn[i]= True


ans = 0
k=n-1
b=0

for i in range(n):
    if b==1:
        break

    if not(cn[i]):
        for j in range(k,0,-1):

            if cn[j]:
                k =j-1
                if i < j:
                    #cn[i]=True
                    #cn[j]=False
                    ans +=1
                    break
                elif i>=j:
                    b=1
                    break
print(ans)

### FIXED CODE ###

n = int(input())
c = str(input())
cn = [False]*n
for i in range(n):
    if c[i]=="R":
        cn[i]= True


ans = 0
k=n-1
b=0

for i in range(n):
    if b==1:
        break

    if not(cn[i]):
        for j in range(k,0,-1):

            if cn[j]:
                k =j-1
                if i < j:
                    #cn[i]=True
                    #cn[j]=False
                    ans +=1
                    break
            if i>=j:
                b=1
                break
print(ans)
### BUGGY CODE ###

import sys
import fractions
N, M = map(int, input().split())
S = input()
T = input()

g = fractions.gcd(N, M)
L = N * M // g

ss = L // N
ts = L // M
for i in range(L):
  if i % ss == 0 and i % ts == 0:
    if S[i//ss] != T[i//ts]:
      print(-1)
      sys.exit()
print(L)

### FIXED CODE ###

import sys
import fractions
N, M = map(int, input().split())
S = input()
T = input()

g = fractions.gcd(N, M)
L = N * M // g

ss = L // N
ts = L // M
for i in range(N):
  if (i * ss) % ts == 0:
    j = (i * ss) // ts
    if S[i] != T[j]:
      print(-1)
      sys.exit()

for i in range(M):
  if (i * ts) % ss == 0:
    j = (i * ts) // ss
    if T[i] != S[j]:
      print(-1)
      sys.exit()

print(L)
### BUGGY CODE ###

import sys
def input(): return sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)

INF = 1001001001
N, X, Y = map(int, input().split())
d = [[INF]*N for _ in range(N)]
for i in range(N):
    d[i][i] = 0
for i in range(N-1):
    d[i][i+1] = 1
    d[i+1][i] = 1
d[X-1][Y-1] = 1
d[Y-1][X-1] = 1

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])

ans = [0]*(N-1)
for i in range(N-1):
    for j in range(i+1,N):
        ans[d[i][j]-1] += 1
for i in range(N-1):
    print(ans[i])


### FIXED CODE ###

import sys
def input(): return sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)

N, X, Y = map(int, input().split())

ans = [0]*(N-1)
branch1 = X-1
branch2 = N-Y
cycle = Y-X+1
for i in range(cycle//2):   # 輪の1/2まで両側(2通り）で行ける
    ans[i] += cycle
if cycle%2==0:     # 輪は偶数（両方から同じ距離がある）
    ans[cycle//2-1] -= cycle//2
ans[0] -= 1

for i in range(branch1):
    for j in range(cycle//2):
        if cycle%2==0 and j==cycle//2-1:
            ans[i+j+1] += 1
        elif j == 0:    # 直線経路は除外
            ans[i+j+1] += 1
        else:
            ans[i+j+1] += 2
for i in range(branch2):
    for j in range(cycle//2):
        if cycle%2==0 and j==cycle//2-1:
            ans[i+j+1] += 1
        elif j == 0:    # 直線経路は除外
            ans[i+j+1] += 1
        else:
            ans[i+j+1] += 2

for i in range(branch1+branch2+1):   # 直線経路
    ans[i] += branch1+branch2+1 - i

for i in range(N-1):
    print(ans[i])
### BUGGY CODE ###

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

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
ans = 1 
if 0 in a:
        print(0)
else:
        for i in range(n):
                ans = ans * a[i]
                if ans > 10**18:
                        ans = -1
                        break
        print(ans)
### BUGGY CODE ###

import heapq
X, Y, Z, K = map(int, input().split())
A = sorted([int(i) for i in input().split()], reverse=True)
B = sorted([int(i) for i in input().split()], reverse=True)
C = sorted([int(i) for i in input().split()], reverse=True)
# done = [[[True for _ in range(Z+1)] for _ in range(Y+1)] for _ in range(Z+1)]
done = []

h = []
heapq.heappush(h, (-(A[0]+B[0]+C[0]), 0, 0, 0))  # リストaのheap化
done.append((0, 0, 0))

for _ in range(K):
    p, i, j, k = heapq.heappop(h)
    # print(h)
    print(-p)

    if (i+1, j, k) not in done and i+1 < X:
        # heap化されたリストaに要素xを追加
        heapq.heappush(h, (-(A[i+1]+B[j]+C[k]), i+1, j, k))
        # print(h)
        done.append((i+1, j, k))

    if (i, j+1, k) not in done and j+1 < Y:
        # heap化されたリストaに要素xを追加
        heapq.heappush(h, (-(A[i]+B[j+1]+C[k]), i, j+1, k))
        # print(h)
        done.append((i, j+1, k))

    if (i, j, k+1) not in done and k+1 < Z:
        # heap化されたリストaに要素xを追加
        heapq.heappush(h, (-(A[i]+B[j]+C[k+1]), i, j, k+1))
        # print(h)
        done.append((i, j, k+1))


### FIXED CODE ###

import heapq
X, Y, Z, K = map(int, input().split())
A = sorted([int(i) for i in input().split()], reverse=True)
B = sorted([int(i) for i in input().split()], reverse=True)
C = sorted([int(i) for i in input().split()], reverse=True)
# done = [[[True for _ in range(Z+1)] for _ in range(Y+1)] for _ in range(Z+1)]
done = set()

h = []
heapq.heappush(h, (-(A[0]+B[0]+C[0]), 0, 0, 0))  # リストaのheap化
done.add((0, 0, 0))

for _ in range(K):
    p, i, j, k = heapq.heappop(h)
    # print(h)
    print(-p)

    if (i+1, j, k) not in done and i+1 < X:
        # heap化されたリストaに要素xを追加
        heapq.heappush(h, (-(A[i+1]+B[j]+C[k]), i+1, j, k))
        # print(h)
        done.add((i+1, j, k))

    if (i, j+1, k) not in done and j+1 < Y:
        # heap化されたリストaに要素xを追加
        heapq.heappush(h, (-(A[i]+B[j+1]+C[k]), i, j+1, k))
        # print(h)
        done.add((i, j+1, k))

    if (i, j, k+1) not in done and k+1 < Z:
        # heap化されたリストaに要素xを追加
        heapq.heappush(h, (-(A[i]+B[j]+C[k+1]), i, j, k+1))
        # print(h)
        done.add((i, j, k+1))
### BUGGY CODE ###

d_list_a = []
d_list_c = []
d_list_g = []
d_list_t = []

times = int(input())
for i in range(times):
    elm = input().split()
    if elm[0] == "insert":
        if elm[1][0] == "A":
            d_list_a.append(elm[1])
        if elm[1][0] == "C":
            d_list_c.append(elm[1])
        if elm[1][0] == "G":
            d_list_g.append(elm[1])
        else:
            d_list_t.append(elm[1])
    elif elm[0] == "find":
        if elm[1][0] =="A":
            if elm[1] in d_list_a:
                print("yes")
            else:
                print("no")
        elif elm[1][0] =="C":
            if elm[1] in d_list_c:
                print("yes")
            else:
                print("no")
        elif elm[1][0] =="G":
            if elm[1] in d_list_g:
                print("yes")
            else:
                print("no")
        elif elm[1][0] =="T":
            if elm[1] in d_list_t:
                print("yes")
            else:
                print("no")

### FIXED CODE ###

n = int(input())

d = set()
for i in range(n):
    com = input().split()
    if com[0] == 'insert':
        d.add(com[1])
    else:
        print('yes' if com[1] in d else 'no')
### BUGGY CODE ###

N, D, A = map(int, input().split())
XH = [list(map(int, input().split())) for _ in range(N)]

XH.sort()
result = 0
for i in range(N):
    if XH[i][1] < 0:
        continue
    c = (XH[i][1] + (A - 1)) // A
    result += c
    for j in range(i + 1, N):
        if XH[j][0] > XH[i][0] + 2 * D:
            break
        XH[j][1] -= c * A
print(result)


### FIXED CODE ###

N, D, A = map(int, input().split())
XH = [list(map(int, input().split())) for _ in range(N)]

XH.sort()
q = []
t = 0
result = 0
for x, h in XH:
    while True:
        if len(q) == 0:
            break
        if x <= q[0][0]:
            break
        t -= q[0][1]
        q.pop(0)
    h -= t
    if h < 0:
        continue
    c = (h + A - 1) // A
    result += c
    t += c * A
    q.append((x + 2 * D, c * A))
print(result)
### BUGGY CODE ###

#!/usr/bin/env python3
N, M = [int(str) for str in input().strip().split()]
H = [int(str) for str in input().strip().split()]
AB = [[int(str) - 1 for str in input().strip().split()] for _ in range(M)]


def solve():
    G = [[False] * N for _ in range(N)]
    for a, b in AB:
        G[a][b] = True
        G[b][a] = True

    ans = 0
    for start in range(N):
        if all([H[start] > H[next] for next in [i for i in range(N) if G[start][i]]]):
            ans += 1
    print(ans)


solve()


### FIXED CODE ###

#!/usr/bin/env python3
N, M = [int(str) for str in input().strip().split()]
H = [int(str) for str in input().strip().split()]
AB = [[int(str) - 1 for str in input().strip().split()] for _ in range(M)]


def solve():
    G = [0] * N
    for a, b in AB:
        G[a] = max(G[a], H[b])
        G[b] = max(G[b], H[a])

    ans = 0
    for i in range(N):
        if H[i] > G[i]:
            ans += 1

    print(ans)


solve()
### BUGGY CODE ###

import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n,k = LI()
    p = LI()
    for i,x in enumerate(p):
        if x%2 == 0:
            x = x//2+0.5
        else:
            x = x//2+1
        p[i] = x
    
    ans = 0
    for i in range(n-k+1):
        lst = p[i:i+k]
        # print(p,lst)
        ans = max(ans,sum(lst))
    
    print(ans)
main()            


### FIXED CODE ###

import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n,k = LI()
    p = LI()
    for i,x in enumerate(p):
        if x%2 == 0:
            x = x//2+0.5
        else:
            x = x//2+1
        p[i] = x

    nowsum = sum(p[:k])
    ans = nowsum
    for i in range(k,n):
        nowsum = nowsum - p[i-k] + p[i]
        # print(p,lst)
        ans = max(ans,nowsum)
    
    print(ans)
main()
### BUGGY CODE ###

from collections import OrderedDict
X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

"""
max_b = max(B)
max_c = max(C)
"""

summs_ab = []
summs = []
for a in A:
    for b in B:
        summs_ab.append(a+b)
summs_ab.sort(reverse=True)
summs_ab = summs_ab[0:min(3001, X*Y*Z+1)]

for c in C:
    for sab in summs_ab:
        summs.append(sab+c)
summs.sort(reverse=True)
"""
for a in A[:K]:
    if len(summs) >= K and a + max_b + max_c < summs[K-1]:
        break

    for b in B[:K]:
        if len(summs) >= K and a + b + max_c < summs[K-1]:
            break

        for c in C[:K]:
            summ = a + b + c
            if len(summs) >= K:
                min_sum = summs[K-1]    
                if summ < min_sum:
                    break
            summs.append(summ)
        summs = merge_sort(summs)
        summs.reverse()
"""
for i in range(K):
    print(summs[i])

### FIXED CODE ###

x,y,z,k = map(int,input().split())
lisa = list(map(int,input().split()))
lisb = list(map(int,input().split()))
lisc = list(map(int,input().split()))
lisa.sort(reverse=True)
lisb.sort(reverse=True)
lisc.sort(reverse=True)
an = []
ans = []
for num in lisb:
    for nu in lisa:
        an.append(num+nu)
an.sort(reverse=True)
an = an[:min(3001,x*y*z+1)]
for num in lisc:
    for nu in an:
        ans.append(num+nu)
ans.sort(reverse=True)
for i in range(k):
    print(ans[i])
### BUGGY CODE ###

INFTY = 10**9

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

dp = [INFTY] * (N+1)
dp[0] = 0
for i in range(1,N):
    j = 1
    while j <= K and i-j >= 0:
        dp[i] = min(dp[i], dp[i-j]+abs(H[i-j]-H[i]))
        j += 1

print(dp[N-1])

### FIXED CODE ###

INFTY = 10**9

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

dp = [INFTY] * N
dp[0] = 0
for i in range(1,N):
    j = 1
    while j <= K and i-j >= 0:
        dp[i] = min(dp[i], dp[i-j]+abs(H[i-j]-H[i]))
        j += 1

print(dp[N-1])
### BUGGY CODE ###

# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10000)
from collections import defaultdict, deque
def inpl(): return tuple(map(int, input().split()))
 
N, M = inpl()
Q = [inpl() for _ in range(M)]
# グループごとにトポロジカルソートをして、最後の方から遡って計算
 
tree = [[-1, 1]  for _ in range(N+1)] # [next, rank]
  
def find(i):
    if tree[i][0] == -1:
        group = i
    else:
        group = find(tree[i][0]) 
        tree[i][0] = group
    return group
  
def unite(x, y):
    px = find(x)
    py = find(y)
      
    if tree[px][1] == tree[py][1]: # rank is same
        tree[py][0] = px
        tree[px][1] += 1
    else:
        if tree[px][1] < tree[py][1]:
            px, py = py, px
        tree[py][0] = px
 
adj = [[] for _ in range(N+1)]  
 
for l, r, d in Q:
    if find(l) != find(r) or find(l) == -1:
        unite(l, r)
    adj[l].append([r, d])
    adj[r].append([l, -d])
 
 
G = set([find(n) for n in range(1, N+1)])
 
X = [-1 for _ in range(N+1)]
searched = [False for _ in range(N+1)]
res = True
 
for g in G:
    X[g] = 0
    Q = deque([g])
    searched[g] = True
    while Q:
        p = Q.popleft()
        for np, d in adj[p]:
            if searched[np]:
                if X[np] != X[p] + d:
                    Q = []
                    res = False
                    break
            else:
                X[np] = X[p] + d
                Q.append(np)
                searched[np] = True
    if not res:
        break
 
if res:
    print("Yes")
else:
    print("No")

### FIXED CODE ###

# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10000)
from collections import defaultdict, deque
def inpl(): return tuple(map(int, input().split()))
 
N, M = inpl()
Q = [inpl() for _ in range(M)]
# グループごとにトポロジカルソートをして、最後の方から遡って計算
 
tree = [[-1, 1]  for _ in range(N+1)] # [next, rank]
  
def find(i):
    if tree[i][0] == -1:
        group = i
    else:
        group = find(tree[i][0]) 
        tree[i][0] = group
    return group
  
def unite(x, y):
    px = find(x)
    py = find(y)
      
    if tree[px][1] == tree[py][1]: # rank is same
        tree[py][0] = px
        tree[px][1] += 1
    else:
        if tree[px][1] < tree[py][1]:
            px, py = py, px
        tree[py][0] = px
 
adj = [[] for _ in range(N+1)]  
 
for l, r, d in Q:
    if find(l) != find(r):
        unite(l, r)
    adj[l].append([r, d])
    adj[r].append([l, -d])
 
 
G = set([find(n) for n in range(1, N+1)])
 
X = [-1 for _ in range(N+1)]
searched = [False for _ in range(N+1)]
res = True
 
for g in G:
    X[g] = 0
    Q = deque([g])
    searched[g] = True
    while Q:
        p = Q.popleft()
        for np, d in adj[p]:
            if searched[np]:
                if X[np] != X[p] + d:
                    Q = []
                    res = False
                    break
            else:
                X[np] = X[p] + d
                Q.append(np)
                searched[np] = True
    if not res:
        break
 
if res:
    print("Yes")
else:
    print("No")