#include <bits/stdc++.h>   using namespace std;   int p[200005], q[200005];   int fa[200005], top[200005], sz[200005], dep[200005];   vector<int> E[200005];   void dfs1(int root);   void dfs2(int root, int toproot);   int lca(int node1, int node2);   int dis(int node1, int node2);   pair<int, int> merge(int node1, int node2, int node3);   pair<int, int> merge(pair<int, int> path1, pair<int, int> path2);   void modify(int index, int lboundary, int rboundary, int position,               pair<int, int> path);   int query(int index, int lboundary, int rboundary, pair<int, int> path);   pair<int, int> tr[4 * 200005];   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , p + i), q[p[i]] = i;     for (int i = 2; i <= n; i++) scanf( %d , fa + i);     for (int i = 2; i <= n; i++) {       E[i].push_back(fa[i]);       E[fa[i]].push_back(i);     }     dfs1(1);     dfs2(1, 1);     int k;     scanf( %d , &k);     for (int i = 1; i <= n; i++) {       modify(1, 0, n - 1, p[i], make_pair(q[p[i]], q[p[i]]));     }     while (k--) {       int t, u, v;       scanf( %d , &t);       if (t == 1) {         scanf( %d %d , &u, &v);         swap(p[u], p[v]);         swap(q[p[u]], q[p[v]]);         modify(1, 0, n - 1, p[u], make_pair(q[p[u]], q[p[u]]));         modify(1, 0, n - 1, p[v], make_pair(q[p[v]], q[p[v]]));       } else if (t == 2) {         if (tr[1].first)           cout << n << endl;         else           cout << query(1, 0, n - 1, make_pair(q[0], q[0])) << endl;       }     }   }   void dfs1(int r) {     sz[r] = 1;     for (int u : E[r])       if (u != fa[r]) {         dfs1(u);         sz[r] += sz[u];       }   }   void dfs2(int r, int t) {     top[r] = t;     dep[r] = dep[fa[r]] + 1;     int tid = -1, msz = 0;     for (int u : E[r])       if (u != fa[r] && sz[u] > msz) {         msz = sz[u];         tid = u;       }     if (tid == -1) return;     dfs2(tid, t);     for (int u : E[r])       if (u != fa[r] && u != tid) dfs2(u, u);   }   int lca(int a, int b) {     while (top[a] != top[b]) {       if (dep[top[a]] < dep[top[b]]) swap(a, b);       a = fa[top[a]];     }     return dep[a] < dep[b] ? a : b;   }   int dis(int a, int b) { return dep[a] + dep[b] - 2 * dep[lca(a, b)]; }   pair<int, int> merge(int a, int b, int c) {     int ab = dis(a, b), ac = dis(a, c), bc = dis(b, c);     if (ab == bc + ac)       return make_pair(a, b);     else if (ac == ab + bc)       return make_pair(a, c);     else if (bc == ab + ac)       return make_pair(b, c);     else       return make_pair(0, 0);   }   pair<int, int> merge(pair<int, int> a, pair<int, int> b) {     if (!a.first || !b.first) return make_pair(0, 0);     pair<int, int> tmp = merge(a.first, a.second, b.first);     if (tmp.first) tmp = merge(tmp.first, tmp.second, b.second);     return tmp;   }   void modify(int o, int l, int r, int p, pair<int, int> v) {     if (l == r) {       tr[o] = v;       return;     }     int mid = (l + r) >> 1;     if (p <= mid)       modify((o << 1), l, mid, p, v);     else       modify((o << 1 | 1), mid + 1, r, p, v);     tr[o] = merge(tr[(o << 1)], tr[(o << 1 | 1)]);   }   int query(int o, int l, int r, pair<int, int> p) {     if (l == r) return l;     int mid = (l + r) >> 1;     pair<int, int> q = merge(p, tr[(o << 1)]);     if (!q.first)       return query((o << 1), l, mid, p);     else       return query((o << 1 | 1), mid + 1, r, q);   }


#include <bits/stdc++.h>   using namespace std;   string s;   bool atted, dotted, goned;   int cntu, cnth = -1, cntr = -1, cntd = -1;   int main() {     cin >> s;     for (int i = 0; i < ((int)(s).size()); i++) {       if (!atted && s[i] ==  @ )         atted = 1;       else if (atted && !goned && s[i] ==  .  && s[i - 1] !=  .  &&                s[i - 1] !=  @  && cntd <= 16)         dotted = 1, cntd = -1;       else if (atted && !goned && s[i] ==  / )         goned = 1;       else if ((!atted || goned) && s[i] ==  . )         return puts( NO ), 0;       else if (!isalnum(s[i]) && s[i] !=  _ )         return puts( NO ), 0;       if (!atted)         cntu++;       else if (!goned)         cnth++, cntd++;       else         cntr++;     }     if (cntd == 0) return puts( NO ), 0;     if (cntu < 1 || cntu > 16 || cnth < 1 || cnth > 32 || goned && cntr < 1 ||         cntr > 32)       return puts( NO ), 0;     return puts( YES ), 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   long long dd[200000], h[200000];   int a, b;   int aaa[100000], bbb[100000];   long long d[200000];   long long mmin[200000][18], mmax[200000][18];   long long xxx(int x) { return h[x] - d[x]; }   long long yyy(int y) { return h[y] + d[y]; }   long long xval(int aa, int bb) {     int llog = 0;     while ((bb + 1 - aa) >= (1 << llog)) {       llog++;     }     llog--;     if (xxx(mmax[aa][llog]) >= xxx(mmax[bb + 1 - (1 << llog)][llog])) {       return mmax[aa][llog];     } else {       return mmax[bb + 1 - (1 << llog)][llog];     }   }   long long yval(int aa, int bb) {     int llog = 0;     while ((bb + 1 - aa) >= (1 << llog)) {       llog++;     }     llog--;     if (yyy(mmin[aa][llog]) >= yyy(mmin[bb + 1 - (1 << llog)][llog])) {       return mmin[aa][llog];     } else {       return mmin[bb + 1 - (1 << llog)][llog];     }   }   int main() {     ios::sync_with_stdio(0);     cin >> n >> m;     for (int i = 0; i < n; i++) {       cin >> dd[i];     }     for (int i = 0; i < n; i++) {       cin >> h[i];       h[i] *= 2;     }     d[0] = dd[n - 1];     for (int i = 0; i < n - 1; i++) {       d[i + 1] = d[i] + dd[i];     }     for (int i = 0; i < n; i++) {       d[i + n] = d[i] + d[n - 1];       h[i + n] = h[i];     }     n *= 2;     for (int i = 0; i < n; i++) {       mmax[i][0] = i;       mmin[i][0] = i;     }     for (int j = 1; (1 << j) <= n; j++) {       for (int i = 0; i < n; i++) {         mmin[i][j] = mmin[i][j - 1];         if ((i + (1 << (j - 1)) < n) &&             (yyy(mmin[i + (1 << (j - 1))][j - 1]) > yyy(mmin[i][j - 1])))           mmin[i][j] = mmin[i + (1 << (j - 1))][j - 1];         mmax[i][j] = mmax[i][j - 1];         if ((i + (1 << (j - 1)) < n) &&             (xxx(mmax[i + (1 << (j - 1))][j - 1]) > xxx(mmax[i][j - 1])))           mmax[i][j] = mmax[i + (1 << (j - 1))][j - 1];       }     }     for (int i = 0; i < m; i++) {       cin >> aaa[i] >> bbb[i];     }     for (int i = 0; i < m; i++) {       b = aaa[i];       a = bbb[i];       a--;       b--;       a = (a + 1) % (n / 2);       b = (b + n - 1) % (n / 2);       if (a > b) {         b += n / 2;       }       long long r1 = -(1ll << 60);       int xx = xval(a, b);       int yy;       if (xx < b) {         yy = yval(xx + 1, b);         r1 = yyy(yy) + xxx(xx);       }       yy = yval(a, b);       long long r2 = -(1ll << 60);       if (yy > a) {         xx = xval(a, yy - 1);         r2 = yyy(yy) + xxx(xx);       }       cout << max(r1, r2) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100005;   int main() {     int n, k, m, t, i, c[N];     scanf( %d%d , &n, &k);     int ans = 0;     while (k--) {       scanf( %d , &m);       for (i = 0; i < m; i++) {         scanf( %d , &c[i]);       }       if (c[0] == 1) {         for (i = 0; i < m; i++) {           if (c[i] != i + 1) break;         }         ans += m - i;         t = i;       } else if (c[0] != 1)         ans += m - 1;     }     ans += n - t;     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long res, n, k, d = 0, f, s, moy, mx;   long long t[300003];   bool b = true;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n >> k;     for (long long i = 0; i < n; i++) cin >> t[i];     reverse(t, t + n);     f = k - 1;     while (d <= f) {       b = true;       moy = (d + f) / 2;       mx = k - 1;       for (long long i = 0; i < n; i++) {         if (t[i] <= mx)           mx = min(mx, t[i] + moy);         else if ((t[i] + moy) >= k)           mx = min(mx, t[i] + moy - k);         else {           b = false;           break;         }       }       if (b) {         res = moy;         f = moy - 1;       } else         d = moy + 1;     }     cout << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   const int maxn = 200000 + 10;   int n, m, k, nv;   vector<pair<int, pair<int, int> > > edge;   vector<pair<int, int> > e[maxn];   int f[maxn];   bool vis[maxn];   long long dis[maxn];   vector<long long> q;   void print(int a[], int n, int st = 0) {     for (int i = 0; i < n; i++) {       if (i != 0) printf(   );       printf( %d , a[i + st]);     }     printf(  n );   }   void init() {     scanf( %d%d%d , &n, &m, &k);     for (int i = 0; i < m; i++) {       int u, v, w;       scanf( %d%d%d , &u, &v, &w);       edge.push_back(make_pair(w, make_pair(min(u, v), max(u, v))));     }   }   void doit() {     sort(edge.begin(), edge.end());     memset(f, -1, sizeof(f));     nv = 0;     for (int i = 0; i < min(k, m); i++) {       int u = edge[i].second.first, v = edge[i].second.second, w = edge[i].first;       if (f[u] == -1) f[u] = nv++;       u = f[u];       if (f[v] == -1) f[v] = nv++;       v = f[v];       e[u].push_back(make_pair(v, w));       e[v].push_back(make_pair(u, w));     }     for (int st = 0; st < nv; st++) {       for (int i = 0; i < nv; i++) vis[i] = false;       vis[st] = true;       for (int i = 0; i < nv; i++) dis[i] = -1;       dis[st] = 0;       int cur = st;       while (1) {         for (int i = 0; i < e[cur].size(); i++) {           int v = e[cur][i].first, w = e[cur][i].second;           if (!vis[v] && (dis[v] == -1 || dis[v] > dis[cur] + w)) {             dis[v] = dis[cur] + w;           }         }         long long mn = -1;         for (int i = 0; i < nv; i++) {           if (!vis[i] && (dis[i] != -1 && (mn == -1 || dis[i] < mn))) {             mn = dis[i];             cur = i;           }         }         if (vis[cur]) break;         vis[cur] = true;       }       for (int i = st + 1; i < nv; i++) {         if (vis[i]) q.push_back(dis[i]);       }     }     sort(q.begin(), q.end());     printf( %lld n , q[k - 1]);   }   int main() {     init();     doit();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline T sqr(const T& x) {     return x * x;   }   template <class T>   inline void updMin(T& a, const T& b) {     if (b < a) a = b;   }   template <class T>   inline void updMax(T& a, const T& b) {     if (b > a) a = b;   }   vector<string> a;   int cross(int i, int j) {     int neq = 0;     for (int t = 0; t < 6; t++) {       if (a[i][t] != a[j][t]) neq++;     }     return (neq + 1) / 2;   }   int main() {     int n;     cin >> n;     a.resize(n);     for (int i = 0; i < n; i++) {       cin >> a[i];     }     if (n == 1) {       cout << 6 <<   n ;     } else {       int ans = 6;       for (int i = 0; i < n; i++) {         for (int j = i + 1; j < n; j++) {           ans = min(ans, cross(i, j) - 1);         }       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t, n, c0, c1, h, cc0 = 0, cc1 = 0;     cin >> t;     cout << endl;     for (int i = 0; i < t; i++) {       cin >> n >> c0 >> c1 >> h;       cout << endl;       string ali;       cin >> ali;       bitset<1001> ss(ali);       cc1 = ss.count();       cc0 = n - cc1;       int min_d = (cc1 * c1) + (cc0 * c0);       if (c0 < c1) {         int maliyet = cc1 * c0 + cc0 * c0 + cc1 * h;         min_d = min(min_d, maliyet);       } else {         int maliyet = cc1 * c1 + cc0 * c1 + cc0 * h;         min_d = min(min_d, maliyet);       }       cout << endl << min_d << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;       typedef long double ld;   const long long inf = 1100000000000000000;   #define maxn 200050   #define mod 1000000007 //998244353    #define int long long    #define PB push_back   #define F first   #define S second   #define vi vector<int>   #define rep(i,a,b)  for( int i = a; i < b; i++ )   #define rep1(i,a,b) for( int i = a; i <= b; i++)   #define repd(i,a,b) for( int i = a; i >= b; --i)   #define sz(x) (int)((x).size())     #define all(c) (c).begin(),(c).end()   #define allr(v) v.rbegin(),v.rend()   #define mem(a,x) memset(a,x,sizeof(a))   #define dbg(a) cout << #a <<   is   << (a); cout <<   n ;   template <class T> void PR(T a[], int n, int x = 0) { for ( int i = x; i < n; i++ ) cout << a[i] <<    ; cout<<   n  ; }   template <class T> void PR(vector <T> v) { for ( T i : v ) cout << i <<     ; cout <<   n ; }   template <class T> void PR(set <T> s) { for ( T i : s ) cout << i <<     ; cout <<   n ; }   template <class T> void PR(multiset <T> s) { for ( T i : s ) cout << i <<     ; cout <<   n  ; }   const long double PI=3.141592653589793238462643383279502884197169399375105820974944;   //...   void solve(){    int n, k; cin>>n>>k;    string s =   ;    rep(i, 0, k){     rep(j, i, k){      s += (j+ a );      if(j != k-1)       s += (i+ a );     }    }    int m = sz(s);    rep(i, 0, n)     cout<<s[i%m];   }      signed main(){       ios_base::sync_with_stdio(false), cin.tie(nullptr);    int T=1;// cin>>T;        while(T--){           solve();cout<<  n ;       }       return 0;   }


