#include <bits/stdc++.h> n using namespace std; n int main(int argc, char* argv[]) { n   char str[105]; n   cin.getline(str, 104); n   for (unsigned i = 1; i < strlen(str) + 1; i++) { n     if ((str[i] == str[i - 1]) && (str[i - 1] ==  / )) { n       for (unsigned j = i; j < strlen(str); j++) { n         str[j] = str[j + 1]; n       } n       i--; n     } n   } n   if ((str[strlen(str) - 1] ==  / ) && (strlen(str) >= 2)) { n     str[strlen(str) - 1] = 0; n   } n   cout << str << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-6; n void __print(int x) { cerr << x; } n void __print(long x) { cerr << x; } n void __print(long long x) { cerr << x; } n void __print(unsigned x) { cerr << x; } n void __print(unsigned long x) { cerr << x; } n void __print(unsigned long long x) { cerr << x; } n void __print(float x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(char x) { cerr <<      << x <<     ; } n void __print(const char *x) { cerr <<     << x <<    ; } n void __print(const string &x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  , ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &x) { n   int f = 0; n   cerr <<  { ; n   for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n const long long N = 2010; n long long n; n long long k; n string s[N]; n string ans; n long long dp[N][N]; n long long path[N][N]; n void solve() { n   cin >> n; n   cin >> k; n   for (long long i = 0; i < n; ++i) cin >> s[i]; n   for (long long i = 0; i < n; ++i) { n     for (long long j = 0; j < n; ++j) { n       if (i == 0 && j == 0) { n         dp[i][j] += (s[i][j] !=  a ); n         continue; n       } n       if (i == 0) { n         dp[i][j] = dp[i][j - 1]; n         dp[i][j] += (s[i][j] !=  a ); n         continue; n       } n       if (j == 0) { n         dp[i][j] = dp[i - 1][j]; n         dp[i][j] += (s[i][j] !=  a ); n         continue; n       } n       dp[i][j] += min(dp[i - 1][j], dp[i][j - 1]) + (s[i][j] !=  a ); n     } n   } n   if (k >= 2 * n - 1) { n     ans = string(2 * n - 1,  a ); n     cout << ans <<   n ; n     return; n   } n   set<pair<long long, long long>> pool; n   long long len = -1; n   for (long long i = 0; i < n; ++i) { n     for (long long j = 0; j < n; ++j) { n       if (dp[i][j] <= k) { n         len = max(len, i + j); n       } n     } n   } n   if (len == -1) { n     ans.push_back(s[0][0]); n     len = 0; n     if (1 < n) { n       pool.insert({0, 1}); n       pool.insert({1, 0}); n     } n   } else { n     ans = string(len + 1,  a ); n     for (long long i = 0; i < n; ++i) { n       for (long long j = 0; j < n; ++j) { n         if (i + j == len && dp[i][j] <= k) { n           if (i + 1 < n) pool.insert({i + 1, j}); n           if (j + 1 < n) pool.insert({i, j + 1}); n         } n       } n     } n   } n   long long req = 2 * n - 1 - len - 1; n   ; n   while (req > 0) { n     char c =  z ; n     for (auto it : pool) { n       c = min(c, s[it.first][it.second]); n     } n     vector<pair<long long, long long>> toerase; n     for (auto it : pool) { n       if (c != s[it.first][it.second]) toerase.push_back(it); n     } n     for (auto it : toerase) pool.erase(it); n     vector<pair<long long, long long>> topush; n     for (auto [i, j] : pool) { n       if (i + 1 < n) topush.push_back({i + 1, j}); n       if (j + 1 < n) topush.push_back({i, j + 1}); n     } n     pool.clear(); n     for (auto it : topush) pool.insert(it); n     req--; n     ans.push_back(c); n   } n   cout << ans <<   n ; n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (int)1e9 + 7, mod = (int)1e9 + 9, pw = 31, N = (int)1e5 + 123, n           M = (int)1e6 + 123; n const double eps = 1e-11; n const unsigned long long inf = 5e18 + 1; n unsigned long long n, a, b, x, m, ans; n bool check = 0; n unsigned long long binpow(unsigned long long a, unsigned long long b) { n   unsigned long long rec = 0; n   while (b) { n     if (b % 2) { n       rec = rec + a; n       b--; n     } else { n       a = a + a; n       b /= 2; n     } n     if (rec > n || a > n) { n       check = 1; n       return 0; n     } n   } n   return rec; n } n long long gcd(long long x, long long y) { return (y ? gcd(y, x % y) : x); } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cin >> n >> a >> b; n   ans = min(n, min(a, b) - 1); n   m = binpow(a, b / gcd(a, b)); n   if (check) { n     cout << ans / gcd(ans, n) <<  /  << n / gcd(ans, n); n     return 0; n   } n   ans += ((n / m) - 1) * min(a, b); n   ans += min((n / m) * m + min(a, b) - 1, n) - (n / m) * m + 1; n   cout << ans / gcd(ans, n) <<  /  << n / gcd(ans, n); n   return 0; n } n


#include <bits/stdc++.h> n const int N = 7e3 + 5; n int n, k[2], a[2][N], cnt[2][N]; n bool vis[2][N], win[2][N]; n void dfs(int v, int now) { n   if (vis[v][now]) return; n   vis[v][now] = 1; n   int u = v ^ 1; n   for (int i = 1; i <= k[u]; ++i) { n     int pre = (now - a[u][i] + n - 1) % n + 1; n     if (pre == 1) continue; n     if (!win[v][now]) { n       win[u][pre] = 1; n       dfs(u, pre); n     } else if (++cnt[u][pre] == k[u]) { n       win[u][pre] = 0; n       dfs(u, pre); n     } n   } n } n int main() { n   scanf( %d , &n); n   for (int o = 0; o <= 1; ++o) { n     scanf( %d , &k[o]); n     for (int i = 1; i <= k[o]; ++i) scanf( %d , &a[o][i]); n   } n   dfs(0, 1), dfs(1, 1); n   for (int o = 0; o <= 1; ++o) { n     for (int i = 2; i <= n; ++i) { n       if (!vis[o][i]) n         printf( %s%c ,  Loop ,    n [i == n]); n       else n         printf( %s%c , win[o][i] ?  Win  :  Lose ,    n [i == n]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Z = (int)2e5 + 111; n const int inf = (int)1e9 + 111; n const long long llinf = (long long)1e18 + 5; n const int MOD = (int)1e9 + 7; n vector<long long> pref[4 * Z]; n vector<int> t[4 * Z]; n int a[Z]; n void build(int v, int vl, int vr) { n   if (vl == vr) { n     t[v].push_back(a[vl]); n     pref[v].push_back(a[vl]); n     return; n   } n   int vm = (vl + vr) / 2; n   build(2 * v, vl, vm); n   build(2 * v + 1, vm + 1, vr); n   merge((t[2 * v]).begin(), (t[2 * v]).end(), (t[2 * v + 1]).begin(), n         (t[2 * v + 1]).end(), back_inserter(t[v])); n   pref[v].resize((int)t[v].size()); n   pref[v][0] = t[v][0]; n   for (int i = 1; i < (int)t[v].size(); ++i) { n     pref[v][i] = pref[v][i - 1] + t[v][i]; n   } n } n pair<long long, long long> query(int v, int vl, int vr, int l, int r, int val1, n                                  int val2) { n   if (vl > r || vr < l) return make_pair(0, 0); n   if (l <= vl && r >= vr) { n     long long cnt = upper_bound((t[v]).begin(), (t[v]).end(), val2) - n                     lower_bound((t[v]).begin(), (t[v]).end(), val1); n     int posL = lower_bound((t[v]).begin(), (t[v]).end(), val1) - t[v].begin(); n     int posR = upper_bound((t[v]).begin(), (t[v]).end(), val2) - t[v].begin(); n     posR--; n     long long sum; n     if (posR == -1) n       sum = 0; n     else n       sum = pref[v][posR] - (posL == 0 ? 0 : pref[v][posL - 1]); n     return make_pair(sum, cnt); n   } n   int vm = (vl + vr) / 2; n   pair<long long, long long> ql = query(2 * v, vl, vm, l, r, val1, val2); n   pair<long long, long long> qr = n       query(2 * v + 1, vm + 1, vr, l, r, val1, val2); n   return make_pair(ql.first + qr.first, ql.second + qr.second); n } n int main() { n   srand(time(0)); n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n   } n   build(1, 1, n); n   long double ans = 0; n   for (int i = 1; i <= n; ++i) { n     pair<long long, long long> l; n     if (a[i] - 2 >= 1) n       l = query(1, 1, n, i + 1, n, 1, a[i] - 2); n     else n       l = make_pair(0, 0); n     pair<long long, long long> r = query(1, 1, n, i + 1, n, a[i] + 2, inf); n     ans += l.first + r.first - (l.second + r.second) * a[i]; n   } n   cout << fixed << setprecision(0) << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxN = 1e7 + 5; n const long long inf = 1e10; n const long long mod = 1e9 + 7; n long long n; n long long a[maxN]; n double f[maxN]; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   for (long long i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   long long cnt = 0; n   for (long long i = 1; i <= n; i++) { n     for (long long j = i + 1; j <= n; j++) n       if (a[i] > a[j]) cnt++; n   } n   f[0] = 0.0; n   f[1] = 1.0; n   for (long long i = 2; i <= cnt + 2; i++) { n     f[i] = f[i - 2] + 4.0; n   } n   printf( %.10f , f[cnt]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 200000; n int a[M]; n vector<int> all[M]; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   long long init = 0; n   for (int i = 1; i <= m; i++) scanf( %d , &a[i]); n   for (int i = 2; i <= m; i++) init += abs(a[i] - a[i - 1]); n   for (int i = 1; i <= m; i++) { n     if (i != 1 && a[i] != a[i - 1]) all[a[i]].push_back(a[i - 1]); n     if (i != m && a[i] != a[i + 1]) all[a[i]].push_back(a[i + 1]); n   } n   long long ans = init; n   for (int i = 1; i <= n; i++) { n     int t = all[i].size(); n     if (t == 0) continue; n     long long cur = init; n     sort(all[i].begin(), all[i].end()); n     int be = all[i][t >> 1]; n     for (int j = 0; j < t; j++) n       cur += -abs(i - all[i][j]) + abs(be - all[i][j]); n     ans = min(ans, cur); n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   vector<int> ans; n   int power = 1; n   while (n > 0) { n     if (n % 10 > 0) { n       ans.push_back((n % 10) * power); n     } n     n /= 10; n     power *= 10; n   } n   cout << ans.size() << endl; n   for (auto number : ans) cout << number <<    ; n   cout << endl; n } n int main() { n   int t; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int n, q, a[N], s1[N], t1, s2[N], t2; n vector<int> ans1[N], ans2[N]; n bool tag[N]; n inline void down(int &x, const int y) { x > y ? x = y : 0; } n int main() { n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n   set<int> cand; n   int mp[2] = {n, n}; n   for (int i = n; i; --i) { n     if (i < n && a[i] != a[i + 1]) { n       bool t = a[i] > a[i + 1]; n       if (mp[t ^ 1] < n) ans1[i] = {i, mp[t ^ 1], mp[t ^ 1] + 1}; n       mp[t] = i; n     } n     while (t1 && a[s1[t1]] > a[i]) { n       if (tag[s1[t1]]) cand.emplace(s1[t1]); n       tag[s1[t1--]] = true; n     } n     while (t2 && a[s2[t2]] < a[i]) { n       if (tag[s2[t2]]) cand.emplace(s2[t2]); n       tag[s2[t2--]] = true; n     } n     int _t1 = t1, _t2 = t2, l, r; n     for (l = 1, r = t1, t1 = 0; l <= r;) { n       int mid = l + r >> 1; n       if (a[s1[mid]] != a[i]) n         t1 = mid, l = mid + 1; n       else n         r = mid - 1; n     } n     for (l = 1, r = t2, t2 = 0; l <= r;) { n       int mid = l + r >> 1; n       if (a[s2[mid]] != a[i]) n         t2 = mid, l = mid + 1; n       else n         r = mid - 1; n     } n     if (t1 && t2) { n       int pos = max(s1[t1], s2[t2]); n       auto nxt = cand.upper_bound(pos); n       if (nxt != cand.end()) { n         int p1, p2, l, r, rr = *nxt; n         for (l = 1, r = t1; l <= r;) { n           int mid = l + r >> 1; n           if (s1[mid] < rr) n             p1 = s1[mid], r = mid - 1; n           else n             l = mid + 1; n         } n         for (l = 1, r = t2; l <= r;) { n           int mid = l + r >> 1; n           if (s2[mid] < rr) n             p2 = s2[mid], r = mid - 1; n           else n             l = mid + 1; n         } n         if (p1 > p2) swap(p1, p2); n         ans2[i] = {i, p1, p2, rr}; n       } n     } n     s1[t1 = _t1 + 1] = i, s2[t2 = _t2 + 1] = i; n   } n   for (int i = n; i; --i) { n     if (!ans1[i + 1].empty() && n         (ans1[i].empty() || ans1[i + 1].back() < ans1[i].back())) n       ans1[i] = ans1[i + 1]; n     if (!ans2[i + 1].empty() && n         (ans2[i].empty() || ans2[i + 1].back() < ans2[i].back())) n       ans2[i] = ans2[i + 1]; n   } n   for (int t = 0, l, r; t < q; ++t) { n     scanf( %d%d , &l, &r); n     if (!ans2[l].empty() && ans2[l].back() <= r) { n       puts( 4 ); n       for (auto x : ans2[l]) printf( %d  , x); n       puts(  ); n     } else if (!ans1[l].empty() && ans1[l].back() <= r) { n       puts( 3 ); n       for (auto x : ans1[l]) printf( %d  , x); n       puts(  ); n     } else n       puts( 0 ), puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n int mod, n, a, b, good; n std::set<std::pair<int, int> > set; n std::pair<int, int> make_pair(int i, int j) { n   return {std::min(i, j), std::max(i, j)}; n } n int main() { n   scanf( %d%d , &mod, &n); n   for (int i = 0; i < n; i++) { n     scanf( %d%d , &a, &b); n     set.insert(make_pair(a - 1, b - 1)); n   } n   for (int k = 1; k < mod && !good; k++) { n     if (mod % k == 0) { n       good = true; n       for (auto p = set.begin(); good && p != set.end(); p++) n         if (!set.count(make_pair((p->first + k) % mod, (p->second + k) % mod))) n           good = false; n     } n   } n   printf( %s n , good ?  yes  :  no ); n   return 0; n } n


