malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   #pragma optimize( Ofast )   using namespace std;   using ll = long long int;   using ull = unsigned long long int;   using dd = double;   using ldd = long double;   using si = short int;   using pii = pair<int, int>;   using pll = pair<ll, ll>;   int _size = 1 << 20;   vector<ll> tree(_size << 1), cnt(_size << 1);   vector<ll> pos;   vector<ll> x;   vector<ll> price;   vector<ll> if_now;   vector<ll> to_pay;   vector<vector<pll>> gr;   vector<ll> ans;   int n;   void upd(int v, ll cn) {     ll c = cn;     cn *= price[v];     v = pos[v];     v += _size;     cnt[v] += c;     tree[v] += cn;     v >>= 1;     while (v) {       tree[v] += cn;       cnt[v] += c;       v >>= 1;     }   }   int find_f(ll t, int v = 1) {     if (v >= _size) return v - _size;     if (tree[v << 1] > t)       return find_f(t, v << 1);     else       return find_f(t - tree[v << 1], v << 1 | 1);   }   ll get_cnt(ll rr, int v = 1, int l = 0, int r = _size) {     if (r <= rr) return cnt[v];     if (l >= rr) return 0;     return get_cnt(rr, v << 1, l, (r + l) >> 1) +            get_cnt(rr, v << 1 | 1, (r + l) >> 1, r);   }   ll get_pay(ll rr, int v = 1, int l = 0, int r = _size) {     if (r <= rr) return tree[v];     if (l >= rr || r <= l) return 0;     return get_pay(rr, v << 1, l, (r + l) >> 1) +            get_pay(rr, v << 1 | 1, (r + l) >> 1, r);   }   ll get_now(ll t) {     0;     if (t >= tree[1]) {       return get_cnt(_size);     }     0;     int ind = find_f(t);     0;     ll ans = get_cnt(ind);     0;     t -= get_pay(ind);     0;     if (ind >= to_pay.size() || to_pay[ind] == 0) {       return ans;     }     return ans + t / to_pay[ind];   }   void dfs(int v, ll t) {     0;     upd(v, x[v]);     0;     if_now[v] = get_now(t);     0;     for (pll i : gr[v]) {       t -= i.second * 2;       if (t >= 0) dfs(i.first, t);       t += i.second * 2;     }     upd(v, -x[v]);   }   void afs(int v) {     ans[v] = if_now[v];     vector<ll> ch;     for (pii i : gr[v]) {       afs(i.first);       ch.push_back(ans[i.first]);     }     sort((ch).begin(), (ch).end());     reverse((ch).begin(), (ch).end());     ans[v] = max(ans[v], (ch.size() >= 2 ? ch[1] : 0));   }   int main() {     ios_base::sync_with_stdio(0);     cout.tie(0);     cin.tie(0);     ll t;     cin >> n >> t;     x.resize(n);     pos.resize(n);     price.resize(n);     if_now.resize(n);     gr.resize(n);     to_pay.resize(n);     ans.resize(n);     for (int i = 0; i < n; ++i) cin >> x[i];     map<ll, ll> ttt;     int s = 0;     for (int i = 0; i < n; ++i) {       cin >> price[i];       ttt[price[i]] = 0;     }     for (auto& i : ttt) {       i.second = s;       to_pay[s] = i.first;       ++s;     }     0;     for (int i = 0; i < n; ++i) pos[i] = ttt[price[i]];     0;     vector<pair<ll, ll>> tt;     for (int i = 0; i < n - 1; ++i) {       ll a, b;       cin >> a >> b;       tt.push_back({a, b});       gr[a - 1].push_back({i + 1, b});     }     0;     dfs(0, t);     0;     0;     afs(0);     0;     for (pll i : gr[0]) {       ans[0] = max(ans[0], ans[i.first]);     }     cout << ans[0];   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int TESTS = 1;     cin >> TESTS;     while (TESTS--) {       long long n, x, y, d;       cin >> n >> x >> y >> d;       if (abs(y - x) % d == 0) {         cout << abs(y - x) / d <<   n ;         continue;       }       long long l = 1000000007, r = 1000000007;       if ((n - y) % d == 0) {         l = (n - y) / d + (n - x + d - 1) / d;       }       if ((y - 1) % d == 0) {         r = (y - 1) / d + (x - 1 + d - 1) / d;       }       long long ans = min(l, r);       if (ans != 1000000007)         cout << ans <<   n ;       else         cout <<  -1 n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long nch(long long n) {     long long ans = 0, tmp = 1, k = 1;     while (n) {       ans += min(n, tmp * 9) * k;       n -= min(n, tmp * 9);       tmp *= 10;       k++;     }     return ans;   }   char get(long long n, long long d) {     string s;     stringstream ss;     ss << n;     ss >> s;     return s[d - 1];   }   char kth(long long n) {     long long s = 0, nine = 9, d = 0, len;     for (len = 1;; len++) {       s += nine * len;       d += nine;       if (s >= n) {         s -= nine * len;         d -= nine;         n -= s;         break;       }       nine *= 10;     }     long long diff = ceil((double)n / len);     long long dd = n % len;     if (dd == 0) dd = len;     return get(d + diff, dd);   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long q;     cin >> q;     while (q--) {       long long k, n = 1;       cin >> k;       for (n = 1; nch(n) < k; n++) k -= nch(n);       cout << kth(k) << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 5e5 + 7;   vector<int> g[N];   int used[N];   int pos[N];   vector<vector<int>> cycles;   vector<int> st;   void dfs(int u, int p = -1) {     st.push_back(u);     pos[st.back()] = (int)st.size() - 1;     used[u] = 1;     for (auto v : g[u]) {       if (!used[v]) {         dfs(v, u);       } else if (v != p && used[v] == 1) {         int x = pos[v];         cycles.push_back({});         for (int i = x; i < (int)st.size(); ++i) {           cycles.back().push_back(st[i]);         }       }     }     st.pop_back();     used[u] = 2;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, m;     cin >> n >> m;     vector<pair<int, int>> edges;     map<pair<int, int>, int> id;     for (int i = 0; i < m; ++i) {       int u, v;       cin >> u >> v;       --u, --v;       g[u].push_back(v);       g[v].push_back(u);       edges.push_back({u, v});       id[{u, v}] = id[{v, u}] = i;     }     dfs(0);     vector<int> res(m);     vector<int> who(m, -1);     vector<int> cmp(n, -1);     for (int i = 0; i < (int)cycles.size(); ++i) {       vector<int> ce;       for (int j = 0; j < (int)cycles[i].size(); ++j) {         cmp[cycles[i][j]] = i;         ce.push_back(             id[{cycles[i][j], cycles[i][(j + 1) % (int)cycles[i].size()]}]);       }       rotate(ce.begin(), min_element(ce.begin(), ce.end()), ce.end());       int mx = max_element(ce.begin(), ce.end()) - ce.begin();       bool ok = true;       for (int i = 1; i <= mx; ++i) ok &= ce[i] > ce[i - 1];       for (int i = mx + 1; i < ce.size(); ++i) ok &= ce[i] < ce[i - 1];       if (ok) who[ce[0]] = ce[mx];     }     vector<int> ans(n, 1);     for (int i = m - 1; i >= 0; --i) {       int a = edges[i].first;       int b = edges[i].second;       int sum = ans[a] + ans[b];       if (who[i] != -1) sum -= res[who[i]];       res[i] = sum;       ans[a] = ans[b] = sum;     }     for (int i = 0; i < n; ++i) cout << ans[i] - 1 <<    ;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 200015;   const int MOD = 1000000007;   int a[MAXN];   bool F(int len, int num) {     bool flag;     int pres;     for (int i = 0; i <= num - 1; i++) {       flag = true;       pres = i;       for (int j = 0; j <= len - 1; j++) {         if (a[pres] == 0) flag = false;         pres += num;       }       if (flag == true) return flag;     }     return false;   }   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i <= n - 1; i++) scanf( %d , &a[i]);     bool ok = false;     for (int i = 1; i <= sqrt(n); i++) {       if (n % i == 0) {         if (i >= 3) ok = ok | F(i, n / i);         if (i != sqrt(n) && n / i >= 3) ok = ok | F(n / i, i);       }     }     if (ok)       cout <<  YES n ;     else       cout <<  NO n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int signum(long long a) { return (a == 0 ? 0 : (a > 0 ? 1 : -1)); }   int dir(pair<long long, long long>& a, pair<long long, long long>& b,           pair<long long, long long>& c) {     return signum((b.first - a.first) * (c.second - a.second) -                   (b.second - a.second) * (c.first - a.first));   }   bool inside(vector<pair<long long, long long> >& hull,               pair<long long, long long>& p) {     if (dir(hull[0], hull[1], p) != 1) return false;     if (dir(hull[0], hull[hull.size() - 1], p) != -1) return false;     int l = 1, r = hull.size() - 2;     while (l < r) {       int m = (l + r) / 2 + 1;       if (dir(hull[0], hull[m], p) == -1)         r = m - 1;       else         l = m;     }     return dir(hull[l], hull[l + 1], p) == 1;   }   int main() {     cin.sync_with_stdio(false);     cin.tie(0);     int n, m;     cin >> n;     vector<pair<long long, long long> > A(n);     for (pair<long long, long long>& a : A) cin >> a.first >> a.second;     reverse(A.begin(), A.end());     cin >> m;     vector<pair<long long, long long> > B(m);     bool sol = true;     for (pair<long long, long long>& a : B) {       cin >> a.first >> a.second;       sol &= inside(A, a);     }     cout << (sol ?  YES  :  NO ) << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int Maxn = 500010;   const int Maxk = 110;   const int Maxp = 110;   const int inf = 2147483647;   int read() {     int x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 )       x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();     return x * f;   }   int n, k, p, s[Maxn], f[Maxn][Maxk];   int mn[2][Maxp], v[Maxp];   void modify(int o, int x, int y) {     for (; x <= p; x += (x & -x)) mn[o][x] = min(mn[o][x], y);   }   int query(int o, int x) {     int re = inf;     for (; x; x -= (x & -x)) re = min(re, mn[o][x]);     return re;   }   int main() {     memset(f, 63, sizeof(f));     n = read(), k = read(), p = read();     s[0] = 0;     for (int i = 1; i <= n; i++)       s[i] = (read() % p + s[i - 1]) % p, f[i][1] = s[i];     for (int j = 2; j <= k; j++) {       memset(v, 63, sizeof(v));       memset(mn, 63, sizeof(mn));       v[s[j - 1]] = f[j - 1][j - 1] - s[j - 1];       modify(0, s[j - 1] + 1, v[s[j - 1]]);       modify(1, p - s[j - 1], v[s[j - 1]]);       for (int i = j; i <= n; i++) {         f[i][j] = min(s[i] + query(0, s[i] + 1), s[i] + p + query(1, p - s[i]));         if (f[i][j - 1] - s[i] < v[s[i]]) {           v[s[i]] = f[i][j - 1] - s[i];           modify(0, s[i] + 1, v[s[i]]);           modify(1, p - s[i], v[s[i]]);         }       }     }     printf( %d , f[n][k]);   }
#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) {     while (b) {       a %= b;       swap(a, b);     }     return a;   }   int main() {     int t;     cin >> t;     while (t--) {       long long x, y;       cin >> x >> y;       if (gcd(x, y) == 1) {         cout <<  FINITE  << endl;       } else {         cout <<  INFINITE  << endl;       }     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 200005;   int cycl[N], set_val[N], cycl_size[N];   int main() {     int h, m, n;     cin >> h >> m >> n;     vector<int> strt;     bool vis[h];     memset(vis, false, sizeof vis);     int kk = 0;     set<int> dmm[h + 1];     for (long long i = 0; i < m; i++) {       if (vis[i]) {         continue;       }       int ii = i;       int jp = 0;       while (1) {         if (vis[ii]) {           break;         }         set_val[ii] = jp;         cycl[ii] = kk;         dmm[kk].insert(jp);         vis[ii] = true;         ii += m;         ii %= h;         jp++;       }       cycl_size[kk] = jp;       kk++;     }     map<int, int> hash;     map<int, int> final;     long long cnt = 0;     while (n--) {       char ch;       scanf(  %c , &ch);       if (ch ==  + ) {         int id, t;         scanf( %d%d , &id, &t);         hash[id] = t;         if (dmm[cycl[t]].find(set_val[t]) != dmm[cycl[t]].end()) {           final[id] = set_val[t];           dmm[cycl[t]].erase(set_val[t]);           continue;         }         set<int>::iterator it, it1;         it = dmm[cycl[t]].lower_bound(set_val[t]);         int xx1;         if (it == dmm[cycl[t]].end()) {           it = dmm[cycl[t]].begin();           xx1 = cycl_size[cycl[t]] + *it - set_val[t];           int jj = *it;           final[id] = jj;           dmm[cycl[t]].erase(jj);         } else {           xx1 = *it - set_val[t];           int jj = *it;           final[id] = jj;           dmm[cycl[t]].erase(jj);         }         cnt += (long long)xx1;       } else {         int id;         scanf( %d , &id);         dmm[cycl[hash[id]]].insert(final[id]);       }     }     cout << cnt << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     long long int a[n], p[n], s[n];     for (int i = 0; i < n; i++) {       cin >> a[i];     }     int blocks = 0;     p[0] = a[0];     for (int i = 1; i < n; i++) {       p[i] = max(p[i - 1], a[i]);     }     s[n - 1] = a[n - 1];     for (int i = n - 2; i >= 0; i--) {       s[i] = min(s[i + 1], a[i]);     }     for (int i = 0; i < n - 1; i++) {       if (p[i] <= s[i + 1]) {         blocks++;       }     }     cout << blocks + 1 << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, k, i, j;     cin >> n >> m >> k;     int a[n][m];     for (i = 0; i < n; i++) {       for (j = 0; j < m; j++) {         cin >> a[i][j];       }     }     int x;     for (i = 0; i < k; i++) {       cin >> x;       x--;       int v = 0;       while (v < n) {         if (a[v][x] == 2) {           v++;         } else if (a[v][x] == 1) {           a[v][x] = 2;           x++;         } else {           a[v][x] = 2;           x--;         }       }       cout << x + 1 <<    ;     }     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i = 0;     cin >> n;     long long a;     vector<long long> vc;     while (n--) {       cin >> a;       vc.push_back(a);     }     sort(vc.begin(), vc.end());     i = vc.size();     long long ans = vc[i - 1];     for (int j = i - 2; j >= 0; j--) {       if (vc[j] >= vc[j + 1]) {         vc[j] = vc[j + 1] - 1;       }       if (vc[j] > 0) ans += vc[j];     }     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   int q, n;   long long s, cur;   char str[100100];   int main() {     scanf( %lld%d , &s, &q);     while (q--) {       scanf( %lld , &cur);       scanf( %s , str);       n = strlen(str);       for (int i = 0; i < n; i++) {         long long t = 1;         while (cur % (t << 1) == 0) t <<= 1;         if (str[i] ==  U ) {           if ((t << 1) > s) continue;           cur ^= t;           cur |= (t << 1);         } else if (str[i] ==  L ) {           if (t == 1) continue;           cur ^= t;           cur |= (t >> 1);         } else {           if (t == 1) continue;           cur |= (t >> 1);         }       }       printf( %lld n , cur);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool iss(int d) {     for (int i = 2; i * i <= d; ++i)       if (d % i == 0) return false;     return true;   }   int main() {     int a, b, c;     scanf( %d%d%d , &a, &b, &c);     vector<int> simple;     for (int i = 2; i < 100; ++i)       if (iss(i)) simple.push_back(i);     vector<vector<int> > d(101);     for (int i = 1; i <= 100; ++i) {       d[i].resize(simple.size());       for (int j = 0, tmp = i; j < simple.size(); ++j)         while (tmp % simple[j] == 0) {           tmp /= simple[j];           d[i][j]++;         }     }     int ans = 0;     int mod = 1 << 30;     for (int i = 1; i <= a; ++i)       for (int j = 1; j <= b; ++j)         for (int k = 1; k <= c; ++k) {           int prod = 1;           for (int l = 0; l < simple.size(); ++l)             prod *= d[i][l] + d[j][l] + d[k][l] + 1;           ans = (ans + prod) % mod;         }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char s[60][30];   long long id[1 << 20];   int num[1 << 20];   double f[1 << 20];   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       scanf(  %s , &s[i]);     }     int m = strlen(s[1]);     for (int i = 1; i <= n; i++) {       for (int j = i + 1; j <= n; j++) {         int mask = 0;         for (int k = 0; k < m; k++) {           mask |= s[i][k] == s[j][k] ? (1ll << k) : 0;         }         id[mask] |= (1ll << i) | (1ll << j);       }     }     for (int mask = (1 << m) - 1; mask >= 0; mask--) {       for (int i = 0; i < m; i++) {         if ((mask >> i) & 1) {           id[mask ^ (1 << i)] |= id[mask];         }       }     }     for (int i = 0; i <= (1 << m) - 1; i++) {       long long tmp = id[i];       int ans = 0;       while (tmp) {         ans += tmp & 1;         tmp >>= 1;       }       num[i] = ans;       if (num[i] == 1) num[i] = 0;     }     f[(1 << m) - 1] = 0;     for (int s = (1 << m) - 2; s >= 0; s--) {       if (!num[s]) {         f[s] = 0;         continue;       }       int tot = 0;       for (int i = 0; i < m; i++)         if (!(s & (1 << i))) tot++;       for (int i = 0; i < m; i++) {         if (((1 << i) & s)) continue;         f[s] += (1.0 * f[s | (1 << i)] / (double)tot * (double)num[s | (1 << i)] /                  (double)num[s]);       }       f[s] += 1.0;     }     printf( %.10lf , f[0]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     int l, r, res = 0;     cin >> l >> r;     vector<unsigned long long> two, three, temp;     for (int i = 0; i < 31; ++i) {       two.push_back(1LL << i);       if (i > 19) continue;       three.push_back(pow(3LL, i));     }     for (int i = 0; i < two.size(); ++i) {       for (int j = 0; j < three.size(); ++j) {         if (two[i] * three[j] <= 2000000000LL) temp.push_back(two[i] * three[j]);       }     }     sort(temp.begin(), temp.end());     res = upper_bound(temp.begin(), temp.end(), r) -           lower_bound(temp.begin(), temp.end(), l);     cout << res;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int T, arr[100005], a, b, ans;   int cal(int v) {     int vv = v * (v - 1);     return (vv >> 1);   }   int main() {     cin >> T;     for (int i = 1; i < T; i++) {       cin >> a >> b;       arr[a]++;       arr[b]++;     }     for (int i = 1; i <= T; i++) {       if (arr[i] >= 2) ans += cal(arr[i]);     }     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 200010;   struct node {     int x, y;   } a[maxn];   int ans[maxn];   int f[maxn];   bool cmp(int c, int d) { return c < d; }   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       scanf( %d , &f[i]);     }     stack<int> s;     node ff;     ff.x = 0;     ff.y = 0;     s.push(0);     a[0].y = 0;     a[0].x = 0;     for (int i = 1; i <= m; i++) {       scanf( %d%d , &a[i].x, &a[i].y);     }     for (int i = m; i >= 1; i--) {       if (a[i].y > a[s.top()].y) {         s.push(i);       }     }     int flag = n;     int kk = 0;     int gg = a[s.top()].y;     for (int i = gg + 1; i <= n; i++) ans[i] = f[i];     sort(f + 1, f + 1 + gg);     int l = 1;     int r = gg;     int per = s.top();     s.pop();     while (!s.empty()) {       int now = s.top();       s.pop();       if (a[per].x == 1) {         for (int i = a[per].y; i > a[now].y; i--) ans[i] = f[r--];       } else {         for (int i = a[per].y; i > a[now].y; i--) {           ans[i] = f[l++];         }       }       per = now;     }     for (int i = 1; i < n; i++) {       printf( %d  , ans[i]);     }     printf( %d n , ans[n]);     return 0;   }
#include <bits/stdc++.h>   #pragma warning(disable : 4786)   using namespace std;   int a[200000];   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);     }     a[n + 1] = a[1];     int y = 0;     for (int i = 2; i <= n + 1; i++) {       if (a[i] < a[i - 1]) {         if (y != 0) {           printf( -1 n );           return 0;         } else {           y = i;         }       }     }     if (y == 0) y = n + 1;     printf( %d , n - y + 1);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxN = 11111;   pair<int, int> a[maxN];   short dp[maxN][maxN];   int s[maxN];   bool mycmp(pair<int, int> a, pair<int, int> b) {     if (a.second != b.second) return (a.second < b.second);     return (a.first > b.first);   }   int main() {     ios_base::sync_with_stdio(false);     int n, l, r;     cin >> n >> l >> r;     for (int i = 1; i <= n; ++i) {       cin >> a[i].first;     }     for (int i = 1; i <= n; ++i) {       cin >> a[i].second;     }     sort(a + 1, a + n + 1, mycmp);     int sum = 0;     for (int i = 1; i <= n; ++i) {       sum += a[i].first;     }     s[0] = 0;     for (int i = 1; i <= n; ++i) {       s[i] = s[i - 1] + a[i].first;     }     for (int i = 0; i <= n; ++i) {       for (int j = 0; j <= sum; ++j) {         dp[i][j] = -1;       }     }     dp[0][0] = 0;     for (int i = 0; i < n; ++i) {       for (int j = 0; j <= s[i]; ++j) {         if (dp[i][j] < 0) continue;         int type = a[i + 1].second, w = a[i + 1].first;         if (type == 0) {           dp[i + 1][j + w] = max(dp[i + 1][j + w], dp[i][j]);           dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);         } else {           int x = j, y = sum - (s[i] - j);           short f1 = (x >= l && x <= r);           if (dp[i][j] + f1 > dp[i + 1][j + w]) {             dp[i + 1][j + w] = dp[i][j] + f1;           }           short f2 = (y - a[i + 1].first >= l && y - a[i + 1].first <= r);           if (dp[i][j] + f2 > dp[i + 1][j]) {             dp[i + 1][j] = dp[i][j] + f2;           }         }       }     }     short ans = -1;     for (int i = 0; i <= sum; ++i) {       ans = max(ans, dp[n][i]);     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   pair<int, pair<int, int> > a[200020];   int f[200020];   int v[200020];   int n, m;   int F(int x) {     if (f[x] == x) {       return x;     }     f[x] = F(f[x]);     v[f[x]] |= v[x];     return f[x];   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       f[i] = i;       v[i] = 0;     }     for (int i = 0; i < m; i++) {       scanf( %d%d%d , &a[i].second.first, &a[i].second.second, &a[i].first);     }     sort(a, a + m);     reverse(a, a + m);     long long ans = 0;     for (int i = 0; i < m; i++) {       int x = a[i].second.first;       int y = a[i].second.second;       x = F(x);       y = F(y);       if (x == y && v[x] == 0) {         v[x] = 1;         ans += a[i].first;       } else if (x != y && (v[x] == 0 || v[y] == 0)) {         v[y] |= v[x];         f[x] = y;         ans += a[i].first;       }     }     cout << ans << endl;     return 0;   }