#include <bits/stdc++.h>   struct V {     int a, b;     int wybor;   };   std::vector<int> v[507];   V prev[507][507][2];   int odl[507][507][2];   bool odw[507][507][2];   std::queue<V> Q;   int wynik[1000007][2];   int main() {     int N, M;     scanf( %d%d , &N, &M);     int a, b;     for (int i = 0; i < M; i++) {       scanf( %d%d , &a, &b);       v[a].push_back(b);       v[b].push_back(a);     }     Q.push({1, N, 0});     odw[1][N][0] = true;     while (!Q.empty()) {       V ter = Q.front();       Q.pop();       if (ter.a == N && ter.b == 1 && ter.wybor == 0) break;       if (ter.a == ter.b && ter.wybor == 0) continue;       a = ter.a, b = ter.b;       if (ter.wybor == 0) {         for (auto it : v[a]) {           if (!odw[it][b][1]) {             odw[it][b][1] = true;             odl[it][b][1] = odl[ter.a][ter.b][ter.wybor] + 1;             prev[it][b][1] = ter;             Q.push({it, b, 1});           }         }       } else {         for (auto it : v[b]) {           if (!odw[a][it][0]) {             odw[a][it][0] = true;             odl[a][it][0] = odl[ter.a][ter.b][ter.wybor] + 1;             prev[a][it][0] = ter;             Q.push({a, it, 0});           }         }       }     }     if (odl[N][1][0] == 0) {       printf( -1 );       return 0;     }     int wyn = odl[N][1][0] / 2;     auto ter = V{N, 1, 0};     wynik[wyn][1] = 1;     while (ter.a != 0) {       wynik[odl[ter.a][ter.b][ter.wybor] / 2][ter.wybor] =           ter.wybor ? ter.b : ter.a;       ter = prev[ter.a][ter.b][ter.wybor];     }     printf( %d n , wyn);     for (int i = 0; i <= wyn; i++) printf( %d  , wynik[i][0]);     printf(  n );     for (int i = 0; i <= wyn; i++) printf( %d  , wynik[i][1]);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   #pragma GCC target( avx2 )   #pragma GCC optimization( O3 )   #pragma GCC optimization( unroll-loops )   const long long INFll = 1e18;   const int INFint = 1e9;   const long long MOD = 1e9 + 7;   void needForSpeed() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   bool isPrime(long long a) {     if (a < 2) return 0;     for (long long i = 2; i <= sqrt(a); i++)       if (a % i == 0) return 0;     return 1;   }   long long binpow(long long base, long long pow, long long mod = INFll) {     if (pow == 0)       return 1;     else if (pow % 2)       return base * binpow(base, pow - 1, mod) % mod;     else {       long long p = binpow(base, pow / 2LL, mod);       return p * p % mod;     }   }   template <class T>   void PR_VEC(const vector<T> &vec) {     cout <<  size(  << vec.size() <<  ):[   ;     if (vec.size() == 0) {       cout <<  !EMPTY!  ] n ;       return;     }     cout << vec[0] <<    ;     for (int i = 1; i < vec.size(); ++i) cout <<   ,    << vec[i] <<    ;     cout <<   ] n ;   }   int solve() {     long long n, m, ans;     cin >> n >> m;     string s;     cin >> s;     ans = n * (m - 1LL);     for (int i = 1; i < n; ++i) {       if (s[i] != s[i - 1]) ans += n * (m - 1LL);     }     int len = 1;     for (int i = 1; i < n; ++i) {       if (len == 1) {         if (s[i] != s[i - 1]) {           len = 2;         } else {           continue;         }       } else {         if (s[i] != s[i - 2]) {           ans -= (len * (len - 1LL)) / 2LL;           if (s[i] == s[i - 1])             len = 1;           else             len = 2;         } else {           ++len;         }       }     }     ans -= (len * (len - 1LL)) / 2LL;     cout << ans;     return 0;   }   int main() {     needForSpeed();     int tests = 1;     while (tests--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, ans;     scanf( %d %d , &a, &b);     for (ans = 0; a > 0 && b > 0; ans++) {       if (a > b) swap(a, b);       if (b == 1) break;       a++, b -= 2;     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   int add(int a, int b) {     long long x = a + b;     if (x >= 998244353) x -= 998244353;     if (x < 0) x += 998244353;     return x;   }   long long mul(long long a, long long b) { return (a * b) % 998244353; }   long long pw(long long a, long long b) {     long long ans = 1;     while (b) {       if (b & 1) ans = (ans * a) % 998244353;       a = (a * a) % 998244353;       b >>= 1;     }     return ans;   }   int n;   long long dp[2][1000002];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n;     dp[1][0] = 1;     dp[0][0] = 1;     for (int i = 1; i <= n; ++i) {       int x = (i - 1);       if (x % 2 == 0) {         int par = (1 + x / 2) % 2;         dp[par][i] = mul(dp[!par][x / 2], dp[0][x / 2]);       } else {         for (int j = x / 2; j <= 1 + x / 2; j++) {           int par = (1 + j) % 2;           dp[par][i] = mul(dp[!par][j], dp[0][i - j - 1]);         }       }     }     cout << (dp[1][n] + dp[0][n]) % 998244353;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s;   int main() {     long long n, x, y, br = 0, k = 0;     cin >> n >> x >> y;     cin >> s;     for (long i = 0; i < n; i++) {       if (s[i] ==  0 ) {         if (i == 0) {           br++;         } else if (s[i - 1] !=  0 ) {           br++;         }       }     }     long long sol = min(br * y, (br - 1) * x + y);     if (sol < 0) {       sol = br * y;     }     cout << sol << endl;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, a, b, v[2100][2100];   int k, d[2100][2100], x, y;   bool solve(int x, int y, int k) {     int l = max(1, x - k), r = min(x + k, n), t;     for (int i = l; i <= r; i++) {       t = k - abs(i - x);       if (y - t > 0 && !v[i][y - t]) {         a = i;         b = y - t;         return 1;       }       if (y + t <= m && !v[i][y + t]) {         a = i;         b = y + t;         return 1;       }     }     return 0;   }   int main() {     cin >> n >> m >> k;     for (int i = 0; i < k; i++) {       cin >> x >> y;       for (int j = -2; j <= 2; j++)         for (int k = -2; k <= 2; k++) {           if (x + j < 1 || x + j > n || y + k < 1 || y + k > m) continue;           d[x][y] = max(d[x][y], d[x + j][y + k] - abs(j) - abs(k));         }       while (!solve(x, y, d[x][y])) d[x][y]++;       v[a][b] = 1;       cout << a <<     << b << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0, f = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) f = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       x = (x << 1) + (x << 3) + c -  0 ;       c = getchar();     }     return x * f;   }   const int maxn = 305;   int n, a[maxn][maxn];   int dp[maxn << 1][maxn][maxn];   int main() {     n = read();     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) a[i][j] = read();     memset(dp, 0x8f, sizeof(dp));     dp[0][1][1] = a[1][1];     for (int i = 1; i <= 2 * n - 2; i++)       for (int j = max(1, i + 2 - n); j <= min(i + 1, n); j++)         for (int k = max(1, i + 2 - n); k <= min(i + 1, n); k++) {           dp[i][j][k] = max(max(dp[i - 1][j - 1][k - 1], dp[i - 1][j - 1][k]),                             max(dp[i - 1][j][k - 1], dp[i - 1][j][k]));           dp[i][j][k] += a[j][i - j + 2];           if (j != k) dp[i][j][k] += a[k][i - k + 2];         }     printf( %d n , dp[2 * n - 2][n][n]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     long long int num, ans = INT_MAX, temp, multi1 = INT_MAX, multi2 = INT_MAX;     cin >> num;     for (long long int i = 1; i <= sqrt(num); i++) {       if (num % i == 0) {         temp = num / i;         if (abs(i - temp) < ans) ans = abs(i - temp), multi1 = i, multi2 = temp;       }     }     cout << multi1 <<     << multi2;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   constexpr auto INF = 9223372036854775807;   using namespace std;   struct custom_hash {     static uint64_t splitmix64(uint64_t x) {       x += 0x9e3779b97f4a7c15;       x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;       x = (x ^ (x >> 27)) * 0x94d049bb133111eb;       return x ^ (x >> 31);     }     size_t operator()(uint64_t x) const {       static const uint64_t FIXED_RANDOM =           chrono::steady_clock::now().time_since_epoch().count();       return splitmix64(x + FIXED_RANDOM);     }   };   inline long long int gcd(long long int a, long long int b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long int mod_power(long long int x, long long int y) {     long long int res = 1;     x = x % 1000000007;     if (x == 0) return 0;     while (y > 0) {       if (y & 1) res = (res * x) % 1000000007;       y = y >> 1;       x = (x * x) % 1000000007;     }     return res;   }   long long modInverse(long long n) { return mod_power(n, 1000000007 - 2); }   long long nCrModPFermat(long long n, long long int r) {     if (r == 0) return 1;     vector<long long int> fac(n + 1, 0);     fac[0] = 1;     for (int i = 1; i <= n; i++) fac[i] = (fac[i - 1] * i) % 1000000007;     return (fac[n] * modInverse(fac[r]) % 1000000007 * modInverse(fac[n - r]) %             1000000007) %            1000000007;   }   bool isprime(long long int n) {     if (n == 2) return true;     if (n % 2 == 0 || n == 1) {       return false;     }     for (long long int j = 3; j <= sqrt(n); j += 2) {       if (n % j == 0) {         return false;       }     }     return true;   }   vector<vector<long long int>> arr;   vector<bool> visited;   vector<long long int> out_time;   vector<long long int> pre_time_vertex;   vector<long long int> vertex_pre_time;   vector<long long int> in_time;   vector<long long int> depth;   long long int cnt = 1, d = 0, cnt2 = 1;   void dfs(long long int u) {     pre_time_vertex[cnt] = u;     vertex_pre_time[u] = cnt;     in_time[u] = cnt2;     cnt2++;     cnt++;     visited[u] = true;     for (long long int i : arr[u]) {       if (!visited[i]) {         depth[i] = depth[u] + 1;         dfs(i);       }     }     out_time[u] = cnt2;     cnt2++;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     long long int n, q;     cin >> n >> q;     vector<long long int> parent(n + 1, -1);     for (long long int i = (long long int)2; i < (long long int)n + 1; i += 1) {       cin >> parent[i];     }     arr.resize(n + 1, vector<long long int>());     visited.resize(n + 1, false);     pre_time_vertex.resize(n + 1, 0);     vertex_pre_time.resize(n + 1, 0);     out_time.resize(n + 1, 0);     in_time.resize(n + 1, 0);     depth.resize(n + 1, 0);     for (long long int i = (long long int)2; i < (long long int)n + 1; i += 1) {       arr[parent[i]].push_back(i);       arr[i].push_back(parent[i]);     }     for (long long int i = (long long int)1; i < (long long int)n + 1; i += 1) {       sort(arr[i].begin(), arr[i].end());     }     dfs(1);     for (long long int i = (long long int)0; i < (long long int)q; i += 1) {       long long int u, k;       cin >> u >> k;       long long int req_pre = vertex_pre_time[u] + k - 1;       if (k == 1) {         cout << u <<   n ;       } else if (req_pre <= n && depth[pre_time_vertex[req_pre]] > depth[u]) {         long long int vertex = pre_time_vertex[req_pre];         if (out_time[u] > out_time[vertex])           cout << pre_time_vertex[req_pre] <<   n ;         else           cout << -1 <<   n ;       } else {         cout << -1 <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[n];     for (int i = 0; i < n; i++) cin >> a[i];     int d[n];     d[0] = a[0];     map<int, int> even, odd;     even[d[0]]++;     odd[0]++;     long long int c = 0;     for (int i = 1; i < n; i++) {       d[i] = a[i] ^ d[i - 1];       if (i % 2 == 0) {         even[d[i]]++;         if (even.find(d[i]) != even.end()) c += even[d[i]] - 1;       } else {         odd[d[i]]++;         if (odd.find(d[i]) != odd.end()) c += odd[d[i]] - 1;       }     }     cout << c;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     double n;     cin >> n;     double sum = 0, y;     for (int i = 0; i < n; i++) {       cin >> y >> y;       sum += y;     }     sum = sum / n;     cout << sum + 5 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int i, t, m, n;     cin >> t;     int arr[t];     for (i = 0; i < t; i++) {       int sum = 0, k = 0;       cin >> n >> m;       int arr1[n];       for (int j = 0; j < n; j++) {         cin >> arr1[j];         if (j != 0) sum += arr1[j];       }       k = arr1[0];       while (k < m) {         if (sum != 0) {           k++;           sum--;         } else           break;       }       arr[i] = k;     }     for (i = 0; i < t; i++) cout << arr[i] <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long int> small;   vector<long long int> large;   long long int largest(long long int digits, long long int sum) {     long long int i;     if (digits == 0) return sum;     for (i = 9; i >= 0; i--) {       if (sum >= i) {         large.push_back(i);         sum = sum - i;         break;       }     }     return largest(digits - 1, sum);   }   long long int smallest(long long int digits, long long int sum) {     long long int i;     if (digits == 0) return sum;     for (i = 0; i <= 9; i++) {       if (sum >= i && ((sum - i) <= 9 * (digits - 1))) {         small.push_back(i);         sum = sum - i;         break;       }     }     return smallest(digits - 1, sum);   }   int main() {     long long int i, j, t, n, m, temp, sum1, sum;     cin >> n >> sum;     sum1 = sum;     for (i = 1; i <= 9; i++) {       if (sum >= i && ((sum - i) <= 9 * (n - 1))) {         small.push_back(i);         sum = sum - i;         break;       }     }     temp = smallest(n - 1, sum);     if (temp > 0 || sum1 == 0) {       if (sum1 == 0 && n == 1)         cout << 0 <<    ;       else         cout << -1 <<    ;     } else {       for (i = 0; i < small.size(); i++) cout << small[i];       cout <<    ;     }     temp = largest(n, sum1);     if (temp > 0 || sum1 == 0) {       if (sum1 == 0 && n == 1)         cout << 0 << endl;       else         cout << -1 << endl;     } else {       for (i = 0; i < large.size(); i++) cout << large[i];       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   long long n, m, k;   long long mpow(long long b, long long e) {     if (e == 0) return 1;     if (e == 1) return b % MOD;     long long tmp = mpow(b, e / 2) % MOD;     tmp = tmp * tmp % MOD;     if (e % 2) tmp = tmp * (b % MOD) % MOD;     return tmp;   }   int main() {     cin >> n >> m >> k;     if (n % 2 != m % 2 && k == -1)       cout << 0 << endl;     else {       long long r = mpow(2, n - 1) % MOD;       r = mpow(r, m - 1) % MOD;       cout << r << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int segtree[4 * ((24 * 3600) + 100)], lazy[4 * ((24 * 3600) + 100)];   void propagate(int root, int l, int r) {     segtree[root] += lazy[root];     if (l != r) {       lazy[2 * root + 1] += lazy[root];       lazy[2 * root + 2] += lazy[root];     }     lazy[root] = 0;   }   int query(int root, int l, int r, int a, int b) {     if (b < l || r < a) return 0;     propagate(root, l, r);     if (a <= l && r <= b) return segtree[root];     int mid = (l + r) / 2;     return max(query(2 * root + 1, l, mid, a, b),                query(2 * root + 2, mid + 1, r, a, b));   }   void update(int root, int l, int r, int a, int b, int val) {     if (b < l || r < a) return;     if (a <= l && r <= b) {       lazy[root] += val;       return;     }     int mid = (l + r) / 2;     update(2 * root + 1, l, mid, a, b, val);     update(2 * root + 2, mid + 1, r, a, b, val);     propagate(2 * root + 1, l, mid);     propagate(2 * root + 2, mid + 1, r);     segtree[root] = max(segtree[2 * root + 1], segtree[2 * root + 2]);   }   int n, M, T, h, m, s;   int main() {     scanf( %d %d %d , &n, &M, &T);     vector<int> resp;     int user = 0;     int ult = -1;     for (int i = 0; i < n; i++) {       scanf( %d:%d:%d , &h, &m, &s);       int ini = h * 3600 + m * 60 + s;       int fim = ini + T - 1;       int val = query(0, 0, ((24 * 3600) + 100), ini, fim);       if (val < M) {         user++;         resp.push_back(user);         update(0, 0, ((24 * 3600) + 100), ini, fim, 1);       } else {         update(0, 0, ((24 * 3600) + 100), max(ult + 1, ini), fim, 1);         resp.push_back(user);       }       ult = fim;     }     if (query(0, 0, ((24 * 3600) + 100), 0, ((24 * 3600) + 100)) != M)       printf( No solution n );     else {       printf( %d n , user);       for (int i = 0; i < resp.size(); i++) {         printf( %d n , resp[i]);       }     }   }