#include <bits/stdc++.h> n inline int read(void) { n   int x = 0, f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) f = c ==  -  ? -1 : 1, c = getchar(); n   while (c >=  0  && c <=  9 ) n     x = (x << 3) + (x << 1) + (c & 15), c = getchar(); n   return x * f; n } n inline int getm(long long x, int mod) { return (x % mod + mod) % mod; } n const int N = 300005; n const int mod1 = 998244353, mod2 = 1000000007; n const int inv1 = 729486258, inv2 = 576923081; n int n, m; n int pw[N][2], inv[N][2]; n char s[N]; n struct Edge { n   int to, nxt; n } E[N << 1]; n int head[N], tot; n inline void add(int f, int t) { n   E[++tot] = (Edge){t, head[f]}, head[f] = tot; n   return; n } n int hh1[N][2], hh2[N][2]; n int lg[N << 1], dep[N], fa[N][20], len[N], son[N], dfn[N], tt, st[N << 1][21]; n int top[N], *up[N], *down[N]; n void init(void) { n   pw[0][0] = pw[0][1] = inv[0][0] = inv[0][1] = 1; n   for (int i = 1; i <= n; ++i) n     pw[i][0] = 26ll * pw[i - 1][0] % mod1, n     pw[i][1] = 26ll * pw[i - 1][1] % mod2, n     inv[i][0] = 1ll * inv1 * inv[i - 1][0] % mod1, n     inv[i][1] = 1ll * inv2 * inv[i - 1][1] % mod2; n   for (int i = 1; i <= n << 1; ++i) lg[i] = lg[i >> 1] + 1; n   return; n } n void dfs1(int x, int from) { n   dep[x] = dep[from] + 1, fa[x][0] = from, st[dfn[x] = ++tt][0] = x; n   hh1[x][0] = (26ll * hh1[fa[x][0]][0] + (s[x] -  a )) % mod1; n   hh1[x][1] = n       (hh1[fa[x][0]][1] + 1ll * pw[dep[x] - 1][0] * (s[x] -  a )) % mod1; n   hh2[x][0] = (26ll * hh2[fa[x][0]][0] + (s[x] -  a )) % mod2; n   hh2[x][1] = n       (hh2[fa[x][0]][1] + 1ll * pw[dep[x] - 1][1] * (s[x] -  a )) % mod2; n   for (int i = 1; i < lg[dep[x]]; ++i) fa[x][i] = fa[fa[x][i - 1]][i - 1]; n   for (int i = head[x]; i; i = E[i].nxt) { n     int y = E[i].to; n     if (y == from) continue; n     dfs1(y, x); n     st[++tt][0] = x, son[x] = len[son[x]] > len[y] ? son[x] : y; n   } n   len[x] = len[son[x]] + 1; n   return; n } n void dfs2(int x, int tp) { n   top[x] = tp; n   if (tp == x) { n     up[x] = new int[len[x] + 5], down[x] = new int[len[x] + 5]; n     for (int y = x, l = 0; y != 0 && l <= len[x]; y = fa[y][0], ++l) n       up[x][l] = y; n   } n   down[tp][dep[x] - dep[tp]] = x; n   if (son[x]) dfs2(son[x], tp); n   for (int i = head[x]; i; i = E[i].nxt) { n     int y = E[i].to; n     if (y == fa[x][0] || y == son[x]) continue; n     dfs2(y, y); n   } n   return; n } n inline void init_st(void) { n   for (int k = 1; k < lg[tt]; ++k) n     for (int i = 1; i + (1 << k) - 1 <= tt; ++i) n       st[i][k] = dep[st[i][k - 1]] < dep[st[i + (1 << (k - 1))][k - 1]] n                      ? st[i][k - 1] n                      : st[i + (1 << (k - 1))][k - 1]; n   return; n } n inline int lca(int l, int r) { n   l = dfn[l], r = dfn[r]; n   if (l > r) std::swap(l, r); n   int k = lg[r - l + 1] - 1; n   return dep[st[l][k]] < dep[st[r - (1 << k) + 1][k]] ? st[l][k] n                                                       : st[r - (1 << k) + 1][k]; n } n inline int kth(int x, int k) { n   if (k == 0) n     return x; n   else if (k >= dep[x]) n     return 0; n   x = fa[x][lg[k] - 1], k ^= 1 << (lg[k] - 1); n   if (dep[top[x]] <= dep[x] - k) n     return down[top[x]][dep[x] - k - dep[top[x]]]; n   else n     return up[top[x]][dep[top[x]] - dep[x] + k]; n } n inline std::pair<int, int> hashh(int a, int b, int k, int f, int l) { n   int hash1 = 0, hash2 = 0; n   int t = kth(a, k), tt = kth(b, l - k); n   if (k <= dep[a] - dep[f] + 1) n     hash1 = getm(hh1[a][0] - 1ll * pw[k][0] * hh1[t][0], mod1), n     hash2 = getm(hh2[a][0] - 1ll * pw[k][1] * hh2[t][0], mod2); n   else n     hash1 = n         (getm(hh1[a][0] - 1ll * pw[dep[a] - dep[f] + 1][0] * hh1[fa[f][0]][0], n               mod1) + n          1ll * inv[dep[f]][0] * pw[dep[a] - dep[f] + 1][0] % mod1 * n              getm(hh1[tt][1] - hh1[f][1], mod1) % mod1) % n         mod1, n     hash2 = n         (getm(hh2[a][0] - 1ll * pw[dep[a] - dep[f] + 1][1] * hh2[fa[f][0]][0], n               mod2) + n          1ll * inv[dep[f]][1] * pw[dep[a] - dep[f] + 1][1] % mod2 * n              getm(hh2[tt][1] - hh2[f][1], mod2) % mod2) % n         mod2; n   return std::make_pair(hash1, hash2); n } n inline int solve(int a, int b, int c, int d) { n   int f1 = lca(a, b), f2 = lca(c, d), l1 = dep[a] + dep[b] - dep[f1] * 2 + 1, n       l2 = dep[c] + dep[d] - dep[f2] * 2 + 1; n   int l = 1, r = std::min(l1, l2), res = 0; n   while (l <= r) { n     int mid = (l + r) >> 1; n     if (hashh(a, b, mid, f1, l1) == hashh(c, d, mid, f2, l2)) n       res = mid, l = mid + 1; n     else n       r = mid - 1; n   } n   return res; n } n int main(void) { n   n = read(); n   scanf( %s , s + 1); n   for (int i = 1, u, v; i < n; ++i) n     u = read(), v = read(), add(u, v), add(v, u); n   init(); n   dfs1(1, 0), dfs2(1, 1); n   init_st(); n   m = read(); n   for (int i = 1, a, b, c, d; i <= m; ++i) n     a = read(), b = read(), c = read(), d = read(), n     printf( %d n , solve(a, b, c, d)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, x, y, z, d[200 + 5], nows[200 + 5], fr[200 + 5], en[200 + 5], n     A[200 + 5], S, T, fa[200 + 5], now, Bh, Ch, B[200 + 5], C[200 + 5], H, un, n     wn, ToT; n struct yyy { n   int to, w, z; n } tmp; n struct ljb { n   int head, h[200 + 5]; n   yyy f[1000 + 5 << 2]; n   inline void add(int x, int y, int z) { n     f[head] = (yyy){y, z, h[x]}; n     h[x] = head++; n   } n } s, G; n queue<int> Q; n struct ques { n   int x, y, z; n } F[200 + 5]; n inline bool cmp(ques x, ques y) { return x.z > y.z; } n inline void Get(int x, int y, int z) { n   s.add(x, y, z); n   s.add(y, x, z); n } n inline int bfs() { n   while (!Q.empty()) Q.pop(); n   memset(d, 0x3f, sizeof(d)); n   d[S] = 0; n   Q.push(S); n   nows[S] = s.h[S]; n   while (!Q.empty()) { n     now = Q.front(); n     Q.pop(); n     for (int i = s.h[now]; ~i; i = tmp.z) { n       tmp = s.f[i]; n       if (!tmp.w || d[tmp.to] < 1e9) continue; n       d[tmp.to] = d[now] + 1; n       Q.push(tmp.to); n       nows[tmp.to] = s.h[tmp.to]; n       if (tmp.to == T) return 1; n     } n   } n   return 0; n } n inline int dfs(int x, int sum) { n   if (x == T) return sum; n   int i, k, pus = 0; n   yyy tmp; n   for (i = nows[x]; ~i; i = tmp.z) { n     tmp = s.f[i]; n     nows[x] = i; n     if (!tmp.w || d[tmp.to] != d[x] + 1) continue; n     k = dfs(tmp.to, ((sum) < (tmp.w) ? (sum) : (tmp.w))); n     if (!k) d[tmp.to] = 1e9; n     pus += k; n     sum -= k; n     s.f[i].w -= k; n     s.f[i ^ 1].w += k; n     if (!sum) break; n   } n   return pus; n } n inline void build(int l, int r) { n   if (l == r) return; n   int Ans = 0, i; n   S = A[l]; n   T = A[r]; n   s = G; n   while (bfs()) Ans += dfs(S, 1e9); n   F[++H] = (ques){S, T, Ans}; n   ToT += Ans; n   Bh = Ch = 0; n   for (i = l; i <= r; i++) (d[A[i]] < 1e9 ? B[++Bh] : C[++Ch]) = A[i]; n   for (i = l; i <= l + Bh - 1; i++) A[i] = B[i - l + 1]; n   for (i = l + Bh; i <= r; i++) A[i] = C[i - l - Bh + 1]; n   Ans = Bh; n   build(l, l + Ans - 1); n   build(l + Ans, r); n } n inline int Getfa(int x) { return x == fa[x] ? x : fa[x] = Getfa(fa[x]); } n int main() { n   register int i; n   scanf( %d%d , &n, &m); n   memset(s.h, -1, sizeof(s.h)); n   for (i = 1; i <= m; i++) scanf( %d%d%d , &x, &y, &z), Get(x, y, z); n   for (i = 1; i <= n; i++) A[i] = i; n   G = s; n   build(1, n); n   sort(F + 1, F + n, cmp); n   for (i = 1; i <= n; i++) fa[i] = en[i] = fr[i] = i; n   for (i = 1; i < n; i++) n     un = Getfa(F[i].x), wn = Getfa(F[i].y), en[un] = fr[wn], fr[wn] = fr[un], n     fa[un] = wn; n   now = fr[Getfa(1)]; n   printf( %d n , ToT); n   while (en[now] ^ now) printf( %d  , now), now = en[now]; n   printf( %d n , now); n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int n; n   cin >> n; n   int ans = 1; n   for (int i = 2; i <= n; ++i) { n     ans = 1LL * ans * i % M; n   } n   int two = 1; n   for (int i = 1; i < n; ++i) { n     two = (two + two) % M; n   } n   cout << (ans - two + M) % M <<   n ; n } n


