#include <bits/stdc++.h> n using namespace std; n int hcf(long long a, long long b) { n   if (b == 0) return a; n   return hcf(b, a % b); n } n void solve() { n   long long n; n   cin >> n; n   long long x = hcf(n + 1, 4 * n); n   long long lcm = ((4 * n) * (n + 1)) / x; n   cout << lcm / (n + 1) + 1; n } n int main() { n   int t = 1; n   cin >> t; n   while (t--) { n     solve(); n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, n, i, k, l, r, d, kol1, kol2, ans; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> k; n   for (i = 1; i <= n; i++) { n     cin >> a; n     if (a < k) n       kol1++; n     else if (a > k) n       kol2++; n   } n   l = n; n   r = n * 3; n   while (l <= r) { n     d = (l + r) / 2; n     if (((d + 1) / 2 - 1 >= kol1) && (d - (d + 1) / 2 >= kol2)) { n       r = d - 1; n       ans = d - n; n     } else n       l = d + 1; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1e5 + 10; n int to[maxn << 1], nex[maxn << 1], head[maxn << 1], e, cnt; n int u[maxn << 1], v[maxn << 1]; n int dep[maxn << 1], siz[maxn << 1], son[maxn << 1], top[maxn << 1], n     fa[maxn << 1], id[maxn << 1]; n int sum[maxn << 2]; n int n; n void add(int x, int y) { n   to[++e] = y; n   nex[e] = head[x]; n   head[x] = e; n } n int ls(int p) { return p << 1; } n int rs(int p) { return p << 1 | 1; } n void push_up(int p) { sum[p] = sum[ls(p)] + sum[rs(p)]; } n int query(int p, int l, int r, int qx, int qy) { n   if (qx <= l && qy >= r) { n     return sum[p]; n   } n   int res = 0; n   int mid = (l + r) >> 1; n   if (qx <= mid) res = res + query(ls(p), l, mid, qx, qy); n   if (qy > mid) { n     res = res + query(rs(p), mid + 1, r, qx, qy); n   } n   return res; n } n void update(int p, int l, int r, int pos, int val) { n   if (l == r) { n     sum[p] = val; n     return; n   } n   int mid = (l + r) >> 1; n   if (pos <= mid) { n     update(ls(p), l, mid, pos, val); n   } n   if (pos > mid) update(rs(p), mid + 1, r, pos, val); n   push_up(p); n } n int getrange(int x, int y) { n   int ans = 0; n   while (top[x] != top[y]) { n     if (dep[top[x]] < dep[top[y]]) swap(x, y); n     ans = ans + query(1, 1, n, id[top[x]], id[x]); n     x = fa[top[x]]; n   } n   if (dep[x] > dep[y]) swap(x, y); n   ans = ans + query(1, 1, n, id[x] + 1, id[y]); n   return ans; n } n void dfs1(int x, int f, int deep) { n   fa[x] = f; n   siz[x] = 1; n   dep[x] = deep; n   int maxson = -1; n   for (int i = head[x]; i; i = nex[i]) { n     int y = to[i]; n     if (y == f) continue; n     dfs1(y, x, deep + 1); n     siz[x] += siz[y]; n     if (maxson < siz[y]) { n       maxson = siz[y]; n       son[x] = y; n     } n   } n } n void dfs2(int x, int topf) { n   id[x] = ++cnt; n   top[x] = topf; n   if (!son[x]) return; n   dfs2(son[x], topf); n   for (int i = head[x]; i; i = nex[i]) { n     int y = to[i]; n     if (y == fa[x] || y == son[x]) continue; n     dfs2(y, y); n   } n } n int lca(int x, int y) { n   while (top[x] != top[y]) { n     if (dep[top[x]] < dep[top[y]]) { n       swap(x, y); n     } n     x = fa[top[x]]; n   } n   if (dep[x] > dep[y]) swap(x, y); n   return x; n } n int main() { n   memset(sum, 0, sizeof(sum)); n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     scanf( %d%d , &u[i], &v[i]); n     add(u[i], v[i]); n     add(v[i], u[i]); n   } n   dfs1(1, 0, 1); n   dfs2(1, 0); n   int m; n   scanf( %d , &m); n   while (m--) { n     int op; n     scanf( %d , &op); n     if (op == 1) { n       int x; n       scanf( %d , &x); n       if (dep[u[x]] > dep[v[x]]) n         update(1, 1, n, id[u[x]], 0); n       else n         update(1, 1, n, id[v[x]], 0); n     } n     if (op == 2) { n       int x; n       scanf( %d , &x); n       if (dep[u[x]] > dep[v[x]]) { n         update(1, 1, n, id[u[x]], 1); n       } else { n         update(1, 1, n, id[v[x]], 1); n       } n     } n     if (op == 3) { n       int x, y; n       scanf( %d%d , &x, &y); n       if (getrange(x, y)) { n         printf( -1 n ); n       } else { n         printf( %d n , dep[x] + dep[y] - 2 * dep[lca(x, y)]); n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, i, k; n   cin >> n >> k; n   int a[n], c = 0; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   for (i = 0; i < n; i += 3) { n     if (a[i] <= 5 && a[i + 1] <= 5 && a[i + 2] <= 5) { n       if ((a[i] += k) <= 5 && (a[i + 1] += k) <= 5 && (a[i + 2] += k) <= 5) c++; n     } n   } n   cout << c; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1); n const double eps = 1e-9; n const int inf = 2000000000; n const long long infLL = 9000000000000000000; n inline bool checkBit(long long n, int i) { return n & (1LL << i); } n inline long long setBit(long long n, int i) { return n | (1LL << i); } n inline long long resetBit(long long n, int i) { return n & (~(1LL << i)); } n inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } n inline bool isLeapYear(long long year) { n   return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); n } n inline void normal(long long &a) { n   a %= 1000000007; n   (a < 0) && (a += 1000000007); n } n inline long long modMul(long long a, long long b) { n   a %= 1000000007, b %= 1000000007; n   normal(a), normal(b); n   return (a * b) % 1000000007; n } n inline long long modAdd(long long a, long long b) { n   a %= 1000000007, b %= 1000000007; n   normal(a), normal(b); n   return (a + b) % 1000000007; n } n inline long long modSub(long long a, long long b) { n   a %= 1000000007, b %= 1000000007; n   normal(a), normal(b); n   a -= b; n   normal(a); n   return a; n } n inline long long modPow(long long b, long long p) { n   long long r = 1; n   while (p) { n     if (p & 1) r = modMul(r, b); n     b = modMul(b, b); n     p >>= 1; n   } n   return r; n } n inline long long modInverse(long long a) { n   normal(a); n   return modPow(a, 1000000007 - 2); n } n inline long long modDiv(long long a, long long b) { n   return modMul(a, modInverse(b)); n } n int dx[] = {0, 0, +1, -1}; n int dy[] = {+1, -1, 0, 0}; n int n, a[15][15], num[15][15], cur[105], dp[2][5][5][105], lagbe[2][5][105]; n vector<int> vt[5][105]; n int knightx[] = {-2, -2, +2, +2, -1, -1, +1, +1}; n int knighty[] = {-1, +1, -1, +1, +2, -2, +2, -2}; n void valid(int ro, int col) { n   for (int i = 0; i < 8; i++) { n     int tmp = ro + knightx[i]; n     int tpp = col + knighty[i]; n     if (tmp > 0 && tmp <= n && tpp > 0 && tpp <= n) n       vt[1][num[ro][col]].push_back(num[tmp][tpp]); n   } n   for (int i = 1; i <= 10; i++) { n     int tmp = ro + i; n     int tpp = col + i; n     if (tmp > 0 && tmp <= n && tpp > 0 && tpp <= n) n       vt[2][num[ro][col]].push_back(num[tmp][tpp]); n     tmp = ro - i; n     tpp = col - i; n     if (tmp > 0 && tmp <= n && tpp > 0 && tpp <= n) n       vt[2][num[ro][col]].push_back(num[tmp][tpp]); n     tmp = ro + i; n     tpp = col - i; n     if (tmp > 0 && tmp <= n && tpp > 0 && tpp <= n) n       vt[2][num[ro][col]].push_back(num[tmp][tpp]); n     tmp = ro - i; n     tpp = col + i; n     if (tmp > 0 && tmp <= n && tpp > 0 && tpp <= n) n       vt[2][num[ro][col]].push_back(num[tmp][tpp]); n   } n   for (int i = 1; i <= 10; i++) { n     int tmp = ro + i; n     int tpp = col; n     if (tmp > 0 && tmp <= n && tpp > 0 && tpp <= n) n       vt[3][num[ro][col]].push_back(num[tmp][tpp]); n     tmp = ro - i; n     tpp = col; n     if (tmp > 0 && tmp <= n && tpp > 0 && tpp <= n) n       vt[3][num[ro][col]].push_back(num[tmp][tpp]); n     tmp = ro; n     tpp = col + i; n     if (tmp > 0 && tmp <= n && tpp > 0 && tpp <= n) n       vt[3][num[ro][col]].push_back(num[tmp][tpp]); n     tmp = ro; n     tpp = col - i; n     if (tmp > 0 && tmp <= n && tpp > 0 && tpp <= n) n       vt[3][num[ro][col]].push_back(num[tmp][tpp]); n   } n } n bool one(int beg, int fin, int ro, int col, int &mov, int &change) { n   if (ro == col) { n     change++; n     return true; n   } n   if (beg != fin) return false; n   for (int i = 0; i < vt[beg][ro].size(); i++) n     if (vt[beg][ro][i] == col) { n       mov++; n       return true; n     } n   return false; n } n bool berkoro(int num, int beg, int fin, int ro, int col, int &mov, n              int &change) { n   int res = 0, val1 = 0, val2 = 0; n   if (num == 1) return one(beg, fin, ro, col, mov, change); n   for (int i = 0, tmp; !res && i < vt[beg][ro].size(); i++) { n     tmp = vt[beg][ro][i]; n     val1 = 1, val2 = 0; n     res |= berkoro(num - 1, beg, fin, tmp, col, val1, val2); n   } n   for (int i = 1; !res && i <= 3; i++) { n     if (beg == i) continue; n     val2 = 1, val1 = 0; n     res |= berkoro(num - 1, i, fin, ro, col, val1, val2); n   } n   if (res) mov += val1, change += val2; n   return res; n } n void process(int beg, int fin, int ro, int col, int &mov, int &change) { n   for (int i = 1; i <= 4; i++) { n     mov = 0, change = 0; n     if (berkoro(i, beg, fin, ro, col, mov, change)) return; n   } n } n void process(int mark, int pos, int &mov, int &change) { n   int &ret1 = lagbe[0][mark][pos]; n   int &ret2 = lagbe[1][mark][pos]; n   if (ret1 != -1) { n     mov += ret1; n     change += ret2; n     return; n   } n   if (pos == n * n) return; n   int val1 = 0, val2 = 0, res1 = 1e9, res2 = 1e9; n   if (pos == 1) { n     for (int i = 1; i <= 3; i++) n       for (int j = 1; j <= 3; j++) { n         val1 = dp[0][i][j][pos], val2 = dp[1][i][j][pos]; n         process(j, pos + 1, val1, val2); n         if (val1 + val2 < res1 + res2) n           res1 = val1, res2 = val2; n         else if (val1 + val2 == res1 + res2 && val2 < res2) n           res1 = val1, res2 = val2; n       } n   } else { n     for (int i = 1; i <= 3; i++) { n       val1 = dp[0][mark][i][pos], val2 = dp[1][mark][i][pos]; n       process(i, pos + 1, val1, val2); n       if (val1 + val2 < res1 + res2) n         res1 = val1, res2 = val2; n       else if (val1 + val2 == res1 + res2 && val2 < res2) n         res1 = val1, res2 = val2; n     } n   } n   mov += res1, change += res2; n   ret1 = res1, ret2 = res2; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n; n   for (int i = 1, cnt = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) { n       cin >> a[i][j]; n       num[i][j] = cnt; n       cur[a[i][j]] = cnt; n       cnt++; n     } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) valid(i, j); n   for (int i = 1; i < n * n; i++) { n     for (int k = 1; k <= 3; k++) n       for (int l = 1; l <= 3; l++) n         process(k, l, cur[i], cur[i + 1], dp[0][k][l][i], dp[1][k][l][i]); n   } n   memset(lagbe, -1, sizeof(lagbe)); n   int mov = 0, change = 0; n   process(0, 1, mov, change); n   cout << mov + change <<     << change <<   n ; n   return 0; n } n


