#include <bits/stdc++.h>   using namespace std;   void Solve() {     long long n;     cin >> n;     vector<int> arr(n);     for (int i = 0; i < n; i++) cin >> arr[i];     reverse(arr.begin(), arr.end());     for (int i = 0; i < n; i++) cout << arr[i] <<    ;     cout <<   n ;   }   int main() {     long long t;     cin >> t;     while (t--) Solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline char gc() {     static char buf[1 << 16], *S, *T;     if (S == T) {       T = (S = buf) + fread(buf, 1, 1 << 16, stdin);       if (T == S) return EOF;     }     return *S++;   }   inline int read() {     int x = 0, f = 1;     char ch = gc();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = gc();     }     while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = gc();     return x * f;   }   int n, w, mxb, mxa, bel[100010], nn, cnt[350], q[350][350], qh[350], qt[350];   long long d[100010];   struct data {     int a, b;     friend bool operator<(data a, data b) { return a.b < b.b; }   } A[100010];   inline double slope(int i, int j) { return (d[i] - d[j]) * 1.0 / (j - i); }   inline void gao(int i, int x) {     while (qh[i] < qt[i] && slope(q[i][qh[i]], q[i][qh[i] + 1]) <= x) ++qh[i];   }   inline void rebuild(int id, int l, int r) {     qh[id] = 1;     qt[id] = 0;     for (int i = l; i <= r; ++i) {       while (qh[id] < qt[id] &&              slope(q[id][qt[id]], i) < slope(q[id][qt[id] - 1], q[id][qt[id]]))         --qt[id];       q[id][++qt[id]] = i;     }     gao(id, 0);   }   inline void add(int x) {     if (!x) return;     for (int i = 1; i < bel[x]; ++i) gao(i, ++cnt[i]);     int l = (bel[x] - 1) * nn + 1, r = min(bel[x] * nn, mxa), id = bel[x];     for (int i = l; i <= r; ++i) d[i] += (long long)cnt[id] * i;     for (int i = l; i <= x; ++i) d[i] += i;     cnt[id] = 0;     rebuild(id, l, r);   }   int main() {     n = read();     w = read();     for (int i = 1; i <= n; ++i)       A[i].a = read(), A[i].b = read(), mxb = max(mxb, A[i].b),       mxa = max(mxa, A[i].a);     sort(A + 1, A + n + 1);     int now = 1;     nn = sqrt(mxa);     for (int i = 1; i <= mxa; ++i) bel[i] = (i - 1) / nn + 1;     int tot = bel[mxa];     for (int i = 1; i <= tot; ++i) qh[i] = 1, q[i][++qt[i]] = min(i * nn, mxa);     int flag = 0;     for (int c = 0; c <= mxb + 1; ++c) {       while (now <= n && A[now].b < c) add(A[now].a), ++now;       long long ans = 0;       int ans1 = 0;       for (int i = 1; i <= tot; ++i) {         int x = q[i][qh[i]];         if (d[x] + (long long)x * cnt[i] > ans)           ans = d[x] + (long long)x * cnt[i], ans1 = x;       }       printf( %I64d %d n , ans + (long long)w * c * (n - now + 1), ans1);       if (c == 1) flag = ans + (long long)w * c * (n - now + 1);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[2000][2000], k, p[2000], Index;   void add(int x, int y) { a[x][y] = a[y][x] = 1; }   int main() {     scanf( %d , &k);     int maxbit = 0;     for (int i = (1); i <= (30); ++i)       if ((1 << i) & k) maxbit = i;     Index = 2;     int cur = ++Index;     add(1, cur);     for (int i = (1); i <= (maxbit); ++i) {       add(cur, Index + 1);       add(cur, Index + 2);       add(Index + 1, Index + 3);       add(Index + 2, Index + 3);       cur = Index += 3;       p[maxbit - i] = cur;     }     add(cur, 2);     add(1, ++Index);     cur = Index;     for (int i = (maxbit - 1); i >= (0); --i) {       add(cur, ++Index), cur = Index;       if ((1 << i) & k) add(cur, p[i]);       add(cur, ++Index), cur = Index;     }     printf( %d n , Index);     for (int i = (1); i <= (Index); ++i) {       for (int j = (1); j <= (Index); ++j)         if (a[i][j])           printf( Y );         else           printf( N );       puts(  );     }     assert(Index <= 150);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   const int MAXN = 200100;   int win = 0, ti = 0;   int vis[MAXN][2] = {0};   int vs[MAXN] = {0};   int path[MAXN], cnt;   vector<int> g[MAXN];   bool dfsloop(int u) {     vs[u] = 1;     bool f = false;     for (int i = 0; i < g[u].size(); i++) {       int v = g[u][i];       if (vs[v] == 1)         f = true;       else if (vs[v] == 0)         f = dfsloop(v);       if (f) return true;     }     vs[u] = 2;     return false;   }   void dfs(int u, int ly) {     vis[u][ly % 2] = 1;     int sz = g[u].size();     cnt = ly;     path[ly] = u;     if (!sz) {       if (ly % 2 == 0) {         win = 1;         return;       } else         ;     }     for (int i = 0; i < sz; i++) {       int v = g[u][i];       if (vis[v][(ly + 1) % 2] == 0) {         dfs(v, ly + 1);         if (win) return;       }     }   }   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       int ci;       scanf( %d , &ci);       while (ci--) {         int v;         scanf( %d , &v);         g[i].push_back(v);       }     }     int s;     scanf( %d , &s);     dfs(s, 1);     if (win) {       printf( Win n );       for (int i = 1; i <= cnt; i++) printf( %d  , path[i]);     } else {       bool res = dfsloop(s);       if (res)         printf( Draw );       else         printf( Lose );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   set<int> s;   int t, n, a[200020], cnt, d[200020], e[200020];   int main() {     for (scanf( %d , &t); t--; s.clear(), cnt = 0) {       scanf( %d , &n);       for (int i = 0; i < n; i++) d[i] = e[i] = 0;       for (int i = 0; i < n; i++) scanf( %d , a + i);       for (int i = 0; i < n; i++) {         if (s.count(a[i])) break;         s.insert(a[i]);         while (s.count(cnt + 1)) cnt++;         if (s.size() == cnt) d[i] = 1;       }       s.clear(), cnt = 0;       for (int i = n - 1; i >= 0; i--) {         if (s.count(a[i])) break;         s.insert(a[i]);         while (s.count(cnt + 1)) cnt++;         if (s.size() == cnt) e[i] = 1;       }       vector<int> v;       for (int i = 0; i < n - 1; i++) {         if (d[i] && e[i + 1]) v.push_back(i + 1);       }       printf( %d n , v.size());       for (int x : v) printf( %d %d n , x, n - x);     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 2e5 + 10;   const double eps = 1e-6;   const double mod = 1e9 + 7;   struct num {     int v, i;   } b[MAX];   bool cmp(const num &x, const num &y) { return x.v > y.v; }   bool cmp1(const num &x, const num &y) { return x.i < y.i; }   int main() {     int m, a[MAX];     cin >> m;     for (int i = 1; i <= m; i++) cin >> a[i];     for (int i = 1; i <= m; i++) cin >> b[i].v, b[i].i = i;     sort(a + 1, a + m + 1);     sort(b + 1, b + m + 1, cmp);     for (int i = 1; i <= m; i++) b[i].v = a[i];     sort(b + 1, b + m + 1, cmp1);     for (int i = 1; i <= m; i++) cout << b[i].v <<    ;     putchar(  n );   }


