#include <bits/stdc++.h>   using namespace std;   const int maxn = (int)2e5 + 7;   const int mod = 998244353;   int n, m, l;   int a[maxn << 2], b[maxn << 2], rev[maxn << 2];   int num[maxn << 2];   const int pr = 3;   const int phi = mod - 1;   int qpow(int x, int y) {     int t = 1;     while (y) {       if (y & 1) t = 1ll * t * x % mod;       x = 1ll * x * x % mod;       y >>= 1;     }     return t;   }   void NTT(int *P, int n, int opt) {     for (int i = 0; i < n; i++) {       if (i < rev[i]) {         swap(P[i], P[rev[i]]);       }     }     for (int i = 1; i < n; i <<= 1) {       int W = qpow(pr, phi / (i << 1));       for (int p = i << 1, j = 0; j < n; j += p) {         int w = 1;         for (int k = 0; k < i; k++, w = 1ll * w * W % mod) {           int X = P[j + k], Y = 1ll * w * P[i + j + k] % mod;           P[j + k] = (X + Y) % mod;           P[i + j + k] = (X - Y + mod) % mod;         }       }     }     if (opt == -1) reverse(P + 1, P + n);   }   vector<int> solve(int l, int r) {     if (l == r) {       vector<int> res;       for (int i = 0; i <= num[l]; i++) {         res.push_back(1);       }       return res;     }     vector<int> v1 = solve(l, (l + r >> 1));     vector<int> v2 = solve((l + r >> 1) + 1, r);     int siz1 = v1.size(), siz2 = v2.size();     int k = 0, s = 2;     while ((1 << k) < max(siz1, siz2) + 1) k++, s <<= 1;     for (int i = 1; i < s; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (k));     for (int i = 0; i < s; i++) {       if (i < siz1) {         a[i] = v1[i];       }       if (i < siz2) {         b[i] = v2[i];       }     }     NTT(a, s, 1);     NTT(b, s, 1);     int inv = qpow(s, mod - 2);     for (int i = 0; i < s; i++) {       a[i] = 1ll * a[i] * b[i] % mod;     }     NTT(a, s, -1);     for (int i = 0; i < s; i++) {       a[i] = 1ll * a[i] * inv % mod;     }     vector<int> v3;     for (int i = 0, tt = siz1 + siz2 - 1; i < tt; i++) {       v3.push_back(a[i]);     }     for (int i = 0; i < s; i++) {       a[i] = b[i] = 0;     }     return v3;   }   int vis[maxn * 30];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       int t;       scanf( %d , &t);       vis[t]++;     }     int nn = 0;     for (int i = 1; i <= 3000000; i++) {       if (vis[i]) {         num[++nn] = vis[i];       }     }     vector<int> res = solve(1, nn);     int maxx = 0;     maxx = res[n / 2];     printf( %d n , maxx);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = (1e9) + 7;   const long long C = (1 << (17));   vector<vector<long long> > up, sum;   vector<long long> cost, maxw, d;   long long LOG = 20;   void push(long long v, long long x) {     cost.push_back(x);     maxw.push_back(x);     d.push_back(0);     for (int i = 0; i < LOG; ++i) {       sum[i].push_back(x);       up[i].push_back(v);     }   }   void init() {     up.resize(20);     sum.resize(20);     push(0, 0);   }   void add(long long pr, long long w) {     long long v = up[0].size();     if (maxw[pr] < w) {       push(v, w);       return;     }     for (int i = LOG - 1; i >= 0; --i) {       if (cost[up[i][pr]] < w) pr = up[i][pr];     }     if (cost[pr] < w) {       pr = up[0][pr];       assert(cost[pr] >= w);     }     cost.push_back(w);     maxw.push_back(maxw[pr]);     d.push_back(d[pr] + 1);     up[0].push_back(pr);     sum[0].push_back(cost[pr]);     for (int i = 1; i < LOG; ++i) {       long long pp = up[i - 1][v];       up[i].push_back(up[i - 1][pp]);       if (pp != up[i - 1][pp]) {         sum[i].push_back(sum[i - 1][v] + sum[i - 1][pp]);       } else {         sum[i].push_back(sum[i - 1][v]);       }     }   }   long long req(long long v, long long first) {     if (cost[v] > first) return 0;     first -= cost[v];     long long ans = 1;     for (int i = LOG - 1; i >= 0; --i) {       if (sum[i][v] <= first) {         ans += d[v] - d[up[i][v]];         first -= sum[i][v];         v = up[i][v];       }     }     return ans;   }   int source() {     init();     long long Q;     cin >> Q;     long long last = 0;     for (long long q1 = 0; q1 < Q; ++q1) {       long long type, p, q;       cin >> type >> p >> q;       long long R = p ^ last;       --R;       long long W = q ^ last;       if (type == 1) {         add(R, W);       } else {         last = req(R, W);         cout << last <<   n ;       }     }     return 0;   }   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     source();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = INT_MAX / 2;   int main() {     int n, k, q;     scanf( %d%d%d , &n, &k, &q);     vector<int> vec(n);     for (int i = 0; i < n; i++) {       scanf( %d , &vec[i]);     }     set<pair<int, int> > st;     map<int, int> mp;     for (int i = 0; i < q; i++) {       int type, id;       scanf( %d%d , &type, &id);       id--;       if (type == 1) {         if (st.size() < k) {           st.insert(make_pair(vec[id], id));           mp[id] = 1;         } else {           set<pair<int, int> >::iterator it = st.begin();           if ((*it).first < vec[id]) {             mp.erase((*it).second);             st.erase(it);             st.insert(make_pair(vec[id], id));             mp[id] = 1;           }         }       } else {         if (mp.count(id))           printf( YES n );         else           printf( NO n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n = 0, ans = 1e9 + 1;     string s;     cin >> n >> s;     vector<double> x(n);     for (int i = 0; i < n; i++) {       cin >> x[i];     }     for (int i = 0; i < n; i++) {       if (i < n + 1 && s[i] ==  R  && s[i + 1] ==  L  &&           (x[i + 1] - x[i]) / 2 < ans) {         ans = (x[i + 1] - x[i]) / 2;       }     }     if (ans == 1e9 + 1) {       cout << -1;     } else {       cout << ans;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 100009;   int n, m, k, a[MAX], b[MAX], pos[MAX];   int main() {     cin >> n >> m >> k;     for (int i = 1; i <= n; i++) {       cin >> a[i];       pos[a[i]] = i;     }     long long ans = 0;     for (int i = 1; i <= m; i++) {       cin >> b[i];       int current = pos[b[i]];       ans += (current + k - 1) / k;       if (current > 1) {         swap(pos[a[current]], pos[a[current - 1]]);         swap(a[current], a[current - 1]);       }     }     printf( %I64d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[200001];   bool b[200001];   int main() {     string s1, s2;     int n, i;     cin >> s1 >> s2;     n = s1.size();     for (i = 1; i <= n; i++) cin >> a[i];     int l, r, mid, pr = 0, j;     l = 0;     r = n;     while (l != r) {       mid = (l + r + 1) / 2;       for (i = 0; i <= n; i++) b[i] = 1;       for (i = 1; i <= mid; i++) b[a[i] - 1] = 0;       j = 0;       for (i = 0; i < n && j < s2.size(); i++) {         if (b[i]) {           if (s1[i] == s2[j]) j++;         }       }       if (j == s2.size())         l = mid;       else         r = mid - 1;     }     cout << l << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n, k;       cin >> n >> k;       k++;       vector<int> v;       for (int i = 0, x; i < n; i++) {         cin >> x;         v.push_back(x);       }       int lo = 1;       int hi = 1000000000;       while (lo < hi) {         int me = (lo + hi) / 2;         bool can = 0;         for (int i = 0; i + k - 1 < n; i++) {           if (v[i + k - 1] - v[i] <= me) can = 1;         }         if (!can)           lo = me + 1;         else           hi = me;       }       int x = -10;       for (int i = 0; i + k - 1 < n; i++) {         if (v[i + k - 1] - v[i] <= lo) x = (v[i] + v[i + k - 1]) / 2;       }       cout << x <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 12;   unsigned long long ans;   unsigned long long n;   unsigned long long data[maxn];   void init() {     scanf( %I64d , &n);     memset(data, 0, sizeof(data));     ans = 0;   }   void solve() {     for (int i = 1; i <= n; ++i) {       ++data[i % 9];       ans -= n / i;     }     for (int i = 0; i < 9; ++i)       for (int j = 0; j < 9; ++j) ans += data[i] * data[j] * data[i * j % 9];   }   void write() { printf( %I64d n , ans); }   int main() {     init();     solve();     write();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, k;   long long x[1000005], r, cur[1000005];   long long len;   bool check(long long cnt) {     long long res = 0, s = 0;     for (int i = (1); i < (n + 1); i++) cur[i] = 0;     for (int i = (1); i < (r + 1); i++) s += x[i];     for (int i = 1; i <= n; i++) {       s += x[i + r];       if (i > r) {         s = s - x[i - r - 1] - cur[i - r - 1];       }       if (s < cnt) {         long long d = cnt - s;         res += d;         cur[i + r] += d;         s += d;         if (res > k) return 0;       }     }     return 1;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     cin >> n >> r >> k;     for (int i = 1; i <= n; i++) {       cin >> x[i];     }     long long l = 0, r = 2e18, mid, ans = 0;     while (l <= r) {       mid = (l + r) >> 1ll;       if (check(mid)) {         l = mid + 1;         ans = max(ans, mid);       } else {         r = mid - 1;       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 6 * 1e5;   const long long Q = 1e9 + 7;   int ans;   int a[10];   bool used[10];   void dfs(int i) {     used[i] = true;     if (!used[a[i]]) dfs(a[i]);   }   void gen(int i, int n) {     if (i == n + 1) {       bool ok = true;       for (int j = 1; j <= n; j++) {         for (int f = 1; f <= n; f++) used[f] = false;         dfs(j);         if (!used[1]) {           ok = false;           break;         }       }       if (ok) ans++;       return;     }     for (int j = 1; j <= n; j++) {       a[i] = j;       gen(i + 1, n);     }   }   int cnt(int n) {     ans = 0;     gen(1, n);     int tmp = ans;     return tmp;   }   int main() {     srand(time(NULL));     int n, k;     scanf( %d%d , &n, &k);     long long x = 1;     for (int i = 0; i < (n - k); i++) x = (x * (n - k)) % Q;     long long y = cnt(k);     cout << (x * y) % Q;     cerr << clock() / (CLOCKS_PER_SEC + 0.);     return 0;   }


#include <bits/stdc++.h>   int ans = 2, n, t;   double m;   struct T {     int x, t;   } a[1111];   bool my(T a, T b) { return (a.x < b.x); }   int main() {     scanf( %d%d , &n, &t);     for (int i = 1; i <= n; i++) scanf( %d%d , &a[i].x, &a[i].t);     std::sort(a + 1, a + n + 1, &my);     for (int i = 2; i <= n; i++) {       m = a[i].x - a[i - 1].x - (double)a[i].t / 2 - (double)a[i - 1].t / 2;       if (m > t)         ans += 2;       else if (m == t)         ++ans;     }     printf( %d n , ans);     return 0;   }


