#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   int n, l, m;   const int MAXN = 1e6 + 5;   long long cini[MAXN], cfin[MAXN];   vector<vector<long long>> owo;   vector<vector<long long>> mul(vector<vector<long long>> A,                                 vector<vector<long long>> B) {     int n = A.size();     vector<vector<long long>> res(n, vector<long long>(n));     for (int k = 0; k < n; k++)       for (int i = 0; i < n; i++)         for (int j = 0; j < n; j++)           res[i][j] = (res[i][j] + A[i][k] * B[k][j]) % MOD;     return res;   }   vector<vector<long long>> mexp(vector<vector<long long>> M, long long e) {     int n = M.size();     vector<vector<long long>> res(n, vector<long long>(n));     for (int i = 0; i < n; i++) res[i][i] = 1;     for (long long k = 1; k <= e; k *= 2) {       if (k & e) res = mul(res, M);       M = mul(M, M);     }     return res;   }   int cost[MAXN];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> n >> l >> m;     for (int i = 0; i < n; i++) {       int x;       cin >> x;       cini[x % m]++;     }     owo.assign(m, vector<long long>(m));     for (int i = 0; i < n; i++) {       int x;       cin >> x;       cost[i] = x;       for (int i = 0; i < m; i++) {         owo[i][(x + i) % m]++;       }     }     for (int i = 0; i < n; i++) {       int x;       cin >> x;       cfin[(x + cost[i]) % m]++;     }     vector<vector<long long>> res = mexp(owo, l - 2);     long long ans = 0;     for (int a = 0; a < m; a++) {       for (int b = 0; b < m; b++) {         int rem = (a + b) % m;         if (rem > 0) rem = m - rem;         long long te = ((long long)cini[a] * cfin[b]) % MOD;         ans = (ans + te * res[0][rem]) % MOD;       }     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1000000007;   const int INF = 1034567890;   const long long LL_INF = 1234567890123456789ll;   const double PI = acos(-1);   const long double ERR = 1E-10;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int cnt = 1;       int place{};       int ph;       for (auto i{0}; i < 9; i++) {         if (n >= cnt) {           place = cnt;         }         cnt *= 3;       }       place *= 3;       ph = place;       int res = 0;       if (n > place / 2) {         res = place;       } else {         place /= 3;         res += place;         n -= place;         while (n && place) {           place /= 3;           if (n > place / 2) {             n -= place;             res += place;           }         }       }       cout << res << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, sum = 0;     vector<int> ans;     cin >> n;     cout << (n * (n + 1) / 2) % 2 <<   n ;     sum = n * (n + 1) / 4;     while (sum) {       if (n <= sum) {         sum -= n;         ans.emplace_back(n);       }       n--;     }     cout << ans.size() <<    ;     for (int i = 0; i < ans.size(); i++) cout << ans[i] <<    ;     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<long long> s(n);     for (int(i) = (int)(0); (i) < (int)(n); ++(i)) cin >> s[i];     long long a, b, c, d, e;     cin >> a >> b >> c >> d >> e;     long long r = 0;     vector<long long> t(5, 0);     for (int(i) = (int)(0); (i) < (int)(n); ++(i)) {       r += s[i];       if (r >= e) t[4] += r / e, r -= (r / e) * e;       if (r >= d) t[3] += r / d, r -= (r / d) * d;       if (r >= c) t[2] += r / c, r -= (r / c) * c;       if (r >= b) t[1] += r / b, r -= (r / b) * b;       if (r >= a) t[0] += r / a, r -= (r / a) * a;     }     for (int(i) = (int)(0); (i) < (int)(5); ++(i)) cout << t[i] <<    ;     cout << endl;     cout << r << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2000 + 5;   vector<int> g[maxn];   bool used[maxn][maxn];   int v[maxn], stk[maxn];   void dfs(int z, int m, int x, bool eq) {     stk[z] = x;     if (z == m) {       if (eq) return;       for (int i = 0; i < m + 1; ++i) printf( %d  , stk[i] + 1);       puts(  );       exit(0);     }     for (int i = 0; i < (int)g[x].size(); ++i) {       if (!used[x][g[x][i]] && !used[g[x][i]][x]) {         if (!eq || g[x][i] >= v[z + 1]) {           used[x][g[x][i]] = true;           dfs(z + 1, m, g[x][i], g[x][i] > v[z + 1] ? false : eq);           used[x][g[x][i]] = false;         }       }     }   }   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 0; i < m + 1; ++i) {       scanf( %d , &v[i]);       --v[i];     }     for (int i = 0; i < m; ++i) {       g[v[i]].push_back(v[i + 1]);       g[v[i + 1]].push_back(v[i]);     }     for (int i = 0; i < n; ++i) {       sort(g[i].begin(), g[i].end());     }     dfs(0, m, v[0], true);     puts( No solution );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAGIC = 777777777;   struct point {     long long x, y, z;     point() {}     point(long long val) { x = y = z = val; }   };   vector<point> arr;   struct equation {     pair<long long, long long> S;     pair<long long, long long> a;     pair<long long, long long> b;     pair<long long, long long> c;     equation() {}     equation(long long f, long long s) {       S.first = a.first = b.first = c.first = f;       S.second = b.second = c.second = a.second = s;     }   };   equation calcCur(long long mid) {     equation cur = equation(-9e18, 9e18);     for (int i = 0; i < arr.size(); i++) {       equation item;       item.S.first = arr[i].x + arr[i].y + arr[i].z - mid;       item.S.second = arr[i].x + arr[i].y + arr[i].z + mid;       item.a.first = -arr[i].x + arr[i].y + arr[i].z - mid;       item.a.second = -arr[i].x + arr[i].y + arr[i].z + mid;       item.b.first = arr[i].x - arr[i].y + arr[i].z - mid;       item.b.second = arr[i].x - arr[i].y + arr[i].z + mid;       item.c.first = arr[i].x + arr[i].y - arr[i].z - mid;       item.c.second = arr[i].x + arr[i].y - arr[i].z + mid;       cur.S.first = max(cur.S.first, item.S.first);       cur.S.second = min(cur.S.second, item.S.second);       cur.a.first = max(cur.a.first, item.a.first);       cur.a.second = min(cur.a.second, item.a.second);       cur.b.first = max(cur.b.first, item.b.first);       cur.b.second = min(cur.b.second, item.b.second);       cur.c.first = max(cur.c.first, item.c.first);       cur.c.second = min(cur.c.second, item.c.second);     }     return cur;   }   point getSolution(equation cur) {     if (cur.S.first > cur.S.second || cur.a.first > cur.a.second ||         cur.b.first > cur.b.second || cur.c.first > cur.c.second ||         cur.a.second + cur.b.second + cur.c.second < cur.S.first ||         cur.a.first + cur.b.first + cur.c.first > cur.S.second) {       return point(MAGIC);     }     point result;     result.x = cur.a.first;     result.y = cur.b.first;     result.z = cur.c.first;     long long delta = max(0ll, cur.S.first - result.x - result.y - result.z);     result.x += min(cur.a.second - cur.a.first, delta);     delta -= min(cur.a.second - cur.a.first, delta);     result.y += min(cur.b.second - cur.b.first, delta);     delta -= min(cur.b.second - cur.b.first, delta);     result.z += min(cur.c.second - cur.c.first, delta);     delta -= min(cur.c.second - cur.c.first, delta);     assert(delta == 0);     return result;   }   long long div2(long long x) { return (x - (x & 1)) / 2; }   point can(long long mid) {     equation cur = calcCur(mid);     for (int r = 0; r <= 1; r++) {       equation final;       final.S.first = div2(cur.S.first - 3 * r + 1);       final.a.first = div2(cur.a.first - r + 1);       final.b.first = div2(cur.b.first - r + 1);       final.c.first = div2(cur.c.first - r + 1);       final.S.second = div2(cur.S.second - 3 * r);       final.a.second = div2(cur.a.second - r);       final.b.second = div2(cur.b.second - r);       final.c.second = div2(cur.c.second - r);       point result = getSolution(final);       if (result.x != MAGIC) {         point answer;         answer.x = result.y + result.z + r;         answer.y = result.x + result.z + r;         answer.z = result.x + result.y + r;         return answer;       }     }     return point(MAGIC);   }   void solve() {     int n;     cin >> n;     arr.clear();     arr.resize(n);     for (int i = 0; i < n; i++) {       scanf( %I64d%I64d%I64d , &arr[i].x, &arr[i].y, &arr[i].z);     }     long long lf = -1;     long long rg = 3e18 + 20;     while (rg - lf > 1) {       long long middle = lf + (rg - lf) / 2;       if (can(middle).x != MAGIC) {         rg = middle;       } else {         lf = middle;       }     }     point answer;     for (long long i = lf; i <= rg; i++) {       if (can(i).x != MAGIC) {         answer = can(i);         cout << answer.x <<     << answer.y <<     << answer.z << endl;         return;       }     }     int s = -4;     cout << arr[s].x << endl;   }   int main() {     int t;     cin >> t;     for (int i = 0; i < t; i++) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     map<int, vector<int> > m;     int e;     for (int i = 2; i <= n; i++) {       cin >> e;       m[e].push_back(i);     }     vector<bool> leaf(n + 1, true);     leaf[1] = false;     for (int i = 1; i <= n; i++) {       if (m.count(i) > 0) {         int c = 0;         for (auto it = m[i].begin(); it != m[i].end(); it++) {           if (m.count(*it) != 1) c++;         }         if (c < 3) {           cout <<  No n ;           return 0;         }       }     }     cout <<  Yes n ;   }


#include <bits/stdc++.h>   using namespace std;   int arr[1100];   int main() {     int n, a, b, c, t, ans = 0, sum = 0;     cin >> n >> a >> b >> c >> t;     for (int i = 0; i < n; i++) {       cin >> arr[i];       sum += t - arr[i];     }     ans = n * a;     if (b >= c) {       cout << ans << endl;     } else {       cout << ans + sum * (c - b) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 4005;   int f[maxn][maxn], prea[maxn][26], preb[maxn][26];   char a[maxn], b[maxn];   int n, m, ti, td, tr, te;   int main() {     int i, j, x, y;     scanf( %d%d%d%d , &ti, &td, &tr, &te);     scanf( %s%s , a + 1, b + 1);     n = strlen(a + 1);     m = strlen(b + 1);     for (i = 1; i <= n; ++i) {       memcpy(prea[i], prea[i - 1], sizeof prea[0]);       prea[i][a[i] -  a ] = i;       f[i][0] = i * td;     }     for (i = 1; i <= m; ++i) {       memcpy(preb[i], preb[i - 1], sizeof preb[0]);       preb[i][b[i] -  a ] = i;       f[0][i] = i * ti;     }     for (i = 1; i <= n; ++i)       for (j = 1; j <= m; ++j) {         f[i][j] = min(f[i - 1][j] + td, f[i][j - 1] + ti);         f[i][j] = min(f[i][j], f[i - 1][j - 1] + (a[i] != b[j] ? tr : 0));         x = prea[i - 1][b[j] -  a ];         y = preb[j - 1][a[i] -  a ];         if (x && y)           f[i][j] = min(f[i][j], f[x - 1][y - 1] + te + (i - x - 1) * td +                                      (j - y - 1) * ti);       }     printf( %d n , f[n][m]);     return 0;   }


