malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   void itval(istream_iterator<string> it) {}   template <typename T, typename... Args>   void itval(istream_iterator<string> it, T a, Args... args) {     cerr << *it <<   =   << a << endl;     itval(++it, args...);   }   const long long int MOD = 1e9 + 7;   template <typename T>   inline void print(T x) {     cout << x <<   n ;   }   template <typename T>   inline void printvec(T x) {     for (auto a : x) cout << a <<    ;     cout <<   n ;   }   struct custom {     bool operator()(const pair<long long int, long long int> &p1,                     const pair<long long int, long long int> &p2) const {       return p1.first < p2.first;     }   };   long long int get_pow(long long int a, long long int b, long long int M = MOD) {     long long int res = 1;     while (b) {       if (b & 1) res = (res * a) % M;       a = (a * a) % M;       b >>= 1;     }     return res;   }   const long long int N = 2e5 + 5, inf = 4e18, M = 302;   void solve() {     long long int c, p, q, a, b;     cin >> c >> p >> q >> a >> b;     long long int x = c / a;     long long int ans = 0;     for (long long int i = (long long int)max(0ll, x - 10000);          i < (long long int)(x + 10000); i++) {       if (i * a <= c) {         ans = max(ans, i * p + ((c - i * a) / b) * q);       } else         break;     }     long long int y = c / b;     for (long long int i = (long long int)max(0ll, y - 10000);          i < (long long int)(y + 10000); i++) {       if (i * b <= c) {         ans = max(ans, i * q + ((c - i * b) / a) * p);       } else         break;     }     if (p * b == q * a) {       long long int x = c / a / 2;       for (long long int i = (long long int)max(0ll, x - 10000);            i < (long long int)(x + 10000); i++) {         if (i * a <= c) {           ans = max(ans, i * p + ((c - i * a) / b) * q);         } else           break;       }       long long int y = c / b / 2;       for (long long int i = (long long int)max(0ll, y - 10000);            i < (long long int)(y + 10000); i++) {         if (i * b <= c) {           ans = max(ans, i * q + ((c - i * b) / a) * p);         } else           break;       }     }     cout << ans <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int test = 1;     clock_t z = clock();     for (long long int tes = (long long int)0; tes < (long long int)(test);          tes++) {       solve();     }     fprintf(stderr,  Total Time:%.4f n , (double)(clock() - z) / CLOCKS_PER_SEC),         fflush(stderr);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10;   int n, x, m, tmp, ans, sum, t;   int a[N], b[N], h[N];   string s, p;   map<string, int> pol;   vector<int> nei[N];   bool mark[N];   void read_input() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];       if (a[i] > 1 && tmp == 0) tmp = i;       sum += a[i];     }     if (sum == n) {       cout << (n % 2 ?  First n  :  Second n );       return;     }     cout << (tmp % 2 ?  First n  :  Second n );   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> t;     while (t--) {       sum = 0;       tmp = 0;       read_input();     }     return 0;   }
#include <bits/stdc++.h>   using pi = std::pair<int, int>;   using vec = std::vector<pi>;   const int N = 300007, P = 1019260817;   char str[N];   int n, fa[N], size[N], dep[N], son[N], top[N], dfn[N], id[N], pw[N], ipw[N],       h[N][2];   std::vector<int> e[N];   int sgn(int x) { return x < 0 ? -1 : 1; }   int sub(int a, int b) { return a -= b, a += a >> 31 & P; }   int mul(int a, int b) { return 1ll * a * b % P; }   int read() {     int x = 0, c = getchar();     while (!isdigit(c)) c = getchar();     while (isdigit(c)) (x *= 10) += c & 15, c = getchar();     return x;   }   void dfs1(int u, int f) {     size[u] = 1, dep[u] = dep[fa[u] = f] + 1,     h[u][0] = (str[u] + 233ll * h[f][0]) % P,     h[u][1] = (h[f][1] + 1ll * str[u] * pw[dep[u] - 1]) % P;     for (int v : e[u])       if (v ^ f)         dfs1(v, u), size[u] += size[v],             son[u] = size[v] > size[son[u]] ? v : son[u];   }   void dfs2(int u, int t) {     static int T;     top[u] = t, id[dfn[u] = ++T] = u;     if (son[u]) dfs2(son[u], t);     for (int v : e[u])       if (v ^ fa[u] && v ^ son[u]) dfs2(v, v);   }   vec split(int u, int v) {     vec l, r;     while (top[u] ^ top[v])       dep[top[u]] > dep[top[v]] ? (l.push_back({u, top[u]}), u = fa[top[u]])                                 : (r.push_back({top[v], v}), v = fa[top[v]]);     l.push_back({u, v}), std::reverse(r.begin(), r.end()),         l.insert(l.end(), r.begin(), r.end());     return l;   }   int ask(int x, int fx, int d) {     return fx == 1                ? mul(sub(h[x][1], h[fa[id[dfn[x] - d + 1]]][1]), ipw[dep[x] - d])                : sub(h[id[dfn[x] + d - 1]][0], mul(h[fa[x]][0], pw[d]));   }   int cal(int u, int fu, int v, int fv, int lim) {     if (ask(u, fu, lim) == ask(v, fv, lim)) return lim;     int l = 1, r = lim - 1;     for (int mid; l <= r;)       mid = (l + r) / 2,       ask(u, fu, mid) == ask(v, fv, mid) ? l = mid + 1 : r = mid - 1;     return r;   }   void solve() {     int a = read(), b = read(), c = read(), d = read(), ans = 0;     vec l = split(a, b), r = split(c, d);     for (int pl = 0, pr = 0, l1, l2, f1, f2, d, x;          pl < (int)l.size() && pr < (int)r.size();) {       l1 = abs(dep[l[pl].first] - dep[l[pl].second]) + 1,       l2 = abs(dep[r[pr].first] - dep[r[pr].second]) + 1,       f1 = sgn(dep[l[pl].first] - dep[l[pl].second]),       f2 = sgn(dep[r[pr].first] - dep[r[pr].second]), d = std::min(l1, l2);       ans += x = cal(l[pl].first, f1, r[pr].first, f2, d);       if (d ^ x) break;       (l1 == d ? ++pl : l[pl].first = id[dfn[l[pl].first] - f1 * d]),           (l2 == d ? ++pr : r[pr].first = id[dfn[r[pr].first] - f2 * d]);     }     printf( %d n , ans);   }   int main() {     n = read(), scanf( %s , str + 1), pw[0] = ipw[0] = 1;     for (int i = 1; i <= n; ++i)       pw[i] = mul(233, pw[i - 1]), ipw[i] = mul(78741179, ipw[i - 1]);     for (int i = 1, u, v; i < n; ++i)       u = read(), v = read(), e[u].push_back(v), e[v].push_back(u);     dfs1(1, 0), dfs2(1, 1);     for (int m = read(); m; --m) solve();   }
#include <bits/stdc++.h>   using namespace std;   default_random_engine generator;   uniform_int_distribution<int> distribution(1, INT_MAX);   vector<vector<long long> > one(int sz) {     vector<vector<long long> > ret;     ret.resize(sz);     for (int i = 0; i < ret.size(); i++) {       ret[i].resize(sz);       for (int j = 0; j < ret.size(); j++) {         ret[i][j] = (i == j);       }     }     return ret;   }   vector<vector<long long> > mult(vector<vector<long long> > a,                                   vector<vector<long long> > b) {     vector<vector<long long> > ret;     ret.resize(a.size());     for (int i = 0; i < ret.size(); i++) {       ret[i].resize(a.size());       for (int j = 0; j < ret.size(); j++) {         ret[i][j] = 0;       }     }     for (int k = 0; k < ret.size(); k++) {       for (int i = 0; i < a.size(); i++) {         for (int j = 0; j < b.size(); j++) {           ret[i][j] = (ret[i][j] + a[i][k] * b[k][j]);           ret[i][j] %= 998244353;         }       }     }     return ret;   }   vector<vector<long long> > pw(vector<vector<long long> > a, long long b) {     if (b == 0) return one((int)a.size());     if (b % 2) return mult(a, pw(a, b - 1));     return pw(mult(a, a), b / 2);   }   long long my_rand(long long a = 0, long long b = INT_MAX) {     return a + distribution(generator) % b;   }   inline int mul(int a, int b) { return int(a * 1ll * b % 998244353); }   inline int norm(int a) {     if (a >= 998244353) a -= 998244353;     if (a < 0) a += 998244353;     return a;   }   inline int binPow(int a, int k) {     int ans = 1;     while (k > 0) {       if (k & 1) ans = mul(ans, a);       a = mul(a, a);       k >>= 1;     }     return ans;   }   inline int subtract(int a, int b) { return norm(a - b); }   inline int inv(int a) { return binPow(a, 998244353 - 2); }   long long ceil_int(long long a, long long b) { return a / b + (a % b != 0); }   long long gcd(long long a, long long b) {     if (a == 0) return b;     return gcd(b % a, a);   }   long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }   int main() {     generator.seed(chrono::system_clock::now().time_since_epoch().count());     cin.sync_with_stdio(false);     cin.tie(0);     int x;     cin >> x;     int cnt = 0;     vector<int> op;     int ok = 0;     do {       int k = x;       int pos = -1;       int j = 0;       while (k > 0) {         if ((k & 1) == 0) {           pos = j;         }         j++;         k >>= 1;       }       if (pos == -1)         break;       else if (cnt % 2 == 0)         op.push_back(pos + 1);       if (cnt % 2 == 0) {         x = x ^ ((1 << (pos + 1)) - 1);       } else {         x++;       }       cnt++;     } while (1);     cout << cnt << endl;     for (int i = 0; i < op.size(); i++) {       cout << op[i] <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   #pragma GCC optimize( O3 )   #pragma comment(linker,  /stack:200000000 )   #pragma GCC optimize( Ofast,unroll-loops,no-stack-protector,fast-math )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   const long long mod = 1e9 + 7;   const long long N = 1e5 + 5;   void solve(void) {     long long a, flag = 0;     string b;     cin >> a >> b;     long long n = b.length();     for (long long i = 0; i < n; i++) {       if (b[i] !=  9 ) {         flag = 1;         break;       }     }     n -= flag;     cout << n * a <<   n ;   }   int main(void) {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cout << fixed << setprecision(10);     ;     long long ___ = 1;     cin >> ___;     while (___--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int now, ot, mi, last, cnt, le, d[10010], st[10010][15], len[10010], ans[10010],       trs[10010][26], fa[10010], tot[10010], lev[10010];   char s[10010];   inline int ext(int x) {     for (now = last; s[le - len[now]] ^ x; now = fa[now])       ;     if (trs[now][x]) return last = trs[now][x];     for (ot = fa[now]; s[le - len[ot]] ^ x; ot = fa[ot])       ;     ++d[fa[++cnt] = trs[ot][x]];     len[trs[now][x] = cnt] = len[now] + 2;     return last = cnt;   }   inline void topo() {     int dl[10010], l = 0, r = 0;     for (int i = 0; i <= cnt; ++i)       if (!d[i]) dl[++r] = i;     while (l < r) {       int v = fa[dl[++l]];       tot[v] += tot[dl[l]];       if (!--d[v]) dl[++r] = v;     }   }   int main() {     len[last = cnt = 1] = -1;     fa[1] = fa[0] = 1;     for (s[le + 1] = getchar(); s[le + 1] !=   n ; s[++le + 1] = getchar()) {       ext(s[le + 1] -=  a  - 1);       ++tot[last];       if (!lev[last]) {         st[last][0] = fa[last];         for (int i = 1; i <= 13; ++i) st[st[last][i - 1]][i - 1];         if (len[last] == 1)           lev[last] = 1;         else {           for (mi = 13, now = last; mi >= 0;) {             while (mi >= 0 && len[st[now][mi]] < len[last] >> 1) --mi;             if (mi >= 0) now = st[now][mi];           }           if (len[now] == len[last] >> 1)             lev[last] = lev[now] + 1;           else             lev[last] = 1;         }       }     }     topo();     for (int i = 2; i <= cnt; ++i) ans[lev[i]] += tot[i];     for (int i = le; i; --i) ans[i - 1] += ans[i];     for (int i = 1; i <= le; ++i) printf( %d  , ans[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       long n;       cin >> n;       long count1 = 0, count2 = 0;       int f = 0;       while (n != 1) {         if (n % 3 == 0) {           n = n / 3;           count1++;         }         if (n % 2 == 0) {           n = n / 2;           count2++;         }         if (n % 3 != 0 and n % 2 != 0 and n != 1) {           f = 1;           break;         }       }       if (f == 1)         cout <<  -1  << endl;       else if (count1 >= count2 and f == 0) {         long ans = count1 - count2;         if (count1 - count2 != 0)           ans += count1;         else           ans = count1;         cout << ans << endl;       } else         cout <<  -1  << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100000 + 10;   struct Edge {     int v;     bool used;     Edge(int v, int used) : v(v), used(used) {}   };   vector<int> adj[MAXN], odd, ans;   vector<Edge> edges;   int p, n, m, deg[MAXN];   int cur[MAXN];   void dfs(int u) {     for (auto &idx = cur[u]; idx < adj[u].size(); ++idx) {       if (!edges[adj[u][idx]].used) {         edges[adj[u][idx]].used = 1;         edges[adj[u][idx] ^ 1].used = 1;         dfs(edges[adj[u][idx]].v);       }     }     ans.push_back(u);   }   int main() {     cin >> n >> m;     for (int i = 0; i < m; ++i) {       int u, v;       cin >> u >> v;       ++deg[u], ++deg[v];       edges.emplace_back(v, 0);       edges.emplace_back(u, 0);       int sz = edges.size();       adj[u].push_back(sz - 2);       adj[v].push_back(sz - 1);     }     for (int i = (1); i < (n + 1); ++i)       if (deg[i] & 1) {         odd.push_back(i);       }     for (int i = 0; i < (int)odd.size(); i += 2) {       int u = odd[i], v = odd[i + 1];       edges.emplace_back(v, 0);       edges.emplace_back(u, 0);       int sz = edges.size();       adj[u].push_back(sz - 2);       adj[v].push_back(sz - 1);     }     p = m + (odd.size() / 2);     dfs(1);     if (ans.size() % 2 == 0) {       ++p;       ans.push_back(1);     }     cout << p <<   n ;     for (int i = 1; i < (int)ans.size(); ++i) {       int u = ans[i - 1], v = ans[i];       if (!(i & 1)) {         swap(u, v);       }       cout << u <<     << v <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long double PI = acos(-1.0);   const long double eps = 1e-6;   const int maxw = 1e6 + 10;   const int maxn = 5e2 + 10;   int n, m, K;   char s[maxn][maxn];   vector<int> res[maxn];   int num[maxn][maxn], dp[maxn][maxn];   void solve() {     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if (s[i][j] ==  1 ) {           res[i].push_back(j);         }       }     }     int ret, tmp;     for (int i = 1; i <= n; i++) {       if (res[i].empty()) {         for (int j = 0; j <= K; j++) {           num[i][j] = 0;         }         continue;       }       for (int j = 0; j <= m; j++) {         num[i][j] = maxw;       }       for (int j = 0; j < (int)res[i].size(); j++) {         for (int k = j; k < (int)res[i].size(); k++) {           ret = (int)res[i].size() - (k - j + 1);           num[i][ret] = min(num[i][ret], res[i][k] - res[i][j] + 1);         }       }       num[i][(int)res[i].size()] = 0;     }     for (int i = 0; i <= n; i++) {       for (int j = 0; j <= K; j++) {         dp[i][j] = maxw;       }     }     memset(dp[0], 0, sizeof(dp[0]));     for (int i = 1; i <= n; i++) {       for (int j = K; j >= 0; j--) {         for (int k = 0; k <= j; k++) {           dp[i][j] = min(dp[i][j], dp[i - 1][j - k] + num[i][k]);         }       }     }     printf( %d n , dp[n][K]);   }   int main(void) {     scanf( %d %d %d , &n, &m, &K);     for (int i = 1; i <= n; i++) {       scanf( %s , s[i] + 1);     }     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long t, x, y, n;   int main() {     cin.tie(NULL);     ios::sync_with_stdio(false);     cin >> t;     while (t--) {       cin >> n >> x >> y;       vector<int> ans;       int number = y - x;       for (int i = 1; i <= number; i++) {         if (number % i) continue;         if (number / i + 1 > n) continue;         number = i;         break;       }       for (int i = y; i >= 1 && ans.size() < n; i -= number) ans.push_back(i);       for (int i = y + number; i <= (int)1e9 && ans.size() < n; i += number)         ans.push_back(i);       for (auto here : ans) cout << here <<    ;       cout <<   n ;     }   }
#include <bits/stdc++.h>   const int dx[4] = {1, 0, -1, 0};   const int dy[4] = {0, -1, 0, 1};   using namespace std;   char str[5001];   class Point {    public:     int Left;     int Right;     int Up;     int Down;   };   Point f1[5001], f2[5001];   int ans1, ans2, n, m;   int dfs(int p, int tot) {     int tmp;     if (str[p] ==  D )       tmp = f2[p].Down;     else if (str[p] ==  U )       tmp = f2[p].Up;     else if (str[p] ==  L )       tmp = f2[p].Left;     else       tmp = f2[p].Right;     if (f2[p].Left != -1) f2[f2[p].Left].Right = f2[p].Right;     if (f2[p].Right != -1) f2[f2[p].Right].Left = f2[p].Left;     if (f2[p].Up != -1) f2[f2[p].Up].Down = f2[p].Down;     if (f2[p].Down != -1) f2[f2[p].Down].Up = f2[p].Up;     if (ans1 == tot) ans2++;     if (ans1 < tot) {       ans1 = tot;       ans2 = 1;     }     if (tmp != -1) dfs(tmp, tot + 1);     return 0;   }   int work() {     scanf( %d%d , &n, &m);     getchar();     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) str[i * m + j] = getchar();       getchar();     }     memset(f1, -1, sizeof(f1));     memset(f2, -1, sizeof(f2));     int k;     for (int i = 0; i < n; ++i)       for (int j = 0; j < m; ++j) {         int p = i * m + j;         if (str[p] ==  . ) continue;         {           k = i + 1;           while (str[k * m + j] ==  .  && k < n) k++;           if (k < n) {             f1[p].Down = k * m + j;             f1[k * m + j].Up = p;           }         }         {           k = i - 1;           while (str[k * m + j] ==  .  && k >= 0) k--;           if (k >= 0) {             f1[p].Up = k * m + j;             f1[k * m + j].Down = p;           }         }         {           k = j - 1;           while (str[i * m + k] ==  .  && k >= 0) k--;           if (k >= 0) {             f1[p].Left = i * m + k;             f1[i * m + k].Right = p;           }         }         {           k = j + 1;           while (str[i * m + k] ==  .  && k < m) k++;           if (k < m) {             f1[p].Right = i * m + k;             f1[i * m + k].Left = p;           }         }       }     ans1 = ans2 = 0;     for (int i = 0; i < n; ++i)       for (int j = 0; j < m; ++j)         if (str[i * m + j] !=  . ) {           for (int k = 0; k < 5001; ++k) f2[k] = f1[k];           dfs(i * m + j, 1);         }     printf( %d %d n , ans1, ans2);     return 0;   }   int main() {     work();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     if (n == 2) {       cout <<  1 2  << endl;       return 0;     }     bool usedN = false;     for (int i = 0; i < n; i++) {       int maxi = 0;       for (int j = 0; j < n; j++) {         int a;         cin >> a;         if (a > maxi) {           maxi = a;         }       }       if (maxi == n - 1 && !usedN) {         usedN = true;         maxi = n;       }       cout << maxi <<    ;     }     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   int a[3003];   double f[3003 * 3003];   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i < n + 1; i++) scanf( %d , &a[i]);     int cnt = 0;     for (int i = 1; i < n + 1; i++) {       for (int j = i + 1; j < n + 1; j++) {         if (a[i] > a[j]) cnt++;       }     }     f[0] = 0;     f[1] = 1;     for (int i = 2; i < cnt + 1; i++) f[i] = f[i - 2] + 4;     printf( %.6f , f[cnt]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   class DSU {    public:     vector<int> parent;     int total_components;     DSU(int n) {       parent.resize(n + 1);       for (int i = 1; i <= n; i++) {         parent[i] = i;       }       total_components = n;     }     int get(int x) {       if (parent[x] == x) {         return x;       }       return parent[x] = get(parent[x]);     }     bool unite(int x, int y) {       int p1 = get(x);       int p2 = get(y);       if (p1 == p2) {         return true;       }       parent[p1] = p2;       total_components--;       return false;     }   };   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, m;     cin >> n >> m;     bool cycle = false;     DSU g(n);     while (m--) {       int u, v;       cin >> u >> v;       if (g.unite(u, v)) {         cycle = true;       }     }     if (cycle || g.total_components > 1) {       cout <<  no  << endl;     } else {       cout <<  yes  << endl;     }     cout.flush();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1.0);   const int N = 10003;   const long long oo = 1e18;   int get(int x, int l, int r) {     if (x <= l) return l - x;     if (r <= x) return x - r;     return 0;   }   int l[N], r[N], zn[N];   long long d1[N], d2[N];   vector<int> st;   map<int, int> mp;   int main() {     if (0) {       freopen( input.txt ,  w , stdout);       return 0;     }     int i, u, n, m, x, li, ri, xp;     long long tt, ans;     scanf( %d%d , &n, &x);     st.push_back(x);     for (i = 0; i < n; ++i) {       scanf( %d%d , &li, &ri);       l[i] = li;       r[i] = ri;       st.push_back(li);       st.push_back(ri);     }     sort(st.begin(), st.end());     m = 0;     for (i = 0; i < st.size(); ++i)       if ((i + 1 == st.size()) || (st[i] != st[i + 1])) {         zn[m] = st[i];         mp[st[i]] = m++;       }     xp = mp[x];     for (i = 0; i < m; ++i) d1[i] = oo;     d1[xp] = 0;     for (u = 0; u < n; ++u) {       for (i = 0; i < m; ++i) d2[i] = oo;       li = l[u];       ri = r[u];       tt = oo;       for (i = 0; i < m; ++i) {         if (i) tt += zn[i] - zn[i - 1];         tt = min(tt, d1[i]);         if (tt >= oo) {           tt = oo;           continue;         }         d2[i] = min(d2[i], tt + get(zn[i], li, ri));       }       tt = oo;       for (i = m - 1; i >= 0; --i) {         if (i != m - 1) tt += zn[i + 1] - zn[i];         tt = min(tt, d1[i]);         if (tt >= oo) {           tt = oo;           continue;         }         d2[i] = min(d2[i], tt + get(zn[i], li, ri));       }       for (i = 0; i < m; ++i) d1[i] = d2[i];     }     ans = oo;     for (i = 0; i < m; ++i) ans = min(ans, d1[i]);     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int pows[51];   vector<int> base_3(long long int n) {     vector<int> f(51);     int r = 0;     while (n > 0) {       f[r++] = (int)n % 3;       n /= 3;     }     return f;   }   int main() {     int q;     cin >> q;     pows[1] = 3;     pows[0] = 1;     for (int i = 1; i <= 50; i++) {       pows[i] = pows[i - 1] * 3;     }     while (q--) {       long long int n;       cin >> n;       vector<int> v = base_3(n);       int ind = -1;       for (int j = 50; j >= 0; j--) {         if (v[j] == 2 && ind == -1) {           ind = j;         }       }       if (ind == -1) {         cout << n <<   n ;       } else {         int r = ind + 1;         while (r < 50 && v[r] == 1) {           r++;         }         v[r] = 1;         for (int i = 0; i < r; i++) {           v[i] = 0;         }         long long int ans = 0;         for (int j = 0; j < 51; j++) {           ans += v[j] * pows[j];         }         cout << ans <<   n ;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int qq = 3e5 + 10;   vector<int> G[qq];   multiset<int, greater<int> > myset;   int num[qq];   void Del(int x) {     auto t = myset.find(x);     myset.erase(t);   }   int main() {     int ans = 2e9 + 10;     int n;     scanf( %d , &n);     int a, b;     for (int i = 1; i <= n; ++i) {       scanf( %d , num + i);       myset.insert(num[i]);     }     for (int i = 1; i <= n - 1; ++i) {       scanf( %d%d , &a, &b);       G[a].push_back(b), G[b].push_back(a);     }     for (int i = 1; i <= n; ++i) {       Del(num[i]);       int ma = num[i];       for (int y : G[i]) {         Del(num[y]);         ma = max(ma, num[y] + 1);       }       if (!myset.empty()) {         ma = max(ma, *myset.begin() + 2);       }       for (int y : G[i]) {         myset.insert(num[y]);       }       myset.insert(num[i]);       ans = min(ans, ma);     }     printf( %d n , ans);   }