#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   const int N = 1e6 + 5;   const int inf = 1 << 30;   const long long llf = 9e18;   long long powmod(long long a, long long b) {     long long res = 1;     a %= mod;     assert(b >= 0);     for (; b; b >>= 1) {       if (b & 1) res = res * a % mod;       a = a * a % mod;     }     return res;   }   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   struct node {     int x, y, time;     friend bool operator<(const node &a, const node &b) {       return a.time > b.time;     }   };   bool cmp(int x, int y) { return x < y; }   int pic[1010][1010];   int book[1010][1010];   int dx[4] = {0, 0, 1, -1};   int dy[4] = {1, -1, 0, 0};   int n, m, t;   bool check(int x, int y) {     bool f1 = false;     bool f2 = false;     for (register int i = 0; i <= 3; ++i) {       if (x + dx[i] < 0 || x + dx[i] > n - 1 || y + dy[i] < 0 ||           y + dy[i] > m - 1)         continue;       if (pic[x + dx[i]][y + dy[i]] != pic[x][y]) {         f2 = true;       } else {         f1 = true;         book[x][y] = 0;       }     }     if (f1 && !f2) return false;     if (f1 && f2) return true;     if (!f1 && f2) return false;   }   int main() {     cin >> n >> m >> t;     string s;     for (register int i = 0; i <= n - 1; ++i) {       cin >> s;       for (register int j = 0; j <= m - 1; ++j) {         pic[i][j] = s[j] -  0 ;       }     }     node now, re;     priority_queue<node> q;     for (register int i = 0; i <= n - 1; ++i) {       for (register int j = 0; j <= m - 1; ++j) {         book[i][j] = inf;         if (check(i, j)) {           now.x = i;           now.y = j;           now.time = 0;           q.push(now);         }       }     }     while (!q.empty()) {       re = q.top();       q.pop();       int x = re.x, y = re.y;       for (register int i = 0; i <= 3; ++i) {         if (x + dx[i] < 0 || x + dx[i] > n - 1 || y + dy[i] < 0 ||             y + dy[i] > m - 1)           continue;         if (pic[x + dx[i]][y + dy[i]] != pic[x][y]) {           now.x = x + dx[i];           now.y = y + dy[i];           now.time = re.time + 1;           if (book[now.x][now.y] > now.time) {             book[now.x][now.y] = now.time;             q.push(now);           }         }       }     }     long long x, y, p;     for (register int i = 1; i <= t; ++i) {       cin >> x >> y >> p;       x--;       y--;       if (p < book[x][y] || book[x][y] == inf)         cout << pic[x][y] << endl;       else         cout << (pic[x][y] + (p - 1ll * book[x][y]) % 2) % 2 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const long long int INF = (long long int)1e9 + 10;   const long long int INFLL = (long long int)1e18 + 10;   const long double EPS = 1e-8;   const long double EPSLD = 1e-18;   const long long int MOD = 1e9 + 7;   template <class T>   T &chmin(T &a, const T &b) {     return a = min(a, b);   }   template <class T>   T &chmax(T &a, const T &b) {     return a = max(a, b);   }   namespace std {   template <class T>   bool operator<(const complex<T> &a, const complex<T> &b) {     return a.real() == b.real() ? a.imag() < b.imag() : a.real() < b.real();   }   }  // namespace std   long long int dot(complex<long long int> a, complex<long long int> b) {     return (conj(a) * b).real();   }   long long int cross(complex<long long int> a, complex<long long int> b) {     return (conj(a) * b).imag();   }   long long int ccw(complex<long long int> a, complex<long long int> b,                     complex<long long int> c) {     b -= a, c -= a;     if (cross(b, c) > 0) return +1;     if (cross(b, c) < 0) return -1;     if (dot(b, c) < 0) return +2;     if (norm(b) < norm(c)) return -2;     return 0;   }   inline bool cross(const complex<long long int> &a,                     const complex<long long int> &b,                     const complex<long long int> &c,                     const complex<long long int> &d) {     return ccw(a, c, b) * ccw(a, d, b) == -1 && ccw(c, a, d) * ccw(c, b, d) == -1;   }   vector<complex<long long int> > convex_hull(       vector<complex<long long int> > ps) {     int n = ((long long int)(ps).size());     int k = 0;     sort((ps).begin(), (ps).end());     vector<complex<long long int> > ch(2 * n);     for (int i = 0; i < n; ch[k++] = ps[i++])       while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) k--;     for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--])       while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) k--;     ch.resize(k - 1);     return ch;   }   enum { OUT, ON, IN };   int contains(const vector<complex<long long int> > pol,                const complex<long long int> p) {     bool in = false;     for (int i = 0; i < pol.size(); ++i) {       complex<long long int> a = pol[i] - p, b = pol[(i + 1) % pol.size()] - p;       if (imag(a) > imag(b)) swap(a, b);       if (imag(a) <= 0 && 0 < imag(b))         if (cross(a, b) < 0) in = !in;       if (cross(a, b) == 0 && dot(a, b) <= 0) return ON;     }     return in ? IN : OUT;   }   long long int n, r;   long long int ansv;   vector<complex<long long int> > ans;   vector<complex<long long int> > cp;   vector<complex<long long int> > ps;   void dfs(long long int c, long long int k, long long int res) {     if (c == n) {       if (ansv < res) {         ansv = res;         ans = cp;       }       return;     }     for (long long int i = (k); i < (long long int)(((long long int)(ps).size()));          i++) {       long long int nres = res;       for (long long int j = (0); j < (long long int)(c); j++)         nres += norm(ps[i] - cp[j]);       cp.emplace_back(ps[i]);       dfs(c + 1, i, nres);       cp.pop_back();     }   }   int main() {     scanf( %lld %lld , &n, &r);     for (long long int i = (-r); i < (long long int)(r + 1); i++)       for (long long int j = (-r); j < (long long int)(r + 1); j++)         if (i * i + j * j <= r * r) ps.emplace_back(complex<long long int>(i, j));     ps = convex_hull(ps);     if (n % 2 == 0)       ps = {complex<long long int>(r, 0), complex<long long int>(-r, 0)};     dfs(0, 0, 0);     printf( %lld n , ansv);     for (auto &w : ans) printf( %lld %lld n , w.real(), w.imag());     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   long double r, v;   long double eps = 1e-8;   const long double mpi = acos(-1.0);   inline long double dist(long double theta) {     return 2.0L * r * (sin(theta) + theta);   }   inline double ti(long double theta, long long ni) {     return 2.0L * r * (mpi * ni + theta) / v;   }   void solve() {     int si, fi;     scanf( %d%d , &si, &fi);     long double d = fi - si;     long long ni = d / (2.0L * mpi * r);     long double left = 0;     long double right = mpi;     d -= 2.0L * r * mpi * ni;     long double dleft = dist(left);     long double dright = dist(right);     long double e = eps * v;     while (dright - dleft > e) {       long double mid = (right + left) / 2.0L;       long double dmid = dist(mid);       if (dmid > d) {         right = mid;         dright = dmid;       } else {         left = mid;         dleft = dmid;       }     }     double res = ti(right, ni);     printf( %.10f n , res);     return;   }   int main() {     int n, ri, vi;     scanf( %d%d%d , &n, &ri, &vi);     r = ri;     v = vi;     while (n--) {       solve();     }     cout.flush();     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   template <class T>   ostream& operator<<(ostream& os, vector<T> V) {     os <<  [  ;     for (auto v : V) os << v <<    ;     return os <<  ] ;   }   template <class L, class R>   ostream& operator<<(ostream& os, pair<L, R> P) {     return os <<  (  << P.first <<  ,  << P.second <<  ) ;   }   template <typename Arg1>   void __f(const char* name, Arg1&& arg1) {     cerr << name <<   :   << arg1 << endl;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     cerr.write(names, comma - names) <<   :   << arg1 <<   |  ;     __f(comma + 1, args...);   }   const long long INF = 0x3f3f3f3f;   const long long M = 1e9 + 7;   const long long N = 1e5 + 5;   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cout << fixed << setprecision(15);     long long n, k;     cin >> n >> k;     long long l[n], r[n];     for (long long i = 0; i < n; ++i) cin >> l[i] >> r[i];     vector<long long> v;     for (long long i = 0; i < n; ++i) v.push_back(l[i]), v.push_back(r[i]);     sort((v).begin(), (v).end());     map<long long, long long> ml, mr;     for (long long i = 0; i < 2 * n; ++i) mr[v[i]] = i;     for (long long i = 0; i < 2 * n; ++i) ml[v[2 * n - 1 - i]] = 2 * n - 1 - i;     long long a[2 * n];     memset(a, 0, sizeof(a));     for (long long i = 0; i < n; ++i) a[ml[l[i]]]++, a[mr[r[i]]]--;     partial_sum(a, a + 2 * n, a);     vector<long long> w;     for (long long i = 0; i < 2 * n; ++i) {       if (w.size() % 2 == 0) {         if (a[i] >= k) {           w.push_back(v[i]);         }       } else {         if (a[i] < k) {           w.push_back(v[i]);         }       }     }     cout << w.size() / 2 <<   n ;     for (long long i = 0; i < w.size(); i += 2) {       cout << w[i] <<     << w[i + 1] <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   struct less_key {     bool operator()(pair<int64_t, int64_t> p1, pair<int64_t, int64_t> p2) {       return (p1.first > p2.first) ||              ((p1.first == p2.first) && (p1.second < p2.second));     }   };   struct pair_hash {     std::size_t operator()(const pair<int64_t, int64_t>& k) const {       return static_cast<size_t>(k.first ^ k.second);     }   };   const int64_t mod = 100000000007ll;   const int64_t inf = 10000000000000007ll;   int64_t n, m;   char a[10004][10004];   int64_t sx, sy;   int64_t tx, ty;   bool x1[10004], x2[10004];   bool _q1[10004], y2[10004];   bool check_x(int64_t x1, int64_t x2, int64_t y) {     bool f = 1;     int64_t q = x1 < x2 ? 1 : -1;     for (int64_t i = x1;; i += q) {       if (a[i][y] ==  * ) {         f = 0;         break;       }       if (i == x2) {         break;       }     }     return f;   }   bool check_y(int64_t _q1, int64_t y2, int64_t x) {     bool f = 1;     int64_t q = _q1 < y2 ? 1 : -1;     for (int64_t i = _q1;; i += q) {       if (a[x][i] ==  * ) {         f = 0;         break;       }       if (i == y2) {         break;       }     }     return f;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> n >> m;     for (int64_t i = 0; i < n; i++) {       for (int64_t j = 0; j < m; j++) {         cin >> a[i][j];         if (a[i][j] ==  S ) {           sx = i;           sy = j;         } else if (a[i][j] ==  T ) {           tx = i;           ty = j;         }       }     }     if (sx == tx) {       bool f = 1;       int64_t q = sy < ty ? 1 : -1;       for (int64_t i = sy; i != ty; i += q) {         if (a[sx][i] ==  * ) {           f = 0;           break;         }       }       if (f) {         cout <<  YES ;         return 0;       }     }     if (sy == ty) {       bool f = 1;       int64_t q = sx < tx ? 1 : -1;       for (int64_t i = sx; i != tx; i += q) {         if (a[i][sy] ==  * ) {           f = 0;           break;         }       }       if (f) {         cout <<  YES ;         return 0;       }     }     bool f = 0;     for (int64_t i = 0; i < n && !f; i++) {       if (check_x(i, sx, sy) && check_x(i, tx, ty) && check_y(sy, ty, i)) {         f = 1;       }     }     for (int64_t j = 0; j < m && !f; j++) {       if (check_y(j, sy, sx) && check_y(j, ty, tx) && check_x(sx, tx, j)) {         f = 1;         break;       }     }     cout << (f ?  YES  :  NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 10010;   char s[N];   char t[N];   int n, m;   int main() {     scanf( %s , s + 1);     n = strlen(s);     scanf( %d , &m);     while (m--) {       int l, r, k;       scanf( %d%d%d , &l, &r, &k);       k %= (r - l + 1);       for (int i = 0; i < k; i++) {         t[l + i] = s[r - k + 1 + i];       }       for (int i = k; i <= r - l; i++) t[l + i] = s[l - k + i];       for (int i = l; i <= r; i++) s[i] = t[i];     }     printf( %s n , s + 1);     return 0;   }


#include <bits/stdc++.h>   const int N = 200;   char a[N][N];   int main() {     int n, m, k;     std::cin >> n >> m >> k;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         std::cin >> a[i][j];       }     }     int ret = 0;     int replaces = 0;     for (int j = 0; j < m; j++) {       int past = 0;       int maxOnes = 0;       int curReplaces = 0;       for (int i = 0; i < n; i++) {         if (a[i][j] !=  1 ) continue;         int ones = 0;         for (int x = i; x < std::min(n, i + k); x++) {           if (a[x][j] ==  1 ) ones++;         }         if (ones > maxOnes) {           maxOnes = ones;           curReplaces = past;         }         past++;       }       ret += maxOnes;       replaces += curReplaces;     }     std::cout << ret <<     << replaces <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int qpow(int b, int e, int m = 998244353) {     int a = 1;     for (; e; e >>= 1, b = (long long)b * b % m)       if (e & 1) a = (long long)a * b % m;     return a;   }   struct node {     long long sum;     long long pre;     long long suf;     long long ans;   };   node segt[1000000];   node combine(node &a, node &b) {     node c;     c.sum = a.sum + b.sum;     c.pre = max(a.pre, a.sum + b.pre);     c.suf = max(b.suf, b.sum + a.suf);     c.ans = max(max(a.ans, b.ans), a.suf + b.pre);     return c;   }   void build(long long i, long long tl, long long tr, vector<long long> &d,              vector<long long> &h) {     if (tl == tr) {       node t;       t.ans = 0;       t.sum = d[tl];       t.pre = 2 * h[tl];       t.suf = 2 * h[tl] + d[tl];       segt[i] = t;       return;     }     long long tm = (tl + tr) / 2;     build(i * 2, tl, tm, d, h);     build(i * 2 + 1, tm + 1, tr, d, h);     node c = combine(segt[i * 2], segt[i * 2 + 1]);     segt[i] = c;   }   node getans(long long i, long long tl, long long tr, long long l, long long r) {     if (l > r) {       node t;       t.sum = t.pre = t.suf = t.ans = INT_MIN;       return t;     }     if (tl == l && tr == r) return segt[i];     long long tm = (tl + tr) / 2;     node a = getans(i * 2, tl, tm, l, min(tm, r));     node b = getans(i * 2 + 1, tm + 1, tr, max(tm + 1, l), r);     node c = combine(a, b);     ;     return c;   }   void pn(node a) {     cout <<  ans :   << a.ans <<   pre:   << a.pre <<   suf :  << a.suf          <<   sum :   << a.sum << endl;   }   void solve() {     long long n, m;     cin >> n >> m;     vector<long long> d(2 * n + 1);     vector<long long> h(2 * n + 1);     for (int i = 1; i <= n; i++) {       cin >> d[i];       d[i + n] = d[i];     }     for (int i = 1; i <= n; i++) {       cin >> h[i];       h[i + n] = h[i];     }     build(1, 1, 2 * n, d, h);     while (m--) {       long long a, b;       cin >> a >> b;       long long l, r;       l = b + 1;       if (a <= b) {         r = n + a - 1;       } else {         r = a - 1;       }       auto ans = getans(1, 1, 2 * n, l, r);       cout << ans.ans << endl;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long t;     t = 1;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[200002];   pair<int, int> b[200002];   int main() {     int n, k, max = 0;     for (int i = 0; i < 200001; i++) {       a[i] = 2147483647;     }     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d %d , &b[i].first, &b[i].second);     }     sort(b, b + n);     for (int i = 0; i < n; i++) {       int d = b[i].first - b[i].second;       int* c = upper_bound(a, a + max, d);       int index = c - a;       if (index + 1 > max) {         max = index + 1;       }       a[index] = min(a[index], b[i].first + b[i].second);     }     printf( %d n , max);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k, p[200005];   int vis[200005], cntn;   int pre[200005], nxt[200005];   int flag = 0;   struct node {     int p, s;     node(int p = 0, int s = 0) : p(p), s(s) {}     bool operator<(const node &b) const { return p < b.p; }   };   void del(int x) {     nxt[pre[x]] = nxt[x];     pre[nxt[x]] = pre[x];     vis[x] = flag + 1;     cntn--;   }   priority_queue<node> q;   int main() {     cin >> n >> k;     cntn = n;     for (int i = 1; i <= n; i++) {       scanf( %d , &p[i]);       pre[i] = i - 1;       nxt[i] = i + 1;       q.push(node(p[i], i));     }     while (cntn) {       while (vis[q.top().s] != 0) q.pop();       node t = q.top();       q.pop();       del(t.s);       int cur = pre[t.s];       for (int i = 0; i < k && cur != 0; i++, cur = pre[cur]) del(cur);       cur = nxt[t.s];       for (int i = 0; i < k && cur != n + 1; i++, cur = nxt[cur]) del(cur);       flag = 1 - flag;     }     for (int i = 1; i <= n; i++) cout << vis[i];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   int n, m, k;   int a[11][11];   int f[11][11];   int cnt[21];   long long read() {     long long x = 0, f = 1;     char c = getchar();     while (!isdigit(c)) {       if (c ==  - ) f = -1;       c = getchar();     }     while (isdigit(c)) {       x = x * 10 + c -  0 ;       c = getchar();     }     return x * f;   }   void write(long long x) {     if (x < 0) putchar( - ), x = -x;     if (x >= 10) write(x / 10);     putchar(x % 10 +  0 );   }   void writern(long long x) { write(x), putchar(   ); }   void writeln(long long x) { write(x), puts(  ); }   long long dfs(int x, int y) {     if (y > m) {       y = 1, x++;     }     if (x > n) return 1;     long long res = 0;     int s = f[x - 1][y] | f[x][y - 1];     int cc = 0, flag = -1;     for (int i = s; i; i -= (i & -i)) cc++;     if (m + n - x - y + 1 > k - cc) return 0;     for (int i = s ^ ((1 << k) - 1); i; i -= (i & -i)) {       int t = i & (-i);       int tt = log2(t) + 1;       if (a[x][y] == 0 || a[x][y] == tt) {         f[x][y] = s | t;         cnt[tt]++;         if (cnt[tt] == 1) {           if (flag == -1) flag = dfs(x, y + 1);           res += flag;         } else if (cnt[tt])           res += dfs(x, y + 1);         res %= mod;         cnt[tt]--;       }     }     return res;   }   int main() {     n = read(), m = read();     k = read();     if (n + m - 1 > k) {       writeln(0);       return 0;     }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         a[i][j] = read();         if (a[i][j]) cnt[a[i][j]]++;       }     writeln(dfs(1, 1) % mod);     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, s1 = 0, s2 = 0, s = 0;     cin >> n;     s1 = ((n + 1) * n) / 2, s2 = s1 / 2;     vector<long> a;     for (long i = n; i > 0 && s2 > 0; --i)       if (s2 >= i)         if (s2 - i < i && s2 - i != 0) {           a.push_back(i), a.push_back(s2 - i), s += s2;           break;         } else           a.push_back(i), s2 -= i, s += i;     cout << abs(s1 - s - s) <<   n  << a.size() <<    ;     for (long i = 0; i < a.size(); cout << a[i] <<    , ++i)       ;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> t;   int sz = 0;   const int INF = 2e9;   void build(vector<int> a) {     t.assign(2 * sz + 2, INF);     for (int i = 0; i < sz; i++) {       t[i + sz] = a[i];     }     for (int i = sz - 1; i > 0; i--) {       t[i] = min(t[2 * i], t[2 * i + 1]);     }   }   int get(int v, int tl, int tr, int l, int r) {     if (tl > r || l > tr) {       return INF;     }     if (tl == l && tr == r) {       return t[v];     }     int tm = (tl + tr) / 2;     return min(get(2 * v, tl, tm, l, min(r, tm)),                get(2 * v + 1, tm + 1, tr, max(l, tm + 1), r));   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     sz = 1;     while (sz < 4 * n) {       sz *= 2;     }     vector<int> a(sz);     vector<pair<int, int>> q;     vector<pair<int, int>> qq;     for (int i = 0; i < n; i++) {       int l, r;       cin >> l >> r;       q.emplace_back(l, 0);       qq.emplace_back(l, r);       q.emplace_back(r, 1);       q.emplace_back(l - 1, -1);       q.emplace_back(r + 1, -1);     }     sort(q.begin(), q.end());     int b = 0;     for (int i = 0; i < 4 * n; i++) {       if (q[i].second == 0) {         b++;         int id = lower_bound(q.begin(), q.end(), make_pair(q[i].first, -1)) -                  q.begin();         a[id] = max(a[id], b);       } else {         int id = lower_bound(q.begin(), q.end(), make_pair(q[i].first, -1)) -                  q.begin();         a[id] = max(a[id], b);         if (q[i].second == 1) b--;       }     }     for (int i = 0; i < a.size(); i++) {       if (a[i] == 0) {         a[i] = INF;       }     }     build(a);     for (int i = 0; i < n; i++) {       int minimum = get(           1, 0, sz - 1,           lower_bound(q.begin(), q.end(), make_pair(qq[i].first, -1)) - q.begin(),           lower_bound(q.begin(), q.end(), make_pair(qq[i].second, -1)) -               q.begin());       if (minimum > 1 && minimum != INF) {         cout << i + 1;         return 0;       }     }     cout << -1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   const long long infinity = 1000000000000000000;   const int inf = 1e9 + 5;   bool do_debug = false;   template <typename T>   ostream& operator<<(ostream& os, vector<T>& v) {     for (auto element : v) {       os << element <<    ;     }     return os;   }   template <typename T, typename S>   ostream& operator<<(ostream& os, pair<T, S>& p) {     os <<  (  << p.first <<  ,   << p.second <<  ) ;     return os;   }   template <typename T>   ostream& operator<<(ostream& os, set<T>& v) {     if (v.size() == 0) {       os <<  empty set n ;       return os;     }     auto endit = v.end();     endit--;     os <<  [ ;     for (auto it = v.begin(); it != v.end(); it++) {       os << *it;       if (it != endit) {         os <<  ,  ;       }     }     os <<  ] ;     return os;   }   template <typename T>   ostream& operator<<(ostream& os, multiset<T>& v) {     if (v.size() == 0) {       os <<  empty multiset n ;       return os;     }     auto endit = v.end();     endit--;     os <<  [ ;     for (auto it = v.begin(); it != v.end(); it++) {       os << *it;       if (it != endit) {         os <<  ,  ;       }     }     os <<  ] ;     return os;   }   template <typename T, typename S>   ostream& operator<<(ostream& os, map<T, S>& v) {     if (v.size() == 0) {       os <<  empty map n ;       return os;     }     auto endit = v.end();     endit--;     os <<  { ;     for (auto it = v.begin(); it != v.end(); it++) {       os <<  (  << (*it).first <<   :   << (*it).second <<  ) ;       if (it != endit) {         os <<  ,  ;       }     }     os <<  } ;     return os;   }   template <typename T>   ostream& operator<<(ostream& os, vector<vector<T>>& v) {     for (auto& subv : v) {       for (auto& e : subv) {         os << e <<    ;       }       os <<   n ;     }     return os;   }   const int maxn = 3e5 + 5;   int n_nodes, n_edges, target;   vector<vector<int>> adj;   vector<array<int, 4>> edges;   long long dist[maxn];   long long depth[maxn];   int incoming[maxn];   int added[maxn];   set<int> ans;   long long ans_w;   void dijkstra() {     int node = target;     priority_queue<pair<long long, long long>> pq;     pq.push(make_pair(0, target));     while (!pq.empty()) {       long long w = -pq.top().first;       node = pq.top().second;       pq.pop();       if (w > dist[node]) {         continue;       }       for (int e : adj[node]) {         int next = edges[e][1];         if (next == node) next = edges[e][2];         if (dist[node] + edges[e][0] < dist[next]) {           if (incoming[next] != -1) {             ans_w -= edges[incoming[next]][0];             ans.erase(incoming[next]);           }           incoming[next] = e;           ans_w += edges[incoming[next]][0];           ans.insert(incoming[next]);           dist[next] = dist[node] + edges[e][0];           pq.push(make_pair(-dist[next], next));         } else if (dist[node] + edges[e][0] == dist[next]) {           if (edges[e][0] < edges[incoming[next]][0]) {             ans_w -= edges[incoming[next]][0];             ans.erase(incoming[next]);             incoming[next] = e;             ans_w += edges[incoming[next]][0];             ans.insert(incoming[next]);           }         }       }     }   }   void mst() {     int node = target;     priority_queue<pair<long long, long long>> pq;     for (int e : adj[node]) {       int u = edges[e][1], v = edges[e][2];       if (u != node) swap(u, v);       depth[v] = edges[e][0];       incoming[v] = e;       ans_w += edges[e][0];       ans.insert(e);       pq.push(make_pair(-edges[e][0], v));     }     while (!pq.empty()) {       long long d = -pq.top().first;       node = pq.top().second;       pq.pop();       for (int e : adj[node]) {         long long w = edges[e][0];         int u = edges[e][1], v = edges[e][2];         if (u != node) swap(u, v);         if (depth[u] + w == dist[v]) {           if (incoming[v] == -1) {             ans_w += w;             ans.insert(e);             incoming[v] = e;           } else {             if (w < edges[incoming[v]][0]) {               ans_w -= edges[incoming[v]][0];               ans.erase(incoming[v]);               incoming[v] = e;               ans_w += edges[incoming[v]][0];               ans.insert(incoming[v]);             }           }           depth[v] = dist[v];         }       }     }   }   void solve() {     for (int i = 0; i < maxn; i++) {       incoming[i] = -1;       dist[i] = infinity;       depth[i] = infinity;       added[i] = 0;     }     cin >> n_nodes >> n_edges;     adj.resize(n_nodes);     for (int i = 0; i < n_edges; i++) {       int w, u, v;       cin >> u >> v >> w;       u--;       v--;       adj[u].push_back(i);       adj[v].push_back(i);       edges.push_back({w, u, v, i});     }     cin >> target;     target--;     dist[target] = 0;     depth[target] = 0;     ans_w = 0;     dijkstra();     cout << ans_w <<   n ;     for (int a : ans) {       cout << a + 1 <<    ;     }     cout <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int q = 1;     while (q-- > 0) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     scanf( %d %d , &n, &k);     vector<int> a(n);     for (int i = 0; i < n; ++i) scanf( %d , &a[i]);     int sum = 0;     for (int i = 0; i < n; ++i) sum += a[i];     if (sum % k) {       printf( No n );       return 0;     }     int req = sum / k;     int cur = 0;     int len = 0;     for (int i = 0; i < n; ++i) {       if (a[i] > req) {         printf( No n );         return 0;       }     }     vector<int> ans;     for (int i = 0; i < n; ++i) {       cur += a[i];       len++;       if (cur == req) {         ans.push_back(len);         cur = 0;         len = 0;       } else if (cur > req) {         cur = a[i];         len = 1;       }     }     if ((int)ans.size() == k) {       printf( Yes n );       for (int i = 0; i < k; ++i) printf( %d  , ans[i]);     } else {       printf( No n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   set<int> S1, S2, S3;   map<int, bool> vis;   int p[2000001], a[200001], b[2000001];   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> p[i];     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n; i++) cin >> b[i];     for (int i = 0; i < n; i++) {       if (a[i] == 1 || b[i] == 1) S1.insert(p[i]);       if (a[i] == 2 || b[i] == 2) S2.insert(p[i]);       if (a[i] == 3 || b[i] == 3) S3.insert(p[i]);     }     int m;     cin >> m;     for (int i = 0; i < m; i++) {       int c;       cin >> c;       if (c == 1) {         if (S1.empty())           cout << -1 <<    ;         else {           auto ans = S1.begin();           S2.erase(*ans);           S3.erase(*ans);           cout << *ans <<    ;           S1.erase(*ans);         }       } else if (c == 2) {         if (S2.empty())           cout << -1 <<    ;         else {           auto ans = S2.begin();           S1.erase(*ans);           S3.erase(*ans);           cout << *ans <<    ;           S2.erase(*ans);         }       } else {         if (S3.empty())           cout << -1 <<    ;         else {           auto ans = S3.begin();           S1.erase(*ans);           S2.erase(*ans);           cout << *ans <<    ;           S3.erase(*ans);         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   char s[5010];   int main() {     scanf( %s , s);     int n = (int)strlen(s);     int res = 0;     for (int i = 0; i < n; i++) {       int cnt1 = 0, cnt2 = 0;       for (int j = i; j < n; j++) {         if (s[j] ==  ( ) cnt1++;         if (s[j] ==  ) ) cnt1--;         if (s[j] ==  ? ) cnt2++;         if (cnt2 && cnt2 > cnt1) cnt2--, cnt1++;         if (cnt1 < 0) break;         if (cnt2 == cnt1) res++;       }     }     printf( %d , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 400000 + 10;   const int M = 26;   namespace Trie {   int trie[N][M], cnt[N], size;   void init() {     size = 0;     memset(trie[0], 0, sizeof(trie[0]));     cnt[0] = 0;   }   void insert(char str[]) {     int u = 0, v;     for (int i = 0; str[i]; i++) {       v = str[i] -  a ;       if (!trie[u][v]) {         trie[u][v] = ++size;         cnt[size] = 0;         memset(trie[size], 0, sizeof(trie[size]));       }       u = trie[u][v];     }     cnt[u]++;   }   }  // namespace Trie   namespace SAM {   struct state {     int len, link, siz;     int next[M];   } st[N];   int sz, last;   void init() {     sz = 1, last = 0;     st[0].len = 0;     st[0].link = -1;   }   void add(int c) {     int r = sz++;     st[r].len = st[last].len + 1;     st[r].siz = 1;     int p = last;     while (p != -1 && !st[p].next[c]) st[p].next[c] = r, p = st[p].link;     if (p == -1)       st[r].link = 0;     else {       int q = st[p].next[c];       if (st[q].len == st[p].len + 1)         st[r].link = q;       else {         int qq = sz++;         st[qq].link = st[q].link;         memcpy(st[qq].next, st[q].next, sizeof(st[q].next));         st[qq].len = st[p].len + 1;         st[qq].siz = 0;         while (p != -1 && st[p].next[c] == q) st[p].next[c] = qq, p = st[p].link;         st[q].link = st[r].link = qq;       }     }     last = r;   }   int tp[N], tmp[N];   void get_topo() {     memset(tmp, 0, sizeof(tmp));     for (int i = 0; i < sz; i++) tmp[st[i].len]++;     for (int i = 1; i < sz; i++) tmp[i] += tmp[i - 1];     for (int i = 0; i < sz; i++) tp[--tmp[st[i].len]] = i;   }   void calc_siz() {     get_topo();     for (int i = sz - 1; i > 0; i--) st[st[tp[i]].link].siz += st[tp[i]].siz;     st[0].siz = 0;   }   }  // namespace SAM   long long dfs(int tu, int su) {     long long ans = 1ll * Trie::cnt[tu] * SAM::st[su].siz;     for (int i = 0; i < M; i++) {       int tv = Trie::trie[tu][i];       int sv = SAM::st[su].next[i];       if (tv == 0 || sv == 0) continue;       ans += dfs(tv, sv);     }     return ans;   }   int id[N];   void ddfs(int i) {     if (id[i] == -1) return;     for (int j = 0; j < M; j++) {       int v = Trie::trie[i][j];       if (v) {         id[v] = SAM::st[id[i]].next[j];         if (id[v] == 0) id[v]--;         ddfs(v);       }     }   }   char s[N];   int main() {     SAM::init(), Trie::init();     scanf( %s , s);     for (int i = 0; s[i]; i++) SAM::add(s[i] -  a );     SAM::calc_siz();     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %s , s);       Trie::insert(s);     }     ddfs(0);     long long ans = 0;     for (int i = 0; i <= Trie::size; i++) {       if (id[i] <= 0) continue;       if (Trie::cnt[i]) ans += 1ll * Trie::cnt[i] * dfs(0, id[i]);     }     printf( %lld n , ans);   }