#include  bits/stdc++.h        using int64 = long long;   const int N = 305;   const int Q = 998244353;       inline void add(int &x, int y) {     x += y;     if (x >= Q) x -= Q;   }       int n, dep[N];   std::vector<int> e[N];   int f[N][N];  // x , y : max uncovered depth   int g[N][N];  // x , y : max extended length       void dfs(int x, int fa, int K) {     dep[x] = 0;     int nf[2][N];     int ng[2][N];     int cur = 0, nxt = 1;     memset(nf[cur], 0, sizeof(nf[cur]));     memset(ng[cur], 0, sizeof(ng[cur]));     ng[cur][0] = 1;         for (auto &y : e[x]) {       if (y == fa) {         continue;       }       dfs(y, x, K);       memset(nf[nxt], 0, sizeof(nf[nxt]));       memset(ng[nxt], 0, sizeof(ng[nxt]));       for (int i = 0; i <= dep[x]; ++i) {         if (nf[cur][i]) {           for (int j = 0; j <= dep[y]; ++j) {             int depth = std::max(i, j);             if (depth <= K) {               add(nf[nxt][depth], (int64)nf[cur][i] * f[y][j] % Q);             }           }           for (int j = 0; j <= K; ++j) {             if (j - 2 >= i) {               add(ng[nxt][j], (int64)nf[cur][i] * g[y][j] % Q);             } else {               add(nf[nxt][i], (int64)nf[cur][i] * g[y][j] % Q);             }           }         }       }       for (int i = 0; i <= K; ++i) {         if (ng[cur][i] > 0) {           for (int j = 0; j <= dep[y]; ++j) {             if (i - 2 >= j) {               add(ng[nxt][i], (int64)ng[cur][i] * f[y][j] % Q);             } else {               add(nf[nxt][j], (int64)ng[cur][i] * f[y][j] % Q);             }           }           for (int j = 0; j <= K; ++j) {             int depth = std::max(i, j);             add(ng[nxt][depth], (int64)ng[cur][i] * g[y][j] % Q);           }         }       }       std::swap(cur, nxt);       dep[x] = std::max(dep[x], dep[y]);     }         for (int i = 0; i <= dep[x]; ++i) {       if (i + 1 < K) {         add(f[x][i + 1], nf[cur][i]);       }       if (i + 1 <= K) {         add(g[x][K], nf[cur][i]);       }     }     for (int i = 0; i <= K; ++i) {       if (i > 0) {         add(g[x][i - 1], ng[cur][i]);       } else {         add(f[x][0], ng[cur][i]);       }       add(g[x][std::max(i - 1, K)], ng[cur][i]);     }     // printf( === %d n , x);     // for (int i = 0; i <= dep[x]; ++i) {     //   printf( %d  , f[x][i]);     // }     // puts(  );     // for (int i = 0; i <= K; ++i) {     //   printf( %d  , g[x][i]);     // }     // puts(  );         ++dep[x];   }       int main() {     scanf( %d , &n);     for (int i = 1; i < n; ++i) {       int x = i, y = i + 1;       scanf( %d%d , &x, &y);       e[x].emplace_back(y);       e[y].emplace_back(x);     }     int res = Q - 1, pow2 = 1, inv = (Q + 1) / 2, inv2 = 1;     for (int i = 1; i <= n; ++i) {       add(pow2, pow2);       inv2 = (int64)inv2 * inv % Q;     }     for (int i = 1; i <= n; ++i) {       memset(f, 0, sizeof(f));       memset(g, 0, sizeof(g));       dfs(1, 0, i);       add(res, pow2);       for (int j = 0; j <= i; ++j) {         add(res, Q - g[1][j]);       }       // std::cout <<  :   << i <<     << res << std::endl;     }     printf( %d n , (int)((int64)res * inv2 % Q));   }


#include <bits/stdc++.h>   using namespace std;   const long long md = 1e9 + 7;   const long long o5 = 716327852;   const int N = 2e5 + 34;   long long f[N], o[N];   long long ans, n, w, h;   long long dp[N];   pair<long long, long long> a[N];   long long c(long long h, long long w) {     return (((f[h + w] * o[h]) % md) * o[w]) % md;   }   int main() {     o[0] = f[0] = 1;     o[100000] = o5;     for (long long i = 1; i < N; i++) f[i] = (f[i - 1] * i) % md;     for (long long i = 99999; i > 0; i--) o[i] = (o[i + 1] * (i + 1)) % md;     cin >> h >> w >> n;     ans = c(h - 1, w - 1);     for (int i = 0; i < n; i++) {       cin >> a[i].first >> a[i].second;       a[i].first--;       a[i].second--;     }     sort(a, a + n);     for (int i = 0; i < n; i++) {       dp[i] = c(a[i].second, a[i].first);       for (int j = 0; j < i; j++)         if (a[j].first <= a[i].first && a[j].second <= a[i].second)           dp[i] = (dp[i] -                    dp[j] * c(a[i].first - a[j].first, a[i].second - a[j].second) +                    md * md) %                   md;       ans = (ans + md * md - dp[i] * c(w - a[i].second - 1, h - a[i].first - 1)) %             md;     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1005;   int k;   long long pa, pb;   long long A, B, C;   long long f[N][N];   void exgcd(int a, int b, long long &x, long long &y) {     if (b == 0) {       x = 1, y = 0;       return;     }     exgcd(b, a % b, y, x);     y -= a / b * x;   }   long long inv(int x) {     long long res, y;     exgcd(x, 1000000007, res, y);     res = ((res % 1000000007) + 1000000007) % 1000000007;     return res;   }   int main() {     std::ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> k >> pa >> pb;     A = pa * inv(pa + pb) % 1000000007;     B = pb * inv(pa + pb) % 1000000007;     C = pa * inv(pb) % 1000000007;     for (int i = k; i >= 1; i--)       for (int j = k; j >= 0; j--) {         if (i + j >= k) {           f[i][j] = i + C;           continue;         }         f[i][j] = (A * f[i + 1][j] + B * (f[i][i + j] + i)) % 1000000007;       }     cout << (f[1][0] + 1000000007) % 1000000007;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> adj[202];   int height[202], best[202][3];   int N;   int getHeight(int ind, int parent) {     if (height[ind] != -1) return height[ind];     int ret = 1;     for (int i = 0; i < (int)adj[ind].size(); i++) {       if (adj[ind][i] == parent) continue;       ret = max(ret, getHeight(adj[ind][i], ind) + 1);     }     return height[ind] = ret;   }   int solve(int ind, int parent, int f) {     if (best[ind][f] != -1) return best[ind][f];     int ret = 0;     if (!f) {       for (int i = 0; i < (int)adj[ind].size(); i++) {         if (adj[ind][i] == parent) continue;         int a = 0, b = 0;         for (int j = 0; j < (int)adj[ind].size(); j++) {           if (adj[ind][j] == parent || j == i) continue;           int h = getHeight(adj[ind][j], adj[ind][j]);           if (h > a)             b = a, a = h;           else if (h > b)             b = h;         }         ret = max(ret, (a + b) * solve(adj[ind][i], ind, f + 1));       }     } else {       int a = 0, b = 0;       for (int j = 0; j < (int)adj[ind].size(); j++) {         if (adj[ind][j] == parent) continue;         int h = getHeight(adj[ind][j], adj[ind][j]);         if (h > a)           b = a, a = h;         else if (h > b)           b = h;         ret = max(ret, solve(adj[ind][j], ind, f));       }       ret = max(ret, a + b);     }     return best[ind][f] = ret;   }   int main() {     scanf( %d , &N);     for (int i = 0; i < N - 1; i++) {       int a, b;       scanf( %d %d , &a, &b);       a--, b--;       adj[a].push_back(b);       adj[b].push_back(a);     }     int ret = 0;     for (int i = 0; i < N; i++) {       memset(height, -1, sizeof height);       memset(best, -1, sizeof best);       getHeight(i, i);       ret = max(ret, solve(i, i, 0));     }     printf( %d n , ret);   }