#include <bits/stdc++.h>   int main() {     unsigned long long n;     unsigned long long answer;     scanf( %I64u , &n);     if (n < 3) {       printf( %lu , n);       return 0;     }     if (n % 2 == 1) {       answer = (n) * (n - 1) * (n - 2);     } else if (n % 3 != 0) {       answer = n * (n - 1) * (n - 3);     } else {       answer = (n - 1) * (n - 2) * (n - 3);     }     printf( %I64u n , answer);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int Int() {     int x;     scanf( %d , &x);     return x;   }   long long Long() {     long long x;     scanf( %lld , &x);     return x;   }   void err(istream_iterator<string> it) { cout << endl; }   template <typename T, typename... Args>   void err(istream_iterator<string> it, T a, Args... args) {     cerr << *it <<   =   << a <<    ;     err(++it, args...);   }   const int N = (int)2e5 + 5;   const int maxN = (int)1e6 + 6;   const long long Mod = (long long)1e9 + 7;   const int inf = (int)2e9;   const long long Inf = (long long)1e18;   long long dp[20][5][5][11][11];   int n, k;   long long call(int pos, int prior, int last, int hump, int uhump) {     if (pos == n) {       return hump == k and uhump == k - 1;     }     long long &ret = dp[pos][prior][last][hump][uhump];     if (~ret) return ret;     ret = 0;     for (int i = 1; i <= 4; ++i) {       if (i == last) continue;       int f = hump;       int first = uhump;       if (prior and last) {         f += (prior < last and last > i);         first += (prior > last and last < i);       }       ret += call(pos + 1, last, i, f, first);     }     return ret;   }   int main() {     int test = 1, tc = 0;     while (test--) {       n = Int(), k = Int();       memset(dp, -1, sizeof dp);       long long res = 0;       for (int i = 1; i <= 4; ++i) {         res += call(1, 0, i, 0, 0);       }       cout << res <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int n, k;   int a[200005];   int main() {     int t;     scanf( %d , &t);     while (t--) {       scanf( %d%d , &n, &k);       int pos = 0, mn = 1000000000;       for (int i = 1; i <= n; i++) {         scanf( %d , &a[i]);         if (i > k && a[i] - a[i - k] < mn) {           mn = a[i] - a[i - k];           pos = i;         }       }       printf( %d n , (a[pos] + a[pos - k]) / 2);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int x;     cin >> x;     for (int a = 1; a <= x; ++a)       for (int b = 1; b <= x; ++b)         if (a % b == 0 && a * b > x && a / b < x) {           cout << a <<     << b << endl;           return 0;         }     puts( -1 );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, k, l, r, sz, i, len, q, p, mid, anspos, ans[1000001];   long long mxl, mxr;   long long pos[1000001], t[1000001], pr[100001];   struct Pair {     long long pos;     long long x;     long long d;   };   Pair a[100001], b[100001];   bool cmp(Pair p1, Pair p2) { return p1.x < p2.x; }   bool cmd(Pair p1, Pair p2) { return p1.pos < p2.pos; }   long long binl(long long L, long long R, long long height) {     long long otv = -1;     while (L <= R) {       mid = L + R;       mid /= 2;       if (b[mid].x > height)         R = mid - 1;       else         otv = mid, L = mid + 1;     }     return otv;   }   long long binr(long long L, long long R, long long height) {     long long otv = -1;     while (L <= R) {       mid = L + R;       mid /= 2;       if (b[mid].x < height)         L = mid + 1;       else         otv = mid, R = mid - 1;     }     return otv;   }   long long tree(long long L, long long R) {     long long nn = 0;     while (L <= R) {       if (L % 2 == 1) nn = max(nn, t[L]);       if (R % 2 == 0) nn = max(nn, t[R]);       L = (L + 1) / 2;       R = (R - 1) / 2;     }     return nn;   }   void update(long long x) {     x /= 2;     while (x) {       t[x] = max(t[x * 2], t[x * 2 + 1]);       x /= 2;     }   }   int main() {     cin >> n >> k;     sz = 1;     while (sz < n) sz *= 2;     for (i = 1; i <= n; i++) {       cin >> a[i].x;       a[i].pos = i;       b[i] = a[i];     }     sort(b + 1, b + n + 1, cmp);     for (int i = 1; i <= n; i++) pos[b[i].pos] = i, pr[i] = b[i].pos;     for (int i = 1; i <= n; i++) {       int l = binl(1, pos[i] - 1, a[i].x - k);       int r = binr(pos[i] + 1, n, a[i].x + k);       int mxl = 0, mxr = 0;       if (l > 0) mxl = tree(sz, l + sz - 1);       if (r > 0) mxr = tree(r + sz - 1, n + sz - 1);       t[pos[i] + sz - 1] = max(mxl, mxr) + 1;       update(pos[i] + sz - 1);       a[pr[pos[i]]].d = t[pos[i] + sz - 1];     }     sort(b + 1, b + n + 1, cmd);     cout << t[1] << endl;     for (int i = 1; i <= n; i++) {       if (a[i].d == t[1]) {         p = i;         break;       }     }     ans[++q] = p;     for (int j = p; j >= 1; j--) {       if (a[j].d == a[p].d - 1 && abs(a[j].x - a[p].x) >= k) {         ans[++q] = j;         p = j;       }     }     for (int i = q; i >= 1; i--) cout << ans[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int helper(long long int edges, long long int nodes) {     if (edges == nodes - 1) return nodes - 1;     edges -= nodes - 1;     return nodes - 1 - ceil((1.0 + sqrt(1.0 + 8.0 * edges)) / 2.0);   }   int main() {     ios::sync_with_stdio(false);     ;     cin.tie(0);     ;     long long int i, j, q, n, lo, mid, hi;     cin >> q;     while (q--) {       cin >> n;       lo = n - 1;       hi = (n * (n - 1)) / 2 + 1;       while (hi - lo > 1) {         mid = (lo + hi) / 2;         if (helper(mid, n) >= (mid + 1) / 2)           lo = mid;         else           hi = mid;       }       cout << lo <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,                  less<pair<long long, long long>>>       q;   priority_queue<long long, vector<long long>, greater<long long>> p;   long long n, k, t, b, temp, res;   int main() {     cin >> n >> k;     for (int i = 0; i < n; i++) {       cin >> t >> b;       q.push({b, t});     }     for (int i = 0; i < n; i++) {       pair<long long, long long> tt = q.top();       if (p.size() < k) {         p.push(tt.second);         temp += tt.second;       } else {         if (tt.second > p.top()) {           temp += (tt.second - p.top());           p.pop();           p.push(tt.second);         }       }       q.pop();       res = max(res, tt.first * temp);     }     cout << res << endl;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   const long long linf = 1001002003004005006ll;   const int inf = 1001001001;   const int mod = 1000000007;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       string s;       cin >> s;       string ans1, ans2;       bool elf = false;       for (int i = 0; i < (n); ++i) {         if (elf) {           if (s[i] ==  2 ) ans1 +=  0 , ans2 +=  2 ;           if (s[i] ==  0 ) ans1 +=  0 , ans2 +=  0 ;           if (s[i] ==  1 ) ans1 +=  0 , ans2 +=  1 ;         } else {           if (s[i] ==  2 ) ans1 +=  1 , ans2 +=  1 ;           if (s[i] ==  0 ) ans1 +=  0 , ans2 +=  0 ;           if (s[i] ==  1 ) {             ans1 +=  1 , ans2 +=  0 ;             elf = true;           }         }       }       cout << ans1 << endl << ans2 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long rd() {     register long long x = 0;     register bool f = 0;     register char c = getchar();     while (!isdigit(c)) {       if (c ==  - ) f = 1;       c = getchar();     }     while (isdigit(c)) {       x = x * 10 + (c ^ 48);       c = getchar();     }     return f ? -x : x;   }   int n, a[200010], bit[31];   inline int insert(int x) {     for (register int i = 30; ~i; --i) {       if (x & (1 << i)) {         if (!bit[i]) {           bit[i] = x;           return 1;         }         x ^= bit[i];       }     }     return 0;   }   int main() {     n = rd();     for (register int i = 1; i <= n; ++i) a[i] = rd() ^ a[i - 1];     if (!a[n]) {       puts( -1 );       return 0;     }     int ans = insert(a[n]);     for (register int i = n - 1; i; --i) ans += insert(a[i]);     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   pair<int, int> Castle[100010];   int Index[100010];   int main() {     int N;     scanf( %d , &N);     int Temp;     for (int i = 1; i <= N; i++) {       scanf( %d , &Temp);       Castle[i] = make_pair(Temp, i);     }     sort(Castle + 1, Castle + N + 1);     int Max = 1;     int Ans = 0;     for (int i = 1; i <= N; i++) Index[Castle[i].second] = i;     for (int i = 1; i <= N; i++) {       Max = max(Max, Index[i]);       if (Max == i) Ans++;     }     printf( %d n , Ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int read() {     int num = 0;     bool f = 0;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       f = (ch ==  - );       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       num = (num << 1) + (num << 3) + ch -  0 ;       ch = getchar();     }     return f ? -num : num;   }   int main() {     int num[111];     int n = read(), cnt = 0, t = 0, ans = 0;     memset(num, 0, sizeof(num));     while (n) {       num[cnt++] = n % 2;       n >>= 1;     }     swap(num[0], num[4]);     swap(num[2], num[3]);     for (int i = 0; i <= 5; i++)       if (num[i]) ans += pow(2, i);     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC target( sse,sse2,sse3,sse3,sse4,popcnt,abm,mmx )   using namespace std;   const int MX = 1e9 + 1;   const int Imp = -1;   const int N = 1e6 + 5;   const int M = 1e6 + 5;   const long long INF = 1e18 + 1;   int n, k;   string s;   set<char> st;   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     string s;     cin >> s;     int k;     cin >> k;     int n = s.size();     if (n < k) {       cout <<  impossible ;     } else {       for (int i = 0; i < n; ++i) st.insert(s[i]);       int x = st.size();       if (x > k)         cout << 0;       else         cout << abs(x - k);     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k, x;     vector<int> a;     cin >> n >> k;     vector<int> w(k);     for (int i = 0; i < n; i++) {       cin >> x;       a.push_back(x);     }     for (int i = 0; i < k; i++) {       for (int j = i; j < n; j += k) {         w[i] += a[j];       }     }     int menor = w[0];     int z = 0;     for (int i = 0; i < k; i++)       if (menor > w[i]) {         menor = w[i];         z = i;       }     cout << z + 1 << endl;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 5000700;   template <typename T>   pair<T, T> operator-(pair<T, T> a, pair<T, T> b) {     return make_pair(a.first - b.first, a.second - b.second);   }   template <typename T>   pair<T, T> operator+(pair<T, T> a, pair<T, T> b) {     return make_pair(a.first + b.first, a.second + b.second);   }   long long ans[N];   long long qx[N];   long long qy[N];   vector<long long> qrs;   bool to_left(const pair<long long, long long> &a,                const pair<long long, long long> &b) {     return a.second * b.first > a.first * b.second;   }   bool to_left_equal(const pair<long long, long long> &a,                      const pair<long long, long long> &b) {     return a.second * b.first >= a.first * b.second;   }   bool in_triangle(const pair<long long, long long> &x,                    const pair<long long, long long> &a,                    const pair<long long, long long> &b,                    const pair<long long, long long> &c) {     bool b1 = to_left_equal(x - a, b - a), b2 = to_left_equal(x - b, c - b),          b3 = to_left_equal(x - c, a - c), b4 = to_left_equal(x - a, c - a),          b5 = to_left_equal(x - b, a - b), b6 = to_left_equal(x - c, b - c);     return ((b1 && b2 && b3) || (b4 && b5 && b6));   }   long long quadrant(const pair<long long, long long> &a) {     if (a.second < 0) {       if (a.first < 0)         return 0;       else         return 1;     } else {       if (a.first > 0)         return 2;       else         return 3;     }   }   bool f(const long long &a, const long long &b) {     if (qx[a] == qx[b] && qy[a] == qy[b]) return false;     if (qx[a] == 0 && qy[a] == 0) return true;     if (qx[b] == 0 && qy[b] == 0) return false;     long long n1 = quadrant(make_pair(qx[a], qy[a])),               n2 = quadrant(make_pair(qx[b], qy[b]));     if (n1 != n2) return n1 < n2;     return to_left(make_pair(qx[b], qy[b]), make_pair(qx[a], qy[a]));   }   bool f2(const pair<long long, long long> &a,           const pair<long long, long long> &b) {     if (a == b) return false;     if (a.first == 0 && a.second == 0) return true;     if (b.first == 0 && b.second == 0) return false;     long long n1 = quadrant(a), n2 = quadrant(b);     if (n1 != n2) return n1 < n2;     return to_left(b, a);   }   vector<pair<long long, long long>> w[5];   vector<pair<long long, long long>> points;   vector<pair<long long, long long>> allPoints;   vector<pair<long long, long long>> half[2];   void sum(vector<pair<long long, long long>> w1,            vector<pair<long long, long long>> w2, long long id) {     points.clear();     allPoints.clear();     half[0].clear();     half[1].clear();     pair<long long, long long> mini1 = w1[0], mini2 = w2[0];     long long mini1it, mini2it;     for (long long i = 0; i < w1.size(); i++) {       if (w1[i].second > mini1.second ||           (w1[i].second == mini1.second && w1[i].first < mini1.first)) {         mini1it = i;         mini1 = w1[i];       }     }     for (long long i = 0; i < w2.size(); i++) {       if (w2[i].second > mini2.second ||           (w2[i].second == mini2.second && w2[i].first < mini2.first)) {         mini2it = i;         mini2 = w2[i];       }     }     for (long long i = 0; i + 1 < w1.size(); i++) {       allPoints.push_back(w1[i + 1] - w1[i]);     }     allPoints.push_back(w1[0] - w1[w1.size() - 1]);     for (long long i = 0; i + 1 < w2.size(); i++) {       allPoints.push_back(w2[i + 1] - w2[i]);     }     allPoints.push_back(w2[0] - w2[w2.size() - 1]);     sort(allPoints.begin(), allPoints.end(), f2);     pair<long long, long long> cp = mini1 + mini2;     for (auto it : allPoints) {       cp = cp + it;       points.push_back(cp);     }     sort(points.begin(), points.end());     vector<pair<long long, long long>> half[2];     for (long long i = 0; i < 2; i++) {       for (auto it : points) {         while (half[i].size() >= 2 &&                to_left_equal(it - half[i][half[i].size() - 2],                              half[i].back() - half[i][half[i].size() - 2]))           half[i].pop_back();         half[i].push_back(it);       }       reverse(points.begin(), points.end());     }     for (auto x : half[0]) {       w[id].push_back(x);     }     for (long long i = 1; i + 1 < half[1].size(); i++)       w[id].push_back(half[1][i]);     reverse(w[id].begin(), w[id].end());   }   void solve() {     for (long long i = 0; i < 3; i++) {       long long n;       cin >> n;       for (long long j = 0; j < n; j++) {         long long x, y;         cin >> x >> y;         w[i].push_back(make_pair(x, y));       }     }     sum(w[0], w[1], 3);     sum(w[2], w[3], 4);     auto mini = w[4][0];     for (auto it : w[4]) {       if (it.second < mini.second ||           (it.second == mini.second && it.first < mini.first))         mini = it;     }     for (auto &it : w[4]) it = it - mini;     sort(w[4].begin(), w[4].end(), f2);     long long q;     cin >> q;     for (long long i = 0; i < q; i++) {       cin >> qx[i] >> qy[i];       qx[i] = 3 * qx[i] - mini.first;       qy[i] = 3 * qy[i] - mini.second;       qrs.push_back(i);     }     long long ptr = 1;     sort(qrs.begin(), qrs.end(), f);     for (long long i = 0; i < q; i++) {       long long query = qrs[i];       if (qy[query] < 0) {         continue;       }       if (qy[query] == 0 && qx[query] == 0) {         ans[query] = 1;         continue;       }       while (ptr + 1 < w[4].size() &&              to_left(make_pair(qx[query], qy[query]), w[4][ptr + 1])) {         ptr++;       }       if (ptr + 1 < w[4].size() &&           in_triangle(make_pair(qx[query], qy[query]), make_pair(0, 0), w[4][ptr],                       w[4][ptr + 1]))         ans[query] = 1;     }     for (long long i = 0; i < q; i++) {       if (ans[i]) {         cout <<  YES n ;       } else {         cout <<  NO n ;       }     }   }   int32_t main() {     long long t = 1;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long k;     cin >> k;     long long res[4][4];     for (long long i = 0; i < 4; ++i) {       for (long long j = 0; j < 4; ++j) {         if (i == 1 && j == 2) {           res[i][j] = (1 << 17);         } else if (i == 2 && j == 1) {           res[i][j] = (1 << 17);         } else if ((i + j) == 3) {           res[i][j] = k;         } else if (i == 3 && j == 3) {           res[i][j] = k;         } else {           res[i][j] = (k + (1 << 17));         }       }     }     cout << 4 <<     << 4 << endl;     for (long long i = 0; i < 4; ++i) {       for (long long j = 0; j < 4; ++j) {         cout << res[i][j] <<    ;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) {     long long tmp;     while (b) {       a %= b;       tmp = a;       a = b;       b = tmp;     }     return a;   }   int main() {     int n;     cin >> n;     vector<int> a(n + 1, 0);     for (int i = 0; i < n; i++) {       cin >> a[i + 1];     }     sort(a.begin(), a.end());     long long sum1 = 0, sum2 = 0;     vector<long long> sum(n + 1, 0);     for (int i = 1; i <= n; i++) {       sum[i] += sum[i - 1] + (long long)i * (a[i] - a[i - 1]);       sum1 += sum[i];       sum2 += a[i];     }     long long num = 2 * sum1 - sum2;     long long den = n;     long long g = gcd(num, den);     cout << num / g <<     << den / g << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, root, m;   int a[100100];   int h[100100];   vector<int> g[100100];   int arr[200100];   int N, tin[100100], tout[100100];   vector<pair<int, int> > t[200100 * 4];   void dfs(int v, int par = -1, int len = 0) {     h[v] = len;     tin[v] = ++N;     arr[N] = v;     for (int to : g[v]) {       if (to != par) {         dfs(to, v, len + 1);       }     }     tout[v] = ++N;     arr[N] = v;   }   void build(int v, int l, int r) {     if (l == r) {       int node = arr[l];       t[v].push_back(make_pair(h[node], a[node]));       return;     }     int mid = (l + r) >> 1;     build(v + v, l, mid), build(v + v + 1, mid + 1, r);     merge((t[v + v]).begin(), (t[v + v]).end(), (t[v + v + 1]).begin(),           (t[v + v + 1]).end(), back_inserter(t[v]));     for (int i = 1; i < ((int)(t[v]).size()); ++i) {       t[v][i].second = min(t[v][i - 1].second, t[v][i].second);     }   }   int get(int v, int l, int r, int L, int R, int k) {     if (r < L || R < l) return 2e9;     if (L <= l && r <= R) {       int lf = 0, rg = ((int)(t[v]).size()) - 1, i = -1;       while (lf <= rg) {         int avg = (lf + rg) >> 1;         if (t[v][avg].first <= k)           i = avg, lf = avg + 1;         else           rg = avg - 1;       }       return i == -1 ? 2e9 : t[v][i].second;     }     int mid = (l + r) >> 1;     return min(get(v + v, l, mid, L, R, k), get(v + v + 1, mid + 1, r, L, R, k));   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0), cout.tie(0);     cin >> n >> root;     for (int i = 1; i <= n; ++i) cin >> a[i];     for (int i = 1; i < n; ++i) {       int x, y;       cin >> x >> y;       g[x].push_back(y);       g[y].push_back(x);     }     dfs(root);     build(1, 1, N);     cin >> m;     int ans = 0;     for (int i = 1; i <= m; ++i) {       int p, q;       cin >> p >> q;       int x = (p + ans) % n + 1;       int k = (q + ans) % n;       ans = get(1, 1, N, tin[x], tout[x], h[x] + k);       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     node *next[26];     node() {       for (int i = 0; i < 26; i++) next[i] = NULL;     }   };   void insert(node *root, string &str) {     node *current = root;     for (char ch : str) {       if (current->next[ch -  a ] == NULL) {         current->next[ch -  a ] = new node();       }       current = current->next[ch -  a ];     }   }   int canLose(node *root) {     int was = 1, leaf = 0;     for (int i = 0; i < 26; i++) {       if (root->next[i]) {         was = min(was, 1 ^ canLose(root->next[i]));         leaf = 1;       }     }     return was & leaf;   }   int canWin(node *root) {     int was = 0;     for (int i = 0; i < 26; i++) {       if (root->next[i]) {         was = max(was, 1 ^ canWin(root->next[i]));       }     }     return was;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     node *root = new node();     int n, k;     cin >> n >> k;     for (int i = 1; i <= n; i++) {       string str;       cin >> str;       insert(root, str);     }     if (canWin(root) && !canLose(root)) {       cout <<  First  << endl;     } else if (canWin(root)) {       if (k & 1) {         cout <<  First  << endl;       } else {         cout <<  Second  << endl;       }     } else {       cout <<  Second  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[100500];   vector<long long> pfx[100500], vtx[100500];   long long proc(long long x, long long sum) {     int n = vtx[x].size();     int mid = n / 2;     long long res = sum;     for (int i = mid, _n = n; i < _n; i++) res += vtx[x][i] - vtx[x][mid];     for (int i = 0, _n = mid; i < _n; i++) res += vtx[x][mid] - vtx[x][i];     for (int i = 0, _n = n; i < _n; i++) res -= abs(x - vtx[x][i]);     return res;   }   int main() {     int n, m;     long long sum = 0LL;     scanf( %d %d , &n, &m);     for (int i = 0, _n = m; i < _n; i++) scanf( %I64d , a + i);     for (int i = 1, _n = m; i < _n; i++) {       if (a[i] == a[i - 1]) continue;       vtx[a[i]].push_back(a[i - 1]);       vtx[a[i - 1]].push_back(a[i]);       sum += abs(a[i] - a[i - 1]);     }     for (int i = 1, _n = n + 1; i < _n; i++) sort(vtx[i].begin(), vtx[i].end());     long long ans = sum;     for (int i = 1, _n = n + 1; i < _n; i++) ans = min(ans, proc(i, sum));     printf( %I64d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     string s;     cin >> s;     long long l = s.length();     long long ans = 0;     for (long long i = l - 1; i >= 1; i--) {       ans = ans + pow(2, i);     }     for (long long i = 0; i < l; i++) {       if (s[i] ==  7 ) {         ans = ans + pow(2, l - i - 1);       }     }     cout << ans + 1 <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   class Timer {     clock_t start;     string name;       public:     Timer() {       name =   ;       start = clock();     }     Timer(string s) {       name = s;       start = clock();     }     ~Timer() {       fprintf(stderr,  %s: %.3gs n , name.c_str(),               1.0 * (clock() - start) / CLOCKS_PER_SEC);     }   };   const double EPS = 1e-9;   const long double PI = acos(-1.0L);   template <typename dtype>   inline dtype sq(dtype a) {     return a * a;   }   template <typename dtype1, typename dtype2>   inline pair<dtype1, dtype2> mp(dtype1 a, dtype2 b) {     return make_pair(a, b);   }   template <typename dtype1, typename dtype2>   inline dtype1 safeMod(dtype1 a, dtype2 m) {     return (a % m + m) % m;   }   template <typename dtype1, typename dtype2>   inline bool isEq(dtype1 a, dtype2 b) {     return abs(a - b) < EPS;   }   template <typename dtype1, typename dtype2, typename dtype3>   inline bool isEq(dtype1 a, dtype2 b, dtype3 eps) {     return abs(a - b) < eps;   }   template <typename dtype>   inline dtype toRad(dtype deg) {     return deg * PI / 180.0;   }   template <typename dtype>   inline dtype toDeg(dtype rad) {     return rad * 180.0 / PI;   }   template <typename dtype>   inline bool isKthBitOn(dtype n, int k) {     assert(n <= numeric_limits<dtype>::max());     assert(k <= numeric_limits<dtype>::digits);     dtype ONE = 1;     return bool((n & (ONE << k)));   }   template <typename dtype>   inline void setKthBit(dtype& n, int k) {     assert(n <= numeric_limits<dtype>::max());     assert(k <= numeric_limits<dtype>::digits);     dtype ONE = 1;     n = (n | (ONE << k));   }   const int oo = 0x3f3f3f3f;   const int MAX = 200010;   const int MOD = 1000000007;   const int precision = 10;   int main(int argc, char* argv[]) {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long n, s, sum = 0;     long long ara[MAX];     cin >> n >> s;     long long mini = INT_MAX;     for (int i = 0; i < n; i++) {       cin >> ara[i];       sum += ara[i];       mini = min(mini, ara[i]);     }     if (sum < s)       cout << -1;     else {       sum = 0;       for (int i = 0; i < n; i++) {         sum += ara[i] - mini;       }       s -= sum;       s = max(s, 0LL);       mini -= s / n + (s % n != 0);       cout << mini << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     while (t--) {       int n, m;       cin >> n >> m;       vector<int> g[n];       for (int i = 0; i < m; i++) {         int u, v;         cin >> u >> v;         u--, v--;         g[v].push_back(u);       }       int l[n];       for (int i = 0; i < n; i++) l[i] = 0;       vector<int> a;       for (int i = 0; i < n; i++) {         for (auto j : g[i]) l[i] = max(l[i], l[j] + 1);         if (l[i] == 2) {           l[i] = -1;           a.push_back(i);         }       }       cout << a.size() <<   n ;       for (auto i : a) cout << i + 1 <<    ;       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int Maxn = 10005;   const int Inf = 1000000000;   int n;   int a[Maxn];   set<pair<int, int> > S;   long long res;   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     for (int i = 0; i < n; i++) S.insert(pair<int, int>(i - a[i], i));     for (int i = 0; i < n; i++) {       int add = 0, j = 0;       set<pair<int, int> >::iterator it = S.begin();       while (j < n - 1) {         int nxt = j;         while (it != S.end() && j >= it->first - i) {           nxt = max(nxt, it->second - i);           it++;         }         j = nxt;         add++;       }       res += add;       S.erase(pair<int, int>(-a[i] + i, i));       S.insert(pair<int, int>(n - 1 - a[i] + i + 1, n - 1 + i + 1));     }     printf( %I64d n , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n;   long long arr[1005];   long long dp[1005][3 * 10000];   long long add(long long a, long long b) {     return ((a % 1000000007) + (b % 1000000007)) % 1000000007;   }   long long f(long long idx, long long sum) {     if (idx == n + 1) {       return 0;     }     if (dp[idx][sum] != -1) return dp[idx][sum];     long long res1 = sum + arr[idx];     long long res2 = sum - arr[idx];     long long val = 0;     if (res1 == 10001 || res2 == 10001) val++;     long long res = add(f(1 + idx, res1), f(1 + idx, res2));     res = add(res, val);     return dp[idx][sum] = res;   }   int main() {     scanf( %lld , &n);     for (long long i = 1; i <= n; i++) scanf( %lld , &arr[i]);     memset(dp, -1, sizeof dp);     long long ans = 0;     for (int i = 1; i <= n; i++) {       ans = add(ans, f(i, 10001));     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   const long long md = 998244353;   int main() {     long long n;     cin >> n;     vector<long long> a(n), b(n);     for (int i = 0; i < n; i++) {       cin >> a[i];       a[i] *= (n - i) * (i + 1);     }     for (int i = 0; i < n; i++) cin >> b[i];     sort(a.begin(), a.end());     sort(b.rbegin(), b.rend());     long long ans = 0;     for (int i = 0; i < n; i++) (ans += (a[i] % md * b[i] % md) % md) %= md;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100005;   int a[N], c[N];   int ans[N];   struct qq {     int x, y, id;   } s[N];   int n, Q;   bool cmp(qq x, qq y) { return x.y < y.y; }   int q[N];   int st, ed, now;   double Y(int x) { return a[x] * x - c[x]; }   double X(int x) { return -a[x]; }   double get_k(int x, int y) {     return (double)(Y(x) - Y(y)) / (double)(X(x) - X(y));   }   int calc(int xx, int x, int y) { return c[y] - c[xx] + a[xx] * (x - y + xx); }   void solve() {     st = 1;     ed = 0;     now = 1;     for (int u = 1; u <= n; u++) {       while (st <= ed && a[q[ed]] >= a[u]) ed--;       while (st < ed && get_k(q[ed - 1], q[ed]) < get_k(q[ed], u)) ed--;       q[++ed] = u;       while (now <= Q && s[now].y == u) {         int l = st, r = ed, lalal;         while (l <= r) {           int mid = (l + r) >> 1;           if (mid == ed || calc(q[mid], s[now].x, s[now].y) <                                calc(q[mid + 1], s[now].x, s[now].y)) {             lalal = mid;             r = mid - 1;           } else             l = mid + 1;         }         ans[s[now].id] = calc(q[lalal], s[now].x, s[now].y);         now++;       }     }   }   int read() {     char ch = getchar();     int x = 0;     while (ch <  0  || ch >  9 ) ch = getchar();     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x;   }   int main() {     n = read();     for (int u = 1; u <= n; u++) {       a[u] = read();       c[u] = c[u - 1] + a[u];     }     Q = read();     for (int u = 1; u <= Q; u++) {       s[u].x = read();       s[u].y = read();       s[u].id = u;     }     sort(s + 1, s + 1 + Q, cmp);     solve();     for (int u = 1; u <= Q; u++) printf( %d n , ans[u]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long MOD = 1000000007;   signed main() {     cin.tie(0);     ios::sync_with_stdio(false);     long long N, a, b;     cin >> N >> a >> b;     vector<long long> A;     A.push_back(a);     A.push_back(a);     A.push_back(a);     A.push_back(a);     A.push_back(b);     A.push_back(b);     sort(A.begin(), A.end());     long long res = 100000;     long long k = 0;     long long tres;     do {       k = 0;       tres = 1;       for (long long i = 0; i < A.size(); i++) {         k += A[i];         if (k > N) {           k = A[i];           tres++;         }       }       res = min(res, tres);     } while (next_permutation(A.begin(), A.end()));     cout << res << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100005, S = 100;   int L[N], R[N], bel[N], to[N];   long long a[N], b[N], taga[N], tagb[N];   long double slope(int x, int y) {     if (a[x] == a[y]) {       if (b[x] == b[y]) return 1;       return b[x] < b[y] ? 1e18 : -1e18;     }     return (long double)(b[y] - b[x]) / (a[y] - a[x]);   }   vector<int> s[N / S + 5];   int pos[N / S + 5];   void build(int x) {     for (int i = L[x]; i <= R[x]; i++) b[to[i]] += a[to[i]] * taga[x] + tagb[x];     taga[x] = tagb[x] = 0;     s[x].clear();     int top = 0;     for (int i = L[x]; i <= R[x]; i++) {       while (top > 1 &&              slope(s[x][top - 2], s[x][top - 1]) <= slope(s[x][top - 1], to[i]))         --top, s[x].pop_back();       ++top, s[x].push_back(to[i]);     }     pos[x] = 0;   }   inline long long calc(int x) {     return a[x] * taga[bel[x]] + b[x] + tagb[bel[x]];   }   long long querymax(int x) {     while (pos[x] < s[x].size() - 1) {       if (calc(s[x][pos[x]]) > calc(s[x][pos[x] + 1])) break;       pos[x]++;     }     return calc(s[x][pos[x]]);   }   bool cmp(const int& x, const int& y) { return a[x] < a[y]; }   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %lld , &a[i]), b[i] = a[i], to[i] = i;     for (int i = 1; i <= n; i++) bel[i] = (i - 1) / S + 1;     for (int i = 1; i <= bel[n]; i++)       L[i] = S * (i - 1) + 1, R[i] = min(S * i, n),       sort(to + L[i], to + R[i] + 1, cmp);     for (int i = 1; i <= bel[n]; i++) build(i);     long long ans = 0;     for (int i = 1; i <= n; i++) {       long long mx = -1ll << 60ll;       int p;       for (int j = 1; j <= bel[n]; j++)         if (querymax(j) > mx) mx = querymax(j), p = s[j][pos[j]];       if (mx > 0 && p != 0)         ans += mx;       else         break;       b[p] = -1ll << 60ll;       for (int j = 1; j < bel[p]; j++) tagb[j] += a[p];       for (int j = bel[p] + 1; j <= bel[n]; j++) taga[j]++;       for (int j = L[bel[p]]; j < p; j++) b[j] += a[p];       for (int j = p + 1; j <= R[bel[p]]; j++) b[j] += a[j];       build(bel[p]);     }     printf( %lld , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   const long long MOD2 = 998244353;   const long long MOD3 = 1812447359;   const long long INF = 1ll << 62;   const double PI = 2 * asin(1);   void yes() { printf( yes n ); }   void no() { printf( no n ); }   void Yes() { printf( Yes n ); }   void No() { printf( No n ); }   void YES() { printf( YES n ); }   void NO() { printf( NO n ); }   struct SegmentTree {     vector<int> Tree[21];     void Make() {       for (int i = 0; i < 21; i++) {         Tree[i].resize(1 << i);       }     }     void Add(int num, int val) {       for (int i = 20; i >= 0; i--) {         Tree[i][num] += val;         num /= 2;       }     }     int Search(int num) {       int now = 0, sum = 0;       for (int i = 1; i < 21; i++) {         now *= 2;         if (sum + Tree[i][now] < num) {           sum += Tree[i][now];           now++;         }       }       return now;     }   };   int main() {     int N, Q;     scanf( %d%d , &N, &Q);     SegmentTree T;     T.Make();     for (int i = 0; i < N; i++) {       int A;       scanf( %d , &A);       T.Add(A, 1);     }     for (int i = 0; i < Q; i++) {       int K;       scanf( %d , &K);       if (K > 0) {         T.Add(K, 1);       } else {         int num = T.Search(-1 * K);         T.Add(num, -1);       }     }     for (int i = 0; i <= 1e6; i++) {       if (T.Tree[20][i] > 0) {         cout << i << endl;         return 0;       }     }     cout << 0 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   int n, m, f[N];   bool vis[2][N];   vector<int> edges[2][N];   void dfs0(int x) {     if (vis[0][x]) return;     vis[0][x] = 1;     for (int to : edges[0][x]) {       if (f[to] != 1) dfs0(to);     }   }   void dfs1(int x) {     if (vis[1][x]) return;     vis[1][x] = 1;     if (f[x] == 1) return;     for (int to : edges[1][x]) {       dfs1(to);     }   }   void solve() {     cin >> n >> m;     for (int i = 1; i <= n; ++i) {       cin >> f[i];     }     for (int i = 1; i <= m; ++i) {       int u, v;       cin >> u >> v;       edges[0][u].push_back(v);       edges[1][v].push_back(u);     }     for (int i = 1; i <= n; ++i) {       if (f[i] == 1) {         dfs0(i);       }       if (f[i] == 2) {         dfs1(i);       }     }     for (int i = 1; i <= n; ++i) {       cout << (vis[0][i] && vis[1][i]) <<    ;     }   }   int main() {     ios::sync_with_stdio(0);     int T = 1;     while (T--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   #pragma GCC optimize( O3 )   template <class c>   struct rge {     c b, e;   };   template <class c>   rge<c> range(c i, c j) {     return rge<c>{i, j};   }   template <class c>   auto dud(c* x) -> decltype(cerr << *x, 0);   template <class c>   char dud(...);   struct debug {     template <class c>     debug& operator<<(const c&) {       return *this;     }   };   int main();   void file() {}   void time() {}   const int INF = 1e9 + 99;   using ll = long long int;   using pii = pair<int, int>;   using vec = vector<int>;   int main() {     ios_base::sync_with_stdio(0);     ;     ll n;     cin >> n;     vector<ll> v(n);     for (ll& x : v) {       cin >> x;     }     sort(v.begin(), v.end());     ll i = 0, j = n - 1;     ll x = 0, y = 0;     bool ok = false;     while (i <= j) {       if (ok) {         x += v[i];         i++;       } else {         y += v[j];         j--;       }       ok ^= 1;     }     ll ans = x * x + y * y;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 5;   const double eps = 1e-8;   const int INF = 0x3f3f3f3f;   const int mod = 1e9 + 7;   const int seed = 131;   int n, a[maxn], q;   struct node {     int l, r, sum, lazy;   } Q[maxn << 2];   void build(int rt, int l, int r) {     Q[rt].l = l, Q[rt].r = r;     if (l == r) {       Q[rt].sum = a[l];       return;     }     int mid = (l + r) / 2;     build((rt << 1), l, mid);     build(((rt << 1) | 1), mid + 1, r);   }   inline void pushdown(int rt) {     if (Q[(rt << 1)].l == Q[(rt << 1)].r)       Q[(rt << 1)].sum = max(Q[(rt << 1)].sum, Q[rt].lazy);     else       Q[(rt << 1)].lazy = max(Q[(rt << 1)].lazy, Q[rt].lazy);     if (Q[((rt << 1) | 1)].l == Q[((rt << 1) | 1)].r)       Q[((rt << 1) | 1)].sum = max(Q[((rt << 1) | 1)].sum, Q[rt].lazy);     else       Q[((rt << 1) | 1)].lazy = max(Q[((rt << 1) | 1)].lazy, Q[rt].lazy);     Q[rt].lazy = 0;   }   void update(int rt, int p, int x) {     if (Q[rt].l == Q[rt].r) {       Q[rt].sum = x;       return;     }     pushdown(rt);     int mid = (Q[rt].l + Q[rt].r) / 2;     if (p <= mid)       update((rt << 1), p, x);     else       update(((rt << 1) | 1), p, x);   }   void show(int rt) { cout << Q[rt].l <<     << Q[rt].r <<   n ; }   void query(int rt) {     if (Q[rt].l == Q[rt].r) {       cout << Q[rt].sum;       if (Q[rt].l != n)         cout <<    ;       else         cout <<   n ;       return;     }     pushdown(rt);     query((rt << 1));     query(((rt << 1) | 1));   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     build(1, 1, n);     scanf( %d , &q);     int op, x, y;     for (int i = 1; i <= q; i++) {       scanf( %d , &op);       if (op == 1) {         scanf( %d %d , &x, &y);         update(1, x, y);       } else {         scanf( %d , &x);         if (n == 1)           Q[1].sum = max(Q[1].sum, x);         else if (n > 1 && Q[1].lazy < x)           Q[1].lazy = x;       }     }     query(1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, arr[100], res, maxi, mini, g;   int gcd(int a, int b) {     for (; b != 0; b = b % a) swap(a, b);     return a;   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d , &arr[i]);     maxi = g = arr[0];     for (int i = 0; i < n; i++) {       maxi = max(maxi, arr[i]);       mini = min(mini, arr[i]);       g = gcd(g, arr[i]);     }     res = (maxi / g) - n;     if (res & 1)       printf( Alice );     else       printf( Bob );   }


#include <bits/stdc++.h>   using namespace std;   long long fS(long long x) {     long long lo = 1, hi = x - 1, cn = 0, ans = -1, out = 1;     while (cn <= 80) {       if (cn & 1)         ans -= (lo + hi) / 2;       else         ans += (lo + hi) / 2;       lo = (lo + hi) / 2 + 1;       out = min(out, ans);       cn++;     }     return out;   }   int main() {     int t;     cin >> t;     while (t--) {       long long n;       cin >> n;       long long start = -fS(n);       cout <<  ?   << start << endl;       long long tr;       cin >> tr;       long long lo = 1LL, hi = n - 1LL, cn = 0LL, best = n, ans = start, x;       while (lo <= hi) {         long long mid = (lo + hi) / 2LL;         if (cn & 1)           ans -= mid;         else           ans += mid;         cout <<  ?   << ans << endl;         cin >> x;         if (x) {           hi = mid - 1LL;           best = mid;         } else {           lo = mid + 1LL;         }         cn++;       }       cout <<  =   << best << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, k, i, j;     cin >> n;     long long ans = 0;     for (i = 1; i <= n; i++) {       for (j = i; j <= n; j++) {         k = sqrt(i * i + j * j);         if (k * k == i * i + j * j && j <= k && k <= n) {           ans++;         }       }     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAXN = 2e5 + 5;   vector<long long> adj[MAXN];   long long a[MAXN], cnt0[MAXN], cnt1[MAXN];   bool t0[MAXN], t1[MAXN];   bool b[MAXN], c[MAXN];   long long n;   void input();   void dfs(long long, long long);   void solve();   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     input();     solve();     return 0;   }   void input() {     cin >> n;     for (long long i = 0; i < n; i++) {       cin >> a[i] >> b[i] >> c[i];       t1[i] = (b[i] == 0 && c[i] == 1);       t0[i] = (b[i] == 1 && c[i] == 0);     }     for (long long i = 1; i < n; i++) {       long long u, v;       cin >> u >> v;       u--, v--;       adj[u].push_back(v);       adj[v].push_back(u);     }   }   void dfs(long long u, long long par) {     for (auto v : adj[u]) {       if (v == par) continue;       a[v] = min(a[v], a[u]);       dfs(v, u);       cnt0[u] += max(0ll, cnt0[v] - cnt1[v]);       cnt1[u] += max(0ll, cnt1[v] - cnt0[v]);     }     cnt0[u] += t0[u];     cnt1[u] += t1[u];   }   void solve() {     dfs(0, -1);     if (cnt0[0] != cnt1[0]) {       cout << -1;       return;     }     long long ans = 0;     for (long long i = 0; i < n; i++) ans += 2 * min(cnt0[i], cnt1[i]) * a[i];     cout << ans << endl;   }


