#include <bits/stdc++.h> n using namespace std; n int main() { n   int N; n   while (cin >> N) { n     int Store[31][31], i, j, SumR, SumC, Count = 0; n     vector<int> Row, Col; n     memset(Store, false, sizeof(Store)); n     for (i = 0; i < N; i++) n       for (j = 0; j < N; j++) cin >> Store[i][j]; n     for (i = 0; i < N; i++) { n       SumR = 0; n       SumC = 0; n       for (j = 0; j < N; j++) { n         SumR += Store[i][j]; n         SumC += Store[j][i]; n       } n       Row.push_back(SumR); n       Col.push_back(SumC); n     } n     for (i = 0; i < N; i++) { n       for (j = 0; j < N; j++) { n         if (Row[i] < Col[j]) Count++; n       } n     } n     cout << Count << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int lim = 400100; n int dx[] = {0, 0, 1, -1}; n int dy[] = {1, -1, 0, 0}; n const long long mod = 1000000007; n long long a, b; n int main() { n   cin >> a >> b; n   long long ans; n   ans = (b * (b - 1)) / 2; n   ans %= mod; n   ans *= (((b * (((a * (a + 1)) / 2) % mod)) % mod) + a) % mod; n   ans %= mod; n   cout << ans << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n = 0; n   cin >> n; n   vector<int> ans; n   int k = 0; n   if (n == 1) { n     cout << 1 << endl << 1; n     return 0; n   } n   for (int i = 1; i <= n; i++) { n     ans.push_back(i); n     if (n - i >= 0) n       n -= i; n     else n       break; n   } n   if (n == 0) { n     cout << ans.size() << endl; n     for (int i = 0; i < ans.size(); i++) cout << ans[i] <<    ; n   } else { n     cout << ans.size() << endl; n     sort(ans.begin(), ans.end()); n     for (int i = 0; i < ans.size() - 1; i++) cout << ans[i] <<    ; n     cout << ans[ans.size() - 1] + n; n     return 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 6; n string lcp(string txt, string pat) { n   string S = pat +  $  + txt; n   vector<int> Arr(S.size(), 0); n   int j = 0; n   for (int i = 1; i < S.size(); i++) { n     while (j > 0 && S[j] != S[i]) j = Arr[j - 1]; n     if (S[j] == S[i]) j++; n     Arr[i] = j; n   } n   int MX = 0; n   for (int i = 0; i < S.size(); i++) MX = max(MX, Arr[i]); n   if (MX == pat.size()) return txt; n   return txt + pat.substr(Arr[S.size() - 1]); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   string s[3]; n   cin >> s[0] >> s[1] >> s[2]; n   sort(s, s + 3); n   int Res = 1e9 + 7; n   do { n     string s1 = lcp(lcp(s[0], s[1]), s[2]); n     string s2 = lcp(s[0], lcp(s[1], s[2])); n     int a = s1.size(), b = s2.size(); n     Res = min(Res, min(a, b)); n   } while (next_permutation(s, s + 3)); n   cout << Res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int qmin(int a, int b) { return (a < b) ? a : b; } n int qmax(int a, int b) { return (a > b) ? a : b; } n int n, m, k; n int x[555], y[555]; n int xl[555], xr[555], yl[555], yr[555]; n set<int> stx, sty; n map<int, int> mpx; n int xpos[1010]; n vector<int> l[1010], r[1010]; n multiset<pair<int, int> > y_seg_l; n bool check(int now) { n   for (int i = 0; i < 1001; i++) l[i].clear(), r[i].clear(); n   for (int i = 0; i < k; i++) n     xl[i] = qmax(x[i] - now, 1), xr[i] = qmin(x[i] + now, n), n     yl[i] = qmax(y[i] - now, 1), yr[i] = qmin(y[i] + now, m); n   stx.clear(), sty.clear(); n   for (int i = 0; i < k; i++) stx.insert(xl[i]), stx.insert(xr[i] + 1); n   stx.insert(1), stx.insert(n + 1); n   int cnt = 0; n   for (set<int>::iterator it = stx.begin(); it != stx.end(); it++, cnt++) n     mpx[*it] = cnt, xpos[cnt] = *it; n   for (int i = 0; i < k; i++) n     l[mpx[xl[i]]].push_back(i), r[mpx[xr[i] + 1]].push_back(i); n   y_seg_l.clear(); n   int xxl = n + 1, xxr = 0, yyl = m + 1, yyr = 0; n   y_seg_l.insert(make_pair(0, 0)); n   y_seg_l.insert(make_pair(m + 1, m + 1)); n   for (int i = 0; i < cnt - 1; i++) { n     for (int j = 0; j < (int)r[i].size(); j++) { n       multiset<pair<int, int> >::iterator it = n           y_seg_l.lower_bound(make_pair(yl[r[i][j]], yr[r[i][j]])); n       y_seg_l.erase(it); n     } n     for (int j = 0; j < (int)l[i].size(); j++) n       y_seg_l.insert(make_pair(yl[l[i][j]], yr[l[i][j]])); n     int nr = 0; n     int nyl = m + 1, nyr = 0; n     for (set<pair<int, int> >::iterator it = y_seg_l.begin(); n          it != y_seg_l.end(); it++) { n       if (it->first > nr + 1) n         nyl = qmin(nyl, nr + 1), nyr = qmax(nyr, it->first - 1); n       nr = qmax(nr, it->second); n     } n     if (nyr > 0) { n       xxl = qmin(xxl, xpos[i]); n       xxr = qmax(xxr, xpos[i + 1] - 1); n     } n     yyl = qmin(yyl, nyl); n     yyr = qmax(yyr, nyr); n   } n   if (xxr == 0) return 1; n   return ((xxr - xxl + 1) <= (now + now + 1)) && n          ((yyr - yyl + 1) <= (now + now + 1)); n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m >> k; n   for (int i = 0; i < k; i++) cin >> x[i] >> y[i]; n   int lb = 0, ub = 1e9; n   while (lb <= ub) { n     int mid = (lb + ub) >> 1; n     if (check(mid)) n       ub = mid - 1; n     else n       lb = mid + 1; n   } n   cout << lb << endl; n   return 0; n } n


