#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 9, mod = 998244353;   template <const int32_t MOD>   struct modint {     int32_t value;     modint() = default;     modint(int32_t value_) : value(value_) {}     inline modint<MOD> operator+(modint<MOD> other) const {       int32_t c = this->value + other.value;       return modint<MOD>(c >= MOD ? c - MOD : c);     }     inline modint<MOD> operator-(modint<MOD> other) const {       int32_t c = this->value - other.value;       return modint<MOD>(c < 0 ? c + MOD : c);     }     inline modint<MOD> operator*(modint<MOD> other) const {       int32_t c = (int64_t)this->value * other.value % MOD;       return modint<MOD>(c < 0 ? c + MOD : c);     }     inline modint<MOD> &operator+=(modint<MOD> other) {       this->value += other.value;       if (this->value >= MOD) this->value -= MOD;       return *this;     }     inline modint<MOD> &operator-=(modint<MOD> other) {       this->value -= other.value;       if (this->value < 0) this->value += MOD;       return *this;     }     inline modint<MOD> &operator*=(modint<MOD> other) {       this->value = (int64_t)this->value * other.value % MOD;       if (this->value < 0) this->value += MOD;       return *this;     }     inline modint<MOD> operator-() const {       return modint<MOD>(this->value ? MOD - this->value : 0);     }     modint<MOD> pow(uint64_t k) const {       modint<MOD> x = *this, y = 1;       for (; k; k >>= 1) {         if (k & 1) y *= x;         x *= x;       }       return y;     }     modint<MOD> inv() const { return pow(MOD - 2); }     inline modint<MOD> operator/(modint<MOD> other) const {       return *this * other.inv();     }     inline modint<MOD> operator/=(modint<MOD> other) {       return *this *= other.inv();     }     inline bool operator==(modint<MOD> other) const {       return value == other.value;     }     inline bool operator!=(modint<MOD> other) const {       return value != other.value;     }     inline bool operator<(modint<MOD> other) const { return value < other.value; }     inline bool operator>(modint<MOD> other) const { return value > other.value; }   };   template <int32_t MOD>   modint<MOD> operator*(int64_t value, modint<MOD> n) {     return modint<MOD>(value) * n;   }   template <int32_t MOD>   modint<MOD> operator*(int32_t value, modint<MOD> n) {     return modint<MOD>(value % MOD) * n;   }   template <int32_t MOD>   istream &operator>>(istream &in, modint<MOD> &n) {     return in >> n.value;   }   template <int32_t MOD>   ostream &operator<<(ostream &out, modint<MOD> n) {     return out << n.value;   }   using mint = modint<mod>;   vector<mint> Thomas(vector<vector<mint>> a) {     int n = a.size();     for (int i = 1; i < n; i++) {       mint x = a[i][0] * a[i - 1][1].inv();       a[i][1] -= x * a[i - 1][2];       a[i][3] -= x * a[i - 1][3];     }     for (int i = n - 2; i >= 0; i--) {       mint x = a[i][2] * a[i + 1][1].inv();       a[i][3] -= x * a[i + 1][3];     }     vector<mint> ans;     for (int i = 0; i < n; i++) {       ans.push_back(a[i][3] * a[i][1].inv());     }     return ans;   }   int a[N];   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     mint m = 0;     for (int i = 0; i < n; i++) cin >> a[i], m += a[i];     vector<vector<mint>> mat(m.value + 1);     for (int i = 0; i <= m.value; i++) {       vector<mint> v(4, 0);       if (i) v[0] = mint(i) / m;       v[1] = (m - i) * (n - 2) / m / (n - 1) - 1;       if (i + 1 <= m.value) v[2] = (m - i) / m / (n - 1);       v[3] = mint(-i) / m;       mat[i] = v;     }     vector<mint> f = Thomas(mat);     mint ans = 0;     for (int i = 0; i < n; i++) ans += f[a[i]];     ans -= (n - 1) * f[0] + f[m.value];     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a[105], ma = 0, mi = 110, maxi, mini, tc;     cin >> tc;     for (int i = 0; i < tc; i++) {       cin >> a[i];       if (a[i] > ma) {         ma = a[i];         maxi = i;       }       if (a[i] <= mi) {         mi = a[i];         mini = i;       }     }     if (maxi > mini) {       mini++;     }     cout << maxi + (tc - 1) - mini << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     char c;     int n, b = 0, a = 0;     cin >> n;     while (n > 0) {       cin >> c;       if (c ==  A )         a++;       else         b++;       n--;     }     if (a > b)       cout <<  Anton ;     else if (a < b)       cout <<  Danik ;     else       cout <<  Friendship ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e9 + 7;   const int N = 5e5 + 10;   int a[N];   int dp[2][N];   int minv[N];   int sum[N];   int n, k, p;   class BIT {     int a[110];       public:     void init() { memset((a), (0x1f), sizeof(a)); }     inline int lowbit(int x) { return x & -x; }     void add(int x, int v) {       x = p - x;       while (x < 110) {         a[x] = min(a[x], v);         x += lowbit(x);       }     }     int sum(int x) {       int ret = INF;       while (x) {         ret = min(ret, a[x]);         x -= lowbit(x);       }       return ret;     }   } b;   template <class T>   inline void readint(T &ret) {     char c;     ret = 0;     while ((c = getchar()) <  0  || c >  9 )       ;     while (c >=  0  && c <=  9 ) {       ret = ret * 10 + (c -  0 ), c = getchar();     }   }   int main() {     readint(n);     readint(k);     readint(p);     int o = 0;     for (int i = 1; i <= n; i++) {       readint(a[i]);       a[i] %= p;       sum[i] = sum[i - 1] + a[i];       dp[o][i] = (a[i] + dp[o][i - 1]) % p;     }     for (int i = 2; i <= k; i++) {       o ^= 1;       b.init();       memset((dp[o]), (0x1f), sizeof(dp[o]));       b.add(0, sum[i - 1]);       int tot = 0;       for (int j = i; j <= n; j++) {         tot = (tot + a[j]);         if (tot >= p) tot -= p;         dp[o][j] = min(b.sum(tot) - p, b.sum(p)) + tot;         int t = (p - tot);         if (t == p) t = 0;         b.add(t, dp[o ^ 1][j] + t);       }     }     printf( %d n , dp[o][n]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k, s[444], dp[444][55][444];   int main() {     cin >> n >> k;     s[0] = 0;     for (int i = 1; i <= n; i++) cin >> s[i], s[i] += s[i - 1];     int ans = 0;     for (int i = 1; i <= n; i++)       for (int j = 2; j <= min(i, k); j++)         for (int l = 1; l <= i - j + 1; l++)           for (int h = 1; h <= i - l - j + 2; h++)             dp[i][j][l] =                 max(dp[i][j][l], dp[i - l][j - 1][h] +                                      abs(s[i] + s[i - l - h] - 2 * s[i - l]));     for (int i = 1; i <= n; i++)       for (int j = 1; j <= i - k + 1; j++) ans = max(ans, dp[i][k][j]);     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<string, int> m;   int main() {     int n;     cin >> n;     string h;     cin >> h;     m[h]++;     for (int i = 0; i < n; i++) {       string s;       cin >> s;       string a =   , b =   ;       for (int i = 0; i < 3; i++) a += s[i];       for (int i = 5; i < 8; i++) b += s[i];       m[a]++;       m[b]++;     }     cout << (m[h] % 2 ?  home  :  contest ) << endl;   }