#include <bits/stdc++.h>   using namespace std;   long long n, ans[100001], curr, deg[100001] = {0};   vector<pair<int, int>> adj[100001];   void dfs(long long u, long long p) {     for (auto v : adj[u]) {       if (v.first == p) {         continue;       }       if (ans[v.second] == -1) {         ans[v.second] = curr++;       }       dfs(v.first, u);     }   }   void solve() {     long long n;     cin >> n;     long long m;     cin >> m;     if (n > m || n % 2 != m % 2) {       cout << -1 << endl;       return;     }     if (n == m && n == 0) {       cout << n << endl;       return;     }     if (n == m) {       cout << 1 << endl << n << endl;       return;     }     long long x = (m - n) / 2;     if ((x & n) == 0) {       cout << 2 << endl << n + x <<     << x << endl;       return;     }     {       cout << 3 << endl << n <<     << x <<     << x << endl;       return;     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     long long ans = 0;     for (int i = (0); i < (n); ++i) {       long long t, T, x, cost;       cin >> t >> T >> x >> cost;       if (t >= T) {         ans += cost + m * x;         continue;       }       long long aux1 = cost;       if (m > (T - t)) aux1 += m * x;       long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1;       aux2 *= cost;       ans += min(aux1, aux2);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<int, int> > ans;   void merge(int l, int r, int lev) {     if (lev == 0) {       ans.push_back({l, r});       return;     }     int mid = (l + r) >> 1;     merge(l, mid, lev - 1);     merge(mid + 1, r, lev - 1);     int j = mid + 1;     for (int i = l; i <= mid; i++) {       ans.push_back({i, j});       j++;     }   }   int main(int argc, char** argv) {     int n;     scanf( %d , &n);     if (n == 1) {       printf( 0 );       return 0;     }     int t = 1;     int lev = -1;     while (t * 2 <= n) t *= 2, lev++;     merge(1, t, lev);     merge(n - t + 1, n, lev);     cout << ans.size() << endl;     for (int i = 0; i <= ans.size() - 1; i++) {       printf( %d %d n , ans[i].first, ans[i].second);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MaxK = 53, MaxN = 2000005;   int N, K;   char A[MaxN];   char B[MaxN];   int id(char c) { return ( a  <= c && c <=  z ) ? c -  a  : c -  A  + 26; }   namespace KuhnMunkres {   const int MAX_N = 505;   const int INF = 100000000;   const int BASE = 1000;   int n;   int graph[MAX_N][MAX_N];   bool usedX[MAX_N];   bool usedY[MAX_N];   int linkTo[MAX_N];   int labelX[MAX_N];   int labelY[MAX_N];   bool findPath(int i) {     usedX[i] = true;     for (int j = 0; j < n; j++)       if (!usedY[j] && labelX[i] + labelY[j] == graph[i][j] && linkTo[j] == -1) {         usedY[j] = true;         linkTo[j] = i;         return true;       }     for (int j = 0; j < n; j++)       if (!usedY[j] && labelX[i] + labelY[j] == graph[i][j]) {         usedY[j] = true;         if (linkTo[j] == -1 || findPath(linkTo[j])) {           linkTo[j] = i;           return true;         }       }     return false;   }   int KuhnMunkres() {     memset(linkTo, -1, sizeof(linkTo));     memset(labelX, 0, sizeof(labelX));     memset(labelY, 0, sizeof(labelY));     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++) labelX[i] = max(labelX[i], graph[i][j]);     for (int k = 0; k < n; k++)       while (true) {         memset(usedX, 0, sizeof(usedX));         memset(usedY, 0, sizeof(usedY));         if (findPath(k)) break;         int delta = INF;         for (int i = 0; i < n; i++)           if (usedX[i])             for (int j = 0; j < n; j++)               if (!usedY[j])                 delta = min(delta, labelX[i] + labelY[j] - graph[i][j]);         if (delta == 0 || delta == INF) break;         for (int i = 0; i < n; i++)           if (usedX[i]) labelX[i] -= delta;         for (int i = 0; i < n; i++)           if (usedY[i]) labelY[i] += delta;       }     int maxTotal = 0;     for (int i = 0; i < n; i++) maxTotal += labelX[i] + labelY[i];     return maxTotal;   }   };  // namespace KuhnMunkres   int main() {     cin.sync_with_stdio(false);     cin >> N >> K;     cin >> A;     cin >> B;     KuhnMunkres::n = K;     for (int i = 0; i < N; i++) {       int u = id(A[i]);       int v = id(B[i]);       KuhnMunkres::graph[u][v]++;     }     cout << KuhnMunkres::KuhnMunkres() << endl;     int array[MaxK];     for (int i = 0; i < K; i++) {       array[KuhnMunkres::linkTo[i]] = i;     }     for (int i = 0; i < K; i++) {       int b = array[i];       if (b < 26)         cout << (char)( a  + b);       else         cout << (char)( A  + b - 26);     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   const int maxi = 2000000000;   const int maxq = 1000000000;   const double eps = 1e-10;   const double pi = 3.1415926535897932;   const double inf = 1e+18;   const int mo = 1000000007;   using namespace std;   int stn, ms[1111][1111], n, k, t, st[1111111], x, y, z, sum;   bool f[11111];   void rec(int x, int y) {     sum += ms[x][y];     if (x == n) {       stn++;       st[stn] = sum;     }     if (y) f[y] = true;     if (x < n)       for (int i = 0; i <= n; i++)         if (f[i] == false && (ms[x + 1][i] || i == 0)) rec(x + 1, i);     sum -= ms[x][y];     f[y] = false;   }   int main() {     cin >> n >> k >> t;     for (int i = 0; i < k; i++) {       cin >> x >> y >> z;       ms[x][y] = z;     }     for (int i = 0; i <= n; i++)       if (ms[1][i] || i == 0) rec(1, i);     sort(st + 1, st + stn + 1);     cout << st[t] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<vector<long long> > dp;   const long long mod = 1e9 + 7;   bool used[1005][1005];   int n;   long long dfs(int opening, int closing) {     if (dp[opening][closing] != -1) return dp[opening][closing];     if (opening > n or closing >= n) return 0;     long long ans = 0;     if (opening < n) {       ans += dfs(opening + 1, closing);       if (!used[opening + 1][closing] and !used[opening][closing]) {         ans++;         used[opening][closing] = 1;       }     }     if (closing < opening) {       ans += dfs(opening, closing + 1);       if (!used[opening][closing + 1] and !used[opening][closing]) {         ans++;         used[opening][closing] = 1;       }     }     return dp[opening][closing] = ans % mod;   }   int main() {     dp = vector<vector<long long> >(1005, vector<long long>(1005, -1));     cin >> n;     cout << dfs(0, 0) << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> v(n);     for (int i = 0; i < n; ++i) v[i] = i + 2;     int n_colors = 1;     vector<bool> prime(n, true);     vector<int> colors(n);     for (int i = 0; v[i] <= sqrt(n + 1); ++i) {       if (prime[i]) {         for (int j = v[i] * v[i] - 2; j < n; j += v[i]) {           prime[j] = false;         }       }     }     for (int i = 0; i < n; ++i) {       if (prime[i]) {         colors[i] = 1;       } else {         n_colors = 2;         colors[i] = 2;       }     }     cout << n_colors << endl;     for (auto c : colors) cout << c <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   unordered_map<long long int, long long int> mp;   long long int n, m, k, ans;   long long int a[22][22];   int dx[] = {1, -1, 0, 0};   int dy[] = {0, 0, 1, -1};   void dfs1(int ex, int ey, int sx, int sy, long long int cur) {     if (sx == ex and sy == ey) mp[cur]++;     if (sx < ex) dfs1(ex, ey, sx + 1, sy, cur ^ (a[sx][sy]));     if (sy < ey) dfs1(ex, ey, sx, sy + 1, cur ^ (a[sx][sy]));   }   void dfs(int x, int y, long long int cur) {     if (x == n - 1 and y == m - 1) cur ^= a[x][y], ans += mp[cur ^ k];     if (x < n - 1) dfs(x + 1, y, cur ^ (a[x][y]));     if (y < m - 1) dfs(x, y + 1, cur ^ (a[x][y]));   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n >> m >> k;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) cin >> a[i][j];     int mid = (n + m - 2) / 2;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (i + j == mid) {           dfs1(i, j, 0, 0, 0);           dfs(i, j, 0);           mp.clear();         }       }     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   const int inf = 0x3f3f3f3f, Inf = 0x7fffffff;   const long long INF = 0x7fffffffffffffff;   template <typename _Tp>   _Tp gcd(const _Tp &a, const _Tp &b) {     return (!b) ? a : gcd(b, a % b);   }   template <typename _Tp>   __inline__ __attribute__((always_inline)) _Tp abs(const _Tp &a) {     return a >= 0 ? a : -a;   }   template <typename _Tp>   __inline__ __attribute__((always_inline)) _Tp max(const _Tp &a, const _Tp &b) {     return a < b ? b : a;   }   template <typename _Tp>   __inline__ __attribute__((always_inline)) _Tp min(const _Tp &a, const _Tp &b) {     return a < b ? a : b;   }   template <typename _Tp>   __inline__ __attribute__((always_inline)) void chmax(_Tp &a, const _Tp &b) {     (a < b) && (a = b);   }   template <typename _Tp>   __inline__ __attribute__((always_inline)) void chmin(_Tp &a, const _Tp &b) {     (b < a) && (a = b);   }   template <typename _Tp>   __inline__ __attribute__((always_inline)) void read(_Tp &x) {     char ch(getchar());     bool f(false);     while (ch < 48 || ch > 57) f |= ch == 45, ch = getchar();     x = ch & 15, ch = getchar();     while (ch >= 48 && ch <= 57)       x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar();     if (f) x = -x;   }   template <typename _Tp, typename... Args>   __inline__ __attribute__((always_inline)) void read(_Tp &t, Args &...args) {     read(t);     read(args...);   }   __inline__ __attribute__((always_inline)) int read_str(char *s) {     char ch(getchar());     while (ch ==     || ch ==   r  || ch ==   n ) ch = getchar();     char *tar = s;     *tar = ch, ch = getchar();     while (ch !=     && ch !=   r  && ch !=   n  && ch != EOF)       *(++tar) = ch, ch = getchar();     return tar - s + 1;   }   const int N = 200005;   const int mod = 998244353;   template <typename _Tp1, typename _Tp2>   __inline__ __attribute__((always_inline)) void add(_Tp1 &a, _Tp2 b) {     (a += b) >= mod && (a -= mod);   }   template <typename _Tp1, typename _Tp2>   __inline__ __attribute__((always_inline)) void sub(_Tp1 &a, _Tp2 b) {     (a -= b) < 0 && (a += mod);   }   long long ksm(long long a, long long b = mod - 2) {     long long res = 1;     while (b) {       if (b & 1) res = res * a % mod;       a = a * a % mod, b >>= 1;     }     return res;   }   long long fac[N], inv[N];   void init() {     fac[0] = fac[1] = inv[0] = inv[1] = 1;     for (int i = 2; i < N; ++i) {       fac[i] = fac[i - 1] * i % mod;       inv[i] = inv[mod % i] * (mod - mod / i) % mod;     }     for (int i = 2; i < N; ++i) {       inv[i] = inv[i - 1] * inv[i] % mod;     }   }   __inline__ __attribute__((always_inline)) long long C(int n, int m) {     return fac[n] * inv[m] % mod * inv[n - m] % mod;   }   long long S(int n, int m) {     long long ans = 0;     for (int i = 0; i <= m; ++i) {       if ((m - i) & 1)         sub(ans, C(m, i) * ksm(i, n) % mod);       else         add(ans, C(m, i) * ksm(i, n) % mod);     }     ans = ans * inv[m] % mod;     return ans;   }   int main() {     init();     int n;     long long k;     read(n, k);     if (k == 0)       printf( %lld n , fac[n]);     else if (k >= n)       printf( %lld n , 0ll);     else       printf( %lld n , 2ll * C(n, n - k) * fac[n - k] % mod * S(n, n - k) % mod);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[2000050];   int b[2000050];   int c[2000050];   struct node {     int x, y;     node() {}     node(int xx, int yy) { x = xx, y = yy; }   } q[2000050];   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; ++i) scanf( %d , &a[i]);     for (int i = 1; i <= n; ++i) {       scanf( %d , &b[i]);       c[b[i]] = i;     }     for (int i = 1; i <= n; ++i) a[i] = c[a[i]];     int tail = 0;     int ans = 0;     for (int i = n; i > 0; --i) {       int p = -1;       for (int j = n; j > 0; --j)         if (a[j] == i) {           p = j;           break;         }       if (p == i) continue;       int now = p + 1;       while (p != i) {         while (now < n && a[now] > p) now++;         q[tail++] = node(p, now);         swap(a[p], a[now]);         ans += now - p;         p = now;         now++;       }     }     printf( %d n , ans);     printf( %d n , tail);     for (int i = 0; i < tail; ++i) {       printf( %d %d n , q[i].x, q[i].y);     }   }


#include <bits/stdc++.h>   using namespace std;   int main(int argc, char const *argv[]) {     int bags[4];     int flag = 0;     for (int i = 0; i < 4; i++) {       cin >> bags[i];     }     if (bags[0] + bags[1] == bags[2] + bags[3]) {       cout <<  YES n ;     } else if (bags[0] + bags[2] == bags[1] + bags[3]) {       cout <<  YES n ;     } else if (bags[0] + bags[3] == bags[1] + bags[2]) {       cout <<  YES n ;     } else if (bags[0] + bags[1] + bags[2] == bags[3] ||                bags[0] + bags[2] + bags[3] == bags[1] ||                bags[0] + bags[1] + bags[3] == bags[2] ||                bags[3] + bags[1] + bags[2] == bags[0]) {       cout <<  YES n ;     } else {       cout <<  NO n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main(void) {     string s, t;     cin >> s >> t;     int ind = -1;     for (int i = 0; i < s.length(); i++) {       if (s[i] !=  0 )         break;       else         ind = i;     }     s = s.substr(ind + 1);     ind = -1;     for (int i = 0; i < t.length(); i++) {       if (t[i] !=  0 )         break;       else         ind = i;     }     t = t.substr(ind + 1);     if (s.length() != t.length()) {       if (s.length() > t.length())         cout <<  > n ;       else         cout <<  < n ;       return 0;     }     for (int i = 0; i < s.length(); i++) {       if (s[i] != t[i]) {         if (s[i] > t[i])           cout <<  > n ;         else           cout <<  < n ;         return 0;       }     }     cout <<  = n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, h;     cin >> n >> h;     vector<pair<int, int>> zeros;     int a, b;     for (int i = 0; i < n; i++) {       cin >> a >> b;       zeros.push_back(make_pair(a, b));     }     int sol = 0;     int pa, pb, ib, c1;     pa = zeros[0].first;     pb = zeros[0].second;     ib = 0;     c1 = 0;     while (c1 < h) {       int d = h - c1;       if (ib + 1 >= n) {         pb = pb + d;         c1 = h;         break;       }       if (d <= zeros[ib + 1].first - pb) {         pb = pb + d;         c1 = h;         break;       }       c1 += zeros[ib + 1].first - pb;       pb = zeros[ib + 1].second;       ib++;     }     sol = pb - pa;     for (int i = 1; i < n; i++) {       int ci1 = zeros[i].first - zeros[i - 1].second;       pa = zeros[i].first;       if (h < ci1) {         pb = zeros[i].second;         ib = i;         c1 = 0;         while (c1 < h) {           int d = h - c1;           if (ib + 1 >= n) {             pb = pb + (h - c1);             c1 = h;             break;           }           if (d <= zeros[ib + 1].first - pb) {             pb = pb + (h - c1);             c1 = h;             break;           }           c1 += zeros[ib + 1].first - pb;           pb = zeros[ib + 1].second;           ib++;         }       } else {         c1 = h - ci1;         while (c1 < h) {           int d = h - c1;           if (ib + 1 >= n) {             pb = pb + (h - c1);             c1 = h;             break;           }           if (d <= zeros[ib + 1].first - pb) {             pb = pb + (h - c1);             c1 = h;             break;           }           c1 += zeros[ib + 1].first - pb;           pb = zeros[ib + 1].second;           ib++;         }       }       sol = max(sol, pb - pa);     }     cout << sol;   }


