#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long n, k; n   cin >> n >> k; n   vector<long long> arr(n); n   for (long long i = 0; i < n; i++) cin >> arr[i]; n   long long c = 0; n   for (long long i = 1; i < n; i++) { n     if (arr[i] + arr[i - 1] < k) { n       c += k - arr[i] - arr[i - 1]; n       arr[i] = k - arr[i - 1]; n     } n   } n   cout << c <<   n ; n   for (auto i : arr) cout << i <<    ; n   ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int MAXN = 1000000005; n long long meta; n bool ok = 0; n map<long long, long long> visite; n void bfs(long long padre) { n   queue<long long> cola; n   cola.push(padre); n   while (!cola.empty()) { n     long long hijo = cola.front(); n     cola.pop(); n     long long hijo1 = hijo * 2; n     long long hijo2 = hijo * 10 + 1; n     if (hijo1 == meta) { n       ok = 1; n       visite[hijo1] = hijo; n       break; n     } n     if (hijo2 == meta) { n       ok = 1; n       visite[hijo2] = hijo; n       break; n     } n     if (hijo1 < MAXN) { n       visite[hijo1] = hijo; n       cola.push(hijo1); n     } n     if (hijo2 < MAXN) { n       visite[hijo2] = hijo; n       cola.push(hijo2); n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   long long padre; n   cin >> padre >> meta; n   bfs(padre); n   if (ok == false) { n     cout <<  NO  << endl; n     return 0; n   } n   cout <<  YES  << endl; n   vector<long long> ans; n   long long hijo = visite[meta]; n   ans.push_back(meta); n   while (hijo != padre) { n     ans.push_back(hijo); n     hijo = visite[hijo]; n   } n   ans.push_back(padre); n   cout << ans.size() << endl; n   for (int i = ans.size() - 1; i >= 0; i--) { n     cout << ans[i] <<    ; n   } n } n


#include <bits/stdc++.h> n int CH, NEG; n template <typename TP> n inline void read(TP& ret) { n   ret = NEG = 0; n   while (CH = getchar(), CH <  ! ) n     ; n   if (CH ==  - ) NEG = true, CH = getchar(); n   while (ret = ret * 10 + CH -  0 , CH = getchar(), CH >  ! ) n     ; n   if (NEG) ret = -ret; n } n using namespace std; n int n, K; n struct node { n   int sz; n   int L[11LL], R[11LL]; n   bool operator<(const node& B) const { n     int i; n     for (i = (1); i <= (K); i++) n       if (R[i] > B.L[i]) break; n     return i > K; n   } n   bool operator>(const node& B) const { n     int i; n     for (i = (1); i <= (K); i++) n       if (L[i] < B.R[i]) break; n     return i > K; n   } n } tmp; n int tot = 0; n set<node> S; n set<node>::iterator it1, it2; n void merge(node& A, const node& B) { n   A.sz += B.sz; n   int i; n   for (i = (1); i <= (K); i++) n     A.L[i] = std::min(A.L[i], B.L[i]), A.R[i] = std::max(A.R[i], B.R[i]); n } n int Add() { n   int i; n   tmp.sz = 1; n   for (i = (1); i <= (K); i++) read(tmp.L[i]), tmp.R[i] = tmp.L[i]; n   if (S.empty()) { n     S.insert(tmp); n     return 1; n   } n   it1 = S.lower_bound(tmp); n   while (true) { n     if (it1 == S.end()) break; n     if (*it1 > tmp) break; n     it2 = it1; n     it1++; n     merge(tmp, *it2); n     S.erase(it2); n   } n   S.insert(tmp); n   it1 = S.end(); n   it1--; n   return it1->sz; n } n int main() { n   S.clear(); n   int i; n   read(n), read(K); n   for (i = (1); i <= (n); i++) { n     printf( %d n , Add()); n   } n END: n   getchar(), getchar(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e4 + 10; n const int N = 5e5 + 10; n const int INF = 0x3f3f3f3f; n const long long mod = 1e9 + 7, G = 3; n const int base = 30000; n int t; n int n, m, k; n int dp[35][35][55]; n int dfs(int n, int m, int k) { n   if (k == 0 || n * m == k) return 0; n   if (dp[n][m][k] != -1) return dp[n][m][k]; n   int ret = INF; n   for (int i = 1; i < n; ++i) n     for (int j = 0; j <= k; ++j) n       ret = min(ret, dfs(i, m, j) + dfs(n - i, m, k - j) + m * m); n   for (int i = 1; i < m; ++i) n     for (int j = 0; j <= k; ++j) n       ret = min(ret, dfs(n, i, j) + dfs(n, m - i, k - j) + n * n); n   if (dp[n][m][k] == -1) dp[n][m][k] = ret; n   return ret; n } n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> t; n   memset(dp, -1, sizeof(dp)); n   while (t--) { n     cin >> n >> m >> k; n     cout << dfs(n, m, k) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   map<char, int> mapdown, mapup, mapleft, mapright; n   char keys[10] = { 0 ,  1 ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ,  9 }; n   int n, count1 = 0, count2 = 0, count3 = 0, count4 = 0; n   char str[15]; n   mapdown.clear(); n   mapup.clear(); n   mapleft.clear(); n   mapright.clear(); n   scanf( %d , &n); n   scanf( %s , str); n   for (int i = 0; i < 10; i++) { n     char c = keys[i]; n     if (keys[i] ==  7  || keys[i] ==  0  || keys[i] ==  9 ) { n       mapdown.insert(make_pair(c, 0)); n     } else { n       mapdown.insert(make_pair(c, 1)); n     } n   } n   for (int i = 0; i < 10; i++) { n     if (keys[i] ==  1  || keys[i] ==  2  || keys[i] ==  3 ) n       mapup.insert(make_pair(keys[i], 0)); n     else n       mapup.insert(make_pair(keys[i], 1)); n   } n   for (int i = 0; i < 10; i++) { n     if (keys[i] ==  3  || keys[i] ==  6  || keys[i] ==  9  || keys[i] ==  0 ) n       mapright.insert(make_pair(keys[i], 0)); n     else n       mapright.insert(make_pair(keys[i], 1)); n   } n   for (int i = 0; i < 10; i++) { n     if (keys[i] ==  1  || keys[i] ==  4  || keys[i] ==  7  || keys[i] ==  0 ) n       mapleft.insert(make_pair(keys[i], 0)); n     else n       mapleft.insert(make_pair(keys[i], 1)); n   } n   for (int i = 0; i < n; i++) { n     if (mapdown[str[i]] != 0) count1++; n   } n   for (int i = 0; i < n; i++) { n     if (mapup[str[i]] != 0) count2++; n   } n   for (int i = 0; i < n; i++) { n     if (mapleft[str[i]] != 0) count3++; n   } n   for (int i = 0; i < n; i++) { n     if (mapright[str[i]] != 0) count4++; n   } n   if (count1 == n || count2 == n || count3 == n || count4 == n) n     printf( NO ); n   else n     printf( YES ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool isSolveabe(int *a, int n, int m) { n   int i = m; n   int j = n - 1; n   int min = INT_MIN; n   while (i < j) { n     if (a[i] < a[j]) { n       if (a[i] < min) { n         return false; n       } n       min = a[i]; n       ++i; n     } else { n       if (a[j] < min) { n         return false; n       } n       min = a[j]; n       --j; n     } n   } n   return true; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int *a = new int[n]; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n     } n     int s = 0; n     int e = n - 1; n     int ans = 0; n     while (s <= e) { n       int m = (s + e) / 2; n       if (isSolveabe(a, n, m)) { n         ans = m; n         e = m - 1; n       } else { n         s = m + 1; n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long GCD(long long a, long long b) { return b ? GCD(b, a % b) : a; } n const long long N = 2e5 + 7; n const long long inf = 1e6 + 1; n const long long mod = 998244353; n int t, n, m, a[N]; n struct node { n   int def, s; n   bool operator<(const node& a) const { return def < a.def; } n } b[N]; n bool cmp(const node& a, int x) { return a.def < x; } n int main() { n   cin >> t; n   while (t--) { n     cin >> n; n     for (int i = 1; i <= n; i++) cin >> a[i]; n     cin >> m; n     for (int i = 1; i <= m; i++) cin >> b[i].def >> b[i].s; n     sort(b + 1, b + 1 + m); n     for (int i = m - 1; i >= 1; i--) { n       b[i].s = max(b[i].s, b[i + 1].s); n     } n     bool ok = true; n     int minn = 2 * n, ans = 0, last = 0; n     for (int i = 1; i <= n; i++) { n       int q = lower_bound(b + 1, b + 1 + m, a[i], cmp) - b; n       if (q > m) { n         ok = false; n         break; n       } n       minn = min(minn, b[q].s); n       if (last + minn < i) { n         ans++; n         last = i - 1; n         minn = b[q].s; n       } n     } n     if (ok == false) n       cout <<  -1 n ; n     else n       cout << ans + 1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using lint = long long int; n using pint = pair<int, int>; n using plint = pair<lint, lint>; n struct fast_ios { n   fast_ios() { n     cin.tie(0); n     ios::sync_with_stdio(false); n     cout << fixed << setprecision(20); n   }; n } fast_ios_; n template <typename T> n void ndarray(vector<T> &vec, int len) { n   vec.resize(len); n } n template <typename T, typename... Args> n void ndarray(vector<T> &vec, int len, Args... args) { n   vec.resize(len); n   for (auto &v : vec) ndarray(v, args...); n } n template <typename T> n bool mmax(T &m, const T q) { n   if (m < q) { n     m = q; n     return true; n   } else n     return false; n } n template <typename T> n bool mmin(T &m, const T q) { n   if (m > q) { n     m = q; n     return true; n   } else n     return false; n } n template <typename T1, typename T2> n pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { n   return make_pair(l.first + r.first, l.second + r.second); n } n template <typename T1, typename T2> n pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { n   return make_pair(l.first - r.first, l.second - r.second); n } n template <typename T> n istream &operator>>(istream &is, vector<T> &vec) { n   for (auto &v : vec) is >> v; n   return is; n } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &vec) { n   os <<  [ ; n   for (auto v : vec) os << v <<  , ; n   os <<  ] ; n   return os; n } n template <typename T> n ostream &operator<<(ostream &os, const deque<T> &vec) { n   os <<  deq[ ; n   for (auto v : vec) os << v <<  , ; n   os <<  ] ; n   return os; n } n template <typename T> n ostream &operator<<(ostream &os, const set<T> &vec) { n   os <<  { ; n   for (auto v : vec) os << v <<  , ; n   os <<  } ; n   return os; n } n template <typename T> n ostream &operator<<(ostream &os, const unordered_set<T> &vec) { n   os <<  { ; n   for (auto v : vec) os << v <<  , ; n   os <<  } ; n   return os; n } n template <typename T> n ostream &operator<<(ostream &os, const multiset<T> &vec) { n   os <<  { ; n   for (auto v : vec) os << v <<  , ; n   os <<  } ; n   return os; n } n template <typename T> n ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { n   os <<  { ; n   for (auto v : vec) os << v <<  , ; n   os <<  } ; n   return os; n } n template <typename T1, typename T2> n ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { n   os <<  (  << pa.first <<  ,  << pa.second <<  ) ; n   return os; n } n template <typename TK, typename TV> n ostream &operator<<(ostream &os, const map<TK, TV> &mp) { n   os <<  { ; n   for (auto v : mp) os << v.first <<  =>  << v.second <<  , ; n   os <<  } ; n   return os; n } n template <typename TK, typename TV> n ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp) { n   os <<  { ; n   for (auto v : mp) os << v.first <<  =>  << v.second <<  , ; n   os <<  } ; n   return os; n } n lint extgcd(lint a, lint b, lint &x, lint &y) { n   lint d = a; n   if (b != 0) n     d = extgcd(b, a % b, y, x), y -= (a / b) * x; n   else n     x = 1, y = 0; n   return d; n } n lint mod_inverse(lint a, lint m) { n   lint x, y; n   extgcd(a, m, x, y); n   return (m + x % m) % m; n } n struct UndirectedTree { n   int V; n   int root; n   int dfs_cnt; n   vector<vector<int>> edge; n   UndirectedTree(int N = 0) : V(N), dfs_cnt(0), edge(N) {} n   void add_edge(int u, int v) { n     edge[u].emplace_back(v); n     edge[v].emplace_back(u); n   } n   vector<int> par; n   vector<int> subtree_size; n   vector<int> dfs_id; n   void _tree_dfs(int now, int prv) { n     par[now] = prv; n     subtree_size[now] = 1; n     dfs_id[now] = dfs_cnt++; n     for (auto nxt : edge[now]) n       if (nxt != prv) { n         _tree_dfs(nxt, now); n         subtree_size[now] += subtree_size[nxt]; n       } n   } n   void tree_dfs(int r) { n     root = r; n     par.resize(V, -1); n     subtree_size.resize(V, -1); n     dfs_id.resize(V, 0); n     dfs_cnt = 0; n     _tree_dfs(root, -1); n   } n   vector<int> prev_heavy; n   int DT; n   void _tree_dfs_th(int now, int prv, int ph) { n     prev_heavy[now] = ph; n     if ((int)edge[now].size() >= DT) ph = now; n     for (auto nxt : edge[now]) n       if (nxt != prv) { n         _tree_dfs_th(nxt, now, ph); n       } n   } n   void tree_dfs_th(int B) { n     DT = B; n     prev_heavy.assign(V, -1); n     _tree_dfs_th(root, -1, -1); n   } n   vector<vector<int>> parinfo; n   void _hp_dfs(int now, int prv, int c, int r) { n     parinfo[r][now] = c; n     for (auto nxt : edge[now]) n       if (nxt != prv) _hp_dfs(nxt, now, c, r); n   } n   void heavy_prepare() { n     parinfo.resize(V); n     for (int now = (0), now_end_ = (V); now < now_end_; now++) n       if ((int)edge[now].size() >= DT) { n         parinfo[now].resize(V); n         for (auto nxt : edge[now]) n           if (nxt != par[now]) _hp_dfs(nxt, now, nxt, now); n       } n   } n }; n constexpr lint MOD = 998244353; n struct BIT { n   using T = lint; n   int len; n   vector<T> val; n   BIT(int num) : len(num), val(num + 1) {} n   BIT() : BIT(0) {} n   void reset() { fill(val.begin(), val.end(), 0); } n   void _add(int pos, T v) { n     while (pos > 0 and pos <= len) { n       val[pos] += v; n       if (val[pos] >= MOD) val[pos] -= MOD; n       pos += pos & -pos; n     } n   } n   T _sum(int pos) const { n     T res = 0; n     while (pos > 0) res += val[pos], pos -= pos & -pos; n     return res % MOD; n   } n   void range_add(int l, int r, T v) { n     _add(l + 1, v); n     _add(r + 1, MOD - v); n   } n   T get(int x) { return _sum(x + 1); } n }; n vector<lint> cache; n constexpr int B = 200; n int main() { n   int N, Q; n   cin >> N >> Q; n   UndirectedTree tree(N); n   for (int _ = (0), __end_ = (N - 1); _ < __end_; _++) { n     int u, v; n     cin >> u >> v; n     tree.add_edge(u - 1, v - 1); n   } n   tree.tree_dfs(0); n   tree.tree_dfs_th(B); n   tree.heavy_prepare(); n   lint Ninv = mod_inverse(N, MOD); n   BIT bit(N); n   cache.resize(N); n   for (int _ = (0), __end_ = (Q); _ < __end_; _++) { n     int q, v; n     cin >> q >> v; n     v--; n     int bid = tree.dfs_id[v]; n     if (q == 1) { n       lint d; n       cin >> d; n       lint stsz = tree.subtree_size[v]; n       lint A = N - stsz; n       bit.range_add(0, N, d * stsz % MOD * Ninv % MOD); n       bit.range_add(bid, bid + stsz, d * A % MOD * Ninv % MOD); n       if ((int)tree.edge[v].size() >= B) { n         (cache[v] += d) %= MOD; n       } else { n         for (auto nxt : tree.edge[v]) n           if (nxt != tree.par[v]) { n             lint cid = tree.dfs_id[nxt], cstsz = tree.subtree_size[nxt]; n             bit.range_add(cid, cid + cstsz, MOD - d * cstsz % MOD * Ninv % MOD); n           } n       } n     } else { n       lint ret = bit.get(bid); n       while (tree.prev_heavy[v] >= 0) { n         int p = tree.prev_heavy[v]; n         int c = tree.parinfo[p][v]; n         ret = ret + MOD - cache[p] * tree.subtree_size[c] % MOD * Ninv % MOD; n         v = p; n       } n       ret %= MOD; n       printf( %lld n , ret); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Elem> n int quick_power(int a, Elem b, int Mod) { n   int ans = 1; n   while (b) { n     if (b & 1) { n       ans = 1ll * ans * a % Mod; n     } n     b >>= 1; n     a = 1ll * a * a % Mod; n   } n   return ans; n } n const int Maxn = 100000; n const int Mod = 998244353; n const int G = 3; n int p[Maxn + 5]; n int n; n long long m; n int frac[Maxn + 5], inv_f[Maxn + 5], inv[Maxn + 5]; n void init() { n   frac[0] = 1; n   for (int i = 1; i <= Maxn + 1; i++) { n     frac[i] = 1ll * frac[i - 1] * i % Mod; n   } n   inv_f[Maxn + 1] = quick_power(frac[Maxn + 1], Mod - 2, Mod); n   for (int i = Maxn; i >= 0; i--) { n     inv_f[i] = 1ll * inv_f[i + 1] * (i + 1) % Mod; n   } n   inv[1] = 1; n   for (int i = 2; i <= Maxn + 1; i++) { n     inv[i] = 1ll * (Mod - Mod / i) * inv[Mod % i] % Mod; n   } n } n void NTT(int *a, int flag, int n) { n   static int R[Maxn << 2 | 5], last_len; n   int len = 1, L = 0; n   while (len < n) { n     len <<= 1; n     L++; n   } n   if (len != last_len) { n     for (int i = 0; i < len; i++) { n       R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1)); n     } n     last_len = len; n   } n   for (int i = 0; i < len; i++) { n     if (i < R[i]) { n       swap(a[i], a[R[i]]); n     } n   } n   for (int j = 1; j < len; j <<= 1) { n     int T = quick_power(G, (Mod - 1) / (j << 1), Mod); n     for (int k = 0; k < len; k += (j << 1)) { n       for (int l = 0, t = 1; l < j; l++, t = 1ll * t * T % Mod) { n         int Nx = a[k + l], Ny = 1ll * t * a[j + k + l] % Mod; n         a[k + l] = (Nx + Ny) % Mod; n         a[j + k + l] = (Nx - Ny + Mod) % Mod; n       } n     } n   } n   if (flag == -1) { n     reverse(a + 1, a + len); n     for (int i = 0, t = quick_power(len, Mod - 2, Mod); i < len; i++) { n       a[i] = 1ll * a[i] * t % Mod; n     } n   } n } n int a[Maxn << 2 | 5], b[Maxn << 2 | 5], f[Maxn << 2 | 5], g[Maxn << 2 | 5]; n int main() { n   init(); n   scanf( %d%lld , &n, &m); n   for (int i = 0; i <= n; i++) { n     scanf( %d , &p[i]); n   } n   for (int i = 0; i <= n; i++) { n     a[i] = inv_f[i]; n     b[i] = 1ll * frac[n - i] * p[n - i] % Mod; n   } n   int len = 1; n   while (len <= (n << 1)) { n     len <<= 1; n   } n   NTT(a, 1, len); n   NTT(b, 1, len); n   for (int i = 0; i < len; i++) { n     a[i] = 1ll * a[i] * b[i] % Mod; n   } n   NTT(a, -1, len); n   for (int i = 0; i <= n; i++) { n     g[i] = 1ll * a[n - i] * inv_f[i] % Mod; n     g[i] = 1ll * g[i] * quick_power(inv[i + 1], m, Mod) % Mod; n   } n   for (int i = 0; i < len; i++) { n     a[i] = b[i] = 0; n   } n   for (int i = 0; i <= n; i++) { n     if (i & 1) { n       a[i] = Mod - inv_f[i]; n     } else { n       a[i] = inv_f[i]; n     } n     b[i] = 1ll * frac[n - i] * g[n - i] % Mod; n   } n   NTT(a, 1, len); n   NTT(b, 1, len); n   for (int i = 0; i < len; i++) { n     a[i] = 1ll * a[i] * b[i] % Mod; n   } n   NTT(a, -1, len); n   for (int i = 0; i <= n; i++) { n     f[i] = 1ll * a[n - i] * inv_f[i] % Mod; n   } n   for (int i = 0; i <= n; i++) { n     printf( %d  , f[i]); n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int n = s.size(); n   bool p1 = (n >= 5); n   bool p2 = 0, p3 = 0, p4 = 0; n   for (int i = 0; i < n; i++) { n     p2 |= (s[i] >=  a  && s[i] <=  z ); n     p3 |= (s[i] >=  A  && s[i] <=  Z ); n     p4 |= (s[i] >=  0  && s[i] <=  9 ); n   } n   if (p1 && p2 && p3 && p4) n     printf( Correct ); n   else n     printf( Too weak ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n int n; n map<string, int> was; n void read() { n   cin >> n; n   while (n--) { n     int c; n     string s; n     cin >> c >> s; n     sort(s.begin(), s.end()); n     if (was.count(s) == 0) { n       was[s] = c; n     } else { n       was[s] = min(was[s], c); n     } n   } n } n int getC(string a, string b) { n   if (!was.count(a) || !was.count(b)) { n     return INF; n   } n   return was[a] + was[b]; n } n void solve() { n   int ans = INF; n   if (was.count( A ) && was.count( B ) && was.count( C )) { n     ans = was[ A ] + was[ B ] + was[ C ]; n   } n   if (was.count( ABC )) { n     ans = min(ans, was[ ABC ]); n   } n   ans = min(ans, getC( A ,  BC )); n   ans = min(ans, getC( AB ,  C )); n   ans = min(ans, getC( AC ,  B )); n   ans = min(ans, getC( AB ,  BC )); n   ans = min(ans, getC( AC ,  BC )); n   ans = min(ans, getC( AB ,  AC )); n   if (ans == INF) { n     ans = -1; n   } n   cout << ans << endl; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   read(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n  n typedef long long ll; n  n  n int main(){ n  ios_base::sync_with_stdio(0); n  cin.tie(0); n  cout.tie(0); n   n  int t; n  cin >> t; n  while (t--){ n   string s; n   cin >> s; n   stack<char> a, b; n   int ans = 0; n   for (auto c: s){ n    if (c ==  (  || c ==  ) ){ n     if (c ==  ( ) n      a.push(c); n     else { n      if (!a.empty()){ n       ans++; n       a.pop(); n      } n     } n    } n    if (c ==  [  || c ==  ] ){ n     if (c ==  [ ) n      b.push(c); n     else { n      if (!b.empty()){ n       ans++; n       b.pop(); n      } n     } n    } n   } n    cout << ans <<   n ; n  } n }