#include <bits/stdc++.h>   using namespace std;   bool isp[300005];   void sv() {     long long i, j;     memset(isp, true, sizeof(isp));     isp[1] = false;     for (i = 2; i * i < 300005; i++) {       if (isp[i]) {         for (j = i * i; j < 300005; j += i) isp[j] = false;       }     }   }   long long powr(long long a, long long b) {     long long res = 1;     while (b) {       if (b & 1) res = (res * a) % 1000000007;       a = (a * a) % 1000000007;       b >>= 1;     }     return res;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long n, i, j, a[300005], x, y;     map<long long, vector<long long> > mp;     cin >> n;     for (i = 0; i < n; i++) {       cin >> a[i];       mp[a[i]].push_back(i);     }     long long ans = 0;     auto it = mp.begin();     while (it != mp.end()) {       (it->second).push_back(n);       it++;     }     auto itr = mp.begin();     while (itr != mp.end()) {       long long l = (itr->second).size();       for (i = l - 1; i > 0; i--) {         if ((itr->second)[i] != (itr->second)[i - 1] + 1) {           long long tmp = (itr->second)[i] - (itr->second)[0] - 1;           if (tmp > ans) {             ans = tmp;           } else             break;         }       }       itr++;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   const int N = 1e6 + 5;   const int K = (1 << 17) + 10;   const long long int INF = 2e18 + 500;   const int inf = 0x7fffffff;   const int SQRT = 500;   const int lgN = 20;   int main(int argc, char const *argv[]) {     cin.sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     stack<int> S;     int ans = 0;     int req = 0;     for (int i = 0; i < 2 * n; i++) {       string s;       cin >> s;       if (s[0] ==  a ) {         int x;         cin >> x;         S.push(x);       } else {         req += 1;         if (S.size() == 0) {           continue;         } else {           int x = S.top();           if (x == req) {             S.pop();           } else {             ans += 1;             S = stack<int>();           }         }       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[31];   int main() {     long long t, n;     long long q = 0;     cin >> t;     for (int i = 0; i <= 29; i++) {       a[i] = pow(2, i);     }     for (int i = 1; i <= t; i++) {       cin >> n;       if (n % 2 == 0) {         q = (n + 1) * (n / 2);       } else {         q = ((n / 2) + 1) * n;       }       for (int i = 0; i <= 29; i++) {         if (a[i] <= n) {           q = q - (a[i] * 2);         } else {           break;         }       }       cout << q << endl;       q = 0;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int mpow(int base, int exp);   void solve() {     long long int n;     cin >> n;     vector<long long int> v(n);     vector<long long int> cnt(2 * n + 1, 0);     for (long long int i = 0; i < (n); i++) {       cin >> v[i];       cnt[v[i]]++;     }     vector<long long int> out;     for (long long int i = 0; i < (n); i++) {       out.push_back(v[i]);       bool flag = true;       for (long long int j = v[i] + 1; j <= 2 * n; j++) {         if (cnt[j] == 0) {           out.push_back(j);           cnt[j]++;           flag = false;           break;         }       }       if (flag) {         cout << -1 <<   n ;         return;       }     }     for (auto i : out) cout << i <<    ;     cout <<   n ;     ;   }   int main() {     int t;     cin >> t;     while (t--) solve();     return 0;   }   int mpow(int base, int exp) {     base %= 1000000007;     int result = 1;     while (exp > 0) {       if (exp & 1) result = ((long long int)result * base) % 1000000007;       base = ((long long int)base * base) % 1000000007;       exp >>= 1;     }     return result;   }


#include <bits/stdc++.h>   const int N = 1 << 20;   int n, m, lim;   char s[22][100005];   int64_t a[N], b[N];   void fwt(int64_t* a, int f) {     for (int i = 1; i < lim; i <<= 1)       for (int j = 0; j < lim; j += i << 1)         for (int k = 0; k < i; ++k) {           int64_t x = a[j + k], y = a[j + k + i];           a[j + k] = x + y >> f;           a[j + k + i] = x - y >> f;         }   }   int main() {     scanf( %d%d , &n, &m);     lim = 1 << n;     for (int i = 0; i < lim; ++i)       b[i] = std::min(__builtin_popcount(i), n - __builtin_popcount(i));     for (int i = 0; i < n; ++i) {       scanf( %s , s[i]);       for (int j = 0; j < m; ++j) s[i][j] ^=  0 ;     }     for (int i = 0; i < m; ++i) {       int p = 0;       for (int j = 0; j < n; ++j) p |= s[j][i] << j;       ++a[p];     }     fwt(a, 0), fwt(b, 0);     for (int i = 0; i < lim; ++i) a[i] *= b[i];     fwt(a, 1);     std::cout << *std::min_element(a, a + lim) << std::endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 105;   int n;   int a[maxn];   int p;   double dp[maxn][maxn];   double release(int x) {     double res = 1;     for (register double i = x + 1; i <= n; i++) {       res *= (i - x);       res /= i;     }     return res;   }   signed main() {     scanf( %d , &n);     for (register int i = 1; i <= n; i++) {       scanf( %d , &a[i]);     }     scanf( %d , &p);     dp[0][0] = 1;     for (register int i = 1; i <= n; i++) {       for (register int j = n - 1; j >= 0; j--) {         for (register int k = p - a[i]; k >= 0; k--) {           dp[j + 1][k + a[i]] += dp[j][k];         }       }     }     double ans = 0;     for (register int i = 1; i <= n; i++) {       for (register int j = 0; j <= p; j++) {         if (dp[i][j]) {           ans += dp[i][j] * release(i);         }       }     }     printf( %.10lf n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int l, r, id;     bool operator<(const node& nd) const {       return l == nd.l ? r > nd.r : l < nd.l;     }   } a[300010];   int main() {     int n;     scanf( %d , &n);     for (int i = (0); i < (n); ++i) {       scanf( %d%d , &a[i].l, &a[i].r);       a[i].id = i;     }     sort(a, a + n);     int idx = 0;     for (int i = (1); i < (n); ++i) {       if (a[i].r <= a[idx].r) {         printf( %d %d n , a[i].id + 1, a[idx].id + 1);         return 0;       } else {         idx = i;       }     }     puts( -1 -1 );     return 0;   }