#include <bits/stdc++.h>   using namespace std;   int log(int x) { return 32 - __builtin_clz(x) - 1; }   int log(long long x) { return 64 - __builtin_clzll(x) - 1; }   int n;   int main() {     cin >> n;     if (n % 2 == 0) {       cout <<  NO ;       return 0;     }     cout <<  YES n ;     bool add = false;     for (int i = 1; i < 2 * n; i += 2, add = !add) cout << i + add <<    ;     add = true;     for (int i = 1; i < 2 * n; i += 2, add = !add) cout << i + add <<    ;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 5e5 + 100;   const long long maxN = 6e6;   const long long oo = 1e18;   const long long mod = 998244353;   long long n, m;   long long a[N], b[N], cost[N], equalpos[N];   long long IT[20 * N], lazy[20 * N];   map<long long, long long> mp;   void updmin(long long &x, long long y) { x = min(x, y); }   void addlazy(long long node, long long nlef, long long nrig) {     if (nlef > nrig || !lazy[node]) return;     if (nlef == nrig && IT[node] < oo) IT[node] += lazy[node];     if (nlef < nrig) {       lazy[2 * node] += lazy[node];       lazy[2 * node + 1] += lazy[node];     }     lazy[node] = 0;   }   void upd(long long node, long long nlef, long long nrig, long long lef,            long long rig, long long val, bool flag) {     addlazy(node, nlef, nrig);     if (nlef > rig || nrig < lef || nlef > nrig) return;     if (lef <= nlef && nrig <= rig) {       if (!flag) {         lazy[node] += val;         addlazy(node, nlef, nrig);       } else         IT[node] = val;       return;     }     long long mid = (nlef + nrig) / 2;     upd(2 * node, nlef, mid, lef, rig, val, flag);     upd(2 * node + 1, mid + 1, nrig, lef, rig, val, flag);   }   long long get(long long node, long long nlef, long long nrig, long long pos) {     addlazy(node, nlef, nrig);     if (nlef > pos || pos > nrig) return oo;     if (nlef == nrig) {       return IT[node];     }     long long mid = (nlef + nrig) / 2;     return min(get(2 * node, nlef, mid, pos),                get(2 * node + 1, mid + 1, nrig, pos));   }   signed main() {     ios_base::sync_with_stdio(false);     cout.tie(0);     cin >> n;     for (long long i = 1; i <= n; ++i) cin >> a[i];     for (long long i = 1; i <= n; ++i) cin >> cost[i];     cin >> m;     for (long long i = 1; i <= m; ++i) cin >> b[i], mp[b[i]] = i;     for (long long i = 1; i <= n; ++i)       if (mp.find(a[i]) != mp.end()) equalpos[i] = mp[a[i]];     for (long long i = 0; i <= 20 * N - 1; ++i) IT[i] = oo;     if (a[1] < b[1])       upd(1, 0, m, 0, 0, cost[1], 1);     else if (a[1] == b[1]) {       upd(1, 0, m, 0, 0, cost[1], 1);       upd(1, 0, m, 1, 1, 0, 1);     } else       upd(1, 0, m, 0, 0, cost[1], 1);     for (long long i = 2; i <= n; ++i) {       long long pos = equalpos[i];       if (pos) {         long long val = get(1, 0, m, pos - 1);         long long tmp = get(1, 0, m, pos);         if (cost[i] < 0 && tmp < oo) updmin(val, tmp + cost[i]);         updmin(val, get(1, 0, m, pos));         upd(1, 0, m, pos, pos, val, 1);       }       pos = lower_bound(b + 1, b + m + 1, a[i]) - b - 1;       if (pos >= 0)         upd(1, 0, m, 0, pos, cost[i], 0);       else         pos = 0;       while (pos <= m && b[pos] <= a[i]) pos += 1;       if (cost[i] < 0 && pos <= m) upd(1, 0, m, pos, m, cost[i], 0);     }     long long ans = get(1, 0, m, m);     if (ans < oo) return cout <<  YES n  << ans, 0;     cout <<  NO ;   }


