#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1000000007;   const int INF = 1e9;   const long long LINF = 1LL * INF * INF;   const int MAXN = 100007;   const double EPS = 1e-7;   int A[MAXN];   int main() {     ios_base::sync_with_stdio(0);     int n, m;     cin >> n >> m;     if (n == 1 && m == 1) {       cout <<  YES n1 ;       return 0;     }     if (n == 1 && m <= 3 || m == 1 && n <= 3 || n == 2 && m <= 3 ||         m == 2 && n <= 3) {       cout <<  NO ;       return 0;     }     if (n == 1 && m == 4) {       cout <<  YES n2 4 1 3 ;       return 0;     }     if (n == 4 && m == 1) {       cout <<  YES n2 n4 n1 n3 ;       return 0;     }     if (n == 2 && m == 4) {       cout <<  YES n ;       cout <<  5 4 7 2  << endl;       cout <<  3 6 1 8 ;       return 0;     }     if (n == 4 && m == 2) {       cout <<  YES n ;       cout <<  6 1 n ;       cout <<  3 8 n ;       cout <<  2 5 n ;       cout <<  7 4 n ;       return 0;     }     if (n == 3 && m == 3) {       cout <<  YES n ;       cout <<  2 9 4 n ;       cout <<  7 5 3 n ;       cout <<  6 1 8 n ;       return 0;     }     if (n == 3 && m == 4) {       cout <<  YES n ;       cout <<  6 8 11 2 n ;       cout <<  9 3 1 10 n ;       cout <<  4 12 7 5 n ;       return 0;     }     if (n == 4 && m == 3) {       cout <<  YES n ;       cout <<  10 3 9 n ;       cout <<  5 1 7 n ;       cout <<  12 6 11 n ;       cout <<  4 8 2 n ;       return 0;     }     if (n == 4 && m == 4) {       cout <<  YES n ;       cout <<  2 11 14 9 n ;       cout <<  16 6 8 1 n ;       cout <<  13 3 10 4 n ;       cout <<  7 5 12 15 n ;       return 0;     }     if (n >= 5) {       cout <<  YES n ;       for (int i = (0); i < (2); ++i) {         for (int f = i * m + 1; f <= n * m; f += 2 * m) {           for (int j = (1); j < (m + 1); ++j) {             int cur = f + j - 1;             if ((j + 1) & 1) cur += m;             if (cur > n * m) cur -= n * m;             cout << cur <<    ;           }           cout << endl;         }       }       return 0;     }     if (m >= 5) {       cout <<  YES n ;       for (int i = (1); i < (n + 1); ++i) {         int cur = 1;         for (int j = (1); j < (m + 1); ++j) {           int c = cur;           cur += 2;           if (cur > m) cur = 2;           if ((i + 1) & 1) ++c;           if (c > m) c -= m;           cout << (i - 1) * m + c <<    ;         }         cout << endl;       }       return 0;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 2e5 + 5;   int n, k, a;   int vis_time[MAX], vis_cnt[MAX], ans;   struct data {     int size, op;     bool operator<(data b) const {       if (size == b.size)         return op > b.op;       else         return size < b.size;     }   };   priority_queue<data> line;   int main() {     scanf( %d%d , &n, &k);     for (register int i = 1; i <= n; ++i) {       scanf( %d , &a);       line.push((data){a, 0});     }     ans = 0x3f3f3f3f;     while (!line.empty()) {       data tmp = line.top();       line.pop();       int size = tmp.size, op = tmp.op;       vis_time[size]++;       vis_cnt[size] += op;       if (vis_time[size] == k) {         ans = min(ans, vis_cnt[size]);       }       if (!size)         continue;       else         line.push((data){size / 2, op + 1});     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long dp[100000 + 25];   struct node {     int iz, der;     long long tiempo;     node() {}     node(long long t, int i, int d) {       tiempo = t;       iz = i;       der = d;     }   };   struct otro {     int proct, pos;     long long tiemp;     otro() {}     otro(int ps, long long time, int timprt) {       pos = ps;       tiemp = time;       proct = timprt;     }     bool operator<(const otro& algo) const { return algo.tiemp < tiemp; }   };   vector<node> v;   int n, m, ini, fin;   int valoreleg;   char eleg;   char ch[500000];   map<long long, int> mp;   void f() {     long long tiemp = 1;     int c = 0;     int pos = ini;     while (true) {       if (v[mp[tiemp]].iz <= pos && v[mp[tiemp]].der >= pos) {         ch[c++] =  X ;         tiemp++;         continue;       }       pos += valoreleg;       ch[c] = eleg;       if (v[mp[tiemp]].iz <= pos && v[mp[tiemp]].der >= pos) {         pos -= valoreleg;         ch[c] =  X ;       }       c++;       tiemp++;       if (pos == fin) {         for (int a = 0; a < c; a++) cout << ch[a];         cout << endl;         return;       }     }   }   int main() {     cin >> n >> m >> ini >> fin;     int li, ri;     long long tim;     v.push_back(node(0, 0, 0));     for (int a = 0; a < m; a++) {       cin >> tim >> li >> ri;       mp[tim] = a + 1;       v.push_back(node(tim, li, ri));     }     if (ini < fin) {       valoreleg = 1;       eleg =  R ;     } else {       valoreleg = -1;       eleg =  L ;     }     f();   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int q;     cin >> q;     while (q--) {       int a, b, n, s;       cin >> a >> b >> n >> s;       if (s - n * min(a, s / n) <= b)         cout <<  YES n ;       else         cout <<  NO n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, cnt, ans;   int a[410][410], c[160010];   int now, f[2][410][410];   void Init() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j) scanf( %d , &a[i][j]);   }   inline void insert(int x) {     if (c[x]) ++cnt;     ++c[x];   }   inline void erase(int x) {     --c[x];     if (c[x]) --cnt;   }   void Work() {     int p;     for (int i = 1; i <= n; ++i) {       p = 1;       for (int j = 1; j <= m; ++j) {         insert(a[i][j]);         while (cnt) {           f[0][i][p] = j - 1;           erase(a[i][p]);           ++p;         }       }       while (p <= m) {         f[0][i][p] = m;         erase(a[i][p]);         ++p;       }       for (int j = 1; j <= m; ++j) ans = max(ans, f[0][i][j] - j + 1);     }   }   void Solve() {     int p;     for (int s = 2; s <= n; ++s) {       now ^= 1;       for (int i = 1; i <= n - s + 1; ++i) {         int j = i + s - 1;         for (int k = 1; k <= m; ++k)           f[now][i][k] = min(f[now ^ 1][i][k], f[now ^ 1][i + 1][k]);         p = 1;         for (int k = 1; k <= m; ++k) {           insert(a[i][k]);           insert(a[j][k]);           while (cnt) {             f[now][i][p] = min(f[now][i][p], k - 1);             erase(a[i][p]);             erase(a[j][p]);             ++p;           }         }         while (p <= m) {           f[now][i][p] = min(f[now][i][p], m);           erase(a[i][p]);           erase(a[j][p]);           ++p;         }         for (int k = 1; k <= m; ++k) ans = max(ans, s * (f[now][i][k] - k + 1));       }     }     printf( %d n , ans);   }   int main() {     Init();     Work();     Solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 61;   int a[N], n;   long long dp[N][2][2], k;   long long Dfs(int pos, int x, int y) {     if (pos < n - pos + 1) return 1;     if (dp[pos][x][y] != -1) return dp[pos][x][y];     long long ans = 0;     for (int i = 0; i < 2; i++)       if (a[pos] == -1 || a[pos] == i)         for (int j = 0; j < 2; j++)           if (a[n - pos + 1] == -1 || a[n - pos + 1] == j) {             if (x && (i > j) || y && (i > !j) || pos == n - pos + 1 && (i != j))               continue;             ans += Dfs(pos - 1, x && (i == j), y && (i != j));           }     return dp[pos][x][y] = ans;   }   int main() {     scanf( %d%lld , &n, &k);     k++;     memset(dp, -1, sizeof(dp));     memset(a, -1, sizeof(a));     a[n] = 0;     if (Dfs(n, 1, 1) < k)       puts( -1 );     else {       for (int i = n - 1; i > 0; i--) {         a[i] = 0;         memset(dp, -1, sizeof(dp));         long long num = Dfs(n, 1, 1);         if (k > num) k -= num, a[i] = 1;       }       for (int i = n; i >= 1; i--) printf( %d , a[i]);       puts(  );     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i, j;     string s1, s2;     cin >> n >> s1 >> s2;     if (s2 ==  week ) {       if (n == 5 || n == 6) {         cout <<  53  << endl;       } else {         cout <<  52  << endl;       }     } else {       if (n <= 29) {         cout <<  12  << endl;       } else if (n == 30) {         cout <<  11  << endl;       } else if (n == 31) {         cout <<  7  << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200002;   long long sum, ans, nd, T;   priority_queue<long long> q;   int n, k, b, c, t[N], i, j;   int main() {     scanf( %d%d%d%d , &n, &k, &b, &c), ans = 1e18;     b = min(b, c * 5);     for (i = 1; i <= n; i++) scanf( %d , &t[i]);     sort(t + 1, t + n + 1);     for (i = 0; i < 5; i++) {       T = 2e9 + i;       while (!q.empty()) q.pop();       sum = 0;       for (j = 1; j <= n; j++) {         nd = (T - t[j]) / 5 * b + (T - t[j]) % 5 * c;         sum += nd;         q.push(nd);         if (q.size() > k) sum -= q.top(), q.pop();         if (q.size() == k) ans = min(ans, sum - (T - t[j]) / 5 * b * k);       }     }     printf( %lld , ans);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long q = 0;     vector<long long> a;     a.push_back(0);     for (long long i = 0; i < 4; i++) {       long long te;       cin >> te;       a.push_back(te);     }     string s;     cin >> s;     for (long long i = 0; i < s.size(); i++) {       q += a[s[i] -  0 ];     }     cout << q;     return 0;     ;   }


#include <bits/stdc++.h>   using namespace std;   typedef int64_t g;   g n,p[269420],v=1e9+7,T,r,i,k,s;   map<g,g> l;   int main(){   cin>>T;   while(T--){   cin>>n;   l.clear();   for(i=0,s=0,p[0]=1,l[0]=1;++i<=n;){   cin>>k;   p[i]=(p[i-1]*2+v-p[l[s]-1])%v;   l[s]=i;   s+=k;   }cout<<p[n]<<endl;   }   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;   const int n_ = 1e5 + 1000;   const long double PI = acos(-1.0);   long long gcd(long long a, long long b) { return (a ? gcd(b % a, a) : b); }   long long power(long long a, long long n) {     long long p = 1;     while (n > 0) {       if (n % 2) {         p = p * a;       }       n >>= 1;       a *= a;     }     return p;   }   long long power(long long a, long long n, long long mod) {     long long p = 1;     while (n > 0) {       if (n % 2) {         p = p * a;         p %= mod;       }       n >>= 1;       a *= a;       a %= mod;     }     return p % mod;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, h;     cin >> n >> h;     int res = 0;     for (int(i) = 0; (i) < (n); (i)++) {       int a;       cin >> a;       if (a > h)         res += 2;       else         res++;     }     cout << res << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     double d, h, v, e;     cin >> d >> h >> v >> e;     d /= 2;     double vol = d * d * acos(-1) * h;     double incre = e * d * d * acos(-1);     if (incre >= v) {       cout <<  NO ;       return 0;     }     double ans = vol / (v - incre);     if (ans > 1e4) {       cout <<  NO ;       return 0;     }     cout <<  YES n ;     cout << setprecision(15) << ans;   }


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