#include <bits/stdc++.h>   int perm[100010 + 1];   using namespace std;   int main() {     int n;     cin >> n;     int s = 1;     int e = n;     while (e - s > 0) {       perm[s] = s + 1;       perm[s + 1] = e;       perm[e] = e - 1;       perm[e - 1] = s;       s += 2;       e -= 2;     }     if (s == e) perm[s] = s;     if ((n % 4) > 1)       cout << -1;     else       for (int i = 1; i <= n; i++) cout << perm[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   int n, m, cnt, head[N], vis[N], in[N], u, v, ans, op[N];   int idx, dfn[N], g[N], f[N];   struct edge {     int v, nxt;   } e[N];   void add(int u, int v) {     e[++cnt] = (edge){v, head[u]};     head[u] = cnt;   }   int dfs(int u) {     vis[u] = in[u] = 1;     for (int i = head[u], v; v = e[i].v, i; i = e[i].nxt)       if (in[v] || (!vis[v] && !dfs(v))) return 0;     return in[dfn[++idx] = u] = 0, 1;   }   int main() {     cin >> n >> m;     for (int i = (1); i <= (m); i++) scanf( %d%d , &u, &v), add(u, v);     for (int i = (1); i <= (n); i++)       if (!vis[i] && !dfs(i)) return puts( -1 ), 0;     memset(g, 0x3f, sizeof g), memset(f, 0x3f, sizeof f);     for (int j = (1); j <= (n); j++)       for (int i = head[dfn[j]], v; v = e[i].v, i; i = e[i].nxt)         g[dfn[j]] = min(g[dfn[j]], min(v, g[v]));     for (int j = (n); j >= (1); j--)       for (int i = head[dfn[j]], v; v = e[i].v, i; i = e[i].nxt)         f[v] = min(f[v], min(dfn[j], f[dfn[j]]));     for (int i = (1); i <= (n); i++)       if (min(g[i], f[i]) > i) ans++, op[i] = 1;     cout << ans << endl;     for (int i = (1); i <= (n); i++) putchar(op[i] ?  A  :  E );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 122;   string s[N];   bool eq[N];   vector<int> g[30];   int used[N];   string ans;   void dfs(int x) {     used[x] = 1;     for (int to : g[x]) {       if (!used[to]) {         dfs(to);       } else if (used[to] == 1) {         puts( Impossible );         exit(0);       }     }     used[x] = 2;     ans += x +  a ;   }   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       cin >> s[i];     }     fill(eq, eq + N, 1);     for (int j = 0; j < N; ++j) {       for (int i = 1; i < n; ++i) {         if (s[i].size() > j && eq[i]) {           if (s[i - 1].size() > j) {             if (s[i - 1][j] != s[i][j]) {               g[s[i - 1][j] -  a ].push_back(s[i][j] -  a );               eq[i] = 0;             } else {               eq[i] = 1;             }           } else {             eq[i] = 0;           }         } else if (eq[i]) {           puts( Impossible );           return 0;         } else {           eq[i] = 0;         }       }     }     for (int i = 0; i < 26; ++i) {       if (!used[i]) {         dfs(i);       }     }     reverse(ans.begin(), ans.end());     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int p1, p2, p3, p4, a, b;   int fun(int x) { return x % p1 % p2 % p3 % p4; }   int main() {     while (~scanf( %d%d%d%d%d%d , &p1, &p2, &p3, &p4, &a, &b)) {       int cnt = 0;       for (int i = a; i <= b; i++) {         if (fun(i) == i) cnt++;       }       printf( %d n , cnt);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void main2() {     long long int n, m, sum = 0, t = INT_MAX, temp, r = 0;     cin >> n >> m;     for (long long int i = 0; i < n; i++) {       for (long long int j = 0; j < m; j++) {         cin >> temp;         if (temp < 0) r++;         sum += abs(temp);         t = min(t, abs(temp));       }     }     if (r % 2 == 0)       cout << sum <<   n ;     else       cout << sum - 2 * t <<   n ;     return;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int t = 1;     cin >> t;     while (t--) {       main2();     }     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   vector<long long int> lucky;   long long int l, r;   void gen(long long int a) {     if (a <= 1e11) lucky.push_back(a);     if (a * 10 + 4 <= 1e11) gen(a * 10 + 4);     if (a * 10 + 7 <= 1e11) gen(a * 10 + 7);   }   void solve() {     cin >> l >> r;     gen(4);     gen(7);     sort(lucky.begin(), lucky.end());     if (l == r) {       cout << *lower_bound(lucky.begin(), lucky.end(), l);       return;     }     long long int lb = lower_bound(lucky.begin(), lucky.end(), l) - lucky.begin();     long long int ans = (min(r, lucky[lb]) - l + 1) * lucky[lb], i;     for (i = lb + 1; lucky[i] <= r; i++) {       ans += (lucky[i] - lucky[i - 1]) * lucky[i];     }     i--;     if (lucky[i] < r) {       ans += (r - lucky[i]) * lucky[i + 1];     }     cout << ans;   }   int32_t main() {     ios::sync_with_stdio(false);     long long int t = 1;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int N, S, M, K;   int A[2020], B[2020];   int L[2020], R[2020];   vector<int> V;   int MR[2020];   int dp[1600][1600];   int ok(int v) {     int i, j;     for (i = 0; i < (N); i++) B[i + 1] = B[i] + (A[i] <= v);     memset(dp, 0, sizeof(dp));     for (i = 0; i < (N); i++) {       for (j = 0; j < (M); j++) {         if (MR[i] > i) {           dp[MR[i]][j + 1] = max(dp[MR[i]][j + 1], dp[i][j] + B[MR[i]] - B[i]);           if (dp[MR[i]][j + 1] >= K) return 1;         }         dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);       }     }     return 0;   }   void solve() {     int i, j, k, l, r, x, y;     string s;     cin >> N >> S >> M >> K;     for (i = 0; i < (N); i++) {       cin >> A[i];       V.push_back(A[i]);     }     for (i = 0; i < (S); i++) {       cin >> L[i] >> R[i];       for (j = L[i] - 1; j < R[i]; j++) MR[j] = max(MR[j], R[i]);     }     sort((V.begin()), (V.end()));     V.erase(unique((V.begin()), (V.end())), V.end());     if (ok(V.back()) == 0) return (void)printf( -1 n );     int ret = V.size() - 1;     for (i = 12; i >= 0; i--) {       if (ret >= (1 << i) && ok(V[ret - (1 << i)])) ret -= 1 << i;     }     cout << V[ret] << endl;   }   int main(int argc, char** argv) {     string s;     int i;     if (argc == 1) ios::sync_with_stdio(false), cin.tie(0);     for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s +=   n ;     for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin);     cout.tie(0);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x7fffffff;   const int maxn = 2e5 + 5;   int bit[2][maxn], num[maxn];   int n, k, a, b, q;   void update(int id, int x, int v) {     while (x <= n) {       bit[id][x] += v;       x += (x & -x);     }   }   int sum(int id, int x) {     int ans = 0;     while (x) {       ans += bit[id][x];       x -= x & -x;     }     return ans;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> k >> a >> b >> q;     int x, y, z;     while (q--) {       cin >> x;       if (x == 1) {         cin >> y >> z;         if (num[y] + z < b) {           update(0, y, z);           update(1, y, z);         } else if (num[y] + z < a) {           if (num[y] < b) update(0, y, b - num[y]);           update(1, y, z);         } else {           if (num[y] < b) update(0, y, b - num[y]);           if (num[y] < a) update(1, y, a - num[y]);         }         num[y] += z;       } else {         cin >> y;         int ans = sum(0, y - 1) + sum(1, n) - sum(1, y + k - 1);         cout << ans << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[100005], c[100005];   int main() {     long long n;     cin >> n;     for (long long i = 1; i <= n; i++) {       cin >> a[i];     }     for (int i = 1; i <= n; i++) {       c[i] = c[i - 1] + 20;       long long x = upper_bound(a + 1, a + 1 + n, a[i] - 90) - a;       c[i] = min(c[i], c[x - 1] + 50);       x = upper_bound(a + 1, a + 1 + n, a[i] - 1440) - a;       c[i] = min(c[i], c[x - 1] + 120);     }     for (int i = 1; i <= n; i++) {       cout << c[i] - c[i - 1] << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;   int n;   char s[N][10];   int val[N];   int l[N], r[N];   set<int> st[N];   void go(int u, int v) {     if (st[v].size() > st[u].size()) {       st[u].swap(st[v]);     }     for (int x : st[v]) st[u].insert(x);     set<int>().swap(st[v]);   }   void dfs(int u) {     if (*s[u] ==  I ) {       st[u].insert(u);       return;     }     if (*s[u] ==  N ) {       int v = l[u];       dfs(v);       val[u] = !val[v];       go(u, v);     } else if (*s[u] ==  A ) {       int v1 = l[u], v2 = r[u];       dfs(v1);       dfs(v2);       val[u] = val[v1] & val[v2];       if (val[u] != ((val[v1] ^ 1) & val[v2])) {         go(u, v1);       }       if (val[u] != (val[v1] & (val[v2] ^ 1))) {         go(u, v2);       }     } else if (*s[u] ==  O ) {       int v1 = l[u], v2 = r[u];       dfs(v1);       dfs(v2);       val[u] = val[v1] | val[v2];       if (val[u] != ((val[v1] ^ 1) | val[v2])) {         go(u, v1);       }       if (val[u] != (val[v1] | (val[v2] ^ 1))) {         go(u, v2);       }     } else {       int v1 = l[u], v2 = r[u];       dfs(v1);       dfs(v2);       val[u] = val[v1] ^ val[v2];       if (val[u] != ((val[v1] ^ 1) ^ val[v2])) {         go(u, v1);       }       if (val[u] != (val[v1] ^ (val[v2] ^ 1))) {         go(u, v2);       }     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       scanf( %s , s[i]);       if (*s[i] ==  I ) {         scanf( %d , val + i);       } else if (*s[i] ==  N ) {         scanf( %d , l + i);       } else {         scanf( %d%d , l + i, r + i);       }     }     dfs(1);     for (int i = 1; i <= n; ++i) {       if (*s[i] ==  I ) {         printf( %d , val[1] ^ (st[1].count(i)));       }     }     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline bool smin(T &a, const T &b) {     return b < a ? a = b, 1 : 0;   }   template <typename T>   inline bool smax(T &a, const T &b) {     return a < b ? a = b, 1 : 0;   }   const long long N = 3e5 + 10;   long long n, bad[N], m;   vector<long long> adj[N];   bool pos[N];   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     while (m--) {       long long u, v;       cin >> u >> v;       adj[v].push_back(u);       adj[u].push_back(v);     }     set<pair<long long, long long> > s;     for (long long i = 1; i <= n; i++)       bad[i] = (long long)adj[i].size(), s.insert({bad[i], i});     while (1) {       pair<long long, long long> pi = *s.rbegin();       s.erase(pi);       long long v = pi.second;       if (bad[v] < 2) break;       bad[v] = (long long)adj[v].size() - bad[v];       for (long long u : adj[v]) {         s.erase({bad[u], u});         if (pos[u] == pos[v]) {           bad[u]--;         } else {           bad[u]++;         }         s.insert({bad[u], u});       }       pos[v] ^= 1;       s.insert({bad[v], v});     }     for (long long i = 1; i <= n; i++) cout << pos[i];   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 3e5 + 5;   int arr[maxn];   int main() {     int n;     scanf( %d , &n);     int pos = n;     cout << 1 << endl;     for (int i = 1; i <= n; i++) {       int tmp;       scanf( %d , &tmp);       arr[tmp] = 1;       while (arr[pos]) pos--;       printf(  %d , i - (n - pos) + 1);     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long pw(long long a, long long b, long long p) {     if (b == 0) return 1;     long long t = pw(a, b / 2, p);     if (b % 2)       return ((a * t * t) % p);     else       return ((t * t) % p);   }   map<long long, pair<long long, long long> > m;   long long b[100005];   void solve() {     long long n, k;     cin >> n >> k;     long long a[n];     for (long long i = 1; i <= n; i++) cin >> a[i];     sort(a + 1, a + n + 1);     for (long long j = 1; j <= n; j++) {       long long i = a[j];       b[j] = i;       b[j] += b[j - 1];       m[i].first++;       if (m[i].first == 1) m[i].second = j;     }     long long mx = 0, mn = -1;     for (auto z : m) {       long long lmx = 0;       lmx += z.second.first;       long long u = z.second.second, l = 0, f = u - 1;       while (u >= l) {         long long mid = (l + u) / 2;         long long ss = b[z.second.second - 1] - b[mid];         ss = z.first * (z.second.second - mid - 1) - ss;         if (k < ss) {           l = mid + 1;         }         if (k >= ss) {           f = mid;           u = mid - 1;         }       }       lmx += z.second.second - f - 1;       if (lmx > mx) {         mx = lmx;         mn = z.first;       }     }     cout << mx <<     << mn;   }   signed main() {     ios_base::sync_with_stdio(false);     long long t = 1;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int t, n, k;   char a[5][500];   bool vis[5][500], check;   bool valid(int x, int y) {     if (x < 0 || x > 2 || vis[x][y]) return 0;     return 1;   }   void dfs(int x, int y, int tm) {     if (!valid(x, y) || check) return;     if (a[x][y + 2 * tm] !=  . ) return;     vis[x][y] = 1;     if (y >= n - 1) {       check = 1;       return;     }     if (a[x][y + 2 * tm + 1] !=  . ) return;     dfs(x, y + 1, tm + 1);     if (a[x + 1][y + 1 + 2 * tm] ==  . ) dfs(x + 1, y + 1, tm + 1);     if (a[x - 1][y + 1 + 2 * tm] ==  . ) dfs(x - 1, y + 1, tm + 1);   }   int main() {     cin >> t;     while (t--) {       check = 0;       memset(vis, 0, sizeof(vis));       for (int i = 0; i < 4; i++)         for (int j = 0; j < 500; j++) a[i][j] =  . ;       cin >> n >> k;       for (int i = 0; i < 3; i++)         for (int j = 0; j < n; j++) cin >> a[i][j];       int p;       for (int i = 0; i < 3; i++)         if (a[i][0] ==  s ) p = i;       a[p][0] =  . ;       dfs(p, 0, 0);       if (check)         cout <<  YES n ;       else         cout <<  NO n ;     }     return 0;   }