#include <bits/stdc++.h>   using namespace std;   mt19937_64 rnd;   const long long maxn = 1e6 + 10;   const long long mod = 998244353;   const long long base = 500;   long long gt[maxn];   long long gtv[maxn];   long long mu(long long a, long long n) {     if (n == 0) return 1;     if (n == 1) return a;     long long t = mu(a, n / 2);     if (n % 2 == 0) return (t * t) % mod;     return ((t * t) % mod * a) % mod;   }   void setup() {     gt[0] = 1;     for (int i = 1; i < maxn; i++) {       gt[i] = (gt[i - 1] * i) % mod;     }     gtv[maxn - 1] = mu(gt[maxn - 1], mod - 2);     for (int i = maxn - 2; i >= 0; i--) {       gtv[i] = (gtv[i + 1] * (i + 1)) % mod;     }   }   long long nck(long long n, long long k) {     if (n < k) return 0;     return ((gt[n] * gtv[k]) % mod * gtv[n - k]) % mod;   }   long long f[maxn];   long long f1[maxn];   long long f2[maxn];   long long dp[maxn][2];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     if (fopen( t.inp ,  r )) {       freopen( test.inp ,  r , stdin);       freopen( test.out ,  w , stdout);     }     setup();     string s;     cin >> s;     long long n = s.length();     s =     + s;     for (int i = 1; i <= n; i++) {       f[i] = f[i - 1] + (s[i] ==  ( );       f1[i] = f1[i - 1] + (s[i] ==  ? );       f2[i] = f2[i - 1] + (s[i] ==  ) );     }     long long all = f1[n];     for (int j = 0; j <= min(all, 1ll); j++) {       for (int i = 0; i <= n; i++) {         dp[i][j] = ((i > 0 ? dp[i - 1][j] : 0) + nck(all - j, i)) % mod;       }     }     long long ans = 0;     for (int i = 1; i <= n; i++) {       if (s[i] ==  ) ) continue;       long long a = f[i - 1];       long long b = f2[n] - f2[i];       long long c = f1[i - 1];       long long d = f1[n] - f1[i];       if (d + b - a > 0) ans = (ans + dp[d + b - a - 1][s[i] ==  ? ]) % mod;     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     string a, b;     cin >> a >> b;     int one[2];     one[0] = one[1] = 0;     for (char ch : a) one[0] += ch ==  1  ? 1 : 0;     for (char ch : b) one[1] += ch ==  1  ? 1 : 0;     if (one[0] + (one[0] & 1) >= one[1])       cout <<  YES ;     else       cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     int arr[100005];     int pos[100005], pr[100005] = {0};     int i, j, ans = 0;     for (i = 0; i < n; i++) {       cin >> arr[i];       if (pr[arr[i]] == 0) ans++;       pr[arr[i]]++;       pos[arr[i]] = i;       if (ans == k) {         j = i;         break;       }     }     for (i = j + 1; i < n; i++) cin >> arr[i];     if (ans < k)       cout <<  -1 -1 ;     else {       int min = j + 1;       for (i = 0; i < j; i++) {         if (pos[arr[i]] < min) min = pos[arr[i]];       }       if (j != 0)         cout << min + 1 <<     << j + 1;       else         cout <<  1 1 ;     }     return (0);   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, T, x, y, x2, y2, a, b, sol = 1;   char c;   string s;   bool t;   vector<int> V;   vector<int> V2;   int p, p2, mn = INT_MAX;   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) {       cin >> x;       V.push_back(x);     }     for (int i = 0; i < m; i++) {       cin >> x;       V2.push_back(x);     }     p = n - 1;     for (int i = 0; i < m - 1; i++) {       x = V2[m - i - 1];       while (p >= 0 && V[p] > x) {         p--;       }       if (p < 0) {         cout << 0;         return 0;       }       if (V[p] < x) sol = 0;       p2 = p;       while (p >= 0 && V[p2] >= x) {         p2--;       }       if (p2 < 0) {         cout << 0;         return 0;       }       sol *= p - p2;       sol %= 998244353;       p = p2;     }     x = V2[0];     for (int i = 0; i <= p; i++) {       mn = min(V[i], mn);     }     if (mn != x) sol = 0;     cout << sol;   }


#include <bits/stdc++.h>   using namespace std;   int s[400], n, ret = 360;   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       int t;       cin >> t;       s[i] = s[i - 1] + t;     }     for (int i = 0; i < n; i++) {       for (int j = i + 1; j <= n; j++) {         ret = min(ret, abs(360 - 2 * (s[j] - s[i])));       }     }     cout << ret << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 5;   const int M = 1e4 + 5;   const int Max = (1 << 26) + 5;   short a[Max];   int n, m, b[M], ans[M];   char str[N], sets[30];   int main() {     scanf( %s , str + 1);     n = strlen(str + 1);     scanf( %d , &m);     for (int i = 1; i <= m; i++) {       scanf( %s , sets);       int len = strlen(sets);       for (int j = 0; j < len; j++) b[i] |= 1 << (sets[j] -  a );       a[b[i]] = i;     }     for (int i = 1; i <= n; i++)       for (int j = i, s = 0; j <= n; j++) {         if (str[j] == str[i - 1]) break;         s |= 1 << (str[j] -  a );         if (!(s >> (str[j + 1] -  a ) & 1)) ans[a[s]]++;       }     for (int i = 1; i <= m; i++) printf( %d n , ans[a[b[i]]]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, d, h;     cin >> n >> d >> h;     if (h * 2 < d) {       cout << -1;       return 0;     }     if (d == 1) {       if (n >= 3) {         cout << -1;         return 0;       }       if (n == 2) {         cout << 1 <<     << 2;         return 0;       }     }     for (int i = 2; i <= h + 1; i++) {       cout << i <<     << i - 1 << endl;     }     int b = d - h;     if (b > 0) {       cout << 1 <<     << h + 2 << endl;       for (int i = h + 3; i <= d + 1; i++) {         cout << i <<     << i - 1 << endl;       }     }     int hd = 1;     if (b == 0) {       hd = 2;     }     for (int i = d + 2; i <= n; i++) {       cout << hd <<     << i << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     vector<int> vec(1, 0);     vector<vector<int> > list;     int counter = 0, i, j;     for (i = 0; i < s.length(); i++) {       if (s[i] ==  0 ) {         if (list.size() > counter) {           list[counter].push_back(i);         } else {           vec[0] = i;           list.push_back(vec);         }         counter++;       } else if (s[i] ==  1 ) {         if (counter - 1 < 0) {           cout << -1;           return 0;         } else {           list[--counter].push_back(i);         }       }     }     if (counter < list.size()) {       cout << -1;       return 0;     }     cout << list.size() <<   n ;     for (i = 0; i < list.size(); i++) {       cout << list[i].size() <<    ;       for (j = 0; j < list[i].size(); j++) {         cout << list[i][j] + 1 <<    ;       }       cout <<   n ;     }     return 0;   }


// minako   #include <bits/stdc++.h>   using namespace std;      #define endl   n    #define f first   #define s second   #define pb push_back   #define pii pair<int,int>   #define ll long long   #define pll pair <ll,ll>   #define faster ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);      const ll M = 1e9 + 7;   const int N = 2e5 + 5;   const ll INF = 1e9;   const ll INF2 = 1e18;   const int K = 750;      ll  n,u,v,a[N],q;      int main() {    faster    cin >> q;    while (q--) {     cin >> n >> u >> v;     bool eq = 1,can = 0;     for (int i = 1; i <= n; i++) {      cin >> a[i];      if (a[i] != a[i - 1] && i > 1) eq = 0;      if (abs(a[i] - a[i - 1]) > 1 && i > 1) can = 1;     }     if (can) cout << 0 << endl;     else if (eq) cout << v + min(u,v) << endl;     else cout << min(u,v) << endl;;    }          return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double EPS = 1E-9;   const int INF = (int)1E9;   const long long INF64 = (long long)1E18;   const long double PI = 2 * acos(.0);   int main() {     string num;     cin >> num;     int a[11];     fill(a, a + 11, 0);     for (int i = 0; i < (int)((num).size()); i++) {       a[num[i] -  0 ]++;     }     int cont = 0;     string temp1 = num;     string temp2 = num;     int a2[11];     int a3[11];     bool cent = 1;     for (int i = 1; i < (int)(10); i++) {       int temp = 0;       char kk1 =  s , kk2 =  s ;       vector<pair<char, int> > t1;       vector<pair<char, int> > t2;       for (int j = 0; j < (int)(10); j++) a2[j] = a3[j] = a[j];       if (a2[i] > 0 && a3[10 - i] > 0) {         a2[i]--;         a3[10 - i]--;         kk1 =  0  + i;         kk2 =  0  + (10 - i);       } else         continue;       temp = 1;       for (int j = 0; j < (int)(10); j++) {         int mini = min(a2[j], a3[9 - j]);         temp += mini;         a2[j] -= mini;         a3[9 - j] -= mini;         t1.push_back(make_pair( 0  + j, mini));         t2.push_back(make_pair( 0  + (9 - j), mini));       }       temp += min(a2[0], a3[0]);       int conta = min(a2[0], a3[0]);       a2[0] -= conta;       a3[0] -= conta;       if (temp > cont) {         cent = 0;         temp1 =   ;         temp2 =   ;         for (int j = 0; j < (int)(10); j++) {           if (a2[j] > 0) temp1.append(a2[j],  0  + j);           if (a3[j] > 0) temp2.append(a3[j],  0  + j);         }         for (int j = 0; j < (int)((t1).size()); j++) {           temp1.append(t1[j].second, t1[j].first);           temp2.append(t2[j].second, t2[j].first);         }         temp1 += kk1;         temp2 += kk2;         temp1.append(conta,  0 );         temp2.append(conta,  0 );         cont = temp;       }     }     if (cent) {       temp1 =   ;       temp2 =   ;       for (int j = 1; j < (int)(10); j++) {         if (a[j] > 0) temp1.append(a[j],  0  + j);         if (a[j] > 0) temp2.append(a[j],  0  + j);       }       temp1.append(a[0],  0 );       temp2.append(a[0],  0 );     }     cout << temp1 << endl << temp2 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string presidents[41] = {         ,          Washington ,  Adams ,      Jefferson ,  Madison ,        Monroe ,    Adams ,       Jackson ,    Van Buren ,  Harrison ,        Tyler ,     Polk ,        Taylor ,     Fillmore ,   Pierce ,        Buchanan ,  Lincoln ,     Johnson ,    Grant ,      Hayes ,        Garfield ,  Arthur ,      Cleveland ,  Harrison ,   Cleveland ,        McKinley ,  Roosevelt ,   Taft ,       Wilson ,     Harding ,        Coolidge ,  Hoover ,      Roosevelt ,  Truman ,     Eisenhower ,        Kennedy ,   Johnson ,     Nixon ,      Ford ,       Carter ,        Reagan ,   };   int main() {     int n;     cin >> n;     cout << presidents[n] << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, m;     cin >> n >> m;     long long t = 1;     if (n < 30) {       for (int i = 0; i < n; i++) {         t *= 2;       }       cout << m % t;     } else {       cout << m;     }     return 0;   }


