#include <bits/stdc++.h> n using namespace std; n const int N = 5005; n const int M = 1005; n const int K = 15; n int n, m; n char name[N][K], oper[N][K], val[N][M]; n bool isV[N]; n int idx1[N], idx2[N]; n map<string, int> mp; n char ansmx[M], ansmn[M]; n int res[N]; n int work(int a, int b, char op[]) { n   if (op[0] ==  A ) return a & b; n   if (op[0] ==  O ) return a | b; n   return a ^ b; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) { n     scanf( %s , name[i]); n     mp[name[i]] = i; n     char buf[M]; n     scanf( %s , buf); n     scanf( %s , buf); n     if (buf[0] >=  0  && buf[0] <=  9 ) { n       memcpy(val[i], buf, sizeof(buf)); n       isV[i] = 0; n     } else { n       isV[i] = 1; n       idx1[i] = buf[0] ==  ?  ? -1 : mp[buf]; n       scanf( %s , oper[i]); n       scanf( %s , buf); n       idx2[i] = buf[0] ==  ?  ? -1 : mp[buf]; n     } n   } n   for (int i = 0; i < m; i++) { n     int ans[2] = {0, 0}; n     for (int j = 0; j < 2; j++) { n       for (int k = 0; k < n; k++) { n         if (!isV[k]) n           res[k] = (val[k][i] -  0 ); n         else { n           int op1 = idx1[k] == -1 ? j : res[idx1[k]]; n           int op2 = idx2[k] == -1 ? j : res[idx2[k]]; n           res[k] = work(op1, op2, oper[k]); n         } n         ans[j] += res[k]; n       } n     } n     if (ans[0] == ans[1]) n       ansmx[i] = ansmn[i] =  0 ; n     else { n       ansmx[i] = ans[0] > ans[1] ?  0  :  1 ; n       ansmn[i] = ans[0] < ans[1] ?  0  :  1 ; n     } n   } n   printf( %s n%s n , ansmn, ansmx); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int64_t INF = 1e18 + 10; n const int N = 2e7 + 10; n int MOD = 1000000007; n int g[26], in[26], used[26]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   for (int i = 0; i < 26; i++) g[i] = -1; n   int n; n   cin >> n; n   vector<string> a(n,   ); n   bool arr = 1; n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     for (auto j : s) used[j -  a ] = 1; n     for (int j = 1; j < s.size(); j++) { n       int u = s[j - 1] -  a ; n       if (g[u] != -1 && g[u] != s[j] -  a  || s[j] == s[j - 1]) arr = 0; n       g[s[j - 1] -  a ] = s[j] -  a ; n       in[s[j] -  a ]++; n     } n   } n   if (!arr) { n     cout <<  NO ; n     return 0; n   } n   string ans =   ; n   for (int i = 0; i < 26; i++) { n     if (in[i] == 0 && used[i]) { n       int x = i; n       while (x != -1) { n         used[x]--; n         if (used[x] < 0) break; n         ans +=  a  + x; n         x = g[x]; n       } n     } n   } n   for (int i = 0; i < 26; i++) { n     if (used[i] != 0) arr = 0; n   } n   if (arr) n     cout << ans; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n using vl = vector<long long>; n using vpl = vector<pair<long long, long long>>; n using vs = vector<string>; n using pl = pair<long long, long long>; n using ll = long long int; n using vb = vector<bool>; n void printcase(ll x) { cout <<  Case #  << x <<  :  ; } n void read(vector<ll> &v, ll &n) { n   for (ll i = 0; i < n; ++i) cin >> v[i]; n } n void read(ll a[], ll &n) { n   for (ll i = 0; i < n; ++i) cin >> a[i]; n } n void print() { cout << endl; } n void write(vector<ll> &v, ll &n) { n   for (ll i = 0; i < n; ++i) cout << v[i] <<    ; n   cout << endl; n } n void write(vector<ll> &v) { n   ll n = v.size(); n   for (ll i = 0; i < n; ++i) cout << v[i] <<    ; n   cout << endl; n } n void write(ll a[], ll &n) { n   for (ll i = 0; i < n; ++i) cout << a[i] <<    ; n   cout << endl; n } n bool sorted(vl &v) { n   ll n = v.size(); n   for (int i = 0; i < n - 1; i++) { n     if (v[i] > v[i + 1]) return 0; n   } n   return 1; n } n bool solve() { n   ll n; n   cin >> n; n   vl v(n); n   read(v, n); n   if (n <= 2) return 1; n   vl suff(n), suffind(n), preind(n); n   suff[n - 1] = v[n - 1]; n   suffind[n - 1] = n - 1; n   preind[0] = 0; n   for (int i = 1; i < n; i++) { n     if (v[i] == v[i - 1]) n       preind[i] = preind[i - 1]; n     else n       preind[i] = i; n   } n   for (int i = n - 2; i >= 0; i--) { n     if (v[i] < suff[i + 1]) n       suffind[i] = i; n     else n       suffind[i] = suffind[i + 1]; n     suff[i] = min(v[i], suff[i + 1]); n   } n   for (int i = 0; i < n - 1; i++) { n     if (v[i] > v[i + 1]) { n       swap(v[preind[i]], v[suffind[i + 1]]); n       if (sorted(v)) n         return 1; n       else n         return 0; n     } n   } n   return 1; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   if (solve()) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct plan { n   bool used; n   int type; n   int single; n   int start; n   int end; n   long long cost; n }; n void getInput(vector<plan> &plans, vector<vector<int> > &outs, n               vector<forward_list<int> > &segtree); n void addPlan(vector<forward_list<int> > &segtree, int st_ind, int cs, int ce, n              int ds, int de, int pi); n void getMinCosts(vector<long long> &min_costs, int source, n                  vector<vector<int> > &outs, n                  vector<forward_list<int> > &segtree, vector<plan> &plans); n void processNode(int n, vector<bool> &visited, set<int> &unvisited, n                  priority_queue<pair<long long, int> > &to_visit, n                  vector<vector<int> > &outs, n                  vector<forward_list<int> > &segtree, vector<plan> &plans, n                  long long cur_cost, vector<long long> &min_costs); n int main() { n   int num_planets, num_plans, source; n   cin >> num_planets >> num_plans >> source; n   source--; n   vector<long long> min_costs(num_planets, LLONG_MAX); n   min_costs[source] = 0; n   vector<vector<int> > outs(num_planets); n   vector<forward_list<int> > segtree(2 * n                                      pow(2, ceil(log(num_planets) / log(2)))); n   vector<plan> plans(num_plans); n   getInput(plans, outs, segtree); n   getMinCosts(min_costs, source, outs, segtree, plans); n   for (int i = 0; i < min_costs.size(); i++) n     cout << ((min_costs[i] < LLONG_MAX) ? min_costs[i] : -1) <<    ; n   cout <<   n ; n   return 0; n } n void getInput(vector<plan> &plans, vector<vector<int> > &outs, n               vector<forward_list<int> > &segtree) { n   for (int pi = 0; pi < plans.size(); pi++) { n     plans[pi].used = false; n     cin >> plans[pi].type >> plans[pi].single >> plans[pi].start; n     plans[pi].type -= 2; n     if (plans[pi].type >= 0) n       cin >> plans[pi].end; n     else { n       plans[pi].end = plans[pi].start; n       plans[pi].type = 0; n     } n     plans[pi].single--; n     plans[pi].start--; n     plans[pi].end--; n     cin >> plans[pi].cost; n     if (plans[pi].type == 0) n       outs[plans[pi].single].push_back(pi); n     else n       addPlan(segtree, 1, 0, segtree.size() / 2 - 1, plans[pi].start, n               plans[pi].end, pi); n   } n } n void addPlan(vector<forward_list<int> > &segtree, int st_ind, int cs, int ce, n              int ds, int de, int pi) { n   if (cs > de || ce < ds) n     return; n   else if (cs >= ds && ce <= de) n     segtree[st_ind].push_front(pi); n   else { n     int cm = (cs + ce) / 2; n     addPlan(segtree, 2 * st_ind, cs, cm, ds, de, pi); n     addPlan(segtree, 2 * st_ind + 1, cm + 1, ce, ds, de, pi); n   } n } n void getMinCosts(vector<long long> &min_costs, int source, n                  vector<vector<int> > &outs, n                  vector<forward_list<int> > &segtree, vector<plan> &plans) { n   vector<bool> visited(min_costs.size(), false); n   set<int> unvisited; n   for (int i = 0; i < min_costs.size(); i++) unvisited.insert(i); n   priority_queue<pair<long long, int> > to_visit; n   processNode(source, visited, unvisited, to_visit, outs, segtree, plans, 0, n               min_costs); n   while (to_visit.size() > 0) { n     int cur_pi = to_visit.top().second; n     long long cur_cost = -to_visit.top().first; n     to_visit.pop(); n     if (plans[cur_pi].type == 0) { n       set<int>::iterator s_it = unvisited.lower_bound(plans[cur_pi].start); n       set<int>::iterator e_it = unvisited.upper_bound(plans[cur_pi].end); n       for (set<int>::iterator it = s_it; it != e_it;) { n         int n = *it; n         it++; n         processNode(n, visited, unvisited, to_visit, outs, segtree, plans, n                     cur_cost, min_costs); n       } n     } else { n       if (!visited[plans[cur_pi].single]) n         processNode(plans[cur_pi].single, visited, unvisited, to_visit, outs, n                     segtree, plans, cur_cost, min_costs); n     } n   } n } n void processNode(int n, vector<bool> &visited, set<int> &unvisited, n                  priority_queue<pair<long long, int> > &to_visit, n                  vector<vector<int> > &outs, n                  vector<forward_list<int> > &segtree, vector<plan> &plans, n                  long long cur_cost, vector<long long> &min_costs) { n   if (visited[n]) return; n   min_costs[n] = cur_cost; n   visited[n] = true; n   unvisited.erase(n); n   for (auto &out : outs[n]) n     to_visit.push(make_pair(-(cur_cost + plans[out].cost), out)); n   for (int st_ind = segtree.size() / 2 + n; st_ind > 0; st_ind /= 2) { n     for (auto &pi : segtree[st_ind]) n       if (!plans[pi].used) { n         plans[pi].used = true; n         to_visit.push(make_pair(-(cur_cost + plans[pi].cost), pi)); n       } n     segtree[st_ind].clear(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int M = INT32_MAX; n const int NMAX = 10007; n int main() { n   cin.tie(0); n   cin.sync_with_stdio(0); n   cout.precision(30); n   string z[2]; n   cin >> z[0] >> z[1]; n   int n; n   cin >> n; n   cout << z[1] <<     << z[0] << endl; n   for (int i = 0; i < n; i++) { n     string k, next; n     cin >> k >> next; n     if (k == z[1]) n       z[1] = next; n     else n       z[0] = next; n     cout << z[1] <<     << z[0] << endl; n   } n   return 0; n }; n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> arr[200000]; n int ans[200000], tree[500000], lazy[500000]; n set<int> s; n int n; n set<int>::iterator it; n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> arr[i].first; n     arr[i].second = i; n   } n   sort(arr, arr + n); n   int l, r, mn = arr[0].second, len; n   for (int i = 0; i < n; i++) { n     ans[i] = arr[0].first; n   } n   s.insert(arr[0].second); n   for (int i = 1; i < n; i++) { n     it = s.lower_bound(arr[i].second); n     r = *it; n     if (it == s.end()) { n       r = n; n     } n     if (*it == mn) { n       l = -1; n     } else { n       it--; n       l = *it; n     } n     mn = min(mn, arr[i].second); n     s.insert(arr[i].second); n     len = r - l - 1; n     ans[len - 1] = max(arr[i].first, ans[len - 1]); n   } n   for (int i = n - 2; i >= 0; i--) { n     ans[i] = max(ans[i], ans[i + 1]); n   } n   for (int i = 0; i < n; i++) { n     cout << ans[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9 + 7; n int arr[200005]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   if (n % 2 == 0) { n     cout <<  NO n ; n     return 0; n   } n   int flag = 1, ct = 1; n   for (int i = 1; i <= n; i++) { n     if (flag) { n       arr[i] = ct; n       arr[n + i] = ct + 1; n       flag = 0; n     } else { n       arr[i] = ct + 1; n       arr[n + i] = ct; n       flag = 1; n     } n     ct += 2; n   } n   cout <<  YES n ; n   for (int i = 1; i <= 2 * n; i++) { n     cout << arr[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, p, i, j, k, result; n   cin >> n >> p; n   int a[n + 1], b[n + 1]; n   for (i = 1; i <= n; i = i + 1) cin >> a[i]; n   i = 0; n   j = p - 1; n   result = p - 1; n   do { n     int m = (i + j) / 2; n     bool check = true; n     b[0] = 0; n     for (int k = 1; k <= n; k = k + 1) b[k] = 1000000000; n     for (int k = 1; k <= n; k = k + 1) { n       if (((a[k] + m) % p >= b[k - 1]) && (a[k] + m >= p)) b[k] = b[k - 1]; n       if ((b[k - 1] - a[k] >= 0) && (b[k - 1] - a[k] <= m)) n         b[k] = min(b[k], b[k - 1]); n       if (a[k] >= b[k - 1]) b[k] = min(b[k], a[k]); n       if (b[k] == 1000000000) { n         check = false; n         break; n       }; n     } n     if (check == true) { n       j = m - 1; n       result = min(result, m); n     } else n       i = m + 1; n   } while (i <= j); n   cout << result; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   vector<int> a; n   scanf( %d%d , &n, &m); n   int min = n + 2; n   for (int i = 0; i < m; i++) { n     int t; n     scanf( %d , &t); n     if (t < min) { n       a.push_back(t); n       min = t; n     } n     if (t == 1) break; n   } n   sort(a.begin(), a.end()); n   for (int i = 0; i < a.size() - 1; i++) n     for (int j = 0; j < a[i + 1] - a[i]; j++) printf( %d  , a[i]); n   for (int i = 0; i < n - a.back(); i++) printf( %d  , a.back()); n   printf( %d n , a.back()); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int t = 1; n   while (t--) { n     map<char, long long int> a, b; n     long long int n; n     cin >> n; n     string s, t; n     cin >> s >> t; n     for (long long int i = 0; i < n; i++) a[s[i]]++, b[t[i]]++; n     vector<long long int> ans; n     if (a != b) n       cout << -1; n     else { n       if (s == t) n         cout << 0; n       else { n         std::vector<int> move; n         for (int i = n - 1; i >= 0; --i) { n           if (s[i] != t[i]) { n             int j = i - 1; n             for (j = i - 1; j >= 0; --j) { n               if (s[j] == t[i]) break; n             } n             while (j < i) { n               move.push_back(j + 1); n               swap(s[j], s[j + 1]); n               ++j; n             } n           } n         } n         cout << move.size() <<   n ; n         for (int x : move) cout << x <<    ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int f; n vector<string> cur; n void upd(string s, int x, int y, int o) { n   int dx = 0, dy = 1; n   if (o) swap(dx, dy); n   for (int i = 0; i < s.size(); i++) { n     int xx = x + dx * i; n     int yy = y + dy * i; n     if (cur[xx][yy] ==  .  || cur[xx][yy] == s[i]) { n       cur[xx][yy] = s[i]; n     } else { n       f = 0; n     } n   } n } n int main() { n   vector<string> a(6); n   for (auto &x : a) { n     cin >> x; n   } n   sort((a).begin(), (a).end()); n   vector<string> ans = { Z }; n   ans[0][0]++; n   vector<string> ans0 = ans; n   do { n     f = 1; n     vector<int> b; n     vector<int> c; n     for (int t = 0; t < 6; t += 3) { n       int mx = 0; n       int s = 0; n       for (int i = t; i < t + 3; i++) { n         s += a[i].size(); n         mx = max(mx, (int)a[i].size()); n       } n       for (int i = t; i < t + 3; i++) { n         if (a[i].size() == mx) { n           b.push_back(i); n           break; n         } n       } n       if (s - 2 * mx != 1) { n         f = 0; n       } n     } n     for (int i = 0; i < 6; i++) { n       if (i != b[0] && i != b[1]) { n         c.push_back(i); n       } n     } n     if (f) { n       vector<int> bb, cc; n       for (auto x : b) bb.push_back(a[x].size()); n       for (auto x : c) cc.push_back(a[x].size()); n       string t; n       for (int i = 0; i < bb[1]; i++) { n         t.push_back( . ); n       } n       cur = vector<string>(bb[0], t); n       upd(a[c[0]], 0, 0, 1); n       upd(a[c[2]], 0, 0, 0); n       upd(a[b[0]], 0, cc[2] - 1, 1); n       upd(a[b[1]], cc[0] - 1, 0, 0); n       upd(a[c[1]], cc[0] - 1, bb[1] - 1, 1); n       upd(a[c[3]], bb[0] - 1, cc[2] - 1, 0); n     } n     if (f) { n       if (cur < ans) ans = cur; n     } n   } while (next_permutation((a).begin(), (a).end())); n   if (ans == ans0) { n     cout <<  Impossible  << endl; n     return 0; n   } n   for (auto s : ans) { n     cout << s << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod(long long a, long long b) { n   if (a % b < 0) { n     return a % b + b; n   } n   return a % b; n } n long long mod_exp(long long a, long long b, long long c) { n   long long res = 1; n   a = a % c; n   while (b > 0) { n     if (b % 2 == 1) { n       res = (res * a) % c; n     } n     b /= 2; n     a = (a * a) % c; n   } n   return res; n } n long long gcd(long long a, long long b) { n   if (b == 0) { n     return a; n   } n   return gcd(b, a % b); n } n bool prime[3001]; n void SieveOfEratosthenes() { n   memset(prime, true, sizeof(prime)); n   prime[1] = false; n   for (int p = 2; p * p <= 3000; p++) { n     if (prime[p] == true) { n       for (int i = p * p; i <= 3000; i += p) prime[i] = false; n     } n   } n } n long long powe[20]; n void power() { n   powe[0] = 1; n   for (long long i = 1; i < 20; i++) { n     powe[i] = (powe[i - 1] * 10) % 1000000007; n     if (powe[i] < 0) powe[i] += 1000000007; n   } n } n long long func(long long A[], long long n) { n   long long lis[n]; n   lis[0] = 1; n   for (long long i = 0; i < n; i++) { n     lis[i] = 1; n   } n   return lis[0]; n } n void solve() { n   long long a, b, c; n   cin >> a >> b >> c; n   if ((c >= b && b >= a) || (a >= b && b >= c)) { n     if (a >= b && b >= c) { n       swap(a, c); n     } n     while (b > (a + c) / 2) { n       b--; n       a++; n     } n     while (b < (a + c) / 2) { n       b++; n       c--; n     } n     cout << ((((a > b) ? a : b) > c) ? ((a > b) ? a : b) : c) <<   n ; n     return; n   } n } n int main() { n   long long n, a, b, c; n   cin >> n; n   cout <<   n? 1 2 n ; n   fflush(stdout); n   cin >> a; n   cout <<  ? 2 3 n ; n   fflush(stdout); n   cin >> b; n   cout <<  ? 1 3 n ; n   fflush(stdout); n   cin >> c; n   long long A[n]; n   A[0] = (a + c - b) / 2; n   A[1] = a - A[0]; n   A[2] = c - A[0]; n   for (long long i = 3; i < n; i++) { n     cout <<  ? 1   << i + 1 <<   n ; n     fflush(stdout); n     cin >> a; n     A[i] = a - A[0]; n   } n   cout <<  !  ; n   for (long long i = 0; i < n; i++) { n     cout << A[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 5; n struct ACA { n   int pool[N], ptp; n   int stk[30], sz[N], rt[N], rtid, tp; n   int nxt[N][26], tran[N][26], fail[N], end[N], num[N], tot; n   inline int newnode() { return ++tot; } n   int merge(int x, int y) { n     if (!x || !y) return x | y; n     for (int i = 0; i < 26; i++) nxt[x][i] = merge(nxt[x][i], nxt[y][i]); n     end[x] += end[y]; n     pool[++ptp] = y; n     return x; n   } n   void build(int p) { n     queue<int> q; n     for (int i = 0; i < 26; i++) n       if (nxt[p][i]) n         fail[tran[p][i] = nxt[p][i]] = p, q.push(nxt[p][i]); n       else n         tran[p][i] = p; n     while (!q.empty()) { n       int k = q.front(); n       q.pop(); n       num[k] = end[k] + num[fail[k]]; n       for (int i = 0; i < 26; i++) { n         if (nxt[k][i]) { n           fail[tran[k][i] = nxt[k][i]] = tran[fail[k]][i]; n           q.push(tran[k][i]); n         } else n           tran[k][i] = tran[fail[k]][i]; n       } n     } n   } n   void ins(char *s, int n, int v) { n     rt[++rtid] = newnode(); n     sz[rtid] = 1; n     int p = rt[rtid]; n     for (int i = 0; i < n; i++) { n       int k = s[i] -  a ; n       if (!nxt[p][k]) nxt[p][k] = newnode(); n       p = nxt[p][k]; n     } n     end[p] += v; n     while (tp && sz[stk[tp]] < 2 * sz[rtid]) n       rt[rtid] = merge(rt[rtid], rt[stk[tp]]), sz[rtid] += sz[stk[tp]], --tp; n     build(rt[rtid]); n     stk[++tp] = rtid; n   } n   int qry(char *s, int n) { n     int ans = 0; n     for (int i = 1; i <= tp; i++) { n       int p = rt[stk[i]]; n       for (int i = 0; i < n; i++) { n         p = tran[p][s[i] -  a ]; n         ans += num[p]; n       } n     } n     return ans; n   } n } ac; n int main() { n   int m; n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) { n     static char s[N]; n     int op; n     scanf( %d%s , &op, s); n     if (op == 1) n       ac.ins(s, strlen(s), 1); n     else if (op == 2) n       ac.ins(s, strlen(s), -1); n     else n       printf( %d n , ac.qry(s, strlen(s))), fflush(stdout); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 1, logN = 21 + 1, mod = 1e9 + 7; n int fl(int n) { n   int ans = 0; n   while (n /= 2) ++ans; n   return ans; n } n int ord2(int n) { n   int ans = 0; n   while (n % 2 == 0) { n     n >>= 1; n     ++ans; n   } n   return ans; n } n long long binpow(long long a, long long s) { n   long long ans = 1; n   while (s) { n     if (s & 1) ans = ans * a % mod; n     a = a * a % mod; n     s >>= 1; n   } n   return ans; n } n long long inv(long long a) { return binpow(a, mod - 2); } n long long f[N], finv[N]; n void init() { n   f[0] = 1; n   finv[0] = 1; n   for (long long i = 1; i < N; ++i) { n     f[i] = f[i - 1] * i % mod; n     finv[i] = inv(f[i]); n   } n } n int n, logn; n int cnt[logN]; n long long solve(int k) { n   for (int i = 0; i < logn; ++i) cnt[i] = 0; n   for (int i = 1; i <= n; ++i) { n     int ord = ord2(i); n     if (ord < k) n       ++cnt[ord]; n     else if (ord >= k) { n       if (i % 3) n         ++cnt[k]; n       else n         ++cnt[ord + 1]; n     } n   } n   long long ans = 1; n   for (int i = 0; i < logn; ++i) ans = ans * cnt[i] % mod; n   ans = ans * f[cnt[0] - 1] % mod; n   int sum = cnt[0] - 1; n   for (int i = 1; i < logn; ++i) { n     ans = ans * (f[sum + cnt[i]] * finv[sum + 1] % mod) % mod; n     sum += cnt[i]; n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cout.tie(0); n   cin.tie(0); n   init(); n   cin >> n; n   logn = fl(n) + 1; n   long long ans = 0; n   for (int i = 0; i < logn; ++i) ans = (ans + solve(i)) % mod; n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, t, a[110]; n int main() { n   scanf( %d%d , &n, &t); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   for (int i = 1; i <= n; i++) { n     t = t - 86400 + a[i]; n     if (t <= 0) { n       printf( %d , i); n       break; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   int a[100005]; n   int b[100005]; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &a[i], &b[i]); n   } n   int flag = 1; n   if (n % 2 == 0) n     for (int i = 1; i < n / 2; i++) { n       if (a[i] + a[i + n / 2] != a[i + 1] + a[i + 1 + n / 2] || n           b[i] + b[i + n / 2] != b[i + 1] + b[i + 1 + n / 2]) { n         flag = 0; n         break; n       } n     } n   else n     flag = 0; n   if (flag) n     printf( YES n ); n   else n     printf( NO n ); n } n