#include <bits/stdc++.h>   using namespace std;   int co;   bool query(int x, int y) {     assert(++co <= 60);     printf( 1 %d %d n , x, y);     fflush(stdout);     char st[5];     scanf( %s , st);     return st[0] ==  N ;   }   int main() {     int fst, scnd, n, k;     scanf( %d %d , &n, &k);     int l = 1, r = n;     while (l < r) {       int m = (l + r) / 2;       if (query(m, m + 1))         l = m + 1;       else         r = m;     }     fst = l;     l = 1, r = fst - 1;     while (l < r) {       int m = (l + r) / 2;       if (query(m, m + 1))         l = m + 1;       else         r = m;     }     if (l == r && !query(l, l + 1)) {       printf( 2 %d %d n , fst, l);       fflush(stdout);       return 0;     }     l = fst + 1, r = n;     while (l < r) {       int m = (l + r) / 2;       if (query(m, m + 1))         l = m + 1;       else         r = m;     }     printf( 2 %d %d n , fst, l);     fflush(stdout);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     vector<int> arrNums;     int iTemp, nNeed, nNum, i;     long long iSum = 0;     cin >> nNum >> nNeed;     while (nNum--) {       cin >> iTemp;       arrNums.push_back(iTemp);     }     sort(arrNums.begin(), arrNums.end());     for (i = 0; i < nNeed; ++i) {       iSum += arrNums[i];     }     cout << iSum;   endapp:     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, Bef[201000];   long long w[201000];   map<pair<int, int>, int> Map[201000];   struct Tree {     int K[262144 + 262144], T[262144 + 262144];     void init() {       for (int i = 0; i < 262144 + 262144; i++) K[i] = -1;     }     void Put2(int nd, int x) { K[nd] = T[nd] = x; }     void UDT(int nd) { T[nd] = (T[nd * 2] | T[nd * 2 + 1]); }     void Spread(int nd) {       if (K[nd] != -1) {         Put2(nd * 2, K[nd]);         Put2(nd * 2 + 1, K[nd]);       }       K[nd] = -1;     }     void Put(int nd, int b, int e, int s, int l, int x) {       if (s > l) return;       if (s <= b && e <= l) {         Put2(nd, x);         return;       }       Spread(nd);       int m = (b + e) >> 1;       if (s <= m) Put(nd * 2, b, m, s, l, x);       if (l > m) Put(nd * 2 + 1, m + 1, e, s, l, x);       UDT(nd);     }     int Left(int nd, int b, int e, int s, int l) {       if (!T[nd]) return l + 1;       if (s > l) return l + 1;       if (b == e) return b;       Spread(nd);       int m = (b + e) >> 1;       if (s <= m) {         int x = Left(nd * 2, b, m, s, l);         if (x <= l) return x;       }       if (l > m) {         return Left(nd * 2 + 1, m + 1, e, s, l);       }       return l + 1;     }   } IT;   struct Range {     int b, e;   };   vector<Range> Save;   long long Sum1(int b, int e) {     return 1ll * ((n - b + 1) + (n - e + 1)) * (e - b + 1) / 2;   }   void Calc(int b, long long x, int e, long long y) {     int i;     for (i = 0; i <= e - b - 1; i++) {       long long t2 = Sum1(b + 1, b + i) + x;       long long t1 = Sum1(e - i, e - 1) + x;       long long tp = n - e + 1;       if (t1 + tp <= y && y <= t2 + tp) {         Map[e][{e, e}] = b;         Save.push_back({e, e});       }       long long bbb = n + 1 + t1 - y + 1;       long long eee = n + 1 + t2 - y + 1;       bbb = max(bbb, (long long)e + 1);       eee = min(eee, (long long)n + 1);       if (bbb <= eee) {         int b1 = bbb, e1 = eee;         Map[e][{b1, e1}] = b;         Save.push_back({b1, e1});       }     }   }   void Do(int b, int e) {     Save.clear();     if (IT.Left(1, 0, n + 1, b, b) == b) {       Calc(b, w[b], e, w[e]);     }     int pp = IT.Left(1, 0, n + 1, b + 1, e - 1);     if (b + 1 <= pp && pp <= e - 1) {       Calc(pp, w[b] - 1, e, w[e]);     }     if (IT.Left(1, 0, n + 1, e, e) == e && w[e] != w[b] - 1) {       IT.Put(1, 0, n + 1, e, e, 0);     }     if (w[b] != w[e]) {       IT.Put(1, 0, n + 1, e + 1, n + 1, 0);     }     IT.Put(1, 0, n + 1, 0, e - 1, 0);     for (auto &t : Save) {       IT.Put(1, 0, n + 1, t.b, t.e, 1);     }   }   int Res[201000];   void Print2(long long b, long long e, int c, long long s) {     if (c == 0) return;     long long t2 = Sum1(b + 1, b + c - 1);     long long t1 = Sum1(e - c + 2, e);     if (t1 <= s - (n - b + 1) && s - (n - b + 1) <= t2) {       Res[b] = 1;       Print2(b + 1, e, c - 1, s - (n - b + 1));     } else {       Print2(b + 1, e, c, s);     }   }   void Print(int ty, long long s1, int x, int y, long long s2) {     Res[ty] = Res[y] = 1;     long long ss = s2 - (n - y + 1) - s1;     int i;     for (i = 0; i <= x - ty - 1; i++) {       long long t2 = Sum1(ty + 1, ty + i);       long long t1 = Sum1(x - i, x - 1);       if (t1 <= ss && ss <= t2) {         Print2(ty + 1, x - 1, i, ss);         return;       }     }   }   int RR[101000];   long long Res2[101000];   int main() {     int i, DEB = 0;     scanf( %d , &n);     mt19937 rng((unsigned)time(NULL));     if (DEB) {       long long ss = 0;       for (i = 1; i <= n + 1; i++) {         RR[i] = !!(rng() % 5);         if (i == n + 1) RR[i] = 1;         if (RR[i]) {           ss += (n - i + 1);           Res2[i] = ss;         }       }       for (i = 1; i <= n; i++) printf( %d , RR[i]);       puts(  );       long long z = Res2[n + 1];       for (i = n; i >= 1; i--) {         if (Res2[i])           z = Res2[i];         else           Res2[i] = z + 1;       }     }     int ck = 0;     for (i = 1; i <= n; i++) {       if (!DEB) {         scanf( %lld , &w[i]);       }       if (DEB) {         if (rng() % 3 == 0) {           w[i] = Res2[i];         }         printf( %lld  , w[i]);       }       if (w[i] != 0 && w[i] != 1) ck = 1;     }     if (!ck) {       for (i = 1; i <= n; i++) printf( 0 );       puts(  );       return 0;     }     IT.Put(1, 0, n + 1, 0, 0, 1);     int prv = 0;     for (i = 1; i <= n; i++) {       if (!w[i]) continue;       Do(prv, i);       Bef[i] = prv;       prv = i;     }     int x = prv, y = IT.Left(1, 0, n + 1, 0, n + 1);     while (x) {       for (auto &t : Map[x]) {         pair<int, int> tp = t.first;         if (tp.first <= y && y <= tp.second) {           int ty = t.second;           long long s1 = w[Bef[x]], s2 = w[x];           if (ty != Bef[x]) {             s1--;           }           if (y != x) {             s2--;           }           Print(ty, s1, x, y, s2);           y = ty;           break;         }       }       x = Bef[x];     }     for (i = 1; i <= n; i++) printf( %d , Res[i]);     puts(  );   }


#include <bits/stdc++.h>   using namespace std;   const int N = 10010, M = 15010, inf = 1e9;   struct st1 {     int to, next, c;   } e[M << 1];   struct st2 {     int to, next, f, op;   } F[1000001];   int n, m, x, y, z, cnt, cnt2, ans, i, col, cyc, S, T, fi[N], c[N], l[N],       op[M << 1], d[N + M], f2[N + M], dep[N + M], num[N + M];   bool vis[N], u[M << 1];   void addl(int x, int y, int z) {     e[++cnt].to = y, e[cnt].next = fi[x], e[cnt].c = z, fi[x] = cnt;   }   void addf(int x, int y, int f) {     F[++cnt2].to = y, F[cnt2].next = f2[x], F[cnt2].f = f, F[cnt2].op = cnt2 + 1,     f2[x] = cnt2;     F[++cnt2].to = x, F[cnt2].next = f2[y], F[cnt2].f = 0, F[cnt2].op = cnt2 - 1,     f2[y] = cnt2;   }   void dfs(int x, int fa, int d) {     vis[x] = 1;     for (int i = fi[x]; i; i = e[i].next)       if (!u[i] && e[i].to != fa) {         int y = e[i].to;         if (vis[y]) {           cyc++, u[i] = u[op[i]] = 1;           addf(S, cyc, dep[x] - dep[y]);           for (int j = dep[y] + 1; j <= dep[x]; j++)             u[l[j]] = u[op[l[j]]] = 1, addf(cyc, c[j] + 1, 1);           addf(cyc, e[i].c + 1, 1);         } else {           c[dep[y] = d + 1] = e[i].c, l[d + 1] = i, dfs(y, x, d + 1);           if (!u[i]) addf(S, ++cyc, 1), addf(cyc, e[i].c + 1, 1);         }       }   }   int sap(int x, int del) {     if (x == T) return del;     int sum = 0, md = cyc + 1;     for (int i = f2[x]; i; i = F[i].next)       if (F[i].f) {         if (d[F[i].to] == d[x] - 1) {           int save = sap(F[i].to, min(F[i].f, del - sum));           sum += save, F[i].f -= save, F[F[i].op].f += save;           if (d[S] >= cyc + 1 || sum == del) return sum;         }         md = min(md, d[F[i].to]);       }     if (!sum) {       if (!(--num[d[x]]))         d[S] = cyc + 1;       else         num[d[x] = md + 1]++;     }     return sum;   }   int main() {     scanf( %d%d n , &n, &m);     for (i = 1; i <= m; i++) {       scanf( %d%d%d n , &x, &y, &z);       addl(x, y, z), addl(y, x, z);       col = max(col, z);     }     for (i = 1; i <= cnt; i += 2) op[op[i] = i + 1] = i;     S = 0, T = 1, cyc = 1 + col;     for (i = 1; i <= col; i++) addf(i + 1, T, 1);     dfs(1, 0, 0);     num[0] = cyc + 1;     for (; d[S] <= cyc;) ans += sap(S, inf);     printf( %d , ans);   }