#include <bits/stdc++.h> n using namespace std; n int q[123456]; n vector<int> sq; n int main() { n   int n, k; n   int a, b; n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &a, &b); n     sq.push_back(min(2 * a, b) - min(a, b)); n     q[i] = min(a, b); n     1; n   } n   sort(sq.begin(), sq.end(), greater<int>()); n   long long sum = 0; n   int flag = 1; n   for (int i = 0; i < k; i++) sum += sq[i], sq[i] < 0 ? flag = 0 : 0; n   for (int i = 1; i <= n; i++) sum += q[i]; n   printf( %lld , flag * sum); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s[n]; n   int a[n]; n   for (int x = 0; x < n; x++) { n     cin >> s[x]; n     a[x] = s[x].length(); n   } n   for (int x = 0; x < n; x++) { n     for (int y = 0; y < n - 1; y++) { n       if (a[y] > a[y + 1]) { n         int t = a[y]; n         a[y] = a[y + 1]; n         a[y + 1] = t; n         string temp = s[y]; n         s[y] = s[y + 1]; n         s[y + 1] = temp; n       } n     } n   } n   int f = 0; n   for (int x = 0; x < n; x++) { n     int i; n     for (int y = x + 1; y < n; y++) { n       i = s[y].find(s[x], 0); n       if (i == -1) { n         f = 1; n         break; n       } n     } n     if (f == 1) { n       break; n     } n   } n   if (f == 1) { n     cout <<  NO ; n   } else { n     cout <<  YES  << endl; n     for (int x = 0; x < n; x++) { n       cout << s[x] << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, i, j, k, m, c1 = 0, count2 = 0, m3 = 0; n   cin >> n; n   long long int a[n]; n   map<long long int, long long int> m1; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     m1[i] = -1; n   } n   cin >> m; n   vector<long long int> v; n   for (i = 0; i < m; i++) { n     long long int p, q, r; n     cin >> p; n     if (p == 1) { n       cin >> q >> r; n       a[q - 1] = r; n       m1[q - 1] = i; n       v.push_back(0); n     } else { n       cin >> q; n       m3 = max(m3, q); n       v.push_back(q); n     } n   } n   long long int max1[v.size() - 1]; n   memset(max1, 0, sizeof(max1)); n   long long int m_1 = 0; n   for (i = (m - 1); i >= 0; i--) { n     if (v[i] > m_1) { n       max1[i] = max(m_1, v[i]); n       m_1 = max(m_1, v[i]); n     } else { n       max1[i] = m_1; n       m_1 = max(m_1, v[i]); n     } n   } n   for (i = 0; i < n; i++) { n     if (m1[i] >= m) { n       cout << a[i] <<    ; n     } else if (m1[i] != -1) { n       cout << max(a[i], max1[m1[i]]) <<    ; n     } else { n       cout << max(a[i], m3) <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double PI = 3.14159265358979323846; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long get(long long x, long long n) { n   long long l, r; n   if (x > n) return 0; n   long long acc = 0; n   if (x % 2 == 0) { n     l = x; n     r = x + 1; n   } else { n     l = 2 * x; n     r = 2 * x + 1; n     acc += 1; n   } n   while (r <= n) { n     acc += r - l + 1; n     l *= 2; n     r = 2 * r + 1; n   } n   if (l <= n) acc += (n - l + 1); n   return acc; n } n int dfs(int x, vector<bool> &chk, vector<int> &val, int step, n         vector<map<int, int> > &e) { n   chk[x] = true; n   int maxval = -1e9 - 1; n   for (auto &k : e[x]) { n     if (chk[k.first]) continue; n     maxval = max(maxval, dfs(k.first, chk, val, step + 1, e)); n   } n   if (step == 0) n     maxval = max(maxval, val[x]); n   else if (step == 1) n     maxval = max(maxval, val[x] + 1); n   else n     maxval = max(maxval, val[x] + 2); n   return maxval; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   vector<int> a(n + 1); n   int mmm = -1e9 - 1; n   for (int i = 0; i < n; i++) { n     cin >> a[i + 1]; n     mmm = max(mmm, a[i + 1]); n   } n   set<int> s; n   for (int i = 1; i <= n; i++) { n     if (a[i] == mmm) s.insert(i); n   } n   vector<map<int, int> > v(n + 1); n   for (int i = 0; i < n - 1; i++) { n     int x, y; n     cin >> x >> y; n     v[x][y] = 1; n     v[y][x] = 1; n   } n   vector<bool> chk(n + 1); n   if (s.size() == 1) { n     int ans = dfs(*(s.begin()), chk, a, 0, v); n     cout << ans << endl; n   } else { n     bool isok = false; n     for (int i = 1; i <= n; i++) { n       if (v[i].size() + 1 < s.size()) continue; n       bool isok2 = true; n       for (auto k : s) { n         if (v[i][k] != 1 && k != i) { n           isok2 = false; n           break; n         } n       } n       if (isok2) { n         isok = true; n         break; n       } n     } n     if (isok) { n       cout << mmm + 1 << endl; n     } else { n       cout << mmm + 2 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1005; n template <typename T> n void read(T &x) { n   x = 0; n   int f = 1; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -1; n   for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + c -  0 ; n   x *= f; n } n int n; n double g[N][N]; n double E[N], prod[N]; n bool vis[N]; n int main() { n   read(n); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) { n       int x; n       read(x); n       g[i][j] = 1. * x / 100; n     } n   if (n == 1) return puts( 0 ); n   vis[n] = true; n   for (int i = 1; i < n; i++) { n     E[i] = 1; n     prod[i] = 1 - g[i][n]; n   } n   for (int i = 1; i <= n; i++) { n     double low = 1e18; n     int pos = 0; n     for (int j = 1; j <= n; j++) n       if (!vis[j] && E[j] / (1 - prod[j]) < low) n         low = E[j] / (1 - prod[j]), pos = j; n     if (pos == 1) return printf( %.10lf n , E[1] / (1 - prod[1])), 0; n     vis[pos] = true; n     for (int j = 1; j <= n; j++) n       E[j] += E[pos] / (1 - prod[pos]) * g[j][pos] * prod[j], n           prod[j] *= (1 - g[j][pos]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(); n   cout.tie(); n   int n, m; n   cin >> n >> m; n   if (n == m) { n     cout << n << endl; n     return 0; n   } n   cout << 2 << endl; n } n