#include <bits/stdc++.h> n typedef int iarrN[100003][35]; n struct IO { n   template <typename T> n   inline IO operator>>(T &n) { n     n = 0; n     char c = getchar(); n     while (c <  0  || c >  9 ) c = getchar(); n     while (c >=  0  && c <=  9 ) n       n = (n << 3) + (n << 1) + (c ^ 48), c = getchar(); n     return *this; n   } n } cin; n constexpr int MX = 33, inf = 0x3f3f3f3f; n int N, son[100003][35], mnw[100003][35]; n long long K, sw[100003][35]; n void init() { n   cin >> N >> K; n   for (int i = 1; i <= N; ++i) cin >> son[i][0], ++son[i][0]; n   for (int i = 1; i <= N; ++i) cin >> mnw[i][0], sw[i][0] = mnw[i][0]; n   for (int j = 1; j <= MX; ++j) n     for (int i = 1; i <= N; ++i) { n       son[i][j] = son[son[i][j - 1]][j - 1]; n       sw[i][j] = sw[i][j - 1] + sw[son[i][j - 1]][j - 1]; n       mnw[i][j] = std::min(mnw[i][j - 1], mnw[son[i][j - 1]][j - 1]); n     } n } n inline void query(int u) { n   int ansmn = inf; n   long long k = K, anss = 0; n   for (int i = MX; i >= 0; --i) n     if (k >= (1ll << i)) { n       k -= 1ll << i; n       anss += sw[u][i]; n       ansmn = std::min(ansmn, mnw[u][i]); n       u = son[u][i]; n     } n   printf( %I64d %d n , anss, ansmn); n } n int main() { n   init(); n   for (int i = 1; i <= N; ++i) query(i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   long long f = 1; n   long long x = 0; n   char ch = getchar(); n   while (ch >  9  || ch <  0 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) n     x = (x << 3) + (x << 1) + ch -  0 , ch = getchar(); n   return x * f; n }; n const int maxn = 1e4 + 5; n int res[maxn]; n int main() { n   int n = read(), d = read(), l = read(); n   int up, dw; n   up = (n / 2 + (n & 1)) * l - (n / 2); n   dw = (n / 2 + (n & 1)) - (n / 2) * l; n   if (d < dw || d > up) { n     printf( -1 ); n     return 0; n   } n   for (int i = 1, now = up; i <= n; ++i) { n     if (now == d) { n       printf( %d  , i & 1 ? l : 1); n       continue; n     } n     int del = min(now - d, l - 1); n     if (i & 1) { n       printf( %d  , l - del); n       now -= del; n     } else { n       printf( %d  , 1 + del); n       now -= del; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n FILE *in; n FILE *out; n const int MAX = 131072; n const int TREE = 262144; n int n; n int a[MAX]; n int tree[TREE]; n void update(int idx, int val) { n   idx += (TREE >> 1); n   while (idx) { n     tree[idx] = max(tree[idx], val); n     idx >>= 1; n   } n } n int query(int idx) { n   idx += (TREE >> 1); n   int ret = tree[idx]; n   bool flag = !(idx & 1); n   idx >>= 1; n   while (idx) { n     if (flag) ret = max(ret, tree[(idx << 1) + 1]); n     flag = !(idx & 1); n     idx >>= 1; n   } n   return ret; n } n int lis() { n   int ans = 0; n   for (int i = n - 1; i >= 0; i--) { n     int cur = query(a[i] + 1) + 1; n     ans = max(ans, cur); n     update(a[i], cur); n   } n   return ans; n } n int main(void) { n   in = stdin; n   out = stdout; n   fscanf(in,  %d , &n); n   for (int i = 0; i < n; i++) fscanf(in,  %d , &a[i]); n   fprintf(out,  %d n , lis()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; n int n, m, gx, gy, len, vis[15][15], ucs; n inline bool out(int x, int y) { return x < 0 || x >= n || y < 0 || y >= m; } n char s[15][15 + 1]; n struct P { n   int x[9], y[9], step, h; n   P() { step = 0; } n   void cal_h() { h = step + abs(gx - x[0]) + abs(gy - y[0]); } n   int get_hsh() { n     int hsh = 0; n     for (int i = 0; i < len; i++) hsh = hsh * 131 + x[i] + 1; n     for (int i = 0; i < len; i++) hsh = hsh * 131 + y[i] + 1; n     return hsh; n   } n   bool operator<(const P &p) const { return h > p.h; } n }; n int vc[15][15]; n bool chk(int x, int y) { n   vc[x][y] = 1; n   if (s[x][y] ==  @ ) return 1; n   for (int i = 0; i < 4; i++) { n     int nx = x + dx[i], ny = y + dy[i]; n     if (out(nx, ny) || vc[nx][ny] || s[nx][ny] ==  # ) continue; n     if (chk(nx, ny)) return 1; n   } n   return 0; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) scanf( %s , s[i]); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (s[i][j] ==  @ ) gx = i, gy = j; n   P p, np; n   for (int i = 1; i <= 9; i++) n     for (int j = 0; j < n; j++) n       for (int k = 0; k < m; k++) n         if (s[j][k] ==  0  + i) { n           p.x[len] = j; n           p.y[len] = k; n           len++; n         } n   if (!chk(p.x[0], p.y[0])) return puts( -1 ), 0; n   set<int> vs; n   priority_queue<P> q; n   p.cal_h(); n   q.push(p); n   vs.insert(p.get_hsh()); n   while (!q.empty()) { n     p = q.top(); n     q.pop(); n     ucs++; n     for (int i = 0; i < len - 1; i++) vis[p.x[i]][p.y[i]] = ucs; n     for (int i = 0; i < 4; i++) { n       int hx = p.x[0] + dx[i], hy = p.y[0] + dy[i]; n       if (out(hx, hy)) continue; n       if (vis[hx][hy] == ucs) continue; n       if (s[hx][hy] ==  # ) continue; n       if (s[hx][hy] ==  @ ) return printf( %d n , p.step + 1), 0; n       np.x[0] = hx; n       np.y[0] = hy; n       np.step = p.step + 1; n       memcpy(np.x + 1, p.x, (len - 1) * sizeof(int)); n       memcpy(np.y + 1, p.y, (len - 1) * sizeof(int)); n       if (vs.count(np.get_hsh())) continue; n       vs.insert(np.get_hsh()); n       np.cal_h(); n       q.push(np); n     } n   } n   puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int customCeil(int k, int s) { n   if (k % s == 0) return k / s; n   return k / s + 1; n } n int main() { n   long long int n; n   cin >> n; n   long long int arr[n]; n   for (int i = (0); i < (n); ++i) { n     cin >> arr[i]; n   } n   long long int m; n   cin >> m; n   long long int c[m]; n   for (int i = (0); i < (n); ++i) { n     cin >> c[i]; n   } n   long long int sum = 0; n   sort(arr, arr + n); n   for (int i = (0); i < (n); ++i) { n     sum += arr[i]; n   } n   for (int i = (0); i < (m); ++i) { n     cout << sum - arr[n - c[i]] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.tie(0); n   cin.sync_with_stdio(0); n   int t; n   cin >> t; n   while (t--) { n     double a, b; n     cin >> a >> b; n     cout << fixed << setprecision(15); n     if (b == 0) { n       cout << 1 << endl; n     } else if (a == 0) { n       cout << 0.5 << endl; n     } else if (a > b * 4) { n       cout << 1 - b / a << endl; n     } else { n       double remain = b - a / 4; n       cout << 1 - (b + remain) / (4 * b) << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n std::istream& operator>>(std::istream& i, pair<T, U>& p) { n   i >> p.first >> p.second; n   return i; n } n template <typename T> n std::istream& operator>>(std::istream& i, vector<T>& t) { n   for (auto& v : t) { n     i >> v; n   } n   return i; n } n template <typename T, typename U> n std::ostream& operator<<(std::ostream& o, const pair<T, U>& p) { n   o << p.first <<     << p.second; n   return o; n } n template <typename T> n std::ostream& operator<<(std::ostream& o, const vector<T>& t) { n   if (t.empty()) o <<   n ; n   for (size_t i = 0; i < t.size(); ++i) { n     o << t[i] <<    n [i == t.size() - 1]; n   } n   return o; n } n template <typename T> n using minheap = priority_queue<T, vector<T>, greater<T>>; n template <typename T> n using maxheap = priority_queue<T, vector<T>, less<T>>; n template <typename T> n bool in(T a, T b, T c) { n   return a <= b && b < c; n } n unsigned int logceil(long long first) { n   return first ? 8 * sizeof(long long) - __builtin_clzll(first) : 0; n } n namespace std { n template <typename T, typename U> n struct hash<pair<T, U>> { n   hash<T> t; n   hash<U> u; n   size_t operator()(const pair<T, U>& p) const { n     return t(p.first) ^ (u(p.second) << 7); n   } n }; n }  // namespace std n template <typename T, typename F> n T bsh(T l, T h, const F& f) { n   T r = -1, m; n   while (l <= h) { n     m = (l + h) / 2; n     if (f(m)) { n       l = m + 1; n       r = m; n     } else { n       h = m - 1; n     } n   } n   return r; n } n template <typename F> n double bshd(double l, double h, const F& f, double p = 1e-9) { n   unsigned int r = 3 + (unsigned int)log2((h - l) / p); n   while (r--) { n     double m = (l + h) / 2; n     if (f(m)) { n       l = m; n     } else { n       h = m; n     } n   } n   return (l + h) / 2; n } n template <typename T, typename F> n T bsl(T l, T h, const F& f) { n   T r = -1, m; n   while (l <= h) { n     m = (l + h) / 2; n     if (f(m)) { n       h = m - 1; n       r = m; n     } else { n       l = m + 1; n     } n   } n   return r; n } n template <typename F> n double bsld(double l, double h, const F& f, double p = 1e-9) { n   unsigned int r = 3 + (unsigned int)log2((h - l) / p); n   while (r--) { n     double m = (l + h) / 2; n     if (f(m)) { n       h = m; n     } else { n       l = m; n     } n   } n   return (l + h) / 2; n } n template <typename T> n T gcd(T a, T b) { n   if (a < b) swap(a, b); n   return b ? gcd(b, a % b) : a; n } n template <typename T> n class vector2 : public vector<vector<T>> { n  public: n   vector2() {} n   vector2(size_t a, size_t b, T t = T()) n       : vector<vector<T>>(a, vector<T>(b, t)) {} n }; n template <typename T> n class vector3 : public vector<vector2<T>> { n  public: n   vector3() {} n   vector3(size_t a, size_t b, size_t c, T t = T()) n       : vector<vector2<T>>(a, vector2<T>(b, c, t)) {} n }; n template <typename T> n class vector4 : public vector<vector3<T>> { n  public: n   vector4() {} n   vector4(size_t a, size_t b, size_t c, size_t d, T t = T()) n       : vector<vector3<T>>(a, vector3<T>(b, c, d, t)) {} n }; n template <typename T> n class vector5 : public vector<vector4<T>> { n  public: n   vector5() {} n   vector5(size_t a, size_t b, size_t c, size_t d, size_t e, T t = T()) n       : vector<vector4<T>>(a, vector4<T>(b, c, d, e, t)) {} n }; n template <typename T> n struct Fenwick { n   explicit Fenwick(unsigned int N = 0, T t = T()) n       : N(1u << logceil(N)), F(this->N, t), t(t) {} n   explicit Fenwick(const vector<T>& A, T t = T()) n       : N(1u << logceil(A.size())), F(A), t(t) { n     F.resize(N); n     fill(F.begin() + A.size(), F.end(), t); n     for (int i = 0; i < N; i++) { n       int j = i + lsb(i + 1); n       if (j < N) F[j] += F[i]; n     } n   } n   void add(int i, T v) { n     while (i <= N) { n       F[i] += v; n       i += lsb(i + 1); n     } n   } n   T sum(int i) const { n     T sum(t); n     while (i) { n       sum += F[i - 1]; n       i -= lsb(i); n     } n     return sum; n   } n   T range(int i, int j) const { n     j++; n     T s(t); n     while (j > i) { n       s += F[j - 1]; n       j -= lsb(j); n     } n     while (i > j) { n       s -= F[i - 1]; n       i -= lsb(i); n     } n     return s; n   } n   T get(int i) const { return range(i, i); } n   void set(int i, T v) { add(i, v - get(i)); } n   constexpr int lsb(int i) const { return i & -i; } n   unsigned int N; n   vector<T> F; n   T t; n   int max_lower(T v) { n     if (F[0] >= v) return 0; n     if (F[N - 1] < v) return N - 1; n     int first = 0; n     T a = t; n     for (int s = N >> 1; s > 0; s >>= 1) n       if (a + F[first + s - 1] < v) { n         a += F[first + s - 1]; n         first += s; n       } n     return first; n   } n }; n template <typename T> n struct Fenwick2 { n   explicit Fenwick2(unsigned int N = 0, T t = T()) n       : N(1u << logceil(N)), F(this->N, this->N, t), t(t) {} n   void add(int first, int second, T v) { n     while (first <= N) { n       int z = second; n       while (z <= N) { n         F[first][z] += v; n         z += lsb(z + 1); n       } n       first += lsb(first + 1); n     } n   } n   T sum(int first, int second) { n     T sum(t); n     while (first <= N) { n       int z = second; n       while (z <= N) { n         sum += F[first][z]; n         z -= lsb(z + 1); n       } n       first -= lsb(first + 1); n     } n     return sum; n   } n   T range(int x1, int y1, int x2, int y2) { n     T ans = sum(x2, y2); n     if (x1) ans -= sum(x1 - 1, y2); n     if (y1) ans -= sum(x2, y1 - 1); n     if (x1 && y1) ans += sum(x1 - 1, y1 - 1); n     return ans; n   } n   constexpr int lsb(int i) const { return i & -i; } n   unsigned int N; n   vector2<T> F; n   T t; n }; n class FNiyazAndSmallDegrees { n  public: n   int N, X; n   vector<map<int, int>> E; n   vector<map<int, int>> ZZ; n   vector<int> D, V; n   vector<Fenwick<int>> CNT; n   vector<Fenwick<long long>> SUM; n   void add(int hiDeg, int idInFenwick, int cost) { n     CNT[hiDeg].add(idInFenwick, 1); n     SUM[hiDeg].add(idInFenwick, cost); n   } n   long long sumLowest(int hiDeg, int cnt) { n     if (cnt <= 0) return 0LL; n     int id = CNT[hiDeg].max_lower(cnt); n     if (id == CNT[hiDeg].N - 1) return 1e12; n     return SUM[hiDeg].sum(id + 1); n   } n   pair<long long, long long> solve(int u, int p, long long parentCost) { n     V[u] = X; n     long long tot = 0; n     vector<long long> Q; n     for (std::pair<int, int> v : E[u]) { n       if (v.first != p) { n         auto sol = solve(v.first, u, v.second); n         tot += sol.first; n         Q.push_back(sol.second); n       } n     } n     sort(Q.begin(), Q.end()); n     long long ans1 = 1e12, ans2 = 1e12, pref = 0; n     vector<long long> sumL(Q.size() + 2); n     int toRem = D[u] - X; n     for (int i = 0; i <= Q.size() + 1; ++i) sumL[i] = sumLowest(u, toRem - i); n     for (int i = 0; i <= Q.size(); ++i) { n       ans1 = min(ans1, pref + sumL[i]); n       ans2 = min(ans2, pref + sumL[i + 1]); n       if (i != Q.size()) pref += Q[i]; n     } n     return {tot + ans1, ans2 - ans1 + parentCost}; n   } n   void solve(istream& cin, ostream& cout) { n     cin >> N; n     E.resize(N); n     D.resize(N); n     ZZ.resize(N); n     for (int i = 0; i < N - 1; ++i) { n       int a, b, c; n       cin >> a >> b >> c; n       --a; n       --b; n       E[a][b] = c; n       E[b][a] = c; n     } n     map<int, vector<int>> ByDeg; n     for (int i = 0; i < N; ++i) { n       D[i] = E[i].size(); n       ByDeg[D[i]].push_back(i); n       vector<std::pair<int, int>> ByCost; n       for (std::pair<int, int> e : E[i]) ByCost.emplace_back(e.second, e.first); n       sort(ByCost.begin(), ByCost.end()); n       for (int j = 0; j < D[i]; ++j) ZZ[i][ByCost[j].second] = j; n       CNT.emplace_back(D[i], 0); n       SUM.emplace_back(D[i], 0LL); n     } n     V.assign(N, -1); n     vector<long long> Cost(N, 0LL); n     for (X = 0; X < N; ++X) { n       for (int v : ByDeg[X]) { n         for (std::pair<int, int> q : E[v]) { n           add(q.first, ZZ[q.first][v], q.second); n           E[q.first].erase(v); n         } n       } n       ByDeg.erase(X); n       for (auto& bd : ByDeg) { n         for (int v : bd.second) { n           if (V[v] != X) { n             auto ans = solve(v, -1, 1e12); n             Cost[X] += ans.first + min(0LL, ans.second); n           } n         } n       } n     } n     cout << Cost; n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   FNiyazAndSmallDegrees solver; n   std::istream& in(std::cin); n   std::ostream& out(std::cout); n   solver.solve(in, out); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int64_t MOD = 998244353; n const int64_t N = 2e5 + 5; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n int64_t mpow(int64_t x, int64_t y) { n   if (y == 0) return 1; n   int64_t temp = mpow(x, y / 2); n   temp *= temp; n   temp %= MOD; n   if (y & 1) return (x * temp) % MOD; n   return temp; n } n int64_t inverse(int64_t x) { return mpow(x, MOD - 2); } n int64_t fact[N], ifact[N]; n int64_t nCr(int64_t n, int64_t r) { n   int64_t ans = fact[n]; n   ans *= ifact[r]; n   ans %= MOD; n   ans *= ifact[n - r]; n   ans %= MOD; n   return ans; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   fact[0] = 1; n   ifact[0] = 1; n   for (int64_t i = 1; i < N; i++) { n     fact[i] = fact[i - 1] * i; n     fact[i] %= MOD; n     ifact[i] = inverse(fact[i]); n   } n   int64_t n, k; n   cin >> n >> k; n   int64_t arr[n]; n   for (int64_t i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   int64_t equal = 0; n   for (int64_t i = 0; i < n; i++) { n     if (arr[i] == arr[(i + 1) % n]) equal++; n   } n   int64_t ans = mpow(k, equal); n   int64_t ans2 = 0; n   for (int64_t i = 0; i <= n - equal - 1; i++) { n     int64_t lol = mpow(k - 2, i); n     if ((n - equal - i) % 2 == 1) { n       lol *= mpow(2, n - equal - i - 1); n       lol %= MOD; n     } else { n       int64_t rem = mpow(2, n - equal - i); n       rem -= nCr(n - equal - i, (n - equal - i + 1) / 2); n       rem = rem + 10 * MOD; n       rem %= MOD; n       rem *= inverse(2); n       rem %= MOD; n       lol *= rem; n       lol %= MOD; n     } n     lol *= nCr(n - equal, i); n     lol %= MOD; n     ans2 += lol; n     ans2 %= MOD; n   } n   ans *= ans2; n   ans %= MOD; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   cout <<  ?   << 1 <<     << 3 << endl; n   int ac; n   cin >> ac; n   cout <<  ?   << 1 <<     << 2 << endl; n   int ab; n   cin >> ab; n   cout <<  ?   << 2 <<     << 3 << endl; n   int bc; n   cin >> bc; n   int amc = ab - bc; n   int a2 = ac + amc; n   int a = a2 / 2; n   int b = ab - a; n   int c = bc - b; n   vector<int> res = {a, b, c}; n   for (int i = 0; i < n - 3; i++) { n     cout <<  ?   << 1 <<     << i + 4 << endl; n     int tmp; n     cin >> tmp; n     res.push_back(tmp - a); n   } n   cout <<  !  ; n   for (int val : res) cout << val <<    ; n   cout << endl; n } n