#include <bits/stdc++.h>   using namespace std;   bool check(int hr, int min) {     if (hr % 10 == 7 || hr / 10 == 7 || min % 10 == 7) {       return true;     }     return false;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int x, hr, min, res = 0;     cin >> x >> hr >> min;     while (!check(hr, min)) {       min -= x;       if (min < 0) {         min += 60;         hr--;       }       if (hr < 0) {         hr += 24;       }       res++;     }     cout << res <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   unsigned long long k, n, i, j, m;   unsigned long long v[105][105];   vector<unsigned long long> l;   int main() {     cin >> n >> m;     for (i = 1; i <= n; i++)       for (j = 1; j <= m; j++) {         cin >> v[i][j];         k = max(k, v[i][j]);       }     k++;     if (n == 1) {       cout <<  YES n ;       cout << k <<   n ;       cout << 0 <<   n ;       for (i = 1; i <= m; i++) cout << v[1][i] <<    ;       cout <<   n ;       return 0;     }     if (m == 1) {       cout <<  YES n ;       cout << k <<   n ;       for (i = 1; i <= n; i++) cout << v[i][1] <<    ;       cout <<   n ;       cout << 0 <<   n ;       return 0;     }     unsigned long long a = v[1][1] + v[2][2];     unsigned long long b = v[1][2] + v[2][1];     unsigned long long j = max(a, b) - min(a, b);     if (j == 0) j = k;     if (k > j) {       cout <<  NO n ;       return 0;     }     k = j;     unsigned long long x = 0;     l.push_back(x);     for (i = 2; i <= n; i++) {       if (v[i][1] < v[i - 1][1])         x = (x + k - v[i - 1][1] + v[i][1]) % k;       else         x = (x + v[i][1] - v[i - 1][1]) % k;       l.push_back(x);     }     for (i = 0; i < l.size(); i++) {       for (j = 1; j <= m; j++) {         if ((l[i] + v[1][j]) % k != v[i + 1][j]) {           cout <<  NO n ;           return 0;         }       }     }     cout <<  YES n ;     cout << k <<   n ;     for (i = 0; i < l.size(); i++) cout << l[i] <<    ;     cout <<   n ;     for (i = 1; i <= m; i++) cout << v[1][i] <<    ;     cout <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     if (n > m) swap(n, m);     cout << n + 1 << endl;     for (m = 0; n >= 0; m++, n--) cout << n <<     << m << endl;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   void print(vector<T> v) {     cerr <<  [ ;     if (v.size()) cerr << v[0];     for (int i = (1); i < (v.size()); ++i) cerr <<  ,   << v[i];     cerr <<  ] n ;   }   template <class T>   string i2s(T x) {     ostringstream o;     o << x;     return o.str();   }   vector<string> splt(string s, char c =    ) {     vector<string> all;     int p = 0, np;     while (np = s.find(c, p), np >= 0) {       if (np != p) all.push_back(s.substr(p, np - p));       p = np + 1;     }     if (p < s.size()) all.push_back(s.substr(p));     return all;   }   int h, w;   int data[1700][1700];   int edge[1700][1700];   int dr[] = {-1, 1, 0, 0, -1, -1, 1, 1};   int dc[] = {0, 0, -1, 1, -1, 1, -1, 1};   int main() {     cin >> h >> w;     for (int i = (0); i < (h); ++i)       for (int j = (0); j < (w); ++j) scanf( %d , &data[i][j]);     for (int r = (0); r < (h); ++r)       for (int c = (0); c < (w); ++c)         if (data[r][c]) {           for (int d = (0); d < (4); ++d) {             int nr = r + dr[d];             int nc = c + dc[d];             if (nr < 0 || nr >= h || nc < 0 || nc >= w || data[nr][nc] == 0)               edge[r][c] = 1;           }         }     vector<int> rays;     for (int r = (0); r < (h); ++r)       for (int c = (0); c < (w); ++c)         if (edge[r][c]) {           vector<pair<int, int> > v;           int cr = r;           int cc = c;           while (edge[cr][cc]) {             v.push_back(make_pair(cr, cc));             edge[cr][cc] = 0;             for (int d = (0); d < (8); ++d) {               int nr = cr + dr[d];               int nc = cc + dc[d];               if (nr >= 0 && nr < h && nc >= 0 && nc < w && edge[nr][nc]) {                 cr = nr;                 cc = nc;                 break;               }             }           }           int bv = -1;           int bp = -1;           for (int i = (0); i < (v.size()); ++i)             for (int j = (15); j < (110); ++j) {               int sx = v[i].first;               int sy = v[i].second;               int ex = v[(i + j) % v.size()].first;               int ey = v[(i + j) % v.size()].second;               if (abs(sx - ex) > 35 || abs(sy - ey) > 35) break;               if (abs(sx - ex) <= 5 && abs(sy - ey) <= 5 &&                   abs(sx - ex) + abs(sy - ey) <= 7) {                 if (j > bv) {                   bv = j;                   bp = i;                 }               }             }           int count = 0;           for (int i = (0); i < (v.size()); ++i)             for (int j = (15); j < (110); ++j) {               int sx = v[(i + bp) % v.size()].first;               int sy = v[(i + bp) % v.size()].second;               int ex = v[(i + j + bp) % v.size()].first;               int ey = v[(i + j + bp) % v.size()].second;               if (i + j > v.size()) break;               if (abs(sx - ex) > 35 || abs(sy - ey) > 35) break;               if (abs(sx - ex) <= 5 && abs(sy - ey) <= 5 &&                   abs(sx - ex) + abs(sy - ey) <= 7) {                 count++;                 i += j;                 break;               }             }           if (count) rays.push_back(count);         }     sort(rays.begin(), rays.end());     cout << rays.size() << endl;     for (int i = (0); i < (rays.size()); ++i) cout << rays[i] <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k, p, i;   struct reg {     int x, y, z;   } v[100100];   inline bool cmp1(reg a, reg b) {     if (a.x != b.x) return a.x > b.x;     return a.y > b.y;   }   inline bool cmp2(reg a, reg b) { return a.y < b.y; }   char s[5000];   int main() {     int n, sol = 0;     cin >> n;     cin >> (s + 1);     for (i = n + 1; s[i]; i += n)       if (s[i - 1] == s[i - 2] && s[i - 2] == s[i - 3]) ++sol;     cout << sol <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, a, b, mas[30], summ[100005];   long long sum, countt;   string s;   map<pair<char, long long>, int> matr;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     for (int i = 0; i < 26; ++i) cin >> mas[i];     cin >> s;     for (int i = 0; i < s.size(); ++i) {       if (matr.count(make_pair(s[i], sum))) countt += matr[make_pair(s[i], sum)];       sum += mas[s[i] -  a ];       matr[make_pair(s[i], sum)]++;     }     cout << countt;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF_INT = 2147483647;   const long long INF_LL = 9223372036854775807LL;   int gcd(int a, int b) { return ((b == 0) ? a : gcd(b, a % b)); }   int lcm(int a, int b) { return a * b / gcd(a, b); }   void solve() {     int n;     cin >> n;     long long tmp = 1;     while (n) {       if (tmp % 3 == 0 || tmp % 10 == 3) {         tmp++;         continue;       }       tmp++;       n--;     }     cout << tmp - 1 << endl;   }   int main() {     ios::sync_with_stdio();     cin.tie();     ;     int t;     cin >> t;     while (t--) solve();   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using pii = pair<int, int>;   template <class T>   using V = vector<T>;   template <class T>   using VV = V<V<T>>;   constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }   template <class T, class U>   void chmin(T &t, const U &u) {     if (t > u) t = u;   }   template <class T, class U>   void chmax(T &t, const U &u) {     if (t < u) t = u;   }   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 < (v.size()); i++) {       if (i) os <<  , ;       os << v[i];     }     os <<  } ;     return os;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int tc;     cin >> tc;     while (tc--) {       int n, m;       cin >> n >> m;       V<int> ans;       VV<int> g(n);       for (int i = 0; i < (m); i++) {         int a, b;         cin >> a >> b;         --a, --b;         g[a].push_back(b);       }       VV<int> st(n, V<int>(3));       for (int i = 0; i < (n); i++) {         int c;         if (st[i][1]) {           c = 2;         } else if (st[i][0]) {           c = 1;         } else {           c = 0;         }         if (c == 2) ans.push_back(i);         for (int to : g[i]) {           st[to][c] = 1;         }       }       cout << ans.size() <<   n ;       for (auto v : ans) cout << v + 1 <<    ;       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   const long long INF = LLONG_MAX;   int main() {     string s;     cin >> s;     int x = s.size();     cout << 26 * (x + 1) - x;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void Freopen() {     freopen(          title           .in ,          r , stdin);     freopen(          title           .out ,          w , stdout);   }   int read() {     int g = 0, f = 1;     char ch = getchar();     while (ch <  0  ||  9  < ch) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while ( 0  <= ch && ch <=  9 ) {       g = g * 10 + ch -  0 ;       ch = getchar();     }     return g * f;   }   const int N = 5e5 + 5;   const int mod = 1e9 + 7;   int st[N][20], dep[N], vis[N], ta, tb, top, sa[N], sb[N], sta[N];   vector<int> G[N], F[N], R[N], C[N], size[N];   int n, fir[N], las[N], fac[N], du[N];   unordered_map<int, int> M[N];   void dfs(int x, int fa) {     st[x][0] = fa;     dep[x] = dep[fa] + 1;     for (int i = (1); i <= (19); i++)       if (st[x][i - 1])         st[x][i] = st[st[x][i - 1]][i - 1];       else         break;     for (auto y : G[x])       if (y ^ fa) dfs(y, x);   }   int Lca(int x, int y) {     if (dep[x] < dep[y]) swap(x, y);     for (int i = (19); i >= (0); i--)       if (dep[st[x][i]] >= dep[y]) x = st[x][i];     if (x == y) return x;     for (int i = (19); i >= (0); i--)       if (st[x][i] != st[y][i]) x = st[x][i], y = st[y][i];     return st[x][0];   }   int get(int x, vector<int>& f) { return f[x] == x ? x : f[x] = get(f[x], f); }   bool Merge(int x, int y, vector<int>& f, vector<int>& size, vector<int>& ru,              vector<int>& cu) {     cu[x]++, ru[y]++;     if (cu[x] > 1 || ru[y] > 1) return false;     int fx = get(x, f), fy = get(y, f);     if (fx == fy) return false;     size[fx] += size[fy];     f[fy] = fx;     return true;   }   signed main() {     n = read();     memset(fir, -1, sizeof(fir));     memset(las, -1, sizeof(las));     if (n == 1) return cout << 1, signed();     fac[0] = 1;     for (int i = (1); i <= (n); i++) fac[i] = 1ll * fac[i - 1] * i % mod;     for (int i = (1); i <= (n - 1); i++) {       int x = read(), y = read();       G[x].push_back(y), G[y].push_back(x);       M[x][y] = G[x].size() - 1, M[y][x] = G[y].size() - 1;       F[x].push_back(G[x].size() - 1), F[y].push_back(G[y].size() - 1);       size[x].push_back(1), size[y].push_back(1);       R[x].push_back(0), R[y].push_back(0);       C[x].push_back(0), C[y].push_back(0);       du[x]++, du[y]++;     }     dfs(1, 0);     for (int x = (1); x <= (n); x++) {       int y = read();       if (y == 0) continue;       int anc = Lca(x, y);       if (x == y) return cout << 0, signed();       if (vis[y]) return cout << 0, signed();       vis[y] = 1;       ta = tb = top = 0;       for (int t = x; t != anc; t = st[t][0]) sa[++ta] = t;       for (int t = y; t != anc; t = st[t][0]) sb[++tb] = t;       for (int i = (1); i <= (tb); i++) sta[++top] = sb[i];       sta[++top] = anc;       for (int i = (ta); i >= (1); i--) sta[++top] = sa[i];       for (int i = (2); i <= (top - 1); i++) {         int ipre = M[sta[i]][sta[i - 1]], isuf = M[sta[i]][sta[i + 1]];         if (!Merge(ipre, isuf, F[sta[i]], size[sta[i]], R[sta[i]], C[sta[i]]))           return cout << 0, signed();       }       fir[sta[1]] = M[sta[1]][sta[2]];       las[sta[top]] = M[sta[top]][sta[top - 1]];       for (int i = (1); i <= (top); i++) {         if (fir[sta[i]] >= 0 && las[sta[i]] >= 0 &&             get(fir[sta[i]], F[sta[i]]) == get(las[sta[i]], F[sta[i]]) &&             size[sta[i]][get(fir[sta[i]], F[sta[i]])] != du[sta[i]])           return cout << 0, signed();       }     }     int ans = 1;     for (int i = (1); i <= (n); i++) {       for (int j = (0); j <= (du[i] - 1); j++) vis[j] = 0;       int cnt = 0;       for (int j = (0); j <= (du[i] - 1); j++) {         int x = get(j, F[i]);         if (vis[x]) continue;         vis[x] = 1;         cnt++;       }       if (fir[i] >= 0) cnt--;       if (las[i] >= 0) cnt--;       if (fir[i] >= 0 && las[i] >= 0 && get(fir[i], F[i]) == get(las[i], F[i]))         cnt++;       ans = 1ll * ans * fac[cnt] % mod;     }     return cout << ans, signed();   }