#include <bits/stdc++.h> n namespace LYD { n inline int read() { n   int x = 0, data = 1; n   char ch = getchar(); n   while ((ch >  9  || ch <  0 ) && ch !=  - ) ch = getchar(); n   if (ch ==  - ) { n     ch = getchar(); n     data = -1; n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * data; n } n }  // namespace LYD n int st[105], ans[105]; n using namespace LYD; n namespace solve {} n using namespace solve; n int main() { n   int n = read(), k = read(), tot = 0; n   for (int i = 1; i <= n; ++i) { n     int x = read(); n     if (st[x] == 0) { n       ++tot; n       st[x] = i; n     } n   } n   if (tot < k) { n     printf( NO ); n     return 0; n   } n   int num = 0, ans_num = 0; n   printf( YES n ); n   for (int i = 1; i <= k; ++i) { n     while (st[num] == 0) ++num; n     ++ans_num; n     ans[ans_num] = st[num]; n     ++num; n   } n   std ::sort(ans + 1, ans + ans_num + 1); n   for (int i = 1; i < ans_num; ++i) printf( %d  , ans[i]); n   printf( %d , ans[ans_num]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> rs_needed[1000000 + 5]; n int arr[1000000 + 5]; n int main() { n   int i, j, k, n, idx = 0, rs, lst_pos, cminus = 0, cplus = 0; n   long long int dv = 0, mn; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d , &arr[i]); n     dv += abs(arr[i] - i); n   } n   mn = dv; n   for (i = 1; i <= n; i++) { n     if (arr[i] > i) { n       rs_needed[arr[i] - i].push_back(arr[i]); n       cminus++; n     } else { n       rs_needed[n - i + arr[i]].push_back(arr[i]); n       cplus++; n     } n   } n   lst_pos = n; n   for (rs = 0; rs < n; rs++) { n     if (dv < mn) mn = dv, idx = rs; n     for (auto x : rs_needed[rs]) { n       cminus--; n       cplus++; n     } n     dv -= n - arr[lst_pos]; n     dv += arr[lst_pos]; n     lst_pos--; n     cplus--; n     cminus++; n     dv = dv + cplus - cminus; n   } n   printf( %lld %d n , mn, idx); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n struct Item { n   int c; n   int x; n   Item() : c(), x() {} n   Item(int _c, int _x) : c(_c), x(_x) {} n }; n const long long INF = (long long)1e16; n const int N = 505; n int n, m, k; n vector<Item> a[N][N]; n vector<Item> b[N][N]; n long long s[N][N]; n long long dp[N][N]; n int DX[] = {0, 1, 1, 0}; n int DY[] = {0, -1, 0, 1}; n int main() { n   scanf( %d%d%d , &n, &m, &k); n   while (k--) { n     int x, y, d, t, c; n     scanf( %d%d%d%d%d , &x, &y, &d, &t, &c); n     vector<pair<int, int> > cur; n     for (int i = 0; i < 4; i++) { n       int xx = x + d * DX[i], yy = y + d * DY[i]; n       int tt = xx + yy; n       if (tt < t || (tt - t) % 4 != i) continue; n       cur.push_back(make_pair(xx, yy)); n     } n     if ((int)cur.size() > 2) throw; n     for (int i = 0; i < (int)cur.size(); i++) { n       s[cur[i].first][cur[i].second] += c; n       if (i == 1) { n         if (cur[0].first == cur[1].first) n           a[cur[1].first][cur[1].second].push_back(Item(c, cur[0].second)); n         else if (cur[0].second == cur[1].second) n           b[cur[1].first][cur[1].second].push_back(Item(c, cur[0].first)); n       } n     } n   } n   for (int x = 0; x < n; x++) n     for (int y = 0; y < m; y++) dp[x][y] = INF; n   dp[0][0] = 0; n   for (int x = 0; x < n; x++) n     for (int y = 0; y < m; y++) { n       long long w; n       w = dp[x][y]; n       for (int xx = x + 1; xx < n; xx++) { n         for (Item I : b[xx][y]) { n           if (I.x >= x) w -= I.c; n         } n         w += s[xx][y]; n         dp[xx][y] = min(dp[xx][y], w); n       } n       w = dp[x][y]; n       for (int yy = y + 1; yy < m; yy++) { n         for (Item I : a[x][yy]) { n           if (I.x >= y) w -= I.c; n         } n         w += s[x][yy]; n         dp[x][yy] = min(dp[x][yy], w); n       } n     } n   printf( %lld n , dp[n - 1][m - 1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200000; n long long mem[MAXN + 1][2]; n int visited[MAXN + 1][2]; n int b[MAXN + 1][2]; n int a[MAXN + 1]; n int n; n int input() { n   int i; n   scanf( %d , &n); n   for (i = 2; i <= n; i++) scanf( %d , &a[i]); n   return 0; n } n int search(int x, int k) { n   int i, tx; n   if (visited[x][k] == 1) { n     mem[x][k] = -1; n     return 0; n   } n   if (visited[x][k] == 2) { n     return 0; n   } n   if (x == 1 && k == 1) { n     b[x][k] = 1; n     mem[x][k] = 0; n     return 0; n   } n   mem[x][k] += a[x]; n   visited[x][k] = 1; n   tx = x; n   if (k) n     tx -= a[x]; n   else n     tx += a[x]; n   if (tx >= 1 && tx <= n) { n     search(tx, k ^ 1); n     if (mem[tx][k ^ 1] != -1) { n       mem[x][k] += mem[tx][k ^ 1]; n       b[x][k] = b[tx][k ^ 1]; n     } else n       mem[x][k] = -1; n   } n   visited[x][k] = 2; n   return 0; n } n int solve() { n   int i; n   memset(mem, 0, sizeof(mem)); n   memset(b, 0, sizeof(b)); n   memset(visited, 0, sizeof(visited)); n   visited[1][0] = 1; n   for (i = 2; i <= n; i++) { n     if (!visited[i][1]) search(i, 1); n   } n   for (i = 1; i < n; i++) { n     a[1] = i; n     a[0] = 0; n     if (mem[i + 1][1] == -1) n       printf( -1 n ); n     else n       printf( %I64d n , mem[i + 1][1] + i + a[b[i + 1][1]]); n   } n   return 0; n } n int main() { n   input(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1 << 30; n vector<int> w, cn, c, d, p; n vector<bool> used; n int find(int x) { return (p[x] == x ? x : p[x] = find(p[x])); } n void un(int x, int y) { n   x = find(x); n   y = find(y); n   if (rand() & 1) n     p[x] = y; n   else n     p[y] = x; n } n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   used.resize(n, false); n   cn.resize(n + 1, 0); n   p.resize(n); n   for (int i = 0; i < n; ++i) p[i] = i; n   int a, b, comp = n; n   for (int i = 0; i < m; ++i) { n     scanf( %d%d , &a, &b); n     a--; n     b--; n     if (find(a) != find(b)) un(a, b), comp--; n   } n   for (int i = 0; i < n; ++i) cn[find(p[i])]++; n   for (int i = 0; i < n; ++i) n     if (cn[i] > 0) c.push_back(cn[i]); n   cn.assign(n + 1, 0); n   for (int i = 0; i < c.size(); ++i) ++cn[c[i]]; n   c.clear(); n   for (int i = 0; i < cn.size(); ++i) n     if (cn[i] > 0) { n       int st = 1; n       while (cn[i] >= st) { n         c.push_back(st); n         w.push_back(st * i); n         cn[i] -= st; n         st *= 2; n       } n       if (cn[i] > 0) { n         c.push_back(cn[i]); n         w.push_back(cn[i] * i); n       } n     } n   int N = c.size(); n   d.resize(n + 1, INF); n   d[0] = 0; n   for (int i = 1; i <= N; ++i) n     for (int j = n; j >= w[i - 1]; --j) n       if (d[j - w[i - 1]] + c[i - 1] < d[j]) d[j] = d[j - w[i - 1]] + c[i - 1]; n   int ans = INF, tmp; n   for (int i = 4; i <= n; ++i) { n     if (ans <= d[i]) continue; n     tmp = i; n     bool good = true; n     while (tmp > 0 && good) { n       if (!(tmp % 10 == 4 || tmp % 10 == 7)) good = false; n       tmp /= 10; n     } n     if (good) ans = d[i]; n   } n   printf( %d , (ans < INF ? ans - 1 : -1)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, d, i, j, m, l, t, sz = 1, a; n long long cnt(long long p) { n   long long res = 1; n   for (i = 0; i < p; i++) res *= 2; n   return res; n } n int main() { n   cin >> t; n   for (int k = 0; k < t; k++) { n     long long h1 = 0, h0 = 0; n     cin >> n; n     for (i = 0; i < n; i++) { n       cin >> a; n       if (a == 1) h1++; n       if (a == 0) h0++; n     } n     cout << h1 * cnt(h0) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1 << 21; n const int N = int(1e5); n const long long mod = int(1e9) + 7; n const long long inf = 1e18; n const double PI = 3.1415926536; n inline int Set(int n, int pos) { return n = n | 1 << pos; } n inline bool check(int n, int pos) { return n & 1 << pos; } n inline int Reset(int n, int pos) { return n = n & ~(1 << pos); } n inline int lastbit(int n) { return n & (-n); } n inline long long mult_mod(long long a, long long b) { n   a = a % mod; n   b = b % mod; n   long long c = (a * b) % mod; n   return c; n } n inline long long add_mod(long long a, long long b) { n   a = (a + mod) % mod; n   b = (b + mod) % mod; n   long long c = (a + b) % mod; n   return c; n } n inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n inline long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } n inline long long power(long long a, long long b) { n   long long ans = 1; n   a = a % mod; n   while (b) { n     if (b & 1) ans = (ans * a) % mod; n     b >>= 1; n     a = (a * a) % mod; n   } n   return ans; n } n inline long long invmod(long long a) { n   long long ans = power(a, mod - 2); n   return ans; n } n inline unsigned long long mulmod(unsigned long long a, unsigned long long b, n                                  unsigned long long c) { n   unsigned long long x = 0, y = a % c; n   while (b > 0) { n     if (b % 2) x = (x + y) % c; n     y = (y * 2) % c; n     b >>= 1; n   } n   return x % c; n } n inline unsigned long long Largepower(unsigned long long a, unsigned long long b, n                                      unsigned long long c) { n   unsigned long long x = 1, y = a % c; n   while (b > 0) { n     if (b & 1) x = mulmod(x, y, c); n     y = mulmod(y, y, c); n     b >>= 1; n   } n   return x; n } n inline long long Egcd(long long a, long long b, long long *x, long long *y) { n   if (a == 0) { n     *x = 0, *y = 1; n     return b; n   } n   long long x1, y1; n   long long gcd = Egcd(b % a, a, &x1, &y1); n   *x = y1 - (b / a) * x1; n   *y = x1; n   return gcd; n } n inline int bs(int arr[], int l, int r, int item) { n   if (l == r) { n     if (arr[l] == item) n       return r; n     else n       return -1; n   } n   if (item <= arr[(l + r) / 2]) n     return bs(arr, l, (l + r) / 2, item); n   else n     return bs(arr, (l + r) / 2 + 1, r, item); n } n inline long long nCrMod(long long n, long long r) { n   if (r > n / 2) r = n - r; n   long long ans = 1; n   for (long long i = 1; i <= r; i++) { n     ans = (ans % mod * (n - r + i + mod) % mod) % mod; n     ans = (ans % mod * invmod(i) % mod) % mod; n   } n   return ans % mod; n } n inline long long nCr(long long n, long long r) { n   if (r > n / 2) r = n - r; n   long long ans = 1; n   for (long long i = 1; i <= r; i++) { n     ans = (ans * (n - r + i)); n     ans /= i; n   } n   return ans; n } n inline void fastin(int &x) { n   x = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) c = getchar(); n   while (c >=  0  && c <=  9 ) { n     x = (x << 3) + (x << 1) + c -  0 ; n     c = getchar(); n   } n   return; n } n int leap[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n int nleap[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n int a[2 * N + 1]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n; n   cin >> n; n   vector<int> v(2 * n); n   int odd = 0; n   int k = 0; n   for (int i = 1; i <= 2 * n; i += 2) { n     if (odd == 0) n       v[k++] = i; n     else n       v[k++] = i + 1; n     odd ^= 1; n   } n   odd = 0; n   for (int i = 2; i <= 2 * n; i += 2) { n     if (odd == 0) n       v[k++] = i; n     else n       v[k++] = i - 1; n     odd ^= 1; n   } n   int l = n - 1; n   long long s1 = v[0], s2 = v[0]; n   int i = 1, j = 2 * n - 1; n   while (l--) { n     s1 += v[i]; n     i++; n   } n   l = n - 1; n   while (l--) { n     s2 += v[j]; n     j--; n   } n   if (s1 == s2) { n     cout <<  YES n ; n     for (int i = 0; i < 2 * n; i++) cout << v[i] <<    ; n   } else n     cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int getint() { n   unsigned int c; n   int x = 0; n   while (((c = getchar()) -  0 ) >= 10) { n     if (c ==  - ) return -getint(); n     if (!~c) exit(0); n   } n   do { n     x = (x << 3) + (x << 1) + (c -  0 ); n   } while (((c = getchar()) -  0 ) < 10); n   return x; n } n const int L = 222; n const int G = 222; n int nTour, needWin, initCap; n double winProb[200]; n int award[200]; n double cache[L][L + G + 1][L]; n double solve(int pos, int cap, int won) { n   double& res = cache[pos][cap][won]; n   if (res > -1) return res; n   if (pos == nTour) { n     if (won >= needWin) { n       if (cap >= G) { n         return res = 1; n       } else { n         return res = 0; n       } n     } else { n       return res = 0; n     } n   } n   if (award[pos] == -1) { n     res = winProb[pos] * solve(pos + 1, cap - 1, won + 1); n   } else { n     res = winProb[pos] * solve(pos + 1, min(L + G, cap + award[pos]), won + 1); n   } n   res += (1 - winProb[pos]) * solve(pos + 1, cap, won); n   return res; n } n int main() { n   int i, j, k, tcc, tc = 1 << 28; n   for (tcc = 0; tcc < tc; tcc++) { n     nTour = getint(); n     needWin = getint(); n     initCap = getint(); n     for (i = 0; i < nTour; i++) { n       winProb[i] = getint() / 100.0; n     } n     for (i = 0; i < nTour; i++) { n       award[i] = getint(); n     } n     for (i = 0; i < L; i++) n       for (j = 0; j < L + G + 1; j++) n         for (k = 0; k < L; k++) cache[i][j][k] = -1e40; n     double res = solve(0, initCap + G, 0); n     printf( %.10lf n , res); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b; n   cin >> n >> a >> b; n   int position[21] = {0}; n   for (int i = 0; i < n; i++) cin >> position[i]; n   int head = 0, tail = n - 1, cost = 0; n   while (head <= tail) { n     if (position[head] != 2 && position[tail] != 2 && n         (position[head] != position[tail])) { n       cost = -1; n       break; n     } n     if (position[head] == 2 && position[tail] == 2 && head != tail) { n       if (a > b) n         cost += 2 * b; n       else n         cost += 2 * a; n     } else if (position[head] == 2 && position[tail] == 2 && head == tail) { n       if (a > b) n         cost += b; n       else n         cost += a; n     } else if (position[head] == 2 || position[tail] == 2) { n       if (position[head] == 2 && position[tail] == 0) n         cost += a; n       else if (position[head] == 2 && position[tail] == 1) n         cost += b; n       else if (position[tail] == 2 && position[head] == 0) n         cost += a; n       else if (position[tail] == 2 && position[head] == 1) n         cost += b; n     } n     head++; n     tail--; n   } n   cout << cost; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     long long int n, div = 1, ans = 0; n     cin >> n; n     long long int tt = log2(n); n     vector<long long int> w; n     if (log2(n) != tt) tt++; n     for (long long int i = 0; i < n; i++) w.push_back(i + 1); n     for (long long int j = 0; j < tt; j++) { n       vector<long long int> v, u; n       for (long long int i = 0; i < n;) { n         for (long long int j = i; j < i + div; j++) { n           if (j >= n) break; n           v.push_back(w[j]); n         } n         i += div; n         for (long long int j = i; j < i + div; j++) { n           if (j >= n) break; n           u.push_back(w[j]); n         } n         i += div; n       } n       cout << v.size() <<     << u.size() <<    ; n       for (long long int i = 0; i < v.size(); i++) cout << v[i] <<    ; n       for (long long int i = 0; i < u.size(); i++) cout << u[i] <<    ; n       cout <<   n ; n       long long int xxx; n       cin >> xxx; n       ans = max(xxx, ans); n       div *= 2; n       if (xxx == -1) { n         cout << ans; n         exit(0); n       } n     } n     cout <<  -1   << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200200; n int n, k; n int a[maxn]; n vector<int> g[maxn]; n int sz[maxn]; n int dpd[maxn], dpu[maxn]; n int mxd[maxn], mxu[maxn]; n int mark[maxn], cnt[maxn]; n int mxc[maxn], N; n int ans; n void dfs(int v, int par = -1) { n   sz[v] = 1; n   cnt[v] = mark[v]; n   int c = 0; n   for (int u : g[v]) n     if (u != par) { n       dfs(u, v); n       cnt[v] += cnt[u]; n       sz[v] += sz[u]; n       if (!cnt[u]) { n         dpd[v] += dpd[u]; n         mxd[v] += dpd[u]; n       } else n         c = max(c, mxd[u]); n     } n   if (mark[v]) n     dpd[v] = 0, mxd[v] = 0; n   else n     dpd[v]++, mxd[v] += c + 1; n } n void gfs(int v, int par = -1) { n   if (cnt[v] == N) dpu[v] = n - sz[v]; n   if (!mark[v]) { n     mxu[v] += mxc[v]; n     int d, c; n     if (cnt[v] == N) n       d = dpu[v] + 1, c = 0; n     else n       d = 1, c = mxu[v]; n     for (int u : g[v]) n       if (u != par) { n         mxu[u] += d; n         mxc[u] = max(mxc[u], c); n         if (!cnt[u]) n           d += dpd[u]; n         else n           c = max(c, mxd[u]); n       } n     reverse(g[v].begin(), g[v].end()); n     d = 0, c = 0; n     for (int u : g[v]) n       if (u != par) { n         mxu[u] += d; n         mxc[u] = max(mxc[u], c); n         if (!cnt[u]) n           d += dpd[u]; n         else n           c = max(c, mxd[u]); n       } n   } else n     dpu[v] = mxu[v] = 0; n   for (int u : g[v]) n     if (u != par) gfs(u, v); n } n bool check(int x) { n   N = 0; n   memset(sz, 0, sizeof sz); n   memset(dpd, 0, sizeof dpd); n   memset(dpu, 0, sizeof dpu); n   memset(mxc, 0, sizeof mxc); n   memset(mxd, 0, sizeof mxd); n   memset(mxu, 0, sizeof mxu); n   for (int i = 0; i < n; i++) mark[i] = (a[i] < x); n   for (int i = 0; i < n; i++) N += mark[i]; n   dfs(0); n   gfs(0); n   for (int i = 0; i < n; i++) n     if (!mark[i]) n       if (mxd[i] + dpu[i] >= k or mxu[i] + dpd[i] >= k) return true; n   return false; n } n void solve() { n   int l = 1, r = 1000001; n   int mid; n   while (l != r - 1) { n     mid = (l + r) >> 1; n     if (check(mid)) n       l = mid; n     else n       r = mid; n   } n   ans = l; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int v, u, i = 0; i < n - 1; i++) { n     cin >> v >> u; n     v--, u--; n     g[v].push_back(u); n     g[u].push_back(v); n   } n   solve(); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:256000000 ) n using namespace std; n const int INF = (int)1e9 + 7; n const long long LINF = (long long)9e18 + 7; n const long long P1 = 353251; n const long long P2 = 239017; n const long long MOD = 1e9 + 7; n const long long MOD1 = 1e9 + 7; n const long long MOD2 = 1e9 + 9; n int n, m; n long long ans = 0; n pair<long long, long long> a[107]; n long long t[107]; n const bool is_testing = 0; n int main() { n   srand( D  +  E  +  N  +  I  +  S  +  S  +  O  +  N  + time(NULL)); n   if (is_testing) { n     freopen( input.txt ,  r , stdin); n     freopen( output.txt ,  w , stdout); n   } else { n   } n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i].second >> a[i].first; n   cin >> m; n   for (int i = 0; i < m; i++) cin >> t[i]; n   long long sum = t[0]; n   for (int i = 1; i < m; i++) { n     t[i] -= sum; n     sum += t[i]; n   } n   t[m] = LINF; n   sort(a, a + n); n   int uk = 0; n   for (int i = 0; i < n; i++) { n     long long now = a[i].second; n     while (now) { n       if (now < t[uk]) { n         ans += now * a[i].first * (uk + 1); n         t[uk] -= now; n         now = 0; n       } else { n         now -= t[uk]; n         ans += t[uk] * a[i].first * (uk + 1); n         uk++; n       } n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n long long int dp[105][105][105]; n long long int opti[105][105][105]; n long long int lps[105]; n string s, s1, s2, ans; n long long int n, m, o; n long long int rec(long long int i, long long int j, long long int k) { n   if (k >= o) return -1e16; n   if (i >= n || j >= m) return 0; n   if (dp[i][j][k] != -1) return dp[i][j][k]; n   long long int an = -1e16, x = 0; n   if (s[i] == s1[j]) { n     long long int y = k; n     if (s2[k] != s[i]) { n       y--; n       y = max(y, 0ll); n       y = lps[y]; n       while (y > 0) { n         if (s2[y] == s[i]) { n           y++; n           break; n         } n         y = lps[y - 1]; n       } n       if (y == 0 && s2[y] == s[i]) y++; n     } else n       y++; n     x = rec(i + 1, j + 1, y) + 1; n     if (x > an) { n       opti[i][j][k] = 1; n       an = x; n     } n   } n   x = rec(i + 1, j, k); n   if (x > an) { n     opti[i][j][k] = 2; n     an = x; n   } n   x = rec(i, j + 1, k); n   if (x > an) { n     opti[i][j][k] = 3; n     an = x; n   } n   return dp[i][j][k] = an; n } n void computeLPSArray() { n   int len = 0; n   lps[0] = 0; n   int i = 1; n   while (i < o) { n     if (s2[i] == s2[len]) { n       len++; n       lps[i] = len; n       i++; n     } else { n       if (len != 0) { n         len = lps[len - 1]; n       } else { n         lps[i] = 0; n         i++; n       } n     } n   } n } n int main() { n   for (int i = 0; i <= 104; i++) n     for (int j = 0; j <= 104; j++) n       for (int k = 0; k <= 104; k++) { n         dp[i][j][k] = -1; n         opti[i][j][k] = -1; n       } n   cin >> s >> s1 >> s2; n   n = s.length(); n   m = s1.length(); n   o = s2.length(); n   computeLPSArray(); n   long long int an = rec(0, 0, 0); n   if (an <= 0) return cout <<  0  <<   n , 0; n   long long int i = 0, j = 0, k = 0; n   ans =   ; n   while (opti[i][j][k] != -1) { n     if (opti[i][j][k] == 1) { n       ans.push_back(s[i]); n       long long int y = k; n       if (s2[k] != s[i]) { n         y--; n         y = max(y, 0ll); n         y = lps[y]; n         while (y > 0) { n           if (s2[y] == s[i]) { n             y++; n             break; n           } n           y = lps[y - 1]; n         } n         if (y == 0 && s2[y] == s[i]) y++; n       } else n         y++; n       k = y; n       i++, j++; n     } else if (opti[i][j][k] == 2) n       i++; n     else n       j++; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16777216 ) n using namespace std; n template <typename T, int N> n struct SuffixArray { n   T data[N + 1]; n   int result[N + 1], bucket[N + 1], count[N + 1], lcp[N + 1], n; n   pair<T, int> tmp[N + 1]; n   SuffixArray() : n(0) {} n   SuffixArray(T *source, int length) { n     memcpy(data, source, length * sizeof(T)); n     n = length; n   } n   inline void reset(int length) { n = length; } n   inline void reset(T *source, int length) { n     memcpy(data, source, length * sizeof(T)); n     n = length; n   } n   void build(bool cyclic = false, bool char_speedup = false) { n     bool clean_needed = false; n     if (!cyclic) { n       clean_needed = true; n       data[n++] |= (T)1 << (8 * sizeof(T) - 1); n     } n     for (int i = 0; i < n; ++i) result[i] = i; n     int max_group = 0; n     for (int current_length = 1; current_length < n * 2; current_length *= 2) { n       if (current_length == 1) { n         if (!char_speedup) { n           for (int i = 0; i < n; ++i) tmp[i] = make_pair(data[i], i); n           sort(tmp, tmp + n); n           for (int i = 0; i < n; ++i) result[i] = tmp[i].second; n           for (int i = 0; i < n; ++i) bucket[i] = data[i]; n         } else { n           int char_count[256]; n           memset(char_count, 0, 256 * sizeof(int)); n           int minimal_value = 0; n           if (clean_needed) n             minimal_value = data[n - 1]; n           else n             minimal_value = *min_element(data, data + n); n           for (int i = 0; i < n; ++i) ++char_count[data[i] - minimal_value]; n           for (int i = 1; i < 256; ++i) char_count[i] += char_count[i - 1]; n           for (int i = n - 1; i >= 0; --i) n             lcp[--char_count[data[i] - minimal_value]] = i; n           memcpy(result, lcp, n * sizeof(int)); n           for (int i = 0; i < n; ++i) bucket[i] = data[i]; n         } n         int total_count = 0; n         for (int i = 0; i < n;) { n           int j = i; n           int current_bucket = bucket[result[i]]; n           while (j < n && bucket[result[j]] == current_bucket) ++j; n           for (int z = i; z < j; ++z) bucket[result[z]] = total_count; n           ++total_count; n           i = j; n         } n         max_group = total_count; n         continue; n       } n       int prev_length = current_length / 2; n       for (int i = 0; i < n; ++i) result[i] = (result[i] - prev_length + n) % n; n       memset(count, 0, max_group * sizeof(int)); n       for (int i = 0; i < n; ++i) ++count[bucket[i]]; n       for (int i = 1; i < max_group; ++i) count[i] += count[i - 1]; n       for (int i = n - 1; i >= 0; --i) n         lcp[--count[bucket[result[i]]]] = result[i]; n       memcpy(result, lcp, n * sizeof(int)); n       int total_count = 0; n       for (int i = 0; i < n;) { n         int j = i; n         int current_bucket_first = bucket[result[i]], n             current_bucket_second = bucket[(result[i] + prev_length) % n]; n         while (j < n && bucket[result[j]] == current_bucket_first && n                bucket[(result[j] + prev_length) % n] == current_bucket_second) n           ++j; n         for (int z = i; z < j; ++z) lcp[result[z]] = total_count; n         ++total_count; n         i = j; n       } n       max_group = total_count; n       memcpy(bucket, lcp, n * sizeof(int)); n     } n     if (clean_needed) { n       --n; n       for (int i = 0; i < n; ++i) result[i] = result[i + 1]; n     } n   } n   void buildLCP() { n     for (int i = 0; i < n; ++i) count[result[i]] = i; n     int h = 0; n     lcp[0] = 0; n     for (int i = 0; i < n; ++i) { n       if (count[i]) { n         while (data[i + h] == data[result[count[i] - 1] + h]) ++h; n         lcp[count[i]] = h--; n         if (h < 0) h = 0; n       } n     } n   } n }; n int a, b, c, d, i, j, n, m, k; n SuffixArray<char, 100002> q; n char str[200202], ans[200002]; n long long cs[200002]; n inline long long sum(int l, int r) { n   if (!l) n     return cs[r]; n   else n     return cs[r] - cs[l - 1]; n } n int main() { n   scanf( %s , str); n   scanf( %d , &k); n   n = (int)strlen(str); n   if (k > (long long)n * (n + 1) / 2) { n     puts( No such line. ); n     exit(0); n   } n   --k; n   q.reset(str, n); n   q.build(false, true); n   a = 0; n   b = n; n   for (int _n((n)-1), i(0); i <= _n; i++) { n     cs[i] = n - q.result[i]; n     if (i) cs[i] += cs[i - 1]; n   } n   for (int _n((1000000000) - 1), len(0); len <= _n; len++) { n     if (k < 0 && len) { n       ans[len] = 0; n       puts(ans); n       exit(0); n     } n     for (int _n((26) - 1), i(0); i <= _n; i++) { n       char ch =  a  + i; n       int l = a, r = b; n       while (r - l > 1) { n         int xx = (r + l) / 2; n         if (str[q.result[xx] + len] > ch) n           r = xx; n         else n           l = xx; n       } n       int right = l; n       if (str[q.result[right] + len] > ch) --right; n       l = a, r = b; n       while (r - l > 1) { n         int xx = (r + l) / 2; n         if (str[q.result[xx] + len] >= ch) n           r = xx; n         else n           l = xx; n       } n       int left = l; n       if (str[q.result[left] + len] < ch) ++left; n       if (left > right) continue; n       long long cur = sum(left, right) - (long long)len * (right - left + 1); n       if (cur <= k) n         k -= cur; n       else { n         ans[len] = ch; n         a = left; n         b = right + 1; n         k -= b - a; n         break; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n char A[5] = { a ,  e ,  i ,  o ,  u }; n int main() { n   int k; n   cin >> k; n   int rol = 0, col = 0; n   for (int i = 5; i <= sqrt(k); ++i) { n     if ((k % i == 0) && (k / i >= 5)) { n       rol = i; n       col = k / i; n       break; n     } n   } n   if (rol == 0) { n     cout << -1; n   } else { n     for (int i = 1; i <= rol; ++i) { n       for (int j = 0; j < col; ++j) { n         cout << A[(i + j) % 5]; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void __print(int x) { cerr << x; } n void __print(long x) { cerr << x; } n void __print(long long x) { cerr << x; } n void __print(unsigned x) { cerr << x; } n void __print(unsigned long x) { cerr << x; } n void __print(unsigned long long x) { cerr << x; } n void __print(float x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(char x) { cerr <<      << x <<     ; } n void __print(const char *x) { cerr <<     << x <<    ; } n void __print(const string &x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  , ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &x) { n   int f = 0; n   cerr <<  { ; n   for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n const int maxn = 105; n int n; n long long a[maxn], k; n struct Matrix { n   long long val[maxn][maxn]; n   Matrix() { memset(val, 0, sizeof val); } n   Matrix operator*(const Matrix B) { n     Matrix C; n     for (int i = 1; i <= n; ++i) { n       for (int j = 1; j <= n; ++j) { n         for (int k = 1; k <= n; ++k) { n           C.val[i][j] = (C.val[i][j] + val[i][k] * B.val[k][j]) % 1000000007; n         } n       } n     } n     return C; n   } n }; n Matrix Mu(Matrix A, long long b) { n   Matrix C; n   for (int i = 1; i <= n; ++i) C.val[i][i] = 1; n   for (; b; b /= 2, A = A * A) { n     if (b % 2) C = C * A; n   } n   return C; n } n void Solve(int Test) { n   cin >> n >> k; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n   } n   Matrix T; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= n; ++j) { n       long long x = a[i] ^ a[j]; n       if (__builtin_popcountll(x) % 3 == 0) T.val[i][j] = 1; n     } n   } n   Matrix B = Mu(T, k - 1); n   long long res = 0; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= n; ++j) { n       res = (res + B.val[i][j]) % 1000000007; n     } n   } n   cout << res; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   if (fopen( CF  n              .inp , n              r )) { n     freopen( n          CF  n          .inp , n          r , stdin); n     freopen( n          CF  n          .out , n          w , stdout); n   } n   int test = 1; n   for (int i = 1; i <= test; ++i) { n     Solve(i); n   } n } n