#include<bits/stdc++.h> n using namespace std; n #define ll long long n #define srt(s) sort(s.begin(),s.end()) n int main() n { n     ll t; n     cin>>t; n     while(t--) n     { n         ll n; n         cin>>n; n         string s; n         cin>>s; n         ll x=0; n         ll p=0,p1=0; n         ll mark=0; n         stack<char>st,st1; n         for(int i=0; i<n; i++) n         { n             if(s[i]== M ) n             { n                 x++; n             } n         } n         if(3*x!=n) n         { n             mark=1; n         } n         for(int i=0; i<n; i++) n         { n             if(s[i]== T ) n             { n                 st.push(s[i]); n             } n             else n             { n                 if(st.size()>0) n                 { n                     st.pop(); n                 } n                 else n                 { n                     mark=1; n                     break; n                 } n             } n         } n         for(int i=n-1; i>=0; i--) n         { n             if(s[i]== T ) n             { n                 st1.push(s[i]); n             } n             else n             { n                 if(st1.size()>0) n                 { n                     st1.pop(); n                 } n                 else n                 { n                     mark=1; n                     break; n                 } n             } n         } n         if(mark==0) n         { n             cout<< YES <<endl; n         } n         else n         { n             cout<< NO <<endl; n         } 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   cout.tie(NULL); n   long long int f, x; n   cin >> f; n   x = f + 1; n   if (f == 0) { n     f = 0; n   } else if (x % 2 == 0) { n     f = x / 2; n   } else { n     f++; n   } n   cout << f << endl; n   return 0; n } n


#include <bits/stdc++.h> n template <class T1, class T2> n inline bool cmin(T1 &a, const T2 &b) { n   return b < a ? (a = b, true) : false; n } n template <class T1, class T2> n inline bool cmax(T1 &a, const T2 &b) { n   return a < b ? (a = b, true) : false; n } n template <class Type> n Type read() { n   Type a; n   bool b; n   unsigned char c; n   while (c = getchar() - 48, (c > 9) & (c != 253)) n     ; n   for (a = (b = c == 253) ? 0 : c; (c = getchar() - 48) <= 9; a = a * 10 + c) n     ; n   return b ? -a : a; n } n int (*rd)() = read<int>; n namespace G { n const int N = 2e5 + 10, M = 1e6 + 10; n int n, m, s, t, to[M], cap[M], head[N], next[M], deg[N], dis[N], cur[N]; n void init(int v, int src, int dst) { n   n = v, m = 0, s = src, t = dst; n   memset(head, -1, n * sizeof(int)); n   memset(deg, 0, n * sizeof(int)); n } n void edge(int u, int v, int w) { n   to[m] = v, cap[m] = w, next[m] = head[u], head[u] = m++; n   to[m] = u, cap[m] = 0, next[m] = head[v], head[v] = m++; n } n void add_edge(int u, int v, int min, int max) { n   if (max > min) edge(u, v, max - min); n   deg[u] += min; n   deg[v] -= min; n } n bool bfs() { n   static int queue[N]; n   int *qhead = queue, *qtail = queue; n   memset(dis, -1, n * sizeof(int)); n   dis[ *qtail++ = t] = 0; n   while (qhead != qtail) { n     int u = *qhead++; n     for (int i = head[u]; ~i; i = next[i]) n       if (cap[i ^ 1] && dis[to[i]] == -1) { n         dis[ *qtail++ = to[i]] = dis[u] + 1; n         if (to[i] == s) return true; n       } n   } n   return false; n } n int dfs(int u, int lim) { n   if (u == t) return lim; n   int rem = lim; n   for (int &i = cur[u]; ~i; i = next[i]) n     if (cap[i] && dis[to[i]] + 1 == dis[u]) { n       int now = dfs(to[i], std::min(cap[i], rem)); n       cap[i] -= now; n       cap[i ^ 1] += now; n       if ((rem -= now) == 0) break; n     } n   return lim - rem; n } n int flow(int ss, int tt) { n   int msave = m, ans = deg[ss]; n   edge(tt, ss, 1e9); n   for (int i = 0; i < n; ++i) { n     if (deg[i] < 0) edge(s, i, -deg[i]); n     if (deg[i] > 0) edge(i, t, deg[i]); n   } n   while (bfs()) { n     memcpy(cur, head, n * sizeof(int)); n     dfs(s, 1e9); n   } n   for (int i = head[s]; ~i; i = next[i]) n     if (cap[i]) return -1; n   for (int i = 0; i < n; ++i) n     while (~head[i] && head[i] >= msave) head[i] = next[head[i]]; n   s = ss, t = tt; n   while (bfs()) { n     memcpy(cur, head, n * sizeof(int)); n     dfs(s, 1e9); n   } n   for (int i = head[s]; ~i; i = next[i]) ans += cap[i ^ 1]; n   return ans; n } n }  // namespace G n const int N = 1e5; n int x[N], y[N], nx, ny, rx[N], ry[N], cnt[2 * N], lim[2 * N]; n char ans[N + 1]; n int main() { n   int n, m, r, b; n   bool rev = false; n   n = rd(), m = rd(), r = rd(), b = rd(); n   if (r > b) rev = true, std::swap(r, b); n   for (int i = 0; i < n; ++i) { n     x[i] = rx[i] = rd(); n     y[i] = ry[i] = rd(); n   } n   std::sort(rx, rx + n); n   nx = std::unique(rx, rx + n) - rx; n   std::sort(ry, ry + n); n   ny = std::unique(ry, ry + n) - ry; n   G::init(nx + ny + 4, nx + ny + 2, nx + ny + 3); n   for (int i = 0; i < n; ++i) { n     x[i] = std::lower_bound(rx, rx + nx, x[i]) - rx; n     y[i] = std::lower_bound(ry, ry + ny, y[i]) - ry; n     ++cnt[x[i]], ++cnt[nx + y[i]]; n     G::add_edge(x[i], nx + y[i], 0, 1); n   } n   std::fill(lim, lim + nx + ny, n); n   while (m--) { n     int t = rd(), l = rd(), d = rd(); n     if (t == 1) { n       int i = std::lower_bound(rx, rx + nx, l) - rx; n       if (rx[i] == l) cmin(lim[i], d); n     } else { n       int i = std::lower_bound(ry, ry + ny, l) - ry; n       if (ry[i] == l) cmin(lim[nx + i], d); n     } n   } n   for (int i = 0; i < nx + ny; ++i) { n     lim[i] -= (lim[i] + cnt[i]) & 1; n     if (lim[i] < 0) { n       puts( -1 ); n       return 0; n     } n     int min = std::max(0, (cnt[i] - lim[i]) / 2), max = (lim[i] + cnt[i]) / 2; n     if (i < nx) n       G::add_edge(nx + ny, i, min, max); n     else n       G::add_edge(i, nx + ny + 1, min, max); n   } n   int f = G::flow(nx + ny, nx + ny + 1); n   if (f == -1) { n     puts( -1 ); n     return 0; n   } n   for (int i = 0; i < n; ++i) ans[i] =  rb [rev ^ G::cap[2 * i]]; n   printf( %lld n%s n , (long long)r * f + (long long)b * (n - f), ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   while (n--) { n     long long a, b, ans; n     cin >> a >> b; n     long long x; n     x = min(a, b); n     ans = max(max(a, b), 2 * x); n     cout << ans * ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int modpow(long long int a, long long int n, long long int temp) { n   long long int res = 1; n   while (n > 0) { n     res = (res * res) % temp; n     if (n & 1) res = (res * a) % temp; n     n /= 2; n   } n   return res; n } n long long int gcd(long long int a, long long int b) { n   if (a == 0) n     return (b); n   else n     return (gcd(b % a, a)); n } n void f(int a[][1001], int x, int y) { n   if (a[x][y] >= 4) { n     int t = a[x][y] / 4; n     a[x][y + 1] += t; n     a[x + 1][y] += t; n     a[x - 1][y] += t; n     a[x][y - 1] += t; n     a[x][y] = a[x][y] % 4; n     f(a, x + 1, y); n     f(a, x - 1, y); n     f(a, x, y + 1); n     f(a, x, y - 1); n   } n } n int main() { n   int a[1001][1001] = {0}; n   int n, q, x, y; n   cin >> n >> q; n   a[500][500] = n; n   f(a, 500, 500); n   while (q--) { n     cin >> x >> y; n     x += 500; n     y += 500; n     if (x > 1000 || y > 1000 || x < 0 || y < 0) n       cout << 0 << endl; n     else n       cout << a[x][y] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int pi = 10000000; n vector<long long int> prime(pi), pri; n void seive() { n   long long int i, j, k, l; n   for (i = 4; i <= pi; i += 2) prime[i] = 1; n   pri.push_back(2); n   for (i = 3; i <= pi; i += 2) { n     if (prime[i] == 0) pri.push_back(i); n     for (j = i * i; j <= pi; j += i) { n       if (prime[j] == 0) prime[j] = 1; n     } n   } n   cout << pri.size() << endl; n } n int main() { n   long long int n, i = 0, l = -2, k = INT_MAX, j, a = 0, f = 1, c = 0, b = 0, n                    x = 100000000007, d = 0, y, m, zero = 0, one = 10, n                    z = 1000000007; n   string s =  qwertyuiopasdfghjkl;zxcvbnm,./ , mar =   , s2; n   char ch[26] = { a ,  b ,  c ,  d ,  e ,  f ,  g ,  h ,  i , n                   j ,  k ,  l ,  m ,  n ,  o ,  p ,  q ,  r , n                   s ,  t ,  u ,  v ,  w ,  x ,  y ,  z }, n        bo, an; n   vector<long long int> vc, vct, vctt; n   vector<pair<long long int, long long int>> pa; n   map<char, char> mp; n   double num, nr, radi; n   stack<char> bra, bra2; n   cin >> n >> m >> s; n   for (i = 0; i < m; i++) { n     cin >> bo >> an; n     for (j = 0; j < 26; j++) { n       if (ch[j] == bo) n         ch[j] = an; n       else if (ch[j] == an) n         ch[j] = bo; n     } n   } n   for (i = 0; i < n; i++) { n     mar += ch[s[i] -  a ]; n   } n   cout << mar << endl; n } n


