stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <bits/stdc++.h>

# define ll      long long 

# define in      freopen("input.txt", "r", stdin);

# define out     freopen("output.txt", "w", stdout);

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

# define str     string 

# define pb      push_back

# define speed   ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

# define time    cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"

using namespace std;

ll a,b,c,d,e,f,mn=1e9,mx,cnt,bnt,sum; str s,cal,kcal;

set <ll> st; map <char, int> mp; vector <ll> vc1;

void botsolve(){

	int right1=s.size()-1,left1=0;

	for (int i = 0; i <(int) s.size(); ++i){

		if(s[right1]=='Z' && sum==0){

			cnt+=(s.size()-1)-right1;

			sum++;

		}

		if(s[left1]=='A' && mx==0){

			cnt+=left1;

			mx++;

		}

		right1--;

		left1++;

	}

	cout<<s.size()-cnt;

}

int main(){

	speed;

	//in out

    cin>>s;

    botsolve();



	time;

	return 0;

}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <cstring>



const int MAXN = 2e5 + 10;



char s[MAXN];



int main() {

  int i, j;



  scanf(" %s", s);



  i = 0;

  while (s[i] != 'A') ++i;

  

  j = strlen(s);

  while (s[j] != 'Z') --j;



  printf("%d\n", j - i + 1);

  return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include "bits/stdc++.h"

using namespace std;



#define pb push_back

#define mp make_pair

#define fst first

#define snd second



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

#define frr(i,n)	for(int i=1;i<=n;i++)



#define ms(x,i)	memset(x,i,sizeof(x))

#define dbg(x)	cout << #x << " = " << x << endl

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



#define gnl cout << endl

#define olar cout << "olar" << endl

#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)



typedef long long int ll;

typedef pair<int,int> pii;

typedef vector<int> vi;

typedef vector<pii> vii;

typedef pair<ll,ll> pll;



const int INF = 0x3f3f3f3f;

const ll llINF = 0x3f3f3f3f3f3f3f;

const int MOD = 1e9+7;



int main(){



	fastio;

	string s;

	getline(cin,s);



	int a,b;

	bool ok = true;

	fr(i,s.size()){

		if(s[i] == 'A' && ok){

			a = i;

			ok = false;

		}

		if(s[i] == 'Z') b = i;

	}



	cout << b - a + 1 << endl;



}


// Optimized solution to the same problem (faster):
#include <cstdio>

#include <cstring>



const int MAXN = 2e5 + 10;



char s[MAXN];



int main() {

  int i, j;



  scanf(" %s", s);



  i = 0;

  while (s[i] != 'A') ++i;

  

  j = strlen(s);

  while (s[j] != 'Z') --j;



  printf("%d\n", j - i + 1);

  return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

int main(){

	string s;

    cin>>s;

    int size=s.size();

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

        if(s[i]=='A'){

            s.erase(s.begin(),s.begin()+i);

            break;

        }

    }

    for(int j=0;j<size;j++){

        if(s[s.size()-1-j]=='Z'){

            s.erase(s.begin()+s.size()-j,s.end());

            break;

        }

    }

    cout<<s.size()<<endl;

    return 0;

}

// Optimized solution to the same problem (faster):
#include <stdio.h>

#include<stdlib.h>

#include<string.h>

int main(int argc,char **argv){

    char s[200000];

    scanf("%s",s);

    int ans;

    

    ans=strrchr(s,'Z')-strchr(s,'A');

    

    printf("%d",ans+1);

  return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <string>

using namespace std;

int main() {

    string s; cin >> s;

    int min_a = s.find('A');

    int max_z = s.rfind('Z');

    cout << max_z - min_a + 1;

}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <cstring>

using namespace std;

int main() {

    char s[200000]; scanf("%s", s);

    int min_a = strlen(s), max_z = 0;

    for(int i = 0; i < strlen(s); i++) {

        if(s[i] == 'A' && min_a > i) min_a = i;

        if(s[i] == 'Z' && max_z < i) max_z = i;

    }

    printf("%d\n", max_z - min_a + 1);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<cstdio>

#include<algorithm>



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



using namespace std;



const int INF=1<<29;



int n,c[100];

int dp1[1000000],dp2[1000000];



int main(){

	for(int T=1;scanf("%d",&n),n;T++){

		rep(i,n)	scanf("%d",c+i);



		if(c[0]!=1){

			printf("Case #%d: Cannot pay some amount\n",T);

			continue;

		}



		const int L=100*c[n-1];

		dp1[0]=0;

		fill(dp1+1,dp1+L,INF);

		rep(k,n)for(int p=c[k];p<L;p++)	dp1[p]=min(dp1[p],dp1[p-c[k]]+1);



		dp2[0]=0;

		fill(dp2+1,dp2+L,INF);

		rep(p,L)for(int k=n-1;k>=0;k--){

			if(p-c[k]>=0){

				dp2[p]=dp2[p-c[k]]+1;

				break;

			}

		}



		bool ok=true;

		rep(i,L)if(dp1[i]<dp2[i]){ ok=false; break; }

		printf("Case #%d: %s\n",T,ok?"OK":"Cannot use greedy algorithm");

	}



	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>



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



using namespace std;



const int INF=1<<29;



int main(){

	for(int n,c[100],T=1;scanf("%d",&n),n;T++){

		rep(i,n)	scanf("%d",c+i);



		if(c[0]!=1){

			printf("Case #%d: Cannot pay some amount\n",T);

			continue;

		}



		const int L=2*c[n-1];

		int dp1[2000];	dp1[0]=0;

		fill(dp1+1,dp1+L,INF);

		rep(k,n)for(int p=c[k];p<L;p++)	dp1[p]=min(dp1[p],dp1[p-c[k]]+1);



		int dp2[2000];	dp2[0]=0;

		fill(dp2+1,dp2+L,INF);

		rep(p,L)for(int k=n-1;k>=0;k--){

			if(p-c[k]>=0){

				dp2[p]=dp2[p-c[k]]+1;

				break;

			}

		}



		bool ok=true;

		rep(i,L)if(dp1[i]<dp2[i]){ ok=false; break; }

		printf("Case #%d: %s\n",T,ok?"OK":"Cannot use greedy algorithm");

	}



	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}




// Optimized solution to the same problem (faster):
#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;



}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#include<string.h>

#include <iostream>

#include<cstdio>

#include <stack>

#include <vector>

using namespace std;

#define INF 0x7ffffff

typedef long long ll;

const int maxn=1e5+10;

int pre[maxn];

int dis[maxn];

int findset(int x)

{

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

    int root=findset(pre[x]);

    dis[x]=dis[pre[x]]+dis[x];

    return pre[x]=root;

}

bool unionset(int x,int y,int w)

{

    int fx=findset(x),fy=findset(y);

    if(fx==fy)

        return w==dis[y]-dis[x];

    pre[fy]=fx;

    dis[fy]=dis[x]+w-dis[y];

    return true;

}

int n,m;

int main()

{

// Input

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

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

    pre[i]=i;

   bool lose=true;

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

   {

       int x,y,w;

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

        if(lose)

       lose=unionset(x,y,w);

   }

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

   else     printf("No\n");

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
/*

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;

}


// Optimized solution to the same problem (faster):
#include<string.h>

#include <iostream>

#include<cstdio>

#include <stack>

#include <vector>

using namespace std;

#define INF 0x7ffffff

typedef long long ll;

const int maxn=1e5+10;

int pre[maxn];

int dis[maxn];

int findset(int x)

{

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

    int root=findset(pre[x]);

    dis[x]=dis[pre[x]]+dis[x];

    return pre[x]=root;

}

bool unionset(int x,int y,int w)

{

    int fx=findset(x),fy=findset(y);

    if(fx==fy)

        return w==dis[y]-dis[x];

    pre[fy]=fx;

    dis[fy]=dis[x]+w-dis[y];

    return true;

}

int n,m;

int main()

{

// Input

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

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

    pre[i]=i;

   bool lose=true;

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

   {

       int x,y,w;

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

        if(lose)

       lose=unionset(x,y,w);

   }

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

   else     printf("No\n");

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

    }

}


// Optimized solution to the same problem (faster):
#include<string.h>

#include <iostream>

#include<cstdio>

#include <stack>

#include <vector>

using namespace std;

#define INF 0x7ffffff

typedef long long ll;

const int maxn=1e5+10;

int pre[maxn];

int dis[maxn];

int findset(int x)

{

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

    int root=findset(pre[x]);

    dis[x]=dis[pre[x]]+dis[x];

    return pre[x]=root;

}

bool unionset(int x,int y,int w)

{

    int fx=findset(x),fy=findset(y);

    if(fx==fy)

        return w==dis[y]-dis[x];

    pre[fy]=fx;

    dis[fy]=dis[x]+w-dis[y];

    return true;

}

int n,m;

int main()

{

// Input

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

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

    pre[i]=i;

   bool lose=true;

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

   {

       int x,y,w;

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

        if(lose)

       lose=unionset(x,y,w);

   }

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

   else     printf("No\n");

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

#define X first

#define Y second

#define pb push_back

#define sz(a) (int)a.size()

#define int long long



using namespace std;



vector <pair <int, int>> g[100010];



vector <vector <int>> v;



int a[100010];

int used[100010];



void dfs(int vv) {

    used[vv] = 1;

    for (auto u : g[vv]) {

        if (used[u.X] == 1)

            continue;

        a[u.X] = a[vv] + u.Y;

        dfs(u.X);

    }

}



signed main() {

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    int n, m;

    cin >> n >> m;

    int l, r, d;

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

        cin >> l >> r >> d;

        l--;

        r--;

        g[l].pb({r, d});

        g[r].pb({l, -d});

        v.pb({l, r, d});

    }

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

        if (used[i] == 0) {

            dfs(i);

        }

    }

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

        if (a[v[i][1]] - a[v[i][0]] != v[i][2]) {

            cout << "No" << endl;

            exit(0);

        }

    }

    cout << "Yes";

    return 0;

}


// Optimized solution to the same problem (faster):
#include<string.h>

#include <iostream>

#include<cstdio>

#include <stack>

#include <vector>

using namespace std;

#define INF 0x7ffffff

typedef long long ll;

const int maxn=1e5+10;

int pre[maxn];

int dis[maxn];

int findset(int x)

{

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

    int root=findset(pre[x]);

    dis[x]=dis[pre[x]]+dis[x];

    return pre[x]=root;

}

bool unionset(int x,int y,int w)

{

    int fx=findset(x),fy=findset(y);

    if(fx==fy)

        return w==dis[y]-dis[x];

    pre[fy]=fx;

    dis[fy]=dis[x]+w-dis[y];

    return true;

}

int n,m;

int main()

{

// Input

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

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

    pre[i]=i;

   bool lose=true;

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

   {

       int x,y,w;

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

        if(lose)

       lose=unionset(x,y,w);

   }

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

   else     printf("No\n");

}


// Fastest known solution for this problem: