#include <bits/stdc++.h>   inline int read() {     static char ch;     while (ch = getchar(), ch <  0  || ch >  9 )       ;     int res = ch - 48;     while (ch = getchar(), ch >=  0  && ch <=  9 ) res = res * 10 + ch - 48;     return res;   }   inline unsigned int Rand() {     static unsigned int x = 998666131;     return x += x << 2 | 1;   }   const int N = 5001, M = 1e5 + 5, T = 2e6;   int n, m, Q, ans[M];   bool vis[M];   struct QueryT {     int op, x, y, dir, len, lab;     inline void Reset1(const int &_op, const int &_dir, const int &_x,                        const int &_y, const int &_len) {       op = _op;       dir = _dir;       x = _x;       y = _y;       len = _len;     }     inline void Reset2(const int &_op, const int &_dir, const int &_x,                        const int &_y, const int &_lab) {       op = _op;       dir = _dir;       x = _x;       y = _y;       lab = _lab;       len = x + y;     }     inline bool operator<=(const QueryT &rhs) {       if (x != rhs.x) return x < rhs.x;       return op <= rhs.op;     }   } qry[M * 4], tmp[M * 4];   struct node {     node *lc, *rc;     int val, sze;     unsigned int pri;     inline void Update() { sze = (lc ? lc->sze : 0) + (rc ? rc->sze : 0) + 1; }   } used[T], *pool = used, *unused[T], **top = unused;   node *rt[5][N];   inline node *new_node() {     node *res;     if (top != unused)       res = *--top;     else       res = pool++;     res->lc = res->rc = NULL;     return res;   }   inline void del_node(node *&u) {     if (!u) return;     del_node(u->lc);     del_node(u->rc);     *top++ = u;     u = NULL;   }   inline void Zig(node *&u) {     node *v = u->lc;     u->lc = v->rc;     v->rc = u;     v->sze = u->sze;     u->Update();     u = v;   }   inline void Zag(node *&u) {     node *v = u->rc;     u->rc = v->lc;     v->lc = u;     v->sze = u->sze;     u->Update();     u = v;   }   inline void Insert(node *&u, const int &val) {     if (!u) {       u = new_node();       u->val = val;       u->sze = 1;       u->pri = Rand();       return;     }     ++u->sze;     if (val <= u->val) {       Insert(u->lc, val);       if (u->lc->pri < u->pri) Zig(u);     } else {       Insert(u->rc, val);       if (u->rc->pri < u->pri) Zag(u);     }   }   inline int moreQuery(node *u, const int &val) {     if (!u) return 0;     if (u->val < val) return moreQuery(u->rc, val);     return (u->rc ? u->rc->sze + 1 : 1) + moreQuery(u->lc, val);   }   inline void add(node **R, int x, const int &val) {     for (; x <= n; x += x & -x) Insert(R[x], val);   }   inline int Query(node **R, int x, const int &val) {     int res = 0;     for (; x > 0; x -= x & -x) res += moreQuery(R[x], val);     return res;   }   inline void Clear(node **R, int x) {     for (; x <= n; x += x & -x) del_node(R[x]);   }   inline void cdqSolve(const int &l, const int &r) {     if (l == r) return;     int mid = l + r >> 1;     cdqSolve(l, mid);     cdqSolve(mid + 1, r);     int ql = l, qr = mid + 1;     for (int i = (l); i <= (r); ++i) {       if (qr > r || (ql <= mid && qry[ql] <= qry[qr])) {         tmp[i] = qry[ql++];         if (tmp[i].op == 1) add(rt[tmp[i].dir], tmp[i].y, tmp[i].len);       } else {         tmp[i] = qry[qr++];         if (tmp[i].op == 2)           ans[tmp[i].lab] += Query(rt[tmp[i].dir], tmp[i].y, tmp[i].len);       }     }     for (int i = (l); i <= (r); ++i) {       qry[i] = tmp[i];       if (qry[i].op == 1) Clear(rt[qry[i].dir], qry[i].y);     }   }   int main() {     n = read();     Q = read();     int op, x, y, len, dir;     for (int i = (1); i <= (Q); ++i) {       op = read();       if (op == 1) {         dir = read(), x = read(), y = read(), len = read();         if (dir == 2) y = n - y + 1;         if (dir == 3) x = n - x + 1;         if (dir == 4) x = n - x + 1, y = n - y + 1;         len += x + y;         qry[++m].Reset1(op, dir, x, y, len);       } else {         x = read(), y = read();         qry[++m].Reset2(op, 1, x, y, i);         qry[++m].Reset2(op, 2, x, n - y + 1, i);         qry[++m].Reset2(op, 3, n - x + 1, y, i);         qry[++m].Reset2(op, 4, n - x + 1, n - y + 1, i);         vis[i] = true;       }     }     cdqSolve(1, m);     for (int i = (1); i <= (Q); ++i)       if (vis[i]) printf( %d n , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7, A = 1e5 + 2;   bool v[20][20];   long long dp[20][1 << 20];   int n, m, start = 0;   long long solve(int node, int mask) {     if (~dp[node][mask]) return dp[node][mask];     long long ret = 0;     if (v[node][start] && __builtin_popcount(mask) > 2) ret++;     for (int i = start + 1; i < n; i++) {       if (!(mask >> i & 1) && v[node][i]) ret += solve(i, mask | (1 << i));     }     return dp[node][mask] = ret;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> n >> m;     for (int i = 0; i < m; i++) {       int x, y;       cin >> x >> y;       x--, y--;       v[x][y] = v[y][x] = 1;     }     long long ans = 0;     memset(dp, -1, sizeof dp);     for (int i = 0; i < n; i++) start = i, ans += solve(i, 1 << i);     cout << ans / 2;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 105;   int n, m, q, t, r, c, x;   int R[N], C[N], ans[N][N];   pair<int, pair<int, int> > arr[N][N], temp[N];   int main() {     cin >> n >> m >> q;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         arr[i][j].second.first = i;         arr[i][j].second.second = j;       }     while (q--) {       scanf( %d , &t);       if (t == 1) {         scanf( %d , &r);         r--;         for (int i = 0; i < m; i++) temp[(i - 1 + m) % m] = arr[r][i];         for (int i = 0; i < m; i++) arr[r][i] = temp[i];       }       if (t == 2) {         scanf( %d , &c);         c--;         for (int i = 0; i < n; i++) temp[(i - 1 + n) % n] = arr[i][c];         for (int i = 0; i < n; i++) arr[i][c] = temp[i];       }       if (t == 3) {         scanf( %d %d %d , &r, &c, &x);         r--;         c--;         arr[r][c].first = x;       }     }     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++)         ans[arr[i][j].second.first][arr[i][j].second.second] = arr[i][j].first;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) printf( %d  , ans[i][j]);       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e9 + 7;   const int N = 1e5 + 10;   vector<int> v[N];   void init() {     int tot = 0;     for (int i = 1; i < N; i++) {       for (int j = i; j < N; j += i) {         v[j].push_back(i);       }       tot = max(tot, (int)v[i].size());     }   }   int C(int n, int m) {     int U = 1, D = 1;     while (m) {       U *= n++;       D *= m--;     }     return U / D;   }   void solve(int n) {     int ret = 0;     for (int i = 1; i <= n; i++) {       for (int j = i; j <= n; j++) {         ret++;       }     }     cout << n <<     << ret << endl;   }   int main() {     map<tuple<int, int, int>, int> mp;     for (int i = 1; i < 8; i++) {       if (((i >> 0) & 1) == 0) continue;       for (int j = 1; j < 8; j++) {         if (((j >> 1) & 1) == 0) continue;         for (int k = 1; k < 8; k++) {           if (((k >> 2) & 1) == 0) continue;           int a = i, b = j, c = k;           if (a > b) swap(a, b);           if (b > c) swap(b, c);           if (a > b) swap(a, b);           mp[{a, b, c}] = 1;         }       }     }     init();     int ncase;     scanf( %d , &ncase);     while (ncase--) {       map<int, int> mc;       int a, b, c;       scanf( %d%d%d , &a, &b, &c);       vector<int> v1, v2;       merge(v[a].begin(), v[a].end(), v[b].begin(), v[b].end(),             back_inserter(v1));       merge(v[c].begin(), v[c].end(), v1.begin(), v1.end(), back_inserter(v2));       v2.erase(unique(v2.begin(), v2.end()), v2.end());       int sz[8];       memset((sz), (0), sizeof(sz));       for (auto &x : v2) {         int f = 0;         if (a % x == 0) f |= 1;         if (b % x == 0) f |= 2;         if (c % x == 0) f |= 4;         sz[f]++;       }       int ans = 0;       for (auto &t : mp) {         int a, b, c;         tie(a, b, c) = t.first;         if (a == c)           ans += C(sz[a], 3);         else if (a == b)           ans += C(sz[a], 2) * sz[c];         else if (b == c)           ans += C(sz[b], 2) * sz[a];         else           ans += sz[a] * sz[b] * sz[c];       }       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 2e5 + 5;   int n, a[MAX], bk[3];   int ans[MAX];   int main() {     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i], bk[a[i]]++;     sort(a + 1, a + n + 1, greater<int>());     int cur = 0, top = 0, tar;     for (int i = 1; i <= n; i++) {       cur += a[i];       bk[a[i]]--;       ans[++top] = a[i];       if (cur == 2) {         tar = i;         break;       }     }     if (bk[1] > 0) bk[1]--, cur += 1, ans[++top] = 1, n--;     for (int i = tar + 1; i <= n; i++) {       cur += a[i];       bk[a[i]]--;       ans[++top] = a[i];     }     for (int i = 1; i <= top; i++) printf( %d%c , ans[i], i == top ?   n  :    );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T, class U>   ostream& operator<<(ostream& os, const pair<T, U>& p) {     os <<  (  << p.first <<  ,   << p.second <<  ) ;     return os;   }   template <class T>   ostream& operator<<(ostream& os, const vector<T>& v) {     os <<  { ;     for (int i = 0; i < (int)v.size(); i++) {       if (i) os <<  ,  ;       os << v[i];     }     os <<  } ;     return os;   }   template <typename Arg1>   void __f(const char* name, Arg1&& arg1) {     cerr << name <<   :   << arg1 << std::endl;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     cerr.write(names, comma - names) <<   :   << arg1 <<   |  ;     __f(comma + 1, args...);   }   const int mod = 1e9 + 7;   const int N = 2005;   inline int add(int x, int y) {     x += y;     if (x >= mod) x -= mod;     return x;   }   inline int sub(int x, int y) {     x -= y;     if (x < 0) x += mod;     return x;   }   inline int mul(int x, int y) { return (x * 1ll * y) % mod; }   inline int powr(int a, long long b) {     int x = 1 % mod;     while (b) {       if (b & 1) x = mul(x, a);       a = mul(a, a);       b >>= 1;     }     return x;   }   inline int inv(int a) { return powr(a, mod - 2); }   char s[N][N];   int dp[2][N][N], dp2[2][N][N];   int num[2][N][N];   int main() {     int n = 2000, m = 2000;     scanf( %d , &(n)), scanf( %d , &(m));     for (int i = 1; i <= n; i++) {       scanf( %s , s[i] + 1);     }     for (int i = n; i >= 1; i--) {       for (int j = m; j >= 1; j--) {         num[0][i][j] = num[0][i][j + 1] + (s[i][j] ==  R );         num[1][i][j] = num[1][i + 1][j] + (s[i][j] ==  R );         if (i == n) dp[0][i][j] = (num[0][i][j] == 0);         if (j == m) dp[1][i][j] = (num[1][i][j] == 0);         int mxj = m - num[0][i][j];         int mxi = n - num[1][i][j];         if (i != n) dp[0][i][j] = sub(dp2[1][i + 1][j], dp2[1][i + 1][mxj + 1]);         if (j != m) dp[1][i][j] = sub(dp2[0][i][j + 1], dp2[0][mxi + 1][j + 1]);         dp2[0][i][j] = add(dp[0][i][j], dp2[0][i + 1][j]);         dp2[1][i][j] = add(dp[1][i][j], dp2[1][i][j + 1]);       }     }     assert(dp[0][1][1] == dp[1][1][1]);     printf( %d n , dp[0][1][1]);   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   char mat[100][100];   char c;   vector<int> us;   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) cin >> mat[i];     for (int i = 0; i < n; i++) {       us.clear();       for (int j = 0; j < m; j++) {         if (mat[i][j] ==  # ) us.push_back(j);       }       if (us.size() > 0) {         for (int j = 0; j < n; j++) {           c = mat[j][us[0]];           for (int k = 1; k < (int)us.size(); k++) {             if (mat[j][us[k]] != c) {               cout <<  No ;               return 0;             }           }         }       }     }     for (int i = 0; i < m; i++) {       us.clear();       for (int j = 0; j < n; j++) {         if (mat[j][i] ==  # ) us.push_back(j);       }       if (us.size() > 0) {         for (int j = 0; j < m; j++) {           c = mat[us[0]][j];           for (int k = 1; k < (int)us.size(); k++) {             if (mat[us[k]][j] != c) {               cout <<  No ;               return 0;             }           }         }       }     }     cout <<  Yes ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   priority_queue<long long int> dif;   vector<long long int> vec;   long long int a[1000000], b[1000000];   int main() {     long long int x, d, contador = 0, i;     long long int pot, exp;     cin >> x >> d;     while (x > 0) {       x++;       pot = 1;       exp = 0;       while (pot <= x) {         pot = pot * 2;         exp++;       }       for (i = 0; i < exp - 1; i++) {         vec.push_back(d * contador + 1);       }       contador++;       x = x - pot / 2;     }     cout << vec.size() <<   n ;     for (i = 0; i < vec.size(); i++) cout << vec[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   const long long pw10[] = {1, 10, 100, 1000, 10000, 100000};   long long K, F[7], Q, dp[7][2000000];   signed main() {     cin >> K;     for (long long i = 1; i <= 6; ++i) cin >> F[i];     memset(dp, -0x3f, sizeof(dp));     dp[0][0] = 0;     for (long long i = 1; i <= 6; ++i) {       for (long long j = 0; j < 3 * pw10[i - 1]; ++j) {         deque<pair<long long, long long> > q;         for (long long k = j, cnt = 0; k < 1e6; k += 3 * pw10[i - 1], ++cnt) {           long long val = dp[i - 1][k] - cnt * F[i];           while (q.size() && q.back().first <= val) q.pop_back();           q.push_back(make_pair(val, cnt));           if (cnt - q.front().second > 3 * K) q.pop_front();           dp[i][k] = max(dp[i][k], cnt * F[i] + q.front().first);           long long pos = 0;           while (cnt - q[pos].second > 3 * (K - 1)) ++pos;           long long v = cnt * F[i] + q[pos].first;           for (long long t = 0; t < 10; ++t)             dp[i][k + t * pw10[i - 1]] = max(dp[i][k + t * pw10[i - 1]], v);         }       }     }     for (cin >> Q; Q; --Q) {       long long x;       cin >> x;       cout << max(0ll, dp[6][x]) <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 500000 + 5;   long long a[maxn], tmp[maxn];   multiset<long long> save;   vector<pair<long long, long long> > b;   int main() {     long long total = 0;     long long n;     scanf( %lld , &n);     for (long long i = 0; i < n; i++) scanf( %lld , a + i), total += a[i];     sort(a, a + n, greater<long long>());     for (long long i = 0, cnt = 1; i < n; i++, cnt++)       if (i == n - 1 || a[i] != a[i + 1])         b.push_back(make_pair(a[i], cnt)), cnt = 0;     for (long long i = 0, sizesum = 0; i < (long long)b.size(); i++) {       long long num = b[i].first, nowsize = b[i].second;       long long save_size = min(sizesum, sizesum + nowsize >> 1);       long long cnt = max(0LL, save_size - nowsize);       for (long long j = save_size - 1; j >= cnt; j--)         if (j < (long long)save.size())           tmp[j] = *save.begin(), save.erase(save.begin());         else           tmp[j] = 0;       for (long long j = cnt, k = sizesum - cnt; j < k && j < save_size; j++)         if (tmp[j] < num)           tmp[j] = num;         else if (--k < save_size)           tmp[k] = max(0LL, 2LL * num - tmp[j]);       save.insert(tmp + cnt, tmp + save_size);       sizesum += nowsize;     }     for (auto it = save.begin(); it != save.end(); it++) total -= (*it);     printf( %lld n , total);   }