#include<bits/stdc++.h> n using namespace std; n void solve(); n int main() n { n  ios_base::sync_with_stdio(false); cin.tie(NULL); n  n #ifndef ONLINE_JUDGE n  freopen( input1.txt ,  r , stdin); n  freopen( error1.txt ,  w , stderr); n  freopen( output1.txt ,  w , stdout); n #endif n  n  int t ; cin >> t; n  while (t--) n  { n   solve(); n   cout <<   n ; n  } n  n  cerr <<  time taken :   << (float)clock() / CLOCKS_PER_SEC <<   secs  << endl; n  return 0; n } n void solve() n { n  long long n, x ; n  cin >> n >> x ; n  long long a[n], b[n], s = 0, s1 = 0, j = 0; n  for (long long i = 0; i < n; i++) n   cin >> a[i]; n  for (long long i = 0; i < n; i++) n   s += a[i]; n  for (long long i = 0; i < n; i++) n   b[i] = a[i]; n  while (1) { n   if (j >= n) n    j = 0; n   if (b[j] % x != 0) n    break; n   s += a[j]; n   b[j] /= x; n   j++; n  } n  cout << s; n }


#include <bits/stdc++.h> n using namespace std; n char s[2005]; n int n, c[2005][2005], ans = 0x3f3f3f3f, was, ot[2005]; n bool chk() { n   static int ar[2005]; n   int sum = 0; n   for (int i = (1), LIM = (n); i <= LIM; i++) ar[i] = ot[i], sum += ot[i]; n   sort(ar + 1, ar + 1 + n); n   int sm = 0; n   for (int i = (1), LIM = (n - 1); i <= LIM; i++) { n     sm += ar[i]; n     if (sm == i * (i - 1) / 2) return 0; n   } n   return 1; n } n void dfs(int u, int s) { n   if (u == n + 1) { n     if (chk()) { n       if (ans > s) ans = s, was = 0; n       if (ans == s) was++; n     } n     return; n   } n   dfs(u + 1, s); n   for (int i = (1), LIM = (n); i <= LIM; i++) n     if (c[u][i]) n       ot[u]--, ot[i]++; n     else n       ot[u]++, ot[i]--; n   for (int i = (1), LIM = (n); i <= LIM; i++) swap(c[i][u], c[u][i]); n   dfs(u + 1, s + 1); n   for (int i = (1), LIM = (n); i <= LIM; i++) n     if (c[u][i]) n       ot[u]--, ot[i]++; n     else n       ot[u]++, ot[i]--; n   for (int i = (1), LIM = (n); i <= LIM; i++) swap(c[i][u], c[u][i]); n } n int main() { n   scanf( %d , &n); n   for (int i = (1), LIM = (n); i <= LIM; i++) { n     scanf( %s , s + 1); n     for (int j = (1), LIM = (n); j <= LIM; j++) n       c[i][j] = (s[j] ==  1 ), ot[i] += c[i][j]; n   } n   if (n <= 6) { n     dfs(1, 0); n     if (ans == 0x3f3f3f3f) n       puts( -1 ); n     else { n       for (int i = (1), LIM = (ans); i <= LIM; i++) n         was = 1ll * was * i % 998244353; n       printf( %d %d n , ans, was); n     } n     return 0; n   } else { n     if (chk()) { n       puts( 0 1 ); n       return 0; n     } n     for (int u = (1), LIM = (n); u <= LIM; u++) { n       for (int i = (1), LIM = (n); i <= LIM; i++) n         if (c[u][i]) n           ot[u]--, ot[i]++; n         else n           ot[u]++, ot[i]--; n       for (int i = (1), LIM = (n); i <= LIM; i++) swap(c[i][u], c[u][i]); n       if (chk()) was++; n       for (int i = (1), LIM = (n); i <= LIM; i++) n         if (c[u][i]) n           ot[u]--, ot[i]++; n         else n           ot[u]++, ot[i]--; n       for (int i = (1), LIM = (n); i <= LIM; i++) swap(c[i][u], c[u][i]); n     } n     printf( %d %d n , 1, was); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int ans, a, b, c; n int main() { n   cin >> a >> b >> c; n   ans = a; n   while (!(b >= ans + 1 && c >= ans + 2)) ans--; n   cout << 3 * ans + 3; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long inv[500005]; n void getinv() { n   inv[0] = inv[1] = 1; n   for (int i = 2; i < 500005; i++) n     inv[i] = n         ((-1000000007 / i * inv[1000000007 % i]) % 1000000007 + 1000000007) % n         1000000007; n } n long long slove(int n, int m) { n   long long ans = 1; n   for (int i = 1; i <= m; i++) n     ans = ((ans * inv[i]) % 1000000007 * (n - i + 1)) % 1000000007; n   return ans; n } n long long a[500005], n, m, c[500005]; n int main() { n   scanf( %I64d%I64d , &n, &m); n   for (int i = 0; i < n; i++) { n     scanf( %I64d , &a[i]); n   } n   getinv(); n   for (int i = 0; i <= n; i++) { n     c[i] = slove(i + m - 1, i); n   } n   for (int i = 0; i < n; i++) { n     long long ans = 0; n     for (int j = 0; j <= i; j++) { n       ans = (ans + a[j] * c[i - j]) % 1000000007; n     } n     printf( %I64d  , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1e6 + 10, M = 20, mo = 1e9 + 7; n vector<vector<long long> > f, sum; n vector<long long> g; n long long a[maxn], c[maxn]; n long long n, l, k, K, B, ans, i, j, z, x, y, N, r; n int main() { n   scanf( %lld%lld%lld , &n, &l, &k); n   g.push_back(0); n   for (i = 0; i < n; i++) { n     scanf( %lld , &a[i]); n     c[i] = a[i]; n     g.push_back(0); n   } n   if (l <= n) { n     printf( %lld n , l); n     return 0; n   } n   sort(c, c + n); n   K = min(l / n, k); n   N = (l / n) % mo; n   r = l % n; n   f.push_back(g); n   sum.push_back(g); n   for (i = 1; i <= k; i++) { n     f.push_back(g); n     sum.push_back(g); n   } n   for (i = 0; i < n; i++) { n     f[1][i] = 1; n     sum[0][i] = 1; n   } n   sum[1][0] = 1; n   for (i = 1; i < n; i++) sum[1][i] = (f[1][i] + sum[1][i - 1]) % mo; n   for (i = 2; i <= K; i++) { n     for (j = 0; j < n; j++) { n       z = upper_bound(c, c + n, c[j]) - c - 1; n       f[i][j] = sum[i - 1][z]; n     } n     sum[i][0] = f[i][0]; n     for (j = 1; j < n; j++) sum[i][j] = (f[i][j] + sum[i][j - 1]) % mo; n   } n   for (i = 1; i <= K; i++) { n     ans = (ans + (N - i + 1) * sum[i][n - 1]) % mo; n     for (j = 0; j < r; j++) { n       z = upper_bound(c, c + n, a[j]) - c - 1; n       ans = (ans + sum[i - 1][z]) % mo; n     } n   } n   if (r && K + 1 <= k) { n     i = K + 1; n     for (j = 0; j < r; j++) { n       z = upper_bound(c, c + n, a[j]) - c - 1; n       ans = (ans + sum[i - 1][z]) % mo; n     } n   } n   ans = (ans + mo) % mo; n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long i, j, flag, c, k, x, z, n, m, a[300007]; n int main() { n   cin >> n; n   long long lst0 = 0, lst1 = 1; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] == 0) n       lst0 = i + 1; n     else n       lst1 = i + 1; n   } n   cout << min(lst0, lst1) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n template <class T> n inline bool chmin(T& a, T b) { n   if (a > b) { n     a = b; n     return true; n   } n   return false; n } n template <class T> n inline bool chmax(T& a, T b) { n   if (a < b) { n     a = b; n     return true; n   } n   return false; n } n const int MOD = 1000000007; n const int INF = 1000000001; n const ll LINF = 1000000001000000001LL; n void solve(); n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout << fixed << setprecision(numeric_limits<double>::max_digits10); n   solve(); n   return 0; n } n void solve() { n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     vector<int> a(n); n     for (int i = (0); i < ((n)); i++) cin >> a[i]; n     vector<int> b(n, false); n     int p; n     for (int i = (0); i < ((m)); i++) { n       cin >> p; n       p--; n       b[p] = true; n     } n     bool ok = true; n     for (int i = (0); i < ((n)); i++) { n       for (int j = (i); j < (n); j++) { n         if (!b[j]) { n           for (int k = (j + 1); k < (n); k++) { n             if (a[i] > a[k]) { n               ok = false; n               break; n             } n           } n           if (!ok) break; n         } n       } n       if (!ok) break; n       for (int j = (i - 1); j < (0); j++) { n         if (!b[j]) { n           for (int k = (0); k < ((j + 1)); k++) { n             if (a[i] < a[k]) { n               ok = false; n               break; n             } n           } n           if (!ok) break; n         } n       } n       if (!ok) break; n     } n     cout << (ok ?  YES  :  NO ) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 400000 + 5; n vector<int> G[maxn]; n int n; n int down[maxn], up[maxn], sz[maxn], par[maxn]; n void dfs(int u, int fa) { n   int len = G[u].size(); n   par[u] = fa; n   sz[u] = 1; n   for (int i = 0; i < len; i++) { n     int v = G[u][i]; n     if (v == fa) continue; n     dfs(v, u); n     sz[u] += sz[v]; n   } n } n void dfs_down(int u, int fa) { n   int len = G[u].size(); n   down[u] = (sz[u] <= n / 2) ? sz[u] : 0; n   for (int i = 0; i < len; i++) { n     int v = G[u][i]; n     if (v == fa) continue; n     dfs_down(v, u); n     down[u] = max(down[u], down[v]); n   } n } n void dfs_up(int u, int val = 0, int fa = -1) { n   if (val <= n / 2) { n     up[u] = max(val, up[u]); n   } n   if (n - sz[u] <= n / 2) { n     up[u] = max(n - sz[u], up[u]); n   } n   int len = G[u].size(); n   int m0 = 0, m1 = 0; n   for (int i = 0; i < len; i++) { n     int v = G[u][i]; n     if (v == fa) continue; n     if (down[v] >= m0) { n       m1 = m0; n       m0 = down[v]; n     } else if (down[v] >= m1) { n       m1 = down[v]; n     } n   } n   for (int i = 0; i < len; i++) { n     int v = G[u][i]; n     if (v == fa) continue; n     dfs_up(v, max(up[u], down[v] == m0 ? m1 : m0), u); n   } n } n int ans[maxn]; n int main() { n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     G[u].push_back(v); n     G[v].push_back(u); n   } n   dfs(1, -1); n   dfs_down(1, -1); n   dfs_up(1, 0, -1); n   for (int u = 1; u <= n; u++) { n     ans[u] = 1; n     int len = G[u].size(); n     for (int j = 0; j < len; j++) { n       int v = G[u][j]; n       if (v == par[u]) { n         if (n - sz[u] - up[u] > n / 2) ans[u] = 0; n       } else { n         if (sz[v] - down[v] > n / 2) ans[u] = 0; n       } n     } n   } n   printf(  n ); n   for (int i = 1; i <= n; i++) { n     printf( %d%c , ans[i], i == n ?   n  :    ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int mod, h1, a1, x1, y1, h2, a2, x2, y2, c1, c2, i, n       ans = 0, temp, j, m1, m2, h11, h12; n   cin >> mod; n   cin >> h1 >> a1; n   cin >> x1 >> y1; n   cin >> h2 >> a2; n   cin >> x2 >> y2; n   temp = h2; n   ans = 0; n   h11 = h1; n   h12 = h2; n   for (i = 1; i <= mod + 10; i++) { n     ans++; n     h11 = (x1 * h11 + y1) % mod; n     h12 = (x2 * h12 + y2) % mod; n     if (h11 == a1 && h12 == a2) { n       cout << ans << endl; n       return 0; n     } n   } n   ans = 0; n   for (i = 1; i <= mod + 10 && h1 != a1; i++) { n     ans++; n     h1 = (x1 * h1 + y1) % mod; n     h2 = (x2 * h2 + y2) % mod; n   } n   if (h1 != a1) { n     cout << -1 << endl; n     return 0; n   } n   if (h1 == a1 && h2 == a2) { n     cout << ans << endl; n     return 0; n   } n   c1 = 0; n   for (i = 1; i <= mod + 10 && temp != a2; i++) { n     c1++; n     temp = (x2 * temp + y2) % mod; n   } n   if (temp != a2) { n     cout << -1 << endl; n     return 0; n   } n   long long int p, q, r, s; n   p = ans; n   q = c1; n   a1 = h1; n   h1 = (x1 * h1 + y1) % mod; n   ans = 1; n   for (i = 1; i <= mod + 10 && h1 != a1; i++) { n     ans++; n     h1 = (x1 * h1 + y1) % mod; n   } n   if (h1 != a1) { n     cout << -1 << endl; n     return 0; n   } n   r = ans; n   ans = 1; n   a2 = temp; n   h2 = temp; n   h2 = (x2 * h2 + y2) % mod; n   for (i = 1; i <= mod + 10 && h2 != a2; i++) { n     ans++; n     h2 = (x2 * h2 + y2) % mod; n   } n   if (h2 != a2) { n     cout << -1 << endl; n     return 0; n   } n   s = ans; n   if (p + r == q + s) { n     cout << p + r << endl; n     return 0; n   } n   for (i = 1; i <= mod + 10; i++) { n     j = (p - q + r * i) / s; n     if (p + i * r == q + j * s) { n       cout << p + i * r << endl; n       return 0; n     } n   } n   cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 10, C = 30; n int nex[N][C], p, n, par[N], dfa[N][C]; n string s, t; n vector<pair<int, char> > adj_matrix[N]; n long long ans; n void dfs(int v, int p, int state) { n   if (state == (int)t.size()) ans++; n   for (auto pa : adj_matrix[v]) { n     int u = pa.first; n     char c = pa.second; n     if (u == p) continue; n     dfs(u, v, nex[state][c -  a ]); n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   int cur = n + 1; n   for (int i = 1; i < n; i++) { n     cin >> p; n     p--; n     cin >> s; n     int j = 0, lp = p; n     while (j + 1 < s.size()) { n       adj_matrix[lp].push_back({cur, s[j]}); n       lp = cur; n       j++; n       cur++; n     } n     adj_matrix[lp].push_back({i, s[j]}); n   } n   cin >> t; n   nex[0][t[0] -  a ] = 1; n   int l = 0; n   for (int i = 1; i <= (int)t.size(); i++) { n     for (int j = 0; j < 28; j++) { n       if (i < (int)t.size()) { n         if (j == (t[i] -  a )) n           nex[i][j] = i + 1; n         else n           nex[i][j] = nex[l][j]; n       } else n         nex[i][j] = nex[l][j]; n     } n     if (i < (int)t.size()) l = nex[l][t[i] -  a ]; n   } n   dfs(0, 0, 0); n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int bef[100050]; n int main() { n   string s, s1; n   cin >> s; n   int len = s.size(); n   int last_a = -1; n   int now = 0; n   for (int i = 0; i < len; i++) { n     if (s[i] !=  a ) { n       s1 += s[i]; n       bef[now] = i; n       now++; n     } else { n       last_a = i; n     } n   } n   if (now % 2 == 1) n     cout <<  :(  << endl; n   else { n     for (int i = 0; i < now / 2; i++) { n       if (s1[i] != s1[i + now / 2]) { n         cout <<  :(  << endl; n         return 0; n       } n     } n     if (last_a == -1 || bef[now / 2] > last_a || now == 0) { n       for (int i = 0; i < (now != 0 ? bef[now / 2] : len); i++) { n         cout << s[i]; n       } n       cout << endl; n     } else { n       cout <<  :(  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const N = 3e5 + 10, oo = 1e9, M = 64; n long long const OO = 1e18; n int mod = oo + 7; n double const eps = 1e-4, PI = acos(-1); n int n, k, in[N]; n int cc = 0, m = 0, cnt = 0; n vector<int> dv[N]; n bool brute(int i, int h) { n   if (h > k) return 0; n   if (i == m + 1) return h == k; n   if (h == k) return 1; n   int y = 0; n   for (auto v : dv[i]) n     if (in[v]) y++; n   in[i] = 1; n   cnt++; n   if (brute(i + 1, h + y)) return 1; n   cnt--; n   in[i] = 0; n   if (brute(i + 1, h)) return 1; n   return 0; n } n int main() { n   scanf( %d%d , &n, &k); n   int all = 0; n   for (int i = 1; i <= n; i++) n     for (int j = i + i; j <= n; j += i) all++, dv[j].push_back(i); n   if (all < k) { n     puts( No ); n     return 0; n   } n   for (int i = 1; i <= n && cc < k; i++) { n     cc += dv[i].size(); n     in[i] = 1; n     cnt++; n     m++; n   } n   if (m < 10000) { n     cnt = 0; n     memset(in, 0, sizeof in); n     brute(1, 0); n   } else n     for (int i = m; cc != k; i--) n       if ((int)dv[i].size() == 1) cc--, in[i] = 0, cnt--; n   puts( Yes ); n   printf( %d n , cnt); n   for (int i = 1; i <= m; i++) n     if (in[i]) printf( %d  , i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9 + 7; n long long a[20][10][10], b[20][10][10], c[20]; n int read_int() { n   int a = 0; n   int b = 1, c = getchar(); n   if (c ==  - ) n     b = -1; n   else n     a = c - 48; n   while ((c = getchar()) !=     && c !=   n ) a = a * 10 + c - 48; n   a *= b; n   return a; n } n int findmax(long long n) { n   long long temp = n; n   int m = 0; n   while (temp > 0) { n     if (temp % 10 > m) m = temp % 10; n     temp = temp / 10; n   } n   return m; n } n int main() { n   cin.sync_with_stdio(false); n   cout.sync_with_stdio(false); n   long long n; n   cin >> n; n   long long m, total; n   for (int l = 0; l < 19; l++) n     for (int x = 9; x >= 0; x--) n       for (int y = 0; y <= 9; y++) { n         total = 0; n         if (!l) n           if (!x) n             if (y) { n               b[l][x][y] = 1; n               a[l][x][y] = 0; n             } else { n               b[l][x][y] = 0; n               a[l][x][y] = 0; n             } n           else if (x > y) { n             a[l][x][y] = y + 10 - x; n             b[l][x][y] = 1; n           } else { n             a[l][x][y] = 10 - x; n             b[l][x][y] = 2; n           } n         else { n           total = 0; n           m = y; n           for (int j = 9; j >= 0; j--) { n             total += b[l - 1][x > j ? x : j][m]; n             m = a[l - 1][x > j ? x : j][m]; n           } n           a[l][x][y] = m; n           b[l][x][y] = total; n         } n       } n   m = n % 10; n   long long K = n / 10, res = 0; n   int len = 0; n   while (1) { n     while (K % 10 != 9 && K) { n       res += b[len][findmax(K)][m]; n       m = a[len][findmax(K)][m]; n       K--; n     } n     if (!K) { n       res += b[len][0][m]; n       break; n     } n     K /= 10; n     len++; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, ar[10]; n   cin >> a >> b >> c; n   ar[0] = a + b + c; n   ar[1] = a * b * c; n   ar[2] = a + b * c; n   ar[3] = a * b + c; n   ar[4] = (a + b) * c; n   ar[5] = a * (b + c); n   sort(ar, ar + 6); n   cout << ar[6 - 1]; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n int n; n int a[maxn]; n int nivel[maxn], in[maxn], out[maxn], tt; n vector<int> grafo[maxn]; n struct BinaryLifting { n   int tab[maxn][20]; n   void dfs(int u, int p) { n     in[u] = ++tt; n     for (auto v : grafo[u]) { n       if (v == p) continue; n       nivel[v] = nivel[u] + 1, tab[v][0] = u; n       dfs(v, u); n     } n     out[u] = tt; n   } n   void build(void) { n     for (int j = 1; j < 20; j++) n       for (int i = 1; i <= n; i++) tab[i][j] = tab[tab[i][j - 1]][j - 1]; n   } n   int intersect(int u, int v, int w_u, int w_v) { n     if (nivel[u] < nivel[v]) swap(u, v), swap(w_u, w_v); n     for (int i = 19; i >= 0; i--) n       if (nivel[u] - (1 << i) >= nivel[v] && w_u >= (1 << i)) n         u = tab[u][i], w_u -= (1 << i); n     if (nivel[u] != nivel[v]) return -1; n     if (u == v) return u; n     for (int i = 19; i >= 0; i--) n       if (tab[u][i] && tab[v][i] && tab[u][i] != tab[v][i] && n           min(w_u, w_v) >= (1 << i)) n         u = tab[u][i], v = tab[v][i], w_u -= (1 << i), w_v -= (1 << i); n     if (tab[u][0] != tab[v][0] || min(w_u, w_v) == 0) return -1; n     return tab[u][0]; n   } n   int dist(int u, int v) { n     return nivel[u] + nivel[v] - 2 * nivel[intersect(u, v, nivel[u], nivel[v])]; n   } n   int get_intersection(int u, int v, int root) { n     int k = -1; n     int lca_u = intersect(u, root, nivel[u], nivel[root]); n     int lca_v = intersect(v, root, nivel[v], nivel[root]); n     int aux = intersect(u, v, nivel[u] - nivel[lca_u], nivel[v] - nivel[lca_v]); n     if (k == -1 || (aux != -1 && dist(aux, root) > dist(k, root))) k = aux; n     aux = n         intersect(u, root, nivel[u] - nivel[lca_u], nivel[root] - nivel[lca_v]); n     if (k == -1 || (aux != -1 && dist(aux, root) > dist(k, root))) k = aux; n     aux = n         intersect(root, v, nivel[root] - nivel[lca_u], nivel[v] - nivel[lca_v]); n     if (k == -1 || (aux != -1 && dist(aux, root) > dist(k, root))) k = aux; n     aux = intersect(root, root, nivel[root] - nivel[lca_u], n                     nivel[root] - nivel[lca_v]); n     if (k == -1 || (aux != -1 && dist(aux, root) > dist(k, root))) k = aux; n     return k; n   } n   int kth(int u, int k) { n     for (int i = 19; i >= 0; i--) n       if (tab[u][i] && k >= (1 << i)) u = tab[u][i], k -= (1 << i); n     return u; n   } n } LCA; n struct SegmentTree { n   long long tree[3 * maxn], lazy[3 * maxn]; n   void flush(int node, int l, int r) { n     if (!lazy[node]) return; n     if (l != r) { n       lazy[2 * node] += lazy[node]; n       lazy[2 * node + 1] += lazy[node]; n     } n     tree[node] += 1ll * (r - l + 1) * lazy[node]; n     lazy[node] = 0; n   } n   void upd(int node, int l, int r, int a, int b, int v) { n     flush(node, l, r); n     if (l > b || r < a) return; n     if (l >= a && r <= b) { n       lazy[node] = 1ll * v; n       flush(node, l, r); n       return; n     } n     int mid = (l + r) >> 1; n     upd(2 * node, l, mid, a, b, v); n     upd(2 * node + 1, mid + 1, r, a, b, v); n     tree[node] = tree[2 * node] + tree[2 * node + 1]; n   } n   long long query(int node, int l, int r, int a, int b) { n     flush(node, l, r); n     if (l > b || r < a) return 0; n     if (l >= a && r <= b) return tree[node]; n     int mid = (l + r) >> 1; n     return query(2 * node, l, mid, a, b) + n            query(2 * node + 1, mid + 1, r, a, b); n   } n } seg; n bool is_ancestor(int u, int v) { return (in[v] >= in[u] && out[v] <= out[u]); } n int main(void) { n   int q; n   scanf( %d %d , &n, &q); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     grafo[u].push_back(v); n     grafo[v].push_back(u); n   } n   LCA.dfs(1, 0); n   LCA.build(); n   for (int i = 1; i <= n; i++) seg.upd(1, 1, n, in[i], in[i], a[i]); n   int root = 1; n   while (q--) { n     int op; n     scanf( %d , &op); n     if (op == 1) { n       int v; n       scanf( %d , &v); n       root = v; n     } else if (op == 2) { n       int u, v, x; n       scanf( %d %d %d , &u, &v, &x); n       int k = LCA.get_intersection(u, v, root); n       if (!is_ancestor(k, root)) n         seg.upd(1, 1, n, in[k], out[k], x); n       else { n         seg.upd(1, 1, n, 1, n, x); n         if (k != root) { n           int w = LCA.kth(root, nivel[root] - nivel[k] - 1); n           seg.upd(1, 1, n, in[w], out[w], -x); n         } n       } n     } else { n       int u; n       scanf( %d , &u); n       if (!is_ancestor(u, root)) n         printf( %lld n , seg.query(1, 1, n, in[u], out[u])); n       else { n         if (u == root) n           printf( %lld n , seg.query(1, 1, n, 1, n)); n         else { n           int w = LCA.kth(root, nivel[root] - nivel[u] - 1); n           printf( %lld n , n                  seg.query(1, 1, n, 1, n) - seg.query(1, 1, n, in[w], out[w])); n         } n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n #define ll long long n #define deb(x) cout << #x <<  =  << x << endl n #define deb2(x, y) cout << #x <<  =  << x <<  ,  << #y <<  =  << y << endl n #define nl   n  n void solve() n { n     int n; n     cin >> n; n     int arr[n][n]; n     int x = 1; n     for(int i=0;i<n;i++){ n         for(int j=0;j<n;j++){ n             if(i==j){ n                 arr[i][j]=x; n                 x++; n             } n         } n     } n     for (int k = 1; k <= 2 * n - 2; k++) n     { n         for (int i = 0; i < n; i++) n         { n             for (int j = 0; j < n; j++) n             { n                 if(i!=j){ n                     if (i + j == k) n                 { n                     arr[i][j] = x; n                     x++; n                 } n                 } n             } n         } n     } n     if (n == 1) n     { n         cout << 1 <<   n ; n         return; n     } n     else if (n == 2) n     { n         cout << -1 <<   n ; n     } n     else n     { n         for (int i = 0; i < n; i++) n         { n             for (int j = 0; j < n; j++) n             { n                 cout << arr[i][j] <<    ; n             } n             cout <<   n ; n         } n     } n } n int main() n { n     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n     srand(chrono::high_resolution_clock::now().time_since_epoch().count()); n     int t; n     cin >> t; n     while (t--) n     { n         solve(); n     } n     return 0; n }