#include <bits/stdc++.h>   using namespace std;   int N, M, M2, B;   const int MAXN = 100015;   const int MAXM = 20;   const int MAXM2 = (1 << 20);   char S[MAXN];   int bitc[MAXM2];   int bitl[MAXM2];   int dp[MAXM2];   int cnt[MAXM][MAXM];   int mcnt[MAXM2][MAXM];   int last;   int main() {     cin >> N >> M >> S;     M2 = (1 << M);     B = M2 - 1;     for (int i = 0; i < M; ++i)       for (int j = 0; j < M; ++j) cnt[i][j] = 0;     bitc[0] = 0;     bitl[0] = 0;     dp[0] = 0;     for (int j = 0; j < M; ++j) mcnt[0][j] = 0;     for (int i = 1; i < M2; ++i) {       int b = i & (i - 1);       bitc[i] = bitc[b] + 1;       for (int j = 0; j < M; ++j)         if ((1 << j) & i) {           bitl[i] = j;           break;         }       for (int j = 0; j < M; ++j) mcnt[i][j] = 0;       dp[i] = 1e9;     }     for (int i = 0; i < N - 1; ++i) {       if (S[i] == S[i + 1]) continue;       ++cnt[S[i] -  a ][S[i + 1] -  a ];       ++cnt[S[i + 1] -  a ][S[i] -  a ];     }     for (int i = 1; i < M2; ++i)       for (int j = 0; j < M; ++j)         mcnt[i][j] = mcnt[i & (i - 1)][j] + cnt[bitl[i]][j];     for (int i = 0; i < M2; ++i)       for (int j = 0; j < M; ++j) {         if (((1 << j) & i) == 0)           dp[i | (1 << j)] = min(dp[i | (1 << j)],                                  dp[i] + bitc[i] * (mcnt[i][j] - mcnt[B ^ i][j]));       }     cout << dp[B] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   int a[10005];   int num[10005];   int main() {     int n;     scanf( %d , &n);     int mm = 0;     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       num[a[i]]++;       mm = max(mm, a[i]);     }     int nn = 0;     for (int i = 1; i <= mm; i++) {       if (mm % i == 0) num[i]--;     }     for (int i = 10000; i >= 1; i--) {       if (num[i] > 0) {         nn = i;         break;       }     }     printf( %d %d n , mm, nn);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   namespace fastIO {   bool IOerror = 0;   inline char nc() {     static char buf[100000], *p1 = buf + 100000, *pend = buf + 100000;     if (p1 == pend) {       p1 = buf;       pend = buf + fread(buf, 1, 100000, stdin);       if (pend == p1) {         IOerror = 1;         return -1;       }     }     return *p1++;   }   inline bool blank(char ch) {     return ch ==     || ch ==   n  || ch ==   r  || ch ==   t ;   }   inline void read(long long &x) {     char ch;     long long f = 1;     while (blank(ch = nc()))       ;     while (ch ==  - ) f = -f, ch = nc();     if (IOerror) return;     for (x = ch -  0 ; (ch = nc()) >=  0  && ch <=  9 ; x = x * 10 + ch -  0 )       ;     x *= f;   }   inline void read(int &x) {     char ch;     int f = 1;     while (blank(ch = nc()))       ;     while (ch ==  - ) f = -f, ch = nc();     if (IOerror) return;     for (x = ch -  0 ; (ch = nc()) >=  0  && ch <=  9 ; x = x * 10 + ch -  0 )       ;     x *= f;   }   inline void read(char *s) {     char ch;     while (blank(ch = nc()))       ;     if (IOerror) return;     int i = 0;     for (; !blank(ch); s[i++] = ch, ch = nc())       ;     s[i] =   0 ;   }   };  // namespace fastIO   using namespace fastIO;   inline long long read() {     long long x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   int n;   int p[300010];   int c[500010];   int ans[300010];   class Treap {    public:     struct treap_point {       int l, r, x, rep, son, rnd;     } t[500010];     int treesz, root;     inline void update(int k) {       t[k].son = t[t[k].l].son + t[t[k].r].son + t[k].rep;     }     inline void right_rotate(int &k) {       int tt = t[k].l;       t[k].l = t[tt].r;       t[tt].r = k;       update(k);       update(tt);       k = tt;     }     inline void left_rotate(int &k) {       int tt = t[k].r;       t[k].r = t[tt].l;       t[tt].l = k;       update(k);       update(tt);       k = tt;     }     inline void insert_to_tree(int &k, int x) {       if (!k) {         k = ++treesz;         t[k].x = x;         t[k].rnd = rand();         t[k].son = t[k].rep = 1;         t[k].l = t[k].r = 0;         return;       }       t[k].son++;       if (x == t[k].x) {         t[k].rep++;         return;       }       if (x < t[k].x) {         insert_to_tree(t[k].l, x);         if (t[t[k].l].rnd < t[k].rnd) right_rotate(k);       }       if (x > t[k].x) {         insert_to_tree(t[k].r, x);         if (t[t[k].r].rnd < t[k].rnd) left_rotate(k);       }     }     inline bool delete_from_tree(int &k, int x) {       if (!k) return 0;       if (x == t[k].x) {         if (t[k].rep > 1) {           t[k].rep--;           t[k].son--;           return 1;         }         if (!t[k].l || !t[k].r) {           k = t[k].l + t[k].r;           return 1;         }         if (t[t[k].l].rnd < t[t[k].r].rnd) {           right_rotate(k);           return delete_from_tree(k, x);         }         left_rotate(k);         return delete_from_tree(k, x);       }       int res;       if (x < t[k].x) {         res = delete_from_tree(t[k].l, x);         if (res) t[k].son--;         return res;       }       res = delete_from_tree(t[k].r, x);       if (res) t[k].son--;       return res;     }     inline int get_succ_in_tree(int k, int x) {       if (!k) return -1;       int sv;       if (x < t[k].x) {         sv = get_succ_in_tree(t[k].l, x);         return sv == -1 ? t[k].x : sv;       }       return get_succ_in_tree(t[k].r, x);     }     inline int get_pred_in_tree(int k, int x) {       if (!k) return -1;       int sv;       if (x > t[k].x) {         sv = get_pred_in_tree(t[k].r, x);         return sv == -1 ? t[k].x : sv;       }       return get_pred_in_tree(t[k].l, x);     }     inline int ask_kth_in_tree(int k, int x) {       if (!k) return -1;       if (x <= t[t[k].l].son) return ask_kth_in_tree(t[k].l, x);       if (x > t[t[k].l].son + t[k].rep)         return ask_kth_in_tree(t[k].r, x - t[t[k].l].son - t[k].rep);       return t[k].x;     }     inline int ask_rank_in_tree(int k, int x) {       if (!k) return -1;       if (x == t[k].x) return t[t[k].l].son + 1;       if (x < t[k].x) return ask_rank_in_tree(t[k].l, x);       return t[t[k].l].son + t[k].rep + ask_rank_in_tree(t[k].r, x);     }     inline void init() { treesz = root = 0; }     inline void insert(int x) { insert_to_tree(root, x); }     inline int del(int x) { return delete_from_tree(root, x); }     inline int succ(int x) { return get_succ_in_tree(root, x); }     inline int pred(int x) { return get_pred_in_tree(root, x); }     inline int ask_kth(int x) { return ask_kth_in_tree(root, x); }     inline int ask_rank(int x) { return ask_rank_in_tree(root, x); }   } T;   int main() {     srand(time(0));     n = read();     for (int i = 1; i <= n; i++) T.insert(i);     ans[1] = 1;     for (int i = 1; i <= n; i++) {       p[i] = read();       T.del(p[i]);       ans[i + 1] = max(0, (T.ask_kth(n - i) - n + i)) + 1;     }     for (int i = 1; i <= n + 1; i++) printf( %d  , ans[i]);   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int m, k;     scanf( %d %d , &m, &k);     vector<int> a(k, 0);     vector<int> t(m - 1), r(m - 1, 0);     vector<int> last(k, 0);     for (int i = 0; i < k; ++i) {       scanf( %d , &a[i]);     }     int free_num = 0;     for (int i = 0; i + 1 < m; ++i) {       scanf( %d %d , &t[i], &r[i]);       if (t[i] > 0) {         last[t[i] - 1] = i;       }     }     int first_upset_id = -1;     int before_upset_num = 0;     for (int i = 0; i + 1 < m; ++i) {       if (r[i] == 1) {         first_upset_id = i;         break;       } else if (t[i] == 0) {         ++before_upset_num;       }     }     for (int i = 0; i + 1 < m; ++i) {       if (t[i] == 0) {         ++free_num;       } else {         --a[t[i] - 1];       }     }     if (first_upset_id >= 0) {       int min_id = -1;       for (int i = 0; i < k; ++i) {         if ((last[i] < first_upset_id) &&             ((min_id == -1) || (a[min_id] > a[i]))) {           min_id = i;         }       }       free_num -= a[min_id];     }     for (int i = 0; i < k; ++i) {       if ((a[i] <= free_num) ||           ((last[i] < first_upset_id) && (a[i] <= before_upset_num))) {         printf( Y );       } else {         printf( N );       }     }     printf(  n );   }   int main() {     int t;     scanf( %d , &t);     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct om_nom {     int t;     int h;     int m;   } a[2005];   int n, x;   bool eaten[2005];   bool have_candy(int type) {     int flag = false;     for (int i = 0; i < n; ++i)       if (a[i].t == (bool)type && !eaten[i]) {         flag = true;         break;       }     return flag;   }   int solve(int cur, int H) {     int cnt = 0;     for (int i = 0; i < n; ++i) {       eaten[i] = false;     }     while (have_candy(cur)) {       int mm = -1, it = -1;       for (int i = 0; i < n; ++i)         if (a[i].t == cur && a[i].m > mm && a[i].h <= H && !eaten[i]) {           it = i;           mm = a[i].m;         }       if (it != -1) {         H += a[it].m;         cur ^= 1;         eaten[it] = true;       } else         break;       cnt++;     }     return cnt;   }   int main() {     cin >> n >> x;     for (int i = 0; i < n; ++i) {       cin >> a[i].t >> a[i].h >> a[i].m;     }     cout << max(solve(0, x), solve(1, x)) << endl;     return 0;   }


#include <bits/stdc++.h>   inline int read() {     int f = 1, x = 0;     char ch = getchar();     while (!(ch >=  0  && ch <=  9 )) {       if (ch ==  - ) f *= -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return f * x;   }   int dx[8] = {1, 1, -1, -1, 2, 2, -2, -2};   int dy[8] = {2, -2, 2, -2, 1, -1, 1, -1};   struct Point {     int x, y, c, fa;     friend bool operator==(Point a, Point b) {       return (a.x == b.x && a.y == b.y);     }   } w[2], b[2], list[4][1000 * 1000 + 1];   int otherx, othery;   void printlist(int k, Point a) {     if (a.fa == -1) return;     if (a.fa != 1) printlist(k, list[k][a.fa]);     printf( %d %d n , a.x, a.y);   }   int n, m, head, tail, v[4][1000 + 1][1000 + 1];   void closer(int k, Point st, Point ed) {     Point a, b;     head = tail = 1;     memset(v[k], 0, sizeof(v[k]));     v[k][st.x][st.y] = head;     list[k][head] = st, list[k][head].c = 0, list[k][head].fa = -1;     if (st == ed) return;     while (head <= tail) {       a = list[k][head];       for (int i = 0; i < 8; i++) {         b = a, b.x += dx[i], b.y += dy[i], b.c++;         if (b.x < 1 || b.y < 1 || b.x > n || b.y > m || v[k][b.x][b.y] != 0)           continue;         tail++;         v[k][b.x][b.y] = tail;         b.fa = head;         list[k][tail] = b;         if (b == ed) return;       }       head++;     }   }   void printlast(int k) {     if (k == 0) {       printf( %d %d n , b[0].x - 1, b[0].y + 2);       printf( %d %d n , b[0].x - 3, b[0].y + 1);       printf( %d %d n , b[0].x - 1, b[0].y);     } else {       printf( %d %d n , w[0].x + 1, w[0].y + 2);       printf( %d %d n , w[0].x + 3, w[0].y + 1);       printf( %d %d n , w[0].x + 1, w[0].y);     }   }   bool choose;   int main() {     n = read(), m = read(), w[1].x = read(), w[1].y = read(), b[1].x = read(),     b[1].y = read();     w[0].x = n / 2, w[0].y = m / 2, b[0].x = n / 2 + 1, b[0].y = m / 2;     closer(0, w[1], w[0]), closer(1, b[1], b[0]);     closer(2, w[1], b[0]);     closer(3, b[1], w[0]);     if ((w[1].x + w[1].y) % 2 != (b[1].x + b[1].y) % 2) {       if (list[0][v[0][w[0].x][w[0].y]].c <= list[1][v[1][b[0].x][b[0].y]].c)         printf( WHITE n ), printlist(0, list[0][v[0][w[0].x][w[0].y]]);       else if (list[2][v[2][b[0].x][b[0].y]].c >=                list[1][v[1][b[0].x][b[0].y]].c + 2)         printf( BLACK n ), printlist(1, list[1][v[1][b[0].x][b[0].y]]);       else         printf( WHITE n ), printlist(2, list[2][v[2][b[0].x][b[0].y]]),             printlast(0);     } else {       if (list[1][v[1][b[0].x][b[0].y]].c < list[0][v[0][w[0].x][w[0].y]].c)         printf( BLACK n ), printlist(1, list[1][v[1][b[0].x][b[0].y]]);       else if (list[3][v[3][w[0].x][w[0].y]].c > list[0][v[0][w[0].x][w[0].y]].c)         printf( WHITE n ), printlist(0, list[0][v[0][w[0].x][w[0].y]]);       else         printf( BLACK n ), printlist(3, list[3][v[3][w[0].x][w[0].y]]),             printlast(1);     }     fflush(stdout);   }