#include <bits/stdc++.h>   using namespace std;   long long int b[101];   int main() {     long long int n;     cin >> n;     long long int a[n];     for (int i = 0; i < n; i++) {       cin >> a[i];       b[a[i]]++;     }     long long int ans = 0;     for (int i = 1; i < 101; i++) {       if (b[i] >= ans) {         ans = b[i];       }     }     cout << ans << endl;     return 0;   }


#include <stdio.h>   #include <vector>   #include <algorithm>   #include <set>   #define pii pair<int,int>   #define ll long long   using namespace std;   vector<int> edge[200001];   vector<pii > pt[200001];   int depth[200001];   ll d[200001];   int a[200001],v[200001];   void dfs(int x) {    for (auto nxt : edge[x]) {     depth[nxt] = depth[x] + 1;     dfs(nxt);    }   }   int main() {    int t;    for (scanf( %d , &t); t--;) {     int i, n;     for (scanf( %d , &n), i = 1; i <= n; ++i) {      edge[i].clear();      pt[i].clear();      d[i] = 0;     }     for (i = 2; i <= n; ++i) {      scanf( %d ,v+i);      edge[v[i]].push_back(i);     }     depth[1] = 1;     dfs(1);     for (i = 2; i <= n; ++i) {      scanf( %d ,a+i);      pt[depth[i]].push_back({a[i],i});     }     pt[1].push_back({0,1});     for (i = 1; i <= n; ++i)      sort(pt[i].begin(), pt[i].end());     int lev = 2;     while (pt[lev].size()) {      multiset < ll > l, r;      for (auto p : pt[lev]) {       int x = p.second;       r.insert(d[v[x]]+a[x]);      }      for (auto p : pt[lev]) {       int x = p.second;       auto it = r.lower_bound(d[v[x]]+a[x]);       r.erase(it);          if (r.size()) {        auto mx = prev(r.end());        d[x] = max(d[x], *mx - a[x]);        int rightmost = pt[lev].back().first;        d[x] = max(d[x], d[v[x]] + rightmost - a[x]);       }       if (l.size()) {        auto mx = prev(l.end());        d[x] = max(d[x], a[x] + *mx);        int leftmost = pt[lev][0].first;        d[x] = max(d[x], d[v[x]] + a[x] - leftmost);       }       l.insert({d[v[x]]-a[x]});      }      ++lev;     }        ll ans = 0;     for (i = 1; i <= n; ++i) {      if (ans < d[i]) ans = d[i];     }     printf( %I64d n ,ans);    }    return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, res1, res2;     cin >> n;     vector<int> a(n);     for (int i = 0; i < n; i++) cin >> a[i];     res1 = a[0] ^ a[1];     vector<int> st;     for (int i = 0; i < n; i++) {       while (!st.empty() && st.back() < a[i]) st.pop_back();       st.push_back(a[i]);       if (st.size() >= 2) res1 = max(res1, st[st.size() - 1] ^ st[st.size() - 2]);     }     res2 = a[n - 1] ^ a[n - 2];     vector<int> st1;     for (int i = n - 1; i >= 0; i--) {       while (!st1.empty() && st1.back() < a[i]) st1.pop_back();       st1.push_back(a[i]);       if (st1.size() >= 2)         res2 = max(res2, st1[st1.size() - 1] ^ st1[st1.size() - 2]);     }     cout << max(res1, res2);   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC optimize( unroll-loops )   #pragma GCC target( sse2 )   using namespace std;   template <typename Arg1>   void __f(const char* name, Arg1&& arg1) {     cout << name <<   :   << arg1 <<   n ;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     cout.write(names, comma - names) <<   :   << arg1 <<   |  ;     __f(comma + 1, args...);   }   void solution() {     int n;     cin >> n;     vector<long long> arr(n);     map<long long, long long> cnt;     map<long long, long long> freq;     for (int i = 0; i < n; i++) cin >> arr[i];     long long ans = 1;     for (int i = 1; i < n + 1; i++) {       cnt[arr[i - 1]]++;       freq[cnt[arr[i - 1]]]++;       long long x = cnt[arr[i - 1]];       long long y = freq[cnt[arr[i - 1]]];       if (x * y == i && i < n)         ans = i + 1;       else if (x * y == i - 1)         ans = i;     }     cout << ans <<   n ;   }   signed main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t = 1;     for (int i = 1; i < t + 1; i++) {       solution();     }   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int N, K;     cin >> N >> K;     set<int> s;     for (int i = 0; i < N; i++) {       int a;       cin >> a;       s.insert(a);     }     if (s.size() > K) {       cout << -1 << endl;       return;     }     cout << N * K << endl;     for (int i = 0; i < N; i++) {       for (int b : s) cout << b <<    ;       for (int j = 0; j < K - (int)s.size(); j++) cout << 1 <<    ;     }     cout << endl;   }   int main() {     int t;     cin >> t;     while (t--) solve();   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   int a[100005];   int l[1000], r[1000];   vector<int> v[100000];   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int i = 1; i <= m; i++) {       int s, t;       scanf( %d%d , &s, &t);       l[i] = s, r[i] = t;     }     int ans = -100000000, anss = 0;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         int mi = a[i], ma = a[j];         for (int k = 1; k <= m; k++) {           if (l[k] <= i && i <= r[k] && (l[k] > j || r[k] < j))             mi--, v[(i - 1) * 300 + j].push_back(k);         }         if (ans < ma - mi) {           ans = ma - mi;           anss = (i - 1) * 300 + j;         }       }     }     printf( %d n , ans);     printf( %d n , v[anss].size());     for (int i = 0; i < v[anss].size(); i++) printf( %d  , v[anss][i]);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long power(long long b, long long e, long long m) {     long long p = 1;     while (e > 0) {       if (e & 1) p = (p * b) % m;       e = e >> 1;       b = (b * b) % m;     }     return p;   }   long long cnt[16] = {1, 0, 0, 0, 1, 0, 1, 0, 2, 1, 1, 2, 0, 1, 0, 0};   long long getAns(long long n) {     long long ans = 0;     if (n == 0) return cnt[0];     while (n) {       ans += cnt[n % 16];       n /= 16;     }     return ans;   }   int main() {     long long n, i = 0, j, m, h, h1, h2, q, k, l, r, ans;     scanf( %lld , &n);     printf( %lld n , getAns(n));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int sum[705][705], a[500100];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int q, t, x, y;     memset(sum, 0, sizeof(sum));     cin >> q;     while (q--) {       cin >> t >> x >> y;       if (t == 1) {         a[x] += y;         for (int i = 1; i < 705; ++i) {           sum[i][x % i] += y;         }       } else {         if (x < 705) {           cout << sum[x][y] << endl;         } else {           int ans = 0;           for (int i = y; i < 500100; i += x) ans += a[i];           cout << ans << endl;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   const int N = 300001;   int dp[82][82][82][82], n, k, m, u, v, c;   vector<pair<int, int> > g[83];   int calc(int idx, int l, int r, int rem) {     if (rem == 0) return 0;     if (l > r) return 1e9;     int &ret = dp[idx][l][r][rem];     if (ret != -1) return ret;     ret = 1e9;     for (int i = 0; i < g[idx].size(); ++i) {       int v = g[idx][i].first;       int cost = g[idx][i].second;       if (v < l) continue;       if (v > r) continue;       ret = min(ret, cost + calc(v, v + 1, r, rem - 1));       ret = min(ret, cost + calc(v, l, v - 1, rem - 1));     }     return ret;   }   int main() {     memset(dp, -1, sizeof dp);     scanf( %d%d , &n, &k);     scanf( %d , &m);     for (int i = 0; i < m; ++i) {       scanf( %d%d%d , &u, &v, &c);       if (u == v) continue;       g[u].push_back(make_pair(v, c));     }     int ret = 1e9;     for (int i = 1; i <= n; ++i) {       ret = min(ret, calc(i, i + 1, n, k - 1));       ret = min(ret, calc(i, 1, i - 1, k - 1));     }     if (ret == 1e9) ret = -1;     printf( %d n , ret);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n, m;     cin >> n >> m;     vector<pair<string, int>> v(n);     for (int i = 0; i < n; i++) {       string s;       cin >> s;       v[i].first = s;       v[i].second = i + 1;     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (j % 2 == 1) {           v[i].first[j] = (char)( Z  - v[i].first[j]);         }       }     }     sort(v.begin(), v.end());     for (int i = 0; i < n; i++) {       cout << v[i].second <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long FastExp(long long a, long long b, long long c) {     if (b == 1) return a;     long long ans = FastExp(a, b / 2, c);     if (b % 2 == 0) return ans % c * ans % c;     if (b % 2 != 0) return ans % c * ans % c * a % c;   }   int main() {     int n;     cin >> n;     int a[n + 5];     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     for (int i = 1; i <= n; i++) {       if (i == a[a[a[i]]]) {         cout <<  YES  << endl;         return 0;       }     }     cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int mas[15] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47};     int m, n;     bool ans = false;     cin >> n >> m;     for (int i = 0; i < 15; i++) {       if (mas[i] == n) {         if (mas[i + 1] == m) {           ans = true;           break;         }       }     }     if (ans)       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std::chrono;   using namespace std;   const int mod = 1e9 + 7;   string s;   int op[501], cl[501];   int sop[501], scl[501];   int n;   void f() {     memset(op, 0, sizeof(op));     memset(cl, 0, sizeof(cl));     if (s[0] ==  ( )       op[0]++;     else       cl[0]++;     memset(sop, 0, sizeof(sop));     memset(scl, 0, sizeof(scl));     if (s[n - 1] ==  ( )       sop[n - 1]++;     else       scl[n - 1]++;     for (int i = n - 2; i >= 0; --i) {       if (s[i] ==  ) ) {         sop[i] = sop[i + 1];         scl[i] = scl[i + 1] + 1;       } else {         if (scl[i + 1]) {           sop[i] = sop[i + 1];           scl[i] = scl[i + 1] - 1;         } else {           sop[i] = sop[i + 1] + 1;           scl[i] = scl[i + 1];         }       }     }     for (int i = 1; i < n; ++i) {       if (s[i] ==  ( ) {         op[i] = op[i - 1] + 1;         cl[i] = cl[i - 1];       } else {         if (op[i - 1]) {           op[i] = op[i - 1] - 1;           cl[i] = cl[i - 1];         } else {           op[i] = op[i - 1];           cl[i] = cl[i - 1] + 1;         }       }     }   }   void solve() {     cin >> n;     cin >> s;     int x1 = 1, x2 = 1, mx = 0, c = 0;     for (int i = 0; i < n; ++i) {       for (int j = i; j < n; ++j) {         int c = 0;         swap(s[i], s[j]);         f();         for (int k = 0; k < n; ++k) {           int open;           open = 0;           if (k + 1 < n) open = sop[k + 1];           if (cl[k] == open && op[k] == 0) {             if (k + 1 < n) {               if (scl[k + 1] == 0) c++;             } else               c++;           }         }         swap(s[i], s[j]);         if (mx < c) {           mx = c;           x1 = i + 1;           x2 = j + 1;         }       }     }     cout << mx << endl;     cout << x1 <<     << x2;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t;     t = 1;     while (t--) solve();   }


