#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n const int INF = 1 << 30; n const long long LINF = 1ll << 60; n const int BUFFER_SZ = 1 << 15; n const int MOD = 1000 * 1000 * 1000 + 7; n char BUFFER[BUFFER_SZ]; n int gi() { n   int x; n   scanf( %d , &x); n   return x; n } n double gd() { n   double x; n   scanf( %lf , &x); n   return x; n } n long long gll() { n   long long x; n   cin >> x; n   return x; n } n vector<int> gvi(int n) { n   vector<int> a; n   while (n-- > 0) a.push_back(gi()); n   return a; n } n string gs() { n   scanf( %s , BUFFER); n   return string(BUFFER); n } n template <class T> n void print(vector<T> &x, string format) { n   format +=  %c ; n   for (int i = 0; i < x.size(); ++i) n     printf(format.c_str(), x[i], i + 1 == x.size() ?   n  :    ); n } n int c_ins, c_del, c_rep, c_swp; n int dp[4444][4444]; n int main() { n   int c_ins = gi(); n   int c_del = gi(); n   int c_rep = gi(); n   int c_swp = gi(); n   string a = gs(); n   string b = gs(); n   int prev[256]; n   memset((prev), (0), sizeof(prev)); n   for (int i = 0; i <= a.size(); ++i) { n     int last = 0; n     for (int j = 0; j <= b.size(); ++j) { n       if (!i) n         dp[i][j] = j * c_ins; n       else if (!j) n         dp[i][j] = i * c_del; n       else { n         dp[i][j] = 1 << 30; n         int ii = j > 0 ? prev[b[j - 1]] : 0; n         int jj = last; n         if (a[i - 1] == b[j - 1]) { n           dp[i][j] = dp[i - 1][j - 1]; n           last = j; n         } n         int v = min(min(dp[i - 1][j] + c_del, dp[i][j - 1] + c_ins), n                     dp[i - 1][j - 1] + c_rep); n         if (i && j && ii && jj) { n           v = min(v, dp[ii - 1][jj - 1] + c_swp + c_del * (i - ii - 1) + n                          c_ins * (j - jj - 1)); n         } n         dp[i][j] = min(dp[i][j], v); n       } n     } n     if (i > 0) { n       prev[a[i - 1]] = i; n     } n   } n   printf( %d n , dp[a.size()][b.size()]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long int; n using P = pair<ll, ll>; n const int INF = 1001001001; n const ll mod = 998244353; n const ll INFL = 1000000001000000000; n int M = 100005; n int main() { n   int n, q; n   cin >> n >> q; n   vector<ll> v(n); n   for (int i = 0; i < (n); i++) cin >> v[i]; n   vector<int> c(n); n   for (int i = 0; i < (n); i++) cin >> c[i]; n   for (int i = 0; i < (n); i++) c[i]--; n   while (q--) { n     ll a, b; n     cin >> a >> b; n     vector<ll> dp(M, -INFL); n     ll bigM1 = 0, bigM2 = 0; n     int bigC1 = -1, bigC2 = -1; n     for (int i = 0; i < (n); i++) { n       if (c[i] == bigC1) { n         ll nM = max(dp[c[i]] + v[i] * a, bigM2 + v[i] * b); n         dp[c[i]] = max(dp[c[i]], nM); n         if (bigM1 < nM) { n           bigM1 = nM; n           bigC1 = c[i]; n         } n       } else { n         ll nM = max(dp[c[i]] + v[i] * a, bigM1 + v[i] * b); n         dp[c[i]] = max(dp[c[i]], nM); n         if (bigM2 < nM) { n           bigM2 = nM; n           bigC2 = c[i]; n         } n         if (bigM1 < bigM2) { n           swap(bigM1, bigM2); n           swap(bigC1, bigC2); n         } n       } n     } n     ll ans = 0; n     for (int i = 0; i < (M); i++) ans = max(ans, dp[i]); n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (a == 0) n     return b; n   else if (b == 0) n     return a; n   else { n     a = a % b; n     return gcd(b, a); n   } n } n int main() { n   int n; n   int sum = 0, odd = 0; n   scanf( %d , &n); n   int t; n   for (int i = 0; i < n; i++) { n     scanf( %d , &t); n     sum += t; n     if (t % 2) odd++; n   } n   if (sum % 2) n     cout << odd; n   else n     cout << n - odd; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   char arr1[n][m]; n   char arr2[m][n]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       char a; n       cin >> a; n       arr1[i][j] = a; n       arr2[j][i] = a; n     } n   } n   string ans =   ; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (count(arr1[i], arr1[i] + m, arr1[i][j]) == 1 && n           count(arr2[j], arr2[j] + n, arr1[i][j]) == 1) { n         ans += string(1, arr1[i][j]); n       } n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n static const int INF = std::numeric_limits<int>::max(); n int main() { n   int n; n   std::cin >> n; n   std::vector<int> a(n + 1), b(n + 1); n   for (int i = 1; i <= n; ++i) { n     std::cin >> a[i]; n   } n   for (int i = 1; i <= n; ++i) { n     std::cin >> b[i]; n   } n   std::vector<int> res(n + 1); n   for (int i = 1; i <= n; ++i) { n     res[a[i]] = b[i]; n   } n   for (int i = 1; i <= n; ++i) { n     std::cout << res[i] <<    ; n   } n   std::cout << std::endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n char c[109][109]; n bool ja = 1; n bool useless = 1; n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> c[i][j]; n       if (c[i][j] ==  . ) { n         if (i % 2 == 0 && j % 2 == 0 || i % 2 == 1 && j % 2 == 1) { n           c[i][j] =  B ; n         } else { n           c[i][j] =  W ; n         } n       } n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cout << c[i][j]; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e2 + 10; n int main() { n   mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, i, in, last, ans = 0, prelast = -1; n   cin >> n; n   cin >> last; n   for (i = 1; i < n; i++) { n     cin >> in; n     if (last == 1 && in == 2) { n       ans += 3; n       if (prelast == 3) ans--; n     } else if (last == 1 && in == 3) n       ans += 4; n     else if (last == 2 && in == 1) n       ans += 3; n     else if (last == 2 && in == 3) { n       cout <<  Infinite n ; n       return 0; n     } else if (last == 3 && in == 2) { n       cout <<  Infinite n ; n       return 0; n     } else if (last == 3 && in == 1) n       ans += 4; n     prelast = last; n     last = in; n   } n   cout <<  Finite n ; n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const long long e = 1e5 + 110; n bool zs[e]; n long long num[e]; n void sai() { n   for (long long i = 2; i <= e - 99; i++) { n     if (zs[i]) continue; n     for (long long j = i + i; j <= e - 99; j += i) { n       zs[j] = true; n     } n   } n   for (long long tot = 0, i = 2; i <= e - 99; i++) { n     if (!zs[i]) tot++, num[tot] = i; n   } n } n struct fengjie { n   long long v, t; n } f[e]; n long long cnf = 0; n void fj(long long x) { n   long long tmp = x; n   for (long long i = 2; i <= x; i++) { n     long long cnt = 0; n     while (tmp % i == 0) { n       tmp /= i; n       cnt++; n     } n     if (cnt == 0) continue; n     cnf++; n     f[cnf].v = i, f[cnf].t = cnt; n   } n   return; n } n long long ksm(long long a, long long z) { n   long long ret = 1; n   if (z == 0) return 1; n   while (z != 1) { n     if (z % 2 == 1) { n       ret *= a; n       a *= a; n     } else { n       a *= a; n     } n     z /= 2; n   } n   return a * ret; n } n fengjie an[e]; n signed main() { n   long long n; n   cin >> n; n   sai(); n   fj(n); n   long long ans = 1, g = 0, cna = 0, minn; n   for (long long i = cnf; i > 0; i--) { n     for (long long j = 1; j <= f[i].t; j++) { n       if (cna == 0) { n         g++; n         ans *= ksm(num[g], f[i].v - 1); n         an[++cna].v = num[g]; n         an[cna].t = f[i].v - 1; n         continue; n       } n       long long pl = -1; n       minn = ans * ksm(num[g + 1], f[i].v - 1); n       for (long long l = 1; l <= cna; l++) { n         long long ct = f[i].v * (an[l].t + 1) - 1; n         long long tmp = ans / ksm(an[l].v, an[l].t); n         tmp *= ksm(an[l].v, ct); n         if (tmp < ans or ksm(an[l].v, ct) < an[l].v) continue; n         if (tmp < minn) { n           pl = l; n           minn = tmp; n         } n       } n       if (pl == -1) { n         g++; n         ans *= ksm(num[g], f[i].v - 1); n         an[++cna].v = num[g]; n         an[cna].t = f[i].v - 1; n       } n       if (pl != -1) { n         long long tmp = ans / ksm(an[pl].v, an[pl].t); n         long long ct = f[i].v * (an[pl].t + 1) - 1; n         ans = tmp * ksm(an[pl].v, ct); n       } n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n stack<int> s; n priority_queue<int> pq; n long long maxm = numeric_limits<long long>::max(); n const int maxn = 10005; n struct node { n   int l, r; n } q[1001]; n bool cmp(node a, node b) { return a.l < b.l; } n int main() { n   int n, sum = 0; n   scanf( %d , &n); n   int a[maxn], l[maxn], r[maxn]; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     sum += a[i]; n   } n   int m, tmp; n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &q[i].l, &q[i].r); n   } n   sort(q + 1, q + 1 + m, cmp); n   if (sum > q[m].r) n     printf( -1 n ); n   else if (sum < q[1].l) n     printf( %d n , q[1].l); n   else { n     for (int i = 1; i <= m; i++) { n       if (sum > q[i].r && sum < q[i + 1].l) { n         printf( %d n , q[i + 1].l); n         break; n       } else if (sum >= q[i].l && sum <= q[i].r) { n         printf( %d n , sum); n         break; n       } 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   bool b[4][n][n]; n   for (int i = 0; i < 4; i++) { n     for (int j = 0; j < n; j++) { n       char c[n + 1]; n       scanf( %s , &c); n       for (int k = 0; k < n; k++) b[i][j][k] = c[k] ==  1  ? 1 : 0; n     } n   } n   int s[] = {0, 1, 2, 3}, mi = 1e9, co = 0; n   bool o = 0; n   do { n     for (int k = 0; k < 4; k += 2) { n       for (int j = 0; j < n; j++) { n         for (int i = 0; i < n; i++) { n           if (b[s[k]][j][i] != o) co++; n           o ^= 1; n         } n         for (int i = 0; i < n; i++) { n           if (b[s[k + 1]][j][i] != o) co++; n           o ^= 1; n         } n         o ^= 1; n       } n     } n     mi = min(mi, co); n     co = 0; n   } while (next_permutation(s, s + 4)); n   cout << mi <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, M; n int A[50050], X[50500]; n int L[50050], R[50500]; n int XX[1010101]; n int ma[50505]; n int ret[50505]; n signed long long xorxor(signed long long val) { n   int i; n   signed long long ret = ((val + 1) / 2) % 2; n   for (i = 0; i <= 29; i++) n     if ((val >> i) % 2) ret |= (1 ^ (val % 2)) << i; n   return ret; n } n void solve() { n   int i, j, k, l, r, x, y; n   string s; n   for (i = 1; i <= 1000000; i++) XX[i] = XX[i - 1] ^ i; n   cin >> N >> M; n   for (i = 0; i < (N); i++) cin >> A[i], X[i] = XX[A[i]]; n   for (i = 0; i < (M); i++) cin >> L[i] >> R[i], L[i]--, R[i]--; n   for (x = 0; x < (N); x++) { n     ma[x - 1] = 0; n     for (y = x; y < N; y++) n       ma[y] = max(ma[y - 1], X[x] ^ X[y] ^ ((A[x] > A[y]) ? A[y] : A[x])); n     for (i = 0; i < (M); i++) n       if (L[i] <= x) ret[i] = max(ret[i], ma[R[i]]); n   } n   for (i = 0; i < (M); i++) cout << ret[i] << endl; n } n int main(int argc, char** argv) { n   string s; n   int i; n   if (argc == 1) ios::sync_with_stdio(false); n   for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s +=   n ; n   for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string a; n int f[1005][5]; n int main() { n   cin >> n >> a; n   string t =   , s =   ; n   char cur =  0 ; n   for (int i = 0; i <= n - 1; i++) { n     t += cur; n     s +=  1  - cur +  0 ; n     cur =  1  - cur +  0 ; n   } n   int ans = (int)2e9; n   int res = 0, res1 = 0; n   for (int i = 0; i <= n - 1; i++) { n     if (a[i] != s[i]) res++; n     if (a[i] != t[i]) res1++; n   } n   ans = min(res1, res); n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k; n string s; n int MOD = 1e9 + 7; n const int INF = 1e9; n const long long INF64 = 2e18; n const double PI = 3.141592653589793238463; n long long ar[1000004], ar1[1000004], vis[1000004], pre[1000004], dp[3004][3004]; n vector<long long> gr[1000004], path; n set<long long> myset; n pair<long long, long long> seg[1000004]; n map<long long, long long> mymap; n long long c = 0; n string st1[1000004], st2[1000004]; n vector<long long> arr[1000004]; n int comp(pair<long long, long long> a, pair<long long, long long> b) { n   return a.second < b.second; n } n int gcd(int a, int b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(nullptr); n   cin >> n; n   double w, cargo; n   cin >> w; n   for (int i = 0; i < int(n); i++) cin >> ar[i]; n   for (int i = 0; i < int(n); i++) cin >> ar1[i]; n   if (ar[0] == 1 || ar1[0] == 1) return cout << -1, 0; n   cargo = w; n   w += w / (ar1[0] - 1); n   for (int i = n - 1; i > 0; i--) { n     if (ar[i] == 1 || ar1[i] == 1) return cout << -1, 0; n     w += w / (ar[i] - 1); n     w += w / (ar1[i] - 1); n   } n   w += w / (ar[0] - 1); n   return printf( %10.10f , w - cargo), 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 7005; n const int M = 200000; n struct state { n   int l, r, val; n   state() {} n   state(int l, int r, int val) { n     this->l = l; n     this->r = r; n     this->val = val; n   } n }; n int n, m, dl[M], dr[M]; n vector<state> v[N]; n int solve(int d, int x, int y) { n   set<int> ans; n   for (int i = d; i <= n; i++) { n     for (int j = 0; j < v[i].size(); j++) { n       state& c = v[i][j]; n       if (c.l <= y && c.r >= x) ans.insert(c.val); n     } n     x = (dl[x] == -1 ? dr[x] : dl[x]); n     y = dr[y]; n   } n   return ans.size(); n } n void init() { n   int cnt = 1, p = 3; n   memset(dl, -1, sizeof(dl)); n   memset(dr, -1, sizeof(dr)); n   dl[1] = 1; n   dr[1] = 2; n   for (int i = 2; i < M; i++) { n     if ((1 << cnt) == i) { n       cnt++; n       dl[i] = p++; n     } n     dr[i] = p++; n   } n } n int main() { n   init(); n   int order, d, x, y, val; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     scanf( %d , &order); n     if (order == 1) { n       scanf( %d%d%d%d , &d, &x, &y, &val); n       v[d].push_back(state(x, y, val)); n     } else { n       scanf( %d%d , &d, &x); n       printf( %d n , solve(d, x, x)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long modpow(long long base, long long exp, long long modulus) { n   base %= modulus; n   long long result = 1; n   while (exp > 0) { n     if (exp & 1) result = (result * base) % modulus; n     base = (base * base) % modulus; n     exp >>= 1; n   } n   return result; n } n int main() { n   long long n, k; n   cin >> n >> k; n   long long w[100001]; n   long long q[100001]; n   memset(q, 0, sizeof(q)); n   memset(w, 0, sizeof(w)); n   w[0] = 1; n   if (1 >= k) n     w[1] = 2; n   else n     w[1] = 1; n   q[0] = 0; n   q[1] += q[0] + w[1]; n   for (long long i = 2; i < 100001; i++) { n     if (k <= i) { n       w[i] += (w[i - 1] + w[i - k] - 1000000007) % 1000000007; n     } else n       w[i] = 1; n     q[i] += (q[i - 1] + w[i] - 1000000007) % 1000000007; n     q[i] %= 1000000007; n   } n   while (n--) { n     long long a, b; n     cin >> a >> b; n     cout << (q[b] - q[a - 1] + 1000000007) % 1000000007 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n const long long inf = 0x3f3f3f3f; n using namespace std; n const long long maxn = 2e5 + 100; n long long n, k; n long long a[maxn]; n int32_t main() { n   long long q; n   scanf( %lld , &(q)); n   while (q--) { n     scanf( %lld%lld , &(n), &(k)); n     long long maxx = 0, minn = 0x3f3f3f3f; n     for (long long i = (1); i <= (n); i++) { n       scanf( %lld , &(a[i])); n       if (maxx < a[i]) maxx = a[i]; n       if (minn > a[i]) minn = a[i]; n     } n     long long x = minn + k; n     if (abs(x - maxx) > k) n       printf( -1 ); n     else n       printf( %lld , (x)); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   char c = getchar(); n   int x = 0, f = 1; n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   return x * f; n } n const int maxn = 1e6 + 10; n const int inf = 2147483647; n int main() { n   vector<int> v, prim; n   vector<bool> isprim; n   int lim = sqrt(1.0 * 1000000000) + 10; n   isprim.resize(lim); n   isprim[0] = isprim[1] = 1; n   for (int i = 2; i <= lim; ++i) { n     if (isprim[i] == false) { n       for (int j = i + i; j <= lim; j = j + i) isprim[j] = true; n     } n   } n   for (int i = 2; i <= lim; ++i) { n     if (isprim[i] == false) prim.push_back(i); n   } n   int sz = prim.size(); n   set<int> se; n   set<int>::iterator it; n   int T; n   scanf( %d , &T); n   while (T--) { n     int n = read(); n     bool flag = false; n     se.clear(); n     for (int i = 2; i * i <= n; ++i) { n       if (n % i == 0 && isprim[i] == false) { n         se.insert(i); n         n /= i; n         flag = true; n         break; n       } n     } n     if (!flag) { n       puts( NO ); n       continue; n     } n     flag = false; n     for (int i = sqrt(n); i >= 2; i--) { n       if (n % i == 0 && se.count(i) == 0 && se.count(n / i) == 0 && n           i != n / i) { n         flag = true; n         se.insert(i); n         se.insert(n / i); n         break; n       } n     } n     if (!flag) { n       puts( NO ); n       continue; n     } n     puts( YES ); n     for (it = se.begin(); it != se.end(); ++it) { n       printf( %d  , *it); n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n int max(int a, int b) { return a < b ? b : a; } n int min(int a, int b) { return a < b ? a : b; } n void swap(int *a, int *b) { n   int t = *a; n   *a = *b; n   *b = t; n } n int can(int x, int y, int a, int b) { n   if ((x <= a && y <= b) || (x <= b && y <= a)) return 1; n   return 0; n } n int cal(int x1, int y1, int x2, int y2, int a, int b) { n   if (x1 > y1) swap(&x1, &y1); n   if (x2 > y2) swap(&x2, &y2); n   if (can(x1 + x2, max(y1, y2), a, b)) return x1 * y1 + x2 * y2; n   if (can(max(x1, x2), y1 + y2, a, b)) return x1 * y1 + x2 * y2; n   if (can(max(x2, y1), x1 + y2, a, b)) return x1 * y1 + x2 * y2; n   if (can(max(x1, y2), y1 + x2, a, b)) return x1 * y1 + x2 * y2; n   return 0; n } n int main() { n   int maxx = 0, n, a, b, i, j, s[200][2]; n   scanf( %d %d %d , &n, &a, &b); n   for (i = 0; i < n; i++) scanf( %d %d , &s[i][0], &s[i][1]); n   for (i = 0; i < n; i++) { n     for (j = i + 1; j < n; j++) { n       int k = cal(s[i][0], s[i][1], s[j][0], s[j][1], a, b); n       if (maxx < k) maxx = k; n     } n   } n   printf( %d n , maxx); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300000; n int x[N][2]; n int ans[N]; n bool hear[N]; n vector<pair<int, int> > G[N]; n int color[N]; n void dfs1(int s) { n   color[s] = 1; n   for (int i = 0; i < (int)G[s].size(); i++) { n     int v = G[s][i].first; n     if (color[v] == 0) { n       if (G[s][i].second > 0) { n         ans[G[s][i].second] = 1; n       } else { n         ans[-G[s][i].second] = 0; n       } n       dfs1(v); n     } n   } n   color[s] = 2; n } n pair<int, int> cyc; n void dfs2(int s, int per = -1) { n   color[s] = 1; n   for (int i = 0; i < (int)G[s].size(); i++) { n     int v = G[s][i].first; n     if (color[v] == 0) { n       dfs2(v, abs(G[s][i].second)); n     } n     if ((color[v] == 1) && (per != abs(G[s][i].second))) { n       cyc.first = s; n       cyc.second = i; n     } n   } n   color[s] = 2; n } n int color1[N]; n void dfs3(int s, int per) { n   color1[s] = 1; n   for (int i = 0; i < (int)G[s].size(); i++) { n     int v = G[s][i].first; n     if (abs(G[s][i].second) == per) { n       continue; n     } n     if (color1[v] == 0) { n       ans[abs(G[s][i].second)] = (G[s][i].second > 0 ? 1 : 0); n       dfs3(v, abs(G[s][i].second)); n     } n   } n   color1[s] = 2; n } n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     int k; n     scanf( %d , &k); n     for (int j = 0; j < k; j++) { n       int r; n       scanf( %d , &r); n       if (x[abs(r)][0] == 0) { n         x[abs(r)][0] = i * (r > 0 ? 1 : -1); n       } else { n         x[abs(r)][1] = i * (r > 0 ? 1 : -1); n       } n     } n   } n   for (int i = 1; i <= m; i++) { n     if ((x[i][0] >= 0) && (x[i][1] >= 0)) { n       ans[i] = 1; n       hear[x[i][0]] = 1; n       hear[x[i][1]] = 1; n     } else if ((x[i][0] <= 0) && (x[i][1] <= 0)) { n       ans[i] = 0; n       hear[-x[i][0]] = 1; n       hear[-x[i][1]] = 1; n     } else { n       int v1 = abs(x[i][0]); n       int v2 = abs(x[i][1]); n       G[v1].push_back(make_pair(v2, i * (x[i][1] > 0 ? 1 : -1))); n       G[v2].push_back(make_pair(v1, i * (x[i][0] > 0 ? 1 : -1))); n     } n   } n   for (int i = 1; i <= n; i++) { n     if ((hear[i] == 1) && (color[i] == 0)) { n       dfs1(i); n     } n   } n   for (int i = 1; i <= n; i++) { n     if (color[i] == 0) { n       cyc.first = 0; n       dfs2(i); n       if (cyc.first == 0) { n         cout <<  NO  << endl; n         return 0; n       } else { n         int s = cyc.first; n         int j = cyc.second; n         ans[abs(G[s][j].second)] = (G[s][j].second > 0 ? 1 : 0); n         dfs3(G[s][j].first, abs(G[s][j].second)); n       } n     } n   } n   cout <<  YES  << endl; n   for (int i = 1; i <= m; i++) { n     cout << ans[i]; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int bits = 10; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   vector<pair<string, int> > commands; n   for (int i = 0; i < n; ++i) { n     string tp; n     int x; n     cin >> tp >> x; n     commands.emplace_back(tp, x); n   } n   int ansxor = 0; n   int ansand = (1 << bits) - 1; n   int ansor = 0; n   for (int bit = 0; bit < bits; ++bit) { n     vector<int> f = {0, 1}; n     for (int index = 0; index < 2; ++index) { n       for (int i = 0; i < n; ++i) { n         string tp; n         int x; n         tie(tp, x) = commands[i]; n         x = (x >> bit) & 1; n         if (tp ==  & ) f[index] &= x; n         if (tp ==  | ) f[index] |= x; n         if (tp ==  ^ ) f[index] ^= x; n       } n     } n     if (f == vector<int>{0, 0}) { n       ansand ^= 1 << bit; n     } else if (f == vector<int>{1, 0}) { n       ansxor ^= 1 << bit; n     } else if (f == vector<int>{1, 1}) { n       ansor ^= 1 << bit; n     } n   } n   cout << 3 << endl; n   cout <<  ^   << ansxor << endl; n   cout <<  &   << ansand << endl; n   cout <<  |   << ansor << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   vector<pair<int, int>> times; n   for (int i = 0; i < m; i++) { n     int s, e; n     cin >> s >> e; n     times.push_back(make_pair(s, e)); n   } n   sort(times.begin(), times.end(), [](pair<int, int> a, pair<int, int> b) { n     return (a.first == b.first) ? (a.second < b.second) : (a.first < b.first); n   }); n   vector<int> ans(n, 0); n   for (auto t : times) { n     for (int i = t.first + 1; i <= t.second; i++) { n       ans[i - 1] = !ans[i - 2]; n     } n   } n   for (auto a : ans) cout << a; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> adj[100001]; n long long vis[100001] = {0}; n long long n; n long long nodecolor[100001]; n long long treecolor[100001]; n long long cc[100001]; n map<long long, long long> colorcnt; n long long dfs(long long root, long long col) { n   long long cnt = 1; n   vis[root] = 1; n   treecolor[root] = nodecolor[root]; n   for (auto x : adj[root]) { n     if (vis[x] == 0 && nodecolor[x] != col) treecolor[root] = -1; n   } n   long long flag = 0; n   for (auto x : adj[root]) { n     if (vis[x] == 0) { n       flag = 1; n       cnt += dfs(x, nodecolor[x]); n       if (treecolor[x] != nodecolor[root]) treecolor[root] = -1; n     } n   } n   if (flag == 0) { n     treecolor[root] = nodecolor[root]; n   } n   return cc[root] = cnt; n } n long long fun(long long root) { n   vis[root] = 1; n   long long flag = 0; n   set<long long> st; n   long long parentcolor = -1; n   for (auto x : adj[root]) { n     if (vis[x] == 0) { n       if (treecolor[x] == -1) flag = 1; n     } else { n       parentcolor = nodecolor[x]; n     } n   } n   if (flag == 0) { n     if (parentcolor == -1) return root; n     for (auto x : adj[root]) { n       if (vis[x] == 0) { n         colorcnt[treecolor[x]] -= cc[x]; n       } n     } n     colorcnt[nodecolor[root]]--; n     if (colorcnt[parentcolor] == n - cc[root]) { n       return root; n     } n     for (auto x : adj[root]) { n       if (vis[x] == 0) { n         colorcnt[treecolor[x]] += cc[x]; n       } n     } n     colorcnt[nodecolor[root]]++; n   } n   long long ans = -1; n   for (auto x : adj[root]) { n     if (vis[x] == 0) { n       ans = fun(x); n       if (ans != -1) return ans; n     } n   } n   return ans; n } n void solve(long long cno) { n   long long m, i, j, k, l; n   cin >> n; n   long long flag = 0; n   for (i = 0; i < n - 1; i++) { n     cin >> j >> k; n     j--; n     k--; n     adj[j].push_back(k); n     adj[k].push_back(j); n   } n   for (i = 0; i < n; i++) { n     cin >> nodecolor[i]; n     colorcnt[nodecolor[i]]++; n   } n   long long temp = dfs(0, nodecolor[0]); n   for (i = 0; i < n; i++) vis[i] = 0; n   long long ans = -1; n   ans = fun(0); n   if (ans == -1) n     cout <<  NO ; n   else { n     cout <<  YES  <<   n  << ans + 1; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int TESTS = 1; n   long long i = 1; n   while (TESTS--) { n     solve(i); n     i++; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n long long dp[N], f[N], ok[N], p[N]; n vector<long long> node[N]; n long long cur, now; n void dfs(long long u, long long p) { n   for (auto j : node[u]) { n     if (j == p) continue; n     dfs(j, u); n     if (f[j]) { n       dp[u] += dp[j] + 1; n       f[u] = 1; n     } n   } n   if (ok[u]) f[u] = 1; n } n void find_dia(long long u, long long p, long long dis) { n   if (dis >= cur && ok[u]) { n     if (dis > cur) n       now = u; n     else n       now = min(now, u); n     cur = dis; n   } n   for (auto j : node[u]) { n     if (j == p) continue; n     find_dia(j, u, dis + 1); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   if (fopen( A.INP ,  r )) { n     freopen( A.INP ,  r , stdin); n     freopen( A.OUT ,  w , stdout); n   } n   long long n, m; n   cin >> n >> m; n   for (int i = 1; i < n; i++) { n     long long u, v; n     cin >> u >> v; n     node[u].push_back(v); n     node[v].push_back(u); n   } n   for (int i = 1; i <= m; i++) cin >> p[i], ok[p[i]] = 1; n   dfs(p[1], p[1]); n   long long ans = dp[p[1]] * 2; n   cur = 0; n   now = 1e9; n   find_dia(p[1], p[1], 0); n   cur = 0; n   long long f = now; n   now = 1e9; n   find_dia(f, f, 0); n   cout << min(f, now) <<   n ; n   cout << ans - cur; n } n