#include <bits/stdc++.h>   #pragma comment(linker,  /stack:200000000 )   #pragma GCC optimize( Ofast )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4 )   using namespace std;   const int inf = (int)2e9 + 99999, mod = (int)1e9 + 7;   const long long linf = (long long)2e18 + 99999;   int kob(int x, int y) { return ((long long)x * y) % mod; }   int aza(int x, int y) {     x -= y;     if (x < 0) return x + mod;     return x;   }   int kos(int x, int y) {     x += y;     if (x >= mod) return x - mod;     return x;   }   const int maxn = (int)1e5 + 777, maxn2 = (int)1e6 + 55;   int main() {     int a1, a2, a3, a4;     cin >> a1 >> a2 >> a3 >> a4;     int a11 = a1, a22 = a2, a33 = a3, a44 = a4;     a1 -= a3;     a2 -= a3;     bool bad = false;     if (a3 <= a4) bad = 1, --a1;     if (a3 < a4) --a2;     if (abs(a3 - a4) >= 2 || a1 < 0 || a2 < 0) {       if (a33 == a44 && a11 < a22) {         a11 -= a33;         a22 -= a33;         a22--;         if (a11 < 0 || a22 < 0) {           cout << -1;           exit(0);         }         for (int i = 1; i <= a33; ++i) cout <<  74 ;         for (int i = 1; i <= a11; ++i) cout << 4;         cout << 7;         for (int i = 1; i <= a22; ++i) cout << 7;         exit(0);       }       cout <<  -1 , exit(0);     }     if (a3 < a4) cout <<  7 ;     for (int i = 1; i <= a1; ++i) cout <<  4 ;     for (int i = 1; i <= a3; ++i) cout <<  47 ;     for (int i = 1; i <= a2; ++i) cout <<  7 ;     if (bad) cout <<  4 ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 10;   const int SQRTN = 320;   const int LOGN = 20;   const double PI = acos(-1);   int n, val;   map<int, int> dp;   int dp2[N];   int range[N];   bool en[N];   int grundy(long long n) {     if (n < 1e6) return dp2[n];     if (dp.find(n) != dp.end()) return dp[n];     int& mex = dp[n];     int st = 0;     double e = sqrt(n);     double s = sqrt(e);     int i = ceil(s), _ = floor(e);     while (i < n && i <= _) {       i = range[i];       st = st | (1 << grundy(i));       i++;     }     while (st & (1 << mex)) mex++;     return mex;   }   int calc(int n) {     int& mex = dp2[n];     int st = 0;     double e = sqrt(n);     double s = sqrt(e);     for (int i = ceil(s), _ = floor(e); i < n && i <= _; i++) {       st = st | (1 << dp2[i]);     }     while (st & (1 << mex)) mex++;     return mex;   }   int main() {     for (int i = 0; i < 1000000; i++) {       calc(i);     }     range[999999] = 999999;     en[999999] = 1;     for (int i = 999998; i > -1; i--) {       if (dp2[i] != dp2[i + 1])         range[i] = i, en[i] = 1;       else         range[i] = range[i + 1];     }     cin >> n;     long long a;     for (int i = 0; i < n; i++) {       scanf( %lld , &a);       val = val ^ grundy(a);     }     cout << (val ?  Furlo  :  Rublo ) << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long cross2(long long x1, long long y1, long long x2, long long y2) {     return x1 * y2 - x2 * y1;   }   long long cross(long long i, long long j, long long k, const long long* xx,                   const long long* yy) {     return cross2(xx[j] - xx[i], yy[j] - yy[i], xx[k] - xx[i], yy[k] - yy[i]);   }   int main() {     int n;     cin >> n;     auto* xx = new long long[n];     auto* yy = new long long[n];     for (int i = 0; i < n; i++) {       cin >> xx[i];       cin >> yy[i];     }     bool flip = xx[0] < xx[1];     long long x = xx[1], y = yy[1];     for (int i = 0; i < n; i++) {       xx[i] -= x;       yy[i] -= y;       if (flip) {         xx[i] = -xx[i];         yy[i] = -yy[i];       }     }     long long left = 0, right = xx[0];     for (int i = 2; i + 1 < n; i++) {       long long cright = cross(i, i + 1, 0, xx, yy);       long long cleft = cross(i, i + 1, 1, xx, yy);       if (cright > 0 && cleft > 0) {         cout << 0;         return 0;       }       long long x0 = xx[i], y0 = yy[i], x1 = xx[i + 1], y1 = yy[i + 1];       if (cright > 0 || cleft > 0) {         long long p = x0 * (y1 - y0) - y0 * (x1 - x0);         long long q = y1 - y0;         if (q < 0) {           p = -p;           q = -q;         }         if (cright > 0)           right = min(right, (p / q));         else           left = max(left, ((p + q - 1) / q));       }     }     cout << (left <= right ? right - left + 1 : 0) << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000010;   bitset<N> b;   int p[N];   int cnt[N];   bool was[N];   int main() {     int n, k;     scanf( %d %d , &n, &k);     for (int i = 0; i < n; i++) {       scanf( %d , p + i);       p[i]--;       was[i] = false;     }     for (int i = 1; i <= n; i++) {       cnt[i] = 0;     }     int c1 = 0, c2 = 0;     for (int i = 0; i < n; i++) {       if (was[i]) {         continue;       }       int x = i;       int len = 0;       while (!was[x]) {         was[x] = true;         x = p[x];         len++;       }       c1 += len % 2;       c2 += len / 2;       cnt[len]++;     }     b.set(0);     for (int i = 1; i <= n; i++) {       if (cnt[i] == 0) {         continue;       }       int j = 1;       while (cnt[i] > 0) {         j = min(j, cnt[i]);         int u = i * j;         b |= (b << u);         cnt[i] -= j;         j *= 2;       }     }     int ans_min = k;     if (!b.test(k)) {       ans_min++;     }     int ans_max = 0;     ans_max += 2 * min(k, c2);     k -= min(k, c2);     ans_max += min(k, c1);     printf( %d %d n , ans_min, ans_max);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[100001], first, space, op = 1, last;   int main() {     long long k, n, page;     cin >> k >> n >> page;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     if (n == 0) {       cout << 0;       return 0;     }     first = (a[1] - space) / page;     if ((a[1] - space) % page) first++;     for (int i = 1; i <= n; i++) {       long long pg_now = (a[i] - space) / page;       if ((a[i] - space) % page) pg_now++;       if (pg_now == first)         last++;       else {         space = last;         first = (a[i] - space) / page;         if ((a[i] - space) % page) first++;         last++;         op++;       }     }     cout << op;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int md = 1000000007;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int tt{}, T{};     tt = 1;     while (tt--) {       int n{}, m{}, ans{}, k{}, a{}, b{}, c{}, f{};       cin >> n;       if (n == 1) {         cout <<  1 n ;         return 0;       }       if (n == 2) {         cout <<  2 1 n ;         return 0;       }       a = sqrt(n);       b = n - a * a;       for (int j = 0; j < b; ++j) {         for (int i = n + 1 - j * (a + 1) - a - 1; i < n + 1 - j * (a + 1); ++i) {           if (i > 0) cout << i <<    ;         }       }       if (b > 0) n = n - (b) * (a + 1);       for (int i = 0; i < a - b; ++i) {         for (int j = n + 1 - i * a - a; j < n + 1 - i * a; ++j) {           if (j > 0) cout << j <<    ;         }       }       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   int n;   char a[1000005], b[1000005], dic[130];   int main() {     dic[ W ] =  E ;     dic[ E ] =  W ;     dic[ N ] =  S ;     dic[ S ] =  N ;     scanf( %d%s%s , &n, a, b);     unsigned int ha = 0, hb = 0, rol = 1, mul = 233;     for (int i = n - 2; i >= 0; i--) {       if ((ha = a[i] * rol + ha) == (hb = dic[b[i]] + hb * mul)) {         puts( NO );         return 0;       }       rol *= mul;     }     puts( YES );   }


#include <bits/stdc++.h>   using namespace std;   using T = int;   struct Edge {     int from, to;     T cost, flow, cap;     Edge(int _from = 0, int _to = 0, T _cost = 0, T _cap = 0)         : from(_from), to(_to), cost(_cost), flow(0), cap(_cap) {}   };   const int N = 7010;   const int INF = 0x3f3f3f3f;   int n;   vector<Edge> edges;   vector<int> g[N];   int par[N];   int dist[N];   int pi[N];   void addEdge(int from, int to, T cost, T cap) {     g[from].push_back(edges.size());     edges.emplace_back(from, to, cost, cap);     g[to].push_back(edges.size());     edges.emplace_back(to, from, -cost, 0);   }   bool dijkstra(int s, int t) {     for (int i = 0; i <= t; ++i) {       dist[i] = INF;       par[i] = -1;     }     priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> pq;     dist[s] = 0;     pq.push({0, s});     while (!pq.empty()) {       auto on = pq.top();       pq.pop();       int chosen = on.second;       int d = on.first;       if (d > dist[chosen]) continue;       for (int edge_id : g[chosen]) {         int to = edges[edge_id].to;         int cost = edges[edge_id].cost;         T flowing = edges[edge_id].flow;         T cap = edges[edge_id].cap;         if (cap <= flowing) continue;         if (dist[to] > dist[chosen] + pi[chosen] - pi[to] + cost) {           dist[to] = dist[chosen] + pi[chosen] - pi[to] + cost;           par[to] = edge_id;           pq.push({dist[to], to});         }       }     }     return dist[t] != INF;   }   int mcmf(int s, int t) {     memset(pi, 0, sizeof pi);     int cost = 0, max_flow = 0;     while (dijkstra(s, t)) {       for (int i = 0; i <= t; ++i) {         pi[i] += dist[i];       }       int cur = t, flow = (1 << 30);       for (int cur = t; par[cur] != -1; cur = edges[par[cur]].from) {         flow = min(flow, edges[par[cur]].cap - edges[par[cur]].flow);       }       for (int cur = t; par[cur] != -1; cur = edges[par[cur]].from) {         cost += edges[par[cur]].cost * flow;         edges[par[cur]].flow += flow;         edges[par[cur] ^ 1].flow -= flow;       }       max_flow += flow;     }     return cost;   }   int main() {     scanf( %d , &n);     vector<int> a(n);     addEdge(0, 1, 0, 4);     for (int i = 0; i < n; ++i) {       scanf( %d , &a[i]);     }     for (int i = 0; i < n; ++i) {       addEdge(1, i + 2, 0, 1);       addEdge(i + 2, n + i + 2, -1, 1);       addEdge(n + i + 2, 2 * n + 2, 0, 1);       for (int j = i + 1; j < n; ++j) {         if (abs(a[i] - a[j]) == 1 or a[i] % 7 == a[j] % 7) {           addEdge(n + i + 2, j + 2, 0, 1);         }       }     }     printf( %d n , -1 * mcmf(0, 2 * n + 2));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAXN = 200100;   const long long INF = 2000000100;   const long long MOD = 998244353;   long long N, S[MAXN], P[MAXN];   long long B[MAXN];   void update(int ind, long long v) {     for (int i = ind; i <= N; i += (i & -i)) {       B[i] += v;     }   }   long long query(int ind) {     if (ind == 0) return 0;     long long s = 0;     for (int i = ind; i > 0; i -= (i & -i)) {       s += B[i];     }     return s;   }   int main() {     ios_base::sync_with_stdio(0);     cin >> N;     for (int i = 0; i < N; i++) cin >> S[i];     for (int i = 1; i <= N; i++) update(i, i);     for (int i = N - 1; i >= 0; i--) {       long long a = 1, b = N, c;       long long ans = -39;       while (a <= b) {         c = (a + b) / 2;         if (query(c - 1) > S[i]) {           b = c - 1;         } else {           ans = c;           a = c + 1;         }       }       P[i] = ans;       update(ans, -ans);     }     for (int i = 0; i < N; i++) cout << P[i] << (i == N - 1 ?   n  :    );   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long a, b;     cin >> b >> a;     a--;     b -= a;     if (b >= 0 && b % 2 == 0 && a > 0) {       cout <<  Yes  << endl;     } else if (b == 0 && a == 0)       cout <<  Yes  << endl;     else       cout <<  No  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double pi = 3.141592653589793238463;   int a[100009], p[200009];   int main() {     cin.tie(0);     cin.sync_with_stdio(0);     int h;     cin >> h;     for (int i = (0); i <= (h); i++) cin >> a[i];     bool perfect = true;     for (int i = 0; i < (h); i++)       if (a[i] > 1 && a[i + 1] > 1) perfect = false;     if (perfect) {       cout <<  perfect  <<   n ;       return 0;     }     cout <<  ambiguous  <<   n ;     int idx = 1;     p[0] = 0;     for (int i = (0); i <= (h); i++) {       p[i + 1] = idx;       for (int j = (0); j < (a[i]); j++) cout << p[i] <<    ;       idx += a[i];     }     cout <<   n ;     idx = 1;     p[0] = 0;     for (int i = (0); i <= (h); i++) {       p[i + 1] = idx;       if (i > 0 && a[i] > 1 && a[i - 1] > 1) {         cout << p[i] <<    ;         for (int j = (0); j < (a[i] - 1); j++) cout << p[i] + 1 <<    ;       } else         for (int j = (0); j < (a[i]); j++) cout << p[i] <<    ;       idx += a[i];     }     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 2;   const int M = 1e9 + 7;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     vector<int> v(n);     for (int i = 0; i < n; i++) cin >> v[i];     vector<int> pre(n, 1);     for (int i = 1; i < n; i++) {       if (v[i] <= v[i - 1] * 2) pre[i] = pre[i - 1] + 1;     }     cout << *max_element(pre.begin(), pre.end());     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     float a, b, c, d;     float mpts;     float vpts;     cin >> a >> b >> c >> d;     mpts = max((3 * a) / 10, a - ((a / 250) * c));     vpts = max((3 * b) / 10, b - ((b / 250) * d));     if (mpts == vpts) {       cout <<  Tie n ;       return 0;     }     mpts > vpts ? cout <<  Misha n  : cout <<  Vasya n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3e3 + 10;   const int oo = 1e9;   vector<int> g[N], gt[N];   vector<pair<int, int> > b[N], bt[N];   int cost[N][N];   void bfs(int u, int n, vector<int> adj[], vector<pair<int, int> > &b, bool q) {     queue<int> Q;     vector<int> d(n + 1, oo);     d[u] = 0;     Q.push(u);     while (!Q.empty()) {       int cur = Q.front();       Q.pop();       for (auto v : adj[cur]) {         int dist = d[cur] + 1;         if (dist >= d[v]) continue;         d[v] = dist;         Q.push(v);         if (q) cost[u][v] = dist;       }     }     for (int i = 1; i <= n; i++) b.push_back({d[i], i});     sort(b.begin(), b.end());     while (!b.empty() && b.back().first >= oo) b.pop_back();     reverse(b.begin(), b.end());   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n, m;     cin >> n >> m;     for (int i = 0; i <= n; i++) {       for (int j = 0; j <= n; j++) cost[i][j] = oo;       cost[i][i] = 0;     }     for (int i = 0; i < m; i++) {       int u, v;       cin >> u >> v;       g[u].push_back(v);       gt[v].push_back(u);     }     for (int i = 1; i <= n; i++) {       bfs(i, n, g, b[i], 1);       bfs(i, n, gt, bt[i], 0);     }     vector<int> ans;     int best = -1;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         if (i == j) continue;         for (int k = 0; k < min(3, (int)bt[i].size()); k++) {           for (int l = 0; l < min(3, (int)b[j].size()); l++) {             int u = bt[i][k].second, du = bt[i][k].first;             int v = b[j][l].second, dv = b[j][l].first;             int cur_ans = du + cost[i][j] + dv;             if (cur_ans >= oo || cur_ans <= best) continue;             set<int> S = {u, i, j, v};             if (S.size() < 4) continue;             best = cur_ans;             ans = {u, i, j, v};           }         }       }     }     for (auto x : ans) cout << x <<    ;     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool isprime(long long n) {     if (n < 2) return false;     if (n == 2) return true;     if (n % 2 == 0) return false;     for (int i = 3; i * i <= n; i = i + 2) {       if (n % i == 0) return false;     }     return true;   }   int main() {     int n;     cin >> n;     int a[n];     for (int i = 0; i < n; i++) cin >> a[i];     sort(a, a + n);     int temp = -1, cnt = 0;     for (int i = 0; i < n; i++) {       if (temp != a[i] && a[i] != 0) {         cnt++;         temp = a[i];       }     }     cout << cnt;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, c, d, e, f;     cin >> a >> b >> c >> d >> e >> f;     cout << (b * d * f > a * c * e || c == 0 && d > 0 || a == 0 && b > 0 && d > 0                  ?  Ron                   :  Hermione )          <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     int n;     cin >> n;     int a[n];     int ans = 0, sum = 0;     for (int i = 0; i < n; i++) {       cin >> a[i];       ans += a[i];     }     sum = ans;     sort(a, a + n);     for (int j = n - 1; j >= 1; j--) {       for (int x = 2; x < a[j]; x++) {         if (a[j] % x != 0) continue;         ans = min(ans, sum - a[j] - a[0] + a[j] / x + a[0] * x);       }     }     cout << ans << endl;     cerr <<  Time :   << (double)clock() / (double)CLOCKS_PER_SEC <<  s           <<   n ;     return 0;   }