#include <bits/stdc++.h>   void read(long long &t) {     char c;     while ((c = getchar()) <  0  || c >  9 )       ;     t = c -  0 ;     while ((c = getchar()) >=  0  && c <=  9 ) (t *= 10) += c -  0 ;   }   static const long long N = 100010;   long long a[N], cnt[45];   bool is_power(long long t) { return !(t - (t & -t)); }   long long get_cnt(long long t) {     long long ret = -1;     while (t) t >>= 1, ++ret;     return ret;   }   signed main() {     long long n;     read(n);     for (long long i = 0; i < n; ++i) read(a[i]);     for (long long i = 0; i < n; ++i)       if (is_power(a[i])) ++cnt[get_cnt(a[i])];     long long mx = cnt[0], mn = 0;     for (long long i = n - 1; i >= 0; --i)       if (!is_power(a[i])) {         long long c = get_cnt(a[i]);         for (long long j = 44; j >= c; --j)           if (cnt[j] > 0) {             for (long long k = j; ~k; --k) --cnt[k];             goto find;           }         printf( -1 n );         return 0;       find:         ++mn;       }     while (cnt[0] > 0)       for (long long i = 1; i < 45; ++i)         if (cnt[i] <= 0) {           for (long long j = 0; j < i; ++j) --cnt[j];           long long mx = 0, p = -1;           for (long long j = i - 1; j >= 0; --j)             if (cnt[j] > mx) mx = cnt[p = j];           if (mx) --cnt[p];           ++mn;           break;         }     for (long long i = 0; i < 45; ++i)       if (cnt[i]) {         printf( -1 n );         return 0;       }     for (long long i = mn; i <= mx; ++i) printf( %lld  , i);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 5;   const int Mod = 1000000007;   const int INF = 0x3f3f3f3f;   const long long LL_INF = 0x3f3f3f3f3f3f3f3f;   const double e = exp(1);   const double PI = acos(-1);   const double ERR = 1e-10;   int head[maxn], cnt, n, tot, mstnum;   int dfn[maxn], father[maxn], rank_[maxn], sz[maxn];   pair<int, int> range[maxn];   long long c[maxn];   bool mark[maxn];   set<int> se;   unordered_map<long long, int> mp;   struct node {     int v, nxt;   } Edge[2 * maxn];   struct edge {     int u, v, id;     long long w;     edge() {}     edge(int _id, int _u, int _v, long long _w) : id(_id), u(_u), v(_v), w(_w) {}   } mst[2 * maxn];   bool operator<(edge a, edge b) { return a.w < b.w; }   vector<edge> ve[maxn];   void init() {     for (int i = 0; i <= n; i++) {       head[i] = -1;       father[i] = i;       rank_[i] = 0;     }     cnt = 0;     tot = 0;     mstnum = 0;   }   void addedge(int u, int v) {     Edge[cnt].v = v;     Edge[cnt].nxt = head[u];     head[u] = cnt++;   }   void dfs(int id, int fa) {     sz[id] = 1;     range[id].first = 0x3f3f3f3f;     range[id].second = -1;     for (int i = head[id]; i != -1; i = Edge[i].nxt) {       int v = Edge[i].v;       if (v == fa) continue;       dfs(v, id);       range[id].first = min(range[id].first, range[v].first);       range[id].second = max(range[id].second, range[v].second);       sz[id] += sz[v];     }     if (sz[id] == 1) {       dfn[id] = ++tot;       range[id].first = range[id].second = tot;     }   }   int find(int id) {     return father[id] == id ? id : father[id] = find(father[id]);   }   void merge(int x, int y) {     x = find(x);     y = find(y);     if (x != y) {       if (rank_[x] > rank_[y])         father[y] = x;       else {         father[x] = y;         if (rank_[x] == rank_[y]) rank_[y]++;       }     }   }   int main() {     int u, v;     scanf( %d , &n);     init();     for (int i = 1; i <= n; i++) {       scanf( %lld , &c[i]);     }     for (int i = 1; i < n; i++) {       scanf( %d%d , &u, &v);       addedge(u, v);       addedge(v, u);     }     dfs(1, -1);     long long ans = 0;     int num = 0;     for (int i = 1; i <= n; i++) {       mst[i] = edge(i, range[i].first - 1, range[i].second, c[i]);     }     sort(mst + 1, mst + 1 + n);     for (int i = 1; i <= n; i++) {       if (mp.find(mst[i].w) == mp.end()) {         mp[mst[i].w] = ++num;       }       ve[mp[mst[i].w]].push_back(mst[i]);     }     for (int i = 1; i <= num; i++) {       for (auto ed : ve[i]) {         int x = find(ed.u);         int y = find(ed.v);         int id = ed.id;         if (x != y) {           se.insert(id);         }       }       for (auto ed : ve[i]) {         int x = find(ed.u);         int y = find(ed.v);         if (x != y) {           ans += ed.w;           merge(x, y);         }       }     }     printf( %lld %d n , ans, (int)se.size());     for (auto x : se) {       printf( %d  , x);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char __buf[1 << 20], *__p1, *__p2;   int read() {     int s = 0, w = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) w = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 )       s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar();     return s * w;   }   const int maxn = 710;   struct Wint : vector<long long> {     const static long long BIT = 1e8;     Wint(long long n = 0) {       push_back(n);       check();     }     Wint& operator=(const char* num) {       int Len = strlen(num) - 1;       clear();       for (int i = Len; i >= 0; i -= 9) {         push_back(0);         long long w = 1;         for (int j = i; j > i - 9 && j >= 0; --j)           back() += (num[j] ^ 48) * w, w *= 10;       }       return *this;     }     Wint& check() {       while (!empty() && !back()) pop_back();       if (empty()) return *this;       for (int i = 1; i < size(); ++i)         (*this)[i] += (*this)[i - 1] / BIT, (*this)[i - 1] %= BIT;       while (back() >= BIT) {         push_back(back() / BIT);         (*this)[size() - 2] %= BIT;       }       return *this;     }   };   bool operator<(Wint a, Wint b) {     if (a.size() != b.size()) return a.size() < b.size();     for (int i = a.size() - 1; i >= 0; --i)       if (a[i] != b[i]) return a[i] < b[i];     return 0;   }   bool operator>(Wint a, Wint b) { return b < a; }   bool operator<=(Wint a, Wint b) { return !(a > b); }   bool operator>=(Wint a, Wint b) { return !(a < b); }   bool operator!=(Wint a, Wint b) { return a < b || b < a; }   bool operator==(Wint a, Wint b) { return !(a < b) && !(b < a); }   Wint& operator+=(Wint& a, Wint b) {     if (a.size() < b.size()) a.resize(b.size());     for (int i = 0; i < b.size(); ++i) a[i] += b[i];     return a.check();   }   Wint operator+(Wint a, Wint b) { return a += b; }   Wint& operator-=(Wint& a, Wint b) {     for (int i = 0; i < b.size(); a[i] -= b[i], ++i)       if (a[i] < b[i]) {         int j = i + 1;         while (!a[j]) ++j;         while (j > i) --a[j], a[--j] += Wint::BIT;       }     return a.check();   }   Wint operator-(Wint a, Wint b) { return a -= b; }   Wint operator*(Wint a, Wint b) {     Wint n;     n.assign(a.size() + b.size() - 1, 0);     for (int i = 0; i < a.size(); ++i)       for (int j = 0; j < b.size(); ++j) n[i + j] += a[i] * b[j];     return n.check();   }   Wint& operator*=(Wint& a, Wint b) { return a = a * b; }   Wint operator/(Wint a, int b) {     Wint n;     bool wp = 0;     long long t = 0;     for (int i = a.size() - 1; i >= 0; --i) {       t = t * Wint::BIT + a[i];       if (wp || t / b) wp = 1, n.push_back(t / b);       t %= b;     }     reverse(n.begin(), n.end());     return n;   }   Wint& operator/=(Wint& a, int b) { return a = a / b; }   void writeX(Wint n) {     if (n.empty()) return putchar( 0 ), void();     int Len = n.size() - 1;     printf( %lld , n[Len]);     for (int i = Len - 1; i >= 0; --i) printf( %08lld , n[i]);   }   Wint Win(long long num) {     Wint t = num;     return t;   }   vector<int> G[maxn];   Wint f[maxn][maxn], t[maxn];   int n, sz[maxn];   void add(int x, int y) { G[x].push_back(y), G[y].push_back(x); }   template <class T>   void Max(T& x, T y) {     x = max(x, y);   }   void dfs(int x, int pa) {     f[x][1] = Win(1), sz[x] = 1;     for (int y : G[x])       if (y ^ pa) {         dfs(y, x);         for (int i = 1; i <= sz[x]; i++) {           for (int j = 1; j <= sz[y]; j++)             Max(t[i], f[x][i] * f[y][j] * Win(j)),                 Max(t[i + j], f[x][i] * f[y][j]);         }         for (int i = 1; i <= sz[x] + sz[y]; i++) f[x][i] = t[i], t[i] = Win(0);         sz[x] += sz[y];       }   }   signed main() {     n = read();     for (int i = 1; i < n; i++) add(read(), read());     dfs(1, 0);     Wint ans = Win(0);     for (int i = 1; i <= n; i++) ans = max(ans, f[1][i] * Win(i));     writeX(ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N, Q, T;   bool matr[300][300];   int amounts[300];   vector<int> chains[300];   bool was[300];   int sorted[300];   int s = 0;   long long mas[2][100010];   void srt(int v) {     was[v] = true;     for (int i = 0; i < N; i++)       if (!was[i] && matr[v][i]) srt(i);     sorted[s] = v;     s++;   }   bool z = false;   void f(int v, int c) {     was[v] = true;     for (int i = 0; i < N; i++)       if (matr[v][i]) {         if (was[i])           z = true;         else           f(i, c);       }     chains[c].push_back(v);   }   int main() {     scanf( %d%d%d , &N, &Q, &T);     for (int i = 0; i < N; i++) scanf( %d , &amounts[i]);     for (int i = 0; i < 300; i++)       for (int j = 0; j < 300; j++) matr[i][j] = false;     for (int i = 0; i < Q; i++) {       int a, b;       scanf( %d%d , &a, &b);       a--;       b--;       matr[a][b] = true;     }     for (int i = 0; i < 300; i++) was[i] = false;     for (int i = 0; i < N; i++)       if (!was[i]) srt(i);     reverse(sorted, sorted + N);     for (int i = 0; i < 300; i++) was[i] = false;     int cCnt = 0;     for (int i = 0; i < N; i++)       if (!was[sorted[i]]) {         f(sorted[i], cCnt);         cCnt++;       }     for (int i = 0; i < cCnt; i++) {       for (int j = chains[i].size() - 1; j >= 0; j--) {         chains[i][j] = amounts[chains[i][j]];         if (j + 1 < chains[i].size()) chains[i][j] += chains[i][j + 1];       }     }     if (z) {       printf( %d n , 0);       return 0;     }     memset(mas, 0, sizeof(long long) * 100010 * 2);     mas[0][0] = 1;     int index = 0;     for (int i = 0; i < cCnt; i++) {       for (int j = 0; j < chains[i].size(); j++) {         int amount = chains[i][j];         for (int k = 0; k < 100010; k++) mas[(index + 1) % 2][k] = 0;         for (int k = 0; k < 100010; k++) {           if (k - amount >= 0)             mas[index % 2][k] =                 (mas[index % 2][k] + mas[index % 2][k - amount]) % 1000000007;           if (j == 0)             mas[(index + 1) % 2][k] = mas[index % 2][k];           else if (k - amount >= 0)             mas[(index + 1) % 2][k] = mas[index % 2][k - amount];         }         index++;       }     }     cout << mas[index % 2][T] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<long long int, vector<long long int>> v;   map<long long int, long long int> vis;   void dfs(long long int n) {     long long int i;     cout << n <<    ;     vis[n] = 1;     for (i = 0; i < v[n].size(); i++) {       if (!vis[v[n][i]]) {         vis[v[n][i]] = 1;         dfs(v[n][i]);       }     }   }   int main() {     long long int n, i, j, k, p, q, ans = 0;     cin >> n;     map<long long int, long long int> make_pair;     for (i = 1; i <= n; i++) {       cin >> p >> q;       v[p].push_back(q);       v[q].push_back(p);       make_pair[p]++;       make_pair[q]++;     }     for (auto it : make_pair) {       if (it.second == 1) {         ans = it.first;         break;       }     }     dfs(ans);   }