#include <bits/stdc++.h>   using namespace std;   void __print(int x) { cerr << x; }   void __print(long x) { cerr << x; }   void __print(long long x) { cerr << x; }   void __print(unsigned x) { cerr << x; }   void __print(unsigned long x) { cerr << x; }   void __print(unsigned long long x) { cerr << x; }   void __print(float x) { cerr << x; }   void __print(double x) { cerr << x; }   void __print(long double x) { cerr << x; }   void __print(char x) { cerr <<      << x <<     ; }   void __print(const char *x) { cerr <<     << x <<    ; }   void __print(const string &x) { cerr <<     << x <<    ; }   void __print(bool x) { cerr << (x ?  true  :  false ); }   template <typename T, typename V>   void __print(const pair<T, V> &x) {     cerr <<  { ;     __print(x.first);     cerr <<  , ;     __print(x.second);     cerr <<  } ;   }   template <typename T>   void __print(const T &x) {     int f = 0;     cerr <<  { ;     for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i);     cerr <<  } ;   }   void _print() { cerr <<  ] n ; }   template <typename T, typename... V>   void _print(T t, V... v) {     __print(t);     if (sizeof...(v)) cerr <<  ,  ;     _print(v...);   }   template <typename T>   void printVec(const T &v, char sep =    ) {     for (auto &i : v) cout << i << sep;   }   template <typename T>   void printVecPair(const T &v, char sep =    ) {     for (auto &i : v) cout << i.first <<     << i.second << sep;   }   struct Points {     int x1, y1, x2, y2, x3, y3;   };   void solve() {     int n, m;     cin >> n >> m;     vector<string> g(n);     for (int i = 0; i < n; i++) {       cin >> g[i];     }     vector<Points> ans;     for (int i = 0; i < n - 1; i++) {       for (int j = 0; j < m - 1; j++) {         if (g[i][j] ==  1 ) {           g[i][j] =  0 ;           ans.push_back({i, j, i, j + 1, i + 1, j});           g[i][j + 1] = (g[i][j + 1] ==  1  ?  0  :  1 );           g[i + 1][j] = (g[i + 1][j] ==  1  ?  0  :  1 );         }       }     }     for (int i = 0; i < n - 1; i += 2) {       int cntOne = 0;       cntOne += (g[i][m - 1] ==  1 );       cntOne += (g[i + 1][m - 1] ==  1 );       if (cntOne == 1) {         if (g[i][m - 1] ==  1 ) {           ans.push_back({i, m - 1, i, m - 2, i + 1, m - 2});           ans.push_back({i, m - 1, i + 1, m - 1, i + 1, m - 2});           ans.push_back({i, m - 1, i, m - 2, i + 1, m - 1});           for (int i = ans.size() - 3; i < ans.size(); i++) {             g[ans[i].x1][ans[i].y1] =                 (g[ans[i].x1][ans[i].y1] ==  1  ?  0  :  1 );             g[ans[i].x2][ans[i].y2] =                 (g[ans[i].x2][ans[i].y2] ==  1  ?  0  :  1 );             g[ans[i].x3][ans[i].y3] =                 (g[ans[i].x3][ans[i].y3] ==  1  ?  0  :  1 );           }         } else {           ans.push_back({i, m - 2, i, m - 1, i + 1, m - 1});           ans.push_back({i, m - 1, i + 1, m - 1, i + 1, m - 2});           ans.push_back({i, m - 2, i + 1, m - 1, i + 1, m - 2});           for (int i = ans.size() - 3; i < ans.size(); i++) {             g[ans[i].x1][ans[i].y1] =                 (g[ans[i].x1][ans[i].y1] ==  1  ?  0  :  1 );             g[ans[i].x2][ans[i].y2] =                 (g[ans[i].x2][ans[i].y2] ==  1  ?  0  :  1 );             g[ans[i].x3][ans[i].y3] =                 (g[ans[i].x3][ans[i].y3] ==  1  ?  0  :  1 );           }         }       } else if (cntOne == 2) {         ans.push_back({i, m - 2, i + 1, m - 2, i + 1, m - 1});         ans.push_back({i, m - 2, i, m - 1, i + 1, m - 2});         for (int i = ans.size() - 2; i < ans.size(); i++) {           g[ans[i].x1][ans[i].y1] = (g[ans[i].x1][ans[i].y1] ==  1  ?  0  :  1 );           g[ans[i].x2][ans[i].y2] = (g[ans[i].x2][ans[i].y2] ==  1  ?  0  :  1 );           g[ans[i].x3][ans[i].y3] = (g[ans[i].x3][ans[i].y3] ==  1  ?  0  :  1 );         }       }     }     for (int i = 0; i < m - 1; i += 2) {       int cntOne = 0;       cntOne += (g[n - 1][i] ==  1 );       cntOne += (g[n - 1][i + 1] ==  1 );       if (cntOne == 1) {         if (g[n - 1][i] ==  1 ) {           ans.push_back({n - 2, i, n - 1, i, n - 2, i + 1});           ans.push_back({n - 2, i, n - 1, i, n - 1, i + 1});           ans.push_back({n - 2, i + 1, n - 1, i + 1, n - 1, i});           for (int i = ans.size() - 3; i < ans.size(); i++) {             g[ans[i].x1][ans[i].y1] =                 (g[ans[i].x1][ans[i].y1] ==  1  ?  0  :  1 );             g[ans[i].x2][ans[i].y2] =                 (g[ans[i].x2][ans[i].y2] ==  1  ?  0  :  1 );             g[ans[i].x3][ans[i].y3] =                 (g[ans[i].x3][ans[i].y3] ==  1  ?  0  :  1 );           }         } else {           ans.push_back({n - 2, i, n - 2, i + 1, n - 1, i + 1});           ans.push_back({n - 2, i, n - 1, i, n - 1, i + 1});           ans.push_back({n - 2, i + 1, n - 1, i + 1, n - 1, i});           for (int i = ans.size() - 3; i < ans.size(); i++) {             g[ans[i].x1][ans[i].y1] =                 (g[ans[i].x1][ans[i].y1] ==  1  ?  0  :  1 );             g[ans[i].x2][ans[i].y2] =                 (g[ans[i].x2][ans[i].y2] ==  1  ?  0  :  1 );             g[ans[i].x3][ans[i].y3] =                 (g[ans[i].x3][ans[i].y3] ==  1  ?  0  :  1 );           }         }       } else if (cntOne == 2) {         ans.push_back({n - 2, i, n - 2, i + 1, n - 1, i});         ans.push_back({n - 2, i, n - 2, i + 1, n - 1, i + 1});         for (int i = ans.size() - 2; i < ans.size(); i++) {           g[ans[i].x1][ans[i].y1] = (g[ans[i].x1][ans[i].y1] ==  1  ?  0  :  1 );           g[ans[i].x2][ans[i].y2] = (g[ans[i].x2][ans[i].y2] ==  1  ?  0  :  1 );           g[ans[i].x3][ans[i].y3] = (g[ans[i].x3][ans[i].y3] ==  1  ?  0  :  1 );         }       }     }     if (g[n - 1][m - 1] ==  1 ) {       ans.push_back({n - 2, m - 2, n - 2, m - 1, n - 1, m - 1});       ans.push_back({n - 2, m - 2, n - 1, m - 2, n - 1, m - 1});       ans.push_back({n - 2, m - 1, n - 1, m - 2, n - 1, m - 1});       for (int i = ans.size() - 3; i < ans.size(); i++) {         g[ans[i].x1][ans[i].y1] = (g[ans[i].x1][ans[i].y1] ==  1  ?  0  :  1 );         g[ans[i].x2][ans[i].y2] = (g[ans[i].x2][ans[i].y2] ==  1  ?  0  :  1 );         g[ans[i].x3][ans[i].y3] = (g[ans[i].x3][ans[i].y3] ==  1  ?  0  :  1 );       }     }     cout << ans.size() << endl;     for (auto p : ans) {       cout << p.x1 + 1 <<     << p.y1 + 1 <<    ;       cout << p.x2 + 1 <<     << p.y2 + 1 <<    ;       cout << p.x3 + 1 <<     << p.y3 + 1 << endl;     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int t;     cin >> t;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200000;   long long x[N], y[N];   vector<int> ids[N];   int order[N];   long long cross(int a, int b, int c) {     return x[c] * y[c] * (x[b] * y[a] - x[a] * y[b]);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int n;     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> x[i] >> y[i];     }     iota(order, order + n, 0);     sort(order, order + n,          [&](int a, int b) { return x[a] == x[b] ? y[a] > y[b] : x[a] > x[b]; });     vector<int> hull;     for (int i = 0, j = 0; i < n; i = j) {       int a = order[i];       while (hull.size() >= 2) {         int b = hull[hull.size() - 1], c = hull[hull.size() - 2];         if (cross(a, c, b) + cross(b, a, c) + cross(c, b, a) >= 0) {           break;         }         hull.pop_back();       }       hull.push_back(a);       while (j < n && x[order[j]] == x[a] && y[order[j]] == y[a]) {         ids[a].push_back(order[j++]);       }     }     while (hull.size() >= 2) {       int a = hull[hull.size() - 1], b = hull[hull.size() - 2];       if (y[b] < y[a]) {         break;       }       hull.pop_back();     }     vector<int> ans;     for (auto i : hull) {       ans.insert(ans.begin(), ids[i].begin(), ids[i].end());     }     sort(ans.begin(), ans.end());     for (auto i : ans) {       cout << i + 1 <<    ;     }     cout <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1000005;   class fenwick {     int n;     vector<int> v;       public:     fenwick(int _n) : n(_n) { v.resize(n); }     void update(int at, int by) {       while (at < v.size()) {         v[at] += by;         at |= (at + 1);       }     }     int query(int at) {       int res = 0;       while (at >= 0) {         res += v[at];         at = (at & (at + 1)) - 1;       }       return res;     }   };   map<int, int> indexes;   int get_index(int x) {     if (indexes.count(x) == 0) {       int sz = indexes.size();       indexes[x] = sz;     }     return indexes[x];   }   vector<int> f(vector<int> &v) {     int n = v.size();     vector<int> cnt(maxn, 0);     vector<int> ret;     for (auto &x : v) {       cnt[x]++;       ret.push_back(cnt[x]);     }     return ret;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     int n;     cin >> n;     vector<int> v(n);     for (int i = 0; i < n; ++i) {       int x;       cin >> x;       v[i] = get_index(x);     }     auto left = f(v);     reverse(v.begin(), v.end());     auto right = f(v);     reverse(right.begin(), right.end());     fenwick ft = fenwick(maxn);     for (int i = 0; i < n; ++i) {       ft.update(right[i], 1);     }     long long ans = 0;     for (int i = 0; i < n; ++i) {       ft.update(right[i], -1);       int cnt = ft.query(left[i] - 1);       ans += cnt;     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int mod = 998244353;   vector<long long int> prime;   long long int n = 1000001;   vector<bool> vis(n, true);   void sieve() {     vis[0] = vis[1] = false;     for (long long int i = 2; i * i <= n; i++)       if (vis[i])         for (long long int j = i * i; j <= n; j += i) vis[j] = false;     for (long long int i = 2; i <= n; i++)       if (vis[i]) prime.push_back(i);   }   bool check(long long int n, long long int k) {     if (n < 2 * k) return false;     if (k == 1) return vis[n];     if (k == 2) {       if (n % 2) return vis[n - 2];       return true;     }     return true;   }   int32_t main() {     sieve();     long long int n;     cin >> n;     for (auto it = lower_bound(prime.begin(), prime.end(), n); it != prime.end();          it++) {       if (*it > ((n * (n - 1)) >> 1)) break;       if (!check((*it) << 1, n)) continue;       long long int m = *it;       cout << m << endl;       m -= n;       for (long long int i = 1; i <= n; i++) {         long long int z = i + 1;         if (z > n) z = 1;         cout << i <<     << z << endl;       }       long long int diff = 2, init = (n >> 1) + (n & 1);       while (m--) {         cout << init <<     << init + diff << endl;         diff += 2;         init--;       }       return 0;     }     cout <<  -1 ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   const int MAXN = 1e5 + 3;   const int inf = 2e9;   int max(int a, int b) {     if (a > b)       return a;     else       return b;   }   int min(int a, int b) {     if (a < b)       return a;     else       return b;   }   bool cmp(pair<int, int> p1, pair<int, int> p2) {     if (p1.first < p2.first)       return true;     else if (p1.first == p2.first) {       if (p1.second < p2.second) return true;       return false;     }     return false;   }   void solve() {     int n, m, d;     cin >> n >> m >> d;     int a[n][m];     for (int i = (int)0; i < (int)n; ++i) {       for (int j = (int)0; j < (int)m; ++j) {         cin >> a[i][j];       }     }     int check = a[0][0] % d;     vector<int> v;     bool pr = false;     for (int i = (int)0; i < (int)n; ++i) {       for (int j = (int)0; j < (int)m; ++j) {         if (a[i][j] % d == check) {           v.push_back(a[i][j]);         } else {           pr = true;           break;         }       }     }     if (pr)       cout << -1 <<   n ;     else {       sort((v).begin(), (v).end());       int cc = (n * m);       int bi = (cc / 2);       int ans = 0;       for (int i = (int)0; i < (int)cc; ++i) {         ans += abs(v[i] - v[bi]);       }       cout << ans / d <<   n ;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t;     t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) {     if ((a == 0) || (b == 0)) return a + b;     return gcd(b, a % b);   }   long long pow_mod(long long a, long long b) {     long long res = 1;     while (b) {       if (b & 1) res = (res * a) % 1000000007;       a = (a * a) % 1000000007;       b >>= 1;     }     return (res % 1000000007);   }   long long pow_2(long long a, long long b) {     long long res = 1;     while (b) {       if (b & 1) res = (res * a);       a = (a * a);       b >>= 1;     }     return res;   }   bool isPrime(long long a) {     for (int i = 3; (i * i) <= a; i += 2) {       if ((a % i) == 0) return false;     }     if ((a != 2) && ((a % 2) == 0)) return false;     if (a == 1) return false;     return true;   }   string con_ll_to_str(int a) {     stringstream mystr;     mystr << a;     return mystr.str();   }   long long con_str_to_ll(string st) {     long long numb = 0;     int len = st.size(), i, j = 0;     for (i = len - 1; i >= 0; i--) {       numb += (pow_2(10, j) * (st[i] -  0 ));       j++;     }     return numb;   }   long long baseno_to_decimal(string st, long long basee) {     long long i = 0, j, anss = 0;     for (j = (int)st.length() - 1; j >= 0; j--) {       anss += (st[j] -  0 ) * pow_2(basee, i);       i++;     }     return anss;   }   string decimal_to_string(long long num, long long basee) {     long long i = 0, j, opop;     string anss =   ;     vector<string> stri;     stri.push_back( 0 );     stri.push_back( 1 );     stri.push_back( 2 );     stri.push_back( 3 );     stri.push_back( 4 );     stri.push_back( 5 );     stri.push_back( 6 );     stri.push_back( 7 );     stri.push_back( 8 );     stri.push_back( 9 );     stri.push_back( A );     stri.push_back( B );     stri.push_back( C );     stri.push_back( D );     stri.push_back( E );     stri.push_back( F );     if (num == 0) {       return  0 ;     }     while (num) {       opop = num % basee;       anss += stri[opop];       num /= basee;     }     reverse(anss.begin(), anss.end());     return anss;   }   const int dx[] = {0, 1, 1, 1, 0, -1, -1, -1};   const int dy[] = {1, 1, 0, -1, -1, -1, 0, 1};   long long arr[100010];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     long long T, N, K, i, j;     string st =   ;     cin >> N >> K;     if (K > N) {       cout <<  -1 ;       return 0;     }     if (K == 1 && N > 1) {       cout <<  -1 ;       return 0;     }     if (N == 1) {       cout <<  a ;       return 0;     }     long long opop = (N - K + 2);     for (i = 1; i <= opop; i++) {       if (i % 2) {         st.push_back( a );       } else {         st.push_back( b );       }     }     long long po = st.length(), pu = 0;     for (i = po + 1; i <= N; i++) {       st.push_back( c  + pu);       pu++;     }     cout << st;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   constexpr long long MOD = 1e9 + 7;   struct {     int val, lazy;   } tree[4 * 1000010];   int bal(int p) { return tree[p].val + tree[p].lazy; }   void ins(int S, int val, int L = 1, int R = 1000000, int p = 1) {     if (S < L || R < 1) return;     if (1 <= L && R <= S) {       tree[p].lazy += val;       return;     }     tree[2 * p].lazy += tree[p].lazy;     tree[2 * p + 1].lazy += tree[p].lazy;     tree[p].lazy = 0;     int M = (L + R) / 2;     ins(S, val, L, M, 2 * p);     ins(S, val, M + 1, R, 2 * p + 1);     tree[p].val = max(bal(2 * p), bal(2 * p + 1));   }   int query(int L = 1, int R = 1000000, int p = 1) {     if (L == R) {       return bal(p) > 0 ? L : -1;     }     int M = (L + R) / 2;     tree[2 * p].lazy += tree[p].lazy;     tree[2 * p + 1].lazy += tree[p].lazy;     tree[p].val += tree[p].lazy;     tree[p].lazy = 0;     if (bal(2 * p + 1) > 0) return query(M + 1, R, 2 * p + 1);     return query(L, M, 2 * p);   }   int main() {     int n, m;     scanf( %d%d , &n, &m);     vector<int> a[2];     for (int i = 0; i < n; ++i) {       int x;       scanf( %d , &x);       a[0].push_back(x);       ins(x, 1);     }     for (int i = 0; i < m; ++i) {       int x;       scanf( %d , &x);       a[1].push_back(x);       ins(x, -1);     }     int q;     scanf( %d , &q);     while (q--) {       int idx, i, x;       scanf( %d%d%d , &idx, &i, &x);       --i;       --idx;       if (idx == 0) {         ins(a[idx][i], -1);         ins(x, 1);       } else {         ins(a[idx][i], 1);         ins(x, -1);       }       a[idx][i] = x;       printf( %d n , query());     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   int main() {     long long k;     long long now = 9;     long long last = 1;     cin >> k;     if (k <= 9) {       cout << k;       return 0;     }     long long finalk = k;     long long pow = 1;     for (int step = 1; step <= 15; step++) {       long long tmplast = last - 1;       if (k <= now) {         long long this_num = tmplast + (k / step);         bool OK = 1;         long long tmpnum = this_num;         for (int i = 1; i <= step; i++) {           if (tmpnum % 10 != 9) OK = false;           tmpnum /= 10;         }         if (OK) step++;         if (k % step) this_num += 1;         long long yiwei = (step - k % step);         if (yiwei == step) yiwei = 0;         for (int i = 1; i <= yiwei; i++) {           this_num /= 10;         }         printf( %d n , this_num % 10);         return 0;       }       k -= now;       pow *= 10;       now = 9 * pow;       now *= (step + 1);       last *= 10;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int m;   vector<vector<int>> row;   vector<vector<int>> col;   vector<string> g;   vector<vector<bool>> vis;   struct node {     int d;     int i;     int j;   };   struct NODE {     vector<int> e;     bool vis = false;   };   vector<NODE> G;   bool operator<(const node a, const node b) { return a.d > b.d; }   int dijk() {     priority_queue<pair<int, int>> q;     q.push({-1, n - 1});     pair<int, int> cur;     while (!q.empty()) {       cur = q.top();       q.pop();       if (!G[cur.second].vis) {         G[cur.second].vis = true;         if (cur.second == 0) {           return -cur.first - 1;         }         for (int i = 0; i < G[cur.second].e.size(); ++i) {           int nex = G[cur.second].e[i];           q.push({cur.first - 1, nex});         }       }     }     return -1;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n >> m;     g.resize(n);     row.resize(n);     col.resize(m);     string T1(n,  . );     string T2(m,  # );     vis.resize(n, (vector<bool>(m, false)));     G.resize(n + m);     for (int i = 0; i < n; ++i) {       cin >> g[i];     }     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) {         if (g[i][j] ==  # ) {           G[i].e.push_back(n + j);           G[n + j].e.push_back(i);         }       }     }     cout << dijk() <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T1, typename T2>   istream &operator>>(istream &in, pair<T1, T2> &a) {     in >> a.first >> a.second;     return in;   }   template <typename T1, typename T2>   ostream &operator<<(ostream &out, pair<T1, T2> a) {     out << a.first <<     << a.second;     return out;   }   template <typename T, typename T1>   T amax(T &a, T1 b) {     if (b > a) a = b;     return a;   }   template <typename T, typename T1>   T amin(T &a, T1 b) {     if (b < a) a = b;     return a;   }   const long long INF = 1e18;   const int32_t M = 1e9 + 7;   const int32_t MM = 998244353;   const int N = 5e5 + 5;   const int32_t MOD = MM;   struct mod_int {     int32_t val;     mod_int(long long v = 0) {       if (v < 0) v = v % MOD + MOD;       if (v >= MOD) v %= MOD;       val = v;     }     static int32_t mod_inv(int32_t a, int32_t m = MOD) {       int32_t g = m, r = a, x = 0, y = 1;       while (r != 0) {         int32_t q = g / r;         g %= r;         swap(g, r);         x -= q * y;         swap(x, y);       }       return x < 0 ? x + m : x;     }     static unsigned fast_mod(uint64_t x, unsigned m = MOD) {       return x % m;       unsigned x_high = x >> 32, x_low = (unsigned)x;       unsigned quot, rem;       asm( divl %4 n  :  =a (quot),  =d (rem) :  d (x_high),  a (x_low),  r (m));       return rem;     }     explicit operator int() const { return val; }     mod_int &operator+=(const mod_int &other) {       val += other.val;       if (val >= MOD) val -= MOD;       return *this;     }     mod_int &operator-=(const mod_int &other) {       val -= other.val;       if (val < 0) val += MOD;       return *this;     }     mod_int &operator*=(const mod_int &other) {       val = fast_mod((uint64_t)val * other.val);       return *this;     }     mod_int &operator/=(const mod_int &other) { return *this *= other.inv(); }     friend mod_int operator+(const mod_int &a, const mod_int &b) {       return mod_int(a) += b;     }     friend mod_int operator-(const mod_int &a, const mod_int &b) {       return mod_int(a) -= b;     }     friend mod_int operator*(const mod_int &a, const mod_int &b) {       return mod_int(a) *= b;     }     friend mod_int operator/(const mod_int &a, const mod_int &b) {       return mod_int(a) /= b;     }     mod_int &operator++() {       val = val == MOD - 1 ? 0 : val + 1;       return *this;     }     mod_int &operator--() {       val = val == 0 ? MOD - 1 : val - 1;       return *this;     }     mod_int operator++(int32_t) {       mod_int before = *this;       ++*this;       return before;     }     mod_int operator--(int32_t) {       mod_int before = *this;       --*this;       return before;     }     mod_int operator-() const { return val == 0 ? 0 : MOD - val; }     bool operator==(const mod_int &other) const { return val == other.val; }     bool operator!=(const mod_int &other) const { return val != other.val; }     mod_int inv() const { return mod_inv(val); }     friend ostream &operator<<(ostream &stream, const mod_int &m) {       return stream << m.val;     }     friend istream &operator>>(istream &stream, mod_int &m) {       return stream >> m.val;     }     mod_int pow(long long p) const {       assert(p >= 0);       mod_int a = *this, result = 1;       while (p > 0) {         if (p & 1) result *= a;         a *= a;         p >>= 1;       }       return result;     }   };   int n, k, m;   pair<pair<int, int>, int> x[N];   vector<int> v[N];   bool is1[N][32];   int cnt[32];   int last[32];   int f[N][32];   mod_int dp[N][32];   mod_int ps[N][32];   void solve() {     cin >> n >> k >> m;     for (int i = 1; i < m + 1; i++) {       cin >> x[i];     }     sort(x + 1, x + m + 1);     for (int i = 1; i < m + 1; i++) {       v[x[i].first.first].push_back(i);       v[x[i].first.second].push_back(-i);     }     for (int i = 1; i < n + 2; i++) {       for (int y : v[i]) {         if (y < 0) {         } else {           for (int j = 0; j < k; j++) {             if (((x[y].second >> j) & 1)) cnt[j]++;           }         }       }       for (int j = 0; j < k; j++) {         if (cnt[j]) is1[i][j] = 1;       }       for (int j = 0; j < k; j++) {         f[i][j] = last[j];       }       for (int y : v[i]) {         if (y < 0) {           y *= -1;           for (int j = 0; j < k; j++) {             if (((x[y].second >> j) & 1))               cnt[j]--;             else               amax(last[j], x[y].first.first);           }         }       }     }     for (int j = 0; j < k; j++) {       dp[0][j] = 1;       ps[0][j] = 1;     }     for (int i = 1; i < n + 2; i++) {       for (int j = 0; j < k; j++) {         if (!is1[i][j]) {           dp[i][j] = ps[i - 1][j];           if (f[i][j]) dp[i][j] -= ps[f[i][j] - 1][j];         }         ps[i][j] = dp[i][j] + ps[i - 1][j];       }     }     mod_int ans = 1;     for (int i = 0; i < k; i++) {       ans *= dp[n + 1][i];     }     cout << ans;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t = 1;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int f[200005][3], ans, dep[200005];   int head[200005], cnt;   struct data {     int next, to;     data(int next = 0, int to = 0) : next(next), to(to) {}   } edge[200005 << 1];   int read() {     char c = getchar();     int x = 0;     while (!isdigit(c)) c = getchar();     while (isdigit(c)) {       x = (x << 3) + (x << 1) + c -  0 ;       c = getchar();     }     return x;   }   void connect(int u, int v) {     edge[++cnt] = data(head[u], v);     head[u] = cnt;   }   void DFS(int u, int pre) {     f[u][0] = 1;     dep[u] = dep[pre] + 1;     int mini = 0x3f3f3f3f;     for (int i = head[u]; i; i = edge[i].next) {       int v = edge[i].to;       if (v == pre) continue;       DFS(v, u);       f[u][0] = f[u][0] + min(min(f[v][0], f[v][1]), f[v][2]);       f[u][2] = f[u][2] + min(f[v][0], f[v][1]);       mini = min(mini, f[v][0] - min(f[v][0], f[v][1]));     }     f[u][1] = f[u][2] + mini;     if (dep[u] == 3) ans = ans + min(f[u][0], min(f[u][1], f[u][2]));   }   int main() {     n = read();     for (int i = 1; i < n; ++i) {       int u = read(), v = read();       connect(u, v);       connect(v, u);     }     DFS(1, 0);     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int q;     cin >> q;     vector<int> powers;     powers.reserve(10000);     for (int i = 0; i < (1 << 10); i++) {       long long tot = 0;       long long pow = 1;       for (int j = 0; j < 10; j++) {         if (i & (1 << j)) tot += pow;         pow *= 3;       }       powers.push_back(tot);     }     sort(powers.begin(), powers.end());     while (q--) {       int n;       cin >> n;       if (find(powers.begin(), powers.end(), n) == powers.end())         cout << *upper_bound(powers.begin(), powers.end(), n) <<   n ;       else         cout << n <<   n ;     }   }