#include <bits/stdc++.h>   using namespace std;   int n;   string s;   int main() {     cin >> n;     cin >> s;     for (int i = 0; i < n; ++i) {       int c1 = 0, c2 = 0;       for (int j = i - 1; j >= 0; --j) {         if (s[j] ==  L ) {           break;         }         if (s[j] ==  R ) {           ++c1;         }       }       for (int j = i; j < n - 1; ++j) {         if (s[j] ==  R ) {           break;         }         if (s[j] ==  L ) {           ++c2;         }       }       cout << max(c1, c2) + 1 <<    ;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n, m;     cin >> n >> m;     vector<long long> x(n), y(n);     for (int i = 0; i < n; i++) {       cin >> x[i] >> y[i];       y[i] -= x[i];     }     vector<vector<int>> g(n);     for (int i = 0; i < m; i++) {       int foo, bar;       cin >> foo >> bar;       foo--;       bar--;       g[foo].push_back(bar);       g[bar].push_back(foo);     }     long long sum_x = accumulate(x.begin(), x.end(), 0LL);     vector<long long> sorted_y = y;     sort(sorted_y.begin(), sorted_y.end());     vector<long long> pref(n + 1);     for (int i = 0; i < n; i++) {       pref[i + 1] = pref[i] + sorted_y[i];     }     for (int i = 0; i < n; i++) {       long long ans = x[i] * (n - 1) + (sum_x - x[i]);       int pos = (int)(lower_bound(sorted_y.begin(), sorted_y.end(), y[i]) -                       sorted_y.begin());       ans += pref[pos] + y[i] * (n - pos - 1);       for (int j : g[i]) {         long long cur = x[i] + x[j] + min(y[i], y[j]);         ans -= cur;       }       if (i > 0) {         cout <<    ;       }       cout << ans;     }     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int m, n;     scanf( %d%d , &n, &m);     printf( %d n , m * n / 2);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int boxes;     cin >> boxes;     int arr[boxes];     for (int i = 0; i < boxes; i++) cin >> arr[i];     int shots;     cin >> shots;     int x, y;     for (int i = 0; i < shots; i++) {       cin >> x >> y;       if (x == 1) {         arr[x] += (arr[x - 1] - y);         arr[0] = 0;       } else if (x == boxes) {         arr[boxes - 1] = 0;         arr[x - 2] += (y - 1);       } else {         arr[x] += (arr[x - 1] - y);         arr[x - 2] += (y - 1);         arr[x - 1] = 0;       }     }     for (int i = 0; i < boxes; i++) cout << arr[i] << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     long long int a[2 * n];     for (int i = 0; i < 2 * n; i++) cin >> a[i];     sort(a, a + (2 * n));     long long int s = 2e19;     ;     for (int i = 1; i < n; i++) {       s = min(s, (a[n * 2 - 1] - a[0]) * (a[n + i - 1] - a[i]));     }     s = min(s, (a[n - 1] - a[0]) * (a[n * 2 - 1] - a[n]));     cout << s;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   struct node {     char type;     vector<node*> children;     ll dp = 0;     ll r = 0;   };   node* get(string& s, int& idx) {     node* v = new node();     if (s[idx] ==  * ) {       v->type =  * ;       idx++;       return v;     } else if (s[idx] ==  ( ) {       idx++;       while (true) {         v->children.push_back(get(s, idx));         if (s[idx] ==  P  || s[idx] ==  S ) {           v->type = s[idx];           idx++;         } else if (s[idx] ==  ) ) {           idx++;           break;         } else           assert(false);       }     } else       assert(false);     return v;   }   const ll Z = 2e18;   ll sq(ll r) {     if (r > Z / r) return Z;     return min(r * r, Z);   }   void make_dp(node* v) {     if (v->type ==  * ) {       v->dp = 1;     } else {       for (node* w : v->children) {         make_dp(w);       }       if (v->type ==  S ) {         v->dp = Z;         for (node* w : v->children) v->dp = min(v->dp, w->dp);       } else if (v->type ==  P ) {         v->dp = 0;         for (node* w : v->children) v->dp += w->dp;       } else         assert(false);     }   }   ll res = 0;   void make_resistances(node* v, bool good) {     if (v->type ==  * ) {       cout <<     << (good ? res : 0);     } else if (v->type ==  P ) {       for (node* w : v->children) {         make_resistances(w, good);       }     } else if (v->type ==  S ) {       for (node* w : v->children) {         if (w->dp == v->dp) {           make_resistances(w, good);           good = false;         } else {           make_resistances(w, false);         }       }     } else       assert(false);   }   void solve() {     string s;     getline(cin, s);     auto g = s.find(   );     ll R = stoll(s.substr(0, g));     s = s.substr(g + 1);     s.resize(remove_if(s.begin(), s.end(), [&](char a) { return a ==    ; }) -              s.begin());     int idx = 0;     node* z = get(s, idx);     make_dp(z);     res = R * z->dp;     cout <<  REVOLTING ;     make_resistances(z, true);     cout <<   n ;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int T;     string s;     getline(cin, s);     T = stoi(s);     while (T--) solve();   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 3e5 + 10;   int n, b[maxn];   pair<int, int> a[maxn];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i].first);       a[i].second = i;     }     sort(a + 1, a + 1 + n);     reverse(a + 1, a + 1 + n);     for (int i = 1; i <= n; i++) {       b[i] = a[i].second * 2 - 1;       if (i > 1) printf( %d %d n , b[i], b[i - 1]);     }     int res, cur = n, des;     for (int i = 1; i <= n; i++) {       res = a[i].second * 2;       des = i + a[i].first - 1;       if (des >= cur) {         b[++cur] = res;         printf( %d %d n , res, b[cur - 1]);       } else         printf( %d %d n , res, b[des]);     }   }


#include <bits/stdc++.h>   int main() {     int n, x, y, z, sum1 = 0, sum2 = 0, sum3 = 0;     scanf( %d , &n);     while (n--) {       scanf( %d %d %d , &x, &y, &z);       sum1 = sum1 + x;       sum2 = sum2 + y;       sum3 = sum3 + z;     }     if (sum1 == 0 && sum2 == 0 && sum3 == 0)       printf( YES );     else       printf( NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 22;   long long a[N];   long long dp[N][N][N];   long long DP(long long pos, long long sum, long long limit) {     if (!pos) return 1;     if (!limit && dp[pos][sum][limit] != -1) return dp[pos][sum][limit];     long long high = limit ? a[pos] : 9;     long long ans = 0;     for (long long i = 0; i <= high; i++) {       if (sum == 3 && i != 0) continue;       ans += DP(pos - 1, sum + (i != 0), limit && (i == high));     }     if (!limit) dp[pos][sum][limit] = ans;     return ans;   }   long long slove(long long n) {     long long len = 0;     while (n) {       a[++len] = n % 10;       n = n / 10;     }     return DP(len, 0, 1);   }   signed main() {     memset(dp, -1, sizeof(dp));     long long T;     cin >> T;     while (T--) {       long long L, R;       cin >> L >> R;       cout << slove(R) - slove(L - 1) <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[101000];   long long pre[101000];   map<long long, long long> Hash;   int main() {     int n, k;     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]);     for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + a[i];     Hash[0] = 1;     long long count = 0;     for (int i = 1; i <= n; i++) {       if (abs(k) > 1) {         for (long long p = 1; (p > 0 ? p : -p) <= 1e15; p *= k) {           if (Hash[pre[i] - p]) count += Hash[pre[i] - p];         }       } else {         if (k == 1 || k == -1) {           if (Hash[pre[i] - 1]) count += Hash[pre[i] - 1];           if (k == -1) {             if (Hash[pre[i] + 1]) count += Hash[pre[i] + 1];           }         } else {           if (Hash[pre[i]]) count += Hash[pre[i]];         }       }       Hash[pre[i]] += 1;     }     cout << count << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int mat[4005][4005];   int acum[4005][4005];   int dp[4005][805];   int step(int n, int k, int min_i, int max_i) {     dp[n][k] = dp[min_i - 1][k - 1] + (acum[n][n] + acum[min_i - 1][min_i - 1] -                                        acum[n][min_i - 1] - acum[min_i - 1][n]) /                                           2;     for (int i = min_i + 1; i <= max_i; i++) {       if (dp[i - 1][k - 1] + (acum[n][n] + acum[i - 1][i - 1] - acum[n][i - 1] -                               acum[i - 1][n]) /                                  2 <           dp[n][k]) {         dp[n][k] = dp[i - 1][k - 1] + (acum[n][n] + acum[i - 1][i - 1] -                                        acum[n][i - 1] - acum[i - 1][n]) /                                           2;         min_i = i;       }     }     return min_i;   }   void conquer(int min_n, int max_n, int k, int min_i, int max_i) {     if (min_n > max_n) return;     max_i = min(max_n, max_i);     if (min_n == max_n) {       step(min_n, k, min_i, max_i);       return;     }     if (min_i == max_i) {       for (int i = min_n; i <= max_n; i++) step(i, k, min_i, max_i);       return;     }     int mid = (min_n + max_n) / 2;     int i = step(mid, k, min_i, min(max_i, mid));     conquer(min_n, mid - 1, k, min_i, i);     conquer(mid + 1, max_n, k, i, max_i);   }   char buff[100005];   int main() {     int n, k;     scanf( %d %d n , &n, &k);     for (int i = 1; i <= n; i++) {       int acum2 = 0;       gets(buff);       for (int j = 1; j <= n; j++) {         acum2 += buff[j * 2 - 2] -  0 ;         acum[i][j] = acum[i - 1][j] + acum2;       }     }     for (int i = 1; i <= n; i++) {       dp[i][1] = acum[i][i] / 2;     }     for (int j = 2; j <= k; j++) {       conquer(1, n, j, j - 1, n);     }     printf( %d n , dp[n][k]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   set<int> si;   int n, k;   bool solve(set<int> &cands, vector<int> &chosen) {     if (cands.empty()) {       if (chosen.empty()) {         return false;       }       for (int i = 0; i < k; ++i) {         int cnt = 0;         for (const int num : chosen) {           cnt += (((num >> i) & 0x1) ? -1 : 1);         }         if (cnt < 0) {           return false;         }       }       return true;     }     int val = *cands.begin();     bool ok = false;     cands.erase(val);     ok |= solve(cands, chosen);     chosen.push_back(val);     ok |= solve(cands, chosen);     chosen.pop_back();     cands.insert(val);     return ok;   }   int main(int argc, char **argv) {     std::ios::sync_with_stdio(false);     cin >> n >> k;     for (int i = 0; i < n; ++i) {       int val = 0;       for (int j = 0; j < k; ++j) {         int t;         cin >> t;         val |= (t << j);       }       si.insert(val);     }     vector<int> chosen;     cout << (solve(si, chosen) ?  YES n  :  NO n );   }


#include <bits/stdc++.h>   using namespace std;   int a[5005], cnt, tmp, nxt, V[5005], j, n, i;   long long dp[2][5005], ans;   struct LS {     int pos, val;   } ls[5005];   inline bool cmp(LS aa, LS bb) { return aa.val < bb.val; }   int main() {     cin >> n;     for (i = 1; i <= n; ++i) cin >> a[i], ls[i].val = a[i], ls[i].pos = i;     sort(ls + 1, ls + n + 1, cmp);     ls[0].val = -1000000001;     for (i = 1; i <= n; ++i) {       if (ls[i].val != ls[i - 1].val) ++cnt, V[cnt] = ls[i].val;       a[ls[i].pos] = cnt;     }     for (i = 1; i <= a[1]; ++i) dp[0][i] = V[a[1]] - V[i];     for (i = a[1] + 1; i <= cnt; ++i) dp[0][i] = 0;     for (i = 2; i <= n; tmp ^= 1, ++i) {       nxt = tmp ^ 1;       dp[nxt][0] = 1e16;       for (j = 1; j <= cnt; ++j)         dp[nxt][j] = min(dp[nxt][j - 1], dp[tmp][j] + abs(V[a[i]] - V[j]));     }     for (ans = 1e16, j = 1; j <= cnt; ++j) ans = min(ans, dp[tmp][j]);     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, d;     cin >> n >> d;     int t;     cin >> t;     while (t--) {       long long int x, y;       cin >> x >> y;       long long int eq1 = y * (n - d) - ((n - d) * (x - d));       long long int eq2 = (y - (n - d)) + (x - (n));       long long int eq3 = ((y - n) - (x - (n - d)));       long long int eq4 = (y - d) + (x);       if (eq1 >= 0 && eq2 <= 0 && eq3 <= 0 && eq4 >= 0)         cout <<  YES n ;       else         cout <<  NO n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     bool flag = 0, flag2 = 0;     long long n, m, c = 0;     cin >> n >> m;     for (long long i = 0; i < n; i++) {       for (long long j = 0; j < m; j++) {         if (i % 2 != 0 && c % 2 == 0 && j != m - 1) {           cout <<  . ;           flag = 1;         } else if (i % 2 != 0 && c % 2 != 0 && j != 0) {           cout <<  . ;           flag2 = 1;         } else           cout <<  # ;       }       cout << endl;       if (flag || flag2) c++;       flag = 0, flag2 = 0;     }     return 0;   }


#include<bits/stdc++.h>   using namespace std;   typedef long long int ll;   ll mod=1e9+7;   int main()   {    int t;    cin>>t;    while(t--)    {     ll n,k;     cin>>n>>k;     ll ans=1;     for(ll i=0;i<k;i++)     {      ans=(ans*n)%mod;     }     cout<<ans<<endl;         }   }