#include <bits/stdc++.h>   using namespace std;   int n, m, x, y;   int p[100010], q[100010];   vector<int> t[100010], v[100010];   int mn[100010];   bool flag[100010];   inline int dist(int x, int y) {     if (p[x] - q[y] >= 0)       return p[x] - q[y];     else       return q[y] - p[x];   }   int main() {     scanf( %d%d%d%d , &n, &m, &x, &y);     memset(mn, 63, sizeof(mn));     for (int i = 1; i <= n; i++) scanf( %d , &p[i]);     for (int i = 1; i <= m; i++) scanf( %d , &q[i]);     q[m + 1] = 1e9;     q[0] = -1e9;     int pos = 1;     for (int i = 1; i <= n; i++) {       while (pos <= m && q[pos] <= p[i]) pos++;       pos--;       if (dist(i, pos) == dist(i, pos + 1)) {         t[i].push_back(pos);         t[i].push_back(pos + 1);       } else if (dist(i, pos) < dist(i, pos + 1)) {         t[i].push_back(pos);       } else {         t[i].push_back(pos + 1);       }     }     for (int i = 1; i <= n; i++)       if (t[i].size() == 1) {         int t1 = t[i][0];         v[t1].push_back(i);         mn[t1] = min(mn[t1], dist(i, t1));       }     for (int i = 1; i <= n; i++)       if (t[i].size() == 1) {         if (dist(i, t[i][0]) > mn[t[i][0]]) flag[i] = 1;       }     for (int i = 1; i <= n; i++)       if (t[i].size() == 2) {         bool flag1 = 0, flag2 = 0;         int cnt1 = 0, cnt2 = 0;         int t1 = t[i][0], t2 = t[i][1], d = dist(i, t1);         for (int j = 0; j < v[t1].size(); j++) {           if (dist(v[t1][j], t1) < d) flag1 = 1;           if (d < dist(v[t1][j], t1))             if (flag[v[t1][j]] == 0) cnt1++;         }         for (int j = 0; j < v[t2].size(); j++) {           if (dist(v[t2][j], t2) < d) flag2 = 1;           if (d < dist(v[t2][j], t2))             if (flag[v[t2][j]] == 0) cnt2++;         }         if (cnt1 + flag1 <= cnt2 + flag2) {           if (flag1) flag[i] = 1;           for (int j = 0; j < v[t1].size(); j++) {             if (d < dist(v[t1][j], t1))               if (flag[v[t1][j]] == 0) flag[v[t1][j]] = 1;           }           mn[t1] = min(mn[t1], dist(i, t1));           v[t1].push_back(i);         } else {           if (flag2) flag[i] = 1;           for (int j = 0; j < v[t2].size(); j++) {             if (d < dist(v[t2][j], t2))               if (flag[v[t2][j]] == 0) flag[v[t2][j]] = 1;           }           mn[t2] = min(mn[t2], dist(i, t2));           v[t2].push_back(i);         }       }     int ans = 0;     for (int i = 1; i <= n; i++) ans += flag[i];     printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double Pi = acos(-1.0);   const int Inf = 0x3f3f3f3f;   const int MAXN = 2e5 + 10;   inline int read() {     register int x = 0, f = 1, ch = getchar();     while (!isdigit(ch)) f = ch ==  -  ? -1 : 1, ch = getchar();     while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();     return x * f;   }   inline long long readll() {     register long long x = 0, f = 1, ch = getchar();     while (!isdigit(ch)) f = ch ==  -  ? -1 : 1, ch = getchar();     while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();     return x * f;   }   struct Edge1 {     int u, v, d;     Edge1(int u_ = 0, int v_ = 0, int d_ = 0) { u = u_, v = v_, d = d_; }   } e[MAXN << 1];   struct Edge2 {     int x, y;     bool s;   };   struct Node {     int f[35];     inline void Insert(int x) {       for (int i = 30; i >= 0; --i)         if (x & (1 << i)) {           if (!f[i]) {             f[i] = x;             return;           }           x ^= f[i];         }     }     inline int Query(int x) {       for (int i = 30; i >= 0; --i)         if ((x ^ f[i]) < x) x ^= f[i];       return x;     }   } n1;   map<pair<int, int>, int> mp;   vector<Edge1> v[MAXN << 2];   int n, m, c, Q, tim;   int st[MAXN << 1], ed[MAXN << 1];   int qx[MAXN], qy[MAXN];   int fa[MAXN], f[MAXN], dep[MAXN];   inline int Find_fa(int x) { return fa[x] == x ? x : Find_fa(fa[x]); }   inline int Find_dis(int x) { return fa[x] == x ? 0 : f[x] ^ Find_dis(fa[x]); }   inline void Insert(int k, int l, int r, int x, int y, Edge1 e1) {     if (x <= l && r <= y) {       v[k].push_back(e1);       return;     }     int mid = (l + r) >> 1;     if (x <= mid) Insert(k << 1, l, mid, x, y, e1);     if (y > mid) Insert(k << 1 | 1, mid + 1, r, x, y, e1);   }   inline void Solve(int k, int l, int r, Node G) {     stack<Edge2> stk;     for (int i = 0; i < (int)v[k].size(); ++i) {       int x = v[k][i].u, y = v[k][i].v, d = v[k][i].d;       int ex = Find_fa(x), ey = Find_fa(y);       d ^= Find_dis(x) ^ Find_dis(y);       if (ex == ey)         G.Insert(d);       else {         if (dep[ex] > dep[ey]) swap(ex, ey), swap(x, y);         Edge2 cur = (Edge2){ex, ey, 0};         fa[ex] = ey;         f[ex] = d;         if (dep[ex] == dep[ey]) ++dep[ey], cur.s = 1;         stk.push(cur);       }     }     if (l == r)       printf( %d n , G.Query(Find_dis(qx[l]) ^ Find_dis(qy[l])));     else {       int mid = (l + r) >> 1;       Solve(k << 1, l, mid, G);       Solve(k << 1 | 1, mid + 1, r, G);     }     while (!stk.empty())       f[fa[stk.top().x] = stk.top().x] = 0, dep[stk.top().y] -= stk.top().s,                           stk.pop();   }   int main() {     n = read();     m = read();     for (int i = 1; i <= n; ++i) fa[i] = i;     for (int i = 1; i <= m; ++i) {       int u = read(), v = read(), d = read();       mp[make_pair(u, v)] = i;       st[i] = 1;       ed[i] = -1;       e[i] = Edge1(u, v, d);     }     c = m;     tim = 1;     Q = read();     for (int i = 1, opt, x, y, z; i <= Q; ++i) {       opt = read();       x = read();       y = read();       if (opt == 1) {         z = read();         mp[make_pair(x, y)] = ++c;         st[c] = tim;         ed[c] = -1;         e[c] = Edge1(x, y, z);       } else if (opt == 2)         ed[mp[make_pair(x, y)]] = tim - 1;       else         qx[tim] = x, qy[tim] = y, ++tim;     }     --tim;     for (int i = 1; i <= c; ++i) {       if (ed[i] == -1) ed[i] = tim;       if (st[i] <= ed[i]) Insert(1, 1, tim, st[i], ed[i], e[i]);     }     Solve(1, 1, tim, n1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int mx[4] = {0, 0, 1, -1};   int my[4] = {-1, 1, 0, 0};   int n, m, cnt, cou;   char a[55][55];   bool used[55][55];   bool good(int x, int y) { return (x >= 0 && x < n && y >= 0 && y < m); }   void dfs(int i, int j) {     used[i][j] = true;     cou++;     for (int k = 0; k < 4; k++)       if (good(i + mx[k], j + my[k]) && a[i + mx[k]][j + my[k]] ==  #  &&           !used[i + mx[k]][j + my[k]])         dfs(i + mx[k], j + my[k]);   }   int main() {     scanf( %d%d n , &n, &m);     for (int i = 0; i < n; i++) {       scanf( %s n , &a[i]);       for (int j = 0; j < m; j++)         if (a[i][j] ==  # ) cnt++;     }     if (cnt <= 2)       printf( -1 n );     else {       int ans = 2;       for (int i = 0; i < n; i++)         for (int j = 0; j < m; j++)           if (a[i][j] ==  # ) {             a[i][j] =  . ;             cou = 0;             memset(used, 0, sizeof(used));             for (int si = 0; si < n; si++)               for (int sj = 0; sj < m; sj++)                 if (a[si][sj] ==  # ) dfs(si, sj), si = n, sj = m;             if (cou != cnt - 1) ans = 1;             a[i][j] =  # ;           }       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 2e9 + 1;   const long long INFLL = 1e18 + 1;   struct Graph {     struct edge {       int c, f, link, to, ind;       int c2 = 0;       edge() {}       edge(int c, int f, int link, int to, int ind)           : c(c), f(f), link(link), to(to), ind(ind) {}     };     vector<vector<edge>> g;     int s, t, n;     Graph(int N, int S, int T) {       n = N;       s = S, t = T;       g.resize(n);     }     vector<int> usd;     int dfs(int v, int now) {       usd[v] = 1;       if (v == t) return now;       for (auto &e : g[v]) {         if (usd[e.to] || e.f == e.c) continue;         int x = dfs(e.to, min(now, e.c - e.f));         if (x == 0) continue;         e.f += x;         g[e.to][e.link].f -= x;         return x;       }       return 0;     }     int Ford_Fulkerson() {       int ans = 0;       while (1) {         usd.assign(n, 0);         int d = dfs(s, INF);         ans += d;         if (d == 0) break;       }       return ans;     }     void add_not_dir(int a, int b, int c, int i) {       g[a].emplace_back(c, 0, g[b].size(), b, i);       g[b].emplace_back(c, 0, g[a].size() - 1, a, i);     }     void add_dir(int a, int b, int c, int i) {       g[a].emplace_back(c, 0, g[b].size(), b, i);       g[b].emplace_back(0, 0, g[a].size() - 1, a, i);     }     int Edmonds_Karp() {       int ans = 0;       while (1) {         queue<int> q;         vector<int> p(n, -1), dist(n, INF), mn(n, INF);         q.push(s);         dist[s] = 0;         while (!q.empty()) {           int v = q.front();           q.pop();           for (auto &e : g[v]) {             if (e.f == e.c || dist[e.to] != INF) continue;             p[e.to] = v;             q.push(e.to);             dist[e.to] = dist[v] + 1;             mn[e.to] = min(mn[v], e.c - e.f);           }         }         if (dist[t] == INF) break;         vector<int> path;         int now = t;         while (now != -1) {           path.emplace_back(now);           now = p[now];         }         reverse((path).begin(), (path).end());         int F = mn[t];         ans += F;         for (int i = 0; i < (int)path.size() - 1; i++) {           int v = path[i];           for (auto &e : g[v]) {             if (e.to == path[i + 1] && e.c - e.f >= F) {               e.f += F;               g[e.to][e.link].f -= F;               break;             }           }         }       }       return ans;     }     vector<int> d, uk;     int dfs_Dinic(int v, int now) {       if (v == t || now == 0) return now;       for (int i = uk[v]; i < (int)g[v].size(); i++) {         if (d[g[v][i].to] == d[v] + 1) {           int can = g[v][i].c2 - g[v][i].f;           int x = dfs_Dinic(g[v][i].to, min(can, now));           if (x != 0) {             g[v][i].f += x;             g[g[v][i].to][g[v][i].link].f -= x;             return x;           }         }         uk[v]++;       }       return 0;     }     bool bfs() {       d.assign(n, INF);       d[s] = 0;       deque<int> q;       q.push_back(s);       while (!q.empty()) {         int v = q.front();         q.pop_front();         for (auto &e : g[v]) {           if (e.f < e.c2 && d[e.to] == INF) {             d[e.to] = d[v] + 1;             q.push_back(e.to);           }         }       }       return d[t] != INF;     }     long long Dinic() {       long long ans = 0;       while (bfs()) {         uk.assign(n, 0);         while (1) {           int d = dfs_Dinic(s, INF);           if (d == 0) break;           ans += d;         }       }       return ans;     }     long long Fast_Dinic() {       long long ans = 0;       for (int j = 29; j >= 0; j--) {         for (int i = 0; i < n; i++) {           for (int k = 0; k < (int)g[i].size(); k++) {             if (g[i][k].c & (1 << j)) g[i][k].c2 += (1 << j);           }         }         ans += Dinic();       }       return ans;     }   };   bool is_prime(int x) {     for (int i = 2; i * i <= x; i++) {       if (x % i == 0) return false;     }     return true;   }   vector<int> usd;   vector<vector<int>> gr;   void dfs_ans(int v, vector<int> &comp) {     comp.emplace_back(v);     usd[v] = 1;     for (auto &to : gr[v]) {       if (!usd[to]) dfs_ans(to, comp);     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     vector<int> a(n);     int cnt0 = 0, cnt1 = 0;     for (auto &c : a) {       cin >> c;       if (c % 2 == 0)         cnt0++;       else         cnt1++;     }     if (cnt0 != cnt1) {       cout <<  Impossible ;       return 0;     }     Graph g(n + 2, n, n + 1);     for (int i = 0; i < n; i++) {       if (a[i] % 2 == 1) {         g.add_dir(n, i, 2, -1);       } else {         g.add_dir(i, n + 1, 2, i);       }     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         if (a[i] % 2 == 1 && a[j] % 2 == 0) {           if (is_prime(a[i] + a[j])) {             g.add_dir(i, j, 1, -1);           }         }       }     }     int x = g.Edmonds_Karp();     if (x != 2 * cnt0) {       cout <<  Impossible ;       return 0;     }     gr.resize(n);     vector<vector<int>> ans;     for (int i = 0; i < n; i++) {       for (auto &e : g.g[i]) {         if (e.f == 1) {           gr[i].emplace_back(e.to);           gr[e.to].emplace_back(i);         }       }     }     usd.assign(n, 0);     for (int i = 0; i < n; i++) {       if (!usd[i]) {         ans.emplace_back(vector<int>());         dfs_ans(i, ans.back());       }     }     cout << ans.size() <<   n ;     for (auto &c : ans) {       cout << c.size() <<    ;       for (auto &l : c) {         cout << l + 1 <<    ;       }       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline T abs(T a) {     return a > 0 ? a : -a;   }   int n;   int m;   int main() {     int last;     cin >> n >> last;     if (n == 1) {       if (last == 1) {         cout << 0 << endl <<  T  << endl;       } else {         cout <<  IMPOSSIBLE  << endl;       }       return 0;     }     int ans = -1;     int errans = 1e9;     for (int i = 1; i <= last; i++) {       int a = last, b = i;       int err = 0, len = 0;       while (b) {         err += (a / b) - 1;         len += a / b;         a %= b;         swap(b, a);       }       if (a != 1) continue;       err--;       if (len == n) {         if (errans > err) {           errans = err;           ans = i;         }       }     }     if (ans < 0) {       cout <<  IMPOSSIBLE  << endl;       return 0;     }     vector<int> res;     int a = last, b = ans;     while (b) {       res.push_back(a / b);       a %= b;       swap(b, a);     }     reverse((res).begin(), (res).end());     res[0]--;     cout << errans << endl;     cout <<  T ;     for (int i = 0; i < (((int)(res).size())); i++)       for (int j = 0; j < (res[i]); j++) printf( %c , ((i + 1) % 2 ?  B  :  T ));     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int RLEN = 1 << 18 | 1;   inline char nc() {     static char ibuf[RLEN], *ib, *ob;     (ib == ob) && (ob = (ib = ibuf) + fread(ibuf, 1, RLEN, stdin));     return (ib == ob) ? -1 : *ib++;   }   inline int rd() {     char ch = nc();     int i = 0, f = 1;     while (!isdigit(ch)) {       if (ch ==  - ) f = -1;       ch = nc();     }     while (isdigit(ch)) {       i = (i << 1) + (i << 3) + ch -  0 ;       ch = nc();     }     return i * f;   }   inline void W(long long x) {     static int buf[50];     if (!x) {       putchar( 0 );       return;     }     if (x < 0) {       putchar( - );       x = -x;     }     while (x) {       buf[++buf[0]] = x % 10;       x /= 10;     }     while (buf[0]) putchar(buf[buf[0]--] +  0 );   }   const int N = 4e5 + 50, M = 1e7 + 50;   int n, a[N], b[N], li[N], ri[N];   int rt[N], lc[M], rc[M], c1[M], tot;   long long s1[M], s2[M], ans;   inline int ga(int *anc, int x) {     return (anc[x] == x) ? x : (anc[x] = ga(anc, anc[x]));   }   inline void upt(int k) {     c1[k] = c1[lc[k]] + c1[rc[k]];     s1[k] = s1[rc[k]] + s1[lc[k]] + c1[rc[k]] * s2[lc[k]];     s2[k] = s2[lc[k]] + s2[rc[k]];   }   inline void inc(int &k, int l, int r, int p) {     if (!k) k = ++tot;     if (l == r) {       c1[k] = 1;       s1[k] = s2[k] = p;       return;     }     int mid = (l + r) >> 1;     if (p <= mid)       inc(lc[k], l, mid, p);     else       inc(rc[k], mid + 1, r, p);     upt(k);   }   inline int merge(int x, int y) {     if (!x || !y) return (x + y);     lc[x] = merge(lc[x], lc[y]);     rc[x] = merge(rc[x], rc[y]);     upt(x);     return x;   }   inline void inc(int x, int y) {     int r = ga(ri, x);     li[r + 1] = r;     ri[r] = ri[r + 1] = r + 1;     ans -= s1[rt[x]] + (long long)(x - 1) * s2[rt[x]];     inc(rt[x], 1, n, y);     ans += s1[rt[x]] + (long long)(x - 1) * s2[rt[x]];   }   inline void _merge(int x, int y) {     ans -= s1[rt[x]] + (long long)(x - 1) * s2[rt[x]];     ans -= s1[rt[y]] + (long long)(y - 1) * s2[rt[y]];     rt[x] = merge(rt[x], rt[y]);     ans += s1[rt[x]] + (long long)(x - 1) * s2[rt[x]];     ri[y - 1] = y;     li[y] = y - 1;   }   int main() {     n = rd();     for (int i = 1; i <= n; i++) {       a[i] = rd(), b[i] = rd();       ans -= (long long)a[i] * b[i];       if (li[a[i] - 1]) {         inc(ga(li, a[i] - 1), b[i]);       } else {         if (!li[a[i]]) {           li[a[i]] = ri[a[i]] = a[i];           inc(rt[a[i]], 1, n, b[i]);           ans += s1[rt[a[i]]] + (a[i] - 1) * s2[rt[a[i]]];         } else           inc(a[i], b[i]);       }       while (li[ga(ri, a[i]) + 1]) _merge(ga(li, a[i]), ga(ri, a[i]) + 1);       W(ans);       putchar(  n );     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 24;   int f[1 << MAXN], a[MAXN], N;   vector<pair<int, int> > p[MAXN];   int dp(int state, int pos) {     if (f[state] != -1) return f[state];     if (state == 1) return 1;     if (p[pos].size() == 0) {       f[state] = 0x3F3F3F3F;       return f[state];     }     int tot = 0, flag = 0;     for (int i = 0; i <= pos; i++)       if ((state >> i) & 1) tot++;     int ret = 0x3F3F3F3F;     for (int i = 0; i < p[pos].size(); i++) {       int nxtstate = ((state & (~(1 << pos))) | (1 << pos - 1) |                       (1 << p[pos][i].first) | (1 << p[pos][i].second));       ret = min(dp(nxtstate, pos - 1), ret);     }     return f[state] = max(ret, tot);   }   int main() {     scanf( %d , &N);     memset(f, -1, sizeof(f));     for (int i = 0; i < N; i++) scanf( %d , a + i);     for (int i = 1; i < N; i++) {       for (int j = 0; j < i; j++)         for (int k = j; k < i; k++)           if (a[j] + a[k] == a[i]) p[i].push_back(make_pair(j, k));     }     int ans = dp(1 << (N - 1), N - 1);     if (ans == 0x3F3F3F3F)       printf( -1 n );     else       printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string A, B;   int has[10];   string res;   void Create(int from) {     for (int i = 0; i < 10; i++) {       for (int j = 0; j < has[i]; j++) {         res[from++] = i +  0 ;       }     }   }   int main() {     cin >> A >> B;     int st = 0;     for (int i = 0; i < A.length(); i++) has[A[i] -  0 ]++;     while (A.length() < B.length()) {       A =  0  + A;       st++;     }     res = A;     for (int i = st; i < A.length(); i++) {       for (int j = 10 - 1; j >= 0; j--) {         if (has[j] > 0) {           has[j]--;           res[i] = j +  0 ;           Create(i + 1);           if (res <= B) break;           has[j]++;         }       }     }     int pos = 0;     while (res[pos] ==  0 ) pos++;     cout << res.substr(pos) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());   const int MAXN = 200100, MAXK = 25;   const long long MOD = 998244353;   int st[MAXN][MAXK];   int n, q;   int a[MAXN];   pair<int, int> lastP[MAXN];   int lg[MAXN];   void buildST() {     for (int j = (1); j <= (MAXK); j++)       for (int i = 0; i + (1 << j) <= q; i++)         st[i][j] = max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);   }   int getMax(int l, int r) {     int p = lg[r - l + 1];     return max(st[l][p], st[r - (1 << p) + 1][p]);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(nullptr);     cin >> n;     for (int i = (1); i <= (n); i++) cin >> a[i];     for (int i = (1); i <= (n); i++) lastP[i] = {a[i], -1};     cin >> q;     for (int i = (2); i <= (q); i++) lg[i] = lg[i / 2] + 1;     for (int i = (0); i <= (q - 1); i++) {       int t;       cin >> t;       if (t == 1) {         int p, x;         cin >> p >> x;         lastP[p] = {x, i};       } else {         int x;         cin >> x;         st[i][0] = x;       }     }     buildST();     for (int i = (1); i <= (n); i++) {       int cur = lastP[i].first, wh = lastP[i].second;       cur = max(cur, getMax(wh + 1, q - 1));       cout << cur <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 1e5 + 1;   long long n, a[maxn], b[maxn], f1, f2;   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     for (long long i = 0; i < n; ++i) cin >> a[i];     for (long long i = 0; i < n; ++i) cin >> b[i];     long long max1 = a[0], max2 = b[0];     f1 = a[0];     f2 = b[0];     for (long long i = 1; i < n; ++i) {       f1 = max2 + a[i];       f2 = max1 + b[i];       max1 = max(max1, f1);       max2 = max(max2, f2);     }     cout << max(max1, max2) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2010;   int n, m, k, p, q, d[maxn][maxn];   bool vis[maxn][maxn];   bool solve(int x, int y, int r) {     for (int i = max(1, x - r); i <= min(n, x + r); i++) {       int tmp = r - abs(i - x);       if (y - tmp >= 1 && !vis[i][y - tmp]) {         p = i, q = y - tmp;         return 1;       }       if (y + tmp <= m && !vis[i][y + tmp]) {         p = i, q = y + tmp;         return 1;       }     }     return 0;   }   int main() {     scanf( %d %d %d , &n, &m, &k);     for (int x, y, i = 1; i <= k; i++) {       scanf( %d %d , &x, &y);       for (int j = -1; j <= 1; j++)         for (int k = -1; k <= 1; k++) {           if (x + j < 1 || x + j > n || y + j < 1 || y + j > m) continue;           d[x][y] = max(d[x][y], d[x + j][y + k] - abs(j) - abs(k));         }       while (!solve(x, y, d[x][y])) d[x][y]++;       printf( %d %d n , p, q);       vis[p][q] = 1;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int pro(int *a, int *b, int t, int s) {     int k = 0;     for (int i = 1; i < t; i++) {       if (b[i] != s) {         return 2;       }       if (b[i] == 0) {         k++;       }     }     if (k == (t - 1)) {       return 3;     }     return 1;   }   int main() {     int t, a[105], b[105];     a[0] = 0;     cin >> t;     for (int i = 1; i <= t; i++) {       cin >> a[i];       b[i - 1] = a[i] - a[i - 1];     }     if (t == 1) {       cout << a[1];     }     int c = pro(a, b, t, b[1]);     if (c == 3) {       cout << a[1];     } else if (c == 1) {       cout << a[t] + b[1];     } else {       cout << a[t];     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   const int maxn = 1e6 + 5;   const int inf = 0x3f3f3f3f;   const long long INF = 0x3f3f3f3f3f3f3f3f;   const double eps = 1e-8;   const double PI = acos(-1.0);   long long dp[1005][105];   long long d, res, tot = 9, tmp = 1;   int main() {     int n, k, m;     scanf( %d%d%d , &n, &k, &m);     for (int i = 1; i <= n; i++) {       for (int j = 0; j < k; j++) {         for (int t = 0 + (i == n); t <= 9; t++) {           d = (t * tmp + j) % k;           if (d) dp[i][d] = (dp[i][d] + dp[i - 1][j] + (j == 0)) % m;         }       }       tmp = (tmp * 10) % k;     }     for (int i = 1; i < n; i++) tot = tot * 10 % m;     for (int i = 1; i < k; i++) res = (res + dp[n][i]) % m;     printf( %lld n , (m + tot - res) % m);     return 0;   }