#include <bits/stdc++.h> n int main(void) { n   int n; n   std::cin >> n; n   std::vector<std::string> a(n); n   for (int i = 0; i < n; ++i) { n     std::cin >> a[i]; n   } n   std::set<std::string> used; n   for (int i = n - 1; i >= 0; --i) { n     if (used.find(a[i]) == used.end()) { n       used.insert(a[i]); n       std::cout << a[i] << std::endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, l, r; n   cin >> s; n   long long a, b, n; n   cin >> a >> b; n   n = s.size(); n   long long ar[n]; n   long long br[n]; n   ar[n - 1] = -1; n   br[0] = -1; n   ar[0] = (s[0] -  0 ) % a; n   br[n - 1] = (s[n - 1] -  0 ) % b; n   for (int i = 1; i < n - 1; i++) { n     ar[i] = ((ar[i - 1] * 10) % a + (s[i] -  0 )) % a; n   } n   long long x = 10; n   for (int i = n - 2; i >= 1; i--) { n     br[i] = (br[i + 1] + ((s[i] -  0 ) * x)) % b; n     x = (x * 10) % b; n   } n   int d = -1; n   for (int i = 0; i < n; i++) { n     if (s[i + 1] ==  0 ) continue; n     if (ar[i] == 0 && br[i + 1] == 0) { n       cout <<  YES  <<   n ; n       d = i; n       break; n     } n   } n   if (d == -1) { n     cout <<  NO ; n     return 0; n   } n   cout << s.substr(0, d + 1) <<   n  << s.substr(d + 1, n - d + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q, w, e, r, t; n   char a[51]; n   cin >> a; n   if (a[0] ==  h ) { n     cout <<  http:// ; n     w = 4; n   } else { n     cout <<  ftp:// ; n     w = 3; n   } n   cout << a[w]; n   w++; n   while ((a[w] !=  r ) || (a[w + 1] !=  u )) { n     cout << a[w]; n     w++; n   } n   cout <<  .ru ; n   w += 2; n   if (a[w]) { n     cout <<  / ; n     while (a[w]) { n       cout << a[w]; n       w++; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 3e+5; n bool vis[MAX_N] = {0}; n char t =  . ; n int main() { n   int n, m, ans = 0, cnt = 0; n   string str; n   cin >> n >> m >> str; n   for (int i = 0; i < n; i++) { n     if (str[i] == t) { n       ans++; n       if (i == 0 || str[i - 1] != t) cnt++; n       vis[i + 1] = 1; n     } n   } n   while (m--) { n     int i; n     char ch; n     cin >> i >> ch; n     bool a = vis[i], b = ch == t; n     if (a != b) { n       if (a) n         ans--; n       else n         ans++; n       if (vis[i - 1] && vis[i + 1] && !b) n         cnt++; n       else if (vis[i - 1] && vis[i + 1] && b) n         cnt--; n       else if (!vis[i - 1] && !vis[i + 1] && !b) n         cnt--; n       else if (!vis[i - 1] && !vis[i + 1] && b) n         cnt++; n     } n     vis[i] = b; n     cout << ans - cnt << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   string lan1, lan2; n   map<string, string> link; n   for (int i = 0; i < m; ++i) { n     cin >> lan1 >> lan2; n     link[lan1] = lan2; n   } n   for (int j = 0; j < n; ++j) { n     cin >> lan1; n     if (lan1.size() <= link[lan1].size()) n       cout << lan1 <<    ; n     else n       cout << link[lan1] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int T; n   cin >> T; n   while (T--) { n     int n, k; n     cin >> n >> k; n     string N = to_string(n); n     string ans =  99999999999999 ; n     set<char> S; n     for (auto c : N) { n       S.insert(c); n     } n     if ((int)S.size() <= k) { n       cout << N <<   n ; n       continue; n     } n     for (int d = 0; d < (int)N.size(); d++) { n       string base = N.substr(0, d); n       for (int v = N[d] -  0  + 1; v <= 9; v++) { n         string s = base + to_string(v); n         set<char> have; n         for (int i = 0; i < d; i++) { n           have.insert(N[i]); n         } n         have.insert((char)(v +  0 )); n         if ((int)have.size() > k) { n           continue; n         } n         if ((int)have.size() < k) { n           have.insert( 0 ); n         } n         while (s.size() < N.size()) { n           s += *have.begin(); n         } n         assert(s >= N); n         ans = min(ans, s); n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = (long long)1e15 + 88; n const int maxn = (int)1e6 + 88; n const int mod = 1000000007; n long long a[maxn]; n long long b[maxn]; n long long myabs(long long a) { n   if (a > 0) n     return a; n   else n     return -a; n } n int main() { n   long long n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int i = 1; i <= m; i++) { n     cin >> b[i]; n   } n   sort(b + 1, b + m + 1); n   b[0] = -inf; n   b[m + 1] = inf; n   long long dd; n   long long Max = 0; n   for (int i = 0; i < n; i++) { n     long long l = 0, r = m + 2; n     while (l <= r) { n       long long mid = (l + r) / 2; n       if (a[i] == b[mid]) { n         l = mid; n         break; n       } else if (b[mid] < a[i]) n         l = mid + 1; n       else n         r = mid - 1; n     } n     dd = min(myabs(b[l] - a[i]), myabs(b[l - 1] - a[i])); n     Max = max(Max, dd); n   } n   cout << Max; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 7; n int n, w, h; n struct Point { n   int d, p, id; n   bool operator<(const Point& r) const { return p < r.p; } n }; n vector<Point> px[N], py[N]; n int ansx[N], ansy[N], tmpx[N], tmpy[N]; n int main() { n   scanf( %d%d%d , &n, &w, &h); n   for (int i = 0; i < n; ++i) { n     int g, p, t; n     scanf( %d%d%d , &g, &p, &t); n     if (g == 1) n       px[p - t + 100000].push_back({0, p, i}); n     else n       py[p - t + 100000].push_back({1, p, i}); n   } n   for (int i = 0; i < N; ++i) sort(px[i].begin(), px[i].end()); n   for (int i = 0; i < N; ++i) sort(py[i].begin(), py[i].end()); n   for (int i = 0; i < N; ++i) { n     int cur = 0; n     for (int j = 0; j < px[i].size(); ++j) n       tmpx[cur] = px[i][j].p, tmpy[cur] = h, ++cur; n     for (int j = (int)py[i].size() - 1; j >= 0; --j) n       tmpx[cur] = w, tmpy[cur] = py[i][j].p, ++cur; n     cur = 0; n     for (int j = (int)py[i].size() - 1; j >= 0; --j) n       ansx[py[i][j].id] = tmpx[cur], ansy[py[i][j].id] = tmpy[cur], ++cur; n     for (int j = 0; j < px[i].size(); ++j) n       ansx[px[i][j].id] = tmpx[cur], ansy[px[i][j].id] = tmpy[cur], ++cur; n   } n   for (int i = 0; i < n; ++i) printf( %d %d n , ansx[i], ansy[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, t; n   cin >> t; n   while (t--) { n     int count = 0; n     int ans; n     cin >> n; n     for (int i = 1; i <= 1666; i++) { n       if ((i % 10 != 3) && i % 3 != 0) { n         count++; n       } n       if (count == n) { n         ans = i; n         break; n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> Arr; n int main() { n   int N, K; n   cin >> N >> K; n   int Num, Acum; n   int i, j; n   for (i = 0; i < N; i++) { n     cin >> Num; n     Arr.push_back(Num); n   } n   sort(Arr.begin(), Arr.end()); n   j = 0; n   Acum = 0; n   for (i = 0; i < K; i++) { n     if (j >= Arr.size()) { n       cout << 0 <<   n ; n       continue; n     } n     Arr[j] -= Acum; n     cout << Arr[j] <<   n ; n     Acum += Arr[j]; n     Arr[j] = 0; n     while (Arr[j] <= Acum && j < Arr.size()) j += 1; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, P, S; n int s, t; n vector<int> res1, res2; n struct node { n   int to; n   int cap; n   int opp; n   int cost; n }; n vector<node> v[3500]; n int d[3500]; n bool in[3500]; n int pre[3500], wh[3500]; n void add(int x, int y, int cap, int cost) { n   node tmp; n   tmp.to = y; n   tmp.cap = cap; n   tmp.cost = cost; n   tmp.opp = v[y].size(); n   v[x].push_back(tmp); n   tmp.to = x; n   tmp.cap = 0; n   tmp.cost = -cost; n   tmp.opp = v[x].size() - 1; n   v[y].push_back(tmp); n } n bool spfa() { n   memset(in, 0, sizeof(in)); n   for (int i = s; i <= t; i++) d[i] = -199912310; n   queue<int> q; n   q.push(s); n   d[s] = 0; n   while (!q.empty()) { n     int x = q.front(); n     q.pop(); n     in[x] = false; n     for (int i = 0; i < (int)v[x].size(); i++) { n       if (v[x][i].cap <= 0) continue; n       if (d[v[x][i].to] < d[x] + v[x][i].cost) { n         d[v[x][i].to] = d[x] + v[x][i].cost; n         pre[v[x][i].to] = x; n         wh[v[x][i].to] = i; n         if (!in[v[x][i].to]) { n           in[v[x][i].to] = true; n           q.push(v[x][i].to); n         } n       } n     } n   } n   return d[t] != -199912310; n } n long long max_flow() { n   long long ret = 0; n   while (spfa()) { n     int flow = 199912310; n     for (int i = t; i; i = pre[i]) { n       flow = min(flow, v[pre[i]][wh[i]].cap); n     } n     ret += 1ll * flow * d[t]; n     for (int i = t; i; i = pre[i]) { n       v[pre[i]][wh[i]].cap -= flow; n       v[i][v[pre[i]][wh[i]].opp].cap += flow; n     } n   } n   return ret; n } n int main() { n   scanf( %d , &n); n   scanf( %d%d , &P, &S); n   s = 0; n   t = n + 3; n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     add(i, n + 1, 1, x); n   } n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     add(i, n + 2, 1, x); n     add(0, i, 1, 0); n   } n   add(n + 1, t, P, 0); n   add(n + 2, t, S, 0); n   printf( %d n , max_flow()); n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < (int)v[i].size(); j++) { n       if (v[i][j].to == n + 1 && v[i][j].cap == 0) { n         res1.push_back(i); n       } n       if (v[i][j].to == n + 2 && v[i][j].cap == 0) { n         res2.push_back(i); n       } n     } n   } n   for (int i = 0; i < (int)res1.size(); i++) printf( %I64d  , res1[i]); n   puts(  ); n   for (int i = 0; i < (int)res2.size(); i++) printf( %d  , res2[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a, b; n   cin >> a; n   cin >> b; n   sort(a.begin(), a.end()); n   for (int i = 0; i < a.size(); i++) { n     if (a[i] !=  0  && a[0] ==  0 ) { n       swap(a[i], a[0]); n       break; n     } n   } n   if (a == b) n     cout <<  OK  << endl; n   else n     cout <<  WRONG_ANSWER  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int n, x; n   cin >> n >> x; n   long long int a[n]; n   set<long long int> s; n   long long int sum = 0; n   for (long long int i = 0; i < n; i++) { n     cin >> a[i]; n     s.insert(a[i]); n     sum += a[i]; n   } n   long long int y = *(s.begin()); n   if (s.size() == 1 && y == x) { n     cout << 0 <<   n ; n     return; n   } n   if (s.find(x) != s.end()) { n     cout << 1 <<   n ; n     return; n   } n   if (sum == n * x) { n     cout << 1 <<   n ; n     return; n   } n   cout << 2 <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   cout.precision(20); n   long long int t = 1; n   cin >> t; n   for (long long int i = 1; i <= t; i++) { n     if (0) cout <<  Case #  << i <<  :  ; n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n #pragma optimize( -O3 ) n   n #define f first n #define s second n #define pb push_back n #define endl   n  n   n map<vector<int>, int>ma; n map<vector<int>, bool>che; n   n   n int main() { n  ios::sync_with_stdio(false); n  cin.tie(NULL); n  cout.tie(NULL); n  queue<vector<int>>q; n  vector<int>v; n  int n; n  cin>>n; n   n  for(int i=1; i<=2*n; i++){ n   v.pb(i); n  } n  q.push(v); n   n  ma[v]=0; n  while(!q.empty()){ n   vector<int>u=q.front(); n   q.pop(); n   if(che[u]){ n    continue; n   } n   che[u]=1; n   vector<int>v1; n   vector<int>v2; n   v1.resize(2*n); n   v2.resize(2*n); n   for(int i=0; i<u.size(); i++){ n    if(i%2==0){ n     v1[i+1]=u[i]; n    } n    else{ n     v1[i-1]=u[i]; n    } n   } n   for(int i=0; i<n; i++){ n    v2[i+n]=u[i]; n   } n   for(int i=n; i<2*n; i++){ n    v2[i-n]=u[i]; n   } n    n   int d1=ma[u]; n   int d2=ma[v1]; n   int d3=ma[v2]; n   if(d2>1+d1 || !che[v1]){ n    ma[v1]=1+d1; n    q.push(v1); n   } n   if(d3>1+d1 || !che[v2]){ n    ma[v2]=1+d1; n    q.push(v2); n   } n    n  } n  vector<int>vec; n  for(int i=1; i<=2*n; i++){ n   int x; n   cin>>x; n   vec.pb(x); n  } n  if(!che[vec]){ n   cout<<-1; n  } n  else{ n   cout<<ma[vec]; n  } n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 998244353; n int Power(int n, int x) { n   int mul = n; n   int res = 1; n   while (x > 0) { n     if (x & 1) res = 1ll * res * mul % MOD; n     mul = 1ll * mul * mul % MOD; n     x /= 2; n   } n   return res; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   int n; n   cin >> n; n   vector<int> a(n + 1), pref(n + 1); n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   sort(begin(a) + 1, end(a), greater<int>()); n   for (int i = 1; i <= n; i++) { n     pref[i] = (a[i] + pref[i - 1]) % MOD; n   } n   vector<int> ans; n   for (int k = 1; k <= n; k++) { n     int res = 0; n     for (int i = 0, j = 0; i <= n; i += k, j++) { n       res = (res + (1ll * j * ((pref[min(i + k, n)] - pref[i]) % MOD) % MOD)) % n             MOD; n     } n     ans.emplace_back(1ll * res * Power(n, MOD - 2) % MOD); n   } n   for (auto i : ans) { n     if (i < 0) i += MOD; n     cout << i <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool odd(int N) { n   while (N < 0) { n     N += 2; n   } n   return N % 2 == 1 ? true : false; n } n bool even(int N) { n   while (N < 0) { n     N += 2; n   } n   return N % 2 == 0 ? true : false; n } n int main() { n   string s, t; n   cin >> s >> t; n   int N = s.size(); n   string result =   ; n   int num = 0; n   for (int i = 0; i != N; ++i) { n     if (s[i] != t[i]) { n       ++num; n       if (num % 2) { n         result += s[i]; n       } else { n         result += t[i]; n       } n     } else { n       result += s[i]; n     } n   } n   if (even(num)) { n     cout << result; n   } else { n     cout <<  impossible ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   long long x, y; n } A[100005]; n int pos1[100005], pos2[100005], pos3[100005], pos4[100005]; n int cmp1(int a, int b) { return A[a].x < A[b].x; } n int cmp2(int a, int b) { return A[a].x > A[b].x; } n int cmp3(int a, int b) { return A[a].y < A[b].y; } n int cmp4(int a, int b) { return A[a].y > A[b].y; } n int mark[100005]; n int main() { n   int i, n, k, p1, p2, p3, p4; n   long long x1, x2, y1, y2; n   scanf( %d%d , &n, &k); n   for (i = 1; i <= n; ++i) { n     scanf( %lld%lld%lld%lld , &x1, &y1, &x2, &y2); n     A[i].x = x1 + x2; n     A[i].y = y1 + y2; n     pos1[i] = pos2[i] = pos3[i] = pos4[i] = i; n   } n   sort(pos1 + 1, pos1 + n + 1, cmp1); n   sort(pos2 + 1, pos2 + n + 1, cmp2); n   sort(pos3 + 1, pos3 + n + 1, cmp3); n   sort(pos4 + 1, pos4 + n + 1, cmp4); n   int now = 0, tot; n   long long ans = 1LL << 62; n   for (p1 = 0; p1 <= k; ++p1) { n     for (p2 = 0; p2 <= k; ++p2) { n       for (p3 = 0; p3 <= k; ++p3) { n         for (p4 = 0; p4 <= k; ++p4) { n           now++; n           tot = 0; n           for (i = 1; i <= p1; ++i) n             if (mark[pos1[i]] != now) mark[pos1[i]] = now, tot++; n           for (i = 1; i <= p2; ++i) n             if (mark[pos2[i]] != now) mark[pos2[i]] = now, tot++; n           for (i = 1; i <= p3; ++i) n             if (mark[pos3[i]] != now) mark[pos3[i]] = now, tot++; n           for (i = 1; i <= p4; ++i) n             if (mark[pos4[i]] != now) mark[pos4[i]] = now, tot++; n           if (tot != k) continue; n           long long max_x = -0x7fffffff, min_x = 0x7fffffff, n                     max_y = -0x7fffffff, min_y = 0x7fffffff; n           for (i = 1; i <= n; ++i) n             if (mark[i] != now) { n               max_x = (max_x > A[i].x ? max_x : A[i].x); n               min_x = (min_x > A[i].x ? A[i].x : min_x); n               max_y = (max_y > A[i].y ? max_y : A[i].y); n               min_y = (min_y > A[i].y ? A[i].y : min_y); n             } n           long long x = max_x - min_x, y = max_y - min_y; n           x = (2LL > x ? 2LL : x); n           y = (2LL > y ? 2LL : y); n           ans = (ans > x * y ? x * y : ans); n         } n       } n     } n   } n   printf( %lld n , ans / 4); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n using v2d = vector<vector<T> >; n template <class T> n bool uin(T &a, T b) { n   return a > b ? (a = b, true) : false; n } n template <class T> n bool uax(T &a, T b) { n   return a < b ? (a = b, true) : false; n } n mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); n const int maxN = 1e3 + 10; n const int maxK = 1e3 + 10; n const long long mod = 998244353; n int n, k, a[maxN], lowbound[maxN], upbound[maxN]; n long long dp[maxN][maxK], res = 0; n inline void normalize(long long &a) { a = (a % mod + mod) % mod; } n long long Try(int dist) { n   long long r = 0; n   for (int i = 1; i <= (int)(n); ++i) { n     lowbound[i] = lowbound[i - 1]; n     while (a[i] - a[lowbound[i] + 1] >= dist) { n       ++lowbound[i]; n     } n     upbound[i] = upbound[i - 1]; n     while (a[i] - a[upbound[i] + 1] > dist) { n       ++upbound[i]; n     } n   } n   for (int i = 1; i <= (int)(n); ++i) { n     dp[i][1] = dp[i - 1][1] + 1; n     for (int j = (int)(2); j <= (int)(k); ++j) { n       dp[i][j] = dp[lowbound[i]][j - 1] + dp[i - 1][j]; n       if (dp[i][j] >= mod) { n         dp[i][j] -= mod; n       } n     } n   } n   r = dp[n][k]; n   for (int i = 1; i <= (int)(n); ++i) { n     dp[i][1] = dp[i - 1][1] + 1; n     for (int j = (int)(2); j <= (int)(k); ++j) { n       dp[i][j] = dp[upbound[i]][j - 1] + dp[i - 1][j]; n       if (dp[i][j] >= mod) { n         dp[i][j] -= mod; n       } n     } n   } n   r -= dp[n][k]; n   if (r < 0) { n     r += mod; n   } n   return r; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> n >> k; n   for (int i = 1; i <= (int)(n); ++i) { n     cin >> a[i]; n   } n   sort(a + 1, a + n + 1); n   for (int i = 1; i <= (int)(a[n] / (k - 1)); ++i) { n     res += i * Try(i) % mod; n     if (res >= mod) { n       res -= mod; n     } n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const int maxn = 1e6 + 5; n const int mul = 9997; n long long h[maxn]; n long long p[maxn]; n string s; n vector<int> Z() { n   int L = 0, R = 0; n   vector<int> z(s.size()); n   z[0] = s.size(); n   for (int i = 1; i < s.size(); i++) { n     if (i <= R) { n       z[i] = z[i - L]; n     } else { n       L = i; n       if (i > R) { n         R = i; n       } n       while (R < z[0] && s[R - L] == s[R]) R++; n       z[i] = (R--) - L; n     } n   } n   return z; n } n void go() { n   int n, k; n   cin >> n >> k; n   cin >> s; n   auto z = Z(); n   int f = 1; n   int cnt = 0; n   for (int i = 0; i < k; i++) { n     int pos; n     cin >> pos; n     cnt += max(0, pos - f); n     if (pos - f < 0 && z[z.size() - f + pos] < f - pos) { n       cout << 0 <<   n ; n       return; n     } n     f = pos + s.size(); n   } n   cnt += n + 1 - f; n   long long ans = 1; n   for (int i = 0; i < cnt; i++) { n     ans *= 26; n     ans %= mod; n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int c = 0; n   int t; n   if (!c) { n     t = 1; n   } else { n     cin >> t; n   } n   while (t--) { n     go(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n void __print(int x) { cerr << x; } n void __print(long x) { cerr << x; } n void __print(long long x) { cerr << x; } n void __print(unsigned x) { cerr << x; } n void __print(unsigned long x) { cerr << x; } n void __print(unsigned long long x) { cerr << x; } n void __print(float x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(char x) { cerr <<      << x <<     ; } n void __print(const char *x) { cerr <<     << x <<    ; } n void __print(const string &x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  , ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &x) { n   int f = 0; n   cerr <<  { ; n   for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n string s[105]; n long long tot(long long i, long long j) { n   long long ans = 0; n   if (s[i][j] ==  1 ) ans++; n   if (s[i + 1][j] ==  1 ) ans++; n   if (s[i][j + 1] ==  1 ) ans++; n   if (s[i + 1][j + 1] ==  1 ) ans++; n   return ans; n } n vector<long long> calc(long long i, long long j, long long cnt) { n   vector<pair<long long, long long> > st1, st0; n   vector<long long> res; n   if (s[i][j] ==  1 ) n     st1.push_back({i, j}); n   else n     st0.push_back({i, j}); n   if (s[i + 1][j] ==  1 ) n     st1.push_back({i + 1, j}); n   else n     st0.push_back({i + 1, j}); n   if (s[i][j + 1] ==  1 ) n     st1.push_back({i, j + 1}); n   else n     st0.push_back({i, j + 1}); n   if (s[i + 1][j + 1] ==  1 ) n     st1.push_back({i + 1, j + 1}); n   else n     st0.push_back({i + 1, j + 1}); n   if (cnt == 1 || cnt == 2) { n     for (long long k = 0; k < 1; k++) { n       long long x = st1[k].first; n       long long y = st1[k].second; n       res.push_back(x); n       res.push_back(y); n       s[x][y] =  0 ; n     } n     for (long long k = 0; k < 2; k++) { n       long long x = st0[k].first; n       long long y = st0[k].second; n       res.push_back(x); n       res.push_back(y); n       s[x][y] =  1 ; n     } n   } else if (cnt == 3 || cnt == 4) { n     for (long long k = 0; k < 3; k++) { n       long long x = st1[k].first; n       long long y = st1[k].second; n       res.push_back(x); n       res.push_back(y); n       s[x][y] =  0 ; n     } n   } n   return res; n } n void solve() { n   long long n, m; n   cin >> n >> m; n   for (long long i = 0; i < n; i++) cin >> s[i]; n   vector<vector<long long> > ans; n   if (n & 1) { n     for (long long i = 0; i < m; i += 2) { n       long long x = (n - 1), y = i; n       if (i == (m - 1)) y = (i - 1); n       vector<long long> res; n       if (s[x][y] ==  1  || s[x][y + 1] ==  1 ) { n         if (s[x][y] ==  1  && s[x][y + 1] ==  1 ) { n           res.push_back(x); n           res.push_back(y); n           s[x][y] =  0 ; n           res.push_back(x); n           res.push_back(y + 1); n           s[x][y + 1] =  0 ; n           res.push_back(x - 1); n           res.push_back(y); n           s[x - 1][y] = (!(s[x - 1][y] -  0 )) +  0 ; n         } else if (s[x][y] ==  1 ) { n           res.push_back(x); n           res.push_back(y); n           s[x][y] =  0 ; n           res.push_back(x - 1); n           res.push_back(y); n           s[x - 1][y] = (!(s[x - 1][y] -  0 )) +  0 ; n           res.push_back(x - 1); n           res.push_back(y + 1); n           s[x - 1][y + 1] = (!(s[x - 1][y + 1] -  0 )) +  0 ; n         } else if (s[x][y + 1] ==  1 ) { n           res.push_back(x); n           res.push_back(y + 1); n           s[x][y + 1] =  0 ; n           res.push_back(x - 1); n           res.push_back(y); n           s[x - 1][y] = (!(s[x - 1][y] -  0 )) +  0 ; n           res.push_back(x - 1); n           res.push_back(y + 1); n           s[x - 1][y + 1] = (!(s[x - 1][y + 1] -  0 )) +  0 ; n         } n       } n       if (res.size() > 0) ans.push_back(res); n     } n     n--; n   } n   if (m & 1) { n     for (long long i = 0; i < n; i += 2) { n       long long x = i, y = (m - 1); n       if (i == (n - 1)) x = (i - 1); n       vector<long long> res; n       if (s[x][y] ==  1  || s[x + 1][y] ==  1 ) { n         if (s[x][y] ==  1  && s[x + 1][y] ==  1 ) { n           res.push_back(x); n           res.push_back(y); n           s[x][y] =  0 ; n           res.push_back(x + 1); n           res.push_back(y); n           s[x + 1][y] =  0 ; n           res.push_back(x); n           res.push_back(y - 1); n           s[x][y - 1] = (!(s[x][y - 1] -  0 )) +  0 ; n         } else if (s[x][y] ==  1 ) { n           res.push_back(x); n           res.push_back(y); n           s[x][y] =  0 ; n           res.push_back(x); n           res.push_back(y - 1); n           s[x][y - 1] = (!(s[x][y - 1] -  0 )) +  0 ; n           res.push_back(x + 1); n           res.push_back(y - 1); n           s[x + 1][y - 1] = (!(s[x + 1][y - 1] -  0 )) +  0 ; n         } else if (s[x + 1][y] ==  1 ) { n           res.push_back(x + 1); n           res.push_back(y); n           s[x + 1][y] =  0 ; n           res.push_back(x); n           res.push_back(y - 1); n           s[x][y - 1] = (!(s[x][y - 1] -  0 )) +  0 ; n           res.push_back(x + 1); n           res.push_back(y - 1); n           s[x + 1][y - 1] = (!(s[x + 1][y - 1] -  0 )) +  0 ; n         } n       } n       if (res.size() > 0) ans.push_back(res); n     } n     m--; n   } n   for (long long i = 0; i < (n); i += 2) { n     for (long long j = 0; j < (m); j += 2) { n       long long cnt = tot(i, j); n       while (cnt > 0) { n         vector<long long> res = calc(i, j, cnt); n         cnt = tot(i, j); n         ans.push_back(res); n       } n     } n   } n   cout << ans.size() << endl; n   for (long long i = 0; i < ans.size(); i++) { n     for (long long j = 0; j < 6; j++) { n       cout << ans[i][j] + 1 <<    ; n     } n     cout << endl; n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   for (long long i = 0; i < t; i++) { n     solve(); n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 1; n const int D = 200 + 1; n int n; n pair<int, int> a[N]; n int cn[N]; n int cd[D]; n long long sum, ans; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i].first; n     cn[a[i].first]++; n   } n   for (int i = 0; i < n; i++) { n     cin >> a[i].second; n     sum += a[i].second; n   } n   sort(a, a + n); n   int it = 0; n   ans = (long long)1e18; n   for (int i = 0; i < N && it < n; i++) { n     if (a[it].first > i) continue; n     int ln = 0; n     long long ls = 0; n     for (int j = 0; j < D; j++) { n       int cnt = min(cd[j], max((it - ln) - cn[i] + 1, 0)); n       ln += cnt, ls += j * cnt; n     } n     while (it < n && a[it].first == i) { n       sum -= a[it].second; n       cd[a[it].second]++; n       it++; n     } n     ans = min(ans, ls + sum); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<int> v[100086]; n int x, y; n int deg[100086], mxd; n int ans[100086 << 3][2], cnt; n void dfs(int i, int fa, int val) { n   int x = deg[i] - (i != 1); n   int fin = val - 1; n   ans[++cnt][0] = i, ans[cnt][1] = val; n   for (int j = 0; j < v[i].size(); j++) { n     if (v[i][j] == fa) continue; n     --x; n     if (val + 1 > mxd) val = fin - x - 1, ans[++cnt][0] = i, ans[cnt][1] = val; n     dfs(v[i][j], i, ++val); n     ans[++cnt][0] = i, ans[cnt][1] = val; n   } n   if ((i ^ 1) && val != fin) ans[++cnt][0] = i, ans[cnt][1] = fin; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     scanf( %d%d , &x, &y); n     v[x].push_back(y), v[y].push_back(x); n     deg[x]++, deg[y]++; n   } n   for (int i = 1; i <= n; i++) mxd = max(mxd, deg[i]); n   dfs(1, 0, 0); n   printf( %d n , cnt); n   for (int i = 1; i <= cnt; i++) printf( %d %d n , ans[i][0], ans[i][1]); n } n