#include <bits/stdc++.h> n using namespace std; n string s[70010]; n map<pair<int, int>, int> m; n pair<int, int> ans[70010]; n int main() { n   int k = 64, p = 5; n   int n, i, j, l; n   cin >> n; n   for (i = 1; i <= n; i++) cin >> s[i]; n   for (i = 1; i <= n; i++) n     for (j = 0; j < 9; j++) { n       int x = 0; n       for (l = j; l < 9; l++) { n         x = x * 10 + s[i][l] -  0 ; n         if (m.find(pair<int, int>(x, l - j + 1)) != m.end() && n             m[pair<int, int>(x, l - j + 1)] != i) n           m[pair<int, int>(x, l - j + 1)] = -1; n         else n           m[pair<int, int>(x, l - j + 1)] = i; n       } n     } n   for (auto it = m.begin(); it != m.end(); it++) { n     if (it->second == -1) continue; n     if (ans[it->second].second == 0 || n         ans[it->second].second > it->first.second) n       ans[it->second] = pair<int, int>(it->first.first, it->first.second); n   } n   for (i = 1; i <= n; i++) { n     char s[10]; n     sprintf(s,  %%0%dd n , ans[i].second); n     printf(s, ans[i].first); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 1e5 + 10; n int f[MAX_N], g[MAX_N], h[MAX_N], pos[MAX_N]; n vector<int> adj[MAX_N]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   int m = 0; n   for (int i = 1; i <= n; i++) { n     cin >> f[i]; n     if (adj[f[i]].empty()) { n       m++; n       h[m] = f[i]; n       pos[f[i]] = m; n     } n     adj[f[i]].push_back(i); n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < adj[i].size(); j++) { n       if (g[adj[i][j]] != 0) { n         cout <<  -1 n ; n         return 0; n       } n       g[adj[i][j]] = pos[i]; n     } n   } n   for (int i = 1; i <= n; i++) { n     if (!g[i]) { n       cout <<  -1 n ; n       return 0; n     } n   } n   for (int i = 1; i <= m; i++) { n     if (g[h[i]] != i) { n       cout <<  -1 n ; n       return 0; n     } n   } n   cout << m <<   n ; n   for (int i = 1; i <= n; i++) { n     cout << g[i]; n     if (i < n) cout <<    ; n   } n   cout <<   n ; n   for (int i = 1; i <= m; i++) { n     cout << h[i]; n     if (i < m) cout <<    ; n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, xa, ya, xb, yb; n int main() { n   scanf( %d%d%d%d%d%d , &a, &b, &xa, &ya, &xb, &yb); n   a <<= 1, b <<= 1; n   int x1 = (xa + ya), y1 = (xa - ya), x2 = (xb + yb), y2 = (xb - yb); n   int sector_1_x = (x1 / a) - (x1 < 0), sector_1_y = (y1 / b) - (y1 < 0); n   int sector_2_x = (x2 / a) - (x2 < 0), sector_2_y = (y2 / b) - (y2 < 0); n   printf( %d n , n          max(abs(sector_1_x - sector_2_x), abs(sector_1_y - sector_2_y))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long M, n; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long ttttt = 1; n   while (ttttt--) { n     cin >> M >> n; n     long long a[n], ans[n]; n     cin >> a[0]; n     long long ok = -1; n     if (a[0] <= M * -1) ok = 1; n     for (long long i = 1; i < n; i++) { n       cin >> a[i], a[i] += a[i - 1]; n       if (a[i] <= M * -1 && ok == -1) ok = i + 1; n     } n     if (ok != -1) { n       cout << ok; n       cout <<   n ; n       return 0; n     } n     if (a[n - 1] >= 0) { n       cout << -1; n       cout <<   n ; n       return 0; n     } n     for (long long i = 0; i < n; i++) { n       long long x = M + a[i]; n       x = ceil(x / (a[n - 1] * -1.0)); n       ans[i] = x * n + i + 1; n     } n     for (long long i = 0; i < n; i++) ans[0] = min(ans[0], ans[i]); n     cout << ans[0]; n     cout <<   n ; n     return 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mpow(int base, int exp); n void ipgraph(int m); n void dfs(int u, int par); n const int mod = 1000000007; n const int N = 1e6, M = N; n const int LN = 1e9; n long long dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; n using namespace std; 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 lcm(long long a, long long b) { return a * b / gcd(a, b); } n struct cmp { n   bool operator()(const pair<int, int> &a, const pair<int, int> &b) { n     int len1 = a.second - a.first + 1; n     int len2 = b.second - b.first + 1; n     if (len1 == len2) { n       return a.first < b.first; n     } n     return len1 > len2; n   } n }; n long long fac[N], inv[N], b[N]; n int mpow(int base, int exp) { n   base %= mod; n   int result = 1; n   while (exp > 0) { n     if (exp & 1) result = ((long long)result * base) % mod; n     base = ((long long)base * base) % mod; n     exp >>= 1; n   } n   return result; n } n void pre() { n   int i; n   fac[0] = inv[0] = 1; n   fac[1] = inv[1] = 1; n   for (i = 2; 2 < N ? i < N : i > N; 2 < N ? i += 1 : i -= 1) { n     fac[i] = (i * fac[i - 1]) % mod; n     inv[i] = (mpow(i, mod - 2) * inv[i - 1]) % mod; n   } n } n long long C(int n, int r) { n   if (r > n) return 0; n   long long ans = fac[n]; n   ans *= inv[r]; n   ans %= mod; n   ans *= inv[n - r]; n   ans %= mod; n   return ans; n } n long long n; n vector<vector<int>> arr(50, vector<int>(50)); n vector<int> permutation; n int chosen[100] = {0}; n long long power(long long a, long long b) { n   long long ans = 1; n   while (b--) ans *= a; n   return ans; n } n long long k; n long long dp[N][2]; n vector<int> adj[N]; n long long m; n void solve() { n   long long a, b, c, p = 0, q, d, r, s, l, t, x, h, y, z, avg, x1, x2, y1, y2, n                      i, j, ii, jj, count = 0; n   string s1, s2 =   , sa =   , sb =   , sc =   , ans =   ; n   cin >> a >> b >> c; n   if (a >= c) { n     cout << -1 <<    ; n   } else { n     cout << 1 <<    ; n   } n   if (c < a * b) { n     cout << b << endl; n   } else n     cout << -1 << endl; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t; n   t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int const MOD = 1000000007; n void add(int &a, int b) { n   a += b; n   if (a >= MOD) a -= MOD; n } n int f[42], when[42]; n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   int s = 1; n   int c = getchar(); n   while (c <= 32) { n     c = getchar(); n   } n   int cn = 0; n   while (c > 32) { n     int olds = s; n     add(s, s); n     add(s, MOD - f[c -  a ]); n     f[c -  a ] = olds; n     when[c -  a ] = ++cn; n     c = getchar(); n   } n   for (int i = 0; i < n; i++) { n     int v = -1; n     for (int j = 0; j < k; j++) { n       if (v < 0 || when[v] > when[j]) { n         v = j; n       } n     } n     when[v] = ++cn; n     int olds = s; n     add(s, s); n     add(s, MOD - f[v]); n     f[v] = olds; n   } n   printf( %d n , s); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int l, d, v, g, r, tmu; n   double tm, tmod; n   scanf( %d%d%d%d%d , &l, &d, &v, &g, &r); n   tmu = (int)((long double)(d / v) / (g + r)); n   tmod = (double)d / v - tmu * (g + r); n   if (tmod < g) n     tm = (double)l / v; n   else n     tm = (double)l / v + (r - tmod + g); n   printf( %.8f n , tm); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, a; n   cin >> n >> m; n   int l = 0, r = n + 1; n   string s, s1; n   for (int i = 0; i < m; i++) { n     cin >> s >> s >> s1 >> s >> a; n     if (s1[0] ==  r ) n       l = max(l, a); n     else n       r = min(a, r); n   } n   int ans = r - l - 1; n   if (ans <= 0) n     cout << -1 << endl; n   else n     cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x, y; n   cin >> n >> x >> y; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) { n     int j = i - x; n     int k = i + y; n     if (j < 0) j = 0; n     if (k > n - 1) k = n - 1; n     int min = INT_MAX, index = -1; n     for (int l = j; l <= k; l++) { n       if (min > a[l]) { n         min = a[l]; n         index = l; n       } n     } n     if (index == i) { n       cout << index + 1 <<   n ; n       break; n     } else { n       i = index - 1; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void reverseStr(string& str) { n   int n = str.length(); n   for (int i = 0; i < n / 2; i++) swap(str[i], str[n - i - 1]); n } n int main() { n   string str, str1; n   cin >> str; n   cin >> str1; n   reverseStr(str); n   if (str == str1) { n     cout <<  YES  << endl; n   } else { n     cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n void solve() { n   long long n, m; n   cin >> n >> m; n   vector<long long> out(n); n   long long leafs = n; n   for (long long i = 0; i < m; ++i) { n     long long x, y; n     cin >> x >> y; n     --x; n     --y; n     if (x > y) swap(x, y); n     out[x]++; n     if (out[x] == 1) --leafs; n   } n   long long q; n   cin >> q; n   while (q--) { n     long long t; n     cin >> t; n     if (t == 3) { n       cout << leafs << endl; n     } else if (t == 1) { n       long long u, v; n       cin >> u >> v; n       --u; n       --v; n       if (u > v) swap(u, v); n       ++out[u]; n       if (out[u] == 1) --leafs; n     } else { n       long long u, v; n       cin >> u >> v; n       --u; n       --v; n       if (u > v) swap(u, v); n       --out[u]; n       if (out[u] == 0) ++leafs; n     } n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cout << fixed << setprecision(15); n   long long t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> graph[3010]; n int cycle[3010]; n int cycle_size; n bool visited[3010]; n bool found_cycle; n int dist[3010]; n int b, e; n void dfs(int node, int father, int c) { n   if (found_cycle) return; n   visited[node] = true; n   for (int i = 0; i < graph[node].size() && !found_cycle; i++) { n     int nextn = graph[node][i]; n     if (visited[nextn] && nextn != father) { n       cycle[c] = node; n       cycle_size = c + 1; n       while (cycle[c] != nextn) { n         c--; n       } n       b = c; n       found_cycle = true; n       return; n     } n     if (nextn == father) continue; n     cycle[c] = node; n     dfs(nextn, node, c + 1); n   } n } n void bfs() { n   queue<pair<int, int> > q; n   for (int i = b; i < cycle_size; i++) { n     visited[cycle[i]] = true; n     q.push(make_pair(cycle[i], 0)); n   } n   while (!q.empty()) { n     pair<int, int> aux = q.front(); n     q.pop(); n     dist[aux.first] = aux.second; n     for (int i = 0; i < graph[aux.first].size(); i++) { n       int nextn = graph[aux.first][i]; n       if (visited[nextn]) continue; n       visited[nextn] = true; n       q.push(make_pair(nextn, aux.second + 1)); n     } n   } n } n int main() { n   int n; n   scanf( %d , &n); n   int xi, yi; n   for (int i = 0; i < n; i++) { n     scanf( %d %d , &xi, &yi); n     graph[xi].push_back(yi); n     graph[yi].push_back(xi); n   } n   found_cycle = false; n   memset(visited, false, sizeof(visited)); n   dfs(1, -1, 0); n   memset(visited, false, sizeof(visited)); n   bfs(); n   for (int i = 1; i <= n; i++) printf( %d  , dist[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n const int N = 2e5 + 10; n int T, n, a[N], vis[N], d[N], mi, mx; n signed main() { n   cin >> T; n   while (T--) { n     cin >> n; n     mi = 0x3f3f3f3f, mx = -999; n     for (int i = 1; i <= n; i++) scanf( %d , &a[i]), vis[a[i]] = i; n     for (int i = 1; i <= n; i++) { n       mi = min(mi, vis[i]), mx = max(mx, vis[i]); n       if (mx - mi == i - 1) n         putchar( 1 ); n       else n         putchar( 0 ); n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000 * 1000 * 1000; n const int MOD = INF + 7; n const int XD = 100100; n int facts[XD << 1]; n void calcFacts(int n) { n   facts[0] = 1; n   for (int i = 1; i <= n; ++i) { n     facts[i] = 1LL * facts[i - 1] * i % MOD; n   } n } n int exgcd(int a, int b, int &x, int &y) { n   if (a == 0) { n     x = 0; n     y = 1; n     return b; n   } n   int xx, yy; n   int d = exgcd(b % a, a, xx, yy); n   x = yy - b / a * xx; n   y = xx; n   return d; n } n int getInv(int n) { n   int x, y; n   exgcd(n, MOD, x, y); n   x = (x % MOD + MOD) % MOD; n   return x; n } n int cnk(int n, int k) { n   return 1LL * facts[n] * getInv(facts[n - k]) % MOD * getInv(facts[k]) % MOD; n } n int main() { n   int zeroes, ones, sm; n   cin >> zeroes >> ones >> sm; n   if (zeroes == 0) { n     if (sm == 1) { n       cout << ((ones == 1) ? 1 : 0); n     } else { n       cout << ((ones == 1) ? 0 : 1); n     } n     return 0; n   } n   if (ones == 0) { n     if (sm == 1) { n       cout << ((zeroes & 1) ? 0 : 1); n     } else { n       cout << ((zeroes & 1) ? 1 : 0); n     } n     return 0; n   } n   calcFacts(zeroes + ones); n   int res = 0; n   for (int pos = 0; pos <= zeroes; ++pos) { n     if (sm == 0 && n         (pos % 2 == 0 && (pos < zeroes || pos == zeroes && ones > 1) || n          pos % 2 == 1 && (pos == zeroes && ones == 1))) { n       res += cnk(zeroes + ones - pos - 1, ones - 1); n       res %= MOD; n     } n     if (sm == 1 && n         (pos % 2 == 1 && (pos < zeroes || pos == zeroes && ones > 1) || n          pos % 2 == 0 && (pos == zeroes && ones == 1))) { n       res += cnk(zeroes + ones - pos - 1, ones - 1); n       res %= MOD; n     } n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200][200], ans, n; n bool flag = true; n int main() { n   ios::sync_with_stdio(false); n   cin >> a[80][80] >> n; n   while (flag) { n     flag = false; n     for (int i = 1; i < 160; i++) n       for (int j = 1; j < 160; j++) n         if (a[i][j] >= 4) { n           flag = true; n           int k = a[i][j] / 4; n           a[i + 1][j] += k, a[i - 1][j] += k; n           a[i][j + 1] += k, a[i][j - 1] += k; n           a[i][j] -= 4 * k; n         } n   } n   for (int i = 0; i < n; i++) { n     int x, y; n     cin >> x >> y; n     if (-80 < x && x < 80 && -80 < y && y < 80) n       cout << a[x + 80][y + 80] <<   n ; n     else n       cout << 0 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   long long int n, m, k; n   cin >> n >> m >> k; n   vector<long long int> ans; n   if (k > 1) { n     long long int last = 0; n     for (long long int i = 0; i < k; i++) { n       ans.push_back(0); n     } n     last = n - 1; n     for (long long int i = 1; i < n + m; i++) { n       if (i == last) { n         ans.push_back(i); n       } else if (i == last + 1) { n         for (long long int j = 0; j < k - 1; j++) ans.push_back(i); n         last = i + n - 1; n       } n     } n   } else { n     ans.push_back(0); n     long long int last = n - 1; n     for (long long int i = 1; i < n + m; i++) { n       if (i == last) { n         ans.push_back(i); n         last = i + n - 1; n       } n     } n   } n   cout << ans.size() <<   n ; n   for (auto i : ans) cout << i + 1 <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v[150005]; n int Size[150005]; n int forbid[150005]; n int val[150005]; n vector<int> stk; n void dfs(int first, int f) { n   Size[first] = 1; n   stk.push_back(first); n   for (auto it : v[first]) { n     if (it != f && !forbid[it]) { n       dfs(it, first); n       Size[first] += Size[it]; n     } n   } n } n bool cmp(const int &a, const int &b) { return Size[a] < Size[b]; } n int find_cen(int first) { n   stk.clear(); n   dfs(first, 0); n   sort(stk.begin(), stk.end(), cmp); n   for (auto it : stk) { n     if (Size[it] >= Size[first] / 2) { n       return it; n     } n   } n } n long long ans; n vector<pair<long long, long long> > vv; n void dfs2(int first, int f, long long sum, long long sum2, long long len) { n   sum += val[first]; n   sum2 += sum; n   vv.push_back(make_pair(len, sum2)); n   for (auto it : v[first]) { n     if (it != f && !forbid[it]) { n       dfs2(it, first, sum, sum2, len + 1); n     } n   } n } n void dfs3(int first, int f, long long sum, long long sum2, long long len) { n   sum += val[first]; n   sum2 += val[first] * len; n   vv.push_back(make_pair(sum, sum2)); n   for (auto it : v[first]) { n     if (it != f && !forbid[it]) { n       dfs3(it, first, sum, sum2, len + 1); n     } n   } n } n struct DATA { n   long long first, second, st; n }; n int op; n bool operator<(const DATA &p, const DATA &q) { n   if (op == 0) n     return p.first < q.first; n   else n     return p.st < q.st; n } n void go(int first) { n   int cen = find_cen(first); n   set<DATA> s; n   for (int t = 0; t < 2; t++) { n     s.insert({0, 0, -1000000000}); n     for (auto first : v[cen]) { n       if (forbid[first]) continue; n       dfs2(first, cen, val[cen], val[cen], 2); n       op = 1; n       for (auto p : vv) { n         if (s.empty()) break; n         auto it = prev(s.upper_bound({0, 0, p.first})); n         ans = max(ans, p.first * it->first + it->second + p.second); n       } n       vv.clear(); n       dfs3(first, cen, 0, 0, 1); n       op = 0; n       for (auto p : vv) { n         auto it = s.lower_bound({p.first, p.second, 0}); n         if (it != s.end() && it->first == p.first) { n           if (it->second > p.second) n             continue; n           else { n             s.erase(*it); n             it = s.lower_bound({p.first, p.second, 0}); n           } n         } n         while (it != s.end()) { n           DATA d = *it; n           auto it2 = next(it); n           s.erase(it); n           it = it2; n           long long st = (p.second - d.second) / (d.first - p.first); n           d.st = st; n           if (it == s.end() || it->st > st) { n             s.insert(d); n             break; n           } n         } n         it = s.lower_bound({p.first, p.second, 0}); n         int st = -1e9; n         while (it != s.begin()) { n           DATA d = *prev(it); n           st = (d.second - p.second) / (p.first - d.first); n           if (st <= d.st) { n             s.erase(d); n           } else { n             break; n           } n         } n         if (it == s.end() || it->st > st) s.insert({p.first, p.second, st}); n       } n       vv.clear(); n     } n     reverse(v[cen].begin(), v[cen].end()); n     s.clear(); n   } n   forbid[cen] = 1; n   for (auto it : v[cen]) { n     if (!forbid[it]) { n       go(it); n     } n   } n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int first, second; n     scanf( %d %d , &first, &second); n     v[first].push_back(second); n     v[second].push_back(first); n   } n   for (int i = 1; i <= n; i++) scanf( %d , &val[i]); n   go(1); n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n int a, b, c, d, n; n char s[4 << 20]; n void output() { n   for (int i = 0; i < n; ++i) n     if (s[i] ==  4 ) n       a--; n     else n       b--; n   for (int i = 0; i < n; ++i) n     if (s[i] ==  4 ) { n       while (a) { n         printf( 4 ); n         a--; n       } n       printf( 4 ); n     } else { n       while ((i + 2 >= n) && b) { n         printf( 7 ); n         b--; n       } n       printf( 7 ); n     } n   printf(  n ); n   exit(0); n } n void look(char c4, char c7, int a, int b, int c, int d) { n   n = 0; n   while (c) { n     if (n) d--; n     s[n++] = c4; n     s[n++] = c7; n     a--; n     b--; n     c--; n   } n   if (d > 0) { n     s[n++] = c4; n     a--; n     d--; n   } n   if ((a < 0) || (b < 0) || (c != 0) || (d != 0)) return; n   output(); n } n int main() { n   scanf( %d%d%d%d , &a, &b, &c, &d); n   look( 4 ,  7 , a, b, c, d); n   look( 7 ,  4 , b, a, d, c); n   printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 100; n const int inf = 0x3f3f3f3f; n int n, m; n long long ans; n long long b[maxn], g[maxn]; n bool cmp(int a, int b) { return a > b; } n int main() { n   long long bmx = 0; n   int flag = 1, cnt = 0; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &b[i]); n     ans += b[i] * m; n     bmx = max(bmx, b[i]); n   } n   sort(b + 1, b + 1 + n, cmp); n   for (int i = 1; i <= m; i++) { n     scanf( %lld , &g[i]); n     if (g[i] < bmx) flag = 0; n     if (g[i] > bmx) { n       cnt++; n       ans += g[i] - b[1 + cnt / m]; n     } n   } n   if (flag) { n     printf( %lld n , ans); n   } else { n     printf( -1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n * 2]; n   for (int i = 0; i < n * 2; i++) { n     cin >> a[i]; n   } n   int room = 0; n   for (int i = 0; i < n * 2; i++) { n     if (i % 2 == 0) { n       if (a[i + 1] - a[i] >= 2) { n         room++; n       } n     } n   } n   cout << room; 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 m, n; n     scanf( %d %d , &m, &n); n     if (m % n == 0) n       printf( YES n ); n     else n       printf( NO n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s =   ; n   int f[n]; n   f[0] = 1, f[1] = 1; n   if (n > 1 && n < 5) { n     for (int i = 2; i < 2 * n; i++) f[i] = f[i - 2] + f[i - 1]; n     int g = 1, k = 0; n     while (g <= n) { n       for (int j = 0; j < 2 * n; j++) n         if (g == f[j]) { n           k++; n         } n       if (k > 0) n         s = s +  O ; n       else if (k == 0) n         s = s +  o ; n       g++; n       k = 0; n     } n   } else { n     for (int i = 2; i < n; i++) f[i] = f[i - 2] + f[i - 1]; n     int g = 1, k = 0; n     while (g <= n) { n       for (int j = 0; j < n; j++) n         if (g == f[j]) { n           k++; n         } n       if (k > 0) n         s = s +  O ; n       else if (k == 0) n         s = s +  o ; n       g++; n       k = 0; n     } n   } n   cout << s; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool check(long long int arr1[], long long int arr2[], long long int m, n            long long int n) { n   sort(arr1, arr1 + n); n   sort(arr2, arr2 + m); n   for (int i = 0; i < n; i++) n     if (arr1[i] != arr2[i]) return false; n   return true; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     long long int n, m, x, y; n     cin >> n >> m >> x >> y; n     char a[n][m], dp[n + 1][m + 1]; n     long long int c = 0, ans = 0; n     for (long long int i = 0; i < n; i++) { n       for (long long int j = 0; j < m; j++) { n         cin >> a[i][j]; n       } n     } n     memset(dp, 0, sizeof(dp)); n     dp[1][1] = x; n     for (long long int i = 0; i < n; i++) { n       c = 0; n       for (long long int j = 0; j < m; j++) { n         if (a[i][j] ==  .  && j + 1 < m && a[i][j + 1] ==  . ) { n           ans += min(x * 2, y); n           j++; n         } else if (a[i][j] ==  . ) n           ans += x; n       } n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2e9 + 10; n int read() { n   int xx = 0, ww = 1; n   char ch = getchar(); n   while (ch >  9  || ch <  0 ) { n     if (ch ==  - ) ww = -ww; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     xx = 10 * xx + ch - 48; n     ch = getchar(); n   } n   return xx * ww; n } n deque<int> q; n int n, k, a; n int main() { n   n = read(); n   k = read(); n   for (int i = 1; i <= n; i++) q.push_back(i); n   for (int i = 1; i <= k; i++) { n     a = read(); n     a %= q.size(); n     while (a--) { n       q.push_back(q.front()); n       q.pop_front(); n     } n     int v = q.front(); n     q.pop_front(); n     if (i != 1) printf(   ); n     printf( %d , v); n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, l, r, x, ans[300300], tree[4 * 300300]; n void build(int node, int st, int en) { n   if (st == en) { n     tree[node] = 1; n     return; n   } n   build((node << 1), st, (st + en) / 2); n   build((node << 1) + 1, ((st + en) / 2) + 1, en); n   tree[node] = tree[(node << 1)] + tree[(node << 1) + 1]; n } n void update(int node, int st, int en, int left, int right, int val) { n   if (left > en || right < st) return; n   if (tree[node] == 0) return; n   if (st == en) { n     tree[node] = (st == x ? 1 : 0); n     ans[st] = val; n     return; n   } n   update((node << 1), st, (st + en) / 2, left, right, val); n   update((node << 1) + 1, ((st + en) / 2) + 1, en, left, right, val); n   tree[node] = tree[(node << 1)] + tree[(node << 1) + 1]; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   build(1, 1, n); n   while (m--) { n     cin >> l >> r >> x; n     update(1, 1, n, l, r, x); n   } n   ans[x] = 0; n   for (int i = 1; i <= n; i++) cout << ans[i] <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n struct Nodo { n   int a; n   int sgte; n } Nodo[100100]; n long long aux[50010][510]; n long long int cont; n int dir[50001]; n int n, k; n void agregar(int x, int y, int c); n void busqueda(int fa, int u); n int main() { n   int x, y, i, t, cargado = 0; n   scanf( %d %d , &n, &k); n   for (t = 0; t < 50010; t++) { n     dir[t] = -1; n   } n   for (i = 0; i < (n - 1); i++) { n     scanf( %d %d , &x, &y); n     agregar(x, y, cargado); n     cargado++; n     agregar(y, x, cargado); n     cargado++; n   } n   busqueda(0, 1); n   printf( %I64d , cont); n   return 0; n } n void busqueda(int ant, int act) { n   int i = 0, v, j; n   aux[act][0] = 1; n   for (i = dir[act]; i != -1; i = Nodo[i].sgte) { n     v = Nodo[i].a; n     if (ant != v) { n       busqueda(act, v); n       for (j = 0; j < k; j++) { n         cont += aux[act][j] * aux[v][k - j - 1]; n       } n       for (j = 1; j <= k; j++) { n         aux[act][j] += aux[v][j - 1]; n       } n     } n   } n } n void agregar(int x, int y, int c) { n   Nodo[c].a = y; n   Nodo[c].sgte = dir[x]; n   dir[x] = c; n } n