#include <bits/stdc++.h>       #define PI 3.141592653589793238462643383279   #define EXP 2.71828182846       #define ll long long      using namespace std;      double log2(double n)   {    return log(n)/log(2);   }      double ln(double n)   {    return log(n)/log(EXP);   }       void Heap(signed long long int A[],signed long long int n,signed long long int l,signed long long int r)   {    signed long long int x,i,j;    x=A[l];    i=l;    while(1)    {     j=2*i;     if(j>r)break;     if(j<r&&A[j+1]>=A[j])     j++;     if(x>=A[j]) break;     A[i]=A[j];     i=j;    }    A[i]=x;   }       void HeapSort(signed long long int A[],signed long long int n)   {    signed long long int l,r,temp;    l=n/2;    while(l>=0)    {     Heap(A,n,l,n-1);     l--;    }    r=n-1;    while(r>=1)    {     temp=A[0];     A[0]=A[r];     A[r]=temp;     r--;     Heap(A,n,0,r);    }   }        bool issimple(unsigned long long n){     for(unsigned long long i=2;i<=sqrt(n);i++)     if(n%i==0)      return false;    return true;   }       long long nod(long long a,long long b)   {       long long temp;       while(b)       {           a%=b;           temp=a;           a=b;           b=temp;       }       return a;   }       long long nok(long long a,long long b,long long nod)   {       return a/nod*b;   }       unsigned long long fac(unsigned long long n)   {    unsigned long long f=1;    if(n==0||n==1) return f;    for(int i=2;i<=n;i++)    {     f*=i;    }    return f;   }       int bSearch(int A[],int n,int x)   {    int l=0,r=n-1,m,found=0,c=0;    while(l<=r)     {     m=(l+r)/2;     c++;     if(A[m]==x)      {      found=1;      break;     }     //c++;     if(A[m]<x) l=m+1;     else r=m-1;    }    if(found)     return c;    return 0;   }      map<long long int,long long int> factorize(long long x) {       map<long long int,long long int> factors;           for (int i = 2; i <= sqrt(x); i++) {           while (x % i == 0) {            if(factors.find(i)==factors.end())                factors.insert({i,1});               else                factors[i]++;               x /= i;           }       }           if (x != 1) {           if(factors.find(x)==factors.end())                factors.insert({x,1});               else                factors[x]++;       }           return factors;   }      void answer(bool yes)   {    yes? cout<< YES n :cout<< NO n ;   }      long long ab(long long val)   {    if(val<0) return -val;    return val;   }      bool even(long long number)   {    if (number & 1)        return 0; //íå÷åòíîå    else         return 1; //÷åòíîå   }      int main()   {       long long n,k,i,count,j,t;       bool yes,flag;    cin>>t;    while(t--)    {     cin>>n;     count=0;          count=0;          for(i=1+n;!count;i++)     {      if(issimple(i))      {       for(j=i+n;;j++)       {        if(issimple(j))        {         count=(i)*(j);         break;        }       }             }     }          cout<<count<<endl;     //answer(yes);    }              return 0;   }