#include<bits/stdc++.h>   using namespace std;      struct _field   {       bool white = false;   };         int mod = 998244353;         int main()   {       ios_base::sync_with_stdio(0);       cin.tie(0);       cout.tie(0);   #ifndef ONLINE_JUDGE       freopen( input.txt ,  r , stdin);       //freopen( a.out , w ,stdout); // console output is better (in most cases)   #else       // add i/o method of specific testing system   #endif          int n, m;       cin >> n >> m;       vector<vector<_field>> field(m + 1, vector<_field>(n + 1));       for(int y = 1; y <= n; ++y)       {           for(int x = 1; x <= m; ++x)           {               char temp;               cin >> temp;               if(temp ==  * )               {                   field[x][y].white = false;               }               if(temp ==  o )               {                   field[x][y].white = true;               }           }       }       long long sol = 0; // value       long long v = 0; // value       long long h = 0; // combinations that have a half to the left       long long e = 0; // combinations that are ended on the left       long long c = 1; // #combinations       for(int y = 0; y <= n; ++y)       {           for(int x = 0; x <= m; ++x)           {               long long nh = 0;               long long ne = 0;               // count if we match the color               nh = (c + e) % mod;               ne = h;                     c = ((c + e) % mod + h) % mod;               if(field[x][y].white)               {                   v = ((v * 2) % mod + ne) % mod;               }               else               {                   nh = 0;                   ne = 0;               }                  h = nh;               e = ne;              }       }       sol = v;       int sol1 = v;       v = 0;       h = 0; // combinations that have a half to the left       e = 0; // combinations that are ended on the left       c = 1; // black ones       for(int x = 0; x <= m; ++x)       {           for(int y = 0; y <= n; ++y)           {               long long nh = 0;               long long ne = 0;               // count if we match the color               nh = (c + e) % mod;               ne = h;                     c = ((c + e) % mod + h) % mod;               if(field[x][y].white)               {                   v = ((v * 2) % mod + ne) % mod;               }               else               {                   nh = 0;                   ne = 0;               }                  h = nh;               e = ne;              }       }       int sol2 = v;          sol = (sol + v) % mod;       //cout << sol1 <<   n ;       //cout << sol2 <<   n ;       cout << sol <<   n ;          return 0;   }