#include <bits/stdc++.h>   void dfs(int x);   struct node {     int n, h;   } h[100001];   bool operator<(const node &A, const node &B) { return A.h < B.h; }   std::vector<int> map[100001];   int range[100001][2];   void init(int n);   int getmax(int f, int r);   void set(int x, int v);   int imax[100001][3];   int dmax[100001][3];   int main() {     int n;     int i, j;     int t, a, b;     scanf( %d , &n);     for (i = 1; i <= n; i++) {       scanf( %d , &h[i].h);       h[i].n = i;     }     std::sort(h + 1, h + n + 1);     for (i = 1; i < n; i++) {       scanf( %d%d , &a, &b);       map[a].push_back(b);       map[b].push_back(a);     }     dfs(1);     init(n);     for (i = 1; i <= n; i = j) {       for (j = i; j <= n && h[j].h == h[i].h; j++) {         int x = h[j].n;         for (int k = 0; k < map[x].size(); k++) {           int xx = map[x][k];           if (range[xx][0] < range[x][0]) continue;           int t = getmax(range[xx][0], range[xx][1]);           if (t > imax[x][0]) {             imax[x][2] = imax[x][0];             imax[x][1] = xx;             imax[x][0] = t;           } else if (t > imax[x][2]) {             imax[x][2] = t;           }         }       }       for (j = i; j <= n && h[j].h == h[i].h; j++) {         int x = h[j].n;         set(range[x][0], imax[x][0] + 1);       }     }     init(n);     for (i = n; i >= 1; i = j) {       for (j = i; j >= 1 && h[j].h == h[i].h; j--) {         int x = h[j].n;         for (int k = 0; k < map[x].size(); k++) {           int xx = map[x][k];           if (range[xx][0] < range[x][0]) continue;           int t = getmax(range[xx][0], range[xx][1]);           if (t > dmax[x][0]) {             dmax[x][2] = dmax[x][0];             dmax[x][1] = xx;             dmax[x][0] = t;           } else if (t > dmax[x][2]) {             dmax[x][2] = t;           }         }       }       for (j = i; j >= 1 && h[j].h == h[i].h; j--) {         int x = h[j].n;         set(range[x][0], dmax[x][0] + 1);       }     }     int ans = 0;     for (i = 1; i <= n; i++) {       if (imax[i][1] == dmax[i][1]) {         if (ans < imax[i][0] + dmax[i][2] + 1) ans = imax[i][0] + dmax[i][2] + 1;         if (ans < imax[i][2] + dmax[i][0] + 1) ans = imax[i][2] + dmax[i][0] + 1;       } else {         if (ans < imax[i][0] + dmax[i][0] + 1) ans = imax[i][0] + dmax[i][0] + 1;       }     }     init(n);     for (i = 1; i <= n; i = j) {       for (j = i; j <= n && h[j].h == h[i].h; j++) {         int x = h[j].n;         int t = getmax(range[x][1] + 1, n);         if (ans < t + dmax[x][0] + 1) ans = t + dmax[x][0] + 1;       }       for (j = i; j <= n && h[j].h == h[i].h; j++) {         int x = h[j].n;         set(range[x][0], imax[x][0] + 1);       }     }     init(n);     for (i = n; i >= 1; i = j) {       for (j = i; j >= 1 && h[j].h == h[i].h; j--) {         int x = h[j].n;         int t = getmax(range[x][1] + 1, n);         if (ans < t + imax[x][0] + 1) ans = t + imax[x][0] + 1;       }       for (j = i; j >= 1 && h[j].h == h[i].h; j--) {         int x = h[j].n;         set(range[x][0], dmax[x][0] + 1);       }     }     printf( %d , ans);     return 0;   }   int dT = 0;   void dfs(int x) {     range[x][0] = ++dT;     for (int i = 0; i < map[x].size(); i++) {       int xx = map[x][i];       if (range[xx][0] != 0) continue;       dfs(xx);     }     range[x][1] = dT;   }   int IT[400001];   int b;   void init(int n) {     for (b = 1; b <= n; b *= 2)       ;     for (int i = b * 2; i >= 1; i--) IT[i] = 0;   }   int max(int a, int b) { return a > b ? a : b; }   int getmax(int f, int r) {     f += b;     r += b;     int m = 0;     while (f < r) {       if (f % 2 == 1) m = max(m, IT[f++]);       if (r % 2 == 0) m = max(m, IT[r--]);       f /= 2;       r /= 2;     }     if (f == r) m = max(m, IT[f]);     return m;   }   void set(int x, int v) {     IT[x += b] = v;     while (x /= 2) IT[x] = max(IT[x * 2], IT[x * 2 + 1]);   }