#include <bits/stdc++.h>   using namespace std;   unsigned long long int ncr(unsigned long long int a, unsigned long long int b) {     unsigned long long int ans = 1;     for (unsigned long long int i = 1; i <= b; i++) {       ans *= (a - i + 1);     }     for (unsigned long long int i = 1; i <= b; i++) {       ans /= i;     }     return ans;   }   unsigned long long int power(unsigned long long int a,                                unsigned long long int b) {     unsigned long long int ans = 1;     for (int i = 1; i <= b; i++) {       ans *= a;     }     return ans;   }   unsigned long long int solvelessthansingle(int numofdigit) {     unsigned long long int ans = ncr(numofdigit - 1, 1);     ans *= 9;     return ans;   }   unsigned long long int solvelessthandouble(int numofdigit) {     unsigned long long int ans = ncr(numofdigit - 1, 2);     ans *= 81;     return ans;   }   unsigned long long int solvelessthantriple(unsigned long long int numofdigit) {     unsigned long long int ans = ncr(numofdigit - 1, 3);     ans *= 729;     return ans;   }   unsigned long long int solvelessthannone(unsigned long long int numofdigit) {     return 1;   }   unsigned long long int solve(unsigned long long int n,                                unsigned long long int rem) {     if (rem == 0) {       return 0;     }     if (n < 10) {       return n;     }     int numofdigit = 0;     unsigned long long int n2 = n;     while (n2) {       numofdigit++;       n2 /= 10;     }     unsigned long long int ans = 0;     if (rem >= 1) ans += solvelessthansingle(numofdigit);     if (rem >= 2) ans += solvelessthandouble(numofdigit);     if (rem >= 3) ans += solvelessthantriple(numofdigit);     int lastdigit;     unsigned long long int n1 = n;     while (n1) {       int temp = n1 % 10;       lastdigit = temp;       n1 /= 10;     }     if (rem >= 1) ans += (lastdigit)*solvelessthannone(numofdigit);     if (rem >= 2) ans += (lastdigit - 1) * solvelessthansingle(numofdigit);     if (rem >= 3) ans += (lastdigit - 1) * solvelessthandouble(numofdigit);     unsigned long long int mul =         power((unsigned long long int)10, (unsigned long long int)numofdigit - 1);     mul *= lastdigit;     n -= mul;     ans += solve(n, rem - 1);     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t = 1;     cin >> t;     while (t--) {       unsigned long long int n, m;       cin >> n >> m;       unsigned long long int ans = solve(m, 3) - solve(n - 1, 3);       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int block_size = 320;   const long long mod = 1e9 + 7;   const long long inf = 1e9 + 7;   const long double eps = 1e-9;   const double PI = atan(1) * 4;   template <typename T>   inline int sign(const T &a) {     if (a < 0) return -1;     if (a > 0) return 1;     return 0;   }   template <typename T, typename S>   inline bool upmin(T &a, const S &b) {     return a > b ? a = b, 1 : 0;   }   template <typename T, typename S>   inline bool upmax(T &a, const S &b) {     return a < b ? a = b, 1 : 0;   }   template <typename T>   inline void in(T &x) {     x = 0;     T f = 1;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (isdigit(ch)) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     x *= f;   }   long long twop(int x) { return 1LL << x; }   template <typename A, typename B>   inline void in(A &x, B &y) {     in(x);     in(y);   }   template <typename A, typename B, typename C>   inline void in(A &x, B &y, C &z) {     in(x);     in(y);     in(z);   }   template <typename A, typename B, typename C, typename D>   inline void in(A &x, B &y, C &z, D &d) {     in(x);     in(y);     in(z);     in(d);   }   int c[500010], tsize[500010], hv[500010], ans[500010];   vector<int> son[500010];   int mp[4194304 + 10];   int global;   int addLen;   int opt[4194304], cnt = 0;   void change(int id, int to) {     upmax(mp[id], to);     opt[cnt++] = id;   }   void rollback() {     while (cnt > 0) {       mp[opt[--cnt]] = -inf;     }   }   void tra(int cur, int len, int mask, int &ans) {     for (long long i = 0; i < 22; i++) {       ans = max(ans, mp[mask ^ twop(i) ^ global] + len + addLen);     }     ans = max(ans, mp[mask ^ global] + len + addLen);     for (auto i : son[cur]) {       tra(i, len + 1, mask ^ twop(c[i]), ans);     }   }   void add(int cur, int len, int mask) {     change(mask ^ global, len - addLen);     for (auto i : son[cur]) {       add(i, len + 1, mask ^ twop(c[i]));     }   }   void solve(int cur, bool record) {     for (auto s : son[cur]) {       if (s != hv[cur]) {         solve(s, 0);         upmax(ans[cur], ans[s]);       }     }     if (hv[cur]) {       solve(hv[cur], 1);       global ^= twop(c[hv[cur]]);       addLen++;       change(global, -addLen);       upmax(ans[cur], ans[hv[cur]]);       { upmax(ans[cur], mp[global] + addLen); }       for (long long i = 0; i < 22; i++) {         upmax(ans[cur], mp[0 ^ twop(i) ^ global] + addLen);       }       for (auto i : son[cur]) {         if (i != hv[cur]) {           tra(i, 1, twop(c[i]), ans[cur]);           add(i, 1, twop(c[i]));         }       }     } else {       mp[0] = 0;       global = 0;       addLen = 0;     }     if (!record) {       global = 0;       rollback();       addLen = 0;     }   }   void dfs(int cur) {     tsize[cur] = 1;     for (auto i : son[cur]) {       dfs(i), tsize[cur] += tsize[i];       if (tsize[i] > tsize[hv[cur]]) hv[cur] = i;     }   }   int main() {     memset(mp, 0xcf, sizeof(mp));     int n;     in(n);     for (long long i = 2; i < n + 1; i++) {       int p;       in(p);       char cc = getchar();       c[i] = cc -  a ;       son[p].push_back(i);     }     map<int, int> mp;     int global = 0, addLen = 0;     dfs(1);     solve(1, 0);     for (long long i = 1; i < n + 1; i++) printf( %d  , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int t = 1;     ios_base::sync_with_stdio(false);     cin.tie(NULL);     while (t--) {       long long int i, j, k, m, n, ans = 0;       cin >> n >> k;       if (n >= k) n = k - 1;       if (k - n >= n) {         cout << 0;         return 0;       }       long long int hi = n, lo = k - n;       cout << (hi - lo + 1) / 2;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long sum(long long n) { return (n * (n - 1)) / 2; }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n;     cin >> n;     int arr[n + 1];     for (int i = 0; i < n; i++) cin >> arr[i];     pair<int, int> b[n + 1];     for (int i = 0; i < n; i++) {       cin >> b[i].first;       b[i].second = i;     }     sort(b, b + n);     sort(arr, arr + n, greater<int>());     long long a[n + 1];     for (int i = 0; i < n; i++) {       a[b[i].second] = arr[i];     }     for (int i = 0; i < n; i++) cout << a[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   bool uin(T &a, T b) {     return a > b ? (a = b, true) : false;   }   template <class T>   bool uax(T &a, T b) {     return a < b ? (a = b, true) : false;   }   struct TPoint {     long long x, y;     TPoint(long long x, long long y) : x(x), y(y) {}     TPoint operator+(const TPoint &p) const { return TPoint(x + p.x, y + p.y); }     TPoint operator-(const TPoint &p) const { return TPoint(x - p.x, y - p.y); }     long long operator%(const TPoint &p) const { return x * p.y - y * p.x; }     long long operator*(const TPoint &p) const { return x * p.x + y * p.y; }     long long operator~() const { return *this * *this; }     bool operator<(const TPoint &p) const {       return make_pair(x, y) < make_pair(p.x, p.y);     }   };   bool cmp(const TPoint &a, const TPoint &b) {     if (a % b) return a % b > 0;     return ~a < ~b;   }   const int maxn = 210000;   int miny[maxn], maxy[maxn];   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cout.precision(10);     cout << fixed;     int n, m;     while (1) {       cin >> n >> m;       if (!n) break;       for (int i = 0; i < (int)(n + 10); ++i) miny[i] = 1e9, maxy[i] = -1e9;       for (int i = 0; i < (int)(m); ++i) {         int x, y;         cin >> x >> y;         uin(miny[x], y);         uax(maxy[x], y);       }       vector<TPoint> p;       for (int i = 0; i < (int)(n + 2); ++i) {         if (i && miny[i] < miny[i - 1]) p.push_back(TPoint(i, miny[i]));         if (miny[i] < miny[i + 1]) p.push_back(TPoint(i - 1, miny[i]));         if (i && maxy[i] > maxy[i - 1]) p.push_back(TPoint(i, maxy[i] - 1));         if (maxy[i] > maxy[i + 1]) p.push_back(TPoint(i - 1, maxy[i] - 1));       }       TPoint O = *min_element((p).begin(), (p).end());       for (auto &w : p) w = w - O;       sort((p).begin(), (p).end(), cmp);       vector<TPoint> st;       for (auto w : p) {         while (st.size() > 1) {           TPoint prv = st.back(), pprv = st[st.size() - 2];           if ((w - pprv) % (prv - pprv) >= 0)             st.pop_back();           else             break;         }         st.push_back(w);       }       reverse((st).begin(), (st).end());       for (int i = 0; i < (int)(st.size()); ++i) {         st[i] = st[i] + O;       }       int j = min_element((st).begin(), (st).end()) - st.begin();       cout << st.size() <<   n ;       for (int i = 0; i < (int)(st.size()); ++i) {         TPoint w = st[(i + j) % st.size()];         cout << w.x <<     << w.y <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int nextInt() {     char c;     int i;     for (c = getchar(); c <  0  || c >  9 ; c = getchar())       ;     for (i = c -  0 , c = getchar(); c >=  0  && c <=  9 ;          i = i * 10 + c -  0 , c = getchar())       ;     return i;   }   const int inf = 1 << 30;   struct Edge {     int v, w;     Edge(int v = 0, int w = 0) : v(v), w(w) {}   };   struct Info {     int dis, lca;     Info(int dis = 0, int lca = 0) : dis(dis), lca(lca) {}     Info inc_dist(int w) const { return Info(dis + w, lca); }   };   Info up[101000], down[101000];   int anc[101000][20], fa[101000], dep[101000], w[101000];   bool live[101000];   vector<Edge> e[101000];   int n, m;   Info merge_info(const Info& a, const Info& b, int w, int v) {     Info info = b.inc_dist(w);     if (info.dis > a.dis) {       return info;     } else if (info.dis == a.dis) {       info.lca = v;       return info;     } else {       return a;     }   }   void calc_up(int x) {     vector<Edge> son;     for (Edge edge : e[x])       if (edge.v != fa[x]) son.push_back(edge);     int deg = son.size();     vector<Info> pre_max = vector<Info>(deg);     for (int i = 0; i < deg; ++i) {       pre_max[i] =           (i == 0 ? down[son[i].v].inc_dist(son[i].w)                   : merge_info(pre_max[i - 1], down[son[i].v], son[i].w, x));     }     vector<Info> suf_max = vector<Info>(deg);     for (int i = deg - 1; i >= 0; --i) {       suf_max[i] = (i == deg - 1 ? down[son[i].v].inc_dist(son[i].w)                                  : merge_info(suf_max[i + 1], down[son[i].v],                                               son[i].w, x));     }     for (int i = 0; i < deg; ++i) {       int v = son[i].v, w = son[i].w;       Info info = up[x];       if (i != 0) info = merge_info(info, pre_max[i - 1], 0, x);       if (i != deg - 1) info = merge_info(info, suf_max[i + 1], 0, x);       up[v] = merge_info(Info(live[v] ? 0 : -inf, v), info, w, v);       calc_up(v);     }   }   void calc_down(int x) {     down[x] = Info(live[x] ? 0 : -inf, x);     anc[x][0] = fa[x];     for (int i = 1; (1 << i) <= dep[x]; ++i) {       anc[x][i] = anc[anc[x][i - 1]][i - 1];     }     for (Edge edge : e[x])       if (edge.v != fa[x]) {         int v = edge.v;         fa[v] = x, dep[v] = dep[x] + 1, calc_down(v);         down[x] = merge_info(down[x], down[v], edge.w, x);       }   }   int lca(int x, int y) {     if (dep[x] < dep[y]) swap(x, y);     for (int i = 20 - 1; i >= 0; --i)       if (dep[anc[x][i]] >= dep[y]) x = anc[x][i];     if (x == y) return x;     for (int i = 20 - 1; i >= 0; --i)       if (anc[x][i] != anc[y][i]) x = anc[x][i], y = anc[y][i];     return fa[x];   }   void chain_inc(int x, int y) {     int z = lca(x, y);     ++w[x], ++w[y], --w[z], --w[fa[z]];   }   void calc_ans(int x) {     for (Edge edge : e[x])       if (edge.v != fa[x]) {         calc_ans(edge.v), w[x] += w[edge.v];       }   }   int main() {     dep[0] = -1;     n = nextInt();     m = nextInt();     for (int i = 1; i <= m; ++i) {       int v = nextInt();       live[v] = true;     }     for (int i = 1; i <= n - 1; ++i) {       int u = nextInt();       int v = nextInt();       int w = nextInt();       e[u].push_back(Edge(v, w));       e[v].push_back(Edge(u, w));     }     int root = 1;     calc_down(root);     up[root] = Info(live[root] ? 0 : -inf, root);     calc_up(root);     for (int i = 1; i <= n; ++i)       if (live[i]) {         if (up[i].dis > down[i].dis) {           chain_inc(i, up[i].lca);         } else if (up[i].dis < down[i].dis) {           chain_inc(i, down[i].lca);         }       }     calc_ans(1);     int ans = 0, ans_cnt = 0;     for (int i = 1; i <= n; ++i)       if (!live[i]) {         if (w[i] > ans)           ans = w[i], ans_cnt = 1;         else if (w[i] == ans)           ++ans_cnt;       }     printf( %d %d n , ans, ans_cnt);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000, M = 2000;   int head[N + 10], ver[M + 10], nxt[M + 10], tot = 0;   void add(int x, int y) {     ver[++tot] = y;     nxt[tot] = head[x];     head[x] = tot;   }   int col[N + 10], cnt = 0;   void dfs(int x) {     col[x] = cnt;     for (int i = head[x]; i; i = nxt[i]) {       int y = ver[i];       if (col[y]) continue;       dfs(y);     }   }   vector<int> seq[N + 10];   pair<int, int> que[N + 10];   int h = 1, t = 0, fa[N + 10];   bool vis[N + 10];   pair<int, int> bfs(int x, bool f) {     memset(vis, 0, sizeof(vis));     if (f) memset(fa, -1, sizeof(fa));     h = 1;     t = 0;     pair<int, int> ans = make_pair(0, 0);     que[++t] = make_pair(0, x);     vis[x] = 1;     while (h <= t) {       pair<int, int> H = que[h++];       ans = max(ans, H);       for (int i = head[H.second]; i; i = nxt[i]) {         int y = ver[i];         if (vis[y]) continue;         vis[y] = 1;         if (f) fa[y] = H.second;         que[++t] = make_pair(H.first + 1, y);       }     }     return ans;   }   int dis[N + 10], v[N + 10];   int main() {     int n, m;     scanf( %d %d , &n, &m);     for (int i = 1; i <= m; i++) {       int x, y;       scanf( %d %d , &x, &y);       add(x, y);       add(y, x);     }     int maxx = 0, f = 0;     for (int i = 1; i <= n; i++) {       if (!col[i]) {         cnt++;         dfs(i);         pair<int, int> tmp = bfs(bfs(i, false).second, true);         int y = tmp.second;         if (tmp.first >= maxx) {           maxx = tmp.first;           f = cnt;         }         while (~y) {           seq[cnt].push_back(y);           y = fa[y];         }         v[cnt] = seq[cnt][seq[cnt].size() / 2];       }     }     for (int i = 1; i <= cnt; i++) {       if (i == f) continue;       add(v[i], v[f]);       add(v[f], v[i]);     }     printf( %d n , bfs(bfs(1, false).second, false).first);     for (int i = 1; i <= cnt; i++) {       if (i == f) continue;       printf( %d %d n , v[i], v[f]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100010;   int n, a, b, t;   pair<int, int> m[MAXN];   bool ans[MAXN];   int main() {     cin >> n >> a >> b;     for (int i = 0; i < n; i++) {       scanf( %d , &m[i].first);       m[i].second = i;       if (a < b) m[i].second *= -1;     }     if (a != b) sort(m, m + n);     for (int i = 0; i < (a < b ? b : a); i++)       if (a < b)         ans[a < b ? -m[i].second : m[i].second] = true;       else         ans[a < b ? -m[i].second : m[i].second] = true;     for (int i = 0; i < n; i++)       printf( %d  , ((a >= b) ? (ans[i] ? 1 : 2) : (ans[i] ? 2 : 1)));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   T min(T a, T b, T c) {     return min(a, min(b, c));   }   template <class T>   T max(T a, T b, T c) {     return max(a, max(b, c));   }   struct matrix {     vector<vector<long long> > a;     int m, n;     matrix(int _m, int _n) {       m = _m;       n = _n;       a.resize(m + 1);       for (int i = (1); i <= (m); ++i) a[i].resize(n + 1);     }     void read() {       for (int i = (1); i <= (m); ++i) {         for (int j = (1); j <= (n); ++j) scanf( %lld , &a[i][j]);       }     }     bool inline operator==(const matrix &o) const {       if (m != o.m || n != o.n) return false;       for (int i = (1); i <= (m); ++i) {         for (int j = (1); j <= (n); ++j) {           if (a[i][j] != o.a[i][j]) return false;         }       }       return true;     }     void inline operator=(const matrix &o) {       m = o.m;       n = o.n;       for (int i = (1); i <= (m); ++i) {         for (int j = (1); j <= (n); ++j) a[i][j] = o.a[i][j];       }     }     matrix inline operator*(const matrix &o) const {       matrix c(m, o.n);       for (int i = (1); i <= (m); ++i) {         for (int j = (1); j <= (o.n); ++j) {           c.a[i][j] = 0;           for (int k = (1); k <= (n); ++k)             c.a[i][j] = (c.a[i][j] + a[i][k] * o.a[k][j]) % 1000003;         }       }       return c;     }     void print() {       for (int i = (1); i <= (m); ++i) {         for (int j = (1); j <= (n); ++j) printf( %lld  , a[i][j]);         puts(  );       }       puts(  );     }   };   matrix matPow(matrix A, int n) {     if (n == 1) return A;     if (n % 2 == 1) return A * matPow(A, n - 1);     matrix B = matPow(A, n / 2);     return B * B;   }   int n, H, W;   int main() {     scanf( %d%d%d , &n, &W, &H);     n += 1;     matrix A(W + 1, W + 1);     for (int i = (1); i <= (W); ++i) A.a[i][i + 1] = 1;     A.a[W + 1][W + 1] = 1;     for (int j = (W); j >= (1); --j)       A.a[W + 1][j] = (A.a[W + 1][j + 1] * H) % 1000003;     matrix F(W + 1, 1);     F.a[1][1] = 1;     for (int i = (2); i <= (W + 1); ++i) {       int p = 1;       for (int j = (i - 1); j >= (1); --j) {         F.a[i][1] = (F.a[i][1] + F.a[j][1] * p) % 1000003;         p = (p * H) % 1000003;       }     }     if (n <= W)       cout << F.a[n + 1][1];     else       cout << (matPow(A, n - W) * F).a[W + 1][1];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int to, u, w;   } edge[200000];   struct data {     int x, y, w, d;   } v[200010];   int k = 1, f[200000], low[200010], dfu[200010], n, m, ans[200010], head[200010],       cnt, l;   bool vis[200010];   void add(int x, int y, int z) {     edge[k].u = y;     edge[k].to = head[x];     edge[k].w = z;     head[x] = k++;   }   int getfather(int x) {     if (f[x] == x) return x;     f[x] = getfather(f[x]);     return f[x];   }   void dfs(int now, int from) {     low[now] = dfu[now] = cnt++;     vis[now] = 1;     for (int i = head[now]; i != -1; i = edge[i].to) {       int u = edge[i].u;       if (vis[u] == 1 && edge[i].w != from)         low[now] = min(low[now], dfu[u]);       else if (vis[u] != 1) {         dfs(u, edge[i].w);         low[now] = min(low[now], low[u]);         if (dfu[now] < low[u]) ans[edge[i].w] = 2;       }     }   }   void add(int x, int y) {     int xx = getfather(x);     int yy = getfather(y);     if (xx != yy) {       dfu[x] = dfu[y] = dfu[xx] = dfu[yy] = 0;       low[x] = low[y] = low[xx] = low[yy] = 0;       vis[x] = vis[y] = vis[xx] = vis[yy] = 0;       head[x] = head[y] = head[xx] = head[yy] = -1;       f[xx] = yy;     }   }   bool comp(data x, data y) { return x.w < y.w; }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++) {       scanf( %d%d%d , &v[i].x, &v[i].y, &v[i].w);       v[i].d = i;     }     sort(v + 1, v + m + 1, comp);     memset(head, -1, sizeof(head));     for (int i = 1; i <= n; i++) f[i] = i;     l = 1;     for (int i = 1; i <= m; i++)       if (v[i].w != v[i + 1].w) {         for (int j = l; j <= i; j++) {           int xx = getfather(v[j].x);           int yy = getfather(v[j].y);           if (xx == yy) {             ans[v[j].d] = 1;           } else {             add(xx, yy, v[j].d);             add(yy, xx, v[j].d);           }         }         for (int j = l; j <= i; j++) {           int xx = getfather(v[j].x);           int yy = getfather(v[j].y);           if (xx != yy && vis[xx] == 0) dfs(xx, -1);           if (xx != yy && vis[yy] == 0) dfs(yy, -1);         }         for (int j = l; j <= i; j++) {           add(v[j].x, v[j].y);         }         l = i + 1;       }     for (int i = 1; i <= m; i++)       if (ans[i] == 1)         printf( %s n ,  none );       else if (ans[i] == 0)         printf( %s n ,  at least one );       else         printf( %s n ,  any );   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k, t = 0, a[30005];     cin >> n >> k;     for (int i = 1; i < n; i++) cin >> a[i];     for (t = 1; t < k; t += a[t])       ;     cout << (t == k ?  YES  :  NO );   }