#include <bits/stdc++.h>   int fr[182], ne[362], v[362], bs = 0;   void addb(int a, int b) {     v[bs] = b;     ne[bs] = fr[a];     fr[a] = bs++;   }   int dp[182][182], wz[182], sz[182], cd[182][182], n, k;   int fa[182], sd[182], ans[182], zy[362][182];   void dfs0(int u, int f) {     fa[u] = f;     sd[u] = sd[f] + 1;     for (int i = fr[u]; i != -1; i = ne[i]) {       if (v[i] != f) dfs0(v[i], u);     }   }   int dfscd(int a, int b) {     if (a == b) return 0;     if (cd[a][b]) return cd[a][b];     if (sd[a] > sd[b])       cd[a][b] = dfscd(fa[a], b) + 1;     else       cd[a][b] = dfscd(a, fa[b]) + 1;     return cd[a][b];   }   void dfs(int u, int f) {     for (int i = fr[u]; i != -1; i = ne[i]) {       if (v[i] != f) dfs(v[i], u);     }     for (int a = 1; a <= n; a++) {       dp[u][a] = sz[cd[u][a]] + k;       for (int i = fr[u]; i != -1; i = ne[i]) {         if (v[i] == f) continue;         int t = dp[v[i]][wz[v[i]]];         if (dp[v[i]][a] - k < t) {           t = dp[v[i]][a] - k;           zy[i][a] = a;         } else           zy[i][a] = wz[v[i]];         dp[u][a] += t;       }       if (a == 1 || dp[u][a] < dp[u][wz[u]]) wz[u] = a;     }   }   void dfs1(int u, int f, int a) {     ans[u] = a;     for (int i = fr[u]; i != -1; i = ne[i]) {       if (v[i] != f) dfs1(v[i], u, zy[i][a]);     }   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) fr[i] = -1;     for (int i = 1; i < n; i++) scanf( %d , &sz[i]);     for (int i = 0; i < n - 1; i++) {       int a, b;       scanf( %d%d , &a, &b);       addb(a, b);       addb(b, a);     }     dfs0(1, 0);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         if (i != j && cd[i][j] == 0) dfscd(i, j);       }     }     dfs(1, 0);     int z = 1;     for (int i = 2; i <= n; i++) {       if (dp[1][i] < dp[1][z]) z = i;     }     printf( %d n , dp[1][z]);     dfs1(1, 0, z);     for (int i = 1; i <= n; i++) printf( %d  , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long double EPS = 1e-9;   long long cel(long long a, int b) { return ((a - 1) / b + 1); }   long long gcd(long long a, long long b) {     if (a < b) swap(a, b);     return (b == 0) ? a : gcd(b, a % b);   }   long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }   long long po(long long x, long long y) {     long long ans = 1;     while (y) {       if (y & 1) {         ans = (ans * x) % 1000000007;       }       y >>= 1;       x = (x * x) % 1000000007;     }     return ans;   }   vector<int> v(3000);   long long m = 0, n;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t, x, y, ans = 0;     cin >> n;     for (int i = 2; i < 1 << (n + 1); i++) cin >> v[i];     for (int i = (1 << n) - 1; i >= 1; i--) {       ans += abs(v[i * 2] - v[i * 2 + 1]);       v[i] += max(v[i * 2], v[i * 2 + 1]);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     long long l;     cin >> n >> l;     long double arr[n];     for (int i = 0; i < n; i++) {       cin >> arr[i];     }     sort(arr, arr + n);     long double ans = arr[0] - 0;     for (int i = 1; i < n; i++) {       ans = max(ans, (arr[i] - arr[i - 1]) / 2.0);     }     ans = max(ans, l - arr[n - 1]);     cout << fixed << setprecision(10) << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long flag, cnt, j, i, k, powe, count1, count2;   long long int n, a, b, c, x, y, m, ans;   long long maxy, miny, sum, ann[1000010];   string s, s2;   long long mod = 1000000007;   int main() {     cin >> n;     n--;     long long arr1[n], arr2[n];     for (i = 0; i < n; i++) {       cin >> arr1[i];       if (arr1[i] == 0) i--;     }     for (i = 0; i < n; i++) {       cin >> arr2[i];       if (arr2[i] == 0) i--;     }     for (i = 0; i < n; i++) {       if (arr2[i] == arr1[0]) break;     }     for (j = 0; j < n; j++) {       if (arr1[j] != arr2[(i + j) % n]) {         flag = 1;         break;       }     }     if (flag == 1)       cout <<  No ;     else       cout <<  Yes ;   }


#include <bits/stdc++.h>   using namespace std;   map<string, string> m;   string a =  ......... ;   inline int judge() {     for (int i = 0; i < 3; i++) {       if (a[3 * i] !=  . )         if (a[3 * i] == a[3 * i + 1] && a[3 * i] == a[3 * i + 2]) return 3 * i;       if (a[i] !=  . )         if (a[i] == a[i + 3] && a[i] == a[i + 6]) return i;     }     if (a[4] ==  . )       return -1;     else if ((a[0] == a[4] && a[4] == a[8]) || (a[2] == a[4] && a[4] == a[6]))       return 4;     return -1;   }   void search(int n) {     int s;     s = judge();     if (s != -1) {       m[a] = (a[s] ==  X  ?  the first player won  :  the second player won );       return;     } else if (n == 9) {       m[a] =  draw ;       return;     } else {       m[a] = (n % 2 == 0 ?  first  :  second );       for (int i = 0; i < 9; i++)         if (a[i] ==  . ) {           a[i] = (n % 2 == 0 ?  X  :  0 );           search(n + 1);           a[i] =  . ;         }     }   }   int main() {     search(0);     string str1, str2, str3;     cin >> str1 >> str2 >> str3;     a = str1 + str2 + str3;     if (m[a] !=   )       cout << m[a] << endl;     else       cout <<  illegal  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   #define ll long long    #define ld long double   #define vi vector<int>   #define pi pair<int,int>   #define F first   #define S second   #define pb push_back   #define mp make_pair   #define FOR for(int i=a;i<b;++i)   #define sq(a) (a)*(a)   #define vll vector<long long>    const int inf=INT_MAX;    const ll INF=LLONG_MAX;    const int MOD=1000000007;    //char-48   int dp[100000];    int main(){    ios::sync_with_stdio(0);     cin.tie(0);      ll t;     cin >> t;     while(t--){     ll n, ans=0;      cin >> n;      vector<ll> v;      for(ll i=0;i<n;++i){      ll u;       cin >> u;       v.pb(u);             ans=max(ans,u);       }     sort(v.begin(), v.end());      ll b=v.size()-1;      ll g=b;      for(ll i=0; i<b; ++i){      ans+=(v[i]-v[i+1])*(g*(i+1));                     --g;       }     cout << ans <<   n ;           }    }      //Erase reps   // sort(v.begin(), v.end() );   // v.erase(unique(v.begin(), v.end() ), v.end());      //Find something to eliminate   // auto i = find(begin(s), end(s),  { );   // s.erase(i);