#include <bits/stdc++.h> n using namespace std; n long long Set(long long N, int pos) { return N = N | (1LL << pos); } n long long reset(long long N, int pos) { return N = N & ~(1LL << pos); } n bool check(long long N, int pos) { return (bool)(N & (1LL << pos)); } n struct node { n   bool endmark; n   node *next[2]; n   node() { n     endmark = 0; n     for (int i = 0; i < 2; i++) next[i] = NULL; n   } n } * root; n void insert(long long m) { n   node *cur = root; n   for (int i = 63; i >= 0; i--) { n     int id = check(m, i); n     if (cur->next[id] == NULL) cur->next[id] = new node(); n     cur = cur->next[id]; n   } n   cur->endmark = true; n } n long long query(long long m) { n   node *cur = root; n   long long ret = 0; n   for (int i = 63; i >= 0; i--) { n     int id = check(m, i); n     int a = 1 - id; n     if (cur->next[a] != NULL) { n       ret = Set(ret, i); n       cur = cur->next[a]; n     } else n       cur = cur->next[id]; n   } n   return ret; n } n long long ara[100005], lft[100005], rgt[100005]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   root = new node(); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> ara[i]; n   for (int i = 1; i <= n; i++) lft[i] = lft[i - 1] ^ ara[i]; n   for (int i = n; i >= 1; i--) rgt[i] = rgt[i + 1] ^ ara[i]; n   long long ans = 0; n   insert(0); n   for (int i = 1; i <= n; i++) { n     long long temp = query(rgt[i]); n     ans = max(ans, temp); n     insert(lft[i]); n   } n   ans = max(ans, query(0)); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string to_string(string c) { return string(c); } n string to_string(char ch) { return string(1, ch); } n template <size_t N> n string to_string(bitset<N> B) { n   string second =   ; n   for (long long i = 0; i < (long long)N; i++) second += (char) 0  + B[i]; n   return second; n } n template <typename T, typename U> n string to_string(pair<T, U> P) { n   return  {  + to_string(P.first) +  ,  + to_string(P.second) +  } ; n } n template <typename T> n string to_string(T A) { n   string second =  [ ; n   bool first = 0; n   for (auto x : A) { n     if (first) second +=    ; n     first = 1; n     second += to_string(x); n   } n   return second +  ] ; n } n template <typename T, size_t N> n string to_string(T (&A)[N]) { n   string second =  [ ; n   bool first = 0; n   for (long long i = 0; i < N; i++) { n     if (first) second +=    ; n     first = 1; n     second += to_string(A[i]); n   } n   return second +  ] ; n } n void _deb() { n   cerr <<  ]  n        <<   n ; n } n template <typename T, typename... U> n void _deb(const T& t, const U&... u) { n   cerr << to_string(t); n   if (sizeof...(u)) cout <<  ,  ; n   _deb(u...); n } n long long ceil(long long a, long long b) { n   return a / b + (a % b && (a ^ b) > 0); n } n long long floor(long long a, long long b) { n   return a / b - (a % b && (a ^ b) < 0); n } n long long SBITS(long long n) { return __builtin_popcountll(n); } n long long BITS(long long n) { return 63 - __builtin_clzll(n); } n const long long MX = 3e5 + 5; n const long long OO = 1e18; n const long long MOD = 998244353; n long long DX[8] = {1, 0, -1, 0, 1, -1, 1, -1}; n long long DY[8] = {0, 1, 0, -1, 1, -1, -1, 1}; n long long FAC[MX]; n long long FACINV[MX]; n long long nCr(long long n, long long r) { n   if (n < r) return 0; n   return FAC[n] * FACINV[r] % MOD * FACINV[n - r] % MOD; n } n long long fastExp(long long base, long long expo, long long mod) { n   if (expo == 0) return 1; n   if (expo == 1) return base % mod; n   long long t = fastExp(base, expo / 2, mod); n   t = (t * t) % mod; n   if (expo % 2 == 0) n     return t; n   else n     return ((base % mod) * t) % mod; n } n void calcFact() { n   FAC[1] = 1; n   for (long long i = 2; i < MX; i++) FAC[i] = i * FAC[i - 1] % MOD; n   FACINV[MX - 1] = fastExp(FAC[MX - 1], MOD - 2, MOD); n   for (long long i = MX - 1; i > 0; i--) FACINV[i - 1] = i * FACINV[i] % MOD; n } n void solve() { n   calcFact(); n   long long n, ans = 0; n   cin >> n; n   vector<long long> l(n), r(n); n   for (long long i = 0; i < n; i++) cin >> l[i]; n   for (long long i = 0; i < n; i++) cin >> r[i]; n   sort(l.begin(), l.end()); n   sort(r.begin(), r.end()); n   reverse(r.begin(), r.end()); n   for (long long i = 0; i < n; i++) ans += abs(l[i] - r[i]); n   ans %= MOD; n   ans *= nCr(2 * n, n); n   ans %= MOD; n   cout << ans <<   n ; n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n char s[maxn], t[maxn], st[maxn]; n long long cnts[maxn], cntt[maxn]; n long long Next[maxn]; n int n, m; n long long ans; n long long lent, lens; n long long f, e; n void init() { n   ans = 0; n   lent = lens = 0; n   s[0] = t[0] = 0; n   char c[2]; n   long long x; n   for (int i = 1; i <= n; i++) { n     scanf( %I64d-%s , &x, c); n     if (c[0] == s[lens]) n       cnts[lens] += x; n     else { n       cnts[++lens] = x; n       s[lens] = c[0]; n     } n   } n   for (int i = 1; i <= m; i++) { n     scanf( %I64d-%s , &x, c); n     if (c[0] == t[lent]) n       cntt[lent] += x; n     else { n       cntt[++lent] = x; n       t[lent] = c[0]; n     } n   } n } n void getnxt() { n   Next[0] = Next[1] = 0; n   long long j = 0; n   for (long long i = 2; i <= lent - 2; i++) { n     while (j && (st[j + 1] != st[i] || cntt[i] != cntt[j + 1])) j = Next[j]; n     if (st[j + 1] == st[i] && cntt[i] == cntt[j + 1]) j++; n     Next[i] = j; n   } n } n long long kmp() { n   long long sum = 0; n   long long j = 0; n   for (long long i = 2; i < lens; i++) { n     while (j && (st[j + 1] != s[i] || cnts[i] != cntt[j + 1])) j = Next[j]; n     if (st[j + 1] == s[i] && cnts[i] == cntt[j + 1]) j++; n     if (j == lent - 2) { n       if (s[i - (lent - 2)] == t[1] && s[i + 1] == t[lent] && n           cnts[i - (lent - 2)] >= f && cnts[i + 1] >= e) n         sum++; n       j = Next[j]; n     } n   } n   return sum; n } n int main() { n   while (scanf( %d %d , &n, &m) != EOF) { n     init(); n     if (lent == 1) { n       for (int i = 1; i <= lens; i++) { n         if (s[i] == t[lent] && cnts[i] >= cntt[lent]) n           ans += cnts[i] - cntt[lent] + 1; n       } n     } else if (lent == 2) { n       for (int i = 1; i < lens; i++) { n         if (s[i] == t[1] && s[i + 1] == t[2] && cnts[i] >= cntt[1] && n             cnts[i + 1] >= cntt[2]) n           ans++; n       } n     } else { n       strcpy(st + 1, t + 2); n       st[lent] = 0; n       f = cntt[1], e = cntt[lent]; n       for (int i = 1; i < lent; i++) cntt[i] = cntt[i + 1]; n       getnxt(); n       ans = kmp(); n     } n     printf( %I64d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1010; n int n, a[N], b[N]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   sort(a + 1, a + 1 + n); n   for (int i = 1; i <= n / 2; i++) { n     b[i * 2 - 1] = a[i]; n     b[i * 2] = a[n - i + 1]; n   } n   if (n % 2) b[n] = a[n / 2 + 1]; n   int flag = 1; n   for (int i = 3; i <= n; i += 2) { n     if (b[i] > b[i - 1]) { n       flag = 0; n       break; n     } n   } n   for (int i = 2; i <= n && flag; i += 2) { n     if (b[i] < b[i - 1]) { n       flag = 0; n       break; n     } n   } n   if (!flag) { n     puts( Impossible ); n     return 0; n   } n   for (int i = 1; i <= n; i++) { n     printf( %d , b[i]); n     putchar(i == n ?   n  :    ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<long long, long long> q[9]; n char s[2]; n int main() { n   long long n, x0, y0; n   scanf( %lld , &n); n   scanf( %lld%lld , &x0, &y0); n   for (long long m = 0; m < 9; m++) q[m] = make_pair(1E10, INT_MAX); n   while (n--) { n     long long x, y, i, j, k, g, m, d; n     scanf( %s , s); n     scanf( %lld%lld , &x, &y); n     i = x - x0, j = y - y0; n     if (abs(i) != abs(j) && i != 0 && j != 0) continue; n     k = max(abs(i), abs(j)); n     i /= k, j /= k; n     i++, j++; n     m = 3 * i + j; n     if (s[0] ==  Q ) n       k = 3; n     else n       k = (s[0] !=  R ) + 1; n     d = abs(x - x0) + abs(y - y0); n     q[m] = min(q[m], make_pair(d, k)); n   } n   for (long long m = 0; m < 9; m++) { n     long long i = m / 3, j = m % 3, check; n     i--, j--; n     if (i == 0 || j == 0) n       check = 0; n     else n       check = 1; n     if (q[m].first != 1E10 && (q[m].second & (1 << check))) { n       cout <<  YES n ; n       return 0; n     } n   } n   cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   long long n, k; n   cin >> n >> k; n   vector<long long> a(n); n   for (auto& i : a) cin >> i; n   while (a.back() <= k) a.pop_back(); n   reverse(a.begin(), a.end()); n   while (a.back() <= k) a.pop_back(); n   cout << n - a.size() <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 30; n signed main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   string s; n   cin >> s; n   int n = s.size(); n   int pref = 0, suf = n - 1; n   while (pref < n - 1 && s[pref] != s[pref + 1]) pref++; n   while (suf > 0 && s[suf] != s[suf - 1]) suf--; n   int ans = min(n, (s[n - 1] != s[0] ? pref + 1 + (n - suf) : 0)); n   for (int i = 0; i < n; i++) { n     int j = i; n     while (j < n - 1 && s[j] != s[j + 1]) j++; n     ans = max(ans, j - i + 1); n     i = j; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1000000007; n const long long INF = 1e18; n int mod = 998244353; n template <typename T1, typename T2> n inline bool chmin(T1& a, T2 b) { n   if (a > b) { n     a = b; n     return 1; n   } n   return 0; n } n template <typename T1, typename T2> n inline bool chmax(T1& a, T2 b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; 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 long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n template <typename T, typename U> n T pow_(T a, U b) { n   return b ? pow_(a * a, b / 2) * (b % 2 ? a : 1) : 1; n } n long long modpow(long long a, long long b, long long _mod) { n   return b ? modpow(a * a % _mod, b / 2, _mod) * (b % 2 ? a : 1) % _mod : 1; n } n template <class T, class U> n ostream& operator<<(ostream& os, const pair<T, U>& p) { n   os << p.first <<     << p.second; n   return os; n } n template <class T> n ostream& operator<<(ostream& os, const vector<T>& vec) { n   for (int i = 0; i < ((int)vec.size()); ++i) { n     if (i) os <<    ; n     os << vec[i]; n   } n   return os; n } n template <typename T> n inline istream& operator>>(istream& is, vector<T>& v) { n   for (int j = 0; j < ((int)v.size()); ++j) is >> v[j]; n   return is; n } n template <class T, class T2> n inline void add(T& a, T2 b) { n   a += b; n   if (a >= mod) a -= mod; n } n void solve(); n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout << fixed << setprecision(10); n   int T; n   T = 1; n   while (T--) solve(); n } n void solve() { n   int n; n   long long k; n   cin >> n >> k; n   vector<int> a(n); n   cin >> a; n   long long ma = *max_element(a.begin(), a.end()); n   int ans = 0; n   vector<int> b(n); n   auto Get = [&](long long limit) { n     long long sum = 0; n     for (int i = 0; i < (n); ++i) { n       auto binary_search = [&](int ok, int ng) { n         auto check = [&](int x) { n           if (a[i] - 3LL * x * (x + 1) - 1 >= limit) return 1; n           return 0; n         }; n         int d = 1; n         while ((ng - ok >= 0 ? ng - ok : -(ng - ok)) > d) { n           int mid = (ng + ok) / 2; n           (check(mid) ? ok : ng) = mid; n         } n         return ok; n       }; n       b[i] = binary_search(0, a[i] + 1); n       sum += b[i]; n     } n     return sum; n   }; n   auto binary_search = [&](long long ok, long long ng) { n     auto check = [&](long long x) { n       long long s = Get(x); n       if (s >= k) return 1; n       return 0; n     }; n     int d = 1; n     while ((ng - ok >= 0 ? ng - ok : -(ng - ok)) > d) { n       long long mid = (ng + ok) / 2; n       (check(mid) ? ok : ng) = mid; n     } n     return ok; n   }; n   long long L = binary_search(-4e18, 4e18); n   long long s = Get(L); n   for (int i = 0; i < (s - k); ++i) { n     long long mi = INF; n     int id = -1; n     for (int j = 0; j < (n); ++j) n       if (chmin(mi, a[j] - 3LL * b[j] * b[j] + 3 * b[j] - 1)) id = j; n     b[id]--; n   } n   cout << b <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int len = 0, coun = 0; n bool visited[1000006]; n vector<int> check; n vector<int> grid[1000006]; n void dfs(int v) { n   visited[v] = true; n   for (int i = 0; i < grid[v].size(); i++) { n     if (!visited[grid[v][i]]) { n       dfs(grid[v][i]); n     } n   } n   check.push_back(v); n } n void toposort(int n) { n   memset(visited, false, sizeof(visited)); n   coun = 0; n   for (int i = 0; i < n; i++) { n     if (!visited[i + 1]) dfs(i + 1); n   } n } n int main() { n   int n, m, x, y; n   cin >> n >> m; n   map<pair<int, int>, int> ma; n   for (int i = 0; i < m; i++) { n     cin >> x >> y; n     ma[{x, y}] = i; n     grid[x].push_back(y); n   } n   toposort(n); n   reverse(check.begin(), check.end()); n   bool ans = true; n   int maxi = 0; n   for (int i = 0; i < check.size() - 1; i++) { n     if (ma.find(make_pair(check[i], check[i + 1])) == ma.end()) n       ans = false; n     else n       maxi = max(maxi, ma[make_pair(check[i], check[i + 1])]); n   } n   if (ans == false) n     cout << -1; n   else n     cout << maxi + 1; n } n