#include<bits/stdc++.h> n #define pb push_back n #define all(v) v.begin(),v.end() n #define allr(v) v.rbegin(),v.rend() n #define SORT(v) sort(all(v)) n #define int long long n #define ff first n #define ss second n #define mod 1000000007 n  n using namespace std; n  n void oj() n { n   ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); n   #ifndef ONLINE_JUDGE   n   freopen( input.txt ,  r , stdin); n   freopen( output.txt ,  w , stdout); n   #endif n } n  n void solve() n { n   int n, k; cin>>n>>k; n   vector<int>h(n); n   vector<pair<int,int>>val; n  n   for(int i = 0; i<n; i++) n     cin>>h[i]; n  n   int mini = h[0]; n   int maxi = h[0] + k; n  n   for(int i = 1; i<n - 1; i++){ n  n     if(h[i] + 2*k - 1 <= mini || h[i] >= maxi) n     { n       cout<< NO <<endl;  n       return; n     } n  n  n     maxi = min(maxi + k - 1LL, h[i] + 2 * k - 1LL); n     mini = max(mini - k + 1LL, h[i] ); n  n   } n  n   if(h[n-1] + k <= mini || h[n-1] >= maxi) n   { n     cout<< NO <<endl; n       return; n   } n   cout<< YES <<endl; n  n  n  n } n signed main() n { n   oj(); n   int t; cin>>t; n   while(t--) n     solve(); n  n   return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int NMAX = 2020; n vector<int> Gph[2][NMAX]; n int N, A, B, K; n int st[2][NMAX], dr[2][NMAX], edges[2][NMAX]; n int val[2][NMAX][NMAX]; n int dp[NMAX / 2]; n void dfs(int node) { n   if (node != 1) edges[K][node] = 1; n   for (int i = 0; i < Gph[K][node].size(); i++) { n     int v = Gph[K][node][i]; n     dfs(v); n     st[K][node] = min(st[K][node], st[K][v]); n     dr[K][node] = max(dr[K][node], dr[K][v]); n     edges[K][node] += edges[K][v]; n   } n   val[K][st[K][node]][dr[K][node]] = n       max(val[K][st[K][node]][dr[K][node]], edges[K][node]); n } n int main() { n   scanf( %d%d , &N, &A); n   int x; n   K = 0; n   st[K][1] = A + 1; n   dr[K][1] = 0; n   for (int i = 2; i <= A; i++) { n     st[K][i] = A + 1; n     dr[K][i] = 0; n     scanf( %d , &x); n     Gph[K][x].push_back(i); n   } n   for (int i = 1; i <= N; i++) { n     scanf( %d , &x); n     st[K][x] = dr[K][x] = i; n   } n   dfs(1); n   scanf( %d , &B); n   K = 1; n   st[K][1] = B + 1; n   dr[K][1] = 0; n   for (int i = 2; i <= B; i++) { n     st[K][i] = B + 1; n     dr[K][i] = 0; n     scanf( %d , &x); n     Gph[K][x].push_back(i); n   } n   for (int i = 1; i <= N; i++) { n     scanf( %d , &x); n     st[K][x] = dr[K][x] = i; n   } n   dfs(1); n   for (int i = 1; i <= N; i++) n     for (int j = i; j <= N; j++) n       dp[j] = max(dp[j], dp[i - 1] + max(val[0][i][j], val[1][i][j])); n   printf( %d , dp[N]); n   return 0; n } n


#include <bits/stdc++.h> n struct Mock { n   Mock(int n, const std::string &s) : n(n), s(s) {} n   std::vector<std::string> ask(int l, int r) { n     if (l > r) { n       return {}; n     } n     std::vector<std::string> substrings; n     for (int i = l - 1; i < r; ++i) { n       for (int j = 1; i + j <= r; ++j) { n         auto ss = s.substr(i, j); n         std::shuffle(ss.begin(), ss.end(), gen); n         substrings.push_back(ss); n       } n     } n     std::shuffle(substrings.begin(), substrings.end(), gen); n     return substrings; n   } n   void out(const std::string &t) { assert(s == t); } n   int n; n  n  private: n   std::string s; n   std::mt19937 gen; n }; n struct IO { n   IO() { scanf( %d , &n); } n   std::vector<std::string> ask(int l, int r) { n     if (l > r) { n       return {}; n     } n     printf( ? %d %d n , l, r); n     fflush(stdout); n     std::vector<char> buf(r - l + 2); n     std::vector<std::string> substrings; n     int size = (r - l + 2) * (r - l + 1) / 2; n     for (int i = 0; i < size; ++i) { n       scanf( %s , buf.data()); n       substrings.push_back(buf.data()); n     } n     return substrings; n   } n   void out(const std::string &t) { n     printf( ! %s n , t.c_str()); n     fflush(stdout); n   } n   int n; n }; n static const int C = 26; n using Count = std::array<int, C>; n Count toCount(const std::string &s) { n   Count result; n   memset(result.data(), 0, sizeof(result)); n   for (char c : s) { n     result[c -  a ]++; n   } n   return result; n } n template <typename IO> n std::string solve(IO &io) { n   int n = io.n; n   if (n == 1) { n     return io.ask(1, 1)[0]; n   } n   int n2 = n >> 1; n   std::vector<int> s(n, 0); n   { n     std::map<Count, int> halfs; n     for (auto &&s : io.ask(1, n2)) { n       halfs[toCount(s)]++; n     } n     for (auto &&s : io.ask(2, n2)) { n       halfs[toCount(s)]--; n     } n     std::vector<std::pair<int, Count>> counts; n     for (auto &&kv : halfs) { n       if (kv.second) { n         int size = 0; n         for (int c : kv.first) { n           size += c; n         } n         counts.emplace_back(size, kv.first); n       } n     } n     std::sort(counts.begin(), counts.end()); n     assert(static_cast<int>(counts.size()) == n2); n     for (int i = 0; i < n2; ++i) { n       auto count = counts[i].second; n       for (int j = 0; j < i; ++j) { n         count[s[j]]--; n       } n       while (!count[s[i]]) { n         s[i]++; n       } n     } n   } n   { n     std::vector<Count> counts(n + 1); n     for (auto &&s : io.ask(1, n)) { n       auto count = toCount(s); n       for (int j = 0; j < C; ++j) { n         counts[s.length()][j] += count[j]; n       } n     } n     for (int i = n + 1 >> 1; i >= 1; --i) { n       auto count = counts[i]; n       for (int j = 0; j < C; ++j) { n         count[j] -= counts[i - 1][j]; n       } n       int left = i - 1, right = n - i; n       for (int j = left; j < right; ++j) { n         count[s[j]]--; n       } n       while (!count[s[right]]) { n         s[right]++; n       } n     } n   } n   std::string buf(n,    ); n   for (int i = 0; i < n; ++i) { n     buf[i] =  a  + s[i]; n   } n   return buf; n } n int main() { n   IO io; n   io.out(solve(io)); n } n


#include <bits/stdc++.h> n using namespace std; n const long long LIM = 1e18; n long long root(long long x) { n   long long L = 0LL, R = 1e9 + 7; n   while (L < R - 1) { n     long long mid = (L + R) >> 1LL; n     if (mid * mid <= x) n       L = mid; n     else n       R = mid; n   } n   return L; n } n int main() { n   set<long long> s; n   for (long long i = 2; i <= 1e6; i++) { n     for (long long j = i * i * i; j <= LIM; j *= i) { n       long long val = sqrt(j); n       if (val * val != j) s.insert(j); n       if (j > LIM / i) break; n     } n   } n   vector<long long> v(s.begin(), s.end()); n   int t; n   scanf( %d , &t); n   while (t--) { n     long long L, R; n     scanf( %lld %lld , &L, &R); n     long long ans = n         upper_bound(v.begin(), v.end(), R) - lower_bound(v.begin(), v.end(), L); n     ans += root(R) - root(L - 1); n     printf( %lld n , ans); n   } n   return 0; n } n


#include <iostream> n #include <bits/stdc++.h> n  n using namespace std; n  n int main () { n  int t; n  cin >> t; n  for (int k=0; k<t;k++){ n   int n; n   cin >> n; n   int a[n]; n   for (int i=0; i<n; i++){ n    cin >> a[i]; n   } n   for (int j=n-1; j>=0; j--){ n    if(a[j]+j < n){ n     a[j] = a[j]+a[a[j]+j]; n    } n   } n   int max=0; n   for (int i=0; i<n; i++){ n    if(a[i]>max){ n     max=a[i]; n    } n   } n   cout << max << endl; n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n long long ncr_2(long long n, long long k) { n   if (k > n) return 0; n   if (k * 2 > n) k = n - k; n   if (k == 0) return 1; n   long long result = n; n   for (long long i = 2; i <= k; ++i) { n     result = result * (n - i + 1); n     result /= i; n   } n   return result; n } n long long pow(int x, unsigned int y, int p) { n   int res = 1; n   x = x % p; n   if (x == 0) return 0; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n long long _pow(int a, int b) { n   if (!b) return 1; n   int temp = _pow(a, b / 2); n   temp = (temp * temp); n   if (b % 2) return (a * temp); n   return temp; n } n int invmod(int a, int m) { return pow(a, m - 2, m); } n long long feelthepower(long long x, long long y) { n   long long res = 1LL; n   while (y) { n     if (y & 1) { n       res = (res * 1LL * x) % 1000000007; n     } n     x = (x * 1LL * x) % 1000000007; n     y /= 2; n   } n   return res; n } n vector<int> Adj[100001], A; n int cnt = 0; n vector<bool> vis(100001, false); n void dfs(int s) { n   if (vis[s]) return; n   vis[s] = true; n   A.push_back(s); n   cnt++; n   for (auto v : Adj[s]) { n     if (!vis[v]) { n       dfs(v); n     } n   } n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int tc = 1; n   for (long long ti = 0; ti < tc; ti++) { n     int n; n     cin >> n; n     vector<int> V(n); n     map<int, int> mp; n     for (long long i = 0; i < n; i++) { n       cin >> V[i]; n       mp[V[i]] = i + 1; n     } n     vector<int> vec(V); n     sort(vec.begin(), vec.end()); n     for (long long i = 0; i < n; i++) { n       Adj[mp[vec[i]]].push_back(i + 1); n       Adj[i + 1].push_back(mp[vec[i]]); n     } n     vector<int> Store[100001]; n     int j = 0, cnt1 = 0; n     for (int i = 1; i <= n; i++) { n       if (!vis[i]) { n         cnt = 0; n         dfs(i); n         Store[j].push_back(cnt); n         for (auto x : A) { n           Store[j].push_back(x); n         } n         A.clear(); n         j++; n       } n     } n     cout << j << endl; n     for (long long i = 0; i < j; i++) { n       for (long long k = 0; k < Store[i].size(); k++) { n         cout << Store[i][k] <<    ; n       } n       cout << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e16; n const int maxn = 1e5; n pair<long long, int> tree[4 * maxn + 11]; n long long ans[maxn + 11]; n long long minn[4 * maxn + 11], lazy[4 * maxn + 11]; n set<pair<long long, int> > t; n void push_up(int rt) { tree[rt] = min(tree[rt << 1], tree[rt << 1 | 1]); } n void build(int rt, int l, int r) { n   if (l == r) { n     tree[rt] = make_pair(inf, l); n     return; n   } n   int mid = (l + r) >> 1; n   build(rt << 1, l, mid); n   build(rt << 1 | 1, mid + 1, r); n   push_up(rt); n } n void update(int rt, int l, int r, int pos, long long val) { n   if (l > pos || r < pos) return; n   if (l == r) { n     tree[rt] = make_pair(val, l); n     return; n   } n   int mid = (l + r) >> 1; n   update(rt << 1, l, mid, pos, val); n   update(rt << 1 | 1, mid + 1, r, pos, val); n   push_up(rt); n } n void Push_up(int rt) { minn[rt] = min(minn[rt << 1], minn[rt << 1 | 1]); } n void push_down(int rt) { n   if (minn[rt << 1] < inf) { n     minn[rt << 1] -= lazy[rt]; n     lazy[rt << 1] += lazy[rt]; n   } n   if (minn[rt << 1 | 1] < inf) { n     minn[rt << 1 | 1] -= lazy[rt]; n     lazy[rt << 1 | 1] += lazy[rt]; n   } n   lazy[rt] = 0; n   minn[rt << 1] = max(minn[rt << 1], 0ll); n   minn[rt << 1 | 1] = max(minn[rt << 1 | 1], 0ll); n } n void update(int rt, int l, int r, int al, int ar, long long val) { n   if (l > ar || r < al) return; n   if (l >= al && r <= ar) { n     minn[rt] -= val; n     minn[rt] = max(minn[rt], 0ll); n     lazy[rt] += val; n     return; n   } n   int mid = (l + r) >> 1; n   if (lazy[rt]) push_down(rt); n   update(rt << 1, l, mid, al, ar, val); n   update(rt << 1 | 1, mid + 1, r, al, ar, val); n   Push_up(rt); n } n pair<long long, int> query(int rt, int l, int r, int al, int ar) { n   if (l > ar || r < al) return make_pair(inf, maxn + 1); n   if (l >= al && r <= ar) return tree[rt]; n   int mid = (l + r) >> 1; n   return min(query(rt << 1, l, mid, al, ar), n              query(rt << 1 | 1, mid + 1, r, al, ar)); n } n pair<long long, int> query(int rt, int l, int r) { n   int mid = (l + r) >> 1; n   if (l == r) return make_pair(minn[rt], l); n   if (lazy[rt]) push_down(rt); n   if (minn[rt << 1] <= minn[rt << 1 | 1]) return query(rt << 1, l, mid); n   return query(rt << 1 | 1, mid + 1, r); n } n void upd(int rt, int l, int r, int pos, long long val) { n   if (l > pos || r < pos) return; n   if (l == r) { n     minn[rt] = val; n     return; n   } n   int mid = (l + r) >> 1; n   if (lazy[rt]) push_down(rt); n   upd(rt << 1, l, mid, pos, val); n   upd(rt << 1 | 1, mid + 1, r, pos, val); n   Push_up(rt); n } n int main() { n   int n; n   long long p; n   cin >> n >> p; n   build(1, 1, n); n   for (int i = 1; i <= 4 * n; i++) minn[i] = inf, lazy[i] = 0; n   for (int i = 1; i <= n; i++) { n     long long x; n     cin >> x; n     update(1, 1, n, i, x); n     upd(1, 1, n, i, x); n   } n   int out = 0; n   queue<int> q; n   set<int> s; n   long long Pas = 0; n   long long res = p; n   while (out < n) { n     if (q.empty()) { n       pair<long long, int> pi = query(1, 1, n); n       Pas += pi.first; n       update(1, 1, n, 1, n, pi.first); n       s.insert(pi.second); n       q.push(pi.second); n       upd(1, 1, n, pi.second, inf); n     } else { n       int lef = *s.begin(); n       if (lef == 1) { n         Pas += p; n         res = p; n         update(1, 1, n, 1, n, p); n         ans[q.front()] = Pas; n         update(1, 1, n, q.front(), inf); n         s.erase(q.front()); n         q.pop(); n         out++; n       } else { n         pair<long long, int> pi = query(1, 1, n, 1, lef - 1); n         if (Pas + res > pi.first) { n           res -= pi.first - Pas; n           q.push(pi.second); n           upd(1, 1, n, pi.second, inf); n           s.insert(pi.second); n         } else { n           Pas += p; n           res = p; n           update(1, 1, n, 1, n, p); n           ans[q.front()] = Pas; n           update(1, 1, n, q.front(), inf); n           s.erase(q.front()); n           q.pop(); n           out++; n         } n       } n     } n   } n   for (int i = 1; i <= n; i++) cout << ans[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n  n typedef long long ll; n typedef pair<ll,ll> pi; n typedef vector <ll> vi; n typedef vector <pi> vpi; n typedef pair<pi,ll> pii; n typedef set <ll> si; n typedef long double ld; n #define f first n #define s second n #define FOR(i,s,e) for(ll i=s;i<=ll(e);++i) n #define DEC(i,s,e) for(ll i=s;i>=ll(e);--i) n #define pb push_back n #define all(x) (x).begin(), (x).end() n #define lbd(x, y) lower_bound(all(x), y) n #define ubd(x, y) upper_bound(all(x), y) n #define aFOR(i,x) for (auto i: x) n #define mem(x,i) memset(x,i,sizeof x) n #define fast ios_base::sync_with_stdio(false),cin.tie(0) n  n int N,Q,a,b; n string S,F; n struct node{ n  int s,e,m, val, lazy = 0, sz; n  node *l,*r; n  node (int ss,int ee){ n   s = ss; e = ee; m = (s+e) / 2; sz = e - s + 1; n   if (s != e){ n    l = new node(s,m); r = new node(m+1,e); n   } n  } n   n  void prop(){ n   if (lazy == 0 || s == e) return; n   l->lazy = r->lazy = lazy; n   l->val = lazy * l->sz; r->val = lazy * r->sz; n   lazy = 0; n  } n   n  void upd(int a,int b,int c){ n   prop(); n   if (a <= s && e <= b){ n    lazy = c; n    val = c * sz; n   }else if (a > e || s > b) return; n   else{ n    l->upd(a,b,c); r->upd(a,b,c); n    val = l->val + r->val; n   } n  } n   n  int qry(int a,int b){ n   prop(); n   if (a <= s && e <= b) return val; n   else if (a > e || s > b) return 0; n   else return l->qry(a,b) + r->qry(a,b); n  } n }*root; n  n void solve(){ n  cin >> N >> Q >> S >> F; n  swap(S,F); n  root = new node(1,N); n  n  FOR(i,0,N-1) root->upd(i + 1,i + 1, (S[i] ==  0  ? -1 : 1)); n   n  vpi v; n  FOR(i,1,Q){ n   cin >> a >> b; n   v.pb(pi(a,b)); n  } n  reverse(all(v)); n  aFOR(i,v){ n   a = i.f, b = i.s; n    n   int x = root->qry(a,b); n   if (x == 0){ n    cout <<  NO n ; return; n   }else if (x < 0) root->upd(a,b,-1); n   else root->upd(a,b,1); n  } n  FOR(i,0,N-1){ n   if (root->qry(i+1,i+1) != (F[i] ==  0  ? -1 : 1)){ n    cout <<  NO n ; return; n   } n  } n  cout <<  YES n ; n } n int main(){ n  fast; n  int TC; n  cin >> TC; n  while (TC--) solve(); n } n  n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N; n   cin >> N; n   int black = 0, white = 1000000001; n   cout << 0 <<     << 0 << endl; n   cout << flush; n   string S; n   cin >> S; n   if (S ==  white ) swap(white, black); n   for (int i = 1; i < N; i++) { n     int mid = (white + black) / 2; n     cout << mid <<     << 0 << endl; n     cout << flush; n     cin >> S; n     if (S ==  black ) { n       black = mid; n     } else { n       white = mid; n     } n   } n   if (black + 1 < white || white + 1 < black) n     cout << (black + white) / 2 <<     << 1 <<     << (black + white) / 2 <<     n          << 2 << endl; n   else { n     if (black < white) { n       if (black > 0) n         cout << black <<     << 1 <<     << black - 1 <<     << 3 << endl; n       else n         cout << black + 1 <<     << 1 <<     << black + 2 <<     << 3 << endl; n     } else { n       if (white > 0) n         cout << white <<     << 1 <<     << white - 1 <<     << 3 << endl; n       else n         cout << white + 1 <<     << 1 <<     << white + 2 <<     << 3 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long modPow(long long a, long long b); n long long modInv(long long a); n void solve() { n   long long n; n   cin >> n; n   long long a[n]; n   for (long long i = 0; i < n; i++) cin >> a[i]; n   long long dp[n + 1][n + 1]; n   memset(dp, 0, sizeof(dp)); n   long long ans = 0; n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < i - 1; j++) { n       dp[a[j]][a[i - 1]]++; n     } n     for (long long j = i + 1; j < n; j++) { n       ans += dp[a[i]][a[j]]; n     } n   } n   cout << ans <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long test = 1; n   cin >> test; n   while (test--) solve(); n } n long long modPow(long long a, long long b) { n   if (b == 0) return 1; n   if (b % 2 == 0) { n     long long x = a * a; n     x %= 1000000007; n     return modPow(x, b / 2); n   } n   return (a * modPow(a, b - 1)) % 1000000007; n } n long long modInv(long long a) { return modPow(a, 1000000007 - 2); } n