#include <bits/stdc++.h>   int len;   int big;   int Dgt[30];   int N;   int ans;   void tochar(int);   int main() {     while (scanf( %d , &N) != EOF) {       memset(Dgt, 0, sizeof(Dgt));       ans = 0;       while (N) {         big = -1;         tochar(N);         for (int i = 0; i < len; i++) {           if (Dgt[i] > big) big = Dgt[i];         }         N -= big;         ans++;       }       printf( %d n , ans);     }   }   void tochar(int n) {     len = 0;     while (n) {       Dgt[len] = n % 10;       n /= 10;       len++;     }   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int n;     cin >> n;     long long a[3], b[3];     cin >> a[0] >> a[1] >> a[2];     cin >> b[0] >> b[1] >> b[2];     cout << max(0ll, a[0] - b[0] - b[2]) + max(0ll, a[1] - b[1] - b[0]) +                 max(0ll, a[2] - b[2] - b[1])          <<    ;     cout << min(a[0], b[1]) + min(a[1], b[2]) + min(a[2], b[0]) << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     int t;     t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool equal(string s) {     int n = 0;     for (int i = 0; i < s.length(); i++) {       if (s[i] ==  a ) n++;     }     if (n * 2 != s.length()) return false;     return true;   }   string s, s1, sex;   int n, d, k;   int main() {     cin >> k;     cin >> s;     n = s.length();     sex = s;     for (int i = 2; i <= n; i = i + 2) {       for (int j = 0; j <= n - i; j++) {         s1 = s.substr(j, i);         if (equal(s1)) {           while (s.find(s1) != -1) {             s.erase(s.find(s1), 1);             d++;           }         }       }     }     cout <<  25 ;   }


#include <bits/stdc++.h>   using namespace std;   long long SG[1110000] = {0, 1, 2, 1, 4, 3, 2, 1, 5,  6, 2, 1, 8,  7,  5,  9,                            8, 7, 3, 4, 7, 4, 2, 1, 10, 9, 3, 6, 11, 12, 14, 13};   long long n, Ans, vis[1110000];   set<long long> Hehe;   int main() {     scanf( %I64d , &n);     long long Orz = (long long)(floor)(sqrt(1.0 * n));     for (long long i = 2; i * i <= n; ++i) {       long long c = 0;       if (vis[i]) continue;       for (long long j = i; j <= n; j *= i, c++) {         if (j > Orz)           Hehe.insert(j);         else           vis[j] = 1;       }       Ans ^= SG[c];     }     long long tmp = n - Orz - Hehe.size();     if (!(tmp & 1)) Ans ^= 1;     if (Ans)       printf( Vasya );     else       printf( Petya );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, cnt;   bool vis[1000005];   vector<int> G[1000005];   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++) {       int u, v;       scanf( %d%d , &u, &v);       G[u].push_back(v);     }     for (int u = 1; u <= n; u++) {       if (vis[u]) continue;       for (int i = 0; i < G[u].size(); i++) {         int v = G[u][i];         if (v > u) vis[v] = true;       }     }     for (int u = n; u > 0; u--) {       if (vis[u]) continue;       for (int i = 0; i < G[u].size(); i++) vis[G[u][i]] = true;     }     for (int i = 1; i <= n; i++)       if (!vis[i]) cnt++;     printf( %d n , cnt);     for (int i = 1; i <= n; i++)       if (!vis[i]) printf( %d  , i);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2013;   char p[1 << 20], s[205];   int k, n, m;   int main() {     gets(p);     gets(s);     cin >> k;     n = strlen(p);     m = strlen(s);     string minLex;     for (int i = 1; i <= k + 1; ++i) minLex +=  1 ;     int rboud = min(m, k), cntIn = n / k, R = n % k;     for (int d = 1; d <= rboud && cntIn * d <= m; ++d) {       string cans;       for (int i = 1; i <= k; ++i) cans +=  0 ;       {         int r = d - 1, i = min(n - 1, k - 1);         for (; i >= 0 && r >= 0; --i) {           int l1 = m / d + (r < m % d);           int l2 = cntIn + (i < R);           if (l1 != l2) continue;           bool diff = false;           for (int e = 0; e < l1 && !diff; ++e)             diff = s[r + e * d] != p[i + e * k];           if (diff) continue;           cans[i] =  1 ;           --r;         }         if (r == -1) minLex = min(minLex, cans);       }     }     if (minLex.size() > k)       cout << 0;     else       cout << minLex;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int x, y, z;   } a[10];   int flag;   long long v[5];   long long dist(int i, int j) {     long long x = a[i].x - a[j].x;     long long y = a[i].y - a[j].y;     long long z = a[i].z - a[j].z;     return x * x + y * y + z * z;   }   int ok() {     int cnt = 0;     for (int i = 1; i < 8; i++)       for (int j = 0; j < i; j++) {         long long w = dist(i, j);         if (!cnt)           v[++cnt] = w;         else {           int k = 1;           while (k <= cnt && v[k] != w) k++;           if (k > cnt && cnt == 3) return 0;           if (k > cnt) v[++cnt] = w;         }       }     if (cnt == 3) return 1;     return 0;   }   void dfs(int p) {     if (flag) return;     if (p == 8) {       if (ok()) flag = 1;       return;     }     node tmp = a[p];     dfs(p + 1);     if (!flag) {       a[p].y = tmp.z;       a[p].z = tmp.y;       dfs(p + 1);     }     if (!flag) {       a[p].x = tmp.y;       a[p].y = tmp.x;       a[p].z = tmp.z;       dfs(p + 1);     }     if (!flag) {       a[p].x = tmp.y;       a[p].y = tmp.z;       a[p].z = tmp.x;       dfs(p + 1);     }     if (!flag) {       a[p].x = tmp.z;       a[p].y = tmp.x;       a[p].z = tmp.y;       dfs(p + 1);     }     if (!flag) {       a[p].x = tmp.z;       a[p].y = tmp.y;       a[p].z = tmp.x;       dfs(p + 1);     }   }   int main() {     for (int i = 0; i < 8; i++) cin >> a[i].x >> a[i].y >> a[i].z;     dfs(0);     if (!flag)       puts( NO );     else {       puts( YES );       for (int i = 0; i < 8; i++) printf( %d %d %d n , a[i].x, a[i].y, a[i].z);     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1024;   int n, m;   bool b[MAXN][MAXN];   int main() {     cin >> n >> m;     for (register int l = (1); l < (int)(m + 1); ++l) {       int x, y;       cin >> x >> y;       x--;       y--;       b[x][y] = true;       for (register int xx = (x - 2); xx < (int)(x + 1); ++xx)         for (register int yy = (y - 2); yy < (int)(y + 1); ++yy)           if (xx >= 0 && yy >= 0) {             bool ok = true;             for (register int i = (0); i < (int)(3); ++i)               for (register int j = (0); j < (int)(3); ++j)                 if (!b[i + xx][j + yy]) ok = false;             if (ok) {               cout << l << endl;               return 0;             }           }     }     cout << -1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 2147483647;   const long long LLINF = 9223372036854775807LL;   template <int MOD>   struct Num {     int x;     Num() : x(0) {}     explicit Num(int x, bool skip_check = false) {       if (skip_check)         this->x = x;       else {         if (x >= 0 && x < MOD)           this->x = x;         else {           this->x = x % MOD;           if (this->x < 0) this->x += MOD;         };       }     }     Num<MOD> operator+(const Num<MOD>& oth) const {       int rem = this->x + oth.x;       if (rem >= MOD) rem -= MOD;       return Num(rem, true);     }     Num<MOD> operator-(const Num<MOD>& oth) const {       int rem = this->x - oth.x;       if (rem < 0) rem += MOD;       return Num(rem, true);     }     Num<MOD> operator*(const Num<MOD>& oth) const {       int rem = (long long)this->x * oth.x % MOD;       return Num(rem, true);     }     Num<MOD> to_pow(int n) const {       Num<MOD> res(1);       Num<MOD> cur = *this;       while (n) {         if (n & 1) res = res * cur;         cur = cur * cur;         n >>= 1;       }       return res;     }     Num<MOD> inv_prime() const { return this->to_pow(MOD - 2); }     Num<MOD> operator/(const Num<MOD>& oth) const {       return *this * oth.inv_prime();     }     bool operator==(const Num<MOD>& oth) const { return x == oth.x; }     bool operator!=(const Num<MOD>& oth) const { return !(x == oth); }   };   int sgn(long long x) {     if (x > 0) return 1;     if (x < 0) return -1;     return 0;   }   struct Frac {     int numer, den;     explicit Frac(int numer, int den) : numer(numer), den(den) {}     int cmp(const Frac& oth) const {       long long res_numer =           (long long)numer * oth.den - (long long)oth.numer * den;       long long res_den = (long long)den * oth.den;       return sgn(res_numer) * sgn(res_den);     }   };   const int mod = 998244353;   struct Event {     int lind;     int ldir, rdir;     Frac t;     Event(int lind, int ldir, int rdir, Frac t)         : lind(lind), ldir(ldir), rdir(rdir), t(t) {}     bool operator<(const Event& oth) const { return this->t.cmp(oth.t) < 0; }   };   struct Pipe {     int x, v, p_right;     Pipe() {}     Pipe(int x, int v, int p_right) : x(x), v(v), p_right(p_right) {}   };   struct TreeNodeData {     Num<mod> p_ok[2][2];     Num<mod> p_all[2][2];     TreeNodeData() {}     TreeNodeData combine(const TreeNodeData& oth, const int forb[2][2]) const {       TreeNodeData res;       for (int d1 = 0; d1 < 2; ++d1)         for (int d2 = 0; d2 < 2; ++d2)           for (int d3 = 0; d3 < 2; ++d3)             for (int d4 = 0; d4 < 2; ++d4) {               res.p_all[d1][d4] =                   res.p_all[d1][d4] + p_all[d1][d2] * oth.p_all[d3][d4];               if (forb[d2][d3]) continue;               res.p_ok[d1][d4] =                   res.p_ok[d1][d4] + p_ok[d1][d2] * oth.p_ok[d3][d4];             }       return res;     }   };   const int maxtr = 270000;   TreeNodeData tree[maxtr];   const int maxn = 100010;   int forb[maxn][2][2];   vector<TreeNodeData> leaves;   void build_tree(int v, int L, int R) {     if (L == R) {       tree[v] = leaves[L];       return;     }     int m = (L + R) >> 1;     build_tree(v + v + 1, L, m);     build_tree(v + v + 2, m + 1, R);     tree[v] = tree[v + v + 1].combine(tree[v + v + 2], forb[m]);   }   void tree_update(int v, int L, int R, int pos) {     if (L == R) return;     int m = (L + R) >> 1;     if (pos <= m)       tree_update(v + v + 1, L, m, pos);     else       tree_update(v + v + 2, m + 1, R, pos);     tree[v] = tree[v + v + 1].combine(tree[v + v + 2], forb[m]);   }   int main() {     int n;     scanf( %d , &n);     vector<Pipe> pipes;     for (int i = 0; i < n; ++i) {       int cx, cv, cp;       scanf( %d%d%d , &cx, &cv, &cp);       pipes.push_back(Pipe(cx, cv, cp));     }     for (auto pipe : pipes) {       TreeNodeData curres;       curres.p_ok[1][1] = curres.p_all[1][1] =           Num<mod>(pipe.p_right) / Num<mod>(100);       curres.p_ok[0][0] = curres.p_all[0][0] =           Num<mod>(100 - pipe.p_right) / Num<mod>(100);       leaves.push_back(curres);     }     build_tree(0, 0, n - 1);     vector<Event> events;     for (int i = 0; i + 1 < n; ++i) {       auto p1 = pipes[i];       auto p2 = pipes[i + 1];       events.push_back(Event(i, 1, 0, Frac(p2.x - p1.x, p1.v + p2.v)));       if (p1.v > p2.v)         events.push_back(Event(i, 1, 1, Frac(p2.x - p1.x, p1.v - p2.v)));       else if (p2.v > p1.v)         events.push_back(Event(i, 0, 0, Frac(p2.x - p1.x, p2.v - p1.v)));     }     sort(events.begin(), events.end());     Num<mod> result(0);     Num<mod> pred_nok_prob(0);     for (int ev_ind = 0; ev_ind < int((events).size());) {       int fr_ev = ev_ind;       int to_ev = ev_ind;       while (to_ev < int((events).size()) &&              events[fr_ev].t.cmp(events[to_ev].t) == 0)         ++to_ev;       Num<mod> cur_ok_prob(tree[0].p_ok[0][0] + tree[0].p_ok[0][1] +                            tree[0].p_ok[1][0] + tree[0].p_ok[1][1]);       for (int i = fr_ev; i < to_ev; ++i) {         forb[events[i].lind][events[i].ldir][events[i].rdir] = 1;         tree_update(0, 0, n - 1, events[i].lind);       }       Num<mod> cur_nok_prob = ((tree[0].p_all[0][0] + tree[0].p_all[0][1] +                                 tree[0].p_all[1][0] + tree[0].p_all[1][1]) -                                (tree[0].p_ok[0][0] + tree[0].p_ok[0][1] +                                 tree[0].p_ok[1][0] + tree[0].p_ok[1][1]));       Num<mod> cur_delta = (cur_nok_prob - pred_nok_prob) *                            Num<mod>(events[fr_ev].t.numer) /                            Num<mod>(events[fr_ev].t.den);       result = result + cur_delta;       pred_nok_prob = cur_nok_prob;       ev_ind = to_ev;     }     cout << result.x << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int N = 1e6 + 66;   vector<int> g[N], f[N];   int used[N];   vector<int> topsort;   void dfs(int v) {     used[v] = 1;     for (int to : g[v])       if (used[to] != 1) dfs(to);     topsort.emplace_back(v);   }   int c[N];   void color(int v) {     if (false) cerr << v <<   color n ;     if (c[v] == 0) c[v] = v;     used[v] = 2;     for (int to : f[v])       if (used[to] != 2) c[to] = c[v], color(to);   }   int S;   vector<int> newg[N];   void fnd(int v) {     if (false) cerr << v <<   fnd n ;     used[v] = 3;     for (int to : newg[v])       if (used[to] != 3) fnd(to);     if (S == -1) S = v;   }   void clr(int v) {     used[v] = 4;     for (int to : g[v])       if (used[to] != 4) clr(to);   }   void solve() {     int n, m;     cin >> n >> m;     topsort.clear();     for (int i = 1; i <= n; ++i) {       used[i] = 0;       g[i].clear();       f[i].clear();       newg[i].clear();       c[i] = 0;     }     for (int i = 1; i <= m; ++i) {       int a, b;       cin >> a >> b;       g[a].emplace_back(b);       f[b].emplace_back(a);     }     for (int i = 1; i <= n; ++i) {       if (!used[i]) {         dfs(i);       }     }     reverse(topsort.begin(), topsort.end());     for (int i : topsort) {       if (used[i] != 2) {         color(i);       }     }     for (int i = 1; i <= n; ++i) {       if (false) cerr << c[i] <<    n [i == n];       for (int j : g[i]) {         newg[c[i]].emplace_back(c[j]);       }     }     S = -1, fnd(c[1]);     clr(S);     if (false) cerr << S <<   S n ;     vector<int> j, p;     for (int i = 1; i <= n; ++i) {       if (used[i] != 4)         p.emplace_back(i);       else         j.emplace_back(i);     }     assert(!j.empty());     if (p.empty()) {       cout <<  No n ;     } else {       cout <<  Yes n  << j.size() <<     << p.size() <<   n ;       for (int i : j) cout << i <<    ;       cout <<   n ;       for (int i : p) cout << i <<    ;       cout <<   n ;     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0), cout.tie(0);     int TT;     cin >> TT;     while (TT--) solve();   }


