malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const long long int mxn = 1e5 + 1;   string s, s1, s2, s3;   char ch[10000][10000];   int main() {     long long int n, m, i, z, j, a = 0, ans = 0, k = 0, cnt = 0, b;     long long int c = 0, x, y, d = 0, sum = 0, mn = INT_MAX, mx = 0;     string ss, strn;     char ch;     cin >> n;     long long int evn[n + 1], odd[n + 1], e = 0, o = 0;     long long int ara[n + 2], bra[n + 1], cra[n + 1];     map<long long int, long long int> mp;     set<long long int> st;     set<long long int>::iterator sit;     priority_queue<long long int> pq;     for (i = 1; i <= n; i++) {       cin >> ara[i];     }     sort(ara + 1, ara + n + 1);     for (i = n; i > 1; i--) {       x = abs(ara[i] - ara[i - 1]);       if (x <= 1) {         if (!cnt) a = ara[i - 1];         if (cnt == 1) b = ara[i - 1];         cnt++;         if (cnt == 2) {           ans += (a * b);           cnt = 0;         }         --i;       }     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int T = 1000;   const long long MAX = 1000000000000007;   const long long MOD = 100000000;   long long n, m, k;   vector<long long> A[T];   vector<pair<char, int> > ans;   long long K[T];   long long N[T];   int main() {     cin >> n;     for (int i = (int)0; i <= (int)n - 1; i++) {       cin >> N[i];     }     cin >> k;     for (int i = (int)0; i <= (int)k - 1; i++) {       cin >> K[i];     }     int j = 0;     for (int i = (int)0; i <= (int)k - 1; i++) {       long long sum = 0;       while (sum < K[i] && j <= n - 1) {         sum += N[j];         A[i].push_back(N[j]);         j++;       }       if (sum != K[i]) {         cout <<  NO  << endl;         return 0;       }       if (A[i].size() == 1) {         continue;       }       long long ma = 0;       for (int g = (int)0; g <= (int)A[i].size() - 1; g++) {         ma = max(ma, A[i][g]);       }       bool can = false;       for (int g = (int)0; g <= (int)A[i].size() - 1; g++) {         if (A[i][g] == ma) {           if (g != 0 && A[i][g - 1] < ma ||               g != A[i].size() - 1 && A[i][g + 1] < ma) {             can = true;           }           if (can) {             while (A[i].size() != 1) {               if (g != 0 && A[i][g - 1] < ma) {                 ma = ma + A[i][g - 1];                 A[i][g - 1] = ma;                 ans.push_back(make_pair( L , i + g + 1));                 A[i].erase(A[i].begin() + g - 1);                 g--;               } else {                 if (g != A[i].size() - 1 && A[i][g + 1] < ma) {                   ma = ma + A[i][g + 1];                   A[i][g] = ma;                   ans.push_back(make_pair( R , i + g + 1));                   A[i].erase(A[i].begin() + g);                 }               }             }             break;           }         }       }       if (!can) {         cout <<  NO  << endl;         return 0;       }     }     if (j == n) {       cout <<  YES  << endl;     } else {       cout <<  NO  << endl;       return 0;     }     for (int i = (int)0; i <= (int)ans.size() - 1; i++) {       cout << ans[i].second <<     << ans[i].first << endl;     }     return 0;   }
#include <bits/stdc++.h>   int n, i, j, k, t, len;   char st[200];   int main() {     scanf( %d , &n);     while (n--) {       scanf( %s , st);       len = strlen(st);       t = 8;       for (i = 0; i < len; i++)         if (st[i] !=  : ) {           t--;           while (i < len && st[i] !=  : ) i++;         }       j = 0;       for (i = 0; i < len; i++) {         if (st[i] ==  : ) {           if (j != i) {             if (j != 0) printf( : );             for (k = 0; k < 4 - i + j; k++) printf( 0 );             for (k = j; k < i; k++) printf( %c , st[k]);           }           if (st[i + 1] ==  : ) {             if (j == i)               printf( 0000 );             else               printf( :0000 );             for (k = 1; k < t; k++) printf( :0000 );             i++;           }           j = i + 1;         }       }       if (st[len - 1] !=  : ) {         printf( : );         for (k = 0; k < 4 - i + j; k++) printf( 0 );         for (k = j; k < i; k++) printf( %c , st[k]);       }       printf(  n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int Z = (int)1e5 + 111;   const int inf = (int)1e9 + 111;   const long long llinf = (long long)1e18 + 5;   const int MOD = (int)1e9 + 7;   vector<int> ans[Z];   int a[Z];   pair<int, int> b[Z];   int main() {     ios_base::sync_with_stdio(false);     int n;     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> a[i];       b[i].first = a[i];       b[i].second = i;     }     int cnt = 0;     sort(b + 1, b + 1 + n);     for (int i = 1; i <= n; ++i) {       if (a[i] == -inf) continue;       set<int> cur_pos;       cur_pos.insert(i);       int pos = lower_bound(b + 1, b + 1 + n, make_pair(a[i], -inf)) - b;       while (!cur_pos.count(pos)) {         int x = pos;         cur_pos.insert(pos);         pos = lower_bound(b + 1, b + 1 + n, make_pair(a[pos], -inf)) - b;         a[x] = -inf;       }       cnt++;       for (int to : cur_pos) {         ans[cnt].push_back(to);       }     }     cout << cnt <<   n ;     for (int i = 1; i <= cnt; ++i) {       cout << (int)ans[i].size() <<    ;       for (int j = 0; j < (int)ans[i].size(); ++j) {         cout << ans[i][j] <<    ;       }       cout <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1.0);   const long long INF = 1e9;   const long long LINF = INF * INF;   const int MAX = 1 << 10;   void go(pair<int, int> s, pair<int, int> f, vector<pair<int, int> >& a) {     a.push_back(s);     while (s.first > f.first) {       s.first--;       a.push_back(s);     }     while (s.first < f.first) {       s.first++;       a.push_back(s);     }     while (s.second > f.second) {       s.second--;       a.push_back(s);     }     while (s.second < f.second) {       s.second++;       a.push_back(s);     }     a.push_back(f);   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     vector<pair<int, int> > a(3);     for (int i = (0); i < (3); ++i) cin >> a[i].first >> a[i].second;     vector<pair<int, int> > ans1, ans2;     sort(a.begin(), a.end());     go(a[0], a[1], ans1);     go(a[1], a[2], ans1);     go(a[0], {a[1].first, a[2].second}, ans2);     go(a[1], {a[1].first, a[2].second}, ans2);     go(a[2], {a[1].first, a[2].second}, ans2);     sort(ans1.begin(), ans1.end());     ans1.resize(unique(ans1.begin(), ans1.end()) - ans1.begin());     sort(ans2.begin(), ans2.end());     ans2.resize(unique(ans2.begin(), ans2.end()) - ans2.begin());     vector<pair<int, int> > ans = ans1;     if ((int)((ans2).size()) < (int)((ans1).size())) ans = ans2;     sort(ans.begin(), ans.end());     ans.resize(unique(ans.begin(), ans.end()) - ans.begin());     cout << (int)((ans).size()) << endl;     for (auto i : ans) cout << i.first <<     << i.second << endl;     cerr <<  Time elapsed:   << clock() / (double)CLOCKS_PER_SEC << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int i, h, j, w;   string s, t, v;   pair<string, int> k;   stack<int> p;   vector<pair<string, int>> a;   int main() {     cin >> s;     v = s;     t = s;     for (i = 0; i < s.size(); i++)       if (s[i] ==  (  || s[i] ==  [ ) p.push(i);     while (!p.empty()) {       j = p.top();       if (s[j] ==  (  && s[j + 1] ==  ) )         v[j] =  0 , v[v.find( ) , j)] =  0 , s.erase(j, 2);       if (s[j] ==  [  && s[j + 1] ==  ] )         v[j] =  0 , v[v.find( ] , j)] =  0 , s.erase(j, 2);       p.pop();     }     for (i = 0; i < v.size(); i++) {       if (v[i] ==  0 ) {         k.first += t[i];         if (t[i] ==  [ ) k.second++;       }       if (v[i] !=  0  && k.first.size() || i == v.size() - 1 && k.first.size())         a.push_back(k), k.first.clear(), h = max(h, k.second), k.second = 0;     }     if (!h)       cout << 0;     else if (!a.size())       cout << k.second << endl << k.first;     else {       for (i = 0; i < a.size(); i++)         if (a[i].second == h) {           cout << h << endl << a[i].first;           break;         }     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, k, p, x, y;     cin >> n >> k >> p >> x >> y;     long long int sum = 0;     long long int cov = 0;     for (int i = 0; i < k; i++) {       long long int kk;       cin >> kk;       if (kk < y) cov++;       sum += kk;     }     int used = k;     int remain;     if (cov > (n / 2)) {       cout <<  -1 ;       return 0;     } else {       remain = (n / 2) - cov;       if (remain > n - k) remain = n - k;       sum += remain;       used += remain;     }     int finalre = n - used;     if (sum + finalre * y <= x) {       for (int i = 0; i < remain; i++)         cout <<  1               <<    ;       for (int i = 0; i < finalre; i++) cout << y <<    ;     } else       cout <<  -1  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int p[2][252], f[252][252], C[252], mod = 1e9 + 7, inv[252];   int qp(int a, int x) {     int i = 1;     for (; x; x >>= 1, a = 1ll * a * a % mod)       if (x & 1) i = 1ll * i * a % mod;     return i;   }   int main() {     int n, m, i, j, k, a;     scanf( %d%d , &n, &m);     for (inv[1] = 1, i = 2; i <= n; i++)       inv[i] = mod - 1ll * mod / i * inv[mod % i] % mod;     for (i = C[0] = p[0][0] = p[1][0] = 1; i <= n; i++) {       C[i] = 1ll * C[i - 1] * (n - i + 1) % mod * inv[i] % mod;       p[0][i] = 1ll * p[0][i - 1] * m % mod;       p[1][i] = 1ll * p[1][i - 1] * (m - 1) % mod;     }     for (i = k = 0, j = 1; i <= n; i++, j *= -1) {       a = 1ll * p[0][n - i] * p[1][i] % mod - p[1][n];       if (a < 0) a += mod;       if (a >= mod) a -= mod;       k += 1ll * j * C[i] * qp(a, n) % mod;       if (k < 0) k += mod;       if (k >= mod) k -= mod;     }     printf( %d , k);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     int m;     cin >> n >> m;     int a[n];     for (int i = 0; i < n; i++) {       cin >> a[i];     }     sort(a, a + n);     int ans = 0;     for (int i = 0; i < m; i++) {       if (a[i] >= 0) break;       ans += a[i];     }     cout << abs(ans) << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct __s {     __s() {       if (1) {         ios_base::Init i;         cin.sync_with_stdio(0);         cin.tie(0);       }     }     ~__s() {       if (!1)         fprintf(stderr,  Execution time: %.3lf s. n ,                 (double)clock() / CLOCKS_PER_SEC);       long long n;       cin >> n;     }   } __S;   long long d[311111];   long long s[311111];   long long I[311111];   bool u[311111];   bool comp(long long i, long long j) { return d[i] < d[j]; }   int main(void) {     long long n, k;     cin >> n >> k;     long long cnt = 0;     for (long long i = 1; i <= n; i++) {       for (long long j = 2 * i; j <= n; j += i) {         d[j]++;       }       cnt += d[i];       s[i] = s[i - 1] + d[i];     }     d[1] = 1e18;     if (cnt < k) {       cout <<  No  <<   n ;       return 0;     }     for (long long i = 1; i <= n; i++) {       u[i] = true;       I[i] = i;       if (s[i] >= k) {         n = i;         cnt = s[i];         break;       }     }     sort(I + n / 2 + 1, I + n + 1, comp);     long long m = n;     for (long long ii = n; ii >= 1; ii--) {       long long i = I[ii];       if (k <= cnt - d[i]) {         cnt -= d[i];         u[i] = false;         m--;       }     }     cout <<  Yes  <<   n ;     cout << m <<   n ;     for (long long i = 1; i <= n; i++) {       if (u[i]) cout << i <<    ;     }     cout <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   map<string, int> nums;   int liking[7][7];   bool usable[7];   int team[7];   vector<int> members[3];   int mindifference, maxliking;   int a, b, c;   void teaming(int currteam, int memberno) {     int i, j, t;     if (currteam == 2 && memberno == 7) {       int exp1 = a / members[0].size(), exp2 = b / members[1].size(),           exp3 = c / members[2].size();       int minexp = min(exp1, min(exp2, exp3));       int maxexp = max(exp1, max(exp2, exp3));       int diff = maxexp - minexp;       int lliking = 0;       for (t = 0; t < 3; t++) {         for (i = 0; i < members[t].size(); i++) {           for (j = 0; j < 7; j++) {             if (liking[members[t][i]][j] == 1 && team[j] == t &&                 team[members[t][i]] == t)               lliking++;           }         }       }       if (diff < mindifference ||           (diff == mindifference && lliking > maxliking)) {         mindifference = diff;         maxliking = lliking;       }       return;     }     if (memberno >= 7) return;     for (i = 0; i < 7; i++)       if (usable[i]) {         usable[i] = false;         team[i] = currteam;         members[currteam].push_back(i);         teaming(currteam, memberno + 1);         members[currteam].pop_back();         if (currteam < 2 && members[currteam].size() > 0) {           team[i] = currteam + 1;           members[currteam + 1].push_back(i);           teaming(currteam + 1, memberno + 1);           members[currteam + 1].pop_back();         }         team[i] = -1;         usable[i] = true;       }   }   int main() {     int i, j, n;     nums.insert(make_pair( Anka , 0));     nums.insert(make_pair( Chapay , 1));     nums.insert(make_pair( Cleo , 2));     nums.insert(make_pair( Troll , 3));     nums.insert(make_pair( Dracul , 4));     nums.insert(make_pair( Snowy , 5));     nums.insert(make_pair( Hexadecimal , 6));     scanf( %d , &n);     for (i = 0; i < 7; i++)       for (j = 0; j < 7; j++) liking[i][j] = 0;     for (i = 0; i < 7; i++) {       usable[i] = true;       team[i] = -1;     }     for (i = 0; i < n; i++) {       char name1[30], tmp[30], name2[30];       scanf( %s %s %s , name1, tmp, name2);       liking[nums[name1]][nums[name2]] = 1;     }     scanf( %d %d %d , &a, &b, &c);     mindifference = 2000000000;     maxliking = 0;     teaming(0, 0);     printf( %d %d , mindifference, maxliking);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 998244353;   int n, m;   int a[66], w[66];   long long dp[66][66][66];   int sa, sb, sum;   long long inv[10010];   void solve1(int x) {     memset(dp, 0, sizeof(dp));     dp[0][0][0] = 1;     long long zi, mu;     for (int i = 0; i < m; ++i)       for (int j = 0; j <= i; ++j)         for (int k = j; k <= i; ++k) {           if (!dp[i][j][k]) continue;           mu = inv[sum + k * 2 - i];           zi = (w[x] + j) % mod;           (dp[i + 1][j + 1][k + 1] += dp[i][j][k] * zi % mod * mu % mod) %= mod;           zi = (sa + k - j - w[x] + mod) % mod;           (dp[i + 1][j][k + 1] += dp[i][j][k] * zi % mod * mu % mod) %= mod;           zi = (sb - i + k + mod) % mod;           (dp[i + 1][j][k] += dp[i][j][k] * zi % mod * mu % mod) %= mod;         }     long long r = 0;     for (int j = 0; j <= m; ++j)       for (int k = j; k <= m; ++k) {         if (!dp[m][j][k]) continue;         r += dp[m][j][k] * (w[x] + j) % mod;         r %= mod;       }     printf( %lld n , r);     return;   }   void solve2(int x) {     memset(dp, 0, sizeof(dp));     dp[0][0][0] = 1;     long long zi, mu;     for (int i = 0; i < m; ++i)       for (int j = 0; j <= i; ++j)         for (int k = j; k <= i; ++k) {           if (!dp[i][j][k]) continue;           mu = inv[sum - k * 2 + i];           zi = (w[x] - j) % mod;           (dp[i + 1][j + 1][k + 1] += dp[i][j][k] * zi % mod * mu % mod) %= mod;           zi = (sb - w[x] - k + j + mod) % mod;           (dp[i + 1][j][k + 1] += dp[i][j][k] * zi % mod * mu % mod) %= mod;           zi = (sa + i - k + mod) % mod;           (dp[i + 1][j][k] += dp[i][j][k] * zi % mod * mu % mod) %= mod;         }     long long r = 0;     for (int j = 0; j <= m; ++j)       for (int k = j; k <= m; ++k) {         if (!dp[m][j][k]) continue;         r += dp[m][j][k] * max(0, w[x] - j) % mod;         r %= mod;       }     printf( %lld n , r);     return;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; ++i) scanf( %d , &a[i]);     for (int i = 1; i <= n; ++i) {       scanf( %d , &w[i]);       sa += a[i] * w[i];       sb += (a[i] ^ 1) * w[i];     }     sum = sa + sb;     inv[1] = 1;     for (int i = 2; i <= 10000; ++i)       inv[i] = (mod - mod / i) * inv[mod % i] % mod;     for (int i = 1; i <= n; ++i)       if (a[i])         solve1(i);       else         solve2(i);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   map<string, int> m1;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     int n, k, t;     cin >> n >> k >> t;     int temp = n * k * t / 100;     int cnt = 0;     while (n--) {       if (temp > 0) {         if (temp < k) {           cout << temp <<    ;           temp = 0;         } else {           cout << k <<    ;           temp -= k;         }       } else         cout << 0 <<    ;     }   }
#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e8;   const double EPS = 1e-9;   const long long MOD = (long long)(1e9 + 7);   const long long MAXV = (long long)(5e3 + 10);   const long long MAXE = (long long)(1e5 + 10);   int n, m;   int c[200];   vector<pair<int, int> > aux;   bool cmp(pair<int, int> a, pair<int, int> b) { return a.first > b.first; }   vector<int> L, R;   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) {       int a;       cin >> a;       c[a]++;     }     for (int i = 0; i <= 100; i++)       if (c[i]) aux.push_back(pair<int, int>(c[i], i));     sort(aux.begin(), aux.end(), cmp);     int r = aux[0].first;     for (int i = 0; i < aux.size(); i++)       for (int j = 0; j < aux[i].first; j++) L.push_back(aux[i].second);     R = L;     rotate(R.begin(), R.begin() + r, R.end());     int cnt = 0;     for (int i = 0; i < L.size(); i++)       if (L[i] != R[i]) cnt++;     cout << cnt <<   n ;     for (int i = 0; i < L.size(); i++) cout << L[i] <<     << R[i] <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[400100];   int b[400100];   int main() {     int n;     cin >> n;     int k = 0;     for (int i = 1; i <= n; i++) {       int l;       cin >> l;       if (l == 0) {         k = 1;       } else {         if (k == 1)           a[i - 1] = l;         else           a[i] = l;       }     }     k = 0;     for (int i = 1; i <= n; i++) {       int l;       cin >> l;       if (l == 0) {         k = 1;       } else {         if (k == 1)           b[i - 1] = l;         else           b[i] = l;       }     }     int uk = n;     int uk1 = 1;     for (int i = 1; i < n; i++) {       if (b[i] != a[1]) {         b[uk] = b[i];         uk++;       } else {         uk1 = i;         break;       }     }     for (int i = 1; i < n; i++) {       if (a[i] != b[uk1]) {         cout <<  NO ;         exit(0);       }       uk1++;     }     cout <<  YES ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long a[100000];     long long n, i;     cin >> n;     for (i = 0; i < n; i++) {       cin >> a[i];     }     sort(a, a + n);     a[0] = 1;     for (i = 1; i <= n - 1; i++) {       if (a[i] == a[i - 1] || a[i] == a[i - 1] + 1)         continue;       else         a[i] = a[i - 1] + 1;     }     cout << a[n - 1] + 1;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long sqr(long long a) { return a * a; }   int main() {     ios_base::sync_with_stdio(0);     int n, m;     cin >> n >> m;     vector<long long> sumx(n, 0ll);     vector<long long> sumy(m, 0ll);     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         long long c;         cin >> c;         sumx[i] += c;         sumy[j] += c;       }     }     vector<long long> ansx(n + 1, 0ll);     vector<long long> ansy(m + 1, 0ll);     for (int i = 0; i <= n; i++) {       for (int j = 0; j < n; j++) {         ansx[i] += sumx[j] * sqr((2 * j + 1) - 2 * i);       }     }     for (int i = 0; i <= m; i++) {       for (int j = 0; j < m; j++) {         ansy[i] += sumy[j] * sqr((2 * j + 1) - 2 * i);       }     }     pair<long long, pair<int, int> > res =         make_pair((long long)1e18, make_pair(-1, -1));     for (int i = 0; i <= n; i++) {       for (int j = 0; j <= m; j++) {         res = min(res, make_pair(ansx[i] + ansy[j], make_pair(i, j)));       }     }     cout << 4 * res.first << endl;     cout << res.second.first <<     << res.second.second << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, ans;   bool pd(int n) {     for (int i = 2; i <= sqrt(n); i++)       if (n % i == 0) return false;     return true;   }   int main() {     scanf( %d , &n);     if (pd(n))       puts( 1 );     else if (n % 2 == 0 || pd(n - 2))       puts( 2 );     else       puts( 3 );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> ans, adj[200007];   bool vis[200007];   int inputorder[200007], relorder[200007];   bool cmp(int a, int b) { return relorder[a] < relorder[b]; }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     int n, x, a, b, i, j;     cin >> n;     for (i = 0; i < n - 1; i++) {       cin >> a >> b;       adj[a].push_back(b);       adj[b].push_back(a);     }     for (i = 0; i < n; i++) {       cin >> inputorder[i];       relorder[inputorder[i]] = i;     }     for (i = 1; i <= n; i++) sort(adj[i].begin(), adj[i].end(), cmp);     queue<int> q;     q.push(1);     memset(vis, false, sizeof(vis));     while (!(q.empty())) {       queue<int> temp;       while (!(q.empty())) {         int x = q.front();         q.pop();         ans.push_back(x);         vis[x] = true;         for (j = 0; j < adj[x].size(); j++)           if (vis[adj[x][j]] == false) temp.push(adj[x][j]);       }       q = temp;     }     for (i = 0; i < n; i++)       if (inputorder[i] != ans[i]) {         cout <<  No ;         return 0;       }     cout <<  Yes ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10;   int n, sz[N], sum[N];   int64_t r[N][3], ans;   vector<pair<int, int>> g[N];   void dfs(int x, int f) {     sz[x] = 1;     for (auto &e : g[x]) {       int y = e.first, w = e.second;       if (y == f) continue;       dfs(y, x), sz[x] += sz[y];       if (w) sum[y] = sz[y], r[y][2] += r[y][0] + r[y][1], r[y][0] = r[y][1] = 0;       r[x][0] += r[y][0];       r[x][1] += r[y][1];       r[x][2] += r[y][2];       sum[x] += sum[y];     }     for (auto &e : g[x]) {       int y = e.first, w = e.second;       if (y == f) continue;       ans += sum[y] * (int64_t)(sz[x] - sz[y]) * (sz[x] - sz[y] - 1);       ans +=           (sz[y] - sum[y]) * (int64_t)(sum[x] - sum[y]) * (sum[x] - sum[y] - 1);       ans += 2 * r[y][0] * (sum[x] - sum[y]);       ans += 2 * (r[y][1] + r[y][2]) * (sz[x] - sz[y]);       r[x][0] += (sz[y] - sum[y]) * (int64_t)(sum[x] - sum[y]);       r[x][1] += sum[y] * (int64_t)(sz[x] - sz[y]);     }     r[x][0] += sum[x];     ans += sum[x] * (sum[x] - 1ll);   }   int main() {     scanf( %d , &n);     for (int i = 1; i < n; ++i) {       int u, v, w;       scanf( %d%d%d , &u, &v, &w);       int ok = 1;       while (w) {         if (w % 10 != 7 && w % 10 != 4) ok = 0;         w /= 10;       }       g[u].push_back({v, ok});       g[v].push_back({u, ok});     }     dfs(1, 0);     printf( %lld n , ans);   }