#include <bits/stdc++.h>   using namespace std;   int _I() {     int x;     scanf( %d , &x);     return x;   }   const int lmt = 10000000;   bool prime[lmt + 5];   int rng = 1, v[lmt + 5];   void sv() {     v[0] = 2;     for (int i = 4; i <= lmt; i += 2) prime[i] = 1;     for (int i = 3; i <= lmt; i += 2) {       if (!prime[i]) {         v[rng++] = i;         for (int j = i * 3; j <= lmt; j += i << 1) prime[j] = 1;       }     }     prime[1] = 1;   }   int rev(int n) {     int num = 0;     while (n) {       num *= 10;       num += (n % 10);       n /= 10;     }     return num;   }   void solve() {     int n = _I();     int ans[100001];     for (int i = 13, j = 1; j <= 11184; i++) {       if (i != rev(i) && !prime[i] && !prime[rev(i)]) ans[j++] = i;     }     cout << ans[n] <<   n ;   }   int main() {     sv();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double radian = 180 / acos(-1);   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long x, mini = LLONG_MAX, p;     cin >> x;     x %= 360;     if (x < 0) x = 360 + x;     for (int i = 0; i < 4; i++) {       if (min(x, 360 - x) < mini) {         mini = min(x, 360 - x);         p = i;       }       x = (x + 270) % 360;     }     cout << p;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline T sqr(const T &a) {     return a * a;   }   template <typename T>   inline int nread(vector<T> &a) {     int n;     cin >> n;     a.clear();     a.resize(n);     for (int i = 0; i < n; i++) cin >> a[i];     return n;   }   template <typename T>   inline void nwread(int n, vector<T> &a) {     a.clear();     a.resize(n);     for (int i = 0; i < n; i++) cin >> a[i];   }   int main() {     int n, k;     cin >> n >> k;     vector<int> a(n);     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         if (a[j] != 0) continue;         cout << j + 1 <<    ;         a[j] = -1;         for (int l = 0; l <= j - k; l++) a[l]--;         break;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 100;   long long t;   long long n;   long long a[N];   long long b[N];   map<long long, long long> ma;   int main() {     scanf( %lld , &t);     while (t--) {       ma.clear();       memset(a, 0, sizeof(a));       memset(b, 0, sizeof(b));       scanf( %lld , &n);       for (long long i = 1; i <= n; i++) scanf( %lld , &a[i]);       for (long long i = 1; i <= n; i++) scanf( %lld , &b[i]);       if (a[1] != b[1]) {         printf( NO n );         continue;       }       ma[a[1]] = 1;       bool f = false;       for (long long i = 2; i <= n; i++) {         if (a[i] == b[i]) {           ma[a[i]] = 1;           continue;         }         if (f) break;         long long cha = b[i] - a[i];         if (cha < 0) {           if (ma[-1] > 0) {           } else {             f = true;             break;           }         }         if (cha > 0) {           if (ma[1] > 0) {           } else {             f = true;             break;           }         }         if (f) break;         ma[a[i]] = 1;       }       if (f) {         printf( NO n );       } else {         printf( YES n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long x;     cin >> x;     return x;   }   const int MAX_N = 15;   const int MAX_F = (1 << (MAX_N - 2)) + 4;   int n, m, nQ, id[MAX_N];   bool isEdge[MAX_N][MAX_N], isParent[MAX_N][MAX_N], isNotParent[MAX_N][MAX_N],       isLCA[MAX_N][MAX_N][MAX_N];   bool ok1[MAX_F][MAX_N], ok2[MAX_F][MAX_N], ok3[MAX_F][MAX_N][MAX_N],       ok4[MAX_F][MAX_F], ok5[MAX_F][MAX_N];   long long f[MAX_F][MAX_N];   inline bool check_0(int u, int v, int r) {     if (u == v && r != u) return 0;     if ((u == 1 || v == 1) && r != 1) return 0;     return 1;   }   void init(int nMask) {     memset(ok1, 0, sizeof(ok1));     memset(ok2, 0, sizeof(ok2));     memset(ok3, 0, sizeof(ok3));     memset(ok4, 0, sizeof(ok4));     memset(ok5, 0, sizeof(ok5));     for (long long x = (0); x <= (nMask); ++x)       for (long long i = (1); i <= (n); ++i)         if (x & id[i]) {           ok1[x][i] = 1;           for (long long j = (1); j <= (n); ++j)             if (j != i && (x & id[j]))               if (isParent[j][i] || isNotParent[i][j]) {                 ok1[x][i] = 0;                 break;               }         }     for (long long x = (0); x <= (nMask); ++x)       for (long long i = (1); i <= (n); ++i)         if (!(x & id[i])) {           ok2[x][i] = 1;           for (long long j = (1); j <= (n); ++j)             if (x & id[j])               if (isParent[j][i] || isParent[i][j] || isEdge[j][i] ||                   isEdge[i][j]) {                 ok2[x][i] = 0;                 break;               }         }     for (long long x = (0); x <= (nMask); ++x)       for (long long i = (1); i <= (n); ++i)         if (x & id[i])           for (long long r = (1); r <= (n); ++r)             if (!(x & id[r])) {               ok3[x][i][r] = 1;               for (long long j = (1); j <= (n); ++j)                 if (j != i && (x & id[j]))                   if (isEdge[r][j]) {                     ok3[x][i][r] = 0;                     break;                   }             }     for (long long x = (0); x <= (nMask); ++x) {       for (long long y = (0); y <= (nMask); ++y)         if (!(x & y)) {           ok4[x][y] = 1;           for (long long i = (1); i <= (n); ++i)             if (x & id[i]) {               for (long long j = (1); j <= (n); ++j)                 if (y & id[j])                   if (isParent[i][j] || isParent[j][i] || isEdge[i][j] ||                       isEdge[j][i]) {                     ok4[x][y] = 0;                     break;                   }               if (!ok4[x][y]) break;             }         }     }     for (long long x = (0); x <= (nMask); ++x)       for (long long r = (1); r <= (n); ++r)         if (!(x & id[r])) {           ok5[x][r] = 1;           for (long long i = (1); i <= (n); ++i)             if (ok5[x][r])               if (x & id[i])                 for (long long j = (1); j <= (n); ++j)                   if (x & id[j])                     if (i != j)                       if (isLCA[r][i][j]) {                         ok5[x][r] = 0;                         break;                       }         }   }   inline bool check(int mask, int r, int newMask, int newR) {     int x = mask - newMask;     return ok1[newMask][newR] && ok2[x][newR] && ok3[newMask][newR][r] &&            ok4[newMask][x] && ok5[newMask][r];   }   long long solve(int mask, int par) {     if (mask == 0) return 1;     if (f[mask][par] > -1) return f[mask][par];     long long ans = 0;     int x = 0;     do {       if (x > 0 && __builtin_clz(x) == __builtin_clz(mask))         for (long long r = (1); r <= (n); ++r)           if (x & id[r]) {             if (!check(mask, par, x, r)) continue;             long long t = solve(x - id[r], r) * solve(mask - x, par);             ans += t;           }       x = (x + 1 + (~mask)) & mask;     } while (x);     return f[mask][par] = ans;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.precision(9);     cout.setf(ios::fixed, ios::floatfield);     memset(isEdge, 0, sizeof(isEdge));     memset(isParent, 0, sizeof(isParent));     memset(isNotParent, 0, sizeof(isNotParent));     memset(isLCA, 0, sizeof(isLCA));     cin >> n >> m >> nQ;     for (long long i = (1); i <= (n); ++i) id[i] = 1 << (i - 1);     while (m--) {       int u, v;       cin >> u >> v;       isEdge[u][v] = isEdge[v][u] = 1;     }     while (nQ--) {       int u, v, r;       cin >> u >> v >> r;       if (!check_0(u, v, r)) {         cout << 0 <<   n ;         return 0;       }       if (r != u) isNotParent[u][v] = 1;       if (r != v) isNotParent[v][u] = 1;       isParent[r][u] = isParent[r][v] = 1;       if (r != u && r != v) isLCA[r][u][v] = isLCA[r][v][u] = 1;     }     for (long long i = (1); i <= (n); ++i) isParent[i][i] = isNotParent[i][i] = 0;     memset(f, -1, sizeof(f));     int mask = 0;     for (long long i = (2); i <= (n); ++i) mask |= id[i];     init(mask);     cout << solve(mask, 1) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k;   char playLog[1111];   char posLet[3] = { W ,  D ,  L };   int delta[3] = {1, 0, -1};   int val[256];   char dp[1111][2222];   void clearArray() {     for (int i = 0; i <= n + 1; i++) {       for (int j = -k; j <= k; j++) dp[i][j + k] =  X ;     }   }   void printAnswer() {     int currentState = 0;     for (int i = 0; i < n; i++) {       char thisOne = dp[i][currentState + k];       printf( %c , thisOne);       currentState += val[thisOne];     }     printf(  n );   }   int main() {     val[ W ] = 1;     val[ D ] = 0;     val[ L ] = -1;     scanf( %d%d , &n, &k);     scanf( %s , playLog);     clearArray();     dp[n][k + k] =  P ;     for (int i = n - 1; i >= 0; i--) {       for (int j = -(k - 1); j <= (k - 1); j++) {         for (int q = 0; q < 3; q++) {           if (playLog[i] !=  ?  && (playLog[i] != posLet[q])) continue;           int target = j + delta[q];           if (dp[i + 1][target + k] ==  X ) continue;           dp[i][j + k] = posLet[q];           break;         }       }     }     if (dp[0][0 + k] !=  X ) {       printAnswer();       return 0;     }     clearArray();     dp[n][0] =  P ;     for (int i = n - 1; i >= 0; i--) {       for (int j = -(k - 1); j <= (k - 1); j++) {         for (int q = 0; q < 3; q++) {           if (playLog[i] !=  ?  && (playLog[i] != posLet[q])) continue;           int target = j + delta[q];           if (dp[i + 1][target + k] ==  X ) continue;           dp[i][j + k] = posLet[q];           break;         }       }     }     if (dp[0][0 + k] !=  X ) {       printAnswer();       return 0;     }     printf( NO n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int shape[4][4][2] = {{{1, 0}, {2, 0}, {1, 1}, {1, 2}},                               {{1, 0}, {2, 0}, {2, -1}, {2, 1}},                               {{0, 1}, {0, 2}, {-1, 2}, {1, 2}},                               {{0, 1}, {0, 2}, {1, 1}, {2, 1}}};   int x, y;   struct node {     char g[9][9];     int num;     int sx, sy;     node() {       sx = sy = 0;       memset(g, 0, sizeof(g));       num = 0;     }     void printNode() {       for (int i = 0; i < y; i++) {         for (int j = 0; j < x; j++) printf( %c , g[i][j] ? g[i][j] :  . );         cout <<    << endl;       }     }   };   stack<node> q;   int ans;   node ansNode;   void bfs() {     while (true) {     The_End_Where_I_Began:       if (q.empty()) break;       node now = q.top();       q.pop();       if (now.num > ans) {         ans = now.num;         ansNode = now;       }       now.num++;       char ch =  A  + now.num - 1;       bool flag = true;       for (int i = now.sy; i < y; i++) {         int j = (now.sy == i ? now.sx : 0);         for (; j < x; j++) {           if (!now.g[i][j]) {             flag = true;             for (int k = 3; k >= 0; k--) {               flag = true;               for (int l = 0; l < 4; l++) {                 int tx = j + shape[k][l][1];                 int ty = i + shape[k][l][0];                 if (tx < 0 || tx >= x || ty < 0 || ty >= y || now.g[ty][tx]) {                   flag = false;                   break;                 }               }               if (flag) {                 node tNode = now;                 tNode.g[i][j] = ch;                 for (int l = 0; l < 4; l++) {                   int tx = j + shape[k][l][1];                   int ty = i + shape[k][l][0];                   tNode.g[ty][tx] = ch;                 }                 tNode.sx = j + 1;                 tNode.sy = i;                 q.push(tNode);               }             }             if (flag) goto The_End_Where_I_Began;           }         }       }     }   }   int main() {     ans = -1;     cin >> y >> x;     while (!q.empty()) q.pop();     q.push(node());     bfs();     cout << ans << endl;     ansNode.printNode();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using pii = pair<int, int>;   using pll = pair<ll, ll>;   const ll inf = 1;   const ll mod = 1E9;   int main() {     int n, m;     scanf( %d %d , &n, &m);     int colors = 1;     vector<vector<int> > mset(n, vector<int>());     vector<vector<int> > G(n, vector<int>());     for (int i = 0; i < n; i++) {       int lim;       scanf( %d , &lim);       for (int j = 0; j < lim; j++) {         int x;         scanf( %d , &x);         mset[i].push_back(x - 1);       }       colors = max(colors, lim);     }     for (int i = 0; i < n - 1; i++) {       int u, v;       scanf( %d %d , &u, &v);       G[u - 1].push_back(v - 1);       G[v - 1].push_back(u - 1);     }     set<int> ms;     vector<int> col(m, 0);     vector<bool> vis(n, false);     queue<int> Q;     Q.push(0);     vis[0] = true;     while (!Q.empty()) {       int u = Q.front();       Q.pop();       for (auto j : mset[u]) {         if (col[j] != 0) ms.insert(col[j]);       }       int now = 1;       for (auto j : mset[u]) {         if (col[j] == 0) {           while (ms.find(now) != ms.end()) now++;           col[j] = now;           now++;         }       }       for (auto v : G[u]) {         if (!vis[v]) {           vis[v] = true;           Q.push(v);         }       }       ms.clear();     }     printf( %d n , colors);     for (int i = 0; i < m; i++) {       if (col[i] == 0) col[i] = 1;       printf( %d  , col[i]);     }     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     cout << s;     reverse(s.begin(), s.end());     cout << s;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline T Max(T a, T b) {     return a > b ? a : b;   }   template <typename T>   inline T Min(T a, T b) {     return a < b ? a : b;   }   template <typename T>   inline void checkMax(T &a, T b) {     if (a < b) a = b;   }   template <typename T>   inline void checkMin(T &a, T b) {     if (a > b) a = b;   }   template <typename T>   inline T Abs(T a) {     return a > 0 ? a : -a;   }   int a;   string s;   int num[4005];   int SumR[4005];   int Cnt[50000];   long long ans;   void work() {     cin >> s;     int len = s.length();     for (int i = 0; i < len; i++) {       num[i] = s[i] -  0 ;     }     for (int i = 0; i < len; i++) {       if (i == 0)         SumR[i] = num[i];       else         SumR[i] = SumR[i - 1] + num[i];     }     memset(Cnt, 0, sizeof(Cnt));     for (int i = 0; i < len; i++) {       for (int j = i; j < len; j++) {         int tmp = SumR[j];         if (i != 0) tmp -= SumR[i - 1];         Cnt[tmp]++;       }     }     ans = 0;     for (int i = 0; i < len; i++) {       for (int j = i; j < len; j++) {         int tmp = SumR[j];         if (i != 0) tmp -= SumR[i - 1];         if (tmp != 0) {           if (a % tmp == 0) {             long long div = a / tmp;             if (div <= SumR[len - 1]) {               ans += Cnt[div];             }           }         } else {           if (a == 0) {             ans += len * (len + 1) / 2;           }         }       }     }     cout << ans << endl;   }   int main() {     while (cin >> a) {       work();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n, k;       cin >> n >> k;       string s;       cin >> s;       sort(s.begin(), s.end());       map<char, int> store;       for (int i = 0; i < n; i++) {         store[s[i]]++;       }       if (store.size() == 1) {         int temp = n / k;         if (n % k != 0) temp++;         for (int i = 0; i < temp; i++) cout << s[0];       } else if (store.size() == 2 && store[s[0]] == k) {         cout << s[0];         int temp = n / k;         if (n % k != 0) temp++;         for (int i = 0; i < temp - 1; i++) cout << s[k];       } else {         if (s[0] != s[k - 1])           cout << s[k - 1];         else {           for (int i = k - 1; i < n; i++) cout << s[i];         }       }       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   set<int> v;   multiset<int> diff;   void clean() {     if (v.size() < 2) {       cout << 0 << endl;       return;     }     cout << *v.rbegin() - *v.begin() - *diff.rbegin() << endl;   }   int main() {     v.clear();     diff.clear();     int n, q;     cin >> n >> q;     for (int i = 0; i < n; i++) {       int x;       cin >> x;       v.insert(x);     }     for (auto it = v.begin(); next(it) != v.end(); it++)       diff.insert(*next(it) - *it);     clean();     while (q--) {       int t, x;       cin >> t >> x;       if (t == 1) {         v.insert(x);         set<int>::iterator it = v.find(x);         if (it == v.begin())           diff.insert(*next(it) - *it);         else if (next(it) == v.end())           diff.insert(*it - *prev(it));         else {           diff.erase(diff.find(*next(it) - *prev(it)));           diff.insert(*next(it) - *it);           diff.insert(*it - *prev(it));         }       } else {         set<int>::iterator it = v.find(x);         if (v.size() >= 2) {           if (it == v.begin())             diff.erase(diff.find(*next(it) - *it));           else if (next(it) == v.end())             diff.erase(diff.find(*it - *prev(it)));           else {             diff.erase(diff.find(*it - *prev(it)));             diff.erase(diff.find(*next(it) - *it));             diff.insert(*next(it) - *prev(it));           }         }         v.erase(it);       }       clean();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int const oo = 1e9, bound = 1e6, mod = oo + 7;   long long const OO = 1e18;   int n, m, k, aa[20];   int cc[20][20];   long long dp[18][1 << 18];   long long rec(int idx, int msk) {     long long &ret = dp[idx][msk];     if (ret != -1) return ret;     int cop = msk, e = 0;     while (cop) {       if (cop & 1) e++;       cop >>= 1;     }     if (e == m) return ret = 0;     for (int(i) = 0; (i) < (n); (i)++) {       if (!((msk >> i) & 1))         ret = max(ret,                   rec(i, (1 << i) | msk) + aa[i] + (msk != 0 ? cc[idx][i] : 0));     }     return ret;   }   int main() {     memset(dp, -1, sizeof dp);     scanf( %d , &n), scanf( %d , &m), scanf( %d , &k);     for (int(i) = 0; (i) < (n); (i)++) scanf( %d , &aa[i]);     for (int(i) = 0; (i) < (k); (i)++) {       int a, b, c;       scanf( %d , &a), scanf( %d , &b), scanf( %d , &c);       a--, b--;       cc[a][b] = c;     }     long long mx = rec(0, 0);     printf( %lld n , mx);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int INF = 98769876987698889LL;   const int MOD = 1e9 + 7;   const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1};   const int dy8[] = {0, 1, 1, 1, 0, -1, -1, -1};   const int dx4[] = {1, -1, 0, 0};   const int dy4[] = {0, 0, 1, -1};   const int N = 2e5 + 5;   long long int dp1[N], a[N], f[N], dp2[N], s[N];   int n;   vector<int> G[N];   void dfs(int x, int pa) {     if (G[x].size() == 1 and G[x][0] == pa) {       s[x] = a[x];       return;     }     s[x] = a[x];     for (auto k : G[x]) {       if (k == pa) continue;       dfs(k, x);       s[x] += s[k];     }   }   void dfs2(int x, int pa) {     if (G[x].size() == 1 and G[x][0] == pa) {       f[x] = 0;       return;     }     f[x] = 0;     vector<long long int> v;     for (auto k : G[x]) {       if (k == pa) continue;       dfs2(k, x);       if (s[k] >= dp1[k]) {         v.push_back(s[k]);       } else {         v.push_back(dp1[k]);       }     }     sort(v.begin(), v.end());     dp1[x] = v[v.size() - 1];     if (v.size() > 1) {       f[x] = 1;       dp2[x] = v[v.size() - 2];     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 0; i < N; i++) {       dp1[i] = -INF;       dp2[i] = -INF;     }     for (int i = 2; i <= n; i++) {       int x, y;       cin >> x >> y;       G[x].push_back(y);       G[y].push_back(x);     }     if (n == 1) {       cout <<  Impossible  << endl;       return 0;     }     dfs(1, 0);     dfs2(1, 0);     int fl = 0;     for (int i = 1; i <= n; i++) {       if (f[i]) fl = 1;     }     if (!fl) {       cout <<  Impossible  << endl;       return 0;     }     long long int ans = -INF;     for (int i = 1; i <= n; i++) {       if (f[i]) {         ans = max(ans, dp1[i] + dp2[i]);       }     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     for (int i = 0; i < t; ++i) {       int n, m, h;       cin >> n >> m;       long long int sum = 0;       for (int j = 0; j < n; ++j) {         int x;         cin >> x;         if (j != 0) sum = sum + x;         if (j == 0) h = x;       }       if (m - h < sum)         h = m;       else         h = h + sum;       cout << h <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   long long n, k, p;   string s;   vector<long long> mem;   long long solve(int i) {     if (mem[i] != -1) return mem[i];     if (i == 0) return 0;     long long tmp = stoll(s.substr(i - 1, 1));     long long res = 1ll << 62;     for (int j = i - 1; tmp < n; --j) {       if (s[j] !=  0  || j == i - 1) {         long long tmptmp = solve(j);         if (tmptmp < ((1ll << 62) - tmp) / n && tmptmp > -1)           res = min(res, tmp + n * tmptmp);       }       if (j - 1 < 0) break;       tmp = stoll(s.substr(j - 1, i - j + 1));     }     mem[i] = res;     return res;   }   int main(int argc, char const *argv[]) {     cin >> n;     cin >> s;     k = s.length();     if (n < 11) {       long long res = 0;       for (int i = 0; i < k; ++i) {         res *= n;         res += stoi(s.substr(i, 1));       }       cout << res << endl;       return 0;     }     mem = vector<long long>(k + 1, -1);     cout << solve(k) << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC target( avx,avx2,fma )   #pragma GCC optimization( unroll-loops )   using namespace std;   const int MX = 2000006;   char a[MX], b[MX];   char arb[MX], abr[MX], ba[MX];   int parb[MX], zabr[MX], zba[MX], pos[MX];   void compute_pi(int n, char s[], int pi[]) {     pi[0] = 0;     for (int i = 1; i < n; i++) {       int j = pi[i - 1];       while (j) {         if (s[j] == s[i]) {           pi[i] = j + 1;           break;         }         j = pi[j - 1];       }       if (j == 0) pi[i] = (s[i] == s[0]) ? 1 : 0;     }   }   void compute_z(int n, char s[], int z[]) {     int l = 0, r = 0;     for (int i = 1; i < n; i++) {       if (i <= r) z[i] = min(z[i - l], r - i + 1);       for (int j = i + z[i], p = z[i]; j < n; j++, p++) {         if (s[p] == s[j])           z[i]++;         else           break;       }       if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;     }   }   void calc(int n, int m) {     compute_pi(n + m + 1, arb, parb);     compute_z(n + m + 1, abr, zabr);     compute_z(n + m + 1, ba, zba);     memset(pos, -1, sizeof(pos));     for (int i = n + 1, p = 0; i <= n + m; i++, p++) {       if (parb[i] < 1) continue;       int j = p - parb[i] + 1;       if (pos[p] == -1) pos[p] = j;     }   }   void make(int n, int m) {     char sep[] =    ;     sep[0] = 31;     strcpy(ba, b);     strcat(ba, sep);     strcat(ba, a);     strcpy(arb, a);     reverse(arb, arb + n);     strcat(arb, sep);     strcat(arb, b);     strcpy(abr, a);     reverse(b, b + m);     strcat(abr, sep);     strcat(abr, b);   }   int main() {     gets(a), gets(b);     int n = strlen(a), m = strlen(b);     make(n, m);     calc(n, m);     int x = -1, y = -1;     for (int i = m + 2; (i <= n + m && n == m); i++) {       int p = pos[n + m - i];       if (p < 0 || p > zba[i]) continue;       if (zabr[n + 1] >= i - m - 1) x = i - m - 2, y = i - m - 1 + p;     }     printf( %d %d n , x, y);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC target( avx2 )   #pragma GCC optimization( O3 )   #pragma GCC optimization( unroll-loops )   using namespace std;   inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }   inline int msbp(int x) { return 31 - __builtin_clz(x); }   inline int msb(int x) { return 1 << msbp(x); }   const int INF = 0x3f3f3f3f;   struct hash_pair {     template <class T1, class T2>     size_t operator()(const pair<T1, T2>& p) const {       auto hash1 = hash<T1>{}(p.first);       auto hash2 = hash<T2>{}(p.second);       return hash1 ^ hash2;     }   };   const int N = 1e6 + 10;   int parent[N], rnk[N];   int find(int x) { return parent[x] = (x == parent[x] ? x : find(parent[x])); }   bool disjoint(int x, int y) { return find(x) != find(y); }   void join(int x, int y) {     if (!disjoint(x, y)) return;     int xPar = find(x), yPar = find(y);     int newPar, newChild;     if (rnk[xPar] > rnk[yPar]) {       newPar = xPar, newChild = yPar;     } else if (rnk[xPar] < rnk[yPar]) {       newPar = yPar, newChild = xPar;     } else {       newPar = xPar, newChild = yPar;       ++rnk[xPar];     }     parent[newChild] = newPar;   }   int n, m, b[N];   pair<int, int> a[N];   vector<pair<int, int>> all_vals;   map<int, vector<int>> s;   vector<int> g[N];   int d[N], dp[N];   void process_eq(vector<pair<int, int>>& cur) {     map<int, int> val_map;     for (auto [val, id] : cur) {       if (val_map.count(val)) {         join(id, val_map[val]);       } else {         val_map[val] = id;       }     }   }   void process(vector<int>& cur) {     sort(cur.begin(), cur.end());     for (int i = 0; i < (cur.size() - 1); ++i) {       if (cur[i] != cur[i + 1]) {         g[cur[i]].push_back(cur[i + 1]);         ++d[cur[i + 1]];       }     }   }   signed main() {     ios::sync_with_stdio(false);     std::iota(parent, parent + N, 0);     scanf( %d %d , &n, &m);     for (int i = 0; i < (n); ++i) {       for (int j = 0; j < (m); ++j) {         scanf( %d , &a[i * m + j].first);         s[a[i * m + j].first].push_back(i * m + j);       }     }     vector<pair<int, int>> curm(m), curn(n);     for (int i = 0; i < (n); ++i) {       for (int j = 0; j < (m); ++j) curm[j] = {a[i * m + j].first, i * m + j};       process_eq(curm);     }     for (int j = 0; j < (m); ++j) {       for (int i = 0; i < (n); ++i) curn[i] = {a[i * m + j].first, i * m + j};       process_eq(curn);     }     for (auto [val, ids] : s) {       int cur = 1;       for (int id : ids) {         if (a[find(id)].second == 0) {           all_vals.push_back({val, cur});           a[find(id)].second = cur++;         }         a[id].second = a[find(id)].second;       }     }     sort(all_vals.begin(), all_vals.end());     map<pair<int, int>, int> id_map;     for (int i = 0; i < (all_vals.size()); ++i) id_map[all_vals[i]] = i;     for (int i = 0; i < (n); ++i)       for (int j = 0; j < (m); ++j) b[i * m + j] = id_map[a[i * m + j]];     vector<int> ccurm(m), ccurn(n);     for (int i = 0; i < (n); ++i) {       for (int j = 0; j < (m); ++j) ccurm[j] = b[i * m + j];       process(ccurm);     }     for (int j = 0; j < (m); ++j) {       for (int i = 0; i < (n); ++i) ccurn[i] = b[i * m + j];       process(ccurn);     }     queue<int> nxt;     for (int x = 0; x < (all_vals.size()); ++x) {       if (d[x] == 0) {         nxt.push(x);         dp[x] = 1;       }     }     while (!nxt.empty()) {       int cur = nxt.front();       nxt.pop();       for (auto v : g[cur]) {         dp[v] = max(dp[v], dp[cur] + 1);         --d[v];         if (d[v] == 0) {           nxt.push(v);         }       }     }     for (int i = 0; i < (n); ++i) {       for (int j = 0; j < (m); ++j) cout << dp[b[i * m + j]] <<    ;       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:64777216 )   using namespace std;   const long long maxn = 100001;   const long long maxk = 200001;   const long long maxx = 10001;   const long long secret_number = 87468267;   const long long inf = 1e18;   const int iinf = 2147483647 / 2;   const double pi = 3.141592653589793238462643383279502884;   const double eps = 1e-7;   const string something_very_long =        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa         aaaaaaaa ;   const string govno =  g ;   const int maxp = 101;   int n, p, q;   int pr[maxp];   int main() {     cin.sync_with_stdio(0);     cin >> n >> p >> q;     string s;     cin >> s;     for (int i = 0; i < maxp; ++i) pr[i] = -2;     pr[0] = 0;     queue<int> qq;     qq.push(0);     while (!qq.empty()) {       int cur = qq.front();       qq.pop();       if ((cur + p <= s.length()) && (pr[cur + p] == -2))         pr[cur + p] = cur, qq.push(cur + p);       if ((cur + q <= s.length()) && (pr[cur + q] == -2))         pr[cur + q] = cur, qq.push(cur + q);     }     stack<string> v;     int cur = s.length();     if (pr[cur] == -2) {       cout << -1;       return 0;     }     while (true) {       if (cur != 0) {         v.push(s.substr(pr[cur], cur - pr[cur]));         cur = pr[cur];       } else         break;     }     cout << v.size() <<   n ;     while (!v.empty()) {       string aa = v.top();       v.pop();       cout << aa <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   struct p {     ll v;     int i;   } pp[3];   bool cmp(p x, p y) { return x.v < y.v; }   void dd() {     sort(pp, pp + 3, cmp);     ll y = 2 * pp[2].v - pp[0].v - pp[1].v;     cout << y << endl;     cout.flush();     int x;     cin >> x;     if (x == 0) {       return;     } else {       if (pp[0].i == x) {         cout << pp[2].v - pp[1].v << endl;         cout.flush();         int g;         cin >> g;         return;       } else if (pp[1].i == x) {         cout << pp[2].v - pp[0].v << endl;         cout.flush();         int g;         cin >> g;       } else {         pp[2].v += y;         dd();       }     }   }   int main() {     ll a, b, c;     cin >> a >> b >> c;     pp[0].v = a;     pp[0].i = 1;     pp[1].v = b;     pp[1].i = 2;     pp[2].v = c;     pp[2].i = 3;     cout <<  First  << endl;     cout.flush();     dd();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     scanf( %d , &n);     vector<vector<int>> g(n);     for (int i = 0; (i) < (int)(n - 1); (i)++) {       int a, b;       scanf( %d%d , &a, &b);       a--, b--;       g[a].push_back(b);       g[b].push_back(a);     }     int ans = 0;     for (int s = 0; (s) < (int)(n); (s)++) {       for (int t = s + 1; t < n; t++) {         vector<int> tmp, path;         function<void(int, int)> dfs = [&](int u, int prev) {           if (u == t) {             path = tmp;             return;           }           for (auto v : g[u])             if (v != prev) {               tmp.push_back(v);               dfs(v, u);               tmp.pop_back();             }         };         tmp.push_back(s);         dfs(s, -1);         vector<bool> ng(n);         for (int p : path) ng[p] = true;         int len = (int)path.size() - 1;         vector<int> dp1(n), dp2(n);         function<void(int, int)> dfs2 = [&](int u, int prev) {           vector<int> pro;           for (auto v : g[u])             if (v != prev) {               dfs2(v, u);               dp1[u] = max(dp1[u], dp1[v]);               dp1[u] = max(dp1[u], dp2[v]);               if (!ng[v]) pro.push_back(dp2[v]);             }           if (pro.size() == 0 || ng[u]) return;           sort(pro.rbegin(), pro.rend());           dp2[u] = pro[0] + 1;           if (pro.size() > 1) {             dp1[u] = max(dp1[u], pro[0] + pro[1] + 2);           }         };         dfs2(s, -1);         ans = max(ans, len * dp1[s]);       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   const double pi = acos(-1.0);   int main() {     int n;     double s, f, r, v, t;     scanf( %d %lf %lf , &n, &r, &v);     double p = 2 * pi * r;     double tp = p / v;     while (n--) {       scanf( %lf %lf , &s, &f);       double x = f - s;       double cyc = floor(x / p);       double rem = (x - cyc * p) / r / 2;       double lo = 0, hi = pi;       for (int i = 0; i < 60; ++i) {         double mid = (lo + hi) / 2;         if (mid + sin(mid) < rem)           lo = mid;         else           hi = mid;       }       t = cyc * tp + 2 * lo * r / v;       printf( %.8lf n , t);     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10;   const int K = 18;   long long a[N], tree[N << 2], lazy[N << 2];   vector<int> v[N];   int anc[N][K], h[N], sz[N];   int c[N], pos[N];   int ct;   void build(int node, int st, int end) {     if (st == end) {       tree[node] = c[st];       return;     }     long long mid = st + end >> 1LL;     build(node << 1, st, mid);     build((node << 1) + 1, mid + 1, end);     tree[node] = tree[node << 1] + tree[(node << 1) + 1];   }   void update(int node, int st, int end, int l, int r, long long val) {     if (lazy[node]) {       tree[node] += (end - st + 1) * lazy[node];       if (st != end) {         lazy[node << 1] += lazy[node];         lazy[(node << 1) + 1] += lazy[node];       }       lazy[node] = 0;     }     if (st > end || r < st || end < l) return;     if (l <= st && end <= r) {       tree[node] += (end - st + 1) * val;       if (st != end) {         lazy[node << 1] += val;         lazy[(node << 1) + 1] += val;       }       return;     }     long long mid = st + end >> 1LL;     update(node << 1, st, mid, l, r, val);     update((node << 1) + 1, mid + 1, end, l, r, val);     tree[node] = tree[node << 1] + tree[(node << 1) + 1];   }   long long query(int node, int st, int end, int l, int r) {     if (st > end || r < st || end < l) return 0;     if (lazy[node]) {       tree[node] += (end - st + 1) * lazy[node];       if (st != end) {         lazy[node << 1] += lazy[node];         lazy[(node << 1) + 1] += lazy[node];       }       lazy[node] = 0;     }     if (l <= st && end <= r) return tree[node];     long long mid = st + end >> 1LL;     return (query(node << 1, st, mid, l, r) +             query((node << 1) + 1, mid + 1, end, l, r));   }   void dfs(int s, int P) {     c[ct + 1] = a[s];     pos[s] = ++ct;     anc[s][0] = P;     h[s] = h[P] + 1;     sz[s]++;     for (int i = 1; i < K; i++) anc[s][i] = anc[anc[s][i - 1]][i - 1];     for (auto i : v[s])       if (i != P) dfs(i, s), sz[s] += sz[i];   }   int LCA(int x, int y) {     int d = h[y] - h[x];     if (d < 0) swap(x, y);     d = abs(d);     for (int i = K - 1; i >= 0; i--)       if ((1 << i) & d) y = anc[y][i];     if (x == y) return x;     for (int i = K - 1; i >= 0; i--)       if (anc[x][i] != anc[y][i]) x = anc[x][i], y = anc[y][i];     return anc[x][0];   }   int lift(int x, int d) {     for (int i = K - 1; i >= 0; i--)       if ((1 << i) & d) x = anc[x][i];     return x;   }   int main() {     ios_base ::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int n, q;     cin >> n >> q;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i < n; i++) {       int x, y;       cin >> x >> y;       v[x].push_back(y);       v[y].push_back(x);     }     dfs(1, 0);     build(1, 1, n);     int t, val, l, r, x, ro = 1, yo, vo, go;     long long ho;     for (int i = 0; i < q; i++) {       cin >> t;       if (t == 1)         cin >> ro;       else if (t == 2) {         cin >> l >> r >> x;         yo = LCA(l, r), vo = LCA(l, ro), go = LCA(r, ro);         if (h[yo] < h[vo]) yo = vo;         if (h[yo] < h[go]) yo = go;         if (yo == ro) {           update(1, 1, n, 1, n, x);         } else if (LCA(yo, ro) == yo) {           update(1, 1, n, 1, n, x);           yo = lift(ro, h[ro] - h[yo] - 1);           update(1, 1, n, pos[yo], pos[yo] + sz[yo] - 1, -x);         } else {           update(1, 1, n, pos[yo], pos[yo] + sz[yo] - 1, x);         }       } else {         cin >> l;         if (l == ro) {           cout << query(1, 1, n, 1, n) <<   n ;         } else if (LCA(ro, l) == l) {           ho = query(1, 1, n, 1, n);           yo = lift(ro, h[ro] - h[l] - 1);           cout << ho - query(1, 1, n, pos[yo], pos[yo] + sz[yo] - 1) <<   n ;         } else {           cout << query(1, 1, n, pos[l], pos[l] + sz[l] - 1) <<   n ;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 50;   const int LOG = 19;   const int INF = 1e9;   int m, p[MAXN], par[12][MAXN], nxt[MAXN], id[MAXN], ss[MAXN], ans, l[MAXN],       r[MAXN];   int tt[LOG][MAXN], lc[LOG][MAXN];   string s[14];   pair<pair<int, int>, int> vec[MAXN];   int lcp(int a, int b) {     int ta = a, tb = b;     for (int i = LOG - 1; i >= 0 && a < nxt[ta] && b < nxt[tb]; i--)       if (a + (1 << i) <= nxt[ta] && b + (1 << i) <= nxt[tb] &&           tt[i][a] == tt[i][b])         a += 1 << i, b += 1 << i;     return a - ta;   }   bool ok(int sz, int ind) {     int tl = ind, tr = ind;     for (int w = LOG - 1; w >= 0; w--)       if (tr + (1 << w) < p[m] && lc[w][tr] >= sz) tr += 1 << w;     tr++;     for (int w = LOG - 1; w >= 0; w--)       if (tl - (1 << w) >= 0 && lc[w][tl - (1 << w)] >= sz) tl -= (1 << w);     for (int i = 1; i < m; i++)       if (par[i][tr] - par[i][tl] > r[i]) return false;     return true;   }   bool ok2(int sz, int ind) {     int tl = ind, tr = ind;     for (int w = LOG - 1; w >= 0; w--)       if (tr + (1 << w) < p[m] && lc[w][tr] >= sz) tr += 1 << w;     tr++;     for (int w = LOG - 1; w >= 0; w--)       if (tl - (1 << w) >= 0 && lc[w][tl - (1 << w)] >= sz) tl -= (1 << w);     for (int i = 1; i < m; i++)       if (par[i][tr] - par[i][tl] < l[i]) return false;     return true;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> s[0] >> m;     m++;     for (int i = 1; i < m; i++)       cin >> s[i] >> l[i] >> r[i], p[i] = p[i - 1] + (int)s[i - 1].size();     p[m] = p[m - 1] + (int)s[m - 1].size();     for (int i = 0; i < m; i++)       for (int j = 0; j < (int)s[i].size(); j++) tt[0][p[i] + j] = s[i][j];     for (int w = 1; w < LOG; w++) {       for (int i = 0; i < m; i++)         for (int j = 0; j < (int)s[i].size(); j++)           if (j + (1 << (w - 1)) >= (int)s[i].size())             vec[p[i] + j] = {{tt[w - 1][p[i] + j], -1}, p[i] + j};           else             vec[p[i] + j] = {                 {tt[w - 1][p[i] + j], tt[w - 1][p[i] + j + (1 << (w - 1))]},                 p[i] + j};       sort(vec, vec + p[m]);       for (int i = 1; i < p[m]; i++)         if (vec[i].first == vec[i - 1].first)           tt[w][vec[i].second] = tt[w][vec[i - 1].second];         else           tt[w][vec[i].second] = i;     }     for (int i = 0; i < p[m]; i++) ss[i] = vec[i].second;     for (int i = 0; i < m; i++)       for (int j = 0; j < (int)s[i].size(); j++)         id[p[i] + j] = i, nxt[p[i] + j] = p[i + 1];     for (int i = 0; i < p[m]; i++)       for (int j = 1; j < m; j++)         par[j][i + 1] = par[j][i] + bool(id[ss[i]] == j);     for (int i = 0; i < p[m] - 1; i++) lc[0][i] = lcp(ss[i], ss[i + 1]);     for (int i = 1; i < LOG; i++)       for (int j = 0; j < p[m] - 1; j++)         if (j + (1 << (i - 1)) >= p[m] - 1)           lc[i][j] = lc[i - 1][j];         else           lc[i][j] = min(lc[i - 1][j], lc[i - 1][j + (1 << (i - 1))]);     int mn = 0;     for (int i = 0; i < p[m]; i++) {       if (id[ss[i]]) {         mn = min(mn, lc[0][i]);         continue;       }       int sz = nxt[ss[i]] - ss[i];       int cur = ss[i];       for (int w = LOG - 1; w >= 0 && cur < nxt[ss[i]]; w--)         if (cur + (1 << w) <= nxt[ss[i]] && !ok(cur + (1 << w) - ss[i], i))           cur += (1 << w);       int cur2 = ss[i];       for (int w = LOG - 1; w >= 0 && cur2 < nxt[ss[i]]; w--)         if (cur2 + (1 << w) <= nxt[ss[i]] && ok2(cur2 + (1 << w) - ss[i], i))           cur2 += (1 << w);       cur = max(cur, ss[i] + mn);       ans += max(0, cur2 - cur);       mn = lc[0][i];     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, arr[110], c1, c0;   int main() {     scanf( %d , &n);     for (int i = 0; i < n; ++i) scanf( %d , &arr[i]), c1 += arr[i];     int out = 0;     for (int i = 0; i < n; ++i) {       c0 += (!arr[i]);       out = max(out, c0 + c1);       c1 -= arr[i];     }     cout << out;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int a;     int b;     int index;   };   bool compare(node &n1, node &n2) {     if (n1.a < n2.a)       return true;     else       return false;   }   bool compare1(node &n1, node &n2) {     if (n1.b < n2.b)       return true;     else       return false;   }   int main() {     int n;     cin >> n;     vector<node> pq(n);     for (int i = 0; i < n; i++) {       cin >> pq[i].a >> pq[i].b;       pq[i].index = i;     }     int num = 0;     for (int i = 0; i < n; i++) {       if (pq[i].a < pq[i].b) num++;     }     vector<node> track;     if (num >= n / 2) {       for (int i = 0; i < n; i++) {         if (pq[i].a < pq[i].b) track.push_back(pq[i]);       }       sort(track.begin(), track.end(), compare);       cout << track.size() << endl;       for (int i = track.size() - 1; i >= 0; i--) {         cout << track[i].index + 1 <<    ;       }     } else {       for (int i = 0; i < n; i++) {         if (pq[i].a > pq[i].b) track.push_back(pq[i]);       }       sort(track.begin(), track.end(), compare1);       cout << track.size() << endl;       for (int i = 0; i < track.size(); i++) {         cout << track[i].index + 1 <<    ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int hy, ay, dy;     int hm, am, dm;     int h, a, d;     cin >> hy >> ay >> dy;     cin >> hm >> am >> dm;     cin >> h >> a >> d;     int res = 1e9;     for (int atk = dm + 1; atk <= 200; ++atk) {       for (int def = dy; def <= 100; ++def) {         int rnd = (hm + atk - dm - 1) / (atk - dm);         int h_add = max(0, rnd * max(0, am - def) - hy + 1);         int a_add = max(0, atk - ay);         int d_add = max(0, def - dy);         res = min(res, h_add * h + a_add * a + d_add * d);       }     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N, br[20][200200], d[20][200200], lev[200200];   vector<int> in, adj[200200], myc[200200], ans;   bool dead[200200];   vector<multiset<int>> m[200200];   multiset<int> sub, best[200200], outofall;   int go(int l, int i, int p = -1) {     in.push_back(i);     for (int j : adj[i])       if (j != p && !dead[j]) {         d[l][i] += go(l, j, i);       }     return ++d[l][i];   }   void dfs(int l, int i, int p) {     for (int j : adj[i])       if (j != p && !dead[j]) {         br[l][j] = br[l][i];         d[l][j] = d[l][i] + 1;         dfs(l, j, i);       }   }   void rec(int l, int i) {     in.clear(), go(l, i);     int C = -1;     for (int k : in) {       int mx = d[l][i] - d[l][k];       for (int j : adj[k])         if (d[l][j] < d[l][k]) {           mx = max(mx, d[l][j]);         }       if (mx <= d[l][i] / 2) {         C = k;         break;       }     }     lev[C] = l;     for (int k : in) {       myc[k].push_back(C);     }     for (int k : in) {       d[l][k] = 0;     }     for (int j : adj[C])       if (!dead[j]) {         sub.clear(), d[l][j] = 1;         br[l][j] = (int)m[C].size();         dfs(l, j, C), m[C].push_back(sub);       }     dead[C] = 1;     for (int j : adj[C])       if (!dead[j]) {         rec(l + 1, j);       }   }   int main() {     scanf( %d , &N);     for (int i = (2); i < (N + 1); i++) {       int p;       scanf( %d , &p);       adj[i].push_back(p), adj[p].push_back(i);     }     rec(0, 1);     for (int i = (1); i < (N + 1); i++) {       for (int c : myc[i]) {         if ((int)best[c].size() >= 2) {           auto it = --best[c].end();           int t = *it + *prev(it);           if (outofall.find(t) != outofall.end()) {             outofall.erase(outofall.find(t));           }         }         if (c == i) {           best[c].insert(0);         } else {           int myb = br[lev[c]][i], myd = d[lev[c]][i];           if (m[c][myb].size() &&               best[c].find(*m[c][myb].rbegin()) != best[c].end()) {             best[c].erase(best[c].find(*m[c][myb].rbegin()));           }           m[c][myb].insert(myd);           while ((int)m[c][myb].size() > 2) m[c][myb].erase(m[c][myb].begin());           best[c].insert(*m[c][myb].rbegin());           while ((int)best[c].size() > 2) best[c].erase(best[c].begin());         }         if ((int)best[c].size() >= 2) {           auto it = --best[c].end();           outofall.insert(*it + *prev(it));           while ((int)outofall.size() > 2) outofall.erase(outofall.begin());         }       }       if (i >= 2) printf( %d  , *outofall.rbegin());     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<pair<long long, long long>, long long> m;   int main() {     long long t, ai, bi, dr = 1;     cin >> t;     vector<pair<long long, long long>> v(t);     for (int i = 0; i < t; i++) {       cin >> ai >> bi;       v[i] = {ai, bi};     }     m[{0, 0}] = 1;     dr += min(v[0].first, v[0].second);     m[{v[0].first, v[0].second}] = 1;     for (int i = 1; i < t; i++) {       if (v[i].first > v[i - 1].first && v[i].second > v[i - 1].second &&           m[{v[i].first, v[i].second}] == 0 &&           min(v[i].first, v[i].second) >= max(v[i - 1].second, v[i - 1].first)) {         if ((v[i - 1].first != 0 || v[i - 1].second != 0) &&             v[i - 1].first != v[i - 1].second) {           dr += min(v[i].first, v[i].second) -                 max(v[i - 1].first, v[i - 1].second) + 1;         } else {           dr +=               min(v[i].first, v[i].second) - max(v[i - 1].first, v[i - 1].second);         }         m[{v[i].first, v[i].second}] = 1;       } else if ((v[i].first > v[i].second && v[i].second == v[i - 1].second &&                   v[i - 1].first < v[i - 1].second) ||                  (v[i].second > v[i].first && v[i].first == v[i - 1].first &&                   v[i - 1].second < v[i - 1].first) &&                      m[{v[i].first, v[i].second}] == 0) {         dr++;         m[{v[i].first, v[i].second}] = 1;       } else if (v[i].first == v[i].second && m[{v[i].first, v[i].second}] == 0) {         dr++;         m[{v[i].first, v[i].second}] = 1;       }     }     cout << dr;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline T qmin(const T a, const T b) {     return a < b ? a : b;   }   template <typename T>   inline T qmax(const T a, const T b) {     return a > b ? a : b;   }   template <typename T>   inline void getmin(T &a, const T b) {     if (a > b) a = b;   }   template <typename T>   inline void getmax(T &a, const T b) {     if (a < b) a = b;   }   inline void fileio(string s) {     freopen((s +  .in ).c_str(),  r , stdin);     freopen((s +  .out ).c_str(),  w , stdout);   }   const int inf = 1e9 + 7;   const long long linf = 1e17 + 7;   const int N = 2e5 + 7;   int q[N], st, ed;   int n, deg[N], s[N];   int tot, ans[N][2];   void dfs(int u) {     int v = s[u];     ++tot, ans[tot][0] = u, ans[tot][1] = v;     --deg[v], s[v] ^= u;     --deg[u], s[u] = 0;     if (deg[v] == 1) dfs(v);   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       scanf( %d%d , deg + i, s + i);     }     for (int i = 0; i < n; ++i) {       if (deg[i] == 1) dfs(i);     }     printf( %d n , tot);     for (int i = 1; i <= tot; ++i) printf( %d %d n , ans[i][0], ans[i][1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   const int MAXN = 1500907;   vector<vector<long long>> adj;   bool visited[MAXN];   long long n_nodes;   long long n_edges;   void dfs(int node) {     visited[node] = 1;     n_nodes++;     for (auto i : adj[node]) {       n_edges++;       if (!visited[i]) {         dfs(i);       }     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     long long n, m, a, b;     cin >> n >> m;     adj.resize(n + 5);     while (m--) {       cin >> a >> b;       a--;       b--;       adj[a].push_back(b);       adj[b].push_back(a);     }     for (long long i = 1; i <= n; i++) {       if (!visited[i]) {         n_nodes = 0;         n_edges = 0;         dfs(i);         if (n_edges != n_nodes * (n_nodes - 1)) {           cout <<  NO  << endl;           return 0;         }       }     }     cout <<  YES  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   set<long long> num;   void f(int x, int a, int b, long long now) {     if (x == 0) {       num.insert(now);       return;     }     if (a) f(x - 1, a - 1, b, now * 10 + 4);     if (b) f(x - 1, a, b - 1, now * 10 + 7);   }   void init() {     int i;     for (i = 1; i <= 5; i++) f(i * 2, i, i, 0LL);   }   int main() {     long long n;     init();     scanf( %I64d , &n);     printf( %I64d , *num.upper_bound(n - 1));   }