#include <bits/stdc++.h> n using namespace std; n int n, x, y; n vector<vector<int> > adjList(100009, vector<int>()); n bool vis[100009]; n vector<int> ans; n bool check_valid_edges(vector<vector<int> > &a, int n) { n   for (size_t i = 1; i <= n; i++) n     if (a[i].size() != 4) return false; n   return true; n } n int comp(int node1, int node2) { n   int c = 0; n   for (int i = 0; i < adjList[node1].size(); i++) { n     for (int j = 0; j < adjList[node2].size(); j++) { n       if (adjList[node1][i] == adjList[node2][j]) c++; n     } n   } n   return c; n } n void dfs(int node) { n   vis[node] = true; n   bool f = true; n   for (int i = 0; i < adjList[node].size() && f; i++) { n     if (comp(node, adjList[node][i]) == 2 && !vis[adjList[node][i]]) n       ans.push_back(adjList[node][i]), dfs(adjList[node][i]), f = false; n   } n } n int main() { n   cin >> n; n   for (int i = 1; i <= (2 * n); i++) { n     cin >> x >> y; n     adjList[x].push_back(y); n     adjList[y].push_back(x); n   } n   if (!check_valid_edges(adjList, n)) return cout << -1 << endl, 0; n   if (n == 5) n     return cout <<  1 2 3 4 5 n  << endl, 0; n   else if (n == 6) { n     int s_node = 21; n     for (int i = 0; i < adjList[1].size(); i++) s_node -= adjList[1][i]; n     s_node--; n     cout <<  1   << adjList[1][0] <<     << adjList[1][1] <<     << s_node n          <<     << adjList[1][2] <<     << adjList[1][3] << endl; n     ; n   } else { n     dfs(1); n     ans.push_back(1); n     if (ans.size() == n) n       for (int i = 0; i < ans.size(); i++) cout << ans[i] <<    ; n     else n       cout << -1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ii = pair<int, int>; n using ll = long long int; n using vi = vector<int>; n using graph = vector<vi>; n const int INF = 0x3f3f3f3f; n const ll INFL = 0x3f3f3f3f3f3f3f3f; n const int MAXN = 200005; n int fq[4]; n int main() { n   int n, m, k; n   string s; n   cin >> n; n   cin >> s; n   for (auto c : s) { n     int x = c -  0 ; n     fq[x]++; n   } n   if (n == 1) { n     cout << s <<   n ; n   } else if (n == 2) { n     if (fq[1] == 2) { n       cout << 1 <<   n ; n     } else { n       cout << s <<   n ; n     } n   } else { n     cout << 1; n     while (fq[0]--) { n       cout << 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long h[100005], d; n int dp[100005]; n int arr[300003]; n int tree[(1200004)]; n int lazy[(1200004)]; n void build_tree(int node, int a, int b) { n   if (a > b) return; n   if (a == b) { n     tree[node] = arr[a]; n     return; n   } n   build_tree(node * 2, a, (a + b) / 2); n   build_tree(node * 2 + 1, 1 + (a + b) / 2, b); n   tree[node] = max(tree[node * 2], tree[node * 2 + 1]); n } n void update_tree(int node, int a, int b, int i, int j, int value) { n   if (lazy[node] != 0) { n     tree[node] += lazy[node]; n     if (a != b) { n       lazy[node * 2] += lazy[node]; n       lazy[node * 2 + 1] += lazy[node]; n     } n     lazy[node] = 0; n   } n   if (a > b || a > j || b < i) return; n   if (a >= i && b <= j) { n     tree[node] += value; n     if (a != b) { n       lazy[node * 2] += value; n       lazy[node * 2 + 1] += value; n     } n     return; n   } n   update_tree(node * 2, a, (a + b) / 2, i, j, value); n   update_tree(1 + node * 2, 1 + (a + b) / 2, b, i, j, value); n   tree[node] = max(tree[node * 2], tree[node * 2 + 1]); n } n int query_tree(int node, int a, int b, int i, int j) { n   if (a > b || a > j || b < i) return -0x7fffffff; n   if (lazy[node] != 0) { n     tree[node] += lazy[node]; n     if (a != b) { n       lazy[node * 2] += lazy[node]; n       lazy[node * 2 + 1] += lazy[node]; n     } n     lazy[node] = 0; n   } n   if (a >= i && b <= j) return tree[node]; n   int q1 = query_tree(node * 2, a, (a + b) / 2, i, j); n   int q2 = query_tree(1 + node * 2, 1 + (a + b) / 2, b, i, j); n   int res = max(q1, q2); n   return res; n } n void reconstruct(vector<int>& ret, int val) { n   long long last = -1e18; n   for (int i = 0; i < n; i++) { n     if (abs(last - h[i]) >= d && dp[i] == val) { n       ret.push_back(i + 1); n       last = h[i]; n       val--; n     } n   } n } n map<long long, int> htoi; n set<long long> ss; n int main() { n   cin >> n >> d; n   int counter = 0; n   for (int i = 0; i < n; i++) { n     scanf( %I64d , &h[i]); n     ss.insert(h[i]); n   } n   set<long long>::iterator iit; n   for (iit = ss.begin(); iit != ss.end(); iit++) htoi[*iit] = counter++; n   ss.clear(); n   update_tree(1, 0, 300003 - 1, htoi[h[n - 1]], htoi[h[n - 1]], 1); n   ss.insert(h[n - 1]); n   dp[n - 1] = 1; n   for (int ind = n - 2; ind >= 0; ind--) { n     int index = htoi[h[ind]]; n     long long curmin = *ss.begin(); n     iit = ss.end(); n     iit--; n     long long curmax = *iit; n     int ucand = 0; n     int dcand = 0; n     if (ss.lower_bound(h[ind] + d) != ss.end()) { n       long long curhplud = *ss.lower_bound(h[ind] + d); n       ucand = max(ucand, n                   query_tree(1, 0, 300003 - 1, htoi[curhplud], htoi[curmax])); n     } n     iit = ss.lower_bound(h[ind] - d); n     if (iit == ss.begin()) { n       if (*iit <= h[ind] - d) { n         dcand = max(dcand, n                     query_tree(1, 0, 300003 - 1, htoi[curmin], htoi[curmin])); n       } n     } else { n       if (*iit > h[ind] - d) iit--; n       if (iit == ss.end()) iit--; n       long long curhmind = *iit; n       dcand = max(dcand, n                   query_tree(1, 0, 300003 - 1, htoi[curmin], htoi[curhmind])); n     } n     int curval = query_tree(1, 0, 300003 - 1, index, index); n     int newval = max(max(ucand, dcand) + 1, curval); n     update_tree(1, 0, 300003 - 1, index, index, newval - curval); n     ss.insert(h[ind]); n     dp[ind] = query_tree(1, 0, 300003 - 1, index, index); n   } n   int len = dp[0]; n   for (int i = 1; i < n; i++) len = max(len, dp[i]); n   vector<int> ret; n   reconstruct(ret, len); n   cout << ret.size() << endl; n   for (int i = 0; i < ret.size(); i++) cout << ret[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int inf = 1 << 30; n const long long INF = 1ll << 61; n using namespace std; n const int MOD = 1000000; n int n; n int nnum[500000 + 55]; n int vis[1000000 + 55]; n int flag[1000000 + 55]; n vector<int> G; n void init() { n   memset(flag, 0, sizeof(flag)); n   memset(nnum, 0, sizeof(nnum)); n   memset(vis, 0, sizeof(vis)); n   G.clear(); n } n bool input() { n   while (cin >> n) { n     for (int i = 0; i < n; i++) { n       scanf( %d , &nnum[i]); n       vis[nnum[i]]++; n     } n     return false; n   } n   return true; n } n void cal() { n   int cnt = 0; n   for (int i = 0; i < n; i++) { n     if (vis[nnum[i]] && !vis[MOD - nnum[i] + 1]) { n       flag[MOD - nnum[i] + 1]++; n       cnt++; n     } n   } n   for (int i = 1; i <= MOD && cnt < n; i++) { n     if (!vis[i] && !vis[MOD - i + 1]) { n       flag[i] = 1; n       cnt++; n       flag[MOD - i + 1] = 1; n       cnt++; n     } n   } n   cout << cnt << endl; n   for (int i = 1; i <= MOD; i++) n     if (flag[i]) printf( %d  , i); n   puts(  ); n } n void output() {} n int main() { n   while (true) { n     init(); n     if (input()) return 0; n     cal(); n     output(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> g[2000]; n int vis[2000]; n vector<pair<int, int> > cyc; n void dfs(int x, int scc) { n   if (vis[x] > 0) { n     return; n   } n   vis[x] = 1; n   int j; n   for (int i = 0; i < g[x].size(); i++) { n     for (j = 0; j < g[g[x][i]].size(); j++) { n       if (g[g[x][i]][j] == x) { n         g[g[x][i]].erase(g[g[x][i]].begin() + j); n       } n     } n     if (vis[g[x][i]] > 0) { n       cyc.push_back(make_pair(x, g[x][i])); n     } n     dfs(g[x][i], scc); n   } n   return; n } n int main() { n   int n, i, j, x, y, t, u; n   cin >> n; n   for (i = 0; i < n - 1; i++) { n     cin >> x >> y; n     x--; n     y--; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   memset(vis, 0, sizeof(vis)); n   int scc = 0, days = 0; n   cyc.clear(); n   vector<int> store; n   store.clear(); n   for (i = 0; i < n; i++) { n     if (vis[i] == 0) { n       scc++; n       store.push_back(i); n       dfs(i, scc); n     } n   } n   if (scc == 1) { n     cout <<  0 n ; n     return 0; n   } n   j = 1; n   vector<pair<pair<int, int>, pair<int, int> > > v; n   v.clear(); n   for (i = 0; i < cyc.size(); i++) { n     v.push_back(make_pair(make_pair(cyc[i].first + 1, cyc[i].second + 1), n                           make_pair(store[j - 1] + 1, store[j] + 1))); n     j++; n   } n   cout << v.size() <<   n ; n   for (i = 0; i < v.size(); i++) { n     cout << v[i].first.first <<     << v[i].first.second <<     n          << v[i].second.first <<     << v[i].second.second <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gi() { n   int x = 0, w = 1; n   char ch = getchar(); n   while ((ch <  0  || ch >  9 ) && ch !=  - ) ch = getchar(); n   if (ch ==  - ) w = 0, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) n     x = (x << 3) + (x << 1) + ch -  0 , ch = getchar(); n   return w ? x : -x; n } n const int N = 5e5 + 5; n struct node { n   int h, nxt; n } a[N]; n int n, m, nxt[N], head[N], val[N], ft[N]; n char s[N]; n int dep[N], sz[N], son[N], vis[N], sum[N], ans[N]; n void add(int v, int h, int id) { n   a[id] = (node){h, ft[v]}; n   ft[v] = id; n } n void dfs1(int u, int f) { n   dep[u] = dep[f] + 1; n   sz[u] = 1; n   for (int v = head[u]; v; v = nxt[v]) { n     dfs1(v, u), sz[u] += sz[v]; n     if (sz[v] > sz[son[u]]) son[u] = v; n   } n } n void update(int u) { n   sum[dep[u]] ^= val[u]; n   for (int v = head[u]; v; v = nxt[v]) n     if (!vis[v]) update(v); n } n bool cal(int x) { n   int res = 0; n   while (x) ++res, x ^= x & -x; n   return res <= 1; n } n void dfs(int u, int f, int keep) { n   for (int v = head[u]; v; v = nxt[v]) n     if (v != son[u]) dfs(v, u, 0); n   if (son[u]) dfs(son[u], u, 1), vis[son[u]] = 1; n   update(u); n   for (int i = ft[u]; i; i = a[i].nxt) ans[i] = cal(sum[a[i].h]); n   vis[son[u]] = 0; n   if (!keep) update(u); n } n int main() { n   n = gi(); n   m = gi(); n   for (int i = 2, f; i <= n; ++i) f = gi(), nxt[i] = head[f], head[f] = i; n   scanf( %s , s + 1); n   for (int i = 1; i <= n; ++i) val[i] = 1 << s[i] -  a ; n   for (int i = 1, v, h; i <= m; ++i) v = gi(), h = gi(), add(v, h, i); n   dfs1(1, 0); n   dfs(1, 0, 1); n   for (int i = 1; i <= m; ++i) puts(ans[i] ?  Yes  :  No ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool spj(set<int>& s) { n   vector<int> v{s.begin(), s.end()}; n   return (v[2] == 3 * v[0] && v[1] == 2 * v[0]); n } n int main() { n   int n; n   scanf( %d , &n); n   vector<int> result; n   for (int i = n, factor = 1; i != 0; i /= 2, factor *= 2) { n     if (i == 3) { n       { n         result.push_back(factor); n         result.push_back(factor); n         result.push_back(3 * factor); n         break; n       } n     } n     int k = (i + 1) / 2; n     for (int j = 0; j < k; ++j) { n       result.emplace_back(factor); n     } n   } n   for (int x : result) { n     printf( %d  , x); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, s, f, l, r, t; n int stay() { n   int next = (s < f) ? s + 1 : s - 1; n   return (s >= l && s <= r) || (next >= l && next <= r); n } n int di[] = {0, 1}; n int dj[] = {1, 0}; n void write(vector<string> &res, int x, int y, int dir, string s) { n   for (int i = 0; i < s.size(); i++) { n     res[x][y] = s[i]; n     x += di[dir]; n     y += dj[dir]; n   } n } n void print(vector<string> res) { n   for (int i = 0; i < res.size(); i++) { n     for (int j = 0; j < res[i].size(); ++j) { n       cout << res[i][j]; n     } n     cout << endl; n   } n } n bool same(string a, string b, int i, int j) { return a[i] == b[j]; } n int main() { n   vector<string> arr(6); n   vector<bool> vis(6, 0); n   for (int i = 0; i < 6; ++i) { n     cin >> arr[i]; n   } n   sort(arr.begin(), arr.end()); n   vector<string> best, res; n   do { n     if (!same(arr[0], arr[1], 0, 0) || n         !same(arr[4], arr[5], arr[4].size() - 1, arr[5].size() - 1)) n       continue; n     if (arr[3].size() != arr[0].size() + arr[4].size() - 1 || n         arr[2].size() != arr[1].size() + arr[5].size() - 1) n       continue; n     if (!same(arr[3], arr[2], arr[0].size() - 1, arr[1].size() - 1) || n         !same(arr[2], arr[0], 0, arr[0].size() - 1) || n         !same(arr[2], arr[4], arr[2].size() - 1, 0)) n       continue; n     if (!same(arr[3], arr[1], 0, arr[1].size() - 1) || n         !same(arr[3], arr[5], arr[3].size() - 1, 0)) n       continue; n     int n = arr[2].size(), m = arr[3].size(); n     string tmp = string(m,  . ); n     res = vector<string>(n, tmp); n     write(res, 0, 0, 0, arr[0]); n     write(res, 0, 0, 1, arr[1]); n     write(res, 0, arr[0].size() - 1, 1, arr[2]); n     write(res, arr[1].size() - 1, 0, 0, arr[3]); n     write(res, n - 1, arr[0].size() - 1, 0, arr[4]); n     write(res, arr[1].size() - 1, m - 1, 1, arr[5]); n     if (!best.size() || best > res) best = res; n   } while (next_permutation(arr.begin(), arr.end())); n   if (!best.size()) n     cout <<  Impossible  << endl; n   else n     print(best); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int dpl[2010], dpr[2010]; n int main() { n   cin >> s; n   memset(dpr, 0, sizeof(dpr)); n   memset(dpl, 0, sizeof(dpl)); n   int i, j, k; n   for (i = 0; i < s.size(); i++) { n     for (j = i, k = i; j >= 0 && k < s.size() && s[k] == s[j]; j--, k++) n       dpl[j]++, dpr[k]++; n     for (j = i, k = i + 1; j >= 0 && k < s.size() && s[k] == s[j]; j--, k++) n       dpl[j]++, dpr[k]++; n   } n   for (i = 1; i < s.size(); i++) dpr[i] += dpr[i - 1]; n   long long ans = 0; n   for (i = 1; i < s.size(); i++) ans += dpr[i - 1] * dpl[i]; n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> ara; n int main() { n   int n, k; n   cin >> n >> k; n   int t = n / 2; n   if (n == 1 && k != 0) { n     cout << -1; n     return 0; n   } n   if (n == 1 && k == 0) { n     cout << 1; n     return 0; n   } n   if (k < t) { n     cout << -1; n     return 0; n   } else { n     int t2 = k - t; n     t2++; n     cout << t2 <<     << t2 * 2; n     cout <<    ; n     ara[t2] = 1; n     ara[t2 * 2] = 1; n     int cnt = 1; n     for (int i = 1; i <= n - 2; i++) { n       if (ara[cnt] == 1) cnt++; n       if (ara[cnt] == 1) cnt++; n       cout << cnt <<    ; n       cnt++; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const double eps = 1e-6; n const int inf = 1e9; n const long long llf = 1e18; n const int mod = 1e9 + 7; n const int maxn = 1e5 + 10; n char s[110]; n int f[310]; n int main() { n   int tc; n   cin >> tc; n   while (tc--) { n     cin >> s + 1; n     int n = strlen(s + 1); n     sort(s + 1, s + n + 1); n     string s1 =   , s2 =   ; n     for (int i = 1; i <= n; i++) { n       f[s[i]]++; n     } n     for (int i =  a ; i <=  z ; i++) { n       if (i & 1) { n         while (f[i]) { n           s1 += i; n           f[i]--; n         } n       } else { n         while (f[i]) { n           s2 += i; n           f[i]--; n         } n       } n     } n     string t = s1 + s2; n     int flag = 1; n     for (int i = 1; i < t.size(); i++) { n       if (abs(t[i] - t[i - 1]) == 1) { n         flag = 0; n         break; n       } n     } n     if (flag) { n       cout << t << endl; n       continue; n     } n     t = s2 + s1; n     flag = 1; n     for (int i = 1; i < t.size(); i++) { n       if (abs(t[i] - t[i - 1]) == 1) { n         flag = 0; n         break; n       } n     } n     if (flag) { n       cout << t << endl; n     } else { n       cout <<  No answer  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   std::vector<int> count(1 << 19); n   auto modify = [&](char *s, int dt) { n     int len = strlen(s); n     std::reverse(s, s + len); n     int val = 0; n     for (int i = 0; i < 18; ++i) { n       val = val | (i >= len ? 0 : (s[i] -  0  & 1)) << i; n     } n     count[val] += dt; n   }; n   auto query = [&](char *s) { n     int len = strlen(s); n     std::reverse(s, s + len); n     int val = 0; n     for (int i = 0; s[i]; ++i) { n       val |= s[i] -  0  << i; n     } n     return count[val]; n   }; n   int t; n   scanf( %d , &t); n   for (int _ = 0; _ < t; ++_) { n     char op[2]; n     char str[18 + 1]; n     scanf( %s%s , op, str); n     if (op[0] ==  + ) { n       modify(str, 1); n     } else if (op[0] ==  - ) { n       modify(str, -1); n     } else { n       printf( %d n , query(str)); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n namespace Solve { n const int MAXN = 200010; n inline int ScanInt(void) { n   int r = 0, c, d; n   while (!isdigit(c = getchar()) && c !=  - ) n     ; n   if (c !=  - ) r = c -  0 ; n   d = c; n   while (isdigit(c = getchar())) r = r * 10 + c -  0 ; n   return d ==  -  ? -r : r; n } n int a[MAXN], n, b[MAXN], cnt = 0, sum = 0; n int zero = 0, l1 = 0, l2 = 0; n bool c[MAXN]; n inline void solve(void) { n   n = ScanInt(); n   for (int i = 1; i <= n; i++) { n     a[i] = ScanInt(), sum += a[i]; n     if (a[i] == 0) zero++; n   } n   if (!zero) { n     puts( -1 ); n     return; n   } n   sort(a + 1, a + n + 1); n   for (int i = 1; i <= n; i++) { n     if (a[i] % 3 == 0) b[++cnt] = a[i], c[i] = true; n     if (a[i] % 3 == 1) l1++; n     if (a[i] % 3 == 2) l2++; n   } n   if (sum % 3 == 1) { n     if (l1 == 0) { n       int t = 2; n       for (int i = 1; i <= n; i++) n         if (a[i] % 3 == 2 && t) c[i] = true, t--; n     } else { n       int t = 1; n       for (int i = 1; i <= n; i++) n         if (a[i] % 3 == 1 && t) c[i] = true, t--; n     } n   } n   if (sum % 3 == 2) { n     if (l2 == 0) { n       int t = 2; n       for (int i = 1; i <= n; i++) n         if (a[i] % 3 == 1 && t) c[i] = true, t--; n     } else { n       int t = 1; n       for (int i = 1; i <= n; i++) n         if (a[i] % 3 == 2 && t) c[i] = true, t--; n     } n   } n   for (int i = 1; i <= n; i++) n     if (!c[i]) b[++cnt] = a[i]; n   sort(b + 1, b + cnt + 1); n   sum = 0; n   for (int i = 1; i <= cnt; i++) sum += b[i]; n   if (sum == 0) { n     puts( 0 ); n     return; n   } n   assert(sum % 3 == 0); n   for (int i = cnt; i >= 1; i--) printf( %d , b[i]); n   printf(  n ); n } n }  // namespace Solve n int main(void) { n   Solve::solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rang(chrono::high_resolution_clock::now().time_since_epoch().count()); n const long double PI = 3.141592653589793; n const long long INF = 9223372036854775807ll; n const long long mod = 1e9 + 7; n const int N = 1e5 + 5; n long long tree[4 * N + 10]; n long long lazy[4 * N + 10]; n long long b[N]; n void build(int node, int start, int end) { n   if (start == end) { n     tree[node] = b[start]; n     return; n   } n   int mid = (start + end) / 2; n   build(2 * node, start, mid); n   build(2 * node + 1, mid + 1, end); n   tree[node] = tree[2 * node] + tree[2 * node + 1]; n } n long long query(int node, int start, int end, int l, int r) { n   if (lazy[node] != -INF / 10000) { n     tree[node] = lazy[node] * (end - start + 1); n     if (start != end) { n       lazy[2 * node] = lazy[node]; n       lazy[2 * node + 1] = lazy[node]; n     } n     lazy[node] = -INF / 10000; n   } n   if (l > end || r < start) return 0; n   if (l <= start && r >= end) { n     return tree[node]; n   } n   int mid = (start + end) / 2; n   return query(2 * node, start, mid, l, r) + n          query(2 * node + 1, mid + 1, end, l, r); n } n void update(int node, int start, int end, int l, int r, long long val) { n   if (lazy[node] != (-INF / 10000)) { n     tree[node] = lazy[node] * (end - start + 1ll); n     if (start != end) { n       lazy[2 * node] = lazy[node]; n       lazy[2 * node + 1] = lazy[node]; n     } n     lazy[node] = (-INF / 10000); n   } n   if (l > end || r < start) return; n   if (l <= start && r >= end) { n     tree[node] = (end - start + 1) * val; n     if (start != end) { n       lazy[2 * node] = val; n       lazy[2 * node + 1] = val; n     } n     return; n   } n   int mid = (start + end) / 2; n   update(2 * node, start, mid, l, r, val); n   update(2 * node + 1, mid + 1, end, l, r, val); n   tree[node] = tree[2 * node] + tree[2 * node + 1]; n } n void solve() { n   fill(tree, tree + 4 * N + 4, 0ll); n   fill(lazy, lazy + 4 * N + 4, -INF / 10000); n   fill(b, b + N, 0); n   long long n; n   cin >> n; n   long long a[n]; n   long long t[n + 1]; n   t[0] = 0; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   for (int i = 1; i < n; ++i) { n     long long k; n     cin >> k; n     t[i] = t[i - 1] + k; n   } n   for (int i = 0; i < n; ++i) { n     b[i] = a[i] - t[i]; n   } n   for (int i = 1; i < n; ++i) { n     t[i] += t[i - 1]; n   } n   b[n] = INF / 10000; n   build(1, 0, n); n   int q; n   cin >> q; n   while (q--) { n     char now; n     cin >> now; n     if (now ==  s ) { n       int l; n       int r; n       cin >> l >> r; n       l--; n       r--; n       long long pr = query(1, 0, n, l, r); n       if (l - 1 < 0) l++; n       cout << (pr + t[r] - t[l - 1]) <<   n ; n     } else { n       int i; n       long long x; n       cin >> i >> x; n       i--; n       long long val = query(1, 0, n, i, i) + x; n       int lo = i; n       int hi = n; n       while (hi - lo > 1) { n         int mid = (hi + lo) / 2; n         if (query(1, 0, n, mid, mid) <= val) { n           lo = mid; n         } else { n           hi = mid; n         } n       } n       update(1, 0, n, i, lo, val); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   srand(chrono::high_resolution_clock::now().time_since_epoch().count()); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int x1 = 0, x2 = 0, y1 = 0, y2 = 0, position = 0; n   int length = s.size(); n   for (int i = 0; i < length; i++) { n     if (s[i] ==  R ) { n       x1++; n       position++; n     } n     if (s[i] ==  L ) { n       x1--; n       position--; n     } n     if (s[i] ==  U ) { n       y1++; n       position++; n     } n     if (s[i] ==  D ) { n       y1--; n       position--; n     } n   } n   if (length % 2 == 0) { n     printf( %d , (abs(x1) + abs(y1)) / 2); n   } else { n     cout << -1; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long ka[6] = {0, 0, 0, 0, 2, 4}; n int main() { n   long long n, m; n   while (cin >> n >> m) { n     if (n > m) { n       int tmp = n; n       n = m; n       m = tmp; n     } n     long long ret; n     if (n == 1) n       ret = m / 6 * 6 + ka[m % 6]; n     else if (n == 2) { n       if (m == 2) n         ret = 0; n       else if (m == 3) n         ret = 4; n       else if (m == 7) n         ret = 12; n       else n         ret = n * m; n     } else { n       if (n % 2 && m % 2) n         ret = n * m - 1; n       else n         ret = n * m; n     } n     cout << ret << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int n, i, x, ind[210], o = 0, e = 0; n char a[200001], arr[200010]; n int main() { n   scanf( %s , a); n   n = strlen(a); n   if (n == 1) { n     printf( %s n , a); n     return 0; n   } n   for (int i = 0; i < n; i++) { n     ind[a[i]]++; n   } n   for (int i =  a ; i <=  z ; i++) { n     if (ind[i] > 0) { n       ind[i] % 2 == 0 ? e++ : o++; n     } n   } n   for (int i =  z ; i >=  a ; i--) { n     if (ind[i] % 2 != 0) { n       for (int x =  a ; x < i; x++) { n         if (ind[x] % 2 != 0) { n           ind[x]++, ind[i]--; n           break; n         } n       } n     } n   } n   int j = 0; n   for (int i =  a ; i <=  z ; i++) { n     if (ind[i] > 0) { n       for (int x = 0; x < ind[i] / 2; x++) { n         printf( %c , i); n         arr[j++] = i; n       } n     } n   } n   if (n % 2 != 0) { n     for (int i =  a ; i <=  z ; i++) { n       if (ind[i] % 2 == 1) { n         printf( %c , i); n         break; n       } n     } n   } n   for (int i = j - 1; i >= 0; i--) { n     printf( %c , arr[i]); n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n inline int read() { n   int x = 0; n   char c = getchar(); n   bool f = 0; n   while (!isdigit(c)) f |= (c ==  - ), c = getchar(); n   while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); n   return f ? -x : x; n } n int n, m, t; n int sq[500005], a[500005], pos[500005], cnt[500005], ans[500005], st[500005], n     top; n struct node { n   int l, r, id; n } q[500005]; n int nowx, nowy; n inline bool operator<(node x, node y) { n   return (sq[x.l] ^ sq[y.l]) ? x.l < y.l n                              : ((sq[x.l] & 1) ? x.r < y.r : x.r > y.r); n } n inline void Add(int x) { n   if (cnt[a[x]] == 1) { n     st[pos[a[x]]] = st[top]; n     pos[st[top]] = pos[a[x]]; n     pos[a[x]] = st[top--] = 0; n   } n   cnt[a[x]]++; n   if (cnt[a[x]] == 1) st[++top] = a[x], pos[a[x]] = top; n } n inline void Delete(int x) { n   if (cnt[a[x]] == 1) { n     st[pos[a[x]]] = st[top]; n     pos[st[top]] = pos[a[x]]; n     pos[a[x]] = st[top--] = 0; n   } n   cnt[a[x]]--; n   if (cnt[a[x]] == 1) st[++top] = a[x], pos[a[x]] = top; n } n int main() { n   n = read(), t = sqrt(n); n   for (register int i = 1; i <= n; ++i) a[i] = read(), sq[i] = i / t; n   m = read(); n   for (register int i = 1; i <= m; ++i) n     q[i].l = read(), q[i].r = read(), q[i].id = i; n   sort(q + 1, q + m + 1); n   nowx = 1, nowy = 0; n   for (register int i = 1; i <= m; ++i) { n     while (nowx < q[i].l) Delete(nowx++); n     while (nowx > q[i].l) Add(--nowx); n     while (nowy > q[i].r) Delete(nowy--); n     while (nowy < q[i].r) Add(++nowy); n     ans[q[i].id] = st[top]; n   } n   for (register int i = 1; i <= m; ++i) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x1, x2, x3; n   cin >> x1 >> x2 >> x3; n   int min, max; n   int t = (x1 < x2) ? x1 : x2; n   min = (t < x3) ? t : x3; n   t = (x1 > x2) ? x1 : x2; n   max = (t > x3) ? t : x3; n   cout << max - min; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n const int p = 998244353; n int n, k, cnt, a[N], d[N], jc[N], ny[N]; n long long s = 1, ans; n inline int read() { n   int ans = 0; n   char c = getchar(); n   while (c < 48 || c > 57) c = getchar(); n   while (c >= 48 && c <= 57) n     ans = (ans << 3) + (ans << 1) + (c ^ 48), c = getchar(); n   return ans; n } n inline long long ksm(long long x, int y) { n   long long z = 1; n   while (y) { n     if (y & 1) z = z * x % p; n     x = x * x % p, y >>= 1; n   } n   return z; n } n inline long long C(int x, int y) { n   return 1ll * jc[x] * ny[y] % p * ny[x - y] % p; n } n int main() { n   n = read(), k = read() % p; n   for (register int i = 1; i <= n; ++i) { n     int u = read(); n     if (u != a[cnt]) n       a[++cnt] = u; n     else n       s = s * k % p; n   } n   if (a[1] == a[cnt]) --cnt, s = s * k % p; n   if (cnt <= 1) { n     putchar(48); n     return 0; n   } n   d[0] = jc[0] = 1, ans = ksm(k, cnt); n   for (register int i = 1; i <= cnt; ++i) n     d[i] = 1ll * d[i - 1] * (k - 2) % p, jc[i] = 1ll * jc[i - 1] * i % p; n   ny[cnt] = ksm(jc[cnt], p - 2); n   for (register int i = cnt - 1; i >= 0; --i) n     ny[i] = 1ll * ny[i + 1] * (i + 1) % p; n   for (register int i = 0; (i << 1) <= cnt; ++i) n     ans = (ans - C(cnt, i) * C(cnt - i, i) % p * d[cnt - (i << 1)] % p + p) % p; n   ans = ans * ny[2] % p * s % p; n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= mod; n   assert(b >= 0); n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n   } n   return res; n } n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n int v[100066]; n vector<int> prime; n int mu[100066]; n void init() { n   int N = 100000; n   for (int i = 2; i <= N; i++) { n     if (!v[i]) { n       mu[i] = 1; n       v[i] = i; n       prime.push_back(i); n     } n     for (auto p : prime) { n       if (p > v[i] || i > N / p) break; n       v[i * p] = p; n       if (i % p == 0) n         mu[i * p] = 0; n       else n         mu[i * p] = -mu[i]; n     } n   } n } n long long solve(vector<pair<int, int> > vec) { n   long long sum = 0, all = 0; n   long long all1 = 0; n   for (auto x : vec) { n     sum = (sum + 1ll * x.first * x.second) % mod; n     all = (all + x.second) % mod; n     all1 = (all1 + x.second) % (mod - 1); n   } n   long long pp = powmod(2, all1 - 2 + mod - 1) * ((all - 1 + mod) % mod) % mod; n   long long ret = 0; n   long long two0 = powmod(2, all1 - 3 + mod - 1); n   long long two1 = two0 * 2 % mod; n   long long two2 = two1 * 2 % mod; n   long long qq = two1 + (all - 2 + mod) * two0; n   qq = qq % mod; n   for (auto x : vec) { n     ret += pp * x.first % mod * x.first % mod * x.second % mod; n     ret %= mod; n     long long tmp = (sum + mod - x.first) % mod; n     tmp = 1ll * x.second * x.first % mod * tmp % mod * qq % mod; n     ret = (ret + tmp) % mod; n   } n   return ret; n } n int n; n vector<pair<int, int> > vec, V[100066]; n int main() { n   init(); n   scanf( %d , &n); n   int x, y; n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &x, &y); n     vec.push_back(make_pair(x, y)); n     for (int j = 1; 1ll * j * j <= x; j++) { n       if (x % j) continue; n       V[j].push_back(make_pair(x, y)); n       if (x / j != j) V[x / j].push_back(make_pair(x, y)); n     } n   } n   long long ans = solve(vec); n   for (int i = 2; i <= 100000; i++) { n     long long tmp = (-solve(V[i]) * mu[i] + mod) % mod; n     ans = (ans + tmp) % mod; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (!(ch >=  0  && ch <=  9 )) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + (ch -  0 ); n     ch = getchar(); n   } n   return x * f; n } n struct edge { n   int next, to; n } e[900500]; n vector<int> v[900500]; n int cnt, head[900500], size[900500], col[900500]; n bool used[900500], vis[900500]; n inline void ins(int u, int v) { n   e[++cnt].to = v; n   e[cnt].next = head[u]; n   head[u] = cnt; n } n inline void insert(int u, int v) { n   ins(u, v); n   ins(v, u); n } n void dfs(int x) { n   int j = 1; n   vis[x] = 1; n   for (int i = 0; i < size[x]; i++) used[col[v[x][i]]] = 1; n   for (int i = 0; i < size[x]; i++) n     if (!col[v[x][i]]) { n       for (; used[j]; j++) n         ; n       used[col[v[x][i]] = j] = 1; n     } n   for (int i = 0; i < size[x]; i++) used[col[v[x][i]]] = 0; n   for (int i = head[x]; i; i = e[i].next) n     if (!vis[e[i].to]) dfs(e[i].to); n } n int main() { n   int n = read(), m = read(), ans = 0; n   for (int i = 1; i <= n; i++) { n     size[i] = read(); n     for (int j = 1; j <= size[i]; j++) v[i].push_back(read()); n     ans = max(ans, size[i]); n   } n   for (int i = 1; i < n; i++) insert(read(), read()); n   printf( %d n , ans ? ans : 1); n   dfs(1); n   for (int i = 1; i <= m; i++) printf( %d  , col[i] ? col[i] : 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Z = (int)3e3 + 228; n const int N = (int)1e6 + 228; n const int INF = (int)1e9 + 228; n const int MOD = (int)1e9 + 7; n int n, d; n long long a[N], delta[N], k; n bool can(long long mid) { n   fill(delta + 1, delta + 1 + n, 0LL); n   for (int i = 1; i <= n; i++) { n     delta[max(1, i - d)] += a[i]; n     delta[i + d + 1] -= a[i]; n   } n   long long cur = 0, res = 0; n   for (int i = 1; i <= n; i++) { n     cur += delta[i]; n     if (cur < mid) { n       res += mid - cur; n       if (res > k) return false; n       delta[i + 2 * d + 1] -= mid - cur; n       cur = mid; n     } n   } n   return true; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> d >> k; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   long long l = 0, r = 2e18 + 5; n   while (l < r) { n     long long mid = (l + r + 1) / 2; n     if (can(mid)) n       l = mid; n     else n       r = mid - 1; n   } n   cout << l; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool shown[100001]; n vector<pair<int, int> > v; n vector<int> cycle; n vector<vector<int> > ans; n int main() { n   int n, tmp; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &tmp); n     v.push_back(make_pair(tmp, i)); n   } n   sort(v.begin(), v.end()); n   for (int i = 0; i < n; i++) { n     int j = i; n     while (!shown[v[j].second]) { n       shown[v[j].second] = true; n       cycle.push_back(v[j].second); n       j = v[j].second; n     } n     if (cycle.size() != 0) { n       sort(cycle.begin(), cycle.end()); n       ans.push_back(cycle); n       cycle.clear(); n     } n   } n   printf( %d n , ans.size()); n   for (int i = 0; i < ans.size(); i++) { n     printf( %d , ans[i].size()); n     for (int j = 0; j < ans[i].size(); j++) printf(  %d , ans[i][j] + 1); n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, x, r, z, sol; n int main() { n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> x; n     if (r > 0) { n       x = x - k + r; n       sol++; n     } n     z = x / k; n     sol += z; n     r = x % k; n   } n   if (r > 0) { n     sol++; n   } n   cout << sol; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1E6; n int n, q, sz = 1; n long long value[6]; n long long dp[MAXN]; n inline void upData(int v, long long w) { n   for (int i = MAXN - 1; i >= v; --i) dp[i] = max(dp[i], dp[i - v] + w); n } n inline void Divide(int x) { n   int v = 3 * sz, all = min(MAXN / v, n); n   for (int i = 1; i < all; i <<= 1) upData(v * i, 1ll * value[x] * i), all -= i; n   upData(v * all, 1ll * value[x] * all); n } n int main(void) { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n, n = 3 * (n - 1); n   for (int i = 0; i < 6; ++i) cin >> value[i]; n   for (int i = 3; i < MAXN; ++i) { n     int num = i, base = 0; n     while (num) { n       if (((num % 10) % 3) == 0) dp[i] += 1ll * value[base] * ((num % 10) / 3); n       num /= 10, ++base; n     } n   } n   for (int i = 0; i < 6; ++i) Divide(i), sz *= 10; n   cin >> q; n   while (q--) cin >> n, cout << dp[n] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1000000000 + 7; n map<int, vector<int>> mp; n vector<int> cur(1, -1), goal(1, -1), done(1, -1); n vector<int> ans; n int c; n void funct(int node, int level, int even, int odd) { n   int r = level % 2; n   done[node] = 1; n   if (r == 1 && odd == 1) n     cur[node] = 1 - cur[node]; n   else if (r == 0 && even == 1) n     cur[node] = 1 - cur[node]; n   if (cur[node] != goal[node]) { n     c++; n     ans.push_back(node); n     cur[node] = goal[node]; n     if (level % 2 == 0) n       even = 1 - even; n     else n       odd = 1 - odd; n   } n   for (int i = 0; i < mp[node].size(); i++) { n     if (done[mp[node][i]] == 0) funct(mp[node][i], level + 1, even, odd); n   } n } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     cin >> a >> b; n     mp[a].push_back(b); n     mp[b].push_back(a); n   } n   int num; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &num); n     cur.push_back(num); n   } n   for (int i = 1; i <= n; i++) { n     scanf( %d , &num); n     goal.push_back(num); n     done.push_back(0); n   } n   c = 0; n   int level = 0, even = 0, odd = 0; n   funct(1, level, even, odd); n   cout << c << endl; n   if (c > 0) { n     int i = 0; n     while (i < ans.size()) { n       printf( %d n , ans[i]); n       i++; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, a[60], b[60]; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   cin >> m; n   for (int i = 0; i < m; i++) cin >> b[i]; n   int BG = -1; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (b[j] % a[i] == 0) { n         if (b[j] / a[i] > BG) { n           BG = b[j] / a[i]; n         } n       } n     } n   } n   int ans = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (b[j] % a[i] == 0 && b[j] / a[i] == BG) { n         ans++; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n using ll = long long; n using ld = long double; n ll mod = 998244353; n ll gcd(ll a, ll b) { n   if (a == 0 or b == 0) return a + b; n   return gcd(b, a % b); n } n ll sum(ll a, ll b) { n   a += b; n   a %= mod; n   return a; n } n ll sub(ll a, ll b) { n   a += mod - b; n   a %= mod; n   return a; n } n inline ll mul(ll a, ll b) { n   if (a * b < mod) return a * b; n   return a * b % mod; n } n inline ll pew(ll a, ll b) { n   ll res = 1; n   while (b) { n     if (b & 1) res = mul(res, a); n     a = mul(a, a); n     b /= 2; n   } n   return res; n } n int parent[600001]; n int rnk[600001]; n int suk[600001]; n void make_set(int v) { n   parent[v] = v; n   rnk[v] = 0; n   suk[v] = 0; n } n int find_set(int v) { n   if (v == parent[v]) return v; n   return parent[v] = find_set(parent[v]); n } n void union_sets(int a, int b) { n   a = find_set(a); n   b = find_set(b); n   if (a != b and (suk[a] * suk[b] == 0)) { n     if (rnk[a] < rnk[b]) swap(a, b); n     parent[b] = a; n     if (rnk[a] == rnk[b]) ++rnk[a]; n     suk[a] += suk[b]; n   } n } n vector<ll> primes(ll cur) { n   vector<ll> res; n   if (cur <= 1) return res; n   for (ll j = 2; j * j <= cur; j++) { n     if (cur % j == 0) { n       res.push_back(j); n       while (cur % j == 0) { n         cur /= j; n       } n     } n   } n   if (cur != 1) res.push_back(cur); n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   ll n; n   cin >> n; n   srand(35324); n   vector<ll> kek(n); n   for (int i = 0; i < n; i++) { n     cin >> kek[i]; n   } n   vector<ll> lul(n); n   for (int i = 0; i < n; i++) { n     lul[i] = kek[i]; n   } n   random_shuffle(lul.begin(), lul.end()); n   ll bst = n; n   for (int q = 0; q < n and q < 23; q++) { n     ll cur = lul[q] - 1; n     vector<ll> pr = primes(cur); n     for (int j = 0; j < pr.size(); j++) { n       ll p = pr[j]; n       ll cnt = 0; n       for (int i = 0; i < n; i++) { n         ll a = kek[i] % p; n         if (kek[i] == a) { n           cnt += p - a; n         } else { n           cnt += min(a, p - a); n         } n       } n       bst = min(bst, cnt); n     } n     cur = lul[q]; n     pr = primes(cur); n     for (int j = 0; j < pr.size(); j++) { n       ll p = pr[j]; n       ll cnt = 0; n       for (int i = 0; i < n; i++) { n         ll a = kek[i] % p; n         if (kek[i] == a) { n           cnt += p - a; n         } else { n           cnt += min(a, p - a); n         } n       } n       bst = min(bst, cnt); n     } n     cur = lul[q] + 1; n     pr = primes(cur); n     for (int j = 0; j < pr.size(); j++) { n       ll p = pr[j]; n       ll cnt = 0; n       for (int i = 0; i < n; i++) { n         ll a = kek[i] % p; n         if (kek[i] == a) { n           cnt += p - a; n         } else { n           cnt += min(a, p - a); n         } n       } n       bst = min(bst, cnt); n     } n   } n   cout << bst << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int flag = 1; n   int a, b, c, d; n   cin >> a >> b >> c >> d; n   if ((a + b) % 2 == 0) { n   } else n     flag = -1; n   if (a + d == 0) { n     if (flag == 1) { n       cout <<  Tidak Tidak Ya Tidak  n            <<   n ; n       return; n     } else { n       cout <<  Tidak Ya Tidak Tidak  n            <<   n ; n       return; n     } n   } n   if (b + c == 0) { n     if (flag == 1) { n       cout <<  Tidak Tidak Tidak Ya  n            <<   n ; n       return; n     } else { n       cout <<  Ya Tidak Tidak Tidak  n            <<   n ; n       return; n     } n   } n   if (flag == 1) { n     cout <<  Tidak Tidak Ya Ya  n          <<   n ; n     return; n   } else { n     cout <<  Ya Ya Tidak Tidak  n          <<   n ; n     return; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   int _ = 1; n   cin >> _; n   while (_--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   vector<int> a(n); n   for (auto &x : a) cin >> x; n   sort(a.begin(), a.end()); n   set<int> s; n   int ans = 0, x; n   for (int i = 0; i < n; i++) { n     x = a[i]; n     if (s.find(x) != s.end()) { n       while (1) { n         if (s.find(x) != s.end()) n           ans++; n         else n           break; n         x++; n       } n     } n     s.insert(x); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   char a[1000]; n   scanf( %s , a); n   if (a[0] > 96) a[0] -= 32; n   printf( %s , a); n } n