#include <bits/stdc++.h> n using namespace std; n long long po(long long a, long long b) { n   long long x = 1, y = a; n   while (b > 0) { n     if (b % 2) { n       x = x * y; n       x %= 1000000007; n     } n     y = y * y; n     y %= 1000000007; n     b /= 2; n   } n   return x; n } n int main() { n   if (0) { n     freopen( C:/Users/gold/Desktop/sublime IO/input.txt ,  r , stdin); n     freopen( C:/Users/gold/Desktop/sublime IO/output.txt ,  w , stdout); n   } n   int T = 1; n   while (T--) { n     string s, s1; n     cin >> s >> s1; n     int n = s.length(); n     static int cnt[256]; n     for (int i = 0; i < n; i++) { n       cnt[s[i]]++; n     } n     int m = s1.length(); n     int ans = 0, ans1 = 0; n     for (int i = 0; i < m; i++) { n       if (cnt[s1[i]] > 0) { n         cnt[s1[i]]--; n         ans++; n         s1[i] =  1 ; n       } n     } n     for (int i = 0; i < m; i++) { n       if (s1[i] ==  1 ) { n         continue; n       } n       if (s1[i] >=  a  && cnt[s1[i] -  a  +  A ] > 0) { n         ans1++; n         cnt[s1[i] -  a  +  A ]--; n       } else if (cnt[s1[i] -  A  +  a ] > 0) { n         ans1++; n         cnt[s1[i] -  A  +  a ]--; n       } n     } n     printf( %d %d n , ans, ans1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000100; n int n, q, s[N / 2], f[N / 2], par[N / 2], timer; n vector<int> g[N / 2]; n void dfs(int x = 1, int pp = 0) { n   s[x] = ++timer; n   par[x] = pp; n   for (auto &it : g[x]) n     if (it != par[x]) { n       dfs(it, x); n     } n   f[x] = ++timer; n } n struct node { n   int sum; n   bool set; n   void merge(node &lt, node &rt) { sum = lt.sum + rt.sum; } n } st[N << 2]; n void push(int x, int lx, int rx) { n   if (st[x].set) { n     st[x].set = 0; n     st[x].sum = rx - lx + 1; n     if (lx == rx) return; n     st[x << 1].set = st[x << 1 | 1].set = 1; n   } n } n void bld(int x = 1, int lx = 1, int rx = n + n) { n   if (lx == rx) { n     st[x] = {0, 0}; n     return; n   } n   int mid = lx + rx >> 1; n   bld(x << 1, lx, mid); n   bld(x << 1 | 1, mid + 1, rx); n   st[x].merge(st[x << 1], st[x << 1 | 1]); n } n void modify(bool z, int &u, int &v, int x = 1, int lx = 1, int rx = n + n) { n   push(x, lx, rx); n   if (v < lx or u > rx) return; n   if (u <= lx and rx <= v) { n     if (z) { n       st[x].set = 1; n       push(x, lx, rx); n     } else { n       st[x].sum = 0; n     } n     return; n   } n   int mid = lx + rx >> 1; n   modify(z, u, v, x << 1, lx, mid); n   modify(z, u, v, x << 1 | 1, mid + 1, rx); n   st[x].merge(st[x << 1], st[x << 1 | 1]); n } n int qry(int &u, int &v, int x = 1, int lx = 1, int rx = n + n) { n   push(x, lx, rx); n   if (v < lx or u > rx) return 0; n   if (u <= lx and rx <= v) return st[x].sum; n   int mid = lx + rx >> 1; n   return qry(u, v, x << 1, lx, mid) + qry(u, v, x << 1 | 1, mid + 1, rx); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int u, v, i = 1; i < n; i++) { n     cin >> u >> v; n     g[u].emplace_back(v); n     g[v].emplace_back(u); n   } n   timer = 0; n   dfs(); n   bld(); n   cin >> q; n   int ty, v; n   while (q--) { n     cin >> ty >> v; n     if (ty == 1) { n       if (par[v] != 0 and qry(s[v], f[v]) != f[v] - s[v] + 1) { n         modify(0, s[par[v]], s[par[v]]); n       } n       modify(1, s[v], f[v]); n     } else if (ty == 2) { n       modify(0, s[v], s[v]); n     } else { n       cout << (qry(s[v], f[v]) == f[v] - s[v] + 1) <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n int n, k, root, col[N], sz[N]; n vector<int> g[N], a[N]; n int dfs(int v, int pr = -1) { n   int mx = col[v]; n   sz[v] = col[v]; n   for (auto& to : g[v]) { n     if (to == pr) continue; n     int root = dfs(to, v); n     if (root != -1) return root; n     sz[v] += sz[to]; n     mx = max(mx, sz[to]); n   } n   mx = max(mx, 2 * k - sz[v]); n   if (mx > k) return -1; n   return v; n } n void go(int v, int pr = -1) { n   priority_queue<pair<int, int>> q; n   if (col[v]) { n     a[v].push_back(v); n     q.push({1, v}); n   } n   for (auto& to : g[v]) { n     if (to == pr) continue; n     go(to, v); n     if (pr != -1) { n       if (a[v].size() < a[to].size()) { n         a[v].swap(a[to]); n       } n       a[v].insert(a[v].end(), a[to].begin(), a[to].end()); n       a[to].clear(); n     } else { n       if (!a[to].empty()) { n         q.push({(int)a[to].size(), to}); n       } n     } n   } n   if (pr == -1) { n     while (!q.empty()) { n       auto x = q.top(); n       q.pop(); n       auto y = q.top(); n       q.pop(); n       cout << a[x.second].back() + 1 <<     << a[y.second].back() + 1 <<     n            << root + 1 <<   n ; n       --x.first; n       --y.first; n       a[x.second].pop_back(); n       a[y.second].pop_back(); n       if (x.first > 0) q.push(x); n       if (y.first > 0) q.push(y); n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> k; n   for (int(i) = 0; (i) < (n - 1); ++(i)) { n     int x, y; n     cin >> x >> y; n     --x; n     --y; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   for (int(i) = 0; (i) < (2 * k); ++(i)) { n     int x; n     cin >> x; n     --x; n     col[x] = 1; n   } n   root = dfs(0); n   cout << 1 <<   n  << root + 1 <<   n ; n   go(root); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:100000000 ) n int matrix[900][900]; n int tmp[900][900]; n int dx[4] = {1, 0, -1, 0}; n int dy[4] = {0, 1, 0, -1}; n vector<pair<int, int> > all; n void go(int x, int y) { n   queue<pair<int, int> > q; n   tmp[x][y] = 1; n   q.push(make_pair(x, y)); n   all.push_back(make_pair(x, y)); n   while (!q.empty()) { n     x = q.front().first; n     y = q.front().second; n     q.pop(); n     for (int i = 0; i < 4; i++) { n       if (x + dx[i] >= 0 && x + dx[i] <= 799 && y + dy[i] >= 0 && n           y + dy[i] <= 799) { n         if (matrix[x + dx[i]][y + dy[i]] != 255 && n             tmp[x + dx[i]][y + dy[i]] == 0) { n           tmp[x + dx[i]][y + dy[i]] = 1; n           q.push(make_pair(x + dx[i], y + dy[i])); n           all.push_back(make_pair(x + dx[i], y + dy[i])); n         } n       } n     } n   } n } n vector<int> path; n int cnt = 0; n void bfs(int x1, int y1, int x2, int y2) { n   for (int i = 0, maxi = (int)(all).size(); i < maxi; i++) { n     int x = all[i].first; n     int y = all[i].second; n     tmp[x][y] = 0; n   } n   all.clear(); n   path.clear(); n   cnt = 0; n   queue<pair<int, int> > q; n   q.push(make_pair(x1, y1)); n   tmp[x1][y1] = 111; n   all.push_back(make_pair(x1, y1)); n   while (!q.empty()) { n     for (int i = 0, maxi = (int)(q).size(); i < maxi; i++) { n       int x = q.front().first; n       int y = q.front().second; n       q.pop(); n       if (x == x2 && y == y2) { n         cnt = 0; n         while (x != x1 || y != y1) { n           int j = tmp[x][y] - 1; n           cnt++; n           path.push_back(j); n           x = x - dx[j]; n           y = y - dy[j]; n         } n         return; n       } n       for (int j = 0; j < 4; j++) { n         if (x + dx[j] >= 0 && x + dx[j] <= 799 && y + dy[j] >= 0 && n             y + dy[j] <= 799) { n           if (matrix[x + dx[j]][y + dy[j]] != 255 && n               tmp[x + dx[j]][y + dy[j]] == 0) { n             tmp[x + dx[j]][y + dy[j]] = j + 1; n             q.push(make_pair(x + dx[j], y + dy[j])); n             all.push_back(make_pair(x + dx[j], y + dy[j])); n           } n         } n       } n     } n   } n } n string ans =   ; n int main() { n   int x1, y1, x2, y2; n   scanf( %d %d %d %d , &x1, &y1, &x2, &y2); n   x1 += 400; n   y1 += 400; n   x2 += 400; n   y2 += 400; n   int m; n   scanf( %d , &m); n   int max_x = 0, min_x = 800, max_y = 0, min_y = 800; n   for (int i = 0; i < m; i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     x += 400; n     y += 400; n     min_x = min(min_x, x); n     max_x = max(max_x, x); n     min_y = min(min_y, y); n     max_y = max(max_y, y); n     matrix[x][y] = 255; n   } n   go(x1, y1); n   if (m == 0 || tmp[x1][y1] != tmp[x2][y2]) { n     printf( -1 ); n     return 0; n   } n   while (true) { n     bfs(x1, y1, x2, y2); n     reverse(path.begin(), path.end()); n     for (int i = 0; i < cnt; i++) { n       if (path[i] == 0) n         ans +=  R ; n       else if (path[i] == 1) n         ans +=  U ; n       else if (path[i] == 2) n         ans +=  L ; n       else if (path[i] == 3) n         ans +=  D ; n       int j = path[i]; n       x1 += dx[j]; n       y1 += dy[j]; n       if (matrix[x2 + dx[j]][y2 + dy[j]] != 255) { n         x2 += dx[j]; n         y2 += dy[j]; n         path.push_back(j); n         cnt++; n       } n       if (min(y1, y2) > max_y || max(y1, y2) < min_y || min(x1, x2) > max_x || n           max(x1, x2) < min_x || x1 == x2 && y1 == y2) n         break; n     } n     if (x1 == x2 && y1 == y2) { n       cout << ans; n       return 0; n     } n     bool flag = false; n     if (min(y1, y2) > max_y) { n       if (max(x1, x2) >= min_x) { n         int diff = max(x1, x2) - min_x + 1; n         x1 -= diff; n         x2 -= diff; n         for (int i = 0; i < diff; i++) ans +=  L ; n       } n       if (max(y1, y2) >= min_y) { n         int diff = max(y1, y2) - min_y + 1; n         y1 -= diff; n         y2 -= diff; n         for (int i = 0; i < diff; i++) ans +=  D ; n       } n       flag = true; n     } else if (max(y1, y2) < min_y) { n       if (max(x1, x2) >= min_x) { n         int diff = max(x1, x2) - min_x + 1; n         x1 -= diff; n         x2 -= diff; n         for (int i = 0; i < diff; i++) ans +=  L ; n       } n       if (max(y1, y2) >= min_y) { n         int diff = max(y1, y2) - min_y + 1; n         y1 -= diff; n         y2 -= diff; n         for (int i = 0; i < diff; i++) ans +=  D ; n       } n       flag = true; n     } else if (min(x1, x2) > max_x) { n       if (max(y1, y2) >= min_y) { n         int diff = max(y1, y2) - min_y + 1; n         y1 -= diff; n         y2 -= diff; n         for (int i = 0; i < diff; i++) ans +=  D ; n       } n       if (max(x1, x2) >= min_x) { n         int diff = max(x1, x2) - min_x + 1; n         x1 -= diff; n         x2 -= diff; n         for (int i = 0; i < diff; i++) ans +=  L ; n       } n       flag = true; n     } else if (max(x1, x2) < min_x) { n       if (max(y1, y2) >= min_y) { n         int diff = max(y1, y2) - min_y + 1; n         y1 -= diff; n         y2 -= diff; n         for (int i = 0; i < diff; i++) ans +=  D ; n       } n       if (max(x1, x2) >= min_x) { n         int diff = max(x1, x2) - min_x + 1; n         x1 -= diff; n         x2 -= diff; n         for (int i = 0; i < diff; i++) ans +=  L ; n       } n       flag = true; n     } n     if (flag) { n       if (y2 > y1) { n         int y = min_y; n         int x; n         for (int i = 290; i < 510; i++) { n           if (matrix[i][y] == 255) { n             x = i; n             break; n           } n         } n         if (x2 < x) { n           int diff = x - x2; n           x1 += diff; n           x2 += diff; n           for (int i = 0; i < diff; i++) ans +=  R ; n         } n         if (y2 != y1) { n           int diff = y - y1 - 1; n           y2 = y - 1; n           y1 = y2; n           for (int i = 0; i < diff; i++) ans +=  U ; n         } n       } else { n         if (min(y1, y2) <= max_y) { n           int diff = max_y - min(y1, y2) + 1; n           y1 += diff; n           y2 += diff; n           for (int i = 0; i < diff; i++) ans +=  U ; n         } n         int y = max_y; n         int x; n         for (int i = 290; i < 510; i++) { n           if (matrix[i][y] == 255) { n             x = i; n             break; n           } n         } n         if (x2 < x) { n           int diff = x - x2; n           x1 += diff; n           x2 += diff; n           for (int i = 0; i < diff; i++) ans +=  R ; n         } n         if (y2 != y1) { n           int diff = y1 - y - 1; n           y1 -= diff; n           y2 = y + 1; n           for (int i = 0; i < diff; i++) ans +=  D ; n         } n       } n       if (x2 > x1) { n         if (max(x1, x2) >= min_x) { n           int diff = max(x1, x2) - min_x + 1; n           x2 -= diff; n           x1 -= diff; n           for (int i = 0; i < diff; i++) ans +=  L ; n         } n         if (max(y1, y2) >= min_y) { n           int diff = max(y1, y2) - min_y + 1; n           y2 -= diff; n           y1 -= diff; n           for (int i = 0; i < diff; i++) ans +=  D ; n         } n         int x = min_x; n         int y; n         for (int i = 290; i < 510; i++) { n           if (matrix[x][i] == 255) { n             y = i; n             break; n           } n         } n         if (y1 < y) { n           int diff = y - y1; n           y1 += diff; n           y2 += diff; n           for (int i = 0; i < diff; i++) ans +=  U ; n         } n         if (x2 != x1) { n           int diff = x - x1 - 1; n           x1 += diff; n           x2 = x - 1; n           for (int i = 0; i < diff; i++) ans +=  R ; n         } n         cout << ans; n         return 0; n       } else { n         if (min(x1, x2) <= max_x) { n           int diff = max_x - min(x1, x2) + 1; n           x1 += diff; n           x2 += diff; n           for (int i = 0; i < diff; i++) ans +=  R ; n         } n         if (max(y1, y2) >= min_y) { n           int diff = max(y1, y2) - min_y + 1; n           y2 -= diff; n           y1 -= diff; n           for (int i = 0; i < diff; i++) ans +=  D ; n         } n         int x = max_x; n         int y; n         for (int i = 290; i < 510; i++) { n           if (matrix[x][i] == 255) { n             y = i; n             break; n           } n         } n         if (y1 < y) { n           int diff = y - y1; n           y1 += diff; n           y2 += diff; n           for (int i = 0; i < diff; i++) ans +=  U ; n         } n         while (x2 != x1) { n           int diff = x1 - x - 1; n           x1 -= diff; n           x2 = x + 1; n           for (int i = 0; i < diff; i++) ans +=  L ; n         } n         cout << ans; n         return 0; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1e9 + 7; n long long INF = 1e7; n double EPS = 1e-12; n double tick() { n   static clock_t oldt; n   clock_t newt = clock(); n   double diff = 1.0 * (newt - oldt) / CLOCKS_PER_SEC; n   oldt = newt; n   return diff; n } n long long int powP(long long int a, long long int b) { n   if (b == 0) return 1 % mod; n   long long int k; n   k = powP(a, b / 2); n   k = k * k % mod; n   if (b % 2 == 0) n     return k; n   else n     return a * k % mod; n } n bool bitSet(long long int n, long long int i) { n   if ((n & (1LL << i)) != 0) n     return true; n   else n     return false; n } n long long int findGcd(long long int a, long long int b) { n   if (a == 0) n     return b; n   else n     return findGcd(b % a, a); n } n long long int A[700005], B[700005], C[700005]; n int main() { n   long long int T, i, n, m, p, j, l, e, r, b, c, k, q, a, d, w, x, y, v, z, t, n       curr, prev, sum, ans, pos, val, countA, secondMin, indicator; n   scanf( %lld , &n); n   stack<int> S; n   ans = 0; n   S.push(0); n   B[0] = INT_MAX; n   for (long long int i = (1); i <= (n); ++i) { n     scanf( %lld , &A[i]); n     B[i] = 1; n     while (!S.empty() && A[i] > A[S.top()]) { n       B[i] = max(B[i], B[S.top()] + 1); n       S.pop(); n     } n     if (B[i] < INT_MAX) ans = max(ans, B[i]); n     S.push(i); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxx = 2000010; n const double pi = acos(-1.0); n const double eps = 1e-15; n map<pair<long long, long long>, long long> h, mk; n map<pair<long long, pair<long long, long long> >, long long> hh; n int t, n, m, k; n long long ans = 0, total = 0, sum = 0, cnt = 0, pos = 0, s1 = 0, s2 = 0, a1 = 0, n           a2 = 0; n long long a[maxx], vis[maxx]; n char c[maxx], ch[maxx]; n struct nod { n   long long v1, v2; n } kl[maxx]; n struct node { n   int num; n   long long val1, val2; n } b[maxx]; n bool cmp(node a, node b) { n   if (a.val1 == b.val1) return a.val2 < b.val2; n   return a.val1 < b.val1; n } n int main() { n   scanf( %d%d , &n, &k); n   n = n * k; n   scanf(  %s , c + 1); n   for (int i = n + 1; i <= 2 * n; i++) c[i] = c[i - n]; n   scanf( %d , &m); n   a1 = a2 = 1; n   for (int i = 1; i < k; i++) n     a1 = a1 * 237 % 998244353, a2 = a2 * 237 % 1000000007; n   for (int i = 1; i <= m; i++) { n     scanf(  %s , ch + 1); n     s1 = s2 = 0; n     for (int j = 1; j <= k; j++) n       s1 = (s1 * 237 % 998244353 + ch[j] -  a  + 1) % 998244353, n       s2 = (s2 * 237 % 1000000007 + ch[j] -  a  + 1) % 1000000007; n     h[make_pair(s1, s2)]++, b[i].val1 = s1, b[i].val2 = s2, b[i].num = i; n   } n   sort(b + 1, b + n + 1, cmp); n   for (int i = 1; i <= n; i++) { n     mk[make_pair(b[i].val1, b[i].val2)] = i; n     for (int j = i; j <= n; j++) { n       if (b[i].val1 != b[j].val1 || b[i].val2 != b[j].val2) { n         i = j - 1; n         break; n       } n       if (j == n) i = n + 1; n     } n   } n   s1 = s2 = 0; n   for (int i = 1; i <= k; i++) n     s1 = (s1 * 237 % 998244353 + c[i] -  a  + 1) % 998244353, n     s2 = (s2 * 237 % 1000000007 + c[i] -  a  + 1) % 1000000007; n   kl[1].v1 = s1, kl[1].v2 = s2; n   for (int i = k + 1; i <= 2 * n; i++) { n     s1 = (s1 - a1 * (c[i - k] -  a  + 1) % 998244353 + 998244353) % 998244353, n     s1 = (s1 * 237 % 998244353 + c[i] -  a  + 1) % 998244353; n     s2 = n         (s2 - a2 * (c[i - k] -  a  + 1) % 1000000007 + 1000000007) % 1000000007, n     s2 = (s2 * 237 % 1000000007 + c[i] -  a  + 1) % 1000000007; n     kl[i - k + 1].v1 = s1, kl[i - k + 1].v2 = s2; n   } n   for (int i = 1; i <= n; i++) { n     int u = i % k; n     if (vis[u]) continue; n     hh[make_pair(u, make_pair(kl[i].v1, kl[i].v2))]++; n     if (hh[make_pair(u, make_pair(kl[i].v1, kl[i].v2))] > n             h[make_pair(kl[i].v1, kl[i].v2)] || n         !h.count(make_pair(kl[i].v1, kl[i].v2))) n       vis[u] = 1; n   } n   pos = -1; n   for (int i = 0; i < k; i++) n     if (!vis[i]) { n       pos = i; n       break; n     } n   if (pos == -1) n     printf( NO ); n   else { n     printf( YES n ); n     if (!pos) pos = k; n     for (int i = pos; i <= n; i += k) n       printf( %d  , b[mk[make_pair(kl[i].v1, kl[i].v2)]++].num); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, M, K, P[105]; n int main() { n   scanf( %d %d %d , &N, &M, &K); n   for (int i = 0; i < K; i++) scanf( %d , &P[i]); n   int ans = 0; n   for (int i = 0; i < N; i++) { n     for (int j = 0; j < M; j++) { n       int x; n       scanf( %d , &x); n       int idx; n       for (int k = 0; k < K; k++) n         if (x == P[k]) { n           idx = k; n           break; n         } n       for (int k = idx; k > 0; k--) P[k] = P[k - 1]; n       P[0] = x; n       ans += idx + 1; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f, N = 2e5 + 5; n const long long inf = 1e18; n inline int read() { n   int sum = 0, f = 1; n   char c = getchar(); n   while (c >  9  || c <  0 ) { n     if (c ==  - ) f = -f; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) sum = sum * 10 + c - 48, c = getchar(); n   return sum * f; n } n int n, m, k, q, t[N][2], safe[N]; n long long dp[N][2]; n long long cost(int a, int b, int c, int d) { n   int pos = lower_bound(safe + 1, safe + q + 1, t[a][b]) - safe; n   long long ans = inf; n   if (pos <= q) n     ans = abs(t[a][b] - safe[pos]) + abs(t[c][d ^ 1] - safe[pos]) + n           abs(t[c][d] - t[c][d ^ 1]); n   pos = upper_bound(safe + 1, safe + q + 1, t[a][b]) - safe - 1; n   if (pos >= 1) n     ans = n         min(ans, (long long)abs(t[a][b] - safe[pos]) + n                      abs(t[c][d ^ 1] - safe[pos]) + abs(t[c][d] - t[c][d ^ 1])); n   return ans; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m >> k >> q; n   for (int i = 1; i <= k; i++) { n     int r, c; n     cin >> r >> c; n     if (t[r][0] == 0) n       t[r][0] = c; n     else n       t[r][0] = min(t[r][0], c); n     t[r][1] = max(t[r][1], c); n   } n   t[1][0] = 1, t[1][1] = max(t[1][1], 1); n   for (int i = 1; i <= q; i++) cin >> safe[i]; n   sort(safe + 1, safe + q + 1); n   for (int i = 2; i <= n; i++) dp[i][0] = dp[i][1] = inf; n   dp[1][0] = abs(t[1][1] - 1) * 2; n   dp[1][1] = abs(t[1][1] - 1); n   int pre = 1; n   for (int i = 2; i <= n; i++) { n     if (t[i][0] == 0) continue; n     for (int j = 0; j < 2; j++) { n       for (int k = 0; k < 2; k++) { n         dp[i][j] = min(dp[i][j], dp[pre][k] + i - pre + cost(pre, k, i, j)); n       } n     } n     pre = i; n   } n   long long ans = min(dp[pre][0], dp[pre][1]); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   char s[51]; n   scanf( %s , s); n   int i, tf, na = 0, t1 = strlen(s); n   if (t1 >= 1 && t1 <= 50) { n     for (i = 0; i < t1; i++) { n       if (s[i] ==  a ) { n         na = na + 1; n       } n     } n     if (2 * na > t1) { n       tf = t1; n     } else { n       tf = (2 * na) - 1; n     } n     printf( %d n , tf); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 131; n int a[100002], rmp[100002], n, cnt, ans; n unsigned int h[100002], p[100002]; n vector<int> v[100002]; n vector<pair<int, int> > del; n map<int, int> mp; n inline bool cmp(int x, int y) { n   int xe = y - 1, ye = 2 * y - x - 1; n   if (ye > n) return 0; n   int hx = h[xe] - h[x - 1] * p[y - x], hy = h[ye] - h[y - 1] * p[y - x]; n   return hx == hy; n } n inline int read() { n   int t = 0; n   char v = getchar(); n   while (v <  0 ) v = getchar(); n   while (v >=  0 ) t = (t << 3) + (t << 1) + v - 48, v = getchar(); n   return t; n } n int main() { n   p[0] = 1; n   n = read(); n   for (int i = 1; i <= n; ++i) { n     int x = read(); n     if (mp.find(x) != mp.end()) n       a[i] = mp[x]; n     else { n       mp[x] = ++cnt; n       a[i] = cnt; n       rmp[cnt] = x; n     } n     p[i] = p[i - 1] * M; n     v[a[i]].push_back(i); n     h[i] = h[i - 1] * M + a[i]; n   } n   for (int i = 1; i <= cnt; ++i) n     for (int j = 0; j < v[i].size(); ++j) n       for (int k = j + 1; k < v[i].size(); ++k) n         if (cmp(v[i][j], v[i][k])) n           del.push_back(make_pair(v[i][k] - v[i][j], v[i][j])); n   sort(del.begin(), del.end()); n   for (int i = 0; i < del.size(); ++i) n     if (del[i].second > ans) ans = del[i].second + del[i].first - 1; n   printf( %d n , n - ans); n   for (int i = ans + 1; i <= n; ++i) printf( %d  , rmp[a[i]]); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 5; n int N, M; n int arr[MAXN]; n int dp[MAXN]; n int main() { n   cin.tie(0); n   cin.sync_with_stdio(0); n   cin >> N >> M; n   int prv; n   cin >> prv; n   int lft = 1; n   for (int a = 2; a <= N; a++) { n     int cur; n     cin >> cur; n     if (prv <= cur) { n     } else { n       lft = a; n     } n     if (prv >= cur) { n       dp[a] = min(dp[a - 1], lft); n     } else { n       dp[a] = lft; n     } n     prv = cur; n   } n   while (M--) { n     int l, r; n     cin >> l >> r; n     if (l >= dp[r]) { n       cout <<  Yes n ; n     } else { n       cout <<  No n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int arr[100][100] = {0}; n   int n, x; n   cin >> n >> x; n   for (int i = 0; i < n; i++) { n     arr[i][i] = x; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       cout << arr[i][j] <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int trn = 1 << (n + 1); n   trn--; n   int a[30000]; n   for (int i = 2; i <= trn; i++) cin >> a[i]; n   int q = 1 << n; n   int mx = 0; n   int leng[30000]; n   for (int i = q; i <= trn; i++) { n     leng[i] = 0; n     int j = i; n     while (j > 0) { n       leng[i] += a[j]; n       j /= 2; n     } n     mx = max(mx, leng[i]); n   } n   int add[30000]; n   for (int i = q; i <= trn; i++) add[i] = mx - leng[i]; n   for (int depth = n + 1; depth > 1; depth--) { n     q = 1 << (depth - 1); n     for (int i = q; i < q * 2; i += 2) { n       if ((add[i] > 0) && (add[i + 1] > 0)) { n         int t = min(add[i], add[i + 1]); n         add[i / 2] = t; n         add[i] = add[i] - t; n         add[i + 1] = add[i + 1] - t; n       } n     } n   } n   int ans = 0; n   for (int i = 0; i <= trn; i++) ans += add[i]; n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n const int N = 1e5 + 5; n void __print(int x) { cerr << x; } n void __print(long x) { cerr << x; } n void __print(long long x) { cerr << x; } n void __print(unsigned x) { cerr << x; } n void __print(unsigned long x) { cerr << x; } n void __print(unsigned long long x) { cerr << x; } n void __print(float x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(char x) { cerr <<      << x <<     ; } n void __print(const char *x) { cerr <<     << x <<    ; } n void __print(const string &x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  , ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &x) { n   int f = 0; n   cerr <<  { ; n   for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n vector<vector<int> > ans; n void solve(int n) { n   int i = 1, j = 2, k = 3; n   while (k <= n) { n     ans.push_back({i, j, k}); n     i += 2; n     j += 2; n     k += 2; n   } n   i = 1, j = 2, k -= 2; n   while (j < n - 1) { n     ans.push_back({i, j, k}); n     i += 2, j += 2; n   } n } n void solve() { n   int n; n   cin >> n; n   long long a[n]; n   long long x = 0; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     x ^= a[i]; n   } n   if (n % 2 == 0) { n     if (x) { n       cout <<  NO n ; n       return; n     } else { n       solve(n - 1); n     } n   } else { n     solve(n); n   } n   cout <<  YES n ; n   cout << ans.size() <<   n ; n   for (auto v : ans) { n     printf( %d %d %d n , v[0], v[1], v[2]); n   } n } n signed main() { n   int t = 1; n   for (long long i = 1; i < t + 1; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int N = 1e5 + 5; n int n, k, a[N]; n int ans[N], top; n int main() { n   scanf( %d%d , &n, &k); n   int sum = 0; n   for (int i = 1; i <= n; i++) scanf( %d , a + i), sum += a[i]; n   if (sum % k) return !printf( No ); n   int num = sum / k; n   top = 0; n   int now = 0, l = 1; n   for (int i = 1; i <= n; i++) { n     now += a[i]; n     if (now > num) return !printf( No ); n     if (now == num) { n       ans[++top] = i - l + 1; n       l = i + 1; n       now = 0; n     } n   } n   printf( Yes n ); n   for (int i = 1; i <= top; i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long int n, m, t, q, a, b, c, h, d, x, y, z, i, j, k, n       ans = 0, ans2 = 0, cur = 0, cur2 = 0, sum = 0, sum2 = 0, maxn; n   string str =   , str2, str3; n   cin >> t; n   while (t--) { n     cin >> n >> x >> y; n     if (abs(x - y) > 1) { n       cout <<  -1 n ; n       continue; n     } else if (x + y > n - 2) { n       cout <<  -1 n ; n       continue; n     } n     a = 1; n     b = n; n     if (x == y) { n       cout <<  1  ; n       a++; n       for (i = 0; i < x; i++) { n         cout << b <<     << a <<    ; n         a++; n         b--; n       } n       for (i = a; i <= b; i++) cout << i <<    ; n       cout <<   n ; n     } else if (x > y) { n       for (i = 0; i < x; i++) { n         cout << a <<     << b <<    ; n         a++; n         b--; n       } n       for (i = b; i >= a; i--) cout << i <<    ; n       cout <<   n ; n     } else { n       for (i = 0; i < y; i++) { n         cout << b <<     << a <<    ; n         a++; n         b--; n       } n       for (i = a; i <= b; i++) cout << i <<    ; n       cout <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long A[100005], Nr[100005], Viz[100005]; n vector<pair<long long, long long> > V[100005]; n long long n; n long long ans; n long long DFSNR(long long x) { n   long long nrc = 1; n   Viz[x] = 1; n   for (auto i : V[x]) n     if (!Viz[i.first]) { n       nrc += DFSNR(i.first); n     } n   Nr[x] = nrc; n   return nrc; n } n void DFS(long long x, long long dist) { n   if (A[x] < dist) { n     ans += Nr[x]; n     return; n   } n   Viz[x] = 1; n   for (auto i : V[x]) n     if (!Viz[i.first]) { n       DFS(i.first, max(dist + i.second, i.second)); n     } n } n int main() { n   cin >> n; n   for (long long i = 1; i <= n; i++) cin >> A[i]; n   for (long long i = 1; i < n; i++) { n     long long x, y; n     cin >> x >> y; n     V[x].push_back({i + 1, y}); n     V[i + 1].push_back({x, y}); n   } n   DFSNR(1); n   memset(Viz, 0, sizeof(Viz)); n   DFS(1, 0); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n int w, n, m, c[1 << 12], id[1 << 12], f[531441]; n int dfs(char* g, int s, int t) { n   if (!*g) return c[s] * f[t]; n   s <<= 1; n   t *= 3; n   switch (*g++) { n     case  A : n       return dfs(g, s, t + 2) + dfs(g, s | 1, t); n     case  O : n       return dfs(g, s, t); n     case  X : n       return dfs(g, s, t) + dfs(g, s | 1, t + 1); n     case  a : n       return dfs(g, s | 1, t + 1); n     case  o : n       return dfs(g, s, t + 1) + dfs(g, s | 1, t + 2); n     case  x : n       return dfs(g, s, t + 1) + dfs(g, s | 1, t); n   } n } n int main() { n   scanf( %d%d%d , &w, &n, &m); n   for (int i = 1; i < 1 << w; i++) id[i] = id[i / 2] * 3 + i % 2; n   while (n--) { n     int a; n     scanf( %d , &a); n     c[a]++; n     f[id[a]]++; n   } n   for (int i = 0, lim = 1; i < w; i++, lim *= 3) n     for (int s = 0; s < 1 << w; s += 1 << i + 1) n       for (int t = 0, j = id[s]; t < lim; t++, j++) n         f[j + lim * 2] += f[j] + f[j + lim]; n   while (m--) { n     char g[15]; n     scanf( %s , g); n     printf( %d n , dfs(g, 0, 0)); n   } n } n


