#include <bits/stdc++.h> n namespace fastIO { n static const int IN_BUF = 1 << 23, OUT_BUF = 1 << 23; n char buf[IN_BUF], *ps = buf, *pt = buf; n inline char my_getchar() { n   return ps == pt && (pt = (ps = buf) + fread(buf, 1, IN_BUF, stdin), ps == pt) n              ? EOF n              : *ps++; n } n template <typename T> n inline bool read(T &x) { n   char op = 0, ch = my_getchar(); n   for (x = 0; !isdigit(ch) && ch != EOF; ch = my_getchar()) n     if (ch ==  - ) op ^= 1; n   if (ch == EOF) return false; n   for (; isdigit(ch); ch = my_getchar()) x = x * 10 + (ch ^  0 ); n   if (ch !=  . ) n     return op ? x = -x : 0, true; n   else n     ch = my_getchar(); n   T t = 1; n   for (; isdigit(ch); ch = my_getchar()) t /= 10, x += (ch ^  0 ) * t; n   return op ? x = -x : 0, true; n } n inline int reads(char *s) { n   int n = 0; n   char ch = my_getchar(); n   for (; isspace(ch) && ch != EOF; ch = my_getchar()) n     ; n   for (; !isspace(ch) && ch != EOF; ch = my_getchar()) s[n++] = ch; n   return s[n] =   0 , n; n } n char pbuf[OUT_BUF], *pp = pbuf; n struct _flusher { n   ~_flusher() { fwrite(pbuf, 1, pp - pbuf, stdout); } n } OutputFlusher; n inline void my_putchar(char x) { n   pp == pbuf + OUT_BUF ? fwrite(pbuf, 1, OUT_BUF, stdout), pp = pbuf : 0, n                                                            *pp++ = x; n } n template <typename T> n inline void print_(T x) { n   if (x == 0) return my_putchar( 0 ), void(0); n   std::vector<int> num; n   for (x < 0 ? my_putchar( - ), x = -x : 0; x; x /= 10) num.push_back(x % 10); n   while (!num.empty()) my_putchar(num.back() ^  0 ), num.pop_back(); n } n template <typename T> n inline void print(T x, char ch =   n ) { n   print_(x), my_putchar(ch); n } n inline void prints_(const char *s, int n = -1) { n   if (n == -1) n = strlen(s); n   for (register int i = 0; i < n; ++i) my_putchar(s[i]); n } n inline void prints(const char *s, int n = -1, char ch =   n ) { n   prints_(s, n), my_putchar(ch); n } n template <typename T> n inline void printd_(T x, int n) { n   if (x < 0) x = -x, my_putchar( - ); n   long long a, b; n   for (register int i = 0; i < n; ++i) x *= 10; n   x = (long long)(x + 0.5); n   for (register int i = 0; i < n; ++i) x /= 10; n   a = x, x -= a; n   for (register int i = 0; i < n; ++i) x *= 10; n   b = x; n   print_(a); n   if (n) n     my_putchar( . ); n   else n     return; n   std::vector<int> num(n); n   for (register int i = 0; i < n; ++i) num[i] = b % 10, b /= 10; n   while (!num.empty()) my_putchar(num.back() ^  0 ), num.pop_back(); n } n template <typename T> n inline void printd(T x, int n, char ch =   n ) { n   printd_(x, n), my_putchar(ch); n } n }  // namespace fastIO n using namespace fastIO; n const int N = 200005; n int n, p[N], c[N], vis[N]; n int solve(std::vector<int> &a) { n   std::vector<int> d; n   int l = a.size(); n   for (register int i = 1; 1ll * i * i <= l; ++i) n     if (l % i == 0) { n       d.push_back(i); n       if (i * i < l) d.push_back(l / i); n     } n   std::sort(d.begin(), d.end()); n   for (int k : d) { n     for (register int i = 0; i < k; ++i) { n       bool flag = 1; n       for (register int j = i; j < l; j += k) flag &= a[j] == a[i]; n       if (flag) return k; n     } n   } n   return l; n } n void solve() { n   read(n); n   for (register int i = 1; i <= n; ++i) read(p[i]), vis[i] = 0; n   for (register int i = 1; i <= n; ++i) read(c[i]); n   int ans = n; n   for (register int i = 1; i <= n; ++i) n     if (!vis[i]) { n       std::vector<int> tmp; n       tmp.push_back(c[i]), vis[i] = 1; n       for (register int j = p[i]; j != i; j = p[j]) n         tmp.push_back(c[j]), vis[j] = 1; n       ans = std::min(ans, solve(tmp)); n     } n   print(ans); n } n int main() { n   int T = 1; n   read(T); n   while (T--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int p[1001][1001], row[1001], col[1001]; n int main() { n   int n, m, k; n   scanf( %d%d%d , &n, &m, &k); n   for (int r = 1; r <= n; ++r) row[r] = r; n   for (int c = 1; c <= m; ++c) col[c] = c; n   for (int r = 1; r <= n; ++r) n     for (int c = 1; c <= m; ++c) scanf( %d , &p[r][c]); n   while (k--) { n     char s[2]; n     int x, y; n     scanf( %s%d%d , s, &x, &y); n     switch (s[0]) { n       case  r : n         swap(row[x], row[y]); n         break; n       case  c : n         swap(col[x], col[y]); n         break; n       case  g : n         printf( %d n , p[row[x]][col[y]]); n         break; n       default: n         break; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool ok = false, a[5][5]; n int u[5]; n string p, maxs; n int pol(char s) { n   if (s ==  A ) return 1; n   if (s ==  B ) return 2; n   if (s ==  C ) return 3; n } n string pol2(int s) { n   if (s == 1) return  A ; n   if (s == 2) return  B ; n   if (s == 3) return  C ; n } n void dfs(int v) { n   if (ok) return; n   u[v] = 1; n   for (int i = 1; i <= 3; i++) n     if (a[v][i] && u[i] == 0) n       dfs(i); n     else if (a[v][i] && u[i] == 1) n       ok = true; n   u[v] = 2; n   p = p + pol2(v); n } n int main() { n   string s; n   for (int i = 1; i <= 3; i++) { n     cin >> s; n     if (s[1] ==  > ) n       a[pol(s[0])][pol(s[2])] = 1; n     else n       a[pol(s[2])][pol(s[0])] = 1; n   } n   for (int i = 1; i <= 3; i++) { n     for (int j = 1; j <= 3; j++) u[j] = 0; n     p =   ; n     dfs(i); n     if (p.size() > maxs.size()) maxs = p; n   } n   if (ok) n     cout <<  Impossible ; n   else n     cout << maxs; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned int seed; n unsigned int getrand() { n   seed ^= seed << 13; n   seed ^= seed >> 17; n   seed ^= seed << 5; n   return seed; n } n int get() { n   char ch; n   while (ch = getchar(), (ch <  0  || ch >  9 ) && ch !=  - ) n     ; n   if (ch ==  - ) { n     int s = 0; n     while (ch = getchar(), ch >=  0  && ch <=  9 ) s = s * 10 + ch -  0 ; n     return -s; n   } n   int s = ch -  0 ; n   while (ch = getchar(), ch >=  0  && ch <=  9 ) s = s * 10 + ch -  0 ; n   return s; n } n const int N = 2e5 + 5; n int n; n int fa[N * 2]; n struct node { n   int l, r, tot; n   long long sum, sumv; n   unsigned int key; n   int val; n } tree[N]; n int lef[N], rig[N]; n int rt[N * 2]; n long long ans; n int tot; n int getfather(int x) { return fa[x] == x ? x : fa[x] = getfather(fa[x]); } n void update_ans(int x, long long v) { n   ans = ans + v * (tree[rt[x]].sum + tree[rt[x]].sumv * (lef[x] - 1)); n } n void update(int now) { n   int l = tree[now].l, r = tree[now].r; n   tree[now].tot = tree[l].tot + tree[r].tot + 1; n   tree[now].sumv = tree[l].sumv + tree[r].sumv + tree[now].val; n   tree[now].sum = tree[l].sum + 1ll * tree[now].val * (tree[l].tot + 1) + n                   tree[r].sum + tree[r].sumv * (tree[l].tot + 1); n } n pair<int, int> split(int now, int v) { n   if (!now) return make_pair(0, 0); n   if (tree[now].val > v) { n     pair<int, int> u = split(tree[now].r, v); n     tree[now].r = u.first; n     update(now); n     return make_pair(now, u.second); n   } n   pair<int, int> u = split(tree[now].l, v); n   tree[now].l = u.second; n   update(now); n   return make_pair(u.first, now); n } n int merge(int x, int y) { n   if (!x || !y) return x | y; n   if (tree[x].key < tree[y].key) { n     tree[x].r = merge(tree[x].r, y); n     update(x); n     return x; n   } n   tree[y].l = merge(x, tree[y].l); n   update(y); n   return y; n } n void add(int x, int v, int id = 0) { n   if (!id) update_ans(x, -1); n   pair<int, int> u = split(rt[x], v); n   int t = id; n   if (!id) t = ++tot; n   tree[t].l = tree[t].r = 0; n   tree[t].sum = tree[t].val = tree[t].sumv = v; n   tree[t].tot = 1; n   tree[t].key = getrand(); n   rt[x] = merge(u.first, merge(t, u.second)); n   if (!id) update_ans(x, 1); n } n int que[N]; n int id[N]; n int k; n void getque(int now) { n   if (!now) return; n   getque(tree[now].l); n   que[++k] = tree[now].val; n   id[k] = now; n   getque(tree[now].r); n } n void Merge(int x, int y) { n   update_ans(x, -1); n   update_ans(y, -1); n   if (tree[rt[x]].tot < tree[rt[y]].tot) { n     k = 0; n     getque(rt[x]); n     for (int i = 1; i <= k; i++) add(y, que[i], id[i]); n     lef[y] = lef[x]; n     fa[x] = y; n     update_ans(y, 1); n   } else { n     k = 0; n     getque(rt[y]); n     for (int i = 1; i <= k; i++) add(x, que[i], id[i]); n     rig[x] = rig[y]; n     fa[y] = x; n     update_ans(x, 1); n   } n } n int main() { n   seed = 29382343; n   n = get(); n   ans = 0; n   for (int i = 1; i <= n; i++) { n     int x = get(), v = get(); n     ans = ans - 1ll * x * v; n     if (!fa[x]) { n       fa[x] = x; n       lef[x] = rig[x] = x; n       add(x, v); n     } else { n       int y = getfather(x); n       fa[++rig[y]] = y; n       add(y, v); n     } n     int y = getfather(x); n     if (fa[rig[y] + 1]) Merge(y, getfather(rig[y] + 1)); n     y = getfather(y); n     if (fa[lef[y] - 1]) Merge(getfather(lef[y] - 1), y); n     printf( %I64d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 69; n const int MAXP = 2e6 + 69; n int n, q; n int val[MAXN]; n vector<int> a[MAXN]; n int check[MAXP]; n vector<int> p; n void build() { n   for (long long i = 2; i < MAXP; i++) { n     if (check[i] == 0) { n       p.push_back(i); n       check[i] = i; n       for (long long j = i * i; j < MAXP; j += i) { n         if (check[j] == 0) check[j] = i; n       } n     } n   } n } n int ans[MAXN]; n int h[MAXN]; n vector<int> prim[MAXP]; n void ptich(int i) { n   int x = val[i]; n   while (check[x] != 0) { n     int t = check[x]; n     if (!prim[t].empty()) { n       int u = prim[t].back(); n       if (h[u] > h[ans[i]]) { n         ans[i] = u; n       } n     } n     prim[t].push_back(i); n     while (x % t == 0) x /= t; n   } n } n void xoa(int i) { n   int x = val[i]; n   while (check[x] != 0) { n     int t = check[x]; n     prim[t].pop_back(); n     while (x % t == 0) x /= t; n   } n } n void dfs(int x, int p) { n   h[x] = h[p] + 1; n   ptich(x); n   for (int u : a[x]) { n     if (u == p) continue; n     dfs(u, x); n   } n   xoa(x); n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   build(); n   cin >> n >> q; n   for (int i = 1; i <= n; i++) cin >> val[i]; n   for (int i = 1; i < n; i++) { n     int x, y; n     cin >> x >> y; n     a[x].push_back(y); n     a[y].push_back(x); n   } n   dfs(1, 1); n   while (q--) { n     int type; n     cin >> type; n     if (type == 2) { n       for (int i = 1; i <= n; i++) ans[i] = 0, h[i] = 0; n       for (int i = 1; i < MAXP; i++) prim[i].clear(); n       int x, y; n       cin >> x >> y; n       val[x] = y; n       dfs(1, 1); n     } else { n       int x; n       cin >> x; n       if (ans[x] != 0) n         cout << ans[x] <<   n ; n       else n         cout << -1 <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int v_1, v_2, v_3, v_m, m_1, m_2, m_3; n   cin >> v_1 >> v_2 >> v_3 >> v_m; n   if (v_m >= v_2 || v_m < v_2 && v_m > v_3 && 2 * v_3 < v_m || n       v_m <= v_3 && 2 * v_m < v_3) { n     cout << -1; n     return 0; n   } n   cout << 2 * v_1 <<   n  << 2 * v_2 <<   n ; n   if (v_m > v_3) { n     cout << v_m; n   } else n     cout << v_3; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int MN = 1000010; n int exp(int x, int n) { n   int ret = 1; n   while (n) { n     if (n & 1) ret = 1LL * ret * x % mod; n     x = 1LL * x * x % mod; n     n >>= 1; n   } n   return ret; n } n int inv(int x) { return exp(x, mod - 2); } n int fact[MN], invf[MN]; n int K, W, ans; n int main() { n   fact[0] = 1; n   for (int i = 1; i < MN; i++) { n     fact[i] = 1LL * fact[i - 1] * i % mod; n   } n   for (int i = 0; i < MN; i++) { n     invf[i] = inv(fact[i]); n   } n   cin >> K >> W; n   for (int i = 1; i <= min(K, W - 1); i++) { n     ans += 1LL * i * fact[K] % mod * fact[K] % mod * exp(K, W - 1 - i) % mod * n            invf[K - i] % mod * invf[K - i] % mod; n     ans %= mod; n   } n   for (int i = W; i <= K; i++) { n     ans += 1LL * W * fact[K] % mod * fact[K - i + W - 1] % mod * invf[K - i] % n            mod * invf[K - i] % mod; n     ans %= mod; n   } n   for (int i = 1; i <= min(K - 1, W - 2); i++) { n     ans += 1LL * i * fact[K] % mod * fact[K] % mod * exp(K, W - 2 - i) % mod * n            invf[K - i] % mod * invf[K - i - 1] % mod; n     ans %= mod; n   } n   for (int i = W - 1; i <= K - 1; i++) { n     ans += 1LL * (W - 1) * fact[K] % mod * fact[K - i + W - 2] % mod * n            invf[K - i] % mod * invf[K - i - 1] % mod; n     ans %= mod; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int inf = 9e18; n const long double pi = 2 * acos(0.0); n long long int arr[100005]; n void solve() { n   long long int n, k; n   cin >> n >> k; n   ; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   ; n   long long int mn = inf, pos = 0; n   for (long long int i = 0; i < n; ++i) { n     if (arr[i] < mn) { n       mn = arr[i]; n       pos = i; n     } n   } n   long long int ans = 0; n   for (long long int i = 0; i < n; ++i) { n     if (i == pos) continue; n     long long int t = abs(k - arr[i]); n     ans += (t / mn); n   } n   cout << ans <<   n ; n   return; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long int tc = 1; n   cin >> tc; n   while (tc--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n string s; n vector<vector<int> > a[28]; n vector<int> t, v1, v2; n int ans = 1e9; n void gen(int ind) { n   if (ind == 3) { n     a[t[0] + t[1] + t[2]].push_back(t); n     return; n   } n   for (int i = 0; i <= 9; i++) { n     t.push_back(i); n     gen(ind + 1); n     t.pop_back(); n   } n } n int dif(vector<int> aa, vector<int> bb) { n   int ret = 0; n   for (int i = 0; i < aa.size(); i++) ret += (aa[i] != bb[i]); n   return ret; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> s; n   v1.push_back(s[0] -  0 ); n   v1.push_back(s[1] -  0 ); n   v1.push_back(s[2] -  0 ); n   v2.push_back(s[3] -  0 ); n   v2.push_back(s[4] -  0 ); n   v2.push_back(s[5] -  0 ); n   gen(0); n   for (int i = 0; i <= 27; i++) n     for (int j = 0; j < a[i].size(); j++) n       for (int k = 0; k < a[i].size(); k++) n         ans = min(ans, dif(v1, a[i][j]) + dif(v2, a[i][k])); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct trie { n   trie* child[2]; n   int cnt; n   trie() { n     child[0] = child[1] = NULL; n     cnt = 0; n   } n } * root; n int n, k, sum; n long long res; n void add(int val) { n   trie* node = root; n   for (int i = 29; i >= 0; i--) { n     int j = (val >> i) & 1; n     if (!node->child[j]) node->child[j] = new trie; n     node = node->child[j]; n     node->cnt++; n   } n } n void get(trie* node, int i, int val, int remain) { n   if (i == -1) return; n   if (remain > (1 << (i + 1)) - 1) return; n   int j = (val >> i) & 1; n   if (node->child[j ^ 1]) { n     if ((1 << i) >= remain) n       res += node->child[j ^ 1]->cnt; n     else n       get(node->child[j ^ 1], i - 1, val, remain - (1 << i)); n   } n   if (node->child[j]) get(node->child[j], i - 1, val, remain); n } n int main(int argc, char const* argv[]) { n   ios ::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   root = new trie; n   add(0); n   for (int i = 1; i <= n; i++) { n     int a; n     cin >> a; n     sum ^= a; n     get(root, 29, sum, k); n     add(sum); n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, A[1000100], A2[1000010], N; n int main() { n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> A[i]; n   } n   N = n; n   long long cumsum = 0; n   for (int i = 2, rank = 2; i <= n; i++) { n     long long cumsum2 = cumsum + (rank - 2) * A2[rank - 1]; n     long long d = cumsum2 - (rank - 1) * (N - rank) * A[i]; n     if (d < k) { n       cout << i << endl; n       N--; n     } else { n       A2[rank] = A[i]; n       rank++; n       cumsum = cumsum2; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> vis; n string s[1000009]; n int n, m, i; n bool dfs(int x, int y) { n   if (x == n - 1 && y == m - 1) return true; n   if (x < 0 || y < 0 || x >= n || y >= m || vis[y + x * m] == 1 || n       s[x][y] ==  # ) n     return false; n   if (x || y) { n     vis[y + x * m] = 1; n   } n   return dfs(x + 1, y) || dfs(x, y + 1); n } n int main() { n   cin >> n >> m; n   for (i = 0; i < n; i++) cin >> s[i]; n   if (dfs(0, 0) == false) { n     cout <<  0 ; n     return 0; n   } n   if (dfs(0, 0) == false) { n     cout <<  1 ; n     return 0; n   } n   cout <<  2 ; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int init[110]; n string lst[110]; n vector<int> fin; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (int i = (int)0; i < (int)n; ++i) cin >> lst[i]; n   for (int i = (int)0; i < (int)n; ++i) cin >> init[i]; n   bool flag = 1; n   while (flag) { n     flag = 0; n     for (int i = (int)0; i < (int)n; ++i) n       if (init[i] == 0) { n         flag = 1; n         fin.push_back(i + 1); n         for (int j = (int)0; j < (int)n; ++j) init[j] -= lst[i][j] -  0 ; n       } n   } n   cout << fin.size() << endl; n   for (int ele : fin) cout << ele <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mm[110][110]; n int tmp[110][110]; n void draw(int a, int b, int v) { n   for (int lx = 0; lx < a; lx++) n     for (int ly = 0; ly < b; ly++) tmp[lx][ly] += v; n   return; n } n void prt(int a, int b) { n   for (int lx = 0; lx < a; lx++) { n     for (int ly = 0; ly < b; ly++) printf( %c , tmp[lx][ly] ?  W  :  B ); n     puts(  ); n   } n   return; n } n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   for (int lx = 0; lx < n; lx++) { n     char buf[200]; n     scanf( %s , buf); n     for (int ly = 0; ly < m; ly++) mm[lx][ly] = (buf[ly] ==  W ); n   } n   int cnt = 1; n   for (int lx = 0; lx < n; lx++) n     for (int ly = 0; ly < m; ly++) tmp[lx][ly] = mm[n - 1][m - 1]; n   for (int cc = n + m - 3; cc >= 0; cc--) { n     for (int x = 0; x <= cc; x++) { n       int y = cc - x; n       if (x >= n or y >= m) continue; n       if (tmp[x][y] != mm[x][y]) { n         draw(x + 1, y + 1, mm[x][y] - tmp[x][y]), cnt++; n       } n     } n   } n   printf( %d n , cnt); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int gcd(long long int a, long long int b) { n   return (!b) ? a : gcd(b, a % b); n } n int n, k; n void solve() { n   cin >> n >> k; n   string s; n   cin >> s; n   vector<vector<int>> mvs; n   int mxs = 0; n   int cnt = 0; n   while (cnt <= n) { n     vector<int> cr; n     for (int i = 0; i <= (int)s.size() - 2; i++) { n       if (s[i] ==  R  && s[i + 1] ==  L ) { n         cr.push_back(i); n         s[i] =  L ; n         s[i + 1] =  R ; n         i += 1; n       } n     } n     if ((int)cr.size() == 0) break; n     mvs.push_back(cr); n     mxs += (int)cr.size(); n     cnt += 1; n   } n   if (k >= (int)mvs.size() && k <= mxs) { n     int rem = (int)mvs.size(); n     for (auto fv : mvs) { n       vector<int> cv = fv; n       int i = 0; n       while (i < (int)cv.size()) { n         int ch = 1; n         if (rem == k) ch = (int)cv.size() - i; n         cout << ch <<    ; n         for (int j = 0; j < ch; j++) { n           cout << cv[i] + 1 <<    ; n           i += 1; n         } n         cout <<   n ; n         k -= 1; n       } n       rem -= 1; n     } n   } else { n     cout << -1; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n vector<string> results(100001); n vector<pair<int, string> > wishes; n vector<pair<int, string> > S_M, M_L, L_XL, XL_XXL, XXL_XXXL; n bool compare(pair<int, string> a, pair<int, string> b) { n   if (a.second ==  S,M ) return false; n   if (a.second ==  M,L ) return b.second ==  S,M ; n   if (a.second ==  L,XL ) return b.second ==  S,M  || b.second ==  M,L ; n   if (a.second ==  XL,XXL ) return b.second !=  XXL,XXXL ; n   return true; n } n int main() { n   int S = 0, M = 0, L = 0, XL = 0, XXL = 0, XXXL = 0; n   cin >> S >> M >> L >> XL >> XXL >> XXXL; n   map<string, int> sizes; n   sizes[ S ] = S; n   sizes[ M ] = M; n   sizes[ L ] = L; n   sizes[ XL ] = XL; n   sizes[ XXL ] = XXL; n   sizes[ XXXL ] = XXXL; n   int n; n   string s; n   cin >> n; n   unsigned long long xx = 0; n   for (int i = 0; i < n; i++) { n     cin >> s; n     int pos = s.find( , ); n     if (pos == string::npos) { n       results[i] = s; n       if (sizes[s] > 0) n         sizes[s]--; n       else { n         cout <<  NO ; n         return 0; n       } n     } else { n       if (s ==  S,M ) S_M.push_back(make_pair(i, s)); n       if (s ==  M,L ) M_L.push_back(make_pair(i, s)); n       if (s ==  L,XL ) L_XL.push_back(make_pair(i, s)); n       if (s ==  XL,XXL ) XL_XXL.push_back(make_pair(i, s)); n       if (s ==  XXL,XXXL ) { n         XXL_XXXL.push_back(make_pair(i, s)); n         xx++; n       } n     } n   } n   wishes.insert(wishes.end(), S_M.begin(), S_M.end()); n   wishes.insert(wishes.end(), M_L.begin(), M_L.end()); n   wishes.insert(wishes.end(), L_XL.begin(), L_XL.end()); n   wishes.insert(wishes.end(), XL_XXL.begin(), XL_XXL.end()); n   wishes.insert(wishes.end(), XXL_XXXL.begin(), XXL_XXXL.end()); n   for (int i = 0; i < wishes.size(); i++) { n     pair<int, string> wish = wishes[i]; n     string sz = wish.second; n     int ind = wish.first; n     int pos = sz.find( , ); n     string first = sz.substr(0, pos); n     string second = sz.substr(pos + 1); n     if (sizes[first] == 0 && sizes[second] == 0) { n       cout <<  NO ; n       return 0; n     } n     if (sizes[first] != 0) { n       results[ind] = first; n       sizes[first]--; n     } else { n       results[ind] = second; n       sizes[second]--; n     } n   } n   cout <<  YES  << endl; n   for (int i = 0; i < n; i++) cout << results[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n struct TSubStr { n   TSubStr(const std::string& text, size_t begin, size_t end) n       : Text(text), Begin(begin), End(end) {} n   const std::string& Text; n   size_t Begin; n   size_t End; n   static inline size_t getId(const std::string& text, size_t begin, n                              size_t end) { n     size_t id = 0; n     size_t power = 1; n     for (size_t i = begin; i < end; ++i) { n       id += (text[i] -  a  + 1) * power; n       power *= 27; n     } n     return id; n   } n   static inline size_t getId(const std::string& str) { n     size_t id = 0; n     size_t power = 1; n     for (size_t i = 0; i < str.size(); ++i) { n       id += (str[i] -  a  + 1) * power; n       power *= 27; n     } n     return id; n   } n   inline size_t getId() const { n     size_t id = 0; n     size_t power = 1; n     for (size_t i = Begin; i < End; ++i) { n       id += (Text[i] -  a  + 1) * power; n       power *= 27; n     } n     return id; n   } n }; n void FillIndex(const std::string& text, n                std::vector<std::vector<size_t> >& index) { n   index.clear(); n   index.resize(27 * 27 * 27 * 27); n   for (size_t i = 0; i < text.size(); ++i) { n     index[TSubStr::getId(text, i, i + 1)].push_back(i); n     if (i + 1 < text.size()) index[TSubStr::getId(text, i, i + 2)].push_back(i); n     if (i + 2 < text.size()) index[TSubStr::getId(text, i, i + 3)].push_back(i); n     if (i + 3 < text.size()) index[TSubStr::getId(text, i, i + 4)].push_back(i); n   } n } n std::string ReverseStr(const std::string& str) { n   std::string res(str); n   std::reverse(res.begin(), res.end()); n   return res; n } n struct TProblemQuery { n   TProblemQuery(const std::string& firstStr, const std::string& secondStr) { n     if (firstStr < secondStr) { n       FirstStr = firstStr; n       SecondStr = secondStr; n     } else { n       FirstStr = secondStr; n       SecondStr = firstStr; n     } n   } n   TProblemQuery(const TProblemQuery& rhs) n       : FirstStr(rhs.FirstStr), SecondStr(rhs.SecondStr) {} n   TProblemQuery() {} n   bool operator==(const TProblemQuery& rhs) const { n     return FirstStr == rhs.FirstStr && SecondStr == rhs.SecondStr; n   } n   std::string FirstStr; n   std::string SecondStr; n }; n struct TProblemQueryHash { n   size_t operator()(const TProblemQuery& query) const { n     size_t x = HashFn(query.FirstStr); n     size_t y = HashFn(query.SecondStr); n     return (x * 1000000009) ^ y; n   } n   std::hash<std::string> HashFn; n }; n inline void TryImprove( n     const std::string& freqStr, const std::string& text, n     const std::vector<size_t>& freqStrOccurencies, n     std::unordered_map<TProblemQuery, size_t, TProblemQueryHash>& results, n     int freqStrI, int textI, int len) { n   if (textI + len > text.size()) { n     return; n   } n   TProblemQuery query(freqStr, text.substr(textI, len)); n   if (results.count(query)) { n     size_t& res = results[query]; n     size_t begI = std::min<size_t>(textI, freqStrOccurencies[freqStrI]); n     size_t endI = std::max<size_t>( n         textI + len, freqStrOccurencies[freqStrI] + freqStr.size()); n     size_t potentialRes = endI - begI; n     if (freqStrI > 0) { n       begI = std::min<size_t>(textI, freqStrOccurencies[freqStrI - 1]); n       endI = std::max<size_t>( n           textI + len, freqStrOccurencies[freqStrI - 1] + freqStr.size()); n       potentialRes = std::min<size_t>(potentialRes, endI - begI); n     } n     res = std::min<size_t>(res, potentialRes); n   } n } n void ProcessFreq( n     const std::string& freqStr, const std::string& text, n     const std::vector<std::vector<size_t> >& index, n     std::unordered_map<TProblemQuery, size_t, TProblemQueryHash>& results, n     std::vector<bool>& processedAsFreq) { n   const std::vector<size_t>& freqStrOccurencies = n       index[TSubStr::getId(freqStr)]; n   int freqStrI = 0; n   int textI = 0; n   while (textI < text.size()) { n     if (freqStrI < freqStrOccurencies.size() - 1 && n         textI > freqStrOccurencies[freqStrI]) { n       ++freqStrI; n       continue; n     } n     TryImprove(freqStr, text, freqStrOccurencies, results, freqStrI, textI, 1); n     TryImprove(freqStr, text, freqStrOccurencies, results, freqStrI, textI, 2); n     TryImprove(freqStr, text, freqStrOccurencies, results, freqStrI, textI, 3); n     TryImprove(freqStr, text, freqStrOccurencies, results, freqStrI, textI, 4); n     ++textI; n   } n   processedAsFreq[TSubStr::getId(freqStr)] = true; n } n void ProcessQuery( n     const TProblemQuery& query, const std::string& text, n     const std::vector<std::vector<size_t> >& index, n     std::unordered_map<TProblemQuery, size_t, TProblemQueryHash>& results, n     std::vector<bool>& processedAsFreq) { n   if (index[TSubStr::getId(query.FirstStr)].empty() || n       index[TSubStr::getId(query.SecondStr)].empty()) { n     return; n   } n   if (processedAsFreq[TSubStr::getId(query.FirstStr)] || n       processedAsFreq[TSubStr::getId(query.SecondStr)]) { n     return; n   } n   if (index[TSubStr::getId(query.FirstStr)].size() > 800) { n     ProcessFreq(query.FirstStr, text, index, results, processedAsFreq); n     return; n   } n   if (index[TSubStr::getId(query.SecondStr)].size() > 800) { n     ProcessFreq(query.SecondStr, text, index, results, processedAsFreq); n     return; n   } n   const std::vector<size_t>& firstStrOccurencies = n       index[TSubStr::getId(query.FirstStr)]; n   const std::vector<size_t>& secondStrOccurencies = n       index[TSubStr::getId(query.SecondStr)]; n   size_t& res = results[query]; n   int firstI = 0; n   int secondI = 0; n   int potentialSubstrBegin = n       std::min(firstStrOccurencies[firstI], secondStrOccurencies[secondI]); n   int potentialSubstrEnd = n       std::max(firstStrOccurencies[firstI] + query.FirstStr.size(), n                secondStrOccurencies[secondI] + query.SecondStr.size()); n   size_t potentialRes = potentialSubstrEnd - potentialSubstrBegin; n   res = std::min(res, potentialRes); n   while (secondI < secondStrOccurencies.size() && n          secondStrOccurencies[secondI] < firstStrOccurencies[firstI]) { n     potentialSubstrBegin = n         std::min(firstStrOccurencies[firstI], secondStrOccurencies[secondI]); n     potentialSubstrEnd = n         std::max(firstStrOccurencies[firstI] + query.FirstStr.size(), n                  secondStrOccurencies[secondI] + query.SecondStr.size()); n     potentialRes = potentialSubstrEnd - potentialSubstrBegin; n     res = std::min(res, potentialRes); n     ++secondI; n   } n   while (secondI < secondStrOccurencies.size()) { n     if (firstI < firstStrOccurencies.size() - 1 && n         firstStrOccurencies[firstI + 1] < secondStrOccurencies[secondI]) { n       ++firstI; n       continue; n     } n     potentialSubstrBegin = n         std::min(firstStrOccurencies[firstI], secondStrOccurencies[secondI]); n     potentialSubstrEnd = n         std::max(firstStrOccurencies[firstI] + query.FirstStr.size(), n                  secondStrOccurencies[secondI] + query.SecondStr.size()); n     potentialRes = potentialSubstrEnd - potentialSubstrBegin; n     res = std::min(res, potentialRes); n     if (firstI < firstStrOccurencies.size() - 1) { n       potentialSubstrBegin = std::min(firstStrOccurencies[firstI + 1], n                                       secondStrOccurencies[secondI]); n       potentialSubstrEnd = n           std::max(firstStrOccurencies[firstI + 1] + query.FirstStr.size(), n                    secondStrOccurencies[secondI] + query.SecondStr.size()); n       potentialRes = potentialSubstrEnd - potentialSubstrBegin; n       res = std::min(res, potentialRes); n     } n     ++secondI; n   } n } n int main() { n   std::string text; n   std::cin >> text; n   std::vector<std::vector<size_t> > index; n   FillIndex(text, index); n   std::vector<bool> processedAsFreq(27 * 27 * 27 * 27, false); n   std::unordered_map<TProblemQuery, size_t, TProblemQueryHash> results; n   results.max_load_factor(3.0); n   results.reserve(300000); n   std::vector<TProblemQuery> queries; n   size_t m; n   std::cin >> m; n   size_t NO_ANSWER = 1000000009; n   for (size_t i = 0; i < m; ++i) { n     std::string strFirst; n     std::string strSecond; n     std::cin >> strFirst; n     std::cin >> strSecond; n     TProblemQuery query(strFirst, strSecond); n     queries.push_back(query); n     results[query] = NO_ANSWER; n   } n   for (size_t i = 0; i < m; ++i) { n     ProcessQuery(queries[i], text, index, results, processedAsFreq); n     size_t res = results[queries[i]]; n     std::cout << (res == NO_ANSWER ? -1 : (int)res) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   int mex = 1e9; n   for (int i = 0; i < m; i++) { n     int l, r; n     cin >> l >> r; n     mex = min(mex, r - l + 1); n   } n   cout << mex <<   n ; n   for (int i = 0; i < n; i++) { n     cout << i % mex <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int maxn = 1e4 + 5; n class Matrix { n  public: n   long long a[3][3]; n   int n; n   void Init(int key) { n     memset(a, 0, sizeof a); n     if (key) n       for (int i = 0; i < n; i++) a[i][i] = 1; n   } n   Matrix operator*(const Matrix& b) { n     Matrix c; n     c.n = n; n     c.Init(0); n     for (int i = 0; i < n; i++) n       for (int j = 0; j < n; j++) n         for (int k = 0; k < n; k++) n           c.a[i][j] = (c.a[i][j] + a[i][k] * b.a[k][j] % mod) % mod; n     return c; n   } n   Matrix Power(long long t) { n     Matrix ans, p = *this; n     ans.n = p.n; n     ans.Init(1); n     while (t) { n       if (t & 1) ans = ans * p; n       p = p * p; n       t >>= 1; n     } n     return ans; n   } n   void Print() { n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < n; j++) { n         if (j == 0) n           printf( %lld , a[i][j]); n         else n           printf(  %lld , a[i][j]); n       } n       puts(  ); n     } n   } n }; n struct node { n   int x, tag; n   long long y; n   friend bool operator<(const node& a, const node& b) { return a.y < b.y; } n } a[maxn * 2]; n long long sum[3], b[3]; n void calc(long long n) { n   Matrix f, A; n   f.Init(0); n   A.Init(0); n   f.n = A.n = 3; n   for (int i = 0; i < 3; i++) { n     for (int j = 0; j < 3; j++) { n       if (sum[j] == 0) n         f.a[i][j] = 1; n       else n         f.a[i][j] = 0; n     } n   } n   f.a[0][2] = f.a[2][0] = 0; n   A.a[0][0] = b[0], A.a[0][1] = b[1], A.a[0][2] = b[2]; n   A = A * f.Power(n); n   b[0] = A.a[0][0], b[1] = A.a[0][1], b[2] = A.a[0][2]; n } n int main() { n   int n; n   long long m; n   scanf( %d%lld , &n, &m); n   int cnt = 0; n   for (int i = 1; i <= n; i++) { n     int x; n     long long l, r; n     scanf( %d%lld%lld , &x, &l, &r); n     a[++cnt].x = x - 1, a[cnt].y = l - 1, a[cnt].tag = 1; n     a[++cnt].x = x - 1, a[cnt].y = r, a[cnt].tag = -1; n   } n   sort(a + 1, a + cnt + 1); n   b[0] = b[2] = 0, b[1] = 1; n   long long cur = 1; n   for (int i = 1; i <= cnt; i++) { n     calc(a[i].y - cur); n     cur = a[i].y; n     sum[a[i].x] += a[i].tag; n   } n   calc(m - cur); n   printf( %lld n , b[1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline bool smin(T &a, const T &b) { n   return b < a ? a = b, 1 : 0; n } n template <typename T> n inline bool smax(T &a, const T &b) { n   return a < b ? a = b, 1 : 0; n } n const long long N = 1e5 + 10, mod = (long long)1e9 + 7; n long long n, k, ps[N], f[N], inv[N], pw10[N]; n string s; n long long pw(long long x, long long y) { n   x %= mod; n   long long ret = 1; n   for (; y; y >>= 1, (x *= x) %= mod) n     if (y & 1) (ret *= x) %= mod; n   return ret; n } n long long C(long long n, long long r) { n   if (r > n || r < 0 || n < 0) return 0; n   return ((f[n] * inv[r] % mod) * inv[n - r] % mod) % mod; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k >> s; n   ps[0] = s[0] -  0 ; n   for (long long i = 1; i < n; ++i) (ps[i] = ps[i - 1] + (s[i] -  0 )) %= mod; n   f[0] = inv[0] = 1; n   for (long long i = 1; i < N; ++i) { n     (f[i] = f[i - 1] * i) %= mod; n     inv[i] = pw(f[i], mod - 2); n   } n   pw10[0] = 1; n   for (long long i = 1; i < N; ++i) (pw10[i] = pw10[i - 1] * 10) %= mod; n   long long res = 0; n   for (long long sz = 0; sz <= n - 2; ++sz) { n     long long add = n         pw10[sz] * C(n - sz - 2, k - 1) % mod * ps[n - sz - 2] % mod; n     (res += add) %= mod; n   } n   for (long long i = 0; i < n; ++i) { n     long long add = (s[i] -  0 ) * pw10[n - i - 1] % mod * C(i, k); n     (res += add) %= mod; n   } n   cout << res <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n const int inf = 0x3f3f3f3f; n const double eps = 1e-8; n map<int, int> ma; n int n, x, num; n int getId(int x) { n   if (ma.count(x) == 0) { n     ma[x] = num++; n   } n   return ma[x]; n } n int cnt[maxn]; n vector<int> v; n int main() { n   while (~scanf( %d , &n)) { n     memset(cnt, 0, sizeof(cnt)); n     num = 1; n     ma.clear(); n     v.clear(); n     for (int i = 1; i <= n; i++) { n       scanf( %d , &x); n       cnt[getId(x)]++; n       x = getId(x); n     } n     for (int i = 0; i <= n + 1; i++) { n       if (cnt[i] != 0) v.push_back(cnt[i]); n     } n     sort(v.begin(), v.end()); n     int now = 0, ans = 0; n     int len = v.size(); n     for (int i = 1; i <= v[len - 1]; i++) { n       int sum = 0; n       now = 0; n       for (int j = i;;) { n         int pos = lower_bound(v.begin() + now, v.end(), j) - v.begin(); n         if (pos == len) break; n         now = pos + 1; n         sum += j; n         j = j * 2; n       } n       ans = max(ans, sum); n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC target(   n      sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC optimize( fast-math ) n using namespace std; n mt19937 rnd(time(0) + 228 +  k  +  e  +  k  +  e  +  r  +  o  +  f  +  e  + n              y ); n vector<int> genvec(int sz, int minelem, int maxelem) { n   ++maxelem; n   vector<int> ans(sz); n   for (int i = 0; i < sz; ++i) { n     ans[i] = minelem + rnd() % (maxelem - minelem); n   } n   return ans; n } n const long double EPS = 1e-14; n bool checkprime(int x) { n   for (int i = 2; i * i <= x; ++i) n     if (x % i == 0) return 0; n   return 1; n } n const long double PI = acos(-1); n const int MOD7 = 1000000007; n const int MOD9 = 1000000009; n const long long INF = 2e18; n int mod = MOD7; n const int inf = 1e9; n signed main() { n   ios_base::sync_with_stdio(0); n   cout.tie(0); n   cin.tie(0); n   srand(time(NULL)); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<int> p(2 * n); n     for (long long i = 0; i < p.size(); ++i) cin >> p[i]; n     ; n     int mx = 0; n     vector<int> luls; n     int cnt = 0; n     for (int i : p) { n       if (i > mx) { n         mx = i; n         if (cnt) luls.push_back(cnt); n         cnt = 1; n       } else { n         ++cnt; n       } n     } n     luls.push_back(cnt); n     vector<bool> dp(n + 1); n     dp[0] = 1; n     int ps = 0; n     for (int i : luls) { n       ps += i; n       for (int sum = n; sum >= 0; --sum) { n         if (!dp[sum]) continue; n         if (ps - sum - i > n) continue; n         if (sum + i <= n) { n           dp[sum + i] = 1; n         } n       } n     } n     cout << (dp[n] ?  YES  :  NO ) << endl; n   } n } n