//Shrey Dubey         #include<iostream>   #include<string>   #include<algorithm>   #include<map>   #include<unordered_map>   #include<vector>   #include<set>   #include<list>   #include<iomanip>   #include<queue>   #include<stack>   #include <math.h>   #include<climits>   #include<bitset>   #include<cstring>   #include<numeric>   #include<array>         using namespace std;   typedef long long ll;   typedef long double ld;      #define YES cout<< YES n    #define Yes cout<< Yes n    #define NO cout<< NO n    #define No cout<< No n    #define prDouble(x) cout<<fixed<<setprecision(10)<<x //to print decimal numbers   #define pb push_back   #define ff first   #define sec second   #define umap unordered_map   #define mp make_pair   #define KOBE ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)   #define fo(n) for(ll i = 0; i<n; i++)   #define fnd(stl, data) find(stl.begin(), stl.end(), data)   #define forn(x,n) for(ll x = 0; x<n; x++)   #define imax INT_MAX   #define lmax LLONG_MAX   #define imin INT_MIN   #define lmin LLONG_MIN   #define vi vector<int>   #define vl vector<ll>   #define vp vector<pair<ll,ll> >   #define vb vector<bool>   #define pr(t) cout<<t<<  n    #define int long long   #define ql queue<ll>   #define qp queue<pair<ll,ll> >   #define endl   n    #define nl cout<<  n    #define re cin >>   #define pll pair<ll,ll>   #define FOR(a,b) for(ll i = a; i<=b; i++)   #define all(x) x.begin(),x.end()      // ll dx[] = {1,0,-1,0};   // ll dy[] = {0,1,0,-1};      ll mod = 1e9 + 7;      ll cl(ld a){       if(a>(ll) a){           return (ll)a+1;       }       else{           return (ll)a;       }   }      ll flr(ld a){       return (ll) a;   }            //code starts here      const ll M = 2e5+100;   ll s[M];      ll n,m,a,b;   bool check(ll pos){       ll diff = abs(b - a);       ll t = diff;       if(diff > pos){           if(a > b){               t += (n - a);           }else{               t += (a - 1);           }       }       // if(pos == 1) cout<<t<< () n ;       while(pos > 0){           if(s[pos] >= t) return false;           t--;           pos--;       }       return true;   }      void solve(){       re n; re m; re a; re b;       fo(m) re s[i+1];       sort(s+1,s+m+1);       // fo(m) cout<<s[i+1]<<   ;nl;       ll ans = 0;       ll s = 0, e = m;       while(s <= e){           // cout<<s<< () <<e<<  n ;           ll mid = (s+e)/2;           if(check(mid)){               ans = mid;               s = mid+1;           }else{               e = mid-1;           }       }       pr(ans);   }      int32_t main(){       KOBE;       ll t;       t = 1;       re t;       while(t--) solve();   }         //common errors   // row - n, col - m always and loop var   // see the freq of numbers carefully   // see if there s array overflow   // use map for large inputs         //problem ideas   //check piegonhole wherever possible   //there might be many instances of limited answers like 0,1,2 only   // see suffix and prefix   //don t be obsessed with binary search   // try to find repeating pattern in matrices


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   const int MAXN = 1000000;   int n;   char s[MAXN + 1];   int pac[MAXN], npac;   int ast[MAXN], nast;   int prvpac[MAXN], prvast[MAXN];   int mxpos[MAXN];   bool canrgt[MAXN];   int calcmxpos(int i, int t) {     int x = i - t >= 0 ? prvast[i - t] : -1;     if (x == -1) return prvpac[i] == -1 ? i : max(i, prvpac[i] + t);     if (prvpac[i] == -1 || prvpac[i] <= x) return canrgt[x] ? i : -1;     if (prvpac[prvpac[i]] != -1 && prvpac[prvpac[i]] > x)       return mxpos[prvpac[prvpac[i]]] != -1 ? max(i, prvpac[i] + t) : -1;     return canrgt[x] ? max(i, prvpac[i] + t) : mxpos[prvpac[i]] != -1 ? i : -1;   }   bool calccanrgt(int x, int t) {     int i = prvpac[x];     if (i == -1 || x - i > t) return false;     int y = prvast[i];     return mxpos[i] != -1 && mxpos[i] >= x || y == -1 ||            (prvpac[i] != -1 && prvpac[i] > y && mxpos[prvpac[i]] != -1) ||            canrgt[y];   }   bool canall(int t) {     for (int i = (0); i < (n); ++i) {       if (s[i] ==  P ) {         mxpos[i] = calcmxpos(i, t);       }       if (s[i] ==  * ) {         canrgt[i] = calccanrgt(i, t);       }     }     int idx = max(pac[npac - 1], ast[nast - 1]);     if (s[idx] ==  P )       return mxpos[idx] != -1;     else if (s[idx] ==  * )       return canrgt[idx];     else {       assert(false);       return false;     }   }   pair<int, int> solve() {     npac = nast = 0;     for (int i = (0); i < (n); ++i) {       prvast[i] = nast == 0 ? -1 : ast[nast - 1];       prvpac[i] = npac == 0 ? -1 : pac[npac - 1];       if (s[i] ==  P )         pac[npac++] = i;       else if (s[i] ==  * )         ast[nast++] = i;     }     assert(npac > 0 && nast > 0);     if (npac == 1) {       int nleft = 0, tleft = 0, nright = 0, tright = 0;       for (int i = (0); i < (nast); ++i)         if (ast[i] < pac[0]) {           ++nleft;           tleft = max(tleft, pac[0] - ast[i]);         } else {           ++nright;           tright = max(tright, ast[i] - pac[0]);         }       if (nleft > nright || nleft == nright && tleft <= tright)         return make_pair(nleft, tleft);       else         return make_pair(nright, tright);     }     int l = 0, r = n - 1;     while (l + 1 < r) {       int m = l + (r - l) / 2;       if (canall(m))         r = m;       else         l = m;     }     return make_pair(nast, r);   }   void run() {     scanf( %d , &n);     scanf( %s , s);     pair<int, int> ans = solve();     printf( %d %d n , ans.first, ans.second);   }   int main() {     run();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 1e9 + 7;   const long long llinf = 1ll * inf * inf;   const double eps = 1e-12;   const int N = 1e5 + 10;   int f[N], h[N], g[N];   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= int(n); ++i) scanf( %d , &f[i]);     map<int, vector<int> > uf;     for (int i = 1; i <= int(n); ++i) uf[f[i]].push_back(i);     int m = uf.size();     auto itr = uf.begin();     for (int x = 1; x <= m; ++x) {       h[x] = itr->first;       vector<int> nv = itr->second;       itr++;       for (int y : nv) g[y] = x;     }     int bad = 0;     for (int x = 1; x <= m; ++x)       if (g[h[x]] != x) bad = 1;     for (int x = 1; x <= n; ++x)       if (h[g[x]] != f[x]) bad = 1;     if (bad)       puts( -1 );     else {       printf( %d n , m);       for (int x = 1; x <= int(n); ++x) printf( %d%c , g[x], x == n ?   n  :    );       for (int x = 1; x <= int(m); ++x) printf( %d%c , h[x], x == m ?   n  :    );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     long long ans = 0;     for (int i = (0); i < (n); ++i) {       long long t, T, x, cost;       cin >> t >> T >> x >> cost;       if (t >= T) {         ans += cost + m * x;         continue;       }       long long aux1 = cost;       if (m > (T - t)) aux1 += m * x;       long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1;       aux2 *= cost;       ans += min(aux1, aux2);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long S = (1 << 19);   long long x, y, a, q, pref_x[S], pref_y[S], ans[S];   pair<long long, long long> tab[S];   vector<pair<pair<long long, long long>, pair<long long, long long> > > X;   int main() {     ios_base::sync_with_stdio(0);     cout.tie(0);     cin.tie(0);     cin >> a >> q;     for (int i = 1; i <= a; i++) {       cin >> x >> y;       tab[i] = {x, y};       X.push_back({{x - y, i}, {x, y}});     }     sort(X.begin(), X.end());     for (int i = 1; i <= X.size(); i++)       pref_x[i] = pref_x[i - 1] + X[i - 1].second.first,       pref_y[i] = pref_y[i - 1] + X[i - 1].second.second;     for (int i = 0; i < X.size(); i++) {       ans[X[i].first.second] += pref_x[i] + i * X[i].second.second;       ans[X[i].first.second] += pref_y[X.size()] - pref_y[i + 1] +                                 (X.size() - 1 - i) * X[i].second.first;     }     for (int i = 0; i < q; i++) {       cin >> x >> y;       long long res =           min(tab[x].first + tab[y].second, tab[x].second + tab[y].first);       ans[x] -= res;       ans[y] -= res;     }     for (int i = 1; i <= X.size(); i++) cout << ans[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     long long b[n], c[n];     for (int i = 0; i < n; i++) cin >> b[i];     for (int i = 0; i < n; i++) cin >> c[i];     long long d[n], a[n], sum = 0;     for (int i = 0; i < n; i++) {       d[i] = b[i] + c[i];       sum += d[i];     }     sum /= (2 * n);     bool flag = true;     long long k[64];     for (int j = 0; j < 63; j++) k[j] = 0;     for (int i = 0; i < n; i++) {       a[i] = (d[i] - sum) / n;       if (a[i] < 0) {         flag = false;         break;       }       for (int j = 0; j < 63; j++)         if (a[i] & (1LL << j)) k[j]++;     }     for (int i = 0; i < n && flag; i++) {       long long num1 = 0, num2 = 0;       for (int j = 0; j < 63; j++) {         long long tmp = 1LL << j;         if (a[i] & tmp) {           num1 += k[j] << j;           num2 += n << j;         } else {           num2 += k[j] << j;         }       }       if (num1 != b[i] || num2 != c[i]) {         flag = false;         break;       }     }     if (flag) {       for (int i = 0; i < n; i++) cout << a[i] <<    ;       cout << endl;     } else       cout <<  -1 n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int pwr(long long int base, long long int p, long long int mod) {     long long int ans = 1;     while (p) {       if (p & 1) ans = (ans * base) % mod;       base = (base * base) % mod;       p /= 2;     }     return ans;   }   long long int gcd(long long int a, long long int b) {     if (b == 0) return a;     return gcd(b, a % b);   }   int main() {     int n;     scanf( %d , &n);     int a[n + 1];     int par[300005];     int cnt = 0;     int lvl = 0, flag = 0, p = -1;     vector<int> g[n + 2];     for (int i = 0; i <= n; i++) {       scanf( %d , &a[i]);       for (int j = 1; j <= a[i]; j++) {         g[i].push_back(++cnt);         par[cnt] = lvl;       }       lvl = cnt;       if (i >= 1) {         if (a[i] > 1 && a[i - 1] > 1) flag = i;       }     }     if (flag == 0) {       printf( perfect n );       return 0;     }     printf( ambiguous n );     for (int i = 1; i <= cnt; i++) {       printf( %d  , par[i]);     }     printf(  n );     par[g[flag][0]] = g[flag - 1][0];     for (int i = 1; i <= cnt; i++) {       printf( %d  , par[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int max_n = 1e5 + 10;   vector<long long> adj[max_n];   long long par[max_n], st[max_n], p[max_n], ft[max_n], doc[max_n], x[max_n],       l[max_n], r[max_n], cnt, tmp, k, n, m;   bool mark[max_n];   int get_par(int u) { return par[u] == u ? u : par[u] = get_par(par[u]); }   void dfs(long long v) {     st[v] = cnt++;     for (auto u : adj[v]) dfs(u);     ft[v] = cnt;   }   bool cmp(long long v, long long u) {     if (get_par(v) == get_par(u) && st[v] >= st[u] && ft[v] <= ft[u])       return true;     else       return false;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> n >> m;     for (int i = 1; i <= n; ++i) {       par[i] = i;       p[i] = i;     }     while (m--) {       int t;       cin >> t;       long long v, u;       if (t == 1) {         cin >> v >> u;         par[v] = u;         p[v] = u;       } else if (t == 2) {         cin >> v;         l[tmp] = v;         r[tmp++] = get_par(v);       } else {         cin >> x[k] >> doc[k];         doc[k++]--;       }     }     for (int i = 1; i <= n; ++i) {       if (p[i] != i) adj[p[i]].push_back(i);     }     for (int i = 1; i <= n; ++i) {       if (p[i] == i) {         cnt = 0;         dfs(i);       }     }     for (int i = 0; i < k; ++i) {       if (cmp(l[doc[i]], x[i]) && cmp(x[i], r[doc[i]]))         cout <<  YES  << endl;       else         cout <<  NO  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   void solve() {     string s1;     string s2;     cin >> s1 >> s2;     string ans =   ;     for (int i = s2.length() - 1; i >= 0; --i) {       ans += s2[i];     }     if (s1 == ans) {       cout <<  YES             <<   n ;     } else {       cout <<  NO             <<   n ;     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int t = 1;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int Abs(int x) {     if (x < 0) return -x;     return x;   }   int N, M, K;   bool no[2001][2001] = {false}, visited[2001] = {false}, rooted[2001] = {false},        notgo[2001];   vector<int> G[2001], g[2001];   int cn = 0;   bool run = true;   void dfs(int r) {     if (!run) return;     cn++;     visited[r] = true;     rooted[r] = true;     for (int i = 0; i < g[r].size(); i++) {       notgo[g[r][i]] = true;     }     for (int i = 0; i < G[r].size(); i++) {       if (notgo[G[r][i]]) run = false;       if (!visited[G[r][i]]) dfs(G[r][i]);     }   }   int main() {     scanf( %d , &N);     scanf( %d , &M);     for (int i = 0; i < M; i++) {       int v, u;       scanf( %d%d , &v, &u);       G[v].push_back(u);       G[u].push_back(v);     }     scanf( %d , &K);     for (int i = 0; i < K; i++) {       int v, u;       scanf( %d%d , &v, &u);       g[v].push_back(u);       g[u].push_back(v);     }     int ans = 0;     for (int i = 1; i <= N; i++) {       cn = 0;       run = true;       memset(visited, false, sizeof(visited));       memset(notgo, false, sizeof(notgo));       dfs(i);       if (run) ans = max(ans, cn);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   struct node {     T val;     T lazy;     int l, r;     node* left;     node* right;     node(int l, int r) {       this->l = l;       this->r = r;       this->left = nullptr;       this->right = nullptr;     }   };   template <class T, int SZ>   struct segtree {     T combIdentity = -1e9;     T comb(T l, T r) { return max(l, r); }     T pushIdentity = 0;     void push(node<T>* n) {       n->val += n->lazy;       if (n->l != n->r) {         n->left->lazy += n->lazy;         n->right->lazy += n->lazy;       }       n->lazy = pushIdentity;     }     node<T>* root;     segtree() {       int ub = 1;       while (ub < SZ) ub *= 2;       root = new node<T>(0, ub - 1);       root->val = pushIdentity;       root->lazy = pushIdentity;     }     void propagate(node<T>* n) {       if (n->l != n->r) {         int mid = ((n->l) + (n->r)) / 2;         if (n->left == nullptr) {           n->left = new node<T>(n->l, mid);           n->left->val = pushIdentity;           n->left->lazy = pushIdentity;         }         if (n->right == nullptr) {           n->right = new node<T>(mid + 1, n->r);           n->right->val = pushIdentity;           n->right->lazy = pushIdentity;         }       }       push(n);     }     void updN(node<T>* n, int i1, int i2, T val) {       propagate(n);       if (i2 < n->l || i1 > n->r) return;       if (i1 <= n->l && i2 >= n->r) {         n->lazy = val;         push(n);         return;       }       updN(n->left, i1, i2, val);       updN(n->right, i1, i2, val);       n->val = comb(n->left->val, n->right->val);     }     void upd(int i1, int i2, T val) { updN(root, i1, i2, val); }     T queryN(node<T>* n, int i1, int i2) {       propagate(n);       if (i2 < n->l || i1 > n->r) return combIdentity;       if (n->l >= i1 && n->r <= i2) return n->val;       T a = combIdentity;       if (n->left != nullptr) a = comb(a, queryN(n->left, i1, i2));       if (n->right != nullptr) a = comb(a, queryN(n->right, i1, i2));       return a;     }     T query(int i1, int i2) { return queryN(root, i1, i2); }   };   const int MAX_M = 20020;   const int MAX_N = 52;   int n, m, k;   int arr[MAX_N][MAX_M];   int pref[MAX_N][MAX_M];   int dp[MAX_N][MAX_M];   int getsum(int i, int j) {     if (j == 0)       return pref[i][j + k - 1];     else {       return pref[i][j + k - 1] - pref[i][j - 1];     }   }   int get(int i, int j) { return getsum(i, j) + getsum(i + 1, j); }   pair<int, int> getBounds(int j) {     int l = j - k + 1;     int r = j;     l = max(0, l);     r = min(m - k, r);     return make_pair(l, r);   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n >> m >> k;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) cin >> arr[i][j];     for (int i = 0; i < n; i++) {       pref[i][0] = arr[i][0];       for (int j = 1; j < m; j++) pref[i][j] = pref[i][j - 1] + arr[i][j];     }     for (int i = 0; i < m - k + 1; i++) dp[0][i] = get(0, i);     for (int i = 1; i < n; i++) {       segtree<int, MAX_M> st;       for (int j = 0; j < m - k + 1; j++) st.upd(j, j, dp[i - 1][j]);       for (int j = 0; j < k; j++) {         pair<int, int> b = getBounds(j);         st.upd(b.first, b.second, -arr[i][j]);       }       for (int j = 0; j < m - k + 1; j++) {         dp[i][j] = st.query(0, m - k) + get(i, j);         pair<int, int> b = getBounds(j);         st.upd(b.first, b.second, arr[i][j]);         b = getBounds(j + k);         st.upd(b.first, b.second, -arr[i][j + k]);       }     }     int res = 0;     for (int i = 0; i < m - k + 1; i++) res = max(res, dp[n - 1][i]);     return cout << res <<   n , 0;     ;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e3 + 5, mod = 1e9 + 7;   int n, m, k;   long long fact[maxn], dp[maxn], inv[maxn];   long long qpow(long long n, int p) {     long long res = 1;     while (p) {       if (p & 1) res = res * n % mod;       n = n * n % mod;       p >>= 1;     }     return res;   }   long long COM(int n, int m) {     if (m > n) return 0;     return fact[n] * inv[m] % mod * inv[n - m] % mod;   }   int main() {     scanf( %d%d%d , &n, &m, &k);     fact[0] = 1;     for (int i = 1; i < maxn; ++i) fact[i] = fact[i - 1] * i % mod;     inv[maxn - 1] = qpow(fact[maxn - 1], mod - 2);     for (int i = maxn - 2; i >= 0; --i) inv[i] = inv[i + 1] * (i + 1) % mod;     printf( %lld n , COM(n - 1, k << 1) * COM(m - 1, k << 1) % mod);   }


#include <bits/stdc++.h>   using namespace std;   const int M = 1600;   int dr[] = {-1, 0, 1, 0}, dc[] = {0, 1, 0, -1};   int n, m, pr[M][M], pc[M][M];   char g[M][M], f[M][M];   bool dfs(int r, int c) {     int u = (r % n + n) % n, v = (c % m + m) % m;     if ( #  == g[u][v]) return 0;     if (f[u][v]) return r != pr[u][v] || c != pc[u][v];     f[u][v] = 1, pr[u][v] = r, pc[u][v] = c;     for (int d = 0; d < 4; ++d)       if (dfs(r + dr[d], c + dc[d])) return 1;     return 0;   }   int main(void) {     cin >> n >> m;     for (int i = 0; i < n; ++i) cin >> g[i];     for (int i = 0; i < n; ++i)       for (int j = 0; j < m; ++j)         if ( S  == g[i][j])           cout << (dfs(i + n * 2, j + m * 2) ?  Yes  :  No ) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool visited[2010];   vector<int> g[2010];   int dfs(int src) {     visited[src] = true;     int ans = 0;     for (auto x : g[src]) {       if (!visited[x]) {         ans = max(ans, dfs(x));       }     }     return ans + 1;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n;     cin >> n;     for (int i = 0; i < n; ++i) {       int x;       cin >> x;       if (x == -1) x = 0;       g[i + 1].push_back(x);       g[x].push_back(i + 1);     }     cout << dfs(0) - 1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 10;   const long long mod = 1e9 + 7;   int prime[maxn], g[maxn], last[maxn], num[maxn], cnt[maxn];   long long fac[maxn], invf[maxn];   int t[500];   bool vis[maxn];   long long fpow(long long a, long long b, long long p = mod) {     long long ans = 1;     while (b) {       if (b & 1) ans = ans * a % p;       a = a * a % p;       b >>= 1;     }     return ans;   }   void init() {     int cnt = 0;     g[1] = 1;     last[1] = 1;     fac[1] = 1;     fac[0] = 1;     for (int i = 2; i < maxn; i++) {       fac[i] = fac[i - 1] * i % mod;       if (!vis[i]) prime[++cnt] = i, g[i] = i - 1, last[i] = 1;       for (int j = 1; j <= cnt && i * prime[j] < maxn; j++) {         int x = i * prime[j];         vis[x] = true;         if (i % prime[j] == 0) {           last[x] = last[i];           if (last[x] == 1)             g[x] = x - i;           else             g[x] = g[x / last[x]] * g[last[x]];           break;         }         last[x] = i;         g[x] = g[i] * g[prime[j]];       }     }     invf[maxn - 1] = fpow(fac[maxn - 1], mod - 2);     for (int i = maxn - 2; i >= 0; i--) invf[i] = invf[i + 1] * (i + 1) % mod;   }   long long C(int n, int m) {     return n < m ? 0 : fac[n] * invf[m] % mod * invf[n - m] % mod;   }   int getfac(int x) {     int len = 0;     for (int i = 1; i * i <= x; i++) {       if (x % i) continue;       t[++len] = i;       if (i * i != x) t[++len] = x / i;     }     sort(t + 1, t + 1 + len);     return len;   }   int main() {     std::ios::sync_with_stdio(false);     cout.tie(0);     init();     int n, k, q;     cin >> n >> k >> q;     for (int i = 1; i <= n; i++) {       int x;       cin >> x;       num[x]++;     }     for (int i = maxn - 1; i >= 1; i--) {       for (int j = i; j < maxn; j += i) cnt[i] += num[j];     }     long long ans = 0;     for (int i = 1; i < maxn; i++) {       ans = (ans + C(cnt[i], k) * g[i] % mod) % mod;     }     while (q--) {       int x;       cin >> x;       int len = getfac(x);       for (int i = len; i > 0; i--) {         long long dert = C(cnt[t[i]] + 1, k) - C(cnt[t[i]], k);         dert *= g[t[i]];         ans = (ans + dert + mod) % mod;         cnt[t[i]]++;       }       ans = (ans % mod + mod) % mod;       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int num[1 << 18];   char str[20];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     int x, y;     cin >> x >> y;     int a, b, c, ans = 0;     a = b = c = y;     while (a < x) {       swap(a, b);       swap(b, c);       c = a + b - 1;       ans++;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int v[100005], b[100005];   int k, n, m, maxim;   int main() {     cin >> n >> m;     for (int i = 1; i <= m; i++) {       int nr;       cin >> nr;       b[nr]++;       v[b[nr]]++;       if (v[b[nr]] == n)         cout << 1;       else         cout << 0;     }   }


#include <bits/stdc++.h>   using namespace std;   template <class T1, class T2>   ostream& operator<<(ostream& o, pair<T1, T2> x) {     return o << x.first <<     << x.second;   }   template <class T1, class T2>   istream& operator>>(istream& o, pair<T1, T2>& x) {     return o >> x.first >> x.second;   }   template <class T1, class T2>   pair<T1, T2> operator+(pair<T1, T2> a, pair<T1, T2> b) {     a.first += b.first;     a.second += b.second;     return a;   }   template <class T1, class T2>   pair<T1, T2> operator-(pair<T1, T2> a, pair<T1, T2> b) {     a.first -= b.first;     a.second -= b.second;     return a;   }   template <class T1, class T2>   void operator+=(pair<T1, T2>& a, pair<T1, T2> b) {     a.first += b.first;     a.second += b.second;   }   template <class T1, class T2>   void operator-=(pair<T1, T2>& a, pair<T1, T2> b) {     a.first -= b.first;     a.second -= b.second;   }   template <long long int D, typename T>   struct Vec : public vector<Vec<D - 1, T>> {     static_assert(D >= 1,  Vector dimension must be greater than zero! );     template <typename... Args>     Vec(long long int n = 0, Args... args)         : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {}   };   template <typename T>   struct Vec<1, T> : public vector<T> {     Vec(long long int n = 0, const T& val = T()) : vector<T>(n, val) {}   };   const long long int md = 998244353;   inline long long int add(long long int x, long long int y) {     x += y;     if (x >= md) x -= md;     return x;   }   inline long long int sub(long long int x, long long int y) {     x -= y;     if (x < 0) x += md;     return x;   }   inline long long int mul(long long int x, long long int y) {     return (x * y) % md;   }   inline long long int power(long long int x, long long int y) {     long long int res = 1;     for (; y; y >>= 1, x = mul(x, x)) {       if (y & 1) res = mul(res, x);     }     return res;   }   inline long long int inv(long long int a) { return power(a, md - 2); }   void solve() {     long long int a, b, x, y, ans;     cin >> a >> b >> x >> y;     ans = max(b * max(x, a - x - 1), a * (max(y, b - y - 1)));     cout << ans <<   n ;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int T;     cin >> T;     while (T--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int search(int a[], int x, int n) {     int lo = 0;     int hi = n;     while (lo <= hi) {       int mid = (lo + hi) / 2;       if (a[mid] >= x && a[mid - 1] < x)         return mid;       else if (a[mid] > x)         hi = mid - 1;       else         lo = mid + 1;     }     return 0;   }   int main() {     int n, m;     int a[100000];     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];     }     cin >> m;     int b[100000];     int ans[100000];     for (int i = 0; i < m; i++) {       cin >> b[i];     }     for (int i = 1; i < n; i++) {       a[i] += a[i - 1];     }     for (int i = 0; i < m; i++) {       ans[i] = search(a, b[i], n);     }     for (int i = 0; i < m; i++) {       cout << ans[i] + 1 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int res[50010], cot;   int main() {     string a, b;     int n;     cin >> n;     cin >> a;     cin >> b;     int s1, s2, s3;     s1 = s2 = s3 = 0;     int sa = 0, sb = 0;     for (int i = 0; i < n; i++) {       if (a[i] ==  1 ) sa++;       if (b[i] ==  1 ) sb++;       if (a[i] ==  1  && b[i] ==  1 )         s3++;       else if (a[i] ==  1 )         s1++;       else if (b[i] ==  1 )         s2++;     }     int ans[5];     int flag = -1, s4 = n - s1 - s2 - s3;     for (int i = 0; flag == -1 && i <= min(s3, n / 2); i++) {       int res1 = i, res2 = (s3 - i);       if (res1 + s1 < res2) continue;       if (res2 + s2 < res1) continue;       memset(ans, 0, sizeof ans);       ans[3] += res1;       int ss1 = s1, ss2 = s2;       if (res1 < res2) {         ss1 -= (res2 - res1);         ans[1] += res2 - res1;         res1 = res2;         int temp = min(ss1, ss2);         ans[1] += temp;         ss1 -= temp;         ss2 -= temp;         if (s4 >= max(ss1, ss2)) {           if (ss1) {             ans[4] += (s4 + ss1) / 2;           } else {             ans[2] += ss2;             ans[4] += (s4 - (s4 + ss2) / 2);           }           flag = 1;         }       } else {         ss2 -= (res1 - res2);         res2 = res1;         int temp = min(ss1, ss2);         ans[1] += temp;         ss1 -= temp;         ss2 -= temp;         if (s4 >= max(ss1, ss2)) {           if (ss1) {             ans[4] += (s4 + ss1) / 2;           } else {             ans[2] += ss2;             ans[4] += (s4 - (s4 + ss2) / 2);           }           flag = 1;         }       }     }     if (flag == -1)       puts( -1 );     else {       for (int i = 0; i < n; i++) {         if (a[i] ==  1  && b[i] ==  1  && ans[3]) {           res[++cot] = i + 1;           ans[3]--;           continue;         }         if (a[i] ==  1  && b[i] ==  0  && ans[1]) {           res[++cot] = i + 1;           ans[1]--;           continue;         }         if (a[i] ==  0  && b[i] ==  1  && ans[2]) {           res[++cot] = i + 1;           ans[2]--;           continue;         }         if (a[i] ==  0  && b[i] ==  0  && ans[4]) {           res[++cot] = i + 1;           ans[4]--;           continue;         }       }       for (int i = 1; i <= cot; i++) cout << res[i] <<    ;       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using std::max;   const int MAXN = 500111;   int N;   struct Data {     int Cnt, Id;     long long Val;     int next;     Data() {}     Data(int _c, int _i, long long _v) {       Cnt = _c;       Id = _i;       Val = _v;     }   } T[MAXN << 2];   int Tcnt;   namespace Map {   struct Vert {     int FE;     long long Val;   } V[MAXN];   struct Edge {     int x, y;     long long l;     int next;   } E[MAXN];   int Ecnt;   void addE(int a, int b, long long c) {     ++Ecnt;     E[Ecnt].x = a;     E[Ecnt].y = b;     E[Ecnt].l = c;     E[Ecnt].next = V[a].FE;     V[a].FE = Ecnt;   }   void DFS(int at) {     for (int k = V[at].FE, to; k > 0; k = E[k].next) {       to = E[k].y;       V[to].Val = V[at].Val + E[k].l;       DFS(to);     }   }   }  // namespace Map   namespace Tree {   struct Vert {     int FE;     int Fa, Son;     int Dep, Len;     int Head;     long long Ans;   } V[MAXN];   struct Edge {     int x, y, next;   } E[MAXN << 1];   int Ecnt;   void addE(int a, int b) {     ++Ecnt;     E[Ecnt].x = a;     E[Ecnt].y = b;     E[Ecnt].next = V[a].FE;     V[a].FE = Ecnt;   }   void DFS1(int at) {     for (int k = V[at].FE, to; k > 0; k = E[k].next) {       to = E[k].y;       if (to == V[at].Fa) continue;       V[to].Dep = V[at].Dep + 1;       V[to].Fa = at;       DFS1(to);       V[at].Len = max(V[at].Len, V[to].Len + 1);       if (V[at].Son <= 0 || V[V[at].Son].Len < V[to].Len) V[at].Son = to;     }   }   void Merge(int p, Data &A, Data &d) {     d.Val += 1LL * V[p].Dep * A.Cnt;     A.Val += 1LL * V[p].Dep * d.Cnt;     Map::addE(A.Id, d.Id, d.Val - A.Val);     A.Cnt += d.Cnt;   }   void DFS2(int at) {     if (V[at].Son) DFS2(V[at].Son);     T[++Tcnt] = Data(1, at, 0);     V[at].Head = Tcnt;     T[Tcnt].next = V[V[at].Son].Head;     for (int k = V[at].FE, to; k > 0; k = E[k].next) {       to = E[k].y;       if (to == V[at].Fa || to == V[at].Son) continue;       DFS2(to);       for (int a = T[V[at].Head].next, p = V[to].Head; p > 0;            a = T[a].next, p = T[p].next) {         Merge(at, T[a], T[p]);       }     }   }   void DFS3(int at) {     for (int k = V[at].FE, to; k > 0; k = E[k].next) {       to = E[k].y;       if (to == V[at].Fa) continue;       V[to].Ans = V[at].Ans + Map::V[to].Val + V[at].Dep;       DFS3(to);     }   }   }  // namespace Tree   int main() {     scanf( %d , &N);     int Root;     for (int i = 1, f; i <= N; ++i) {       scanf( %d , &f);       if (f == 0)         Root = i;       else {         Tree::addE(i, f);         Tree::addE(f, i);       }     }     Tree::V[Root].Dep = 1;     Tree::DFS1(Root);     Tree::DFS2(Root);     for (int p = Tree::V[Root].Head; p > 0; p = T[p].next) {       Map::V[T[p].Id].Val = T[p].Val;       Map::DFS(T[p].Id);     }     Tree::DFS3(Root);     for (int i = 1; i <= N; ++i) printf( %I64d  , Tree::V[i].Ans);     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int read();   int n;   char t[102];   int w[102][102], rnk[102][102], pos[102];   vector<int> p[102];   queue<int> q;   void init(int t) {     for (int i = 1; i <= n; ++i) {       p[i].clear(), p[i + n].clear();       for (int j = 1; j <= n; ++j) p[i].push_back(j + n), p[i + n].push_back(j);       sort(p[i].begin(), p[i].end(),            [&](int a, int b) { return w[i][a - n] > w[i][b - n] ^ t; });       sort(p[i + n].begin(), p[i + n].end(),            [&](int a, int b) { return w[a][i] < w[b][i] ^ t; });       q.push(i), pos[i] = 0, pos[i + n] = n;       for (int j = 0; j < n; ++j)         rnk[j][p[i][j]] = j, rnk[i + n][p[i + n][j]] = j;     }     while (!q.empty()) {       int u = q.front(), v;       while (pos[v = p[u][pos[u]]] < rnk[v][u]) ++pos[u];       if (pos[v] < n) q.push(p[v][pos[v]]);       pos[v] = rnk[v][u], q.pop();     }   }   int main() {     for (int T = read(); T; --T) {       n = read();       for (int i = 1; i <= n; ++i)         for (int j = 1; j <= n; ++j) w[i][j] = read();       puts( B ), fflush(stdout), scanf( %s , t);       if (t[0] ==  I )         for (int i = 1; i <= n; ++i)           for (int j = 1; j <= n; ++j) w[i][j] = -w[i][j];       int u;       for (init((u = read()) > n); ~u; u = read())         printf( %d n , p[u][pos[u]]), fflush(stdout);     }     return 0;   }   int read() {     int x = 0, f = 1;     char c = getchar();     while (c <  0  || c >  9 ) f = (c ==  - ) ? -1 : f, c = getchar();     while (c >=  0  && c <=  9 ) x = x * 10 + c -  0 , c = getchar();     return x * f;   }


#include <bits/stdc++.h>   using namespace std;   long long prime[1000005];   long long fac[1000005];   int mul(int a, int b) {     int res = 0;     while (b) {       if (b & 1) res = (res + a);       a = (a + a);       b = b / 2;     }     return res;   }   int bexp(int a, int b) {     int res = 1;     while (b) {       if (b & 1) res = mul(res, a);       a = mul(a, a);       b = b / 2;     }     return res;   }   int inv(int a) { return bexp(a, 1000000007 - 2); }   int nCr(int n, int r) { return mul(fac[n], mul(inv(fac[n - r]), inv(fac[r]))); }   long long power(long long a, long long b, long long c) {     long long res = 1;     while (b) {       if (b & 1) res = (res * a) % c;       a = (a * a) % c;       b /= 2;     }     return res % c;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long n, m, k;     cin >> n >> m >> k;     long long a[n];     vector<pair<long long, long long>> v;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     long long p = n - k;     if (p == 0) {       cout << n << endl;       return 0;     }     for (int i = 2; i <= n; i++)       v.push_back(make_pair(a[i] - a[i - 1] + 1, i - 1));     sort(v.begin(), v.end());     long long count = 0;     long long sum = 0;     vector<pair<long long, long long>> final;     for (int i = 0; i < v.size(); i++) {       sum += v[i].first;       final.push_back(make_pair(v[i].second, v[i].first));       count++;       if (count == p) break;     }     long long check = 2;     sort(final.begin(), final.end());     for (int i = 1; i < final.size(); i++) {       if (final[i].first - final[i - 1].first == 1) {         sum -= 1;         check += 1;       } else         check += 2;     }     cout << sum + n - check << endl;   }


#include <bits/stdc++.h>   using namespace std;   int const N = 100;   int const M = 900;   typedef double (*F)[N];   int a[N];   double _f[N][N], _g[N][N];   F f = _f, g = _g;   inline int c2(int n) { return n * (n + 1) >> 1; }   int main() {     int n, k;     scanf( %d%d , &n, &k);     for (int i = 0; i < n; ++i) scanf( %d , a + i);     int n2 = c2(n);     for (k = min(k, M); k--;) {       for (int i = 0; i < n; ++i)         for (int j = i + 1; j < n; ++j) {           g[i][j] = (c2(i) + c2(n - 1 - j) + c2(j - i - 1)) * f[i][j];           for (int x = i; x < i + j; ++x) {             int a = max(0, x - j + 1), b = min(i, x - i);             g[i][j] += (b - a + 1) * f[x - i][j];           }           for (int x = i + j + 1; x < n + j; ++x) {             int a = max(i + 1, x - n + 1), b = min(j, x - j);             g[i][j] += (b - a + 1) * f[i][x - j];           }           for (int x = j; x < n + i; ++x) {             int a = max(0, x - n + 1), b = min(i, x - j);             g[i][j] += (b - a + 1) * (1 - f[x - j][x - i]);           }           g[i][j] /= n2;         }       swap(f, g);     }     double e = 0;     for (int i = 0; i < n; ++i)       for (int j = i + 1; j < n; ++j) {         e += a[i] > a[j] ? 1 - f[i][j] : f[i][j];       }     printf( %.16f n , e);     return 0;   }


#include <bits/stdc++.h>   const int N = 100010;   int n;   int head[N], to[2 * N], next[2 * N], graphmr;   double ans;   inline void add(int x, int y) {     int p = graphmr++;     to[p] = y;     next[p] = head[x];     head[x] = p;   }   inline void dfs(int x, int fa, int dis) {     ans += 1.0 / dis;     for (int i = head[x]; ~i; i = next[i])       if (to[i] != fa) dfs(to[i], x, dis + 1);   }   int main() {     scanf( %d , &n);     memset(head, -1, sizeof head);     for (int i = 1, x, y; i < n; i++) {       scanf( %d%d , &x, &y);       add(x, y);       add(y, x);     }     dfs(1, 0, 1);     printf( %lf n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[300001];   int f[300001];   vector<int> g[33];   map<vector<int>, int> mp;   vector<pair<vector<int>, int> > v;   vector<int> cur;   int br = 0;   int n;   void F(int pos, int mask) {     if (pos == br) {       v.push_back({cur, mask});       return;     }     F(pos + 1, mask);     if (g[pos].empty() || g[pos].size() == n) return;     for (auto to : g[pos]) cur[to]++;     F(pos + 1, (mask ^ (1 << pos)));     for (auto to : g[pos]) cur[to]--;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i <= n; i++) f[i] = __builtin_popcount(a[i]);     set<int> st;     for (int i = 1; i <= n; i++) st.insert(f[i] % 2);     if (st.size() == 2) {       cout << -1;       return 0;     }     for (int i = 1; i <= n; i++) f[i] /= 2;     for (int i = 1; i <= n; i++) {       for (int j = 0; j < 30; j++) {         if (a[i] & (1 << j)) continue;         g[j].push_back(i - 1);       }     }     cur.resize(n);     for (int i = 0; i < n; i++) cur[i] = 0;     br = 15;     F(0, 0);     for (auto cur : v) {       mp[cur.first] = cur.second;     }     for (int i = 0; i < n; i++) cur[i] = 0;     br = 30;     F(15, 0);     for (auto x : v) {       vector<int> cur = x.first;       int mask = x.second;       for (int t = 0; t <= 60; t++) {         vector<int> to(n, t);         for (int i = 0; i < n; i++) to[i] -= f[i + 1] + cur[i];         if (mp.find(to) != mp.end()) {           cout << (mask ^ mp[to]);           return 0;         }       }     }     cout << -1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, c;     cin >> n >> c;     int a[n];     int cnt = 0;     vector<int> b;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 1; i < n; i++) {       if (abs(a[i] - a[i - 1]) <= c)         cnt++;       else {         cnt = 0;       }     }     cout << cnt + 1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string str;     int n;     cin >> n >> str;     int A[n], m = 0;     string RE[n];     A[0] = 0;     for (int i = 1, t = 0, tmp =  [ ; i < n; i++) {       if (str[i] == tmp) {         if (str[i] ==  ] )           t--;         else           t++;       }       A[i] = t;       if (t > m) m = t;       tmp = str[i];     }     for (int i = 0; i < m + 2; i++) {       for (int j = 0; j < n; j++) {         if (A[j] == i) {           if (str[j] ==  [ )             RE[i].append( +- );           else if (str[j] ==  ]  && str[j - 1] ==  [ )             RE[i].append(  -+ );           else             RE[i].append( -+ );         } else if (A[j] > i) {           if (str[j - 1] == str[j])             RE[i].append(   );           else if (str[j - 1] ==  [  && str[j] ==  ] )             RE[i].append(    );           else             RE[i].append(     );         } else {           if (str[j - 1] ==  [  && str[j] ==  ] ) {             RE[i].append(    | );           } else {             RE[i].append( | );           }         }       }       cout << RE[i] << endl;     }     for (int i = m; i >= 0; i--) {       cout << RE[i] << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int root, n, m, tot, ans, a[2000010], zy[2000010];   bool b[2000010], v[2000010];   void dfs(int k) {     if (b[a[k]]) {       b[a[k]] = v[a[k]] = false;       dfs(a[k]);       v[a[k]] = true;     } else {       if (!v[a[k]]) {         tot++;         if (root == 0) {           root = k;           zy[k] = k;         } else           zy[k] = root;       }     }   }   int work(int k) {     if (v[a[k]]) {       v[a[k]] = false;       return (work(a[k]));     } else       return k;   }   int main(int argc, const char* argv[]) {     memset(b, true, sizeof(b));     int i, j, k;     tot = 0;     cin >> n;     root = 0;     for (i = 1; i <= n; ++i) {       scanf( %d , &a[i]);       if (a[i] == i) root = i;       zy[i] = a[i];     }     if (root != 0) tot = -1;     for (i = 1; i <= n; ++i)       if (b[i]) {         b[i] = v[i] = false;         dfs(i);         v[i] = true;       }     cout << tot << endl;     for (i = 1; i <= n; ++i) cout << zy[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   const double PI = 3.14159265359;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     vector<int> facts;     for (int i = 1; i * i <= n; i++) {       if (!(n % i)) {         facts.push_back(i);         facts.push_back(n / i);       }     }     vector<long long> ans;     for (int i : facts) {       long long a = (n - 1) / i;       ans.push_back(a * (a + 1) * i / 2 + a + 1);     }     sort(ans.begin(), ans.end());     auto it = unique(ans.begin(), ans.end());     ans.resize(it - ans.begin());     for (auto i : ans) {       cout << i <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[100005], n, x, minn = 3;   bool m1[100005], m2[100005];   int read() {     int w = 0, f = 1;     char c = getchar();     while (c >  9  || c <  0 ) {       if (c ==  - ) f = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       w = (w << 3) + (w << 1) + (c ^ 48);       c = getchar();     }     return w * f;   }   int main() {     n = read(), x = read();     for (int i = 1; i <= n; i++) {       a[i] = read();     }     for (int i = 1; i <= n; i++) {       if (m1[a[i]]) minn = min(0, minn);       if (m2[a[i]]) minn = min(1, minn);       if (m1[a[i] & x]) minn = min(1, minn);       if (m2[a[i] & x]) minn = min(2, minn);       m1[a[i]] = 1, m2[a[i] & x] = 1;     }     if (minn == 3)       puts( -1 );     else       printf( %d n , minn);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string str;     cin >> str;     int count = 0;     while (str.size() > 1) {       string temp;       int num = 0;       count++;       for (int i = 0; i < str.size(); i++) {         num += str[i] - 48;       }       str =   ;       str = to_string(num);     }     cout << count << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve(long long k) {     for (long long n = 3; n <= 2000; ++n) {       for (long long a = 2; a < n; ++a) {         if ((n + k) % a == 0) {           long long y = (n + k) / a;           if (y <= 1000000 && (n - a) * y >= (a - 1) &&               n * (y - 1) > (n - a) * y) {             vector<long long> ans(n, 0);             ans[0] = 1;             ans[a - 1] = -2;             ans[a] = y;             cout << n <<   n ;             for (long long x : ans) cout << x <<    ;             cout <<   n ;             return;           }         }       }     }     cout << k << endl;     exit(0);   }   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     long long k;     cin >> k;     solve(k);   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e18, N = 1e6, mod = 1e9 + 7;   const long double PI = acos(-1), eps = -1e9;   inline void read(long long &a) {     char c;     bool neg = 0;     c = getchar();     a = 0;     if (c ==  - ) {       neg = 1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       a = (a << 3) + (a << 1) + (c -  0 );       c = getchar();     }     if (neg) a *= -1;   }   long long a[N];   void solve() {     long long n;     read(n);     long long sum = 0, cur = 0;     for (long long i = 1; i <= n; i++) {       long long x;       read(x);       if (a[x] != 0) {         cur -= a[x];       }       a[x] = i;       cur += a[x];       sum += cur;     }     sum -= n;     sum *= 2;     sum += n;     cout << setprecision(20) << (long double)sum / ((long double)n * n);   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t = 1;     while (t--) {       solve();     }     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   int main() {     int num;     int a = 0;     int b = 0;     int n;     cin >> n;     int sol[n];     for (a = 0; a < n; a++) {       cin >> num;       sol[num - 1] = (a + 1);     }     for (b = 0; b < n; b++) {       cout << sol[b] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 200005;   const int E = 80;   vector<long long> pr, f;   mt19937_64 gen(time(0));   long long a[maxn];   map<long long, long long> val;   int n;   long long solve(long long p) {     if (val.find(p) != val.end()) return val[p];     long long ret = 0;     for (int i = 0; i < n; i++) {       if (a[i] < p)         ret += p - a[i];       else {         long long h = a[i] % p;         ret += min(h, p - h);       }     }     val[p] = ret;     return ret;   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %lld , &a[i]);     for (int i = 0; i < n; i++) {       f.push_back(a[i] + 1);       if (a[i] > 1) f.push_back(a[i]);       if (a[i] > 2) f.push_back(a[i] - 1);     }     int m = f.size();     long long ans = n;     for (int _ = 0; _ < E; _++) {       long long x = f[gen() % m], j = 2;       f.clear();       while (j * j <= x) {         if (x % j == 0) {           while (x % j == 0) x /= j;           pr.push_back(j);         }         ++j;       }       if (x > 1) pr.push_back(x);       for (auto p : pr) ans = min(ans, solve(p));     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main(int argc, char const *argv[]) {     int n;     scanf( %d , &n);     long long int a[n], b[n], p = 0, q = 0;     for (int i = 0; i < n; ++i) {       scanf( %I64d , &a[i]);       p |= a[i];     }     for (int i = 0; i < n; ++i) {       scanf( %I64d , &b[i]);       q |= b[i];     }     printf( %I64d n , p + q);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int max_n = 1e5 + 10;   const long long inf = 10000000000000000;   vector<int> adj[max_n];   vector<long long> cst[max_n];   vector<int> arr[max_n];   vector<long long> closest[max_n];   void cmp_closest(int i) {     int s = arr[i].size();     if (s > 0) {       closest[i].reserve(s);       closest[i][s - 1] = arr[i][s - 1] + 1;       for (int j = s - 2; j >= 0; --j) {         if (arr[i][j] + 1 != arr[i][j + 1]) {           closest[i][j] = arr[i][j] + 1;         } else {           closest[i][j] = closest[i][j + 1];         }       }     }   }   int n, m;   void add_edge(int a, int b, int c) {     adj[a].push_back(b);     cst[a].push_back(c);     adj[b].push_back(a);     cst[b].push_back(c);   }   void read() {     ios_base::sync_with_stdio(false);     cin >> n >> m;     for (int i = 0; i < m; ++i) {       int a, b, c;       cin >> a >> b >> c;       --a;       --b;       add_edge(a, b, c);     }     for (int i = 0; i < n; ++i) {       int k;       cin >> k;       for (int j = 0; j < k; ++j) {         long long t;         cin >> t;         arr[i].push_back(t);       }       cmp_closest(i);     }   }   long long dist[max_n];   struct cmpl {     inline bool operator()(int a, int b) const {       return (dist[a] < dist[b] || (dist[a] == dist[b] && a < b));     }   };   int cost(int u, size_t i) {     vector<int>::iterator it = lower_bound(arr[u].begin(), arr[u].end(), dist[u]);     if (it != arr[u].end() && *it == dist[u]) {       return cst[u][i] + (closest[u][it - arr[u].begin()]) - dist[u];     } else {       return cst[u][i];     }   }   set<int, cmpl> pq;   int main() {     read();     for (int i = 1; i < n; ++i) {       dist[i] = inf;     }     pq.insert(0);     int res = -1;     while (!pq.empty()) {       int curr = *pq.begin();       pq.erase(pq.begin());       if (curr == n - 1) {         res = dist[curr];         break;       } else {         for (size_t i = 0; i < adj[curr].size(); ++i) {           int v = adj[curr][i];           int c = cost(curr, i);           if (dist[curr] + c < dist[v]) {             pq.erase(v);             dist[v] = dist[curr] + c;             pq.insert(v);           }         }       }     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<long long, vector<int> > > out;   int n, k;   int a[64];   bool found(long long x) {     for (int i = 0; i < out.size(); i++)       if (out[i].first == x) return true;     return false;   }   int main() {     cin >> n >> k;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; out.size() < k && i < n; i++) {       int sz = out.size();       if (found(a[i]) == false) {         vector<int> push;         push.push_back(i);         out.push_back(make_pair(a[i], push));       }       for (int j = 0; out.size() < k && j < sz; j++) {         if (found(a[i] + out[j].first) == false) {           out.push_back(out[j]);           out[out.size() - 1].second.push_back(i);           out[out.size() - 1].first += a[i];         }       }     }     for (int i = 0; i < out.size(); i++) {       cout << out[i].second.size();       for (int j = 0; j < out[i].second.size(); j++)         cout <<     << a[out[i].second[j]];       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   static const int INF = 0x3f3f3f3f;   static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;   void solve4() {     string s;     cin >> s;     int c = 0;     int n = int((s).size());     for (char ch : s) {       c += (ch ==  a );     }     long long ans;     cerr <<  n           <<  =  << n <<  ,           <<  c           <<  =  << c << endl;     if (c > n / 2)       ans = n;     else       ans = 2 * c - 1;     cout << ans << endl;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     solve4();   }


#include <bits/stdc++.h>   using namespace std;   int n, m, t;   set<pair<int, int> > s;   string s1;   vector<int> v, v1;   int kek[100000];   int main() {     cin >> n >> m >> t;     for (int i = 1; i <= n; i++) {       cin >> s1;       int cur = 0;       cur += 60 * 60 * 10 * (s1[0] -  0 );       cur += 60 * 60 * (s1[1] -  0 );       cur += 60 * 10 * (s1[3] -  0 );       cur += 60 * (s1[4] -  0 );       cur += 10 * (s1[6] -  0 );       cur += (s1[7] -  0 );       v.push_back(cur);     }     s.clear();     int cur = 0;     int ans = 0;     int mx = 0;     for (int i = 0; i < v.size(); i++) {       while (!s.empty()) {         set<pair<int, int> >::iterator it = (--s.end());         if (-(*it).first < v[i])           s.erase(it);         else           break;         cur--;       }       if (cur < m) {         ans++;         kek[i] = ans;         cur++;         s.insert(make_pair(-(v[i] + t - 1), ans));       } else {         kek[i] = (*s.begin()).second;         s.erase(s.begin());         s.insert(make_pair(-(v[i] + t - 1), kek[i]));       }       mx = max(mx, cur);     }     if (mx != m) {       cout <<  No solution ;       return 0;     }     cout << ans <<   n ;     for (int i = 0; i < v.size(); i++) cout << kek[i] <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 60;   long long h, n;   int step[MAXN];   inline long long calc(long long high) { return (1ll << (high + 1)) - 1; }   void solve() {     long long tmp = (1ll << h) - 1 + n;     int cnt = 0;     while (tmp != 1) {       if (tmp % 2 == 1)         step[cnt++] = 1;       else         step[cnt++] = 0;       tmp /= 2;     }     long long res = 0;     int curdir = 0, dep = 0;     for (int i = cnt - 1; i >= 0; i--) {       if (curdir == step[i]) {         dep++;         res++;         curdir ^= 1;       } else {         long long tmp = calc(h - dep - 1);         res += tmp;         curdir = step[i];         i++;       }     }     cout << res << endl;   }   int main() {     cin >> h >> n;     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int t;     t = 1;     while (t--) {       long long int n, c1 = 0, c2 = 0, x;       cin >> n;       while (n--) {         cin >> x;         if (x == 1)           c1++;         else           c2++;       }       x = min(c1, c2);       c1 -= x;       c2 -= x;       cout << x + (c1 / 3);     }     return 0;   }