malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   template <typename T>   bool Chkmax(T &x, T y) {     return x < y ? x = y, true : false;   }   template <typename T>   bool Chkmin(T &x, T y) {     return y < x ? x = y, true : false;   }   const int MAXN = 1e5 + 1e4, MAXK = 5 + 5;   const double eps = 1e-4;   int n;   int k;   std::pair<int, int> ans[MAXK];   inline int Dcmp(const double &x) {     if (fabs(x) < eps) return 0;     return x > 0 ? 1 : -1;   }   struct Point {     double x, y;     Point() {}     Point(double x, double y) : x(x), y(y) {}   };   struct Line {     int A, B, C, id;     Line() {}     Line(int A, int B, int C, int id) : A(A), B(B), C(C), id(id) {}     inline bool OnLine(const Point &p) {       return Dcmp(A * p.x + B * p.y + C) == 0;     }   } s[MAXN];   Point GetInter(Line l1, Line l2) {     if (l2.A == 0) std::swap(l1, l2);     double x, y;     x = (double)(-l2.C + (double)l2.B * l1.C / l1.B) /         (l2.A - (double)l1.A * l2.B / l1.B);     y = (double)(-l1.C - l1.A * x) / l1.B;     return Point(x, y);   }   bool Work(int N, int K) {     if (N <= K) {       printf( YES n%d n , k - K + N);       for (int i = 1, _ = N; i <= _; i++) printf( %d %d n , s[i].id, -1);       return true;     }     if (K == 0) return false;     Point p[MAXK * MAXK];     std::pair<int, int> fr[MAXK * MAXK];     int tot = 0;     for (int i = 1, _ = K + 1; i <= _; i++)       for (int j = i + 1, _ = K + 1; j <= _; j++) {         ++tot;         p[tot] = GetInter(s[i], s[j]);         fr[tot] = std::make_pair(s[i].id, s[j].id);       }     for (int i = 1, _ = tot; i <= _; i++) {       int newN = N;       for (int j = 1; j <= newN; j++) {         if (s[j].OnLine(p[i])) {           std::swap(s[j], s[newN--]);           j--;         }       }       if (Work(newN, K - 1)) {         printf( %d %d n , fr[i].first, fr[i].second);         return true;       }     }     return false;   }   void Init() {     int A, B, C;     scanf( %d%d , &n, &k);     for (int i = 1, _ = n; i <= _; i++) {       scanf( %d%d%d , &A, &B, &C);       s[i] = Line(A, B, C, i);     }   }   int main() {     if (fopen( D.in ,  r ) != NULL) {       freopen( D.in ,  r , stdin);       freopen( D.out ,  w , stdout);     }     Init();     if (!Work(n, k)) printf( NO n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n = ({       int x;       scanf( %d , &x);       x;     });     string s;     cin >> s;     string ans =  no ;     for (int d = (1); d < (30); d += (1)) {       for (int i = (0); i < (n); i += (1)) {         int g = 1;         for (int j = (0); j < (5); j += (1)) {           if (i + j * d >= n || s[i + j * d] ==  . ) {             g = 0;           }         }         if (g) {           ans =  yes ;           break;         }       }     }     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   using ull = unsigned long long;   using pii = pair<int, int>;   using C = complex<double>;   const double PI = 3.14159265;   const double eps = 1e-8;   const int N = 1 << 17;   const int M = 1e9 + 7;   const int inf = 1e9 + 7;   const ll INF = 1e18;   ll pw(ll a, ll b) {     ll t = 1;     a %= M;     for (; b; b >>= 1) {       if (b & 1) {         t = t * a % M;       }       a = a * a % M;     }     return t;   }   ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }   int n;   int num[N];   int mx[N << 1];   int seg[N << 1];   int find() {     int u = 1, w = 0;     while (u < N) {       if (mx[u << 1 | 1] + w > 0) {         u = u << 1 | 1;       } else {         w += seg[u << 1 | 1];         u <<= 1;       }     }     return u - N;   }   void add(int p, int v) {     p += N;     seg[p] += v;     mx[p] = seg[p] == 1;     for (p >>= 1; p; p >>= 1) {       seg[p] = seg[p << 1] + seg[p << 1 | 1];       mx[p] = max(mx[p << 1 | 1], mx[p << 1] + seg[p << 1 | 1]);     }   }   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> n;     int p, t, x;     for (int i = 0; i < n; i++) {       cin >> p >> t;       if (t == 1) {         cin >> num[p];         add(p, 1);       } else {         add(p, -1);       }       int ps = find();       if (mx[1] <= 0) {         puts( -1 );       } else {         printf( %d n , num[ps]);       }     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       long long x, y;       cin >> x >> y;       long long l = x - y;       l = l < 0 ? -l : l;       int r = l % 10;       long long a = l / 10.0;       if (r) a++;       cout << a;       cout <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   const long double INF = 1e9;   const long double EPS = 1e-9;   long double dist(long double x1, long double y1, long double x2,                    long double y2) {     return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));   }   long double func(long double X, long double Y, long double L, long double R) {     return min(dist(L, 0, X, Y), dist(R, 0, X, Y)) + R - L;   }   int main() {     cout.setf(ios::fixed);     cout.precision(15);     int n, k;     cin >> n >> k;     k--;     vector<int> x(n);     for (int i = 0; i < n; i++) cin >> x[i];     int X, Y;     cin >> X >> Y;     int xStart;     if (k < n) xStart = x[k];     sort(x.begin(), x.end());     if (k == n) {       cout << func(X, Y, x[0], x[n - 1]);       return 0;     }     k = lower_bound(x.begin(), x.end(), xStart) - x.begin();     long double ans = INF;     for (int i = 0; i < n; i++) {       long double cur1 =           min(x[k] - x[0] + x[i] - x[0] + dist(x[i], 0, X, Y),               abs(x[i] - x[k]) + x[i] - x[0] + dist(x[0], 0, X, Y));       if (i + 1 < n) cur1 += func(X, Y, x[i + 1], x[n - 1]);       long double cur2 =           min(x[n - 1] - x[k] + x[n - 1] - x[i] + dist(x[i], 0, X, Y),               abs(x[k] - x[i]) + x[n - 1] - x[i] + dist(x[n - 1], 0, X, Y));       if (i - 1 >= 0) cur2 += func(X, Y, x[0], x[i - 1]);       ans = min(ans, min(cur1, cur2));     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int v, e;   vector<int> out[30005];   vector<int> ans;   bool vis[30005];   void dfstree(int n) {     for (auto x : out[n]) {       if (!vis[x]) {         vis[x] = true;         dfstree(x);       }     }     ans.push_back(n);   }   int main() {     cin >> v >> e;     for (int i = 0; i < e; i++) {       int x, y;       cin >> x >> y;       out[x].push_back(y);     }     for (int i = 1; i < v + 1; i++) {       if (!vis[i]) {         vis[i] = true;         dfstree(i);       }     }     for (auto x : ans) cout << x <<    ;     cout <<   n ;     ;   }
#include <bits/stdc++.h>   using namespace std;   long long n, a[300005], ans;   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     sort(a + 1, a + n + 1);     for (int i = 1; i <= n / 2; i++) {       ans += (a[i] + a[n - i + 1]) * (a[i] + a[n - i + 1]);     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 100;   int n, m, ans;   int num[maxn];   set<int> st[2];   set<int>::iterator it, itt;   void get_st(int begin, int end, set<int> &st) {     st.clear();     st.insert(0);     for (int i = 0; i < (1 << (end - begin)); ++i) {       int tmp = 0;       for (int j = 0; j < end - begin; ++j) {         if (((i >> j) & 1) == 1) {           tmp = (tmp + num[j + begin]) % m;         }       }       st.insert(tmp);     }   }   int main() {     ios::sync_with_stdio(false);     while (scanf( %d%d , &n, &m) != EOF) {       ans = 0;       for (int i = 0; i < n; ++i) {         scanf( %d , &num[i]);       }       get_st(0, (n + 1) / 2, st[0]);       get_st((n + 1) / 2, n, st[1]);       for (it = st[0].begin(); it != st[0].end(); ++it) {         for (int i = 1; i <= 2; ++i) {           itt = st[1].lower_bound(m * i - *it);           if (itt != st[1].begin()) {             --itt;             ans = max(ans, (*it + *itt) % m);           }         }       }       printf( %d n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, a[200005], head[200005];   struct edge {     int to, nxt;     edge(int _t = -1, int _n = -1) {       to = _t;       nxt = _n;     }   } e[400005];   void addedge(int u, int v) {     e[m] = edge(v, head[u]);     head[u] = m++;     e[m] = edge(u, head[v]);     head[v] = m++;   }   vector<int> g;   int dp[200005], s;   void dfs1(int fa, int u) {     if (a[u] == -1)       dp[u] = 1;     else       dp[u] = 0;     for (int p = head[u]; p != -1; p = e[p].nxt) {       int v = e[p].to;       if (v == fa) continue;       dfs1(u, v);       dp[u] += dp[v];     }   }   void dfs2(int fa, int u) {     if (s == 0 || dp[u] == 0) return;     int x;     for (int p = head[u]; p != -1; p = e[p].nxt) {       int v = e[p].to;       x = v;       if (v == fa || dp[v] == 0) continue;       g.push_back(v + 1);       if (a[v] == -1) {         s--;         dp[v]--;       } else {         s++;         dp[v]++;       }       a[v] = -a[v];       dfs2(u, v);       if (s == 0) return;       g.push_back(u + 1);       if (a[u] == -1) {         s--;         dp[u]--;       } else {         s++;         dp[u]++;       }       a[u] = -a[u];     }     if (a[u] == -1) {       a[u] = 1;       if (fa == -1) {         g.push_back(x + 1);         g.push_back(1);         g.push_back(x + 1);         return;       } else {         g.push_back(fa + 1);         g.push_back(u + 1);         s--;         dp[u]--;         if (a[fa] == -1) {           s--;           dp[fa]--;         } else {           s++;           dp[fa]++;         }         a[fa] = -a[fa];       }     }   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       scanf( %d , &a[i]);       if (a[i] == -1) s++;     }     memset(head, -1, sizeof(head));     m = 0;     for (int i = 1; i < n; ++i) {       int u, v;       scanf( %d%d , &u, &v);       addedge(u - 1, v - 1);     }     if (s == 0) {       printf( 1 n );       return 0;     }     g.clear();     dfs1(-1, 0);     dfs2(-1, 0);     printf( 1 );     for (int i = 0; i < g.size(); ++i) printf(  %d , g[i]);     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a, b, c, d, p;   int main() {     cin >> a >> b >> c >> d;     d -= c - 1;     a -= c;     b -= c;     if (d == 2) {       a--;       b--;       p = 3;     }     if (d == 1) {       if (a) {         a--;         p = 1;       } else {         b--;         p = 2;       }     }     if (a < 0 || b < 0 || d > 2 || d < 0)       cout <<  -1 ;     else {       if (p & 2) cout <<  7 ;       for (; a > 0; a--) cout <<  4 ;       for (; c > 0; c--) cout <<  47 ;       for (; b > 0; b--) cout <<  7 ;       if (p & 1) cout <<  4 ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 10;   int n, k;   int ver[maxn], t[maxn], dp[maxn], tin[maxn],       cnt = 0, tout[maxn], h[maxn], p[maxn][30], important[maxn], ans = 0;   vector<int> e[maxn];   void dfs(int u, int par = 0) {     cnt++;     tin[u] = cnt;     h[u] = h[par] + 1;     for (int i = 0; i < e[u].size(); i++) {       int v = e[u][i];       if (v == par) continue;       p[v][0] = u;       for (int j = 1; j < 22; j++) p[v][j] = p[p[v][j - 1]][j - 1];       dfs(v, u);     }     tout[u] = cnt;     return;   }   int get(int u, int len) {     for (int i = 22; i >= 0; i--)       if (len & (1 << i)) u = p[u][i];     return u;   }   int lca(int u, int v) {     if (h[u] > h[v]) swap(u, v);     int len = h[v] - h[u];     v = get(v, len);     if (u == v) return u;     for (int i = 22; i >= 0; i--)       if (p[u][i] != p[v][i]) {         u = p[u][i];         v = p[v][i];       }     return p[u][0];   }   bool cmp(int u, int v) { return tin[u] < tin[v]; }   bool compare(int u, int v) { return tin[u] < tin[v] && tout[u] >= tout[v]; }   bool buildGraph() {     for (int i = 1; i < k; i++) ver[i + k] = lca(ver[i], ver[i + 1]);     sort(ver + 1, ver + 2 * k, cmp);     int cnter = unique(ver + 1, ver + 2 * k) - ver - 1;     for (int i = 1; i <= cnter; i++) e[ver[i]].clear();     stack<int> s;     s.push(ver[1]);     for (int i = 2; i <= cnter; i++) {       int v = ver[i];       while (!compare(s.top(), v)) s.pop();       int u = s.top();       if (important[u] && important[v] && u == p[v][0]) return 0;       s.push(v);       e[u].push_back(v);     }     return 1;   }   void dfsSolve(int u) {     int cnter = 0;     for (int i = 0; i < e[u].size(); i++) {       int v = e[u][i];       dfsSolve(v);       cnter += dp[v];     }     if (important[u]) {       ans += cnter;       dp[u] = 1;     } else {       if (cnter >= 2)         ans++, dp[u] = 0;       else if (cnter == 1)         dp[u] = 1;       else         dp[u] = 0;     }   }   int main() {     memset(h, 0, sizeof(h));     memset(tin, 0, sizeof(tin));     memset(tout, 0, sizeof(tout));     memset(dp, 0, sizeof(dp));     memset(p, 0, sizeof(p));     memset(important, 0, sizeof(important));     ios_base::sync_with_stdio(false);     cin >> n;     for (int i = 1; i < n; i++) {       int u, v;       cin >> u >> v;       e[u].push_back(v);       e[v].push_back(u);     }     dfs(1);     int q;     cin >> q;     while (q--) {       cin >> k;       for (int i = 1; i <= k; i++) {         cin >> ver[i];         important[ver[i]] = 1;         t[i] = ver[i];       }       sort(ver + 1, ver + k + 1, cmp);       if (!buildGraph()) {         cout << -1 << endl;       } else {         ans = 0;         dfsSolve(ver[1]);         cout << ans << endl;       }       for (int i = 1; i <= k; i++) important[t[i]] = 0;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   string ar;   string br;   int n = 0, lst = 0, lstmin = 0, lp = 0, lpc = 0;   int gzg = 0, rzg = 0, gzy2 = 0, rzy2 = 0;   int gzf = 0, gzc = 0, rzf = 0, rzc = 0;   int gzcc = 0, rzff = 0;   int main() {     int f = 0;     cin >> n;     ar.reserve(n + 1);     br.reserve(n + 1);     cin >> ar;     for (f = 0; f < n; f++) {       if (lst == lstmin) lpc++;       if (ar[f] ==  ( )         lst++;       else         lst--;       if (lst < lstmin) {         lstmin = lst;         lp = f + 1;         lpc = 0;       }     }     if (lst != 0) {       cout <<  0 n1 1 n ;       return 0;     }     rzg = lpc;     gzcc = lp;     rzff = lp;     for (f = lp; f < lp + n * 2 + 2; f++) {       if (ar[f % n] ==  ( )         lst++;       else         lst--;       switch (lst) {         case 0:           if (gzy2 <= gzg) {             gzy2 = gzg;             gzc = (gzcc + 1) % n + 1;             gzf = f % n + 1;           }           gzg = 0;           gzcc = f % n;           break;         case 1:           gzg++;           if (rzy2 <= rzg) {             rzy2 = rzg;             rzf = rzff % n + 1;             rzc = f % n + 1;           }           rzff = f % n + 1;           rzg = lpc;           break;         case 2:           rzg++;           break;       }     }     if (rzy2 >= gzy2 && rzy2 > lpc) {       cout << rzy2 <<   n  << rzf <<     << rzc <<   n ;     } else if (gzy2 > rzy2 && gzy2 > lpc) {       cout << gzy2 <<   n  << gzf <<     << gzc <<   n ;     } else {       cout << lpc <<   n1 1 n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int ToInt(string& s, int len) {     int r = 0;     for (int i = 0, max_ = (len); i < max_; ++i) {       r *= 10;       r += s[i] -  0 ;     }     return r;   }   int GCD(int a, int b) { return b != 0 ? GCD(b, a % b) : a; }   int LCM(int a, int b) { return a * (b / GCD(a, b)); }   long long Pow(long long n, long long e) {     if (e == 0) return 1;     if (e == 1)       return n;     else if (e & 1) {       long long t = Pow(n, e / 2);       return n * t * t;     } else {       long long t = Pow(n, e / 2);       return t * t;     }   }   int main() {     string s;     int N;     scanf( %d , &N);     getline(cin, s);     getline(cin, s);     int h = 0;     for (int i = 0, max_ = ((int)(s).size()); i < max_; ++i)       if (s[i] ==  H ) ++h;     int min = 10000;     for (int start = 0, max_ = ((int)(s).size()); start < max_; ++start) {       int swaps = 0;       for (int i = 0, max_ = (h); i < max_; ++i)         if (s[(start + i) % (int)(s).size()] ==  T ) ++swaps;       min = std::min(min, swaps);     }     printf( %d n , min);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int cnt[(100000 + 10)];   int f[(100000 + 10)];   int main() {     int n;     cin >> n;     int ans = 0;     int mx = 0;     for (int i = 1; i <= n; i++) {       int v;       cin >> v;       cnt[f[v]]--;       f[v]++;       cnt[f[v]]++;       int ok = 0;       mx = max(mx, f[v]);       if (cnt[1] == i) {         ok = 1;       } else if (cnt[mx] == i) {         ok = 1;       } else if (cnt[mx] * mx == i - 1 && cnt[1] == 1) {         ok = 1;       } else if (cnt[mx - 1] * (mx - 1) == i - mx && cnt[mx] == 1) {         ok = 1;       }       if (ok) {         ans = i;       }     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void amin(T &x, const T &y) {     if (y < x) x = y;   }   template <class T>   inline void amax(T &x, const T &y) {     if (x < y) x = y;   }   template <class Iter>   void rprintf(const char *fmt, Iter begin, Iter end) {     for (bool sp = 0; begin != end; ++begin) {       if (sp)         putchar(   );       else         sp = true;       printf(fmt, *begin);     }     putchar(  n );   }   int A, B, C;   int N;   void MAIN() {     scanf( %d%d%d%d , &A, &B, &C, &N);     int ans = 0;     for (int i = 0, i_len = (N); i < i_len; ++i) {       int x;       scanf( %d , &x);       if (B < x && x < C) ans++;     }     printf( %d n , ans);   }   int main() {     int TC = 1;     for (int tc = 0, tc_len = (TC); tc < tc_len; ++tc) MAIN();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct art {     int t, d, p, id;   } a[101];   pair<int, int> go[101][2001];   bool cmp(art xx, art yy) { return xx.d < yy.d; }   int f[101][2001], num[101][2001], ans[1000], tt;   int main() {     int n;     cin >> n;     for (register int i = 1; i <= n; i++)       cin >> a[i].t >> a[i].d >> a[i].p, a[i].id = i;     sort(a + 1, a + n + 1, cmp);     for (register int i = 1; i <= n; i++)       for (register int k = 1; k <= 2000; k++) {         if (k <= a[i].d && k - a[i].t > 0) {           f[i][k] = f[i - 1][k - a[i].t] + a[i].p,           go[i][k] = make_pair(i - 1, k - a[i].t), num[i][k] = i;         }         if (f[i][k - 1] > f[i][k]) {           f[i][k] = f[i][k - 1];           go[i][k] = make_pair(i, k - 1), num[i][k] = -1;         }         if (f[i - 1][k] > f[i][k]) {           f[i][k] = f[i - 1][k];           go[i][k] = make_pair(i - 1, k), num[i][k] = -1;           ;         }       }     cout << f[n][2000] << endl;     int n1 = n, n2 = 2000;     while (go[n1][n2].first) {       if (num[n1][n2] != -1) ans[++tt] = a[num[n1][n2]].id;       pair<int, int> cur = go[n1][n2];       n1 = cur.first;       n2 = cur.second;     }     if (num[n1][n2] != -1 && num[n1][n2] != 0) ans[++tt] = a[num[n1][n2]].id;     cout << tt << endl;     for (register int i = tt; i; i--) cout << ans[i] <<    ;     cout << endl;     return 0;   }
#include <bits/stdc++.h>   int main() {     int i = 0, z, flag = 0, even = 0;     char str[100002], temp;     scanf( %s , str);     int size = strlen(str);     char x = str[size - 1];     for (i = 0; i < size; i++) {       if (str[i] % 2 == 0) {         even = 1;         if (str[i] < x) {           flag = 1;           str[size - 1] = str[i];           str[i] = x;           break;         } else {           z = i;         }       }     }     if (i >= size && even == 1) {       flag = 1;       str[size - 1] = str[z];       str[z] = x;     }     if (flag == 1) {       printf( %s , str);     } else       printf( -1 );     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   long long n;   bool valid(long long x) {     long long a = 0, b = 0;     long long r = n;     while (r) {       a += min(r, x);       r -= min(r, x);       b += r / 10;       r -= r / 10;     }     return a >= b;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long seed;     asm( rdtsc  :  =A (seed));     srand(seed);     cin >> n;     long long lo = 0, hi = n;     while (lo + 1 < hi) {       long long mid = (lo + hi) / 2;       if (valid(mid))         hi = mid;       else         lo = mid;     }     cout << lo + 1 <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 1e5 + 7;   int T, n, t, a[N];   map<long long, long long> mp;   int main() {     scanf( %d , &T);     while (T--) {       mp.clear();       scanf( %d%d , &n, &t);       for (int i = 1; i <= n; ++i) {         scanf( %d , &a[i]);       }       if (t % 2 == 1) {         for (int i = 1; i <= n; ++i) {           mp[a[i]] = 1;           mp[t - a[i]] = 0;         }         for (int i = 1; i <= n; ++i) {           printf( %d  , mp[a[i]]);         }         printf(  n );       } else {         for (int i = 1; i <= n; ++i) {           mp[a[i]] = 1;           mp[t - a[i]] = 0;         }         int flag = 0;         for (int i = 1; i <= n; ++i) {           if (a[i] * 2 == t) {             printf( %d  , flag);             flag = 1 - flag;           } else {             printf( %d  , mp[a[i]]);           }         }         printf(  n );       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1000010;   int dp[maxn][2];   int n, cnt = 1, head[maxn], to[maxn], nxt[maxn], val[maxn];   inline void add(int x, int y, int z) {     nxt[++cnt] = head[x];     head[x] = cnt;     to[cnt] = y;     val[cnt] = z;   }   void dfs(int x, int fa) {     for (int i = head[x]; i; i = nxt[i]) {       if (to[i] == fa) continue;       dfs(to[i], x);       dp[x][0] += dp[to[i]][0] + val[i];     }   }   void dfs1(int x, int fa) {     for (int i = head[x]; i; i = nxt[i]) {       if (to[i] == fa) continue;       dp[to[i]][1] = dp[x][0] - dp[to[i]][0] - val[i] + dp[x][1] + val[i ^ 1];       dfs1(to[i], x);     }   }   int st[maxn], top;   int main() {     cin >> n;     for (int i = 1, x, y; i < n; ++i) {       cin >> x >> y;       add(x, y, 0);       add(y, x, 1);     }     dfs(1, 1);     dfs1(1, 1);     int tot = 1e9;     for (int i = 1; i <= n; ++i) {       tot = min(tot, dp[i][0] + dp[i][1]);     }     for (int i = 1; i <= n; ++i) {       if (dp[i][0] + dp[i][1] == tot) {         st[++top] = i;       }     }     cout << tot << endl;     for (int i = 1; i <= top; ++i) {       cout << st[i] <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int T;     cin >> T;     while (T--) {       int a, b, c;       scanf( %d%d%d , &a, &b, &c);       int temp = 0;       if (a == b && b == c) {         printf( 0 n );       } else {         if (b > a) {           temp = b;           b = a;           a = temp;         }         if (c > a) {           temp = c;           c = a;           a = temp;         }         if (b >= c) {           printf( %d n , max(2 * (a - c) - 4, 0));         } else {           printf( %d n , max(2 * (a - b) - 4, 0));         }       }     }   }
#include <bits/stdc++.h>   using namespace std;   int fireball1, fireball2, health[100], attack[100], attack1[100];   int n, sum = 2e+8;   void dfs(int x, int y) {     if (y >= sum) return;     if (x == n) {       sum = y;       for (int i = 1; i <= n; i++) attack[i] = attack1[i];       return;     }     for (int i = 0; i <= 100; i++) {       if (y + i > sum) break;       if (health[x - 1] - fireball2 * i >= 0) continue;       if (x == n - 1)         if (health[n] - fireball2 * i >= 0) continue;       attack1[x] = i;       health[x - 1] -= fireball2 * i;       health[x] -= fireball1 * i;       health[x + 1] -= fireball2 * i;       dfs(x + 1, y + i);       health[x - 1] += fireball2 * i;       health[x] += fireball1 * i;       health[x + 1] += fireball2 * i;       if (health[x] - fireball1 * i < 0 && health[x + 1] - fireball2 * i < 0)         break;     }   }   int main() {     scanf( %d%d%d , &n, &fireball1, &fireball2);     for (int i = 1; i <= n; i++) scanf( %d , &health[i]);     dfs(2, 0);     printf( %d n , sum);     for (int i = 2; i < n; i++)       for (int j = 1; j <= attack[i]; j++) printf( %d  , i);   }