stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using pii = pair<int, int>;



const ll MOD = (ll)1e9 + 7;

int N;

int T[100010], A[100010];



int main() {

  cin.tie(0);

  ios_base::sync_with_stdio(false);

  cout << fixed << setprecision(10);

  

  cin >> N;

  for (int i = 0; i < N; i++) {

    cin >> T[i];

  }  

  for (int i = 0; i < N; i++) {

    cin >> A[i];

  }



  int a[100010], b[100010];

  fill_n((int*)a, 100010, -1);

  fill_n((int*)b, 100010, -1);



  int tmp = -1;

  for (int i = 0; i < N; i++) {

    if (T[i] == tmp) continue;

    a[i] = tmp = T[i];

  }

  tmp = -1;

  for (int i = N - 1; i >= 0; i--) {

    if (A[i] == tmp) continue;

    b[i] = tmp = A[i];

  }



  for (int i = 0; i < N; i++) {

    if (a[i] < 0 && b[i] < 0) continue;

    if (a[i] < 0 && T[i] >= A[i]) continue;

    if (b[i] < 0 && T[i] <= A[i]) continue;

    if (a[i] == b[i]) continue;

    cout << 0 << endl;

    return 0;

  }

  ll ans = 1;

  for (int i = 0; i < N; i++) {

    if (a[i] >= 0 || b[i] >= 0) continue;

    ans = ans * min(T[i], A[i]) % MOD;

  }

  cout << ans << endl;



  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.8042471443779804x):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using pii = pair<int, int>;



const ll MOD = 1e9 + 7;

int N;

int T[100010], A[100010];

int L[100010], R[100010];