#include<bits/stdc++.h>   #define ll long long   #define L(i, j, k) for(int i = (j); i <= (k); ++i)   #define R(i, j, k) for(int i = (j); i >= (k); --i)   #define vi vector < int >    #define sz(a) ((int) a.size())    using namespace std;   const int N = 1e6 + 7;   int T;      int n, m, c[N], a[N], b[N];    void Main () {    n = m = 0;    int x;    cin >> x;    L(i, 1, x) {     int w;     cin >> w;     c[i] = w;     if(i & 1) a[++n] = w;     else b[++m] = w;    }    sort(a + 1, a + n + 1);    sort(b + 1, b + m + 1);    sort(c + 1, c + x + 1);    n = 0, m = 0;    L(i, 1, x) {     if(i & 1) {      if(a[++n] != c[i]) return cout <<  NO n , void();     }     else {      if(b[++m] != c[i]) return cout <<  NO n , void();     }     }    cout <<  YES n ;   }   int main() {     ios::sync_with_stdio(false);    cin.tie(0); cout.tie(0);    cin >> T;    while (T--) Main();    return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline T BMOD(T p, T e, T m) {     T ret = 1;     while (e) {       if (e & 1) ret = (ret * p) % m;       p = (p * p) % m;       e >>= 1;     }     return (T)ret;   }   template <class T>   inline T MODINV(T a, T m) {     return BMOD(a, m - 2, m);   }   template <class T>   inline T isPrime(T a) {     for (T i = 2; i <= sqrt(double(a)); i++) {       if (a % i == 0) {         return 0;       }     }     return 1;   }   template <class T>   inline T lcm(T a, T b) {     return (a / __gcd(a, b)) * b;   }   template <class T>   inline T power(T a, T b) {     return (b == 0) ? 1 : a * power(a, b - 1);   }   template <class T>   inline string to_str(T t) {     stringstream ss;     ss << t;     return ss.str();   }   int main() {     int h, m;     scanf( %d:%d , &h, &m);     h %= 12;     double a = h * 30.0 + (m / 2.0);     double b = m * 6.0;     printf( %.10lf %.10lf n , a, b);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   long long f[102][102];   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i <= n; i++) f[0][i] = 1;     for (int j = 1; j <= n; j++)       for (int i = 1; i <= n; i++)         for (int k = 0; k < i; k++) f[i][j] += f[k][j - 1] * f[i - k - 1][j - 1];     printf( %lld , f[n][n] - f[n][m - 1]);   }


#include <bits/stdc++.h>   using namespace std;   int cnt[26];   void sol() {     int n;     cin >> n;     for (int i = 0; i < n; i++) {       long long k, x;       cin >> k >> x;       cout << (x + (k - 1) * 9) <<   n ;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t = 1;     while (t--) sol();   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     int ans = 0;     for (int i = 0; i < n; ++i)       for (int j = 0; j < n; ++j) {         int inp;         cin >> inp;         if (i == j || i + j == n - 1 || i == n / 2 || j == n / 2) ans += inp;       }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MXN = 1 << 19;   const int MOD = 1000000007;   int arr[MXN];   int suf[2 * MXN];   int pre[2 * MXN];   int lb[2 * MXN];   int rb[2 * MXN];   void build(int c, int l, int r) {     lb[c] = l;     rb[c] = r;     if (l == r) return;     int k = (l + r) / 2;     build(2 * c, l, k);     build(2 * c + 1, k + 1, r);   }   void updatesuf(int c, int idx, int amt) {     if (lb[c] == rb[c]) {       suf[c] += amt;       return;     }     int k = (lb[c] + rb[c]) / 2;     if (idx <= k)       updatesuf(2 * c, idx, amt);     else       updatesuf(2 * c + 1, idx, amt);     suf[c] = suf[2 * c] + suf[2 * c + 1];     suf[c] %= MOD;   }   void updatepre(int c, int idx, int amt) {     if (lb[c] == rb[c]) {       pre[c] += amt;       return;     }     int k = (lb[c] + rb[c]) / 2;     if (idx <= k)       updatepre(2 * c, idx, amt);     else       updatepre(2 * c + 1, idx, amt);     pre[c] = pre[2 * c] + pre[2 * c + 1];     pre[c] %= MOD;   }   int querysuf(int c, int l, int r) {     if (lb[c] == l && rb[c] == r) return suf[c];     int k = (lb[c] + rb[c]) / 2;     if (l <= k && k < r)       return (querysuf(2 * c, l, k) + querysuf(2 * c + 1, k + 1, r)) % MOD;     else if (r <= k)       return querysuf(2 * c, l, r);     else       return querysuf(2 * c + 1, l, r);   }   int querypre(int c, int l, int r) {     if (lb[c] == l && rb[c] == r) return pre[c];     int k = (lb[c] + rb[c]) / 2;     if (l <= k && k < r)       return (querypre(2 * c, l, k) + querypre(2 * c + 1, k + 1, r)) % MOD;     else if (r <= k)       return querypre(2 * c, l, r);     else       return querypre(2 * c + 1, l, r);   }   int main() {     int n;     scanf( %d , &n);     vector<pair<int, int> > proc;     for (int i = 1; i <= n; i++) {       scanf( %d , arr + i);       proc.emplace_back(arr[i], i);     }     build(1, 0, MXN - 1);     sort(proc.begin(), proc.end());     int ans = 0;     for (auto p : proc) {       int x = p.first;       int idx = p.second;       int l = idx - 1;       int r = n - l - 1;       int a = querysuf(1, idx, n);       int b = querypre(1, 1, idx);       long long cur =           1ll * a * (l + 1) + 1ll * b * (r + 1) + 1ll * (l + 1) * (r + 1);       cur %= (long long)MOD;       cur *= (long long)x;       cur %= (long long)MOD;       ans += cur;       ans %= MOD;       updatesuf(1, idx, r + 1);       updatepre(1, idx, l + 1);     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline bool chkmin(T &x, T y) {     return x > y ? x = y, 1 : 0;   }   template <typename T>   inline bool chkmax(T &x, T y) {     return x < y ? x = y, 1 : 0;   }   inline void read(int &x) {     char ch = getchar();     int f = 1;     x = 0;     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar();     x *= f;   }   int n, m, p[100050], v[100050], nx[100050], pr[100050], id[100050];   set<pair<int, int> > se;   int get(int x, int y) {     if (x == y) return 1000000005;     int d = (p[y] - p[x] + m) % m;     if (x > y) d += v[y];     if (d <= v[x]) return 1;     if (v[x] <= v[y]) return 1000000005;     return (d - v[y] - 1) / (v[x] - v[y]) + 1;   }   bool cmp(int x, int y) { return p[x] < p[y]; }   int main() {     read(n), read(m);     for (register int i = 1; i <= n; ++i)       read(p[i]), --p[i], read(v[i]), id[i] = i;     sort(id + 1, id + n + 1, cmp);     for (register int i = 1; i <= n; ++i)       pr[id[i]] = id[i - 1], nx[id[i]] = id[i + 1];     nx[pr[id[1]] = id[n]] = id[1];     for (register int i = 1; i <= n; ++i) se.insert(make_pair(get(i, nx[i]), i));     while (1) {       set<pair<int, int> >::iterator it = se.begin();       if (it->first >= 1000000005) break;       int u = it->second, t = it->first;       se.erase(it);       se.erase(make_pair(get(pr[u], u), pr[u])),           se.erase(make_pair(get(nx[u], nx[nx[u]]), nx[u]));       pr[nx[u] = nx[nx[u]]] = u;       p[u] = (p[u] + t) % m, --v[u];       se.insert(make_pair(get(pr[u], u), pr[u])),           se.insert(make_pair(get(u, nx[u]), u));     }     printf( %d n , se.size());     for (auto v : se) printf( %d  , v.second);     puts(  );     return 0;   }


#include  bits/stdc++.h    using namespace std;   typedef long long ll;   typedef long double ld;   typedef unsigned long long ull;   #define int ll   #define sz(a) int(a.size())   #define dbg(a) cout << #a <<   =   << a << endl   #define dbg1(a) cout << #a <<   = [  ; for (auto& _ : a) cout << _ <<    ; cout <<  ]  << endl   #define dbg2(a) cout << #a <<   = [  ; for (auto& _ : a) cout <<  (  << _.first <<     << _.second <<  )  ; cout <<  ]  << endl   const int inf = int(1e9) + 7;   void solve() {     int n;     cin >> n;     bool ok = false;     for (int i = 0; i < n; ++i) {       int q;       cin >> q;       ok |= q != int(sqrt(q)) * int(sqrt(q));     }     cout << (ok ?  YES n  :  NO n );   }   signed main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout << fixed << setprecision(15);     int tests = 1;     cin >> tests;     while (tests--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int main(void) {     int n, a[12][12];     cin >> n;     memset(a, 0, sizeof(a));     for (int i = 1; i <= n; i++) {       a[1][i] = 1;       a[i][1] = 1;     }     for (int i = 2; i <= n; i++) {       for (int j = 2; j <= n; j++) {         a[i][j] = a[i - 1][j] + a[i][j - 1];       }     }     cout << a[n][n];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     vector<pair<int, int>> v(n);     vector<int> v2(k);     for (int i = 0; i < n; i++) {       cin >> v[i].first;       v[i].second = i + 1;     }     sort(v.begin(), v.end());     int sum = 0;     for (int i = 0; i < k; i++) {       sum += v[n - 1 - i].first;       v2[i] = v[n - 1 - i].second;     }     cout << sum << endl;     sort(v2.begin(), v2.end());     for (int i = k - 1; i > 0; i--) {       v2[i] = v2[i] - v2[i - 1];     }     int cnt = 0;     for (int i = 0; i < k - 1; i++) {       cout << v2[i] <<    ;       cnt += v2[i];     }     cout << n - cnt;     return 0;   }