#include <bits/stdc++.h> n const int N = 1e5 + 10; n const int LOGN = 20; n long long ans; n int n; n int pre[N << 1], to[N << 1], head[N], wcnt; n long long dis[N]; n int val[N], fa[N]; n int dep[N]; n int table[LOGN][N << 1]; n int LOG2[N << 1]; n int idcnt; n int st[N]; n inline int minp(int x, int y) { return (dep[x] < dep[y]) ? x : y; } n inline void swap(int &x, int &y) { n   int t = x; n   x = y; n   y = t; n } n template <class T> n inline T max(const T &x, const T &y) { n   return (x < y) ? y : x; n } n inline void addedge(int u, int v) { n   wcnt++; n   pre[wcnt] = head[u]; n   head[u] = wcnt; n   to[wcnt] = v; n } n inline void add2edge(int u, int v) { n   addedge(u, v); n   addedge(v, u); n } n void init() { n   int i, j, k; n   for (i = int(2); i <= int(idcnt); i++) LOG2[i] = LOG2[i >> 1] + 1; n   for (i = k = 1; k <= idcnt; i++, k <<= 1) n     for (j = 1; j <= int(idcnt - k); j++) n       table[i][j] = minp(table[i - 1][j], table[i - 1][j + k]); n } n inline long long getdis(int x, int y) { n   int l, r, k, z; n   l = st[x]; n   r = st[y]; n   if (l > r) swap(l, r); n   k = LOG2[r - l + 1]; n   z = minp(table[k][l], table[k][r - (1 << k) + 1]); n   return dis[x] + dis[y] - 2 * dis[z] + val[z]; n } n struct Node { n   int x, y; n   long long d; n   Node() { n     x = y = 0; n     d = -1; n   } n   Node(int x, int y) : x(x), y(y) { d = getdis(x, y); } n   bool operator<(const Node &b) const { return d < b.d; } n   Node operator+(const Node &b) const { n     if (d == -1) n       return b; n     else if (b.d == -1) n       return *this; n     Node res = max(*this, b); n     res = max(res, Node(x, b.x)); n     res = max(res, Node(x, b.y)); n     res = max(res, Node(y, b.x)); n     res = max(res, Node(y, b.y)); n     return res; n   } n }; n Node sum[N], g[N]; n void predfs(int u) { n   int i; n   table[0][st[u] = ++idcnt] = u; n   dis[u] = dis[fa[u]] + val[u]; n   for (i = head[u]; i; i = pre[i]) n     if (fa[u] ^ to[i]) { n       fa[to[i]] = u; n       dep[to[i]] = dep[u] + 1; n       predfs(to[i]); n       table[0][++idcnt] = u; n     } n } n void dfs1(int u) { n   int i; n   sum[u] = Node(u, u); n   for (i = head[u]; i; i = pre[i]) n     if (fa[u] ^ to[i]) { n       dfs1(to[i]); n       sum[u] = sum[u] + sum[to[i]]; n     } n } n void dfs2(int u) { n   static Node pref[N], suff[N]; n   static int son[N]; n   int i, sn = 0; n   for (i = head[u]; i; i = pre[i]) n     if (fa[u] ^ to[i]) son[++sn] = to[i]; n   pref[0] = suff[sn + 1] = g[u] + Node(u, u); n   for (i = 1; i <= int(sn); i++) pref[i] = pref[i - 1] + sum[son[i]]; n   for (i = int(sn); i; i--) suff[i] = suff[i + 1] + sum[son[i]]; n   for (i = 1; i <= int(sn); i++) g[son[i]] = pref[i - 1] + suff[i + 1]; n   for (i = int(0); i <= int(sn + 1); i++) pref[i] = suff[i] = Node(); n   if (u > 1) ans = max(ans, sum[u].d + g[u].d); n   for (i = head[u]; i; i = pre[i]) n     if (fa[u] ^ to[i]) dfs2(to[i]); n } n int main() { n   int i; n   int u, v; n   scanf( %d , &n); n   for (i = 1; i <= int(n); i++) scanf( %d , &val[i]); n   for (i = 1; i <= int(n - 1); i++) { n     scanf( %d %d , &u, &v); n     add2edge(u, v); n   } n   predfs(1); n   init(); n   dfs1(1); n   ans = sum[1].d; n   dfs2(1); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n ifstream fin( input.txt ); n ofstream fout( output.txt ); n bool isPrime(int x) { n   if (x <= 4 || x % 2 == 0 || x % 3 == 0) return x == 2 || x == 3; n   for (int i = 5; i * i <= x; i += 6) n     if (x % i == 0 || x % (i + 2) == 0) return 0; n   return 1; n } n inline void boostIO() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n int main() { n   boostIO(); n   int n, s; n   cin >> n >> s; n   int prev = 0; n   int h = 0; n   int m = 0; n   for (int i = 0; i < (n); ++i) { n     cin >> h >> m; n     m += h * 60; n     if (i == 0 && m - prev >= s + 1) { n       cout << 0 <<     << 0 << endl; n       return 0; n     } n     if (m - prev >= s * 2 + 2) { n       cout << (prev + s + 1) / 60 <<     << (prev + s + 1) % 60 << endl; n       return 0; n     } n     prev = m; n   } n   cout << (prev + s + 1) / 60 <<     << (prev + s + 1) % 60; n   return 0; n } n