#include <bits/stdc++.h>   using namespace std;   void Speed() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   struct xi {     long long a, b;   };   const int XS = 1e3 + 12;   const int second = 1e4 + 12;   const int M = 1e5 + 12;   const int L = 1e6 + 12;   const int XL = 1e7 + 12;   const long long INF1 = 1e9 + 7;   const long long INF2 = 1e12 + 7;   long long T, n;   void Tramp() {     cin >> n;     long long a[n + 12], b[n + 12];     for (int i = 1; i <= n; ++i) cin >> a[i];     for (int i = 1; i <= n; ++i) {       if (i & 1) {         b[i] = a[i + 1] * (-1);         b[i + 1] = a[i];       }     }     for (int i = 1; i <= n; ++i) cout << b[i] <<    ;     cout <<   n ;   }   int main() {     Speed();     cin >> T;     while (T--) Tramp();   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1000003ll;   long long npow[64];   long long fast_pow(long long n, long long e) {     if (e == 0) return 1ll;     long long tmp = fast_pow(n, e / 2ll);     tmp = (tmp * tmp) % MOD;     if (e & 1ll) return (tmp * n) % MOD;     return tmp;   }   int main() {     ios::sync_with_stdio(0);     npow[0] = 1ll;     for (int i = 1; i < 63; i++) npow[i] = (npow[i - 1] * 2ll);     long long n, k;     cin >> n >> k;     if (n < 63 && k > npow[n]) {       cout <<  1 1 n ;       return 0;     }     long long tgt = k - 1ll;     long long pcount = 0;     long long val = 2ll;     while (true) {       long long adder = tgt / val;       if (!adder) break;       val *= 2ll;       pcount += adder;     }     long long kmult = n - (pcount / (k - 1));     long long den = fast_pow(2, k - 1ll);     den = fast_pow(den, kmult - 1ll);     den = den * fast_pow(2ll, k - 1 - (pcount % (k - 1)));     den %= MOD;     long long num = 0;     if (k - 1ll < MOD) {       long long prod = fast_pow(2ll, pcount);       long long rem = fast_pow(2ll, n);       num = fast_pow(prod, MOD - 2ll);       for (int i = 1; i <= k - 1ll; i++) {         long long mult = rem - i;         while (mult < 0) mult += MOD;         num = (num * mult) % MOD;       }     }     num = (den - num);     while (num < 0) num += MOD;     cout << num <<     << den <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long su = 0, f = 1;     char c = getchar();     for (; c <  0  || c >  9 ; c = getchar())       if (c ==  - ) f = -1;     for (; c >=  0  && c <=  9 ; c = getchar()) su = su * 10 + c -  0 ;     return su * f;   }   inline void wr(long long x) {     if (x < 0)       putchar( - ), wr(-x);     else {       if (x >= 10) wr(x / 10);       putchar(x % 10 +  0 );     }   }   inline void wri(long long x) {     wr(x);     putchar(   );   }   inline void wrn(long long x) {     wr(x);     putchar(  n );   }   inline void wrn(long long x, long long y) {     wri(x);     wrn(y);   }   inline void wrn(long long x, long long y, long long z) {     wri(x);     wrn(y, z);   }   int n, m, ans[600055], k, a[600055], x, y, ss[600055];   vector<pair<long long, long long> > q[600055];   pair<long long, long long> z[600055];   char str[600055];   int Next[600055 * 2], head[600055], to[600055 * 2], nedge, g[600055][4];   void add(int a, int b) {     Next[++nedge] = head[a];     head[a] = nedge;     to[nedge] = b;   }   void add_ne(int a, int b) {     add(a, b);     add(b, a);   }   void ad(int x, int k) {     for (; x <= n; x += x & -x) ss[x] += k;   }   int que(int x) {     int su = 0;     for (; x; x -= x & -x) su += ss[x];     return su;   }   signed main() {     n = read();     for (int i = 1; i <= n; i++)       a[i] = read(), z[i].first = a[i], z[i].second = n - i + 1;     sort(z + 1, z + n + 1);     for (int i = 1; i <= n; i++) z[i].second = n - z[i].second + 1;     m = read();     for (int i = 1; i <= m; i++) {       x = read();       y = read();       q[x].push_back(make_pair(y, i));     }     for (int i = 1; i <= n; i++) {       ad(z[n - i + 1].second, 1);       for (int j = 0; j < int(q[i].size()); j++) {         int l = 1, r = n, t = q[i][j].first;         while (l < r) {           int mid = (l + r) >> 1;           if (que(mid) < t)             l = mid + 1;           else             r = mid;         }         ans[q[i][j].second] = l;       }     }     for (int i = 1; i <= m; i++) wrn(a[ans[i]]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;      int main() {        int t , x , k;        cin >> t;        while(t--)    {        cin >> x;                k = (int)(sqrt(8 * x + 1)) / 2;                          if((k * (k + 1) - 2 * x)!=2 && (k * (k + 1) - 2 * x) >= 0)          cout << k <<   n ;                  else          cout << k + 1 <<   n ;                     }             return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int LG = 18;   const int MX_N = 100005;   int n, q;   struct LowestCommonAncestor {     int clk, par[MX_N][LG];     int st[MX_N], h[MX_N];     vector<int> G[MX_N];     void addEdge(int u, int v) {       G[u].push_back(v);       G[v].push_back(u);     }     void dfs(int u = 1, int p = 0) {       st[u] = ++clk;       par[u][0] = p;       h[u] = h[p] + 1;       for (int i = 1; i < LG; i++) par[u][i] = par[par[u][i - 1]][i - 1];       for (auto& v : G[u])         if (v != p) dfs(v, u);     }     int LCA(int u, int v) {       if (h[u] < h[v]) swap(u, v);       for (int i = LG - 1; i >= 0; i--)         if (h[par[u][i]] >= h[v]) u = par[u][i];       if (u == v) return u;       for (int i = LG - 1; i >= 0; i--)         if (par[u][i] != par[v][i]) u = par[u][i], v = par[v][i];       return par[u][0];     }   };   struct SegTree {     pair<int, int> seg1[MX_N * 4], seg2[MX_N * 4];     void Merge(int nd, int lft, int rgt) {       seg1[nd] = min(seg1[lft], seg1[rgt]);       seg2[nd] = max(seg2[lft], seg2[rgt]);     }     void Bld(int* a, int nd = 1, int l = 1, int r = n) {       if (l == r) {         seg1[nd] = seg2[nd] = {a[l], l};         return;       }       int mid = l + (r - l) / 2, lft = nd << 1, rgt = lft | 1;       Bld(a, lft, l, mid);       Bld(a, rgt, mid + 1, r);       Merge(nd, lft, rgt);     }     pair<int, int> QryMN(int L, int R, int nd = 1, int l = 1, int r = n) {       if (l > R or r < L) return {INT_MAX, INT_MAX};       if (l >= L and r <= R) return seg1[nd];       int mid = l + (r - l) / 2, lft = nd << 1, rgt = lft | 1;       return min(QryMN(L, R, lft, l, mid), QryMN(L, R, rgt, mid + 1, r));     }     pair<int, int> QryMX(int L, int R, int nd = 1, int l = 1, int r = n) {       if (l > R or r < L) return {INT_MIN, INT_MIN};       if (l >= L and r <= R) return seg2[nd];       int mid = l + (r - l) / 2, lft = nd << 1, rgt = lft | 1;       return max(QryMX(L, R, lft, l, mid), QryMX(L, R, rgt, mid + 1, r));     }   };   SegTree sg;   LowestCommonAncestor olca;   void calc(int lca, int bd, int& res, int& bad) {     if (res < olca.h[lca]) res = olca.h[lca], bad = bd;   }   void solution(int l, int r, int& res, int& bad) {     pair<int, int> mnD = sg.QryMN(l, r);     pair<int, int> mxD = sg.QryMX(l, r);     int L = mnD.second;     int R = mxD.second;     pair<int, int> tmp;     if (L == l) {       tmp = sg.QryMN(l + 1, r);       calc(olca.LCA(tmp.second, R), L, res, bad);     } else if (L == r) {       tmp = sg.QryMN(l, r - 1);       calc(olca.LCA(tmp.second, R), L, res, bad);     } else {       tmp = min(sg.QryMN(l, L - 1), sg.QryMN(L + 1, r));       calc(olca.LCA(tmp.second, R), L, res, bad);     }     if (R == l) {       tmp = sg.QryMX(l + 1, r);       calc(olca.LCA(tmp.second, L), R, res, bad);     } else if (R == r) {       tmp = sg.QryMX(l, r - 1);       calc(olca.LCA(tmp.second, L), R, res, bad);     } else {       tmp = max(sg.QryMX(l, R - 1), sg.QryMX(R + 1, r));       calc(olca.LCA(tmp.second, L), R, res, bad);     }   }   int main() {     scanf( %d %d , &n, &q);     for (int i = 2, p; i <= n; i++) {       scanf( %d , &p);       olca.addEdge(p, i);     }     olca.dfs();     sg.Bld(olca.st);     while (q--) {       int l, r;       scanf( %d %d , &l, &r);       if (r - l == 1) {         int hh = max(olca.h[l], olca.h[r]);         int nn = (hh == olca.h[l]) ? r : l;         printf( %d %d n , nn, hh - 1);         continue;       }       int res = 0, bad = 0;       solution(l, r, res, bad);       printf( %d %d n , bad, res - 1);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int a[1007];   struct Node {     int pos;     int val;   };   Node res[1007];   int gcd(int a, int b) {     int m = std::max(a, b);     int n = std::min(a, b);     if (m % n == 0)       return n;     else       return gcd(n, m % n);   }   bool is_co_prim(int a, int b) { return gcd(a, b) == 1; }   int get(int a, int b) {     int m = std::min(a, b);     int n = std::max(a, b);     for (int i = m + 1;; i++) {       if (i != n && gcd(a, i) == 1 && gcd(b, i) == 1) {         return i;       }     }   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);     }     int cnt = 0;     for (int i = 0; i < n - 1; i++) {       if (is_co_prim(a[i], a[i + 1])) {         ;       } else {         res[cnt].pos = i + 1;         res[cnt].val = 1;         cnt++;       }     }     cout << cnt << endl;     int now = 0;     for (int i = 0; i < cnt; i++) {       while (now != res[i].pos) {         cout << a[now] <<    ;         now++;       }       cout << res[i].val <<    ;       now = res[i].pos;     }     while (now < n) {       cout << a[now] <<    ;       now++;     }   }