#include <bits/stdc++.h>   #pragma hdrstop   using namespace std;   void rot(int n, int *x, int *y, int rx, int ry);   int xy2d(int n, int x, int y) {     int rx, ry, s, d = 0;     for (s = n / 2; s > 0; s /= 2) {       rx = (x & s) > 0;       ry = (y & s) > 0;       d += s * s * ((3 * rx) ^ ry);       rot(s, &x, &y, rx, ry);     }     return d;   }   void d2xy(int n, int d, int *x, int *y) {     int rx, ry, s, t = d;     *x = *y = 0;     for (s = 1; s < n; s *= 2) {       rx = 1 & (t / 2);       ry = 1 & (t ^ rx);       rot(s, x, y, rx, ry);       *x += s * rx;       *y += s * ry;       t /= 4;     }   }   void rot(int n, int *x, int *y, int rx, int ry) {     int t;     if (ry == 0) {       if (rx == 1) {         *x = n - 1 - *x;         *y = n - 1 - *y;       }       t = *x;       *x = *y;       *y = t;     }   }   #pragma argsused   int main(int argc, char *argv[]) {     int a, b;     cin >> a >> b;     int x, y;     d2xy(1 << a, b, &x, &y);     cout << x <<     << y << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void Read(int &p) {     p = 0;     char c = getchar();     for (; c <  0  || c >  9 ; c = getchar())       ;     for (; c >=  0  && c <=  9 ; c = getchar()) p = p * 10 + c -  0 ;   }   int n, k;   struct Node {     long long siz;     int mxx[12], mnn[12];     bool operator<(const Node &b) const {       for (int i = 1; i <= k; i++)         if (mxx[i] > b.mnn[i]) return 0;       return 1;     }   } S;   set<Node> M;   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) {       S.siz = 1;       for (int j = 1; j <= k; j++) {         int x;         Read(x);         S.mnn[j] = S.mxx[j] = x;       }       set<Node>::iterator it = M.find(S);       while (it != M.end()) {         S.siz += it->siz;         for (int j = 1; j <= k; j++) {           S.mxx[j] = max(S.mxx[j], it->mxx[j]);           S.mnn[j] = min(S.mnn[j], it->mnn[j]);         }         M.erase(it);         it = M.find(S);       }       M.insert(S);       printf( %lld n , M.rbegin()->siz);     }   }


#include <bits/stdc++.h>   using namespace std;   int n, ind, mini(1000000000);   int t[11];   string ans =   ;   char chr(int x) { return char( 0  + x); }   int main() {     cin >> n;     for (int i = 1; i < 10; i++) {       cin >> t[i];       if (t[i] <= mini) {         mini = t[i];         ind = i;       }     }     if (mini > n) {       cout << -1;       return 0;     }     while (n - mini >= 0) {       ans += chr(ind);       n -= mini;     }     for (int i = 0; i < (int)ans.size(); i++) {       for (int j = 9; j > ind; j--) {         if (n - t[j] + mini >= 0) {           ans[i] = chr(j);           n -= t[j] - mini;           break;         }       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int INF = 1e18;   const int inf = 1e9;   const int MOD = 1e9 + 7;   const int nax = 500000 + 10;   int arr[nax], color[nax];   vector<int> graph[nax];   map<int, set<int> > map1;   int main() {     ios::sync_with_stdio(0);     int n, m;     cin >> n >> m;     for (int i = 1; i <= n; i++) cin >> color[i];     while (m--) {       int u, v;       cin >> u >> v;       graph[u].push_back(v);       graph[v].push_back(u);     }     for (int i = 1; i <= n; i++) {       for (auto x : graph[i]) {         if (color[i] != color[x]) map1[color[i]].insert(color[x]);       }     }     int maxi = map1[color[1]].size(), ans = color[1];     for (int i = 2; i <= n; i++) {       if (map1[color[i]].size() == maxi && color[i] < ans) {         ans = color[i];       } else if (map1[color[i]].size() > maxi) {         maxi = map1[color[i]].size();         ans = color[i];       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 200001;   const long long int MOD = 1000000007;   long long int N = 1, c[M], i, j, N2, m;   bool flag;   int main() {     cin >> m;     fill(c, c + M, 1);     while (m--) {       int p;       cin >> p;       c[p]++;       N *= p, N %= MOD;     }     for (i = 1; i < M; i++)       if (c[i] % 2 == 0 && !flag) {         c[i] /= 2, flag = true;         break;       }     if (!flag) {       N = 1;       for (i = 1; i < M; i++)         for (j = 1; j * 2 < c[i]; j++) N *= i, N %= MOD;     }     for (i = 1; i < M; i++) {       N2 = N;       for (j = 1; j < c[i]; j++) N *= N2, N %= MOD;     }     cout << N << endl;   }


#include <bits/stdc++.h>   using namespace std;   map<pair<int, int>, int> mp;   int x[2005], y[2005];   int main() {     int n, ans = 0;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d %d , x + i, y + i);       for (int j = 0; j < i; j++) ans += mp[{x[i] + x[j], y[i] + y[j]}]++;     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, T, fa[500005], top, st[500005 << 2], d[500005], a[500005];   struct E {     int x, y, l, r;   } e[500005];   int Getfa(int x) {     while (fa[x] != x) x = fa[x];     return x;   }   int col(int x) {     int now = 0;     while (fa[x] != x) now ^= a[x], x = fa[x];     return now;   }   void link(int x, int y, int D) {     if (d[x] > d[y]) swap(x, y);     if (d[x] == d[y]) d[y]++, st[++top] = -y;     fa[x] = y;     a[x] = D;     st[++top] = x;   }   void RE(int t) {     for (; top > t; top--)       if (st[top] < 0)         d[-st[top]]--;       else         fa[st[top]] = st[top], a[st[top]] = 0;   }   void work(int l, int r, int k) {     int mid = (l + r) >> 1, now = top;     for (int i = 1; i <= k; i++)       if (e[i].l <= l && r <= e[i].r) {         int u = Getfa(e[i].x), v = Getfa(e[i].y);         if (u != v)           link(u, v, col(e[i].x) == col(e[i].y));         else if (col(e[i].x) == col(e[i].y)) {           for (int i = l; i <= r; i++) puts( NO );           RE(now);           return;         }         swap(e[k--], e[i--]);       }     if (l == r)       puts( YES );     else {       int i, j;       for (i = 1, j = 0; i <= k; i++)         if (e[i].l <= mid) swap(e[i], e[++j]);       work(l, mid, j);       for (i = 1, j = 0; i <= k; i++)         if (e[i].r > mid) swap(e[i], e[++j]);       work(mid + 1, r, j);     }     RE(now);   }   map<int, int> M[500005];   int main() {     scanf( %d%d , &n, &T);     for (int i = 1; i <= T; i++) {       int x, y;       scanf( %d%d , &x, &y);       if (x > y) swap(x, y);       if (M[x][y])         e[M[x][y]].r = i - 1, M[x][y] = 0;       else {         M[x][y] = ++m;         e[m].x = x;         e[m].y = y;         e[m].l = i - 1;         e[m].r = T;       }     }     for (int i = 1; i <= m; i++) {       if (++e[i].l > e[i].r) {         i--, m--;         assert(false);       }     }     for (int i = 1; i <= n; i++) fa[i] = i, d[i] = 1;     work(1, T, m);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   char s[100010];   int main() {     cin >> n >> m;     scanf( %s , s + 1);     while (m--) {       int l, r;       cin >> l >> r;       char a[2], b[2];       scanf( %s%s , a, b);       for (int i = l; i <= r; i++)         if (s[i] == a[0]) s[i] = b[0];     }     printf( %s , s + 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1010;   bool vis[N][N];   int n, ai, aj;   bool can(int i, int j) {     if (ai == i || aj == j || i + j == ai + aj || i - j == ai - aj) return 0;     return (i <= n && i >= 1 && j <= n && j >= 1);   }   int bi, bj, ci, cj;   int di[] = {0, 0, 1, 1, 1, -1, -1, -1};   int dj[] = {1, -1, 0, 1, -1, 0, 1, -1};   void dfs(int i, int j) {     if (!can(i, j) || vis[i][j]) return;     vis[i][j] = 1;     for (int z = 0; z < 8; ++z) dfs(i + di[z], j + dj[z]);   }   int main() {     cin >> n >> ai >> aj >> bi >> bj >> ci >> cj;     dfs(bi, bj);     if (vis[ci][cj])       puts( YES );     else       puts( NO );   }


#include <bits/stdc++.h>   using namespace std;   int main() {     vector<long long int> v;     long long int n, num, max_ind = 0, max_num = -1;     cin >> n;     for (long long int i = 0; i < n; i++) {       cin >> num;       v.push_back(num);       if (num > max_num) {         max_num = num;         max_ind = i;       }     }     if (max_num != 1)       v[max_ind] = 1;     else       v[max_ind] = 2;     sort(v.begin(), v.end());     for (long long int i = 0; i < n; i++) {       cout << v[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T, class S>   ostream& operator<<(ostream& os, const pair<T, S>& v) {     return os <<  (  << v.first <<  ,   << v.second <<  ) ;   }   template <class T>   ostream& operator<<(ostream& os, const vector<T>& v) {     os <<  [ ;     for (int i = int(0); i <= int((static_cast<int>((v).size())) - 1); ++i) {       if (i) os <<  ,  ;       os << v[i];     }     return os <<  ] ;   }   template <class T>   bool setmax(T& _a, T _b) {     if (_a < _b) {       _a = _b;       return true;     }     return false;   }   template <class T>   bool setmin(T& _a, T _b) {     if (_b < _a) {       _a = _b;       return true;     }     return false;   }   template <class T>   T gcd(T _a, T _b) {     return _b == 0 ? _a : gcd(_b, _a % _b);   }   int main() {     int n, k;     cin >> n >> k;     vector<int> a(n);     iota((a).begin(), (a).end(), 0);     int cur = 0;     while (k--) {       int x;       cin >> x;       int next = (cur + x) % static_cast<int>((a).size());       cout << a[next] + 1 <<    ;       a.erase(a.begin() + next);       cur = next % static_cast<int>((a).size());     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:536870912 )   #pragma warning(disable : 4996)   using namespace std;   const int N = 305;   int dp[N * 2][N][N], ar[N][N];   int n;   pair<int, int> getC(int t, int i) {     return t <= n - 1 ? make_pair(t - i, i) : make_pair(n - 1 - i, t - n + 1 + i);   }   int getS(int t) { return t <= n - 1 ? t + 1 : (2 * n - 1 - t); }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) scanf( %d , &ar[i][j]);     }     memset(dp, 128, sizeof(dp));     ;     dp[0][0][0] = ar[0][0];     for (int i = 0; i < 2 * n - 2; i++) {       int t = getS(i + 1), c = getS(i);       for (int a = 0; a < c; a++) {         for (int b = 0; b < c; b++) {           for (int p = (i < n - 1 ? 0 : -1); p < (i < n - 1 ? 2 : 1); p++) {             for (int q = (i < n - 1 ? 0 : -1); q < (i < n - 1 ? 2 : 1); q++) {               auto g = getC(i + 1, a + p), h = getC(i + 1, b + q);               if (g.first >= n || g.second >= n || h.first >= n ||                   h.second >= n || g.first < 0 || g.second < 0 || h.first < 0 ||                   h.second < 0)                 continue;               int sum = dp[i][a][b] + ar[g.first][g.second];               if (g != h) sum += ar[h.first][h.second];               dp[i + 1][a + p][b + q] = max(dp[i + 1][a + p][b + q], sum);             }           }         }       }     }     printf( %d , dp[2 * n - 2][0][0]);     return 0;   }


