malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e9 + 7;   const int N = 1e3 + 10;   const int M = 1e4;   int a[N][N];   int main() {     int n, k, d;     cin >> n >> k >> d;     int flag = 1;     memset((a), (0), sizeof(a));     if (n == 1) {       while (d--) cout << 1 << endl;       return 0;     }     for (int i = 1; i <= d; i++) a[1][i] = 1;     for (int i = 2; i <= n; i++) {       for (int j = 1; j <= d; j++) a[i][j] = a[i - 1][j];       a[i][d]++;       for (int j = d; j > 0; j--) {         if (a[i][j] > k)           a[i][j] = 1, a[i][j - 1]++;         else           break;       }       if (a[i][0] == 1) {         flag = 0;         break;       }     }     if (!flag) {       cout << -1 << endl;       return 0;     }     for (int j = 1; j <= d; j++) {       for (int i = 1; i <= n; i++) {         if (i > 1) cout <<    ;         cout << a[i][j];       }       cout <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = (int)3e3 + 5;   const long long INF = (long long)1e15;   pair<int, long long> dp[MAXN][MAXN];   vector<int> adj[MAXN];   int arr[MAXN];   int sub[MAXN];   int n, m;   void dfs(int v, int pr = -1) {     fill(dp[v], dp[v] + n + 1, make_pair(-n, -INF));     dp[v][0] = make_pair(0, 0);     sub[v] = 1;     for (int to : adj[v]) {       if (to == pr) {         continue;       }       dfs(to, v);     }     vector<pair<int, long long> > ndp(n + 5, make_pair(-n, -INF));     for (int to : adj[v]) {       if (to == pr) {         continue;       }       for (int i = 0; i <= sub[v] + sub[to] + 3; ++i) {         ndp[i] = make_pair(-n, -INF);       }       for (int i = sub[v]; i >= 0; --i) {         for (int j = sub[to]; j >= 0; --j) {           if (dp[v][i].second == -INF || dp[to][j].second == -INF ||               dp[v][i].first < 0 || dp[to][j].first < 0) {             continue;           }           if (dp[to][j].second > 0) {             ndp[i + j + 1] = max(                 ndp[i + j + 1],                 make_pair(dp[v][i].first + dp[to][j].first + 1, dp[v][i].second));           } else {             ndp[i + j + 1] =                 max(ndp[i + j + 1],                     make_pair(dp[v][i].first + dp[to][j].first, dp[v][i].second));           }           ndp[i + j] =               max(ndp[i + j], make_pair(dp[v][i].first + dp[to][j].first,                                         dp[v][i].second + dp[to][j].second));         }       }       for (int j = 0; j < n; ++j) {         dp[v][j] = ndp[j];       }       sub[v] += sub[to];     }     for (int i = 0; i < n; ++i) {       if (dp[v][i].second != -INF) {         dp[v][i].second += arr[v];       }     }   }   void solve() {     scanf( %d %d , &n, &m);     for (int i = 1; i <= n; ++i) {       adj[i].clear();       arr[i] = 0;     }     for (int i = 1, x; i <= n; ++i) {       scanf( %d , &x);       arr[i] = -x;     }     for (int i = 1, x; i <= n; ++i) {       scanf( %d , &x);       arr[i] += x;     }     for (int i = 1; i < n; ++i) {       int u, v;       scanf( %d %d , &u, &v);       adj[u].push_back(v);       adj[v].push_back(u);     }     dfs(1);     printf( %d n , dp[1][m - 1].first + (dp[1][m - 1].second > 0));   }   int main() {     int tt;     scanf( %d , &tt);     while (tt--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   int main() {     scanf( %d , &n);     if (n & 1)       printf( %d n , (n - 1) / 2);     else {       int w = 1;       while (w <= n) w *= 2;       w /= 2;       printf( %d n , (n - w) / 2);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int solve(vector<int> a, int bit) {     if (bit == 0) {       return a.size();     }     if (a.size() <= 1) {       return a.size();     }     vector<int> v0, v1;     for (int x : a) {       if (1 & (x >> bit)) {         v0.push_back(x);       } else {         v1.push_back(x);       }     }     return max(solve(v0, bit - 1) + (v1.size() ? 1 : 0),                solve(v1, bit - 1) + (v0.size() ? 1 : 0));   }   int main() {     int n;     cin >> n;     vector<int> arr(n);     for (int i = 0; i < n; i++) {       cin >> arr[i];     }     int ans = solve(arr, 30);     cout << n - ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   T gcd(T x, T y) {     if (x < y) swap(x, y);     while (y > 0) {       T f = x % y;       x = y;       y = f;     }     return x;   }   const int maxn = 100007;   int BLOCKSIZE = 350;   set<int> s[maxn];   int a[maxn];   int nxt[maxn];   int n, m;   long long b[maxn];   int d[maxn];   long long query(int block, int R) {     int iBlockStart = block * BLOCKSIZE;     int iBlockEnd = min((block + 1) * BLOCKSIZE, n);     int idx = upper_bound(d + iBlockStart, d + iBlockEnd, R,                           [](int i, int j) { return i < nxt[j]; }) -               d;     if (idx == iBlockStart) return 0;     return b[idx - 1];   }   void updateBlock(int block) {     int iBlockStart = block * BLOCKSIZE;     int iBlockEnd = min((block + 1) * BLOCKSIZE, n);     sort(d + iBlockStart, d + iBlockEnd,          [](int i, int j) { return nxt[i] < nxt[j]; });     b[iBlockStart] = nxt[d[iBlockStart]] - d[iBlockStart];     for (int i = iBlockStart + 1; i < iBlockEnd; i++)       b[i] = b[i - 1] + nxt[d[i]] - d[i];   }   int main(int argc, char *argv[]) {     std::cin.sync_with_stdio(false);     std::cin.tie(nullptr);     cin >> n >> m;     {       BLOCKSIZE = 1;       while (BLOCKSIZE * BLOCKSIZE < n) BLOCKSIZE++;       BLOCKSIZE = max(BLOCKSIZE, 10);       for (int i = 0; i < n; i++) nxt[i] = n + 1, d[i] = i;       for (int i = 0; i < n; i++) {         cin >> a[i];         if (!s[a[i]].empty()) nxt[*s[a[i]].rbegin()] = i;         s[a[i]].insert(i);       }       for (int i = 0; i * BLOCKSIZE < n; i++) updateBlock(i);       for (int i = 0; i < m; i++) {         int op;         cin >> op;         if (op == 1) {           int p, x;           cin >> p >> x;           p--;           if (a[p] == x) continue;           {             auto it = s[a[p]].find(p);             if (it != s[a[p]].begin()) {               int pr = *prev(it);               auto nxt_it = it == s[a[p]].end() ? it : next(it);               nxt[pr] = nxt_it == s[a[p]].end() ? (n + 1) : *nxt_it;               updateBlock(pr / BLOCKSIZE);             }           }           s[a[p]].erase(p);           a[p] = x;           nxt[p] = n + 1;           if (!s[x].empty()) {             auto it = s[x].upper_bound(p);             if (it != s[x].begin()) {               int pr = *prev(it);               nxt[pr] = p;               updateBlock(pr / BLOCKSIZE);             }             if (it != s[x].end()) {               nxt[p] = (it == s[x].end()) ? (n + 1) : *it;             }           }           s[a[p]].insert(p);           updateBlock(p / BLOCKSIZE);         } else {           int l, r, R;           cin >> l >> r;           l--;           r--;           R = r;           long long res = 0;           for (; l <= r && l % BLOCKSIZE; l++)             if (nxt[l] <= R) res += nxt[l] - l;           for (; l <= r && (r + 1) % BLOCKSIZE; r--)             if (nxt[r] <= R) res += nxt[r] - r;           if (l <= r) {             for (int i = l / BLOCKSIZE, j = r / BLOCKSIZE; i <= j; i++) {               res += query(i, R);             }           }           cout << res << endl;         }       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool comp(pair<long long int, long long int> &a,             pair<long long int, long long int> &b) {     if (a.first == b.first) return a.second < b.second;     return a.first > b.first;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int n, m, d;     cin >> n >> m >> d;     pair<long long int, long long int> py[n], in[m];     for (int i = 0; i < n; i++) cin >> py[i].first >> py[i].second;     for (int i = 0; i < m; i++) cin >> in[i].first >> in[i].second;     sort(py, py + n, comp);     sort(in, in + m, comp);     int p1 = 0, p2 = 1;     long long int sm = py[0].first + in[0].first;     long long int w = py[0].second + in[0].second;     if (w > d) {       cout <<  0 n ;       return 0;     }     long long int ans = sm;     while (p2 < m) {       sm += in[p2].first;       w += in[p2].second;       if (w <= d) ans = sm;       p2++;     }     p2 = m - 1;     while (p1 != n - 1) {       p1++;       sm += py[p1].first;       w += py[p1].second;       while (w > d and p2 > 0) {         sm -= in[p2].first;         w -= in[p2].second;         p2--;       }       if (w <= d) ans = max(ans, sm);     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a, b, h, n, i;   long long x, ans;   pair<pair<int, int>, int> p[100005];   map<long long, long long> f;   void update(int index, long long value) {     while (index < 1e9) {       f[index] = max(f[index], value);       index += (-index & index);     }   }   long long read(int index) {     long long ans = 0;     while (index > 0) {       ans = max(ans, f[index]);       index -= (-index & index);     }     return ans;   }   int main() {     scanf( %d , &n);     for (i = 0; i < n; i++)       scanf( %d%d%d , &p[i].first.second, &p[i].first.first, &p[i].second);     sort(p, p + n);     reverse(p, p + n);     for (i = 0; i < n; i++) {       x = read(p[i].first.first) + p[i].second;       update(p[i].first.second + 1, x);       ans = max(ans, x);     }     printf( %lld , ans);   }
#include <bits/stdc++.h>   using namespace std;   char str[200005];   int main() {     int ts;     scanf( %d , &ts);     while (ts--) {       int n;       scanf( %d %s , &n, str);       vector<int> vec;       for (int i = 0; i < n; i++) {         int j = i, c = 0;         while (i < n && str[i] == str[j]) i++, c++;         i--;         vec.push_back(c);       }       if (vec.size() == 1 && vec[0] > 2) vec[0] += 2;       if (vec.size() != 1 && str[0] == str[n - 1])         vec[0] += vec.back(), vec.pop_back();       int res = 0;       for (int i = 0; i < vec.size(); i++) res += vec[i] / 3;       printf( %d n , res);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }   long long lcm(long long a, long long b) { return a * b / gcd(a, b); }   int main() {     int n;     long long k;     scanf( %d%I64d , &n, &k);     long long ans = 1;     bool ok = false;     for (int i = 0; i < n; ++i) {       long long x;       scanf( %I64d , &x);       if (ans) ans = lcm(ans, x) % k;       if (ans == 0) ok = true;     }     if (ok)       printf( Yes n );     else       printf( No n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int mr[] = {0, 1, 0, -1};   int mc[] = {1, 0, -1, 0};   char a[52][52];   int id[52][52];   int ar[2500], br[2500], ac[2500], bc[2500];   void flood(int r, int c, char color, int tag) {     ar[tag] = min(ar[tag], r);     br[tag] = max(br[tag], r);     ac[tag] = min(ac[tag], c);     bc[tag] = max(bc[tag], c);     id[r][c] = tag;     for (int o = 0; o < 4; o++) {       int dr = r + mr[o];       int dc = c + mc[o];       if (a[dr][dc] == color && id[dr][dc] < 0) flood(dr, dc, color, tag);     }   }   int main() {     int n, m, s, cnt = 0;     scanf( %d%d , &n, &s);     for (int i = 1; i <= n; i++) scanf( %s , a[i] + 1);     m = strlen(a[1] + 1);     memset(id, 255, sizeof(id));     memset(ar, 127, sizeof(ar));     memset(br, 128, sizeof(br));     memset(ac, 127, sizeof(ac));     memset(bc, 128, sizeof(bc));     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (id[i][j] < 0) flood(i, j, a[i][j], cnt++);     int r = 1, c = 1, dp = 0, cp = 0;     while (s--) {       int tr = r, tc = c, tag = id[r][c];       if (dp == 0 && cp == 0) tr = ar[tag], tc = bc[tag] + 1;       if (dp == 0 && cp == 1) tr = br[tag], tc = bc[tag] + 1;       if (dp == 1 && cp == 0) tr = br[tag] + 1, tc = bc[tag];       if (dp == 1 && cp == 1) tr = br[tag] + 1, tc = ac[tag];       if (dp == 2 && cp == 0) tr = br[tag], tc = ac[tag] - 1;       if (dp == 2 && cp == 1) tr = ar[tag], tc = ac[tag] - 1;       if (dp == 3 && cp == 0) tr = ar[tag] - 1, tc = ac[tag];       if (dp == 3 && cp == 1) tr = ar[tag] - 1, tc = bc[tag];       if (!a[tr][tc] || a[tr][tc] ==  0 ) {         cp ^= 1;         if (!cp) dp = (dp + 1) & 3;       } else {         r = tr;         c = tc;       }     }     printf( %c n , a[r][c]);   }
#include <bits/stdc++.h>   using namespace std;   int hx[] = {-2, -2, -1, -1, 1, 1, 2, 2};   int hy[] = {-1, 1, -2, 2, -2, 2, -1, 1};   int dx[] = {1, -1, 0, 0};   int dy[] = {0, 0, 1, -1};   int nx[] = {1, -1, 0, 0, -1, 1, 1, -1};   int ny[] = {0, 0, 1, -1, 1, -1, 1, -1};   class pt {    public:     int x, y;   };   int main() {     int t = 1;     while (t--) {       long long n, m;       cin >> n >> m;       long long ans = 1, tmp = 3;       for (int i = 0; i < 32; i++) {         if (n & 1 << i) ans = (ans * tmp) % m;         tmp = (tmp * tmp) % m;       }       if (ans == 0) ans = m;       cout << ans - 1 << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int N, K, T, to[25];   long double prob[25], dp[2000100], val[25], ans[25];   bool v[2000100];   long double calc(int mask) {     if (v[mask]) return dp[mask];     v[mask] = 1;     int i;     long double tot = 0;     for (i = 0; i < N; i++)       if (mask & (1 << i)) tot += prob[i];     for (i = 0; i < N; i++)       if (mask & (1 << i)) {         tot -= prob[i];         dp[mask] = dp[mask] + (prob[i] * calc(mask - (1 << i))) / (1 - tot);         tot += prob[i];       }     return dp[mask];   }   void get_masks(int mask, int cnt, int p) {     if (p == N || cnt == K) {       if (cnt != K) return;       calc(mask);       int i;       for (i = 0; i < N; i++)         if (mask & (1 << i)) val[i] += dp[mask];       return;     }     get_masks(mask, cnt, p + 1);     get_masks(mask + (1 << p), cnt + 1, p + 1);   }   int main() {     cin >> N >> K;     int i;     for (i = 0; i < N; i++) {       cin >> prob[i];       if (prob[i] != 0) {         T++;         prob[T - 1] = prob[i];         to[T - 1] = i;       }     }     swap(T, N);     if (K > N) K = N;     dp[0] = 1, v[0] = 1;     get_masks(0, 0, 0);     for (i = 0; i < N; i++) ans[to[i]] = val[i];     for (i = 0; i < T; i++) cout << fixed << setprecision(9) << ans[i] <<    ;     cout <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<long long> graph[200010];   long long v[200010], new_visit[200010], MAX_node, mx, cnt;   void dfs(long long node) {     v[node] = 1;     if ((long long)graph[node].size() > mx) {       mx = (long long)graph[node].size();       MAX_node = node;     }     for (auto &x : graph[node])       if (!v[x]) dfs(x);   }   void bfs(long long node) {     queue<long long> q;     q.push(node);     while (!q.empty()) {       long long frt = q.front();       new_visit[frt] = 1;       q.pop();       for (auto &x : graph[frt]) {         if (!new_visit[x]) {           new_visit[x] = 1;           cout << frt <<     << x << endl;           q.push(x);           cnt--;           if (cnt == 0) return;         }       }     }   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long n, m;     cin >> n >> m;     cnt = n - 1;     for (long long i = 0; i < m; i++) {       long long x, y;       cin >> x >> y;       graph[x].push_back(y);       graph[y].push_back(x);     }     dfs(1);     bfs(MAX_node);   }
#include <bits/stdc++.h>   using namespace std;   vector<int> p;   vector<pair<int, int> > v, ans;   bool cmp(pair<int, int> a, pair<int, int> b) {     if (a.first == b.first)       return a.second > b.second;     else       return a.first < b.first;   }   int main() {     int n;     cin >> n;     int a, b;     for (int i = 0; i < n; i++) {       cin >> a;       p.push_back(a);     }     for (int i = 0; i < n; i++) {       cin >> b;       v.push_back({b, i});     }     sort(p.begin(), p.end());     sort(v.begin(), v.end(), cmp);     for (int i = 0; i < n; i++) {       ans.push_back({v[i].second, p[n - i - 1]});     }     sort(ans.begin(), ans.end());     for (int i = 0; i < n; i++) cout << ans[i].second <<    ;     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 1000000007;   const long long INF64 = 1000000000000000007;   int n;   int p[17]{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59};   int a[105];   int number[60];   int dp[105][1 << 17];   int pred[105][1 << 17];   void find_pred(int x, int p) {     if (x == n) return;     cout << pred[x][p] <<    ;     find_pred(x + 1, p | number[pred[x][p]]);   }   int main() {     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 2; i < 60; i++) {       for (int j = 0; j < 17; j++) {         if (i % p[j] == 0) {           number[i] |= (1 << j);         }       }     }     for (int i = n - 1; i >= 0; i--) {       for (int j = 0; j < (1 << 17); j++) {         dp[i][j] = INF;         for (int k = 1; k < 60; k++) {           if (!(j & number[k])) {             int w = dp[i + 1][j | number[k]] + abs(k - a[i]);             if (dp[i][j] > w) {               dp[i][j] = w;               pred[i][j] = k;             }           }         }       }     }     find_pred(0, 0);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, m, budget;   long long price[100005], perMoney[100005];   bool can(int k) {     long long curBudget = 0;     int j = 0;     for (int i = k - 1; i >= 0; i--, j++) {       curBudget += max(0ll, price[j] - perMoney[i]);     }     return curBudget <= budget;   }   int main() {     cin >> n >> m >> budget;     for (int i = 0; i < n; i++) cin >> perMoney[i];     for (int i = 0; i < m; i++) cin >> price[i];     sort(price, price + m);     sort(perMoney, perMoney + n);     reverse(perMoney, perMoney + n);     long long lo = 0, hi = min(m, n);     while (lo < hi) {       long long mid = (lo + hi + 1) / 2;       if (can(mid))         lo = mid;       else         hi = mid - 1;     }     long long spent = 0;     for (int i = 0; i < lo; i++) {       spent += price[i];     }     cout << lo <<     << max(0ll, spent - budget) <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   struct Node {     int loc;     int s;     int v;     Node(int ll, int ss, int vv) : loc(ll), s(ss), v(vv) {}     bool operator<(const Node& other) const { return v > other.v; }   };   vector<pair<int, int> > adj[32];   int main() {     priority_queue<Node> que;     int N, K, T;     scanf( %d%d%d , &N, &K, &T);     while (K--) {       int h, w, r;       scanf( %d%d%d , &h, &w, &r);       h--;       w--;       adj[h].push_back(pair<int, int>(w, r));     }     que.push(Node(0, 0, 0));     int now = 0;     while (now + 1 < T) {       Node p = que.top();       que.pop();       now++;       for (int h = p.loc; h < N; h++) {         for (int i = 0; i < adj[h].size(); i++) {           pair<int, int> w = adj[h][i];           if (!(p.s & (1 << w.first)))             que.push(Node(h + 1, p.s | (1 << w.first), p.v + w.second));         }       }     }     printf( %d n , que.top().v);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long t, n, i, j, k, b[200005], a[200005], x, m, l, r, mid, ans, s;   vector<long long> u, v;   int main() {     ios::sync_with_stdio(NULL);     cin.tie(0);     cout.tie(0);     cin >> n;     for (i = 0; i < n; i++) {       cin >> x;       if (x % 2)         v.push_back(x);       else         u.push_back(x);       s += x;     }     sort(u.begin(), u.end(), greater<long long>());     sort(v.begin(), v.end(), greater<long long>());     n = v.size();     m = u.size();     n = min(n, m);     for (i = 0; i < n; i++) s -= u[i] + v[i];     if (u.size() > v.size()) s -= u[i];     if (u.size() < v.size()) s -= v[i];     cout << s;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   int visit[111111];   vector<int> v[111111], mark[111111];   int dfs(int t) {     vector<int> v1;     int i;     for (i = 0; i < v[t].size(); i++) {       int u = v[t][i];       if (visit[mark[t][i]] == 0) {         visit[mark[t][i]] = 1;         int w = dfs(u);         if (w == 0)           v1.push_back(u);         else           printf( %d %d %d n , t, u, w);       }     }     while (v1.size() > 1) {       printf( %d %d %d n , v1[v1.size() - 1], t, v1[v1.size() - 2]);       v1.pop_back();       v1.pop_back();     }     if (v1.size() == 1)       return v1[0];     else       return 0;   }   int main() {     memset(visit, 0, sizeof(visit));     scanf( %d%d , &n, &m);     int i;     for (i = 0; i < m; i++) {       int a, b;       scanf( %d%d , &a, &b);       v[a].push_back(b);       v[b].push_back(a);       mark[a].push_back(i);       mark[b].push_back(i);     }     if (m % 2)       printf( No solution n );     else {       for (i = 1; i <= n; i++) dfs(i);     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     vector<int> vi1, vi2;     for (int i = 0; i < n; i++) {       int x;       cin >> x;       vi1.push_back(x);     }     sort(vi1.begin(), vi1.end());     vi1.push_back(1e9);     for (int i = 0; i < m; i++) {       int a, b, c;       cin >> a >> b >> c;       if (a == 1) vi2.push_back(b);     }     sort(vi2.begin(), vi2.end());     int j = 0, mini = vi2.size();     for (int i = 0; i <= n; i++) {       int hi = vi1[i];       while (j < vi2.size() && vi2[j] < hi) {         j++;       }       int asdf = vi2.size();       mini = min(mini, i + asdf - j);     }     cout << mini;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   int main() {     int t, h, g;     cin >> t;     while (t--) {       scanf( %d %d , &h, &g);       int n = (1 << h) - 1, m = (1 << g) - 1;       std::vector<pair<int, int> > a(1 << h);       for (int i = 0; i < n; i++) {         scanf( %d , &a[i].first);         a[i].second = i + 1;       }       sort(a.begin(), a.end());       std::vector<int> notFree(n + 1, 0), taken(n + 1, 0);       ll ans = 0LL;       for (int i = 0; i < n; i++) {         int where = a[i].second;         while (where > m or notFree[where]) where = where >> 1;         if (!where) continue;         if (notFree[where << 1] == notFree[where << 1 | 1]) {           notFree[where] = 1;           ans += a[i].first;           taken[a[i].second] = 1;         }       }       printf( %lld n , ans);       for (int i = n; i >= 1; i--) {         if (!taken[i]) printf( %d  , i);         ;       }       puts(  );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void solve() {     int n, cnt = 0;     cin >> n;     vector<vector<int>> adj(n);     vector<int> deg(n);     for (int i = 0; i < n - 1; i++) {       int x, y;       cin >> x >> y;       x--, y--;       adj[x].push_back(y);       adj[y].push_back(x);       deg[x]++, deg[y]++;     }     for (int i = 0; i < n; i++)       if (deg[i] == 1)         cnt++;       else if (deg[i] == 2) {         cnt = 0;         break;       }     if ((long long)cnt * (cnt - 1) >= 2 * (n - 1))       cout <<  YES  <<   n ;     else       cout <<  NO  <<   n ;   }   int main() {     ios_base ::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     solve();     return 0;   }