#include <bits/stdc++.h> n using namespace std; n char grid[2010][2010]; n bool vis[2010][2010]; n int dx[] = {0, 0, 1, -1}; n int dy[] = {-1, 1, 0, 0}; n bool valid(int x, int y, int n, int m) { n   if (x == n || y == m || x == -1 || y == -1 || vis[x][y] || n       grid[x][y] ==  * ) { n     return 0; n   } n   return 1; n } n int main() { n   int n, m; n   int sr, sc; n   int x, y; n   scanf( %d%d%d%d%d%d , &n, &m, &sr, &sc, &x, &y); n   sr--, sc--; n   for (int i = 0; i < n; i++) { n     scanf( %s , grid[i]); n   } n   int ans = 0; n   queue<pair<pair<int, int>, pair<int, int> > > q[2]; n   int curQ = 0, nxtQ = 1; n   q[curQ].push(make_pair(make_pair(sr, sc), make_pair(x, y))); n   while (q[curQ].size()) { n     while (q[curQ].size()) { n       pair<pair<int, int>, pair<int, int> > cur = q[curQ].front(); n       q[curQ].pop(); n       int curX = cur.first.first; n       int curY = cur.first.second; n       int rem[] = {cur.second.first, cur.second.second}; n       if (valid(curX, curY, n, m) == 0) { n         continue; n       } n       ans++; n       vis[curX][curY] = 1; n       for (int i = 0; i < 4; i++) { n         if (i < 2) { n           if (rem[i] == 0) continue; n           rem[i]--; n           q[nxtQ].push(make_pair(make_pair(curX + dx[i], curY + dy[i]), n                                  make_pair(rem[0], rem[1]))); n           rem[i]++; n         } else { n           q[curQ].push(make_pair(make_pair(curX + dx[i], curY + dy[i]), n                                  make_pair(rem[0], rem[1]))); n         } n       } n     } n     curQ = !curQ; n     nxtQ = !nxtQ; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const int MOD = 1e9 + 7; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, i; n   cin >> n; n   while (n--) { n     long long int k, t; n     cin >> k >> t; n     cout << t + (k - 1) * 9 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mxn = 5e5 + 123; n const int inf = 1e9; n const int mod = 1e9 + 7; n int a[mxn], n; n vector<int> v; n int main() { n   long long s; n   cin >> s; n   for (long long i = 1; i <= s; i++) { n     long long n; n     cin >> n; n     long long sum = 0; n     while (n > 0) { n       if (n % 2 == 1) sum++; n       n /= 2; n     } n     cout << (1ll << sum) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300000 + 10; n long long num[maxn], dp[maxn][15]; n void Solve() { n   int n, m, k; n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= n; i++) scanf( %lld , &num[i]); n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (j == 1) n         dp[i][j] = max(dp[i - 1][m] + num[i] - k, num[i] - k); n       else { n         if (i < j) break; n         dp[i][j] = dp[i - 1][j - 1] + num[i]; n       } n       ans = max(ans, dp[i][j]); n     } n   } n   printf( %lld n , ans); n } n int main() { n   Solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long c; n int last, l, r; n int q[200010]; n long long a[400010], ans[200010]; n int main() { n   scanf( %d%lld , &n, &c); n   for (int i = 1; i < 2 * n; i++) scanf( %lld , &a[i]), a[i] += a[i - 1]; n   for (int i = 1; i <= n; i++) { n     while (last < i && a[2 * i - 1] - a[2 * last] >= c) last++; n     while (l <= r && a[2 * i - 1] - a[2 * q[l]] >= c) l++; n     if (last) ans[i] = ans[last - 1] + c; n     if (l <= r) ans[i] = max(ans[i], a[2 * i - 1] + ans[q[l]] - a[2 * q[l]]); n     while (l <= r && ans[i] - a[2 * i] >= ans[q[r]] - a[2 * q[r]]) r--; n     q[++r] = i; n   } n   printf( %lld n , ans[n]); n } n


#include <bits/stdc++.h> n using namespace std; n long long H[200005]; n int a[200005]; n long long c[200005 << 2]; n long long s[200005 << 2]; n long long lz[200005 << 2]; n vector<int> v[200005]; n int u; n int yl, yr; n void build(int o, int l, int r) { n   if (l == r) { n     lz[o] = l; n     c[o] = l; n     s[o] = l; n     return; n   } n   int mid = (l + r) >> 1; n   build(o * 2, l, mid); n   build(o * 2 + 1, mid + 1, r); n   lz[o] = -1; n   c[o] = c[o * 2] + c[o * 2 + 1]; n   s[o] = min(s[o * 2], s[o * 2 + 1]); n } n void pushdown(int o, int l, int r) { n   if (l == r) return; n   if (lz[o] == -1) return; n   lz[o * 2] = lz[o]; n   lz[o * 2 + 1] = lz[o]; n   s[o * 2] = lz[o]; n   s[o * 2 + 1] = lz[o]; n   int mid = (l + r) >> 1; n   c[o * 2] = lz[o] * (mid - l + 1); n   c[o * 2 + 1] = lz[o] * (r - mid); n   lz[o] = -1; n } n void update(int o, int l, int r) { n   if (yl <= l && yr >= r) { n     c[o] = 1LL * u * (r - l + 1); n     s[o] = u; n     lz[o] = u; n     return; n   } n   pushdown(o, l, r); n   int mid = (l + r) >> 1; n   if (yl <= mid) update(o * 2, l, mid); n   if (yr > mid) update(o * 2 + 1, mid + 1, r); n   c[o] = c[o * 2] + c[o * 2 + 1]; n   s[o] = min(s[o * 2], s[o * 2 + 1]); n   if (lz[o * 2] == lz[o * 2 + 1] && lz[o * 2] >= 0) lz[o] = lz[o * 2]; n } n int query(int o, int l, int r) { n   if (l == r && c[o] < u) return l; n   if (l == r && c[o] >= u) return 0; n   if (lz[o] != -1 && lz[o] < u) return r; n   if (lz[o] != -1 && lz[o] >= u) return 0; n   int mid = (l + r) >> 1; n   if (s[o * 2 + 1] < u) n     return query(o * 2 + 1, mid + 1, r); n   else n     return query(o * 2, l, mid); n } n int main() { n   int n; n   scanf( %d , &n); n   int mx = -1; n   memset(a, 0, sizeof(a)); n   for (int i = 1; i < n + 1; ++i) { n     int tmp; n     scanf( %d , &tmp); n     a[tmp] = i; n     mx = max(mx, tmp); n   } n   for (int i = 1; i <= mx; ++i) { n     for (int j = 1; j * i <= mx; ++j) { n       if (a[j * i]) v[i].push_back(a[j * i]); n     } n   } n   for (int i = 0; i <= mx; ++i) sort(v[i].begin(), v[i].end()); n   long long sum = 1LL * n * (n + 1); n   build(1, 1, n); n   for (int i = mx; i >= 0; --i) { n     H[i] = sum - c[1]; n     int k = v[i].size(); n     if (k < 2) { n       continue; n     } n     u = n + 1; n     yl = v[i][1] + 1; n     yr = n; n     if (yl <= yr) update(1, 1, n); n     u = v[i][k - 1]; n     int pos = query(1, 1, n); n     pos = min(pos, v[i][1]); n     if (pos > v[i][0] && pos <= v[i][1]) { n       yl = v[i][0] + 1; n       yr = pos; n       update(1, 1, n); n     } n     u = v[i][k - 2]; n     pos = query(1, 1, n); n     pos = min(pos, v[i][0]); n     if (pos >= 1 && pos <= v[i][0]) { n       yl = 1; n       yr = pos; n       update(1, 1, n); n     } n   } n   long long ans = 0; n   for (int i = 1; i <= mx; ++i) { n     ans += 1LL * i * (H[i] - H[i - 1]); n   } n   printf( %I64d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int MM = 1e9 + 7; n const double eps = 1e-8; n const int MAXN = 2e6 + 10; n int n, m; n int a[MAXN], Q[MAXN], cnt[MAXN], ans[MAXN]; n int f[MAXN]; n void prework() {} n void read() {} n int gao(int k) { n   if (f[k] != -1) return f[k]; n   int ret = 0; n   int now = 0; n   for (int i = 1; i <= n; i++) { n     if (cnt[a[i]] == 0) { n       if (now == k) { n         ret++; n         while (now) { n           cnt[Q[now--]] = 0; n         } n         now = 1; n         Q[now] = a[i]; n         cnt[a[i]] = 1; n       } else { n         Q[++now] = a[i]; n         cnt[a[i]] = 1; n       } n     } else { n       ; n     } n   } n   ret += (now > 0); n   while (now) { n     cnt[Q[now--]] = 0; n   } n   return f[k] = ret; n } n void work(int l, int r) { n   if (l >= r || gao(l) == gao(r)) { n     for (int i = l; i <= r; i++) ans[i] = gao(l); n     return; n   } n   m = (l + r) >> 1; n   work(l, m); n   work(m + 1, r); n } n void solve(int casi) { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   for (int i = 0; i <= n; i++) f[i] = -1; n   work(1, n); n   for (int i = 1; i <= n; i++) printf( %d%c , ans[i],    n [i == n]); n } n void printans() {} n int main() { n   prework(); n   int T = 1; n   for (int i = 1; i <= T; i++) { n     read(); n     solve(i); n     printans(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, l, m, sol; n int a[10]; n bool r[333][333]; n bool ok; n int main() { n   cin >> n; n   for (int(i) = (0); (i) < (n); ++(i)) cin >> a[i]; n   if (n == 1) { n     if (a[0] == 0) n       cout <<  BitAryo  << endl; n     else n       cout <<  BitLGM  << endl; n     return 0; n   } n   if (n == 2) { n     r[0][0] = false; n     for (int(i) = (0); (i) < (a[0] + 1); ++(i)) n       for (int(j) = (0); (j) < (a[1] + 1); ++(j)) { n         if (i + j == 0) continue; n         ok = false; n         m = min(i, j); n         while (m > 0) { n           ok |= (r[i - m][j - m] == 0); n           m--; n         } n         m = i; n         while (m > 0) { n           ok |= (r[i - m][j] == 0); n           m--; n         } n         m = j; n         while (m > 0) { n           ok |= (r[i][j - m] == 0); n           m--; n         } n         r[i][j] = ok; n       } n     if (r[a[0]][a[1]]) n       cout <<  BitLGM  << endl; n     else n       cout <<  BitAryo  << endl; n     return 0; n   } n   if (a[0] ^ a[1] ^ a[2]) n     cout <<  BitLGM  << endl; n   else n     cout <<  BitAryo  << endl; n   return 0; n } n