#include <bits/stdc++.h>   using namespace std;   int i, t, n, ciel, jiro, aux, a[10005];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     for (cin >> t; t; --t) {       cin >> n;       for (i = 1; i <= n; ++i) {         cin >> aux;         if (n & 1)           if (i == n / 2 + 1)             a[++a[0]] = aux;           else if (i <= n / 2)             ciel += aux;           else             jiro += aux;         else if (i <= n / 2)           ciel += aux;         else           jiro += aux;       }     }     sort(a + 1, a + a[0] + 1);     for (i = a[0]; i; --i)       if ((a[0] - i) & 1)         jiro += a[i];       else         ciel += a[i];     cout << ciel <<     << jiro <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   long long n, a[10], C[110][110], dp[110][10], ans;   inline long long add(long long x, long long y) {     x += y;     if (x > MOD) x -= MOD;     return x;   }   inline long long mul(long long x, long long y) {     x *= y;     if (x > MOD) x %= MOD;     return x;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.setf(ios::fixed);     cout.precision(20);     cin >> n;     for (int i = 0; i < 10; ++i) cin >> a[i];     C[0][0] = 1;     for (int i = 1; i <= 100; ++i) {       C[i][0] = 1;       for (int j = 1; j <= i; ++j) C[i][j] = add(C[i - 1][j - 1], C[i - 1][j]);     }     for (int i = a[9]; i <= n; ++i) dp[i][9] = 1;     for (int i = 0; i <= n; ++i) {       for (int j = 8; j > 0; --j)         for (int k = a[j]; k <= i; ++k)           dp[i][j] = add(dp[i][j], mul(dp[i - k][j + 1], C[i][k]));       for (int j = a[0]; j < i; ++j)         dp[i][0] = add(dp[i][0], mul(dp[i - j][1], C[i - 1][j]));     }     for (int i = 0; i <= n; ++i) ans = add(ans, dp[i][0]);     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, ans, num;     scanf( %d , &n);     for (int i = 17; i >= 1; i--) {       num = (pow(2, i) - 1) * pow(2, i - 1);       if (n % num == 0) {         ans = num;         break;       }     }     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   int n, a[100000];   long long cost, mn;   int main() {     cin >> n;     for (int i = 0; i < n; ++i) scanf( %d , a + i);     sort(a, a + n);     for (int i = 0; i < n; ++i) mn += a[i] - 1;     if (n > 40) return cout << mn, 0;     for (int i = 2; i <= 1e5; ++i) {       int k = 0;       cost = 0;       for (long long j = 1; j <= 1e10 && k < n; j *= i) {         cost += abs(a[k++] - j);       }       if (k == n) mn = min(mn, cost);     }     cout << mn;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e6 + 1e3;   long long all[MAXN];   struct node {     node *child[2];     node() { child[0] = child[1] = NULL; }   };   node *root;   deque<short> binary(long long x) {     deque<short> ans;     while (x) {       ans.push_front(x % 2);       x /= 2;     }     while (ans.size() < 50) ans.push_front(0);     return ans;   }   void add(long long x) {     deque<short> a = binary(x);     node *C = root;     while (a.size()) {       if (C->child[a[0]] == NULL) C->child[a[0]] = new node;       C = C->child[a[0]];       a.pop_front();     }   }   long long query(long long x) {     deque<short> a = binary(x);     long long ans = 0;     node *C = root;     while (a.size()) {       if (C->child[1 - a[0]] != NULL) {         C = C->child[1 - a[0]];         ans *= 2;         ans += 1;       } else {         C = C->child[a[0]];         ans *= 2;       }       a.pop_front();     }     return ans;   }   int main() {     root = new node;     int n;     cin >> n;     long long xorall = 0;     for (int i = 1; i <= n; ++i) {       cin >> all[i];       xorall ^= all[i];     }     long long maxgot = 0;     int ai = 0, bi = 0;     long long total = 0;     add(total);     for (int i = 1; i <= n; ++i) {       total ^= all[i];       add(total);       maxgot = max(maxgot, query(total ^ xorall));     }     cout << fixed << maxgot << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int MOD = 1e9 + 7;   const long long int INF = 1e10;   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     string s;     cin >> s;     char c = s[0];     long long int a = s[1] -  0 ;     long long int ans = 0;     for (long long int i = -1; i <= 1; i++) {       for (long long int j = -1; j <= 1; j++) {         if (i == j && i == 0) continue;         if ((char)(c + i) <=  h  && (char)(c + i) >=  a ) {           if (a + j >= 1 && a + j <= 8) ans++;         }       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct UnionFind {     vector<int> par;     vector<int> rank;     vector<int> sz;     UnionFind(int n = 0) {       if (n > 0) initialize(n);     }     void initialize(int n) {       par.resize(n);       rank.resize(n);       sz.resize(n);       for (int i = 0; i < n; i++) {         par[i] = i;         rank[i] = 0;         sz[i] = 1;       }     }     int find(int x) {       if (par[x] == x) {         return x;       } else {         return par[x] = find(par[x]);       }     }     void unite(int x, int y) {       x = find(x);       y = find(y);       if (x == y) return;       if (rank[x] < rank[y]) {         par[x] = y;         sz[y] += sz[x];       } else {         par[y] = x;         sz[x] += sz[y];         if (rank[x] == rank[y]) rank[x]++;       }     }     bool same(int x, int y) { return find(x) == find(y); }   };   int main() {     int N, M;     cin >> N >> M;     static pair<int64_t, int> A[200000];     for (int i = 0; i < N; i++) {       int64_t a;       scanf( %lld , &a);       A[i] = {a, i};     }     vector<vector<int64_t>> edges;     for (int i = 0; i < M; i++) {       int64_t x, y, w;       scanf( %lld %lld %lld , &x, &y, &w);       edges.push_back({w, x - 1, y - 1});     }     sort(A, A + N);     int64_t first = A[0].second;     for (int i = 1; i < N; i++) {       int64_t j = A[i].second;       int64_t cost = A[0].first + A[i].first;       edges.push_back({cost, first, j});     }     sort(edges.begin(), edges.end());     int64_t ans = 0;     UnionFind uf(N);     for (auto& v : edges) {       int i = v[1], j = v[2];       if (!uf.same(i, j)) {         ans += v[0];         uf.unite(i, j);       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int i, k;     int n;     cin >> n;     if (n % 2 == 0) {       k = n / 2;       cout << k <<   n ;       for (i = 0; i < k; i++) cout << 2 <<    ;     } else {       k = (n - 1) / 2;       cout << k <<   n ;       for (i = 0; i < k - 1; i++) {         cout << 2 <<    ;       }       cout << 3 <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100 + 5;   int n, m, ord[N << 1], nord = 0;   vector<int> adj[N << 1], radj[N << 1];   bool vis[N << 1];   int ncmp = 0, cmp[N << 1];   vector<int> ans;   inline void addedge(int u, int v) {     adj[u].push_back((v > n ? v - n : v + n));     ;     radj[(v > n ? v - n : v + n)].push_back(u);     ;     adj[(u > n ? u - n : u + n)].push_back(v);     ;     radj[v].push_back((u > n ? u - n : u + n));     ;   }   inline void dfs(int v) {     vis[v] = 1;     for (int i = 0; i < (int)adj[v].size(); i++)       if (!vis[adj[v][i]]) dfs(adj[v][i]);     ord[++nord] = v;   }   inline void rdfs(int v) {     cmp[v] = ncmp;     vis[v] = 1;     for (int i = 0; i < (int)radj[v].size(); i++)       if (!vis[radj[v][i]]) rdfs(radj[v][i]);   }   int main() {     scanf( %d%d , &n, &m);     int u, v, c;     for (int i = 1; i <= m; i++) {       scanf( %d%d%d , &u, &v, &c);       if (!c) {         addedge(u, v);         addedge(v, u);       } else {         addedge(u, (v > n ? v - n : v + n));         addedge((v > n ? v - n : v + n), u);       }     }     memset(vis, 0, sizeof vis);     for (int i = 1; i <= 2 * n; i++)       if (!vis[i]) dfs(i);     memset(vis, 0, sizeof vis);     for (; nord--;) {       u = ord[nord];       if (!vis[u]) {         ncmp++;         rdfs(u);       }     }     for (int i = 1; i <= n; i++) {       if (cmp[i] == cmp[i + n]) {         printf( Impossible n );         return 0;       }       if (cmp[i + n] < cmp[i]) ans.push_back(i);       ;     }     printf( %d n , (int)ans.size());     for (int i = 0; i < (int)ans.size(); i++) printf( %d  , ans[i]);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long t, n;   int main() {     cin >> t;     vector<int> a;     for (int i = 1; i <= 10000; i++) {       n = i;       vector<int> v;       while (n > 0) {         v.push_back(n % 10);         n = n / 10;       }       reverse(v.begin(), v.end());       for (int j = 0; j < v.size(); j++) {         a.push_back(v[j]);       }     }     for (int i = 0; i < a.size(); i++) {       if (i + 1 == t) {         cout << a[i];         return 0;       }     }     return 0;   }