#include <bits/stdc++.h> n const int MOD = 1e9 + 7; n int primes[100000]; n using namespace std; n bool cmp(const pair<int, int>& a, const pair<int, int>& b) { n   return a.first > b.first; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int n, k; n   cin >> n >> k; n   int a[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   long long sum = 0; n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     sum += a[i]; n     long long p = sum / k; n     cout << abs(p - ans) <<    ; n     ans = p; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool isPrime(int num) { n   for (int i = 2; i <= sqrt(num) + 1; ++i) { n     if (num % i == 0) return false; n   } n   return true; n } n int main() { n   int n; n   cin >> n; n   if (n < 9) { n     cout << 1 << endl; n     cout << n << endl; n   } else if (n == 9) { n     cout << 3 << endl; n     cout << 3 <<     << 3 <<     << 3 << endl; n   } else if (n == 11) { n     cout << 3 << endl; n     cout << 3 <<     << 3 <<     << 5 << endl; n   } else { n     cout << 3 << endl; n     n -= 3; n     for (int i = 3; i <= n / 2; ++i) { n       if (isPrime(i) && isPrime(n - i)) { n         cout << 3 <<     << i <<     << n - i << endl; n         break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool yes(int x1, int x2, int x3, int x4) { n   int num1 = (x1 + x2 + x3 + x4) / 4, num2 = (x2 + x3) / 2, num3 = x4 - x1; n   if (((x1 + x2 + x3 + x4) % 4 != 0) || ((x2 + x3) % 2 != 0) || num1 != num2 || n       num1 != num3 || x1 == -1 || x2 == -1 || x3 == -1 || x4 == -1) n     return false; n   if (x1 <= x2 && x2 <= x3 && x3 <= x4) return true; n   return false; n } n bool fun(int x1, int x2, int x3, int x4, vector<int> sol) { n   for (int i = 1; i <= 2000; i++) { n     vector<int> Sol = sol; n     int X1 = x1, X2 = x2, X3 = x3, X4 = x4; n     x1 = i; n     x4 = i * 3; n     if (x2 == -1 && x3 != -1) n       x2 = x4 + x1 - x3, sol.push_back(x2); n     else if (x2 != -1 && x3 == -1) n       x3 = x4 + x1 - x2, sol.push_back(x3); n     else if (x2 != -1 && x3 != -1 && x2 + x3 != x1 + x4) n       continue; n     if (yes(x1, x2, x3, x4)) { n       cout <<  YES n ; n       for (int i = 0; i < (int)sol.size(); i++) cout << sol[i] <<   n ; n       return true; n     } n     x1 = X1, x2 = X2, x3 = X3, x4 = X4, sol = Sol; n   } n   return false; n } n bool Fun(int x1, int x2, int x3, int x4, vector<int> sol) { n   for (int i = 1; i <= 2000; i++) { n     x2 = i; n     x3 = x4 + x1 - x2; n     vector<int> Sol = sol; n     sol.push_back(x2); n     sol.push_back(x3); n     if (yes(x1, x2, x3, x4)) { n       cout <<  YES n ; n       for (int I = 0; I < (int)sol.size(); I++) cout << sol[I] << endl; n       return true; n     } n     sol = Sol; n   } n   return false; n } n int main() { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   int ar[4]; n   memset(ar, -1, sizeof(ar)); n   for (int i = 0; i < n; i++) cin >> ar[i]; n   if (n == 0) { n     cout <<  YES n1 n1 n3 n3 ; n     goto ss; n   } n   for (int i = 0; i < 4; i++) n     for (int i1 = 0; i1 < 4; i1++) n       for (int i2 = 0; i2 < 4; i2++) n         for (int i3 = 0; i3 < 4; i3++) { n           if (i == i1 || i == i2 || i == i3 || i1 == i2 || i1 == i3 || i2 == i3) n             continue; n           vector<int> sol; n           int x1 = ar[i], x2 = ar[i1], x3 = ar[i2], x4 = ar[i3]; n           if (x1 != -1 && x4 != -1) { n             if (x1 * 3 != x4) continue; n           } else if (x1 == -1 && x4 != -1) { n             x1 = x4 / 3; n             if (x4 % 3 != 0) continue; n             sol.push_back(x1); n           } else if (x1 != -1 && x4 == -1) { n             x4 = x1 * 3; n             sol.push_back(x4); n           } else if (x1 == -1 && x4 == -1) n             if (fun(x1, x2, x3, x4, sol)) goto ss; n           if (x2 == -1 && x3 == -1) { n             if (Fun(x1, x2, x3, x4, sol)) goto ss; n           } else if (x2 == -1) { n             x2 = x4 + x1 - x3; n             sol.push_back(x2); n           } else if (x3 == -1) { n             x3 = x4 + x1 - x2; n             sol.push_back(x3); n           } n           if (yes(x1, x2, x3, x4)) { n             cout <<  YES n ; n             for (int I = 0; I < (int)sol.size(); I++) cout << sol[I] <<   n ; n             goto ss; n           } n         } n   cout <<  NO ; n ss: n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int a[120000]; n int main() { n   cin >> n >> k; n   for (int i = 0; i < n * 2; i++) a[i] = i; n   for (int i = 0; i < k; i++) { n     int now = i * 2; n     swap(a[now], a[now + 1]); n   } n   k = n - k; n   for (int i = 0; i < n * 2; i++) { n     printf( %d  , a[i] + 1); n   } n   puts(  ); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void _dbg(const char* _s, T _h) { n   cerr << _s <<   =   << _h <<   n ; n } n template <typename T, typename... Ts> n void _dbg(const char* _s, T _h, Ts... _t) { n   int _b = 0; n   while (((_b += *_s ==  ( ) -= *_s ==  ) ) != 0 || *_s !=  , ) cerr << *_s++; n   cerr <<   =   << _h <<  , ; n   _dbg(_s + 1, _t...); n } n const int64_t INF = static_cast<int64_t>(1e9) + 7; n const int64_t LINF = INF * INF; n const int MAXN = static_cast<int>(1e6) + 17; n void solve() { n   int n; n   int64_t second; n   cin >> n >> second; n   vector<int> v(n); n   int64_t sum = 0; n   int right = INF; n   for (auto& el : (v)) { n     cin >> el; n     sum += el; n     right = min(right, el); n   } n   if (sum < second) { n     cout <<  -1 n ; n     return; n   } n   ++right; n   int left = 0; n   while (right - left > 1) { n     int mid = (left + right) / 2; n     int64_t amount = 0; n     for (int i = 0; i < n; ++i) { n       amount += v[i] - mid; n     } n     if (amount >= second) { n       left = mid; n     } else { n       right = mid; n     } n   } n   cout << left; n } n int main() { n   cin.tie(nullptr); n   cout.tie(nullptr); n   ios_base::sync_with_stdio(false); n   int q = 1; n   for (int i = 0; i < q; ++i) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[100005], top, s[100005]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(); n   cout.tie(); n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   top = -1; n   for (int i = n - 1; i >= 0; --i) { n     s[++top] = a[i]; n     while (top > 0 && s[top] == s[top - 1]) ++s[--top]; n   } n   cout << s[0] - top; n   return 0; n } n