#include <bits/stdc++.h>   using namespace std;   int n, t;   bool ans = 0;   vector<int> port;   vector<bool> f;   void dfs(int i) {     if (i == t - 1) {       ans = 1;       return;     }     f[i] = 1;     if (i != n - 1 && !f[i + port[i]]) dfs(i + port[i]);   }   int main() {     cin >> n >> t;     port.resize(n - 1);     f.resize(n);     for (int i = 0; i < n - 1; ++i) {       cin >> port[i];     }     dfs(0);     cout << (ans ?  YES  :  NO );     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( omit-frame-pointer )   #pragma GCC optimize( unroll-loops )   const int MAXINT = 2147483640;   const long long MAXLL = 9223372036854775800LL;   const long long MAXN = 1000000;   const double pi = 3.1415926535897932384626433832795;   using namespace std;   int kol[300000];   int x1[300000];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     srand(time(0));     int n, mn = 1e9;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> x1[i];       mn = min(x1[i], mn);     }     for (int i = 1; i <= n; i++) {       x1[i] -= mn;       kol[x1[i]]++;     }     if (kol[2] == 0) {       cout << n << endl;       for (int i = 1; i <= n; i++) cout << x1[i] + mn <<    ;       cout << endl;       return 0;     }     int kol1 = kol[0];     int kol2 = kol[1];     int kol3 = kol[2];     int ans = n;     if (kol2 / 2 * 2 >= min(kol1, kol3) * 2) {       ans -= kol2 / 2 * 2;       kol1 += kol2 / 2;       kol3 += kol2 / 2;       kol2 -= kol2 / 2 * 2;     } else {       int kek = min(kol1, kol3);       ans -= kek * 2;       kol2 += kek * 2;       kol1 -= kek;       kol3 -= kek;     }     cout << ans << endl;     for (int i = 1; i <= kol1; i++) cout << mn <<    ;     for (int i = 1; i <= kol2; i++) cout << mn + 1 <<    ;     for (int i = 1; i <= kol3; i++) cout << mn + 2 <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base ::sync_with_stdio(false);     int n;     cin >> n;     int pass = 0, ans = 0;     while (n) {       ans += n;       ans += (pass * (n - 1));       pass++;       --n;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   #pragma warning(disable : 4996)   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     string a;     cin >> n >> a;     stack<char> q;     int m = 1;     int len = 0;     int he = -1;     for (int i = 0; i < n; i++) {       if (q.empty() || q.top() == a[i]) {         q.push(a[i]);         len++;         m += 2;       } else {         if (a[i] != a[i - 1]) len += 3;         if (a[i] ==  [  && a[i - 1] ==  ] ) len += 4;         q.pop();         len++;         m -= 2;       }       he = max(he, m);     }     vector<vector<char>> res(len, vector<char>(he,    ));     m = 0;     int cur = 0;     for (int i = 0; i < n; i++) {       if (i != 0 && a[i] == a[i - 1] && a[i] ==  [ )         m++;       else if (i != 0 && a[i] == a[i - 1] && a[i] ==  ] )         m--;       if (a[i] ==  [ ) {         res[cur][m] =  + ;         res[cur][he - m - 1] =  + ;         res[cur + 1][m] =  - ;         res[cur + 1][he - m - 1] =  - ;         for (int j = m + 1; j < he - m - 1; j++) res[cur][j] =  | ;       } else {         if (a[i] ==  ]  && a[i - 1] ==  [ ) cur += 3;         res[cur - 1][m] =  - ;         res[cur - 1][he - m - 1] =  - ;         res[cur][m] =  + ;         res[cur][he - m - 1] =  + ;         for (int j = m + 1; j < he - m - 1; j++) res[cur][j] =  | ;       }       cur++;     }     for (int i = 0; i < he; i++) {       for (int j = 0; j < len; j++) cout << res[j][i];       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2000 + 10;   const int MOD = (int)(1e9) + 7;   vector<int> adj[MAXN];   int a[MAXN], f[MAXN];   bool visited[MAXN];   int d, n;   void DFS(int u, int root) {     visited[u] = true;     f[u] = 1;     for (int i = 0; i < adj[u].size(); i++) {       int v = adj[u][i];       if (!visited[v]) {         if ((a[v] < a[root]) || (a[v] > a[root] + d)) continue;         if ((a[v] == a[root]) && (v < root)) continue;         DFS(v, root);         f[u] = ((long long)(f[u]) * (f[v] + 1)) % MOD;       }     }   }   int main() {     cin >> d >> n;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i <= n - 1; i++) {       int u, v;       cin >> u >> v;       adj[u].push_back(v);       adj[v].push_back(u);     }     int result = 0;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) f[j] = 0, visited[j] = false;       DFS(i, i);       result = (result + f[i]) % MOD;     }     cout << result << endl;   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-9;   const int INF = 1000000007;   const int MOD = 1000000007;   const int MAXN = 100055;   int dx[] = {2, 1, -1, -2, -1, 1, 2, 1, -1};   int dy[] = {0, 2, 2, 0, -2, -2, 0, 2, 2};   int main() {     long long n;     while (cin >> n) {       n++;       if (n == 1) {         cout <<  0 0  << endl;         continue;       }       long long L = (n - 1 + 2) / 3;       L = sqrt(L + 0.5);       while (3 * L * (L + 1) + 1 < n) ++L;       n -= 3 * L * (L - 1) + 1;       int side = (n - 1) / L;       n -= L * side;       long long ansx = dx[side] * L + dx[side + 2] * n;       long long ansy = dy[side] * L + dy[side + 2] * n;       cout << ansx <<     << ansy << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Node {     int s, e, d;   };   int a[1010];   int in[1010];   int out[1010];   int vis[1010];   int d[1010][1010];   int n, p;   int cnt = 0;   int ans;   Node node[1010];   void dfs(int x) {     if (!out[x]) {       node[cnt].e = x;       node[cnt].d = ans;       return;     } else {       ans = min(ans, d[x][a[x]]);       dfs(a[x]);     }   }   int cmp(Node a, Node b) { return a.s < b.s; }   int main() {     scanf( %d %d , &n, &p);     cnt = 0;     for (int i = 0; i < p; i++) {       int ta, tb, tc;       scanf( %d %d %d , &ta, &tb, &tc);       a[ta] = tb;       out[ta]++;       in[tb]++;       d[ta][tb] = tc;       vis[ta] = vis[tb] = 1;     }     for (int i = 1; i <= n && p != 0; i++) {       if (in[i] == 0 && vis[i]) {         ans = 1 << 30;         node[cnt].s = i;         dfs(i);         cnt++;       }     }     sort(node, node + cnt, cmp);     printf( %d n , cnt);     for (int i = 0; i < cnt; i++) {       printf( %d %d %d n , node[i].s, node[i].e, node[i].d);     }   }


#include <bits/stdc++.h>   template <class T>   bool setmax(T &_a, T _b) {     if (_b > _a) {       _a = _b;       return true;     }     return false;   }   template <class T>   bool setmin(T &_a, T _b) {     if (_b < _a) {       _a = _b;       return true;     }     return false;   }   const int N = 200010;   int a[N], ox[N], oy[N], ans[N];   int n, m;   int dfs(int k, int l1, int l2) {     setmin(l2, ox[k]);     int r1 = std::max(l1 + 1, oy[k]), r2 = l2 + 4;     if (l1 > l2 || r1 > r2) return -1;     int p = dfs(k + 1, r1 + 1, r2 + 1) - 1;     if (p > 0) {       int q = std::max(l1, p - 4);       for (int i = int(q); i <= int(p); ++i) ans[i] = k;       return q;     }     return (l1 <= n + 1 && l2 >= n + 1 && k > m ? n + 1 : -1);   }   bool solve() {     std::fill(ox, ox + N, n + 1);     std::fill(oy, oy + N, -1);     m = 0;     for (int i = int(1); i <= int(n); ++i)       if (a[i] > 0) {         setmin(ox[a[i]], i);         setmax(oy[a[i]], i);         setmax(m, a[i]);       }     return dfs(1, 1, 1) > 0;   }   int main() {     scanf( %d , &n);     for (int i = int(1); i <= int(n); ++i) scanf( %d , &a[i]);     if (solve()) {       printf( %d n , ans[n]);       for (int i = int(1); i <= int(n); ++i) printf( %d  , ans[i]);       printf(  n );     } else {       printf( -1 n );     }     return 0;   }