#include <bits/stdc++.h>   int main() {     int i, j = 1, x, k = 0;     int c, y;     int ar[1000];     char str[500];     scanf( %s , str);     for (i = 0; str[i] !=   0 ; i++) {       x = isdigit(str[i]);       if (x != 0) {         ar[j] = str[i];         j++;         k++;       }     }     int pass;     for (pass = 1; pass <= k; pass++) {       for (j = 1; j <= k - 1; j++) {         if (ar[j] > ar[j + 1]) {           c = ar[j];           ar[j] = ar[j + 1];           ar[j + 1] = c;         }       }     }     j = 1;     for (i = 0; str[i] !=   0 ; i++) {       y = isdigit(str[i]);       {         if (y != 0) {           str[i] = ar[j];           j++;         }       }     }     printf( %s , str);   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 2000000000000000000LL;   const int inf = 0x3f3f3f3f;   const long double EPS = 1e-9;   int dirx[] = {0, 0, -1, 1};   int diry[] = {-1, 1, 0, 0};   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     int test;     cin >> test;     while (test--) {       int len;       string path;       cin >> len >> path;       std::map<pair<long long, long long>, int> visit;       long long mn = INF;       pair<long long, long long> mnpath{-1, -1}, pos{0, 0};       visit[{0, 0}] = 0;       for (int i = 0; i < len; i++) {         if (path[i] ==  L )           pos.first += dirx[0], pos.second += diry[0];         else if (path[i] ==  R )           pos.first += dirx[1], pos.second += diry[1];         else if (path[i] ==  U )           pos.first += dirx[2], pos.second += diry[2];         else if (path[i] ==  D )           pos.first += dirx[3], pos.second += diry[3];         if (visit.find(pos) != visit.end()) {           int dis = i - visit[pos] + 1;           if (mn >= dis) {             mn = dis;             mnpath = {visit[pos] + 1, i + 1};           }         }         visit[pos] = i + 1;       }       if (mn != INF)         cout << mnpath.first <<     << mnpath.second << endl;       else         cout << -1 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 5;   char s[maxn], t[maxn];   int p[maxn], q[maxn], r[maxn];   int main() {     int n, m, k, d;     scanf( %s%d , s, &m);     n = strlen(s);     while (m--) {       scanf( %d%d , &k, &d);       for (int i = 0; i < n; i++) p[i] = q[i] = i;       int id = 0;       for (int i = 0; i < d; i++) {         for (int j = i; j < k; j += d) p[id++] = j;       }       int last = p[0];       for (int i = 0; i < n - 1; i++) p[i] = p[i + 1];       p[n - 1] = last;       int pw = n - k + 1;       while (pw > 0) {         if (pw & 1) {           for (int i = 0; i < n; i++) r[i] = q[p[i]];           for (int i = 0; i < n; i++) q[i] = r[i];         }         for (int i = 0; i < n; i++) r[i] = p[p[i]];         for (int i = 0; i < n; i++) p[i] = r[i];         pw >>= 1;       }       for (int i = 0; i < n; i++) t[i] = s[q[(k + i - 1) % n]];       for (int i = 0; i < n; i++) s[i] = t[i];       puts(s);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int b[n];       int ans[2 * n];       for (long long i = 0; i < n; i++) cin >> b[i];       set<int> a;       for (long long i = 0; i < 2 * n; i++) a.insert(i + 1);       for (long long i = 0; i < n; i++) {         ans[2 * i] = b[i];         a.erase(b[i]);       }       bool poss = true;       for (long long i = 0; i < n; i++) {         set<int>::iterator up = upper_bound(a.begin(), a.end(), b[i]);         if (up != a.end()) {           ans[2 * i + 1] = *up;           a.erase(up);         } else {           cout << -1 <<   n ;           poss = false;           break;         }       }       if (poss) {         for (long long i = 0; i < 2 * n - 1; i++) cout << ans[i] <<    ;         cout << ans[2 * n - 1] <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast,unroll-loops,no-stack-protector )   using namespace std;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   const int N = 3005;   map<int, vector<int> > dif;   vector<int> mod[8];   int a[N];   int n;   template <class C, class F>   struct Mcmf {     static constexpr F eps = (F)1e-9;     struct Edge {       int u, v, inv;       F cap, flow;       C cost;       Edge(int u, int v, C cost, F cap, int inv)           : u(u), v(v), cost(cost), cap(cap), flow(0), inv(inv) {}     };     int second, t, tm, n, m = 0;     vector<vector<Edge> > g;     vector<Edge*> prev;     vector<C> cost;     vector<int> state;     Mcmf(int n, int ss = -1, int tt = -1)         : n(n), g(n + 5), cost(n + 5), state(n + 5), prev(n + 5) {       second = n + 1;       tm = n + 2;       t = n + 3;     }     void add(int u, int v, C cost, F cap) {       g[u].push_back(Edge(u, v, cost, cap, int(g[v].size())));       g[v].push_back(Edge(v, u, -cost, 0, int(g[u].size()) - 1));       m += 2;     }     bool bfs() {       fill(begin(state), end(state), 2);       fill(begin(cost), end(cost), numeric_limits<C>::max());       fill(begin(prev), end(prev), nullptr);       deque<int> qu;       qu.push_back(second);       state[second] = 1, cost[second] = 0;       while (int(qu.size())) {         int u = qu.front();         qu.pop_front();         state[u] = 0;         for (Edge& e : g[u])           if (e.cap - e.flow > eps)             if (cost[u] + e.cost < cost[e.v]) {               cost[e.v] = cost[u] + e.cost;               prev[e.v] = &e;               if (state[e.v] == 0 ||                   (int(qu.size()) && cost[qu.front()] > cost[e.v]))                 qu.push_front(e.v);               else if (state[e.v] == 2)                 qu.push_back(e.v);               state[e.v] = 1;             }       }       return cost[t] != numeric_limits<C>::max();     }     pair<C, F> minCostFlow() {       C cost = 0;       F flow = 0;       while (bfs()) {         F nflow = numeric_limits<F>::max();         for (Edge* e = prev[t]; e != nullptr; e = prev[e->u])           nflow = min(nflow, e->cap - e->flow);         for (Edge* e = prev[t]; e != nullptr; e = prev[e->u]) {           e->flow += nflow;           g[e->v][e->inv].flow -= nflow;           cost += e->cost * nflow;         }         flow += nflow;       }       return make_pair(cost, flow);     }   };   int main() {     cin.tie(0)->sync_with_stdio(0), cout.tie(0);     cin >> n;     for (auto i = (0) - ((0) > (n)); i != n - ((0) > (n));          i += 1 - 2 * ((0) > (n)))       cin >> a[i];     Mcmf<int, int> mcmf(2 * n);     for (auto i = (0) - ((0) > (n)); i != n - ((0) > (n));          i += 1 - 2 * ((0) > (n))) {       mcmf.add(mcmf.second, (i), 0, 1);       mcmf.add((i), (n + (i)), -1, 1);       mcmf.add((n + (i)), mcmf.tm, 0, 1);       for (auto j = (i + 1) - ((i + 1) > (n)); j != n - ((i + 1) > (n));            j += 1 - 2 * ((i + 1) > (n)))         if (a[i] % 7 == a[j] % 7 || abs(a[i] - a[j]) <= 1)           mcmf.add((n + (i)), (j), 0, 1);     }     mcmf.add(mcmf.tm, mcmf.t, 0, 4);     auto best = mcmf.minCostFlow();     ;     cout << -best.first <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   const int max_length = 1e6;   int a_cnt[max_length];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     string s;     cin >> s;     long long b = 0;     long long ans = 0;     int n = s.size();     for (int i = s.size() - 1; i >= 0; i--) {       if (s[i] ==  a  && b != 0) {         ans += b;         ans %= mod;         b = b * 2;         b %= mod;       } else if (s[i] ==  b )         b++;     }     cout << ans % mod;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int no_of_cards;     scanf( %d , &no_of_cards);     vector<int> card(no_of_cards);     for (int i = 0; i < no_of_cards; i++) scanf( %d , &card[i]);     if (no_of_cards == 1) {       printf( -1 n );       return 0;     }     vector<int> new_cards;     if (no_of_cards == 2 && (card[0] != card[1]) &&         (card[0] + card[1]) % 2 == 0) {       new_cards.push_back((card[0] + card[1]) / 2);     }     sort((card).begin(), (card).end());     int min_difference = card[1] - card[0];     for (int i = 2; i < no_of_cards; i++)       min_difference = min(min_difference, card[i] - card[i - 1]);     int ap_possible = true, new_middle_cards = 0;     for (int i = 1; i < no_of_cards; i++) {       if (card[i] - card[i - 1] != min_difference) {         if (card[i] - card[i - 1] == 2 * min_difference) {           new_middle_cards++;         } else {           ap_possible = false;         }       }     }     if (ap_possible) {       if (min_difference == 0) {         new_cards.push_back(card[0]);       } else if (new_middle_cards == 0) {         new_cards.push_back(card[0] - min_difference);         new_cards.push_back(card[no_of_cards - 1] + min_difference);       } else if (new_middle_cards == 1) {         for (int i = 1; i < no_of_cards; i++)           if (card[i] - card[i - 1] == 2 * min_difference)             new_cards.push_back(card[i - 1] + min_difference);       }     }     sort((new_cards).begin(), (new_cards).end());     printf( %d n , new_cards.size());     for (int i = 0; i < new_cards.size(); i++) printf( %d  , new_cards[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   int dmap[45][45];   int ans[45];   int dans[45];   int nans, zans;   void dfs(int dn) {     if (dn == n + 1) {       if (nans > zans) {         zans = nans;       }       return;     }     int mark = 0;     for (int i = 1; i < dn; i++)       if (ans[i] == 1 && dmap[i][dn] == 0) mark = 1;     if (mark == 0) {       ans[dn] = 1;       nans++;       dfs(dn + 1);       nans--;     }     if (nans + n - dn > zans) {       ans[dn] = 0;       dfs(dn + 1);     }     return;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) scanf( %d , &dmap[i][j]);     nans = 0;     zans = 0;     dfs(1);     double zzans = zans;     printf( %lf n , (zzans - 1) / zzans / 2 * m * m);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int x;   int main() {     ios_base ::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> x;     if (x == 3) {       cout << 5 << endl;       return 0;     }     for (int i = 1; i <= 199; i += 2) {       int temp = i * i + 1;       temp /= 2;       if (temp >= x) {         cout << i << endl;         break;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool able[105], prime[105], left[105], flag = 0;   int least = 1;   string tp;   bool isPrime(int i) {     int to = int(sqrt(double(i) + 1.0));     if (i == 1 || i == 2 || i == 5) return 1;     for (int j = 2; j <= to; j++)       if (i % j == 0) return 0;     return 1;   }   int main() {     for (int i = 1; i <= 100; i++)       if (isPrime(i)) prime[i] = 1;     for (int i = 2; i <= 50; i++) {       if (prime[i] == 0) continue;       cout << i << endl;       cin >> tp;       if (tp[0] ==  y ) {         if (flag == 1) {           cout <<  composite  << endl;           fflush(stdout);           return 0;         }         if (i >= 11) {           cout <<  prime  << endl;           fflush(stdout);           return 0;         }         flag = 1;         cout << i * i << endl;         cin >> tp;         if (tp[0] ==  y ) {           cout <<  composite  << endl;           fflush(stdout);           return 0;         }       }     }     cout <<  prime  << endl;     fflush(stdout);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void err(istream_iterator<string> it) {}   template <typename T, typename... Args>   void err(istream_iterator<string> it, T a, Args... args) {     cerr << *it <<   =   << a <<   n ;     err(++it, args...);   }   long long powMod(long long x, long long y) {     long long p = 1;     while (y) {       if (y % 2) {         p = (p * x) % ((long long)1e9 + 7);       }       y /= 2;       x = (x * x) % ((long long)1e9 + 7);     }     return p;   }   long long CpowMod(long long x, long long y, long long w) {     long long p = 1;     while (y) {       if (y % 2) {         p = (p * x) % w;       }       y /= 2;       x = (x * x) % w;     }     return p;   }   long long invMod(long long x) { return powMod(x, ((long long)1e9 + 7) - 2); }   long long CinvMod(long long x, long long w) { return CpowMod(x, w - 2, w); }   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   void solve() {     long long t;     cin >> t;     while (t--) {       long long n;       cin >> n;       vector<long long> a(n, 0);       string s;       cin >> s;       map<long long, set<long long> > m;       map<long long, vector<long long> > v;       long long num = 1;       for (int i = 0; i <= n - 1; i++) {         long long x = s[i] -  0 ;         long long y = 1 - x;         if ((long long)m[y].size()) {           long long in = *m[y].rbegin();           m[y].erase(in);           m[x].insert(in);           v[in].push_back(i);         } else {           m[x].insert(num);           v[num].push_back(i);           num++;         }       }       for (auto &it : v) {         for (auto &is : it.second) {           a[is] = it.first;         }       }       cout << num - 1 <<   n ;       for (auto &it : a) {         cout << it <<    ;       }       cout <<   n ;     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cout.precision(20);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   #pragma comment(linker,  /STACK:102400000,102400000 )   long long cntx, cnty;   int vx[(1000000 + 10)], vy[(1000000 + 10)];   int main() {     int n, m;     scanf( %d%d , &n, &m);     long long ans = (long long)n * n;     cntx = n, cnty = n;     for (int i = 0; i < m; ++i) {       int x, y;       scanf( %d%d , &x, &y);       int e = 0;       if (!vx[x]) {         vx[x] = 1;         ans -= cnty;         cntx--;         e++;       }       if (!vy[y]) {         vy[y] = 1;         ans -= cntx;         cnty--;         e++;       }       cout << ans <<    ;     }     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   vector<unsigned int> vc;   unsigned long long n, T;   void LIS(unsigned int ll, unsigned int hh, unsigned long long &len,            unsigned long long &offset) {     vector<unsigned int> dp_nn;     vector<unsigned int> dp_nn_plus_n;     dp_nn.resize(ll);     dp_nn_plus_n.resize(hh);     for (unsigned int i = 0; i < hh; ++i) dp_nn_plus_n[i] = 1;     for (unsigned int i = 0; i < ll; ++i) dp_nn[i] = 1;     for (unsigned int i = 0; i < hh; ++i) {       for (unsigned int j = 0; j < i; ++j) {         if (vc[i % n] >= vc[j % n]) {           dp_nn_plus_n[i] = max(dp_nn_plus_n[i], dp_nn_plus_n[j] + 1);           if (i < ll) dp_nn[i] = max(dp_nn[i], dp_nn[j] + 1);         }       }     }     sort(dp_nn_plus_n.begin(), dp_nn_plus_n.end());     sort(dp_nn.begin(), dp_nn.end());     unsigned long long h = dp_nn_plus_n[dp_nn_plus_n.size() - 1];     unsigned long long l = dp_nn[dp_nn.size() - 1];     offset = h - l;     len = l;     return;   }   int main() {     cin >> n >> T;     for (unsigned long long i = 0; i < n; ++i) {       unsigned int x;       cin >> x;       vc.push_back(x);     }     unsigned long long mx = max(n, T);     unsigned long long mn = min(n, T);     unsigned long long lim = mn * n + n;     unsigned long long delta = 0;     unsigned long long L = 0;     LIS(mn * n, lim, L, delta);     unsigned long long pad = (mx - n) * delta;     cout << L + pad << endl;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> fail(string s) {     int n = s.size();     vector<int> z(n);     int l = 0, r = 0;     for (int i = 1; i < n; ++i) {       if (i <= r) z[i] = min(z[i - l], r - i + 1);       while (z[i] + i < n && s[z[i] + i] == s[z[i]]) ++z[i];       if (z[i] + i - 1 > r) {         l = i;         r = z[i] + i - 1;       }     }     return z;   }   string s;   vector<int> z;   vector<pair<int, int> > ans;   int A[100005];   int main() {     ios_base::sync_with_stdio(false);     cout.tie(0);     cin.tie(0);     int n, m, i, j, k, x, y;     cin >> s;     z = fail(s);     n = s.size();     z[0] = n;     for (i = 0; i < n; ++i) A[z[i]]++;     for (i = n - 1; i >= 0; --i) A[i] += A[i + 1];     for (i = n - 1; i >= 0; --i) {       if (z[i] == (n - i)) {         ans.push_back(make_pair(z[i], A[z[i]]));       }     }     sort(ans.begin(), ans.end());     cout << ans.size() << endl;     for (auto it : ans) {       cout << it.first <<     << it.second << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     function<long long(long long, long long)> power = [&](long long x,                                                           long long y) {       if (y == 0) {         return 1LL;       } else {         long long z = power(x, y / 2);         return y % 2 == 0 ? (z * z) % 998244353                           : ((z * z) % 998244353 * x) % 998244353;       }     };     auto div = [&](long long x) { return power(x, 998244353 - 2); };     long long n, m;     cin >> n >> m;     vector<bool> a((size_t)n);     vector<long long> w((size_t)n);     for (long long i = 0; i < n; i++) {       long long v;       cin >> v;       a[i] = v == 1;     }     for (long long i = 0; i < n; i++) {       cin >> w[i];     }     long long zeros = 0, ones = 0;     for (long long i = 0; i < n; i++) {       if (a[i]) {         ones += w[i];       } else {         zeros += w[i];       }     }     map<tuple<bool, long long, long long, long long, long long>, long long> dp;     function<long long(bool, long long, long long, long long, long long)> f =         [&](bool q, long long b, long long z, long long o, long long j) {           if (j >= m) {             return b;           }           auto it = dp.find({q, b, z, o, j});           if (it != dp.end()) {             return it->second;           }           long long r = 0;           if (q) {             r += b * f(q, b + 1, z, o, j + 1);             r += z * f(q, b, z - 1, o, j + 1);             r += o * f(q, b, z, o + 1, j + 1);           } else {             r += b * f(q, b - 1, z, o, j + 1);             r += z * f(q, b, z - 1, o, j + 1);             r += o * f(q, b, z, o + 1, j + 1);           }           r %= 998244353;           r *= div(b + z + o);           r %= 998244353;           return dp[{q, b, z, o, j}] = r;         };     for (long long i = 0; i < n; i++) {       cout << f(a[i], w[i], a[i] ? zeros : zeros - w[i],                 a[i] ? ones - w[i] : ones, 0)            << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char buf[1000][1005];   int main() {     int N;     scanf( %d , &N);     int T[N];     pair<int, int> T2[N];     for (int i = 0; i < (N); i++) {       scanf( %d , &T[i]);       T2[i].first = T[i];       T2[i].second = i;     }     sort(T2, T2 + N);     if (T2[0].first == 0) {       printf( NO n );       return 0;     }     for (int i = 0; i < (T2[0].first); i++) buf[0][i] =  0 ;     buf[0][T2[0].first] = 0;     for (int i = 1; i < N; i++) {       for (int j = 0; j < (T2[i - 1].first); j++) buf[i][j] = buf[i - 1][j];       int k = T2[i - 1].first - 1;       while (k >= 0 && buf[i][k] ==  1 ) {         buf[i][k] =  0 ;         k--;       }       if (k < 0) {         printf( NO n );         return 0;       }       buf[i][k] =  1 ;       for (int j = T2[i - 1].first; j < T2[i].first; j++) buf[i][j] =  0 ;       buf[i][T2[i].first] = 0;     }     int rev[N];     for (int i = 0; i < (N); i++) rev[T2[i].second] = i;     printf( YES n );     for (int i = 0; i < (N); i++) printf( %s n , buf[rev[i]]);   }