#include <bits/stdc++.h>   using namespace std;   void read_file(bool outToFile = true) {}   int n, K;   const int MAXN = 200000 + 99;   int A[MAXN];   set<int> X;   int vis[MAXN];   bool check() {     memset(vis, 0, sizeof vis);     stack<int> s;     int idx = 0;     int cur = 1;     while (true) {       while (s.empty() || s.top() != cur) {         s.push(A[idx]);         vis[A[idx]] = 1;         idx++;       }       s.pop();       cur++;       if (cur == n + 1) break;       if (vis[cur] && cur != s.top()) return false;     }     return true;   }   int main() {     read_file();     while (scanf( %d%d , &n, &K) != -1) {       X.clear();       memset(vis, 0, sizeof vis);       for (int i = 0; i < K; i++) {         scanf( %d , &A[i]);         if (A[i] != 1) X.insert(A[i] - 1);         vis[A[i]] = 1;       }       for (int i = 0; i < K; i++) {         if (X.count(A[i])) X.erase(A[i]);       }       int idx = K;       while (!X.empty()) {         int x = *X.begin();         X.erase(X.begin());         A[idx++] = x;         vis[x] = 1;         if (x == 1) continue;         if (!vis[x - 1]) X.insert(x - 1);       }       int mx = 0;       for (int i = 0; i < idx; i++) mx = max(mx, A[i]);       for (int i = n; i > mx; i--) A[idx + n - i] = i;       if (!check()) {         printf( -1 n );         continue;       }       for (int i = 0; i < n; i++) printf( %d%c , A[i],    n [i == n - 1]);     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long a, b, t, tt, i;     long long t1, t2, t3, t4;     while (~scanf( %lld %lld , &a, &b)) {       t = b - a;       t1 = a % 10;       t2 = b % 10;       t3 = (a / 10) % 10;       t4 = (b / 10) % 10;       if (t >= 10 || (t4 != t3)) {         printf( 0 n );       } else {         if (t1 <= 2 && t2 >= 5)           printf( 0 n );         else {           tt = 1;           for (i = t1 + 1; i <= t2; i++) {             tt *= i;           }           printf( %lld n , tt % 10);         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxN = 100050, mod = 1000000007;   ;   int n, ans, k;   int fa[maxN], c[maxN];   int getfa(int u) {     if (fa[u] == u) return u;     int t = fa[u];     fa[u] = getfa(fa[u]);     while (c[u] < 0) c[u] += mod;     while (c[t] < 0) c[t] += mod;     c[u] = (c[u] + c[t]) % mod;     return fa[u];   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       fa[i] = i;       c[i] = 0;     }     int v, x;     for (int i = 1; i <= n; i++) {       scanf( %d , &k);       for (int j = 1; j <= k; j++) {         scanf( %d%d , &v, &x);         int t = getfa(v);         fa[t] = i;         while (c[v] < 0) c[v] += mod;         while (x < 0) x += mod;         c[t] = (c[v] + x) % mod;         while (c[t] < 0) c[t] += mod;         while (ans < 0) ans += mod;         ans = (ans + c[t]) % mod;       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:5000000000 )   const long long mod = 1000000007;   long long Inf = (long long)2e9;   long long LINF = (long long)1e18 + 1e17;   using namespace std;   int main() {     ios::sync_with_stdio(false);     int n;     cin >> n;     vector<long long> a(n);     vector<bool> can(n, true);     map<long long, set<int>> pos;     for (int(i) = 0; (i) < n; (i)++) {       cin >> a[i];       pos[a[i]].insert(i);     }     int k = n;     while (pos.size()) {       if (pos.begin()->second.size() < 2) {         pos.erase(pos.begin());         continue;       }       int i1 = *pos.begin()->second.begin();       pos.begin()->second.erase(i1);       int i2 = *pos.begin()->second.begin();       pos.begin()->second.erase(i2);       long long x = pos.begin()->first;       k--;       can[i1] = false;       pos[2 * x].insert(i2);       a[i2] = 2 * x;     }     cout << k << endl;     for (int(i) = 0; (i) < n; (i)++)       if (can[i]) cout << a[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /stack:200000000 )   #pragma GCC optimize( Ofast )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   using namespace std;   long long int mod = 1e9 + 7;   long long int mod1 = 998244351;   long long int power(long long int a, long long int b) {     if (b == 0)       return 1;     else if (b % 2 == 0)       return power(a * a % mod, b / 2) % mod;     else       return (a % mod * power(a * a % mod, b / 2)) % mod;   }   long long int bit(long long int x) { return __builtin_popcountll(x); }   long long int temp1, temp2, temp3, temp4;   void fi(long long int x3, long long int y3, long long int x4,           long long int y4) {     long long int x5 = max(temp1, x3);     long long int y5 = max(temp2, y3);     long long int x6 = min(temp3, x4);     long long int y6 = min(temp4, y4);     if (x5 > x6 || y5 > y6) {       temp1 = -1000000009, temp2 = -1e9 - 1, temp3 = -1e9 - 1, temp4 = -1e9 - 1;       return;     }     temp1 = x5;     temp2 = y5;     temp3 = x6;     temp4 = y6;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int n;     cin >> n;     long long int a[n], b[n], c[n], d[n];     for (int i = 0; i < n; ++i) {       cin >> a[i] >> b[i] >> c[i] >> d[i];     }     temp1 = a[0], temp2 = b[0], temp3 = c[0], temp4 = d[0];     long long int count = 0;     vector<int> index;     for (int i = 1; i < n; ++i) {       long long int p = temp1, p1 = temp2, p3 = temp3, p4 = temp4;       fi(a[i], b[i], c[i], d[i]);       if (temp1 == -1000000009) {         temp1 = p;         temp2 = p1;         temp3 = p3;         temp4 = p4;         count++;       }     }     if (count <= 1) {       cout << temp1 <<     << temp2 <<   n ;       return 0;     }     temp1 = a[n - 1];     temp2 = b[n - 1];     temp3 = c[n - 1];     temp4 = d[n - 1];     count = 0;     for (int i = n - 1; i >= 0; --i) {       long long int p = temp1, p1 = temp2, p3 = temp3, p4 = temp4;       fi(a[i], b[i], c[i], d[i]);       if (temp1 == -1000000009) {         temp1 = p;         temp2 = p1;         temp3 = p3;         temp4 = p4;         count++;       }     }     if (count <= 1) {       cout << temp1 <<     << temp2 <<   n ;       return 0;     }     temp1 = a[1], temp2 = b[1], temp3 = c[1], temp4 = d[1], count = 0;     for (int i = 2; i < n; ++i) {       long long int p = temp1, p1 = temp2, p3 = temp3, p4 = temp4;       fi(a[i], b[i], c[i], d[i]);     }     cout << temp3 <<     << temp4 <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int n, numone, numtwo, sum, tmp;   char nlong;   string nameone, nametwo;   string read;   struct node {     string name;     int goal, moregoal, score;     int q;   } wxy[100];   bool check(int t) {     for (int i = 0; i < tmp; i++)       if (wxy[t].name[i] != nameone[i]) return false;     return true;   }   bool checktwo(int t) {     for (int i = tmp + 1; i < sum; i++)       if (wxy[t].name[i - tmp - 1] != nametwo[i - tmp - 1]) return false;     return true;   }   bool cmpfirst(node a, node b) {     if (a.score > b.score) return 1;     if (a.score < b.score) return 0;     if (a.moregoal > b.moregoal) return 1;     if (a.moregoal < b.moregoal) return 0;     if (a.goal > b.goal) return 1;     return 0;   }   bool cmplast(node a, node b) { return a.name < b.name; }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> wxy[i].name;       wxy[i].q = wxy[i].name.size();     }     for (int j = 1; j <= n * (n - 1) / 2; j++) {       sum = 0;       tmp = 0;       nameone =   , nametwo =   ;       numone = numtwo = 0;       while (1) {         scanf( %c , &nlong);         if (nlong ==  - )           break;         else if ((nlong >=  a  && nlong <=  z ) ||                  (nlong >=  A  && nlong <=  Z )) {           nameone += nlong;           sum++;         }       }       tmp = sum;       sum++;       while (1) {         scanf( %c , &nlong);         if (nlong ==    )           break;         else {           nametwo += nlong;           sum++;         }       }       while (1) {         scanf( %c , &nlong);         if (nlong ==  : )           break;         else if (nlong >=  0  && nlong <=  9 )           numone = numone * 10 + nlong -  0 ;       }       cin >> numtwo;       int one, two;       for (int i = 1; i <= n; i++)         if (wxy[i].q == tmp)           if (check(i)) {             one = i;             break;           }       for (int i = 1; i <= n; i++)         if (wxy[i].q + tmp + 1 == sum)           if (checktwo(i)) {             two = i;             break;           }       wxy[one].goal += numone;       wxy[two].goal += numtwo;       wxy[one].moregoal += numone - numtwo;       wxy[two].moregoal += numtwo - numone;       if (numone > numtwo)         wxy[one].score += 3;       else if (numtwo > numone)         wxy[two].score += 3;       else {         wxy[one].score++;         wxy[two].score++;       }     }     sort(wxy + 1, wxy + n + 1, cmpfirst);     sort(wxy + 1, wxy + (n) / 2 + 1, cmplast);     for (int i = 1; i <= n / 2; i++) cout << wxy[i].name << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int f[26];   char s[100005];   int dp[100005][26];   int main(void) {     int n;     scanf( %s , s + 1);     scanf( %d , &n);     for (int i = 0; i < 26; i++) f[i] = -1;     for (int i = 0; i < n; i++) {       char x[10];       scanf( %s , x);       f[x[0] -  a ] = x[1] -  a ;       f[x[1] -  a ] = x[0] -  a ;     }     for (int i = 1; s[i]; i++) {       int mins = 1 << 20;       for (int j = 0; j < 26; j++)         if (j != f[s[i] -  a ]) mins = min(dp[i - 1][j], mins);       for (int j = 0; j < 26; j++) {         if (j == s[i] -  a ) {           dp[i][j] = mins;         } else {           dp[i][j] = dp[i - 1][j] + 1;         }       }     }     int m = strlen(s + 1);     printf( %d n , *min_element(dp[m], dp[m] + 26));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int T;     cin >> T;     while (T--) {       int a, b;       cin >> a >> b;       int x = abs(a - b);       int ans = x / 5;       x %= 5;       ans += x / 2;       x %= 2;       ans += x;       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a, b, c, s;   vector<double> x, y11, z;   double rx, ry, rz, mn = -1000000000;   double mln(double x) { return log(x); }   void upd(double nx, double ny, double nz) {     if (nx + ny + nz > (s + 1e-7)) return;     double cur = 0;     if (a) cur += a * mln(nx);     if (b) cur += b * mln(ny);     if (c) cur += c * mln(nz);     if (cur > mn) {       mn = cur;       rx = nx;       ry = ny;       rz = nz;     }   }   int main() {     scanf( %d %d %d %d , &s, &a, &b, &c);     if (a == 0 && b == 0 && c == 0) {       puts( 0.0 0.0 0.0 );       return 0;     }     if (a != 0) {       double S = s;       double m = 1 + b * 1. / a + c * 1. / a;       double x0 = S / m;       x.push_back(x0);       y11.push_back(x0 * b * 1. / a);       z.push_back(x0 * c * 1. / a);     } else if (b != 0) {       double S = s;       double m = 1 + a * 1. / b + c * 1. / b;       double x0 = S / m;       x.push_back(x0 * a * 1. / b);       y11.push_back(x0);       z.push_back(x0 * c * 1. / b);     } else if (c != 0) {       double S = s;       double m = 1 + a * 1. / c + b * 1. / c;       double x0 = S / m;       x.push_back(x0 * a * 1. / c);       y11.push_back(x0 * b * 1. / c);       z.push_back(x0);     }     double S = s;     x.push_back(0);     y11.push_back(0);     z.push_back(s);     x.push_back(0);     y11.push_back(s);     z.push_back(0);     x.push_back(s);     y11.push_back(0);     z.push_back(0);     if (a + b) {       z.push_back(0);       z.push_back(0);       x.push_back(a * c / (b + a + 0.));       y11.push_back(S - a * c / (b + a + 0.));       x.push_back(b * c / (b + a + 0.));       y11.push_back(S - b * c / (b + a + 0.));     }     if (b + c) {       x.push_back(0);       x.push_back(0);       y11.push_back(b * a / (b + c + 0.));       z.push_back(S - b * a / (b + c + 0.));       y11.push_back(c * a / (b + c + 0.));       z.push_back(S - c * a / (b + c + 0.));     }     if (a + c) {       y11.push_back(0);       y11.push_back(0);       x.push_back(a * b / (c + a + 0.));       z.push_back(S - a * b / (c + a + 0.));       x.push_back(c * b / (c + a + 0.));       z.push_back(S - c * b / (c + a + 0.));     }     for (int i = 0; i < x.size(); i++) {       upd(x[i], y11[i], z[i]);     }     printf( %.11lf %.11lf %.11lf n , rx, ry, rz);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Pnt {     int x, y;     Pnt() {}     Pnt(int _x, int _y) {       x = _x;       y = _y;     }     bool operator<(const Pnt& o) const {       if (x != o.x) return x < o.x;       return y < o.y;     }     void show() { printf( Pnt %d %d n , x, y); }   };   const int MAXN = 1000005;   const int MAXS = 20;   const int dx[] = {1, 0, -1, 0};   const int dy[] = {0, 1, 0, -1};   int n, m;   char s[MAXN];   int f[MAXN][2];   void preGao() {}   void init() {     if (scanf( %s , s) == EOF) exit(0);     n = strlen(s);   }   void work() {     f[n][0] = 0;     f[n][1] = 2;     for (int i = n - 1; i >= 0; i--) {       f[i][0] = f[i + 1][0] + (s[i] ==  1 );       f[i][0] = min(f[i][0], f[i + 1][1]);       f[i][1] = f[i + 1][0] + 2 + (s[i] ==  0 );       f[i][1] = min(f[i][1], f[i + 1][1] + (s[i] ==  0 ));     }     printf( %d n , min(f[0][0], f[0][1]));   }   int main() {     preGao();     while (true) {       init();       work();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-9;   const double pi = acos(-1.0);   const int INF = 1000000000;   const int dx[4] = {0, 0, 1, -1};   const int dy[4] = {1, -1, 0, 0};   const int N = 100001;   int n, a[N];   int ans;   int main() {     ios_base::sync_with_stdio(0);     cin >> n;     for (int i = 0; (i) < (n); ++i) cin >> a[i];     sort(a, a + n);     int c = 1;     for (int i = 0; i < n; i++) {       if (a[i] >= c) {         c++;       }     }     cout << c << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 5;   const int inf = 1e9;   char aa[maxn];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       int n, k;       cin >> n >> k;       for (int i = 1; i <= n; i++) {         cin >> aa[i];       }       sort(aa + 1, aa + 1 + n);       if (aa[1] != aa[k])         cout << aa[k] << endl;       else {         int mark = 1;         if (aa[n] == aa[k + 1]) mark = 0;         if (mark == 0) {           for (int i = 1; i <= n; i += k) cout << aa[i];           cout << endl;         } else {           cout << aa[1];           for (int i = k + 1; i <= n; i++) cout << aa[i];           cout << endl;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> G[14];   int dp[1 << 14], dp2[14][14][1 << 14];   vector<int> re[1 << 14];   pair<int, int> last_pair[1 << 14];   int pre[1 << 14];   int last[14][14][1 << 14];   bool v[1 << 14];   int main() {     int n, m, x, y;     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++) {       scanf( %d%d , &x, &y);       G[x - 1].push_back(y - 1);       G[y - 1].push_back(x - 1);     }     for (int i = 0; i < n; i++) v[1 << i] = 1;     memset(dp, 0x3f, sizeof(dp));     dp[1] = 0;     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++)         for (int k = 0; k < (1 << n); k++) {           dp2[i][j][k] = 0x3f3f3f3f;         }     for (int i = 0; i < n; i++)       for (auto x : G[i]) {         dp2[i][x][0] = 1;         last[i][x][0] = i;       }     for (int i = 0; i < (1 << n); i++) {       for (int j = 0; j < n; j++) {         if ((i >> j) & 1) continue;         for (int k = 0; k < n; k++) {           if ((i >> k) & 1) continue;           if (j == k || dp2[j][k][i] == 0x3f3f3f3f) continue;           for (auto z : G[k]) {             if ((i >> z) & 1) continue;             if (z == last[j][k][i]) continue;             int tmp = i ^ (1 << k);             if (dp2[j][z][tmp] == 0x3f3f3f3f) {               dp2[j][z][tmp] = 1;               last[j][z][tmp] = k;             }           }         }       }     }     for (int i = 0; i < n; i++) dp[1 << i] = 0;     for (int i = 0; i < (1 << n); i++)       for (int j = 0; j < n; j++) {         if ((i >> j) & 1) re[i].push_back(j);       }     for (int i = 0; i < (1 << n); i++) {       for (int j = i; j; j = (j - 1) & i) {         int tmp = i ^ j;         int cnt = __builtin_popcount(j) + 1;         if (dp[i] < dp[tmp] + cnt) continue;         for (auto x : re[tmp])           for (auto y : re[tmp]) {             if (dp2[x][y][j] == 1) {               dp[i] = min(dp[i], dp[tmp] + cnt);               last_pair[i] = make_pair(x, y);               pre[i] = tmp;             }           }       }     }     if (dp[(1 << n) - 1] == 0x3f3f3f3f) {       printf( -1 n );     } else {       printf( %d n , dp[(1 << n) - 1]);       int now = (1 << n) - 1;       while (!v[now]) {         int x = last_pair[now].first, y = last_pair[now].second;         int tmp = now ^ pre[now];         while (tmp) {           int tmp1 = last[x][y][tmp];           printf( %d %d n , y + 1, tmp1 + 1);           y = tmp1;           tmp ^= (1 << tmp1);         }         printf( %d %d n , x + 1, y + 1);         now = pre[now];       }     }   }