#include <bits/stdc++.h>   const int MAXN = 2e5 + 5;   using namespace std;   struct info {     int coord;     bool taxi;   } v[MAXN];   int n, m, dist[MAXN], man[MAXN], fr[MAXN];   int main() {     cin >> n >> m;     for (int i = 1; i <= n + m; i++) {       cin >> v[i].coord;       dist[i] = 2e9;     }     for (int i = 1; i <= n + m; i++) cin >> v[i].taxi;     int cur = -1;     for (int i = 1; i <= n + m; i++) {       if (v[i].taxi) cur = i;       if (!v[i].taxi && cur != -1) {         dist[i] = v[i].coord - v[cur].coord;         man[i] = cur;       }     }     cur = -1;     for (int i = n + m; i >= 1; i--) {       if (v[i].taxi) cur = i;       if (!v[i].taxi && cur != -1) {         if (v[cur].coord - v[i].coord < dist[i]) {           dist[i] = v[cur].coord - v[i].coord;           man[i] = cur;         }       }     }     for (int i = 1; i <= n + m; i++)       if (!v[i].taxi) fr[man[i]]++;     for (int i = 1; i <= n + m; i++) {       if (v[i].taxi) cout << fr[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   const int MAX = 1000010;   const long long MOD = 1e9 + 7;   const int INF = 0x3f3f3f3f;   const long long LLINF = 0x3f3f3f3f3f3f3f3f;   const long double EPS = 1e-6;   const long double PI = acos(-1.0);   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, x, y;     cin >> n;     cout <<  YES n ;     for (int i = 0; i < n; i++) {       cin >> x >> y;       cin >> x >> y;       if (x & 1) {         if (y & 1)           cout << 1 <<   n ;         else           cout << 2 <<   n ;       } else {         if (y & 1)           cout << 3 <<   n ;         else           cout << 4 <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int rd() {     register int ans = 0, flag = 1;     register char c = getchar();     for (; c >  9  || c <  0 ; c = getchar())       if (c ==  - ) flag = -1;     for (; c >=  0  && c <=  9 ; c = getchar()) ans = ans * 10 -  0  + c;     return ans * flag;   }   const int MAXN = 1e5 + 10;   int a[MAXN];   int ans[MAXN];   int tmp[MAXN];   int _[20][MAXN];   void dfs(int now, int n) {     if (n < 2 && _[now][1] == 0) {       if (tmp[0] < ans[0]) {         ans[0] = tmp[0];         for (register int i = 1; i <= tmp[0]; ++i) {           ans[i] = tmp[i];         }       }       return;     }     if (now > 18 || tmp[0] >= ans[0]) {       return;     }     for (register int I = 1; I <= n; ++I) {       if (_[now][I] & 1) {         for (register int i = 1; i <= n; ++i) {           _[now + 1][i] = (_[now][i] - (_[now][i] & 1)) >> 1;         }         tmp[++tmp[0]] = (1 << now);         register int nn =             unique(_[now + 1] + 1, _[now + 1] + n + 1) - _[now + 1] - 1;         dfs(now + 1, nn);         --tmp[0];         for (register int i = 1; i <= n; ++i) {           _[now + 1][i] = (_[now][i] + (_[now][i] & 1)) >> 1;         }         tmp[++tmp[0]] = -(1 << now);         nn = unique(_[now + 1] + 1, _[now + 1] + n + 1) - _[now + 1] - 1;         dfs(now + 1, nn);         --tmp[0];         return;       }     }     for (register int i = 1; i <= n; ++i) {       _[now + 1][i] = _[now][i] >> 1;     }     n = unique(_[now + 1] + 1, _[now + 1] + n + 1) - _[now + 1] - 1;     dfs(now + 1, n);   }   int main() {     register int n = rd();     for (register int i = 1; i <= n; ++i) {       a[i] = rd();     }     sort(a + 1, a + n + 1);     n = unique(a + 1, a + n + 1) - a - 1;     ans[0] = 0x3f3f3f3f;     for (register int i = 1; i <= n; ++i) {       _[0][i] = a[i];     }     dfs(0, n);     printf( %d n , ans[0]);     for (register int i = 1; i <= ans[0]; ++i) {       printf( %d  , ans[i]);     }     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double PI = 3.1415926535897932384626433832795;   const long double EPS = 1e-11;   int n, m, first, second;   bool a[110][110];   pair<long double, int> find_k(int s, int e) {     queue<int> q;     int ur[110];     long double kr[110];     memset(ur, 0xFF, sizeof(ur));     memset(kr, 0, sizeof(kr));     q.push(s);     ur[s] = 1;     kr[s] = 1.0;     int p;     while (!q.empty()) {       p = q.front();       q.pop();       for (int i = 0; i < (n); i++)         if ((a[p][i]) && (ur[i] + 1 == ur[p])) kr[p] += kr[i];       for (int i = 0; i < (n); i++)         if ((a[p][i]) && (ur[i] == -1)) {           ur[i] = ur[p] + 1;           q.push(i);         }     }     return make_pair(kr[e], ur[e] - 1);   }   int main() {     cout << setiosflags(ios::fixed) << setprecision(10);     cin >> n >> m;     for (int i = 0; i < (m); i++) {       cin >> first >> second;       first--;       second--;       a[first][second] = 1;       a[second][first] = 1;     }     pair<long double, int> k = find_k(0, n - 1);     if (k.first == 0) cout <<  LOLO ;     long double ans = 1.0;     for (int i = 1; i < n - 1; i++) {       pair<long double, int> k1 = find_k(0, i), k2 = find_k(i, n - 1);       if (k1.second + k2.second == k.second)         ans = max(ans, 2.0 * k1.first * k2.first / k.first);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long d[40000];   char s[4005];   void input() {     int i, j, len, a, sum;     long long res;     scanf( %d %s , &a, &s);     len = strlen(s);     for ((i) = 0; (i) < (len); (i)++) {       sum = 0;       for ((j) = (i); (j) <= (len - 1); (j)++) {         sum += s[j] -  0 ;         d[sum]++;       }     }     if (a == 0) {       res = d[0] * len * (len + 1) - d[0] * d[0];       printf( %I64d n , res);       return;     }     res = 0;     for ((i) = (9 * len); (i) >= (1); (i)--)       if (a % i == 0 && a / i <= 36000) res += d[i] * d[a / i];     printf( %I64d , res);   }   int main() {     input();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a[1000005], m, M, N, sum;   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; ++i) scanf( %d , a + i), M = max(a[i], M);     for (int i = 1; i <= n; ++i) sum += M - a[i];     if (m <= sum)       printf( %d %d n , M, M + m);     else       printf( %d %d n , (m - sum + n - 1) / n + M, M + m);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1.0);   const long long INF = 1000 * 1000 * 1000 + 7;   const long long LINF = INF * (long long)INF;   const int MAX = 100 + 47;   int A[MAX];   vector<int> B;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     for (int i = (0); i < (n); i++) cin >> A[i];     int cnt = 0;     int vid = 0;     for (int i = (0); i < (n + 1); i++) {       if ((A[i] < 0 && vid == 2) || i == n) {         B.push_back(cnt);         cnt = 1;         vid = 1;         continue;       }       cnt++;       if (A[i] < 0) vid++;     }     cout << (int)B.size() << endl;     for (int i = (0); i < ((int)B.size()); i++) {       cout << B[i] <<    ;     }     cout << endl;   }


#include <bits/stdc++.h>   int main() {     std::ios_base::sync_with_stdio(false);     std::cin.tie(nullptr);     std::cout.tie(nullptr);     int tc;     std::cin >> tc;     while (tc--) {       long long x, y;       std::cin >> x >> y;       if ((x == 1 || x == 3) && y > x) {         std::cout <<  NO n ;       } else if (x == 2 && y > 3) {         std::cout <<  NO n ;       } else {         std::cout <<  YES n ;       }     }     return 0;   }


#include <bits/stdc++.h>   int main(void) {     int n;     int k;     int M;     scanf( %d%d%d , &n, &k, &M);     int number[45][45] = {0};     for (int i = 0; i < k; i++) {       int temp;       scanf( %d , &temp);       for (int j = 0; j < n; j++) {         number[j][i] = temp;       }     }     for (int i = 0; i < n; i++) {       std::sort(number[i], number[i] + k);     }     int ptrAddR = 0;     int ptrAddL = 0;     int ptrDelR = 0;     int ptrDelL = -1;     int lockR = 0;     int time = M;     int score = 0;     for (int i = 0; i < k; i++) {       for (int j = 0; j < n; j++) {         if (time >= number[j][i]) {           time -= number[j][i];           ptrDelL = j;           ptrDelR = i;         } else {           goto FAST_JUMP;         }       }     }   FAST_JUMP:     if (ptrDelL == n) {       score = (ptrDelR - 1 + 1) * n;     } else {       score = (ptrDelR - 1 + 1) * n + ptrDelL + 1;     }     if (ptrDelR == k - 1) {       lockR = ptrDelL + 1;       ptrAddL = lockR;       ptrAddR = k - 1;       score += lockR;     } else {       ptrAddR = ptrDelR + 1;       ptrAddL = 0;     }     if (ptrDelL <= lockR) {       ptrDelL = n - 1;       ptrDelR--;     }     int warning = 0;     int tptrAddR = ptrAddR;     int tptrAddL = ptrAddL;     int tptrDelR = ptrDelR;     int tptrDelL = ptrDelL;     int tlockR = lockR;     int flag = 0;     while (!(lockR >= n - 1 || ptrDelR < 0 || warning >= 2)) {       if (tptrDelL <= tlockR) {         tptrDelL = n - 1;         tptrDelR--;       }       time += number[tptrDelL][tptrDelR];       tptrDelL--;       if (tptrDelL <= tlockR) {         tptrDelL = n - 1;         tptrDelR--;       }       flag = -1;       if (tptrAddR > k - 1) {         tptrAddR = tptrDelR + 1;         tptrAddL++;         tlockR++;       }       if (tptrAddR > k - 1 || tptrAddL > n - 1) {         break;       }       if (time >= number[tptrAddL][tptrAddR]) {         time -= number[tptrAddL][tptrAddR];         flag = 0;         tptrAddR++;         if (tptrAddR > k - 1) {           tptrAddR = tptrDelR + 1;           tptrAddL++;           tlockR++;           flag = 1;         }       } else {       }       warning -= flag;       if (warning <= 0) {         score -= warning;         warning = 0;         ptrAddL = tptrAddL;         ptrAddR = tptrAddR;         ptrDelL = tptrDelL;         ptrDelR = tptrDelR;         lockR = tlockR;       }     }     printf( %d , score);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n, m;       cin >> n >> m;       if (n % m == 0)         cout <<  YES  << endl;       else         cout <<  NO  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     if (n % 2 == 0) {       cout <<  Mahmoud n ;     } else {       cout <<  Ehab n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n, m;     cin >> n >> m;     vector<bool> hero(n);     vector<long long> v(n);     vector<int> pos_num(n);     for (int i = 0; i < m; i++) {       int s, h;       cin >> s >> h;       hero[--s] = 1;       pos_num[s] = i;       v[s] = h;     }     for (int i = 0; i < n; i++) {       int x;       cin >> x;       if (!hero[i]) v[i] = x;     }     for (int cell = 0; cell < n; cell++) {       vector<int> order;       vector<long long> v_;       vector<bool> hero_;       for (bool x : hero) hero_.push_back(x);       for (long long x : v) v_.push_back(x);       for (int dum = 0; dum < m; dum++) {         vector<bool> can(n);         for (int i = 0; i < n; i++)           if (hero_[i]) {             int inc = i <= cell ? 1 : -1;             long long sum = 0;             int idx = i;             bool ok = 1;             while (1) {               if (idx == i || !hero_[idx]) sum += v_[idx];               if (sum < 0) ok = 0;               if (idx == cell) break;               idx += inc;             }             if (ok) can[i] = 1;           }         int idx = -1;         for (int i = 0; i < cell + 1; i++) {           if (can[i]) {             idx = i;             break;           }         }         for (int i = n - 1; i >= cell; i--)           if (can[i]) {             idx = i;             break;           }         if (idx == -1) break;         int inc = idx <= cell ? 1 : -1;         order.push_back(pos_num[idx]);         hero_[idx] = 0;         while (1) {           v_[idx] = 0;           if (idx == cell) break;           idx += inc;         }       }       if (order.size() == m) {         cout << cell + 1 <<   n ;         for (int i = 0; i < m; i++) {           if (i) cout <<    ;           cout << order[i] + 1;         }         cout <<   n ;         return 0;       }     }     cout << -1 <<   n ;   }