/* n AUTHOR: lz.askey n CREATED: 23.07.2021 13:57:23 n LANG: C++11 n */ n #include <assert.h> n  n #include <algorithm> n #include <bitset> n #include <cmath> n #include <cstdio> n #include <cstdlib> n #include <cstring> n #include <deque> n #include <iostream> n #include <iterator> n #include <map> n #include <new> n #include <queue> n #include <set> n #include <sstream> n #include <stack> n #include <string> n #include <unordered_map> n #include <unordered_set> n #include <vector> n  n using namespace std; n typedef vector<int> vi; n typedef pair<int, int> ii; n typedef vector<pair<int, int> > vii; n typedef long long ll; n #define bit(x, i) (x & (1 << i)) n #define in(i, l, r) (l < i && i < r) n #define linr(i, l, r) (l <= i && i <= r) n #define lin(i, l, r) (l <= i && i < r) n #define inr(i, l, r) (l < i && i <= r) n #define gi(a) scanf( %d , &a) n #define gii(a, b) scanf( %d%d , &a, &b) n #define giii(a, b, c) scanf( %d%d%d , &a, &b, &c) n #define gs(x) scanf( %s , x) n #define clr(a, x) memset(a, x, sizeof(a)) n #define c2i(c) (c -  0 ) n #define sz(x) ((int)((x).size())) n #define all(c) (c).begin(), (c).end() n #define mp make_pair n #define pb push_back n #define eb emplace_back n #define ff first n #define ss second n // for debug n #define what_is(x)   n   cerr <<  Line   << __LINE__ <<  :   << #x <<   is   << (x) << endl; n #define error(args...)                         n   {                                            n     string _s = #args;                         n     replace(_s.begin(), _s.end(),  , ,    );   n     stringstream _ss(_s);                      n     istream_iterator<string> _it(_ss);         n     err(_it, args);                            n   } n void err(istream_iterator<string> it) { cerr << endl; } n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cerr << *it <<   =   << a << endl; n   err(++it, args...); n } n  n #define REP(i, a, b) for (int i = int(a); i < int(b); i++) n const int inf = 0x3f3f3f3f; n const int mod = 1e9 + 7; n const int fx[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; n const int fxx[8][2] = {{0, 1}, {0, -1}, {1, 0},  {-1, 0}, n                        {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; n  n // struct n  n // data n int m, k, ans1, ans2; n ll a[1010][1010], c[1010]; n ll x, y; n  n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> m >> k; n   REP(i, 0, k) { n     REP(j, 0, m) { n       cin >> a[i][j]; n       c[i] += a[i][j]; n     } n   } n   x = (c[k - 1] - c[0]) / (k - 1); n   REP(i, 1, k) { n     if (c[i] - c[0] != x * i) { n       ans1 = i; n       y = c[i] - c[0] - x * i; n       break; n     } n   } n   REP(i, 1, k - 1) { n     if (i - 1 != ans1 && i != ans1 && i + 1 != ans1) { n       x = 0; n       REP(j, 0, m) { n         x += a[i - 1][j] * a[i - 1][j] + a[i + 1][j] * a[i + 1][j] - n              2 * a[i][j] * a[i][j]; n       } n       break; n     } n   } n   ll ad = 0, mi = 0; n   REP(j, 0, m) { n     ad += a[ans1 - 1][j] * a[ans1 - 1][j] + a[ans1 + 1][j] * a[ans1 + 1][j]; n     mi += a[ans1][j] * a[ans1][j] * 2; n   } n   ad -= x; n   REP(i, 0, m) { n     ll temp = mi - a[ans1][i] * a[ans1][i] * 2 + n               (a[ans1][i] - y) * (a[ans1][i] - y) * 2; n     if (temp == ad) { n       ans2 = a[ans1][i] - y; n       break; n     } n   } n   cout << ans1 <<     << ans2 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s[102]; n int main() { n   int n, i, cnt[27], j, k; n   cin >> n; n   for (i = 0; i < n; i++) cin >> s[i]; n   int ans = 0, temp, first, second, x, r; n   for (i = 1; i <= 26; i++) { n     for (j = 1; j <= 26; j++) { n       temp = 0; n       for (k = 0; k < n; k++) { n         memset(cnt, 0, sizeof(cnt)); n         x = 0; n         for (r = 0; r < s[k].size(); r++) { n           cnt[(int)s[k][r] - 96]++; n         } n         for (r = 1; r <= 26; r++) { n           if (cnt[r]) x++; n         } n         if (x <= 2) { n           int flag = 0; n           for (r = 1; r <= 26; r++) { n             if (cnt[r]) { n               if (r != i && r != j) flag = 1; n             } n           } n           if (flag == 0) temp += s[k].size(); n         } n       } n       ans = max(ans, temp); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string ans[10] = {  ,   ,  2 ,  3 ,  223 ,  5 ,  53 ,  7 ,  7222 ,  7332 }; n int main() { n   int n; n   string s, a =   ; n   cin >> n; n   cin >> s; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  0  || s[i] ==  1 ) continue; n     a += ans[(s[i] -  0 )]; n   } n   sort(a.begin(), a.end()); n   reverse(a.begin(), a.end()); n   cout << a << endl; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n void solve() { n   ll a, b, p; n   string s; n   cin >> a >> b >> p >> s; n   ll n = s.length(); n   s[n - 1] = s[n - 2]; n   vector<ll> cost(n, 0); n   if (s[n - 1] ==  A ) n     cost[n - 1] = a; n   else n     cost[n - 1] = b; n   for (int i = n - 2; i >= 0; i--) { n     if (s[i] != s[i + 1]) { n       if (s[i] ==  A ) n         cost[i] = cost[i + 1] + a; n       else n         cost[i] = cost[i + 1] + b; n     } else n       cost[i] = cost[i + 1]; n   } n   for (int i = 0; i < n; i++) n     if (cost[i] <= p) { n       cout << i + 1 << endl; n       return; n     } n   cout << n << endl; n } n int main() { n   int ___T; n   scanf( %d , &___T); n   for (int cs = 1; cs <= ___T; cs++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef struct { n   int sum, l; n   bool f[55]; n } node; n queue<node> qu; n node x, y; n map<int, int> m; n map<int, int>::iterator pos; n int main() { n   int n, k, a[55], i, j, t; n   while (scanf( %d %d , &n, &k) != EOF) { n     for (i = 0; i < n; i++) scanf( %d , &a[i]); n     x.sum = x.l = 0; n     for (i = 0; i < 55; i++) x.f[i] = true; n     while (!qu.empty()) qu.pop(); n     qu.push(x); n     m.clear(); n     t = 0; n     while (!qu.empty()) { n       x = qu.front(); n       qu.pop(); n       for (i = 0; i < n; i++) { n         if (x.f[i]) { n           y.sum = x.sum + a[i]; n           pos = m.find(y.sum); n           if (pos != m.end()) continue; n           y.l = x.l + 1; n           for (j = 0; j < n; j++) y.f[j] = x.f[j]; n           y.f[i] = false; n           qu.push(y); n           m.insert(pair<int, int>(y.sum, 0)); n           printf( %d , y.l); n           for (j = 0; j < n; j++) n             if (!y.f[j]) printf(  %d , a[j]); n           printf(  n ); n           t++; n           if (t == k) break; n         } n       } n       if (t == k) break; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n int a[maxn]; n queue<int> q; n queue<int> m; n int main() { n   int n; n   scanf( %d , &n); n   int fu = 0; n   int maxfu = -0x3f3f3f3f; n   int idfu; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     if (a[i] < 0) { n       fu++; n       if (maxfu < a[i]) { n         maxfu = a[i]; n         idfu = i; n       } n     } n   } n   int flag = 0; n   if (fu % 2 != 0) { n     m.push(idfu); n     flag = 1; n   } n   for (int i = 1; i <= n; i++) { n     if (flag && i == idfu) continue; n     if (a[i] == 0) n       m.push(i); n     else n       q.push(i); n   } n   while (m.size() != 0) { n     if (m.size() == 1) { n       if (q.size() == 0) n         return 0; n       else n         printf( 2 %d n , m.front()); n       break; n     } else { n       int t1, t2; n       t1 = m.front(); n       m.pop(); n       t2 = m.front(); n       printf( 1 %d %d n , t1, t2); n     } n   } n   if (q.size() < 2) return 0; n   while (q.size() != 1) { n     int t1, t2; n     t1 = q.front(); n     q.pop(); n     t2 = q.front(); n     printf( 1 %d %d n , t1, t2); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int T; n   cin >> T; n   string ans =  / ; n   while (T--) { n     string s; n     cin >> s; n     if (s[0] ==  p ) { n       cout << ans << endl; n       continue; n     } n     cin >> s; n     if (s[0] ==  / ) n       ans = s +  / ; n     else n       ans += s +  / ; n     while (1) { n       int flag = 1; n       int x = ans.find( .. ); n       if (x == -1) break; n       int c = 0; n       int i; n       for (i = x + 1; i >= 0; i--) { n         if (ans[i] ==  / ) { n           if (!flag) break; n           flag = 0; n         } n         c++; n       } n       ans.erase(i, c + 1); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int deg[100000]; n int xr[100000]; n int edges[100000][2]; n int main() { n   int n, t = 0; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d%d , &deg[i], &xr[i]); n   } n   for (int i = 0; i < n; i++) { n     if (deg[i] == 1) { n       int k = i; n       do { n         edges[t][0] = k; n         edges[t][1] = xr[k]; n         t++; n         deg[k] -= 1; n         xr[xr[k]] ^= k; n         deg[xr[k]] -= 1; n         k = xr[k]; n       } while (deg[k] == 1); n     } n   } n   printf( %d n , t); n   for (int i = 0; i < t; i++) printf( %d %d n , edges[i][0], edges[i][1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, L = 1; n   cin >> n; n   int *a = new int[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (i != 0) { n       if (a[i] > a[L - 1]) L = i + 1; n     } n   } n   if (a[0] > a[L - 1]) L = 1; n   sort(a, a + n); n   cout << L <<     << a[n - 2]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6; n long long int n, m, ans, col; n vector<long long int> v[N], par(N), cola(N); n set<long long int> se[N]; n void dfs(long long int s, long long int p) { n   par[s] = p; n   col = 1; n   set<long long int> no, used; n   for (auto ic : se[s]) { n     if (se[p].find(ic) == se[p].end()) { n       no.insert(ic); n     } else { n       used.insert(cola[ic]); n     } n   } n   for (auto x : no) { n     while (used.find(col) != used.end()) col++; n     cola[x] = col; n     used.insert(col); n     ans = max(ans, col); n   } n   for (auto u : v[s]) n     if (u != p) { n       dfs(u, s); n     } n } n int main() { n   cin >> n >> m; n   for (long long int i = (long long int)1; i <= (long long int)n; i++) { n     long long int sz; n     cin >> sz; n     for (long long int j = (long long int)0; j <= (long long int)sz - 1; j++) { n       long long int x; n       cin >> x; n       se[i].insert(x); n     } n   } n   for (long long int i = (long long int)1; i <= (long long int)n - 1; i++) { n     long long int x, y, z; n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   dfs(1, 0); n   ans = max(ans, 1LL); n   cout << ans <<   n ; n   for (long long int i = (long long int)1; i <= (long long int)m; i++) n     cout << max(1LL, cola[i]) <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 0x3f3f3f3f3f3f3f3f; n int32_t main() { n   long long n; n   cin >> n; n   vector<long long> v1, v2; n   long long i, j, ans = 0; n   for (i = 0; i < n; i++) { n     long long len; n     cin >> len; n     bool fg = true; n     long long mi = INT_MAX, mx = INT_MIN; n     for (j = 0; j < len; j++) { n       long long x; n       cin >> x; n       if (x > mi) { n         fg = false; n       } n       mi = min(mi, x); n       mx = max(mx, x); n     } n     if (fg) { n       v1.push_back(mi); n       v2.push_back(mx); n     } n   } n   sort(v1.begin(), v1.end()); n   sort(v2.begin(), v2.end()); n   long long c = 0; n   for (auto x : v1) { n     auto p = upper_bound(v2.begin(), v2.end(), x) - v2.begin(); n     c += p; n   } n   ans = n * n - c; n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t = 1; n   while (t--) { n     int n, k; n     cin >> n >> k; n     vector<int> m(n), c(k + 1), cnt(k + 1, 0); n     for (int i = 0; i < n; i++) { n       cin >> m[i]; n       cnt[m[i]]++; n     } n     for (int i = 1; i <= k; i++) cin >> c[i]; n     for (int i = k - 1; i >= 1; i--) cnt[i] += cnt[i + 1]; n     sort(m.begin(), m.end()); n     int x, mx = 0; n     for (int i = 1; i <= k; i++) { n       x = cnt[i] / c[i]; n       if (cnt[i] % c[i] != 0) x++; n       mx = max(x, mx); n     } n     cout << mx <<   n ; n     vector<vector<int> > ans(mx); n     int j = 0; n     for (int i = n - 1; i >= 0; i--) { n       ans[j].push_back(m[i]); n       j = (j + 1) % mx; n     } n     for (int i = 0; i < mx; i++) { n       cout << ans[i].size() <<    ; n       for (auto x : ans[i]) cout << x <<    ; n       cout <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long x, sum; n   while (~scanf( %lld , &x)) { n     if (x < 0) x = -x; n     if (x == 2) n       printf( 3 n ); n     else if (x == 0) n       printf( 0 n ); n     else { n       for (long long i = 1; i <= x; i++) { n         sum = i * (i + 1) / 2; n         if (sum == x || (sum > x && ((sum - x) % 2 == 0))) { n           printf( %d n , i); n           break; n         } n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long x, y, ans; n   cin >> x >> y; n   ans = (x / 2) * y; n   if (x % 2) ans += y / 2; n   cout << ans; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   long long t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long n, x, m = 1e18; n   cin >> n; n   vector<long long> v; n   for (int i = 0; i < n; i++) { n     cin >> x; n     v.push_back(x); n   } n   sort(v.begin(), v.end()); n   for (int i = 0; i < v.size() / 2; i++) { n     m = min(m, v[v.size() / 2 + i] - v[i]); n   } n   cout << m; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1005; n const int MAXM = 1e5 + 5; n struct EC { n   vector<pair<int, int> > has[2][MAXN]; n   int deg[2][MAXN], color[MAXM], c[2]; n   void prep(int d) { n     for (int i = 0; i < 2; i++) n       for (int j = 0; j < MAXN; j++) has[i][j].resize(d + 1, {0, 0}); n   } n   void dfs(int x, int p) { n     auto i = has[p][x][c[!p]]; n     if (has[!p][i.first][c[p]].second) n       dfs(i.first, !p); n     else n       has[!p][i.first][c[!p]] = {0, 0}; n     has[p][x][c[p]] = i, has[!p][i.first][c[p]] = {x, i.second}; n     color[i.second] = c[p]; n   } n   int solve(vector<pair<int, int> > &e, vector<int> &res) { n     int m = ((int)e.size()), ans = 0; n     for (int i = 0; i < m; i++) n       ans = max({ans, ++deg[0][e[i].first + 1], ++deg[1][e[i].second + 1]}); n     prep(ans); n     for (int i = 0; i < m; i++) { n       int x[2] = {e[i].first + 1, e[i].second + 1}; n       for (int d = 0; d < 2; d++) n         for (c[d] = 1; has[d][x[d]][c[d]].second; c[d]++) n           ; n       if (c[0] != c[1]) dfs(x[1], 1); n       for (int d = 0; d < 2; d++) has[d][x[d]][c[0]] = {x[!d], i + 1}; n       color[i + 1] = c[0]; n     } n     res.resize(m); n     for (int i = 0; i < m; i++) res[i] = color[i + 1]; n     return ans; n   } n } ec; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int a, b, m; n   cin >> a >> b >> m; n   vector<pair<int, int> > e; n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     e.push_back({x - 1, y - 1}); n   } n   vector<int> res; n   cout << ec.solve(e, res) <<   n ; n   for (auto x : res) cout << x <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, k, sum = 0, curr = 0, j = 0; n   vector<long long int> a(100000), res; n   cin >> n >> k; n   for (long long int i = 0; i < n; i++) { n     cin >> a[i]; n     sum += a[i]; n   } n   if (sum % k) { n     cout <<  No ; n   } else { n     for (long long int i = 0; i < n; i++) { n       curr += a[i]; n       if (curr == sum / k) { n         res.push_back(i + 1 - j); n         j = i + 1; n         curr = 0; n       } n     } n     if (res.size() == k) { n       cout <<  Yes  << endl; n       for (long long int i = 0; i < k; i++) { n         cout << res[i] <<    ; n       } n     } else n       cout <<  No ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, s = 2, ans = 0, i; n   cin >> n; n   if (n < 2) { n     cout << 0; n     return 0; n   } n   for (i = 1; i <= 1e6; ++i) { n     if (s > n) break; n     if ((n - s) % 3 == 0) ++ans; n     s += 3 * i + 2; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   double x, y; n   point() { x = y = 0.0; } n   point(double A, double B) { x = A, y = B; } n   double operator*(point B) { return x * B.x + y * B.y; } n   point turn(double A) { n     return point(x * cos(A) - y * sin(A), y * cos(A) + x * sin(A)); n   } n } zero, e, s, t; n double ans, L1, L2, L3, L4, ev, ew, sv, r; n double dis(point &A, point &B) { n   return sqrt(((B.x - A.x) * (B.x - A.x)) + ((B.y - A.y) * (B.y - A.y))); n } n bool check(double mid) { n   double A, B, C; n   t = e.turn(ew * mid); n   L2 = dis(t, zero); n   L4 = sqrt(((L2) * (L2)) - ((r) * (r))); n   C = acos(t * s / L1 / L2); n   A = acos(r / L1); n   B = acos(r / L2); n   if (A + B - C > 1e-8) { n     if (sv * mid - dis(s, t) > 1e-8) return true; n     return false; n   } else { n     C -= A + B; n     if (sv * mid - C * r - L3 - L4 > 1e-8) return true; n     return false; n   } n } n int main() { n   scanf( %lf%lf%lf , &e.x, &e.y, &ev); n   double R = dis(e, zero); n   ew = ev / R; n   scanf( %lf%lf%lf%lf , &s.x, &s.y, &sv, &r); n   L1 = dis(s, zero); n   L3 = sqrt(((L1) * (L1)) - ((r) * (r))); n   double l = 0, r = dis(s, e); n   while (r - l > 1e-8) { n     double mid = (l + r) / 2.0; n     if (check(mid)) n       ans = mid, r = mid; n     else n       l = mid; n   } n   printf( %lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1234567]; n int w[1234567]; n int main() { n   int n, m, q, i; n   while (scanf( %d%d , &n, &m) != EOF) { n     int k = 0; n     memset(a, 0, sizeof(a)); n     for (i = 0; i < n; i++) { n       scanf( %d , &q); n       if (q < 1234560) a[q] = 1; n     } n     i = 1; n     while (1) { n       if (m < i) break; n       if (a[i] == 0 && m >= i) { n         m -= i; n         w[k++] = i; n       } n       i++; n     } n     if (m != 0) { n       for (i = m + w[k - 1]; i >= w[k - 1]; i--) { n         if (a[i] == 0) { n           w[k - 1] = i; n           break; n         } n       } n     } n     printf( %d n , k); n     for (i = 0; i < k; i++) printf( %d%c , w[i], i == k - 1 ?   n  :    ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9; n int n, m; n long long a[200100], fibo[200100], sumfibo[200100]; n struct Segment_Tree { n   int left, right, len; n   long long s0, s1; n } tree[200100 << 2]; n struct Data { n   long long a[2][2]; n } bb[200100], one, b; n Data operator*(const Data x, const Data y) { n   Data res; n   res.a[0][0] = n       (x.a[0][0] * y.a[0][0] % mod + x.a[0][1] * y.a[1][0] % mod) % mod; n   res.a[0][1] = n       (x.a[0][0] * y.a[0][1] % mod + x.a[0][1] * y.a[1][1] % mod) % mod; n   res.a[1][0] = n       (x.a[1][0] * y.a[0][0] % mod + x.a[1][1] * y.a[1][0] % mod) % mod; n   res.a[1][1] = n       (x.a[1][0] * y.a[0][1] % mod + x.a[1][1] * y.a[1][1] % mod) % mod; n   return res; n } n long long cal_fx(long long s0, long long s1, int l) { n   long long res; n   res = (bb[l - 1].a[0][0] * s1 % mod + bb[l - 1].a[0][1] * s0 % mod) % mod; n   return res; n } n void plant_tree(int id, int l, int r) { n   tree[id].left = l, tree[id].right = r; n   tree[id].len = r - l + 1; n   if (l == r) { n     tree[id].s0 = fibo[0] * a[l] % mod; n     tree[id].s1 = fibo[1] * a[l] % mod; n     return; n   } n   int mid = (l + r) >> 1; n   plant_tree(id << 1, l, mid); n   plant_tree(id << 1 | 1, mid + 1, r); n   tree[id].s0 = n       (tree[id << 1].s0 + n        cal_fx(tree[id << 1 | 1].s0, tree[id << 1 | 1].s1, tree[id << 1].len)) % n       mod; n   tree[id].s1 = n       (tree[id << 1].s1 + cal_fx(tree[id << 1 | 1].s0, tree[id << 1 | 1].s1, n                                  tree[id << 1].len + 1)) % n       mod; n } n void push_down() {} n void update_pos(int id, int pos, long long val) { n   if (tree[id].left == tree[id].right) { n     tree[id].s0 = fibo[0] * val % mod; n     tree[id].s1 = fibo[1] * val % mod; n     return; n   } n   int mid = (tree[id].left + tree[id].right) >> 1; n   if (pos <= mid) n     update_pos(id << 1, pos, val); n   else n     update_pos(id << 1 | 1, pos, val); n   tree[id].s0 = n       (tree[id << 1].s0 + n        cal_fx(tree[id << 1 | 1].s0, tree[id << 1 | 1].s1, tree[id << 1].len)) % n       mod; n   tree[id].s1 = n       (tree[id << 1].s1 + cal_fx(tree[id << 1 | 1].s0, tree[id << 1 | 1].s1, n                                  tree[id << 1].len + 1)) % n       mod; n } n Segment_Tree query(int id, int l, int r) { n   if (tree[id].left == l && tree[id].right == r) { n     return tree[id]; n   } n   int mid = (tree[id].left + tree[id].right) >> 1; n   if (r <= mid) n     return query(id << 1, l, r); n   else if (mid < l) n     return query(id << 1 | 1, l, r); n   else { n     Segment_Tree leftson, rightson, now; n     leftson = query(id << 1, l, mid); n     rightson = query(id << 1 | 1, mid + 1, r); n     now.s0 = (leftson.s0 + cal_fx(rightson.s0, rightson.s1, leftson.len)) % mod; n     now.s1 = n         (leftson.s1 + cal_fx(rightson.s0, rightson.s1, leftson.len + 1)) % mod; n     now.len = leftson.len + rightson.len; n     return now; n   } n } n int main() { n   fibo[0] = fibo[1] = 1; n   for (int i = 2; i < 200100; i++) { n     fibo[i] = (fibo[i - 2] + fibo[i - 1]) % mod; n   } n   sumfibo[0] = fibo[0]; n   for (int i = 1; i < 200100; i++) { n     sumfibo[i] = (sumfibo[i - 1] + fibo[i]) % mod; n   } n   one.a[0][0] = one.a[1][1] = 1; n   one.a[1][0] = one.a[0][1] = 0; n   b.a[0][0] = b.a[0][1] = b.a[1][0] = 1; n   b.a[1][1] = 0; n   bb[0] = one; n   for (int i = 1; i < 200100; i++) { n     bb[i] = bb[i - 1] * b; n   } n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , a + i); n   } n   plant_tree(1, 1, n); n   while (m--) { n     int type; n     scanf( %d , &type); n     if (type == 1) { n       int x, v; n       scanf( %d %d , &x, &v); n       update_pos(1, x, v); n     } else if (type == 2) { n       int l, r; n       scanf( %d %d , &l, &r); n       Segment_Tree res = query(1, l, r); n       printf( %lld n , res.s0); n     } else if (type == 3) { n       int l, r, d; n       scanf( %d %d %d , &l, &r, &d); n     } n   } n } n


