#include <bits/stdc++.h> n using namespace std; n vector<int> g[5000]; n int n, m; n int c[100][100]; n char t[100][100]; n bool used[100][100]; n char clr[5000]; n bool valid(int i, int j) { return (0 <= i && i < n && 0 <= j && j < m); }; n int di[4] = {-1, 1, 0, 0}; n int dj[4] = {0, 0, -1, 1}; n void dfs(int i, int j, int color) { n   used[i][j] = true; n   c[i][j] = color; n   for (int k = 0; k < 4; k++) { n     int i1 = i + di[k]; n     int j1 = j + dj[k]; n     if (valid(i1, j1) && t[i1][j1] == t[i][j] && !used[i1][j1]) n       dfs(i1, j1, color); n   } n } n void add_edge(int i, int j) { n   g[i].push_back(j); n   g[j].push_back(i); n } n void dfs1(int i, int j) { n   used[i][j] = true; n   for (int k = 0; k < 4; k++) { n     int i1 = i + di[k]; n     int j1 = j + dj[k]; n     if (valid(i1, j1)) { n       if (t[i1][j1] == t[i][j] && !used[i1][j1]) dfs1(i1, j1); n       if (t[i1][j1] != t[i][j]) add_edge(c[i1][j1], c[i][j]); n     } n   } n } n int l = 0; n bool _used[10000]; n int h[3000]; n int q[10000]; n int bfs(int v) { n   for (int i = 0; i < l; i++) { n     _used[i] = false; n     h[i] = 1000 * 1000 * 1000; n   }; n   int rp = 0; n   int wp = 1; n   q[0] = v; n   h[v] = 0; n   while (rp != wp) { n     v = q[rp]; n     rp++; n     for (int i = 0; i < g[v].size(); i++) { n       int u = g[v][i]; n       if (h[u] > h[v] + 1) { n         h[u] = h[v] + 1; n         q[wp] = u; n         wp++; n       } n     } n   } n   int vmax = 0; n   for (int i = 0; i < l; i++) vmax = max(vmax, h[i]); n   return vmax; n } n int main() { n   scanf( %d%d n , &n, &m); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) scanf( %c , &t[i][j]); n     scanf(  n ); n   } n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (!used[i][j]) { n         clr[l] = t[i][j]; n         dfs(i, j, l++); n       } n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) used[i][j] = false; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (!used[i][j]) dfs1(i, j); n   int best = 1000 * 1000 * 1000; n   for (int i = 0; i < l; i++) { n     int cur = bfs(i); n     if (clr[i] ==  W  && (cur & 1)) cur++; n     if (clr[i] ==  B  && (cur % 2 == 0)) cur++; n     best = min(best, cur); n   } n   cout << best << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1 << 22; n int h, g; n long long a[N]; n vector<int> ans; n int path[25]; n long long pull(int u) { n   long long tp = a[u]; n   int cnt = 0; n   path[cnt++] = u; n   a[u] = 0; n   while ((u << 1) < (1 << h) && max(a[u << 1], a[(u << 1) | 1]) > 0) { n     if (a[u << 1] > a[(u << 1) | 1]) { n       swap(a[u << 1], a[u]); n       u <<= 1; n     } else { n       swap(a[(u << 1) | 1], a[u]); n       u = (u << 1) | 1; n     } n     path[cnt++] = u; n   } n   if (u < (1 << g)) { n     for (int i = cnt - 1; i >= 0; i--) { n       a[path[i]] = a[path[i - 1]]; n     } n     a[path[0]] = tp; n     return 0; n   } n   return tp; n } n void solve() { n   ans.clear(); n   cin >> h >> g; n   long long sum = 0; n   for (int i = 1; i <= (1 << h) - 1; i++) { n     cin >> a[i]; n     sum += a[i]; n   } n   for (int i = 1; i <= (1 << g) - 1; i++) { n     while (true) { n       long long v = pull(i); n       if (v) { n         sum -= v; n         ans.push_back(i); n       } else n         break; n     } n   } n   cout << sum << endl; n   for (auto v : ans) cout << v <<    ; n   cout << endl; n } n int main() { n   int t; n   ios::sync_with_stdio(0); n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cnt[27][1505]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   string s; n   int n, q, m; n   char c; n   cin >> n; n   cin >> s; n   for (int i = (0); i <= (25); i++) { n     char ch = i +  a ; n     for (int j = (0); j <= (n - 1); j++) { n       int tot = 0; n       int need = 0; n       for (int k = (j); k <= (n - 1); k++) { n         tot++; n         if (s[k] != ch) need++; n         cnt[i][need] = max(cnt[i][need], tot); n       } n     } n     for (int j = (1); j <= (n); j++) cnt[i][j] = max(cnt[i][j], cnt[i][j - 1]); n   } n   cin >> q; n   while (q--) { n     cin >> m >> c; n     cout << cnt[c -  a ][m] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const double eps = 1e-11; n template <class T> n inline void checkmin(T& a, T b) { n   if (b < a) a = b; n } n template <class T> n inline void checkmax(T& a, T b) { n   if (b > a) a = b; n } n template <class T> n inline T sqr(T x) { n   return x * x; n } n template <class T> n inline T lsb(T n) { n   return (n & (-n)); n } n template <class T> n inline int countbit(T n) { n   return (n == 0) ? 0 : (1 + countbit(n & (n - 1))); n } n template <class T> n inline T gcd(T a, T b) { n   if (a < 0) return gcd(-a, b); n   if (b < 0) return gcd(a, -b); n   return (b == 0) ? a : gcd(b, a % b); n } n bool isequal(vector<int> a, vector<int> b) { n   for (int i = 0; i < (a.size()); i++) n     if (a[i] != b[i]) return false; n   return true; n } n template <class T> n bool allequal(vector<T> a) { n   sort(a.begin(), a.end()); n   return a[0] == a[a.size() - 1]; n } n template <class T> n void printarray(vector<T> a) { n   for (int i = 0; i < (a.size()); i++) cout << a[i] <<    ; n   cout << endl; n } n template <class T> n void printsubarray(vector<T> a, int start, int end) { n   if (start < 0 or end > a.size() - 1) cout <<  Out of bounds  << endl; n   for (int i = start; i <= end; i++) cout << a[i] <<    ; n   cout << endl; n } n bool isprime(int n) { n   if (n < 2) return false; n   if (n == 2) return true; n   for (int i = 2; i * i <= n; i++) { n     if (n % i == 0) return false; n   } n   return true; n } n void split(const string& s, char c, vector<string>& result) { n   stringstream ss(s); n   string x; n   while (getline(ss, x, c)) result.push_back(x); n } n void err(vector<string>::iterator it) {} n template <typename T, typename... Args> n void err(vector<string>::iterator it, T a, Args... args) { n   cerr << it->substr((*it)[0] ==    , it->length()) <<   =   << a <<   n ; n   err(++it, args...); n } n void readarray(vector<int>& a, int size) { n   int tmp; n   a.clear(); n   for (int i = 0; i < (size); i++) { n     cin >> tmp; n     a.push_back(tmp); n   } n } n bool invalid(int n, int k) { n   int nbits = countbit(n); n   if (k < nbits or k > n) return true; n   return false; n } n vector<int> res; n void split(int n, int k) { n   if (k == 1) { n     res.push_back(n); n   } else { n     int nbits = countbit(n); n     if (nbits == 1) { n       split(n >> 1, ((k >> 1) + k % 2)); n       split(n >> 1, (k >> 1)); n     } else { n       int a, b; n       int msb = (int)log2(n); n       a = (1 << msb); n       b = n ^ a; n       int excess = k - nbits + 1; n       int first = min(excess, a); n       { n         vector<string> _v; n         split( a, b, excess, nbits, k, first ,  , , _v); n         err(_v.begin(), a, b, excess, nbits, k, first); n       }; n       split(a, first); n       split(b, k - first); n     } n   } n } n void solvecase() { n   int n, k; n   cin >> n >> k; n   if (invalid(n, k)) { n     cout <<  NO  << endl; n     return; n   } n   split(n, k); n   cout <<  YES  << endl; n   printarray(res); n } n int main() { n   int cases = 1; n   while (cases--) { n     solvecase(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, cnt; n   int i, ln, sz; n   string tx; n   vector<string> v; n   cin >> n; n   getchar(); n   getline(cin, tx); n   ln = tx.size(); n   int s = 0; n   for (i = 0; i < (ln); i++) n     if (tx[i] ==  .  || tx[i] ==  ?  || tx[i] ==  ! ) { n       v.push_back(tx.substr(s, i - s + 1)); n       if (i + 1 < ln) s = i + 1; n     } n   cnt = sz = 0; n   for (i = 0; i < ((int)v.size()); i++) { n     if ((v[i][0] ==     && v[i].size() > n + 1) || n         (v[i][0] !=     && v[i].size() > n)) { n       cout <<  Impossible  << endl; n       return 0; n     } n     if (i > 0 && sz + (int)v[i].size() <= n) n       sz += (int)v[i].size(); n     else { n       sz = (v[i][0] ==    ) ? (int)v[i].size() - 1 : (int)v[i].size(); n       cnt++; n     } n   } n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const int N = 1e5 + 7, M = 1e7, OO = 0x3f3f3f3f; n map<int, int> dp; n int solve(int num) { n   if (num == 0) { n     return 0; n   } n   if (dp[num]) { n     return dp[num]; n   } n   vector<int> v; n   int temp = num; n   while (temp) { n     v.push_back(temp % 10); n     temp /= 10; n   } n   int ret = -OO; n   for (int i = 0; i < v.size(); ++i) { n     ret = max(ret, v[i]); n   } n   return dp[num] = 1 + solve(num - ret); n } n int main() { n   dp.clear(); n   int num; n   cin >> num; n   cout << solve(num); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5003; n long long a[N + 2], x[N + 2], dp[N + 2]; n int l[N + 2], r[N + 2], cnt[N + 2]; n int main() { n   ios_base::sync_with_stdio(0); n   for (int i = 0; i <= N; i++) { n     l[i] = 1000000; n   } n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i + 1]; n     l[a[i + 1]] = min(i + 1, l[a[i + 1]]); n     r[a[i + 1]] = max(i + 1, r[a[i + 1]]); n   } n   for (int i = n + 1; i >= 1; i--) { n     if (i == n + 1) n       dp[i] = 0; n     else { n       dp[i] = max(dp[i], dp[i + 1]); n       if (l[a[i]] == i) { n         long long sum = 0; n         int tope = 0; n         int tt = 10000000; n         for (int j = i; j <= n; j++) { n           if (cnt[a[j]] == 0) { n             sum ^= a[j]; n             cnt[a[j]] = 1; n           } n           tt = min(tt, l[a[j]]); n           if ((r[a[j]] == j) && tope <= j && tt >= i) { n             dp[i] = max(dp[j + 1] + sum, dp[i]); n           } n           tope = max(tope, r[a[j]]); n         } n         for (int j = i; j <= n; j++) { n           cnt[a[j]] = 0; n         } n       } n     } n   } n   cout << dp[1] << endl; n } n