#include <bits/stdc++.h>   using namespace std;   void err(istream_iterator<string> it) {}   template <typename T, typename... Args>   void err(istream_iterator<string> it, T a, Args... args) {     cerr << *it <<   =   << a << endl;     err(++it, args...);   }   void faster() {     ios::sync_with_stdio(false);     cin.tie(nullptr);   }   long long n, m;   vector<long long> vst;   vector<long long> dis;   long long bfs(long long u, vector<set<long long> > &adj) {     queue<pair<long long, long long> > q;     q.push(make_pair(u, 0));     while (!q.empty()) {       long long v, d;       tie(v, d) = q.front();       q.pop();       vst[v] = 1;       dis[v - 1] = d;       for (auto e : adj[v]) {         if (!vst[e]) {           q.push(make_pair(e, d + 1));           vst[e] = 1;         }       }     }     return dis[n - 1];   }   void solve() {     cin >> n >> m;     vector<set<long long> > adj(n + 1);     vst.resize(n + 1);     dis.resize(n);     fill((dis).begin(), (dis).end(), 1000);     bool flag = true;     for (__typeof(m) i = (0) - ((0) > (m)); i != (m) - ((0) > (m));          i += 1 - 2 * ((0) > (m))) {       long long x, y;       cin >> x >> y;       if (min(x, y) == 1 && max(x, y) == n) flag = false;       adj[x].insert(y);       adj[y].insert(x);     }     if (flag) {       long long dist = bfs(1, adj);       if (dist == 1000) dist = -1;       cout << dist << endl;     } else {       vector<set<long long> > adjp(n + 1);       for (long long i = 1; i < n + 1; i++) {         for (long long j = 1; j < n + 1; j++) {           if (j != i && !adj[i].count(j)) adjp[i].insert(j);         }       }       long long dist = bfs(1, adjp);       if (dist == 1000) dist = -1;       cout << dist << endl;     }   }   signed main() {     faster();     long long t = 1;     while (t--) solve();   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 5;   int n, k;   vector<pair<int, int> > g[maxn];   int d[maxn], par[maxn];   void dfs1(int u, int fa) {     par[u] = fa;     for (auto p : g[u]) {       int v = p.first;       if (v == fa) continue;       d[v] = d[u] + p.second;       dfs1(v, u);     }   }   int a[maxn];   bool f[maxn];   int c[maxn];   void dfs(int u, int fa) {     d[u] = 0;     par[u] = fa;     for (auto p : g[u]) {       int v = p.first;       if (v == fa) continue;       dfs(v, u);       c[v] = p.second;       if (!f[v]) d[u] = max(d[u], d[v] + p.second);     }   }   int l[maxn], r[maxn], t[maxn], tail = 1;   int main() {     scanf( %d%d , &n, &k);     int u, v, w;     for (int i = 1; i <= n - 1; i++)       scanf( %d%d%d , &u, &v, &w), g[u].push_back(pair<int, int>(v, w)),           g[v].push_back(pair<int, int>(u, w));     dfs1(1, -1);     d[1] = 0;     int rt = 1;     for (int i = 1; i <= n; i++)       if (d[i] > d[rt]) rt = i;     d[rt] = 0;     dfs1(rt, -1);     int y = rt;     for (int i = 1; i <= n; i++)       if (d[i] > d[y]) y = i;     int p = y;     int m = 0;     while (p > 0) a[++m] = p, p = par[p];     dfs(rt, -1);     for (int i = 1; i <= n; i++) l[i] = d[i];     dfs(y, -1);     for (int i = 1; i <= n; i++) r[i] = d[i];     for (int i = 1; i <= m; i++) f[a[i]] = 1;     dfs(rt, -1);     tail = 1;     int head = 1;     int ans = 1e9;     for (int i = 1; i <= m; i++) {       while (tail > head && d[a[t[tail - 1]]] <= d[a[i]]) tail--;       t[tail++] = i;       int q = d[a[t[head]]];       q = max(q, (i - k + 1 > 0 ? l[a[i - k + 1]] : 0));       q = max(q, r[a[i]]);       ans = min(ans, q);       if (t[head] == i - k) head++;     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   int main() {     static char cc[100000 + 1];     int n, k, a, i;     scanf( %d%d , &n, &k);     a = (n - k) / 2;     for (i = 0; i < n; i++) cc[i] =  0  + (i % (a + 1) == a);     printf( %s n , cc);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, l, r, ar[110], ubah;   int main() {     cin >> n >> m;     for (int i = 1; i <= m; i++) {       cin >> l >> r;       ar[l]++;       ar[r + 1]--;     }     for (int i = 1; i <= n; i++) {       ubah += ar[i];       if (ubah != 1) {         cout << i <<     << ubah <<   n ;         return 0;       }     }     cout <<  OK n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   unsigned long long lvl_len(int lvl) {     static long long len[64] = {0};     if (len[lvl])       return len[lvl];     else {       len[1] = 1;       for (int i = 2; i < 64; ++i) len[i] = len[i - 1] * 2 + 1;     }     return len[lvl];   }   void rec(int lvl, unsigned long long pos) {     if (pos < lvl_len(lvl - 1))       rec(lvl - 1, pos);     else if ((pos -= lvl_len(lvl - 1)) < 1)       cout << lvl;     else       rec(lvl - 1, pos - 1);   };   int main(int, char **) {     ios::sync_with_stdio(false);     unsigned long long n, p;     cin >> n >> p;     rec(n, p - 1);     return 0;   }