#include <bits/stdc++.h>   using namespace std;   struct ii {     int a, b;     bool operator<(ii o) const { return make_tuple(a, b) < make_tuple(o.a, o.b); }   };   struct iii {     int a, b, c;     bool operator<(iii o) const {       return make_tuple(a, b, c) < make_tuple(o.a, o.b, o.c);     }   };   int TC, N, M, K, Q, x, y, z;   int arr[35005], par[35005], now[35005], st[4 * 35005], lz[4 * 35005],       dp[55][35005];   void build(int p, int l, int r, int rem) {     if (l == r) {       st[p] = dp[rem][l];       lz[p] = 0;       return;     }     int m = (l + r) >> 1;     build(p << 1, l, m, rem);     build(p << 1 | 1, m + 1, r, rem);     st[p] = max(st[p << 1], st[p << 1 | 1]);     lz[p] = 0;   }   void update(int p, int l, int r, int b1, int b2, int v) {     if (lz[p]) {       st[p] += lz[p];       if (l < r) {         lz[p << 1] += lz[p];         lz[p << 1 | 1] += lz[p];       }       lz[p] = 0;     }     if (b1 <= l && r <= b2) {       st[p] += v;       if (l < r) {         lz[p << 1] += v;         lz[p << 1 | 1] += v;       }       return;     }     if (r < b1 || b2 < l) return;     int m = (l + r) >> 1;     update(p << 1, l, m, b1, b2, v);     update(p << 1 | 1, m + 1, r, b1, b2, v);     st[p] = max(st[p << 1], st[p << 1 | 1]);   }   int query(int p, int l, int r, int b1, int b2) {     if (lz[p]) {       st[p] += lz[p];       if (l < r) {         lz[p << 1] += lz[p];         lz[p << 1 | 1] += lz[p];       }       lz[p] = 0;     }     if (b1 <= l && r <= b2) {       return st[p];     }     if (r < b1 || b2 < l) return 0;     int m = (l + r) >> 1;     return max(query(p << 1, l, m, b1, b2), query(p << 1 | 1, m + 1, r, b1, b2));   }   int main() {     scanf( %d%d , &N, &K);     for (int i = 0; i <= N; i++) now[i] = N;     for (int i = 0; i < N; i++) scanf( %d , &arr[i]);     for (int i = N - 1; i >= 0; i--) {       par[i] = now[arr[i]];       now[arr[i]] = i;     }     for (int rem = 1; rem <= K; rem++) {       build(1, 0, N, rem - 1);       for (int pos = N; pos >= 0; pos--) {         if (pos == N) {           dp[rem][pos] = 0;         } else {           update(1, 0, N, pos + 1, par[pos], 1);           dp[rem][pos] = query(1, 0, N, pos + 1, N - rem + 1);         }       }     }     printf( %d n , dp[K][0]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int key[300];   int main() {     int n, k, x, i;     scanf( %d%d , &n, &k);     memset(key, -1, sizeof key);     while (n--) {       scanf( %d , &x);       if (key[x] == -1) {         for (i = x - 1; i >= 0 && key[i] == -1 && x - i < k; i--)           ;         if (i >= 0 && x - i < k && x - key[i] < k)           key[x] = key[i];         else {           i++;           key[i] = key[x] = i;         }       }       printf( %d%c , key[x], n ?     :   n );     }   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC target( sse4 )   using namespace std;   template <class T>   bool ckmin(T& a, const T& b) {     return b < a ? a = b, 1 : 0;   }   template <class T>   bool ckmax(T& a, const T& b) {     return a < b ? a = b, 1 : 0;   }   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   const int MOD = 1000000007;   const char nl =   n ;   const int MX = 100001;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int T;     cin >> T;     while (T--) {       int N;       cin >> N;       int A[N], B[N];       for (int i = 0; i < (N); i++) cin >> A[i];       for (int i = 0; i < (N); i++) cin >> B[i];       int S = 0;       int last = B[0];       for (int i = 1; i < (4 * N + 5); i++) {         int cur = B[(i + N - 1) % N] - last;         last = A[i % N] - cur;         ckmax(last, 0);         if (last > B[i % N]) {           S = i;           last = B[i % N];         }       }       if (S < 2 * N) {         cout <<  YES  << nl;       } else {         cout <<  NO  << nl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int a[210000];   void work() {     int cnt[33];     memset((cnt), (0), sizeof(cnt));     int xorsum = 0;     cin >> n;     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);       xorsum ^= a[i];       for (int j = 0; j <= 30; j++) cnt[j] += (a[i] >> j) & 1;     }     for (int i = 30; i >= 0; i--) {       if ((xorsum >> i) & 1) {         if ((cnt[i] - 1) % 4 == 0) {           printf( WIN n );           return;         } else {           if (n & 1) {             printf( LOSE n );             return;           } else {             printf( WIN n );             return;           }         }       }     }     printf( DRAW n );   }   int main() {     int t;     cin >> t;     while (t--) {       work();     }     return ~~(0 ^ 0 ^ 0);   }


#include <bits/stdc++.h>   using namespace std;   const int maxl = 2e5 + 10;   int n, m, cnt, tot, cas, ans;   int a[maxl], b[maxl], num[maxl * 32];   int ind[maxl * 32], sz[maxl * 32], tr[maxl * 32][2];   bool vis[maxl], pr[maxl];   char s[maxl];   vector<int> e[maxl];   inline void prework() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       int u = 0, c;       for (int j = 30; j >= 0; j--) {         c = (a[i] >> j) & 1;         if (!tr[u][c]) tr[u][c] = ++tot;         u = tr[u][c];         ++num[u];       }       ind[u] = i;     }   }   inline int dfs(int u) {     if (!tr[u][0] && !tr[u][1]) return 1;     int l = 0, r = 0;     if (tr[u][0]) l = dfs(tr[u][0]);     if (tr[u][1]) r = dfs(tr[u][1]);     if (!tr[u][1]) return l;     if (!tr[u][0]) return r;     return max(l, r) + 1;   }   inline void mainwork() { ans = n - dfs(0); }   inline void print() { printf( %d n , ans); }   int main() {     int t = 1;     for (cas = 1; cas <= t; cas++) {       prework();       mainwork();       print();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long mod = 1000000007;   long long power(long long n, long long k) {     long long ans = 1;     while (k) {       if (k & 1) ans = (ans * n) % mod;       k /= 2;       n = (n * n) % mod;     }     return ans;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     int ar[n];     for (int i = 0; i < int(n); i++) cin >> ar[i];     int l[n], r[n], m[n];     l[0] = 1;     for (int i = 1; i < int(n); i++) l[i] = min(ar[i], l[i - 1] + 1);     r[n - 1] = 1;     for (int i = n - 2; i > -1; i--) r[i] = min(r[i + 1] + 1, ar[i]);     int ans = 0;     for (int i = 0; i < int(n); i++) {       m[i] = min(l[i], r[i]);       ans = max(ans, m[i]);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int i, j, k, n;     cin >> n;     vector<long double> probs(n);     for (i = 0; i < n; i++) {       cin >> probs[i];     }     sort(probs.begin(), probs.end());     reverse(probs.begin(), probs.end());     long double prob0 = 1;     long double prob1 = 0.;     for (i = 0; i < n; i++) {       long double p = prob1;       p = probs[i] * prob0 + p * (1 - probs[i]);       if (p > prob1) {         prob1 = p;         prob0 = (1 - probs[i]) * prob0;       }     }     printf( %.9lf , double(prob1 + 0.0000000001));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   const long long inf = 10000000;   inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }   inline bool isLeapYear(long long year) {     return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);   }   inline void normal(long long &a) {     a %= MOD;     (a < 0) && (a += MOD);   }   inline long long modMul(long long a, long long b) {     a %= MOD, b %= MOD;     normal(a), normal(b);     return (a * b) % MOD;   }   inline long long modAdd(long long a, long long b) {     a %= MOD, b %= MOD;     normal(a), normal(b);     return (a + b) % MOD;   }   inline long long modSub(long long a, long long b) {     a %= MOD, b %= MOD;     normal(a), normal(b);     a -= b;     normal(a);     return a;   }   inline long long modPow(long long b, long long p) {     long long r = 1;     while (p) {       if (p & 1) r = modMul(r, b);       b = modMul(b, b);       p >>= 1;     }     return r;   }   inline long long modInverse(long long a) { return modPow(a, MOD - 2); }   inline long long modDiv(long long a, long long b) {     return modMul(a, modInverse(b));   }   inline bool checkBit(long long n, int i) { return n & (1LL << i); }   inline long long setBit(long long n, int i) {     return n | (1LL << i);     ;   }   inline long long resetBit(long long n, int i) { return n & (~(1LL << i)); }   int dx[] = {0, 0, 1, -1};   int dy[] = {1, -1, 0, 0};   void input(long long n, const long long a[], int co = 0) {     long long i = 1;     for (; i <= n; i++) {       if (co == 0)         scanf( %lld , &a[i]);       else         printf( %lld  , a[i]);     }   }   int n, k;   int dp[55][2], a[55], sm;   int dfs(int ps, int k) {     if (ps == n) return 0;     int &rs = dp[ps][k];     if (rs != -1) return rs;     rs = 0;     if (k == 0)       rs = max(a[ps] + dfs(ps + 1, 1), dfs(ps + 1, 0));     else       rs = min(a[ps] + dfs(ps + 1, 1), dfs(ps + 1, 0));     return rs;   }   bool cmp() {}   void solve() {     memset(dp, -1, sizeof dp);     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);       sm += a[i];     }     int rs = dfs(0, 0);     printf( %d %d n , sm - rs, rs);   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long val;   int visid;   const int mod = 1e9 + 7;   long long dp[60][10005];   long long Inv[100];   int vis[60][10005];   long long fp(long long b, long long p) {     if (p == 0) return 1;     long long tmp = fp(b, p / 2);     if (p % 2) return (((tmp * tmp) % mod) * b) % mod;     return (tmp * tmp) % mod;   }   long long solve(int cur, int k) {     if (k == 0) return fp(val, cur);     long long &ret = dp[cur][k];     if (vis[cur][k] == visid) return ret;     vis[cur][k] = visid;     ret = 0;     for (int i = 0; i <= cur; i++) {       ret += solve(i, k - 1) * Inv[cur + 1] % mod;       ret %= mod;     }     return ret;   }   long long n, ans = 1;   int k;   void go(long long x) {     int cnt = 0;     while (n % x == 0) {       cnt++;       n /= x;     }     visid++;     val = x;     ans *= solve(cnt, k);     ans %= mod;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     for (int i = 0; i <= 60; i++) Inv[i] = fp(i, mod - 2);     cin >> n >> k;     long long x = 2;     while (x * x <= n) {       if (n % x == 0) go(x);       x++;     }     if (n != 1) go(n);     cout << ans << endl;     return 0;   }