#include <bits/stdc++.h> n struct Node { n   int sum, value; n   Node *ch[2], *p; n   __inline Node(int value = 0) : value(value) { n     sum = 1; n     ch[0] = ch[1] = p = NULL; n   } n   __inline void update() { n     sum = 1 + (ch[0] ? ch[0]->sum : 0) + (ch[1] ? ch[1]->sum : 0); n   } n } pool[211111], *cur = pool; n __inline bool cut(Node *y) { n   if (!y) return 0; n   Node *x = y->p; n   if (x != NULL) { n     bool d = y == x->ch[1]; n     x->ch[d] = NULL; n     x->sum -= y->sum; n     y->p = NULL; n     return d; n   } n   return 0; n } n __inline void link(Node *x, Node *y, bool d) { n   if (y == NULL) return; n   ; n   ; n   if (x) { n     ; n     ; n     y->p = x; n     x->ch[d] = y; n     x->sum += y->sum; n   } n } n __inline void rotate(Node *x, bool d) { n   ; n   ; n   Node *y = x->ch[d], *px = x->p, *B = y->ch[!d]; n   bool dd = cut(x); n   cut(y); n   cut(B); n   link(x, B, d); n   link(y, x, !d); n   link(px, y, dd); n } n __inline Node *trace_root(Node *x) { n   if (x == NULL) return NULL; n   for (; x->p != NULL; x = x->p) n     ; n   return x; n } n __inline Node *trace(Node *x, bool d) { n   if (x == NULL) return NULL; n   for (; x->ch[d] != NULL; x = x->ch[d]) n     ; n   return x; n } n __inline void splay(Node *x, Node *term) { n   if (x == NULL) return; n   for (; x->p != term;) { n     Node *y = x->p; n     bool dx = x == y->ch[1]; n     if (y->p == term) n       rotate(y, dx); n     else { n       Node *z = y->p; n       bool dy = y == z->ch[1]; n       if (dx == dy) { n         rotate(z, dy); n         rotate(y, dx); n       } else { n         rotate(y, dx); n         rotate(z, dy); n       } n     } n   } n } n __inline void rand_splay(Node *T) { n   if (T == NULL) return; n   Node *last; n   for (last = T, T = T->ch[rand() & 1]; T != NULL;) T = T->ch[rand() & 1]; n   splay(last, NULL); n } n const int N = 222222; n std::map<int, Node *> node; n Node *tree[N]; n int nxt[N], prv[N]; n int n, h, m; n int main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(0); n   std::cin >> h >> m >> n; n   long long ans = 0; n   for (int i = 0; i < h; ++i) nxt[i] = (i + m) % h, prv[i] = (i + h - m) % h; n   for (int _ = 0; _ < n; ++_) { n     char buf[3]; n     int id; n     std::cin >> buf >> id; n     if (buf[0] ==  + ) { n       int th; n       std::cin >> th; n       Node *T = tree[th]; n       splay(T, NULL); n       ans += T ? (T->ch[1] ? T->ch[1]->sum + 1 : 1) : 0; n       if (T != NULL) { n         Node *t = trace(T, 1); n         th = nxt[t->value]; n         T = tree[th]; n         ; n         ; n       } n       T = cur++; n       T->value = th; n       tree[th] = T; n       Node *rt0 = tree[prv[th]]; n       splay(rt0, NULL); n       Node *rt1 = trace_root(tree[nxt[th]]); n       link(T, rt0, 0); n       if (rt1 != rt0) link(T, rt1, 1); n       node[id] = T; n     } else { n       Node *T = node[id]; n       splay(T, NULL); n       Node *t = trace(T->ch[1], 1); n       cut(T->ch[0]); n       cut(T->ch[1]); n       tree[T->value] = NULL; n       node[id] = NULL; n       if (t) n         T = trace_root(tree[nxt[t->value]]); n       else n         T = NULL; n       if (T) { n         splay(t, NULL); n         link(t, T, 1); n       } n     } n   } n   std::cout << ans << std::endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e4 + 5; n int a[MAXN]; n int dp[MAXN][2005]; n int solve(int n) { n   int len = 2005; n   for (int i = 1; i <= n; ++i) n     for (int j = 0; j < len; ++j) dp[i][j] = INT_MAX; n   for (int j = 0; j < len; ++j) dp[0][j] = j; n   for (int i = 1; i <= n; ++i) { n     for (int j = 0; j < len; ++j) { n       if (j >= a[i]) dp[i][j - a[i]] = min(dp[i][j - a[i]], dp[i - 1][j]); n       if (j + a[i] < len) n         dp[i][j + a[i]] = min(dp[i][j + a[i]], max(dp[i - 1][j], j + a[i])); n     } n   } n   int ans = INT_MAX; n   for (int j = 0; j < len; ++j) ans = min(ans, dp[n][j]); n   return ans; n } n int main() { n   int n, t; n   cin >> t; n   while (t--) { n     cin >> n; n     for (int i = 1; i <= n; ++i) cin >> a[i]; n     cout << solve(n) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = (int)1e5 + 1; n char s[maxn]; n const int ci = 26; n struct vertex { n   vertex *go[ci]; n   vertex *suf; n   bool term; n   int len; n   int d[2]; n   vertex(int _len = 0) : suf(0), term(0), len(_len) { n     for (int i = 0; i < ci; i++) go[i] = 0; n   } n }; n vertex vs[2 * maxn]; n vertex *last; n int m; n void addch(int ch) { n   vs[m++] = vertex(); n   vertex *newlast = &vs[m - 1]; n   newlast->len = last->len + 1; n   for (; last && !last->go[ch]; last = last->suf) last->go[ch] = newlast; n   if (!last) n     newlast->suf = &vs[0]; n   else { n     vertex *next = last->go[ch]; n     if (last->len + 1 == next->len) n       newlast->suf = next; n     else { n       vs[m++] = *next; n       vertex *clone = &vs[m - 1]; n       clone->len = last->len + 1; n       next->suf = clone; n       newlast->suf = clone; n       for (; last && last->go[ch] == next; last = last->suf) n         last->go[ch] = clone; n     } n   } n   last = newlast; n } n int checked[maxn]; n void dfscheck(vertex *v, string s, string now) { n   if (v->term) { n     assert(now == string(s, (int)s.length() - (int)now.length())); n     checked[(int)now.length()]++; n   } n   for (int i = 0; i < ci; i++) n     if (v->go[i]) dfscheck(v->go[i], s, now + (char)( a  + i)); n } n vertex *fivs[2 * maxn]; n const int maxl = 2 * maxn; n int cnt[maxl]; n int head[maxl]; n int main() { n   while (scanf( %s , s) >= 1) { n     m = 0; n     vs[m++] = vertex(); n     last = &vs[0]; n     int n = strlen(s); n     for (int i = 0; i < n; i++) { n       addch(s[i] -  a ); n     } n     for (vertex *now = last; now; now = now->suf) now->term = 1; n     memset(cnt, 0, sizeof(cnt)); n     for (int i = 0; i < m; i++) cnt[vs[i].len]++; n     head[0] = 0; n     for (int i = 1; i < m; i++) head[i] = head[i - 1] + cnt[i - 1]; n     for (int i = 0; i < m; i++) fivs[head[vs[i].len]++] = &vs[i]; n     reverse(fivs, fivs + m); n     for (int i = 0; i < m; i++) { n       vertex *v = fivs[i]; n       v->d[0] = (int)v->term; n       for (int j = 0; j < ci; j++) n         if (v->go[j]) v->d[0] += v->go[j]->d[0]; n       v->d[1] = 0; n     } n     fivs[m - 1]->d[1] = 1; n     for (int i = m - 1; i >= 0; i--) { n       vertex *v = fivs[i]; n       for (int j = 0; j < ci; j++) n         if (v->go[j]) v->go[j]->d[1] += v->d[1]; n     } n     long long ans = 0; n     for (int i = 0; i < m - 1; i++) { n       int f = fivs[i]->d[0], s = fivs[i]->d[1]; n       ans += (long long)s * (long long)f * (f + 1) / 2; n     } n     printf( n          %lld  n           n , n         ans); n   } n   return 0; 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   long long tc; n   cin >> tc; n   while (tc--) { n     long long n, m; n     cin >> n >> m; n     vector<long long> p(n); n     for (long long z = 0; (1 > 0 and z < n) or (1 < 0 and z > n); z += 1) n       cin >> p[z]; n     ; n     function<long long(long long)> num_swaps = [&](long long k) { n       vector<long long> b(n); n       for (long long i = 0; (1 > 0 and i < n) or (1 < 0 and i > n); i += 1) n         b[i] = (i - k + n) % n + 1; n       vector<vector<long long> > g(n, vector<long long>()); n       ; n       for (long long i = 0; (1 > 0 and i < n) or (1 < 0 and i > n); i += 1) { n         g[p[i] - 1].push_back(b[i] - 1); n         g[b[i] - 1].push_back(p[i] - 1); n       } n       vector<bool> v(n, false); n       function<void(long long)> dfs = [&](long long rt) { n         v[rt] = true; n         for (auto ch : g[rt]) n           if (!v[ch]) dfs(ch); n       }; n       long long c = 0; n       for (long long i = 0; (1 > 0 and i < n) or (1 < 0 and i > n); i += 1) n         if (!v[i]) { n           dfs(i); n           ++c; n         } n       return n - c; n     }; n     vector<long long> cnt(n, 0); n     for (long long i = 0; (1 > 0 and i < n) or (1 < 0 and i > n); i += 1) n       ++cnt[(i + 1 - p[i] + n) % n]; n     vector<long long> ans; n     for (long long k = 0; (1 > 0 and k < n) or (1 < 0 and k > n); k += 1) n       if (cnt[k] >= n - m * 2 and num_swaps(k) <= m) ans.push_back(k); n     cout << ans.size(); n     for (auto x : ans) cout <<     << x; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 50; n long long a[MAXN], b[MAXN], dp[MAXN]; n long long la[MAXN], lb[MAXN]; n int sz, cur, n; n double cross(int x, int y) { return (double)(lb[x] - lb[y]) / (la[y] - la[x]); } n void newLine(long long p, long long q) { n   la[sz] = p; n   lb[sz] = q; n   while (sz > 1 && cross(sz - 1, sz - 2) > cross(sz, sz - 1)) { n     la[sz - 1] = la[sz]; n     lb[sz - 1] = lb[sz]; n     sz--; n   } n   sz++; n } n long long find(long long x) { n   while (cur + 1 < sz && x > cross(cur, cur + 1)) cur++; n   return la[cur] * x + lb[cur]; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) cin >> b[i]; n   dp[1] = 0; n   newLine(b[1], 0); n   for (int i = 2; i <= n; i++) { n     dp[i] = find(a[i]); n     newLine(b[i], dp[i]); n   } n   cout << dp[n]; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int tc; n   cin >> tc; n   while (tc--) { n     int n, m, x1, y1, x2, y2, w, h; n     cin >> n >> m >> x1 >> y1 >> x2 >> y2 >> w >> h; n     int right = n - x2; n     int left = x1; n     int up = m - y2; n     int down = y1; n     int mn = 1e9; n     int w1 = x2 - x1; n     int h1 = y2 - y1; n     if (w1 + w <= n) { n       mn = min(mn, max(0, w - right)); n       mn = min(mn, max(0, w - left)); n     } n     if (h1 + h <= m) { n       mn = min(mn, max(0, h - up)); n       mn = min(mn, max(0, h - down)); n     } n     if (mn == 1e9) mn = -1; n     cout << mn <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n vector<int> E; n vector<int> a[N]; n int value[N]; n long long down[N]; n long long max_path[N]; n int n; n long long calc_down(int edge_index) { n   long long &ans = down[edge_index]; n   if (ans != -1) return ans; n   int v = E[edge_index]; n   ans = value[v]; n   for (int e : a[v]) n     if (e != (edge_index ^ 1)) ans = max(ans, calc_down(e) + value[v]); n   return ans; n } n long long calc_max_path(int edge_index) { n   long long &ans = max_path[edge_index]; n   if (ans != -1) return ans; n   int v = E[edge_index]; n   long long first = 0, second = 0; n   for (int e : a[v]) n     if (e != (edge_index ^ 1)) { n       long long cur = calc_down(e); n       if (first <= cur) n         second = first, first = cur; n       else if (second < cur) n         second = cur; n       ans = max(ans, calc_max_path(e)); n     } n   return ans = max(ans, first + second + value[v]); n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> value[i]; n   for (int i = 1; i < n; ++i) { n     int u, v; n     cin >> u >> v; n     a[u].push_back(E.size()); n     E.push_back(v); n     a[v].push_back(E.size()); n     E.push_back(u); n   } n   memset(down, -1, sizeof down); n   memset(max_path, -1, sizeof max_path); n   long long ans = 0; n   for (int i = 0; i < E.size(); i += 2) n     ans = max(ans, calc_max_path(i) + calc_max_path(i ^ 1)); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<int> v(n), inc(n, -1), dec(n, n - 1); n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n     if (i > 0) { n       if (v[i] > v[i - 1]) n         inc[i] = i; n       else n         inc[i] = inc[i - 1]; n     } n   } n   for (int i = n - 2; i >= 0; i--) { n     if (v[i] > v[i + 1]) n       dec[i] = i; n     else n       dec[i] = dec[i + 1]; n   } n   for (int i = 0; i < m; i++) { n     int l, r; n     cin >> l >> r; n     l--; n     r--; n     if (dec[l] >= inc[r]) n       cout <<  Yes n ; n     else n       cout <<  No n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   int n, b, a; n   cin >> n >> b >> a; n   int a_cap = a; n   int x, c = 0; n   while (a + b) { n     cin >> x; n     if (!x) { n       if (a) n         a--; n       else n         b--; n     } n     if (x) { n       if (b) { n         if (a == a_cap) n           a--; n         else n           b--, a++; n       } else { n         a--; n       } n     } n     c++; n     if (c == n) { n       break; n     } n   } n   cout << c; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   string l; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> l; n     int a = l.length(); n     if (a > 10) { n       cout << l[0] << a - 2 << l[a - 1] << endl; n     } else { n       cout << l << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n auto fastio = (ios::sync_with_stdio(false), cin.tie(nullptr)); n void solve() { n   long long n, m; n   cin >> n >> m; n   string s; n   cin >> s; n   vector<long long> p(m); n   for (long long i = 0; i < (m); ++i) { n     cin >> p[i]; n   } n   sort((p).begin(), (p).end()); n   vector<long long> cnt(27); n   long long ptr = 0; n   for (long long i = 0; i < (n); ++i) { n     while (i == p[ptr]) { n       m--; n       ptr++; n     } n     cnt[s[i] -  a ] += (m ? m + 1 : 1); n   } n   for (long long i = 0; i < (26); ++i) { n     if (i) { n       cout <<    ; n     } n     cout << cnt[i]; n   } n   cout <<   n ; n } n int32_t main() { n   long long tt; n   cin >> tt; n   while (tt--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n using vv = vector<vector<T>>; n template <class T> n ostream &operator<<(ostream &os, const vector<T> &t) { n   os <<  { ; n   for (int(i) = 0; (i) < (t.size()); ++(i)) { n     os << t[i] <<  , ; n   } n   os <<  }  << endl; n   return os; n } n template <class S, class T> n ostream &operator<<(ostream &os, const pair<S, T> &t) { n   return os <<  (  << t.first <<  ,  << t.second <<  ) ; n } n const long long MOD = 1e9 + 9; n const int N = (1 << 17), r = 11451; n struct Seg { n   long long h, q, r; n   Seg(int v = 0) { n     h = v; n     q = 0; n     r = (v ? ::r : 1); n   } n   void comp(const Seg &ope) { q = ope.q; } n   void clear() { q = 0; } n   static Seg Ope(int v = 0) { n     Seg re; n     re.q = v; n     return re; n   } n   static Seg e; n }; n Seg Seg::e = Seg(); n ostream &operator<<(ostream &os, const Seg &t) { n   return os <<  (  << t.h <<  ,  << t.q <<  ,  << t.r <<  ) ; n } n Seg seg[2 * N + 10]; n Seg operator+(const Seg l, const Seg r) { n   Seg re; n   re.h = (l.h * r.r % MOD + r.h) % MOD; n   re.r = l.r * r.r % MOD; n   return re; n } n vector<long long> ser(N); n inline void lazy_eval(int k, int a, int b) { n   if (!seg[k].q) return; n   seg[k].h = seg[k].q * ser[b - a - 1] % MOD; n   if (2 * k <= 2 * N + 5) { n     seg[2 * k + 1].comp(seg[k]); n     seg[2 * k + 2].comp(seg[k]); n   } n   seg[k].clear(); n } n inline void update_node(int k) { seg[k] = seg[2 * k + 1] + seg[2 * k + 2]; } n void update(int l, int r, Seg ope, int k = 0, int a = 0, int b = N) { n   lazy_eval(k, a, b); n   if (b <= l || r <= a) return; n   if (l <= a && b <= r) { n     seg[k].comp(ope); n     lazy_eval(k, a, b); n     return; n   } n   int m = (a + b) / 2; n   update(l, r, ope, 2 * k + 1, a, m); n   update(l, r, ope, 2 * k + 2, m, b); n   update_node(k); n } n Seg get(int l, int r, int k = 0, int a = 0, int b = N) { n   lazy_eval(k, a, b); n   if (b <= l || r <= a) return Seg::e; n   if (l <= a && b <= r) { n     return seg[k]; n   } n   int m = (a + b) / 2; n   Seg vl = get(l, r, 2 * k + 1, a, m); n   Seg vr = get(l, r, 2 * k + 2, m, b); n   update_node(k); n   return vl + vr; n } n int main() { n   ios_base::sync_with_stdio(false); n   cout << fixed << setprecision(0); n   ser[0] = 1; n   for (int(i) = 0; (i) < (N - 1); ++(i)) n     ser[i + 1] = (1 + ser[i] * r % MOD) % MOD; n   int n, m, t; n   cin >> n >> m >> t; n   string str; n   cin >> str; n   int a, l, r, x; n   for (int(i) = 0; (i) < (n); ++(i)) seg[(1 << 17) - 1 + i] = Seg(str[i]); n   for (int(i) = ((1 << 17) - 1) - 1; (i) >= 0; --(i)) update_node(i); n   for (int(i) = 0; (i) < (t + m); ++(i)) { n     cin >> a >> l >> r >> x; n     --l; n     if (a == 1) { n       update(l, r, Seg::Ope(x +  0 )); n     } else { n       cout << (get(l, r - x).h == get(l + x, r).h ?  YES  :  NO ) << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n const long long INF = mod * mod; n const long double eps = 1e-8; n void solve() { n   int n; n   cin >> n; n   vector<long long> val(n), mask(n); n   long long sum = 0; n   for (int i = 0; i < n; i++) { n     cin >> val[i] >> mask[i]; n     sum += val[i]; n   } n   if (sum < 0) { n     for (int i = 0; i < n; i++) { n       val[i] = -val[i]; n       sum = -sum; n     } n   } n   long long ans = 0; n   for (int i = 0; i < 62; i++) { n     long long k = (long long)1 << i; n     long long csum = 0; n     for (int j = 0; j < n; j++) { n       if (mask[j] < 2 * k && (mask[j] & k)) { n         csum += val[j]; n       } n     } n     if (csum > 0) { n       ans += k; n       for (int j = 0; j < n; j++) { n         if (mask[j] & k) { n           val[j] = -val[j]; n         } n       } n     } n   } n   cout << ans << endl; n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout << fixed << setprecision(10); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int xor_sum(int x) { n   if (x % 4 == 0) { n     return x; n   } n   if (x % 4 == 1) { n     return 1; n   } n   if (x % 4 == 2) { n     return x + 1; n   } n   if (x % 4 == 3) { n     return 0; n   } n   return -1; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   long long tmp, q, tmp2 = 0; n   vector<int> v(n); n   for (int i = 0; i < n; ++i) { n     cin >> v[i]; n   } n   for (int i = 1; i <= n; ++i) { n     tmp = xor_sum(i - 1); n     if (!(n / i % 2)) { n       tmp ^= xor_sum(i - 1); n     } n     tmp ^= xor_sum(n % i); n     tmp ^= v[i - 1]; n     if (i == 1) { n       q = tmp; n     } else { n       q ^= tmp; n     } n   } n   cout << q <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline char getc() { n   static char buf[1 << 20], *p1 = buf, *p2 = buf; n   return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) n              ? EOF n              : *p1++; n } n template <typename T> n void rd(T& x) { n   int f = 0, c; n   while (!isdigit(c = getc())) f ^= !(c ^ 45); n   x = (c & 15); n   while (isdigit(c = getc())) x = x * 10 + (c & 15); n   if (f) x = -x; n } n template <typename T> n void pt(T x, int c = -1) { n   if (x < 0) putchar( - ), x = -x; n   if (x > 9) pt(x / 10); n   putchar(x % 10 + 48); n   if (c != -1) putchar(c); n } n const int N = 200005; n int n, m, Q, dp[N]; n long long a[N]; n namespace xay1 { n void sol() { n   while (Q--) { n     int t; n     rd(t); n     if (t == 1) { n       int l, r, d; n       rd(l), rd(r), rd(d); n       if (d & 1) n         for (int i = (l); i <= (r); ++i) a[i] ^= 1; n     } else { n       int l, r; n       rd(l), rd(r); n       int s = 0; n       for (int i = (r); i >= (l); --i) { n         dp[i] = a[i] & 1; n         if (s) dp[i] = 0; n         s = (s << 1 | dp[i]) & ((1 << m) - 1); n       } n       if (dp[l]) n         puts( 2 ); n       else n         puts( 1 ); n     } n   } n } n }  // namespace xay1 n namespace xay2 { n struct node { n   int w[22], len; n } t[N * 4][2]; n int tg[N * 4]; n void pst(int k1) { tg[k1] ^= 1, swap(t[k1][0], t[k1][1]); } n void psd(int k1) { n   if (tg[k1]) { n     pst(k1 * 2); n     pst(k1 * 2 + 1); n     tg[k1] = 0; n   } n } n node mer(const node& k1, const node& k2) { n   node k3; n   k3.len = k1.len + k2.len; n   for (int i = (1); i <= (m + 1); ++i) { n     if (k2.w[i] == -1) { n       if (k1.w[min(m + 1, i + k2.len)] == -1) { n         k3.w[i] = -1; n       } else n         k3.w[i] = k1.w[min(m + 1, i + k2.len)]; n     } else { n       if (k1.w[k2.w[i]] == -1) { n         k3.w[i] = min(m + 1, k1.len + k2.w[i]); n       } else n         k3.w[i] = k1.w[k2.w[i]]; n     } n   } n   return k3; n } n void upd(int k1) { n   t[k1][0] = mer(t[k1 * 2][0], t[k1 * 2 + 1][0]); n   t[k1][1] = mer(t[k1 * 2][1], t[k1 * 2 + 1][1]); n } n void bud(int k1, int k2, int k3) { n   if (k2 == k3) { n     t[k1][0].len = t[k1][1].len = 1; n     if (a[k2] & 1) { n       for (int i = (1); i <= (m); ++i) t[k1][0].w[i] = -1; n       t[k1][0].w[m + 1] = 1; n       for (int i = (1); i <= (m + 1); ++i) t[k1][1].w[i] = -1; n     } else { n       for (int i = (1); i <= (m); ++i) t[k1][1].w[i] = -1; n       t[k1][1].w[m + 1] = 1; n       for (int i = (1); i <= (m + 1); ++i) t[k1][0].w[i] = -1; n     } n     return; n   } n   psd(k1); n   int mid = (k2 + k3) >> 1; n   bud(k1 * 2, k2, mid), bud(k1 * 2 + 1, mid + 1, k3); n   upd(k1); n } n void mdf(int k1, int k2, int k3, int k4, int k5) { n   if (k2 > k5 || k3 < k4) return; n   if (k4 <= k2 && k3 <= k5) { n     pst(k1); n     return; n   } n   psd(k1); n   int mid = (k2 + k3) >> 1; n   mdf(k1 * 2, k2, mid, k4, k5), mdf(k1 * 2 + 1, mid + 1, k3, k4, k5); n   upd(k1); n } n int res; n void ask(int k1, int k2, int k3, int k4, int k5) { n   if (k2 > k5 || k3 < k4) return; n   if (k4 <= k2 && k3 <= k5) { n     if (t[k1][0].w[res] == -1) { n       res = min(m + 1, res + t[k1][0].len); n     } else { n       res = t[k1][0].w[res]; n     } n     return; n   } n   psd(k1); n   int mid = (k2 + k3) >> 1; n   ask(k1 * 2 + 1, mid + 1, k3, k4, k5); n   ask(k1 * 2, k2, mid, k4, k5); n } n void sol() { n   bud(1, 1, n); n   while (Q--) { n     int t; n     rd(t); n     if (t == 1) { n       int l, r; n       long long d; n       rd(l), rd(r), rd(d); n       if (d & 1) { n         mdf(1, 1, n, l, r); n       } n     } else { n       int l, r; n       rd(l), rd(r); n       res = m + 1; n       ask(1, 1, n, l, r); n       if (res == 1) n         puts( 2 ); n       else n         puts( 1 ); n     } n   } n } n }  // namespace xay2 n int main() { n   rd(n), rd(m), rd(Q); n   for (int i = (1); i <= (n); ++i) rd(a[i]), a[i] &= 1; n   if (n <= 5000 && m <= 5000) { n     xay1::sol(); n   } else { n     xay2::sol(); n   } n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n typedef long long ll; n typedef pair<int,int> pii; n const int MAXN = 6 + 5; n const int MAXM = 6 + 5; n const int base = 5; n const int ALL = 1.6e4 + 5; n const int inf = 0x3f3f3f3f; n  n int pwb[MAXM]; n  n int n,m; n int a[MAXN], b[MAXM], c[MAXN][MAXM]; n int f[MAXN][ALL]; n  n inline int encode(int t[]) n { n  int res = 0; n  for(int i=m; i>=1; --i) n   res = res * base + t[i]; n  return res; n } n  n inline int get_dig(int mask,int k) n { n  return mask / pwb[k - 1] % base; n } n  n inline int reduce(int mask,int k,int x) n { n  return mask - x * pwb[k - 1]; n } n  n inline void dfs_f(int dep,int mask,int curc,int rem,int lvl) n { n  if(dep > m) n  { n   if(!rem) f[lvl][mask] = min(f[lvl][mask], curc); n   return; n  } n   n  dfs_f(dep+1, mask, curc, rem, lvl); n   n  curc += c[lvl][dep]; n  int lim = min(rem, get_dig(mask, dep)); n   n  for(int i=1; i<=lim; ++i) n   dfs_f(dep+1, reduce(mask, dep, i), curc, rem - i, lvl); n } n  n int main(void) n { n  pwb[0] = 1; n  for(int i=1; i<MAXM; ++i) pwb[i] = pwb[i-1] * base; n   n  scanf( %d%d ,&n,&m); n  for(int i=1; i<=n; ++i) scanf( %d ,&a[i]); n  for(int i=1; i<=m; ++i) scanf( %d ,&b[i]); n  for(int i=1; i<=n; ++i) n   for(int j=1; j<=m; ++j) scanf( %d ,&c[i][j]); n   n  int tot = 0; n  for(int i=1; i<=n; ++i) tot += a[i]; n  for(int i=1; i<=m; ++i) tot -= b[i]; n  if(tot > 0) return printf( -1 ), 0; n   n  int all = pwb[m] - 1; n  memset(f,0x3f,sizeof(f)); n  f[0][encode(b)] = 0; n  for(int i=1; i<=n; ++i) n  { n   for(int mask=0; mask<=all; ++mask) n    if(f[i-1][mask] < inf) dfs_f(1, mask, f[i-1][mask], a[i], i); n  } n   n  int res = *min_element(f[n], f[n]+all+1); n  if(res == inf) printf( -1 ); n  else printf( %d ,res); n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200200; n int n; n long long b[N], c[N]; n long long a[N]; n int dg[32]; n void solve() { n   memset((dg), (0), sizeof(dg)); n   long long sum = 0, suma = 0; n   for (int i = (int)0; i < (int)n; ++i) sum += b[i] + c[i]; n   suma = sum / (n + n); n   bool flag = 1; n   for (int i = (int)0; i < (int)n; ++i) { n     a[i] = (b[i] + c[i] - suma) / n; n     if (a[i] < 0) flag = 0; n   } n   if (!flag) { n     cout <<  -1 ; n     return; n   } n   for (int i = (int)0; i < (int)n; ++i) { n     int t = 0; n     for (long long x = a[i]; x; x >>= 1) dg[t++] += (x & 1); n   } n   for (int i = (int)0; i < (int)n; ++i) { n     long long x = a[i], sumi = 0; n     for (int j = (int)0; j < (int)32; ++j) n       if ((x >> j) & 1) sumi += (1 << j) * dg[j]; n     if (sumi != b[i]) { n       flag = 0; n       break; n     } n   } n   if (!flag) { n     cout <<  -1 ; n     return; n   } n   for (int i = (int)0; i < (int)n; ++i) { n     long long x = a[i], sumi = 0; n     for (int j = (int)0; j < (int)32; ++j) { n       if ((x >> j) & 1) n         sumi += (1 << j) * n; n       else n         sumi += (1 << j) * dg[j]; n     } n     if (sumi != c[i]) { n       flag = 0; n       break; n     } n   } n   if (!flag) { n     cout <<  -1 ; n     return; n   } n   for (int i = (int)0; i < (int)n; ++i) cout << a[i] <<    ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   for (int i = (int)0; i < (int)n; ++i) cin >> b[i]; n   for (int i = (int)0; i < (int)n; ++i) cin >> c[i]; n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (1 << 30) - 1; n const long double EPS = 1e-9; n const long double PI = fabs(atan2(0.0, -1.0)); n int get(int n) { n   int res = 0; n   for (int b = 0; n > 0; b++) { n     if (n & 1) res = b; n     n >>= 1; n   } n   return res; n } n int p[1000100]; n void solve() { n   int n; n   cin >> n; n   vector<bool> was(n + 1, false); n   for (int i = n; i >= 0; i--) { n     for (int k = 23; k >= 0; k--) { n       int cur = (1 << k) - 1; n       int add = cur - i; n       if (add < 0 || add > n) continue; n       if (was[add]) continue; n       was[add] = true; n       p[i] = add; n       break; n     } n   } n   printf( n        %lld  n         n , n       n * 1LL * (n + 1)); n   for (int i = 0; i <= n; i++) { n     if (i > 0) printf(   ); n     printf( %d , p[i]); n   } n   printf(  n ); n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   map<long long, long long> mp; n   deque<long long> a; n   long long n, k, d, m = 0; n   cin >> n >> k; n   for (auto i = 0; i < n; i++) { n     cin >> d; n     if (!mp[d]) { n       mp[d] = 1; n       a.push_front(d); n       if (a.size() > k) { n         mp[a.back()] = 0; n         a.pop_back(); n       } n     } n   } n   cout << a.size() <<   n ; n   for (const auto val : a) cout << val <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n template <typename T> n using v2 = vector<vector<T>>; n template <typename T> n inline v2<T> fill(int r, int c, const T& t) { n   return v2<T>(r, vector<T>(c, t)); n } n ll a, b, c, d; n const ll INF = 1e18; n ll nc2(ll n) { return (n * (n - 1)) / 2; } n ll diff(ll n) { return a - b * d * (nc2(n) - nc2(n - 1)); } n ll value(ll n) { n   ll x = nc2(n); n   if (b * x > INF / d) { n     return -INF; n   } n   return a * n - b * d * x; n } n void solve() { n   cin >> a >> b >> c >> d; n   if (a > b * c) { n     cout <<  -1 n ; n   } else { n     ll lo = 1; n     ll hi = c; n     ll ans = -INF; n     while (lo <= hi) { n       ll mid = (lo + hi) / 2; n       if (diff(mid) <= 0) { n         hi = mid - 1; n       } else { n         lo = mid + 1; n       } n       ans = max(ans, value(mid)); n     } n     cout << ans <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int T; n   cin >> T; n   for (int i = 0; i < T; ++i) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, l, r; n long long ans; n int pr[10000005]; n int cnt[10000005]; n void solve() { n   n--; n   for (int i = (1); i <= (10000000); i++) pr[i] = i; n   for (int i = (2); i <= (10000000); i++) n     if (pr[i] == i) { n       for (int j = i + i; j <= 10000000; j += i) { n         if (pr[j] == j) pr[j] = i; n       } n     } n   for (int i = (l); i <= (r); i++) { n     int temp = 1, num = i, tc = 0, tp = 1; n     while (num > 1) { n       int np = pr[num]; n       if (np == tp) { n         tc++; n         if (tc >= n) tc = 0; n       } else { n         for (int j = (1); j <= (tc); j++) temp *= tp; n         tp = np; n         tc = 1 % n; n       } n       num /= np; n     } n     for (int j = (1); j <= (tc); j++) temp *= tp; n     cnt[temp]++; n   } n   for (int i = (1); i <= (r); i++) ans += 1LL * cnt[i] * (cnt[i] - 1); n   printf( %lld n , ans); n } n int main() { n   scanf( %d %d %d , &n, &l, &r); n   if (n == 1) n     printf( %d n , r - l + 1); n   else n     solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 200005; n int n, f[M], ans; n int tot, head[M], Next[M * 2], vet[M * 2], len[M * 2]; n void add(int a, int b, int c) { n   Next[++tot] = head[a], vet[tot] = b, len[tot] = c; n   head[a] = tot; n } n void dfs1(int x, int pre) { n   for (int i = head[x]; i; i = Next[i]) { n     int y = vet[i], z = len[i]; n     if (y == pre) continue; n     dfs1(y, x); n     f[x] += f[y] + z; n   } n } n void dfs2(int x, int pre) { n   for (int i = head[x]; i; i = Next[i]) { n     int y = vet[i], z = len[i]; n     if (y == pre) continue; n     if (z) n       z = -1; n     else n       z = 1; n     f[y] = f[x] + z; n     ans = min(ans, f[y]); n     dfs2(y, x); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 2; i <= n; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     add(a, b, 0); n     add(b, a, 1); n   } n   dfs1(1, 0); n   ans = f[1]; n   dfs2(1, 0); n   printf( %d n , ans); n   for (int i = 1; i <= n; i++) n     if (f[i] == ans) printf( %d  , i); n   return 0; n } n