#include <bits/stdc++.h> n using namespace std; n long long n, arr[100003], pre[100003], suf[100003]; n int bitidx; n bool cmp(long long a, long long b) { n   return ((a >> bitidx) & 1) < ((b >> bitidx) & 1); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   pre[0] = 0; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     pre[i + 1] = pre[i] ^ arr[i]; n   } n   suf[0] = 0; n   for (int i = 0; i < n; i++) suf[i + 1] = suf[i] ^ arr[n - i - 1]; n   sort(suf, suf + n + 1); n   long long mx = 0; n   for (int k = 0; k <= n; k += 1) { n     int st = 0, en = n; n     for (int i = 40; i >= 0; i -= 1) { n       int bit = (pre[k] >> i) & 1; n       bitidx = i; n       int pos = lower_bound(suf + st, suf + en + 1, (1LL << i), cmp) - suf; n       if (pos > en) continue; n       if (!bit) n         st = pos; n       else n         en = pos - 1; n     } n     mx = max(mx, pre[k] ^ suf[st]); n   } n   cout << mx; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> gra[28]; n char s[100005], re[100005]; n int main() { n   int i, l, j, st, l1; n   while (scanf( %s , &s) == 1) { n     l = strlen(s); n     l1 = 0; n     for (i = 0; i < 26; i++) gra[i].clear(); n     for (i = 0; i < l; i++) gra[s[i] -  a ].push_back(i); n     st = -1; n     for (i = 25; i >= 0; i--) n       if (gra[i].size() > 0) { n         for (j = 0; j < gra[i].size(); j++) n           if (gra[i][j] > st) re[l1++] =  a  + i, st = gra[i][j]; n       } n     re[l1] =   0 ; n     printf( %s n , re); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, M; n int A[200], B[200], D[200]; n map<int, vector<int>> MM; n set<int> S; n struct mat { n   signed long long v[150][3] = {}; n }; n struct vec { n   signed long long v[3] = {}; n }; n void print(mat a, int n) { n   int x, y; n   for (x = 0; x < n; x++) { n     (void)printf( %d:  , x); n     for (y = 0; y < n; y++) n       (void)printf( %d , (a.v[x][y / 64] & (1LL << (y % 64))) != 0); n     (void)printf(  n ); n   } n } n void print(vec a, int n) { n   int i; n   for (i = 0; i < n; i++) n     (void)printf( %d , (a.v[i / 64] & (1LL << (i % 64))) != 0); n   (void)printf(  n ); n } n mat mult(mat a, mat b, int n) { n   mat c, d; n   int x, y, z; n   for (x = 0; x < n; x++) n     for (y = 0; y < n; y++) n       if (b.v[y][x / 64] & (1LL << (x % 64))) c.v[x][y / 64] |= 1LL << (y % 64); n   for (x = 0; x < n; x++) n     for (y = 0; y < n; y++) { n       signed long long v = 0; n       for (z = 0; z < (n + 63) / 64; z++) v |= a.v[x][z] & c.v[y][z]; n       v = (v != 0); n       d.v[x][y / 64] |= v << (y % 64); n     } n   return d; n } n mat init(int n) { n   mat r; n   int x; n   for (x = 0; x < n; x++) r.v[x][x / 64] |= 1LL << (x % 64); n   return r; n } n mat powm(mat a, int p, int n) { n   mat r = init(n); n   while (p) { n     if (p % 2) r = mult(r, a, n); n     a = mult(a, a, n); n     p /= 2; n   } n   return r; n } n vec mult(mat a, vec b, int n) { n   vec c; n   int x, y; n   for (x = 0; x < n; x++) n     if (b.v[x / 64] & (1LL << (x % 64))) n       for (y = 0; y < (n + 63) / 64; y++) c.v[y] |= a.v[x][y]; n   return c; n } n bool reachable(mat a, vec b) { n   int x, y; n   for (x = 0; x < 150; x++) { n     vec c = mult(a, b, N); n     b.v[0] |= c.v[0]; n     b.v[1] |= c.v[1]; n     b.v[2] |= c.v[2]; n   } n   int n = N - 1; n   return (b.v[n / 64] & (1LL << (n % 64))) != 0; n } n void solve() { n   int i, j, k, l, r, x, y; n   string s; n   cin >> N >> M; n   for (i = 0; i < M; i++) { n     cin >> A[i] >> B[i] >> D[i]; n     A[i]--; n     B[i]--; n     MM[D[i]].push_back(i); n     S.insert(D[i]); n   } n   int cur = 0; n   vec v; n   mat m; n   v.v[0] = 1; n   for (auto& r : MM) { n     v = mult(powm(m, r.first - cur, N), v, N); n     cur = r.first; n     for (auto& r2 : r.second) m.v[A[r2]][B[r2] / 64] |= 1LL << (B[r2] % 64); n     if (reachable(m, v)) break; n   } n   if (!reachable(m, v)) return (void)printf( Impossible n ); n   int n = N - 1; n   while ((v.v[n / 64] & (1LL << (n % 64))) == 0) { n     v = mult(m, v, N); n     cur++; n     if (S.count(cur)) n       for (auto& r2 : MM[cur]) m.v[A[r2]][B[r2] / 64] |= 1LL << (B[r2] % 64); n   } n   cout << cur << endl; n } n int main(int argc, char** argv) { n   string s; n   int i; n   if (argc == 1) ios::sync_with_stdio(false); n   for (i = 0; i < argc - 1; i++) s += argv[i + 1], s +=   n ; n   for (i = 0; i < s.size(); i++) ungetc(s[s.size() - 1 - i], stdin); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool isPrime(long long int n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   long long int p = sqrt(n); n   for (int i = 5; i <= p; i += 6) n     if (n % i == 0 || n % (i + 2) == 0) return false; n   return true; n } n string d2b(int n) { n   string s; n   for (int i = 31; i >= 0; i--) { n     int k = n >> i; n     if (k & 1) n       s +=  1 ; n     else n       s +=  0 ; n   } n   return s; n } n long long int gcd(long long int a, long long int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long int lcm(long long int a, long long int b) { n   return (a * b) / gcd(a, b); n } n long long int b2d(string n) { n   string num = n; n   long long int dec_value = 0; n   long long int base = 1; n   long long int len = num.length(); n   for (int i = len - 1; i >= 0; i--) { n     if (num[i] ==  1 ) dec_value += base; n     base = base * 2; n   } n   return dec_value; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int test_case = 1; n   cin >> test_case; n   while (test_case--) { n     int n, k; n     cin >> n >> k; n     int a[n]; n     for (long long i = 0; i < n; i++) cin >> a[i]; n     sort(a, a + n); n     for (long long i = 1; i < n; i++) { n       a[i] = k - a[i]; n     } n     int temp = a[0]; n     for (long long i = 1; i < n; i++) { n       a[i] = a[i] / temp; n     } n     int s = 0; n     for (long long i = 1; i < n; i++) s += a[i]; n     cout << s << endl; n   } n } n