#include <bits/stdc++.h> n const long long LL_INF = (long long)2e18 + 5; n using namespace std; n const long long N = 2e5 + 10; n void solve() { n   long long n, a0, a, b0, b, k; n   cin >> n; n   vector<long long> v(n), pref(n + 1); n   ; n   { n     for (auto &i : v) cin >> i; n   } n   sort(v.begin(), v.end()), reverse(v.begin(), v.end()); n   for (long long i = (long long)(1); i < (long long)(n + 1); i++) n     pref[i] = pref[i - 1] + v[i - 1]; n   cin >> a0 >> a >> b0 >> b >> k; n   if (a0 < b0) swap(a0, b0), swap(a, b); n   auto sum = [&](long long l, long long r) { return pref[r] - pref[l]; }; n   long long ctall = 0, cta = 0, ctb = 0; n   for (long long i = (long long)(1); i < (long long)(n + 1); i++) { n     if (i % a == 0 and i % b == 0) n       ctall++; n     else if (i % a == 0) n       cta++; n     else if (i % b == 0) n       ctb++; n     if (sum(0, ctall) * (a0 + b0) / 100 + sum(ctall, ctall + cta) * a0 / 100 + n             sum(ctall + cta, ctall + cta + ctb) * b0 / 100 >= n         k) { n       cout << i <<   n ; n       return; n     } n   } n   cout <<  -1 n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long tt; n   cin >> tt; n   while (tt--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n struct cate { n   long long num, t; n   bool operator<(const cate& other) const { return t > other.t; } n } a[maxn]; n int n; n set<long long> has; n map<long long, long long> fa; n long long find(long long x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i].num; n     fa[a[i].num] = a[i].num; n   } n   for (int i = 1; i <= n; ++i) cin >> a[i].t; n   sort(a + 1, a + n + 1); n   long long ans = 0; n   for (int i = 1; i <= n; ++i) { n     long long cur = a[i].num; n     if (has.find(cur) != has.end()) { n       cur = find(cur); n       cur += 1; n       fa[cur] = cur; n       ans += (cur - a[i].num) * a[i].t; n     } n     has.insert(cur); n     if (has.find(cur - 1) != has.end()) fa[cur - 1] = cur; n     if (has.find(cur + 1) != has.end()) fa[cur] = cur + 1; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long LL_MIN = std::numeric_limits<long long>::min(); n const long long LL_MAX = std::numeric_limits<long long>::max(); n template <class T, class R> n ostream& operator<<(ostream& o, const pair<T, R>& p) { n   o <<  (  << p.first <<  ,   << p.second <<  ) ; n   return o; n } n template <class T, class R> n istream& operator>>(istream& o, pair<T, R>& p) { n   o >> p.first >> p.second; n   return o; n } n template <class T> n string to_string(const T& x) { n   stringstream o; n   o <<  [ ; n   for (__typeof((x).begin()) i = ((x).begin()); i != (x).end(); ++i) { n     o << ((i != x.begin()) ?  ,   :   ) << *i; n   } n   o <<  ] ; n   string r; n   getline(o, r); n   return r; n } n template <class T> n T get_next(string& s) { n   stringstream ss(s); n   T r; n   ss >> r; n   getline(ss, s); n   return r; n } n template <class T> n void split_string(const string& s, vector<T>& o) { n   stringstream ss(s); n   o.clear(); n   T x; n   while (ss >> x) { n     o.push_back(x); n   } n } n void trim(string& s) { n   stringstream ss; n   ss << s; n   s.clear(); n   ss >> s; n } n template <class T, class R> n ostream& operator<<(ostream& o, const map<T, R>& x) { n   o << to_string(x); n   return o; n } n template <class T> n ostream& operator<<(ostream& o, const vector<T>& x) { n   o << to_string(x); n   return o; n }; n template <class T> n ostream& operator<<(ostream& o, const set<T>& x) { n   o << to_string(x); n   return o; n }; n template <class T> n ostream& operator<<(ostream& o, const deque<T>& x) { n   o << to_string(x); n   return o; n }; n const long long INFL = 1000000000LL * 1000000000LL + 1000LL; n const int INF = 1000000000 + 1000; n const long long MOD = 1000000009; n struct Tri { n   int a, b, c; n   Tri(int a, int b, int c) : a(a), b(b), c(c) {} n }; n queue<Tri> q; n int n, m, a, b; n vector<int> graf[510]; n bool sol[510][510][2]; n int dist[510][510][2], f[510][510][2]; n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d %d , &a, &b); n     graf[a].push_back(b); n     graf[b].push_back(a); n   } n   dist[1][n][0] = 0; n   q.push(Tri(1, n, 0)); n   sol[1][n][0] = true; n   while (q.size()) { n     Tri t = q.front(); n     q.pop(); n     if (t.c == 0) { n       for (__typeof((graf[t.a]).begin()) a = ((graf[t.a]).begin()); n            a != (graf[t.a]).end(); ++a) { n         if (!sol[*a][t.b][1]) { n           sol[*a][t.b][1] = true; n           dist[*a][t.b][1] = dist[t.a][t.b][0] + 1; n           q.push(Tri(*a, t.b, 1)); n           f[*a][t.b][1] = t.a; n         } n       } n     } else { n       for (__typeof((graf[t.b]).begin()) b = ((graf[t.b]).begin()); n            b != (graf[t.b]).end(); ++b) { n         if (!sol[t.a][*b][0] && t.a != *b) { n           sol[t.a][*b][0] = true; n           dist[t.a][*b][0] = dist[t.a][t.b][1]; n           q.push(Tri(t.a, *b, 0)); n           f[t.a][*b][0] = t.b; n         } n       } n     } n   } n   if (!sol[n][1][0]) { n     printf( -1 n ); n   } else { n     printf( %d n , dist[n][1][0]); n     a = n; n     b = 1; n     vector<int> ra, rb; n     ra.push_back(n); n     rb.push_back(1); n     while (make_pair(a, b) != make_pair(1, n)) { n       rb.push_back(f[a][b][0]); n       b = f[a][b][0]; n       ra.push_back(f[a][b][1]); n       a = f[a][b][1]; n     } n     for (__typeof((rb).begin()) it = ((rb).begin()); it != (rb).end(); ++it) { n       printf( %d  , *it); n     } n     printf(  n ); n     for (__typeof((ra).begin()) it = ((ra).begin()); it != (ra).end(); ++it) { n       printf( %d  , *it); n     } n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int present(int *lib, int k, int x) { n   int i; n   for (i = 0; i < k; i++) { n     if (lib[i] == x) return 1; n   } n   return 0; n } n int findMax(int *lib, int k, queue<int> *q, int n) { n   int i, max = -1, maxi = 0; n   for (i = 0; i < k; i++) { n     if (q[lib[i]].empty()) return i; n     if (max < q[lib[i]].front()) { n       max = q[lib[i]].front(); n       maxi = i; n     } n   } n   return maxi; n } n int main() { n   int n, k, i, ans = 0, size = 0, z; n   scanf( %d%d , &n, &k); n   int *lib = (int *)calloc(k, sizeof(int)); n   int *arr = (int *)calloc(n, sizeof(int)); n   queue<int> q[n + 1]; n   for (i = 0; i < n; i++) { n     scanf( %d , &arr[i]); n     q[arr[i]].push(i); n   } n   for (i = 0; i < n; i++) { n     if (present(lib, k, arr[i]) == 0) { n       if (size < k) { n         q[arr[i]].pop(); n         lib[size++] = arr[i]; n         ans++; n       } else { n         q[arr[i]].pop(); n         z = findMax(lib, k, q, n); n         lib[z] = arr[i]; n         ans++; n       } n     } else { n       q[arr[i]].pop(); n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 3e5 + 5; n using namespace std; n long long n, m; n int main() { n   scanf( %d%d , &n, &m); n   if (min(n, m) == 1) { n     long long t = max(n, m); n     long long ti = t / 6, res = t % 6; n     printf( %lld n , ti * 6 + max(0ll, res - 3) * 2); n     return 0; n   } n   if ((n % 2) && (m % 2)) { n     printf( %lld n , n * m - 1); n   } else if (n % 2 == 0 && m % 2 == 0) { n     if (max(n, m) <= 2) n       printf( 0 n ); n     else n       printf( %lld n , n * m); n   } else { n     if (min(n, m) == 2) { n       long long t = max(n, m); n       if (t == 3 || t == 7) n         printf( %lld n , n * m - 2); n       else n         printf( %lld n , n * m); n     } else n       printf( %lld n , n * m); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double p[600][600], pp[600][600]; n double f[600][600]; n bool checkInRound(int I, int K, int Round) { n   int i = I - 1; n   int k = K - 1; n   if (i / (1 << Round) != k / (1 << Round)) return false; n   if (i / (1 << (Round - 1)) == k / (1 << (Round - 1))) return false; n   return true; n } n int main() { n   memset(f, 0, sizeof(f)); n   int N; n   cin >> N; n   int SL = 1 << N; n   for (int i = 1; i <= SL; i++) { n     int temp; n     for (int j = 1; j <= SL; j++) { n       scanf( %d , &temp); n       p[i][j] = temp * 0.01; n     } n   } n   int Round = N; n   for (int i = 1; i <= SL; i++) { n     if (i % 2) { n       f[i][1] = p[i][i + 1]; n     } else { n       f[i][1] = p[i][i - 1]; n     } n   } n   for (int i = 1; i <= SL; i++) pp[i][1] = f[i][1]; n   for (int R = 2; R <= Round; R++) { n     for (int i = 1; i <= SL; i++) { n       for (int k = 1; k <= SL; k++) { n         if (checkInRound(i, k, R)) { n           pp[i][R] += pp[i][R - 1] * pp[k][R - 1] * p[i][k]; n         } n       } n     } n   } n   for (int R = 2; R <= Round; R++) { n     for (int i = 1; i <= SL; i++) { n       double mem = 0; n       for (int k = 1; k <= SL; k++) { n         if (checkInRound(i, k, R)) { n           f[i][R] += pp[i][R - 1] * pp[k][R - 1] * p[i][k] * (1 << (R - 1)); n           mem = max(mem, f[k][R - 1]); n         } n       } n       f[i][R] += mem + f[i][R - 1]; n     } n   } n   double ans = 0; n   for (int i = 1; i <= SL; i++) ans = max(ans, f[i][Round]); n   printf( %.12f , ans); 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, x, p, m = 0; n   stack<int> s; n   cin >> n; n   p = n % 2; n   for (int i = 0; i < n; i++) { n     cin >> x; n     m = max(x, m); n     if (!s.empty() && s.top() < x) cout <<  NO , exit(0); n     if (s.empty() || (s.top() != x)) n       s.push(x); n     else n       s.pop(); n   } n   if (p) { n     if (s.size() == 0) { n       cout <<  YES ; n     } else if (s.size() == 1 && s.top() == m) { n       cout <<  YES ; n     } else { n       cout <<  NO ; n     } n   } else { n     if (s.size()) n       cout <<  NO ; n     else n       cout <<  YES ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool check(char s) { n   if (s ==  A  || s ==  H  || s ==  I  || s ==  M  || s ==  O  || s ==  T  || n       s ==  U  || s ==  V  || s ==  W  || s ==  X  || s ==  Y ) n     return true; n   else n     return false; n } n int main() { n   int n, i, x, p; n   bool ans = true; n   string st; n   cin >> st; n   n = st.size(); n   if (n == 1 && check(st[0]) == true) { n     cout <<  YES n ; n     ; n   } else if (n == 2 && st[0] == st[1] && check(st[0]) == true) { n     cout <<  YES n ; n     ; n   } else if (n >= 3) { n     ans = true; n     x = 0, p = n - 1; n     while (x <= p) { n       if (st[x] == st[p] && check(st[x]) == true) { n         x += 1, p -= 1; n       } else { n         ans = false; n         break; n       } n     } n     if (ans == false) { n       cout <<  NO n ; n       ; n     } else { n       cout <<  YES n ; n       ; n     } n   } else { n     cout <<  NO n ; n     ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int count[26] = {0}, c = 0, i; n   string str; n   getline(cin, str); n   for (i = 1; i <= str.length() - 1; i++) { n     if (str[i] ==  }  || str[i] ==  , ) count[str[i - 1] -  a ]++; n   } n   for (i = 0; i <= 25; i++) n     if (count[i] > 0) c++; n   printf( %d n , c); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.tie(0); n   ios_base::sync_with_stdio(0); n   long long t; n   cin >> t; n   while (t--) { n     vector<long long> v(7); n     for (auto& e : v) cin >> e; n     sort(v.begin(), v.end()); n     cout << v[0] <<     << v[1] <<    ; n     if (v[2] != v[1] + v[0]) n       cout << v[2] << endl; n     else n       cout << v[3] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000100], n, m; n int sum[1000100], now; n long long ans; n int idx, cnt; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]), ans += a[i]; n   if (ans % m) { n     puts( No ); n     return 0; n   } n   ans /= m; n   for (int i = 1; i <= n; i++) { n     cnt += a[i], now++; n     if (cnt == ans) { n       sum[++idx] = now; n       now = 0, cnt = 0; n     } else if (cnt > ans) { n       puts( No ); n       return 0; n     } n   } n   puts( Yes ); n   for (int i = 1; i <= idx; i++) printf( %d  , sum[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read(int f = 1, int x = 0, char ch =    ) { n   while (!isdigit(ch = getchar())) n     if (ch ==  - ) f = -1; n   while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar(); n   return f * x; n } n const int N = 2e5 + 5; n int m, n, T, s[2][N], c[2], f[2], ans, q[N], t[N], type[N], tot; n long long a, b; n int main() { n   m = read(); n   while (m--) { n     n = read(), T = read(), a = read(), b = read(); n     c[0] = c[1] = f[0] = f[1] = ans = 0; n     for (int i = 1; i <= n; ++i) type[i] = read(), s[0][i] = s[1][i] = 0; n     for (int i = 1; i <= n; ++i) q[i] = t[i] = read(); n     sort(q + 1, q + 1 + n), tot = unique(q + 1, q + 1 + n) - q - 1; n     for (int i = 1; i <= n; ++i) { n       int j = lower_bound(q + 1, q + 1 + tot, t[i]) - q; n       ++s[type[i]][j], ++c[type[i]]; n     } n     q[tot + 1] = max(q[tot] + 1, T + 1); n     for (int i = 0; i <= tot; ++i) { n       f[0] += s[0][i], f[1] += s[1][i]; n       int t = min(q[i + 1] - 1, T), resa = c[0] - f[0], resb = c[1] - f[1]; n       ; n       if (f[0] * a + f[1] * b > t) continue; n       int rest = t - (f[0] * a + f[1] * b); n       if (resa * a <= rest) n         ans = max(f[0] + f[1] + resa + min((rest - resa * a) / b, 1ll * resb), n                   1ll * ans); n       else n         ans = max(f[0] + f[1] + rest / a, 1ll * ans); n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:66777216 ) n using namespace std; n const double eps = 1e-7; n const double pi = acos(-1.0); n const long long INF = (long long)2e9 + 1; n const long long LINF = (long long)8e18; n const long long inf = (long long)2e9 + 1; n const long long linf = (long long)8e18; n const long long MM = (long long)1e9 + 7; n int solve(); n void gen(); n int main() { n   solve(); n   return 0; n } n const int dd = 3e5 + 7; n int get(int t) { n   while (t < 0) t += 360; n   while (t > 360) t -= 360; n   return min(t, 360 - t); n } n int solve() { n   long long x; n   cin >> x; n   x *= -1; n   x %= 360; n   vector<pair<int, int> > M; n   M.push_back(make_pair(get(x), 0)); n   M.push_back(make_pair(get(x + 90), 1)); n   M.push_back(make_pair(get(x + 180), 2)); n   M.push_back(make_pair(get(x + 270), 3)); n   sort(M.begin(), M.end()); n   cout << M[0].second; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 10 + 1e6; n const int INF = 1e9 + 10; n long long a, b; n map<long long, int> f; n map<long long, long long> dad; n long long ans[100]; n void dfs(long long x, int s) { n   if (x > b) return; n   if (f[2 * x] == 0 || f[2 * x] > s + 1) { n     dad[2 * x] = x; n     f[2 * x] = s + 1; n     dfs(2 * x, s + 1); n   } n   if (f[10 * x + 1] == 0 || f[10 * x + 1] > s + 1) { n     dad[10 * x + 1] = x; n     f[10 * x + 1] = s + 1; n     dfs(10 * x + 1, s + 1); n   } n } n int main() { n   cin >> a >> b; n   dad[a] = a; n   dfs(a, 1); n   if (f[b] == 0) n     cout <<  NO ; n   else { n     cout <<  YES  << endl; n     int l = 0; n     while (dad[b] != b) { n       ans[++l] = b; n       b = dad[b]; n     } n     l++; n     ans[l] = a; n     cout << l << endl; n     for (int i = (l), _b = (1); i >= _b; i--) cout << ans[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, t; n   cin >> s >> t; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] == t[s.size() - i - 1]) { n       continue; n     } else { n       cout <<  NO ; n       return 0; n     } n   } n   cout <<  YES ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a, b; n int main() { n   cin >> n >> a >> b; n   long long chores[n]; n   for (int i = 0; i < n; i++) cin >> chores[i]; n   sort(chores, chores + n); n   cout << chores[b] - chores[b - 1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     double n, x; n     cin >> n >> x; n     vector<double> a(n); n     for (__typeof(n) i = 0; i < n; i++) { n       cin >> a[i]; n     } n     sort(a.rbegin(), a.rend()); n     a.push_back(0); n     double no = 1, am = a[0]; n     while ((am / no >= x) && (no <= n)) { n       am += a[no]; n       no++; n     } n     cout << no - 1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int fromStrtoInt(string s, string c) { n   int x = 0; n   for (int i = 0; i < c.size(); i++) { n     x *= 10; n     x += s[c[i] -  0 ] -  0 ; n   } n   return x; n } n int main() { n   int n, k; n   cin >> n >> k; n   string s, arr[n]; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   for (int i = 0; i < k; i++) s += i +  0 ; n   int a[n]; n   int ans = 1e9; n   do { n     for (int i = 0; i < n; i++) a[i] = fromStrtoInt(arr[i], s); n     sort(a, a + n); n     ans = min(ans, a[n - 1] - a[0]); n   } while (next_permutation(s.begin(), s.end())); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x, y, ans = 0; n   scanf( %d , &n); n   while (n--) scanf( %d%d , &x, &y), ans = max(ans, x + y); n   printf( %d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n long long int full = 0; n long long int arr[51][51]; n long long int n, t; n void solve(long long int i, long long int j, long long int level) { n   if (level > n - 1) { n     return; n   } else { n     if (arr[i][j] < (1ll << level)) { n       arr[i][j]++; n       if (arr[i][j] == (1ll << level)) { n         full++; n       } n     } else { n       solve(i + 1, j + 1, level + 1); n       solve(i + 1, j, level + 1); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> t; n   for (long long int i = 0; i < 10ll; i++) { n     for (long long int j = 0; j < 10ll; j++) { n       arr[i][j] = 0; n     } n   } n   for (long long int i = 1; i <= t; i++) { n     solve(0, 0, 0); n   } n   cout << full << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int V[102][102], M2[2][102]; n int main() { n   int n, a; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) { n       scanf( %d , &a); n       M2[0][i] += a; n       M2[1][j] += a; n     } n   int cnt = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) n       if (M2[0][i] < M2[1][j]) cnt++; n   cout << cnt << endl; n } n