#include <bits/stdc++.h> n const int N = 2e5 + 5; n const int inf = 2e9; n struct Edge { n   int u, v, cost, isin; n   int ind; n   bool operator<(const Edge& x) const { return cost < x.cost; } n }; n std::vector<Edge> e; n std::vector<std::pair<int, int> > l[N]; n int tata[N], sz[N], ans[N]; n int findDaddy(int node) { n   if (node == tata[node]) return node; n   return tata[node] = findDaddy(tata[node]); n } n bool areConnected(int u, int v) { return (findDaddy(u) == findDaddy(v)); } n void connect(int u, int v) { n   if (sz[findDaddy(u)] > sz[findDaddy(v)]) { n     sz[findDaddy(u)] += sz[findDaddy(v)]; n     tata[findDaddy(v)] = findDaddy(u); n   } else { n     sz[findDaddy(v)] += sz[findDaddy(u)]; n     tata[findDaddy(u)] = findDaddy(v); n   } n } n int rmq[21][N], mx[21][N], h[N], mn[N]; n void dfs(int node, int from = 0, int c = 0) { n   rmq[0][node] = from, mx[0][node] = c, h[node] = h[from] + 1; n   for (std::pair<int, int> to : l[node]) n     if (to.first != from) dfs(to.first, node, to.second); n } n std::pair<int, int> maxlca(int from, int to) { n   int sol = 0; n   if (h[from] < h[to]) std::swap(from, to); n   for (int i = 20; i >= 0; i--) n     if (h[from] - (1 << i) >= h[to]) { n       sol = (((sol) > (mx[i][from])) ? (sol) : (mx[i][from])); n       from = rmq[i][from]; n     } n   if (from == to) return {from, sol}; n   for (int i = 20; i >= 0; i--) n     if (rmq[i][from] != rmq[i][to]) { n       sol = (((sol) > n               ((((mx[i][from]) > (mx[i][to])) ? (mx[i][from]) : (mx[i][to])))) n                  ? (sol) n                  : ((((mx[i][from]) > (mx[i][to])) ? (mx[i][from]) n                                                    : (mx[i][to])))); n       from = rmq[i][from], to = rmq[i][to]; n     } n   sol = n       (((sol) > ((((mx[0][from]) > (mx[0][to])) ? (mx[0][from]) : (mx[0][to])))) n            ? (sol) n            : ((((mx[0][from]) > (mx[0][to])) ? (mx[0][from]) : (mx[0][to])))); n   return {rmq[0][from], sol}; n } n void update(int from, int to, int cost) { n   to = findDaddy(to), from = findDaddy(from); n   while (from != to) { n     if (h[from] < h[to]) std::swap(from, to); n     mn[from] = cost - 1; n     tata[from] = rmq[0][from]; n     from = findDaddy(from); n   } n } n int main() { n   int n, m, from, to, cost; n   std::cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     std::cin >> from >> to >> cost; n     e.push_back({from, to, cost, 0, i}); n   } n   for (int i = 1; i <= n; i++) tata[i] = i, sz[i] = 1; n   std::sort(e.begin(), e.end()); n   for (Edge& curr : e) { n     if (!areConnected(curr.u, curr.v)) { n       l[curr.u].push_back({curr.v, curr.cost}); n       l[curr.v].push_back({curr.u, curr.cost}); n       connect(curr.u, curr.v); n       curr.isin = 1; n     } n   } n   dfs(1); n   for (int i = 1; i <= 20; i++) n     for (int j = 1; j <= n; j++) { n       rmq[i][j] = rmq[i - 1][rmq[i - 1][j]]; n       mx[i][j] = (((mx[i - 1][j]) > (mx[i - 1][rmq[i - 1][j]])) n                       ? (mx[i - 1][j]) n                       : (mx[i - 1][rmq[i - 1][j]])); n     } n   for (Edge& curr : e) n     if (!curr.isin) { n       std::pair<int, int> x = maxlca(curr.u, curr.v); n       ans[curr.ind] = x.second - 1; n     } n   for (int i = 1; i <= n; i++) tata[i] = i, mn[i] = inf; n   for (Edge& curr : e) n     if (!curr.isin) update(curr.u, curr.v, curr.cost); n   for (Edge& curr : e) { n     if (!curr.isin) continue; n     if (h[curr.u] < h[curr.v]) std::swap(curr.u, curr.v); n     ans[curr.ind] = -1; n     if (rmq[0][curr.u] == curr.v and mn[curr.u] != inf) n       ans[curr.ind] = mn[curr.u]; n   } n   for (int i = 1; i <= m; i++) std::cout << ans[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n char ok[100] = { A ,  H ,  I ,  M ,  O ,  T ,  U ,  V ,  W ,  X ,  Y }; n int main() { n   while (cin >> s) { n     if (s.length() == 1) { n       int flag = 0; n       for (int i = 0; i <= 10; i++) { n         if (s[0] == ok[i]) { n           puts( YES ); n           flag = 1; n           break; n         } n       } n       if (!flag) puts( NO ); n       continue; n     } else { n       int flag = 1, half = s.length() / 2, flag1 = 0; n       int len = s.length(); n       for (int i = 0; i < half; i++) { n         if (s[i] != s[len - 1 - i]) { n           flag = 0; n           break; n         } else { n           int x = 0; n           for (int j = 0; j <= 10; j++) { n             x += s[i] == ok[j]; n           } n           if (x == 0) { n             flag = 0; n             break; n           } n         } n       } n       if (len % 2 == 1) { n         for (int i = 0; i <= 10; i++) { n           if (s[half] == ok[i]) { n             flag1 = 1; n             break; n           } n         } n       } else n         flag1 = 1; n       printf( %s n , flag == 1 && flag1 == 1 ?  YES  :  NO ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long int, long long int> m; n int main() { n   long long int n; n   cin >> n; n   vector<long long int> v; n   for (long long int i = 0; i < n; i++) { n     long long int x; n     cin >> x; n     v.emplace_back(x); n     m[x]++; n   } n   sort(v.begin(), v.end()); n   long long int ans = 0; n   for (long long int i = 0; i < n; i++) { n     ans = max(ans, m[v[i]]); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int a = 0, b = 0, c = 0, sum1 = 0, d = 0; n   cin >> a >> b >> c >> d; n   for (; sum1 <= d;) { n     sum1 += b; n     a -= c; n   } n   if (a > 0) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long minOf(long long x, long long y) { return (x < y ? x : y); } n inline long long maxOf(long long x, long long y) { return (x > y ? x : y); } n char arr[50000000], *ptr; n long long ret; n string str; n inline long long get_int() { n   bool isNeg = false; n   ret = 0; n   while (!((*ptr >=  0  && *ptr <=  9 ) || *ptr ==  - )) ptr++; n   while ((*ptr >=  0  && *ptr <=  9 ) || *ptr ==  - ) { n     if (*ptr ==  - ) n       isNeg = true; n     else n       ret = ret * 10 + (*ptr -  0 ); n     ptr++; n   } n   if (isNeg) ret = -ret; n   return ret; n } n long long a[100005]; n int bits[33]; n int main() { n   fread(arr, sizeof(char), 50000000, stdin); n   ptr = arr; n   int n = get_int(); n   for (int i = 0; i < 33; i++) bits[i] = -1; n   for (int i = 0; i < n; i++) { n     a[i] = get_int(); n     for (int j = 0; j < 32; j++) { n       if (a[i] & (1 << j)) { n         if (bits[j] == -1) n           bits[j] = a[i]; n         else n           bits[j] = a[i] & bits[j]; n       } n     } n   } n   int k = 0; n   vector<int> list; n   int lk = 0; n   for (int i = 32; i >= 0; i--) { n     if (bits[i] != -1) { n       for (int j = 0; j < 32; j++) { n         if (bits[i] & (1 << j)) { n           if (j > lk) lk = j; n           break; n         } n       } n     } n   } n   for (int j = 0; j < n; j++) { n     if (a[j] & (1 << lk)) { n       k++; n       list.push_back(a[j]); n     } n   } n   cout << k << endl; n   for (int i = 0; i < k; i++) { n     cout << list[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using vi = vector<int>; n using vvi = vector<vi>; n using vll = vector<ll>; n using vvll = vector<vll>; n using vb = vector<bool>; n using vd = vector<double>; n using vs = vector<string>; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n using pdd = pair<double, double>; n using vpii = vector<pii>; n using vvpii = vector<vpii>; n using vpll = vector<pll>; n using vvpll = vector<vpll>; n using vpdd = vector<pdd>; n using vvpdd = vector<vpdd>; n template <typename T> n void ckmin(T& a, const T& b) { n   a = min(a, b); n } n template <typename T> n void ckmax(T& a, const T& b) { n   a = max(a, b); n } n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n namespace __input { n template <class T1, class T2> n void re(pair<T1, T2>& p); n template <class T> n void re(vector<T>& a); n template <class T, size_t SZ> n void re(array<T, SZ>& a); n template <class T> n void re(T& x) { n   cin >> x; n } n void re(double& x) { n   string t; n   re(t); n   x = stod(t); n } n template <class Arg, class... Args> n void re(Arg& first, Args&... rest) { n   re(first); n   re(rest...); n } n template <class T1, class T2> n void re(pair<T1, T2>& p) { n   re(p.first, p.second); n } n template <class T> n void re(vector<T>& a) { n   for (int i = 0; i < (int((a).size())); i++) re(a[i]); n } n template <class T, size_t SZ> n void re(array<T, SZ>& a) { n   for (int i = 0; i < (SZ); i++) re(a[i]); n } n }  // namespace __input n using namespace __input; n namespace __output { n template <class T1, class T2> n void pr(const pair<T1, T2>& x); n template <class T, size_t SZ> n void pr(const array<T, SZ>& x); n template <class T> n void pr(const vector<T>& x); n template <class T> n void pr(const set<T>& x); n template <class T1, class T2> n void pr(const map<T1, T2>& x); n template <class T> n void pr(const T& x) { n   cout << x; n } n template <class Arg, class... Args> n void pr(const Arg& first, const Args&... rest) { n   pr(first); n   pr(rest...); n } n template <class T1, class T2> n void pr(const pair<T1, T2>& x) { n   pr( { , x.first,  ,  , x.second,  } ); n } n template <class T, bool pretty = true> n void prContain(const T& x) { n   if (pretty) pr( { ); n   bool fst = 1; n   for (const auto& a : x) pr(!fst ? pretty ?  ,   :     :   , a), fst = 0; n   if (pretty) pr( } ); n } n template <class T> n void pc(const T& x) { n   prContain<T, false>(x); n   pr(  n ); n } n template <class T, size_t SZ> n void pr(const array<T, SZ>& x) { n   prContain(x); n } n template <class T> n void pr(const vector<T>& x) { n   prContain(x); n } n template <class T> n void pr(const set<T>& x) { n   prContain(x); n } n template <class T1, class T2> n void pr(const map<T1, T2>& x) { n   prContain(x); n } n void ps() { pr(  n ); } n template <class Arg> n void ps(const Arg& first) { n   pr(first); n   ps(); n } n template <class Arg, class... Args> n void ps(const Arg& first, const Args&... rest) { n   pr(first,    ); n   ps(rest...); n } n }  // namespace __output n using namespace __output; n namespace __algorithm { n template <typename T> n void dedup(vector<T>& v) { n   sort((v).begin(), (v).end()); n   v.erase(unique((v).begin(), (v).end()), v.end()); n } n template <typename T> n typename vector<T>::iterator find(vector<T>& v, const T& x) { n   auto it = lower_bound((v).begin(), (v).end(), x); n   return it != v.end() && *it == x ? it : v.end(); n } n template <typename T> n size_t index(vector<T>& v, const T& x) { n   auto it = find(v, x); n   assert(it != v.end() && *it == x); n   return it - v.begin(); n } n template <typename C, typename T> n vector<T> prefixes(const C& v, T zero) { n   vector<T> res(int((v).size()) + 1, zero); n   for (int i = 0; i < (int((v).size())); i++) res[i + 1] = res[i] + v[i]; n   return res; n } n template <typename C, typename T> n vector<T> suffixes(const C& v, T zero) { n   vector<T> res(int((v).size()) + 1, zero); n   for (int i = (int((v).size())) - 1; i >= 0; i--) res[i] = v[i] + res[i + 1]; n   return res; n } n }  // namespace __algorithm n using namespace __algorithm; n struct monostate { n   friend istream& operator>>(istream& is, n                              const __attribute__((unused)) monostate& ms) { n     return is; n   } n   friend ostream& operator<<(ostream& os, n                              const __attribute__((unused)) monostate& ms) { n     return os; n   } n } ms; n template <typename W = monostate> n struct wedge { n   int u, v, i; n   W w; n   wedge<W>(int _u = -1, int _v = -1, int _i = -1) : u(_u), v(_v), i(_i) {} n   int operator[](int loc) const { return u ^ v ^ loc; } n   friend void re(wedge& e) { n     re(e.u, e.v, e.w); n     --e.u, --e.v; n   } n   friend void pr(const wedge& e) { pr(e.u,  <- , e.w,  -> , e.v); } n }; n namespace __io { n void setIn(string second) { freopen(second.c_str(),  r , stdin); } n void setOut(string second) { freopen(second.c_str(),  w , stdout); } n void setIO(string second =   ) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.precision(15); n   if (int((second).size())) { n     setIn(second +  .in ), setOut(second +  .out ); n   } n } n }  // namespace __io n using namespace __io; n template <size_t N> n struct sieve { n   vi primes, least_prime, largest_proper, phi; n   sieve() : least_prime(N + 1), largest_proper(N + 1), phi(N + 1) { n     phi[1] = 1; n     for (int i = 2; i <= N; i++) { n       if (!least_prime[i]) { n         least_prime[i] = i; n         largest_proper[i] = 1; n         primes.push_back(i); n       } n       phi[i] = phi[largest_proper[i]]; n       phi[i] *= least_prime[i] - n                 int(least_prime[i] != least_prime[largest_proper[i]]); n       for (int x : primes) { n         if (x > least_prime[i] || i * x > N) break; n         least_prime[i * x] = x; n         largest_proper[i * x] = i; n       } n     } n   } n   bool is_prime(int v) { n     assert(0 < v && v < N); n     return least_prime[v] == v; n   } n   vi factor(int v) { n     assert(0 < v && v <= N); n     vi res; n     while (v > 1) { n       res.push_back(least_prime[v]); n       v = largest_proper[v]; n     } n     reverse((res).begin(), (res).end()); n     return res; n   } n   vi divisors(int v) { n     if (v == 1) return {1}; n     int p = least_prime[v], m = 0; n     while (v % p == 0) v /= p, m++; n     vi res = divisors(v), fin; n     for (int d : res) n       for (int pc = 0, t = d; pc <= m; pc++, t *= p) fin.push_back(t); n     return fin; n   } n }; n const int MAXV = 1e6 + 1; n sieve<MAXV> sv; n int main() { n   setIO(); n   int N, K; n   re(N, K); n   if (K == 1) { n     ps(3); n     return 0; n   } n   nth_element(sv.phi.begin() + 3, sv.phi.begin() + 3 + K, n               sv.phi.begin() + N + 1); n   ps(accumulate(sv.phi.begin() + 3, sv.phi.begin() + K + 3, 0ll) + 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<unsigned long long>> g; n vector<bool> visited; n vector<unsigned long long> s; n void dfs(unsigned long long x) { n   visited[x] = 1; n   for (unsigned long long i : g[x]) n     if (!visited[i]) dfs(i); n   s.push_back(x); n } n int main() { n   unsigned long long n; n   cin >> n; n   vector<unsigned long long> v(n); n   for (unsigned long long &i : v) cin >> i; n   g.assign(n, vector<unsigned long long>()); n   visited.assign(n, 0); n   for (unsigned long long i = 0; i < n; i++) { n     for (unsigned long long j = 0; j < n; j++) { n       if (v[i] * 2 == v[j]) { n         g[i].push_back(j); n       } else if (v[i] * 3 == v[j]) { n         g[j].push_back(i); n       } n     } n   } n   for (unsigned long long i = 0; i < n; i++) { n     if (!visited[i]) dfs(i); n   } n   while (!s.empty()) { n     cout << v[s.back()] <<    ; n     s.pop_back(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000010; n int n, ans[N], cnt = 1; n vector<int> id[N], cur[N]; n vector<int> g[N]; n bool vis[N], done[N]; n int sz[N], ns[N], ns2[N], comp_cnt, comp[N], allns = 0; n void DFS(int node) { n   sz[comp_cnt]++; n   vis[node] = true; n   comp[node] = comp_cnt; n   for (int i = 0; i < (int)g[node].size(); i++) { n     if (!vis[g[node][i]]) { n       DFS(g[node][i]); n     } n   } n } n void take(int idx, int needed) { n   if (done[idx]) return; n   done[idx] = true; n   int u = cur[idx][0]; n   int v = cur[idx][1]; n   if (v % n == needed) swap(cur[idx][0], cur[idx][1]); n   u = cur[idx][0]; n   v = cur[idx][1]; n   ns[comp[needed]] += cur[idx][0] / n; n   allns += cur[idx][0] / n; n   ns2[comp[needed]] += cur[idx][1] / n; n   int a = id[v % n][0]; n   if (a == idx) a = id[v % n][1]; n   take(a, v % n); n } n int main() { n   scanf( %d , &n); n   if (n % 2 == 0) { n     puts( First ); n     fflush(stdout); n     if ((n / 2) % 2 == 0) { n       ans[1] = ans[2] = cnt++; n       ans[n + 1] = ans[n + 2] = cnt++; n       for (int i = 3; i <= n; i++) { n         ans[i] = ans[i + n] = cnt++; n       } n     } else { n       int last[2] = {-1, -1}; n       for (int cur, i = 1; i <= 2 * n; i++) { n         cur = (i & 1); n         if (last[cur] == -1) n           last[cur] = i; n         else { n           ans[i] = ans[last[cur]] = cnt++; n           last[cur] = -1; n         } n       } n     } n     for (int i = 1; i <= 2 * n; i++) { n       if (i > 1) putchar(   ); n       printf( %d , ans[i]); n     } n     puts(  ); n     fflush(stdout); n     return 0; n   } n   puts( Second ); n   fflush(stdout); n   for (int i = 1; i <= 2 * n; i++) { n     scanf( %d , &ans[i]); n     cur[ans[i]].push_back(i); n     id[i % n].push_back(ans[i]); n   } n   for (int u, v, i = 1; i <= n; i++) { n     u = cur[i][0] % n; n     v = cur[i][1] % n; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   for (int i = 0; i < n; i++) { n     if (vis[i]) continue; n     DFS(i); n     comp_cnt++; n   } n   for (int i = 1; i <= n; i++) { n     if (done[i]) continue; n     take(i, cur[i][0] % n); n   } n   long long val = (long long)n * (n - 1) / 2; n   val += (long long)allns * n; n   val %= (2 * n); n   if (val != 0) { n     assert(val == n); n     int good = -1; n     for (int i = 0; i < comp_cnt; i++) { n       if (abs(ns[i] - ns2[i]) % 2 == 1) { n         good = i; n         break; n       } n     } n     assert(good != -1); n     for (int i = 1; i <= n; i++) { n       if (comp[cur[i][0] % n] == good) { n         swap(cur[i][0], cur[i][1]); n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     if (i > 1) putchar(   ); n     printf( %d , cur[i][0]); n   } n   puts(  ); n   fflush(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000 * 1000 * 1000 + 47; n const long long LINF = (long long)INF * INF; n const int MAX = 2e5; n const double EPS = 1e-9; n const double PI = acos(-1.); n int n; n int a[10][10]; n int dx[8] = {2, 2, 1, -1, -2, -2, -1, 1}; n int dy[8] = {1, -1, -2, -2, -1, 1, 2, 2}; n pair<int, int> dpT[100][10][10][3][212]; n bool ok(int x, int y) { n   if (min(x, y) < 0) return false; n   if (max(x, y) >= n) return false; n   return true; n } n pair<int, int> dp(int cur, int x, int y, int fig, int steps) { n   if (steps == 212) { n     return make_pair(INF, INF); n   } n   if (cur == n * n) { n     return make_pair(0, 0); n   } n   if (dpT[cur][x][y][fig][steps] != make_pair(-1, -1)) { n     return dpT[cur][x][y][fig][steps]; n   } n   if (a[x][y] == cur) { n     return dpT[cur][x][y][fig][steps] = dp(cur + 1, x, y, fig, steps); n   } n   pair<int, int> best = make_pair(INF, INF); n   if (fig != 0) { n     pair<int, int> a = dp(cur, x, y, 0, steps + 1); n     best = min(best, make_pair(a.first + 1, a.second + 1)); n   } n   if (fig != 1) { n     pair<int, int> a = dp(cur, x, y, 1, steps + 1); n     best = min(best, make_pair(a.first + 1, a.second + 1)); n   } n   if (fig != 2) { n     pair<int, int> a = dp(cur, x, y, 2, steps + 1); n     best = min(best, make_pair(a.first + 1, a.second + 1)); n   } n   if (fig == 0) { n     for (int i = (0); i < (8); ++i) { n       int nx = x + dx[i]; n       int ny = y + dy[i]; n       if (ok(nx, ny)) { n         pair<int, int> a = dp(cur, nx, ny, fig, steps + 1); n         best = min(best, make_pair(a.first + 1, a.second)); n       } n     } n   } n   if (fig == 1) { n     for (int i = (-n); i < (n + 1); ++i) { n       if (!i) continue; n       int nx = x + i; n       int ny = y; n       if (ok(nx, ny)) { n         pair<int, int> a = dp(cur, nx, ny, fig, steps + 1); n         best = min(best, make_pair(a.first + 1, a.second)); n       } n       nx = x; n       ny = y + i; n       if (ok(nx, ny)) { n         pair<int, int> a = dp(cur, nx, ny, fig, steps + 1); n         best = min(best, make_pair(a.first + 1, a.second)); n       } n     } n   } n   if (fig == 2) { n     for (int i = (-n); i < (n + 1); ++i) { n       if (!i) continue; n       int nx = x + i; n       int ny = y + i; n       if (ok(nx, ny)) { n         pair<int, int> a = dp(cur, nx, ny, fig, steps + 1); n         best = min(best, make_pair(a.first + 1, a.second)); n       } n       nx = x - i; n       ny = y + i; n       if (ok(nx, ny)) { n         pair<int, int> a = dp(cur, nx, ny, fig, steps + 1); n         best = min(best, make_pair(a.first + 1, a.second)); n       } n     } n   } n   return dpT[cur][x][y][fig][steps] = best; n } n int main() { n   ios_base::sync_with_stdio(0); n   memset(dpT, -1, sizeof(dpT)); n   cin >> n; n   int x, y; n   for (int i = (0); i < (n); ++i) { n     for (int j = (0); j < (n); ++j) { n       cin >> a[i][j]; n       a[i][j]--; n       if (a[i][j] == 0) { n         x = i; n         y = j; n       } n     } n   } n   pair<int, int> best = make_pair(INF, INF); n   best = min(best, dp(1, x, y, 0, 0)); n   best = min(best, dp(1, x, y, 1, 0)); n   best = min(best, dp(1, x, y, 2, 0)); n   cout << best.first <<     << best.second << endl; 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 n, m, i, j, k; n   cin >> n >> m; n   int a[101] = {0}; n   for (i = 0; i < m; i++) { n     cin >> j; n     a[j]++; n   } n   vector<int> v; n   for (i = 1; i <= 100; i++) { n     if (a[i] != 0) v.push_back(a[i]); n   } n   sort(v.rbegin(), v.rend()); n   j = m / n; n   if (j == 0) n     cout << j; n   else { n     for (i = j; i >= 1; i--) { n       int x = 0; n       for (k = 0; k < v.size(); k++) { n         x += (v[k] / i); n       } n       if (x >= n) break; n     } n     cout << i; n   } n } n