#include <bits/stdc++.h> n const int MaxN = 100111; n const int mod = 1000000009; n int f[MaxN]; n int getFather(int x) { n   if (f[x] == x) return f[x]; n   return f[x] = getFather(f[x]); n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) f[i] = i; n   int ans = 0; n   for (int i = 0; i < m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     if (getFather(x) == getFather(y)) n       ans = ((ans * 2) % mod + 1) % mod; n     else n       f[getFather(y)] = getFather(x); n     printf( %d n , ans % mod); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const double eps = 1e-12; n const int INF = (1 << 31) - 1; n const long long LLINF = ((long long)1 << 62) - 1; n const int maxn = 1000 * 100 + 10; n int a[maxn], sum[maxn], sum2[maxn]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     if (i) n       sum[i] = sum[i - 1] + a[i]; n     else n       sum[i] = a[i]; n   } n   string s; n   getline(cin, s); n   getline(cin, s); n   for (int i = (int)s.size() - 1; i >= 0; i--) { n     if (i != (int)s.size() - 1) { n       sum2[i] = sum2[i + 1] + (s[i] -  0 ) * a[i]; n     } else { n       sum2[i] = (s[i] -  0 ) * a[i]; n     } n   } n   long long maxx = 0LL; n   for (int i = (int)s.size() - 1; i >= 0; i--) { n     if (s[i] ==  1 ) { n       long long cur = 0LL; n       cur = sum[i - 1]; n       cur += sum2[i + 1]; n       maxx = max(maxx, cur); n     } n   } n   printf( %lld , max(maxx, (long long)sum2[0])); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename... T> n inline void inp(T &...args) { n   ((cin >> args), ...); n } n template <typename T> n inline istream &operator>>(istream &in, vector<T> &a) { n   for (auto &x : a) in >> x; n   return in; n } n template <typename T, typename U> n inline istream &operator>>(istream &in, pair<T, U> &a) { n   in >> a.first >> a.second; n   return in; n } n void solve(int &T) { n   long long int n, k; n   cin >> n >> k; n   vector<long long int> a(n); n   cin >> a; n   map<long long int, long long int> m; n   for (long long int i = (0); ((1) > 0 ? i < (n) : i > (n)); i += (1)) { n     m[a[i]]++; n   } n   set<long long int> s; n   for (long long int i = (0); ((1) > 0 ? i < (n) : i > (n)); i += (1)) { n     if (a[i] <= k) { n       long long int x = a[i]; n       long long int y = k - a[i]; n       if (m.count(y) and !s.count(min(x, y))) { n         if (x == y and m[y] >= 2) { n           s.insert(min(x, y)); n         } else n           s.insert(min(x, y)); n       } n     } n   } n   11; n   int ok = 1; n   vector<long long int> ans(n, 0); n   for (long long int i = (0); ((1) > 0 ? i < (n) : i > (n)); i += (1)) { n     if (s.count(a[i])) { n       ans[i] = 1; n       if (k % 2 == 0 and a[i] == k / 2) { n         ans[i] = ok; n         ok ^= 1; n       } n     } n   } n   for (auto &X : ans) { n     cout << X <<    ; n   } n   cout <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int t = 1; n   cin >> t; n   for (int i = 1; i <= t; i++) { n     if (0) cerr <<  Case #  << i <<   n ; n     solve(i); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool debug = false; n int n, m, k; n int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; n long long ln, lk, lm; n int a[10]; n bool v[9], vv[9]; n bool walk(int x) { n   v[x] = 1; n   if (a[x] == 1 || vv[a[x]]) return vv[x] = 1; n   if (v[a[x]]) return 0; n   return vv[x] = walk(a[x]); n } n bool ck() { n   for (int i = 2; i <= k; i++) { n     v[i] = 0; n     vv[i] = 0; n   } n   for (int i = 2; i <= k; i++) n     if (!v[i]) { n       if (!walk(i)) { n         return 0; n       } n     } n   return 1; n } n long long tmp; n void dfs(int x) { n   if (x == k + 1) { n     if (ck()) tmp++; n     return; n   } n   for (int(i) = 1; (i) <= (int)(k); (i)++) n     if (i != x) { n       a[x] = i; n       dfs(x + 1); n     } n } n int main() { n   scanf( %d%d , &n, &k); n   long long ans = k; n   dfs(2); n   (ans *= tmp) %= 1000000007; n   m = n - k; n   for (int i = k + 1; i <= n; i++) { n     (ans *= m) %= 1000000007; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n const int MAXN = 1e5 + 5; n long long ans[MAXN], b[MAXN]; n int n, first[65], cnt_ans; n bool selected[MAXN]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %lld , &b[i]); n   std::sort(b, b + n); n   memset(first, 0xff, sizeof(first)); n   for (int i = 0; i < n; i++) { n     for (int j = 60; j >= 0; j--) { n       if (b[i] >> j & 1) { n         if (first[j] == -1) first[j] = i; n         break; n       } n     } n   } n   long long cur = 0; n   bool flag = true; n   for (int i = 0; i < n; i++) { n     int j; n     for (j = 0; j <= 60; j++) n       if (!(cur >> j & 1)) { n         if (first[j] != -1 && !selected[first[j]]) { n           selected[first[j]] = true; n           cur ^= b[first[j]]; n           ans[cnt_ans++] = b[first[j]++]; n           if (!(b[first[j]] >> j)) first[j] = -1; n           break; n         } n       } n     if (j == 61) { n       flag = false; n       break; n     } n   } n   if (flag) { n     printf( Yes n ); n     for (int i = 0; i < n; i++) printf( %lld  , ans[i]); n     printf(  n ); n   } else { n     printf( No n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, K; n char c[10]; n int vl(int x, int y) { n   printf( 1 %d %d n , x, y), fflush(stdout); n   return scanf( %s , c), c[0] ==  T ; n } n int ef(int L, int R) { n   for (; L < R;) n     vl((L + R >> 1), (L + R >> 1) + 1) ? R = (L + R >> 1) n                                        : L = (L + R >> 1) + 1; n   return L; n } n int main() { n   scanf( %d%d , &N, &K); n   int Ans1 = ef(1, N), Ans2 = ef(1, Ans1 - 1); n   if (Ans1 == Ans2 || !vl(Ans2, Ans1)) Ans2 = ef(Ans1 + 1, N); n   printf( 2 %d %d n , Ans1, Ans2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int b, f, p, h, c, money = 0; n     scanf( %d %d %d %d %d , &b, &p, &f, &h, &c); n     b /= 2; n     if (h > c) { n       money = min(b, p) * h; n       b -= min(b, p); n       money += min(b, f) * c; n     } else { n       money = min(b, f) * c; n       b -= min(b, f); n       money += min(b, p) * h; n     } n     printf( %d n , money); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   { n     int n, m; n     cin >> n >> m; n     vector<int> a(n + 2); n     for (int i = 1; i <= n; i++) cin >> a[i]; n     a[0] = 0, a[n + 1] = m; n     vector<int> light(n + 2, 0), dark(n + 2, 0); n     for (int i = 1; i <= n + 1; i++) { n       light[i] = light[i - 1], dark[i] = dark[i - 1]; n       if (i % 2) n         light[i] += a[i] - a[i - 1]; n       else n         dark[i] += a[i] - a[i - 1]; n     } n     int ans = light[n + 1]; n     for (int i = 1; i <= n + 1; i++) { n       ans = n           max(ans, light[i - 1] + dark[n + 1] - dark[i] + a[i] - a[i - 1] - 1); n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1 << 23; n bool a[maxn]; n bool vis[maxn]; n int n, m, x; n void dfs(int x) { n   if (vis[x]) return; n   vis[x] = true; n   for (int i = 0; i <= n; i++) n     if (x & (1 << i)) dfs(x ^ (1 << i)); n   if (a[x]) dfs((1 << n) - 1 - x); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) scanf( %d , &x), a[x] = true; n   int ans = 0; n   for (int i = 0; i < (1 << n); i++) n     if (!vis[i] && a[i]) dfs(i), ans++; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long n, pos, lpos; n   int q; n   cin >> n >> q; n   for (int i = 0; i < q; i++) { n     cin >> pos; n     while (pos % 2 == 0) { n       lpos = pos / 2 + 1; n       pos += n - lpos + 1; n     } n     cout << pos / 2 + 1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e9; n const long long N = 1e5 + 10; n const long long bits = 30; n const long long mod = 1e9 + 7; n const long double pi = 3.14159265358979323846; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   std::cout << std::fixed << std::setprecision(20); n   ; n   long double n; n   cin >> n; n   long double ans = 0; n   for (long double i = 1; i <= n; i++) { n     ans += (long double)1.0 / i; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int nx[10000][26], al[26], l1, l2; n string s1, s2; n int main() { n   cin >> s1 >> s2; n   l1 = s1.size(); n   l2 = s2.size(); n   for (int i = 0; i < l1; i++) fill(nx[i], nx[i] + 26, -1); n   fill(al, al + 26, -1); n   for (int i = l1 - 1; i >= 0; i--) al[s1[i] -  a ] = i; n   for (int i = l1 - 1; i >= 0; i--) { n     for (int j = 0; j < 26; j++) nx[i][j] = al[j]; n     al[s1[i] -  a ] = i; n   } n   int cur = l1 - 1, ans = 0; n   for (int i = 0; i < l2; i++) { n     if (nx[cur][s2[i] -  a ] == -1) { n       cout << -1 << endl; n       return 0; n     } n     if (nx[cur][s2[i] -  a ] <= cur) ans++; n     cur = nx[cur][s2[i] -  a ]; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long Getint() { n   char ch = getchar(); n   long long x = 0, fh = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) fh = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     (x *= 10) += ch ^ 48; n     ch = getchar(); n   } n   return fh * x; n } n const int N = 200005; n int n, h; n long long mod, fc[N], fiv[N], inv[N], sm[N]; n int su[N]; n struct nod { n   int a, l; n   nod(int x = 0, int y = 0) { n     a = x; n     l = y; n   } n }; n vector<nod> a; n inline long long Solve(int l1, int l2) { n   long long Ans = 1ll * l1 * l2 % mod * inv[2] % mod; n   for (int i = 1; i <= l1; i++) (Ans += sm[i] - sm[i + l2] + mod) %= mod; n   return (Ans % mod + mod) % mod; n } n void Build(int l, int r, int h) { n   if (h == 1) { n     su[r - l + 1]++; n     return; n   } n   if (l == r) { n     su[1]++; n     return; n   } n   int mid = l + r >> 1; n   Build(l, mid, h - 1); n   Build(mid + 1, r, h - 1); n } n int main() { n   n = Getint(); n   h = Getint(); n   mod = Getint(); n   fc[0] = fc[1] = fiv[0] = fiv[1] = inv[0] = inv[1] = 1; n   for (int i = 2; i <= N - 1; i++) { n     fc[i] = fc[i - 1] * i % mod; n     inv[i] = (mod - mod / i) * inv[mod % i] % mod; n     fiv[i] = fiv[i - 1] * inv[i] % mod; n   } n   for (int i = 1; i <= N - 1; i++) { n     sm[i] = (sm[i - 1] + inv[i]) % mod; n   } n   Build(1, n, h); n   long long Ans = 0; n   for (int i = 1; i <= n; i++) { n     if (!su[i]) continue; n     a.push_back(nod(su[i], i)); n     (Ans += 1ll * su[i] * i % mod * (i - 1) % mod * inv[4]) %= mod; n   } n   for (int i = 0; i <= int(a.size()) - 1; i++) { n     (Ans += 1ll * a[i].a * (a[i].a - 1) % mod * inv[2] % mod * n             Solve(a[i].l, a[i].l)) %= mod; n     for (int j = i + 1; j <= int(a.size()) - 1; j++) { n       (Ans += 1ll * a[i].a * a[j].a % mod * Solve(a[i].l, a[j].l)) %= mod; n     } n   } n   cout << Ans % mod <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int G = 2520; n const int B = 41; n const int MAXN = B * G; n unsigned char S[G][B], ps[B + 1][G][4], mp[256]; n string e; n void Pre() { n   mp[ A ] = 0; n   mp[ G ] = 1; n   mp[ T ] = 2; n   mp[ C ] = 3; n } n void build(int g) { n   for (int i = 0; i < 4; i++) { n     ps[0][g][i] = 0; n     for (int j = 0; j < B; j++) ps[j + 1][g][i] = ps[j][g][i] + (S[g][j] == i); n   } n } n void chng(int p, char c) { n   S[p % G][p / G] = c; n   build(p % G); n } n inline int nxt(int x, int y) { n   if (x % G <= y) n     return (x + (y - x % G)) / G; n   else n     return (x + (G + y - x % G)) / G; n } n inline int get(int g, int l, int r, int x) { return ps[r][g][x] - ps[l][g][x]; } n int t(int i) { return e[i % e.size()]; } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   Pre(); n   string s; n   cin >> s; n   for (int i = 0; i < s.size(); i++) S[i % G][i / G] = mp[s[i]]; n   for (int i = 0; i < G; i++) build(i); n   int q; n   cin >> q; n   while (q--) { n     int type; n     cin >> type; n     if (type == 1) { n       int x; n       char c; n       cin >> x >> c; n       x--; n       chng(x, mp[c]); n     } else { n       int l, r; n       cin >> l >> r >> e; n       l--; n       int ans = 0; n       for (int i = 0; i < G; i++) { n         ans += get(i, nxt(l, i), nxt(r, i), mp[t((i - l % G + G) % G)]); n       } n       cout << ans << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 9e4; n const int INF = 1 << 30; n const long long mod = 1e9 + 7; n const double eps = 1e-8; n int main() { n   long long a[100] = {0,   4,   10,  20,  35,  56,  83, 116, n                       155, 198, 244, 292, 341, 390, 439}; n   long long n; n   while (cin >> n) n     if (n <= 14) n       cout << a[n] << endl; n     else n       cout << a[14] + (long long)49 * (n - 14) << endl; n } n