#include <bits/stdc++.h> n unsigned long long c; n int n, i, j, k, ks, ke, pd, pp, pa; n char s[1000100]; n bool f, ff, fff; n int main() { n   scanf( %s , s); n   n = strlen(s); n   for (i = 0; i < n; i++) { n     if (s[i] >=  a  && s[i] <=  z ) { n       k = 0, f = 1, ff = 0, fff = 0, ks = 1, ke = 0, pa = 0, pd = 0, pp = 0; n       for (j = i + 1; f && j < n; j++) { n         if (!k) { n           if (s[j] ==  @ ) n             ++k, pa = j; n           else if (!((s[j] >=  a  && s[j] <=  z ) || n                      (s[j] >=  0  && s[j] <=  9 ) || s[j] ==  _ )) n             f = 0; n           else if (s[j] >=  a  && s[j] <=  z ) n             ++ks; n         } else if (k == 1) { n           if (s[j] ==  . ) n             ++k, pd = j; n           else if ((s[j] >=  a  && s[j] <=  z ) || (s[j] >=  0  && s[j] <=  9 )) n             ff = 1; n           else n             f = 0; n         } else { n           if (s[j] >=  a  && s[j] <=  z ) n             fff = 1, ++ke, pp = j; n           else n             f = 0; n         } n       } n       if (ff && fff) c += (unsigned long long)ks * ke; n       if (pa) n         i = pa; n       else if (pd) n         i = pd; n       else if (pp) n         i = pp - 1; n       else n         i = j - 1; n     } n   } n   printf( %I64u n , c); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2000; n int n, i, j; n char s[maxn]; n long long F[maxn][maxn]; n long long P[maxn]; n int main() { n   scanf( %d , &n); n   scanf( %s , s); n   P[1] = 1; n   for (i = 2; i <= 18; ++i) P[i] = 10LL * P[i - 1]; n   memset(F, 0, sizeof F); n   for (i = 0; i <= n; ++i) n     for (j = 0; j <= n; ++j) { n       if (i) n         F[i][j] = n             max(F[i][j], F[i - 1][j] + P[i - 1] * (s[n + n - i - j] -  0 )); n       if (j) n         F[i][j] = n             max(F[i][j], F[i][j - 1] + P[j - 1] * (s[n + n - i - j] -  0 )); n     } n   i = n, j = n; n   while (i || j) { n     if (i && F[i][j] == (F[i - 1][j] + P[i - 1] * (s[n + n - i - j] -  0 ))) n       putchar( H ), --i; n     else n       putchar( M ), --j; n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n int n, from[300005], p[300005], dis[300005], a[300005], b[300005]; n std::queue<int> que; n std::set<int, std::greater<int>> l; n void work(int now) { n   if (now != n) work(from[now]), std::cout << p[now] <<    ; n } n void print(int now) { n   std::cout << dis[now] + 1 << std::endl; n   work(now); n   std::cout << 0 << std::endl; n   exit(0); n } n signed main() { n   std::ios::sync_with_stdio(false); n   std::cin >> n; n   for (int i = 1; i <= n; ++i) std::cin >> a[i]; n   for (int i = 1; i <= n; ++i) std::cin >> b[i]; n   for (int i = 0; i <= n; ++i) l.insert(i); n   std::fill(dis, dis + n, INT_MAX); n   que.push(n); n   while (!que.empty()) { n     int now = que.front(); n     que.pop(); n     if (now - a[now] <= 0) print(now); n     for (std::set<int>::iterator i = l.lower_bound(now); n          i != l.end() && now - *i <= a[now]; l.erase(i++)) { n       int tmp = *i + b[*i]; n       if (dis[tmp] > dis[now] + 1) n         dis[tmp] = dis[now] + 1, p[tmp] = *i, from[tmp] = now, que.push(tmp); n     } n   } n   std::cout << -1 << std::endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX = -1000000000000000000; n const long long MIN = 1000000000000000000; n const long long inf = 1000000000; n const long long KOK = 100000; n const long long LOG = 30; n const long long li = 500005; n const long long mod = 1000000007; n int n, m, b[li], a[li], k, flag, t, yes, say, ind, cev1, mn, cev2; n int cev; n string s; n vector<int> v; n int main(void) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> s; n   for (int i = 0; i < (int)s.size(); i++) { n     if (s[i] ==  # ) { n       say++; n       ind = i; n     } n   } n   for (int i = 0; i < (int)s.size(); i++) { n     if (ind == i) { n       if (cev <= 0) { n         printf( -1 n ); n         return 0; n       } n       a[1] = cev; n       cev = 0; n       continue; n     } n     if (i > ind) { n       if (s[i] ==  ( ) n         cev++; n       else if (s[i] ==  )  && cev > 0) { n         cev--; n       } else if (s[i] ==  )  && a[1] > 1) { n         a[1]--; n       } else { n         printf( -1 n ); n         return 0; n       } n     } else { n       if (s[i] ==  ( ) n         cev++; n       else n         cev--; n       if (cev < 0) { n         printf( -1 n ); n         return 0; n       } n     } n   } n   if (cev > 0) { n     printf( -1 n ); n     return 0; n   } n   for (int i = 1; i < say; i++) printf( 1 n ); n   printf( %d n , a[1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int n; n   string s; n   cin >> n; n   cin >> s; n   sort(s.begin(), s.end()); n   cout << s; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, k; n   cin >> n >> k; n   long long a; n   int g = 0; n   for (int i = 0; i < n; i++) { n     cin >> a; n     set<int> d; n     while (a) { n       if (a % 10 <= k) { n         d.insert(a % 10); n       } n       a /= 10; n     } n     if (d.size() == k + 1) { n       g++; n     } n   } n   cout << g; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   set<int> st; n   for (int i = 2; i <= n; i++) { n     int a = i; n     for (int j = 2; j * j <= a; j++) { n       int num = 1; n       while (a % j == 0 and a > 1) { n         a /= j; n         num *= j; n       } n       if (num != 1) st.insert(num); n     } n     if (a > 1) st.insert(a); n   } n   cout << st.size() << endl; n   for (auto item : st) cout << item <<    ; n } n