#include <bits/stdc++.h> n template <class T> n inline void read(T &x) { n   x = 0; n   int f = 0; n   char ch = getchar(); n   while (!isdigit(ch)) { n     f |= ch ==  - ; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = (x << 1) + (x << 3) + (ch ^ 48); n     ch = getchar(); n   } n   x = f ? -x : x; n   return; n } n std::vector<long long> r, g, b; n long long ans, nr, ng, nb, t, p; n void solve(std::vector<long long> a, std::vector<long long> b, n            std::vector<long long> c) { n   for (auto x : a) { n     auto y = std::lower_bound(b.begin(), b.end(), x), n          z = std::upper_bound(c.begin(), c.end(), x); n     if (y == b.end() || z == c.begin()) continue; n     --z; n     ans = std::min( n         ans, (x - *y) * (x - *y) + (*y - *z) * (*y - *z) + (x - *z) * (x - *z)); n   } n } n int main() { n   read(t); n   while (t--) { n     ans = 9e18; n     r.clear(), g.clear(), b.clear(); n     read(nr), read(ng), read(nb); n     for (int i = 1; i <= nr; ++i) read(p), r.push_back(p); n     for (int i = 1; i <= ng; ++i) read(p), g.push_back(p); n     for (int i = 1; i <= nb; ++i) read(p), b.push_back(p); n     std::sort(r.begin(), r.end()), std::sort(g.begin(), g.end()), n         std::sort(b.begin(), b.end()); n     solve(r, g, b), solve(r, b, g), solve(g, r, b), solve(g, b, r), n         solve(b, r, g), solve(b, g, r); n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxb = 20; n const int maxn = 300000 + 7; n int dp[maxn][maxb]; n int n, q; n int seq[maxn]; n int last[maxb]; n void init() { n   cin >> n >> q; n   for (int i = (1); i <= (n); ++i) cin >> seq[i]; n } n void solve() { n   for (int i = (0); i < (maxb); ++i) dp[n + 1][i] = n + 1; n   for (int i = (0); i < (maxb); ++i) last[i] = n + 1; n   for (int i = (n); i >= (1); --i) { n     for (int j = (0); j < (maxb); ++j) dp[i][j] = n + 1; n     int now = seq[i]; n     for (int j = (0); j < (maxb); ++j) { n       if ((now >> j) & 1) { n         for (int k = (0); k < (maxb); ++k) n           dp[i][k] = min(dp[i][k], dp[last[j]][k]); n         dp[i][j] = i; n         last[j] = i; n       } n     } n   } n   while (q--) { n     int x, y; n     cin >> x >> y; n     int r = seq[y]; n     int ok = 0; n     for (int j = (0); j < (maxb); ++j) { n       if (((r >> j) & 1) && dp[x][j] <= y) { n         ok = 1; n         break; n       } n     } n     if (ok) n       cout <<  Shi n ; n     else n       cout <<  Fou n ; n   } n } n int main(void) { n   ios::sync_with_stdio(false); n   cin.tie(0); n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 2e3 + 5; n using namespace std; n int arr[maxn]; n int dp[maxn][maxn]; n int check(int x, int lef, int rig) { return (lef <= x && x <= rig ? 1 : 0); } n int main() { n   int n, h, l, r; n   scanf( %d %d %d %d , &n, &h, &l, &r); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &arr[i]); n   } n   memset((dp), (-1), sizeof(dp)); n   dp[0][0] = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < h; j++) { n       if (dp[i - 1][j] != -1) { n         dp[i][(j + arr[i]) % h] = n             max(dp[i - 1][j] + check((j + arr[i]) % h, l, r), n                 dp[i][(j + arr[i]) % h]); n         dp[i][(j + arr[i] - 1) % h] = n             max(dp[i - 1][j] + check((j + arr[i] - 1) % h, l, r), n                 dp[i][(j + arr[i] - 1) % h]); n       } n     } n   } n   int ans = -1; n   for (int i = 0; i < h; i++) { n     ans = max(ans, dp[n][i]); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace ::std; n const long double PI = acos(-1); n const long long MOD = 1000000000 + 7; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n long long add(long long a, long long b, long long m = MOD) { n   return (a % m + b % m + 2 * m) % m; n } n long long mul(long long a, long long b, long long m = MOD) { n   return ((a % m + m) * (b % m + m)) % m; n } n long long pow_mod(long long a, long long b, long long m = MOD) { n   long long res = 1LL; n   a = a % m; n   while (b) { n     if (b & 1) res = mul(res, a, m); n     b >>= 1; n     a = mul(a, a, m); n   } n   return res; n } n long long fastexp(long long a, long long b) { n   long long res = 1LL; n   while (b) { n     if (b & 1) res = res * a; n     b >>= 1; n     a *= a; n   } n   return res; n } n int gcdExtendido(int a, int b, int *x, int *y) { n   if (a == 0) { n     *x = 0; n     *y = 1; n     return b; n   } n   int x1, y1; n   int gcd = gcdExtendido(b % a, a, &x1, &y1); n   *x = y1 - (b / a) * x1; n   *y = x1; n   return gcd; n } n int modInverso(int a, int m) { n   int x, y; n   int g = gcdExtendido(a, m, &x, &y); n   if (g != 1) n     return -1; n   else n     return (x % m + m) % m; n } n const int M = 10000 + 5; n const int N = 500 + 5; n int n, m, k; n pair<int, int> edges[M]; n int carry[N]; n int s[N]; n int sizes[3][M][N]; n int parent[3][M][N]; n int get(int x, int id, int type) { n   return parent[type][id][x] == x n              ? x n              : parent[type][id][x] = get(parent[type][id][x], id, type); n } n void join(int a, int b, int id, int type) { n   a = get(a, id, type); n   b = get(b, id, type); n   if (a == b) return; n   if (sizes[type][id][a] > sizes[type][id][b]) swap(a, b); n   parent[type][id][a] = b; n   sizes[type][id][b] += sizes[type][id][a]; n } n void initialize(int id, int type) { n   for (int i = 1; i <= n; i++) { n     parent[type][id][i] = i; n     sizes[type][id][i] = 1; n   } n } n int get2(int x) { return carry[x] == x ? x : carry[x] = get2(carry[x]); } n void join2(int a, int b) { n   a = get2(a); n   b = get2(b); n   if (a == b) return; n   if (s[a] > s[b]) swap(a, b); n   carry[a] = b; n   s[b] += s[a]; n } n int main() { n   scanf( %d %d , &(n), &(m)); n   initialize(0, 0); n   for (int i = 1; i <= m; i++) { n     for (int j = 1; j <= n; j++) { n       parent[0][i][j] = parent[0][i - 1][j]; n       sizes[0][i][j] = sizes[0][i - 1][j]; n     } n     scanf( %d %d , &(edges[i].first), &(edges[i].second)); n     join(edges[i].first, edges[i].second, i, 0); n   } n   initialize(m + 1, 1); n   for (int i = m; i >= 1; i--) { n     for (int j = 1; j <= n; j++) { n       parent[1][i][j] = parent[1][i + 1][j]; n       sizes[1][i][j] = sizes[1][i + 1][j]; n     } n     join(edges[i].first, edges[i].second, i, 1); n   } n   scanf( %d , &(k)); n   int l, r; n   while (k--) { n     scanf( %d %d , &(l), &(r)); n     l--; n     r++; n     for (int i = 1; i <= n; i++) { n       carry[i] = parent[0][l][i]; n       s[i] = sizes[0][l][i]; n     } n     queue<pair<int, int> > Q; n     for (int i = 1; i <= n; i++) { n       int x = get(i, r, 1); n       join2(i, x); n     } n     int ans = 0; n     for (int i = 1; i <= n; i++) { n       if (get2(i) == i) ans++; n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, t; n   cin >> s >> t; n   int n = s.size(); n   int m = t.size(); n   if (n == m && s == t) n     cout << -1; n   else n     cout << max(n, m); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n; n   cin >> n; n   if (n == 0) n     cout << 1; n   else { n     switch (n % 4) { n       case 0: n         cout << 6 << endl; n         break; n       case 1: n         cout << 8 << endl; n         break; n       case 2: n         cout << 4 << endl; n         break; n       case 3: n         cout << 2 << endl; n         break; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long max_n = INT_MAX - 1000000; n long long dp[30][30001]; n pair<int, int> par[30][30001]; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     long long n, m, k; n     cin >> n >> m >> k; n     if (n > m) swap(n, m); n     map<char, long long> mp; n     string ss =   ; n     for (long long i = 0; i < k; i++) { n       char a; n       cin >> a; n       mp[a]++; n       ss += a; n     } n     vector<int> vv; n     for (auto i : mp) { n       vv.push_back(i.second); n     } n     long long ans = LONG_MAX; n     for (int i1 = 0; i1 < vv.size(); i1++) { n       vector<int> v = vv; n       v.erase(v.begin() + i1); n       v.insert(v.begin(), vv[i1]); n       for (long long i = 0; i < v.size(); i++) { n         for (long long j = 0; j <= m; j++) { n           dp[i][j] = max_n; n         } n       } n       for (int i = 0; i < v.size(); i++) { n         for (long long j = 1; j <= m; j++) { n           if (i == 0) { n             if (j <= v[i]) { n               dp[i][j] = v[i]; n             } n           } else { n             if (dp[0][j] == max_n) { n               if (j <= v[i]) { n                 dp[i][j] = v[i] + v[0]; n                 dp[i][j] = min(dp[i - 1][j], dp[i][j]); n               } else { n                 dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - v[i]] + v[i]); n               } n             } else { n               dp[i][j] = dp[0][j]; n             } n           } n         } n       } n       if (v[0] - (dp[v.size() - 1][n] - n) >= 0) n         ans = min(ans, (v[0] - (dp[v.size() - 1][n] - n)) * n                            max((long long)0, n                                (dp[v.size() - 1][n] - n - (k - n - m)))); n       if (v[0] - (dp[v.size() - 1][m] - m) >= 0) n         ans = min(ans, (v[0] - (dp[v.size() - 1][m] - m)) * n                            max((long long)0, n                                (dp[v.size() - 1][m] - m - (k - n - m)))); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n static const int N = 1000; n int t[N][N]; n int d[4][N][N]; n int di[4] = {1, 0, -1, 0}; n int dj[4] = {0, 1, 0, -1}; n int n, m, q; n int p[N]; n bool check(int k, int i) { n   if (i < 0) return 0; n   if (k) return i < m; n   return i < n; n } n int solve(int c, int b) { n   int glob = 1; n   int res = 0; n   int l = c; n   int d = c; n   while (glob) { n     while (check(b, d + 1) && p[d] == p[d + 1]) d++; n     while (check(b, l - 1) && p[l] == p[l - 1]) l--; n     if (res < (d - l + 1) * (min(p[l], p[d]))) n       res = (d - l + 1) * (min(p[l], p[d])); n     if (!check(b, l - 1) && !check(b, d + 1)) n       glob = 0; n     else { n       if (!check(b, l - 1)) n         d++; n       else { n         if (!check(b, d + 1)) n           l--; n         else { n           if (p[d + 1] > p[l - 1]) n             d++; n           else n             l--; n         } n       } n     } n   } n   return res; n } n int main() { n   scanf( %i , &n); n   scanf( %i , &m); n   scanf( %i , &q); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) scanf( %i , &t[i][j]); n   int si, sj, ei, ej; n   for (int k = 0; k < 4; k++) { n     si = di[k] * di[k] * ((n - 1) * (1 - di[k]) / 2); n     sj = dj[k] * dj[k] * ((m - 1) * (1 - dj[k]) / 2); n     ei = n - si - 1; n     ej = m - sj - 1; n     int i = si; n     int j = sj; n     int sum = 0; n     while ((!di[k] && i < n) || (!dj[k] && j < m)) { n       if (t[i][j]) n         sum += t[i][j]; n       else n         sum = 0; n       d[k][i][j] = sum; n       i += di[k]; n       j += dj[k]; n       if (!di[k] && (dj[k] * j > dj[k] * ej)) { n         j = sj; n         i++; n         sum = 0; n       } n       if (!dj[k] && (di[k] * i > di[k] * ei)) { n         i = si; n         j++; n         sum = 0; n       } n     } n   } n   int x, y, b; n   for (int l = 0; l < q; l++) { n     scanf( %i , &b); n     scanf( %i , &x); n     scanf( %i , &y); n     x--; n     y--; n     if (b - 1) { n       int best = 0; n       int res; n       for (int k = 0; k < 4; k++) { n         int i = x; n         int j = y; n         if (di[k]) { n           p[j] = d[k][i][j]; n           int cmin = p[j]; n           for (int f = j; f < m; f++) { n             if (d[k][i][f] < cmin) cmin = d[k][i][f]; n             p[f] = cmin; n           } n           cmin = p[j]; n           for (int f = j; f >= 0; f--) { n             if (d[k][i][f] < cmin) cmin = d[k][i][f]; n             p[f] = cmin; n           } n           res = solve(j, 1); n         } else { n           p[i] = d[k][i][j]; n           int cmin = p[i]; n           for (int f = i; f < n; f++) { n             if (d[k][f][j] < cmin) cmin = d[k][f][j]; n             p[f] = cmin; n           } n           cmin = p[i]; n           for (int f = i; f >= 0; f--) { n             if (d[k][f][j] < cmin) cmin = d[k][f][j]; n             p[f] = cmin; n           } n           res = solve(i, 0); n         } n         if (res > best) best = res; n       } n       printf( %i n , best); n     } else { n       t[x][y] ^= 1; n       for (int k = 0; k < 4; k++) { n         int i = !di[k] * x + di[k] * di[k] * ((n - 1) * (1 - di[k]) / 2); n         int j = !dj[k] * y + dj[k] * dj[k] * ((m - 1) * (1 - dj[k]) / 2); n         int sum = 0; n         while (i >= 0 && i < n && j >= 0 && j < m) { n           if (t[i][j]) n             sum += t[i][j]; n           else n             sum = 0; n           d[k][i][j] = sum; n           i += di[k]; n           j += dj[k]; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1000000007LL; n int n, l; n long long a[102], b[102]; n long long dp[2 * 3002][102][2]; n int main() { n   while (cin >> n >> l) { n     for (int i = 0; i < n; ++i) { n       cin >> a[i] >> b[i]; n     } n     memset(dp, 0, sizeof(dp)); n     for (int i = 0; i < n; ++i) { n       dp[a[i]][i][0] = 1LL; n       if (a[i] != b[i]) { n         dp[b[i]][i][1] = 1LL; n       } n     } n     for (int i = 1; i <= l; ++i) { n       for (int j = 0; j < n; ++j) { n         if (dp[i][j][0]) { n           for (int k = 0; k < n; ++k) { n             if (k != j) { n               if (a[k] == b[j]) { n                 dp[i + b[j]][k][0] = (dp[i + b[j]][k][0] + dp[i][j][0]) % mod; n               } else if (b[k] == b[j]) { n                 dp[i + b[j]][k][1] = (dp[i + b[j]][k][1] + dp[i][j][0]) % mod; n               } n             } n           } n         } n         if (dp[i][j][1]) { n           for (int k = 0; k < n; ++k) { n             if (k != j) { n               if (a[k] == a[j]) { n                 dp[i + a[j]][k][0] = (dp[i + a[j]][k][0] + dp[i][j][1]) % mod; n               } else if (b[k] == a[j]) { n                 dp[i + a[j]][k][1] = (dp[i + a[j]][k][1] + dp[i][j][1]) % mod; n               } n             } n           } n         } n       } n     } n     long long ans = 0LL; n     for (int i = 0; i < n; ++i) { n       ans = (ans + dp[l][i][0]) % mod; n       ans = (ans + dp[l][i][1]) % mod; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int x[1000], y[1000]; n using namespace std; n int main() { n   int n, counter = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x[i] >> y[i]; n     if (x[i] != y[i]) { n       cout <<  rated ; n       return 0; n     } n   } n   for (int i = 0; i < n; i++) { n     if (x[i] < x[i + 1]) { n       cout <<  unrated ; n       return 0; n     } n   } n   cout <<  maybe ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   while (cin >> n) { n     vector<int> a(n); n     for (int i = 0; i < n; scanf( %d , &a[i++])) n       ; n     sort(a.begin(), a.end()); n     bool ok = 0; n     for (int i = 0; i < n; i++) { n       if (a[i] != i + 1) { n         cout << i + 1 << endl; n         ok = 1; n         break; n       } n     } n     if (!ok) cout << n + 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a0, a1, n, ans, table[500]; n int getans(int); n int main() { n   scanf( %d%d%d , &a0, &a1, &n); n   printf( %d n , getans(n)); n   return 0; n } n int getans(int an) { n   if (an == 0) return a0; n   if (an == 1) return a1; n   return getans(an - 1) + getans(an - 2); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n int a[maxn], b[maxn], ww[maxn], www[maxn]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     ww[a[i]] = 1; n   } n   for (int i = 1; i <= n; i++) { n     scanf( %d , &b[i]); n     www[b[i]] = i; n   } n   int t1 = 1; n   while (t1 <= n && b[t1] == 0) t1++; n   if (t1 == n + 1) { n     printf( %d n , n); n   } else { n     int f = 1; n     for (int i = t1; i <= n; i++) { n       if (b[i] != i - t1 + 1) { n         f = 0; n         break; n       } n     } n     if (f) { n       printf( %d n , t1 - 1); n     } else { n       int x = b[n]; n       int fx = 0; n       for (int i = n - 1; i >= 1; i--) { n         if (b[i + 1] == 1) { n           fx = 1; n           break; n         } n         if (b[i] != b[i + 1] - 1) { n           break; n         } n       } n       int ans = 0; n       int fx2 = 0; n       if (fx) { n         ans = n - b[n]; n         if (!ww[b[n] + 1]) { n           fx2 = 1; n         } n         for (int i = b[n] + 2; i <= n; i++) { n           if (www[i] > i - b[n] - 1) { n             fx2 = 1; n             break; n           } n         } n       } n       if (fx2 || fx == 0) { n         for (int i = 1; i <= n; i++) { n           if (ww[i]) continue; n           ans = max(ans, max(0, www[i] + 1 - i) + n); n         } n       } n       printf( %d n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[110][110]; n int main() { n   int n, v; n   cin >> n >> v; n   for (int i = 1; i <= v; i++) dp[1][i] = i; n   for (int i = 2; i <= n; i++) { n     for (int j = 0; j < v; j++) { n       dp[i][j] = min(dp[i - 1][j + 1], dp[i - 1][j] + i); n     } n     dp[i][v] = dp[i - 1][v] + i; n   } n   int ans = 1000000000; n   for (int i = 0; i < v + 1; i++) ans = min(ans, dp[n][i]); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gi() { n   int x = 0, w = 1; n   char ch = getchar(); n   while ((ch <  0  || ch >  9 ) && ch !=  - ) ch = getchar(); n   if (ch ==  - ) w = 0, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) n     x = (x << 3) + (x << 1) + ch -  0 , ch = getchar(); n   return w ? x : -x; n } n const int N = 3e5 + 5; n int n, m, to[N << 1], nxt[N << 1], head[N], cnt, ls[N], rs[N], dis[N], rt[N], n     top[N], vis[N], key[N], tag[N]; n long long ans; n void cover(int x, int v) { n   if (!x) return; n   key[x] += v; n   tag[x] += v; n } n void down(int x) { n   if (!tag[x]) return; n   cover(ls[x], tag[x]); n   cover(rs[x], tag[x]); n   tag[x] = 0; n } n int merge(int x, int y) { n   if (!x || !y) return x | y; n   if (key[x] > key[y]) swap(x, y); n   down(x); n   rs[x] = merge(rs[x], y); n   if (dis[ls[x]] < dis[rs[x]]) swap(ls[x], rs[x]); n   dis[x] = dis[rs[x]] + 1; n   return x; n } n void dfs(int u, int f) { n   for (int e = head[u]; e; e = nxt[e]) n     if (to[e] ^ f) dfs(to[e], u), rt[u] = merge(rt[u], rt[to[e]]); n   if (u ^ 1) { n     vis[u] = 1; n     while (vis[top[rt[u]]]) rt[u] = merge(ls[rt[u]], rs[rt[u]]); n     if (!rt[u]) puts( -1 ), exit(0); n     ans += key[rt[u]]; n     cover(rt[u], -key[rt[u]]); n   } n } n int main() { n   n = gi(); n   m = gi(); n   for (int i = 1; i < n; ++i) { n     int u = gi(), v = gi(); n     to[++cnt] = v; n     nxt[cnt] = head[u]; n     head[u] = cnt; n     to[++cnt] = u; n     nxt[cnt] = head[v]; n     head[v] = cnt; n   } n   for (int i = 1; i <= m; ++i) { n     int u = gi(); n     top[i] = gi(); n     key[i] = gi(); n     rt[u] = merge(rt[u], i); n   } n   dfs(1, 0); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 1; n int bit[N] = {0}; n void update(int x, int val) { n   for (; x <= N; x += x & (-x)) bit[x] += val; n } n int query(int x) { n   int sum = 0; n   for (; x >= 1; x -= x & (-x)) sum += bit[x]; n   return sum; n } n int a[N]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int ans = 0; n   for (int i = n - 1; i >= 0; i--) { n     int ele = a[i]; n     ans += query(a[i]); n     update(a[i], 1); n   } n   int q; n   cin >> q; n   while (q--) { n     int a, b; n     cin >> a >> b; n     int r = b - a; n     int x = r * (r + 1); n     x /= 2; n     ans += x; n     if (ans % 2 == 0) n       cout <<  even n ; n     else n       cout <<  odd n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 101; n int n, a[N][30]; n long long nd[N][N], num[N]; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= n; ++j) cin >> nd[i][j]; n   memset(a, 0, sizeof(a)); n   for (int i = 1; i <= n; ++i) n     for (int j = i + 1; j <= n; ++j) { n       int run = 29; n       while (run >= 0) { n         if (nd[i][j] & (1 << (29 - run))) a[i][run] = a[j][run] = 1; n         --run; n       } n     } n   for (int i = 1; i <= n; ++i) n     for (int k = 0; k <= 29; ++k) num[i] += (1 << k) * (a[i][29 - k]); n   for (int i = 1; i <= n; ++i) cout << num[i] <<    ; n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n inline int Getlen(int x) { n   int res = 0; n   while (x) { n     res++; n     x /= 10; n   } n   return res; n } n int PP[5]; n int a[51], len[51], n, p; n int id[51]; n int res[51], tot; n inline void dfs(int now, int Gmod, int Gxor) { n   if (Gmod == 0 && Gxor == 0 && tot > 0) { n     puts( Yes ); n     cout << tot << endl; n     for (int i = 0; i < tot; i++) cout << res[i] <<    ; n     exit(0); n   } n   if (now > n) return; n   dfs(now + 1, Gmod, Gxor); n   res[tot++] = id[now]; n   dfs(now + 1, (Gmod * PP[len[now]] + a[now]) % p, Gxor ^ a[now]); n   tot--; n } n int main() { n   rand(); n   rand(); n   rand(); n   rand(); n   rand(); n   rand(); n   int tmpp; n   tmpp = 0; n   tmpp++; n   tmpp++; n   tot = 0; n   PP[0] = 1; n   for (int i = 1; i <= 3; i++) PP[i] = PP[i - 1] * 10; n   scanf( %d%d , &n, &p); n   int now = 0, tmp; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &tmp); n     if (tmp <= 31) a[++now] = tmp, len[now] = Getlen(a[now]), id[now] = i; n   } n   n = now; n   dfs(1, 0, 0); n   puts( No ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n + 5]; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   int m = 1; n   for (int i = 2; i < n; i++) { n     if (abs(a[i] - a[i + 1]) < abs(a[m] - a[m + 1])) m = i; n   } n   if (abs(a[n] - a[1]) < abs(a[m] - a[m + 1])) { n     cout << n <<     << 1; n   } else n     cout << m <<     << m + 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n constexpr long long maxN = 1e5 + 43; n long long n, d; n vector<long long> a; n long long suff[maxN]; n long long had_max[maxN]; n signed main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> d; n   a.resize(n + 1); n   for (long long i = 1; i <= n; ++i) cin >> a[i]; n   long long ans = 0; n   long long maxx = 0, minn = 0; n   for (long long i = 1; i <= n; ++i) { n     if (a[i] != 0) { n       maxx += a[i]; n       minn += a[i]; n       maxx = min(maxx, d); n       if (minn > d) { n         cout << -1; n         return 0; n       } n     } else { n       if (maxx < 0) { n         maxx = d; n         minn = 0; n         ++ans; n       } else { n         minn = max((long long)0, minn); n       } n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void output() { n   long long n; n   cin >> n; n   long long a[n], i; n   for (i = 0; i < n; i++) cin >> a[i]; n   map<long long, bool> make_pair; n   long long s = 0; n   make_pair[0] = true; n   long long c = 0; n   for (i = 0; i < n; i++) { n     s += a[i]; n     if (make_pair[s]) { n       c++; n       make_pair.clear(); n       s = a[i]; n       make_pair[s] = true; n       make_pair[0] = true; n     } else { n       make_pair[s] = true; n     } n   } n   cout << c << endl; n } n signed main() { n   long long t; n   t = 1; n   while (t--) output(); n } n