#include <bits/stdc++.h>   using namespace std;   int n;   int a[100100];   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     set<int> s;     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 1; i < n; i++) s.insert(a[i] - a[i - 1]);     if (s.size() == 1)       cout << a[n - 1] + *s.begin() << endl;     else       cout << a[n - 1] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> g[500000];   bool visited[500000];   int n;   unordered_map<int, int> m, p;   vector<pair<int, int> > v;   void check(int u) {     visited[u] = true;     for (auto v : g[u]) {       if (!visited[v]) check(v);     }   }   int main() {     cin >> n;     set<int> s;     for (int i = 0; i < n; i++) {       int a, b;       cin >> a >> b;       m[a] = b;       v.push_back({a, 0});       v.push_back({b, 1});       p[a] = i;       p[b] = i;     }     sort(v.begin(), v.end());     int count = 0;     for (int i = 0; i < v.size(); i++) {       if (v[i].second == 0) {         for (auto ele : s) {           if (ele < m[v[i].first]) {             count++;             g[p[v[i].first]].push_back(p[ele]);             g[p[ele]].push_back(p[v[i].first]);           } else             break;         }         if (count > n - 1) {           break;         }         s.insert(m[v[i].first]);       } else {         s.erase(v[i].first);       }     }     if (count == n - 1) {       check(0);       int i;       for (i = 0; i < n; i++) {         if (!visited[i]) break;       }       if (i == n)         cout <<  YES  << endl;       else         cout <<  NO  << endl;     } else       cout <<  NO  << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 200005;   struct ppi {     long long max;     int id;     int w;   } pp1;   struct pi {     int le;     int ri;     int max;     int id;   } pp[maxn << 2];   void build(int tot, int le, int ri) {     pp[tot].le = le;     pp[tot].ri = ri;     pp[tot].max = -1000000001;     if (le == ri) return;     build(2 * tot, le, (le + ri) / 2);     build(2 * tot + 1, (le + ri) / 2 + 1, ri);   }   void merg(int tot, int p, int k, int id) {     if (pp[tot].le == pp[tot].ri) {       if (pp[tot].max < k) {         pp[tot].max = k;         pp[tot].id = id;       }       return;     }     int mid = (pp[tot].le + pp[tot].ri) / 2;     if (p <= mid) {       merg(2 * tot, p, k, id);     } else       merg(2 * tot + 1, p, k, id);     if (pp[2 * tot].max > pp[2 * tot + 1].max) {       pp[tot].max = pp[2 * tot].max;       pp[tot].id = pp[2 * tot].id;     } else {       pp[tot].max = pp[2 * tot + 1].max;       pp[tot].id = pp[2 * tot + 1].id;     }   }   ppi query(int tot, int l, int r) {     if (pp[tot].le >= l && pp[tot].ri <= r) {       pp1.max = pp[tot].max;       pp1.id = pp[tot].id;       return pp1;     }     int mid;     mid = (pp[tot].le + pp[tot].ri) / 2;     ppi pp2;     pp2.max = -1000000001;     if (l <= mid) {       pp1 = query(2 * tot, l, r);       if (pp1.max > pp2.max) {         pp2.max = pp1.max;         pp2.id = pp1.id;       }     }     if (r > mid) {       pp1 = query(2 * tot + 1, l, r);       if (pp1.max > pp2.max) {         pp2.max = pp1.max;         pp2.id = pp1.id;       }     }     return pp2;   }   struct pppi {     int l;     int r;     int c;     int id;     int ii;   } pp3[maxn], pp4[maxn], pp5[maxn << 1];   int a[maxn];   int cmp(pppi a, pppi b) {     if (a.l != b.l) return a.l < b.l;     return a.id > b.id;   }   int cmp1(pppi a, pppi b) {     if (a.r != b.r) return a.r < b.r;     return a.id < b.id;   }   int cmp2(pppi a, pppi b) {     if (a.r != b.r) return a.r < b.r;     return a.id > b.id;   }   int main() {     int i, n, m;     cin >> n >> m;     int tot = 1;     ppi pp6;     pp6.max = -10000;     for (i = 1; i <= n; i++) {       scanf( %d%d , &pp3[i].l, &pp3[i].r);       a[i] = pp3[i].r;       pp3[i].id = 1;       pp3[i].ii = i;       pp5[tot] = pp3[i];       tot++;     }     sort(a + 1, a + 1 + n);     for (i = 1; i <= m; i++) {       scanf( %d%d%d , &pp4[i].l, &pp4[i].r, &pp4[i].c);       pp4[i].id = -1;       pp4[i].ii = i;       pp5[tot] = pp4[i];       tot++;     }     tot--;     build(1, 1, n);     sort(pp5 + 1, pp5 + 1 + tot, cmp);     for (i = 1; i <= tot; i++) {       if (pp5[i].id == 1) {         int p = lower_bound(a + 1, a + 1 + n, pp5[i].r) - a;         merg(1, p, pp5[i].r, pp5[i].ii);       } else {         int p = lower_bound(a + 1, a + 1 + n, pp5[i].l) - a;         ppi pp7;         if (p >= 1 && p <= n)           pp7 = query(1, p, n);         else           pp7.max = 0;         if ((min(pp7.max, (long long)pp5[i].r) - pp5[i].l) * pp5[i].c > pp6.max) {           pp6.max = (min(pp7.max, (long long)pp5[i].r) - pp5[i].l) * pp5[i].c;           pp6.id = pp7.id;           pp6.w = pp5[i].ii;         }       }     }     for (i = 1; i <= n; i++) {       a[i] = pp3[i].l;     }     sort(a + 1, a + 1 + n);     build(1, 1, n);     sort(pp5 + 1, pp5 + tot + 1, cmp1);     for (i = tot; i >= 1; i--) {       if (pp5[i].id == 1) {         int p = lower_bound(a + 1, a + 1 + n, pp5[i].l) - a;         merg(1, p, -pp5[i].l, pp5[i].ii);       } else {         int p = lower_bound(a + 1, a + 1 + n, pp5[i].r) - a;         if (p > n) p--;         if (p >= 1 && p <= n && a[p] > pp5[i].r) p--;         ppi pp7;         if (p >= 1 && p <= n) {           pp7 = query(1, 1, p);           pp7.max = -pp7.max;           if ((pp5[i].r - max((long long)pp5[i].l, pp7.max)) * pp5[i].c >               pp6.max) {             pp6.max = (pp5[i].r - max((long long)pp5[i].l, pp7.max)) * pp5[i].c;             pp6.id = pp7.id;             pp6.w = pp5[i].ii;           }         }       }     }     sort(pp5 + 1, pp5 + 1 + n, cmp2);     build(1, 1, n);     for (i = 1; i <= tot; i++) {       if (pp5[i].id == 1) {         int p = lower_bound(a + 1, a + 1 + n, pp5[i].l) - a;         merg(1, p, pp5[i].r - pp5[i].l, pp5[i].ii);       } else {         int p = lower_bound(a + 1, a + 1 + n, pp5[i].l) - a;         if (p >= 1 && p <= n) {           ppi pp7 = query(1, p, n);           if (pp7.max * pp5[i].c > pp6.max) {             pp6.max = pp7.max * pp5[i].c;             pp6.id = pp7.id;             pp6.w = pp5[i].ii;           }         }       }     }     if (pp6.max >= 0)       cout << pp6.max << endl;     else       cout << 0 << endl;     if (pp6.max > 0) cout << pp6.id <<     << pp6.w;   }