#include <bits/stdc++.h> n using namespace std; n long long k; n long long maxi = 0, mini = 0; n vector<long long> v[200001]; n long long dp[200001]; n unordered_map<long long, unordered_map<long long, long long> > dis; n long long f(long long parent) { n   if (dp[parent] > 0) return dp[parent]; n   dp[parent] = -1; n   long long ans = 0; n   for (long long i = 0; i < v[parent].size(); i++) { n     long long child = v[parent][i]; n     if (dp[child] != -1) ans += f(child); n   } n   dp[parent] = ans + 1; n   for (long long i = 0; i < v[parent].size(); i++) { n     long long child = v[parent][i]; n     if (dp[child] != -1) { n       maxi += (min(dp[child], 2 * k - dp[child]) * (dis[child][parent])); n       if (dp[child] % 2 == 1 || 2 * k - dp[child] % 2 == 1) n         mini += dis[child][parent]; n     } n   } n   return dp[parent]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     maxi = 0; n     mini = 0; n     long long start; n     cin >> k; n     for (long long i = 1; i <= 2 * k; i++) v[i].clear(); n     dp[2 * k - 1] = 0; n     dp[2 * k] = 0; n     for (long long i = 0; i < 2 * k - 1; i++) { n       dp[i] = 0; n       long long u1, u2, time; n       cin >> u1 >> u2 >> time; n       v[u1].push_back(u2); n       v[u2].push_back(u1); n       dis[u2][u1] = time; n       dis[u1][u2] = time; n     } n     for (long long i = 1; i <= 2 * k; i++) { n       if (v[i].size() == 1) { n         start = i; n         dp[i] = 1; n       } n     } n     dp[start] = -1; n     f(start); n     cout << mini <<     << maxi << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   int arr[1009]; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &arr[i]); n   int x1, y1, x2, y2; n   for (int i = 0; i < n - 1; i++) { n     x1 = min(arr[i], arr[i + 1]); n     y1 = max(arr[i], arr[i + 1]); n     for (int j = i + 1; j < n - 1; j++) { n       x2 = min(arr[j], arr[j + 1]); n       y2 = max(arr[j], arr[j + 1]); n       if ((x1 < x2 && y1 > x2 && y2 > y1) || (x2 < x1 && y2 > x1 && y1 > y2)) { n         printf( yes n ); n         return 0; n       } n     } n   } n   printf( no n ); n   return 0; n } n