#include <bits/stdc++.h> n using namespace std; n const int dx[] = {-1, 1, 0, 0}; n const int dy[] = {0, 0, -1, 1}; n const int max_n = 1000; n char a[max_n][max_n]; n int res[max_n][max_n], val[max_n * max_n]; n vector<pair<int, int> > comp[max_n * max_n]; n bool used[10]; n bool range(int x, int l, int r) { return ((l <= x) && (x <= r)); } n int main() { n   int n, m, r = 0; n   scanf( %d%d n , &n, &m); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) scanf( %c , &a[i][j]); n     scanf(  n ); n   } n   memset(res, -1, sizeof(res)); n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) n       if (a[i][j] ==  . ) n         if ((j < m - 1) && (a[i][j + 1] ==  . )) { n           res[i][j] = res[i][j + 1] = r++, a[i][j] = a[i][j + 1] =  ~ ; n         } else if ((i < n - 1) && (a[i + 1][j] ==  . )) n           res[i][j] = res[i + 1][j] = r++, a[i][j] = a[i + 1][j] =  ~ ; n         else { n           for (int d = 0; d < 4; ++d) n             if (range(i + dx[d], 0, n - 1) && range(j + dy[d], 0, m - 1) && n                 (a[i + dx[d]][j + dy[d]] !=  # )) { n               res[i][j] = res[i + dx[d]][j + dy[d]]; n               break; n             } n           if (res[i][j] != -1) continue; n           printf( -1 n ); n           return 0; n         } n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) n       if (res[i][j] != -1) comp[res[i][j]].push_back(make_pair(i, j)); n   for (int rc = 0; rc < r; ++rc) { n     memset(used, 0, sizeof(used)); n     for (int k = 0; k < (int)comp[rc].size(); ++k) n       for (int d = 0; d < 4; ++d) { n         int cur = res[comp[rc][k].first + dx[d]][comp[rc][k].second + dy[d]]; n         if (cur < rc) used[val[cur]] = true; n       } n     for (int k = 0; k < 10; ++k) n       if (!used[k]) { n         val[rc] = k; n         break; n       } n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) n       printf( %c , res[i][j] == -1 ?  #  :  0  + val[res[i][j]]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n int a[10000]; n int main() { n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) scanf( %d , a + i); n   if (n % 2 == 0) n     puts( 0 ); n   else { n     long long r = m / ((n + 1) / 2); n     long long hi = (1 << 30); n     for (int i = 0; i < n; i += 2) hi = min(hi, (long long)a[i]); n     cout << min(hi, r * k) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 1e5 + 5; n int main() { n   int n, f[MaxN], cnt[MaxN], mm = 0, ans = 0; n   scanf( %d , &n); n   memset(f, 0, sizeof(f)); n   memset(cnt, 0, sizeof(cnt)); n   for (int i = 1; i <= n; i++) { n     int a; n     scanf( %d , &a); n     cnt[f[a]]--; n     f[a]++; n     cnt[f[a]]++; n     mm = max(mm, f[a]); n     bool flag = 0; n     if (cnt[1] == i) n       flag = 1; n     else if (cnt[i] == 1) n       flag = 1; n     else if (cnt[1] == 1 && cnt[mm] * mm == i - 1) n       flag = 1; n     else if (cnt[mm - 1] * (mm - 1) == i - mm && cnt[mm] == 1) n       flag = 1; n     if (flag == 1) ans = i; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const ll OO = 0x3f3f3f3f3f3f3f3fLL; n int di[8] = {0, 0, 1, -1, -1, 1, -1, 1}; n int dj[8] = {1, -1, 0, 0, 1, 1, -1, -1}; n string ys =  YES , no =  NO ; n const int N = 1e5 + 5, M = 5 * (1 << 10) + 10; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     string s; n     cin >> s; n     bool ok = 0; n     for (int i = 0; i < n; i++) { n       int cnta = 0, cntb = 0; n       for (int j = i; j < n; j++) { n         cnta += (s[j] ==  a ); n         cntb += (s[j] ==  b ); n         if (cnta == cntb) { n           cout << i + 1 <<     << j + 1 <<   n ; n           ok = 1; n           goto out; n         } n       } n     } n   out:; n     if (!ok) cout <<  -1 -1  <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string a; n void input() { n   cin >> n; n   cin >> a; n } n void solve() { n   int ans = 0; n   for (int i = 1; i < n; ++i) { n     ans += (a[i] == a[i - 1]); n   } n   cout << (ans + 1) / 2 <<   n ; n } n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     input(); n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int A = 0, B = 0, C = 0; n void add(char o) { n   if (o ==  A ) { n     A++; n   } else { n     if (o ==  B ) { n       B++; n     } else { n       if (o ==  C ) { n         C++; n       } n     } n   } n } n void sub(char o) { n   if (o ==  A ) { n     A--; n   } else { n     if (o ==  B ) { n       B--; n     } else { n       if (o ==  C ) { n         C--; n       } n     } n   } n } n int main() { n   char c[3][3]; n   for (int i = 0; i < 3; i++) { n     cin >> c[i][0]; n     cin >> c[i][1]; n     cin >> c[i][2]; n     if (c[i][1] ==  > ) { n       add(c[i][0]); n       sub(c[i][2]); n     } else { n       if (c[i][1] ==  < ) { n         sub(c[i][0]); n         add(c[i][2]); n       } n     } n   } n   if (A == 0 && B == 0 && C == 0) { n     cout <<  Impossible ; n   } else { n     if (A == -2 && B == 0 && C == 2) { n       cout <<  ABC ; n     } else { n       if (B == -2 && C == 0 && A == 2) { n         cout <<  BCA ; n       } else { n         if (C == -2 && B == 0 && A == 2) { n           cout <<  CBA ; n         } else { n           if (A == -2 && C == 0 && B == 2) { n             cout <<  ACB ; n           } else { n             if (C == -2 && A == 0 && B == 2) { n               cout <<  CAB ; n             } else { n               if (C == 2 && A == 0 && B == -2) { n                 cout <<  BAC ; n               } n             } n           } n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n const long long oo = (long long)1e18 + 1; n using namespace std; n int n, q; n int b[200002]; n pair<int, int> a[200002]; n int T[200002]; n vector<pair<int, int> > query[200002]; n int k[200002], p[200002]; n int res[200002]; n void upd(int x) { n   while (x < 200002) { n     T[x]++; n     x = x + (x & (x ^ (x - 1))); n   } n } n int get(int x) { n   int res = 0; n   while (x > 0) { n     res += T[x]; n     x = x - (x & (x ^ (x - 1))); n   } n   return res; n } n int find_kth(int cnt) { n   int l = 1; n   int r = n; n   int res = n; n   while (l <= r) { n     int mid = (l + r) >> 1; n     int count_value = get(mid); n     if (count_value >= cnt) { n       res = mid; n       r = mid - 1; n     } else n       l = mid + 1; n   } n   return res; n } n bool cmp(pair<int, int> x, pair<int, int> y) { n   return x.first > y.first || (x.first == y.first && x.second < y.second); n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     b[i] = x; n     a[i] = make_pair(x, i); n   } n   sort(a + 1, a + n + 1, cmp); n   cin >> q; n   for (int i = 1; i <= q; i++) { n     cin >> k[i] >> p[i]; n     query[k[i]].push_back(make_pair(p[i], i)); n   } n   for (int i = 1; i <= n; i++) { n     upd(a[i].second); n     for (int j = 0; j < query[i].size(); j++) { n       pair<int, int> cur_query = query[i][j]; n       int pos = cur_query.first; n       int rpos = cur_query.second; n       res[rpos] = b[find_kth(pos)]; n     } n   } n   for (int i = 1; i <= q; i++) cout << res[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n inline int read() { n   char c = getchar(); n   int tot = 1; n   while ((c <  0  || c >  9 ) && c !=  - ) c = getchar(); n   if (c ==  - ) { n     tot = -1; n     c = getchar(); n   } n   int sum = 0; n   while (c >=  0  && c <=  9 ) { n     sum = sum * 10 + c -  0 ; n     c = getchar(); n   } n   return sum * tot; n } n inline void wr(int x) { n   if (x < 0) { n     putchar( - ); n     wr(-x); n     return; n   } n   if (x >= 10) wr(x / 10); n   putchar(x % 10 +  0 ); n } n inline void wrn(int x) { n   wr(x); n   putchar(  n ); n } n inline void wri(int x) { n   wr(x); n   putchar(   ); n } n inline void wrn(int x, int y) { n   wri(x); n   wrn(y); n } n inline void wrn(int a, int b, int c) { n   wri(a); n   wrn(b, c); n } n const int N = 205; n int n, m, now, cnt, l[N], r[N], d[N]; n double ans[N], f[N][N], len, ll, rr, p1, p2, p3; n void calc(int L, int R) { n   cnt = 1; n   for (int i = 0; i <= n; i++) n     for (int j = 0; j <= n; j++) f[i][j] = 0; n   f[0][0] = 1; n   for (int i = 1; i <= n; i++) n     if (i != now) { n       if (r[i] <= L) { n         cnt++; n         continue; n       } n       if (l[i] >= R) continue; n       len = r[i] - l[i]; n       p1 = (L - l[i]) / len; n       p2 = (R - L) / len; n       p3 = (r[i] - R) / len; n       for (int j = i; j >= 0; j--) n         for (int k = i - j; k >= 0; k--) { n           f[j + 1][k] += f[j][k] * p1; n           f[j][k + 1] += f[j][k] * p2; n           f[j][k] *= p3; n         } n     } n   for (int i = 0; i + cnt <= n; i++) { n     for (int j = n - i - cnt; j >= 0; j--) n       ans[i + cnt] += f[i][j] * (R - L) / (j + 1), n           ans[i + j + 1 + cnt] -= f[i][j] * (R - L) / (j + 1); n   } n } n signed main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &l[i], &r[i]); n     d[++m] = l[i]; n     d[++m] = r[i]; n   } n   sort(d + 1, d + m + 1); n   int k = 1; n   for (int i = 2; i <= m; i++) n     if (d[i] != d[i - 1]) { n       k++; n       if (k != i) d[k] = d[i]; n     } n   m = k; n   for (int i = 1; i <= n; i++) { n     now = i; n     for (int j = 1; j <= n; j++) ans[j] = 0; n     for (int j = 1; j < m; j++) n       if (l[i] <= d[j] && r[i] >= d[j + 1]) calc(d[j], d[j + 1]); n     for (int j = 1; j <= n; j++) ans[j] += ans[j - 1]; n     for (int j = 1; j <= n; j++) printf( %.6lf  , ans[j] / (r[i] - l[i])); n     puts(  ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n const int MAXN = 16; n int n; n int mod[MAXN], rem[MAXN]; n int egcd(int a, int b, int &x, int &y) { n   if (b == 0) { n     x = 1, y = 0; n     return a; n   } n   int g = egcd(b, a % b, x, y); n   swap(x, y); n   y -= x * (a / b); n   return g; n } n int mymod(int x, int mod) { n   x %= mod; n   if (x < 0) x += mod; n   return x; n } n bool combine(int &n1, int &a1, int n2, int a2) { n   int m1, m2, g = egcd(n1, n2, m1, m2); n   if (a1 % g != a2 % g) return false; n   int n3 = n1 / g * n2, a3 = mymod(a1 + m1 * (n1 / g) * (a2 - a1), n3); n   n1 = n3, a1 = a3; n   return true; n } n void run() { n   scanf( %d , &n); n   for (int i = (0); i < (n); ++i) scanf( %d , &mod[i]); n   for (int i = (0); i < (n); ++i) scanf( %d , &rem[i]); n   long long den = 20922789888000LL, num = 0; n   for (int i = (0); i < (1 << n); ++i) { n     int cmod = 1, crem = 0, cnt = 0; n     bool ok = true; n     for (int j = (0); j < (n); ++j) n       if (i & (1 << j)) { n         ++cnt; n         if (!combine(cmod, crem, mod[j], rem[j])) ok = false; n       } n     if (!ok) continue; n     if (cnt % 2 == 0) n       num += den / cmod; n     else n       num -= den / cmod; n   } n   printf( %.9lf n , 1.0 * (den - num) / den); n } n int main() { n   run(); n   return 0; n } n