#include <bits/stdc++.h>   using namespace std;   int a[105];   int main() {     int n, k;     cin >> n >> k;     int j = 1000;     for (int i = 0; i < n; i++) {       cin >> a[i];       if (k % a[i] == 0) {         j = min(k / a[i], j);       }     }     cout << j;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 100010, TOUR = 1 << 17;   struct tour {     int val, broj, prop;     tour(int val, int broj, int prop) : val(val), broj(broj), prop(prop) {}     tour() {}   } t[2 * TOUR];   struct event {     int t, l, r, val;     event(int t, int l, int r, int val) : t(t), l(l), r(r), val(val) {}     event() {}   };   bool operator<(const event &a, const event &b) { return a.t < b.t; }   vector<int> V[MAX];   vector<event> E;   int disc[MAX], fin[MAX];   int rje[MAX];   int vr = 0;   void Propagiraj(int x) {     for (int i = 2 * x; i <= 2 * x + 1; i++) {       t[i].prop += t[x].prop;       t[i].val += t[x].prop;     }     t[x].prop = 0;   }   tour Merge(tour l, tour r) {     if (l.val < r.val) return tour(l.val, l.broj, 0);     if (r.val < l.val) return tour(r.val, r.broj, 0);     return tour(l.val, l.broj + r.broj, 0);   }   void Stavi(int pos, int lo, int hi, int begin, int end, int val) {     if (lo >= end || hi <= begin) return;     if (lo >= begin && hi <= end) {       t[pos].val += val;       t[pos].prop += val;       return;     }     Propagiraj(pos);     Stavi(2 * pos + 0, lo, (lo + hi) / 2, begin, end, val);     Stavi(2 * pos + 1, (lo + hi) / 2, hi, begin, end, val);     t[pos] = Merge(t[2 * pos], t[2 * pos + 1]);   }   void Dfs(int node, int prosli) {     disc[node] = vr++;     for (int i = 0; i < (int)V[node].size(); i++) {       int nn = V[node][i];       if (nn != prosli) Dfs(nn, node);     }     fin[node] = vr;   }   void Init(int n) {     for (int i = 0; i < n; i++) t[i + TOUR] = tour(0, 1, 0);     for (int i = n; i < TOUR; i++) t[i + TOUR] = tour(1e9, 0, 0);     for (int i = TOUR - 1; i; i--) t[i] = Merge(t[2 * i], t[2 * i + 1]);   }   int main() {     int n, brq;     scanf( %d%d , &n, &brq);     Init(n);     for (int i = 0; i < n - 1; i++) {       int a, b;       scanf( %d%d , &a, &b);       a--;       b--;       V[a].push_back(b);       V[b].push_back(a);     }     Dfs(0, -1);     for (int i = 0; i < brq; i++) {       int a, b;       scanf( %d%d , &a, &b);       a--;       b--;       E.push_back(event(disc[a], disc[a], fin[a], 1));       E.push_back(event(fin[a], disc[a], fin[a], -1));       E.push_back(event(disc[b], disc[a], fin[a], 1));       E.push_back(event(fin[b], disc[a], fin[a], -1));       E.push_back(event(disc[a], disc[b], fin[b], 1));       E.push_back(event(fin[a], disc[b], fin[b], -1));       E.push_back(event(disc[b], disc[b], fin[b], 1));       E.push_back(event(fin[b], disc[b], fin[b], -1));     }     sort(E.begin(), E.end());     for (int i = 0, ind = 0; i < n; i++) {       for (; ind < (int)E.size() && E[ind].t == i; ind++) {         event ev = E[ind];         Stavi(1, 0, TOUR, ev.l, ev.r, ev.val);       }       if (t[1].val)         rje[i] = n - 1;       else         rje[i] = max(0, n - t[1].broj - 1);     }     for (int i = 0; i < n; i++) printf( %d  , rje[disc[i]]);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   constexpr long long mod = 1000000007;   const long long INF = mod * mod;   const long double eps = 1e-12;   const long double pi = acosl(-1.0);   long long mod_pow(long long x, long long n, long long m = mod) {     long long res = 1;     while (n) {       if (n & 1) res = res * x % m;       x = x * x % m;       n >>= 1;     }     return res;   }   struct modint {     long long n;     modint() : n(0) { ; }     modint(long long m) : n(m) {       if (n >= mod)         n %= mod;       else if (n < 0)         n = (n % mod + mod) % mod;     }     operator int() { return n; }   };   bool operator==(modint a, modint b) { return a.n == b.n; }   modint operator+=(modint& a, modint b) {     a.n += b.n;     if (a.n >= mod) a.n -= mod;     return a;   }   modint operator-=(modint& a, modint b) {     a.n -= b.n;     if (a.n < 0) a.n += mod;     return a;   }   modint operator*=(modint& a, modint b) {     a.n = ((long long)a.n * b.n) % mod;     return a;   }   modint operator+(modint a, modint b) { return a += b; }   modint operator-(modint a, modint b) { return a -= b; }   modint operator*(modint a, modint b) { return a *= b; }   modint operator^(modint a, long long n) {     if (n == 0) return modint(1);     modint res = (a * a) ^ (n / 2);     if (n % 2) res = res * a;     return res;   }   long long inv(long long a, long long p) {     return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);   }   modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }   int max_n;   const int mn = 100000;   struct edge {     int to, cap;     long long cost;     int rev;   };   vector<edge> G[mn];   pair<int, int> par[mn];   long long dist[mn];   void add_edge(int from, int to, int cap, long long cost) {     G[from].push_back({to, cap, cost, (int)G[to].size()});     G[to].push_back({from, 0, -cost, (int)G[from].size() - 1});     max_n = max({max_n, from + 1, to + 1});   }   void add_edge2(int from, int to, int cap, long long cost) {     G[from].push_back({to, cap, cost, -1});     max_n = max({max_n, from + 1, to + 1});   }   pair<long long, long long> minimum_road(int s, int t) {     fill(par, par + max_n, pair<int, int>{-1, -1});     fill(dist, dist + max_n, INF);     dist[s] = 0;     priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,                    greater<pair<long long, long long>>>         q;     q.push({0, s});     while (!q.empty()) {       pair<long long, long long> p = q.top();       q.pop();       int id = p.second;       if (id == t) continue;       if (p.first > dist[id]) continue;       for (int j = 0; j < G[id].size(); j++) {         if (G[id][j].cap > 0) {           int to = G[id][j].to;           long long nd = p.first + G[id][j].cost;           if (nd < dist[to]) {             dist[to] = nd;             par[to] = {id, j};             q.push({dist[to], to});           }         }       }     }     int cur = t;     int f = mod;     while (cur != s) {       int p = par[cur].first, j = par[cur].second;       if (p < 0) return {-1, -1};       f = min(f, G[p][j].cap);       cur = p;     }     cur = t;     while (cur != s) {       int p = par[cur].first, j = par[cur].second;       if (p < 0) return {-1, -1};       G[p][j].cap -= f;       if (G[p][j].rev >= 0) {         G[cur][G[p][j].rev].cap += f;       }       cur = p;     }     return {dist[t], f};   }   long long minimum_cost_flow(int s, int t, int k = mod) {     long long ret = 0;     for (int i = 0; i < k; i++) {       pair<long long, long long> z = minimum_road(s, t);       if (z.first < 0) return ret;       if (k - i <= z.second) {         ret += z.first * (k - i);         break;       }       i += z.second - 1;       ret += z.first * z.second;     }     return ret;   }   void solve() {     int n, m;     cin >> n >> m;     vector<int> d(n + 2 * m);     for (int i = 0; i < m; i++) {       int a, b, c, f;       cin >> a >> b >> c >> f;       a--;       b--;       int le = n + 2 * i;       int ri = n + 2 * i + 1;       add_edge(a, le, mod, 1);       add_edge(le, a, f, 1);       add_edge(ri, b, mod, 1);       add_edge(b, ri, f, 1);       add_edge(le, ri, c, 0);       add_edge(le, ri, mod, 2);       d[a] += -f;       d[le] += f;       d[ri] += -f;       d[b] += f;     }     int sum = 0;     int s = n + 2 * m;     int t = s + 1;     int sum2 = 0;     int S = t + 1;     int T = S + 1;     for (int i = 0; i < n + 2 * m; i++) {       if (d[i] > 0) {         sum += abs(d[i]);         add_edge(s, i, d[i], 0);       } else if (d[i] < 0) {         sum2 += abs(d[i]);         add_edge(i, t, -d[i], 0);       }     }     max_n = T + 1;     add_edge(n - 1, 0, mod, 0);     int ans = 0;     ans += minimum_cost_flow(s, t, sum);     cout << ans / 2 <<   n ;   }   signed main() {     ios::sync_with_stdio(false);     cin.tie(0);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000000 + 10;   int n, m, k;   int a[N], b[N];   char s[N], t[N];   int p[22];   int mn[1 << 20], mx[1 << 20];   int getint(char *str) {     char news[22];     for (int i = 1; i <= k; i++) {       news[p[i]] = str[i];     }     int ans = 0;     for (int i = 1; i <= k; i++) {       ans = ans << 1 | (news[i] ==  1 );     }     return ans;   }   void updmx(int &x, int y) { x = max(x, y); }   void updmn(int &x, int y) { x = min(x, y); }   int bitcount(int x) {     int ans = 0;     for (int i = 0; i < k; i++)       if (x >> i & 1) ans++;     return ans;   }   int main() {     scanf( %d%d%d , &n, &m, &k);     scanf( %s%s , s + 1, t + 1);     for (int i = 1; i <= n; i++) {       scanf( %d%d , &a[i], &b[i]);     }     for (int i = 0; i < 1 << k; i++) mn[i] = 1e9, mx[i] = -1e9;     int all = (1 << k) - 1;     for (int i = 1; i <= k; i++) p[i] = i;     updmx(mx[getint(t) ^ all], 0);     for (int i = 1; i <= n; i++) {       swap(p[a[i]], p[b[i]]);       updmx(mx[getint(t) ^ all], i);     }     for (int i = 1; i <= k; i++) p[i] = i;     updmn(mn[getint(s) ^ all], 0);     for (int i = 1; i <= n; i++) {       swap(p[a[i]], p[b[i]]);       updmn(mn[getint(s) ^ all], i);     }     for (int bit = 0; bit < k; bit++) {       for (int i = 1; i < (1 << k); i++) {         if (((i >> bit) & 1) == 1) {           mx[i] = max(mx[i], mx[i ^ (1 << bit)]);           mn[i] = min(mn[i], mn[i ^ (1 << bit)]);         }       }     }     int bstval = -1, bstL, bstR;     for (int i = 0; i < 1 << k; i++) {       if (mx[all ^ i] - mn[all ^ i] >= m) {         if (bitcount(i) >= bstval) {           bstval = bitcount(i);           bstR = mx[all ^ i];           bstL = mn[all ^ i] + 1;         }       }     }     int s1 = 0, t1 = 0;     for (int i = 1; s[i]; i++) s1 += (s[i] ==  1  ? 1 : 0);     for (int i = 1; t[i]; i++) t1 += (t[i] ==  1  ? 1 : 0);     printf( %d n , k + 2 * bstval - s1 - t1);     printf( %d %d n , bstL, bstR);   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = (x << 3) + (x << 1) + (ch ^ 48);       ch = getchar();     }     return x * f;   }   int n, f[5010][5010][2], out[5010], siz[5010], size, rt = 1;   struct node {     int u, v, nxt;   } e[5010 << 1];   int head[5010], js;   void add(int u, int v) {     e[++js] = (node){u, v, head[u]};     head[u] = js;   }   void dfs1(int u, int pre) {     for (int i = head[u]; i; i = e[i].nxt) {       int v = e[i].v;       out[u]++;       if (v == pre) continue;       dfs1(v, u);     }     size += out[u] == 1;   }   void dfs2(int u, int pre) {     if (out[u] == 1) {       siz[u] = 1;       f[u][0][0] = f[u][1][1] = 0;       return;     } else       f[u][0][0] = f[u][0][1] = 0;     for (int i = head[u]; i; i = e[i].nxt) {       int v = e[i].v;       if (v == pre) continue;       dfs2(v, u);       siz[u] += siz[v];       for (int j = siz[u]; j >= 0; j--) {         int tmp0 = 0x7fffffff, tmp1 = 0x7fffffff;         for (int k = 0; k <= siz[v]; k++)           if (k <= j) {             tmp0 = min(tmp0, f[u][j - k][0] + min(f[v][k][0], f[v][k][1] + 1));             tmp1 = min(tmp1, f[u][j - k][1] + min(f[v][k][1], f[v][k][0] + 1));           }         f[u][j][0] = tmp0;         f[u][j][1] = tmp1;       }     }   }   int main() {     n = read();     memset(f, 63, sizeof f);     for (int i = 1; i < n; i++) {       int x = read(), y = read();       add(x, y), add(y, x);     }     dfs1(1, 0);     while (out[rt] == 1) rt++;     dfs2(rt, 0);     printf( %d , min(f[rt][size >> 1][0], f[rt][size >> 1][1]));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int t, n, a[110];   int main() {     scanf( %d , &t);     while (t--) {       scanf( %d , &n);       for (int i = 0; i < n; i++) scanf( %d , &a[i]);       for (int i = 0; i < n; i++) a[i] %= 2;       int sum = 0;       for (int i = 0; i < n; i++) sum += a[i];       if (sum == 0 || sum == n)         printf( YES n );       else         printf( NO n );     }   }