#include <bits/stdc++.h> n int a[4 * 4]; n int n, n2; n std::map<int, int> sums[4 + 1]; n void gens(int p, int cnt, int sum, std::map<int, int>& s) { n   if (cnt == 0) { n     std::map<int, int>::iterator it = s.find(sum); n     if (it == s.end()) n       s[sum] = 1; n     else n       ++it->second; n     return; n   } n   if (p >= n2) return; n   gens(p + 1, cnt, sum, s); n   gens(p + 1, cnt - 1, sum + a[p], s); n } n int m[4][4]; n int hs[4], vs[4], ds1, ds2; n bool used[4 * 4]; n int sum; n void doit(int i, int j, int v) { n   hs[i] += v; n   vs[j] += v; n   if (i == j) ds1 += v; n   if (i == n - j - 1) ds2 += v; n } n bool gen(int i, int j) { n   if (i == n) return true; n   for (int k = 0; k < n2; ++k) n     if (!used[k]) { n       int v = a[k]; n       m[i][j] = v; n       doit(i, j, v); n       if (i == n - 1 && vs[j] != sum) { n         doit(i, j, -v); n         continue; n       } n       if (j == n - 1 && hs[i] != sum) { n         doit(i, j, -v); n         continue; n       } n       if (i == n - 1 && j == 0 && ds2 != sum) { n         doit(i, j, -v); n         continue; n       } n       if (i == n - 1 && j == n - 1 && ds1 != sum) { n         doit(i, j, -v); n         continue; n       } n       if (hs[i] > sum || vs[j] > sum || ds1 > sum || ds2 > sum) { n         doit(i, j, -v); n         continue; n       } n       used[k] = true; n       int i2 = i, j2 = j + 1; n       if (j2 == n) { n         ++i2; n         j2 = 0; n       } n       if (gen(i2, j2)) return true; n       used[k] = false; n       doit(i, j, -v); n     } n   return false; n } n int main() { n   scanf( %d , &n); n   n2 = n * n; n   int min = 0; n   for (int i = 0; i < n2; ++i) { n     scanf( %d , &a[i]); n     if (a[i] < min) min = a[i]; n   } n   min = -min; n   for (int i = 0; i < n2; ++i) a[i] += min; n   for (int i = 1; i <= n; ++i) { n     gens(0, i, 0, sums[i]); n   } n   bool res = false; n   for (std::map<int, int>::const_iterator i = sums[n].begin(); n        i != sums[n].end(); ++i) { n     sum = i->first; n     memset(hs, 0, sizeof(hs)); n     memset(vs, 0, sizeof(vs)); n     memset(used, 0, sizeof(used)); n     ds1 = 0; n     ds2 = 0; n     if (gen(0, 0)) { n       res = true; n       break; n     } n   } n   if (!res) { n     printf( sorry. i am failed ); n     return 0; n   } n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < n; ++j) m[i][j] -= min; n   int s = 0; n   for (int i = 0; i < n; ++i) s += m[i][0]; n   printf( %d n , s); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       if (j > 0) printf(   ); n       printf( %d , m[i][j]); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long bin_pow(long long x, long long y, long long m) { n   long long ans; n   if (y <= 0) return 1; n   ans = bin_pow(x, y / 2, m); n   if (y % 2 == 0) return (((ans % m) * (ans % m)) % m); n   return (x % m * ((ans % m * ans % m))) % m; n } n long long mult(long long a, long long b, long long m) { n   long long ans = ((a % m) * (b % m)) % m; n   return ans; n } n long long sum(long long a, long long b, long long m) { n   long long ans = (a % m + b % m) % m; n   return ans; n } n long long diff(long long a, long long b, long long m) { n   long long ans = ((a % m) - (b % m) + m) % m; n   return ans; n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n void swap(long long &a, long long &b) { n   long long tm = a; n   a = b; n   b = tm; n } n long long compute_lps_array(string s) { n   string s1 = s; n   reverse(s1.begin(), s1.end()); n   s = s +  $  + s1; n   long long sz = s.size(); n   vector<long long> lps(sz + 1, 0); n   lps[0] = 0; n   for (long long i = 1; i < sz; i++) { n     long long j = lps[i - 1]; n     while (j != 0 && (s[i] != s[j])) j = lps[j - 1]; n     if (s[i] == s[j]) j++; n     lps[i] = j; n   } n   return lps[sz - 1]; n } n long long nCr(long long n, long long r, long long m) { n   long long tm1 = bin_pow(r, 998244353 - 2, 998244353); n   long long tm2 = bin_pow(m, 998244353 - 2, 998244353); n   long long tm3 = n % 998244353; n   long long ans = mult(tm3, mult(tm1, tm2, 998244353), 998244353); n   return ans; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t = 1; n   while (t--) { n     string s, t; n     long long ans = 0; n     cin >> s >> t; n     long long sz1 = s.size(); n     long long sz2 = t.size(); n     vector<long long> pre(sz2 + 2, 0), sfx(sz2 + 2, 0); n     long long j = 0; n     for (long long i = 0; i < sz1; i++) { n       if (s[i] == t[j]) { n         pre[j] = i; n         j++; n         if (j >= sz2) break; n       } n     } n     j = sz2 - 1; n     for (long long i = sz1 - 1; i >= 0; i--) { n       if (s[i] == t[j]) { n         sfx[j] = i; n         j--; n         if (j == -1) break; n       } n     } n     ans = max(sfx[0], sz1 - pre[sz2 - 1] - 1LL); n     for (long long i = 0; i < sz2 - 1; i++) { n       long long df = sfx[i + 1] - pre[i] - 1; n       ans = max(ans, df); n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n int num[10000]; n int map[2000][2000]; n int add(int a, int b, int c) { map[a][b] = 1, map[b][a] = 1; } n int vis[2000][2]; n int main() { n   int n; n   while (scanf( %d , &n) != EOF) { n     memset(map, 0, sizeof(map)); n     memset(vis, 0, sizeof(vis)); n     int i, j; n     int len = 0; n     int ans = 0; n     memset(map, 0, sizeof(map)); n     memset(num, 0, sizeof(num)); n     for (i = 0; i <= 30; i++) { n       if (n & (1 << i)) { n         num[i]++; n         len = i; n       } n     } n     int index = 3; n     for (i = 0; i <= 30; i++) { n       if (i != len) continue; n       int x, y; n       if (i == 0) { n         add(1, 2, 1); n         index = 3; n         continue; n       } n       for (j = 1; j <= i; j++) { n         if (j == 1) { n           add(1, index++, 3); n           add(1, index++, 4); n           x = index - 1; n           y = index - 2; n         } else { n           add(x, index, 5); n           index++; n           add(y, index, 6); n           index++; n           add(x, index - 1, 7); n           add(y, index - 2, 8); n           x = index - 1; n           y = index - 2; n         } n         vis[j][0] = x; n         vis[j][1] = y; n       } n       if (i == len) { n         add(x, 2, 1); n         add(y, 2, 1); n         continue; n       } n     } n     for (i = 0; i <= 30; i++) { n       if (num[i] && i != len) { n         int x = 1; n         for (j = 1; j <= len - i; j++) { n           add(x, index++, 1); n           x = index - 1; n         } n         if (i == 0) { n           add(x, 2, 1); n           continue; n         } n         add(x, vis[len - i + 1][0], 1); n         add(x, vis[len - i + 1][1], 1); n       } n     } n     printf( %d n , index - 1); n     for (i = 1; i < index; i++) { n       for (j = 1; j < index; j++) { n         if (map[i][j] == 1) n           printf( Y ); n         else n           printf( N ); n       } n       printf(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int del(int n) { n   double q = n; n   if (n % 2 == 0) return 2; n   for (int i = 3; i < sqrt(q) + 1; i = i + 2) { n     if (n % i == 0) return i; n   } n   return 1; n } n int main() { n   int n; n   cin >> n; n   long int s; n   s = n; n   int de = n; n   while (n != 1) { n     de = del(n); n     n = n / de; n     if (de == 1) { n       s++; n       cout << s; n       return 0; n     }; n     s += n; n   } n   cout << s; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   long long int n, m; n   cin >> n >> m; n   if (m == 0) { n     cout << 0; n     return 0; n   } n   vector<vector<long long int>> v[n + 1]; n   long long int in[n + 1]; n   memset(in, 0, sizeof(in)); n   for (long long int i = 0; i < m; i++) { n     long long int x, y, z; n     cin >> x >> y >> z; n     v[x].push_back({y, z}); n     in[y] += 1; n   } n   queue<vector<long long int>> q; n   for (long long int i = 1; i <= n; i++) { n     if (in[i] == 0 && v[i].size()) { n       q.push({i, INT_MAX, i}); n     } n   } n   vector<vector<long long int>> ans; n   while (q.size()) { n     vector<long long int> p = q.front(); n     q.pop(); n     if (p.size() && v[p[0]].size() == 0) { n       ans.push_back({p[2], p[0], p[1]}); n     } n     for (auto x : v[p[0]]) { n       in[x[0]] -= 1; n       if (in[x[0]] == 0) { n         q.push({x[0], min(p[1], x[1]), p[2]}); n       } n     } n   } n   cout << ans.size() <<   n ; n   sort(ans.begin(), ans.end()); n   for (auto x : ans) { n     for (auto y : x) cout << y <<    ; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.sync_with_stdio(false); n   cin.tie(nullptr); n   long long int n; n   cin >> n; n   bool prime = true; n   for (long long int i = 2; i < n && i < 101010; ++i) { n     if (n % i == 0) { n       prime = false; n     } n   } n   if (prime) { n     cout <<  1 n ; n     return 0; n   } n   if (n % 2 == 0) { n     cout <<  2 n ; n     return 0; n   } n   bool prime2 = true; n   long long int n2 = n - 2; n   for (long long int i = 2; i < n2 && i < 101010; ++i) { n     if (n2 % i == 0) { n       prime2 = false; n     } n   } n   if (prime2) { n     cout <<  2 n ; n     return 0; n   } n   cout <<  3 n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 13, M = 1e5 + 13; n inline int qread() { n   int res = 0, flag = 1; n   char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) flag = -1; n     c = getchar(); n   } n   while (isdigit(c)) res = res * 10 + (c -  0 ), c = getchar(); n   return res * flag; n } n int n, m, a[N]; n inline int Abs(const int &x) { return x < 0 ? -x : x; } n inline long long min(const long long &x, const long long &y) { n   return x < y ? x : y; n } n inline int max(const int &x, const int &y) { return x > y ? x : y; } n struct BIT { n   int t[M]; n   inline int lowbit(int x) { return x & -x; } n   inline void add(int x, int y) { n     while (x <= m) t[x] += y, x += lowbit(x); n   } n   inline int sum(int x) { n     int res = 0; n     while (x) res += t[x], x -= lowbit(x); n     return res; n   } n } T1, T2, T3; n int main() { n   n = qread(); n   for (int i = 1, x; i <= n; ++i) { n     x = qread(); n     a[i] = Abs(x); n     ++a[i]; n     m = max(m, a[i]); n   } n   long long sum = 0; n   for (int i = n; i >= 1; --i) { n     sum += T1.sum(a[i] - 1); n     T1.add(a[i], 1); n   } n   long long ans = sum, tot = 0; n   for (int i = 1; i <= n; ++i) { n     if (T2.sum(a[i] - 1) >= T1.sum(a[i] - 1) - T3.sum(a[i])) { n       T1.add(a[i], -1); n       T3.add(a[i], 1); n       ++tot; n     } else { n       sum -= T1.sum(a[i] - 1) + tot - T3.sum(a[i]); n       T1.add(a[i], -1); n       sum += T2.sum(a[i] - 1) + tot; n       T2.add(a[i], 1); n       ans = min(ans, sum); n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n void manupulate(string &str) { n   for (long long int i = 0; i < str.size(); ++i) str[i] -= 48; n } n bool isprime(long long int n) { n   if (n <= 1) return false; n   for (long long int i = 2; i * i <= n; ++i) n     if (n % i == 0) return false; n   return true; n } n long long int expo(long long int a, long long int n) { n   if (n == 0) return 1; n   if (n == 1) return a; n   long long int tmp = expo(a, n / 2); n   tmp = (tmp * tmp) % (long)(1e9 + 7); n   if (n & 1) tmp = (tmp * a) % (long)(1e9 + 7); n   return tmp; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     ; n     long long int k; n     cin >> k; n     ; n     long long int ar[n], br[n]; n     for (long long int i = 0; i < n; ++i) cin >> ar[i]; n     ; n     for (long long int i = 0; i < n; ++i) cin >> br[i]; n     ; n     sort(ar, ar + n); n     sort(br, br + n, greater<long long int>()); n     long long int ans = 0; n     long long int x = 0; n     for (long long int i = 0; i < k; ++i) { n       if (br[i] > ar[i]) { n         ans += br[i]; n         ++x; n       } n     } n     for (long long int i = x; i < n; ++i) ans += ar[i]; n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n string i2s(T x) { n   ostringstream o; n   o << x; n   return o.str(); n } n int n, wt[100001], root; n bool visited[100001]; n pair<long long, long long> mem[100001]; n vector<int> adj[100001]; n pair<long long, long long> go(int u) { n   if (visited[u]) return mem[u]; n   visited[u] = true; n   pair<long long, long long>& ret = mem[u]; n   wt[u]--; n   vector<pair<long long, long long> > tmp; n   for (int i = 0; i < adj[u].size(); ++i) { n     int v = adj[u][i]; n     if (!visited[v]) { n       tmp.push_back(go(v)); n     } n   } n   sort(tmp.begin(), tmp.end(), greater<pair<long long, long long> >()); n   long long l = 0, r = wt[u]; n   for (int i = 0; i < tmp.size() && i < wt[u]; ++i) { n     l += tmp[i].first + 2; n     r--; n   } n   if (r > 0) { n     for (int i = 0; i < tmp.size() && r != 0; ++i) { n       if (r >= tmp[i].second) { n         l += tmp[i].second * 2; n         r -= tmp[i].second; n       } else { n         l += r * 2; n         r = 0; n       } n     } n   } n   ret = make_pair(l, r); n   return ret; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     visited[i] = false; n     scanf( %d , wt + i); n   } n   for (int i = 0; i < n - 1; ++i) { n     int a, b; n     scanf( %d %d , &a, &b); n     a--, b--; n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   scanf( %d , &root); n   wt[--root]++; n   printf( %I64d n , go(root).first); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int iINF = 0x3f3f3f3f; n const long long lINF = 0x3f3f3f3f3f3f3f3f; n inline void read(long long& x) { n   int f = 1; n   char c; n   while (((c = getchar()) <  0  || c >  9 ) && c !=  - ) n     ; n   c ==  -  ? (f = -1, x = 0) : (x = c -  0 ); n   while ((c = getchar()) >=  0  && c <=  9 ) x = x * 10 + c -  0 ; n   x *= f; n } n inline void read(int& x) { n   long long t; n   read(t); n   x = t; n } n const int MAXN = 1e5 + 10; n int n, k, m, t; n int main() { n   cin >> n >> k >> m >> t; n   while (t--) { n     int opt, pos; n     cin >> opt >> pos; n     if (opt) { n       if (pos <= k) k++; n       n++; n     } else { n       if (pos < k) { n         k -= pos; n         n -= pos; n       } else { n         n -= n - pos; n       } n     } n     cout << n <<     << k << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double dot(complex<double> a, complex<double> b) { return real(conj(a) * b); } n double calcspeed(complex<double> p1, complex<double> p2, complex<double> w, n                  double vmax) { n   complex<double> r = p2 - p1; n   double k = n       dot(r, w) + sqrt(pow(dot(r, w), 2) - norm(r) * (norm(w) - vmax * vmax)); n   k /= abs(r); n   return k; n } n double calc(complex<double> p1, complex<double> p2, complex<double> w, n             double vmax) { n   double speed = calcspeed(p1, p2, w, vmax); n   return abs(p2 - p1) / speed; n } n int main() { n   int x1, y1, x2, y2; n   cin >> x1 >> y1 >> x2 >> y2; n   double vmax, t; n   cin >> vmax >> t; n   int vx, vy, wx, wy; n   cin >> vx >> vy >> wx >> wy; n   if (x1 == x2 && y1 == y2) { n     cout << 0 << endl; n     return 0; n   } n   complex<double> r(x2, y2), v(vx, vy), w(wx, wy); n   double ret = calc(complex<double>(x1, y1), complex<double>(x2, y2), v, vmax); n   if (ret < t) { n     printf( %.20f n , ret); n     return 0; n   } n   r -= complex<double>(x1, y1); n   v /= r; n   w /= r; n   vmax /= abs(r); n   r /= r; n   const double pi = acos(-1); n   double low = -pi + 1e-6; n   double high = pi - 1e-6; n   double f = 0; n   for (int i = (0); i < (200); i++) { n     double mid1 = (low * 2 + high) / 3; n     double mid2 = (low + high * 2) / 3; n     complex<double> target1 = polar(200.0, mid1); n     complex<double> target2 = polar(200.0, mid2); n     complex<double> speed1 = calcspeed(complex<double>(0, 0), target1, v, vmax); n     complex<double> speed2 = calcspeed(complex<double>(0, 0), target2, v, vmax); n     complex<double> point1 = speed1 * target1 / abs(target1) * t; n     complex<double> point2 = speed2 * target2 / abs(target2) * t; n     double f1 = calc(point1, r, w, vmax); n     double f2 = calc(point2, r, w, vmax); n     if (f1 > f2) { n       low = mid1; n       f = f1; n     } else { n       high = mid2; n       f = f2; n     } n   } n   printf( %.20f n , f + t); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1e6 + 5, P = 1e9 + 7; n long long n, k, m, res, l, T, ans, mi; n int dp[2][M], A[M], B[M], sum[M]; n int main() { n   cin >> n >> l >> k; n   for (int i = 1; i <= n; i++) { n     scanf( %d , A + i); n     B[i] = A[i]; n   } n   sort(B + 1, B + 1 + n); n   m = unique(B + 1, B + 1 + n) - B - 1; n   for (int i = 1; i <= n; i++) A[i] = lower_bound(B + 1, B + 1 + m, A[i]) - B; n   T = l / n; n   res = l % n; n   for (int i = 1; i <= n; i++) dp[1][A[i]]++; n   ans = l % P; n   for (int i = 2; i <= m; i++) dp[1][i] += dp[1][i - 1]; n   for (int i = 1; i <= m; i++) sum[i] += dp[1][i]; n   for (int lop = 2; lop <= min(1ll * k, T); lop++) { n     int c = lop & 1; n     for (int i = 1; i <= m; i++) dp[c][i] = 0; n     for (int i = 1; i <= n; i++) n       dp[c][A[i]] = (1ll * (dp[c][A[i]] % P) + 1ll * (dp[!c][A[i]] % P)) % P; n     for (int i = 2; i <= m; i++) n       dp[c][i] = (1ll * (dp[c][i] % P) + 1ll * (dp[c][i - 1] % P)) % P; n     if (lop < k) n       for (int i = 1; i <= m; i++) n         sum[i] = (1ll * (sum[i] % P) + 1ll * (dp[c][i] % P)) % P; n     long long len = (T - lop + 1) % P; n     ans = (1ll * (ans % P) + 1ll * (len * dp[c][m] % P)) % P; n   } n   if (k >= 2 && T >= 1) n     for (int i = 1; i <= res; i++) n       ans = (1ll * (ans % P) + 1ll * (sum[A[i]] % P)) % P; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int N, M[55][55], C[55]; n int main() { n   cin >> N; n   for (int i = 1; i <= N * (N - 1) / 2 - 1; ++i) { n     int a, b; n     cin >> a >> b; n     M[a][b] = M[b][a] = 1; n     C[a]++; n   } n   for (int i = 1; i <= N; ++i) n     for (int j = i + 1; j <= N; ++j) n       if (!M[i][j]) { n         if (C[i] > C[j]) n           cout << i <<     << j <<   n ; n         else n           cout << j <<     << i <<   n ; n       } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct line { n   int x, l; n } c[105]; n bool cmp(line a, line b) { return a.x < b.x; } n int f[105][105][2]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d%d , &c[i].x, &c[i].l); n   sort(c + 1, c + n + 1, cmp); n   c[0].x = -1e9; n   int ans = 0; n   for (int i = 0; i <= n; i++) n     for (int j = 0; j <= i; j++) n       for (int p1 = 0; p1 <= 1; p1++) { n         ans = max(ans, f[i][j][p1]); n         int t = c[j].x + p1 * c[j].l, mx = -1e9, id = 0, y = 0; n         for (int k = i + 1; k <= n; k++) n           for (int p2 = 0; p2 <= 1; p2++) { n             int o = c[k].x + p2 * c[k].l; n             if (o > mx) mx = o, id = k, y = p2; n             f[k][id][y] = n                 max(f[k][id][y], f[i][j][p1] + min(o - t, c[k].l) + mx - o); n           } n       } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int m, n; n   cin >> m >> n; n   vector<string> nums(n); n   for (int i = 0; i < (int)(n); ++i) cin >> nums[i]; n   vector<long long> c(m, 1); n   for (int i = 0; i < (int)(m); ++i) { n     for (int j = 0; j < (int)(i); ++j) { n       int match = 1; n       for (int k = 0; k < (int)(n); ++k) { n         if (nums[k][i] != nums[k][j]) { n           match = 0; n           break; n         } n       } n       if (match) { n         c[i] = 0; n         c[j]++; n         break; n       } n     } n   } n   vector<vector<long long> > choose(m + 1, vector<long long>(m + 1, 0)); n   for (int i = 0; i < (int)(m + 1); ++i) { n     choose[i][0] = 1; n     choose[i][i] = 1; n     for (int j = 0; j < (int)(m + 1); ++j) { n       if (i && j) n         choose[i][j] = (choose[i - 1][j] + choose[i - 1][j - 1]) % 1000000007; n     } n   } n   vector<long long> dp(m + 1); n   dp[0] = 1; n   for (int i = 0; i < (int)(m + 1); ++i) { n     if (i) { n       for (int j = 0; j < (int)(i); ++j) { n         dp[i] += (choose[i - 1][j] * dp[i - 1 - j]) % 1000000007; n         dp[i] %= 1000000007; n       } n     } n   } n   long long ans = 1; n   for (int i = 0; i < (int)(m); ++i) { n     if (c[i]) ans = (ans * dp[c[i]]) % 1000000007; n   } n   cout << ans << endl; n   return 0; n } n