#pragma GCC optimize ( Ofast )   #include<bits/stdc++.h>   using namespace std;   void*wmem;   char memarr[96000000];   template<class S, class T> inline S min_L(S a,T b){     return a<=b?a:b;   }   template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){     static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};     (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );     (*arr)=(T*)(*mem);     (*mem)=((*arr)+x);   }   template<class T> inline void walloc1d(T **arr, int x1, int x2, void **mem = &wmem){     walloc1d(arr, x2-x1, mem);     (*arr) -= x1;   }   template<class T1> void sortA_L(int N, T1 a[], void *mem = wmem){     sort(a, a+N);   }   template<class T1, class T2> void sortA_L(int N, T1 a[], T2 b[], void *mem = wmem){     int i;     pair<T1, T2>*arr;     walloc1d(&arr, N, &mem);     for(i=(0);i<(N);i++){       arr[i].first = a[i];       arr[i].second = b[i];     }     sort(arr, arr+N);     for(i=(0);i<(N);i++){       a[i] = arr[i].first;       b[i] = arr[i].second;     }   }   inline int my_getchar(){     static char buf[1048576];     static int s = 1048576;     static int e = 1048576;     if(s == e && e == 1048576){       e = fread(buf, 1, 1048576, stdin);       s = 0;     }     if(s == e){       return EOF;     }     return buf[s++];   }   inline void rd(int &x){     int k;     int m=0;     x=0;     for(;;){       k = my_getchar();       if(k== - ){         m=1;         break;       }       if( 0 <=k&&k<= 9 ){         x=k- 0 ;         break;       }     }     for(;;){       k = my_getchar();       if(k< 0 ||k> 9 ){         break;       }       x=x*10+k- 0 ;     }     if(m){       x=-x;     }   }   inline void rd(long long &x){     int k;     int m=0;     x=0;     for(;;){       k = my_getchar();       if(k== - ){         m=1;         break;       }       if( 0 <=k&&k<= 9 ){         x=k- 0 ;         break;       }     }     for(;;){       k = my_getchar();       if(k< 0 ||k> 9 ){         break;       }       x=x*10+k- 0 ;     }     if(m){       x=-x;     }   }   inline int rd_int(void){     int x;     rd(x);     return x;   }   struct MY_WRITER{     char buf[1048576];     int s;     int e;     MY_WRITER(){       s = 0;       e = 1048576;     }     ~MY_WRITER(){       if(s){         fwrite(buf, 1, s, stdout);       }     }   }   ;   MY_WRITER MY_WRITER_VAR;   void my_putchar(int a){     if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){       fwrite(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);       MY_WRITER_VAR.s = 0;     }     MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;   }   inline void wt_L(char a){     my_putchar(a);   }   inline void wt_L(int x){     int s=0;     int m=0;     char f[10];     if(x<0){       m=1;       x=-x;     }     while(x){       f[s++]=x%10;       x/=10;     }     if(!s){       f[s++]=0;     }     if(m){       my_putchar( - );     }     while(s--){       my_putchar(f[s]+ 0 );     }   }   template<class S> inline void arrInsert(const int k, int &sz, S a[], const S aval){     int i;     sz++;     for(i=sz-1;i>k;i--){       a[i] = a[i-1];     }     a[k] = aval;   }   template<class S, class T> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval){     int i;     sz++;     for(i=sz-1;i>k;i--){       a[i] = a[i-1];     }     for(i=sz-1;i>k;i--){       b[i] = b[i-1];     }     a[k] = aval;     b[k] = bval;   }   template<class S, class T, class U> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval){     int i;     sz++;     for(i=sz-1;i>k;i--){       a[i] = a[i-1];     }     for(i=sz-1;i>k;i--){       b[i] = b[i-1];     }     for(i=sz-1;i>k;i--){       c[i] = c[i-1];     }     a[k] = aval;     b[k] = bval;     c[k] = cval;   }   template<class S, class T, class U, class V> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval, V d[], const V dval){     int i;     sz++;     for(i=sz-1;i>k;i--){       a[i] = a[i-1];     }     for(i=sz-1;i>k;i--){       b[i] = b[i-1];     }     for(i=sz-1;i>k;i--){       c[i] = c[i-1];     }     for(i=sz-1;i>k;i--){       d[i] = d[i-1];     }     a[k] = aval;     b[k] = bval;     c[k] = cval;     d[k] = dval;   }   template<class S, class T> inline S chmin(S &a, T b){     if(a>b){       a=b;     }     return a;   }   template<class S, class T> inline S chmax(S &a, T b){     if(a<b){       a=b;     }     return a;   }   int N;   int C;   int Q;   int A[30000];   int X;   long long Y;   long long cnt[5][30000+2];   int sz;   int lis[5];   int ind[5];   int usz[30000+2];   int ulis[30000+2][5];   int dsz[30000+2];   int dlis[30000+2][5];   long long skipL[5][30000+2];   long long skipR[5][30000+2];   long long skipV[5][30000+2];   int skip = 150;   long long skip2L[5][30000+2];   long long skip2R[5][30000+2];   long long skip2V[5][30000+2];   int skip2 = 30;   int main(){     int t_ynMSdg;     wmem = memarr;     int i;     int j;     int k;     int c;     for(i=(0);i<(5);i++){       cnt[i][0] = 1;     }     for(i=(0);i<(5);i++){       for(k=(0);k<(30000);k++){         for(j=(0);j<(min_L(k, i)+1);j++){           cnt[i][k+1] += cnt[i-j][k-j];           if(cnt[i][k+1] > 2000000000000000000LL){             cnt[i][k+1] = 2000000000000000000LL;           }         }       }     }     int KrdatlYV = rd_int();     for(t_ynMSdg=(0);t_ynMSdg<(KrdatlYV);t_ynMSdg++){       int dtiCQK_a;       rd(N);       rd(C);       rd(Q);       {         int a2conNHc;         for(a2conNHc=(0);a2conNHc<(N);a2conNHc++){           rd(A[a2conNHc]);         }       }       for(k=(0);k<(N);k++){         sz = 0;         for(i=(0);i<(C+1);i++){           if(k+i < N){             arrInsert(sz, sz, ind, i, lis, A[k+i]);           }         }         sortA_L(sz, lis, ind);         j = usz[k] = dsz[k] = 0;         for(i=(0);i<(sz);i++){           if(ind[i] == 0){             j++;             continue;           }           if(j==0){             ulis[k][usz[k]++] = ind[i];           }           if(j==1){             dlis[k][dsz[k]++] = ind[i];           }         }       }       for(k=(0);k<(N);k++){         for(c=(0);c<(C+1);c++){           long long sm = 0;           skipL[c][k] = -4611686016279904256LL;           skipR[c][k] = 4611686016279904256LL;           if(k+skip+5 >= N){             swap(skipL[c][k], skipR[c][k]);             continue;           }           for(j=(k);j<(k+skip);j++){             for(i=(0);i<(usz[j]);i++){               if(c >= ulis[j][i]){                 sm += cnt[c-ulis[j][i]][N-j-ulis[j][i]-1];               }             }             if(sm > 4611686016279904256LL){               sm = 2000000000000000000LL;             }             chmax(skipL[c][k], sm);             chmin(skipR[c][k], sm + cnt[c][N-j-1]);           }           skipV[c][k] = sm;         }       }       for(k=(0);k<(N);k++){         for(c=(0);c<(C+1);c++){           long long sm = 0;           skip2L[c][k] = -4611686016279904256LL;           skip2R[c][k] = 4611686016279904256LL;           if(k+skip2+5 >= N){             swap(skip2L[c][k], skip2R[c][k]);             continue;           }           for(j=(k);j<(k+skip2);j++){             for(i=(0);i<(usz[j]);i++){               if(c >= ulis[j][i]){                 sm += cnt[c-ulis[j][i]][N-j-ulis[j][i]-1];               }             }             if(sm > 4611686016279904256LL){               sm = 2000000000000000000LL;             }             chmax(skip2L[c][k], sm);             chmin(skip2R[c][k], sm + cnt[c][N-j-1]);           }           skip2V[c][k] = sm;         }       }       for(dtiCQK_a=(0);dtiCQK_a<(Q);dtiCQK_a++){         rd(X);X += (-1);         rd(Y);Y += (-1);         if(Y >= cnt[C][N]){           wt_L(-1);           wt_L(  n );           continue;         }         c = C;         for(k=(0);k<(N);k++){           if(skipL[c][k] <= Y  &&  Y < skipR[c][k]){             if(X < k + skip){               wt_L(A[X]);               wt_L(  n );               break;             }             Y -= skipV[c][k];             k += skip - 1;             continue;           }           if(skip2L[c][k] <= Y  &&  Y < skip2R[c][k]){             if(X < k + skip2){               wt_L(A[X]);               wt_L(  n );               break;             }             Y -= skip2V[c][k];             k += skip2 - 1;             continue;           }           for(i=(0);i<(usz[k]);i++){             if(c >= ulis[k][i]){               if(Y < cnt[c-ulis[k][i]][N-k-ulis[k][i]-1]){                 if(X <= k + ulis[k][i]){                   wt_L(A[k+ulis[k][i]-(X-k)]);                   wt_L(  n );                   goto qE8LMwYZ;                 }                 c -= ulis[k][i];                 k += ulis[k][i];                 goto lQU550vz;               }               else{                 Y -= cnt[c-ulis[k][i]][N-k-ulis[k][i]-1];               }             }           }           if(Y < cnt[c][N-k-1]){             if(X == k){               wt_L(A[k]);               wt_L(  n );               break;             }             continue;           }           else{             Y -= cnt[c][N-k-1];           }           for(i=(0);i<(dsz[k]);i++){             if(c >= dlis[k][i]){               if(Y < cnt[c-dlis[k][i]][N-k-dlis[k][i]-1]){                 if(X <= k + dlis[k][i]){                   wt_L(A[k+dlis[k][i]-(X-k)]);                   wt_L(  n );                   goto qE8LMwYZ;                 }                 c -= dlis[k][i];                 k += dlis[k][i];                 goto lQU550vz;               }               else{                 Y -= cnt[c-dlis[k][i]][N-k-dlis[k][i]-1];               }             }           }           lQU550vz:;         }         qE8LMwYZ:;       }     }     return 0;   }   // cLay version 20210103-1      // --- original code ---   // //no-unlocked   // int N, C, Q, A[3d4], X; ll Y;   // ll cnt[5][3d4+2];   // int sz, lis[5], ind[5];   // int usz[3d4+2], ulis[3d4+2][5];   // int dsz[3d4+2], dlis[3d4+2][5];   // ll skipL[5][3d4+2], skipR[5][3d4+2], skipV[5][3d4+2]; int skip = 150;   // ll skip2L[5][3d4+2], skip2R[5][3d4+2], skip2V[5][3d4+2]; int skip2 = 30;   // {   //   int i, j, k, c;   //   rep(i,5) cnt[i][0] = 1;   //   rep(i,5) rep(k,3d4) rep(j,min(k,i)+1){   //     cnt[i][k+1] += cnt[i-j][k-j];   //     if(cnt[i][k+1] > 2d18) cnt[i][k+1] = 2d18;   //   }   //   REP(rd_int()){   //     rd(N,C,Q,A(N));   //    //     rep(k,N){   //       sz = 0;   //       rep(i,C+1) if(k+i < N) arrInsert(sz, sz, ind, i, lis, A[k+i]);   //       sortA(sz, lis, ind);   //       j = usz[k] = dsz[k] = 0;   //       rep(i,sz){   //         if(ind[i] == 0) j++, continue;   //         if(j==0) ulis[k][usz[k]++] = ind[i];   //         if(j==1) dlis[k][dsz[k]++] = ind[i];   //       }   //     }   //    //     rep(k,N) rep(c,C+1){   //       ll sm = 0;   //       skipL[c][k] = -ll_inf;   //       skipR[c][k] = ll_inf;   //       if(k+skip+5 >= N) swap(skipL[c][k], skipR[c][k]), continue;   //       rep(j,k,k+skip){   //         rep(i,usz[j]) if(c >= ulis[j][i]) sm += cnt[c-ulis[j][i]][N-j-ulis[j][i]-1];   //         if(sm > ll_inf) sm = 2d18;   //         skipL[c][k] >?= sm;   //         skipR[c][k] <?= sm + cnt[c][N-j-1];   //       }   //       skipV[c][k] = sm;   //     }   //    //     rep(k,N) rep(c,C+1){   //       ll sm = 0;   //       skip2L[c][k] = -ll_inf;   //       skip2R[c][k] = ll_inf;   //       if(k+skip2+5 >= N) swap(skip2L[c][k], skip2R[c][k]), continue;   //       rep(j,k,k+skip2){   //         rep(i,usz[j]) if(c >= ulis[j][i]) sm += cnt[c-ulis[j][i]][N-j-ulis[j][i]-1];   //         if(sm > ll_inf) sm = 2d18;   //         skip2L[c][k] >?= sm;   //         skip2R[c][k] <?= sm + cnt[c][N-j-1];   //       }   //       skip2V[c][k] = sm;   //     }   //    //     rep(Q){   //       rd(X--, Y--);   //       if(Y >= cnt[C][N]) wt(-1), continue;   //       c = C;   //       rep(k,N){   //         if(skipL[c][k] <= Y < skipR[c][k]){   //           if(X < k + skip) wt(A[X]), break;   //           Y -= skipV[c][k];   //           k += skip - 1;   //           continue;   //         }   //         if(skip2L[c][k] <= Y < skip2R[c][k]){   //           if(X < k + skip2) wt(A[X]), break;   //           Y -= skip2V[c][k];   //           k += skip2 - 1;   //           continue;   //         }   //         rep(i,usz[k]) if(c >= ulis[k][i]){   //           if(Y < cnt[c-ulis[k][i]][N-k-ulis[k][i]-1]){   //             if(X <= k + ulis[k][i]) wt(A[k+ulis[k][i]-(X-k)]), break_break;   //             c -= ulis[k][i];   //             k += ulis[k][i];   //             break_continue;   //           } else {   //             Y -= cnt[c-ulis[k][i]][N-k-ulis[k][i]-1];   //           }   //         }   //         if(Y < cnt[c][N-k-1]){   //           if(X == k) wt(A[k]), break;   //           continue;   //         } else {   //           Y -= cnt[c][N-k-1];   //         }   //         rep(i,dsz[k]) if(c >= dlis[k][i]){   //           if(Y < cnt[c-dlis[k][i]][N-k-dlis[k][i]-1]){   //             if(X <= k + dlis[k][i]) wt(A[k+dlis[k][i]-(X-k)]), break_break;   //             c -= dlis[k][i];   //             k += dlis[k][i];   //             break_continue;   //           } else {   //             Y -= cnt[c-dlis[k][i]][N-k-dlis[k][i]-1];   //           }   //         }   //       }   //     }   //   }   // }