#include<bits/stdc++.h>   using namespace std ;      #define ll              long long    #define pb              push_back   #define all(v)          v.begin(),v.end()   #define sz(a)           (ll)a.size()   #define F               first   #define S               second   #define INF             2000000000000000000   #define popcount(x)     __builtin_popcountll(x)   #define pll             pair<ll,ll>    #define pii             pair<int,int>   #define ld              long double      const int M = 1000000007;   const int MM = 998244353;   const long double PI = acos(-1);      template<typename T, typename U> static inline void amin(T &x, U y){ if(y<x) x=y; }   template<typename T, typename U> static inline void amax(T &x, U y){ if(x<y) x=y; }   template<typename T, typename U> ostream& operator<<(ostream &os, const pair<T, U> &p)   {        return os<< ( <<p.F<<  , <<p.S<< ) ;    }            template <typename A, typename B>   string to_string(pair<A, B> p);   string to_string(const string& s) {       return     + s +    ;   }   string to_string(const char* s) {       return to_string((string) s);   }   string to_string(bool b) {       return (b ?  true  :  false );   }   string to_string(char c) {       string t =     + string(1,c) +    ;       return t;   }   string to_string(vector<bool> v) {       bool first = true;       string res =  { ;       for (int i = 0; i < static_cast<int>(v.size()); i++) {           if (!first) {               res +=  ,  ;           }           first = false;           res += to_string(v[i]);       }       res +=  } ;       return res;   }   template <size_t N>   string to_string(bitset<N> v) {       string res =   ;       for(size_t i = 0; i < N; i++) {           res += static_cast<char>( 0  + v[i]);       }       return res;   }   template <typename A>   string to_string(A v) {       bool first = true;       string res =  { ;       for(const auto &x : v) {           if (!first) {               res +=  ,  ;           }           first = false;           res += to_string(x);       }       res +=  } ;       return res;   }   template <typename A, typename B>   string to_string(pair<A, B> p) {       return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ;   }         template<typename T>   void debug_out(const char* name, T&& x) {        cerr <<  [  << name <<  ] :   << to_string(x) << endl;    }   template <typename H, typename... T>   void debug_out(const char* names, H&& arg1, T&&... args){       const char* comma = strchr(names + 1,  , );       cerr <<  [ ;       cerr.write(names, comma - names) <<  ] :   << to_string(arg1) <<  |  ;       debug_out(comma+1, args...);   }   #ifdef LOCAL   #define debug(...) debug_out(#__VA_ARGS__, __VA_ARGS__)   #else   #define debug(...) 2351   #endif      const int N = 300005;      ll ans = 0;   int in[N],out[N],cur = 1;   vector<int> v[N],g[N];      set<pii> s;      void dfs2(int s,int p)   {       in[s] = cur++;       for(auto j:g[s])           dfs2(j,s);       out[s] = cur-1;   }      void dfs(int u,int p)   {       bool added = false;       pii rm = {-1,-1};       // cout<<u<<   <<in[u]<<   <<out[u]<<  n ;       auto it = s.lower_bound({in[u],-1});       if(it != s.end())       {           if(it->F <= out[u])           {               amax(ans,sz(s));               goto yo;           }       }       if(it != s.begin())       {           --it;           if(it->S >= in[u])           {               assert(it->F <= in[u]);               added = true;               rm = {it->F,it->S};               s.erase(rm);               s.insert({in[u],out[u]});               amax(ans,sz(s));               goto yo;           }       }       s.insert({in[u],out[u]});       amax(ans,sz(s));       yo:;       // debug(s);       for(auto j:v[u])           dfs(j,u);       if(s.count({in[u],out[u]}))           s.erase({in[u],out[u]});       if(added)       {           s.insert(rm);       }   }      int _runtimeTerror_()   {       ans = 0;       int n;       cin>>n;       cur = 1;       s.clear();       for(int i=1;i<=n;++i)       {           in[i] = out[i] = 0;           v[i].clear(),g[i].clear();       }       for(int i=2;i<=n;++i)       {           int x;           cin>>x;           v[x].push_back(i);       }       for(int i=2;i<=n;++i)       {           int x;           cin>>x;           g[x].push_back(i);       }       dfs2(1,0);       dfs(1,0);       cout<<ans<<  n ;       return 0;   }      int main()   {       ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);       #ifdef runSieve           sieve();       #endif       #ifdef NCR           initialize();       #endif       int TESTS=1;       cin>>TESTS;       while(TESTS--)           _runtimeTerror_();       return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   #define ll long long int   #define ff first   #define ss second   #define pb push_back   #define mp make_pair   #define deb(x) cout<< #x <<     << x <<   n ;   #define MAX 9223372036854775807   #define MIN -9223372036854775807   #define setbits(n) __builtin_popcountll(n)   #define mkunique(a) sort(a.begin(),a.end()); a.resize(unique(a.begin(),a.end())-a.begin());   #define print(s) for(auto u: s) cout<<u<<   ; cout<<  n ;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   const ll mod=998244353;           const int N=501;   ll dp[N][N][2], n;   vector<ll> a;   ll inc, id;       ll go(ll pos, ll diff, ll f){       if(pos==n){           if(f) return 1;           return 0;       }       if(dp[pos][diff][f]!=-1)           return dp[pos][diff][f];       ll ans = go(pos+1,diff,f);       if(a[pos]>0){           ll val = a[pos];           if(pos == id)               ans = (ans + go(pos+1,diff+1,1))%mod;           else{               if(val<inc || (val==inc && pos<id))                   ans = (ans + go(pos+1,diff+1,f))%mod;               else                   ans = (ans + go(pos+1,diff,f))%mod;           }       }       else{           if(diff-1 < 1)               ans = (ans + go(pos+1,0,0))%mod;           else               ans = (ans + go(pos+1,diff-1,f))%mod;       }       return dp[pos][diff][f] = ans;   }       int main(){           ios_base::sync_with_stdio(false);       cin.tie(NULL);       cout.tie(NULL);       ll TT=clock();           cin>>n;       for(ll i=0;i<n;i++){           char c;           cin>>c;           if(c== + ){               ll num;               cin>>num;               a.pb(num);           }           else{               a.pb(-1);           }       }       ll ans=0;       for(ll i=0;i<n;i++){           if(a[i]>0){               inc = a[i]; id = i;               memset(dp,-1,sizeof(dp));               ll mul = go(0,0,0);               ans = (ans + inc*mul%mod)%mod;           }       }       cout<<ans;               cerr<<  n nTIME:  <<(long double)(clock()-TT)/CLOCKS_PER_SEC<<  sec n ;       TT = clock();       return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     map<string, string> mp;     set<string> s;     for (int i = 0; i < n; i++) {       string old, edg;       cin >> old >> edg;       mp[old] = edg;       s.insert(edg);     }     map<string, string>::iterator p;     p = mp.begin();     vector<pair<string, string>> a;     for (p; p != mp.end(); p++) {       if (s.count(p->first)) continue;       string cur;       cur = p->first;       while (mp.count(cur)) {         cur = mp[cur];       }       a.push_back(make_pair(p->first, cur));     }     cout << a.size() <<   n ;     for (int i = 0; i < a.size(); i++)       cout << a[i].first <<     << a[i].second <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   long long add(long long x, long long y) {     x += y;     while (x >= 998244353) x -= 998244353;     while (x < 0) x += 998244353;     return x;   }   long long mul(long long x, long long y) { return (x * 1ll * y) % 998244353; }   long long binpow(long long x, long long y) {     long long z = 1;     while (y > 0) {       if (y % 2 == 1) z = mul(z, x);       x = mul(x, x);       y /= 2;     }     return z;   }   long long inv(long long x) { return binpow(x, 998244353 - 2); }   long long divide(long long x, long long y) { return mul(x, inv(y)); }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long n, k;     cin >> n >> k;     vector<long long> a(k);     for (long long i = 0; i < (long long)k; ++i) cin >> a[i];     vector<long long> last(n + 1, -1), fst(n + 1, k);     for (long long i = 1; i <= k; i++) {       last[a[i - 1]] = i;     }     for (long long i = k; i >= 1; i--) {       fst[a[i - 1]] = i;     }     long long ans = 0;     for (long long i = 1; i <= n; i++) {       for (long long j = max(1ll, i - 1); j <= min(i + 1, n); j++) {         if (j == i) {           if (last[i] == -1) ans += 1;         } else           ans += (last[i] <= fst[j]);       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 5;   long long T[N];   long long sum[5];   multiset<long long, greater<long long>> ms[5];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n, k, b, c;     cin >> n >> k >> b >> c;     for (int i = 1; i <= n; i++) {       cin >> T[i];     }     sort(T + 1, T + n + 1);     if (T[1] < 0) {       long long val = -T[1];       for (int i = 1; i <= n; i++) {         T[i] += val;       }     }     if (5 * c <= b) {       long long ans = 9e18;       long long sum = 0;       for (int i = 1; i <= n; i++) {         sum += T[i];         if (i >= k) {           ans = min(ans, T[i] * k - sum);           sum -= T[i - k + 1];         }       }       cout << ans <<   n ;       exit(0);     }     long long ans = 9e18;     for (int i = 1; i <= n; i++) {       for (int j = 0; j < 5; j++) {         long long cnt = -((T[i] + j) / 5) * b + j * c;         int mm = (T[i] + j) % 5;         sum[mm] += cnt;         ms[mm].insert(cnt);       }       if (i > k) {         for (int j = 0; j < 5; j++) {           sum[j] -= *ms[j].begin();           ms[j].erase(ms[j].begin());         }       }       if (i >= k) {         for (int j = 0; j < 5; j++) {           int mm = (T[i] + j) % 5;           ans = min(ans, ((T[i] + j) / 5) * b * k + sum[mm]);         }       }     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   long long int mod = 1000000007;   using namespace std;   void solve() {     long long int n, k;     cin >> n >> k;     char ans[4][n];     int nval = n - 2;     for (int i = 0; i < 4; i++) {       for (int j = 0; j < n; j++) {         ans[i][j] =  . ;       }     }     int mid = nval / 2;     mid++;     if (k % 2 == 1) {       for (int i = 1; i < 3; i++) {         if (k <= 0) break;         for (int j = mid; j >= 1; j--) {           if (i > 1 && k % 2 == 0 && k < nval) {             ans[i][1] =  # ;             ans[i][n - 2] =  # ;             k--;             k--;           }           if (k > 0) {             ans[i][j] =  # ;             k--;             if (k <= 0) break;             ans[i][mid + (mid - j)] =  # ;             if (j != mid) k--;           } else             break;         }       }     } else {       for (int i = 1; i < n - 1; i++) {         for (int j = 1; j < 3; j++) {           if (k) {             ans[j][i] =  # ;             k--;           }         }       }     }     cout <<  YES  << endl;     for (int i = 0; i < 4; i++) {       for (int j = 0; j < n; j++) {         cout << ans[i][j];       }       cout << endl;     }   }   int main() {     solve();     return 0;   }