#include <bits/stdc++.h>   using namespace std;   int main(void) {     int n;     scanf( %d , &n);     long a[n];     long long s = 0, ss = 0;     for (int i = 0; i < n; i++) {       scanf( %ld , &a[i]);       s += a[i];     }     if (s % 3 != 0) {       printf( 0 n );       return 0;     }     s = s / 3;     vector<int> sum1;     vector<int> sum2;     for (int i = 0; i < n - 1; i++) {       ss += a[i];       if (ss == s) sum1.push_back(i);       if (ss == 2 * s) sum2.push_back(i);     }     long long ans = 0;     for (int i = 0; i < sum1.size(); i++) {       ans += sum2.end() - lower_bound(sum2.begin(), sum2.end(), sum1[i] + 1);     }     printf( %I64d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, i, j, a[110], b[110], a1, b1;     cin >> n >> m;     for (i = 0; i <= n; i++) cin >> a[i];     for (i = 0; i <= m; i++) cin >> b[i];     a1 = a[0], b1 = b[0];     if (n > m) {       if (a1 * b1 > 0)         cout <<  Infinity  << endl;       else         cout <<  -Infinity  << endl;     } else if (n < m) {       printf( 0/1 n );     } else if (n == m) {       for (i = 1; i <= min(abs(a1), abs(b1)); i++) {         if (a1 % i == 0 && b1 % i == 0) j = i;       }       a1 /= j, b1 /= j;       if (a1 * b1 > 0)         printf( %d/%d n , abs(a1), abs(b1));       else         printf( -%d/%d n , abs(a1), abs(b1));     }     return 0;   }