#include <bits/stdc++.h>   using namespace std;   int n;   struct Trie {     int trie[6000005][2];     int cnt[6000005];     int root = 0;     long long val[6000005];     long long xorsum = 0;     long long ans[6000005 / 10];     int C = 0;     int newroot() {       trie[C][0] = trie[C][1] = -1;       cnt[C] = 0;       return C++;     }     void Insert(long long x) {       long long p = root;       for (int i = 60; i >= 0; i--) {         long long id = (x >> i) & 1;         if (trie[p][id] == -1) trie[p][id] = newroot();         p = trie[p][id];         cnt[p]++;       }       val[p] = x;     }     bool check(long long xorsum, long long &res, int index, bool limit, int dep) {       if (index != root && (cnt[index] == 0 || index == -1)) return false;       if (dep == -1) {         res = val[index];         cnt[index]--;         return true;       }       int id = (xorsum >> dep) & 1;       if (id == 0) {         bool ret = (check(xorsum, res, trie[index][0], limit, dep - 1)) ||                    (!limit && check(xorsum, res, trie[index][1], limit, dep - 1));         if (ret == 0) return false;         cnt[index]--;         return true;       } else {         bool ret = (check(xorsum, res, trie[index][0], limit, dep - 1)) ||                    (check(xorsum, res, trie[index][1], false, dep - 1));         if (ret == 0) return false;         cnt[index]--;         return true;       }     }     void solve() {       for (int i = n - 1; i >= 0; i--) {         long long res;         bool ret = check(xorsum, res, root, true, 60);         if (ret == 0) {           puts( No );           return;         }         ans[i] = res;         assert(xorsum > (xorsum ^ res));         xorsum ^= res;       }       puts( Yes );       for (int i = 0; i < n; i++) {         cout << ans[i] <<    ;       }       puts(  );     }   } Tree;   int main() {     scanf( %d , &n);     Tree.root = Tree.newroot();     for (int i = 0; i < n; i++) {       long long x;       scanf( %I64d , &x);       Tree.Insert(x);       Tree.xorsum ^= x;     }     Tree.solve();   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   void err(istream_iterator<string> it) {}   template <typename T, typename... Args>   void err(istream_iterator<string> it, T a, Args... args) {     cerr << *it <<   =   << a << endl;     err(++it, args...);   }   long long powm(long long a, long long b, long long mod) {     long long res = 1;     while (b) {       if (b & 1) res = (res * a) % mod;       a = (a * a) % mod;       b >>= 1;     }     return res;   }   const long long mod = 1e9 + 7;   const long long N = 1000005;   const long long inf = 1e9;   void solve() {     int n;     cin >> n;     deque<long long> dq;     long long ans = 0;     while (n--) {       long long x;       cin >> x;       if (dq.size() == 0) {         dq.push_back(x);         continue;       }       if (dq.size() > 0) ans = max(ans, (dq.back() ^ x));       while (dq.size() > 0 and dq.back() < x) {         ans = max(ans, (x ^ dq.back()));         dq.pop_back();       }       if (dq.size() > 0) ans = max(ans, (dq.back() ^ x));       dq.push_back(x);     }     long long curr = dq.back();     dq.pop_back();     while (!dq.empty()) {       ans = max(ans, (curr ^ dq.back()));       curr = dq.back();       dq.pop_back();     }     cout << ans << endl;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0), cout.tie(0);     cout << fixed << setprecision(15);     int tc = 1;     while (tc--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double g = 9.8;   const double eps = 1e-9;   int n, m, v;   struct Ques {     double angle;     int id;   } ques[10010];   struct Wall {     double x, y;   } wall[100010];   double ans_x[10010], ans_y[10010];   bool cmp(const Ques &a, const Ques &b) { return a.angle < b.angle; }   bool cmp2(const Wall &a, const Wall &b) { return a.x < b.x; }   int main() {     scanf( %d%d , &n, &v);     for (int i = 1; i <= n; i++) {       scanf( %lf , &ques[i].angle);       ques[i].id = i;     }     scanf( %d , &m);     for (int i = 1; i <= m; i++) scanf( %lf%lf , &wall[i].x, &wall[i].y);     sort(ques + 1, ques + n + 1, cmp);     sort(wall + 1, wall + m + 1, cmp2);     int h = 1;     for (int i = 1; i <= n; i++) {       double v_y = v * sin(ques[i].angle), v_x = v * cos(ques[i].angle);       while (h <= m) {         double time = wall[h].x / v_x;         double y = v_y * time - 0.5 * g * time * time;         if (y < eps) {           ans_x[ques[i].id] = v_x * (v_y / (0.5 * g));           ans_y[ques[i].id] = 0;           break;         } else if (y < wall[h].y + eps) {           ans_x[ques[i].id] = wall[h].x;           ans_y[ques[i].id] = y;           break;         } else           h++;       }     }     for (int i = 1; i <= n; i++)       if (ans_x[ques[i].id] < eps) {         double v_y = v * sin(ques[i].angle), v_x = v * cos(ques[i].angle);         ans_x[ques[i].id] = v_x * (v_y / (0.5 * g));         ans_y[ques[i].id] = 0;       }     for (int i = 1; i <= n; i++) printf( %.9lf %.9lf n , ans_x[i], ans_y[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     k--;     int a[n];     int sum = 0;     for (int i = 0; i < n; ++i) {       cin >> a[i];     }     if (a[k] == 1) sum++;     for (int i = 1; i < n; ++i) {       if (k - i < 0) {         for (int j = k + i; j < n; ++j) {           sum += a[j];         }         break;       }       if (k + i >= n) {         for (int j = k - i; j >= 0; --j) {           sum += a[j];         }         break;       }       if (a[k - i] == 1 && a[k + i] == 1) sum += 2;     }     cout << sum;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   const double eps = 1e-6;   const int mod = 1000000007;   inline string getstr(string &s, int l, int r) {     string ret =   ;     for (int i = l; i <= r; i++) ret.push_back(s[i]);     return ret;   }   int modpow(int x, int y, int md = mod) {     if (y == 0) return 1;     int ret = modpow(x, y >> 1, md);     ret = (long long)ret * ret % md;     if (y & 1) ret = (long long)ret * x % md;     return ret;   }   string s, t;   int n;   int main() {     cin >> n >> s >> t;     int l, r;     for (l = 0;; l++) {       if (s[l] != t[l]) {         break;       }     }     for (r = n - 1;; r--) {       if (s[r] != t[r]) {         break;       }     }     int lft = 1, rgt = 1;     for (int i = l; i < r; i++) {       if (s[i] != t[i + 1]) lft = 0;       if (s[i + 1] != t[i]) rgt = 0;     }     cout << lft + rgt << endl;     return 0;   }


#include <bits/stdc++.h>   const int nax = 1e5 + 5;   using namespace std;   int gcd(int x, int y) {     if (x % y == 0) return y;     return gcd(y, x % y);   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     int n, m;     cin >> n >> m;     vector<int> a(n + 1), b(m + 1);     for (int i = 0; i <= n; i++) {       cin >> a[i];     }     for (int i = 0; i <= m; i++) {       cin >> b[i];     }     if (n != m) {       if (n > m) {         if ((a[0] > 0 && b[0] > 0) || (a[0] < 0 && b[0] < 0)) {           puts( Infinity );         } else {           puts( -Infinity );         }       } else {         cout <<  0/1               <<   n ;       }       return 0;     } else {       int ans = gcd(a[0], b[0]);       if ((a[0] > 0 && b[0] > 0) || (a[0] < 0 && b[0] < 0))         cout << a[0] / ans <<  /  << b[0] / ans <<   n ;       else         cout << -abs(a[0] / ans) <<  /  << abs(b[0] / ans) <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool check(int p) {     if (p == 2) return 1;     if (p % 2 == 0) return 0;     for (long long int i = 3; i * i <= p; i += 2) {       if (p % i == 0) return 0;     }     if (p > 2) return 1;     return 0;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t = 1;     while (t--) {       int n;       cin >> n;       if (check(n)) {         cout <<  1  <<   n ;         cout << n <<   n ;         continue;       }       if (check(n - 2)) {         cout <<  2  <<   n ;         cout <<  2  <<     << n - 2 <<   n ;         continue;       }       cout <<  3  <<   n ;       int a;       for (int i = n; i > 3; i--) {         if (check(i)) {           a = i;           n -= a;           break;         }       }       cout << a <<    ;       for (int i = 2; i <= n; i++) {         if (check(i) && check(n - i)) {           cout << i <<     << n - i <<   n ;           break;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<int, int> m;   bool fd(int x, int y) {     if (x == y) return m[y] >= 2;     return m.find(y) != m.end();   }   int main() {     int n;     cin >> n;     for (int i = 0; i < (n); i++) {       int x;       cin >> x;       m[x]++;     }     int cnt = 0;     for (auto& xp : m) {       int x = xp.first;       for (int s = 1; s <= 30; s++) {         if ((1 << s) <= x) continue;         if (fd(x, (1 << s) - x)) goto cont;       }       cnt += xp.second;     cont:;     }     cout << cnt << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long i, j, k, l, a, v, b, n, m;     string s;     while (cin >> s) {       k = 0;       j = 0;       l = s.size();       for (i = 0; i < l; i++) {         if (s[i] ==  W  && s[i + 1] ==  U  && s[i + 2] ==  B ) {           i = i + 2;           if (k > 0) {             if (j == 1) cout <<    ;             j = 0;           }         } else {           cout << s[i];           j = 1;         }         k = 1;       }     }   }