#include <bits/stdc++.h> n template <typename T> n inline bool chkmin(T &a, const T &b) { n   return a > b ? a = b, 1 : 0; n } n template <typename T> n inline bool chkmax(T &a, const T &b) { n   return a < b ? a = b, 1 : 0; n } n inline int read() { n   static char ch; n   bool sgn = false; n   while (ch = getchar(), ch <  0  || ch >  9 ) n     if (ch ==  - ) sgn = true; n   int res = ch - 48; n   while (ch = getchar(), ch >=  0  && ch <=  9 ) res = res * 10 + ch - 48; n   return sgn ? -res : res; n } n const int N = 2e5 + 5, M = 1e6 + 1; n const int mod = 1e9 + 7; n int n, m, a[N], l[N], r[N], p[N]; n int f[N], ans[N]; n int tot, pri[N], min[M], first[M]; n bool isp[M]; n inline bool cmp(const int &x, const int &y) { return r[x] < r[y]; } n inline int fpm(long long x, int Exp) { n   long long res = 1; n   for (; Exp > 0; x = x * x % mod, Exp >>= 1) n     if (Exp & 1) res = res * x % mod; n   return res; n } n inline void prime_init() { n   isp[0] = isp[1] = true; n   for (int i = 2; i < M; ++i) { n     if (!isp[i]) pri[++tot] = i, min[i] = i; n     for (int j = 1; j <= tot; ++j) { n       if (i * pri[j] >= M) break; n       isp[i * pri[j]] = true; n       min[i * pri[j]] = pri[j]; n       if (i % pri[j] == 0) break; n     } n   } n } n inline void insert(int x, const int &y) { n   for (; x <= n; x += x & -x) f[x] = (long long)f[x] * y % mod; n } n inline int Query(int x) { n   int res = 1; n   for (; x > 0; x -= x & -x) res = (long long)res * f[x] % mod; n   return res; n } n inline void add(const int &x) { n   int num = a[x], tmp; n   while (num > 1) { n     tmp = min[num]; n     if (first[tmp] > 0) { n       insert(first[tmp], fpm(tmp - 1, mod - 2)); n       insert(first[tmp], tmp); n     } n     first[tmp] = x; n     insert(x, tmp - 1); n     num /= tmp; n   } n } n int main() { n   n = read(); n   for (int i = (1); i <= (n); ++i) a[i] = read(), f[i] = 1; n   m = read(); n   for (int i = (1); i <= (m); ++i) { n     l[i] = read(); n     r[i] = read(); n     p[i] = i; n   } n   std::sort(p + 1, p + m + 1, cmp); n   prime_init(); n   int now = 0; n   for (int i = (1); i <= (m); ++i) { n     while (now < r[p[i]]) add(++now); n     ans[p[i]] = n         (long long)Query(r[p[i]]) * fpm(Query(l[p[i]] - 1), mod - 2) % mod; n   } n   for (int i = (1); i <= (m); ++i) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long st; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, m, k; n   cin >> n >> m >> k; n   if (k < n) { n     cout << (long long)(k + 1) <<     << 1; n     return 0; n   } n   k -= (long long)(n - 1); n   long long row = k / (long long)(m - 1), col; n   if ((long long)row * (long long)(m - 1) != (long long)k) row++; n   if ((long long)(row % (long long)2) == (long long)1) { n     st = (long long)(row - (long long)1) * (long long)(m - (long long)1); n     st++; n     col = k - st + (long long)2; n   } else { n     st = (long long)row * (long long)(m - 1); n     col = (long long)2 + st - k; n   } n   cout << (long long)(n - row + 1) <<     << col; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, a[100000], b, c = 0, x, y, z, m, i, j, t; n   string s; n   vector<int> v; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> x; n     v.push_back(x); n   } n   sort(v.begin(), v.end()); n   for (i = 0; i < n / 2; i++) { n     c += (v[i] + v[n - i - 1]) * (v[i] + v[n - i - 1]); n   } n   cout << c << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 3e5 + 2; n const long long N1 = 1e2 + 2; n const long long mod = 1e9 + 7; n const int MASK = 1 << 17 + 1; n const int who = 6 * N * log2(N); n long long power(long long a, long long b, long long c) { n   long long ret = 1; n   while (b) { n     if (b % 2 == 1) { n       ret = (ret * a) % c; n     } n     b /= 2; n     a = (a * a) % c; n   } n   return ret; n } n pair<int, int> a[N + 2]; n map<pair<int, int>, long long> ma, ma1; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     int len = s.size(); n     int plus = 0, minus = 0; n     for (int j = 0; j < len; j++) { n       if (s[j] ==  ( ) { n         plus++; n       } else if (s[j] ==  ) ) { n         if (plus == 0) { n           minus++; n         } else { n           plus--; n         } n       } n     } n     a[i] = make_pair(plus, minus); n     if (min(plus, minus) == 0) { n       ma[a[i]]++; n     } n   } n   long long tm1 = ma[make_pair(0, 0)]; n   long long ans = tm1 * tm1; n   ma1[make_pair(0, 0)] = 1; n   for (int i = 0; i < n; i++) { n     int plus = a[i].first, minus = a[i].second; n     if (min(plus, minus) > 0 || ma1[a[i]] > 0) { n       continue; n     } n     ma1[a[i]]++; n     ma1[make_pair(minus, plus)]++; n     ans += ma[a[i]] * ma[make_pair(minus, plus)]; n   } n   cout << ans <<   n ; n } n