#include <bits/stdc++.h>   using namespace std;   const double EPS = 1e-8, pi = acos(-1);   const int SZ = 100050, INF = 0x7FFFFFFF;   vector<int> mp[SZ];   int res, pos;   int in[SZ];   void dfs(int u, int par) {     int cur = 0;     for (int i = 0; i < mp[u].size(); ++i) {       int to = mp[u][i];       if (to != par) {         ++cur;         dfs(to, u);       }     }     if (par == -1 && cur == 2) --cur;     if (cur > 1) {       pos = u;       ++res;     }   }   int main() {     std::ios::sync_with_stdio(0);     int casenum;     {       int n;       cin >> n;       for (int i = 0; i < n - 1; ++i) {         int u, v;         cin >> u >> v;         mp[u].push_back(v);         mp[v].push_back(u);         ++in[u];         ++in[v];       }       dfs(1, -1);       if (res > 1)         cout <<  No  << endl;       else if (res == 0) {         cout <<  Yes n1  << endl;         vector<int> tmp;         for (int i = 1; i <= n; ++i) {           if (in[i] == 1) {             tmp.push_back(i);           }         }         cout << tmp[0] <<     << tmp[1] << endl;       } else {         cout <<  Yes  << endl;         int num = count(in + 1, in + 1 + n, 1);         cout << num << endl;         for (int i = 1; i <= n; ++i) {           if (in[i] == 1) cout << i <<     << pos << endl;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int len, rt, n, m;   vector<int> g[210];   int a[210], b[210];   void dfs(int cur, int pre, int dep) {     int i;     if (dep > len) {       len = dep;       rt = cur;     }     for (i = 0; i < g[cur].size(); i++) {       int u = g[cur][i];       if (u == pre) continue;       dfs(u, cur, dep + 1);     }   }   int solve(int root) {     rt = root;     len = 0;     dfs(root, root, 0);     len = 0;     dfs(rt, rt, 0);     return len;   }   void Init() {     for (int i = 1; i <= n; ++i) g[i].clear();   }   int main() {     int i, j;     scanf( %d , &n);     for (i = 1; i < n; i++) scanf( %d%d , &a[i], &b[i]);     int ans = 0;     for (i = 1; i < n; i++) {       Init();       int x = a[i], y = b[i];       for (j = 1; j < n; j++)         if (i != j) {           g[a[j]].push_back(b[j]);           g[b[j]].push_back(a[j]);         }       ans = max(ans, solve(x) * solve(y));     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   int n;   int fa[1000005], huan[1000005], zihuan[1000005], siz[1000005];   int find(int now) { return fa[now] == now ? now : fa[now] = find(fa[now]); }   int main() {     scanf( %d , &n);     for (int i = 1; i <= 2 * n; i++) {       fa[i] = i;       siz[i] = 1;     }     for (int i = 1; i <= n; i++) {       int x, y;       scanf( %d%d , &x, &y);       int x1 = find(x), y1 = find(y);       if (x == y) zihuan[x1] = 1;       if (x1 == y1) huan[y1] = 1;       fa[x1] = y1;     }     for (int i = 1; i <= 2 * n; i++) {       int x = find(i);       if (x != i) {         siz[x] += siz[i];         huan[x] |= huan[i];         zihuan[x] |= zihuan[i];       }     }     long long ans = 1;     for (int i = 1; i <= 2 * n; i++) {       if (find(i) == i) {         if (zihuan[i])           continue;         else if (huan[i])           ans = (ans * 2) % mod;         else           ans = (ans * siz[i]) % mod;       }     }     printf( %I64d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void amin(T &x, const T &y) {     if (y < x) x = y;   }   template <class T>   inline void amax(T &x, const T &y) {     if (x < y) x = y;   }   template <class Iter>   void rprintf(const char *fmt, Iter begin, Iter end) {     for (bool sp = 0; begin != end; ++begin) {       if (sp)         putchar(   );       else         sp = true;       printf(fmt, *begin);     }     putchar(  n );   }   int N;   int A[200011];   int B[200011];   using Tuple = tuple<int, int, int>;   Tuple X[100011];   void MAIN() {     scanf( %d , &N);     for (int i = 0, i_len = (N); i < i_len; ++i) scanf( %d , A + i);     int Q;     scanf( %d , &Q);     vector<Tuple> sortedX;     sortedX.reserve(Q);     for (int i = 0, i_len = (Q); i < i_len; ++i) {       int s, t, u;       scanf( %d%d%d , &s, &t, &u);       s--;       u--;       X[i] = Tuple(s, t, u);       sortedX.emplace_back(s, t, -1);     }     sort(sortedX.begin(), sortedX.end());     sortedX.erase(unique(sortedX.begin(), sortedX.end()), sortedX.end());     long long sum = 0;     for (int i = 0, i_len = (N); i < i_len; ++i) sum += A[i];     for (int i = 0, i_len = (Q); i < i_len; ++i) {       int s, t, u;       tie(s, t, u) = X[i];       auto it = lower_bound(sortedX.begin(), sortedX.end(), Tuple(s, t, -1));       int s0, t0, u0;       tie(s0, t0, u0) = *it;       if (0 <= u0) {         sum -= max(0, A[u0] - B[u0]);         B[u0]--;         sum += max(0, A[u0] - B[u0]);       }       if (0 <= u) {         sum -= max(0, A[u] - B[u]);         B[u]++;         sum += max(0, A[u] - B[u]);       }       *it = Tuple(s, t, u);       printf( %lld n , sum);     }   }   int main() {     int TC = 1;     for (int tc = 0, tc_len = (TC); tc < tc_len; ++tc) MAIN();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     for (int q = 0; q < t; ++q) {       int n, k;       cin >> n >> k;       vector<int> x(n);       for (int i = 0; i < n; ++i) {         cin >> x[i];       }       vector<int> p(n, 0);       for (int i = 1; i < n - 1; ++i) {         if (x[i] > x[i + 1] && x[i] > x[i - 1]) {           p[i] = p[i - 1] + 1;         } else {           p[i] = p[i - 1];         }       }       p[n - 1] = p[n - 2];       int ans = 0, left = 0;       for (int l = 0; l + k - 1 < n; ++l) {         if (p[l + k - 2] - p[l] > ans) {           ans = p[l + k - 2] - p[l];           left = l;         }       }       cout << ans + 1 <<     << left + 1 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool isPowerOfTwo(long long x) { return x && (!(x & (x - 1))); }   void solve() {     long long n, m;     cin >> n >> m;     if (n == 1 || m == 1) {       cout <<  YES  <<   n ;       return;     }     if (n != m) {       cout <<  NO  <<   n ;       return;     }     if (n == 2) {       cout <<  YES  <<   n ;       return;     }     cout <<  NO  <<   n ;   }   signed main() {     cin.tie(NULL), ios_base::sync_with_stdio(false), cout.tie(NULL);     long long t = 1;     cin >> t;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     string s;     cin >> s;     vector<string> a, b;     string t =   ;     bool if_num = true, if_first = true;     for (int i = 0; i < (int)s.size(); i++) {       if (s[i] ==  ,  or s[i] ==  ; ) {         if (if_num and t !=   ) {           a.push_back(t);         } else {           b.push_back(t);         }         t =   ;         if_num = true;         if_first = true;         continue;       }       if (s[i] <  0  or s[i] >  9  or           (s[i] ==  0  and if_first and            (i != (int)s.size() - 1 and s[i + 1] !=  ;  and s[i + 1] !=  , ))) {         if_num = false;       }       if_first = false;       t += s[i];     }     if (if_num and t !=   ) {       a.push_back(t);     } else {       b.push_back(t);     }     if (a.empty()) {       cout <<  -  << endl;     } else {       cout <<    ;       for (int i = 0; i < (int)a.size(); i++) {         cout << a[i];         if (i != (int)a.size() - 1) {           cout <<  , ;         }       }       cout <<     << endl;     }     if (b.empty()) {       cout <<  -  << endl;     } else {       cout <<    ;       for (int i = 0; i < (int)b.size(); i++) {         cout << b[i];         if (i != (int)b.size() - 1) {           cout <<  , ;         }       }       cout <<     << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     int a, b;     int count = 0;     for (int a = 0; a <= n + m; a++)       for (int b = 0; b <= n + m; b++) {         if (a * a + b == n && b * b + a == m) count++;       }     cout << count;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1000000007;   const int MAX = 505;   int n, grid[MAX][MAX];   char s[5];   bool valid(int x, int y) {     if (x < 1) return false;     if (y < 1) return false;     if (x > n) return false;     if (y > n) return false;     return true;   }   bool dist(int x1, int y1, int x2, int y2) {     return ((abs(x1 - x2) + abs(y1 - y2)) >= n - 1);   }   bool cmd(int x1, int y1, int x2, int y2) {     printf( ? %d %d %d %d n , x1, y1, x2, y2);     fflush(stdout);     scanf( %s , s);     return (s[0] ==  Y );   }   int main() {     scanf( %d , &n);     grid[1][1] = 1;     grid[n][n] = 1;     int x = 1, y = 1;     while (true) {       int ok = 0;       if (valid(x, y + 1)) {         if (dist(x, y + 1, n, n)) {           if (cmd(x, y + 1, n, n)) {             grid[x][y + 1] = 1;             ok = 1;             y++;           }         }       }       if (ok) continue;       if (valid(x + 1, y)) {         if (dist(x + 1, y, n, n)) {           if (cmd(x + 1, y, n, n)) {             grid[x + 1][y] = 1;             ok = 1;             x++;           }         }       }       if (ok) continue;       break;     }     x = n, y = n;     while (true) {       int ok = 0;       if (valid(x - 1, y)) {         if (dist(x - 1, y, 1, 1)) {           if (cmd(1, 1, x - 1, y)) {             grid[x - 1][y] = 1;             ok = 1;             x--;           }         }       }       if (ok) continue;       if (valid(x, y - 1)) {         if (dist(x, y - 1, 1, 1)) {           if (cmd(1, 1, x, y - 1)) {             grid[x][y - 1] = 1;             ok = 1;             y--;           }         }       }       if (ok) continue;       break;     }     string ans =   ;     x = 1, y = 1;     while (true) {       if (x == n and y == n) break;       if (grid[x][y + 1])         ans +=  R , y++;       else         ans +=  D , x++;     }     printf( !  );     for (int i = 0; i < ans.size(); i++) printf( %c , ans[i]);     printf(  n );     fflush(stdout);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 7;   int Log[N], bin[21], ls[N], rs[N], root, a[N], n, i, j;   set<int> s[N];   pair<int, int> mx[N][20];   long long ans;   void merge(int x, int y) {     if (s[x].size() < s[y].size()) swap(s[x], s[y]);     for (auto& z : s[y]) s[x].insert(z);     s[y].clear();   }   void dfs(int x) {     if (!x) return;     dfs(ls[x]);     dfs(rs[x]);     if (s[ls[x]].size() > s[rs[x]].size()) swap(ls[x], rs[x]);     for (auto& z : s[ls[x]])       if (s[rs[x]].count(a[x] - z)) ans++;     s[x].insert(a[x]);     merge(x, ls[x]);     merge(x, rs[x]);   }   int query(int l, int r) {     int t = Log[r - l + 1];     return max(mx[l][t], mx[r - bin[t] + 1][t]).second;   }   int build(int l, int r) {     if (l > r) return 0;     int x = query(l, r);     ls[x] = build(l, x - 1);     rs[x] = build(x + 1, r);     return x;   }   int main() {     for (scanf( %d , &n), i = 1; i <= n; ++i) scanf( %d , a + i);     for (Log[0] = -1, i = 1; i <= n; ++i) Log[i] = Log[i >> 1] + 1;     for (bin[0] = 1, i = 1; i < 20; ++i) bin[i] = bin[i - 1] << 1;     for (i = 1; i <= n; ++i) mx[i][0] = pair<int, int>(a[i], i);     for (j = 1; j <= Log[n]; ++j)       for (i = 1; i + bin[j - 1] <= n; ++i)         mx[i][j] = max(mx[i][j - 1], mx[i + bin[j - 1]][j - 1]);     root = build(1, n);     dfs(root);     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 510;   long long sum[maxn];   int main() {     long long n, k;     int t;     cin >> t;     while (t--) {       cin >> n >> k;       if (n >= 32) {         cout <<  YES               <<     << n - 1 << endl;       } else {         memset(sum, 0, sizeof(sum));         int ans = -1;         long long total = (pow(4, n) - 1) / 3;         if (k > total) {           cout <<  NO  << endl;           continue;         }         long long temp = pow(2, n - 1) * 2 - 1, cnt = 0;         while (temp) {           sum[++cnt] = 2 * (temp / 2) + 1;           temp /= 2;         }         for (int i = cnt; i >= 1; i--) sum[i] += sum[i + 1];         for (int i = 1; i <= cnt; i++)           if (sum[i] <= k && total - sum[1] >= k - sum[i]) {             ans = i;             break;           }         if (ans == -1)           cout <<  NO  << endl;         else           cout <<  YES                 <<     << ans - 1 << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   void solve();   int main() { solve(); }   void solve() {     int t;     cin >> t;     while (t--) {       string s;       cin >> s;       priority_queue<int> q;       int k = s.length();       int cur = (s[0] ==  1 ) ? 1 : 0;       for (int i = 1; i < k; i++) {         if (s[i] ==  1  && s[i - 1] ==  0 ) {           cur = 1;         }         if (s[i] ==  1  && s[i - 1] ==  1 ) {           cur++;         }         if (s[i] ==  0 ) {           if (cur != 0) {             q.push(cur);             cur = 0;           }         }       }       if (s[k - 1] ==  1 ) {         if (cur != 0) {           q.push(cur);           cur = 0;         }       }       int ans = 0;       int qs = q.size();       for (int i = 0; i < qs; i++) {         if ((i & 0x1) == 0) {           ans += q.top();         } else {         }         q.pop();       }       cout << ans << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     string s, curr[1000];     long long int size = 0, j;     cin >> j;     while (j--) {       cin >> s;       if (s.compare( pwd ) == 0) {         for (long long int i = 0; i < size; ++i) {           cout <<  /  << curr[i];         }         cout <<  /               <<   n ;       } else if (s.compare( cd ) == 0) {         string temp;         cin >> s;         if (s[0] ==  / ) {           size = 0;         }         for (long long int i = 0; i < s.size(); ++i) {           temp =   ;           for (; i < s.size(); ++i) {             if (s[i] ==  / ) break;             temp += s[i];           }           if (temp.compare( .. ) == 0)             --size;           else if (temp.size()) {             curr[size] = temp;             ++size;           }         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 10;   const int M = N * 30;   int tree[M][2];   vector<int> val[M];   int main() {     int n;     scanf( %d , &n);     vector<int> a(n + 1);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     int idx = 0;     auto insert = [&](int n, int id) {       int tmp = 0;       bitset<30> st(n);       for (int i = 29; i >= 0; i--) {         if (!tree[tmp][st[i]]) tree[tmp][st[i]] = ++idx;         tmp = tree[tmp][st[i]];         val[tmp].emplace_back(id);       }     };     for (int i = 1; i <= n; i++) insert(a[i], i);     auto get_cal = [&](vector<int> &a, vector<int> &b) -> long long {       long long ans = 0;       int i = 0, j = 0;       while (i < a.size() && j < b.size()) {         if (a[i] < b[j])           i++;         else           ans += (int)a.size() - i, j++;       }       return ans;     };     vector<vector<long long>> ans(30, vector<long long>(2, 0));     function<void(int, int)> query = [&](int root, int deep) {       if (!tree[root][0] && !tree[root][1]) return;       long long cnt = get_cal(val[tree[root][0]], val[tree[root][1]]);       ans[deep][0] += cnt;       ans[deep][1] +=           1LL * val[tree[root][0]].size() * val[tree[root][1]].size() - cnt;       if (tree[root][0]) query(tree[root][0], deep - 1);       if (tree[root][1]) query(tree[root][1], deep - 1);     };     query(0, 29);     int res = 0;     long long cnt = 0;     for (int i = 29; i >= 0; i--) {       if (ans[i][0] > ans[i][1])         res += 1 << i, cnt += ans[i][1];       else         cnt += ans[i][0];     }     printf( %lld %d n , cnt, res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using vi = vector<int>;   using vvi = vector<vi>;   using vll = vector<ll>;   using vvll = vector<vll>;   using vb = vector<bool>;   using vd = vector<double>;   using vs = vector<string>;   using pii = pair<int, int>;   using pll = pair<ll, ll>;   using pdd = pair<double, double>;   using vpii = vector<pii>;   using vvpii = vector<vpii>;   using vpll = vector<pll>;   using vvpll = vector<vpll>;   using vpdd = vector<pdd>;   using vvpdd = vector<vpdd>;   template <typename T>   void ckmin(T& a, const T& b) {     a = min(a, b);   }   template <typename T>   void ckmax(T& a, const T& b) {     a = max(a, b);   }   mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());   namespace __input {   template <class T1, class T2>   void re(pair<T1, T2>& p);   template <class T>   void re(vector<T>& a);   template <class T, size_t SZ>   void re(array<T, SZ>& a);   template <class T>   void re(T& x) {     cin >> x;   }   void re(double& x) {     string t;     re(t);     x = stod(t);   }   template <class Arg, class... Args>   void re(Arg& first, Args&... rest) {     re(first);     re(rest...);   }   template <class T1, class T2>   void re(pair<T1, T2>& p) {     re(p.first, p.second);   }   template <class T>   void re(vector<T>& a) {     for (int i = 0; i < (int((a).size())); i++) re(a[i]);   }   template <class T, size_t SZ>   void re(array<T, SZ>& a) {     for (int i = 0; i < (SZ); i++) re(a[i]);   }   }  // namespace __input   using namespace __input;   namespace __output {   template <class T1, class T2>   void pr(const pair<T1, T2>& x);   template <class T, size_t SZ>   void pr(const array<T, SZ>& x);   template <class T>   void pr(const vector<T>& x);   template <class T>   void pr(const deque<T>& x);   template <class T>   void pr(const set<T>& x);   template <class T1, class T2>   void pr(const map<T1, T2>& x);   template <class T>   void pr(const T& x) {     cout << x;   }   template <class Arg, class... Args>   void pr(const Arg& first, const Args&... rest) {     pr(first);     pr(rest...);   }   template <class T1, class T2>   void pr(const pair<T1, T2>& x) {     pr( { , x.first,  ,  , x.second,  } );   }   template <class T, bool pretty = true>   void prContain(const T& x) {     if (pretty) pr( { );     bool fst = 1;     for (const auto& a : x) pr(!fst ? pretty ?  ,   :     :   , a), fst = 0;     if (pretty) pr( } );   }   template <class T>   void pc(const T& x) {     prContain<T, false>(x);     pr(  n );   }   template <class T, size_t SZ>   void pr(const array<T, SZ>& x) {     prContain(x);   }   template <class T>   void pr(const vector<T>& x) {     prContain(x);   }   template <class T>   void pr(const deque<T>& x) {     prContain(x);   }   template <class T>   void pr(const set<T>& x) {     prContain(x);   }   template <class T1, class T2>   void pr(const map<T1, T2>& x) {     prContain(x);   }   void ps() { pr(  n ); }   template <class Arg>   void ps(const Arg& first) {     pr(first);     ps();   }   template <class Arg, class... Args>   void ps(const Arg& first, const Args&... rest) {     pr(first,    );     ps(rest...);   }   }  // namespace __output   using namespace __output;   namespace __numeric {   template <typename T>   typename enable_if<is_integral<T>::value, T>::type floor(T n, T d) {     assert(d != 0);     if (d < 0) tie(n, d) = make_pair(-n, -d);     return n / d - ((n < 0) && (n % d));   }   template <typename T>   typename enable_if<is_integral<T>::value, T>::type ceil(T n, T d) {     assert(d != 0);     if (d < 0) tie(n, d) = make_pair(-n, -d);     return n / d + ((n > 0) && (n % d));   }   };  // namespace __numeric   using namespace __numeric;   namespace __algorithm {   template <typename T>   void dedup(vector<T>& v) {     sort((v).begin(), (v).end());     v.erase(unique((v).begin(), (v).end()), v.end());   }   template <typename T>   typename vector<T>::iterator find(vector<T>& v, const T& x) {     auto it = lower_bound((v).begin(), (v).end(), x);     return it != v.end() && *it == x ? it : v.end();   }   template <typename T>   size_t index(vector<T>& v, const T& x) {     auto it = find(v, x);     assert(it != v.end() && *it == x);     return it - v.begin();   }   template <typename C, typename T, typename OP>   vector<T> prefixes(const C& v, T id, OP op) {     vector<T> r(int((v).size()) + 1, id);     for (int i = 0; i < (int((v).size())); i++) r[i + 1] = op(r[i], v[i]);     return r;   }   template <typename C, typename T, typename OP>   vector<T> suffixes(const C& v, T id, OP op) {     vector<T> r(int((v).size()) + 1, id);     for (int i = (int((v).size())) - 1; i >= 0; i--) r[i] = op(v[i], r[i + 1]);     return r;   }   }  // namespace __algorithm   using namespace __algorithm;   #pragma GCC diagnostic push   #pragma GCC diagnostic ignored  -Wunused-parameter    struct monostate {     friend istream& operator>>(istream& is, const monostate& ms) { return is; }     friend ostream& operator<<(ostream& os, const monostate& ms) { return os; }     friend monostate operator+(const monostate& a, const monostate& b) {       return a;     }   } ms;   #pragma GCC diagnostic pop   namespace __io {   void setIn(string second) { freopen(second.c_str(),  r , stdin); }   void setOut(string second) { freopen(second.c_str(),  w , stdout); }   void setIO(string second =   ) {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout << fixed << setprecision(15);     if (int((second).size())) {       setIn(second +  .in ), setOut(second +  .out );     }   }   }  // namespace __io   using namespace __io;   struct union_find {     struct node {       int par, rnk, size;       node(int id = 0) : par(id), rnk(0), size(1) {}     };     vector<node> uf;     union_find(int N = 0) : uf(N) {       for (int i = 0; i < N; i++) uf[i] = node(i);     }     int rep(int i) {       if (i != uf[i].par) uf[i].par = rep(uf[i].par);       return uf[i].par;     }     bool unio(int a, int b) {       a = rep(a), b = rep(b);       if (a == b) return false;       if (uf[a].rnk < uf[b].rnk) swap(a, b);       uf[b].par = a;       uf[a].size += uf[b].size;       if (uf[a].rnk == uf[b].rnk) uf[a].rnk++;       return true;     }     node& operator[](int i) { return uf[rep(i)]; }     friend void pr(const union_find& u) {       pr( { );       bool first = 1;       for (int i = 0; i < int((u.uf).size()); i++)         if (u.uf[i].par == i) {           if (!first)             pr( ,  );           else             first = 0;           pr( [  , i,   | rank= , u.uf[i].rnk,   size= , u.uf[i].size,   ] );         }       pr( } );     }   };   const int MAXB = 18;   const int MAXV = 1 << MAXB;   int ct[MAXV];   int main() {     setIO();     int N;     re(N);     for (int i = 0; i < (N); i++) {       int v;       re(v);       ct[v]++;     }     ll ans = 0;     for (int v = 0; v < (MAXV); v++) {       ans -= ll(v) * ct[v];     }     union_find uf(MAXV + 1);     for (int sum = (MAXV)-1; sum >= 0; sum--) {       for (int v1 = sum, v2 = 0; v1 > v2; v1 = (v1 - 1) & sum, v2 = sum ^ v1) {         if (ct[v1] && ct[v2]) {           if (uf.unio(v1, v2)) {             ans += ll(ct[v1] + ct[v2] - 1) * sum;             ct[v1] = 1;             ct[v2] = 1;           }         }       }       if (ct[sum] && uf.unio(MAXV, sum)) ans += ll(ct[sum]) * sum;     }     ps(ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   long long qpow(long long a, long long b) {     long long res = 1;     while (b) {       if (b & 1) res = res * a;       b >>= 1;       a = a * a;     }     return res;   }   int a[500005];   int main() {     int n, k;     cin >> n >> k;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     sort(a + 1, a + n + 1);     k = 8 * k / n;     if (k >= ceil(log(n) / log(2))) {       cout << 0;       return 0;     }     long long q = pow(2, k);     int min1 = n;     int sum = 0;     for (int l = 1, r = 1; r <= n && l <= n;) {       while (r < n && a[r] == a[r + 1]) r++;       sum++;       if (sum == q) {         min1 = min(min1, n - (r - l + 1)), sum--;         while (l < n && a[l] == a[l + 1]) l++;         l++;       }       r++;     }     if (min1 == n)       cout << 0;     else       cout << min1;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast ,  unroll-loops )   using namespace std;   const int inf = numeric_limits<int>::max() / 2;   const long double eps = 1e-9;   const long double pi = acos(-1);   const int MAXN = (3e8 + 10) / 3;   int n;   unsigned int a, b, c, d;   bitset<MAXN> primes;   unsigned int f(unsigned int x) { return d + x * (c + x * (b + x * a)); }   int fict(int i) { return i - i / 2 - i / 3 + i / 6 - 1; }   void sieve() {     primes.set();     for (int i = 5; i * i <= n; i++) {       if (i % 2 == 0 || i % 3 == 0) {         continue;       }       if (primes[fict(i)]) {         for (int j = i * i; j <= n; j += i) {           if (j % 2 == 0 || j % 3 == 0) {             continue;           }           primes[fict(j)] = 0;         }       }     }   }   inline void solve() {     sieve();     unsigned int ans = 0;     for (int p = 2; p <= n; p++) {       if (p != 2 && p % 2 == 0 || p != 3 && p % 3 == 0) {         continue;       }       if (p != 2 && p != 3 && !primes[fict(p)]) {         continue;       }       unsigned int s = 0;       unsigned int q = n / p;       while (q >= 1) {         s += q;         q /= p;       }       ans += f(p) * s;     }     cout << ans <<   n ;   }   inline void read() { cin >> n >> a >> b >> c >> d; }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     read();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int Max = 2e5 + 10;   vector<int> A;   int ok[Max];   int len;   int seg[Max * 4];   int lazy[Max * 4];   void build(int tl = 0, int tr = len + 1, int idx = 1) {     if (tr - tl == 1) {       seg[idx] = len - tl;       return;     }     int mid = (tl + tr) / 2;     build(tl, mid, idx * 2);     build(mid, tr, idx * 2 + 1);     seg[idx] = min(seg[idx * 2], seg[idx * 2 + 1]);   }   void shift(int idx) {     if (!lazy[idx]) return;     lazy[idx * 2] += lazy[idx];     lazy[idx * 2 + 1] += lazy[idx];     seg[idx * 2] += lazy[idx];     seg[idx * 2 + 1] += lazy[idx];     lazy[idx] = 0;   }   void upd(int i, int val, int tl = 0, int tr = len + 1, int idx = 1) {     if (tr - 1 <= i) {       lazy[idx] += val;       seg[idx] += val;       return;     }     if (tl > i) return;     shift(idx);     int mid = (tl + tr) / 2;     upd(i, val, tl, mid, idx * 2);     upd(i, val, mid, tr, idx * 2 + 1);     seg[idx] = min(seg[idx * 2], seg[idx * 2 + 1]);   }   int check() { return seg[1]; }   int main() {     int n, h;     cin >> n >> len >> h;     for (int i = 0; i < len; i++) {       int a;       cin >> a;       A.push_back(a);     }     sort(A.begin(), A.end());     for (int i = 0; i < n; i++) {       long long a;       cin >> a;       int l = -1, r = len;       while (r - l > 1) {         int idx = (l + r) / 2;         if (a + A[idx] >= h)           r = idx;         else           l = idx;       }       ok[i] = r;     }     int ans = 0;     build();     for (int i = 0; i < len; i++) upd(ok[i], -1);     if (check() >= 0) ans++;     for (int i = len; i < n; i++) {       upd(ok[i], -1);       upd(ok[i - len], 1);       if (check() >= 0) ans++;     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 120000 + 10;   const double PI = acos(-1);   pair<double, double> operator+(pair<double, double> a, pair<double, double> b) {     return make_pair(a.first + b.first, a.second + b.second);   }   pair<double, double> operator-(pair<double, double> a, pair<double, double> b) {     return make_pair(a.first - b.first, a.second - b.second);   }   pair<double, double> operator*(pair<double, double> a, pair<double, double> b) {     return make_pair(a.first * b.first - a.second * b.second,                      a.first * b.second + a.second * b.first);   }   pair<double, double> operator/(pair<double, double> a, double t) {     return make_pair(a.first / t, a.second / t);   }   int rev[N], lim;   pair<double, double> w[N];   void initrever(int n, int &lim) {     lim = 1;     for (; lim < n; lim <<= 1)       ;     for (int i = 1; i < lim; i++) {       rev[i] = (rev[i >> 1] >> 1);       if (i & 1) rev[i] |= (lim >> 1);     }     for (int i = 0; i < lim; i++) {       double t = 2 * PI * i / lim;       w[i] = make_pair(cos(t), sin(t));     }     return;   }   void FFT(vector<pair<double, double> > &a, int t) {     int lim = a.size();     if (t < 0) {       for (int i = 1; i < lim; i++) {         if (i < (lim - i)) swap(a[i], a[lim - i]);       }     }     for (int i = 0; i < lim; i++)       if (i < rev[i]) swap(a[i], a[rev[i]]);     for (int i = 2; i <= lim; i <<= 1) {       for (int j = 0; j < lim; j += i) {         for (int k = 0; k < (i >> 1); k++) {           pair<double, double> first = a[j + k],                                second = a[j + k + (i >> 1)] * w[lim / i * k];           a[j + k] = (first + second);           a[j + k + (i >> 1)] = (first - second);         }       }     }     if (t < 0) {       for (int i = 0; i < lim; i++) a[i] = a[i] / lim;     }     return;   }   vector<pair<double, double> > f[2];   int n, a, p;   double reduce(double first) {     long long tmp = llround(first);     tmp %= p;     return (double)tmp;   }   void add1() {     swap(f[0], f[1]);     for (int i = 0; i <= 1; i++) {       for (int j = n; j >= 1; j--) {         f[i][j].first = reduce(f[i][j].first + f[i][j - 1].first);       }     }     f[1][1].first = reduce(f[1][1].first + 1);     return;   }   void put(vector<pair<double, double> > v) {     for (int i = 0; i < (int)v.size(); i++) printf( %lf  , v[i].first);     printf(  n );     return;   }   void pro2(int v) {     FFT(f[0], 1), FFT(f[1], 1);     for (int i = 0; i < lim; i++) {       pair<double, double> first =           f[v & 1][i] * (f[0][i] + f[1][i]) + f[0][i] + f[v & 1][i];       pair<double, double> second =           f[(v & 1) ^ 1][i] * (f[0][i] + f[1][i]) + f[1][i] + f[(v & 1) ^ 1][i];       f[0][i] = first, f[1][i] = second;     }     FFT(f[0], -1), FFT(f[1], -1);     for (int i = 0; i <= 1; i++) {       for (int j = 0; j <= n; j++) {         f[i][j].first = reduce(f[i][j].first);         f[i][j].second = 0;       }       for (int j = n + 1; j < lim; j++) f[i][j] = make_pair(0, 0);     }     return;   }   vector<int> num;   int main() {     scanf( %d%d%d , &n, &a, &p);     initrever(n * 2 + 1, lim);     f[0].resize(lim), f[1].resize(lim);     for (int no = a; no > 1; no /= 2) {       num.push_back(no);     }     num.push_back(1);     reverse(num.begin(), num.end());     f[1][1].first = 1;     int ans = 0;     for (int i = 0; i < (int)num.size() - 1; i++) {       for (int j = 1; j <= n; j += 2) {         ans = (ans + llround(f[1][j].first)) % p;       }       pro2(num[i]);       if (num[i] * 2 < num[i + 1]) add1();     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int arr[n];     for (int i = 0; i <= n - 1; i++) cin >> arr[i];     vector<pair<int, int> > G[n];     int cnt = 0;     for (int i = 0; i <= n - 1; i++) {       for (int j = i + 1; j <= n - 1; j++) {         if (arr[i] > arr[j]) {           G[i].push_back(make_pair(arr[j], j));           cnt++;         }       }     }     cout << cnt <<   n ;     for (int i = 0; i <= n - 1; i++) {       sort(G[i].begin(), G[i].end());       int s = G[i].size();       for (int j = s - 1; j >= 0; j--) {         cout << i + 1 <<     << G[i][j].second + 1 <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int arr[300022];   int main() {     int q;     scanf( %d , &q);     while (q--) {       int n, i;       scanf( %d , &n);       for (i = 0; i < n; i++) scanf( %d , &arr[i]);       map<int, int> mf, ml;       map<int, int>::iterator it;       for (i = 0; i < n; i++) {         if (mf.find(arr[i]) == mf.end()) mf[arr[i]] = i;         ml[arr[i]] = i;       }       int ans = 0, in = -1, len = 0;       int cou = 0;       for (it = mf.begin(); it != mf.end(); it++) {         cou++;         int ele = it->first;         int focc = it->second, locc = ml[ele];         if (focc > in)           len++;         else           len = 1;         in = locc;         ans = max(ans, len);       }       cout << (cou - ans) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   namespace mainns {   template <typename T>   void relaxMax(T& relaxWhat, const T& relaxBy) {     relaxWhat = max(relaxWhat, relaxBy);   }   template <typename T>   void relaxMin(T& relaxWhat, const T& relaxBy) {     relaxWhat = min(relaxWhat, relaxBy);   }   namespace {   const vector<pair<int, int>> offsets = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};   const unordered_map<char, char> nextLetter = {       { D ,  I }, { I ,  M }, { M ,  A }, { A ,  D }};   }  // namespace   class Solver374C {    public:     void run();     int height, width;     vector<string> board;     vector<vector<int>> maxValues;     int getMaxValue(int x, int y) {       if (maxValues[x][y] > 0) return maxValues[x][y];       if (maxValues[x][y] == -2) return numeric_limits<int>::max() / 10;       maxValues[x][y] = -2;       int maxNeighbourValue = 0;       for (auto& offs : offsets) {         auto neigbour = make_pair(x + offs.first, y + offs.second);         if (neigbour.first >= 0 && neigbour.first < width &&             neigbour.second >= 0 && neigbour.second < height)           if (nextLetter.at(board[y][x]) ==               board[neigbour.second][neigbour.first])             relaxMax(maxNeighbourValue,                      getMaxValue(neigbour.first, neigbour.second));       }       return maxValues[x][y] = maxNeighbourValue + 1;     }   };   void Solver374C::run() {     cin >> height >> width;     board = vector<string>(height);     for (auto& s : board) cin >> s;     maxValues = vector<vector<int>>(width, vector<int>(height, -1));     int maxValue = 0;     for (int x = 0; x < width; ++x)       for (int y = 0; y < height; ++y) {         auto v = getMaxValue(x, y);         if (board[y][x] ==  D ) relaxMax(maxValue, v);       }     if (maxValue < 4)       cout <<  Poor Dima! ;     else if (maxValue >= numeric_limits<int>::max() / 10)       cout <<  Poor Inna! ;     else       cout << (maxValue / 4);   }   }  // namespace mainns   using CurrentSolver = mainns::Solver374C;   int main() {     ios::sync_with_stdio(false);     CurrentSolver().run();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool allCharactersSame(string s) {     int n = s.length();     for (int i = 1; i < n; i++)       if (s[i] != s[0]) return false;     return true;   }   signed main() {     int n;     const int letters = 26;     cin >> n;     for (int i = 0; i < n; i++) {       string str;       cin >> str;       int n = str.size();       vector<int> cnt(letters);       for (int i = 0; i < n; ++i) cnt[str[i] -  a ]++;       int ans = 0, k = 0;       for (int i = 0; i < letters; ++i) {         if (cnt[i] > 1)           ans++;         else if (cnt[i] == 1)           k++;       }       ans += k / 2;       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200010;   int hp[N], dmg[N], p[N];   bool cmp(int a, int b) { return hp[a] - dmg[a] > hp[b] - dmg[b]; }   bool vis[N];   int main() {     int n, a, b;     scanf( %d%d%d , &n, &a, &b);     for (int i = 1; i <= n; i++) {       scanf( %d%d , &hp[i], &dmg[i]);       p[i] = i;     }     sort(p + 1, p + 1 + n, cmp);     long long sum = 0, ans, la = 0, cb = 0;     bool flag = false;     for (int i = 1; i <= n; i++) {       if (i > b || hp[p[i]] <= dmg[p[i]]) {         if (!flag) flag = true, la = hp[p[i - 1]] - dmg[p[i - 1]], cb = i - 1;       }       if (!flag)         sum += hp[p[i]], vis[p[i]] = true;       else         sum += dmg[p[i]];     }     ans = sum;     if (a && b) {       for (int i = 1; i <= n; i++) {         long long tmp = hp[i];         for (int j = 1; j <= a; j++) tmp = tmp * 2;         tmp += sum;         if (vis[i]) {           tmp -= hp[i];         } else {           tmp -= dmg[i];           if (cb == b) tmp -= la;         }         ans = max(ans, tmp);       }     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long Maxn = 10000;   const long double pi = acos(-1);   const long long mod = 1000000007;   template <class T>   ostream &operator<<(ostream &out, vector<T> &A) {     for (auto x : A) out << x <<    ;     return out;   }   template <class T>   ostream &operator<<(ostream &out, set<T> &A) {     for (auto x : A) out << x <<    ;     return out;   }   template <class T1, class T2>   T1 powr(T1 a, T2 b) {     T1 res = 1;     for (long long i = 1; i < b + 1; i++) res = res * a;     return res;   }   int32_t main() {     std::ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long n, l, r, x;     while (cin >> n >> l >> r >> x) {       vector<long long> k(n);       for (long long i = 0; i < n; i++) {         cin >> k[i];       }       long long cnt = 0;       for (long long i = 0; i < (1 << n); i++) {         long long mi = 1000000;         long long ma = -1;         long long sum = 0;         for (long long j = 0; j < n; j++) {           if (i & (1 << j)) {             sum = sum + k[j];             ma = max(ma, k[j]);             mi = min(mi, k[j]);           }         }         if (l <= sum && sum <= r && (ma - mi) >= x) {           cnt++;         }       }       cout << cnt << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1000 + 5;   int N, P[MAXN];   long long D[MAXN];   vector<pair<long long, int>> v, f[2];   int main() {     scanf( %d , &N);     P[1] = 1, P[2] = 2;     for (int i = 3, x; i <= N; ++i) {       printf( 2 1 %d %d n , P[2], i);       fflush(stdout);       scanf( %d , &x);       if (x == -1) P[2] = i;     }     for (int i = 2; i <= N; ++i) {       if (i == P[2]) continue;       printf( 1 1 %d %d n , P[2], i);       fflush(stdout);       scanf( %lld , &D[i]);       v.push_back(make_pair(D[i], i));     }     sort(v.begin(), v.end());     int p = v.back().second;     for (int i = 2, x; i <= N; ++i) {       if (i == p || i == P[2]) continue;       printf( 2 1 %d %d n , p, i);       fflush(stdout);       scanf( %d , &x);       f[x == -1].push_back(make_pair(D[i], i));     }     sort(f[0].begin(), f[0].end());     sort(f[1].begin(), f[1].end());     printf( 0 %d %d  , P[1], P[2]);     for (int i = 0; i < f[1].size(); ++i) printf( %d  , f[1][i].second);     printf( %d  , p);     for (int i = f[0].size() - 1; i >= 0; --i) printf( %d  , f[0][i].second);     fflush(stdout);     return 0;   }