#include <bits/stdc++.h> n const int INF = (int)1e9; n const double PI = acos(-1.); n using namespace std; n string s; n long long sumr, suml, need; n int main() { n   cin >> s; n   for (int i = 0; i < s.length(); i++) { n     if (s[i] ==  ^ ) need = i; n   } n   for (int i = need - 1; i >= 0; i--) { n     if (s[i] !=  = ) { n       suml += (s[i] -  0 ) * (need - i); n     } n   } n   for (int i = need + 1; i < s.length(); i++) { n     if (s[i] !=  = ) { n       sumr += (s[i] -  0 ) * (i - need); n     } n   } n   if (sumr == suml) n     cout <<  balance ; n   else if (suml > sumr) n     cout <<  left ; n   else n     cout <<  right ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s =  abcdefghijklmnopqrstuvwxyz ; n   string s2 =   ; n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) s2 += s[i % k]; n   cout << s2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int forbidInd; n int A[100000 + 10]; n long long int memo[60][60][60]; n long long int solve(int i, int k, int x) { n   if (k < 0 || x < 0) return 0; n   if (i == 0) { n     if (x == 0 && k == 0) return 1; n     if (k == 1 && x == A[i] && forbidInd != 0) n       return 1; n     else n       return 0; n   } n   if (memo[i][k][x] != -1) return memo[i][k][x]; n   long long int ret = 0; n   ret = solve(i - 1, k, x); n   if (i != forbidInd) ret += solve(i - 1, k - 1, x - A[i]); n   return memo[i][k][x] = ret; n } n long long int C[60][60]; n int main() { n   for (int i = 0; i < 51; i++) n     for (int j = 0; j < 51; j++) n       if (i == j || j == 0) n         C[i][j] = 1; n       else n         C[i][j] = C[i - 1][j] + C[i - 1][j - 1]; n   int n, P, x; n   while (cin >> n) { n     int sum = 0; n     for (int i = 0; i < n; i++) { n       cin >> A[i]; n       sum += A[i]; n     } n     cin >> P; n     double ans = 0; n     for (int k = 0; k < n; k++) { n       forbidInd = k; n       memset(memo, -1, sizeof memo); n       for (int s = 1; s <= n - 1; s++) n         for (int x = P - A[k] + 1; x <= P; x++) { n           long long int ret = solve(n - 1, s, x); n           long long int tmp = C[n][s] * (n - s); n           ans = ans + ((ret * s * 1.) / (tmp * 1.0)); n         } n     } n     if (sum <= P) ans = n * 1.; n     printf( %.10lf n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int nsz = 1e5, ksz = 8, msksz = (1 << ksz) - 1, inf = 0x3f3f3f3f; n bool vis[nsz + 5]; n int n, m, k, full, p[nsz + 5], q[ksz + 5], dp[2][msksz + 5], ans; n struct event { n   int pos, id, st; n   event(int pos = 0, int id = 0, int st = 0) { n     this->pos = pos, this->id = id, this->st = st; n   } n }; n vector<event> dat; n bool inline operator<(event a, event b) { n   return a.pos != b.pos ? a.pos < b.pos : a.st < b.st; n } n void inline upd(int &a, int b) { a < b && (a = b); } n string inline bin(int S, int len) { n   string s; n   for (int i = 0; i < len; ++i) { n     s += (S >> i & 1) ^ 48; n   } n   return s; n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= n; ++i) { n     int l, r; n     scanf( %d%d , &l, &r); n     dat.push_back(event(l, i, +1)); n     dat.push_back(event(r + 1, i, -1)); n   } n   sort(dat.begin(), dat.end()); n   memset(dp, -inf, sizeof(dp)); n   int o = 0, x = 1, l = dat[0].pos, i = 0, k = 0; n   for (; i < dat.size() && dat[i].pos == l; ++i) { n     p[dat[i].id] = k; n     q[k] = dat[i].id; n     ++k; n   } n   full = (1 << k) - 1; n   for (int S = 0; S <= full; ++S) { n     dp[o][S] = 0; n   } n   for (; i < dat.size();) { n     l = dat[i].pos; n     int il = i, ir = i, len = dat[i].pos - dat[i - 1].pos, pk = k, dk = 0, n         pfull = full, dfull; n     memset(vis, 0, sizeof(vis)); n     for (; i < dat.size() && dat[i].pos == l; ++i, ++ir) { n       if (dat[i].st == -1) { n         --k; n         vis[p[dat[i].id]] = 1; n       } n     } n     for (int i = 0, j = 0; i < pk; ++i) { n       if (vis[i]) continue; n       p[q[i]] = j; n       q[j] = q[i]; n       ++j; n     } n     for (int i = il; i <= ir - 1; ++i) { n       if (dat[i].st == +1) { n         p[dat[i].id] = k; n         q[k] = dat[i].id; n         ++k; n         ++dk; n       } n     } n     full = (1 << k) - 1; n     dfull = (1 << dk) - 1; n     for (int S = 0; S <= pfull; ++S) { n       int val = dp[o][S] + (__builtin_popcount(S) & 1) * len, nS = 0, j = 0; n       if (val < 0) continue; n       for (int i = 0; i < pk; ++i) { n         if (vis[i]) continue; n         nS |= (S >> i & 1) << j; n         ++j; n       } n       if (dk <= 0) { n         upd(dp[x][nS], val); n       } else { n         for (int dS = 0; dS <= dfull; ++dS) { n           int nnS = nS | (dS << j); n           upd(dp[x][nnS], val); n         } n       } n     } n     swap(o, x); n     memset(dp[x], -inf, sizeof(dp[x])); n   } n   for (int S = 0; S <= full; ++S) { n     upd(ans, dp[o][S]); n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n template <class T1, class T2> n struct cmpf { n   bool rev; n   inline bool operator()(const pair<T1, T2>& a, const pair<T1, T2>& b) const { n     return (a.first < b.first) ^ rev; n   } n   cmpf(bool b = false) { rev = b; } n }; n template <class T1, class T2> n struct cmps { n   bool rev; n   inline bool operator()(const pair<T1, T2>& a, const pair<T1, T2>& b) const { n     return (a.second < b.second) ^ rev; n   } n   cmps(bool b = false) { rev = b; } n }; n string to_string(const char& c) { return string(1, c); } n string to_string(const string& s) { return     + s +    ; } n string to_string(const char* s) { return to_string((string)s); } n string to_string(const bool& b) { return (b ?  true  :  false ); } n string to_string(const vector<bool>& v, const string& sep =    ) { n   string s =  [ ; n   for (int i = 0; i < v.size(); i++) { n     if (i) { n       s += sep; n     } n     s += to_string(v[i]); n   } n   return s +  ] ; n } n template <size_t N> n string to_string(const bitset<N>& v) { n   string s =  [ ; n   for (size_t i = 0; i < N; i++) s += v[i] ?  1  :  0 ; n   return s +  ] ; n } n template <class T1, class T2> n string to_string(const pair<T1, T2>& p, const string& sep =  , ); n template <class T> n string to_string(const T& v, const string& sep =    ) { n   bool first = true; n   string s =  { ; n   for (const auto& x : v) { n     if (!first) n       s += sep; n     else n       first = false; n     s += to_string(x); n   } n   return s +  } ; n } n template <class T> n string to_string(const T& v, const int& sz, const string& sep =    ) { n   string s =  [ ; n   for (int i = 0; i < sz; i++) { n     if (i) { n       s += sep; n     } n     s += to_string(v[i]); n   } n   return s +  ] ; n } n template <class T1, class T2> n string to_string(const pair<T1, T2>& p, const string& sep) { n   return  (  + to_string(p.first) + sep + to_string(p.second) +  ) ; n } n long long binpow(const long long& x, const long long& p, const long long& mod) { n   assert(mod > 0); n   if (p == 0) return 1; n   if (p == 1) return x % mod; n   if (p & 1) return (binpow((x * x) % mod, p / 2, mod) * x) % mod; n   return binpow((x * x) % mod, p / 2, mod) % mod; n } n template <class T> n void chmn(T& a, const T& b) { n   if (a > b) a = b; n } n template <class T> n void chmx(T& a, const T& b) { n   if (a < b) a = b; n } n void setIO() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n } n int chr[8] = {-1, 0, 1, 0, -1, -1, 1, 1}; n int chc[8] = {0, 1, 0, -1, -1, 1, -1, 1}; n const long long MOD = 1e9 + 7; n const int INIT = 1001001001; n const int MN = 10001; n long long BIT[MN]; n void inc(int pos) { n   for (; pos < MN; pos |= pos + 1) ++BIT[pos]; n } n long long query(int pos) { n   long long res = 0; n   for (; pos > 0; pos &= pos - 1) res += BIT[pos - 1]; n   return res; n } n long long N, P; n long long A[MN]; n long long f(long long x) { n   long long cur = 1; n   for (int i = 0; i < N; i++) { n     long long q = query(x + i + 1); n     if (q <= i) return -1; n     cur = (cur * (q - i)) % P; n   } n   return cur; n } n void solve() { n   cin >> N >> P; n   for (int i = 0; i < N; i++) { n     cin >> A[i]; n     inc(A[i]); n   } n   vector<int> ans; n   for (int x = 1; x < 4000; x++) { n     if (f(x) > 0) { n       ans.push_back(x); n     } n   } n   cout << ans.size() << endl; n   for (int i = 0; i < ans.size(); i++) { n     if (i) cout <<    ; n     cout << ans[i]; n   } n   cout << endl; n } n int main() { n   setIO(); n   long long T = 1; n   while (T-- > 0) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 200000000; n struct Point { n   int x, y; n   Point(int _x, int _y) { n     x = _x; n     y = _y; n   } n }; n bool operator<(const Point a, const Point b) { return a.x < b.x; } n long long solve(vector<Point> v) { n   int n = (int)v.size(); n   vector<int> MINI(n + 1); n   vector<int> MAXI(n + 1); n   MINI[n] = inf; n   MAXI[n] = -inf; n   for (int i = n - 1; i >= 0; i--) { n     MINI[i] = min(v[i].y, MINI[i + 1]); n     MAXI[i] = max(v[i].y, MAXI[i + 1]); n   } n   long long lo = 0, hi = 1LL * inf * inf, mi; n   int mini2, maxi2; n   while (lo <= hi) { n     mi = (lo + hi) / 2; n     bool ok = 0; n     int mini = inf, maxi = -inf; n     for (int i = 0, j = -1; i < n; i++) { n       ok = 1; n       while (j + 1 <= i) { n         if (1LL * (v[j + 1].x - v[i].x) * (v[j + 1].x - v[i].x) > mi) { n           mini = min(mini, v[j + 1].y); n           maxi = max(maxi, v[j + 1].y); n           j++; n         } else { n           break; n         } n       } n       mini2 = min(mini, MINI[i + 1]), maxi2 = max(maxi, MAXI[i + 1]); n       if (maxi2 < mini2) { n         ok = 1; n       } else { n         if (1LL * (maxi2 - mini2) * (maxi2 - mini2) > mi) ok = 0; n         if (1LL * mini2 * mini2 + 1LL * v[i].x * v[i].x > mi) { n           ok = 0; n         } n         if (1LL * maxi2 * maxi2 + 1LL * v[i].x * v[i].x > mi) { n           ok = 0; n         } n         if (1LL * mini2 * mini2 + 1LL * v[j + 1].x * v[j + 1].x > mi) { n           ok = 0; n         } n         if (1LL * maxi2 * maxi2 + 1LL * v[j + 1].x * v[j + 1].x > mi) { n           ok = 0; n         } n       } n       if (!ok) { n       } else { n         ok = 1; n         break; n       } n     } n     if (ok) n       hi = mi - 1; n     else n       lo = mi + 1; n   } n   return lo; n } n int main() { n   int n; n   vector<Point> v; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int x, y; n     cin >> x >> y; n     v.push_back(Point(x, y)); n   } n   sort(v.begin(), v.end()); n   long long ans = solve(v); n   ans = min(1LL * (v[0].x - v[n - 1].x) * (v[0].x - v[n - 1].x), ans); n   for (int i = 0; i < n; i++) { n     swap(v[i].x, v[i].y); n   } n   sort(v.begin(), v.end()); n   ans = min(1LL * (v[0].x - v[n - 1].x) * (v[0].x - v[n - 1].x), ans); n   ans = min(ans, solve(v)); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long int aib[15][100005]; n inline void update(long long int aib[], int pos, long long int val) { n   for (; pos <= n; pos += ((pos) & (-(pos)))) aib[pos] += val; n } n inline long long int query(long long int aib[], int pos) { n   long long int ans = 0; n   for (; pos; pos -= ((pos) & (-(pos)))) ans += aib[pos]; n   return ans; n } n long long int dp[15][100005]; n int main() { n   ios_base ::sync_with_stdio(false); n   int k; n   cin >> n >> k; n   ++k; n   long long int ans = 0; n   int val; n   for (int i = 1; i <= n; ++i) { n     cin >> val; n     dp[1][i] = val; n     update(aib[1], val, 1); n     for (int j = 2; j <= k; ++j) { n       dp[j][i] = query(aib[j - 1], val - 1); n       update(aib[j], val, dp[j][i]); n     } n     ans += dp[k][i]; n   } n   cout << ans <<   n ; n   return 0; n } n