#include <bits/stdc++.h>   using namespace std;   bool found = false;   long long b, A[10000], n, orig_a;   void bfs(long long a, long long k) {     if (found) return;     if (a > b) return;     if (a == b) {       found = true;       n = k;       a = orig_a;       cout <<  YES  << endl;       cout << n + 1 << endl;       cout << a;       for (int i = 0; i < n; i++) {         if (A[i] == 1) {           a = a * 10 + 1;           cout <<     << a;         } else if (A[i] == 2) {           a = a * 2;           cout <<     << a;         }       }       cout << endl;       return;     }     A[k] = 2;     bfs(a * 2, k + 1);     A[k] = 1;     bfs((a * 10) + 1, k + 1);   }   int main() {     long long a, c, d, i, j, m;     string s;     cin >> a >> b;     orig_a = a;     bfs(a, 0);     if (!found) cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   class Compare {    public:     bool operator()(pair<long long, long long> a, pair<long long, long long> b) {       return (a.first + a.second) > (b.first + b.second);     }   };   int main() {     long long test, x, n, m, k, w, d, f, y;     priority_queue<pair<long long, long long>,                    vector<pair<long long, long long> >, Compare>         pqw;     priority_queue<pair<long long, long long>,                    vector<pair<long long, long long> >, Compare>         pqd;     priority_queue<pair<long long, long long>,                    vector<pair<long long, long long> >, Compare>         pqf;     scanf( %lld%lld%lld%lld%lld%lld%lld , &x, &n, &m, &k, &w, &d, &f);     for (long long i = 0; i < n; i++) {       pqw.push(make_pair(0, w));     }     for (long long i = 0; i < m; i++) {       pqd.push(make_pair(0, d));     }     for (long long i = 0; i < k; i++) {       pqf.push(make_pair(0, f));     }     long long ans = 0;     for (long long i = 0; i < x; i++) {       pair<long long, long long> cur = pqw.top();       pqw.pop();       pqw.push(make_pair(cur.first + cur.second, cur.second));       pair<long long, long long> curd = pqd.top();       pqd.pop();       curd.first = max(cur.first + cur.second, curd.first) + d;       pqd.push(curd);       pair<long long, long long> curf = pqf.top();       pqf.pop();       curf.first = max(curd.first, curf.first) + f;       pqf.push(curf);       ans = curf.first;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int P = 1e9 + 7;   vector<int> b[200005];   int L[200005], R[200005], u[200005], v[200005], opt[200005], size[200005];   int m, n, ql, qr, upd, tot, now, ans, i, x;   struct data {     int cnt, sum;     data() { cnt = 1; }     void multi(int x) {       cnt = 1ll * cnt * x % P;       sum = 1ll * sum * x % P;     }     int renew(int x) {       cnt = 1;       sum = x;       return 0;     }   } a[200005 << 2];   void DFS(int x, int Fa) {     L[x] = ++tot;     for (int i = 0, y; i < b[x].size(); i++)       if ((y = b[x][i]) != Fa) DFS(y, x);     R[x] = tot;   }   int INV(int a) {     int ret = 1, b = P - 2;     for (; b; b >>= 1, a = 1ll * a * a % P)       if (b & 1) ret = 1ll * ret * a % P;     return ret;   }   inline void down(int x) {     if (a[x].cnt == 1) return;     a[x << 1].multi(a[x].cnt);     a[x << 1 | 1].multi(a[x].cnt);     a[x].cnt = 1;   }   inline void up(int x) { a[x].sum = (a[x << 1].sum + a[x << 1 | 1].sum) % P; }   void Multi(int x, int l, int r) {     if (ql <= l && r <= qr) {       a[x].multi(now);       return;     }     int mid = (l + r) >> 1;     down(x);     if (ql <= mid) Multi(x << 1, l, mid);     if (qr > mid) Multi(x << 1 | 1, mid + 1, r);     up(x);   }   int Update(int x, int l, int r, int pos) {     if (l == r) return (!upd) ? a[x].sum : a[x].renew(now);     int mid = (l + r) >> 1;     down(x);     int ret = (pos <= mid) ? Update(x << 1, l, mid, pos)                            : Update(x << 1 | 1, mid + 1, r, pos);     return up(x), ret;   }   void Query(int x, int l, int r) {     if (ql <= l && r <= qr) {       ans = (ans + a[x].sum) % P;       return;     }     int mid = (l + r) >> 1;     down(x);     if (ql <= mid) Query(x << 1, l, mid);     if (qr > mid) Query(x << 1 | 1, mid + 1, r);   }   int main() {     scanf( %d%d , &v[1], &m);     n = 1;     for (i = 1; i <= m; i++) {       scanf( %d%d , &opt[i], &u[i]);       if (opt[i] == 1) scanf( %d , &v[++n]), b[u[i]].push_back(n);     }     DFS(1, 0);     size[n = 1] = 1;     upd = 1;     now = v[1];     Update(1, 1, tot, L[1]);     for (i = 1; i <= m; i++)       if (opt[i] == 1) {         x = u[i];         size[++n] = 1;         ql = L[x];         qr = R[x];         now = 1ll * (size[x] + 1) * INV(size[x]) % P;         ++size[x];         Multi(1, 1, tot);         upd = 0;         now = Update(1, 1, tot, L[x]);         now = 1ll * now * INV(v[x]) % P;         now = 1ll * now * v[n] % P;         upd = 1;         Update(1, 1, tot, L[n]);       } else {         x = u[i];         ans = 0;         ql = L[x];         qr = R[x];         Query(1, 1, tot);         upd = 0;         now = Update(1, 1, tot, L[x]);         now = 1ll * now * INV(1ll * v[x] * size[x] % P) % P;         ans = 1ll * ans * INV(now) % P;         printf( %d n , ans);       }   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   const long double PI = 3.141592653589793;   const long long maxn = 100005;   long long x, y, z, n, k;   void _print(long long t) { cerr << t; }   void _print(string t) { cerr << t; }   void _print(char t) { cerr << t; }   void _print(long double t) { cerr << t; }   void _print(double t) { cerr << t; }   template <class T, class V>   void _print(pair<T, V> p);   template <class T>   void _print(vector<T> v);   template <class T>   void _print(set<T> v);   template <class T, class V>   void _print(map<T, V> v);   template <class T>   void _print(multiset<T> v);   template <class T, class V>   void _print(pair<T, V> p) {     cerr <<  { ;     _print(p.first);     cerr <<  , ;     _print(p.second);     cerr <<  } ;   }   template <class T>   void _print(vector<T> v) {     cerr <<  [  ;     for (T i : v) {       _print(i);       cerr <<    ;     }     cerr <<  ] ;   }   template <class T>   void _print(set<T> v) {     cerr <<  [  ;     for (T i : v) {       _print(i);       cerr <<    ;     }     cerr <<  ] ;   }   template <class T>   void _print(multiset<T> v) {     cerr <<  [  ;     for (T i : v) {       _print(i);       cerr <<    ;     }     cerr <<  ] ;   }   template <class T, class V>   void _print(map<T, V> v) {     cerr <<  [  ;     for (auto i : v) {       _print(i);       cerr <<    ;     }     cerr <<  ] ;   }   void solve() {     string s, t;     cin >> s >> t;     long long f[26];     memset(f, 0, sizeof(f));     long long n = (long long)s.size(), m = (long long)t.size();     vector<vector<long long>> v(26);     for (long long i = 0; i <= n - 1; i++) {       f[s[i] -  a ]++;       v[s[i] -  a ].push_back(i);     }     for (long long i = 0; i <= m - 1; i++) {       if (f[t[i] -  a ] == 0) {         cout << -1 <<   n ;         return;       }     }     long long ans = 1, last = -1;     for (long long i = 0; i <= m - 1; i++) {       long long prev = last;       long long val = t[i] -  a ;       long long pos =           lower_bound(v[val].begin(), v[val].end(), last + 1) - v[val].begin();       if (pos == v[val].size()) {         ans++;         last = v[val][0];       } else {         last = v[val][pos];       }     }     cout << ans <<   n ;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cout << setprecision(15) << fixed;     long long t;     t = 1;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   const double eps = 1e-8;   const long long INF = 0x3f3f3f3f;   const int mod = 20000311;   const int maxn = 1e5 + 5;   const int N = 1005;   using namespace std;   int a[maxn] = {0};   int dp[maxn][305] = {0};   int n;   int dfs(int p, int k) {     if (p > n) return 0;     if (dp[p][k] != -1) return dp[p][k];     int num = p + a[p] + k;     return dp[p][k] = dfs(num, k) + 1;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int j = 1; j <= 300; j++) {       for (int i = n; i >= 1; i--) {         int num = i + a[i] + j;         if (num > n)           dp[i][j] = 1;         else           dp[i][j] = dp[num][j] + 1;       }     }     int q;     scanf( %d , &q);     while (q--) {       int p, k;       scanf( %d %d , &p, &k);       if (k >= 300) {         int res = 0;         while (p <= n) {           p = a[p] + k + p;           res++;         }         cout << res <<   n ;       } else         cout << dp[p][k] <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string Max(int n, int s) {     string str =   ;     while (s >= 9 && n > 0) {       str +=  9 ;       s -= 9;       n--;     }     if (n == 0 && s > 0) return  -1 ;     if (n > 0) {       str += char(s + 48);       for (int i = 1; i < n; i++) str +=  0 ;     }     return str;   }   int main() {     int n, s;     string str =   ;     cin >> n >> s;     if (n * 9 < s)       cout <<  -1 -1 ;     else if (n > 1 and s == 0)       cout <<  -1 -1 ;     else if (s == 1) {       cout << 1;       for (int i = 1; i < n; i++) cout << 0;       cout <<   1 ;       for (int i = 1; i < n; i++) cout << 0;     } else {       for (int i = Max(n, s).size() - 1; i >= 0; i--) str += Max(n, s)[i];       if (str[0] ==  0  and str.size() > 1) {         str[0] =  1 ;         for (int i = 1; i < str.size(); i++)           if (str[i] >  0 ) {             str[i] = char(int(str[i]) - 1);             break;           }       }       cout << str <<     << Max(n, s);     }   }


#include <bits/stdc++.h>   using namespace std;   const int M = 1e9 + 7;   const int MAX_N = 1002;   int dp_end[MAX_N][MAX_N];   int dp_not[MAX_N][MAX_N];   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       dp_end[0][i] = (i + 1) / 2;       dp_not[0][i] = i / 2;     }     for (int i = 1; i <= n; i++) {       for (int j = i; j <= n; j++) {         dp_end[i][j] = 1 + dp_not[i - 1][j] + dp_end[i][j - 1];         if (i - 1 >= j) {           dp_end[i][j] =               max(dp_end[i][j], 1 + dp_not[i][j - 1] + dp_end[i - 1][j]);         }         dp_end[i][j] %= M;         dp_not[i][j] = dp_end[i][j - 1] + dp_end[i - 1][j];         dp_not[i][j] %= M;       }     }     cout << dp_end[n][n] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void c_p_c() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   int32_t main() {     c_p_c();     long long n;     string s[222222];     map<string, bool> check;     cin >> n;     for (long long i = 1; i <= n; i++) {       cin >> ws;       getline(cin, s[i]);     }     for (long long i = n; i >= 1; i--) {       if (!check[s[i]]) {         check[s[i]] = 1;         cout << s[i] <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using std::max;   using std::min;   const int inf = 0x3f3f3f3f, Inf = 0x7fffffff;   const long long INF = 0x3f3f3f3f3f3f3f3f;   __inline__ __attribute__((always_inline)) unsigned int rnd() {     static unsigned int seed = 416;     return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13;   }   template <typename _Tp>   _Tp gcd(const _Tp &a, const _Tp &b) {     return (!b) ? a : gcd(b, a % b);   }   template <typename _Tp>   __inline__ __attribute__((always_inline)) _Tp abs(const _Tp &a) {     return a >= 0 ? a : -a;   }   template <typename _Tp>   __inline__ __attribute__((always_inline)) void chmax(_Tp &a, const _Tp &b) {     (a < b) && (a = b);   }   template <typename _Tp>   __inline__ __attribute__((always_inline)) void chmin(_Tp &a, const _Tp &b) {     (b < a) && (a = b);   }   template <typename _Tp>   __inline__ __attribute__((always_inline)) void read(_Tp &x) {     char ch(getchar());     bool f(false);     while (!isdigit(ch)) f |= ch == 45, ch = getchar();     x = ch & 15, ch = getchar();     while (isdigit(ch)) x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar();     f && (x = -x);   }   template <typename _Tp, typename... Args>   __inline__ __attribute__((always_inline)) void read(_Tp &t, Args &...args) {     read(t);     read(args...);   }   template <typename _Tp, typename... Args>   __inline__ __attribute__((always_inline)) _Tp min(const _Tp &a, const _Tp &b,                                                     const Args &...args) {     return a < b ? min(a, args...) : min(b, args...);   }   template <typename _Tp, typename... Args>   __inline__ __attribute__((always_inline)) _Tp max(const _Tp &a, const _Tp &b,                                                     const Args &...args) {     return a < b ? max(b, args...) : max(a, args...);   }   __inline__ __attribute__((always_inline)) int read_str(char *s) {     char ch(getchar());     while (ch ==     || ch ==   r  || ch ==   n ) ch = getchar();     char *tar = s;     *tar = ch, ch = getchar();     while (ch !=     && ch !=   r  && ch !=   n  && ch != EOF)       *(++tar) = ch, ch = getchar();     return tar - s + 1;   }   const int N = 100005;   const int MAXN = 10000005;   bool pr[MAXN];   int _p[MAXN >> 3], _pos;   int md[MAXN];   void sieve() {     for (int i = 2; i < MAXN; ++i) {       if (!pr[i]) {         _p[++_pos] = i;         md[i] = i;       }       for (int j = 1; j <= _pos && i * _p[j] < MAXN; ++j) {         pr[i * _p[j]] = true;         md[i * _p[j]] = _p[j];         if (!(i % _p[j])) break;       }     }   }   std::vector<int> p[N], d[N];   int c[MAXN], cnt[1 << 8], mul[1 << 8];   int calc(int id) {     int siz = ((int)p[id].size()), ans = 0;     for (int i = 0; i < siz; ++i) mul[1 << i] = p[id][i];     for (int i = 1; i < 1 << siz; ++i) {       cnt[i] = cnt[i >> 1] + (i & 1);       mul[i] = mul[i ^ ((i) & (-(i)))] * mul[((i) & (-(i)))];       if (cnt[i] & 1)         ans += c[mul[i]];       else         ans -= c[mul[i]];     }     return ans;   }   int a[N];   int _val[11], _cnt[11], pos, cur_id;   void dfs(int dep, int val) {     if (dep == pos + 1) {       d[cur_id].push_back(val);       return;     }     for (int i = 0; i <= _cnt[dep]; ++i) {       dfs(dep + 1, val);       if (i < _cnt[dep]) val *= _val[dep];     }   }   bool ban[N], vis[N];   int deg[N];   int f(int mid) {     while (ban[mid]) --mid;     if (!mid) return false;     int qwq = 0;     for (int i = 1; i <= mid; ++i)       if (!ban[i]) {         ++qwq;         for (auto it : d[i]) ++c[it];       }     int ans = 0;     for (int i = 1; i <= mid; ++i)       if (!ban[i] && calc(i) != qwq) ++ans;     for (int i = 1; i <= mid; ++i)       if (!ban[i])         for (auto it : d[i]) --c[it];     return ans;   }   std::vector<int> getnodes(int mid) {     while (ban[mid]) --mid;     std::vector<int> nd;     int qwq = 0;     for (int i = 1; i <= mid; ++i)       if (!ban[i]) {         ++qwq;         for (auto it : d[i]) ++c[it];       }     for (int i = 1; i <= mid; ++i)       if (!ban[i] && calc(i) != qwq) nd.push_back(i);     for (int i = 1; i <= mid; ++i)       if (!ban[i])         for (auto it : d[i]) --c[it];     return nd;   }   int main() {     mul[0] = 1;     sieve();     int n, k;     read(n, k);     for (int i = 1; i <= n; ++i) {       read(a[i]);       ++c[a[i]];       int cur = a[i], last = 0;       pos = 0;       while (cur > 1) {         if (md[cur] != last)           p[i].push_back(md[cur]), _val[++pos] = md[cur], _cnt[pos] = 1;         else           ++_cnt[pos];         last = md[cur];         cur /= last;       }       cur_id = i;       dfs(1, 1);     }     for (int j = 1; j <= _pos; ++j)       for (int i = (MAXN - 1) / _p[j]; i >= 1; --i) c[i] += c[i * _p[j]];     std::vector<int> nd;     for (int i = 1; i <= n; ++i) {       if ((deg[i] = calc(i)) <= n - 2) {         nd.push_back(i);         for (int j = 1; j <= n && ((int)nd.size()) < 3; ++j)           if (i != j && gcd(a[i], a[j]) == 1) nd.push_back(j);         break;       }     }     if (nd.empty()) {       std::vector<int> clique, rest;       for (int i = 1; i <= n; ++i)         if (deg[i] == n)           clique.push_back(i);         else           rest.push_back(i);       if (((int)clique.size()) > k) clique.resize(k);       for (int i = 1; i <= n && ((int)clique.size()) < k; ++i) {         if (!vis[i] && deg[i] == n - 1) {           clique.push_back(i);           for (auto it : rest)             if (gcd(a[it], a[i]) == 1) vis[it] = true;         }       }       for (auto it : clique) printf( %d  , it);       printf(  n );       return 0;     }     if (k == 3) {       for (auto it : nd) printf( %d  , it);       printf(  n );       return 0;     }     for (auto it : nd) ban[it] = true;     for (auto id : nd)       for (auto it : d[id]) --c[it];     std::vector<int> clique;     for (int i = 1; i <= n; ++i)       if (!ban[i] && ((deg[i] = calc(i)) == n - 3)) clique.push_back(i);     if (((int)clique.size()) >= k) {       clique.resize(k);       for (auto it : clique) printf( %d  , it);       printf(  n );       return 0;     }     memset(c, 0, sizeof(c));     int l = 1, r = n;     while (l < r) {       int mid = (l + r) >> 1;       if (f(mid) + 3 >= k)         r = mid;       else         l = mid + 1;     }     std::vector<int> v1 = getnodes(l - 1);     std::vector<int> v2 = getnodes(l);     std::sort(v1.begin(), v1.end());     std::vector<int> qwq;     for (auto it : v2) {       auto _ = std::lower_bound(v1.begin(), v1.end(), it);       if (_ == v1.end() || *_ != it) qwq.push_back(it);     }     std::sort(qwq.begin(), qwq.end());     if (((int)v1.size()) + 3 + 1 == k && ((int)qwq.size()) != 1)       v1.push_back(nd[0]), v1.push_back(nd[1]);     else       v1.push_back(nd[0]), v1.push_back(nd[1]), v1.push_back(nd[2]);     while (((int)v1.size()) < k) {       v1.push_back(qwq.back());       qwq.pop_back();     }     for (auto it : v1) printf( %d  , it);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void fact(int n) {     const int N = 1e5;     vector<int> arr(N, 0);     int q = 2;     arr[0] = 1;     int len = 1;     int x = 0;     int carry = 0;     while (q <= n) {       x = 0;       carry = 0;       while (x < len) {         arr[x] = arr[x] * q + carry;         carry = arr[x] / 10;         arr[x] = arr[x] % 10;         x++;       }       while (carry) {         arr[len] = carry % 10;         carry = carry / 10;         len++;       }       q++;     }     len--;     for (int i = len; i >= 0; i--) {       cout << arr[i];     }     return;   }   long long int binary_exp(long long int x, long long int n) {     if (n == 0) {       return 1;     } else if (n == 1) {       return x % 1000000007;     } else {       long long int temp = binary_exp(x, n / 2);       temp = (temp * temp) % 1000000007;       if (n % 2 == 0) {         return temp;       } else {         return ((x % 1000000007) * temp) % 1000000007;       }     }   }   int main() {     int k, r;     cin >> k >> r;     int i = 1;     while (i * k % 10 != r and i * k % 10 != 0) {       i++;     }     cout << i;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool vis[100006];   int main() {     int n, k;     while (cin >> n >> k) {       memset(vis, false, sizeof(vis));       int st = 1, ed = 1 + k;       int cnt = 0;       while (cnt < k && st <= ed) {         if (st != ed)           vis[st] = vis[ed] = true, cout << st <<     << ed <<    ;         else           vis[st] = true, cout << st <<    ;         st++, ed--;         cnt++;       }       for (int i = 1; i <= n; i++) {         if (!vis[i]) cout << i <<    ;       }       puts(  );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 500023;   bool vis[N];   vector<int> adj[N];   void solve() {     long long int n, m;     cin >> n >> m;     map<long long int, long long int> cntA;     map<long long int, long long int> cntB;     map<long long int, long long int> cntC;     map<long long int, long long int> cntD;     map<long long int, long long int> cntE;     for (long long int i = 0; i < n; i++) {       string s;       cin >> s;       for (int i = 0; i < s.length(); i++) {         if (s[i] ==  A )           cntA[i]++;         else if (s[i] ==  B )           cntB[i]++;         else if (s[i] ==  C )           cntC[i]++;         else if (s[i] ==  D )           cntD[i]++;         else if (s[i] ==  E )           cntE[i]++;       }     }     long long int ans = 0;     for (int i = 0; i < m; i++) {       long long int c;       cin >> c;       long long int maxi = 0;       maxi = max(max(cntA[i], cntB[i]), cntC[i]);       maxi = max(max(maxi, cntD[i]), cntE[i]);       long long int temp = maxi * c;       ans += temp;     }     cout << ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int T = 1;     int t = 0;     while (t++ < T) {       solve();     }   }


#include<bits/stdc++.h>   #define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);   #define int long long   #define ull unsigned long long   #define FOR(i,a,b) for(int i=a;i<b;i++)   #define all(x) (x).begin(), (x).end()   #define pb push_back   #define mp make_pair   #define in insert   #define ff first   #define ss second   #define endl   n    #define pi pair<int,int>   #define  deb1(a)         cout<<a<<  n ;   #define  deb2(a,b)       cout<<a<<   <<b<<  n ;   #define  deb3(a,b,c)     cout<<a<<   <<b<<   <<c<<  n ;   #define  debp(a)         cout<<a.ff<<   <<a.ss<<  n ;   #define  debvec(a)      for(auto it: a)cout<<it<<   ;cout<<   ; cout<<endl;   #define  debvp(a)      for(auto it: a)cout<<it.ff<<   <<it.ss<<endl;   #define  debmap(a)      for(auto it: a)cout<< { <<it.ff<< , <<it.ss<< },  ;newl;   #define  debarr(a,n)    FOR(i,0,n)cout<<a[i]<<   ;cout<<endl;   #define inf ((int)1<<60)   #define mod 1000000007   using namespace std;   //int,char,float,double,bool,string,break,continue,endl,if,else,set,multiset,multimap,unordered_map,unordered_set,cout,cin,printf,scanf   //strlen,strcmp,compar e,auto,while,return,true,false,sizeof,size,abs,INT_MAX,INT_MIN,itr,it,sort,YES,NO      signed main() {   #ifndef ONLINE_JUDGE    freopen( input.txt ,  r , stdin);    freopen( output.txt ,  w , stdout);   #endif    fast_io    int test, i, j;   //  cin >> test;    test = 1;    while (test--) {     int n, m;     cin >> n >> m;     string s, t;     cin >> s >> t;     int a[m], b[m];     j = 0;     for (int i = 0; i < n; ++i) {      if (s[i] == t[j]) {       a[j] = i;       j++;      }     }     j = m - 1;     for (int i = n - 1; i >= 0 ; i--) {      if (s[i] == t[j]) {       b[j] = i;       j--;      }     }     int ans = 0;     for (int i = 0; i < m - 1; ++i) {      ans = max(ans, b[i + 1] - a[i]);     }     cout << ans << endl;    }      }


#include <bits/stdc++.h>   using namespace std;   const int N = 110;   const int mo = 1e9 + 7;   int n, k, a[N], num[3], nn, f = 0;   long long ans, now[N][N], dp[N][N], aa[N];   long long po(long long a, long long b) {     if (b == 0) return 1;     if (b == 1) return a;     long long c = po(a, b / 2);     if (b & 1)       return c * c % mo * a % mo;     else       return c * c % mo;   }   long long tmp[N][N];   void multi(long long a[][N], long long b[][N], int n) {     memset(tmp, 0, sizeof tmp);     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++)         for (int k = 0; k < n; k++)           tmp[i][j] = (tmp[i][j] + a[i][k] * b[k][j] % mo) % mo;     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++) a[i][j] = tmp[i][j];   }   long long res[N][N];   void Pow(long long a[][N], long long n) {     memset(res, 0, sizeof res);     for (int i = 0; i < num[0] + 1; i++) res[i][i] = 1;     while (n) {       if (n & 1) multi(res, a, num[0] + 1);       multi(a, a, num[0] + 1);       n >>= 1;     }   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]), num[a[i]]++;     if (num[0] < num[1]) {       for (int i = 1; i <= num[0]; i++)         if (!a[i]) nn++;     } else {       swap(num[0], num[1]);       for (int i = n; i >= n - num[0] + 1; i--)         if (a[i]) nn++;     }     for (int j = 0; j <= num[0]; j++) {       now[j][j] += j * (num[0] - j);       now[j][j] += (num[0] - j) * (num[1] - num[0] + j);       now[j][j] += num[0] * (num[0] - 1) / 2ll;       now[j][j] += num[1] * (num[1] - 1) / 2ll;       if (j + 1 <= num[0]) now[j][j + 1] = (j + 1) * (num[1] - (num[0] - j - 1));       if (j) now[j][j - 1] = (num[0] - j + 1) * (num[0] - j + 1);     }     Pow(now, k);     for (int i = 0; i <= num[0]; i++) ans = (ans + res[i][nn]) % mo;     printf( %lld n , res[num[0]][nn] * po(ans, mo - 2) % mo);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-9;   const int INF = (int)1e9 + 7;   const long long linf = (long long)1e16 + 7;   struct pnt {     double x, y;     pnt(double x, double y) : y(y), x(x) {}     pnt() = default;   };   int cmp(double a, double b) {     if (abs(a - b) < eps) return 0;     if (a > b) return 1;     return -1;   }   int main() {     int n;     cin >> n;     int ans = 0;     vector<int> a(n);     for (int i = 0; i < n; ++i) cin >> a[i];     vector<int> cnt0(n + 1);     vector<int> cnt1(n + 1);     for (int i = 0; i < n; ++i) {       if (a[i] == 0) cnt0[i]++;       if (i) cnt0[i] += cnt0[i - 1];     }     for (int i = n - 1; i >= 0; --i) {       if (a[i] == 1) cnt1[i]++;       if (i != n - 1) cnt1[i] += cnt1[i + 1];     }     for (int i = 0; i < n; ++i) {       ans = max(ans, cnt0[i] + cnt1[i + 1]);       ans = max(ans, cnt1[i]);     }     cout << ans;     return 0;   }


#include<bits/stdc++.h>      using namespace std;      #define    pf               printf   #define    PF(a)            printf( %d n ,(a))   #define    PF2(a,b)         printf( %d %d n ,(a), (b))   #define    PF3(a,b,c)       printf( %d %d %d n ,(a), (b), (c))   #define    PFL(a)           printf( %lld n ,(a))   #define    PFL2(a,b)        printf( %lld %lld n ,(a), (b))   #define    PFL3(a,b,c)      printf( %lld %lld %lld n ,(a), (b), (c))   #define    get(a)           cin>>a   #define    sf               scanf   #define    SF(a)            scanf( %d ,&a)   #define    SF2(a,b)         scanf( %d %d ,&a, &b)   #define    SF3(a,b,c)       scanf( %d %d %d ,&a, &b, &c)   #define    SFL(a)           scanf( %lld ,&a)   #define    SFL2(a,b)        scanf( %lld %lld ,&a, &b)   #define    SFL3(a,b,c)      scanf( %lld %lld %lld ,&a, &b, &c)   #define    gl(a)            getline(cin,(a))   #define    gc()             getchar()   #define    pb               push_back   #define    m_p              make_pair   #define    pc()             printf( Case %d:  ,tt++)   #define    tc()             cout<< Case  <<tt++<< :     #define    dbg(x)           cout << #x <<   ->   << x << endl;   #define    SZ(a)            (int)(a).size()   #define    all(a)           a.begin(),a.end()   #define    FF               first   #define    SS               second   #define    MAX              2134567891   #define    MOD              1000000007   #define    MM               200005   #define    PI               2*acos(0.0)   #define    cond(n,m)        (xx>=0 && xx<(n) && yy>=0 && yy<(m))      #define    mem(a)           memset((a),0,sizeof (a))   #define    SET(a)           memset((a),-1,sizeof (a))   #define    output           freopen( output.txt , w ,stdout);   #define    input            freopen( input.txt , r ,stdin);   #define    I_O              ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0)   #define    rep(a)           for(int i=0;i<(a);i++)   #define    FOR(a,b)         for(int i=(a);i<(b);i++)   #define    REP(a)           for(int j=0;j<(a);j++)   #define    rev(a)           for(int i=(a);i>=0;i--)   #define    fr(i,n)          for(i=0;i<n;i++)   #define    fr1(i,n,m)       for(i=n;i<m;i++)   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   int dx[]  = {0, 0, 1, -1};   int dy[]  = {1, -1, 0, 0};   int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};   int dy8[] = {1, -1, -1, 0, 1, -1, 0, 1};   int kx8[] = {1, 1, 2, 2, -1, -1, -2, -2};   int ky8[] = {2, -2, 1, -1, 2, -2, 1, -1};      typedef long long ll;   typedef unsigned long long llu;   typedef priority_queue < int > PQ;   typedef pair < ll , ll > pi;   typedef pair <  ll, pi > pii;   typedef vector < int > vi;   typedef vector < ll > vl;   typedef vector < string > vs;      /*   //FUNCTION//   ll bigmod(ll a, ll b, ll c)   {       if(b==0)return 1%c;ll x=bigmod(a,b/2,c);x=(x*x)%c;       if(b%2==1)x=(x*a)%c;return x;   }   ll mod_inverse(ll a, ll mod){return bigmod(a,mod-2,mod);}   ll poww(ll a, ll b)   {       if(b==0)return 1;ll x=poww(a,b/2);x=x*x;if(b%2==1)x=(x*a);return x;   }            ll LCM(ll a, ll b){ return a*b/ __gcd(a,b);}      int pr = 100005;   vi primelist;   bool a[MM];   void seive( )   {       int i,j,k=sqrt(pr);       a[1]=1;       primelist.pb(2);       for(i=4;i<=pr;i+=2)a[i]=1;       for(i=3;i<=k;i+=2)if(!a[i])for(j=i*i;j<=pr;j+=2*i)a[j]=1;       for(i=3;i<=pr;i+=2)if(!a[i])primelist.pb(i);   }   int phi[MM];   void calculatePhi() {     for (int i = 1; i < MM; i++) {       phi[i] = i;     }     for (int p = 2; p < M; p++) {       if (phi[p] == p) { // p is a prime         for (int k = p; k < M; k += p) {           phi[k] -= phi[k] / p;         }       }     }   }      ll fact_divs( ll n, ll p)   {       ll cnt=0;while(p<=n){cnt += n/p;n /= p;}return cnt;   }   struct point{double x, y;};   double area( point A, point B, point C){   return 0.5*(A.x*(B.y-C.y) - A.y*(B.x-C.x) + B.x*C.y - C.x*B.y);   }      int Set(int N,int pos){return N=N | (1<<pos);}   int reset(int N,int pos){return N= N & ~(1<<pos);}   bool check(int N,int pos){return (bool)(N & (1<<pos));}      //*/   int a[MM];   int main(){       I_O;       int t, tt = 1;       cin>>t;       while(t--){           int n; cin>>n;           int id = 0, mn = MAX;           for(int i=1; i<=n; i++){               cin>>a[i];               if(a[i] < mn){                   mn = a[i];                   id = i;               }           }           cout<<n-1<<endl;           for(int i=id+1, cnt=1; i<=n; i++, cnt++){               cout<<id<<   <<i<<   <<a[id]<<   <<a[id]+cnt<<endl;           }           for(int i=id-1, cnt=1; i >=1; i--, cnt++){               cout<<id<<   <<i<<   <<a[id]<<   <<a[id]+cnt<<endl;           }             }                   return 0;   }   /*   2   5   9 6 3 11 15   3   7 5 13               */


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i;     cin >> n;     string s;     map<char, int> m;     for (i = 0; i < n; i++) {       cin >> s;       m[s[0]]++;     }     int ans = 0, f1, f2;     for (auto it = m.begin(); it != m.end(); it++) {       f1 = it->second / 2;       f2 = it->second - f1;       ans += f1 * (f1 - 1) / 2 + f2 * (f2 - 1) / 2;     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int Read() {     int x = 0;     char ch = getchar();     while (!isdigit(ch)) ch = getchar();     while (isdigit(ch)) x = (x << 3) + (x << 1) + ch -  0 , ch = getchar();     return x;   }   void Write(int x) {     if (x < 0) putchar( - ), x = -x;     if (x == 0) putchar( 0 );     int stk[55], tp = 0;     while (x) stk[++tp] = x % 10, x /= 10;     for (int i = tp; i; i--) putchar(stk[i] +  0 );   }   int Add(int a, int b) {     return (a + b >= 998244353) ? a + b - 998244353 : a + b;   }   int Dec(int a, int b) { return (a - b < 0) ? a - b + 998244353 : a - b; }   int mul(int a, int b) { return 1ll * a * b % 998244353; }   unsigned int qpow(unsigned int a, int b) {     unsigned int res = 1;     for (; b; b >>= 1, a = mul(a, a))       if (b & 1) res = mul(res, a);     return res;   }   namespace NTT {   int sz;   unsigned int w[2500005], w_mf[2500005];   int mf(int x) { return (1ll * x << 32) / 998244353; }   void init(int n) {     for (sz = 2; sz < n; sz <<= 1)       ;     unsigned int pr = qpow(3, (998244353 - 1) / sz);     w[sz / 2] = 1;     w_mf[sz / 2] = mf(1);     for (int i = 1; i <= sz / 2; i++)       w[sz / 2 + i] = mul(w[sz / 2 + i - 1], pr),                  w_mf[sz / 2 + i] = mf(w[sz / 2 + i]);     for (int i = sz / 2 - 1; i; i--) w[i] = w[i << 1], w_mf[i] = w_mf[i << 1];   }   void ntt(vector<unsigned int>& A, int L) {     for (int d = L >> 1; d; d >>= 1)       for (int i = 0; i < L; i += (d << 1))         for (int j = 0; j < d; j++) {           unsigned int x = A[i + j] + A[i + d + j];           if (x >= 998244353 * 2) x -= 998244353 * 2;           long long t = A[i + j] + 998244353 * 2 - A[i + d + j],                     q = t * w_mf[d + j] >> 32, y = t * w[d + j] - q * 998244353;           A[i + j] = x;           A[i + d + j] = y;         }     for (int i = 0; i < L; i++)       if (A[i] >= 998244353) A[i] -= 998244353;   }   void intt(vector<unsigned int>& A, int L) {     for (int d = 1; d < L; d <<= 1)       for (int i = 0; i < L; i += (d << 1))         for (int j = 0; j < d; j++) {           unsigned int x = A[i + j];           if (x >= 998244353 * 2) x -= 998244353 * 2;           long long t = A[i + d + j], q = t * w_mf[d + j] >> 32,                     y = t * w[d + j] - q * 998244353;           A[i + j] = x + y, A[i + d + j] = x + 2 * 998244353 - y;         }     int k = __builtin_ctz(L);     reverse(A.begin() + 1, A.end());     for (int i = 0; i < L; i++) {       long long m = -A[i] & (L - 1);       A[i] = (A[i] + m * 998244353) >> k;       if (A[i] >= 998244353) A[i] -= 998244353;     }   }   }  // namespace NTT   struct poly {     vector<unsigned int> a;     poly(int d = 0, int t = 0) {       a.resize(d + 1);       a[d] = t;     }     int deg() { return a.size() - 1; }     unsigned int& operator[](const int& b) { return a[b]; }     poly extend(int x) {       poly c = *this;       c.a.resize(x + 1);       return c;     }     void resize(int x) { a.resize(x); }     int size() { return a.size(); }     void Get(int n) {       a.resize(n + 1);       for (int i = 0; i <= n; i++) a[i] = Read();     }     void Print(int n) {       for (int i = 0; i <= n; i++) Write(a[i]), putchar(   );       puts(  );     }     friend poly operator*(poly A, poly B) {       int n = A.deg() + B.deg() + 1, lim = 2;       for (; lim < n; lim <<= 1)         ;       NTT::init(lim);       A.resize(lim);       B.resize(lim);       NTT::ntt(A.a, lim);       NTT::ntt(B.a, lim);       for (int i = 0; i < lim; i++) A[i] = mul(A[i], B[i]);       NTT::intt(A.a, lim);       return A.extend(n - 1);     }     poly inv(poly A) {       int n = A.a.size(), lim = 2;       if (n == 1) return A[0] = qpow(A[0], 998244353 - 2), A;       poly B = A;       B.resize((n + 1) >> 1);       B = inv(B);       for (; lim < (n << 1); lim <<= 1)         ;       NTT::init(lim);       A.resize(lim), B.resize(lim), NTT::ntt(A.a, lim);       NTT::ntt(B.a, lim);       for (int i = 0; i < lim; i++) A[i] = mul(Dec(2, mul(A[i], B[i])), B[i]);       NTT::intt(A.a, lim);       return A.extend(n - 1);     }     poly getinv() { return inv(*this); }     poly getdao() {       poly A = *this, B;       int n = A.a.size();       B.resize(n);       for (int i = 1; i < n; i++) B[i - 1] = mul(A[i], i);       B[n - 1] = 0;       return B;     }     poly getjf() {       poly A = *this, B;       int n = A.a.size();       B.resize(n);       for (int i = 1; i < n; i++) B[i] = mul(A[i - 1], qpow(i, 998244353 - 2));       B[0] = 0;       return B;     }     poly getln() {       poly A = *this;       return (A.getdao() * A.getinv()).extend(A.deg()).getjf();     }     poly exp(poly A) {       int n = A.a.size();       if (n == 1) return A[0] = 1, A;       poly B = A;       B.resize((n + 1) >> 1);       B = exp(B);       poly C = B.getln();       C.resize(n);       for (int i = 0; i < n; i++) C[i] = Dec(A[i], C[i]);       ++C[0];       return (B * C).extend(n - 1);     }     poly getexp() { return exp(*this); }     poly getmi(int k) {       poly A = *this;       int n = A.a.size();       poly B = A.getln();       for (int i = 0; i < n; i++) B[i] = mul(B[i], k);       return B.getexp();     }     poly sqrt(poly A) {       int n = A.size(), lim = 2;       if (n == 1) return A[0] = 1, A;       poly B = A;       B.resize((n + 1) >> 1);       B = sqrt(B);       poly C = A.extend(n - 1), D = B.getinv();       for (; lim < (n << 1); lim <<= 1)         ;       NTT::init(lim);       B.resize(lim), C.resize(lim), D.resize(lim), NTT::ntt(B.a, lim);       NTT::ntt(C.a, lim);       NTT::ntt(D.a, lim);       for (int i = 0; i < lim; i++)         B[i] = mul(Add(B[i], mul(C[i], D[i])), 499122177);       NTT::intt(B.a, lim);       return B.extend(n - 1);     }     poly getsqrt() { return sqrt(*this); }   };   signed main() {     int n = Read(), k = Read();     poly A, B;     A.resize(k * 3), B.resize(k * 3);     A[0] = 1;     A[1] = 6;     A[2] = 1;     A = A.getsqrt();     B = A.getinv();     ++A[1];     ++A[0];     for (int i = 0; i <= k; i++) A[i] = 1ll * A[i] * 499122177 % 998244353;     A = A.getmi(n + 1);     A = (A * B).extend(k * 3);     for (int i = 1; i <= k; i++) {       if (i <= n)         printf( %d  , A[i]);       else         printf( %d  , 0);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, d, a, b;     cin >> n >> d >> a >> b;     vector<pair<long long, int> > v(n);     for (int i = 0; i < n; i++) {       long long x, y;       cin >> x >> y;       v[i].first = x * a + y * b;       v[i].second = i + 1;     }     sort(v.begin(), v.end());     int ans = 0, sum = 0, pos = 0;     for (int i = 0; i < n; i++) {       if (sum + v[i].first <= d) {         sum += v[i].first;         ans++;         pos = i;       }     }     cout << ans << endl;     if (ans)       for (int i = 0; i <= pos; i++) cout << v[i].second <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     long long x, y, p, q;     cin >> t;     while (t--) {       cin >> x >> y >> p >> q;       long long l = 0, h = 1e9, ans = -1, mid, a, b;       while (l <= h) {         mid = (l + h) / 2;         a = p * mid;         b = q * mid;         if (b >= y && a >= x && b - a >= y - x) {           ans = b - y;           h = mid - 1;         } else           l = mid + 1;       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int mod = 1e9 + 7;   const long long int N = 2 * (1e5);   const long long int maxo = 1e14;   const long long int inf = 1e17;   int main(int argc, char const *argv[]) {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int x0, y0, ax, ay, bx, by;     cin >> x0 >> y0 >> ax >> ay >> bx >> by;     long long int xs, ys, t;     cin >> xs >> ys >> t;     vector<pair<long long int, long long int> > v;     v.push_back({x0, y0});     for (long long int i = 0; i < 100; i++) {       if (v.back().first > (inf - bx) / (ax)) break;       if (v.back().second > (inf - by) / ay) break;       long long int x1 = v.back().first * ax + bx;       long long int y1 = v.back().second * ay + by;       v.push_back({x1, y1});     }     long long int n = v.size();     long long int ans = 0;     for (long long int l = 0; l < n; l++) {       for (long long int r = l; r < n; r++) {         long long int d1 = abs(xs - v[l].first) + abs(ys - v[l].second);         long long int d2 = abs(xs - v[r].first) + abs(v[r].second - ys);         long long int len =             abs(v[l].first - v[r].first) + abs(v[l].second - v[r].second);         if (d1 + len <= t) {           ans = max(ans, r - l + 1);         }         if (d2 + len <= t) {           ans = max(ans, r - l + 1);         }       }     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   void setIO(string name =   ) {     ios_base::sync_with_stdio(0);     cin.tie(0);     if (int((name).size())) {       freopen((name +  .in ).c_str(),  r , stdin);       freopen((name +  .out ).c_str(),  w , stdout);     }   }   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   long long mcm(long long a, long long b) { return (a * b) / gcd(a, b); }   bool prime(long long n, long long i) {     if (i > sqrt(n)) return true;     if (n % i == 0)       return false;     else       return prime(n, i + 1);   }   struct compii {     bool operator()(const pair<int, int> &a, const pair<int, int> &b) {       if (a.first == a.second) return a.second < b.second;       return a.first > b.first;     }   };   bool comp(int a, int b) { return a > b; }   int dx[] = {1, 0, -1, 0};   int dy[] = {0, 1, 0, -1};   const int mod = 1e9 + 7;   const long long inf = 1e18;   bool check(string a, string b) {     if (a ==  rock  and b ==  scissors ) return true;     if (a ==  paper  and b ==  rock ) return true;     if (a ==  scissors  and b ==  paper ) return true;     return false;   }   void solve() {     string first, m, second;     cin >> first >> m >> second;     if (check(first, m) and check(first, second))       cout <<  F ;     else if (check(m, second) and check(m, first))       cout <<  M ;     else if (check(second, first) and check(second, m))       cout <<  S ;     else       cout <<  ? ;   }   int main() {     setIO(  );     int t = 1;     while (t-- > 0) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s =   ;     for (int i = 1; i < 1000; i++) {       s += to_string(i);     }     int n;     cin >> n;     cout << s[n - 1];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int adj[8][2] = {{-1, 0},  {0, -1}, {1, 0}, {0, 1},                          {-1, -1}, {-1, 1}, {1, 1}, {1, -1}};   const long long int LLINF = 9e18;   const int INF = 2e9;   const int MOD = 1e9 + 7;   const double EPS = 1e-10;   const double PI = acos(-1);   const int SIZE = 100011;   int N;   int cnt[SIZE];   map<string, int> Mp;   string ts[8] = { S ,  M ,  L ,  XL ,  XXL ,  XXXL };   string s;   struct edge {     int v, f, c, u;   };   vector<edge> E[SIZE];   int st[SIZE], lvl[SIZE];   void init() {     for (int i = 0; i < SIZE; i++) {       cnt[i] = 0;       E[i].clear();     }   }   void addedge(int a, int b, int ca, int cb) {     edge A = {a, 0, ca, (int)E[a].size()};     edge B = {b, 0, cb, (int)E[b].size()};     E[a].push_back(B);     E[b].push_back(A);   }   bool BFS(int S, int T) {     memset(lvl, -1, sizeof(lvl));     queue<int> q;     q.push(S);     lvl[S] = 0;     while (!q.empty()) {       int indx = q.front();       q.pop();       for (unsigned int i = 0; i < E[indx].size(); i++) {         edge &t = E[indx][i];         int idx = t.v;         if (lvl[idx] < 0 && t.c > t.f) {           lvl[idx] = lvl[indx] + 1;           q.push(idx);         }       }     }     return lvl[T] >= 0;   }   int DFS(int indx, int T, int flow) {     if (indx == T) return flow;     for (int &i = st[indx]; i < (int)E[indx].size(); i++) {       edge &t = E[indx][i];       int idx = t.v;       if (lvl[idx] == lvl[indx] + 1 && t.c > t.f) {         int cur = min(flow, t.c - t.f);         int ret = DFS(idx, T, cur);         if (ret > 0) {           t.f += ret;           E[t.v][t.u].f -= ret;           return ret;         }       }     }     return 0;   }   int Dinic(int S, int T) {     int ret = 0;     while (BFS(S, T)) {       memset(st, 0, sizeof(st));       while (int flow = DFS(S, T, INF)) {         ret += flow;       }     }     return ret;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int S = 0, T = SIZE - 1, a;     init();     for (int i = 1; i <= 6; i++) {       cin >> a;       Mp[ts[i - 1]] = i;       addedge(i, T, 0, a);     }     cin >> N;     for (int i = 1; i <= N; i++) {       cin >> s;       s +=  , ;       string temp =   ;       for (unsigned int j = 0; j < s.size(); j++) {         if (s[j] ==  , ) {           addedge(i + 6, Mp[temp], 0, 1);           temp.clear();           cnt[i + 6]++;         } else           temp.push_back(s[j]);       }       addedge(S, i + 6, 0, 1);     }     int ans = Dinic(S, T);     if (ans != N)       cout <<  NO  << endl;     else {       cout <<  YES  << endl;       for (int i = 1; i <= N; i++) {         edge t1, t2;         if (cnt[i + 6] == 1) {           t1 = E[i + 6][0];           cout << ts[t1.v - 1] << endl;         } else {           t1 = E[i + 6][0];           t2 = E[i + 6][1];           if (t1.f == 1)             cout << ts[t1.v - 1] << endl;           else if (t2.f == 1)             cout << ts[t2.v - 1] << endl;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[101];   int b[101];   int storage[200001];   int storage2[200001];   int* value = storage + 100000;   int* prevalue = storage2 + 100000;   int n, k;   int main() {     scanf( %d%d , &n, &k);     int gtzero = 0, lezero = 0;     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     for (int i = 0; i < n; i++) scanf( %d , &b[i]), b[i] *= k;     for (int i = 0; i < n; i++) {       b[i] = a[i] - b[i];       if (b[i] > 0)         gtzero += b[i];       else         lezero += b[i];     }     for (int i = 0; i < sizeof(storage) / sizeof(int); i++) {       storage[i] = -19200000;       storage2[i] = -19200000;     }     value[0] = prevalue[0] = 0;     for (int i = 0; i < n; i++) {       int C = b[i];       for (int j = -10000; j <= 10000; j++) {         value[j] = max(prevalue[j], prevalue[j - b[i]] + a[i]);       }       memcpy(storage2, storage, sizeof(storage));     }     if (value[0] > 0)       printf( %d n , value[0]);     else       printf( -1 n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 500005;   const int maxx = 1000005;   int n, x, y, q, l, r;   int a[N];   struct Tree {     int ans, m, tag;   } tr[maxx * 6];   void pushdown(int rt) {     int lson = rt << 1;     int rson = rt << 1 | 1;     tr[lson].m += tr[rt].tag;     tr[rson].m += tr[rt].tag;     tr[lson].tag += tr[rt].tag;     tr[rson].tag += tr[rt].tag;     tr[rt].tag = 0;   }   void updata(int rt) {     int lson = rt << 1;     int rson = rt << 1 | 1;     if (tr[lson].m == tr[rson].m) {       tr[rt].m = tr[lson].m;       tr[rt].ans = tr[lson].ans + tr[rson].ans;     } else if (tr[lson].m < tr[rson].m) {       tr[rt].m = tr[lson].m;       tr[rt].ans = tr[lson].ans;     } else {       tr[rt].m = tr[rson].m;       tr[rt].ans = tr[rson].ans;     }   }   void change(int rt, int l, int r, int cl, int cr, int C) {     if (cl <= l && r <= cr) {       tr[rt].m += C;       tr[rt].tag += C;       return;     }     pushdown(rt);     int mid = (l + r) >> 1;     if (cl <= mid) change(rt << 1, l, mid, cl, cr, C);     if (cr > mid) change(rt << 1 | 1, mid + 1, r, cl, cr, C);     updata(rt);   }   void change2(int rt, int l, int r, int x, int C) {     if (l == r) {       tr[rt].ans += C;       return;     }     pushdown(rt);     int mid = (l + r) >> 1;     if (x <= mid) change2(rt << 1, l, mid, x, C);     if (x > mid) change2(rt << 1 | 1, mid + 1, r, x, C);     updata(rt);   }   int query(int rt, int l, int r, int ql, int qr) {     if (ql <= l && r <= qr) {       if (tr[rt].m == 1) return tr[rt].ans;       return 0;     }     pushdown(rt);     int mid = (l + r) >> 1, summ = 0;     if (ql <= mid) summ += query(rt << 1, l, mid, ql, qr);     if (qr > mid) summ += query(rt << 1 | 1, mid + 1, r, ql, qr);     updata(rt);     return summ;   }   int main() {     scanf( %d %d , &n, &q);     for (int i = 1; i <= n; ++i) scanf( %d , &a[i]);     a[0] = maxx;     a[n + 1] = 0;     for (int i = 1; i <= n; ++i) change2(1, 0, maxx, a[i], 1);     for (int i = 1; i <= n + 1; ++i) {       int l = a[i - 1];       int r = a[i];       if (l == r) continue;       if (l > r) swap(l, r);       change(1, 0, maxx, l, r - 1, 1);     }     for (int i = 1; i <= q; ++i) {       scanf( %d %d , &x, &y);       l = a[x];       r = a[x + 1];       if (l != r) {         if (l > r) swap(l, r);         change(1, 0, maxx, l, r - 1, -1);       }       l = a[x - 1];       r = a[x];       if (l != r) {         if (l > r) swap(l, r);         change(1, 0, maxx, l, r - 1, -1);       }       change2(1, 0, maxx, a[x], -1);       change2(1, 0, maxx, y, 1);       a[x] = y;       l = a[x];       r = a[x + 1];       if (l != r) {         if (l > r) swap(l, r);         change(1, 0, maxx, l, r - 1, 1);       }       l = a[x - 1];       r = a[x];       if (l != r) {         if (l > r) swap(l, r);         change(1, 0, maxx, l, r - 1, 1);       }       printf( %d n , query(1, 0, maxx, 1, maxx - 1));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int inf = 1e9 + 7;   const ll longinf = 1LL << 60;   const ll mod = 1e9 + 7;   int main() {     int h, w, n, q;     cin >> h >> w >> n >> q;     vector<int> v[h];     for (int i = (int)(0); i < (int)(n); ++i) {       int x, y;       cin >> x >> y;       --x;       --y;       v[x].push_back(y);     }     for (int i = (int)(0); i < (int)(h); ++i) sort(v[i].begin(), v[i].end());     vector<ll> ok;     ok.push_back(-longinf);     ok.push_back(longinf);     for (int i = (int)(0); i < (int)(q); ++i) {       int x;       cin >> x;       --x;       ok.push_back(x);     }     sort(ok.begin(), ok.end());     ll pl = 0, pr = 0;     ll dp[2][202020];     for (int i = (int)(0); i < (int)(2); ++i)       for (int j = (int)(0); j < (int)(202020); ++j) dp[i][j] = longinf;     dp[0][1] = dp[1][1] = 0;     if (v[0].size()) {       pl = v[0][0];       pr = v[0].back();       dp[0][1] = 2 * pr - pl;       dp[1][1] = pr;     }     int maxh = 1;     for (int i = (int)(1); i < (int)(h); ++i) {       if (v[i].empty()) {         dp[0][i + 1] = dp[0][i] + 1;         dp[1][i + 1] = dp[1][i] + 1;         continue;       }       int nl = v[i][0], nr = v[i].back();       auto itr = lower_bound(ok.begin(), ok.end(), pl);       dp[0][i + 1] = min(dp[0][i + 1], dp[0][i] + abs(*itr - pl) +                                            abs(nr - *itr) + abs(nr - nl) + 1);       dp[1][i + 1] = min(dp[1][i + 1], dp[0][i] + abs(*itr - pl) +                                            abs(nl - *itr) + abs(nr - nl) + 1);       --itr;       dp[0][i + 1] = min(dp[0][i + 1], dp[0][i] + abs(*itr - pl) +                                            abs(nr - *itr) + abs(nr - nl) + 1);       dp[1][i + 1] = min(dp[1][i + 1], dp[0][i] + abs(*itr - pl) +                                            abs(nl - *itr) + abs(nr - nl) + 1);       itr = lower_bound(ok.begin(), ok.end(), pr);       dp[0][i + 1] = min(dp[0][i + 1], dp[1][i] + abs(*itr - pr) +                                            abs(nr - *itr) + abs(nr - nl) + 1);       dp[1][i + 1] = min(dp[1][i + 1], dp[1][i] + abs(*itr - pr) +                                            abs(nl - *itr) + abs(nr - nl) + 1);       --itr;       dp[0][i + 1] = min(dp[0][i + 1], dp[1][i] + abs(*itr - pr) +                                            abs(nr - *itr) + abs(nr - nl) + 1);       dp[1][i + 1] = min(dp[1][i + 1], dp[1][i] + abs(*itr - pr) +                                            abs(nl - *itr) + abs(nr - nl) + 1);       pl = nl;       pr = nr;       maxh = max(i + 1, maxh);     }     cout << min(dp[0][maxh], dp[1][maxh]) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     int cnt = 0;     cin >> n;     string a;     for (int i = 0; i < n; i++) {       cin >> a;       if (a ==  Tetrahedron )         cnt += 4;       else if (a ==  Cube )         cnt += 6;       else if (a ==  Octahedron )         cnt += 8;       else if (a ==  Dodecahedron )         cnt += 12;       else         cnt += 20;     }     cout << cnt;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   #pragma comment(linker,  /STACK:20000000 )   string problem_name =  a ;   void init() { freopen((problem_name +  .in ).c_str(),  rt , stdin); }   long long res = 0;   int ms[300100];   int len;   void mul(int val) {     int p = 0;     for (int i = 0; i < 300000; i++) {       p += ms[i] *= val;       ms[i] = p % 10;       p /= 10;     }   }   int main() {     string s;     cin >> s;     vector<int> u(255, 0);     res = 1;     int zero = 0;     long long col = 10;     int c = 0;     int c2 = 0;     for (int i = 0; i < int((s).size()); i++) {       if (s[i] ==  ? ) {         c++;       } else if (isalpha(s[i])) {         if (u[s[i]]) continue;         u[s[i]] = 1;         if (col <= 0) {           res = 0;           zero = 1;           break;         }         c2++;         col--;       }     }     if (zero) {       cout << 0;       return 0;     }     if (c) {       if (s[0] ==  ? )         ms[c - 1] = 9;       else         ms[c] = 1;     } else       ms[0] = 1;     if (c2) {       for (int i = 10; i > 10 - c2; i--) {         if (i == 10 && isalpha(s[0]))           mul(9);         else           mul(i);       }     }     int len = 300000;     while (!ms[len]) len--;     for (int i = len; i >= 0; i--) printf( %d , ms[i]);     return 0;   }


#include <bits/stdc++.h>   const long long mod = 1e9 + 7;   using namespace std;   const long long N = 102;   const long long M = 1e4 + 2;   long long dp[N][M], c[N][N], cnt[N];   signed main() {     long long n, i, j, k, l, ans = 0, type = 0;     cin >> n;     dp[0][0] = 1;     c[0][0] = 1;     for (i = 1; i <= n; i++) {       cin >> j;       if (!cnt[j]) {         type++;       }       cnt[j]++;       for (l = i; l >= 1; l--) {         for (k = 10000; k >= j; k--) {           dp[l][k] += dp[l - 1][k - j];           if (dp[l][k] > mod) {             dp[l][k] -= mod;           }         }       }     }     if (type <= 2) {       cout << n;       return 0;     }     for (i = 1; i <= 100; i++) {       for (j = 0; j <= i; j++) {         c[j][i] = c[j][i - 1];         if (j) {           c[j][i] += c[j - 1][i - 1];         }         c[j][i] %= mod;       }     }     for (i = 1; i <= 100; i++) {       for (j = 1; j <= cnt[i]; j++) {         if (c[j][cnt[i]] == dp[j][i * j]) {           ans = max(ans, j);         }       }     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   const int dx[] = {0, 1, -1, 0, 1, -1, -1, 1};   const int dy[] = {-1, 0, 0, 1, 1, 1, -1, -1};   const int N = 0;   const int MOD = 0;   const int INF = 1e9 + 10;   const long long int LLINF = 1e18 + 10;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n, m;     cin >> n >> m;     int v[10010];     int l, r, x;     for (int i = 1; i <= n; i++) {       cin >> v[i];     }     int aux;     bool mudou;     int cnt = 0;     while (m--) {       cin >> l >> r >> x;       aux = x - l;       cnt = 0;       for (int i = l; i <= r; i++) {         if (v[i] < v[x]) cnt++;       }       if (cnt == aux)         cout <<  Yes n ;       else         cout <<  No n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int suma = 0, sumg = 0, a, g;     char *arr = new char[n];     for (int i = 0; i < n; i++) {       scanf( %d %d , &a, &g);       if (abs(suma + a - sumg) < abs(sumg + g - suma)) {         suma += a;         arr[i] =  A ;       } else {         sumg += g;         arr[i] =  G ;       }     }     if (abs(suma - sumg) > 500)       cout <<  -1 ;     else       for (int i = 0; i < n; i++) cout << arr[i];     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool isPowerOfTwo(int x) { return (x && !(x & (x - 1))); }   bool is_prime(int n) {     if (n == 1) return false;     for (int i = 2; i <= sqrt(n); i++) {       if (n % i == 0) {         return false;       }     }     return true;   }   void solve() {     int n;     cin >> n;     long long int a, b;     cin >> a >> b;     set<long long int> st;     st.insert(a);     st.insert(b);     for (int i = 2; i * i <= a; i++) {       if (a % i == 0) {         st.insert(i);         st.insert(a / i);       }     }     for (int i = 2; i * i <= b; i++) {       if (b % i == 0) {         st.insert(i);         st.insert(b / i);       }     }     map<pair<long long int, long long int>, long long int> ch;     ch[{a, b}]++;     for (int i = 1; i < n; i++) {       cin >> a >> b;       if (ch[{a, b}] >= 1) continue;       ch[{a, b}]++;       for (auto it = st.begin(); it != st.end();) {         long long int k = *it;         if (a % k == 0 || b % k == 0) {           it++;         } else {           it = st.erase(it++);         }       }     }     if (st.empty())       cout <<  -1 ;     else       cout << (*st.begin());   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using int64 = long long;   int64 bit_count(int64 n) {     int64 a{};     while (n) {       a += 1;       n >>= 1;     }     return a;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int T;     cin >> T;     while (T--) {       int64 n, m;       cin >> n >> m;       vector<int64> P(m);       for (auto &p : P) cin >> p;       auto s = accumulate(begin(P), end(P), 0ll);       if (s < n) {         cout << -1 <<   n ;         continue;       }       vector<int64> F(65);       for (auto p : P) F[bit_count(p) - 1]++;       int64 ans{};       for (int i = 0, j = -1; i < 64; i++) {         if (j >= 0 and F[i]) {           ans += i - j;           j = -1;           F[i]--;         }         if (n & (1 << i)) {           if (F[i]) {             F[i]--;           } else {             if (j == -1) j = i;           }         }         F[i + 1] += F[i] / 2;       }       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       if (i == 1)         printf( 14 n );       else {         if (i == 2)           printf( 16 n );         else           printf( %lld n , (1LL * i * (i + 1) * (i + 1) - (i - 1)));       }     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast,no-stack-protector )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx )   #pragma GCC target( avx,tune=native )   using namespace std;   const int inf = 0x3f3f3f3f;   const double eps = 1e-6;   const int mod = 1000000007;   inline string getstr(string &s, int l, int r) {     string ret =   ;     for (int i = l; i <= r; i++) ret.push_back(s[i]);     return ret;   }   int modpow(int x, int y, int md = mod) {     if (y == 0) return 1;     int ret = modpow(x, y >> 1, md);     ret = (long long)ret * ret % md;     if (y & 1) ret = (long long)ret * x % md;     return ret;   }   int n, m, t, ans;   int a[10001];   int cnt[10005];   int in[11];   inline int calc(int use) {     int ret = 1;     for (int i = 1; i <= use; i++) {       ret = (long long)ret * cnt[in[i]] % mod;     }     return ret;   }   void dfs(int use, int last, bitset<128> now) {     ans += calc(use);     if (ans >= mod) ans -= mod;     for (int i = last + 1; i <= m / 2; i++) {       if (cnt[i]) {         if (now[i] || now[m - i]) continue;         bitset<128> bs =             now | (now >> i) | (now << i) | (now >> (m - i)) | (now << (m - i));         in[use + 1] = i;         dfs(use + 1, i, bs);       }     }   }   bitset<128> bs;   int main() {     bs.reset();     bs.set(0);     cin >> n >> m >> t;     for (int i = 0; i < t; i++) cin >> a[i], cnt[min(m - a[i] % m, a[i] % m)]++;     dfs(0, 0, bs);     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long t;     cin >> t;     while (t--) {       int n;       string a, b;       cin >> n >> a >> b;       vector<int> res;       for (int i = 0; i < n; i++) {         if (a[i] != b[i]) {           if (i != 0) res.push_back(i + 1);           res.push_back(1);           if (i != 0) res.push_back(i + 1);         }       }       cout << res.size() <<    ;       for (int x : res) {         cout << x <<    ;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 2 * 1e5 + 5;   const int INF = 1e9;   const int MOD = 1e9 + 7;   int N, K, A[MAX], table[20][MAX];   int main() {     cin >> N >> K;     for (int i = 0; i < (N); ++i) cin >> A[i];     for (int i = 0; i < (N); ++i) table[0][i] = A[i];     vector<int> b(N, 0);     for (int i = 0; i < (N); ++i) {       for (int j = 0; j < (19); ++j) {         table[j + 1][i] = table[j][i] / 2;         b[i]++;         if (table[j + 1][i] == 0) break;       }     }     map<int, int> m, cost;     int ans = INF;     for (int i = 0; i < (20); ++i) {       for (int j = 0; j < (N); ++j) {         if (b[j] < i) continue;         int num = table[i][j];         m[num]++;         cost[num] += i;         if (m[num] >= K) ans = min(ans, cost[num]);       }     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int r, g, b, ck, maximum = 0, temp;     scanf( %d %d %d , &r, &g, &b);     if (r % 2 == 1) r++;     r = r / 2;     if (g % 2 == 1) g++;     g = g / 2;     if (b % 2 == 1) b++;     b = b / 2;     temp = 27 + r * 3;     if (temp > maximum) maximum = temp;     temp = 28 + g * 3;     if (temp > maximum) maximum = temp;     temp = 29 + b * 3;     if (temp > maximum) maximum = temp;     printf( %d n , maximum);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     for (int i = 0; i < s.size(); i++) {       for (char a =  a ; a <=  z ; a++) {         string t =   ;         for (int j = 0; j < i; j++) {           t += s[j];         }         t += a;         for (int j = i; j < s.size(); j++) {           t += s[j];         }         string r = t;         reverse(t.begin(), t.end());         if (r == t) {           cout << r;           return 0;         }       }     }     for (char a =  a ; a <=  z ; a++) {       string t = s;       t += a;       string r = t;       reverse(t.begin(), t.end());       if (r == t) {         cout << r;         return 0;       }     }     cout <<  NA ;   }


#include <bits/stdc++.h>   using namespace std;   inline void output(long long int x) {     if (x % 2)       cout << x / 2 <<   n ;     else       cout << x / 2 - 1 <<   n ;     return;   }   long long int N, K;   int main() {     cin >> N >> K;     if (N >= K)       output(K);     else {       if ((N << 1) - 1 < K)         cout << 0 <<   n ;       else         output(N - (K - N) + 2);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 5005;   const int mod = 1000000007;   int n, a, b, k;   int dp[N][N];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> a >> b >> k;     for (long long i = 1; i <= n; ++i) {       dp[0][i] = i;     }     for (int trip = 1; trip <= k; trip++) {       for (int floor = 1; floor <= n; floor++) {         int d = abs(b - floor);         dp[trip][floor] = dp[trip - 1][min(n, floor + d - 1)] -                           dp[trip - 1][max(0, floor - d)] - dp[trip - 1][floor] +                           dp[trip - 1][floor - 1];         while (dp[trip][floor] < 0) dp[trip][floor] += mod;         dp[trip][floor] %= mod;         dp[trip][floor] = (dp[trip][floor] + dp[trip][floor - 1]) % mod;       }     }     cout << (dp[k][a] - dp[k][a - 1] + mod) % mod;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int mdc(int a, int b) { return !b ? a : mdc(b, a % b); }   int main() {     int n, m, x, y, a, b, p, d, x1, y1, x2, y2;     cin >> n >> m >> x >> y >> a >> b;     d = mdc(a, b);     a /= d;     b /= d;     p = min(n / a, m / b);     a *= p;     b *= p;     if (x + a / 2 > n) {       x2 = n;       x1 = n - a;     } else if (x - a / 2 < 0) {       x1 = 0;       x2 = a;     } else {       x2 = x + a / 2;       x1 = x2 - a;     }     if (y + b / 2 > m) {       y2 = m;       y1 = m - b;     } else if (y - b / 2 < 0) {       y1 = 0;       y2 = b;     } else {       y2 = y + b / 2;       y1 = y2 - b;     }     cout << x1 <<     << y1 <<     << x2 <<     << y2 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   const long long N = 3e5 + 10;   long long pow(long long a, long long b, long long m) {     long long ans = 1;     while (b) {       if (b & 1) ans = (ans * a) % m;       b /= 2;       a = (a * a) % m;     }     return ans;   }   long long y[5], m[5];   long long h, a, d;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     for (long long i = 1; i <= 3; i++) {       cin >> y[i];     }     for (long long i = 1; i <= 3; i++) {       cin >> m[i];     }     cin >> h >> a >> d;     long long mn = 1e18;     for (long long i = y[1]; i <= 1000; i++) {       for (long long j = y[2]; j <= 201; j++) {         for (long long k = y[3]; k <= 201; k++) {           long long z = (i - y[1]) * h + (j - y[2]) * a + (k - y[3]) * d;           if (j <= m[3]) {             continue;           } else if (k >= m[2]) {             mn = min(mn, z);           } else {             long long sec_to_take_yong =                 (m[1] / (j - m[3])) + (m[1] % (j - m[3]) != 0);             long long sec_to_take_m = (i / (m[2] - k)) + (i % (m[2] - k) != 0);             if (sec_to_take_yong < sec_to_take_m) mn = min(mn, z);           }         }       }     }     cout << mn << endl;   }


#include <bits/stdc++.h>   using namespace std;   float distance(long long x, long long y, long long a, long long b) {     return sqrt((x - y) * (x - y) + (a - b) * (a - b));   }   int32_t main() {     float n, x1, y1, x2, y2;     cin >> n >> x1 >> y1 >> x2 >> y2;     if ((x1 == x2 && (x1 == n || x1 == 0)) ||         (y1 == y2 && (y1 == 0 || y2 == n))) {       cout << abs(x1 - x2) + abs(y1 - y2);       return 0;     }     long long path1 = 0, path2 = 0;     if (abs(x1 - x2) == n) {       path1 = 2 * n - (y1 + y2);       path2 = y1 + y2;       cout << (path1 > path2 ? path2 : path1) + n;       return 0;     }     if (abs(y1 - y2) == n) {       path1 = 2 * n - (x1 + x2);       path2 = x1 + x2;       cout << (path1 > path2 ? path2 : path1) + n;       return 0;     }     cout << abs(x1 - x2) + abs(y2 - y1);   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 998244353;   const int maxn = 1e3 + 10;   int n, m, mp[maxn][maxn], cnt, dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0};   bool vis[maxn][maxn];   vector<int> ans;   void dfs(int x, int y) {     if (vis[x][y] || x <= 0 || x > n || y <= 0 || y > m) return;     vis[x][y] = 1, ++cnt;     for (int i = 0; i < 4; ++i)       if (!(mp[x][y] & (1 << i))) dfs(x + dx[i], y + dy[i]);   }   void solve() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j) scanf( %d , &mp[i][j]);     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j)         if (!vis[i][j]) cnt = 0, dfs(i, j), ans.push_back(cnt);     sort(ans.begin(), ans.end());     for (int i = ans.size() - 1; ~i; --i) printf( %d  , ans[i]);   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, t;   char v[500010];   vector<pair<int, int> > inter;   int memo[500010];   int calcula(int ind) {     int res = 0;     int x = ind;     int ini = 0;     int fim = 0;     bool erro = false;     int lh = -1;     inter.clear();     for (int i = 0; i < (n); i++) {       if (v[i] ==  H ) {         x--;         lh = i;       } else if (v[i] ==  S ) {         x++;       }       if (x == -1 && !erro) {         ini = i;         erro = true;       }       if (x == 0 && erro) {         if (i < n && v[i + 1] ==  H ) continue;         inter.push_back(make_pair(ini, i));         erro = false;       }     }     if (x < 0) return false;     if ((int)inter.size() == 0) {       if (lh < t) return true;       return false;     }     int i = 0;     res = inter[0].first;     fim = max(lh, inter[(int)inter.size() - 1].second);     memo[inter.size()] = 0;     int next;     for (int i = (int)inter.size() - 1; i >= 0; i--) {       if (i + 1 < (int)inter.size()) {         next = inter[i + 1].first - 1;       } else {         next = fim;       }       memo[i] = min((inter[i].second - inter[i].first + 1) * 3 - 2 + memo[i + 1] +                         (next - inter[i].second),                     (fim - inter[i].first + 1) * 2 - 1);     }     res += memo[0];     if (res <= t) return true;     return false;   }   int main() {     scanf( %d %d , &n, &t);     for (int i = 0; i < (n); i++) {       scanf(  %c , &v[i]);     }     int inicio = 0;     int fim = n;     int res = -1;     while (1) {       if (inicio > fim) break;       int meio = (inicio + fim) / 2;       if (calcula(meio)) {         res = meio;         fim = meio - 1;       } else {         inicio = meio + 1;       }     }     printf( %d n , res);     return 0;   }


#include <bits/stdc++.h>   int main() {     int t = 1, i, n;     scanf( %d , &n);     for (i = 1; i <= n - 1; i++) {       t = (t + i - 1) % n + 1;       printf( %d  , t);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Point {     int x, y;     Point(int x = 0, int y = 0) : x(x), y(y) {}     Point operator-(const Point z) const { return Point(x - z.x, y - z.y); }     friend long long Cross(Point a, Point b) {       return 1ll * a.x * b.y - 1ll * a.y * b.x;     }   } a[2010], b[2010];   bool f[2010];   char ch[2010];   int main(int argc, char const *argv[]) {     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i].x >> a[i].y;     cin >> ch;     int p = 0;     for (int i = 1; i < n; i++)       if (a[i].y < a[p].y || a[i].y == a[p].y && a[i].x < a[p].x) p = i;     f[p] = 1;     cout << p + 1 <<    ;     for (int i = 0; !i || ch[i - 1]; i++) {       int x = -1;       for (int j = 0; j < n; j++)         if (!f[j]) {           if (x == -1)             x = j;           else if (ch[i] ==  L )             if (Cross(a[j] - a[p], a[x] - a[p]) > 0)               x = j;             else               ;           else if (Cross(a[j] - a[p], a[x] - a[p]) < 0)             x = j;         }       cout << x + 1 <<    ;       f[x] = 1;       p = x;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   typedef pair<int, int> pii;   typedef long long ll;   typedef unsigned long long ull;   template <typename T>   bool umax(T& a, const T& b) {     return b <= a ? false : (a = b, true);   }   template <typename T>   bool umin(T& a, const T& b) {     return b >= a ? false : (a = b, true);   }   template <typename T>   void V2A(T a[], const vector<T>& b) {     for (int i = 0; i < b.size(); i++) a[i] = b[i];   }   template <typename T>   void A2V(vector<T>& a, const T b[]) {     for (int i = 0; i < a.size(); i++) a[i] = b[i];   }   const double PI = acos(-1.0);   const int INF = 1e9 + 7;   const int maxn = 2e3 + 7;   bool plot[maxn][maxn];   int r[maxn][maxn];   int n, m, k;   int check(int row, int col) {     if (col >= 0 && col < m) return true;     return false;   }   int main() {     cin >> n >> m >> k;     for (int i = 0; i < k; i++) {       int x, y;       scanf( %d%d , &x, &y);       x--;       y--;       if (!plot[x][y]) {         printf( %d %d n , x + 1, y + 1);         plot[x][y] = true;         continue;       }       for (int d = 1; d <= 3; d++) {         int Max = min(n, x + d + 1);         for (int row = max(0, x - d); row < Max; row++) {           int t = d - abs(x - row), col1 = y - t, col2 = y + t;           if (check(row, col1)) umax(r[x][y], r[row][col1] - d);           if (check(row, col2)) umax(r[x][y], r[row][col2] - d);         }       }       for (int d = r[x][y] + 1;; d++) {         int Max = min(n, x + d + 1);         bool ok = false;         for (int row = max(0, x - d); row < Max; row++) {           int t = d - abs(x - row), col1 = y - t, col2 = y + t;           if (check(row, col1) && !plot[row][col1]) {             ok = true;             printf( %d %d n , row + 1, col1 + 1);             plot[row][col1] = true;             break;           }           if (check(row, col2) && !plot[row][col2]) {             ok = true;             printf( %d %d n , row + 1, col2 + 1);             plot[row][col2] = true;             break;           }         }         if (ok) {           r[x][y] = d - 1;           break;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int p, q;   } a[1009];   bool cmp(node a, node b) {     if (a.p != b.p) return a.p < b.p;     if (a.p == b.p) return a.q > b.q;   }   int main() {     int m, n, i;     scanf( %d%d , &m, &n);     for (i = 1; i <= n; i++) {       scanf( %d%d , &a[i].p, &a[i].q);     }     sort(a + 1, a + 1 + n, cmp);     int sum = m;     for (i = 1; i <= n; i++) {       if (sum > a[i].p)         sum += a[i].q;       else         break;     }     if (i == n + 1)       printf( YES n );     else       printf( NO n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     int len = s.size();     cout << s[0];     int flag = 0;     for (int i = 1; i < len; i++) {       if (s[i] ==  d  && s[i + 1] ==  o  && s[i + 2] ==  t  &&           (i + 2 != len - 1)) {         cout <<  . ;         i += 2;       } else if (s[i] ==  a  && s[i + 1] ==  t  && flag == 0) {         cout <<  @ ;         i++;         flag = 1;       } else         cout << s[i];     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     cerr.tie(nullptr);     long long n, k, a, z = 0;     cin >> n >> k;     vector<int> x(n), c(n);     for (int i = 0; i < n; i++) cin >> x[i];     cin >> a;     for (int i = 0; i < n; i++) cin >> c[i];     multiset<int> ms;     for (int i = 0; i < n; i++) {       ms.insert(c[i]);       while (k < x[i]) {         if (ms.size() == 0) {           cout <<  -1 n ;           return 0;         } else {           k += a;           z += *ms.begin();           ms.erase(ms.begin());         }       }     }     cout << z <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 * 2;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       string s;       cin >> s;       int l = 0, r = n - 1;       while (l <= n && s[l] ==  0 ) l++;       while (r >= 0 && s[r] ==  0 ) r--;       int ans;       if (l > r)         ans = n;       else         ans = max(r + 1, n - l) * 2;       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   #pragma comment(linker,  /stack:200000000 )   #pragma GCC optimize( Ofast )   #pragma GCC target(         sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native )   #pragma GCC optimize( -ffloat-store )   const long long INF = 0xFFFFFFFFFFFFFFFL;   const long long N = 1e9 + 7;   vector<long long> pri;   void yo(int n) {     bool prime[n + 1];     memset(prime, true, sizeof(prime));     for (int p = 2; p * p <= n; p++) {       if (prime[p] == true) {         for (int i = p * p; i <= n; i += p) prime[i] = false;       }     }     for (int p = 2; p <= n; p++)       if (prime[p]) pri.push_back(p);   }   void solve() {     long long n;     cin >> n;     for (long long i = 1; i < 1001; i++) {       if (!binary_search((pri).begin(), (pri).end(), n * i + 1)) {         cout << i;         return;       }     }   }   int32_t main(int32_t argc, char **argv) {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     yo(1000003);     long long t;     t = 1;     while (t--) solve();     cerr <<   n           <<  Time :   << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC          <<  ms n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     scanf( %d%d , &n, &k);     int mat[n + 1][n + 1];     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) mat[i][j] = 0;     }     int br = 0;     for (int i = 1; i <= n; i += 2) {       for (int j = 1, x = i; j <= n && x <= n; j++, x++) {         mat[x][j] = 1;         br++;       }     }     for (int i = 3; i <= n; i += 2) {       for (int h = 1, j = i; h <= n && j <= n; h++, j++) {         mat[h][j] = 1;         br++;       }     }     if (br < k) {       printf( NO );       return 0;     }     printf( YES n );     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         if (k > 0) {           if (mat[i][j]) {             printf( L );             k--;           } else             printf( S );         } else           printf( S );       }       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a = 1;     int b = 2;     bool ok = true;     int k;     for (int i = 0; i < n; i++) {       cin >> k;       if (k != a && k != b) {         ok = false;       } else {         if (b == 3 && a == 1) {           a = min(2, k);           b = max(2, k);         } else if (b == 2 && a == 1) {           a = min(3, k);           b = max(3, k);         } else if (b == 3 && a == 2) {           a = min(1, k);           b = max(1, k);         }       }     }     if (ok) {       cout <<  YES ;     } else {       cout <<  NO ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 5, M = 20;   int n, m, a[N], b[M], ans[M];   set<vector<int> > use[M + 1];   void find(int y, vector<int>& current) {     if (y == m) {       for (int i = 0; i < n; i++)         if (a[i] != current[i]) return;       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) cout <<  .* [(bool)(ans[j] & (1 << i))];         cout << endl;       }       exit(0);     }     if (use[y].find(current) != use[y].end()) return;     for (int i = 0; i < (1 << n); i++) {       if ((((((((i << 1) | i) ^ i) >> 1) >> 0)) & 1) +               (((((((i << 1) | i) ^ i) >> 1) >> 1)) & 1) +               (((((((i << 1) | i) ^ i) >> 1) >> 2)) & 1) +               (((((((i << 1) | i) ^ i) >> 1) >> 3)) & 1) +               (((((((i << 1) | i) ^ i) >> 1) >> 4)) & 1) !=           b[y])         continue;       ans[y] = i;       vector<int> newCur = current;       bool flag = true;       for (int j = 0; j < n; j++) {         if (ans[y] & (1 << j) && (!y || !(ans[y - 1] & (1 << j)))) newCur[j]++;         if (newCur[j] > a[j] || newCur[j] < a[j] - ((m - y) / 2)) {           flag = false;           break;         }       }       if (flag) find(y + 1, newCur);     }     use[y].insert(current);   }   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < m; i++) cin >> b[i];     vector<int> c(n + 1, 0);     find(0, c);   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1100;   int n;   char s[maxn + 10];   int data[maxn + 10];   int dp[maxn + 10][2];   int main() {     cin >> n;     getchar();     gets(s);     int i;     for (i = 0; s[i]; i++) data[i] = s[i] -  0 ;     if (i == 1)       printf( 0 n );     else {       int ans1 = 0, ans2 = 0;       for (int j = 0; j < i; j++) {         if ((j & 1) != data[j]) ans1++;       }       for (int j = 0; j < i; j++) {         if ((j & 1) == data[j]) ans2++;       }       printf( %d n , min(ans1, ans2));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200005;   const int D = 30;   struct node {     int a, b;   };   vector<int> road[N], h[N];   vector<node> q[N];   int n, m, t, fa[N][D + 1], w[N][D + 1], head[N], sum[N], ans[N], g[N];   int cnt, tot, dep[N], id[N], size[N], c[N];   int lowbit(int x) { return x & -x; }   void update(int x) {     for (int i = x; i <= n; i += lowbit(i)) {       c[i] += 1;     }   }   int check(int x) {     int tmp = 0;     for (int i = x; i >= 1; i -= lowbit(i)) {       tmp += c[i];     }     return tmp;   }   struct edge {     int to, nxt;   } e[N * 2];   void add(int u, int v) {     cnt++;     e[cnt].nxt = head[u];     e[cnt].to = v;     head[u] = cnt;   }   void dfs1(int u, int deep) {     dep[u] = deep;     id[u] = ++tot;     size[u] = 1;     for (int i = head[u]; i; i = e[i].nxt) {       int v = e[i].to;       if (v == fa[u][0]) continue;       dfs1(v, deep + 1);       size[u] += size[v];     }   }   void dfs2(int u) {     g[u] = u;     for (int i = 0; i < h[u].size(); i++) {       if (dep[h[u][i]] < dep[g[u]]) g[u] = h[u][i];     }     for (int i = head[u]; i; i = e[i].nxt) {       int v = e[i].to;       if (v == fa[u][0]) continue;       dfs2(v);       if (dep[g[v]] < dep[g[u]]) g[u] = g[v];     }     w[u][0] = g[u];   }   int lca(int x, int y) {     if (dep[x] < dep[y]) swap(x, y);     for (int i = D; i >= 0; i--) {       if (dep[fa[x][i]] >= dep[y]) x = fa[x][i];     }     if (x == y) return x;     for (int i = D; i >= 0; i--) {       if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];     }     return fa[x][0];   }   node work(int x, int LCA) {     if (x == LCA) return (node){x, 0};     int tmp = 0;     for (int i = D; i >= 0; i--)       if (dep[w[x][i]] > dep[LCA]) x = w[x][i], tmp += (1 << i);     if (w[x][0] == x) return (node){x, -1};     return (node){x, tmp};   }   int main() {     scanf( %d , &n);     for (int i = 2; i <= n; i++) {       scanf( %d , &fa[i][0]);       add(fa[i][0], i);       add(i, fa[i][0]);     }     dfs1(1, 1);     for (int i = 1; i <= D; i++)       for (int j = 1; j <= n; j++) {         fa[j][i] = fa[fa[j][i - 1]][i - 1];       }     scanf( %d , &m);     for (int i = 1; i <= m; i++) {       int x, y;       scanf( %d%d , &x, &y);       int c = lca(x, y);       if (id[x] > id[y]) swap(x, y);       road[id[x]].push_back(id[y]);       if (dep[c] < dep[x]) h[x].push_back(c);       if (dep[c] < dep[y]) h[y].push_back(c);     }     dfs2(1);     for (int i = 1; i <= n; i++) {     }     for (int i = 1; i <= D; i++)       for (int j = 1; j <= n; j++) {         w[j][i] = w[w[j][i - 1]][i - 1];       }     scanf( %d , &t);     for (int i = 1; i <= t; i++) {       int x, y;       scanf( %d%d , &x, &y);       int c = lca(x, y);       node A = work(x, c);       node B = work(y, c);       if (A.b == -1 || B.b == -1) {         ans[i] = -1;         continue;       }       if (x == c || y == c) {         ans[i] = A.b + B.b + 1;         continue;       }       ans[i] = A.b + B.b + 2;       x = A.a;       y = B.a;       if (id[x] > id[y]) swap(x, y);       q[id[x] - 1].push_back((node){y, -i});       q[id[x] + size[x] - 1].push_back((node){y, i});     }     for (int i = 1; i <= n; i++) {       for (int j = 0; j < road[i].size(); j++) update(road[i][j]);       for (int j = 0; j < q[i].size(); j++) {         int num = (q[i][j].b > 0 ? 1 : -1);         int x = q[i][j].a;         int y = q[i][j].b * num;         sum[y] += num * (check(id[x] + size[x] - 1) - check(id[x] - 1));       }     }     for (int i = 1; i <= t; i++) {       printf( %d n , ans[i] - (sum[i] > 0));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct prog {     int i;     int cmp;   } a[100005];   int ans[100005];   bool cmp(prog a, prog b) {     if (a.cmp == b.cmp)       return a.i < b.i;     else       return a.cmp < b.cmp;   }   int main() {     int n, m;     while (cin >> n >> m) {       priority_queue<prog> q;       int i, j;       for (i = 0; i < m; i++) {         a[i].i = i + 1;         a[i].cmp = (int)fabs((m + 1) / 2.0 - i - 1);       }       sort(a, a + m, cmp);       for (i = 0; i < n; i++) {         int k = i % m;         cout << a[k].i << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxN = 3e5 + 11, maxlg = 2e1;   typedef int i_N[maxN];   typedef long long l_N[maxN];   int N, M, Q, par[maxN][maxlg], umax[maxN][maxlg], umin[maxN][maxlg];   vector<int> adj[maxN], pos[maxN];   vector<pair<int, int> > query[maxN];   pair<int, int> edge[maxN];   i_N P, depth, mark, F;   l_N cnt, sum, mus, ans;   int lg(int x) {     for (int i = maxlg; i; i--)       if ((1 << i) <= x) return i;     return (1 - 1);   }   int get_root(int u) {     if (P[u] < (1 - 1)) return u;     return P[u] = get_root(P[u]);   }   void join(int u, int v) {     if (P[u] > P[v]) swap(P[u], P[v]);     P[u] += P[v];     P[v] = u;   }   void dfs(int u) {     for (int i = 1, x = lg(depth[u]); i <= x; i++) {       par[u][i] = par[par[u][i - 1]][i - 1];       umax[u][i] = max(umax[u][i - 1], umax[par[u][i - 1]][i - 1]);       umin[u][i] = min(umin[u][i - 1], umin[par[u][i - 1]][i - 1]);     }     for (int i = (1 - 1); i < (int)adj[u].size(); i++) {       int v = adj[u][i];       if (par[v][(1 - 1)]) continue;       par[v][(1 - 1)] = u;       umax[v][(1 - 1)] = max(u, v);       umin[v][(1 - 1)] = min(u, v);       depth[v] = depth[u] + 1;       dfs(v);     }   }   void get(int u, int v, int& x, int& y) {     if (depth[u] < depth[v]) swap(u, v);     for (int i = lg(depth[u]); i >= (1 - 1); i--)       if (depth[u] - (1 << i) >= depth[v]) {         x = min(x, umin[u][i]);         y = max(y, umax[u][i]);         u = par[u][i];       }     for (int i = lg(depth[u]); i >= (1 - 1); i--)       if (par[u][i] != par[v][i]) {         x = min(x, min(umin[u][i], umin[v][i]));         y = max(y, max(umax[u][i], umax[v][i]));         u = par[u][i];         v = par[v][i];       }     if (u != v) {       x = min(x, min(umin[u][(1 - 1)], umin[v][(1 - 1)]));       y = max(y, max(umax[u][(1 - 1)], umax[v][(1 - 1)]));     }   }   void update(long long* bit, int x, int v) {     for (int i = x; i <= N; i |= i + 1) bit[i] += v;   }   long long get_bit(long long* bit, int x) {     long long res = (1 - 1);     for (int i = x; i >= 1; i = (i & (i + 1)) - 1) res += bit[i];     return res;   }   long long get_bit(long long* bit, int l, int r) {     return get_bit(bit, r) - get_bit(bit, l - 1);   }   int main() {     scanf( %d %d n , &N, &M);     for (int i = 1; i <= M; i++) {       int u, v;       scanf( %d %d n , &u, &v);       edge[i] = pair<int, int>(u, v);     }     memset(P, -1, sizeof(P));     for (int i = 1; i <= M; i++) {       int u = edge[i].first, v = edge[i].second;       int r1 = get_root(u), r2 = get_root(v);       if (r1 != r2) {         mark[i] = 1;         join(r1, r2);         adj[u].push_back(v);         adj[v].push_back(u);       }     }     for (int u = 1; u <= N; u++)       if (!par[u][(1 - 1)]) {         par[u][(1 - 1)] = -1;         dfs(u);       }     for (int i = 1; i <= M; i++)       if (!mark[i]) {         int u = edge[i].first, v = edge[i].second;         int x = 1e9, y = (1 - 1);         get(u, v, x, y);         F[x] = y;       }     F[N + 1] = N + 1;     for (int i = N; i >= 1; i--) {       if (F[i] == (1 - 1))         F[i] = F[i + 1];       else         F[i] = min(F[i], F[i + 1]);       pos[F[i]].push_back(i);     }     scanf( %d n , &Q);     for (int i = 1; i <= Q; i++) {       int l, r;       scanf( %d %d n , &l, &r);       query[r].push_back(pair<int, int>(l, i));     }     for (int i = 1; i <= N; i++) {       update(cnt, i, 1);       update(sum, i, i - 1);       for (int j = (1 - 1); j < (int)pos[i].size(); j++) {         int p = pos[i][j];         update(cnt, p, -1);         update(sum, p, 1 - p);         update(mus, p, i - p);       }       for (int j = (1 - 1); j < (int)query[i].size(); j++) {         int r = i, l = query[i][j].first, id = query[i][j].second;         long long C = get_bit(cnt, l, r);         long long S = get_bit(sum, l, r);         long long T = get_bit(mus, l, r);         ans[id] += C * i - S + T;       }     }     for (int i = 1; i <= Q; i++) printf( %I64d n , ans[i]);   }


#include <bits/stdc++.h>   using namespace std;   template <class T, class L>   bool smax(T& x, L y) {     return x < y ? (x = y, 1) : 0;   }   template <class T, class L>   bool smin(T& x, L y) {     return x > y ? (x = y, 1) : 0;   }   const int maxn = 16;   int n, a[maxn], b[maxn][maxn], ex, x[maxn];   bool mark[maxn];   vector<vector<int> > all;   vector<int> todo;   int fond(int x) {     for (int i = 0; i < n * n; i++)       if (!mark[i] && a[i] == x) return i;     return -1;   }   void back(int s = 0) {     if (s == n) {       for (int i = 0, sum = 0; i < n; i++, sum = 0) {         for (int j = 0; j < n; j++) sum += b[j][i];         if (sum != ex) return;       }       {         int sum = 0;         for (int i = 0; i < n; i++) sum += b[i][i];         if (sum != ex) return;       }       {         int sum = 0;         for (int i = 0; i < n; i++) sum += b[i][n - i - 1];         if (sum != ex) return;       }       for (int i = 0; i < n; i++, cout <<   n )         for (int j = 0; j < n; j++) cout << b[i][j] <<    ;       exit(0);     }     if (s == n - 1) {       fill(b[n - 1], b[n - 1] + n, ex);       for (int i = 0; i < n - 1; i++)         for (int j = 0; j < n; j++) b[n - 1][j] -= b[i][j];       int i = 0;       for (int tmp; i < n; i++)         if (~(tmp = fond(b[n - 1][i])))           mark[tmp] = 1, todo.push_back(tmp);         else           break;       if (i == n) back(n);       for (auto x : todo) mark[x] = 0;       todo.clear();     }     for (auto v : all) {       bool reval = 1;       for (auto x : v)         if (mark[x]) {           reval = 0;           break;         }       if (!reval) continue;       do {         for (int i = 0; i < n; i++) b[s][i] = a[v[i]], mark[v[i]] = 1;         back(s + 1);         for (int i = 0; i < n; i++) mark[v[i]] = 0;       } while (next_permutation(v.begin(), v.end()));     }   }   void tof(int idx) {     if (idx == 4 && x[0] + x[1] + x[2] + x[3] != ex) return;     if (idx == 8 && x[4] + x[5] + x[6] + x[7] != ex) return;     if (idx == 12 && x[8] + x[9] + x[10] + x[11] != ex) return;     if (idx == 16 && x[12] + x[13] + x[14] + x[15] != ex) return;     if (idx == 13 && x[0] + x[4] + x[8] + x[12] != ex) return;     if (idx == 14 && x[1] + x[5] + x[9] + x[13] != ex) return;     if (idx == 15 && x[2] + x[6] + x[10] + x[14] != ex) return;     if (idx == 16 && x[3] + x[7] + x[11] + x[15] != ex) return;     if (idx == 13 && x[3] + x[6] + x[9] + x[12] != ex) return;     if (idx == 16 && x[0] + x[5] + x[10] + x[15] != ex) return;     if (idx == 16) {       for (int i = 0; i < n; i++, cout <<   n )         for (int j = 0; j < n; j++) cout << x[i * 4 + j] <<    ;       exit(0);     }     for (int i = 0; !(i >> 4); ++i)       if (!mark[i]) {         x[idx] = a[i], mark[i] = 1;         tof(idx + 1);         mark[i] = 0;       }   }   int main() {     srand(time(0));     ios::sync_with_stdio(0), cin.tie();     cin >> n;     for (int i = 0; i < n * n; i++) cin >> a[i], ex += a[i];     random_shuffle(a, a + n * n);     ex /= n;     cout << ex <<   n ;     if (n < 4) {       for (int i = 0; i < 1 << n * n; i++)         if (__builtin_popcount(i) == n) {           int sum = 0;           for (int j = 0; j < n * n; j++) sum += (i >> j & 1) * a[j];           if (sum == ex) {             all.push_back({});             for (int j = 0; j < n * n; j++)               if (i >> j & 1) all.back().push_back(j);             sort(all.back().begin(), all.back().end());           }         }       return back(), 0;     }     tof(0);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   set<pair<pair<int, int>, pair<int, int> > > s;   vector<int> ed[2][300000];   int n, m, k;   int a[300000], b[300000];   long long ans[300000];   bool add(int x, int y, int dx, int dy) {     pair<pair<int, int>, pair<int, int> > g =         make_pair(make_pair(x, y), make_pair(dx, dy));     if (s.find(g) != s.end()) return false;     s.insert(g);     return true;   }   void go(int x, int y, int dx, int dy, long long t) {     int tt = 1;     int d = x - y + m;     if (dx - dy != 0) {       tt = 0;       d = x + y;     }     bool f = add(x, y, dx, dy);     if (!f) return;     for (int j = 0; j < ed[tt][d].size(); j++) {       int kx = a[ed[tt][d][j]];       int ky = b[ed[tt][d][j]];       kx = kx - x;       ky = ky - y;       kx = kx / dx;       ky = ky / dy;       int g = ed[tt][d][j];       if (ans[g] != -1) continue;       ans[g] = t + min(kx, ky);     }     if (dx == 1 && dy == 1) {       int v = min(n - x, m - y);       x += v;       y += v;       if (x == n && y == m) return;       if (y == m)         go(x, y, 1, -1, t + v);       else         go(x, y, -1, 1, t + v);     }     if (dx == -1 && dy == -1) {       int v = min(x, y);       x -= v;       y -= v;       if (x == 0 && y == 0) return;       if (y == 0)         go(x, y, -1, 1, t + v);       else         go(x, y, 1, -1, t + v);     }     if (dx == 1 && dy == -1) {       int v = min(n - x, y);       x += v;       y -= v;       if (x == n && y == 0) return;       if (y == 0)         go(x, y, 1, 1, t + v);       else         go(x, y, -1, -1, t + v);     }     if (dx == -1 && dy == 1) {       int v = min(x, m - y);       x -= v;       y += v;       if (x == 0 && y == m) return;       if (x == 0)         go(x, y, 1, 1, t + v);       else         go(x, y, -1, -1, t + v);     }   }   int main() {     ios_base::sync_with_stdio(0);     cin >> n >> m >> k;     for (int i = 1; i <= k; i++) ans[i] = -1;     for (int i = 1; i <= k; i++) {       cin >> a[i] >> b[i];       ed[0][a[i] + b[i]].push_back(i);       ed[1][a[i] - b[i] + m].push_back(i);     }     go(0, 0, 1, 1, 0);     for (int i = 1; i <= k; i++) cout << ans[i] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long read() {     char ch = getchar();     long long x = 0, f = 1;     while (ch <  0  || ch >  9 ) f = ch ==  -  ? -1 : 1, ch = getchar();     while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar();     return x * f;   }   int main(int argc, char *argv[]) {     ios::sync_with_stdio(false);     cin.tie(0);     long long base = (1LL << 7) - 1;     long long res = 0, cur;     printf( ? );     for (long long i = (1); i <= (100); i += (1)) printf(  %lld , i);     putchar(  n );     fflush(stdout);     cur = read();     res += (cur >> 7) << 7;     printf( ? );     for (long long i = (1); i <= (100); i += (1)) printf(  %lld , i << 7);     putchar(  n );     fflush(stdout);     cur = read();     res += cur & base;     printf( ! %lld n , res);     fflush(stdout);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 205, MAXL = 1 << 20;   char qq[MAXL];   char s[MAX][MAX];   pair<int, int> kamo[MAX][MAX];   vector<pair<int, int> > R[MAX][MAX];   int indeg[MAX][MAX];   int cik[MAX][MAX];   int imacik[MAX * MAX][10];   int brred, brstup;   int pr[10], ps[10];   int bio[MAX][MAX];   int dokle[MAX][MAX];   int imaq[MAXL][10];   int brc = 1;   int Vani(int red, int stup) {     return red < 0 || red >= brred || stup < 0 || stup >= brstup;   }   void Osisaj() {     queue<pair<int, int> > Q;     for (int i = 0; i < brred; i++)       for (int j = 0; j < brstup; j++)         if (!indeg[i][j]) Q.push(pair<int, int>(i, j));     for (; !Q.empty();) {       pair<int, int> tmp = Q.front();       bio[tmp.first][tmp.second] = 1;       Q.pop();       pair<int, int> nn = kamo[tmp.first][tmp.second];       indeg[nn.first][nn.second]--;       if (!indeg[nn.first][nn.second]) Q.push(nn);     }     for (int i = 0; i < brred; i++) {       for (int j = 0; j < brstup; j++) {         if (!bio[i][j]) {           int sz = 0;           for (pair<int, int> tmp = pair<int, int>(i, j);                tmp != pair<int, int>(i, j) || !sz;                sz++, tmp = kamo[tmp.first][tmp.second]) {             bio[tmp.first][tmp.second] = 1;             cik[tmp.first][tmp.second] = brc;             imacik[brc][s[tmp.first][tmp.second] -  0 ] = 1;           }           brc++;         }       }     }   }   int Rijesi() {     queue<pair<int, int> > Q;     memset(dokle, 0, sizeof dokle);     int len = (int)strlen(qq);     for (int i = 0; i <= len; i++)       for (int j = 0; j < 10; j++) imaq[i][j] = 0;     for (int i = len - 1; i >= 0; i--) {       imaq[i][qq[i] -  0 ] = 1;       for (int j = 0; j < 10; j++) imaq[i][j] |= imaq[i + 1][j];     }     for (int i = 0; i < brred; i++)       for (int j = 0; j < brstup; j++) {         indeg[i][j] = (int)R[i][j].size();         if (!indeg[i][j]) Q.push(pair<int, int>(i, j));       }     for (; !Q.empty();) {       pair<int, int> tmp = Q.front();       Q.pop();       int nd = dokle[tmp.first][tmp.second];       if (s[tmp.first][tmp.second] == qq[nd]) nd++;       if (nd == len) return 1;       pair<int, int> nn = kamo[tmp.first][tmp.second];       dokle[nn.first][nn.second] = max(dokle[nn.first][nn.second], nd);       indeg[nn.first][nn.second]--;       if (!indeg[nn.first][nn.second]) Q.push(nn);     }     for (int i = 0; i < brred; i++) {       for (int j = 0; j < brstup; j++) {         if (cik[i][j]) {           int da = 1;           int indc = cik[i][j];           for (int k = 0; k < 10; k++)             if (!imacik[indc][k] && imaq[dokle[i][j]][k]) da = 0;           if (da) return 1;         }       }     }     return 0;   }   int main() {     int brq;     scanf( %d%d%d , &brred, &brstup, &brq);     for (int i = 0; i < brred; i++) scanf( %s , s[i]);     for (int i = 0; i < 10; i++) scanf( %d%d , &pr[i], &ps[i]);     for (int i = 0; i < brred; i++) {       for (int j = 0; j < brstup; j++) {         int nr = i + pr[s[i][j] -  0 ];         int ns = j + ps[s[i][j] -  0 ];         if (Vani(nr, ns))           kamo[i][j] = pair<int, int>(i, j);         else           kamo[i][j] = pair<int, int>(nr, ns);         indeg[kamo[i][j].first][kamo[i][j].second]++;         R[kamo[i][j].first][kamo[i][j].second].push_back(pair<int, int>(i, j));       }     }     Osisaj();     for (int i = 0; i < brq; i++) {       scanf( %s , qq);       printf( %s n , Rijesi() ?  YES  :  NO );     }     return 0;   }