#include <bits/stdc++.h> n int *ej[100000], eo[100000]; n long long *ew[100000]; n void append(int i, int j, long long w) { n   int o = eo[i]++; n   if (o >= 2 && (o & o - 1) == 0) { n     ej[i] = (int *)realloc(ej[i], o * 2 * sizeof *ej[i]); n     ew[i] = (long long *)realloc(ew[i], o * 2 * sizeof *ew[i]); n   } n   ej[i][o] = j, ew[i][o] = w; n } n long long bb[60], b_; n int sz; n void add(long long b) { n   int l; n   b_ |= b; n   for (l = 0; l < 60; l++) n     if (b & 1LL << l) { n       if (bb[l] & 1LL << l) n         b ^= bb[l]; n       else { n         bb[l] = b, sz++; n         return; n       } n     } n } n long long xx[100000]; n char visited[100000]; n int kk[60], cnt; n void dfs(int p, int i, long long x) { n   int o, l; n   if (visited[i]) { n     add(xx[i] ^ x); n     return; n   } n   xx[i] = x, visited[i] = 1, cnt++; n   for (l = 0; l < 60; l++) n     if (x & 1LL << l) kk[l]++; n   for (o = eo[i]; o--;) { n     int j = ej[i][o]; n     long long w = ew[i][o]; n     if (j != p) dfs(i, j, x ^ w); n   } n } n int main() { n   int n, m, i, j, ans; n   scanf( %d%d , &n, &m); n   for (i = 0; i < n; i++) { n     ej[i] = (int *)malloc(2 * sizeof *ej[i]); n     ew[i] = (long long *)malloc(2 * sizeof *ew[i]); n   } n   while (m--) { n     long long w; n     scanf( %d%d%lld , &i, &j, &w), i--, j--; n     append(i, j, w), append(j, i, w); n   } n   ans = 0; n   for (i = 0; i < n; i++) n     if (!visited[i]) { n       int l, p; n       memset(bb, 0, 60 * sizeof *bb), b_ = 0, sz = 0; n       memset(kk, 0, 60 * sizeof *kk); n       cnt = 0; n       dfs(-1, i, 0); n       p = 1; n       for (l = 0; l < 60; l++) { n         int k; n         if (b_ & 1LL << l) n           k = (long long)cnt * (cnt - 1) / 2 % 1000000007 * n               ((1LL << sz - 1) % 1000000007) % 1000000007; n         else n           k = (long long)kk[l] * (cnt - kk[l]) % 1000000007 * n               ((1LL << sz) % 1000000007) % 1000000007; n         ans = (ans + (long long)k * p) % 1000000007; n         p = p * 2 % 1000000007; n       } n     } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T min(T a, T b, T c) { n   return min(a, min(b, c)); n } n template <class T> n T min(T a, T b, T c, T d) { n   return min(a, min(b, min(c, d))); n } n template <class T> n T max(T a, T b, T c) { n   return max(a, max(b, c)); n } n template <class T> n T max(T a, T b, T c, T d) { n   return max(a, max(b, max(c, d))); n } n bool cmp(const pair<int, int>& a, const pair<int, int>& b) { n   return (a.first > b.first || (a.first == b.first && a.second >= b.second)); n } n long long GCD(long long a, long long b) { return (a % b) ? GCD(b, a % b) : b; } n int f[7][100007]; n int main() { n   int n, a, b, c; n   scanf( %d%d%d%d , &n, &a, &b, &c); n   --n; n   f[0][0] = 0; n   f[1][0] = f[2][0] = 1000000000LL; n   for (int i = (1); i <= (n); ++i) { n     f[0][i] = min(f[2][i - 1] + b, f[1][i - 1] + a); n     f[1][i] = min(f[2][i - 1] + c, f[0][i - 1] + a); n     f[2][i] = min(f[1][i - 1] + c, f[0][i - 1] + b); n   } n   cout << min(f[0][n], f[1][n], f[2][n]) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL); n   long long tests = 1; n   cin >> tests; n   while (tests--) { n     long long n, k; n     cin >> n >> k; n     map<pair<long long, long long>, long long> timers; n     set<pair<long long, long long> > bombsx, bombsy; n     for (long long i = 1; i <= n; ++i) { n       long long x, y, t; n       cin >> x >> y >> t; n       bombsx.insert({x, y}); n       bombsy.insert({y, x}); n       timers[{x, y}] = t; n     } n     vector<long long> min_time; n     queue<pair<long long, long long> > nodes; n     while (bombsx.size()) { n       auto itr = bombsx.begin(); n       long long min_timer = timers[{itr->first, itr->second}]; n       nodes.push({itr->first, itr->second}); n       bombsx.erase({itr->first, itr->second}); n       bombsy.erase({itr->second, itr->first}); n       while (nodes.size()) { n         long long x = nodes.front().first, y = nodes.front().second; n         nodes.pop(); n         itr = bombsx.lower_bound({x, y - k}); n         while (itr != bombsx.end() and itr->first == x and n                abs(itr->second - y) <= k) { n           min_timer = min(min_timer, timers[{itr->first, itr->second}]); n           auto aux = itr; n           itr++; n           nodes.push({aux->first, aux->second}); n           bombsx.erase({aux->first, aux->second}); n           bombsy.erase({aux->second, aux->first}); n         } n         itr = bombsy.lower_bound({y, x - k}); n         while (itr != bombsy.end() and itr->first == y and n                abs(itr->second - x) <= k) { n           min_timer = min(min_timer, timers[{itr->second, itr->first}]); n           auto aux = itr; n           itr++; n           nodes.push({aux->second, aux->first}); n           bombsy.erase({aux->first, aux->second}); n           bombsx.erase({aux->second, aux->first}); n         } n       } n       min_time.push_back(min_timer); n     } n     sort(min_time.begin(), min_time.end()); n     long long ans = min_time.size() - 1; n     n = min_time.size(); n     for (long long i = 0; i < n; ++i) { n       ans = min(ans, max(min_time[i], n - 2ll - i)); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n const double PI = acos(-1.0); n const long long int mod = 1e9 + 7; n const int MAXN = 1e6 + 5; n void cp() { n   int n, k; n   string s; n   cin >> n >> k >> s; n   vector<pair<int, int> > group; n   int l = 0, r = 0; n   while (l < n) { n     while (l < n && s[l] ==  L ) l++; n     if (l >= n) break; n     r = l; n     while (r + 1 < n && s[r + 1] ==  W ) r++; n     group.push_back({l, r}); n     l = r + 1; n   } n   vector<pair<int, int> > diff; n   for (int i = 0; i < ((int)group.size()) - 1; i++) { n     int d = group[i + 1].first - group[i].second - 1; n     diff.push_back({d, i}); n   } n   sort(diff.rbegin(), diff.rend()); n   while (k > 0 && !diff.empty()) { n     pair<int, int> last = diff.back(); n     diff.pop_back(); n     int can = min(last.first, k); n     k -= can; n     for (int i = group[last.second].second + 1; n          i < group[last.second + 1].first && can > 0; i++, can--) n       s[i] =  W ; n   } n   if (k > 0) { n     int pos = -2; n     for (int i = 0; i < n; i++) n       if (s[i] ==  W ) pos = i; n     pos++; n     if (pos >= 0 && pos < n) n       for (int i = pos; i < n && k > 0; i++) n         if (s[i] ==  L ) s[i] =  W , k--; n   } n   if (k > 0 && s[0] ==  L ) { n     int pos = n; n     for (int i = 0; i < n && pos == n; i++) n       if (s[i] ==  W ) pos = i; n     pos--; n     if (pos >= 0) { n       for (int i = pos; i >= 0 && k > 0; i--) n         if (s[i] ==  L ) s[i] =  W , k--; n     } n   } n   int score = 0; n   for (int i = 0; i < n; i++) { n     score += (s[i] ==  W ); n     if (i - 1 >= 0 && s[i] ==  W  && s[i - 1] ==  W ) score++; n   } n   cout << score <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int t; n   t = 1; n   cin >> t; n   while (t--) { n     cp(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n const int inf = 1e9 + 9; n const long long int MOD = 1e9 + 696969; n const long long INF = 4LL * 1000000LL * 1000000LL * 1000000LL; n template <class T> n int size(T &&x) { n   return int(x.size()); n } n template <class A, class B> n ostream &operator<<(ostream &out, const pair<A, B> &p) { n   return out <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <class T> n auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { n   out <<  { ; n   for (auto it = x.begin(); it != x.end(); ++it) n     out << *it << (it == prev(x.end()) ?    :  ,  ); n   return out <<  } ; n } n void dump() {} n template <class T, class... Args> n void dump(T &&x, Args... args) { n   cerr << x <<  ;   ; n   dump(args...); n } n mt19937_64 rng(0); n int random(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } n inline void addmod(int &x, int val) { n   x += val; n   while (x >= mod) x -= mod; n } n const int maxn = 300100; n int dr[maxn]; n int n, m, tab[maxn]; n int N; n int temp[maxn]; n inline void add(int p, int val) { n   for (; p <= n; p += p & (-p)) dr[p] += val; n } n inline int get(int p) { n   if (p == 0) return 0; n   int res = 0; n   for (; p > 0; p -= p & (-p)) res += dr[p]; n   return res; n } n map<long long, long long> M; n vector<pair<int, int> > skal; n int cnt = 0; n long long inversions(int base) { n   if (M.find(base) != M.end()) return M[base]; n   skal.resize(n); n   ++cnt; n   for (int i = (1); i <= (n); ++i) skal[i - 1] = make_pair(tab[i] ^ base, i); n   sort(skal.begin(), skal.end()); n   for (int i = 0; i < n; ++i) temp[skal[i].second] = i + 1; n   long long res = 0; n   fill(dr, dr + n + 1, 0); n   for (int i = n; i > 0; --i) { n     res += get(temp[i]); n     add(temp[i], 1); n   } n   M[base] = res; n   return res; n } n void solveone() { n   cin >> n; n   for (int i = (1); i <= (n); ++i) { n     cin >> tab[i]; n   } n   int b = 0; n   for (int bit = 29; bit >= 0; --bit) { n     if (inversions(b) > inversions(b + (1 << bit))) b += (1 << bit); n   } n   cout << inversions(b) <<     << b <<   n ; n } n int main() { n   { n     ios_base::sync_with_stdio(false); n     cin.tie(0); n     cout.tie(0); n   }; n   int tests; n   tests = 1; n   for (int _ = (1); _ <= (tests); ++_) solveone(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, k, mp; n   cin >> n >> m >> k; n   mp = n / 2 + 1; n   if (n % 2 == 0 || mp > m) { n     cout << 0; n     return 0; n   } n   int ans = 0, i, a[10005]; n   for (i = 0; i < n; i++) cin >> a[i]; n   int min = a[0]; n   for (i = 0; i < n; i++) n     if (i % 2 == 0) n       if (a[i] < min) min = a[i]; n   for (i = 0; i < k; i++) { n     int num = m / mp; n     if (min == 0) break; n     if (num <= min) { n       min -= num; n       ans += num; n     } else { n       ans += min; n       min = 0; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool is_Palindrome(string s) { n   long long int n = s.size(); n   for (long long int i = 0; i < n / 2; i++) { n     if (s[i] != s[n - i - 1]) { n       return 0; n     } n   } n   return 1; n } n void solve() { n   string a; n   cin >> a; n   long long int n = a.size(); n   if (is_Palindrome(a)) { n     cout << 0 << endl; n     return; n   } n   cout << 3 << endl; n   cout <<  L  n        <<     << 2 << endl n        <<  R  n        <<     << 2 << endl n        <<  R  n        <<     << (2 * n - 1); n } n void testcase() { n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int a, b; n   cin >> a >> b; n   long long int c = 0; n   if (a % b == 0) cout << 0 << endl; n   if (a % b != 0) { n     long long int ans = b - (a % b); n     cout << ans << endl; n   } n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1000000007; n void solve() { n   string s; n   cin >> s; n   long long ans = INT_MAX; n   for (long long i = 0; i <= 9; i++) { n     long long count = 0; n     for (long long j = 0; j < (long long)s.size(); j++) { n       if (s[j] == (char)( 0  + i)) count++; n     } n     ans = min(ans, (long long)s.size() - count); n   } n   if (ans == 0) { n     cout << 0 << endl; n     return; n   } n   for (long long i = 0; i <= 9; i++) { n     for (long long j = 0; j <= 9; j++) { n       string temp; n       if (i == j) continue; n       temp += (char)( 0  + i); n       temp += (char)( 0  + j); n       long long count = 0; n       long long n = (long long)s.size(); n       long long curr = 0; n       for (long long k = 0; k < n; k++) { n         if (s[k] == temp[curr]) { n           if (curr == 1) { n             count++; n           } n           curr = (curr + 1) % 2; n         } n       } n       ans = min(ans, (long long)s.size() - 2 * count); n     } n   } n   cout << ans << endl; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX_N = (1e3) + 3; n int a[MX_N][MX_N]; n int main() { n   int n; n   scanf( %d , &n); n   if (n == 4) n     puts( -1 ); n   else { n     for (int i = 0; i < n; ++i) { n       for (int j = i - 2; j >= 0; j -= 2) a[i][j] = 1; n       for (int j = i + 1; j < n; j += 2) a[i][j] = 1; n     } n     if (!(n & 1)) { n       a[0][n - 1] = 0; n       a[n - 1][0] = 1; n       a[0][2] = 1; n       a[2][0] = 0; n     } n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < n; ++j) { n         if (j) printf(   ); n         printf( %d , a[i][j]); n       } n       printf(  n ); n     } n   } n   return 0; n } n