#include <bits/stdc++.h> n long a[2200]; n long absl(long k) { n   if (k < 0) { n     k = 0 - k; n   } n   return k; n } n int main() { n   long i, j, n, ans, l, r; n   scanf( %ld , &n); n   for (i = 0; i < n; i++) { n     scanf( %ld , a + i); n     a[i] = absl(a[i]); n   } n   for (i = 0, ans = 0; i < n; i++) { n     for (j = 0, l = 0; j < i; j++) { n       if (a[j] < a[i]) { n         l++; n       } n     } n     for (j = i + 1, r = 0; j < n; j++) { n       if (a[j] < a[i]) { n         r++; n       } n     } n     if (l > r) { n       ans += r; n     } else { n       ans += l; n     } n   } n   printf( %ld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char a[500001], b[500001]; n int nxt[500000]; n int c[2], la, lb; n int kmp() { n   nxt[0] = -1; n   if (lb == 0) { n     return 0; n   } n   int no = -1; n   for (int i = 1; i < lb; i++) { n     while (no != -1 && b[no + 1] != b[i]) { n       no = nxt[no]; n     } n     if (b[no + 1] == b[i]) { n       no++; n     } n     nxt[i] = no; n   } n   return nxt[lb - 1] + 1; n } n void solve() { n   scanf( %s%s , a, b); n   c[0] = c[1] = 0; n   la = strlen(a); n   lb = strlen(b); n   for (int i = 0; i < la; i++) { n     c[a[i] -  0 ]++; n   } n   int add = kmp(); n   for (int i = 0; i < lb; i++) { n     c[b[i] -  0 ]--; n     if (c[b[i] -  0 ] < 0) { n       printf( %s n , a); n       return; n     } n   } n   printf( %s , b); n   int c0 = 0, c1 = 0; n   for (int i = add; i < lb; i++) { n     if (b[i] ==  0 ) { n       c0++; n     } else { n       c1++; n     } n   } n   while (c[0] >= c0 && c[1] >= c1) { n     c[0] -= c0; n     c[1] -= c1; n     for (int i = add; i < lb; i++) { n       putchar(b[i]); n     } n   } n   while (c[0]-- > 0) { n     putchar( 0 ); n   } n   while (c[1]-- > 0) { n     putchar( 1 ); n   } n   printf(  n ); n } n int main() { solve(); } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n struct node { n   int to, next; n } p[N << 2]; n int head[N], tot, n, now[N], du[N]; n bool vis[N], isok; n vector<pair<int, int> > a; n void add(int x, int y) { n   p[++tot] = (node){y, head[x]}; n   head[x] = tot; n   p[++tot] = (node){x, head[y]}; n   head[y] = tot; n } n inline void del(int x, int fa) { n   du[fa]--; n   vis[x] = 1; n   for (int i = head[x]; i; i = p[i].next) { n     int y = p[i].to; n     if (vis[y] || y == fa) continue; n     del(y, x); n   } n } n inline void dfs(int x, int fa) { n   int son = 0, id = 0; n   for (int i = head[x]; i; i = p[i].next) { n     int y = p[i].to; n     if (vis[y] || y == fa) continue; n     dfs(y, x); n     if (~now[y]) n       son++, id = y; n     else n       son += 2; n   } n   if (!son) { n     now[x] = 1; n     return; n   } n   if (son == 1) { n     now[x] = (~now[id] ? now[id] : -2) + 1; n     return; n   } n   a.clear(); n   for (int i = head[x]; i; i = p[i].next) { n     int y = p[i].to; n     if (vis[y] || y == fa || now[y] == -1) continue; n     a.push_back(make_pair(now[y], y)); n   } n   sort(a.begin(), a.end()); n   int m = a.size(); n   for (int i = 0; i < m; i++) { n     int r = i; n     while (r < m && a[r].first == a[i].first) r++; n     for (int j = i + 1; j < r; j++) del(a[j].second, x), isok = 1; n     i = r - 1; n   } n   now[x] = -1; n } n int check() { n   for (int i = 1; i <= n; i++) n     if (du[i] > 2) return i; n   return -1; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     add(x, y); n     du[x]++; n     du[y]++; n   } n   while (1) { n     int root = check(); n     isok = 0; n     if (root == -1) break; n     dfs(root, -1); n     if (!isok) break; n   } n   if (check() == -1) { n     int ans = n - 1; n     for (int i = 1; i <= n; i++) ans -= vis[i]; n     while (ans % 2 == 0) ans /= 2; n     printf( %d n , ans); n   } else n     puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000005; n const int mod = 998244353; n long long f[5009]; n long long c[5009][5009]; n long long ch(int a, int b) { n   long long sum = 0; n   for (int i = 0; i <= a && i <= b; i++) { n     sum += ((c[a][i] * c[b][i] % mod) * f[i] % mod); n   } n   return sum % mod; n } n int main() { n   f[0] = f[1] = 1; n   for (int i = 2; i <= 5000; i++) { n     f[i] = f[i - 1] * i % mod; n   } n   for (int i = 1; i <= 5000; i++) { n     c[i][0] = c[i][i] = 1; n     for (int j = 1; j < i; j++) { n       c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod; n     } n   } n   int a, b, c; n   while (~scanf( %d%d%d , &a, &b, &c)) { n     long long ans = 1; n     ans = ans * ch(a, b); n     ans %= mod; n     ans = ans * ch(a, c); n     ans %= mod; n     ans = ans * ch(b, c); n     ans %= mod; n     printf( %I64d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000000; n int k; n int n; n vector<int> v; n int read() { n   scanf( %d %d , &n, &k); n   for (int i = 1; i <= n; i++) { n     int t; n     scanf( %d , &t); n     v.push_back(t); n   } n   sort(v.begin(), v.end()); n   printf( %d , v[n - k]); n   return 0; n } n int process() { return 0; } n int main() { n   read(); n   process(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n template <typename T> n void chkmin(T &x, T y) { n   if (y < x) x = y; n } n template <typename T> n void chkmax(T &x, T y) { n   if (y > x) x = y; n } n template <typename T> n void upd(T &x, T y) { n   if ((x += y) >= mod) x -= mod; n } n int n, m, K, N, sl, fh, a[510][510]; n int rd() { n   sl = 0; n   fh = 1; n   char ch = getchar(); n   while (ch <  0  ||  9  < ch) { n     if (ch ==  - ) fh = -1; n     ch = getchar(); n   } n   while ( 0  <= ch && ch <=  9 ) sl = sl * 10 + ch -  0 , ch = getchar(); n   return sl * fh; n } n int main() { n   ios::sync_with_stdio(false); n   K = rd(); n   for (N = 1; N <= K; N <<= 1) n     ; n   a[1][1] = a[1][2] = a[1][3] = (N << 1) - 1; n   a[3][1] = a[3][2] = a[3][3] = (N << 1) - 1; n   a[2][3] = a[3][4] = K; n   a[2][1] = N; n   a[2][2] = N << 1; n   n = 3; n   m = 4; n   printf( %d %d n , n, m); n   for (int i = 1; i <= n; ++i, puts(  )) n     for (int j = 1; j <= m; ++j) printf( %d  , a[i][j]); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:256000000 ) n using namespace std; n const int INF = (1 << 29) + 5; n const long long int LLINF = (1ll << 59) + 5; n const int MOD = 1000 * 1000 * 1000 + 7; n int n, q; n pair<string, string> arr[228]; n map<string, bool> can; n queue<string> now; n int main() { n   cin >> n >> q; n   string st1, st2; n   for (int i = 0; i < q; ++i) { n     cin >> arr[i].first >> arr[i].second; n   } n   now.push( a ); n   string dd; n   string next; n   while (!now.empty()) { n     dd = now.front(); n     now.pop(); n     if (dd.size() >= 6) continue; n     for (int i = 0; i < q; ++i) { n       if (dd[0] == arr[i].second[0]) { n         next = arr[i].first + dd.substr(1, (int)dd.size() - 1); n         if (!can[next]) { n           can[next] = true; n           now.push(next); n         } n       } n     } n   } n   int answer = 0; n   for (auto it = can.begin(); it != can.end(); ++it) { n     if (it->first.size() == n) { n       ++answer; n     } n   } n   cout << answer; n   fclose(stdin); n   fclose(stdout); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n bool isprime(long long int n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (long long int i = 5; i * i <= n; i += 6) { n     if (n % i == 0 || n % (i + 2) == 0) return false; n   } n   return true; n } n long long int smallestfactor(long long int n) { n   for (long long int i = 2; i <= sqrt(n); i++) { n     if (n % i == 0) { n       long long int x = min(i, n / i); n       return x; n     } n   } n } n int main() { n   long long int n; n   cin >> n; n   long long int ans = 0; n   while (1) { n     ans += n; n     if (isprime(n)) { n       ans++; n       break; n     } n     long long int temp = smallestfactor(n); n     n /= temp; n     if (n == 1) { n       ans++; n       break; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n long long P(long long m, long long n); n long long pi(long long x); n long long phi(long long m, long long n); n bool isprm[400005]; n int cntprm[400005]; n vector<long long> prm; n void Init() { n   for (int i = 2; i <= 400000; ++i) { n     isprm[i] = true; n   } n   for (int i = 2; i <= 400000; ++i) { n     cntprm[i] = cntprm[i - 1] + isprm[i]; n     if (isprm[i]) { n       prm.push_back(i); n       for (int j = i + i; j <= 400000; j += i) isprm[j] = false; n     } n   } n } n long long sol(long long x) { n   long long ans = 0; n   long long sq = sqrt(x); n   for (int i = 0; i < (int)prm.size() && prm[i] <= sq; ++i) { n     long long a = prm[i], b = x / a; n     if (b <= 400000) n       ans += cntprm[b] - cntprm[a]; n     else { n       ans += pi(b) - cntprm[a]; n     } n   } n   return ans; n } n int main(void) { n   long long n; n   Init(); n   cin >> n; n   cout << cntprm[(int)cbrt(n)] + sol(n) <<   n ; n } n long long phi(long long m, long long n) { n   if (m) { n     if (n) { n       if (m <= prm[n - 1]) n         return 1; n       else { n         if (prm[n - 1] * prm[n - 1] >= m) n           return pi(m) - n + 1; n         else n           return phi(m, n - 1) - phi(m / prm[n - 1], n - 1); n       } n     } else { n       return m; n     } n   } else { n     return 0; n   } n } n long long P(long long m, long long n) { n   long long res = 0, sq = sqrt(m); n   for (int i = n; i < (int)prm.size() && prm[i] <= sq; ++i) { n     auto num = prm[i]; n     res += pi(m / num) - pi(num) + 1; n   } n   return res; n } n long long pi(long long x) { n   if (x <= 400000) n     return cntprm[x]; n   else { n     long long n = pi(ceil(cbrt(x))), m = x; n     return phi(m, n) + n - 1 - P(m, n); n   } n } n