#include <bits/stdc++.h> n inline int read() { n   int data = 0, w = 1; n   char ch = getchar(); n   while (ch !=  -  && (ch <  0  || ch >  9 )) ch = getchar(); n   if (ch ==  - ) w = -1, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) data = data * 10 + (ch ^ 48), ch = getchar(); n   return data * w; n } n const int N(1e5 + 10), M(105), Mod(998244353); n int n, K, L, f[N][M], s[N], len[N][M], a[N]; n int main() { n   n = read(), K = read(), L = read(); n   for (int i = 1; i <= n; i++) a[i] = read(); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= K; j++) n       if (a[i] == -1 || a[i] == j) len[i][j] = len[i - 1][j] + 1; n   s[0] = 1; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= K; j++) n       if (a[i] == -1 || a[i] == j) { n         f[i][j] = s[i - 1]; n         if (i >= L && len[i][j] >= L) n           f[i][j] = (f[i][j] - s[i - L] + Mod) % Mod, n           f[i][j] = (f[i][j] + f[i - L][j]) % Mod; n         s[i] = (s[i] + f[i][j]) % Mod; n       } n   printf( %d n , s[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5006; n const int mod = 1000000009; n double x[maxn], y[maxn], z[maxn]; n double d[maxn][maxn]; n int n; n double dis(double x1, double y1, double z1, double x2, double y2, double z2) { n   double dx = x1 - x2; n   double dy = y1 - y2; n   double dz = z1 - z2; n   return sqrt(dx * dx + dy * dy + dz * dz); n } n double getans() { n   double ret = 100000000000.0; n   for (int i = 1; i < n; ++i) n     for (int j = i + 1; j < n; ++j) { n       double dd = d[i][j] + d[0][i] + d[0][j]; n       ret = min(ret, dd); n     } n   return ret; n } n void init() { n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < n; ++j) n       d[i][j] = dis(x[i], y[i], z[i], x[j], y[j], z[j]); n } n int main() { n   while (~scanf( %d , &n)) { n     for (int i = 0; i < n; ++i) { n       scanf( %lf%lf%lf , x + i, y + i, z + i); n     } n     init(); n     double ans = getans(); n     ans /= 2; n     printf( %.9lf n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n template <typename Y> n inline bool updmin(Y &a, Y b) { n   if (a > b) { n     a = b; n     return 1; n   } n   return 0; n } n template <typename Y> n inline bool updmax(Y &a, Y b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <typename Y> n inline Y abs(Y a) { n   if (a < 0) a = -a; n   return a; n } n template <typename Y> n inline Y sqr(Y a) { n   return a * a; n } n int read() { n   int w = 1, q = 0, ch =    ; n   for (; ch <  0  || ch >  9 ; ch = getchar()) n     if (ch ==  - ) w = -1; n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) q = q * 10 + ch - 48; n   return q * w; n } n inline void FileIO() { n   freopen( n          n        .in , n        r , stdin); n   freopen( n          n        .out , n        w , stdout); n } n const int maxn = 800010; n std::vector<std::pair<int, long long> > G[maxn]; n int n, m, fa[maxn]; n long long a[maxn], d[maxn], dk[maxn], df[maxn]; n int dep[maxn], dfn[maxn], st[maxn][21], dfc = 0, lg[maxn]; n int lca(int a, int b) { n   a = dfn[a]; n   b = dfn[b]; n   if (a > b) { n     std::swap(a, b); n   } n   int L = lg[b - a], u = st[a][L], v = st[b - (1 << L) + 1][L]; n   return dep[u] < dep[v] ? u : v; n } n void up(int p) { n   d[p] = a[p]; n   st[++dfc][0] = p; n   dfn[p] = dfc; n   dep[p] = dep[fa[p]] + 1; n   for (auto t : G[p]) { n     if (t.first == fa[p]) continue; n     fa[t.first] = p; n     up(t.first); n     dk[t.first] = std::max(0LL, d[t.first] - 2 * t.second); n     d[p] += dk[t.first]; n     st[++dfc][0] = p; n   } n } n void down(int p) { n   for (auto t : G[p]) { n     if (t.first == fa[p]) continue; n     df[t.first] = std::max(0LL, d[p] - dk[t.first] - 2 * t.second); n     d[t.first] += df[t.first]; n     down(t.first); n   } n } n long long ans[maxn]; n std::vector<std::pair<int, int> > Q[maxn]; n long long tr[maxn]; n inline void add(int p, long long v) { n   if (!p) return; n   for (; p <= n; p += p & -p) { n     tr[p] += v; n   } n } n inline long long ask(int p) { n   long long r = 0; n   for (; p; p -= p & -p) { n     r += tr[p]; n   } n   return r; n } n inline long long ask(int l, int r) { return ask(r) - ask(l - 1); } n void dfs(int p, long long ev) { n   add(dep[p] - 1, -ev - dk[p]); n   add(dep[p], d[p] - df[p]); n   for (auto q : Q[p]) { n     ans[q.second] += ask(dep[q.first], dep[p]); n   } n   for (auto t : G[p]) { n     if (t.first == fa[p]) continue; n     dfs(t.first, t.second); n   } n   add(dep[p] - 1, ev + dk[p]); n   add(dep[p], df[p] - d[p]); n } n int main() { n   n = read(); n   m = read(); n   for (int i = 1; i <= n; i++) { n     a[i] = read(); n   } n   for (int i = 1; i < n; i++) { n     int x = read(), y = read(); n     long long z = read(); n     G[x].push_back(std::make_pair(y, z)); n     G[y].push_back(std::make_pair(x, z)); n   } n   up(1); n   down(1); n   lg[1] = 0; n   for (int i = 2; i <= dfc; i++) { n     lg[i] = lg[i >> 1] + 1; n   } n   for (int k = 1; k <= 20; k++) { n     for (int i = 1; i + (1 << k) - 1 <= dfc; i++) { n       int u = st[i][k - 1], v = st[i + (1 << (k - 1))][k - 1]; n       st[i][k] = dep[u] < dep[v] ? u : v; n     } n   } n   for (int i = 1; i <= m; i++) { n     int u = read(), v = read(); n     if (u == v) { n       ans[i] = d[u]; n       continue; n     } n     int l = lca(u, v); n     if (l != u && l != v) { n       ans[i] += 2 * df[l] - d[l]; n     } else { n       ans[i] += df[l]; n     } n     if (l != u) { n       Q[u].push_back(std::make_pair(l, i)); n     } n     if (l != v) { n       Q[v].push_back(std::make_pair(l, i)); n     } n   } n   dfs(1, 0); n   for (int i = 1; i <= m; i++) { n     printf( %lld n , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int main() { n   long long k, d, t; n   scanf( %lld %lld %lld , &k, &d, &t); n   if (k % d == 0) { n     printf( %.10lf n , (double)t); n     return 0; n   } n   t *= 2; n   long long period = (k / d + 1) * d; n   long long periodPoints = 2 * k + period - k; n   long long nPeriods = t / periodPoints; n   long long timeSpent = nPeriods * periodPoints; n   long long rest = t - timeSpent; n   if (rest == 0) { n     printf( %.10lf n , (double)nPeriods * period); n     return 0; n   } n   if (2 * k >= rest) { n     printf( %.10lf n , (double)nPeriods * period + rest / 2.0); n     return 0; n   } n   printf( %.10lf n , (double)nPeriods * period + rest - k); n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void gn(T &first) { n   char c, sg = 0; n   while (c = getchar(), (c >  9  || c <  0 ) && c !=  - ) n     ; n   for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ; n        c = getchar()) n     first = (first << 1) + (first << 3) + c -  0 ; n   if (sg) first = -first; n } n template <class T1, class T2> n inline void gn(T1 &x1, T2 &x2) { n   gn(x1), gn(x2); n } n char S1[1000100], S2[1000100]; n char B[1000100 * 3]; n int fail[1000100 * 3]; n void build_fail_function(char B[], int len) { n   int j = fail[0] = -1; n   for (int i = 1; i < len; i++) { n     while (j != -1 and B[j + 1] != B[i]) j = fail[j]; n     if (B[j + 1] == B[i]) j++; n     fail[i] = j; n   } n } n int main() { n   int n; n   gn(n); n   n--; n   scanf( %s %s , S1, S2); n   for (int i = 0; i < n; i++) { n     if (S1[i] ==  N ) n       S1[i] =  S ; n     else if (S1[i] ==  S ) n       S1[i] =  N ; n     else if (S1[i] ==  W ) n       S1[i] =  E ; n     else n       S1[i] =  W ; n   } n   int nn = 0; n   for (int i = 0; i < n; i++) B[nn++] = S1[n - i - 1]; n   B[nn++] =  * ; n   for (int i = 0; i < n; i++) B[nn++] = S2[i]; n   build_fail_function(B, nn); n   if (fail[nn - 1] == -1) return puts( YES ); n   puts( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2500 + 5; n int cnt = 0; n int rec[MAXN], mp[MAXN][MAXN], dp[MAXN][MAXN]; n bool prime[MAXN]; n void init(int n) { n   memset(prime, true, sizeof(prime)); n   prime[0] = prime[1] = false; n   for (int i = 2; i <= n; i++) { n     if (prime[i]) rec[cnt++] = i; n     for (int j = 0; j < cnt && rec[j] <= n / i; j++) { n       prime[i * rec[j]] = false; n       if (i % rec[j] == 0) break; n     } n   } n } n int main() { n   init(600); n   int n, m; n   scanf( %d%d , &n, &m); n   memset(mp, 0, sizeof(mp)); n   memset(dp, 0, sizeof(dp)); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       scanf( %1d , &mp[i][j]); n       dp[i][j] = dp[i][j - 1] + dp[i - 1][j] - dp[i - 1][j - 1] + mp[i][j]; n     } n   int ans = dp[n][m], up = max(n, m); n   for (int i = 0; rec[i] < up && i < cnt; i++) { n     int tmp = 0; n     for (int j = 0; j < n + rec[i]; j += rec[i]) n       for (int k = 0; k < m + rec[i]; k += rec[i]) { n         int sum = dp[min(n, j + rec[i])][min(m, k + rec[i])] - n                   dp[min(n, j)][min(m, k + rec[i])] - n                   dp[min(n, j + rec[i])][min(m, k)] + dp[min(n, j)][min(m, k)]; n         tmp += min(rec[i] * rec[i] - sum, sum); n       } n     ans = min(ans, tmp); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> prefix_function(string t, string s, int k) { n   int m = (int)s.size(); n   s = t +  ?  + s; n   int n = (int)s.length(); n   vector<int> pi(n); n   for (int i = 1; i < n; ++i) { n     int j = pi[i - 1]; n     while (j > 0 && s[i] != s[j]) j = pi[j - 1]; n     if (s[i] == s[j]) ++j; n     pi[i] = j; n   } n   if (k >= (int)t.size()) { n     for (int i = t.size() + 1, ii = 1; i < n; i++, ii++) { n       if (pi[i] == t.size()) { n         if (m - ii >= k) { n           cout <<  Yes  << endl; n           int x = ii - k + 1, y = ii + 1; n           if (x <= 0) y += 1 - x, x += 1 - x; n           cout << x <<     << y << endl; n           exit(0); n         } n         int jj = ii - (int)t.size(); n         if (jj >= k) { n           cout <<  Yes  << endl; n           int x = jj - k + 1, y = jj + 1; n           if (y > m - k + 1) x += m - k + 1 - y, y += m - k + 1 - y; n           cout << x <<     << y << endl; n           exit(0); n         } n       } n     } n   } n   vector<int> occ(t.size() + 1, -1); n   for (int i = t.size() + k; i < n; i++) { n     int j = pi[i]; n     int idx = i - t.size() - 1; n     while (occ[j] == -1) { n       occ[j] = idx; n       if (j == 0) break; n       j = pi[j - 1]; n     } n   } n   return occ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, m, k; n   cin >> n >> m >> k; n   string s, t; n   cin >> s >> t; n   vector<int> L = prefix_function(t, s, k); n   reverse(t.begin(), t.end()); n   reverse(s.begin(), s.end()); n   vector<int> R = prefix_function(t, s, k); n   for (int i = 0; i < R.size(); i++) { n     if (R[i] == -1) continue; n     R[i] = n - 1 - R[i]; n   } n   for (int l = 0; l <= m; l++) { n     int r = m - l; n     if (L[l] == -1 or R[r] == -1 or l > k or r > k) continue; n     if (L[l] < R[r]) { n       cout <<  Yes  << endl; n       cout << L[l] - k + 1 + 1 <<     << R[r] + 1 << endl; n       exit(0); n     } n   } n   cout <<  No  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1010; n int n, m; n int fw[N][N], fb[N][N]; n int dx[8] = {-2, -2, 2, 2, 1, 1, -1, -1}; n int dy[8] = {1, -1, 1, -1, 2, -2, 2, -2}; n queue<pair<int, int> > q; n void bfs(int x, int y, int f[][N]) { n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) f[i][j] = -1; n   f[x][y] = 0; n   q.push(pair<int, int>(x, y)); n   while (!q.empty()) { n     pair<int, int> h = q.front(); n     q.pop(); n     int x = h.first, y = h.second; n     for (int i = 0; i < 8; ++i) { n       int xx = x + dx[i], yy = y + dy[i]; n       if (xx <= 0 || xx > n || yy <= 0 || yy > m) continue; n       if (f[xx][yy] == -1) { n         f[xx][yy] = f[x][y] + 1; n         q.push(pair<int, int>(xx, yy)); n       } n     } n   } n } n void go(int x, int y, int f[][N]) { n   while (f[x][y]) { n     for (int i = 0; i < 8; ++i) { n       int xx = x + dx[i], yy = y + dy[i]; n       if (xx <= 0 || xx > n || yy <= 0 || yy > m) continue; n       if (f[xx][yy] != -1 && f[xx][yy] < f[x][y]) { n         x = xx, y = yy; n         printf( %d %d n , x, y); n         fflush(stdout); n         break; n       } n     } n   } n } n int main() { n   int xw, yw, xb, yb, hn, hm; n   scanf( %d%d , &n, &m); n   scanf( %d%d%d%d , &xw, &yw, &xb, &yb); n   hn = n >> 1, hm = m >> 1; n   bfs(hn, hm, fw); n   bfs(hn + 1, hm, fb); n   double tw = fw[xw][yw]; n   double tb = fb[xb][yb] + 0.5; n   double twb = fb[xw][yw]; n   double tbb = fw[xb][yb] + 0.5; n   if (((xw + yw) & 1) ^ ((xb + yb) & 1)) { n     if (tw < tb) { n       puts( WHITE ); n       go(xw, yw, fw); n     } else if (twb < tb + 1) { n       puts( WHITE ); n       if (fb[xw][yw]) { n         go(xw, yw, fb); n       } else { n         printf( %d %d n , xw - 2, yw - 1); n         printf( %d %d n , xw, yw); n       } n       go(hn + 1, hm, fw); n     } else { n       puts( BLACK ); n       go(xb, yb, fb); n     } n   } else { n     if (tb < tw) { n       puts( BLACK ); n       go(xb, yb, fb); n     } else if (tbb < tw + 1) { n       puts( BLACK ); n       if (fw[xb][yb]) { n         go(xb, yb, fw); n       } else { n         printf( %d %d n , xb - 2, yb - 1); n         printf( %d %d n , xb, yb); n       } n       go(hn, hm, fb); n     } else { n       puts( WHITE ); n       go(xw, yw, fw); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int N; n   std::cin >> N; n   std::set<std::pair<int, int>> doubled; n   std::set<std::pair<int, int>> pend; n   int to_double = 0; n   int f_doubled = 0; n   int64_t total_damage = 0; n   int t; n   int d; n   for (int i = 0; i < N; i++) { n     std::cin >> t; n     std::cin >> d; n     total_damage += d; n     to_double += t * (d > 0 ? 1 : -1); n     if (d > 0) { n       pend.insert({d, t}); n     } else { n       auto search = doubled.find({-d, t}); n       if (search != doubled.end()) { n         doubled.erase(search); n         if (t == 0) f_doubled--; n         total_damage += d; n       } else { n         pend.erase(pend.find({-d, t})); n       } n       if (t == 1) { n         if (doubled.size() > 0) { n           auto it_doubled = doubled.begin(); n           std::pair<int, int> max = *it_doubled; n           doubled.erase(it_doubled); n           pend.insert(max); n           if (max.second == 0) f_doubled--; n           total_damage -= max.first; n         } n       } n     } n     if (to_double > doubled.size()) { n       if (pend.size() > 0) { n         auto it_pend = --pend.end(); n         std::pair<int, int> max = *it_pend; n         doubled.insert(max); n         pend.erase(it_pend); n         if (max.second == 0) f_doubled++; n         total_damage += max.first; n       } n     } else if (doubled.size() == to_double) { n       if (to_double > 0 && pend.size() > 0) { n         auto it_doubled = doubled.begin(); n         auto it_pend = --pend.end(); n         std::pair<int, int> min = *it_doubled; n         std::pair<int, int> max = *it_pend; n         if (max.first > min.first) { n           if (max.second == 0) f_doubled++; n           if (min.second == 0) f_doubled--; n           doubled.erase(it_doubled); n           pend.erase(it_pend); n           doubled.insert(max); n           pend.insert(min); n           total_damage += max.first - min.first; n         } n       } n     } n     if (doubled.size() > 0 && f_doubled == 0 && doubled.size() == to_double) { n       auto it_doubled = doubled.begin(); n       doubled.erase(it_doubled); n       std::pair<int, int> min = *it_doubled; n       total_damage -= min.first; n       if (pend.size() > 0) { n         auto it_pend = --pend.end(); n         std::pair<int, int> max = *it_pend; n         pend.erase(it_pend); n         doubled.insert(max); n         f_doubled++; n         total_damage += max.first; n       } n       pend.insert(min); n     } n     printf( %lld n , total_damage); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 5005, MAXC = 1000005; n int a[MAXN], b[MAXC], c[MAXC]; n int n, k, _max; n int main() { n   if (0) { n     freopen( input.txt ,  r , stdin); n     freopen( output.txt ,  w , stdout); n   }; n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]), _max = max(_max, a[i]); n   for (int i = 1; i < n; i++) n     for (int j = i + 1; j <= n; j++) b[abs(a[i] - a[j])]++; n   for (int res = n - k; res <= 1000001; res++) { n     int cnt = 0; n     bool ok = true; n     for (int i = res; i <= _max; i += res) { n       cnt += b[i]; n       if (cnt > (k + 1) * k / 2) { n         ok = false; n         break; n       } n     } n     if (!ok) continue; n     int x = 0; n     for (int i = 1; i <= n; i++) { n       if (c[a[i] % res]) x++; n       c[a[i] % res]++; n     } n     if (x <= k) { n       cout << res << endl; n       return 0; n     } n     for (int i = 1; i <= n; i++) c[a[i] % res] = 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxnum = 2005; n char str[maxnum][maxnum]; n int state[maxnum]; n int main() { n   int m, n; n   while (~scanf( %d%d , &n, &m)) { n     memset(state, 0, sizeof(state)); n     for (int i = 0; i < n; ++i) { n       scanf( %s , str[i]); n       for (int j = 0; j < m; ++j) { n         if (str[i][j] ==  1 ) state[j] +=  1 ; n       } n     } n     bool ans = false; n     for (int i = 0; i < n; ++i) { n       bool judge = true; n       for (int j = 0; j < m; ++j) { n         if (judge == false) break; n         if (str[i][j] ==  1 ) { n           judge = state[j] -  1 ; n         } n         if (str[i][j] ==  0 ) { n           judge = state[j]; n         } n       } n       if (judge) { n         ans = true; n         break; n       } n     } n     if (ans) n       printf( YES n ); n     else n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   int to, next, w; n   edge(int _to = 0, int _next = 0, int _w = 0) : to(_to), next(_next), w(_w) {} n } e[300010 << 1]; n int n, m; n int g[300010], nume = 0; n int d[300010]; n int f[300010][2]; n vector<int> ans; n namespace LCT { n int f[300010]; n void init() { n   for (int i = 1; i <= n; i++) f[i] = i; n } n int getf(int x) { n   if (f[x] == x) return x; n   return f[x] = getf(f[x]); n } n int merge(int x, int y) { f[f[x]] = f[y]; } n }  // namespace LCT n void addEdge(int u, int v, int w) { n   e[nume] = edge(v, g[u], w); n   g[u] = nume++; n } n void dfs(int x, int p) { n   for (int i = g[x]; ~i; i = e[i].next) n     if (e[i].to ^ p) dfs(e[i].to, x); n   int numch = 0; n   static int ch[300010][2]; n   int c0 = 0, c1 = 0; n   for (int i = g[x]; ~i; i = e[i].next) n     if (e[i].to ^ p) { n       ++numch; n       ch[numch][0] = f[e[i].to][0]; n       ch[numch][1] = f[e[i].to][1]; n       if (!ch[numch][0] && !ch[numch][1]) { n         f[x][0] = f[x][1] = 0; n         return; n       } n       if (ch[numch][0] && ch[numch][1]) { n         f[x][0] = f[x][1] = 1; n         return; n       } n       if (ch[numch][0]) c0++; n       if (ch[numch][1]) c1++; n     } n   if (d[x] == -1) { n     f[x][0] = f[x][1] = 1; n     return; n   } n   if (d[x] == 0) { n     if (c1 & 1) { n       f[x][1] = 1; n       f[x][0] = 0; n     } else { n       f[x][1] = 0; n       f[x][0] = 1; n     } n   } else { n     if (c1 & 1) { n       f[x][1] = 0; n       f[x][0] = 1; n     } else { n       f[x][1] = 1; n       f[x][0] = 0; n     } n   } n } n void dfs2(int x, int p, int fx) { n   if (f[x][0] != f[x][1]) { n     for (int i = g[x]; ~i; i = e[i].next) n       if (e[i].to ^ p) n         if (f[e[i].to][0]) { n           dfs2(e[i].to, x, 0); n         } else { n           ans.push_back(e[i].w); n           dfs2(e[i].to, x, 1); n         } n   } else { n     int temp = -1, c1 = 0; n     for (int i = g[x]; ~i; i = e[i].next) n       if (e[i].to ^ p) { n         if (f[e[i].to][0] == f[e[i].to][1] && temp == -1) { n           temp = i; n         } else { n           if (f[e[i].to][0]) n             dfs2(e[i].to, x, 0); n           else { n             dfs2(e[i].to, x, 1); n             ans.push_back(e[i].w); n             c1++; n           } n         } n       } n     if (temp == -1) return; n     if ((c1 & 1) == fx ^ d[x]) n       dfs2(e[temp].to, x, 0); n     else { n       dfs2(e[temp].to, x, 1); n       ans.push_back(e[temp].w); n     } n   } n } n int main() { n   memset(g, -1, sizeof g); n   scanf( %d%d , &n, &m); n   LCT::init(); n   for (int i = 1; i <= n; i++) scanf( %d , d + i); n   for (int i = 1; i <= m; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     if (LCT::getf(u) ^ LCT::getf(v)) { n       addEdge(u, v, i); n       addEdge(v, u, i); n       LCT::merge(u, v); n     } n   } n   dfs(1, 0); n   if (!f[1][0]) { n     puts( -1 ); n     return 0; n   } n   dfs2(1, 0, 0); n   int numans = ans.size(); n   printf( %d n , numans); n   for (int i = 0; i < numans; i++) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int dp[360361]; n long long int k; n bool reach(long long int from, long long int to) { n   if (from <= to) return false; n   long long int d = from - to; n   if (d == 1) return true; n   for (long long int i = (long long int)(from - to + 1); n        i < ((long long int)(k + 1)); i++) n     if (to % i == 0 && to + i > from) return true; n   return false; n } n long long int calc(long long int from, long long int to) { n   for (long long int i = (long long int)(from); i < ((long long int)(to + 1)); n        i++) n     dp[i] = 999999; n   dp[from] = 0; n   for (long long int i = (long long int)(from + 1); n        i < ((long long int)(to + 1)); i++) { n     for (long long int j = i - 1; j >= max(from, i - k + 1); j--) { n       if (reach(i, j)) dp[i] = min(dp[i], dp[j] + 1); n     } n   } n   return dp[to]; n } n int main() { n   long long int a, b; n   cin >> a >> b >> k; n   a -= 360360 * (b / 360360); n   b = b % 360360; n   if (a < 360360) { n     cout << calc(b, a) << endl; n   } else { n     long long int res = 0; n     res += calc(b, 360360); n     long long int nums = (a - (a % 360360) - 360360) / 360360; n     res += calc(0, 360360) * nums; n     res += calc(0, a % 360360); n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 10000000000; n const int maxn = 5010; n long long a[maxn]; n long long last[maxn]; n int dp[maxn]; n long long s[maxn]; n int n; n int main(int argc, char *argv[]) { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]); n   fill(last, last + maxn, INF); n   fill(dp, dp + maxn, maxn); n   s[0] = 0; n   for (int i = 1; i <= n; i++) s[i] = s[i - 1] + a[i]; n   dp[0] = 0; n   last[0] = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < i; j++) { n       if (s[i] - s[j] >= last[j]) { n         if (dp[i] >= dp[j] + i - j - 1) { n           dp[i] = dp[j] + i - j - 1; n           last[i] = min(last[i], s[i] - s[j]); n         } n       } n     } n   } n   cout << dp[n] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int abso(int a, int b) { n   if (a < b) n     return b - a; n   else n     return a - b; n } n long long power(long long x, long long e) { n   long long temp; n   if (e == 0) return 1; n   if (e % 2 == 0) { n     temp = power(x, e / 2); n     return temp * temp; n   } else { n     temp = power(x, e / 2); n     return temp * temp * x; n   } n } n bool cmp(pair<pair<int, int>, int> x, pair<pair<int, int>, int> y) { n   if (x.first.first == y.first.first) { n     if (x.first.second == y.first.second) n       return x.second < y.second; n     else n       return x.first.second > y.first.second; n   } n   return x.first.first > y.first.first; n } n bool cmp1(pair<int, int> x, pair<int, int> y) { n   if (x.second == y.second) return x.first < y.first; n   return x.second < y.second; n } n int main() { n   int n; n   cin >> n; n   string str; n   cin >> str; n   int arr[n + 1]; n   for (int i = 1; i <= n; i++) cin >> arr[i]; n   int cur = 1; n   int visit[n + 1]; n   memset(visit, 0, sizeof visit); n   int flag = 0, next; n   visit[cur] = 1; n   while (1) { n     if (str[cur - 1] ==  < ) { n       next = cur - arr[cur]; n       if (next < 1) { n         flag = 1; n         break; n       } else { n         if (visit[next] == 1) { n           flag = 2; n           break; n         } else { n           visit[next] = 1; n           cur = next; n         } n       } n     } else { n       next = cur + arr[cur]; n       if (next > n) { n         flag = 1; n         break; n       } else { n         if (visit[next] == 1) { n           flag = 2; n           break; n         } else { n           visit[next] = 1; n           cur = next; n         } n       } n     } n   } n   if (flag == 1) n     cout <<  FINITE  << endl; n   else n     cout <<  INFINITE  << endl; n   return 0; n } n