#include <iostream> n #include <string> n #include <vector> n #include <algorithm> n #include <queue> n #include <deque> n #include <bitset> n #include <iterator> n #include <list> n #include <stack> n #include <map> n #include <set> n #include <math.h> n #include <stdio.h> n #include <string.h> n #include <stdlib.h> n #include <climits> n #include <iterator> n using namespace std; n   n typedef long long ll; n const ll mod=1e9+7; n const ll N=2e5+1; n const ll INF=1e10;  n  n vector<vector<int>> adj(N); n vector<bool> visited(N); n vector<int> parent(N),root(N); n pair<int,int> backedge; n  n void dfs(int cur){ n  visited[cur]=true; n  for(int next:adj[cur]){ n   if(!visited[next]){ n    parent[next]=cur; n    dfs(next); n   } n   else if(parent[cur]!=next) backedge={cur,next}; n  } n } n  n void solve(){ n  map<int,int> subtree; n  int n; n  cin>>n; n  for(int i=0;i<n;i++){ n   int u,v; n   cin>>u>>v; n   adj[u].push_back(v); n   adj[v].push_back(u); n  } n  dfs(1); n  //cout<<backedge.first<<   <<backedge.second<<  n ;/* n  for(int i=0;i<adj[backedge.first].size();i++){ n   if(adj[backedge.first][i]==backedge.second){ n    adj[backedge.first][i]=adj[backedge.first].back(); n    adj[backedge.first].pop_back(); n    //cout<< done ; n    break; n   } n  } n  for(int i=0;i<adj[backedge.second].size();i++){ n   if(adj[backedge.second][i]==backedge.first){ n    adj[backedge.second][i]=adj[backedge.second].back(); n    adj[backedge.second].pop_back(); n    //cout<< done ; n    break; n   } n  } n  //cout<< done ;/* n  for(int i=1;i<=n;i++) parent[i]=-1,visited[i]=false; n  parent[backedge.first]=0; n  dfs(backedge.first); n  for(int i=1;i<=n;i++) visited[i]=false; n  queue<int> q; n  q.push(backedge.second); n  root[backedge.second]=backedge.second; n  visited[q.back()]=true; n  while(parent[q.back()]){ n   //cout<<q.back()<<   ; n   visited[parent[q.back()]]=true; n   q.push(parent[q.back()]); n   root[q.back()]=q.back(); n  } n  visited[backedge.first]=true; n  root[backedge.first]=backedge.first; n  ll c=q.size(); n  while(!q.empty()){ n   int t=q.front(); n   q.pop(); n   for(int i:adj[t]){ n    if(!visited[i]){ n     root[i]=root[t]; n     subtree[root[i]]++; n     visited[i]=true; n     q.push(i); n    } n   } n  } n  //for(auto i:subtree) cout<<i.first<<   <<i.second<<  n ; n  ll ans1=0,ans2=0,sum=0; n  for(auto i:subtree){ n   ans1+=(ll)i.second*(i.second+1)/2; n   ans2+=i.second*sum; n   sum+=i.second; n  } n  ans1+=2*ans2+2*(c-1)*(n-c)+c*(c-1); n  cout<<ans1<<  n ; n  for(int i=1;i<=n;i++){ n   adj[i].clear(); n   visited[i]=false; n   parent[i]=0; n  } n } n  n int main(){ n  ios_base::sync_with_stdio(0); n  cin.tie(NULL); n  int t; n  cin>>t; n  while(t--) solve(); n }


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