#include <bits/stdc++.h>   using namespace std;   inline int read();   int a[14250];   unsigned long long ans;   bool cmp(int a, int b) { return a < b; }   int main() {     int n = read(), k = read();     for (int i = 1; i <= n; i++) {       a[i] = read();     }     sort(a + 1, a + n + 1, cmp);     for (int i = 1; i <= k; i++) {       ans += a[i];     }     cout << ans;     return 0;   }   inline int read() {     int x = 0, w = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) w = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * w;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> adj[1000009];   int in[1000009], out[1000009];   bool vs[22][1000009];   void dfs(int x, int n) {     vs[x][n] = 1;     for (auto& it : adj[n])       if (!vs[x][it]) dfs(x, it);   }   int main() {     int N, M;     scanf( %d%d , &N, &M);     while (M--) {       int u, v;       scanf( %d%d , &u, &v);       adj[u].push_back(v);       in[v]++;       out[u]++;     }     vector<int> src, snk;     for (int i = 1; i <= N; i++) {       if (in[i] == 0) src.push_back(i);       if (out[i] == 0) snk.push_back(i);     }     int s = src.size();     for (int i = 0; i < s; i++) {       dfs(i, src[i]);     }     for (int i = 1; i < (1 << s) - 1; i++) {       set<int> st;       int c = 0;       for (int j = 0; j < s; j++) {         if (i & (1 << j)) {           ++c;           for (auto& it : snk)             if (vs[j][it]) st.insert(it);         }       }       if (c >= (int)st.size()) return !printf( NO );     }     puts( YES );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char z[500005];   int Qs1[500005], b, Qs2[500005];   int f(char a) {     if (a ==  w ) return b + 1;     return 1;   }   int main() {     int i, j, n, a, t, Max = 0;     int *qs2 = Qs2 + 1, *qs1 = Qs1 + 1;     scanf( %d %d %d %d  , &n, &a, &b, &t);     gets(z);     qs1[0] = f(z[0]);     for (i = 1; i < n; i++) {       qs1[i] = a + f(z[i]) + qs1[i - 1];     }     for (i = n - 1; i >= 0; i--) {       qs2[n - 1 - i] = a + f(z[i]) + qs2[n - 2 - i];     }     for (i = 0; i < n; i++) {       if (qs1[i] <= t) Max = max(min(i + 1, n), Max);       if (qs2[i] + qs1[0] <= t) Max = max(min(i + 2, n), Max);     }     for (i = 0; i < n; i++) {       int tmp = lower_bound(qs2, qs2 + n, t - qs1[i] - a * i) - qs2;       if (t - qs1[i] - a * i < qs2[tmp]) tmp--;       if (tmp == -1) continue;       Max = max(min(tmp + i + 2, n), Max);     }     for (i = 0; i < n; i++) {       int tmp = lower_bound(qs1, qs1 + n, t - qs2[i] - a * i - a) - qs1;       if (t - qs2[i] - a * i - a < qs1[tmp]) tmp--;       if (tmp == -1) continue;       Max = max(min(tmp + i + 2, n), Max);     }     printf( %d , Max);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[105][105], c[105], r[105];   int main() {     int n, m;     cin >> n >> m;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) cin >> a[i][j];     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         c[j] += a[i][j];         r[i] += a[i][j];       }     int co = 0;     set<int> sc, sr;     set<int>::iterator it;     while (co <= 10) {       for (int i = 1; i <= n; i++)         if (r[i] < 0) {           if (sr.find(i) != sr.end())             sr.erase(sr.find(i));           else             sr.insert(i);           r[i] = -r[i];           for (int j = 1; j <= m; j++) c[j] -= 2 * a[i][j];           for (int j = 1; j <= m; j++) a[i][j] = -a[i][j];         }       for (int j = 1; j <= m; j++)         if (c[j] < 0) {           if (sc.find(j) != sc.end())             sc.erase(sc.find(j));           else             sc.insert(j);           c[j] = -c[j];           for (int i = 1; i <= n; i++) r[i] -= 2 * a[i][j];           for (int i = 1; i <= n; i++) a[i][j] = -a[i][j];         }       co++;     }     cout << sr.size() <<    ;     for (it = sr.begin(); it != sr.end(); it++) cout << *it <<    ;     cout << endl;     cout << sc.size() <<    ;     for (it = sc.begin(); it != sc.end(); it++) cout << *it <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   const long long N = 2e5 + 5;   const double pi = acos(-1.0);   long long addM(long long a, long long b) { return (a + b) % MOD; }   long long multM(long long a, long long b) { return (a * b) % MOD; }   long long binpow(long long a, long long b) {     long long res = 1;     while (b > 0) {       if (b & 1) res *= a;       a *= a;       b >>= 1;     }     return res;   }   long long ModInverse(long long n) { return binpow(n, MOD - 2); }   vector<long long> findFact() {     vector<long long> fact(N);     fact[0] = 1;     for (long long i = 1; i <= N; i++) {       fact[i] = multM(i, fact[i - 1]);     }     return fact;   }   long long ncr(long long n, long long r) {     vector<long long> fact = findFact();     return ((fact[n] * ModInverse(fact[n - r])) % MOD * ModInverse(fact[r])) %            MOD;   }   vector<bool> findPrime() {     vector<bool> is_prime(N + 1, true);     is_prime[0] = false;     is_prime[1] = false;     for (long long i = 2; i <= N; i++) {       if (is_prime[i] && i * i <= N) {         for (long long j = i * i; j <= N; j += i) is_prime[j] = false;       }     }     return is_prime;   }   vector<bool> segmentedSieveNoPreGen(long long L, long long R) {     vector<bool> isPrime(R - L + 1, true);     long long lim = sqrt(R);     for (long long i = 2; i <= lim; ++i)       for (long long j = max(i * i, (L + i - 1) / i * i); j <= R; j += i)         isPrime[j - L] = false;     if (L == 1) isPrime[0] = false;     return isPrime;   }   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long tt;     cin >> tt;     while (tt--) {       long long n;       cin >> n;       string x, y;       cin >> x >> y;       bool check = true;       for (long long i = 0; i < n; i++) {         if (x[i] > y[i]) {           check = false;         }       }       if (check) {         long long ans = 0;         for (char c =  a ; c <=  z ; c++) {           char mn =  z ;           bool check = false;           for (long long i = 0; i < n; i++) {             if (x[i] == c && x[i] != y[i]) {               check = true;               mn = min(mn, y[i]);             }           }           for (long long i = 0; i < n; i++) {             if (x[i] == c && x[i] != y[i]) {               x[i] = mn;             }           }           if (check) ans++;         }         cout << ans <<   n ;       } else {         cout <<  -1 n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e6 + 10;   int a[N], nx[N];   long long pr[N];   int main() {     int n, q;     scanf( %d %d , &n, &q);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]), a[i + n] = a[i];     for (int i = 1; i <= n + n; i++) pr[i] = pr[i - 1] + a[i];     while (q--) {       long long b;       scanf( %lld , &b);       int now = 1;       int mx = n + n + 1, id;       for (int i = 1; i <= n + n; i++) {         while (now <= n + n && pr[i - 1] + b >= pr[now]) now++;         nx[i] = now;         if (nx[i] - i >= n) nx[i] = i + n;         if (nx[i] <= n + n && mx > nx[i] - i) {           mx = nx[i] - i;           id = i;         }       }       int ans = n + n;       for (int i = id; i < nx[id]; i++) {         int p = i;         int res = 0;         while (p < i + n) {           res++;           if (nx[p] > i + n && res > 1) {             if (pr[i + n - 1] - pr[p - 1] + pr[nx[i] - 1] - pr[i - 1] <= b) res--;           }           p = nx[p];         }         ans = min(ans, res);       }       printf( %d n , ans);     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   int n, q;   char s[N];   struct node {     int s, x, z, xz, zy, d;   } tr[N << 2];   void upd(int o) {     int S = tr[(o << 1)].s, lx = tr[(o << 1)].x, lz = tr[(o << 1)].z,         lxz = tr[(o << 1)].xz, lzy = tr[(o << 1)].zy;     int rx = tr[(o << 1 | 1)].x + S, rz = tr[(o << 1 | 1)].z - 2 * S,         rxz = tr[(o << 1 | 1)].xz - S, rzy = tr[(o << 1 | 1)].zy - S;     tr[o].s = S + tr[(o << 1 | 1)].s;     tr[o].x = max(lx, rx), tr[o].z = max(lz, rz);     tr[o].xz = max(max(lxz, rxz), lx + rz);     tr[o].zy = max(max(lzy, rzy), lz + rx);     tr[o].d =         max(max(tr[(o << 1)].d, tr[(o << 1 | 1)].d), max(lxz + rx, lx + rzy));   }   void mdf(int o, int l, int r, int p, int c) {     if (l == r) {       tr[o] = {c, c, -2 * c, -c, -c, 0};       return;     }     int mid = (l + r) >> 1;     if (p <= mid)       mdf((o << 1), l, mid, p, c);     else       mdf((o << 1 | 1), mid + 1, r, p, c);     upd(o);   }   int main() {     scanf( %d%d%s , &n, &q, s + 1);     for (int i = (1); i <= (2 * n - 2); ++i)       mdf(1, 1, 2 * n - 2, i, s[i] ==  (  ? 1 : -1);     printf( %d n , tr[1].d);     while (q--) {       int x, y;       scanf( %d%d , &x, &y);       swap(s[x], s[y]);       mdf(1, 1, 2 * n - 2, x, s[x] ==  (  ? 1 : -1);       mdf(1, 1, 2 * n - 2, y, s[y] ==  (  ? 1 : -1);       printf( %d n , tr[1].d);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1.0);   const double eps = 1e-11;   long long n, k;   bool solveIt(long long d1, long long d2) {     if ((k - d1 + d2) % 3 != 0) return false;     long long Y = (k - d1 + d2) / 3;     long long X = d1 + Y;     long long Z = Y - d2;     if (X >= 0 && Y >= 0 && Z >= 0 && X <= k && Y <= k && Z <= k &&         X <= (n / 3) && Y <= (n / 3) && Z <= (n / 3) && abs(X - Y) == abs(d1) &&         abs(Y - Z) == abs(d2))       return true;     return false;   }   int main() {     std::ios_base::sync_with_stdio(0);     long long t, d2, d1;     cin >> t;     while (t--) {       bool flg = false;       cin >> n >> k >> d1 >> d2;       if (n % 3 != 0) {         cout <<  no  << endl;         continue;       }       if (solveIt(d1, d2)) flg = true;       if (solveIt(d1, -d2)) flg = true;       if (solveIt(-d1, d2)) flg = true;       if (solveIt(-d1, -d2)) flg = true;       if (flg)         cout <<  yes  << endl;       else         cout <<  no  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, m;     cin >> n >> m;     vector<pair<long long, long long> > v;     for (int i = 0; i < m; i++) {       long long a, b;       cin >> b >> a;       v.push_back(make_pair(a, b));     }     sort(v.rbegin(), v.rend());     long long total = 0, i = 0, count = 0;     while (i < m && (count + v[i].second) <= n) {       total += v[i].first * v[i].second;       count += v[i].second;       i++;     }     if (i != m) total += v[i].first * (n - count);     cout << total << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000000007;   const int MAX = 1000005;   int SetBit(int n, int x) { return n | (1 << x); }   int ClearBit(int n, int x) { return n & ~(1 << x); }   int ToggleBit(int n, int x) { return n ^ (1 << x); }   bool CheckBit(int n, int x) { return (bool)(n & (1 << x)); }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int w, y, max;     cin >> w >> y;     int a;     if (w > y) {       max = w;     } else if (w == y) {       max = w;     } else {       max = y;     }     if ((w == 6 && y == 6)) {       cout <<  1             <<  /             <<  6  << endl;     } else if ((w == 6 && (y == 1 || y == 2 || y == 3 || y == 4 || y == 5))) {       cout <<  1             <<  /             <<  6  << endl;     } else if ((y == 6 && (w == 1 || w == 2 || w == 3 || w == 4 || w == 5))) {       cout <<  1             <<  /             <<  6  << endl;     } else {       a = 6 - max + 1;       if (a == 2) {         cout <<  1               <<  /               <<  3  << endl;       }       if (a == 3) {         cout <<  1               <<  /               <<  2  << endl;       }       if (a == 4) {         cout <<  2               <<  /               <<  3  << endl;       }       if (a == 5 || a == 1) {         cout << a <<  /               <<  6  << endl;       }       if (a == 6) {         cout <<  1               <<  /               <<  1  << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int max(int &a, int &b) { return (a > b ? a : b); }   unsigned long long max(unsigned long long &a, unsigned long long &b) {     return (a > b ? a : b);   }   int min(int &a, int &b) { return (a < b ? a : b); }   long long int bits_needed(long long int val) {     long long int count = 0;     while (val) count++, val >>= 1;     return count;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t = 0, n = 0, i = 0;     long long int val = 0, prev = 0, ans = 0;     cin >> t;     while (t-- > 0) {       ans = 0;       cin >> n;       cin >> prev;       for (i = 1; i < n; i++) {         cin >> val;         if (val < prev) {           ans = max(ans, bits_needed(prev - val));         } else           prev = val;       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, d, sum, ans;   char s[105];   int main() {     scanf( %d %d , &n, &d);     for (int i = 0; i < d; i++) {       scanf( %s , s);       bool ok = true;       for (int j = 0; j < n; j++)         if (s[j] ==  0 ) ok = false;       if (ok)         sum = 0;       else         sum++, ans = max(ans, sum);     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 5000005;   using ll = long long;   ll f[N], pre[N];   int n, a[N];   vector<int> ans;   void move(int x) {     if (x == n || a[x] == 0 || a[x + 1] == 0) return;     ans.push_back(x);     int t = min(a[x], a[x + 1]);     a[x] -= t;     a[x + 1] -= t;   }   int main() {     ios::sync_with_stdio(false);     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i];     f[2] = a[1];     for (int i = 3; i <= n + 1; i++)       if (f[i - 3] + max(a[i - 1], a[i - 2]) < f[i - 2] + a[i - 1])         f[i] = f[pre[i] = i - 3] + max(a[i - 1], a[i - 2]);       else         f[i] = f[pre[i] = i - 2] + a[i - 1];     for (int i = f[n] < f[n + 1] ? n : n + 1; i > 1; i = pre[i])       if (pre[i] == i - 3) {         move(i - 2), move(i - 1);         if (i > 3) move(i - 3);       } else {         move(i - 1);         if (i > 2) move(i - 2);       }     cout << ans.size() << endl;     for (auto &i : ans) cout << i << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int ReadInt() {     int i = 0;     char c = 0;     char flag = 1;     while ((c <  0  || c >  9 ) && c !=  - ) c = getchar();     if (c ==  - ) {       flag = 0;       c = getchar();     }     for (; c >=  0  && c <=  9 ; c = getchar()) i = i * 10 + c -  0 ;     if (!flag) i = -i;     return i;   }   inline void WriteInt(int n) {     if (n < 0) {       putchar( - );       WriteInt(-n);       return;     }     if (n < 10)       putchar(n +  0 );     else {       WriteInt(n / 10);       putchar(n % 10 +  0 );     }   }   int n, m;   int top;   long long ans, sum, last;   int A[100100];   int tmp;   int main() {     scanf( %i%i , &n, &m);     for (int i = 0; i < n; i++) scanf( %i , &A[i]), sum += A[i];     sort(A, A + n);     int top = 0;     for (int i = 0; i < n; i++)       if (A[i] > top)         ans++, top++;       else         ans++;     ans += A[n - 1] - top;     printf( %I64d , sum - ans);     putc(  n , stdout);   }


#include <bits/stdc++.h>   using namespace std;   int Matrix[1002][2002];   int max(int a, int b) {     if (a > b)       return a;     else       return b;   }   int min(int a, int b) {     if (a < b)       return a;     else       return b;   }   int distance(int a, int b, int c) {     int first, second;     first = a < b ? b - a : a - b;     second = b < c ? c - b : b - c;     return first + second;   }   int main() {     int n, k, p;     int person[1005];     int key[2002];     cin >> n >> k >> p;     for (int i = 1; i <= n; i++) cin >> person[i];     for (int i = 1; i <= k; i++) cin >> key[i];     sort(person + 1, person + n + 1);     sort(key + 1, key + k + 1);     for (int i = 1; i <= n; i++) {       for (int j = i; j <= k; j++) {         if (i == j) {           Matrix[i][j] =               max(Matrix[i - 1][j - 1], distance(person[i], key[i], p));         } else {           Matrix[i][j] =               min(Matrix[i][j - 1],                   max(Matrix[i - 1][j - 1], distance(person[i], key[j], p)));         }       }     }     int ans = 2e9;     for (int i = n; i <= k; i++) ans = min(ans, Matrix[n][i]);     cout << ans << endl;   }