#include <bits/stdc++.h>   const long double eps = 1e-9;   const long long mod = 1e9 + 7, ll_max = (long long)1e18;   const int MX = 2e5 + 10, int_max = 0x3f3f3f3f;   using namespace std;   mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());   long long n, k;   long long arr[MX];   int solve() {     cin >> n >> k;     for (int i = 0; i < k; i++) {       arr[i] += i + 1;       n -= i + 1;     }     if (n < 0) return 0;     for (int i = 0; i < k; i++) {       arr[i] += (n / k);     }     n %= k;     for (int i = k - 1; i > 0; i--) {       int del = min(2 * arr[i - 1] - arr[i], n);       n -= del;       arr[i] += del;     }     return (n == 0);   }   int main() {     cin.tie(0)->sync_with_stdio(0);     if (solve()) {       cout <<  YES n ;       for (int i = 0; i < k; i++) cout << arr[i] <<    ;       cout <<   n ;     } else {       cout <<  NO n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   long long x[300001];   long long p[300001] = {1};   int main() {     std::ios::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> x[i];       p[i + 1] = p[i] * 2 % MOD;     }     sort(x, x + n);     long long ret = 0;     for (int i = 1; i <= n; ++i) {       ret = (ret + x[i - 1] * (p[i - 1] - p[n - i])) % MOD;     }     cout << (ret + MOD) % MOD;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long int> v, v1, v2;   multiset<long long int> mss;   multiset<long long int>::iterator it;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int a, b, c, d, i, j, z = 0, k, w = 1, q = 0, cnt = 0, x = 0, p = 0;     cin >> a >> c;     for (i = 0; i < a; i++) {       cin >> b;       v.push_back(b);     }     sort(v.begin(), v.end());     for (i = 0; i < a / 2; i++) {       v1.push_back(v[i]);     }     for (j = i; j < a; j++) {       mss.insert(v[j]);     }     for (i = 0; i < v1.size(); i++) {       d = v1[i] + c;       it = mss.lower_bound(d);       if (it == mss.end()) {         break;       }       cnt++;       mss.erase(it);     }     cout << cnt;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool cmp(int x, int y) { return x > y; }   const int maxn = 3e5 + 100;   vector<int> v[maxn];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     for (int i = 2; i < maxn; i++)       for (int j = i; j < maxn; j += i) v[j].push_back(i);     v[1].push_back(1);     int t;     cin >> t;     while (t--) {       int n;       char c;       cin >> n >> c;       string s;       cin >> s;       int check[n + 1];       memset(check, 0, sizeof(check));       int cnt = 0;       for (int i = 0; i < n; i++)         if (s[i] != c) {           cnt++;           for (auto j : v[i + 1]) check[j] = 1;         }       if (!cnt) {         cout << 0 << endl;         continue;       }       int cc = 0;       for (int i = 2; i <= n; i++)         if (!check[i] && s[i - 1] == c) cc = i;       if (!cc)         cout << 2 << endl << n <<     << n - 1 << endl;       else         cout << 1 << endl << cc << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   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;     }   };   void test_case() {     int n, k;     cin >> n >> k;     vector<pair<int, int>> a(n);     for (int i = 0; i < n; i++) {       cin >> a[i].first;       a[i].second = i;     }     sort(a.begin(), a.end(), less<pair<int, int>>());     int start = 0, tot = 0;     vector<int> res;     while (start < n && k - a[start].first >= 0) {       debug() <<   [                <<  k                    :                 << (k) <<  ]  ;       tot++;       k = k - a[start].first;       res.push_back(a[start].second + 1);       start++;     }     cout << tot << endl;     for (int x : res) {       cout << x <<    ;     }     cout << endl;   }   int main() {     int t = 1;     for (int i = 0; i < t; i++) test_case();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long a, c;     cin >> a >> c;     vector<long long> aa;     while (a) {       aa.push_back(a % 3);       a /= 3;     }     vector<long long> cc;     while (c) {       cc.push_back(c % 3);       c /= 3;     }     long long i = 0, j = 0;     vector<long long> res;     while (i < aa.size() && j < cc.size()) {       res.push_back((cc[j] - aa[i] + 3) % 3);       i++;       j++;     }     while (i < aa.size()) {       res.push_back((3 - aa[i]) % 3);       i++;     }     while (j < cc.size()) {       res.push_back(cc[j] % 3);       j++;     }     long long p = 1;     long long ans = 0;     for (int i = 0; i < res.size(); i++) {       ans += (res[i] * (p));       p *= 3;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int sett(int n, int pos) { return n = n | (1 << pos); }   int reset(int n, int pos) { return n = n & ~(1 << pos); }   bool check(int n, int pos) { return (bool)(n & (1 << pos)); }   template <typename T>   T bigmod(T x, T p) {     if (p == 0) return 1;     if (p & 1) {       return x * bigmod(x, p - 1) % 1000000007;     } else {       T tmp = bigmod(x, p >> 1) % 1000000007;       return tmp * tmp % 1000000007;     }   }   template <typename T>   void print(const T& v) {     cerr << v <<    ;   }   template <typename T1, typename... T2>   void print(const T1& first, const T2&... rest) {     print(first);     print(rest...);   }   const int inf = (1 << 30);   int a[2000123 + 4];   struct dat {     long long val, id;     dat(long long a, long long b) {       val = a;       id = b;     };     bool operator<(dat x) const { return val == x.val ? id < x.id : val < x.val; }   };   set<dat> st;   vector<dat> v;   bool compare(dat x, dat y) { return x.id < y.id; }   void func(dat x) { cout << x.val <<     << x.id <<   n ; }   void solve() {     set<dat>::iterator it, it2, it3;     while (1) {       if (st.size() < 2) break;       it = st.begin();       it2 = st.begin();       it2++;       if (it->val != it2->val) {         v.push_back(*it);         st.erase(it);       } else {         dat x = dat(it->val + it2->val, it2->id);         st.erase(it);         it = st.begin();         st.erase(it);         st.insert(x);       }     }     if (st.size() == 1) {       v.push_back(*(st.begin()));     }     sort(v.begin(), v.end(), compare);     cout << v.size() <<   n ;     for (int i = 0; i < v.size(); i++) cout << v[i].val <<    ;   }   int main() {     long long cnt = 1, n, qr, k;     scanf( %I64d , &n);     for (int i = 1; i <= (int)n; i++) {       scanf( %I64d , &k);       st.insert(dat(k, (long long)i));     }     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<int, vector<int> > mx;   map<int, vector<int> > my;   int pow(int x) {     long long i = 2;     long long r = 1;     while (x) {       if (x & 1) r = (r * i) % 1000000007;       i = (i * i) % 1000000007;       x = x >> 1;     }     return r;   }   int main() {     ios::sync_with_stdio(false);     int i, j, n, t;     cin >> n;     int x[n];     int y[n];     long long an = 1;     for (i = 0; i < n; i++) {       cin >> x[i] >> y[i];       mx[x[i]].push_back(i);       my[y[i]].push_back(i);     }     bool visit[i];     memset(visit, 0, i);     set<int> sx;     set<int> sy;     int c;     queue<int> q;     for (i = 0; i < n; i++)       if (!visit[i]) {         c = 0;         sx.clear();         sy.clear();         for (j = 0; j < mx[x[i]].size(); j++) {           c++;           q.push(mx[x[i]][j]);           visit[mx[x[i]][j]] = 1;         }         sx.insert(x[i]);         for (j = 0; j < my[y[i]].size(); j++) {           c++;           q.push(my[y[i]][j]);           visit[my[y[i]][j]] = 1;         }         sy.insert(y[i]);         t = i;         while (!q.empty()) {           i = q.front();           q.pop();           if (sx.find(x[i]) == sx.end())             for (j = 0; j < mx[x[i]].size(); j++) {               c++;               q.push(mx[x[i]][j]);               visit[mx[x[i]][j]] = 1;             }           sx.insert(x[i]);           if (sy.find(y[i]) == sy.end())             for (j = 0; j < my[y[i]].size(); j++) {               c++;               q.push(my[y[i]][j]);               visit[my[y[i]][j]] = 1;             }           sy.insert(y[i]);         }         i = t;         if (c / 2 < sx.size() + sy.size())           an = (an * (pow(sx.size() + sy.size()) + 1000000007 - 1)) % 1000000007;         else           an = (an * pow(sx.size() + sy.size())) % 1000000007;       }     cout << an;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int n, d, m;     cin >> n >> m >> d;     int c[m], sum = 0;     for (int i = 0; i < m; i++) {       cin >> c[i];       sum += c[i];     }     d--;     if (sum + (m + 1) * d >= n) {       cout <<  YES  << endl;       int k = (d > 0 ? (n - sum) / d : 0), res = (d > 0 ? (n - sum) % d : 0),           cnt = 0, id = 0;       bool flag = 1;       for (int i = 1; i <= n; i++) {         int j;         if (cnt == k && res) {           k++, d = res;           res = 0;         }         if (cnt < k && flag) {           for (j = i; j < i + d && j <= n; j++) {             cout << 0 <<    ;           }           cnt++;         } else {           for (j = i; j < i + c[id]; j++) {             cout << id + 1 <<    ;           }           id++;         }         flag = !flag;         i = j - 1;       }       cout << endl;     } else       cout <<  NO  << endl;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t = 1;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2e6 + 7;   const int ALPHA = 40;   const long long INF = 1e16 + 7;   const int MOD = 1e9 + 7;   const int LOG = 22;   const int BASE[2] = {313, 239};   long long n, d;   bool ns[MAXN], seen[MAXN];   vector<long long> group[MAXN], plast[MAXN];   void solve() {     cin >> n >> d;     for (int i = 0; i < n; i++) {       group[i].clear();       plast[i].clear();     }     for (int i = 0; i < n; i++) cin >> ns[i];     fill(seen, seen + n, 0);     long long ptr = 0, cnt = 0;     for (int i = 0; i < n; i++) {       ptr = i;       if (seen[ptr]) continue;       while (!seen[ptr]) {         seen[ptr] = 1;         group[cnt].push_back(ns[ptr]);         ptr = (ptr + d) % n;       }       cnt++;     }     for (int i = 0; i < cnt; i++) {       long long k = group[i].size();       for (int j = 0; j < k; j++) group[i].push_back(group[i][j]);       long long last = -INF;       for (int j = 0; j < group[i].size(); j++) {         if (!group[i][j]) last = j;         plast[i].push_back(j - last);       }     }     long long maxim = 0;     for (int i = 0; i < cnt; i++) {       for (int j = 0; j < group[i].size() / 2; j++) {         maxim = max(maxim, min(plast[i][j], plast[i][j + (group[i].size() / 2)]));       }     }     if (maxim >= INF)       cout << -1 <<   n ;     else       cout << maxim <<   n ;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     ;     int t;     cin >> t;     while (t--) solve();   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 3000 + 50;   vector<int> edge[maxn];   int sub[maxn][maxn], fa[maxn][maxn];   long long n, ans, dp[maxn][maxn];   void dfs(int root, int k, int dad) {     sub[root][k]++;     fa[root][k] = dad;     for (auto t : edge[k]) {       if (t != dad) {         dfs(root, t, k);         sub[root][k] += sub[root][t];       }     }   }   long long DP(int a, int b) {     if (a == b) return 0;     if (dp[a][b] != -1) return dp[a][b];     return dp[a][b] =                sub[a][b] * sub[b][a] + max(DP(fa[a][b], a), DP(fa[b][a], b));   }   int main() {     cin >> n;     memset(dp, -1, sizeof(dp));     for (int i = 1; i < n; i++) {       int u, v;       cin >> u >> v;       edge[u].push_back(v);       edge[v].push_back(u);     }     for (int i = 1; i <= n; i++) dfs(i, i, -1);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++)         if (i != j) ans = max(ans, DP(i, j));     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int i, n;     long long a, sum, ans;     priority_queue<long long, vector<long long>, greater<long long> > Q;     scanf( %d , &n);     for (i = 0; i < n; i++) {       scanf( %lld , &a);       Q.push(a);     }     if (n % 2 == 0) Q.push(0);     ans = 0;     while (Q.size() > 2) {       sum = Q.top();       Q.pop();       sum += Q.top();       Q.pop();       sum += Q.top();       Q.pop();       ans += sum;       Q.push(sum);     }     printf( %lld n , ans);     return 0;   }