#include <bits/stdc++.h> n int n, m, q; n std::vector<int> dp; n std::vector<std::pair<int, int> > edges; n struct bip { n   std::vector<int> pers; n   std::stack<std::pair<int, int> > updates; n   std::stack<int> change; n   void update(int i, int j) { n     updates.push({i, pers[i]}); n     pers[i] = j; n   } n   int root(int u) { n     while (pers[(u + 1)] >= 0) u = pers[(u + 1)]; n     return u; n   } n   void merge(int u, int v) { n     if (!pers[(0)]) { n       change.push(0); n       return; n     } n     u = root(u); n     v = root(v); n     if (pers[(n + 1 + u)] == v) { n       change.push(0); n       return; n     } n     if (u == v) { n       update((0), 0); n       change.push(1); n       return; n     } n     int c = 0; n     int eu = pers[(n + 1 + u)]; n     int ev = pers[(n + 1 + v)]; n     if (ev != -1) { n       if (pers[(ev + 1)] < pers[(u + 1)]) u ^= ev ^= u ^= ev; n       update((u + 1), pers[(ev + 1)] + pers[(u + 1)]); n       c++; n       update((ev + 1), u); n       c++; n     } n     if (eu != -1) { n       if (pers[(eu + 1)] < pers[(v + 1)]) v ^= eu ^= v ^= eu; n       update((v + 1), pers[(eu + 1)] + pers[(v + 1)]); n       c++; n       update((eu + 1), v); n       c++; n     } n     update((n + 1 + u), v); n     c++; n     update((n + 1 + v), u); n     c++; n     change.push(c); n   } n   void undo() { n     int u = change.top(); n     change.pop(); n     while (u--) { n       std::pair<int, int> next = updates.top(); n       updates.pop(); n       pers[next.first] = next.second; n     } n   } n } graph; n void solve(int l, int r, int tl, int tr) { n   int mid = (l + r) / 2; n   for (int i = l; i < mid; i++) { n     graph.merge(edges[i].first, edges[i].second); n   } n   int opt = tr; n   for (int i = tr; i > std::max(tl, mid); i--) { n     graph.merge(edges[i].first, edges[i].second); n     if (graph.pers[(0)]) opt = i - 1; n   } n   dp[mid] = opt; n   for (int i = tr; i > std::max(tl, mid); i--) graph.undo(); n   dp[mid] = opt; n   if (l == r) { n     for (int i = l; i < mid; i++) graph.undo(); n     return; n   } n   graph.merge(edges[mid].first, edges[mid].second); n   solve(mid + 1, r, opt, tr); n   for (int i = mid; i >= l; i--) graph.undo(); n   if (l < mid) { n     for (int i = tr; i > opt; i--) graph.merge(edges[i].first, edges[i].second); n     solve(l, mid - 1, tl, opt); n     for (int i = opt + 1; i <= tr; i++) graph.undo(); n   } n } n signed main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(0); n   std::cin >> n >> m >> q; n   graph.pers = std::vector<int>(2 * n + 1, -1); n   graph.pers[(0)] = 1; n   edges.resize(m); n   int last = 0; n   for (int i = 0; i < m; i++) { n     std::cin >> edges[i].first >> edges[i].second; n     edges[i].first--; n     edges[i].second--; n     if (graph.pers[(0)]) last = i; n     graph.merge(edges[i].first, edges[i].second); n   } n   dp.resize(last + 1); n   for (int i = 0; i < m; i++) graph.undo(); n   solve(0, last, 0, m - 1); n   while (q--) { n     int l, r; n     std::cin >> l >> r; n     l--; n     r--; n     if (l > last || r < dp[l]) n       std::cout <<  YES  << std::endl; n     else n       std::cout <<  NO  << std::endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, k, s = 0, k2 = 0, k1 = 0, z; n   cin >> n >> m >> k; n   z = k % 2; n   while (true) { n     if (s + 2 * m >= k) { n       goto v; n     } else { n       s = s + 2 * m; n       k2++; n     } n   } n v:; n   cout << k2 + 1 <<    ; n   while (true) { n     if (s >= (k - 2)) n       goto z; n     else { n       s = s + 2; n       k1++; n     } n   } n z:; n   cout << k1 + 1 <<    ; n   if (z == 0) n     cout <<  R ; n   else n     cout <<  L ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, j, r; n   char str[] = { R ,  O ,  Y ,  B ,  G ,  I ,  V }; n   cin >> n; n   r = n % 7; n   n -= r; n   for (i = 0; i < n; i++) cout << str[i % 7]; n   for (i = 0; i < r; i++) cout << str[3 + i % 4]; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {0, 0, +1, -1}; n int dy[] = {+1, -1, 0, 0}; n int dxx[] = {+1, 0, -1, 0, +1, +1, -1, -1}; n int dyy[] = {0, +1, 0, -1, +1, -1, +1, -1}; n inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } n vector<pair<long long, long long> > ans; n int main() { n   long long x, i, sz = 0, l, y; n   scanf( %lld , &x); n   l = min(x, 10000000LL); n   for (i = 1; i <= l; i++) { n     y = x * 6LL; n     if (y % i) continue; n     y /= i; n     if (y % (i + 1)) continue; n     y /= (i + 1); n     y += (i - 1); n     if (y % 3) continue; n     y /= 3; n     if (y < i) continue; n     ans.push_back(make_pair(i, y)); n     if (i == y) n       sz++; n     else n       sz += 2; n   } n   printf( %lld n , sz); n   for (i = 0; i < ans.size(); i++) n     printf( %lld %lld n , ans[i].first, ans[i].second); n   for (i = ans.size() - 1; ~i; i--) n     if (ans[i].first != ans[i].second) n       printf( %lld %lld n , ans[i].second, ans[i].first); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX = 4e6; n vector<long long> vec[1000000]; n long long N, T, ok, cur = 2, arr[MAX], res[MAX], cnt[MAX], prime[MAX]; n void cal() { n   prime[0] = 1; n   prime[1] = 1; n   for (long long A = 2; A < MAX; A++) n     if (!prime[A]) { n       if (A < 1000000) vec[A].push_back(A); n       for (long long B = A + A; B < MAX; B += A) { n         prime[B] = 1; n         if (B < 1000000) vec[B].push_back(A); n       } n     } n   return; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cal(); n   cin >> N; n   for (long long A = 1; A <= N; A++) cin >> arr[A]; n   for (long long A = 1; A <= N; A++) { n     if (ok == 0) n       for (auto B : vec[arr[A]]) { n         cnt[B]++; n         if (cnt[B] > 1) ok = 1; n       } n     if (ok == 0) n       res[A] = arr[A]; n     else if (ok == 1) { n       for (auto B : vec[arr[A]]) cnt[B]--; n       cur = arr[A] + 1; n       while (true) { n         T = 0; n         for (auto B : vec[cur]) T += cnt[B]; n         if (T == 0) { n           res[A] = cur; n           for (auto B : vec[cur]) cnt[B]++; n           break; n         } n         cur++; n       } n       cur = 2; n       ok = 2; n     } else { n       while ((prime[cur]) or (cnt[cur])) cur++; n       res[A] = cur++; n     } n   } n   for (long long A = 1; A <= N; A++) cout << res[A] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool r[1010], c[1010]; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     r[a] = 1; n     c[b] = 1; n   } n   int ans = 0; n   for (int i = 1; i < n - 1; i++) { n     if (!c[i]) ans++; n     if (!r[i]) ans++; n     if (!c[i] && !r[i] && !c[n - i] && !r[n - i] && (n - 1 - i) == i) ans--; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long int modinverse(long long int x, long long int y) { n   if (y == 0) { n     return 1; n   } n   long long int p = modinverse(x, y / 2) % 1000000007; n   p = (p * p) % 1000000007; n   return (y % 2 == 0) ? p : (x * p) % 1000000007; n } n long long int modexp(long long int a, long long int n) { n   long long int ans = 1; n   a = a % 1000000007; n   while (n > 0) { n     if (n % 2) { n       ans = (ans * a) % 1000000007; n     } n     n /= 2; n     a = (a * a) % 1000000007; n   } n   return ans; n } n long long int arr[300001], on[30], off[30]; n void solve(vector<long long int> index, long long int k) { n   if (k == -1 or index.size() <= 1) { n     return; n   } n   vector<long long int> one, zero; n   long long int cnt1 = 0, cnt2 = 0, inversion1 = 0, inversion2 = 0; n   for (auto itr : index) { n     if ((arr[itr] >> k) % 2 == 1) { n       one.push_back(itr); n       cnt1++; n       inversion1 += cnt2; n     } else { n       zero.push_back(itr); n       cnt2++; n       inversion2 += cnt1; n     } n   } n   on[k] += inversion1; n   off[k] += inversion2; n   solve(zero, k - 1); n   solve(one, k - 1); n } n long long int countInversion(vector<long long int> index, long long int k) { n   if (k == -1 or index.size() <= 1) { n     return 0; n   } n   vector<long long int> zero, one; n   long long int cnt = 0, inversion = 0; n   for (auto itr : index) { n     if ((arr[itr] >> k) % 2 == 1) { n       one.push_back(itr); n       cnt++; n     } else { n       zero.push_back(itr); n       inversion += cnt; n     } n   } n   inversion += countInversion(zero, k - 1); n   inversion += countInversion(one, k - 1); n   return inversion; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int tt = 1; n   for (long long int tc = 1; tc <= tt; tc++) { n     long long int n; n     cin >> n; n     for (long long int i = 0; i <= n - 1; i++) { n       cin >> arr[i]; n     } n     vector<long long int> v; n     for (long long int i = 0; i <= n - 1; i++) { n       v.push_back(i); n     } n     for (long long int i = 0; i <= 29; i++) { n       on[i] = off[i] = 0; n     } n     solve(v, 29); n     long long int x = 0; n     for (long long int i = 0; i <= 29; i++) { n       if (on[i] < off[i]) { n         x += pow(2, i); n       } n     } n     for (long long int i = 0; i <= n - 1; i++) { n       arr[i] = (arr[i] ^ x); n     } n     long long int b = countInversion(v, 29); n     cout << b <<     << x; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   for (int tmp = 0; tmp < t; tmp++) { n     unsigned long long int n, k; n     cin >> n >> k; n     if (k >= n) { n       cout << 1 <<   n ; n       continue; n     } n     bool fl = false; n     for (int i = 2; i * i <= n; i++) { n       if (n % i == 0 && n / i <= k) { n         cout << i <<   n ; n         fl = true; n         break; n       } n     } n     if (!fl) { n       for (int i = int(sqrt(n)); i >= 2; i--) { n         if (n % i == 0 && i <= k) { n           cout << n / i <<   n ; n           fl = true; n           break; n         } n       } n     } n     if (!fl) { n       cout << n <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n double prob[maxn]; n int main() { n   ios::sync_with_stdio(false); n   int n, p; n   while (cin >> n >> p) { n     int l, r; n     for (int i = 0; i < n; ++i) { n       cin >> l >> r; n       prob[i] = (double)(r / p - (l - 1) / p) / (r + 1 - l); n     } n     double sum = 0; n     for (int i = 0; i < n; ++i) { n       int j = (i + 1) % n; n       sum += 1.0 - (1 - prob[i]) * (1 - prob[j]); n     } n     printf( %.8lf n , sum * 2000); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int MAX = -1000000000000000000; n const long long int MIN = 1000000000000000000; n const long long int inf = 1000000000000000000; n const long long int KOK = 100000; n const long long int LOG = 30; n const long long int li = 105; n const long long int mod = 1000000007; n long long int n, m, cev, b, a[li], k, dp[105][100005], PS[li][li]; n string s; n vector<int> v; n long long int f(long long int sira, long long int topl) { n   long long int mn = inf; n   if (topl <= 0) return 0; n   if (sira > n) { n     return inf; n   } n   if (~dp[sira][topl]) return dp[sira][topl]; n   for (int i = sira; i <= n; i++) { n     mn = min(mn, f(i + 1, topl - PS[sira][i] + PS[sira][sira - 1]) + 1); n   } n   return dp[sira][topl] = mn; n } n int main() { n   memset(dp, -1, sizeof(dp)); n   scanf( %lld %lld , &n, &m); n   for (long long int i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n   } n   sort(a + 1, a + n + 1); n   reverse(a + 1, a + n + 1); n   for (long long int i = 1; i <= n; i++) { n     long long int at = 0; n     for (long long int j = i; j <= n; j++) { n       PS[i][j] = PS[i][j - 1] + max(a[j] - at, 0ll); n       at++; n     } n   } n   long long int yess = f(1, m); n   if (yess >= inf) { n     printf( -1 n ); n     return 0; n   } n   printf( %lld n , yess); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int INF = 1061109567; n const int MAXN = 510005; n int dp[MAXN]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout.precision(20); n   int n, d; n   cin >> n >> d; n   memset(dp, 0, sizeof dp); n   dp[0] = 1; n   int items[n]; n   for (int i = 0; i < n; i++) cin >> items[i]; n   for (int j = 0; j < n; j++) { n     for (int i = MAXN - items[j]; i >= 0; i--) n       if (dp[i]) dp[i + items[j]] = 1; n   } n   int itCount = 0; n   int i = 0; n   while (i < 500000) { n     bool flag = false; n     for (int j = i + d; j >= i + 1; j--) { n       if (dp[j]) { n         itCount++; n         i = j; n         flag = true; n         break; n       } n     } n     if (!flag) break; n   } n   cout << i <<     << itCount << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int logN = 18; n const int maxN = 200005; n char pch[maxN]; n int n, m, q, sz; n vector<pair<int, char> > g[maxN]; n pair<int, int> orange[maxN][logN]; n pair<int, int> rrange[maxN][logN]; n vector<array<int, 4> > queries[maxN]; n int par[maxN][logN], pos[maxN], ord[maxN], ind[maxN], ans[maxN], dep[maxN], n     f[maxN]; n inline void update(int i, int x) { n   while (i <= m) { n     f[i] += x; n     i += i & (-i); n   } n } n inline int query(int i) { n   int ret = 0; n   while (i) { n     ret += f[i]; n     i &= (i - 1); n   } n   return ret; n } n void dfs(int v, int p) { n   par[v][0] = p; n   for (auto x : g[v]) { n     if (x.first != p) { n       dep[x.first] = dep[v] + 1; n       pch[x.first] = x.second; n       dfs(x.first, v); n     } n   } n } n void buildSuffixArray(string s) { n   int m = (int)s.size(); n   { n     vector<pair<char, int> > vec; n     for (int i = 0; i < m; i++) { n       vec.emplace_back(s[i], i); n     } n     sort(vec.begin(), vec.end()); n     for (int i = 0; i < m; i++) { n       if (i == 0 || vec[i].first != vec[i - 1].first) n         pos[vec[i].second] = i; n       else n         pos[vec[i].second] = pos[vec[i - 1].second]; n     } n   } n   for (int k = 1; k < logN; k++) { n     vector<pair<pair<int, int>, int> > vec; n     for (int i = 0; i < m; i++) { n       int j = i + (1 << (k - 1)); n       if (j < m) { n         vec.push_back({{pos[i], pos[j]}, i}); n       } else { n         vec.push_back({{pos[i], -1}, i}); n       } n     } n     sort(vec.begin(), vec.end()); n     for (int i = 0; i < m; i++) { n       if (i == 0 || vec[i].first != vec[i - 1].first) n         pos[vec[i].second] = i; n       else n         pos[vec[i].second] = pos[vec[i - 1].second]; n     } n     if ((1 << k) >= m) break; n   } n   for (int i = 0; i < m; i++) { n     ord[pos[i]] = i; n   } n } n inline pair<int, int> compute(pair<int, int> range1, pair<int, int> range2, n                               int len1, int len2) { n   if (range1.first == -1 || range2.first == -1) return {-1, -1}; n   if (len1 == 0) return range2; n   if (len2 == 0) return range1; n   pair<int, int> ret = {-1, -1}; n   { n     int low = range1.first, hig = range1.second; n     if (ord[hig] + len1 == sz || range2.first > pos[ord[hig] + len1]) n       return {-1, -1}; n     while (low < hig) { n       int mid = (low + hig) / 2; n       if (ord[mid] + len1 < sz && range2.first <= pos[ord[mid] + len1]) { n         hig = mid; n       } else { n         low = mid + 1; n       } n     } n     ret.first = low; n   } n   { n     int low = range1.first, hig = range1.second; n     if (ord[low] + len1 < sz && range2.second < pos[ord[low] + len1]) n       return {-1, -1}; n     while (low < hig) { n       int mid = (low + hig + 1) / 2; n       if (ord[mid] + len1 >= sz || pos[ord[mid] + len1] <= range2.second) { n         low = mid; n       } else { n         hig = mid - 1; n       } n     } n     ret.second = low; n   } n   if (ret.first > ret.second) { n     ret = {-1, -1}; n   } n   return ret; n } n inline int lca(int u, int v) { n   if (dep[u] < dep[v]) swap(u, v); n   int diff = dep[u] - dep[v]; n   for (int i = logN - 1; i >= 0; i--) { n     if ((diff >> i) & 1) u = par[u][i]; n   } n   if (u == v) return u; n   for (int i = logN - 1; i >= 0; i--) { n     if (par[u][i] != par[v][i]) { n       u = par[u][i]; n       v = par[v][i]; n     } n   } n   return par[u][0]; n } n pair<int, int> get(int u, int v) { n   int w = lca(u, v); n   pair<int, int> range1 = {0, sz - 1}; n   pair<int, int> range2 = {0, sz - 1}; n   int len1 = 0, len2 = 0; n   { n     int dist = dep[u] - dep[w]; n     for (int i = logN - 1; i >= 0; i--) { n       if ((dist >> i) & 1) { n         range1 = compute(range1, orange[u][i], len1, 1 << i); n         len1 += (1 << i); n         u = par[u][i]; n       } n     } n   } n   { n     int dist = dep[v] - dep[w]; n     for (int i = logN - 1; i >= 0; i--) { n       if ((dist >> i) & 1) { n         range2 = compute(rrange[v][i], range2, 1 << i, len2); n         len2 += (1 << i); n         v = par[v][i]; n       } n     } n   } n   return compute(range1, range2, len1, len2); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> n >> m >> q; n   for (int i = 1; i < n; i++) { n     int u, v; n     char ch; n     cin >> u >> v >> ch; n     g[u].emplace_back(v, ch); n     g[v].emplace_back(u, ch); n   } n   dep[1] = 0; n   dfs(1, 1); n   for (int k = 1; k < logN; k++) { n     par[1][k] = 1; n     for (int i = 2; i <= n; i++) { n       par[i][k] = par[par[i][k - 1]][k - 1]; n     } n   } n   string s =   ; n   memset(ind, -1, sizeof(ind)); n   for (int i = 1; i <= m; i++) { n     string t; n     cin >> t; n     for (int j = 0; j < (int)t.size(); j++) { n       ind[j + (int)s.size()] = i; n     } n     s += t; n     s +=  # ; n   } n   sz = (int)s.size(); n   buildSuffixArray(s); n   memset(orange, -1, sizeof(orange)); n   memset(rrange, -1, sizeof(rrange)); n   { n     vector<int> lo(26, -1); n     vector<int> hi(26, -1); n     for (int i = 0; i < sz; i++) { n       if (s[ord[i]] ==  # ) continue; n       if (lo[s[ord[i]] -  a ] == -1) { n         lo[s[ord[i]] -  a ] = i; n       } n       hi[s[ord[i]] -  a ] = i; n     } n     for (int i = 2; i <= n; i++) { n       orange[i][0] = {lo[pch[i] -  a ], hi[pch[i] -  a ]}; n       rrange[i][0] = {lo[pch[i] -  a ], hi[pch[i] -  a ]}; n     } n   } n   for (int k = 1; k < logN; k++) { n     for (int u = 1; u <= n; u++) { n       int v = par[u][k - 1]; n       orange[u][k] = compute(orange[u][k - 1], orange[v][k - 1], 1 << (k - 1), n                              1 << (k - 1)); n     } n     for (int u = 1; u <= n; u++) { n       int v = par[u][k - 1]; n       rrange[u][k] = compute(rrange[v][k - 1], rrange[u][k - 1], 1 << (k - 1), n                              1 << (k - 1)); n     } n   } n   memset(ans, 0, sizeof(ans)); n   for (int i = 1; i <= q; i++) { n     int u, v, l, r; n     cin >> u >> v >> l >> r; n     pair<int, int> range = get(u, v); n     if (range.first == -1) { n       continue; n     } n     queries[range.second].push_back({l, r, i, 1}); n     if (range.first > 0) { n       queries[range.first - 1].push_back({l, r, i, -1}); n     } n   } n   memset(f, 0, sizeof(f)); n   for (int i = 0; i < sz; i++) { n     if (ind[ord[i]] != -1) { n       update(ind[ord[i]], 1); n     } n     for (auto x : queries[i]) { n       ans[x[2]] += x[3] * (query(x[1]) - query(x[0] - 1)); n     } n   } n   for (int i = 1; i <= q; i++) { n     cout << ans[i] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1005][1005], i, m, ans, k, l, j, q, x, n, ma, mi; n string s; n int main() { n   cin >> n >> k; n   if (k >= (n + 1) / 2) { n     cout << -1; n     return 0; n   } n   cout << n * k << endl; n   for (int i = 1; i <= n; i++) { n     int w = k; n     for (int j = 1; j <= n; j++) { n       if (a[i][j] == 1 || i == j) continue; n       cout << i <<     << j << endl; n       a[i][j] = 1; n       a[j][i] = 1; n       w--; n       if (!w) break; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct Node { n   string oper; n   bool val; n   int left = -1; n   int right = -1; n   bool will_left_change = false; n   bool will_right_change = false; n }; n vector<Node> tree; n bool f(bool l_val, bool r_val, string oper) { n   if (oper ==  AND ) { n     return (l_val && r_val); n   } else if (oper ==  OR ) { n     return (l_val || r_val); n   } else if (oper ==  XOR ) { n     return (l_val ^ r_val); n   } n } n void calc(Node &v) { n   if (v.oper ==  IN ) return; n   calc(tree[v.left]); n   if (v.right != -1) { n     calc(tree[v.right]); n   } n   if (v.right == -1) n     v.val = !tree[v.left].val; n   else { n     v.val = f(tree[v.left].val, tree[v.right].val, v.oper); n   } n   if (v.right == -1) n     v.will_left_change = true; n   else { n     v.will_left_change = (f(tree[v.left].val, tree[v.right].val, v.oper) != n                           f(!tree[v.left].val, tree[v.right].val, v.oper)); n     v.will_right_change = (f(tree[v.left].val, tree[v.right].val, v.oper) != n                            f(tree[v.left].val, !tree[v.right].val, v.oper)); n   } n } n vector<int> ans; n void calc_leaves(Node &v, int ones_cnt, int depth, int v_index) { n   if (v.oper ==  IN ) { n     if (ones_cnt == depth) { n       ans[v_index] = !tree[1].val; n     } else { n       ans[v_index] = tree[1].val; n     } n     return; n   } n   calc_leaves(tree[v.left], ones_cnt + (int)v.will_left_change, depth + 1, n               v.left); n   if (v.right != -1) { n     calc_leaves(tree[v.right], ones_cnt + (int)v.will_right_change, depth + 1, n                 v.right); n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   tree.resize(n + 1); n   ans.resize(n + 1, -1); n   for (int i = 1; i <= n; i++) { n     string s; n     cin >> s; n     if (s ==  IN ) { n       int z; n       cin >> z; n       tree[i].oper = s; n       tree[i].val = z; n     } else if (s ==  NOT ) { n       int id; n       cin >> id; n       tree[i].oper = s; n       tree[i].left = id; n     } else { n       int left, right; n       cin >> left >> right; n       tree[i].oper = s; n       tree[i].left = left; n       tree[i].right = right; n     } n   } n   calc(tree[1]); n   calc_leaves(tree[1], 0, 0, -1); n   for (int i = 1; i <= n; i++) { n     if (ans[i] != -1) cout << ans[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n priority_queue<long long> q; n int n, k; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i < k; i++) q.push(0); n   for (int i = 0; i < n; i++) { n     long long s, m; n     scanf( %I64d%I64d , &s, &m); n     long long num = q.top(); n     q.pop(); n     num = max(-num, s); n     printf( %I64d n , m + num); n     q.push(-(m + num)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, a, b, c, r; n   cin >> t; n   while (t--) { n     cin >> a >> b >> c >> r; n     int min = a < b ? a : b; n     int max = min == a ? b : a; n     int res = max - min + 1; n     if (c - r >= min && c + r <= max) { n       res = res - 2 * r - 1; n     } else if (min > c - r && max < c + r) { n       res = 0; n     } else if (c - r >= min && c - r <= max && c + r >= max) { n       res = res - (max - c + r) - 1; n     } else if (c + r <= max && c + r >= min && min >= c - r) { n       res = res - (c + r - min) - 1; n     } n     res = res == max - min + 1 ? max - min : res; n     cout << res << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[2006]; n int one, g, mn = 1e9; n int gcd(int _a, int _b) { return _b == 0 ? _a : gcd(_b, _a % _b); } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     g = gcd(g, a[i]); n     if (a[i] == 1) one++; n   } n   if (one != 0) { n     printf( %d n , n - one); n     return 0; n   } n   if (g != 1) { n     puts( -1 ); n     return 0; n   } n   for (int i = 1; i <= n; i++) { n     int b = a[i]; n     for (int j = i + 1; j <= n; j++) { n       b = gcd(b, a[j]); n       if (b == 1) { n         mn = min(mn, j - i); n         break; n       } n     } n   } n   printf( %d n , mn + n - 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 405; n const int maxm = 5005; n const int INF = 1000000000; n const int mod = 1000000; n struct EDGE { n   int cap, cost, v, next; n } edge[mod]; n int head[maxn], E, q[mod]; n bool used[maxn]; n int pre[maxn], cur[maxn], dis[maxn]; n void add_edge(int s, int t, int cap, int cost) { n   edge[E].cap = cap; n   edge[E].cost = cost; n   edge[E].next = head[s]; n   edge[E].v = t; n   head[s] = E++; n   edge[E].cap = 0; n   edge[E].cost = -cost; n   edge[E].next = head[t]; n   edge[E].v = s; n   head[t] = E++; n } n int min(int a, int b) { return (a == -1 || b < a) ? b : a; } n int SPFA(int s, int t, int n) { n   int f = -1, r = 0; n   int i, v; n   q[r] = s; n   for (i = 0; i < n; i++) dis[i] = INF; n   dis[s] = 0; n   pre[s] = s; n   cur[s] = -1; n   memset(used, false, sizeof(bool) * n); n   used[s] = true; n   while (f != r) { n     f++; n     if (f >= mod) f -= mod; n     s = q[f]; n     used[s] = false; n     for (i = head[s]; i != -1; i = edge[i].next) { n       v = edge[i].v; n       if (edge[i].cap > 0 && dis[s] + edge[i].cost < dis[v]) { n         dis[v] = dis[s] + edge[i].cost; n         pre[v] = s; n         cur[v] = i; n         if (!used[v]) { n           used[v] = true; n           r++; n           if (r >= mod) r -= mod; n           q[r] = v; n         } n       } n     } n   } n   return dis[t]; n } n int MinCost(int s, int t, int n) { n   int ans = 0; n   int u, v, cap; n   int cost; n   while (1) { n     cost = SPFA(s, t, n); n     if (cost == INF) break; n     u = v = t; n     cap = -1; n     for (u = t; u != s; u = pre[u]) { n       cap = min(cap, edge[cur[u]].cap); n     } n     ans += cost * cap; n     u = v = t; n     for (u = t; u != s; u = pre[u]) { n       edge[cur[u]].cap -= cap; n       edge[cur[u] ^ 1].cap += cap; n     } n   } n   return ans; n } n int dx[maxn]; n int dy[maxn]; n int c[maxm]; n int x[maxm]; n int y[maxm]; n int main() { n   int n, m, g, k, z; n   int i, j; n   int s, t; n   while (scanf( %d%d%d%d , &n, &m, &k, &g) != -1) { n     memset(dx, 0, sizeof(int) * n); n     memset(dy, 0, sizeof(int) * m); n     for (i = 0; i < k; i++) { n       scanf( %d%d , &x[i], &y[i]); n       dx[--x[i]]++; n       dy[--y[i]]++; n     } n     int ans = 0; n     for (i = 0; i < n; i++) ans += dx[i] % g != 0; n     for (i = 0; i < m; i++) ans += dy[i] % g != 0; n     printf( %d n , ans); n     memset(c, -1, sizeof(int) * k); n     s = n + m; n     t = s + 1; n     for (; g >= 1; g--) { n       E = 0; n       memset(head, -1, sizeof(int) * (t + 1)); n       for (i = 0; i < k; i++) n         if (c[i] == -1) add_edge(x[i], y[i] + n, 1, 0); n       for (i = 0; i < n; i++) { n         add_edge(s, i, dx[i] / g, 0); n         if (dx[i] % g) add_edge(s, i, 1, 1); n       } n       for (i = 0; i < m; i++) { n         add_edge(i + n, t, dy[i] / g, 0); n         if (dy[i] % g) add_edge(i + n, t, 1, 1); n       } n       MinCost(s, t, t + 1); n       for (i = 0, z = 0; i < k; i++) n         if (c[i] == -1) { n           if (edge[z].cap == 0) { n             c[i] = g; n             dx[x[i]]--; n             dy[y[i]]--; n           } n           z += 2; n         } n     } n     for (i = 0; i < k; i++) { n       if (i) printf(   ); n       printf( %d , c[i]); n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxk = 11; n long long p[maxk]; n void init() { n   p[0] = 1; n   for (int i = 1; i <= maxk; ++i) p[i] = p[i - 1] * 10; n } n long long f(long long x) { n   if (x == 0) return 0; n   long long ret = 0; n   int pc = maxk - 1; n   while (p[pc] > x) --pc; n   ret += (pc + 1) * (x - p[pc] + 1); n   while (pc > 0) { n     --pc; n     ret += (pc + 1) * (p[pc + 1] - p[pc]); n   } n   return ret; n } n typedef long long (*FUN)(long long); n long long bis(long long pos, FUN fun) { n   long long ans = 0; n   long long l = 0, r = 1000000000; n   while (l < r) { n     long long mid = l + (r - l) / 2; n     if (fun(mid) < pos) n       ans = mid, l = mid + 1; n     else n       r = mid; n   } n   return ans; n } n long long g(long long x) { n   if (x == 0) return 0; n   int pc = maxk - 1; n   while (p[pc] > x) --pc; n   long long ret = 0; n   ret += (pc + 1) * (x - p[pc] + 1) * (1 + x - p[pc] + 1) / 2; n   while (pc > 0) { n     --pc; n     ret += (pc + 1) * 9 * p[pc] * (x + x - (p[pc] - 1) - (p[pc + 1] - 2)) / 2; n   } n   return ret; n } n char cc[100000]; n int main() { n   init(); n   int q; n   long long k; n   scanf( %d , &q); n   long long temp = 0; n   while (q--) { n     scanf( %lld , &k); n     long long ans = bis(k, g); n     k -= g(ans); n     ans = bis(k, f); n     k -= f(ans); n     sprintf(cc,  %lld , ans + 1); n     printf( %c n , cc[k - 1]); n   } n   getchar(); n   getchar(); n   return 0; n } n