#define _CRT_SECURE_NO_WARNINGS n #include <iostream> n #include <vector> n #include <algorithm> n #include <string> n #include <fstream> n #include <set> n #include <deque> n #include <math.h> n #include <iomanip> n #define ll long long n #define ld long double n  n using namespace std; n  n int all = 0; n int price = 0; n  n void dfs(int u, vector<int>& used, vector<int> &col, vector<vector<int>>& graph) { n     all++; n     if (used[u] != col[u]) { n         price++; n     } n     for (int v : graph[u]) { n         if (used[v] == -1) { n             used[v] = 1 - used[u]; n             dfs(v, used, col, graph); n         } n     } n } n  n int main() { n     ios_base::sync_with_stdio(0); n     cin.tie(0); n     //freopen( input.txt ,  r , stdin); n     //freopen( output.txt ,  w , stdout); n     int n; n     cin >> n; n     vector<int> col(n); n     vector<pair<int, int>> arr(n); n     vector<int> L(2 * n); n     for (int i = 0; i < 2 * n; ++i) { n         L[i] = -1; n     } n     for (int i = 0; i < n; ++i) { n         cin >> arr[i].first >> arr[i].second; n         if (arr[i].first > arr[i].second) { n             col[i] = 1; n             swap(arr[i].first, arr[i].second); n         } n         arr[i].first--; n         arr[i].second--; n         L[arr[i].first] = i; n     } n     for (int i = 0; i < n; ++i) { n         if (L[i] == -1) { n             cout <<  -1 n ; n             return 0; n         } n     } n     vector<int> r(n); n     for (int i = 0; i < n; ++i) { n         r[i] = arr[L[i]].second; n     } n     vector<int> mn(n); n     vector<int> mni(n); n     vector<int> mx(n); n     vector<int> mxi(n); n     mn[0] = r[0]; n     mni[0] = 0; n     mx[n - 1] = r[n - 1]; n     mxi[n - 1] = n - 1; n     for (int i = 1; i < n; ++i) { n         if (r[i] < mn[i - 1]) n             mni[i] = i; n         else n             mni[i] = mni[i - 1]; n         mn[i] = min(mn[i - 1], r[i]); n     } n     for (int i = n - 2; i >= 0; --i) { n         if (r[i] > mx[i + 1]) n             mxi[i] = i; n         else n             mxi[i] = mxi[i + 1]; n         mx[i] = max(mx[i + 1], r[i]); n     } n     for (int i = 1; i < n - 1; ++i) { n         if (mn[i - 1] < r[i] && r[i] < mx[i + 1]) { n             cout <<  -1 n ; n             return 0; n         } n     } n     //cerr <<  here n ; n     vector<vector<int>> graph(n); n     vector<int> st; n     for (int i = 0; i < n; ++i) { n         if (i < n - 1 && r[i] < mx[i + 1]) { n             st.push_back(i); n             continue; n         } n         if (i > 0 && r[i] > mn[i - 1]) { n             int useful = st.back(); n             while (!st.empty()){ n                 if (r[st.back()] < r[i]) { n                     graph[L[st.back()]].push_back(L[i]); n                     graph[L[i]].push_back(L[st.back()]); n                     st.pop_back(); n                     continue; n                 } n                 break; n             } n             st.push_back(useful); n  n         } n     } n     vector<int> used(n); n     int ans = 0; n     for (int i = 0; i < n; ++i) { n         used[i] = -1; n     } n     for (int i = 0; i < n; ++i) { n         if (used[i] == -1) { n             price = 0; n             all = 0; n             used[i] = 0; n             dfs(i, used, col, graph); n             ans += min(price, all - price); n         } n     } n     cout << ans <<   n ; n } n //*/


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void chkmax(T &x, T y) { n   x = x > y ? x : y; n } n template <typename T> n void chkmin(T &x, T y) { n   x = x > y ? y : x; n } n const int INF = (1ll << 30); n template <typename T> n void read(T &x) { n   x = 0; n   bool f = 1; n   char ch; n   do { n     ch = getchar(); n     if (ch ==  - ) f = 0; n   } while (ch >  9  || ch <  0 ); n   do { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } while (ch >=  0  && ch <=  9 ); n   x = f ? x : -x; n } n template <typename T> n void write(T x) { n   if (x < 0) x = ~x + 1, putchar( - ); n   if (x > 9) write(x / 10); n   putchar(x % 10 +  0 ); n } n const int N = 5000 + 5; n const int M = 1e5 + 7; n map<int, int> mp; n int n, m, ans, cnt, a[N], b[N], g[N], f[N], prime[M]; n bool vis[M]; n inline int gcd(int a, int b) { n   if (a < b) swap(a, b); n   return b == 0 ? a : gcd(b, a % b); n } n inline void init(int NN) { n   for (int i = 2; i <= NN; i++) { n     if (!vis[i]) { n       prime[++cnt] = i; n     } n     for (int j = 1; j <= cnt && prime[j] * i <= NN; j++) { n       vis[prime[j] * i] = true; n       if (i % prime[j] == 0) break; n     } n   } n } n inline int divide(int x) { n   if (mp.count(x)) return mp[x]; n   int p = 0, tp = x; n   for (int i = 1; i <= m && b[i] <= x; i++) { n     while (x % b[i] == 0) x /= b[i], p--; n   } n   for (int i = 1; i <= cnt && 1ll * prime[i] * prime[i] <= x; i++) { n     while (x % prime[i] == 0) x /= prime[i], p++; n   } n   if (x != 1) p++; n   return mp[tp] = p; n } n int main() { n   read(n); n   read(m); n   for (int i = 1; i <= n; i++) read(a[i]), g[i] = gcd(a[i], g[i - 1]); n   for (int i = 1; i <= m; i++) read(b[i]); n   init(1e5); n   ans = 0; n   for (int i = 1; i <= n; i++) ans += divide(a[i]); n   for (int i = 1; i <= n; i++) f[i] = ans - i * divide(g[i]); n   for (int i = n; i >= 1; i--) { n     ans = max(ans, f[i]); n     for (int j = i - 1; j >= 1; j--) { n       int gg = g[j] / g[i]; n       f[j] = max(f[j], f[i] - j * divide(gg)); n       chkmax(ans, f[j]); n     } 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 using ull = unsigned long long; n using pii = pair<int, int>; n template <class T> n inline void read(T &x) { n   x = 0; n   char c = getchar(); n   bool flag = 0; n   while (!isdigit(c)) { n     if (c ==  - ) flag = 1; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = (x << 3) + (x << 1) + c -  0 ; n     c = getchar(); n   } n   x = flag ? -x : x; n } n template <class T> n inline void write(T x) { n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   if (x >= 10) write(x / 10); n   putchar(x % 10 +  0 ); n } n const int maxn = 5e5 + 10; n const int mod = 998244353; n struct Edge { n   int next, ed; n }; n Edge e[maxn * 2]; n int head[maxn], cnt; n void addEdge(int st, int ed) { n   e[cnt].ed = ed; n   e[cnt].next = head[st]; n   head[st] = cnt++; n } n bool vis[maxn]; n int sum[maxn], d[maxn]; n int ansl[maxn], ansr[maxn]; n void get(int k) { n   sum[k] = 1; n   for (int i = head[k]; i != -1; i = e[i].next) { n     int ed = e[i].ed; n     if (sum[ed] == 0) { n       get(ed); n       sum[k] += sum[ed]; n       ++d[k]; n     } n   } n } n void dfs(int k, int l, int r) { n   vis[k] = 1; n   ansr[k] = l + d[k]; n   int c = l, temp = r; n   for (int i = head[k]; i != -1; i = e[i].next) { n     int ed = e[i].ed; n     if (!vis[ed]) { n       ansl[ed] = c; n       ++c; n       dfs(ed, temp - sum[ed] * 2 + 2, temp); n       temp -= sum[ed] * 2 - 1; n     } n   } n } n int main() { n   int n; n   read(n); n   memset(head, -1, sizeof(head)); n   for (int i = 1; i < n; ++i) { n     int st, ed; n     scanf( %d %d , &st, &ed); n     addEdge(st, ed); n     addEdge(ed, st); n   } n   get(1); n   ansl[1] = 1; n   dfs(1, 2, n * 2); n   for (int i = 1; i <= n; ++i) { n     printf( %d %d n , ansl[i], ansr[i]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 7; n const long long LL_INF = 1ll * INF * INF; n const int MAX_N = 1000000 + 7; n template <typename T> n inline void addmod(T& a, const long long& b, const int& MOD = INF) { n   a = (a + b) % MOD; n   if (a < 0) a += MOD; n } n const int mod = INF; n long long f[MAX_N]; n int n; n vector<int> adj[MAX_N]; n vector<int> rev[MAX_N]; n long long gcd(long long a, long long b) { return a == 0 ? b : gcd(b % a, a); } n vector<int> divisors[MAX_N]; n void init() { n   for (int i = 1; i < MAX_N; ++i) n     if (!rev[i].empty()) { n       for (int j = i; j < MAX_N; j += i) divisors[j].push_back(i); n     } n } n class FenWickTree { n  public: n   void update(int x, int val) { n     for (int t = x; t < MAX_N; t += t & -t) { n       tree[t] += val; n     } n   } n   int get(int x) { n     int res = 0; n     for (int t = x; t > 0; t -= t & -t) { n       res += tree[t]; n     } n     return res; n   } n  n  private: n   static const int MAX_N = ::MAX_N + 7; n   int tree[MAX_N]; n } fenwickTree; n int level[MAX_N]; n int low[MAX_N], high[MAX_N]; n void dfs(int u) { n   static int cnt = 0; n   low[u] = ++cnt; n   for (int v : adj[u]) { n     level[v] = level[u] + 1; n     dfs(v); n     f[u] += f[v]; n   } n   high[u] = cnt; n } n bool check(int d) { n   if (d == 1) return true; n   if (f[0] % d) return false; n   long long sum = f[0] / d; n   int cnt = 0; n   for (int c : divisors[d]) n     if (!rev[c].empty()) { n       cnt += rev[c].size(); n     } n   return d == cnt; n } n int ans[MAX_N]; n void solve() { n   cin >> n; n   for (int i = (0), _b = (n); i < _b; ++i) cin >> f[i]; n   for (int i = (1), _b = (n); i < _b; ++i) { n     int p; n     cin >> p; n     adj[p - 1].push_back(i); n   } n   dfs(0); n   for (int i = 0; i < n; ++i) { n     long long d = gcd(f[i], f[0]); n     if (f[0] / d < MAX_N) { n       rev[f[0] / d].push_back(i); n     } n   } n   init(); n   for (int i = n; i > 0; --i) n     if (check(i)) { n       ans[i] += 1; n       if (ans[i] >= mod) ans[i] -= mod; n       for (int c : divisors[i]) n         if (c != i) { n           ans[c] += ans[i]; n           if (ans[c] >= mod) ans[c] -= mod; n         } n     } n   cout << ans[1] <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   const bool multiple_test = false; n   int test = 1; n   if (multiple_test) cin >> test; n   for (int i = 0; i < test; ++i) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct lichao { n   int l, r, A; n   long long B; n   lichao() : l(0), r(0), A(0), B(0x7fffffffffffffffLL) {} n }; n const int SZ = 1 << 17; n vector<int> adj[100000]; n vector<lichao> tree(2 * SZ); n int node_cnt, num[100000], A[100000], B[100000]; n long long ans[100000]; n int get_sign(long long a) { return a < 0 ? -1 : a > 0; } n void add_tree2(int A, long long B, int p, int s = -SZ, int e = SZ - 1) { n   int m = (s + e) >> 1, &pA = tree[p].A; n   long long &pB = tree[p].B; n   long long ys = 1LL * A * s + B, ym = 1LL * A * m + B, ye = 1LL * A * e + B, n             pys = 1LL * pA * s + pB, pym = 1LL * pA * m + pB, n             pye = 1LL * pA * e + pB; n   if (ym < pym) { n     swap(A, pA); n     swap(B, pB); n     swap(ys, pys); n     swap(ym, pym); n     swap(ye, pye); n   } n   if (ys >= pys && ye >= pye) return; n   if (get_sign(ys - pys) * get_sign(ym - pym) < 0 || ym == pym && ys < pys) { n     if (tree[p].l == 0) tree[p].l = tree.size(), tree.push_back(lichao()); n     add_tree2(A, B, tree[p].l, s, m); n   } else { n     if (tree[p].r == 0) tree[p].r = tree.size(), tree.push_back(lichao()); n     add_tree2(A, B, tree[p].r, m + 1, e); n   } n } n void add_tree(int n, int A, long long B) { n   for (add_tree2(A, B, n += SZ); n >>= 1;) add_tree2(A, B, n); n } n long long get_min2(int x, int p, int s = -SZ, int e = SZ - 1) { n   int m = (s + e) >> 1; n   if (p == 0 || s == e) return 1LL * tree[p].A * x + tree[p].B; n   return min( n       1LL * tree[p].A * x + tree[p].B, n       x <= m ? get_min2(x, tree[p].l, s, m) : get_min2(x, tree[p].r, m + 1, e)); n } n long long get_min(int s, int e, int x) { n   long long ret = 0x7fffffffffffffffLL; n   for (s |= SZ, e |= SZ; s <= e; s >>= 1, e >>= 1) { n     if (s & 1) ret = min(ret, get_min2(x, s++)); n     if (~e & 1) ret = min(ret, get_min2(x, e--)); n   } n   return ret; n } n void dfs(int c) { n   bool leaf = true; n   num[c] = ++node_cnt; n   for (auto n : adj[c]) n     if (num[n] == 0) { n       leaf = false; n       dfs(n); n     } n   ans[c] = (leaf ? 0 : get_min(num[c], node_cnt, A[c])); n   add_tree(num[c], B[c], ans[c]); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ((void)0); n   ((void)0); n   ((void)0); n   int N; n   cin >> N; n   for (int i = 0; i < N; i++) cin >> A[i]; n   for (int i = 0; i < N; i++) cin >> B[i]; n   for (int i = 1; i < N; i++) { n     int a, b; n     cin >> a >> b; n     adj[--a].push_back(--b); n     adj[b].push_back(a); n   } n   dfs(0); n   for (int i = 0; i < N; i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e5 + 9, lg = 30; n int a[inf * 3], n, table[lg][inf << 2]; n int ans[inf], nxt[inf], sml[inf << 2]; n void build() { n   int i, j; n   for (i = 1; i <= n + n; i++) table[0][i] = a[i]; n   for (j = 1; j < lg - 1; j++) n     for (i = 1; i + (1 << (j - 1)) - 1 <= n + n; i++) n       table[j][i] = min(table[j - 1][i], table[j - 1][i + (1 << (j - 1))]); n } n int query(int x, int y) { n   int len, k; n   len = y - x + 1; n   k = log2(len); n   return min(table[k][x], table[k][y + 1 - (1 << k)]); n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) n     cin >> a[i], a[i + n] = a[i], a[i + n + n] = a[i]; n   int mx = 0; n   for (int i = n;; i++) { n     if (mx > a[i]) break; n     if (i == n + n + n) { n       ans[n] = -1; n       break; n     } n     ans[n]++; n     mx = max(mx, (a[i] % 2 == 0 ? a[i] / 2 : a[i] / 2 + 1)); n   } n   ans[n + n] = ans[n]; n   stack<int> st; n   for (int i = n + n; i >= 1; i--) { n     while (!st.empty() && a[st.top()] <= a[i]) st.pop(); n     if (st.empty()) n       nxt[i] = -1; n     else n       nxt[i] = st.top(); n     st.push(i); n   } n   build(); n   for (int i = 1; i <= n; i++) { n     sml[i] = 1e9; n     int val = (a[i] % 2 == 0 ? a[i] / 2 : a[i] / 2 + 1); n     int l = i, r = min(n + n + 1, n + i + 1); n     while (r - l > 1) { n       int mid = (l + r) / 2; n       if (query(i, mid) < val) n         r = mid; n       else n         l = mid; n     } n     if (a[r] < val) sml[i] = r; n   } n   vector<pair<int, int> > v; n   for (int i = 1; i <= n; i++) { n     v.push_back(make_pair(a[i], i)); n   } n   sort(v.begin(), v.end()); n   reverse(v.begin(), v.end()); n   for (auto o : v) { n     int i = o.second; n     if (sml[i] == 1e9) { n       if (nxt[i] == -1) n         ans[i] = -1; n       else n         ans[i] = (ans[nxt[i]] != -1 ? ans[nxt[i]] + nxt[i] - i : -1); n     } else { n       if (nxt[i] == -1) n         ans[i] = sml[i] - i; n       else { n         if (sml[i] < nxt[i]) n           ans[i] = sml[i] - i; n         else n           ans[i] = (ans[nxt[i]] != -1 ? ans[nxt[i]] + nxt[i] - i : -1); n       } n     } n     ans[i + n] = ans[i]; n   } n   for (int i = 1; i <= n; i++) cout << ans[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T abs(T a) { n   return a < 0 ? -a : a; n } n template <typename T> n T sqr(T a) { n   return a * a; n } n const int INF = (int)1e9; n const long double EPS = 1e-9; n const long double PI = 3.1415926535897932384626433832795; n int main() { n   int n; n   cin >> n; n   cout << n <<    ; n   for (int i = 1; i < n; i++) cout << i <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int p; n int pm(int x, int e) { n   int res = 1; n   int tmp = x; n   while (e > 0) { n     if (e & 1) res = ((long long)res * tmp) % p; n     tmp = ((long long)tmp * tmp) % p; n     e >>= 1; n   } n   return res; n } n bool prim() { n   for (int i = 2; i * i <= p; ++i) n     if (p % i == 0) return false; n   return true; n } n vector<int> d; n void roz() { n   int M = p - 1; n   for (int i = 2; i * i < p; ++i) { n     if (M % i == 0) d.push_back(M / i); n   } n } n bool ok(int x) { n   for (int i = 0; i < d.size(); ++i) n     if (pm(x, d[i]) == 1) return false; n   return true; n } n int gen() { n   roz(); n   for (int i = 2; i < p; ++i) { n     if (ok(i)) return i; n   } n } n int main() { n   scanf( %d , &p); n   if (p == 1) { n     printf( YES n1 n ); n   } else if (p == 2) { n     printf( YES n1 n2 n ); n   } else if (p == 4) { n     printf( YES n1 n3 n2 n4 n ); n   } else if (!prim()) { n     printf( NO n ); n   } else { n     int g = gen(); n     printf( YES n ); n     for (int i = 0; i < (p - 1) / 2; ++i) { n       printf( %d n%d n , pm(g, p - 1 - 2 * i), pm(g, 1 + 2 * i)); n     } n     printf( %d n , p); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010; n long long gcd(long long x, long long y) { n   if (y == 0) return x; n   return gcd(y, x % y); n } n int main() { n   long long n; n   cin >> n; n   string s; n   cin >> s; n   vector<int> v(26); n   for (long long i = 0; i < n; i++) { n     v[s[i] -  a ]++; n   } n   for (long long i = 0; i < 26; i++) { n     while (v[i]--) { n       char c = i +  a ; n       cout << c; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 5; n int k, n, m; n long long a[MAXN], g[MAXN], ii[MAXN], ty[MAXN]; n vector<pair<long long, long long> > V[MAXN]; n vector<pair<long long, pair<long long, long long> > > P; n bool Compare(pair<long long, pair<long long, long long> > A, n              pair<long long, pair<long long, long long> > B) { n   assert(A.second.first >= A.second.second); n   assert(B.second.first >= B.second.second); n   A.second.first -= A.second.second; n   B.second.first -= B.second.second; n   return A.second.first * B.second.second > A.second.second * B.second.first; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> k >> n >> m; n   for (int i = 1; i <= k; i++) { n     cin >> a[i]; n   } n   for (int i = 1; i <= n; i++) { n     int t, id, b; n     cin >> t >> id >> b; n     ty[i] = t; n     if (t == 1) { n       if (g[id] < b) { n         g[id] = b; n         ii[id] = i; n       } n     } else if (t == 2) { n       V[id].emplace_back(b, i); n     } else { n       P.emplace_back(i, make_pair(b, 1)); n     } n   } n   for (int i = 1; i <= k; i++) { n     if (a[i] < g[i]) { n       V[i].emplace_back(g[i] - a[i], ii[i]); n     } n     sort(V[i].begin(), V[i].end(), greater<pair<long long, long long> >()); n     for (auto x : V[i]) { n       P.emplace_back(x.second, make_pair(a[i] + x.first, a[i])); n       a[i] += x.first; n     } n   } n   sort(P.begin(), P.end(), Compare); n   vector<pair<long long, long long> > L; n   for (int i = 0; i < min(m, (int)P.size()); i++) { n     L.emplace_back(ty[P[i].first], P[i].first); n   } n   sort(L.begin(), L.end()); n   cout << L.size() <<   n ; n   for (auto x : L) { n     cout << x.second <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<long long, long long> > g[100005]; n long long n, ans, ans2; n void dfs(long long u, long long p, long long cur) { n   ans2 = max(ans2, cur); n   for (auto e : g[u]) n     if (e.first != p) dfs(e.first, u, cur + e.second); n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n; n   for (long long i = 1; i <= n - 1; i++) { n     long long a, b, w; n     cin >> a >> b >> w; n     g[a].push_back({b, w}); n     g[b].push_back({a, w}); n     ans += w * 2; n   } n   dfs(1, 0, 0); n   cout << ans - ans2; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void rd(T &x) { n   char ch; n   x = 0; n   bool fl = false; n   while (!isdigit(ch = getchar())) (ch ==  - ) && (fl = true); n   for (x = (ch ^  0 ); isdigit(ch = getchar()); x = x * 10 + (ch ^  0 )) n     ; n   (fl == true) && (x = -x); n } n template <class T> n inline void output(T x) { n   if (x / 10) output(x / 10); n   putchar(x % 10 +  0 ); n } n template <class T> n inline void ot(T x) { n   if (x < 0) putchar( - ), x = -x; n   output(x); n   putchar(   ); n } n template <class T> n inline void prt(T a[], int st, int nd) { n   for (register int i = st; i <= nd; ++i) ot(a[i]); n   putchar(  n ); n } n namespace Modulo { n const int mod = 998244353; n int ad(int x, int y) { return (x + y) >= mod ? x + y - mod : x + y; } n void inc(int &x, int y) { x = ad(x, y); } n int mul(int x, int y) { return (long long)x * y % mod; } n void inc2(int &x, int y) { x = mul(x, y); } n int qm(int x, int y = mod - 2) { n   int ret = 1; n   while (y) { n     if (y & 1) ret = mul(x, ret); n     x = mul(x, x); n     y >>= 1; n   } n   return ret; n } n }  // namespace Modulo n namespace Miracle { n const int N = 300000 + 5; n const int inf = 0x3f3f3f3f; n int n, m; n int du[N]; n int b[N][2]; n vector<int> to[N]; n int go[N]; n int q[N], l, r, df; n void topo() { n   l = 1, r = 0; n   df = 0; n   for (register int i = 1; i <= n; ++i) { n     if (du[i] == 0) q[++r] = i; n   } n   while (l <= r) { n     int x = q[l]; n     ++df; n     int sz = r - l + 1; n     ++l; n     if (sz == 1) { n       go[x] += n - df; n     } else if (sz == 2) { n       int b = q[l]; n       int lp = 0; n       for (const auto &y : to[b]) { n         if (du[y] == 1) { n           lp = 1; n           break; n         } n       } n       if (!lp) { n         go[x] += n - df - 1; n       } else { n         go[x] = -inf; n       } n     } else { n       go[x] = -inf; n     } n     for (const auto &y : to[x]) { n       --du[y]; n       if (du[y] == 0) { n         q[++r] = y; n       } n     } n   } n } n int main() { n   rd(n); n   rd(m); n   for (register int i = 1; i <= m; ++i) { n     rd(b[i][0]); n     rd(b[i][1]); n     to[b[i][0]].push_back(b[i][1]); n     ++du[b[i][1]]; n   } n   topo(); n   for (register int i = 1; i <= n; ++i) to[i].clear(), du[i] = 0; n   for (register int i = 1; i <= m; ++i) { n     to[b[i][1]].push_back(b[i][0]); n     ++du[b[i][0]]; n   } n   topo(); n   int ans = 0; n   for (register int i = 1; i <= n; ++i) { n     if (go[i] >= n - 2) ++ans; n   } n   ot(ans); n   return 0; n } n }  // namespace Miracle n signed main() { n   Miracle::main(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 2000 + 4, Inf = 1e9 + 10; n int n, m, k; n char f[M][M]; n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) cin >> f[i][j]; n   for (int i = 0; i < m; i++) { n     int ans = 0; n     for (int y = 2; y < n; y += 2) n       if (f[y][i] ==  U ) ans++; n     for (int x = i, y = 0; x >= 0 && y < n; y++, x--) n       if (f[y][x] ==  R ) ans++; n     for (int x = i, y = 0; x < m && y < n; y++, x++) n       if (f[y][x] ==  L ) ans++; n     cout << ans <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &n) { n   n = 0; n   T f = 1; n   char c = getchar(); n   while (!isdigit(c) && c !=  - ) c = getchar(); n   if (c ==  - ) f = -1, c = getchar(); n   while (isdigit(c)) n = n * 10 + c -  0 , c = getchar(); n   n *= f; n } n template <typename T> n void write(T n) { n   if (n < 0) putchar( - ), n = -n; n   if (n > 9) write(n / 10); n   putchar(n % 10 +  0 ); n } n int n, m, vis[105]; n long long f[105][105], p[105][105]; n struct node { n   long long a, b; n   int id; n   friend bool operator<(node a, node b) { return a.b < b.b; } n } v[105]; n int main() { n   int T; n   cin >> T; n   while (T--) { n     read(n), read(m); n     for (int i = 1; i <= n; i++) { n       read(v[i].a), read(v[i].b); n       v[i].id = i; n     } n     sort(v + 1, v + n + 1); n     memset(f, -0x3f, sizeof(f)); n     memset(p, 0, sizeof(p)); n     memset(vis, 0, sizeof(vis)); n     f[0][0] = 0; n     for (int i = 1; i <= n; i++) { n       for (int j = 0; j <= min(i, m); j++) { n         f[i][j] = f[i - 1][j] + v[i].b * (m - 1); n         if (j && f[i - 1][j - 1] + v[i].a + v[i].b * (j - 1) > f[i][j]) { n           f[i][j] = f[i - 1][j - 1] + v[i].a + v[i].b * (j - 1); n           p[i][j] = 1; n         } n       } n     } n     int t = m; n     for (int i = n; i >= 1; i--) { n       if (p[i][t]) { n         vis[i] = 1; n         t--; n       } n     } n     printf( %d n , m + (n - m) * 2); n     int cnt = 0, x; n     for (int i = 1; i <= n; i++) { n       if (!vis[i]) continue; n       cnt++; n       if (cnt == m) n         x = v[i].id; n       else n         printf( %d  , v[i].id); n     } n     for (int i = 1; i <= n; i++) { n       if (!vis[i]) printf( %d %d  , v[i].id, -v[i].id); n     } n     printf( %d n , x); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100050; n const int inf = 0x3f3f3f3f; n struct Edge { n   int v, w; n }; n vector<Edge> G[maxn]; n bool vis[maxn]; n vector<int> st; n int dep[maxn], f[maxn][20]; n long long dis[maxn]; n void dfs(int x, int par, long long sum) { n   f[x][0] = par; n   dep[x] = dep[par] + 1; n   dis[x] = sum; n   vis[x] = true; n   for (int i = 1; f[x][i - 1]; ++i) f[x][i] = f[f[x][i - 1]][i - 1]; n   for (auto e : G[x]) { n     int v = e.v; n     if (v != par && vis[v]) { n       st.push_back(v), st.push_back(x); n     } n     if (v != par && !vis[v]) dfs(v, x, sum + e.w); n   } n } n int lca(int x, int y) { n   if (dep[x] < dep[y]) swap(x, y); n   int h = dep[x] - dep[y]; n   for (int i = 0; h; ++i) { n     if (h & (1 << i)) { n       x = f[x][i]; n       h ^= (1 << i); n     } n   } n   if (x == y) return x; n   for (int i = 17; i >= 0; --i) { n     if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i]; n   } n   return f[x][0]; n } n long long Dis(int x, int y) { n   int z = lca(x, y); n   return dis[x] + dis[y] - dis[z] * 2; n } n struct Dijkstra { n   long long d[maxn]; n   struct node { n     int x; n     long long d; n     node(int x = 0, long long d = 0) : x(x), d(d) {} n     bool operator<(const node& t) const { return d > t.d; } n   }; n   void run(int s) { n     memset(d, 0x3f, sizeof d); n     memset(vis, false, sizeof vis); n     priority_queue<node> q; n     q.push(node(s, 0)), d[s] = 0; n     while (!q.empty()) { n       node u = q.top(); n       q.pop(); n       if (vis[u.x]) continue; n       vis[u.x] = true; n       for (auto e : G[u.x]) { n         int v = e.v; n         if (!vis[v] && d[v] > u.d + e.w) { n           d[v] = u.d + e.w; n           q.push(node(v, d[v])); n         } n       } n     } n   } n } dij[50]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; ++i) { n     int x, y, w; n     scanf( %d%d%d , &x, &y, &w); n     G[x].push_back({y, w}); n     G[y].push_back({x, w}); n   } n   dfs(1, 0, 0); n   sort(st.begin(), st.end()); n   int sz = unique(st.begin(), st.end()) - st.begin(); n   while (st.size() > sz) st.pop_back(); n   for (int i = 0; i < sz; ++i) { n     dij[i].run(st[i]); n   } n   int q; n   scanf( %d , &q); n   while (q--) { n     int u, v; n     scanf( %d%d , &u, &v); n     long long ans = Dis(u, v); n     for (int i = 0; i < sz; ++i) ans = min(ans, dij[i].d[u] + dij[i].d[v]); n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void add(map<pair<long long, long long>, long long> &mp, n          pair<long long, long long> elem) { n   if (mp.count(elem) == 0) { n     mp.insert({elem, 1}); n   } else { n     mp[elem]++; n   } n } n void del(map<pair<long long, long long>, long long> &mp, n          pair<long long, long long> elem) { n   if (mp[elem] == 1) { n     mp.erase(elem); n   } else { n     mp[elem]--; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   long long q; n   cin >> q; n   for (int k = 0; k < q; ++k) { n     long long n, m, c, c0; n     cin >> n >> m >> c >> c0; n     vector<pair<long long, pair<long long, long long>>> fr; n     fr.push_back({0, {0, c0}}); n     fr.push_back({m, {0, 0}}); n     for (int i = 0; i < n; ++i) { n       long long a, aa, aaa; n       cin >> a >> aa >> aaa; n       fr.push_back({a, {aaa, aa}}); n     } n     sort(fr.begin(), fr.end()); n     map<pair<long long, long long>, long long> st; n     long long ans = 0; n     bool bad = false; n     long long sum = 0; n     for (int j = 0; j < fr.size() - 1; ++j) { n       add(st, {fr[j].second.first, fr[j].second.second}); n       sum += fr[j].second.second; n       while (sum > c) { n         int val = (*(--st.end())).first.first; n         int cnt = (*(--st.end())).first.second; n         del(st, {val, cnt}); n         sum -= cnt; n         if (sum < c) { n           add(st, {val, (c - sum)}); n           sum = c; n         } n       } n       long long need = fr[j + 1].first - fr[j].first; n       if (sum < need) { n         cout << -1 <<   n ; n         bad = true; n         break; n       } n       while (need != 0) { n         need -= (*st.begin()).first.second; n         ans += (*st.begin()).first.first * (*st.begin()).first.second; n         sum -= (*st.begin()).first.second; n         int val = (*st.begin()).first.first; n         int cnt = (*st.begin()).first.second; n         del(st, {val, cnt}); n         if (need < 0) { n           add(st, {val, -need}); n           ans -= (-need) * val; n           sum += (-need); n           need = 0; n         } n       } n     } n     if (!bad) cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n const long long INF = 1LL << 55; n struct Tree { n   Tree *nex[26]; n   vector<int> G[2]; n   int deep; n   Tree() { n     for (int i = 0; i < 26; i++) nex[i] = NULL; n     deep = 0; n   } n }; n Tree *tree; n void Insert(char *s, int u, int id) { n   Tree *p = tree; n   for (int i = 0; s[i]; i++) { n     int a = s[i] -  a ; n     if (p->nex[a] == NULL) { n       Tree *q = new Tree; n       q->deep = i + 1; n       p->nex[a] = q; n     } n     p = p->nex[a]; n     p->G[u].push_back(id); n   } n } n char vis1[N], vis2[N]; n int ans[N]; n long long ret; n void DFS(Tree *t) { n   for (int i = 0; i < 26; i++) n     if (t->nex[i]) DFS(t->nex[i]); n   int a = 0; n   int b = t->G[1].size(); n   for (int i = 0; i < t->G[0].size(); i++) { n     while (a < b && vis2[t->G[1][a]]) a++; n     if (a >= b) break; n     int v = t->G[0][i]; n     if (!vis1[v]) { n       vis1[v] = 1; n       vis2[t->G[1][a]] = 1; n       ret += (long long)(t->deep); n       ans[v] = t->G[1][a++]; n     } n   } n } n int main() { n   int i, j, k, m, n; n   char s[800010]; n   while (scanf( %d , &n) == 1) { n     tree = new Tree; n     for (i = 1; i <= n; i++) tree->G[0].push_back(i); n     for (i = 1; i <= n; i++) tree->G[1].push_back(i); n     for (i = 1; i <= n; i++) scanf( %s , s), Insert(s, 0, i); n     for (i = 1; i <= n; i++) scanf( %s , s), Insert(s, 1, i); n     memset(vis1, 0, sizeof(vis1)); n     memset(vis2, 0, sizeof(vis2)); n     ret = 0; n     DFS(tree); n     printf( %I64d n , ret); n     for (i = 1; i <= n; i++) printf( %d %d n , i, ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int> > v(200002); n bool ab[200002]; n int dist[200002]; n queue<int> q; n int cur, i, n, h[200002], r[200002], p[200002]; n void dfs() { n   while (!q.empty()) { n     cur = q.front(); n     q.pop(); n     ab[cur] = true; n     for (i = 0; i < v[cur].size(); i++) n       if (ab[v[cur][i]] == false) { n         ab[v[cur][i]] = true; n         q.push(v[cur][i]); n         dist[v[cur][i]] = dist[cur] + 1; n       } n   } n } n int a, b; n int main() { n   cin >> n; n   pair<int, int> fg[n]; n   for (i = 1; i < n; i++) { n     cin >> a >> b; n     fg[i] = make_pair(a, b); n     v[a].push_back(b); n     v[b].push_back(a); n   } n   q.push(1); n   ab[1] = true; n   dfs(); n   for (i = 1; i < n; i++) { n     if (dist[fg[i].first] < dist[fg[i].second]) n       p[fg[i].second] = fg[i].first; n     else n       p[fg[i].first] = fg[i].second; n   } n   for (i = 0; i < n; i++) { n     cin >> h[i]; n     r[h[i]] = i + 1; n   } n   for (i = 1; i < n; i++) n     if ((r[p[h[i]]] < r[p[h[i - 1]]]) || (dist[h[i]] < dist[h[i - 1]])) { n       cout <<  NO ; n       return 0; n     } n   cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A, typename B> n ostream& operator<<(ostream& os, const pair<A, B>& x) { n   return os <<  (  << x.first <<  ,  << x.second <<  ) ; n } n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     int n, k; n     scanf( %d%d , &n, &k); n     vector<pair<pair<int, int>, int>> M(n); n     long long offset = 0; n     for (int i = 0; i < n; ++i) { n       scanf( %d%d , &M[i].first.second, &M[i].first.first); n       offset += (k - 1) * M[i].first.second; n       M[i].second = i + 1; n     } n     sort(M.begin(), M.end()); n     const long long INF = 1LL << 60; n     vector<vector<vector<pair<long long, int>>>> dp( n         n + 1, n         vector<vector<pair<long long, int>>>( n             k + 1, vector<pair<long long, int>>(2, make_pair(-INF, -1)))); n     dp[0][0][0] = make_pair(0, 0); n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < k; ++j) { n         dp[i + 1][j][0] = max(dp[i + 1][j][0], make_pair(dp[i][j][0].first, 0)); n         dp[i + 1][j][1] = max(dp[i + 1][j][0], make_pair(dp[i][j][1].first, 0)); n         dp[i + 1][j][1] = n             max(dp[i + 1][j][1], n                 make_pair(dp[i][j][0].first + M[i].first.second, 1)); n         if (j + 1 < k) { n           for (int v = 0; v < 2; ++v) { n             dp[i + 1][j + 1][v] = n                 max(dp[i + 1][j + 1][v], n                     make_pair(dp[i][j][v].first + M[i].first.second - n                                   (k - 1 - j) * M[i].first.first, n                               2)); n           } n         } n       } n     } n     if (false) n       cerr <<  dp[n][k - 1][1]  n            <<  =  << dp[n][k - 1][1] << endl; n     int j = k - 1, c = 1; n     int las = -1; n     vector<int> pick; n     for (int i = n; i >= 1; --i) { n       int act = dp[i][j][c].second; n       assert(act != -1); n       if (act == 0) { n       } else if (act == 1) { n         assert(c == 1); n         las = i - 1; n         c = 0; n       } else { n         pick.push_back(i - 1); n         --j; n       } n     } n     assert(j == 0); n     assert(c == 0); n     reverse(pick.begin(), pick.end()); n     assert(las != -1); n     vector<bool> used(n + 1); n     vector<int> ans; n     for (int x : pick) { n       ans.push_back(M[x].second); n       used[M[x].second] = true; n     } n     used[M[las].second] = true; n     for (int i = 1; i <= n; ++i) { n       if (used[i]) continue; n       ans.push_back(i); n       ans.push_back(-i); n     } n     ans.push_back(M[las].second); n     printf( %d n , ans.size()); n     for (int x : ans) printf( %d  , x); n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long a, b, c, d; n   cin >> a >> b >> c >> d; n   if (a > b * c) { n     cout <<  -1 n ; n     return; n   } n   if (c < d) { n     cout << a <<   n ; n     return; n   } n   long long mx = c / d; n   long long k = (2 * a - b * d) / (2 * d * b); n   long long ans = 0; n   for (long long i = k - 1; i <= k + 1; i++) { n     if (i < 0 || i > mx) continue; n     ans = max(ans, (i + 1) * a - i * (i + 1) / 2 * b * d); n   } n   cout << ans <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout << fixed; n   cout.precision(30); n   long long t = 1; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int times, used[200010], t[200010], first[200010], last[200010], d[200010], n, n     a[200010]; n vector<vector<int> > g(200010); n void dfs(int v, int pr) { n   used[v] = 1; n   first[v] = ++times; n   d[v] = d[pr] + 1; n   for (int i = 0; i < g[v].size(); i++) { n     if (!used[g[v][i]]) dfs(g[v][i], v); n   } n   last[v] = times; n } n int f(int i) { return (i & (-i)); } n void in(int i, int val) { n   while (i <= n) { n     t[i] += val; n     i = i + f(i); n   } n } n int gget(int i) { n   int res = 0; n   while (i > 0) { n     res += t[i]; n     i = i - f(i); n   } n   return res; n } n int main() { n   int m, i, x, y, val, q; n   cin >> n >> m; n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (i = 0; i < n - 1; i++) { n     cin >> x >> y; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   dfs(1, 0); n   for (i = 0; i < m; i++) { n     scanf( %d , &q); n     scanf( %d , &x); n     if (q == 1) { n       scanf( %d , &val); n       in(first[x], val * (d[x] % 2 == 0 ? 1 : -1)); n       in(last[x] + 1, val * (d[x] % 2 == 0 ? -1 : 1)); n     } else { n       int ans = gget(first[x]) * (d[x] % 2 == 0 ? 1 : -1) + a[x]; n       printf( %d n , ans); n     } n   } n   return 0; n } n