#include <bits/stdc++.h> n using namespace std; n int n, m, l, r, cnt; n long long ans; n char s[101][10005]; n int dfs(int x, int y, int d) { n   if (x == n - 1) return 1; n   if (s[x + 1][y] ==  . ) { n     l = y; n     r = y; n     ans++; n     cnt = 0; n     return dfs(x + 1, y, d); n   } n   if (s[x][y + d] ==  . ) { n     ans++; n     r = max(r, y + d); n     l = min(l, y + d); n     return dfs(x, y + d, d); n   } else if (s[x][y + d] ==  # ) { n     if (cnt > 2 * m) return 0; n     ans += 1; n     cnt++; n     if (d == 1) { n       ans += y - l; n       r = y; n       return dfs(x, l, -1); n     } else { n       ans += r - y; n       l = y; n       return dfs(x, r, 1); n     } n   } else { n     s[x][y + d] =  . ; n     ans++; n     if (d == 1) { n       ans += y - l; n       r = y + 1; n       return dfs(x, l, -1); n     } else { n       ans += r - y; n       l = y - 1; n       return dfs(x, r, 1); n     } n   } n } n int main() { n   int i, j, k, t; n   while (~scanf( %d%d , &n, &m)) { n     m++; n     for (i = 0; i < n; i++) scanf( %s , s[i] + 1), s[i][0] = s[i][m] =  # ; n     l = r = 1; n     ans = 0; n     j = dfs(0, 1, 1); n     if (j) n       printf( %I64d n , ans); n     else n       puts( Never ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int arr[3]; n   cin >> arr[0] >> arr[1] >> arr[2]; n   sort(arr, arr + 3); n   int d = arr[1] - arr[0]; n   d += arr[2] - arr[1]; n   cout << d; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int in() { n   int res = 0; n   char c; n   int f = 1; n   while ((c = getchar()) <  0  || c >  9 ) n     if (c ==  - ) f = -1; n   while (c >=  0  && c <=  9 ) res = res * 10 + c -  0 , c = getchar(); n   return res * f; n } n const int N = 100010; n long long num[N], rd[N]; n long long ans; n pair<int, int> a[N << 1]; n int main() { n   int n = in(), m = in(); n   for (int i = 1; i <= n; i++) { n     num[i] = 1; n   } n   for (int i = 0; i < m; i++) { n     a[i].first = in(); n     a[i].second = in(); n   } n   sort(a, a + m); n   for (int i = 0; i < m; i++) { n     int x = a[i].first, y = a[i].second; n     if (x > y) { n       num[x] = max(num[x], num[y] + 1); n     } else { n       num[y] = max(num[y], num[x] + 1); n     } n     rd[x]++; n     rd[y]++; n   } n   for (int i = 1; i <= n; i++) { n     ans = max(ans, 1LL * num[i] * rd[i]); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long Cross(int x1, int y1, int x2, int y2) { n   return 1ll * x1 * y2 - 1ll * x2 * y1; n } n int main() { n   int xa, xb, xc, ya, yb, yc; n   scanf( %d%d%d%d%d%d , &xa, &ya, &xb, &yb, &xc, &yc); n   long long cross = Cross(xb - xa, yb - ya, xc - xa, yc - ya); n   if (cross == 0) n     printf( TOWARDS n ); n   else if (cross > 0) n     printf( LEFT n ); n   else n     printf( RIGHT n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool SR(int &_x) { return scanf( %d , &_x) == 1; } n bool SR(long long &_x) { return scanf( %lld , &_x) == 1; } n bool SR(double &_x) { return scanf( %lf , &_x) == 1; } n bool SR(char *_s) { return scanf( %s , _s) == 1; } n bool RI() { return true; } n template <typename I, typename... T> n bool RI(I &_x, T &..._tail) { n   return SR(_x) && RI(_tail...); n } n void SP(const int _x) { printf( %d , _x); } n void SP(const long long _x) { printf( %lld , _x); } n void SP(const double _x) { printf( %.16lf , _x); } n void SP(const char *s) { printf( %s , s); } n void PL() { puts(  ); } n template <typename I, typename... T> n void PL(const I _x, const T... _tail) { n   SP(_x); n   if (sizeof...(_tail)) putchar(   ); n   PL(_tail...); n } n const int maxn = 5e5 + 5; n int n, x[maxn], y[maxn]; n void read() { n   RI(n); n   for (int i = (1); i <= int(n); i++) { n     int a, b; n     RI(x[i], y[i], a, b); n   } n } n int c[maxn]; n void build() { n   for (int i = (1); i <= int(n); i++) { n     c[i] = 1 + (x[i] & 1) * 2 + (y[i] & 1); n   } n } n void sol() { n   PL( YES ); n   for (int i = (1); i <= int(n); i++) PL(c[i]); n } n int main() { n   read(); n   build(); n   sol(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> V; n vector<int> E[200005]; n int d[200005]; n int p[200005]; n int find(int x) { n   while (p[x] != x) p[x] = p[p[x]], x = p[x]; n   return x; n } n int rnk[200005]; n void meld(int x, int y) { n   x = find(x), y = find(y); n   if (rnk[x] > rnk[y]) swap(x, y); n   p[x] = y; n   rnk[y] = rnk[x] == rnk[y] ? rnk[y] + 1 : rnk[y]; n } n int s, t; n void build() { n   fill(begin(d), end(d), 1e9); n   queue<int> Q; n   for (auto x : V) Q.emplace(x), d[x] = 0; n   while (!Q.empty()) { n     auto x = Q.front(); n     Q.pop(); n     for (auto y : E[x]) { n       if (d[y] == 1e9) d[y] = d[x] + 1, Q.emplace(y); n     } n   } n } n int n, m, k; n bool test(int mid) { n   iota(begin(p), end(p), 0); n   fill(begin(rnk), end(rnk), 0); n   for (int x = 1; x <= n; ++x) { n     if (d[x] + 1 > mid) continue; n     for (auto y : E[x]) n       if (d[x] + d[y] + 1 <= mid) meld(x, y); n   } n   return find(s) == find(t); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m >> k; n   for (int i = 1; i <= k; ++i) { n     int x; n     cin >> x; n     V.push_back(x); n   } n   for (int i = 1; i <= m; ++i) { n     int u, v; n     cin >> u >> v; n     E[u].push_back(v); n     E[v].push_back(u); n   } n   cin >> s >> t; n   if (find(begin(V), end(V), t) == end(V)) V.push_back(t); n   build(); n   int l = 1, r = n; n   while (l < r) { n     int mid = (l + r) / 2; n     if (test(mid)) n       r = mid; n     else n       l = mid + 1; n   } n   if (test(l)) n     cout << l << endl; n   else n     cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void R(int &x) { n   char ch = getchar(); n   x = 0; n   for (; ch <  0 ; ch = getchar()) n     ; n   for (; ch >=  0 ; ch = getchar()) x = x * 10 + ch -  0 ; n } n const int N = 100005; n double x, y, ans; n int n; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %lf%lf , &x, &y); n     ans += y; n   } n   printf( %.3lf n , ans / n + 5); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int k; n   cin >> k; n   vector<long long> a(k); n   for (int h = 0; h < a.size(); h++) { n     cin >> a[h]; n   }; n   int gg = max_element(a.begin(), a.end()) - a.begin(); n   for (int i = k - 1; i > gg; i--) { n     if (a[i] > a[i - 1]) { n       cout <<  no ; n       return 0; n     } n   } n   for (int i = 0; i < gg; i++) { n     if (a[i] > a[i + 1]) { n       cout <<  no ; n       return 0; n     } n   } n   cout <<  YES ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x1, y1, x2, y2; n   cin >> x1 >> y1 >> x2 >> y2; n   cout << max(abs(y2 - y1), abs(x2 - x1)); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, k, i; n   double t, z, a, b, d = 0; n   scanf( %d %d , &n, &k); n   int x[n], y[n]; n   for (i = 0; i < n; i++) { n     scanf( %d %d , &x[i], &y[i]); n   } n   for (i = 1; i < n; i++) { n     a = x[i - 1] - x[i]; n     b = y[i - 1] - y[i]; n     a = pow(a, 2); n     b = pow(b, 2); n     z = a + b; n     z = sqrt(z); n     d = d + z; n   } n   t = (d / 50) * k; n   printf( %.9lf , t); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   srand(time(0)); n   cout << fixed << setprecision(7); n   cerr << fixed << setprecision(7); n   int n, q; n   scanf( %d%d , &n, &q); n   static long long s[1 << 23]; n   for (int i = 1; i <= n; ++i) n     scanf( %I64d , &s[i]), s[i + n] = s[i + n + n] = s[i]; n   for (int i = 2; i <= n + n + n; ++i) s[i] += s[i - 1]; n   static int nxt[1 << 23]; n   for (int i = 1; i <= n; ++i) nxt[i] = i; n   for (int i = n + 1; i <= n + n + n; ++i) nxt[i] = nxt[i - n]; n   while (q--) { n     int v; n     scanf( %d , &v); n     if (s[n] <= v) { n       cout <<  1 n ; n       continue; n     } n     static int a[1 << 22]; n     int cnt = 0; n     for (int i = 1; i <= n + n; ++i) { n       while (s[cnt] - s[i - 1] <= v) ++cnt; n       a[i] = cnt; n     } n     int ans = 0; n     cnt = 1; n     for (int i = 1; i <= n; ++i) cnt = nxt[a[cnt]]; n     const int lim = cnt + n; n     while (cnt < lim) ++ans, cnt = a[cnt]; n     cout << ans <<   n ; n   } n   cerr <<  Time elapsed :  << clock() * 1000.0 / CLOCKS_PER_SEC <<   ms  n        <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, h; n double p; n double memexpected[2001][2001][2][2]; n int calculado[2001][2001][2][2]; n int ri[2001], le[2001]; n int v[2001]; n double expected(int i1, int i2, int l1, int l2) { n   if (i1 > i2) return 0; n   double &mem = memexpected[i1][i2][l1][l2]; n   if (calculado[i1][i2][l1][l2]) return mem; n   calculado[i1][i2][l1][l2] = 1; n   mem = 0; n   int distle = h; n   if (i1 > 0) n     distle = n         (l1 == 0) ? min(v[i1] - v[i1 - 1], h) : min(v[i1] - (v[i1 - 1] + h), h); n   int distri = h; n   if (i2 < n - 1) n     distri = n         (l2 == 1) ? min(v[i2 + 1] - v[i2], h) : min(v[i2 + 1] - h - v[i2], h); n   int RI = min(ri[i1], i2); n   int lenri = min(v[RI] + h - v[i1], v[i2] - v[i1] + distri); n   int LE = max(le[i2], i1); n   int lenle = min(v[i2] - v[LE] + h, v[i2] - v[i1] + distle); n   double mem1 = 0.5 * (p * (distle + expected(i1 + 1, i2, 0, l2)) + n                        (1 - p) * (lenri + expected(RI + 1, i2, 1, l2))); n   double mem2 = 0.5 * ((1 - p) * (distri + expected(i1, i2 - 1, l1, 1)) + n                        p * (lenle + expected(i1, LE - 1, l1, 0))); n   mem = mem1 + mem2; n   return mem; n } n int main() { n   cout.setf(ios::fixed); n   cout.precision(10); n   cin >> n >> h >> p; n   for (int i = 0; i < n; i++) cin >> v[i]; n   sort(v, v + n); n   le[0] = 0; n   for (int i = 1; i < n; i++) le[i] = (v[i] - v[i - 1] < h) ? le[i - 1] : i; n   ri[n - 1] = n - 1; n   for (int i = n - 2; i >= 0; i--) n     ri[i] = (v[i + 1] - v[i] < h) ? ri[i + 1] : i; n   cout << expected(0, n - 1, 0, 1) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1e9 + 7; n const int inf = 1e9; n const int N = 2e5; n void solve() { n   long long int d, m; n   cin >> d >> m; n   long long int mx; n   for (long long int i = 32; i >= 0; i--) { n     if ((1LL << i) & d) { n       mx = i; n       break; n     } n   } n   long long int dp[mx + 1]; n   dp[mx] = (d - (1LL << mx) + 1); n   long long int res = dp[mx]; n   res %= m; n   for (long long int i = mx - 1; i >= 0; i--) { n     dp[i] = (1LL << i) * (1 + res); n     res += dp[i]; n     res %= m; n   } n   cout << res <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> m; n pair<bool, char> check(int a, string s) { n   int flag = 0; n   char x = s[a], e; n   for (long long int i = 1 + (x -  a ); i < 26; i++) { n     if (m.count(i)) { n       return {true, char(i +  a )}; n     } n   } n   return {false,  f }; n } n int main() { n   ios_base ::sync_with_stdio(false), cin.tie(NULL), cout.tie(0); n   int a, b; n   cin >> a >> b; n   string s; n   cin >> s; n   for (long long int i = 0; i < s.size(); i++) { n     m[s[i] -  a ]++; n   } n   if (a < b) { n     cout << s; n     sort(s.begin(), s.end()); n     for (long long int i = 0; i < b - a; i++) cout << s[0]; n   } else { n     int k = 0; n     char e; n     for (long long int i = b - 1; i >= 0; i--) { n       pair<bool, char> p = check(i, s); n       if (p.first) { n         e = p.second; n         k = i; n         break; n       } n     } n     for (long long int i = 0; i < k; i++) cout << s[i]; n     cout << e; n     sort(s.begin(), s.end()); n     if (b - k - 1 > 0) { n       for (long long int i = 0; i < b - k - 1; i++) cout << s[0]; n     } n   } n   return 0; n } n