#include <bits/stdc++.h> n const long long inf = 1e16; n using namespace std; n long long read() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar(); n   return f * x; n } n int n; n long long ans, d[505][505]; n struct tmp { n   long long a, b, k; n   bool operator<(const tmp &o) const { return b > o.b; } n } a[505]; n int main() { n   n = read(); n   for (int i = 1; i <= n; i++) a[i] = tmp{read(), read(), read()}; n   sort(a + 1, a + 1 + n); n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j <= i; j++) { n       d[i][j] = d[i - 1][j]; n       if (j > 0) n         d[i][j] = max(d[i][j], d[i - 1][j - 1] + a[i].a - (j - 1) * a[i].b); n       d[i][j] = max(d[i][j], d[i - 1][j] + a[i].a - a[i].k * a[i].b); n     } n   } n   for (int i = 1; i <= n; i++) ans = max(ans, d[n][i]); n   return 0 * printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int c1, c2; n     int d1, d2; n     int diff1, diff2; n     for (int i = 0; i < 3; i++) { n       if ((n - i) % 3 == 0) { n         c2 = (n - i) / 3; n         c1 = n - 2 * c2; n         diff1 = i; n         break; n       } n     } n     for (int i = 0; i < 3; i++) { n       if ((n - i) % 3 == 0) { n         d2 = (n + i) / 3; n         d1 = n - 2 * d2; n         diff2 = i; n         break; n       } n     } n     if (diff1 < diff2) n       cout << c1 <<     << c2 <<   n ; n     else n       cout << d1 <<     << d2 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int n = 0, m, l = 0, k, a, b, c, sum = 0, i, r = 0, j, second, h, n               first = 0, sc, ans = 0, x, t, y; n double aa, pp; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> k >> b >> n >> t; n   if (k == 1) { n     ans = (ceil((1 + n * b - t) * 1.0 / b)); n     if (ans <= 0) n       cout << 0; n     else n       cout << ans; n     return 0; n   } n   aa = (t * (k - 1)) + t * b; n   aa = aa / (t * (k - 1) + b); n   pp = n * (log(k)) - log(t) + log(aa); n   pp = pp / log(k); n   if (pp - floor(pp) < 1e-12) { n     cout << setprecision(10) << floor(pp); n     return 0; n   } n   ans = ceil(pp); n   if (ans <= 0) n     cout << 0; n   else n     cout << ans; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n const long double pi = acos(-1.0); n const long double eps = 1e-5; n const int INF = 0x3f3f3f3f; n const long long INFLL = 0x3f3f3f3f3f3f3f3f; n struct poi { n   double x, y; n } p[200010]; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int n; n   cin >> n; n   for (int i = (1); i <= (n); i++) cin >> p[i].x >> p[i].y; n   if (n & 1) { n     cout <<  NO ; n     return 0; n   } n   poi c; n   c.x = (p[1].x + p[n / 2 + 1].x) / 2; n   c.y = (p[1].y + p[n / 2 + 1].y) / 2; n   for (int i = (2); i <= (n / 2); i++) { n     if (fabs((p[i].x + p[i + n / 2].x) / 2 - c.x) < eps && n         fabs((p[i].y + p[i + n / 2].y) / 2 - c.y) < eps) n       ; n     else { n       cout <<  NO ; n       return 0; n     } n   } n   cout <<  YES ; n   return 0; n } n