#include <bits/stdc++.h> n using namespace std; n string numToBinary(int n, int digits) { n   string s; n   while (n) { n     int bil = n % 2; n     n /= 2; n     s += (bil +  0 ); n   } n   while (s.size() < digits) { n     s +=  0 ; n   } n   reverse(s.begin(), s.end()); n   return s; n } n long long computeInv(const vector<int>& a) { n   int sz = a.size(); n   int suff[sz + 1], i; n   suff[sz] = 0; n   for (i = sz - 1; i >= 0; i--) { n     suff[i] = suff[i + 1]; n     if (a[i] == 0) { n       suff[i]++; n     } n   } n   long long ans = 0; n   for (i = 0; i < sz; i++) { n     if (a[i] == 1) { n       ans += suff[i + 1]; n     } n   } n   return ans; n } n void cetak(const vector<int>& a) { n   for (int i = 0; i < a.size(); i++) { n     printf( %d  , a[i]); n   } n   printf(  n ); n } n int main() { n   int n, i, j; n   int a[300001]; n   while (scanf( %d , &n) != EOF) { n     for (i = 0; i < n; i++) { n       scanf( %d , &a[i]); n     } n     vector<vector<int> > groups; n     vector<int> awalMula; n     for (i = 0; i < n; i++) { n       awalMula.push_back(i); n     } n     groups.push_back(awalMula); n     int sor = 0; n     long long numInv = 0; n     for (i = 29; i >= 0; i--) { n       long long invXor = 0; n       long long invOri = 0; n       for (j = 0; j < groups.size(); j++) { n         vector<int> inv, asli; n         for (int k = 0; k < groups[j].size(); k++) { n           int idx = groups[j][k]; n           if ((1 << i) & a[idx]) { n             asli.push_back(1); n             inv.push_back(0); n           } else { n             asli.push_back(0); n             inv.push_back(1); n           } n         } n         invOri += computeInv(asli); n         invXor += computeInv(inv); n       } n       if (invXor < invOri) { n         sor += (1 << i); n         for (j = 0; j < n; j++) { n           a[j] ^= (1LL << i); n         } n         numInv += invXor; n       } else { n         numInv += invOri; n       } n       vector<vector<int> > newGroups; n       for (j = 0; j < groups.size(); j++) { n         vector<int> group0, group1; n         for (int k = 0; k < groups[j].size(); k++) { n           int idx = groups[j][k]; n           int nilai; n           if ((1 << i) & a[idx]) { n             group1.push_back(idx); n           } else { n             group0.push_back(idx); n           } n         } n         if (group1.size()) { n           newGroups.push_back(group1); n         } n         if (group0.size()) { n           newGroups.push_back(group0); n         } n       } n       groups = newGroups; n     } n     printf( %lld %d n , numInv, sor); n   } n   return 0; n }; n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 150000 + 7; n const long long mod = 1e9 + 7; n const long long INF = 5e18 + 7; n const int inf = 1e9 + 7; n const long long maxx = 1e6 + 700; n int main() { n   int n, m, i; n   scanf( %d%d , &n, &m); n   i = max(2 * n, 3 * m); n   while (i / 2 + i / 3 - i / 6 < n + m || i / 2 < n || i / 3 < m) { n     i++; n   } n   printf( %d , i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 10010; n const int M = 15; n const double eps = 1e-7; n const int INF = 1 << 30; n int n; n struct node { n   double x, y, z; n   node() { x = y = z = 0; } n   node(double xx, double yy, double zz) { n     x = xx; n     y = yy; n     z = zz; n   } n } poy[N], ori; n double vp, vs, prelen; n int ln; n double ABS(double x) { n   while (x < 0) x = -x; n   return x; n } n double dist(node a, node b) { n   double tmp = 0; n   tmp += (a.x - b.x) * (a.x - b.x); n   tmp += (a.y - b.y) * (a.y - b.y); n   tmp += (a.z - b.z) * (a.z - b.z); n   return sqrt(tmp); n } n void init() { n   double x, y, z; n   for (int i = 1; i <= n + 1; i++) { n     cin >> x >> y >> z; n     poy[i] = node(x, y, z); n   } n   cin >> vp >> vs; n   cin >> x >> y >> z; n   ori = node(x, y, z); n } n bool solve() { n   double t = 0; n   for (int i = 2; i <= n + 1; i++) { n     double len = dist(ori, poy[i]) / vp; n     double edge = dist(poy[i - 1], poy[i]) / vs; n     t += edge; n     if ((t - len) >= -eps) { n       ln = i; n       prelen = t - edge; n       return 1; n     } n   } n   return 0; n } n node getmid(node a, node b) { n   double xx, yy, zz; n   xx = (a.x + b.x) * 0.5; n   yy = (a.y + b.y) * 0.5; n   zz = (a.z + b.z) * 0.5; n   return node(xx, yy, zz); n } n void cal() { n   node pre = poy[ln - 1]; n   node l = poy[ln - 1], r = poy[ln]; n   double t; n   while (ABS(dist(l, r)) > eps) { n     node mid = getmid(l, r); n     double len1 = dist(ori, mid); n     double len2 = dist(mid, pre); n     double t1 = len1 / vp; n     double t2 = len2 / vs + prelen; n     t = t1; n     if ((t1 - t2) < 1e-15) n       r = mid; n     else n       l = mid; n   } n   printf( %.10lf n%.10lf %.10lf %.10lf n , t, l.x, l.y, l.z); n } n int main() { n   while (cin >> n) { n     init(); n     if (solve()) n       cout <<  YES  << endl; n     else { n       cout <<  NO  << endl; n       continue; n     } n     cal(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3f; n const int MOD = 1e9 + 7; n template <typename S, typename T> n inline bool Min(S &a, const T &b) { n   return a > b ? a = b, true : false; n } n template <typename S, typename T> n inline bool Max(S &a, const T &b) { n   return a < b ? a = b, true : false; n } n template <typename S, typename T> n inline void Adm(S &a, const T &b) { n   a = (a + b) % MOD; n   if (a < 0) a += MOD; n } n template <typename S, typename T> n inline void Mum(S &a, const T &b) { n   a = 1LL * a * b % MOD; n } n template <typename T> n inline bool IsPri(T x) { n   if (x < 2) return false; n   for (T i = 2; i * i <= x; ++i) n     if (x % i == 0) return false; n   return true; n } n template <typename T> n inline T Gcd(T a, T b) { n   while (b) { n     T t = b; n     b = a % b; n     a = t; n   } n   return a; n } n template <typename T> n inline int BitCnt(T x) { n   int cnt = 0; n   while (x) ++cnt, x &= x - 1; n   return cnt; n } n template <typename T> n inline T MDis(T x1, T y1, T x2, T y2) { n   return abs(x1 - x2) + abs(y1 - y2); n } n template <typename T> n inline T MDis(T x1, T y1, T z1, T x2, T y2, T z2) { n   return abs(x1 - x2) + abs(y1 - y2) + abs(z1 - z2); n } n inline double EDis(double x1, double y1, double x2, double y2) { n   return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); n } n inline double EDis(double x1, double y1, double z1, double x2, double y2, n                    double z2) { n   return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + n               (z1 - z2) * (z1 - z2)); n } n inline long long Pow(long long a, long long n) { n   long long t = 1; n   a %= MOD; n   while (n > 0) { n     if (n & 1) t = t * a % MOD; n     a = a * a % MOD, n >>= 1; n   } n   return t % MOD; n } n inline int read() { n   static char buf[1000000], *p1 = buf, *p2 = buf; n   register int x = false; n   register char ch = n       p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) n           ? EOF n           : *p1++; n   ; n   register bool sgn = false; n   while (ch !=  -  && (ch <  0  || ch >  9 )) n     ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) n              ? EOF n              : *p1++; n   ; n   if (ch ==  - ) n     sgn = true, n     ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) n              ? EOF n              : *p1++; n   ; n   while (ch >=  0  && ch <=  9 ) n     x = (x << 1) + (x << 3) + (ch ^ 48), n     ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) n              ? EOF n              : *p1++; n   ; n   return sgn ? -x : x; n } n const int N = 1e3; n int n, m; n vector<int> dz; n vector<int> e[N]; n int son(int x) { n   int res = 0, y = x; n   while (y <= n) { n     res += min(x, n) - y + 1; n     x = x << 1 | 1, y = y << 1; n   } n   return res; n } n long long ans; n int w[N]; n void DFS1(int x, int fz) { n   w[x] = son(dz[x]); n   for (int y : e[x]) n     if (y != fz) { n       w[x] -= son(dz[y]); n       DFS1(y, x); n     } n } n bool vis[N]; n void DFS2(int x, int rt) { n   vis[x] = 1; n   Adm(ans, 1LL * w[rt] * w[x]); n   for (int y : e[x]) n     if (!vis[y]) DFS2(y, rt); n   vis[x] = 0; n } n int main() { n   cin >> n >> m; n   dz.push_back(1), dz.push_back(INT_MAX); n   vector<pair<int, int> > edge; n   for (int i = 0; i < m; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     edge.push_back({u, v}); n     while (u) dz.push_back(u), u >>= 1; n     while (v) dz.push_back(v), v >>= 1; n   } n   (dz).push_back(INT_MIN), sort((dz).begin(), (dz).end()), n       (dz).erase(unique((dz).begin(), (dz).end()), (dz).end()); n   for (int i = 1; i < ((int)(dz).size()) - 1; ++i) { n     for (int j = dz[i] * 2; j <= dz[i] * 2 + 1; ++j) n       if (dz[(lower_bound(dz.begin(), dz.end(), (j)) - dz.begin())] == j) n         e[i].push_back((lower_bound(dz.begin(), dz.end(), (j)) - dz.begin())), n             e[(lower_bound(dz.begin(), dz.end(), (j)) - dz.begin())].push_back( n                 i); n   } n   DFS1(1, 0); n   for (auto it : edge) n     e[(lower_bound(dz.begin(), dz.end(), (it.first)) - dz.begin())].push_back( n         (lower_bound(dz.begin(), dz.end(), (it.second)) - dz.begin())), n         e[(lower_bound(dz.begin(), dz.end(), (it.second)) - dz.begin())] n             .push_back( n                 (lower_bound(dz.begin(), dz.end(), (it.first)) - dz.begin())); n   for (int i = 1; i < ((int)(dz).size()) - 1; ++i) DFS2(i, i); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char q[7][50] = { Anka ,    Chapay ,  Cleo ,        Troll , n                   Dracul ,  Snowy ,   Hexadecimal }; n map<string, int> h; n int a[3], u[7], v[3], w[7], r, rb, m[7][7]; n void upd() { n   int i, mx, mn, j, k; n   for (i = 0; i < 3; v[i] = 0, i++) n     ; n   for (i = 0; i < 7; v[u[i]]++, i++) n     ; n   for (i = 0; i < 3; i++) n     if (!v[i]) return; n   for (i = 0; i < 7; w[i] = a[u[i]] / v[u[i]], i++) n     ; n   for (mx = w[0], mn = w[0], i = 1; n        i<7; mx = w[i]> mx ? w[i] : mx, mn = w[i] < mn ? w[i] : mn, i++) n     ; n   for (k = 0, i = 0; i < 7; i++) n     for (j = 0; j < 7; k += m[i][j] && u[i] == u[j], j++) n       ; n   if (mx - mn < r || (mx - mn == r && k > rb)) { n     r = mx - mn; n     rb = k; n   } n } n void rec(int i) { n   if (i == 7) n     upd(); n   else n     for (u[i] = 0; u[i] < 3; rec(i + 1), u[i]++) n       ; n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0), cerr <<   ; n   int i, j, t; n   char s[100]; n   for (i = 0; i < 7; h[q[i]] = i, i++) n     ; n   for (cin >> t; t--;) { n     cin >> s; n     i = h[s]; n     cin >> s >> s; n     j = h[s]; n     m[i][j] = 1; n   } n   for (i = 0; i < 3; cin >> a[i], i++) n     ; n   r = 2000000010; n   rec(0); n   cout << r <<     << rb << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mxprime = 2750131; n int prime[mxprime + 1], res; n int func(int n) { n   for (int i = 2; i * i <= n; i++) n     if (n % i == 0) return n / i; n } n vector<int> ord(mxprime, false); n int vc[500001]; n int main() { n   int n; n   cin >> n; n   prime[1] = true; n   map<int, int> mp; n   for (int i = 2; i * i <= mxprime; i++) { n     if (!prime[i]) { n       for (int j = i * i; j <= mxprime; j += i) { n         prime[j] = true; n       } n     } n   } n   for (int i = 2; i <= mxprime; i++) { n     if (!prime[i]) { n       ord[i] = ++res; n     } n   } n   vector<int> a; n   n = n * 2; n   for (int i = 1; i <= n; i++) { n     cin >> vc[i]; n     mp[vc[i]]++; n   } n   sort(vc + 1, vc + n + 1, greater<int>()); n   for (int i = 1; i <= n; i++) { n     if (!mp[vc[i]]) continue; n     if (!prime[vc[i]]) { n       a.push_back(ord[vc[i]]); n       mp[ord[vc[i]]]--; n     } else { n       a.push_back(vc[i]); n       mp[func(vc[i])]--; n     } n     mp[vc[i]]--; n   } n   for (int i = 0; i < a.size(); i++) { n     cout << a[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int c1 = 0, n, t; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> t; n     if (t % 2 == 1) { n       c1++; n     } n   } n   cout << min(c1, n - c1 + (2 * c1 - n) / 3) <<   n ; n   return 0; n } n