int main() {

  cin.tie(0);

  ios_base::sync_with_stdio(false);

  cout << fixed << setprecision(10);

  

  cin >> N;

  for (int i = 0; i < N; i++) {

    cin >> T[i];

  }

  for (int i = 0; i < N; i++) {

    cin >> A[i];

  }



  int tmp = -1;

  for (int i = 0; i < N; i++) {

    if (T[i] != tmp) {

      tmp = T[i];

      L[i] = T[i];

    }

  }

  tmp = -1;

  for (int i = N - 1; i >= 0; i--) {

    if (A[i] != tmp) {

      tmp = A[i];

      R[i] = A[i];

    }

  }



  ll ans = 1;  

  for (int i = 0; i < N; i++) {

    if (L[i] != 0 && R[i] != 0) {

      if (L[i] != R[i]) ans = 0;

    } else if (L[i] == 0 && R[i] != 0) {

      if (T[i] < A[i]) ans = 0;

    } else if (L[i] != 0 && R[i] == 0) {

      if (T[i] > A[i]) ans = 0;

    } else {

      ans *= min(T[i], A[i]);

      ans %= MOD;

    }

  }

  cout << ans << endl;



  return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



typedef long long ll;

typedef unsigned long long ull;

#define FOR(i,a,b) for(int i=(a);i<(int)(b);i++)

#define rep(i,n) FOR(i,0,n)

#define ALL(x) (x).begin(),(x).end()

#define RALL(x) (x).rbegin(),(x).rend()

#define SZ(x) (int)(x).size()

#define ZERO(a) memset(a,0,sizeof(a))

#define dbg(x) cout<<#x<<": "<<(x)<<"\n"

#define pb push_back

#define eb emplace_back

#define mp make_pair



const int mod = 1e9 + 7;

int n;

int x[114514], y[114514];



void solve() {

    vector<int> t(n), a(n);

    for(auto& i : t) scanf("%d",&i);

    for(auto& i : a) scanf("%d",&i);

    if(t.back() != a[0]) {

        puts("0");

        return ;

    }

    x[0] = x[n-1] = 1;

    FOR(i,1,n-1) {

        if(t[i-1] != t[i]) {

            x[i] = 1;

            y[i] = t[i];

        } else {

            x[i] = t[i];

            y[i] = -1;

        }

    }



    for(int i=n-2; i>=0; i--) {

        if(y[i]!=-1 and a[i] < y[i]) {

            puts("0");

            return ;

        }

        if(a[i] > a[i+1])

            x[i] = 1;

        else

            x[i] = min(x[i], a[i]);

    }



    ll ans = 1;

    rep(i,n) {

        ans = (ans * x[i]) % mod;

    }



    cout << ans << "\n";

}





int main() {

    while(cin>>n) {

        ZERO(x); ZERO(y);

        solve();

    }

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.9141092197393361x):
#include "bits/stdc++.h"

using namespace std;



typedef long long ll;

typedef vector<int> vi;

typedef vector<vi> mat;

#define FOR(i,a,b) for(int i=(a);i<(int)(b);++i)

#define rep(i,n) FOR(i,0,n)

#define mset(a,x) memset(a,x,sizeof(a))



const int MOD = int(1e9 + 7);

ll n;



int main() {

    cin >> n;

    vector<ll> t(n), a(n);

    rep(i, n) scanf("%lld", &t[i]);

    rep(i, n) scanf("%lld", &a[i]);



    if(t[n - 1] != a[0]) {

        puts("0"); return 0;

    }



    if(n == 1) {

        puts(t[0] == a[0] ? "1" : "NO");

        return 0;

    }



    vector<ll> mi(n, 1145141919810);

    mi[0] = mi[n - 1] = 1;

    FOR(i, 1, n-1) {

        if(t[i] != t[i - 1]) {

            mi[i] = 1;

        }

    }



    for(int i = n - 2; i >= 1; --i) {

        if(mi[i] == 1 && t[i] > a[i]) {

            puts("0"); return 0;

        }

        if(a[i] != a[i + 1]) mi[i] = 1;

        if(mi[i] == 1) continue;

        mi[i] = min(t[i], a[i]);

    }



    ll ans = 1;

    rep(i, n) {

        ans *= mi[i];

        ans %= MOD;

    }



    cout << ans << endl;



    return 0;

}
// Slower C++ solution:
#include<iostream>

#include<string.h>

#include<string>

#include<cstdio>

#include<algorithm>

#include<stack>

#include<queue>

#include<vector>

#include<cmath>

#include<utility>

#include<set>

#define ll long long int

#define ld long double

#define INF 1000000000

#define EPS 0.0000000001

#define MOD 1000000007

#define rep(i,n) for(i=0;i<n;i++)

using namespace std;

typedef pair<int, int> pii;



int main()

{

  int n;

  ll t[100000];

  ll a[100000];

  pii tr[100000];//t range

  pii ar[100000];//a range

  int i,j;

  ll ans=1LL;



  cin>>n;



  cin>>t[0];

  tr[0].first=tr[0].second=t[0];



  for(i=1;i<n;i++){

    cin>>t[i];

    tr[i].second=t[i];

    if(t[i]>t[i-1])

      tr[i].first=t[i];

    else

      tr[i].first=1;

  }



  rep(i,n){

    cin>>a[i];

    ar[i].second=a[i];

  }



  ar[n-1].first=ar[n-1].second=a[n-1];

  for(i=n-2;i>=0;i--)

    if(a[i]>a[i+1])

      ar[i].first=a[i];

    else

      ar[i].first=1;



  rep(i,n){

    if(ar[i].first<=tr[i].first && tr[i].second<=ar[i].second)//a-t-t-a

      ans=(ans*(tr[i].second-tr[i].first+1))%MOD;

    else if(tr[i].first<=ar[i].first && ar[i].second<=tr[i].second)//t-a-a-t

      ans=(ans*(ar[i].second-ar[i].first+1))%MOD;

    else if(ar[i].first<=tr[i].first && tr[i].first<=ar[i].second)//a-t-a-t

      ans=(ans*(ar[i].second-tr[i].first+1))%MOD;

    else if(tr[i].first<=ar[i].first && ar[i].first<=tr[i].second)//t-a-t-a

      ans=(ans*(tr[i].second-ar[i].first+1))%MOD;

    else{

      cout<<"0"<<endl;

      return 0;

    }

  }

  cout<<ans<<endl;

}




// Faster C++ solution for the same problem (measured speedup: 2.6363853538434836x):
#include<iostream>

#include<algorithm>

#include<cstdio>

#include<cmath>

#include<math.h>

#include<string>

#include<string.h>

#include<stack>

#include<queue>

#include<vector>

#include<utility>

#include<set>

#include<map>

#include<stdlib.h>

#include<iomanip>



using namespace std;



#define ll long long

#define ld long double

#define EPS 0.0000000001

#define INF 1e9

#define MOD 1000000007

#define rep(i,n) for(i=0;i<n;i++)

#define loop(i,a,n) for(i=a;i<n;i++)

#define all(in) in.begin(),in.end()

#define shosu(x) fixed<<setprecision(x)



typedef vector<int> vi;

typedef pair<int,int> pii;



int main(void) {

  int i,j;

  int n;

  cin>>n;

  vi a(n),h(n);

  vector<bool> f(n,false);

  rep(i,n)cin>>a[i];//left

  rep(i,n){

    h[i]=a[i];

    if(i==0 || a[i]!=a[i-1])f[i]=true;

  }

  rep(i,n)cin>>a[i];//right

  for(i=n-1;i>=0;i--){

    if(f[i]){

      if(i==n-1 || a[i]!=a[i+1]){

	if(h[i]!=a[i]){

	  cout<<0<<endl;

	  return 0;

	}

      }else{

	if(h[i]>a[i]){

	  cout<<0<<endl;

	  return 0;

	}

      }

    }else{

      if(i==n-1 || a[i]!=a[i+1]){

	if(h[i]<a[i]){

	  cout<<0<<endl;

	  return 0;

	}

	h[i]=a[i];

	f[i]=true; 

      }else{

	if(h[i]>a[i])h[i]=a[i];

      }

    } 

  }



  ll ans=1;

  rep(i,n)if(f[i]==false)ans=(ans*h[i])%MOD;

  cout<<ans<<endl;



}
// Slower C++ solution:
#include <iostream>

#include <cstdio>

#include <cstring>

#include <vector>

using namespace std;

const int N = 1e5+10;

int n,m;

int dis[N];

bool vis[N];

vector<pair<int,int> >E[N];

bool dfs(int u){

    vis[u] = 1;

    vector<pair<int,int> >::iterator it;

    for(it = E[u].begin(); it != E[u].end(); it++){

        int v = (*it).first;

        int d = (*it).second;

        if(!vis[v]){

            dis[v] = dis[u] + d;

            if(!dfs(v))

                return false;

        }

        else if(dis[u]+d!=dis[v])

            return false;

    }

    return true;

}

int main(){

    cin >> n >> m;

    int u,v,w;

    int i;

    for(i = 1; i <= m; i++){

        cin >> u >> v >> w;

        E[u].push_back(make_pair(v,w));

        E[v].push_back(make_pair(u,-w));

    }

    for(i = 1; i <= n; i++){

        if(!vis[i]){

            if(!dfs(i)){

                cout << "No" << endl;

                return 0;

            }

        }

    }

    cout << "Yes" << endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 3.482877878839071x):
#include "bits/stdc++.h"

using namespace std;

typedef long long ll;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;

const int INF = 1e9;

const ll LINF = 1e18;



/*

<url:https://arc090.contest.atcoder.jp/tasks/arc090_b>

問題文============================================================

 x 軸上に N 人の人が立っています。 人 i の位置を xi とします。

 任意の i に対して、xi は 0 以上 109 以下の整数です。

 同じ位置に複数の人が立っていることもありえます。

 

 これらの人の位置に関する情報が M 個与えられます。

 このうち i 個めの情報は (Li,Ri,Di) という形をしています。

 この情報は、人 Ri は人 Li よりも距離 Di だけ右にいること、

 すなわち、xRi−xLi=Di が成り立つことを表します。

 

 これら M 個の情報のうちのいくつかに誤りがある可能性があることがわかりました。

 与えられる M 個すべての情報と矛盾しないような値の組 (x1,x2,…,xN) が存在するかどうか判定してください。

=================================================================



解説=============================================================



 互いに到達できる頂点集合の中で、一つの頂点を基準位置として、

 グラフのように各頂点を辿っていきながら矛盾がないかどうか確認していけば良い

 

================================================================

*/

int N,M;

struct edge{

    int L,R,D;

    edge(){}

    edge(int L,int R,int D):L(L),R(R),D(D){}

};

vector<vector<edge>> G;



bool dfs(int n,bool first,vector<int>& checked,vector<ll>& loc){

    if(first) loc[n] = 0;

    checked[n] = 1;

    for(edge& e:G[n]){

        if(checked[e.R] == 0){

            loc[e.R] = loc[e.L] + e.D;

            if(!dfs(e.R,false,checked,loc))return false;

        }else{

            if(loc[e.R] != loc[e.L] + e.D) return false;

        }

    }

    return true;

}

int main(void) {

	cin.tie(0); ios::sync_with_stdio(false);

    cin >> N >> M;

    G.resize(N);

    for(int i = 0; i < M;i++){

        int L,R,D; cin >> L >> R >> D;

        L--; R--;

        G[L].push_back(edge(L,R,D));

        G[R].push_back(edge(R,L,-D));

    }

    

    vector<int> checked(N,0);

    vector<ll> loc(N,LINF);

    for(int i = 0; i < N;i++){

        if(checked[i]) continue;

        if(!dfs(i,true,checked,loc)){

            cout << "No" << endl;

            return 0;

        }

    }

    cout << "Yes" << endl;

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <cstdio>

#include <cstring>

#include <vector>

using namespace std;

const int N = 1e5+10;

int n,m;

int dis[N];

bool vis[N];

vector<pair<int,int> >E[N];

bool dfs(int u){

    vis[u] = 1;

    vector<pair<int,int> >::iterator it;

    for(it = E[u].begin(); it != E[u].end(); it++){

        int v = (*it).first;

        int d = (*it).second;

        if(!vis[v]){

            dis[v] = dis[u] + d;

            if(!dfs(v))

                return false;

        }

        else if(dis[u]+d!=dis[v])

            return false;

    }

    return true;

}

int main(){

    cin >> n >> m;

    int u,v,w;

    int i;

    for(i = 1; i <= m; i++){

        cin >> u >> v >> w;

        E[u].push_back(make_pair(v,w));

        E[v].push_back(make_pair(u,-w));

    }

    for(i = 1; i <= n; i++){

        if(!vis[i]){

            if(!dfs(i)){

                cout << "No" << endl;

                return 0;

            }

        }

    }

    cout << "Yes" << endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 3.587158531885471x):
#include<cstdio>

#include<cstring>

#include<algorithm>

#include<iostream>

#include<string>

#include<vector>

#include<stack>

#include<bitset>

#include<cstdlib>

#include<cmath>

#include<set>

#include<list>

#include<deque>

#include<map>

#include<queue>

using namespace std;

typedef long long ll;

const double PI = acos(-1.0);

const double eps = 1e-6;

const int INF = 0x3f3f3f3f;

const int MAXN = 1e5+10;

int f[MAXN];  

int d[MAXN]; 



void init(int N)

{    

    for(int n = 0; n <N; n++){ 

		f[n]=n;       

    }   

}    



int find(int x)  

{  

    if(f[x]==x)  return x;

    else{

		int r=find(f[x]);  

    	d[x]=d[f[x]]+d[x]; 

    	return f[x]=r;

	}

}  

  

  

int union_nodes(int x,int y,int z)  

{  

    int fx=find(x);  

    int fy=find(y);  

    if(fx==fy)  return z==d[y]-d[x];

    else{

    	f[fy]=fx;  

    	d[fy]=d[x]+z-d[y];  

    	return 1;  

	}

}  

  

  

int main()  

{  

    int N,M;  

    int flag=1;  

    scanf("%d %d",&N,&M);  

   	init(N);

   	

    for(int m=0;m<M;m++){  

        int x,y,z;  

        scanf("%d %d %d",&x,&y,&z); 

    	if(flag!=0){ flag=union_nodes(x,y,z);}

    }  

  

    if(flag)  printf("Yes\n"); 

    else  printf("No\n");

    

	return 0;  

}
// Slower C++ solution:
#include <iostream>

#include <cstdio>

#include <cstring>

#include <vector>

using namespace std;

const int N = 1e5+10;

int n,m;

int dis[N];

bool vis[N];

vector<pair<int,int> >E[N];

bool dfs(int u){

    vis[u] = 1;

    vector<pair<int,int> >::iterator it;

    for(it = E[u].begin(); it != E[u].end(); it++){

        int v = (*it).first;

        int d = (*it).second;

        if(!vis[v]){

            dis[v] = dis[u] + d;

            if(!dfs(v))

                return false;

        }

        else if(dis[u]+d!=dis[v])

            return false;

    }

    return true;

}

int main(){

    cin >> n >> m;

    int u,v,w;

    int i;

    for(i = 1; i <= m; i++){

        cin >> u >> v >> w;

        E[u].push_back(make_pair(v,w));

        E[v].push_back(make_pair(u,-w));

    }

    for(i = 1; i <= n; i++){

        if(!vis[i]){

            if(!dfs(i)){

                cout << "No" << endl;

                return 0;

            }

        }

    }

    cout << "Yes" << endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 3.5849906004337564x):
#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <math.h>

#include <time.h>

#include <iostream>

#include <algorithm>

#include <string>

#include <set>

#include <map>

#include <queue>

#include <stack>

#include <bitset>

#include <vector>

using namespace std;



#define bll long long

#define dou double

#define For(i,a,b) for (int i=(a),_##i=(b); i<=_##i; ++i)

#define Rof(i,a,b) for (int i=(a),_##i=(b); i>=_##i; --i)

#define Mem(a,b) memset(a,b,sizeof(a))

#define Cpy(a,b) memcpy(a,b,sizeof(b))



const int maxn=1e5+100;

int N,M,u[maxn],link[maxn];

int dlt[maxn];



int get_u(int x,int &ret)

{

    if (x==u[x]) return x;

    u[x]=get_u(u[x],ret);

    ret+=dlt[x];

    dlt[x]=ret;

    return u[x];

}



bool check(int x,int y,int d)

{

    int rx=0,ry=0;

    int a=get_u(x,rx),b=get_u(y,ry);

    if (a==b) return (dlt[y]-dlt[x]==d);

    if (link[a]<link[b]) swap(a,b),swap(x,y),d=-d;

    u[b]=a;

    link[a]+=(link[a]==link[b]);

    dlt[b]=dlt[x]-dlt[y]+d;

    return 1;

}



int main()

{

    for (; scanf("%d%d",&N,&M)!=EOF; )

    {

        bool flag=1;

        For(i,1,N) u[i]=i,link[i]=0,dlt[i]=0;

        For(i,1,M)

        {

            int x,y,d;

            scanf("%d%d%d",&x,&y,&d);

            if (flag) flag=check(x,y,d);

        }

        if (flag)

            printf("Yes\n");

        else

            printf("No\n");

    }

    return 0;

}
// Slower C++ solution:
#include <iostream>

#include <cstdio>

#include <cstring>

#include <vector>

using namespace std;

const int N = 1e5+10;

int n,m;

int dis[N];

bool vis[N];

vector<pair<int,int> >E[N];

bool dfs(int u){

    vis[u] = 1;

    vector<pair<int,int> >::iterator it;

    for(it = E[u].begin(); it != E[u].end(); it++){

        int v = (*it).first;

        int d = (*it).second;

        if(!vis[v]){

            dis[v] = dis[u] + d;

            if(!dfs(v))

                return false;

        }

        else if(dis[u]+d!=dis[v])

            return false;

    }

    return true;

}

int main(){

    cin >> n >> m;

    int u,v,w;

    int i;

    for(i = 1; i <= m; i++){

        cin >> u >> v >> w;

        E[u].push_back(make_pair(v,w));

        E[v].push_back(make_pair(u,-w));

    }

    for(i = 1; i <= n; i++){

        if(!vis[i]){

            if(!dfs(i)){

                cout << "No" << endl;

                return 0;

            }

        }

    }

    cout << "Yes" << endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.9597217270361873x):
#include <iostream>

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <algorithm>

#include <limits.h>

#include <vector>

#include <map>

#include <stack>

#include <queue>

#include <math.h>

#define ll long long

#define MSET(x) memset((x), 0, sizeof((x)))

#define MMSET(x) memset((x), -1, sizeof((x)))

#define rep(i, a, b) for ( int (i) = a; (i) < (b) ; (i) ++ )

#define repA(i, a, b) for ( int (i) = a ;  (i) <= (b) ; (i) ++ )

#define repD(i, a, b) for ( int (i) = a; (i) >= (b) ; (i) -- )

#define fi first

#define se second

#define pb push_back

#define mfind(m, item) ((m).find((item)) != (m).end())

#define mp(a,b) make_pair((a),(b))

#define mt(a,b,c) make_pair(make_pair((a),(b)), (c))

#define tfi first.first

#define tse first.second

#define tth second

#define print cout << endl

#define tri pair<pair<int, int>, int>

using namespace std;



int n,m;

int fa[100005], cnt[100005];

void find(int x){

    if ( fa[x] == x ) return;

    find(fa[x]);

    cnt[x] += cnt[fa[x]]; fa[x] = fa[fa[x]];

}



int main(){

    while ( cin >> n >> m ) {

        bool has = false;

        MSET(fa); MSET(cnt);

        for ( int i = 0 ; i <= n ; ++ i ) fa[i] = i;

        for ( int i = 0, x, y, z ; i < m ; ++ i ) {

            scanf("%d %d %d", &x, &y, &z);

            find(x); find(y);

            if ( fa[x] == fa[y] ) {

                if ( cnt[y] != cnt[x] + z ) has = true;

            }

            else {

                cnt[fa[y]] = cnt[x] + z - cnt[y];

                fa[fa[y]] = fa[x];

            }

        }

        cout << (has ? "No" : "Yes" ) << endl;

    }

}
// Slower C++ solution:
/*

Problem:People on a Line

Algorithm:"linear" DFS

Status:

*/



#include <iostream>

#include <cstdio>

#include <cmath>

#include <algorithm>

#include <string>

#include <vector>

#include <map>

#include <queue>

#include <deque>

#include <set>

#include <sstream>

#include <utility>

#include <list>

#include <ctime>

#include <memory.h>

#include <stack>

#include <stdio.h>

#include <string.h>



using namespace std;



const int INF=1e9+7;



vector<pair<int,int> > nei[100005];

int pos[100005];

int n,m;

bool used[100005];

bool exist[100005];



void dfs(int x,int cur){

	pos[x]=cur;

	used[x]=true;

	for(int i=0;i<nei[x].size();i++){

		if(used[nei[x][i].first]){

			if(pos[nei[x][i].first]-cur!=nei[x][i].second){

				cout<<"No"<<endl;

				exit(0);

			}

		}

		else{

			dfs(nei[x][i].first,cur+nei[x][i].second);

		}

	}

}



int main()

{

	scanf("%d%d",&n,&m);

	for(int i=0;i<m;i++){

		int l,r,d;

		scanf("%d%d%d",&l,&r,&d);

		l--;r--;

		exist[l]=true;exist[r]=true;

		nei[l].push_back(make_pair(r,d));

		nei[r].push_back(make_pair(l,-d));

	}

	

	for(int i=0;i<n;i++){

		if(!exist[i])continue;

		if(!used[i]){

			dfs(i,0);

		}

	}

	cout<<"Yes"<<endl;

	

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.9553987336000964x):
#include <iostream>

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <algorithm>

#include <limits.h>

#include <vector>

#include <map>

#include <stack>

#include <queue>

#include <math.h>

#define ll long long

#define MSET(x) memset((x), 0, sizeof((x)))

#define MMSET(x) memset((x), -1, sizeof((x)))

#define rep(i, a, b) for ( int (i) = a; (i) < (b) ; (i) ++ )

#define repA(i, a, b) for ( int (i) = a ;  (i) <= (b) ; (i) ++ )

#define repD(i, a, b) for ( int (i) = a; (i) >= (b) ; (i) -- )

#define fi first

#define se second

#define pb push_back

#define mfind(m, item) ((m).find((item)) != (m).end())

#define mp(a,b) make_pair((a),(b))

#define mt(a,b,c) make_pair(make_pair((a),(b)), (c))

#define tfi first.first

#define tse first.second

#define tth second

#define print cout << endl

#define tri pair<pair<int, int>, int>

using namespace std;



int n,m;

int fa[100005], cnt[100005];

void find(int x){

    if ( fa[x] == x ) return;

    find(fa[x]);

    cnt[x] += cnt[fa[x]]; fa[x] = fa[fa[x]];

}



int main(){

    while ( cin >> n >> m ) {

        bool has = false;

        MSET(fa); MSET(cnt);

        for ( int i = 0 ; i <= n ; ++ i ) fa[i] = i;

        for ( int i = 0, x, y, z ; i < m ; ++ i ) {

            scanf("%d %d %d", &x, &y, &z);

            find(x); find(y);

            if ( fa[x] == fa[y] ) {

                if ( cnt[y] != cnt[x] + z ) has = true;

            }

            else {

                cnt[fa[y]] = cnt[x] + z - cnt[y];

                fa[fa[y]] = fa[x];

            }

        }

        cout << (has ? "No" : "Yes" ) << endl;

    }

}
// Slower C++ solution:
#include <iostream>

#include <cstdio>

#include <cstring>

#include <vector>

using namespace std;

const int N = 1e5+10;

int n,m;

int dis[N];

bool vis[N];

vector<pair<int,int> >E[N];

bool dfs(int u){

    vis[u] = 1;

    vector<pair<int,int> >::iterator it;

    for(it = E[u].begin(); it != E[u].end(); it++){

        int v = (*it).first;

        int d = (*it).second;

        if(!vis[v]){

            dis[v] = dis[u] + d;

            if(!dfs(v))

                return false;

        }

        else if(dis[u]+d!=dis[v])

            return false;

    }

    return true;

}

int main(){

    cin >> n >> m;

    int u,v,w;

    int i;

    for(i = 1; i <= m; i++){

        cin >> u >> v >> w;

        E[u].push_back(make_pair(v,w));

        E[v].push_back(make_pair(u,-w));

    }

    for(i = 1; i <= n; i++){

        if(!vis[i]){

            if(!dfs(i)){

                cout << "No" << endl;

                return 0;

            }

        }

    }

    cout << "Yes" << endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 3.5838660159530162x):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 2e5+10;

int f[N], d[N];

int Find(int x) {

    if(f[x] == x) return x;

    int root = Find(f[x]);

    d[x] = d[ f[x] ] + d[x];

    return f[x] = root;

}



int join(int x, int y, int z) {

    int fx = Find(x);

    int fy = Find(y);

    if(fx == fy) return z == d[y] - d[x];

    f[fy] = fx;

    d[fy] = d[x] + z - d[y];

    return 1;

}



int main() {

    int n, m;

    int ok = 1;

    cin >> n >> m;

    for(int i=1; i<=n; ++i) f[i] = i;

    for(int i=1; i<=m; ++i) {

        int x, y, z; cin >> x >> y >> z;

        if(ok) ok = join(x, y, z);

    }

    printf(ok ? "Yes\n" : "No\n");

    return 0;

}