#include <bits/stdc++.h>   struct node {     int* children;     node() {       children = new int[2];       children[0] = children[1] = -1;     }     int& operator[](int i) { return children[i]; }   };   struct trie {     std::vector<node> nodes;     int root;     trie() {       nodes.push_back(node());       root = 0;     }     void insert(long long x) {       int u = this->root;       for (int i = 41; i >= 0; --i) {         int v = ((x & (1LL << i)) ? 1 : 0);         int next = nodes[u][v];         if (next == -1) {           next = nodes[u][v] = nodes.size();           nodes.push_back(node());         }         u = next;       }     }     long long maximize(long long x) {       int u = this->root;       long long ans = 0;       for (int i = 41; i >= 0; --i) {         int cnt = 0, v;         if (nodes[u][0] != -1) {           cnt++;           v = 0;         }         if (nodes[u][1] != -1) {           cnt++;           v = 1;         }         if (cnt == 0)           return ans;         else if (cnt == 1) {           if (v) ans ^= (1LL << i);           u = nodes[u][v];         } else {           v = (x & (1LL << i) ? 1 : 0) ^ 1;           if (v) ans ^= (1LL << i);           u = nodes[u][v];         }       }       return ans;     }   };   int N;   long long partial_xor[100001];   int main() {     std::ios_base::sync_with_stdio(false);     std::cin.tie(NULL);     std::cout.tie(NULL);     std::cin >> N;     for (int i = 0; i < N; ++i) {       std::cin >> partial_xor[i];       if (i) partial_xor[i] ^= partial_xor[i - 1];     }     long long xor_sum = partial_xor[N - 1];     long long ans = xor_sum;     trie t;     for (int i = 0; i < N; ++i) {       ans = std::max(ans, partial_xor[i]);       if (i) {         ans = std::max(ans, partial_xor[i - 1] ^ xor_sum);         ans = std::max(ans, t.maximize(partial_xor[i - 1]) ^ partial_xor[i - 1]);       }       t.insert(partial_xor[i] ^ xor_sum);     }     std::cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   multiset<int> maxH, maxV;   set<pair<int, int> > vH, vV;   int main() {     int w, h, n, x;     std::set<pair<int, int> >::iterator it, itL, itR;     char ch;     cin >> w >> h >> n;     vH.insert(make_pair(0, h));     vH.insert(make_pair(h, 0));     vV.insert(make_pair(0, w));     vV.insert(make_pair(w, 0));     maxH.insert(h);     maxV.insert(w);     for (int i = 0; i < n; i++) {       cin >> ch >> x;       if (ch ==  H ) {         vH.insert(make_pair(x, 0));         it = vH.find(make_pair(x, 0));         pair<int, int> lp, p, rp;         it--;         lp = *it;         it++;         p = *it;         it++;         rp = *it;         vH.insert(make_pair((int)lp.first, x - lp.first));         vH.erase(vH.find(lp));         vH.insert(make_pair(x, rp.first - x));         vH.erase(vH.find(p));         lp = make_pair(lp.first, x - lp.first);         p = make_pair(x, rp.first - x);         maxH.erase(maxH.find(rp.first - lp.first));         maxH.insert(lp.second);         maxH.insert(p.second);         cout << 1ll * (*maxV.rbegin()) * (*maxH.rbegin()) << endl;       } else {         vV.insert(make_pair(x, 0));         it = vV.find(make_pair(x, 0));         pair<int, int> lp, p, rp;         it--;         lp = (*it);         it++;         p = (*it);         it++;         rp = (*it);         vV.insert(make_pair(lp.first, x - lp.first));         vV.erase(vV.find(lp));         vV.insert(make_pair(x, rp.first - x));         vV.erase(vV.find(p));         lp = make_pair(lp.first, x - lp.first);         p = make_pair(x, rp.first - x);         maxV.erase(maxV.find(rp.first - lp.first));         maxV.insert(lp.second);         maxV.insert(p.second);         cout << 1ll * (*maxH.rbegin()) * (*maxV.rbegin()) << endl;       }     }     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   const int INF = 2e9;   const int MAXN = 900;   const int MAXM = 1900000;   int N, edges;   int last[MAXN], preve[MAXM], head[MAXM];   int Cap[MAXM], Flow[MAXM];   int dist[MAXN];   int nextEdge[MAXN];   queue<int> Q;   void init(int N) {     edges = 0;     memset(last, -1, sizeof last);   }   set<int> namex, namey;   vector<int> xx, yy;   void handlex(int f) {     if (namex.find(f) != namex.end()) return;     namex.insert(f);     xx.push_back(f);   }   void handley(int f) {     if (namey.find(f) != namey.end()) return;     yy.push_back(f);     namey.insert(f);   }   inline void add_edge(int u, int v, int cap, int flow = 0) {     head[edges] = v;     preve[edges] = last[u];     Cap[edges] = cap;     Flow[edges] = flow;     last[u] = edges++;     head[edges] = u;     preve[edges] = last[v];     Cap[edges] = 0;     Flow[edges] = 0;     last[v] = edges++;   }   inline bool dinicBfs(int S, int E, int N) {     int from = S, to, cap, flow;     memset(dist, 0, sizeof(int) * N);     dist[from] = 1;     while (!Q.empty()) Q.pop();     Q.push(from);     while (!Q.empty()) {       from = Q.front();       Q.pop();       for (int e = last[from]; e >= 0; e = preve[e]) {         to = head[e];         cap = Cap[e];         flow = Flow[e];         if (!dist[to] && cap > flow) {           dist[to] = dist[from] + 1;           Q.push(to);           if (to == E) return true;         }       }     }     return (dist[E] != 0);   }   inline int dfs(int from, int minEdge, int E) {     if (!minEdge) return 0;     if (from == E) return minEdge;     int to, e, cap, flow, ret;     for (; nextEdge[from] >= 0; nextEdge[from] = preve[e]) {       e = nextEdge[from];       to = head[e];       cap = Cap[e];       flow = Flow[e];       if (dist[to] != dist[from] + 1) continue;       ret = dfs(to, min(minEdge, cap - flow), E);       if (ret) {         Flow[e] += ret;         Flow[e ^ 1] -= ret;         return ret;       }     }     return 0;   }   int dinicUpdate(int S, int E) {     int flow = 0;     while (int minEdge = dfs(S, INF, E)) {       if (minEdge == 0) break;       flow += minEdge;     }     return flow;   }   int maxFlow(int S, int E, int N) {     int totFlow = 0;     while (dinicBfs(S, E, N)) {       for (int i = 0; i <= N; i++) nextEdge[i] = last[i];       totFlow += dinicUpdate(S, E);     }     return totFlow;   }   int x1[60], y11[60], x2[60], y2[60];   int main() {     int i, j, k, l, m, n;     init(32);     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++) {       scanf( %d%d%d%d , &x1[i], &y11[i], &x2[i], &y2[i]);       x2[i]++;       y2[i]++;       handlex(x1[i]);       handlex(x2[i]);       handley(y11[i]);       handley(y2[i]);     }     sort(xx.begin(), xx.end());     sort(yy.begin(), yy.end());     int so = xx.size() + yy.size() + 1;     int sink = so + 1;     for (int i = 1; i <= m; i++) {       for (int j = 1; j < xx.size(); j++) {         if (xx[j] <= x1[i]) continue;         if (xx[j] > x2[i]) break;         for (int k = 1; k < yy.size(); k++) {           if (yy[k] <= y11[i]) continue;           if (yy[k] > y2[i]) break;           int g = xx.size() + k;           add_edge(j, g, INF);         }       }     }     for (int i = 1; i < xx.size(); i++) {       add_edge(so, i, xx[i] - xx[i - 1]);     }     for (int i = 1; i < yy.size(); i++) {       int g = xx.size() + i;       add_edge(g, sink, yy[i] - yy[i - 1]);     }     int ans = maxFlow(so, sink, sink + 2);     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = (long long)1e18;   const long long mod = (long long)1e9 + 7;   const double eps = (double)1e-9;   const double pi = acos(-1.0);   const int dx[] = {0, 0, 1, 0, -1};   const int dy[] = {0, 1, 0, -1, 0};   const int N = 100500;   int n, x, y, a, b, T;   long long k;   multiset<long long> st, st1;   long long check(int j) {     st1 = st;     long long ans = 0;     for (int i = 1; i <= j; ++i)       if (i % a == 0 && i % b == 0) {         ans += *st1.begin() * (x + y);         st1.erase(st1.begin());       }     for (int i = 1; i <= j; ++i)       if (i % a == 0 && i % b != 0) {         ans += *st1.begin() * x;         st1.erase(st1.begin());       }     for (int i = 1; i <= j; ++i)       if (i % b == 0 && i % a != 0) {         ans += *st1.begin() * y;         st1.erase(st1.begin());       }     return -ans;   }   void solve() {     cin >> n;     st.clear();     for (int i = 1; i <= n; ++i) {       long long x;       cin >> x;       st.insert(-x / 100);     }     cerr << st.size() << endl;     cin >> x >> a >> y >> b >> k;     if (x < y) swap(x, y), swap(a, b);     int l = 1, r = n;     while (l <= r) {       int md = (l + r) >> 1;       if (check(md) >= k)         r = md - 1;       else         l = md + 1;     }     if (r == n) r = -2;     cout << r + 1 << endl;   }   int main() {     cin.tie(NULL);     cout.tie(NULL);     ios_base::sync_with_stdio(false);     cin >> T;     while (T--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   pair<long long, long long> add_pair(pair<long long, long long> a,                                       pair<long long, long long> b) {     return make_pair(a.first + b.first, a.second + b.second);   }   long long power(long long a, long long b) {     long long res = 1;     a = a % ((long long)1e9 + 7);     while (b > 0) {       if (b & 1) {         res = (res * a) % ((long long)1e9 + 7);         b--;       }       a = (a * a) % ((long long)1e9 + 7);       b >>= 1;     }     return res;   }   long long fermat_inv(long long y) { return power(y, ((long long)1e9 + 7) - 2); }   long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }   long long mul(long long a, long long b) {     return (a * (b % ((long long)1e9 + 7))) % ((long long)1e9 + 7);   }   long long sub(long long a, long long b) {     return ((a - b) % ((long long)1e9 + 7) + ((long long)1e9 + 7)) %            ((long long)1e9 + 7);   }   long long add(long long a, long long b) {     return ((a + b) % ((long long)1e9 + 7) + ((long long)1e9 + 7)) %            ((long long)1e9 + 7);   }   vector<vector<long long> > G;   map<long long, long long> m[100005];   vector<long long> b;   long long ans;   void dfs(long long rt, long long par) {     m[rt][b[rt]]++;     if (rt != par) {       for (auto it = (m[par]).begin(); it != (m[par]).end(); it++) {         m[rt][gcd(b[rt], it->first)] += it->second;       }     }     for (auto it = (m[rt]).begin(); it != (m[rt]).end(); it++) {       ans = add(ans, mul(it->first, it->second));     }     for (auto it = (G[rt]).begin(); it != (G[rt]).end(); it++) {       if (*it != par) {         dfs(*it, rt);       }     }   }   void solve() {     long long n, u, v, i;     cin >> n;     G = vector<vector<long long> >(n + 1, vector<long long>());     b = vector<long long>(n + 1);     for (i = (0); i < (n); i += (1)) cin >> b[i + 1];     for (i = (0); i < (n - 1); i += (1)) {       cin >> u >> v;       G[u].push_back(v);       G[v].push_back(u);     }     ans = 0;     dfs(1, 1);     cout << ans <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   int add(int a, int b) {     long long x = a + b;     if (x >= 1000000009) x -= 1000000009;     if (x < 0) x += 1000000009;     return x;   }   long long mul(long long a, long long b) { return (a * b) % 1000000009; }   long long pw(long long a, long long b) {     long long ans = 1;     while (b) {       if (b & 1) ans = (ans * a) % 1000000009;       a = (a * a) % 1000000009;       b >>= 1;     }     return ans;   }   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   long long rand_seed() {     long long a = rng();     return a;   }   int n;   map<int, pair<int, int> > pos;   map<pair<int, int>, int> id;   vector<set<int> > p, ch;   bool canrem(int i) {     int ok = 1;     for (auto j : p[i]) ok &= (ch[j].size() > 1);     return ok;   }   void rem(int i) {     for (auto j : p[i]) ch[j].erase(i);     for (auto j : ch[i]) p[j].erase(i);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n;     p.resize(n);     ch.resize(n);     for (int x, y, i = 0; i < n; i++) {       cin >> y >> x;       pos[i] = {x, y};       id[{x, y}] = i;     }     for (int i = 0; i < n; i++) {       int x = pos[i].first, y = pos[i].second;       for (int z = -1; z < 2; z++) {         if (id.count({x + 1, y + z})) p[i].insert(id[{x + 1, y + z}]);         if (id.count({x - 1, y + z})) ch[i].insert(id[{x - 1, y + z}]);       }     }     set<int> can;     for (int i = 0; i < n; i++) {       int x = pos[i].first, y = pos[i].second;       if (canrem(i)) can.insert(i);     }     long long ans = 0;     for (int i = 0; i < n; i++) {       int j;       if (i % 2 == 0)         j = *can.rbegin();       else         j = *can.begin();       ans = mul(ans, n);       ans = add(ans, j);       int x = pos[j].first;       int y = pos[j].second;       rem(j);       can.erase(j);       id.erase(pos[j]);       for (int z = -2; z < 3; z++) {         if (id.count({x, y + z})) can.erase(id[{x, y + z}]);         if (id.count({x - 1, y + z})) can.erase(id[{x - 1, y + z}]);       }       for (int z = -2; z < 3; z++) {         if (id.count({x, y + z}) && canrem(id[{x, y + z}]))           can.insert(id[{x, y + z}]);         x--;         if (id.count({x, y + z}) && canrem(id[{x, y + z}]))           can.insert(id[{x, y + z}]);         x++;       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, x;     cin >> n;     long long b = n, i(0), c(0);     for (int i = n + 1;; i++) {       b = i;       if (b < 0) b *= -1;       while (b != 0) {         x = b % 10;         if (x == 8) {           c = 1;           break;         }         b /= 10;       }       if (c == 1) {         cout << i - n << endl;         return 0;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = 1e10;   long long s[300005];   int main() {     int T;     long long n, k;     scanf( %d , &T);     while (T--) {       scanf( %lld %lld , &n, &k);       for (long long i = 1; i <= n; i++) scanf( %lld n , &s[i]);       long long ans = inf, v;       for (long long i = k + 1; i <= n; i++) {         if (ans > (s[i] - s[i - k])) ans = s[i] - s[i - k], v = i;       }       long long kk = (s[v] + s[v - k]) / 2;       cout << kk << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long deal(long long x, long long y) {     int a = x + y;     return (x + 1) * x * (y + 1) / 2 + y * (y + 1) / 2 * (x + 1);   }   long long max(long long a, long long b) { return a > b ? a : b; }   int main() {     long long m, b;     long long ans = -1;     scanf( %lld%lld , &m, &b);     for (int i = 0;; i += m) {       long long ys = b - i / m;       if (ys < 0) break;       ans = max(ans, deal(i, ys));     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using pii = pair<int, int>;   using pil = pair<int, ll>;   using pli = pair<ll, int>;   using pll = pair<ll, ll>;   template <typename T>   bool chmax(T &x, const T &y) {     return (x < y) ? (x = y, true) : false;   }   template <typename T>   bool chmin(T &x, const T &y) {     return (x > y) ? (x = y, true) : false;   }   template <typename T>   int flg(T x, int i) {     return (x >> i) & 1;   }   template <typename T>   void print(const vector<T> &v, T x = 0) {     int n = v.size();     for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ?   n  :    );   }   template <typename T>   void printn(const vector<T> &v, T x = 0) {     int n = v.size();     for (int i = 0; i < n; i++) cout << v[i] + x <<   n ;   }   template <typename T>   int lb(const vector<T> &v, T x) {     return lower_bound(begin(v), end(v), x) - begin(v);   }   template <typename T>   int ub(const vector<T> &v, T x) {     return upper_bound(begin(v), end(v), x) - begin(v);   }   template <typename T>   void rearrange(vector<T> &v) {     sort(begin(v), end(v));     v.erase(unique(begin(v), end(v)), end(v));   }   template <typename T>   vector<int> id_sort(const vector<T> &v, bool greater = false) {     int n = v.size();     vector<int> ret(n);     iota(begin(ret), end(ret), 0);     sort(begin(ret), end(ret),          [&](int i, int j) { return greater ? v[i] > v[j] : v[i] < v[j]; });     return ret;   }   struct io_setup {     io_setup() {       ios_base::sync_with_stdio(false);       cin.tie(NULL);       cout << fixed << setprecision(15);     }   } io_setup;   const int inf = (1 << 30) - 1;   const ll INF = (1LL << 60) - 1;   const int MOD = 1000000007;   template <int mod>   struct Mod_Int {     int x;     Mod_Int() : x(0) {}     Mod_Int(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}     static int get_mod() { return mod; }     Mod_Int &operator+=(const Mod_Int &p) {       if ((x += p.x) >= mod) x -= mod;       return *this;     }     Mod_Int &operator-=(const Mod_Int &p) {       if ((x += mod - p.x) >= mod) x -= mod;       return *this;     }     Mod_Int &operator*=(const Mod_Int &p) {       x = (int)(1LL * x * p.x % mod);       return *this;     }     Mod_Int &operator/=(const Mod_Int &p) {       *this *= p.inverse();       return *this;     }     Mod_Int &operator++() { return *this += Mod_Int(1); }     Mod_Int operator++(int) {       Mod_Int tmp = *this;       ++*this;       return tmp;     }     Mod_Int &operator--() { return *this -= Mod_Int(1); }     Mod_Int operator--(int) {       Mod_Int tmp = *this;       --*this;       return tmp;     }     Mod_Int operator-() const { return Mod_Int(-x); }     Mod_Int operator+(const Mod_Int &p) const { return Mod_Int(*this) += p; }     Mod_Int operator-(const Mod_Int &p) const { return Mod_Int(*this) -= p; }     Mod_Int operator*(const Mod_Int &p) const { return Mod_Int(*this) *= p; }     Mod_Int operator/(const Mod_Int &p) const { return Mod_Int(*this) /= p; }     bool operator==(const Mod_Int &p) const { return x == p.x; }     bool operator!=(const Mod_Int &p) const { return x != p.x; }     Mod_Int inverse() const {       assert(*this != Mod_Int(0));       return pow(mod - 2);     }     Mod_Int pow(long long k) const {       Mod_Int now = *this, ret = 1;       for (; k > 0; k >>= 1, now *= now) {         if (k & 1) ret *= now;       }       return ret;     }     friend ostream &operator<<(ostream &os, const Mod_Int &p) {       return os << p.x;     }     friend istream &operator>>(istream &is, Mod_Int &p) {       long long a;       is >> a;       p = Mod_Int<mod>(a);       return is;     }   };   using mint = Mod_Int<MOD>;   int main() {     int N;     cin >> N;     vector<int> a(N);     for (int i = 0; i < N; i++) cin >> a[i];     int M = 100000;     vector<vector<int>> ds(M + 1);     vector<ll> c(M + 1);     iota(c.begin(), c.end(), 0);     for (int i = 1; i <= M; i++) {       ds[i].emplace_back(i);       for (int j = 2 * i; j <= M; j += i) {         c[j] -= c[i];         ds[j].emplace_back(i);       }     }     mint ans = 0;     vector<ll> cnt(M + 1, 0);     vector<bool> used(M + 1, false);     for (int i = 1; i <= N; i++) {       mint tmp = 0;       for (int j = i - 1; j < N; j += i) {         for (auto &e : ds[a[j]]) {           used[e] = true;           cnt[e]++;         }       }       for (int j = i - 1; j < N; j += i) {         for (auto &e : ds[a[j]]) {           if (used[e]) {             used[e] = false;             tmp += c[e] * cnt[e] * cnt[e];           }           cnt[e]--;         }       }       ans += tmp * c[i];     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 101;   int prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53};   const int sz = sizeof(prime) / sizeof(int), V = 58;   int pos[V + 1], dp[N][1 << sz], n, A[N], minPrime[V + 1], trace[N][1 << sz];   void prepare() {     for (int i = 2; i * i <= V; ++i)       if (!minPrime[i])         for (int j = i * i; j <= V; j += i)           if (!minPrime[j]) minPrime[j] = i;     for (int i = 2; i <= V; ++i)       if (!minPrime[i]) minPrime[i] = i;     for (int i = 0; i < sz; ++i) pos[prime[i]] = i;   }   int solve(int idx, int msk) {     if (idx > n) return 0;     int &res = dp[idx][msk];     if (res != INT_MAX) return res;     int s, val, d;     for (int x = 1; x <= 58; ++x) {       val = x;       d = -1;       s = 0;       while (val > 1) {         if (minPrime[val] != d) {           d = minPrime[val];           s |= (1 << pos[d]);         }         val /= d;       }       if (!(s & msk)) {         val = abs(x - A[idx]) + solve(idx + 1, msk | s);         if (val < res) {           res = val;           trace[idx][msk] = x;         }       }     }     return res;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> n;     for (int i = 1; i <= n; ++i) cin >> A[i];     fill(&dp[0][0], &dp[0][0] + sizeof(dp) / sizeof(int), INT_MAX);     prepare();     solve(1, 0);     int msk = 0, val, d, s;     for (int i = 1; i <= n; ++i) {       val = trace[i][msk];       cout << val <<    ;       d = -1;       s = 0;       while (val > 1) {         if (minPrime[val] != d) {           d = minPrime[val];           s |= (1 << pos[d]);         }         val /= d;       }       msk |= s;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 1e5 + 3;   struct node {     int nonzero, val;     void merge(node &n1, node &n2, int range) {       if (val)         nonzero = range;       else         nonzero = n1.nonzero + n2.nonzero;     }     void update(int x) { val += x; }   } tree[7 * MAX];   void update(int root, int l, int r, int u, int v, int val) {     if (u <= l and v >= r) {       tree[root].update(val);       tree[root].merge(tree[2 * root], tree[2 * root + 1], r - l + 1);       return;     }     int mid = (l + r) / 2;     if (u <= mid) update(2 * root, l, mid, u, v, val);     if (v > mid) update(2 * root + 1, mid + 1, r, u, v, val);     tree[root].merge(tree[2 * root], tree[2 * root + 1], r - l + 1);   }   vector<int> G[MAX], intervals[MAX];   int st[MAX], en[MAX], Time = 0, ans[MAX], n, m;   void dfs(int x, int par) {     st[x] = ++Time;     for (int i = 0; i < G[x].size(); i++)       if (G[x][i] != par) dfs(G[x][i], x);     en[x] = Time;   }   void dfs2(int x, int par) {     if (intervals[x].size()) intervals[x].push_back(x);     for (int i = 0; i < intervals[x].size(); i++)       update(1, 1, n, st[intervals[x][i]], en[intervals[x][i]], 1);     ans[x] = max(0, tree[1].nonzero - 1);     for (int i = 0; i < G[x].size(); i++)       if (G[x][i] != par) dfs2(G[x][i], x);     for (int i = 0; i < intervals[x].size(); i++)       update(1, 1, n, st[intervals[x][i]], en[intervals[x][i]], -1);   }   int main() {     scanf( %d , &n);     ;     scanf( %d , &m);     ;     memset(tree, 0, sizeof tree);     for (int i = 0; i < n - 1; i++) {       int a, b;       scanf( %d , &a);       ;       scanf( %d , &b);       ;       G[a].push_back(b);       G[b].push_back(a);     }     dfs(1, -1);     while (m--) {       int a, b;       scanf( %d , &a);       ;       scanf( %d , &b);       ;       intervals[a].push_back(b);       intervals[b].push_back(a);     }     dfs2(1, -1);     for (int i = 1; i < n + 1; i++) printf( %d  , ans[i]);     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int pri[100003], top, n;   bool ntp[100002];   struct node {     int a, b;     bool operator<(const node &rhs) const { return b > rhs.b; }   };   void euler() {     for (int i = 2; i <= 100000; ++i) {       if (!ntp[i]) pri[top++] = i;       for (int j = 0; j < top && pri[j] * i <= 100000; ++j) {         ntp[pri[j] * i] = 1;         if (i % pri[j] == 0) break;       }     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     euler();     int t;     cin >> t;     while (t--) {       cin >> n;       int bn = n;       vector<node> v;       int sz = 1;       for (int i = 0; i < top && n != 1; ++i) {         if (n % pri[i] == 0) {           node x;           x.a = pri[i];           x.b = 0;           while (n % pri[i] == 0) ++x.b, n /= pri[i];           sz *= (x.b + 1);           v.push_back(x);         }       }       if (n != 1) {         node x;         x.a = n;         x.b = 1;         v.push_back(x);       }       if (v.size() == 0) {         cout <<  NO n ;       } else if (v.size() == 1) {         if (v[0].b < 6) {           cout <<  NO n ;         } else {           cout <<  YES n ;           const int &t = v[0].a;           cout << t <<     << t * t <<     << bn / (t * t * t) <<   n ;         }       } else if (v.size() == 2) {         if (v[0].b + v[1].b < 4) {           cout <<  NO n ;         } else {           cout <<  YES n ;           cout << v[0].a <<     << v[1].a <<     << bn / v[0].a / v[1].a <<   n ;         }       } else {         cout <<  YES n ;         cout << v[0].a <<     << v[1].a <<     << bn / v[0].a / v[1].a <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   using min_queue = priority_queue<T, vector<T>, greater<T>>;   const long long MOD = 1e9 + 7;   vector<int> g[1010];   long long edg[1010][1010];   map<pair<int, int>, long long> res;   int leaf[1010];   int N;   void dfs(int v, int p) {     leaf[v] = v;     for (int w : g[v]) {       if (w == p) continue;       dfs(w, v);       leaf[v] = leaf[w];     }   }   bool dfs3(int v, int l2, int p, long long r) {     if (v == l2) return 1;     for (int w : g[v]) {       if (w == p) continue;       if (dfs3(w, l2, v, r)) {         edg[v][w] += r;         edg[w][v] += r;         return 1;       }     }     return 0;   }   void add(int l1, int l2, long long r) {     if (r == 0) return;     if (l1 > l2) swap(l1, l2);     res[{l1, l2}] += r;     dfs3(l1, l2, 0, -r);   }   void dfs2(int v, int p, long long r, int l1, int l2) {     add(leaf[v], l1, r / 2);     add(leaf[v], l2, r / 2);     add(l1, l2, -r / 2);     vector<int> ww;     for (int w : g[v]) {       if (w != p) ww.push_back(w);     }     for (int w : g[v]) {       long long r = edg[v][w];       if (w == p) continue;       if (ww.size() > 1) {         if (ww[0] == w)           l2 = leaf[ww[1]];         else           l2 = leaf[ww[0]];       }       dfs2(w, v, r, l1, l2);     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> N;     for (int i = 0; i < N - 1; i++) {       int a, b;       cin >> a >> b;       long long val;       cin >> val;       g[a].push_back(b);       g[b].push_back(a);       edg[a][b] = edg[b][a] = val;     }     bool good = 1;     int rt = 0;     for (int i = 1; i <= N; i++) {       if (g[i].size() != 2) {         if (rt == 0 || g[i].size() >= 3) rt = i;         continue;       }       if (edg[i][g[i][0]] != edg[i][g[i][1]]) {         good = 0;         break;       }     }     if (!good) {       cout <<  NO n ;       return 0;     }     cout <<  YES n ;     dfs(rt, 0);     if (g[rt].size() == 1) {       long long v = edg[rt][g[rt][0]];       cout << 1 << endl;       cout << rt <<     << leaf[rt] <<     << v << endl;       return 0;     }     set<int> ch;     for (int w : g[rt]) {       if (ch.size() >= 3) break;       ch.insert(w);     }     for (int w : g[rt]) {       set<int> ch1 = ch;       ch1.erase(w);       int l1 = leaf[*ch1.begin()];       int l2 = leaf[*ch1.rbegin()];       dfs2(w, rt, edg[rt][w], l1, l2);     }     int cnt = 0;     for (auto pr : res)       if (pr.second != 0 && pr.first.first != pr.first.second) cnt++;     cout << cnt <<   n ;     for (auto pr : res) {       if (pr.second != 0 && pr.first.first != pr.first.second)         cout << pr.first.first <<     << pr.first.second <<     << pr.second              << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0, f = 1;     char c = getchar();     for (; !isdigit(c); c = getchar())       if (c ==  - ) f = -1;     for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ;     return x * f;   }   const int MAXN = 150010;   const int INF = 2147483600;   int N;   long long A, B, C, D;   inline long long Query(long long t) {     if (t * D > C || t < 0) return INF;     return (t * (t + 1) / 2 * D) * B - (t + 1) * A;   }   inline long long Qt(long long l, long long r) {     long long fans = INF, pos = 0;     r /= D;     while (l <= r) {       long long mid = (l + r) >> 1;       if (Query(mid + 1) - Query(mid) < 0)         l = mid + 1;       else         pos = mid, r = mid - 1;     }     return min(Query(pos - 1), min(Query(pos), Query(pos + 1)));   }   int main() {     int T = read();     while (T--) {       A = read(), B = read(), C = read(), D = read();       if (A > C * B) {         puts( -1 );         continue;       }       if (D >= C) {         printf( %lld n , A);       } else {         long long l = 0, r = C - 1;         long long ans = Qt(0, C);         printf( %lld n , max(-ans, 0LL));       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-8;   const int N = 505;   int sign(double x) { return (x > eps) - (x < -eps); }   int n;   struct P {     double x, y;     P(double a = 0., double b = 0.) : x(a), y(b) {}     P operator+(const P& a) const { return P(x + a.x, y + a.y); }     P operator-(const P& a) const { return P(x - a.x, y - a.y); }     double operator*(const P& a) const { return x * a.x + y * a.y; }     double operator^(const P& a) const { return x * a.y - y * a.x; }     double cross(P a, P b) { return (a - *this) ^ (b - *this); }     double radio(P p, P b) {       if (sign(b.x - x)) return (p.x - x) / (b.x - x);       return (p.y - y) / (b.y - y);     }     void in() { scanf( %lf%lf , &x, &y); }   };   struct PLOY {     int pnt;     P ch[6];     bool anti() {       int dr = sign(ch[0].cross(ch[1], ch[2]));       if (dr == 0) return false;       if (dr < 0) reverse(ch, ch + pnt);       ch[pnt] = ch[0];       return true;     }     double S() {       double s = 0.;       for (int i = 0; i < pnt; i++) s += ch[i] ^ ch[i + 1];       return fabs(s) * 0.5;     }   } pl[505];   pair<double, int> pr[2005];   double cover[505];   double get() {     for (int i = 0; i < N; i++) cover[i] = 0.;     double s = 0.;     int cnt = 0;     for (int i = 0; i < n; i++) {       for (int x = 0; x < pl[i].pnt; x++) {         cnt = 0;         pr[cnt++] = make_pair(0., 0);         pr[cnt++] = make_pair(1., 0);         for (int j = 0; j < n; j++) {           for (int y = 0; y < pl[j].pnt; y++) {             int dr1 = sign(pl[i].ch[x].cross(pl[i].ch[x + 1], pl[j].ch[y]));             int dr2 = sign(pl[i].ch[x].cross(pl[i].ch[x + 1], pl[j].ch[y + 1]));             if (dr1 == 0 && dr2 == 0) {               if (i > j && (pl[i].ch[x + 1] - pl[i].ch[x]) *                                    (pl[j].ch[y + 1] - pl[j].ch[y]) >                                0.) {                 pr[cnt++] =                     make_pair(pl[i].ch[x].radio(pl[j].ch[y], pl[i].ch[x + 1]), 1);                 pr[cnt++] = make_pair(                     pl[i].ch[x].radio(pl[j].ch[y + 1], pl[i].ch[x + 1]), -1);               }             } else if (dr1 >= 0 && dr2 < 0) {               double s1 = pl[j].ch[y + 1].cross(pl[j].ch[y], pl[i].ch[x]);               double s2 = pl[j].ch[y + 1].cross(pl[j].ch[y], pl[i].ch[x + 1]);               pr[cnt++] = make_pair(s1 / (s1 - s2), 1);             } else if (dr2 >= 0 && dr1 < 0) {               double s1 = pl[j].ch[y].cross(pl[j].ch[y + 1], pl[i].ch[x]);               double s2 = pl[j].ch[y].cross(pl[j].ch[y + 1], pl[i].ch[x + 1]);               pr[cnt++] = make_pair(s1 / (s1 - s2), -1);             }           }         }         sort(pr, pr + cnt);         double r = 0., cur, last;         int sum;         sum = pr[0].second;         last = min(max(pr[0].first, 0.), 1.);         for (int k = 1; k < cnt; k++) {           cur = min(max(pr[k].first, 0.), 1.);           cover[sum] += (cur - last) * (pl[i].ch[x] ^ pl[i].ch[x + 1]);           sum += pr[k].second, last = cur;         }       }     }     return cover[0] * 0.5;   }   int main() {     while (~scanf( %d , &n)) {       double ans = 0.;       for (int i = 0; i < n; i++) {         for (int j = 0; j < 4; j++) pl[i].ch[j].in();         pl[i].pnt = 4;         if (!pl[i].anti()) continue;         ans += pl[i].S();       }       printf( %.10f n , ans / get());     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int arr[10][10];   bool CheckArr(const string& s) {     const int n = s.length();     for (int a = 0; a < (10); ++a) {       if (arr[a][0] == 0) continue;       char c = s[arr[a][0] - 1];       for (int i = 0; i < (10); ++i) {         if (arr[a][i] == 0) break;         int idx = arr[a][i] - 1;         if (c != s[idx]) return false;       }     }     for (int i = 0; i < 10; ++i) {       if (arr[i][0] == 0) continue;       for (int j = i + 1; j < 10; ++j) {         if (arr[j][0] == 0) continue;         int idx1 = arr[i][0] - 1;         int idx2 = arr[j][0] - 1;         if (s[idx1] == s[idx2]) return false;       }     }     return true;   }   bool Check(const string& s1, const string& s2) {     const int n = s1.length();     if (n != s2.length()) return false;     for (int i = 0; i < (n); ++i) {       if (s1[i] ==  ? ) continue;       if (s1[i] >=  0  && s1[i] <=  9  && s1[i] == s2[i]) continue;       if (s1[i] >=  A  && s1[i] <=  J ) continue;       return false;     }     return true;   }   int main() {     string s;     cin >> s;     const int n = s.length();     if (n > 5) {       cout <<  :(  << endl;       return 0;     }     for (int i = 0; i < (n); ++i)       if (s[i] >=  A  && s[i] <=  J ) {         int alphaIdx = s[i] -  A ;         int posIdx = 0;         while (arr[alphaIdx][posIdx] != 0) ++posIdx;         arr[alphaIdx][posIdx] = i + 1;       }     int ans = 0;     for (int i = 1; i <= 99999; ++i) {       char str[6];       sprintf(str,  %d , i);       string s2(str);       if (Check(s, s2) && CheckArr(s2)) ++ans;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int x, y, i = 1, s, p, q;     cin >> x >> y;     s = x % 10;     q = s;     p = s;     while (p % 10 != 0 || p != y) {       if (p % 10 == 0 || p == y) {         break;       }       i++;       q = q + s;       p = q % 10;     }     cout << i;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   const long long SIZE = 100000;   const int INF = 0x3f3f3f3f;   const long long ll_INF = 0x3f3f3f3f3f3f3f3f;   const long double PI = acos(-1);   const long long MAXN = numeric_limits<long long>::max();   const long long MAX = 2000000;   void disp(vector<long long> v) {     for (auto u : v) cout << u <<    ;     cout <<   n ;   }   void solve() {     long long l, r, x = 0, y = 1, temp, ans = 0;     cin >> l >> r;     y = 1;     while (y <= r) {       temp = y;       if (temp >= l && temp <= r) ans++;       while (temp <= r) {         temp *= 2;         if (temp >= l && temp <= r) ans++;       }       y *= 3;     }     cout << ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     solve();     return 0;   }