#include <bits/stdc++.h> n int main() { n   int tab[2000], i, max = 0, posmax = 0, index = -1, n; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %d , &tab[i]); n     if (tab[i] > max) { n       max = tab[i]; n       posmax = i + 1; n     } n   } n   while (max-- && index == -1) n     for (i = 0; i < n; i++) n       if (tab[i] == max) { n         index = i; n         break; n       } n   printf( %d %d , posmax, tab[index]); n   return 0; n } n


#include <bits/stdc++.h> n char bar[1000002]; n int main() { n   scanf( %s , bar); n   int n = strlen(bar); n   long long int left = 0, mid = 0, right = 0; n   for (int i = 0; bar[i] !=  ^ ; i++) mid = i + 1; n   for (int i = 0; bar[i] !=  ^ ; i++) n     if (bar[i] !=  = ) left += (mid - i) * (bar[i] -  0 ); n   for (int i = n - 1; bar[i] !=  ^ ; i--) n     if (bar[i] !=  = ) right += (i - mid) * (bar[i] -  0 ); n   if (left < right) puts( right ); n   if (right < left) puts( left ); n   if (right == left) puts( balance ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<pair<int, char> > > adj(201); n int dp[101][101][26][2]; n bool solve(int i, int j, char round, bool turn) { n   if (dp[i][j][(int)(round -  a )][turn] != -1) n     return dp[i][j][(int)(round -  a )][turn]; n   int ret = 0; n   if (!turn) { n     for (int k = 0; k < adj[i].size(); k++) { n       if (adj[i][k].second >= round) n         if (!solve(adj[i][k].first, j, adj[i][k].second, !turn)) ret = 1; n     } n   } else { n     for (int k = 0; k < adj[j].size(); k++) { n       if (adj[j][k].second >= round) n         if (!solve(i, adj[j][k].first, adj[j][k].second, !turn)) ret = 1; n     } n   } n   return dp[i][j][(int)(round -  a )][turn] = ret; n } n int main() { n   memset(dp, -1, sizeof dp); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int x, y; n     char z; n     cin >> x >> y >> z; n     adj[x].push_back({y, z}); n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       int h = solve(i, j,  a , 0); n       if (h == 1) n         cout <<  A ; n       else n         cout <<  B ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100001], b[100001], part1[100001], part2[100001]; n int main() { n   int n, m, c; n   cin >> n >> m >> c; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= m; i++) cin >> b[i], part1[i] = part1[i - 1] + b[i]; n   for (int i = 1; i <= m; i++) part2[i] = part2[i - 1] + b[m - i + 1]; n   for (int i = 1; i <= n; i++) { n     if (i <= m) { n       a[i] = (a[i] + n               (i < n - m + 1 ? part1[i] : part1[i] - part1[i - (n - m + 1)])) % n              c; n     } else if (n - i + 1 <= m) { n       a[i] = (a[i] + part2[n - i + 1]) % c; n     } else n       a[i] = (a[i] + part1[m]) % c; n   } n   for (int i = 1; i <= n; i++) cout << a[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   unsigned long long dp[1000000]; n   unsigned long long ans = 0; n   for (long long i = 1; i <= 500000; i += 2) { n     long long val = (i * i - (i - 2) * (i - 2)) * (i / 2); n     ans = ans + val; n     dp[i] = ans; n   } n   while (t--) { n     long n; n     cin >> n; n     cout << dp[n] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> w[200000 + 20]; n int a[200000 + 20][3]; n vector<pair<int, pair<int, int> > > to_compress; n int n; n void maximize(int &a, int b) { n   if (a < b) a = b; n } n struct Fenwick_tree { n   int bit[1000000 + 50]; n   void update(int x, int v) { n     for (; x <= 1000000; x += x & (-x)) maximize(bit[x], v); n   } n   int get(int x) { n     int res = 0; n     for (; x > 0; x -= x & (-x)) maximize(res, bit[x]); n     return res; n   } n }; n Fenwick_tree LIS; n int ans = 0; n void compress() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d %d , &w[i].first, &w[i].second); n   sort(w + 1, w + 1 + n); n   for (int i = 1; i <= n; i++) { n     to_compress.push_back(pair<int, pair<int, int> >(w[i].first + w[i].second, n                                                      pair<int, int>(i, 1))); n     to_compress.push_back(pair<int, pair<int, int> >(w[i].first - w[i].second, n                                                      pair<int, int>(i, 2))); n   } n   sort(to_compress.begin(), to_compress.end()); n   int curr = 0; n   for (int i = 0; i < to_compress.size(); i++) { n     if (i == 0 || to_compress[i].first > to_compress[i - 1].first) curr++; n     a[to_compress[i].second.first][to_compress[i].second.second] = curr; n   } n } n void solve() { n   for (int i = 1; i <= n; i++) { n     int x = LIS.get(a[i][2]) + 1; n     maximize(ans, x); n     LIS.update(a[i][1], x); n   } n   printf( %d , ans); n } n int main() { n   compress(); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, a, b, q, d, ai, p, t, zb[200001], suma[800005], sumb[800005], n     za[200001], res; n void build1(int a[], int v, int tl, int tr) { n   if (tl == tr) n     suma[v] = a[tl]; n   else { n     int tm = (tl + tr) / 2; n     build1(a, v * 2, tl, tm); n     build1(a, v * 2 + 1, tm + 1, tr); n     suma[v] = suma[v * 2] + suma[v * 2 + 1]; n   } n } n void build2(int a[], int v, int tl, int tr) { n   if (tl == tr) n     sumb[v] = a[tl]; n   else { n     int tm = (tl + tr) / 2; n     build2(a, v * 2, tl, tm); n     build2(a, v * 2 + 1, tm + 1, tr); n     sumb[v] = sumb[v * 2] + sumb[v * 2 + 1]; n   } n } n int sum1(int v, int tl, int tr, int l, int r) { n   if (l > r) return 0; n   if (l == tl && r == tr) return suma[v]; n   int tm = (tl + tr) / 2; n   return sum1(v * 2, tl, tm, l, min(r, tm)) + n          sum1(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r); n } n int sum2(int v, int tl, int tr, int l, int r) { n   if (l > r) return 0; n   if (l == tl && r == tr) return sumb[v]; n   int tm = (tl + tr) / 2; n   return sum2(v * 2, tl, tm, l, min(r, tm)) + n          sum2(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r); n } n void update1(int v, int tl, int tr, int pos, long long new_val) { n   if (tl == tr) n     suma[v] = new_val; n   else { n     int tm = (tl + tr) / 2; n     if (pos <= tm) n       update1(v * 2, tl, tm, pos, new_val); n     else n       update1(v * 2 + 1, tm + 1, tr, pos, new_val); n     suma[v] = suma[v * 2] + suma[v * 2 + 1]; n   } n } n void update2(int v, int tl, int tr, int pos, long long new_val) { n   if (tl == tr) n     sumb[v] = new_val; n   else { n     int tm = (tl + tr) / 2; n     if (pos <= tm) n       update2(v * 2, tl, tm, pos, new_val); n     else n       update2(v * 2 + 1, tm + 1, tr, pos, new_val); n     sumb[v] = sumb[v * 2] + sumb[v * 2 + 1]; n   } n } n int main() { n   scanf( %d %d %d %d %d , &n, &k, &b, &a, &q); n   for (int i = 0; i < n; i++) zb[i] = b; n   for (int i = 0; i < n; i++) za[i] = a; n   build1(za, 1, 0, n - 1); n   build2(zb, 1, 0, n - 1); n   for (int i = 0; i < q; i++) { n     scanf( %d , &t); n     if (t == 1) { n       scanf( %d %d , &d, &ai); n       d--; n       za[d] -= ai; n       zb[d] -= ai; n       if (za[d] < 0) za[d] = 0; n       if (zb[d] < 0) zb[d] = 0; n       update1(1, 0, n - 1, d, za[d]); n       update2(1, 0, n - 1, d, zb[d]); n     } else { n       scanf( %d , &p); n       p--; n       int size1 = p; n       int size2 = n - k - p; n       res = 0; n       int resa = 0, resb = 0; n       resa -= sum1(1, 0, n - 1, 0, size1 - 1); n       resa += size1 * a; n       resb -= sum2(1, 0, n - 1, n - size2, n - 1); n       resb += size2 * b; n       printf( %d n , resa + resb); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long t; n long long n; n int x; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(), cout.tie(); n   cin >> t; n   while (t--) { n     cin >> n; n     set<int> s; n     int count = 0; n     for (int i = 0; i < n; i++) { n       cin >> x; n       if (s.find(x) == s.end()) { n         count++; n         s.insert(x); n       } n     } n     cout << count << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 100005; n long long n, k, fd, gcd, sum = 0; n int main() { n   cin >> n >> k; n   if (k > sqrt(2 * n)) { n     cout <<  -1 n ; n   } else { n     if ((k * (k + 1)) / 2 > n) { n       cout << -1; n     } else { n       fd = -1; n       for (long long i = (k * (k + 1)) / 2; i <= N; i++) { n         if (n % i == 0) { n           fd = i; n           break; n         } n       } n       if (fd == -1) { n         for (long long i = N; i >= 1; i--) { n           if (n % i == 0 && ((n / i) >= (k * (k + 1)) / 2)) { n             gcd = i; n             break; n           } n         } n       } else n         gcd = n / fd; n       for (long long i = 1; i < k; i++) { n         sum += gcd * i; n         cout << gcd * i <<    ; n       } n       cout << (n - sum); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n string rotate(string s) { n   for (int i = 1; i <= 3; i++) { n     swap(s[i - 1], s[i]); n   } n   return s; n } n string rotate2(string s) { n   swap(s[0], s[4]); n   swap(s[2], s[4]); n   swap(s[2], s[5]); n   return s; n } n string solve(string s) { n   string ans = s; n   for (int i = 0; i < 2; i++) { n     for (int j = 0; j < 4; j++) { n       for (int k = 0; k < 4; k++) { n         s = rotate(s); n         ans = min(ans, s); n       } n       s = rotate2(s); n     } n     s = rotate(s); n   } n   return ans; n } n int main() { n   string s; n   cin >> s; n   set<string> st; n   sort(s.begin(), s.end()); n   do { n     st.insert(solve(s)); n   } while (next_permutation(s.begin(), s.end())); n   cout << st.size() << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k; n void In() { cin >> n >> k; } n void Solve() { n   k--; n   while (k--) { n     int largest = INT_MIN, smallest = INT_MAX; n     for (long long copy = n; copy; copy /= 10) { n       int r = copy % 10; n       largest = max(r, largest); n       smallest = min(r, smallest); n     } n     if (largest == 0 || smallest == 0) return; n     n += largest * smallest; n   } n } n void Out() { cout << n <<   n ; } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t = 1; n   cin >> t; n   while (t--) { n     In(); n     Solve(); n     Out(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m1 = 0, m2 = 0, mn1 = INT_MAX, mn2 = INT_MAX, s1 = 0, s2 = 0; n   cin >> n; n   vector<pair<int, int> > vt; n   long long tmp1, tmp2; n   for (int i = 0; i < n; i++) { n     cin >> tmp1 >> tmp2; n     vt.push_back(make_pair(tmp1, tmp2)); n   } n   string ans =   ; n   tmp1 = 0, tmp2 = 0; n   for (int i = 0; i < n; i++) { n     if (tmp2 + vt[i].second <= tmp1 + 500) { n       tmp2 += vt[i].second; n       ans +=  G ; n     } else { n       tmp1 += vt[i].first; n       ans +=  A ; n     } n   } n   if (!(abs(tmp1 - tmp2) <= 500)) { n     cout << -1; n     return 0; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long func(long long x, long long y, long long z) { n   return (x - y) * (x - y) + (y - z) * (y - z) + (z - x) * (z - x); n } n void solve() { n   long long na, nb, nc; n   cin >> na >> nb >> nc; n   vector<long long> a(na), b(nb), c(nc); n   long long i, j, k; n   for (i = 0; i < na; i++) cin >> a[i]; n   for (j = 0; j < nb; j++) cin >> b[j]; n   for (k = 0; k < nc; k++) cin >> c[k]; n   sort(a.begin(), a.end()); n   sort(b.begin(), b.end()); n   sort(c.begin(), c.end()); n   i = j = k = 0; n   long long ans, d1, d2, d3; n   ans = LLONG_MAX; n   while (i < na - 1 || j < nb - 1 || k < nc - 1) { n     ans = min(ans, func(a[i], b[j], c[k])); n     d1 = d2 = d3 = LLONG_MAX; n     if (i < na - 1) d1 = func(a[i + 1], b[j], c[k]); n     if (j < nb - 1) d2 = func(a[i], b[j + 1], c[k]); n     if (k < nc - 1) d3 = func(a[i], b[j], c[k + 1]); n     if (d1 <= d2 && d1 <= d3) n       i++; n     else if (d2 <= d1 && d2 <= d3) n       j++; n     else n       k++; n   } n   ans = min(ans, func(a[i], b[j], c[k])); n   cout << ans << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void _R(T &x) { n   cin >> x; n } n void _R(int &x) { scanf( %d , &x); } n void _R(long long &x) { scanf( %lld , &x); } n void _R(double &x) { scanf( %lf , &x); } n void _R(char &x) { scanf(  %c , &x); } n void _R(char *x) { scanf( %s , x); } n void R() {} n template <class T, class... U> n void R(T &head, U &...tail) { n   _R(head); n   R(tail...); n } n template <class T> n void _W(const T &x) { n   cout << x; n } n void _W(const int &x) { printf( %d , x); } n void _W(const long long &x) { printf( %lld , x); } n void _W(const double &x) { printf( %.16f , x); } n void _W(const char &x) { putchar(x); } n void _W(const char *x) { printf( %s , x); } n template <class T, class U> n void _W(const pair<T, U> &x) { n   _W(x.first); n   putchar(   ); n   _W(x.second); n } n template <class T> n void _W(const vector<T> &x) { n   for (auto i = x.begin(); i != x.end(); _W(*i++)) n     if (i != x.cbegin()) putchar(   ); n } n void W() {} n template <class T, class... U> n void W(const T &head, const U &...tail) { n   _W(head); n   putchar(sizeof...(tail) ?     :   n ); n   W(tail...); n } n int MOD = 1e9 + 7; n void ADD(long long &x, long long v) { n   x = (x + v) % MOD; n   if (x < 0) x += MOD; n } n const int SIZE = 1e6 + 10; n const int AA = 1000; n int dp[2001]; n int main() { n   long long target_W; n   R(target_W); n   long long sum = 0; n   long long cnt[9], cnt2[9] = {}; n   for (int i = (1); i <= (8); ++i) { n     R(cnt[i]); n     sum += cnt[i] * i; n   } n   if (sum <= target_W) n     W(sum); n   else { n     long long now = 0; n     for (int i = (1); i <= (8); ++i) { n       if (now + i * cnt[i] <= target_W) { n         now += i * cnt[i]; n         cnt2[i] = cnt[i]; n         cnt[i] = 0; n       } else { n         long long v = (target_W - now) / i; n         now += i * v; n         cnt[i] -= v; n         cnt2[i] += v; n       } n     } n     dp[(0) + AA] = 1; n     for (int v = (1); v <= (8); ++v) { n       for (int j = 0; j < (min(100LL, cnt[v])); ++j) { n         for (int k = 900; k >= -900; k--) { n           if (dp[(k) + AA]) dp[(k + v) + AA] = 1; n         } n       } n       for (int j = 0; j < (min(100LL, cnt2[v])); ++j) { n         for (int k = -900; k <= 900; k++) { n           if (dp[(k) + AA]) dp[(k - v) + AA] = 1; n         } n       } n     } n     long long an = 0; n     for (int i = (0); i <= (target_W - now); ++i) { n       if (dp[(i) + AA]) an = now + i; n     } n     W(an); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int const mod = 1e9 + 7; n long long int power(long long int x, long long int y, long long int p) { n   long long int res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n long long int MIN(long long int a, long long int b) { n   if (a < b) return a; n   return b; n } n long long int MAX(long long int a, long long int b) { n   if (a > b) return a; n   return b; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   bool p[1000001]; n   memset(p, true, sizeof p); n   p[0] = p[1] = false; n   p[2] = true; n   for (int i = 2; i * i <= 1000001; i++) { n     if (p[i]) { n       for (int j = i * i; j <= 1000001; j += i) { n         p[j] = false; n       } n     } n   } n   vector<int> pp; n   for (int i = 2; i <= 1000001; i++) { n     if (p[i]) pp.push_back(i); n   } n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     long long int a[n + 1]; n     for (int i = 0; i < n; i++) cin >> a[i + 1]; n     int dp[n + 1], ans = 1; n     dp[1] = 1; n     for (int i = 2; i <= n; i++) { n       dp[i] = 1; n       for (int j = 1; j * j <= i; j++) { n         if (i % j == 0) { n           if (a[i] > a[j]) dp[i] = MAX(dp[i], dp[j] + 1); n           if (a[i] > a[i / j]) dp[i] = MAX(dp[i], dp[i / j] + 1); n         } n       } n       ans = MAX(dp[i], ans); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n int check_matrix(int** arr, int n, int m) { n   int itN = 0, itJ = n - 1; n   if (n % 2) return n; n   while (itN < itJ) { n     for (int i = 0; i < m; ++i) n       if (arr[itN][i] != arr[itJ][i]) return n; n     itN++; n     itJ--; n   } n   return check_matrix(arr, n / 2, m); n } n int main() { n   int n, m, **arr; n   std::cin >> n >> m; n   arr = new int*[n]; n   for (int i = 0; i < n; ++i) *(arr + i) = new int[m]; n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) std::cin >> arr[i][j]; n   std::cout << check_matrix(arr, n, m); n   for (int i = 0; i < n; ++i) delete[] * (arr + i); n   delete[] arr; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 201000; n struct str { n   long long nr; n   char c; n }; n int n, m; n str a[2 * N], b[N], l[N]; n vector<int> poz; n bool cmp(str a, str b) { return (a.nr == b.nr && a.c == b.c); } n int z[2 * N]; n void calcpot() { n   int i, j; n   for (i = 1; i <= n; ++i) l[i] = a[i]; n   int nr = 0; n   for (i = 2; i < n; ++i) { n     a[++nr] = l[i]; n   } n   for (i = 1; i <= m; ++i) a[++nr] = b[i]; n   int el, pmax = 0; n   for (i = 2; i <= nr; ++i) { n     if (i >= pmax) { n       for (j = i; cmp(a[j], a[j - i + 1]) && j <= nr; ++j) n         ; n       --j; n       z[i] = j - i + 1; n       pmax = j; n       el = i; n     } else { n       z[i] = min(z[i - el + 1], pmax - i + 1); n       for (j = i + z[i] - 1; cmp(a[j], a[j - i + 1]) && j <= nr; ++j) n         ; n       --j; n       z[i] = j - i + 1; n       if (j > pmax) { n         pmax = j; n         el = i; n       } n     } n     if (i > n - 2 && z[i] >= n - 2) { n       poz.push_back(i - (n - 2)); n     } n   } n } n void calc() { n   int i, j; n   calcpot(); n   int r = 0; n   for (i = 0; i < poz.size(); ++i) { n     int e = poz[i]; n     if (e > 1 && e + n - 2 <= m) { n       if (l[1].c == b[e - 1].c && l[1].nr <= b[e - 1].nr) n         if (l[n].c == b[e + n - 2].c && l[n].nr <= b[e + n - 2].nr) ++r; n     } n   } n   cout << r; n } n int main() { n   int i, j; n   cin >> m >> n; n   for (i = 1; i <= m; ++i) { n     scanf( %I64d-%c  , &b[i].nr, &b[i].c); n     if (i > 1 && b[i].c == b[i - 1].c) { n       b[i - 1].nr += b[i].nr; n       --m; n       --i; n     } n   } n   for (i = 1; i <= n; ++i) { n     scanf( %I64d-%c  , &a[i].nr, &a[i].c); n     if (i > 1 && a[i].c == a[i - 1].c) { n       a[i - 1].nr += a[i].nr; n       --n; n       --i; n     } n   } n   if (n == 1) { n     long long r = 0; n     for (i = 1; i <= m; ++i) { n       if (a[1].c == b[i].c) r += max(0LL, b[i].nr - a[1].nr + 1); n     } n     cout << r; n     return 0; n   } n   if (n == 2) { n     if (m == 1) { n       cout << 0; n       return 0; n     } n     long long r = 0; n     for (i = 1; i < m; ++i) { n       if (a[1].c == b[i].c && a[2].c == b[i + 1].c && a[1].nr <= b[i].nr && n           a[2].nr <= b[i + 1].nr) n         ++r; n     } n     cout << r; n     return 0; n   } n   if (m < n) { n     cout << 0; n     return 0; n   } n   calc(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v; n void dfs(int depth, bool state) { n   if (depth == 0) return; n   dfs(depth - 1, !state); n   v.push_back(state); n   dfs(depth - 1, state); n } n int main() { n   int N; n   cin >> N; n   int D = 1; n   while (1 << D <= N) D++; n   dfs(D - 1, 1); n   int n = v.size(); n   for (int i = 0; i < (int)v.size() - 1; i++) n += (v[i] == v[i + 1]); n   cout << (n == N or n + 1 == N) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long double pi = acos(-1); n const long long inf = 1LL << 30; n const double eps = 1e-9; n const long long mod = 1e9 + 7; n const int N = 100100; n pair<int, pair<int, int>> a[N]; n int seg[N << 2], lazy[N << 2]; n ; n int ans[N]; n int dp[N]; n void push(int nd, int L, int R) { n   if (lazy[nd] == 0) return; n   seg[nd] = 0; n   if (L != R) { n     lazy[nd << 1] = 1; n     lazy[nd << 1 | 1] = 1; n   } n   lazy[nd] = 0; n } n void update(int nd, int L, int R, int idx, int val) { n   if (L == R) { n     seg[nd] = val; n     return; n   } n   int mid = (L + R) >> 1; n   if (idx <= mid) n     update(nd << 1, L, mid, idx, val); n   else n     update(nd << 1 | 1, mid + 1, R, idx, val); n   seg[nd] = seg[nd << 1] + seg[nd << 1 | 1]; n } n long long query(int nd, int L, int R, int from, int to) { n   push(nd, L, R); n   if (from <= L && R <= to) return seg[nd]; n   if (from > R || to < L) return 0; n   int mid = (L + R) >> 1; n   long long leftAns = query(nd << 1, L, mid, from, to); n   long long rightAns = query(nd << 1 | 1, mid + 1, R, from, to); n   return leftAns + rightAns; n } n void updateRange(int nd, int L, int R, int from, int to) { n   push(nd, L, R); n   if (R < from || L > to) return; n   if (from <= L && R <= to) { n     lazy[nd] = 1; n     push(nd, L, R); n     return; n   } n   int mid = (L + R) >> 1; n   updateRange(nd << 1, L, mid, from, to); n   updateRange(nd << 1 | 1, mid + 1, R, from, to); n   seg[nd] = seg[nd << 1] + seg[nd << 1 | 1]; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d%d , &a[i].first, &a[i].second.first); n     a[i].second.second = i; n   } n   sort(a, a + n); n   for (int i = n - 1; i >= 0; i--) { n     int j = lower_bound( n                 a, a + n, n                 make_pair(a[i].first + a[i].second.first, make_pair(0, 0))) - n             a - 1; n     dp[i] = 1 + query(1, 1, n, i, j); n     updateRange(1, 1, n, i + 1, j); n     update(1, 1, n, i, dp[i]); n     ans[a[i].second.second] = dp[i]; n   } n   for (int i = 0; i < n; i++) printf( %d  , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 2005; n int n, h, l, r, a[maxN]; n int dp[maxN][maxN]; n inline int getint() { n   int num = 0, bj = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c == EOF) return EOF; n     bj = (c ==  -  || bj == -1) ? -1 : 1, c = getchar(); n   } n   while (c >=  0  && c <=  9 ) num = num * 10 + c -  0 , c = getchar(); n   return num * bj; n } n void init() { n   cin >> n >> h >> l >> r; n   for (int i = 1; i <= n; ++i) { n     a[i] = getint(); n   } n } n int cor(int x) { return (x % h + h) % h; } n void solve() { n   memset(dp, -1, sizeof(dp)); n   int ans = -1; n   (a[1] >= l && a[1] <= r)                     ? dp[1][a[1]] = 1, n                              ans = max(ans, 1) : dp[1][a[1]] = 0, n                              ans = max(ans, 0); n   (a[1] - 1 >= l && a[1] - 1 <= r) n       ? dp[1][a[1] - 1] = 1, n                      ans = max(ans, 1) : dp[1][a[1] - 1] = 0, ans = max(ans, 0); n   for (int i = 2; i <= n; ++i) { n     for (int j = 0; j < h; ++j) { n       if (dp[i - 1][cor(j - a[i])] == -1 && dp[i - 1][cor(j - a[i] + 1)] == -1) n         continue; n       dp[i][j] = max(dp[i - 1][cor(j - a[i])], dp[i - 1][cor(j - a[i] + 1)]); n       if (j >= l && j <= r) dp[i][j]++; n       ans = max(ans, dp[i][j]); n     } n   } n   cout << ans << endl; n } n int main() { n   init(); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> cnt(string s) { n   int bal = 0, minBal = 0; n   for (char c : s) { n     if (c ==  ( ) n       bal++; n     else n       bal--; n     minBal = min(minBal, bal); n   } n   return {bal, minBal}; n } n int k[400400]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   vector<int> bal(n), minBal(n); n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     auto p = cnt(s); n     bal[i] = p.first; n     minBal[i] = p.second; n     if (bal[i] >= 0 && minBal[i] >= 0) k[bal[i]]++; n   } n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     if (bal[i] > 0) continue; n     if (minBal[i] - bal[i] >= 0) ans += (long long)k[-bal[i]]; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n const int INF = 0x3f3f3f3f; n const int mo = 1e9 + 7; n inline int ksm(int a, int b) { n   int ret = 1; n   for (; b; b >>= 1, a = 1ll * a * a % mo) n     if (b & 1) ret = 1ll * ret * a % mo; n   return ret; n } n inline int read() { n   char ch = getchar(); n   int x = 0, f = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 3) + (x << 1) + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n int n = 4, m, f[N][21], dep[N]; n struct node { n   int u, v, mx; n   bool operator<(const node &A) const { return mx < A.mx; } n } a; n int LCA(int x, int y) { n   if (dep[x] < dep[y]) swap(x, y); n   for (int i = 20; i >= 0; i--) n     if (dep[f[x][i]] >= dep[y]) x = f[x][i]; n   if (x == y) return x; n   for (int i = 20; i >= 0; i--) n     if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i]; n   return f[x][0]; n } n int dist(int x, int y) { n   int lca = LCA(x, y); n   return dep[x] + dep[y] - 2 * dep[lca]; n } n void add(int u) { n   int x = n + 1, y = n + 2; n   n += 2; n   dep[x] = dep[y] = dep[u] + 1; n   f[x][0] = f[y][0] = u; n   for (int i = 1; i <= 20; i++) f[x][i] = f[y][i] = f[f[x][i - 1]][i - 1]; n   int xx = a.u, yy = a.v; n   node b = (node){xx, x, dist(xx, x)}, c = (node){xx, y, dist(xx, y)}; n   node d = (node){yy, x, dist(yy, x)}, e = (node){yy, y, dist(yy, y)}; n   node f = (node){xx, yy, dist(xx, yy)}; n   if (a < b) a = b; n   if (a < c) a = c; n   if (a < d) a = d; n   if (a < e) a = e; n   if (a < f) a = f; n } n signed main() { n   dep[1] = 1; n   a.u = 2, a.v = 4, a.mx = 2; n   dep[2] = dep[3] = dep[4] = 2; n   f[1][0] = f[2][0] = f[3][0] = f[4][0] = 1; n   m = read(); n   for (; m; m--) { n     int u = read(); n     add(u); n     printf( %d n , a.mx); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> a[200001]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     a[x].push_back(y); n   } n   set<pair<int, int> > S; n   for (int i = 1; i <= n; i++) { n     sort(a[i].begin(), a[i].end()); n     if (!a[i].empty()) { n       S.insert(make_pair(a[i][a[i].size() - 1], i)); n     } n   } n   int q; n   cin >> q; n   while (q--) { n     int k; n     scanf( %d , &k); n     vector<pair<int, int> > temp; n     for (int i = 1; i <= k; i++) { n       int x; n       scanf( %d , &x); n       if (a[x].size()) { n         S.erase(make_pair(a[x][a[x].size() - 1], x)); n         temp.push_back(make_pair(a[x][a[x].size() - 1], x)); n       } n     } n     if (S.size() == 0) { n       cout <<  0 0 n ; n     } else if (S.size() == 1) { n       set<pair<int, int> >::iterator it; n       it = S.begin(); n       int x = it->second, y = it->first; n       printf( %d %d n , x, a[x][0]); n     } else { n       set<pair<int, int> >::reverse_iterator it; n       it = S.rbegin(); n       int u = it->second; n       it++; n       int x = it->second, y = it->first; n       int ans = *upper_bound(a[u].begin(), a[u].end(), y); n       printf( %d %d n , u, ans); n     } n     for (int i = 0; i < temp.size(); i++) { n       S.insert(temp[i]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, int> c; n int main() { n   int l1, l2, f, ans = 0; n   string s1, s2; n   cin >> s1 >> s2; n   l1 = s1.length(); n   l2 = s2.length(); n   for (int i = 1; i <= min(l1, l2); i++) n     if (l1 % i == 0 && l2 % i == 0) { n       f = 0; n       c.clear(); n       for (int j = 0; j < l1; j += i) c[s1.substr(j, i)]++; n       if (c[s1.substr(0, i)] == l1 / i) f++; n       c.clear(); n       for (int j = 0; j < l2; j += i) c[s2.substr(j, i)]++; n       if (c[s1.substr(0, i)] == l2 / i) f++; n       if (f == 2) ans++; n     } n   cout << ans <<   n ; n   return 0; n } n