#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   t = 1; n   for (int r = 0; r < t; r++) { n     long long n, m, i, x, y, z, sum = 0; n     cin >> n >> m; n     vector<string> v(51); n     char a =  a , b =  A ; n     for (i = 0; i <= 50; i++) { n       if (b >  Z ) b =  A ; n       v[i].push_back(b); n       if (i >= 26) { n         v[i].push_back(a); n         a++; n       } n       b++; n     } n     x = 0, y = 0; n     vector<string> ans; n     string temp; n     while (y < m - 1) { n       ans.push_back(v[y]); n       y++; n     } n     x = y; n     for (i = 0; i < (n - m + 1); i++, x++) { n       cin >> temp; n       if (temp ==  NO ) { n         ans.push_back(ans[x - (m - 1)]); n       } else { n         ans.push_back(v[y]); n         y++; n       } n     } n     for (i = 0; i < n; i++) cout << ans[i] <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   int i, j, k; n   int n, m, idx; n   string s, s2; n   cin >> s; n   n = s.length(); n   int cnt = 0; n   for (i = 0; i < n; i++) { n     if (s[i] ==  a ) cnt++; n   } n   k = 0; n   idx = 0; n   for (i = 0; i < n; i++) { n     if (k == cnt) break; n     if (s[i] ==  a ) n       k++, idx = i + 1; n     else n       s2.push_back(s[i]); n   } n   k = n - idx; n   k -= s2.length(); n   if (k % 2 == 0) { n     k /= 2; n     j = idx; n     idx += k; n     while (k > 0) { n       s2.push_back(s[j]); n       j++; n       k--; n     } n     k = s.length() - 1; n     for (i = s2.length() - 1; i >= 0; i--) { n       if (s2[i] != s[k]) { n         cout <<  :( ; n         return 0; n       } n       k--; n     } n     for (i = 0; i < idx; i++) cout << s[i]; n   } else n     cout <<  :( ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int p1[400010], p2[400010], p3[400010], p0[400010]; n int cnt1, cnt2, cnt3, cnt0; n inline bool cmp(const int a, const int b) { return a > b; } n int main() { n   int n; n   cin >> n; n   for (int i = 1, a, b, va; i <= n; i++) { n     scanf( %1d%1d %d , &a, &b, &va); n     if (a + b == 0) n       p0[++cnt0] = va; n     else if (a + b == 2) n       p3[++cnt3] = va; n     else if (a == 1) n       p1[++cnt1] = va; n     else n       p2[++cnt2] = va; n   } n   sort(p1 + 1, p1 + 1 + cnt1, cmp); n   sort(p2 + 1, p2 + 1 + cnt2, cmp); n   long long ans = 0; n   int fl = 0; n   if (cnt2 < cnt1) swap(cnt2, cnt1), fl = 1; n   for (int i = 1; i <= cnt1; i++) { n     ans += p2[i] + p1[i]; n   } n   if (fl) n     for (int i = cnt1 + 1; i <= cnt2; i++) p0[++cnt0] = p1[i]; n   else n     for (int i = cnt1 + 1; i <= cnt2; i++) p0[++cnt0] = p2[i]; n   sort(p0 + 1, p0 + 1 + cnt0, cmp); n   for (int i = 1; i <= cnt3; i++) { n     ans += p0[i] + p3[i]; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, y, flag = 0, count, z, x; n   cin >> y; n   if (y % 400 == 0 || y % 4 == 0 && y % 100 != 0) { n     x = 1; n   } else { n     x = 2; n   } n   z = y + 1; n   if (x == 1) n     count = 3; n   else n     count = 2; n   while (flag != 1) { n     if (count == 1) { n       if (z % 400 == 0 || z % 4 == 0 && z % 100 != 0) { n         a = 1; n       } else n         a = 2; n       if (a == x) { n         flag = 1; n         break; n       } n     } n     if (z % 400 == 0 || z % 4 == 0 && z % 100 != 0) { n       if (count == 7) { n         count = 2; n       } else if (count == 6) n         count = 1; n       else n         count += 2; n     } else { n       if (count == 7) n         count = 1; n       else n         count += 1; n     } n     z++; n   } n   cout << z; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n = 0, m = 0, k; n vector<string> a; n int CountSpiders(int j) { n   int sum = 0; n   int jR = j, jL = j; n   for (int i = 1; i < n; ++i) { n     if (!(i % 2) && a[i][j] ==  U ) ++sum; n     if (jR > 0) { n       --jR; n       if (a[i][jR] ==  R ) ++sum; n     } n     if (jL < m - 1) { n       ++jL; n       if (a[i][jL] ==  L ) ++sum; n     } n   } n   return sum; n } n int main() { n   cin >> n >> m >> k; n   cin.ignore(100,   n ); n   a.resize(n); n   for (int i = 0; i < n; ++i) getline(cin, a[i]); n   for (int j = 0; j < m; ++j) { n     cout << CountSpiders(j) <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int xx1, xx2, yy1, yy2; n int x, y, r, ans; n char v[30000000]; n int zhuan(int x, int y) { return (x + 1000) * 10000 + y + 1000; } n void swap(int &a, int &b) { n   int h = a; n   a = b; n   b = h; n } n bool dis(int xx, int yy) { n   return (xx - x) * (xx - x) + (yy - y) * (yy - y) <= r * r; n } n void fun(int x1, int y1, int x2, int y2) { n   if (x1 > x2) swap(x1, x2); n   if (y1 > y2) swap(y1, y2); n   for (int i = x1; i <= x2; i++) n     for (int j = y1; j <= y2; j++) { n       int k = zhuan(i, j); n       if (v[k] == 0 && dis(i, j)) { n         v[k] = 1; n         ans++; n       } n     } n } n int main() { n   while (scanf( %d %d %d %d , &xx1, &yy2, &xx2, &yy1) != EOF) { n     if (xx1 > xx2) swap(xx1, xx2); n     if (yy1 > yy2) swap(yy1, yy2); n     int n, i, j, k, w; n     memset(v, 0, sizeof(v)); n     scanf( %d , &n); n     ans = 0; n     for (i = 1; i <= n; i++) { n       scanf( %d %d %d , &x, &y, &r); n       fun(xx1, yy1, xx1, yy2); n       fun(xx1, yy2, xx2, yy2); n       fun(xx2, yy2, xx2, yy1); n       fun(xx2, yy1, xx1, yy1); n     } n     w = (xx2 - xx1 + 1) * 2 + (yy2 - yy1 + 1) * 2 - 4; n     if (xx2 == xx1 && yy1 == yy2) w = 1; n     printf( %d n , w - ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int n = s.length(); n   int ans = 0; n   int num = 0; n   for (int i = 0; i < n; i++) { n     if ((s[i] -  0 ) % 3 == 0) { n       ans++; n       num = 0; n     } else { n       num++; n       if (num == 2) { n         if ((s[i - 1] -  0  + s[i] -  0 ) % 3 == 0) { n           ans++; n           num = 0; n         } n       } n       if (num == 3) { n         ans++; n         num = 0; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long t, n, i, b[1010], ta, tb, k1, a[1010], tem, x[1010], y[1010], k2, tom; n vector<long long> v[1010]; n void dfs(long long aa, long long bb) { n   b[aa] = bb; n   long long ii; n   for (ii = 0; ii < v[aa].size(); ii++) n     if (!b[v[aa][ii]]) dfs(v[aa][ii], bb); n } n int main() { n   cin >> t; n   while (t--) { n     cin >> n; n     for (i = 1; i <= n; i++) v[i].clear(); n     for (i = 1; i <= n; i++) b[i] = 0; n     for (i = 1; i < n; i++) { n       cin >> ta >> tb; n       v[ta].push_back(tb); n       v[tb].push_back(ta); n     } n     for (i = 1; i <= n; i++) b[i] = 0; n     for (i = 1; i <= n; i++) x[i] = 0; n     for (i = 1; i <= n; i++) y[i] = 0; n     cin >> k1; n     for (i = 1; i <= k1; i++) { n       cin >> a[i]; n       b[a[i]] = a[i]; n       x[a[i]] = 1; n     } n     for (i = 1; i <= k1; i++) dfs(a[i], a[i]); n     cin >> k2; n     for (i = 1; i <= k2; i++) { n       cin >> ta; n       y[ta] = 1; n     } n     cout <<  B   << ta << endl; n     cin >> tem; n     if (x[tem]) n       cout <<  C   << tem << endl; n     else { n       cout <<  A   << b[tem] << endl; n       cin >> tom; n       if (y[tom]) n         cout <<  C   << b[tem] << endl; n       else n         cout <<  C   << -1 << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s =  qwertyuiopasdfghjkl;zxcvbnm,./ ; n   char lr; n   cin >> lr; n   string message; n   cin >> message; n   for (int i = 0; i < message.length(); i++) { n     int it = s.find(message[i]); n     if (lr ==  R ) { n       cout << s[it - 1]; n     } else { n       cout << s[it + 1]; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e6 + 3; n long long n, k, x; n pair<long long, long long> ans; n long long powmod(long long a, long long b, long long c) { n   long long ans = 1; n   while (b > 0) { n     if (b & 1) ans = ans * a % c; n     a = a * a % c; n     b /= 2; n   } n   return ans; n } n pair<long long, long long> work(long long n, long long k) { n   if (k == 0) { n     return make_pair(0, 1); n   } n   long long dq = powmod(2, n, mod), now = 1; n   for (int i = 1; i < 2 * mod; i += 2) { n     now = now * (dq - i + 2 * mod) % mod; n   } n   long long ans; n   ans = powmod(now, k / (2 * mod), mod); n   for (int i = 1; i <= (k % (2 * mod)); i += 2) n     ans = ans * (dq - i + 2 * mod) % mod; n   pair<long long, long long> cur = work(n - 1, k / 2); n   ans = ans * cur.second % mod; n   return make_pair((k / 2 + cur.first) % (mod - 1), ans); n } n int main() { n   cin >> n >> k; n   x = 0; n   while ((1LL << x) < k) x++; n   if (n < x) { n     cout <<  1 1  << endl; n     return 0; n   } n   pair<long long, long long> ans = work(n, k - 1); n   long long fz, fm; n   fz = ans.second; n   fm = (n % (mod - 1)) * ((k - 1) % (mod - 1)) % (mod - 1); n   fm = (fm - ans.first + (mod - 1)) % (mod - 1); n   fm = powmod(2, fm, mod); n   fz = (fm - fz + mod) % mod; n   cout << fz <<     << fm << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef pair<int, int> pii; n template <typename T> n inline void read(T &x) { n   int ch = getchar(); n   x = 0; n   bool f = false; n   double d = 1; n   for (; ch !=  -  && (ch <  0  || ch >  9 ); ch = getchar()) n     ; n   if (ch ==  - ) { n     f = true; n     ch = getchar(); n   } n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ; n   if (ch ==  . ) n     for (ch = getchar(); ch >=  0  && ch <=  9 ; ch = getchar()) n       x += (ch -  0 ) * (d *= 0.1); n   if (f) x = -x; n } n template <typename T> n inline bool chkmax(T &a, const T &b) { n   if (a < b) return a = b, 1; n   return 0; n } n template <typename T> n inline bool chkmin(T &a, const T &b) { n   if (a > b) return a = b, 1; n   return 0; n } n const int K = 81, N = 170, M = N << 4, INF = 0x3f3f3f3f; n int n, k, S, T, a[K], c[K], head[N], to[M], nxt[M], cap[M], w[M], tmp[K]; n inline void add(int a, int b, int c, int d) { n   static int cnt = 1; n   to[++cnt] = b; n   nxt[cnt] = head[a]; n   head[a] = cnt; n   cap[cnt] = c; n   w[cnt] = d; n   to[++cnt] = a; n   nxt[cnt] = head[b]; n   head[b] = cnt; n   cap[cnt] = 0; n   w[cnt] = -d; n } n int dis[N], inc[N], q[N], f, r, pre[N], ans; n bool inq[N]; n inline void AMOD(int &x) { n   ++x; n   if (x >= N) x = 0; n } n inline bool spfa() { n   memset(inq, 0, sizeof inq); n   memset(dis, 0x3f, sizeof dis); n   f = r = 0; n   q[r++] = S; n   inc[S] = INF; n   dis[S] = 0; n   inq[S] = true; n   while (f != r) { n     int u = q[f]; n     AMOD(f); n     inq[u] = false; n     for (register int i = head[u]; i; i = nxt[i]) n       if (cap[i] && chkmin(dis[to[i]], dis[u] + w[i])) { n         inc[to[i]] = min(inc[u], cap[i]); n         pre[to[i]] = i; n         if (!inq[to[i]]) { n           inq[to[i]] = true; n           q[r] = to[i]; n           AMOD(r); n         } n       } n   } n   return dis[T] < INF; n } n int main() { n   read(n); n   read(k); n   for (register int i = 1; i <= n; ++i) read(a[i]); n   for (register int i = 1; i <= n; ++i) read(c[i]); n   T = 1 + (S = n << 1 | 1); n   for (register int i = 1; i <= n; ++i) { n     add(S, i, 1, c[a[i]]); n     add(i, i + n, 1, 0); n     add(i + n, T, 1, 0); n     if (i < n) add(i, i + 1, k - 1, 0); n     if (tmp[a[i]]) add(i - 1, tmp[a[i]] + n, 1, -c[a[i]]); n     tmp[a[i]] = i; n   } n   while (spfa()) { n     ans += inc[T] * dis[T]; n     for (register int u = T; u; u = to[pre[u] ^ 1]) { n       cap[pre[u]] -= inc[T]; n       cap[pre[u] ^ 1] += inc[T]; n     } n   } n   printf( %d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n char s[110], ss[110]; n int a[210], ans[210], n, k, namlen; n bool boo; n bool check() { n   int i, j; n   for (i = 1; i <= n; i++) n     if (ss[i] ==  0 ) { n       bool bk = true; n       for (j = 1; j <= namlen; j++) n         if (a[i + j - 1] != s[j]) { n           bk = false; n           break; n         } n       if (bk) return false; n     } n   return true; n } n void search(int x) { n   if (x == n + 1) { n     if (check()) { n       for (int i = 1; i <= n; i++) ans[i] = a[i]; n       boo = true; n     } n     return; n   } n   if (ss[x] ==  1 ) { n     search(x + 1); n     return; n   } n   if (a[x] != 0) { n     search(x + 1); n     return; n   } n   for (int i = 0; i < k; i++) { n     a[x] = i +  a ; n     search(x + 1); n     if (boo) return; n   } n } n int main() { n   int len, i, j; n   scanf( %d%d , &n, &k); n   getchar(); n   gets(s + 1); n   gets(ss + 1); n   namlen = strlen(s + 1); n   len = strlen(ss + 1); n   for (i = 1; i <= namlen; i++) n     if (s[i] -  a  + 1 > k) { n       printf( No solution n ); n       return 0; n     } n   bool bk = true; n   for (i = 1; i <= n; i++) a[0] = 0; n   for (i = 1; i <= len; i++) { n     if (ss[i] ==  1 ) { n       if (i + namlen - 1 > n) { n         bk = false; n         break; n       } n       for (j = 1; j <= namlen; j++) { n         if (a[i + j - 1] != 0 && a[i + j - 1] != s[j]) { n           bk = false; n           break; n         } n         a[i + j - 1] = s[j]; n       } n       if (!bk) break; n     } n   } n   if (!bk) { n     printf( No solution n ); n     return 0; n   } n   boo = false; n   search(1); n   if (!boo) n     printf( No solution n ); n   else { n     for (i = 1; i <= n; i++) printf( %c , char(ans[i])); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   string a; n   cin >> a; n   string res =   ; n   int kolvo = k / 2; n   int i = 0; n   int s = 0; n   int bla = 0; n   while (i < kolvo) { n     res += a[s]; n     if (a[s] ==  ( ) { n       ++i; n     } else { n       ++bla; n     } n     ++s; n   } n   while (bla < kolvo) { n     res +=  ) ; n     ++bla; n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 5, M = 1e18 + 5, OO = 0x3f3f3f3f; n const double pi = 3.141592653589793238462643383279502884; n double n, r; n double calc() { n   double ang = 180 / n; n   ang = (ang / 180) * pi; n   double a = (sin(ang / 2) * r) / sin(pi - (1.5 * ang)); n   double ans = n * a * r * sin(ang); n   return ans; n } n int main() { n   cin >> n >> r; n   printf( %.12lf  n , calc()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3228; n const int MOD = 1000 * 1000 * 1000 + 7; n long long pw(long long a, int first) { n   if (!first) return 1; n   if (first % 2) return a * pw(a * a, first / 2); n   return pw(a * a, first / 2); n } n void solve() { n   int pl[3] = {0}; n   int n, p1, p2; n   cin >> n >> p1 >> p2; n   pl[1] -= p1; n   pl[2] -= p2; n   for (int i = 0; i < n; ++i) { n     int nxt; n     cin >> nxt; n     if (nxt == 2 && pl[2] >= 0) n       ++pl[1]; n     else n       ++pl[nxt]; n   } n   cout << max(0, pl[1]) + max(0, pl[2]); n } n int main() { n   double tt = clock(); n   cout.tie(nullptr); n   cin.tie(nullptr); n   ios_base::sync_with_stdio(false); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n const int N = 300005; n const double PI = 4 * atan(1); n const long long MAX = 9223372036854775807; n int dx[] = {1, -1, 0, 0, 1, -1, 1, -1}; n int dy[] = {0, 0, 1, -1, 1, 1, -1, -1}; n long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); } n long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } n bool issquare(long long w) { return trunc(sqrt(w)) * trunc(sqrt(w)) == w; } n bool isprime(long long u) { n   for (long long i = 2; i <= (int)sqrt(u); i++) { n     if (u % i == 0) return 0; n   } n   return 1; n } n long long mod(long long to_mod, long long MOD) { n   to_mod %= MOD; n   while (to_mod < 0) to_mod += MOD; n   return to_mod % MOD; n } n long long moduloMultiplication(long long a, long long b, long long mod) { n   long long res = 0; n   a %= mod; n   while (b) { n     if (b & 1) res = (res + a) % mod; n     a = (2 * a) % mod; n     b >>= 1; n   } n   return res; n } n long long power(long long x, long long y) { n   long long res = 1; n   x = x; n   while (y > 0) { n     if (y & 1) res = (res * x); n     y = y >> 1; n     x = (x * x); n   } n   return res; n } n long long n, m, x, y, res, t, k, ans = 0, q, z, w, l, a, b; n vector<pair<long long, pair<long long, long long> > > v; n long long dp[N]; n vector<long long> vv; n long long curr[N]; n int main() { n   ios::sync_with_stdio(0); n   memset(dp, 0, sizeof(dp)); n   memset(curr, 0, sizeof(curr)); n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     cin >> x >> y >> z; n     x--; n     y--; n     v.push_back(make_pair(z, make_pair(x, y))); n   } n   sort(v.begin(), v.end()); n   ans = 1; n   for (int i = 0; i < m;) { n     long long j = i; n     vv.clear(); n     while (j < m && v[j].first == v[i].first) { n       curr[v[j].second.second] = n           max(curr[v[j].second.second], 1 + dp[v[j].second.first]); n       vv.push_back(v[j].second.second); n       j++; n     } n     i = j; n     for (auto t : vv) { n       dp[t] = max(dp[t], curr[t]); n     } n   } n   for (int i = 0; i < n; i++) { n     ans = max(ans, dp[i]); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int res = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -f; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     res = (res << 3) + (res << 1) + ch -  0 ; n     ch = getchar(); n   } n   return res * f; n } n namespace qiqi { n const int N = 6e5 + 5; n int n, m, siz[N], si[N], fa[N], ch[N][2], rev[N], cnt, mx[N]; n bool del[N]; n struct Edge { n   int u, v, w, id; n   inline bool friend operator<(Edge a, Edge b) { return a.w < b.w; } n } e[N]; n priority_queue<Edge> q; n inline bool isroot(int x) { return ch[fa[x]][0] != x && ch[fa[x]][1] != x; } n inline void push_up(int x) { n   siz[x] = (x <= n) + si[x] + siz[ch[x][0]] + siz[ch[x][1]]; n   mx[x] = x > n ? x - n : 0; n   for (int i = (0); i <= (1); ++i) n     if (e[mx[ch[x][i]]].w > e[mx[x]].w) mx[x] = mx[ch[x][i]]; n } n inline void push_down(int x, bool k) { n   if (k && !isroot(x)) push_down(fa[x], k); n   if (rev[x]) { n     swap(ch[x][0], ch[x][1]); n     rev[ch[x][0]] ^= 1; n     rev[ch[x][1]] ^= 1; n     rev[x] = 0; n   } n } n inline void rotate(int x) { n   int y = fa[x], z = fa[y], l = x == ch[y][1]; n   if (!isroot(y)) ch[z][y == ch[z][1]] = x; n   fa[x] = z; n   push_up(fa[ch[y][l] = ch[x][l ^ 1]] = y); n   push_up(fa[ch[x][l ^ 1] = y] = x); n } n inline void splay(int x) { n   push_down(x, 1); n   while (!isroot(x)) { n     int y = fa[x]; n     if (!isroot(y)) rotate(x ^ y ^ ch[y][0] ^ ch[fa[y]][0] ? x : y); n     rotate(x); n   } n   push_up(x); n } n inline void access(int x) { n   for (int i = 0; x; x = fa[i = x]) { n     splay(x); n     si[x] += siz[ch[x][1]]; n     si[x] -= siz[ch[x][1] = i]; n     push_up(x); n   } n } n inline void makeroot(int x) { n   access(x); n   splay(x); n   rev[x] ^= 1; n } n inline void split(int x, int y) { n   makeroot(x); n   access(y); n   splay(y); n } n inline int find(int x) { n   access(x); n   splay(x); n   while (ch[x][rev[x]]) x = ch[x][rev[x]]; n   splay(x); n   return x; n } n inline int qry(int x, int y) { n   split(x, y); n   return mx[y]; n } n inline void cut(int x, int y) { n   split(x, y); n   cnt -= siz[y] & 1; n   ch[y][0] = fa[x] = 0; n   push_up(y); n   cnt += (siz[x] & 1) + (siz[y] & 1); n } n inline void link(int x, int y) { n   split(x, y); n   cnt -= (siz[x] & 1) + (siz[y] & 1); n   si[fa[x] = y] += siz[x]; n   push_up(y); n   cnt += siz[y] & 1; n } n inline int upd(int x, int y, int id) { n   bool flg = 1; n   if (find(x) == find(y)) { n     int k = qry(x, y); n     if (e[k].w > e[id].w) { n       del[k] = 1; n       cut(e[k].u, k + n); n       cut(e[k].v, k + n); n     } else n       flg = 0; n   } n   if (flg) { n     link(x, id + n); n     link(y, id + n); n   } n   if (cnt) return -1; n   while (1) { n     Edge e = q.top(); n     q.pop(); n     if (del[e.id]) continue; n     cut(e.u, e.id + n); n     cut(e.v, e.id + n); n     if (cnt) { n       q.push(e); n       link(e.u, e.id + n); n       link(e.v, e.id + n); n       return q.top().w; n     } n   } n } n void main() { n   e[0].w = 0; n   cnt = n = read(); n   m = read(); n   for (int i = (1); i <= (m); ++i) { n     q.push(e[i] = (Edge){read(), read(), read(), i}); n     printf( %d n , upd(e[i].u, e[i].v, i)); n   } n } n }  // namespace qiqi n int main() { n   qiqi::main(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int max(int a, int b) { n   if (a > b) n     return a; n   else n     return b; n } n long long min(long long a, long long b) { n   if (a < b) n     return a; n   else n     return b; n } n int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } n int lcm(int a, int b) { return a * (b / gcd(a, b)); } n vector<bool> isPrime(1000005 + 1, true); n string tos(int n) { n   stringstream ss; n   ss << n; n   return ss.str(); n } n void seive() { n   isPrime[0] = 0, isPrime[1] = 0; n   for (long long i = 2; i * i <= 1000005; i++) n     if (isPrime[i]) n       for (long long j = i * i; j <= 1000005; j += i) isPrime[j] = 0; n } n int dx[] = {1, -1, 0, 0}; n int dy[] = {0, 0, 1, -1}; n const int val = 1 << 20; n long long n, m, st, e, ans = 1, a[1111111], b[1111111]; n int main() { n   string s1, s2; n   cin >> s1 >> s2; n   long long i = 0, j = 0, beg = -1; n   while (j < s2.size()) { n     if (s1[i] == s2[j]) { n       if (beg == -1) { n         beg = j; n         a[0] = j; n       } n       i++; n     } n     if (i == s1.size()) { n       b[0] = j; n       break; n     } n     j++; n   } n   i = s1.size(); n   j = s2.size(); n   beg = -1; n   while (j >= 0) { n     if (s1[i] == s2[j]) { n       if (beg == -1) { n         beg = j; n         b[1] = j; n       } n       i--; n     } n     if (i < 0) { n       a[1] = j; n       break; n     } n     j--; n   } n   ans = max(0, a[1] - b[0]); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T abs(T x) { n   return x < 0 ? -x : x; n } n const double pi = asin(1.0) * 2; n int n; n int m; n long long pl, pr, vl, vr; n int k; n vector<long long> h; n double prob(long long l, long long r, long long L, long long R) { n   L = max(l, L); n   R = min(R, r); n   if (L > R) return 0.0; n   return (R - L + 1) / (double)(r - l + 1); n } n int main() { n   cin >> pl >> pr >> vl >> vr >> k; n   h.push_back(0); n   int cur = 0; n   for (int i = cur; h[i] * 10 + 4 <= 1e10; i++) { n     h.push_back(h[i] * 10 + 4); n     h.push_back(h[i] * 10 + 7); n   } n   sort((h).begin(), (h).end()); n   double ans = 0; n   for (int i = 1; i < (int)((h).size()); i++) { n     if (i + k >= (int)((h).size())) break; n     long long l1 = h[i - 1] + 1, r1 = h[i]; n     long long l2 = h[i + k - 1], r2 = h[i + k] - 1; n     ans += prob(pl, pr, l1, r1) * prob(vl, vr, l2, r2); n     ans += prob(pl, pr, l2, r2) * prob(vl, vr, l1, r1); n     ans -= prob(pl, pr, max(l2, l1), min(r1, r2)) * n            prob(vl, vr, max(l2, l1), min(r1, r2)); n   } n   printf( %.10lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long calc2(long long a, long long b, long long c, long long ab, n                 long long ac, long long bc, long long abc) { n   long long parts[] = {a - ab - ac + abc, n                        b - ab - bc + abc, n                        c - ac - bc + abc, n                        ab - abc, n                        ac - abc, n                        bc - abc, n                        abc}; n   int masks[3] = {1 + 8 + 16 + 64, 2 + 8 + 32 + 64, 4 + 16 + 32 + 64}; n   static int mark[64 * 8] = {0}; n   static int id = 0; n   ++id; n   long long res = 0; n   for (int i = 0; i < 7; ++i) n     if ((masks[0] >> i) & 1) n       for (int j = 0; j < 7; ++j) n         if ((masks[1] >> j) & 1) n           for (int k = 0; k < 7; ++k) n             if ((masks[2] >> k) & 1) { n               int a1 = min(i, min(j, k)); n               int a3 = max(i, max(j, k)); n               int a2 = i + j + k - a1 - a3; n               if (mark[64 * a1 + 8 * a2 + a3] == id) continue; n               mark[64 * a1 + 8 * a2 + a3] = id; n               if (i == j && j == k) { n                 long long x = parts[i]; n                 res += +x * (x - 1) * (x - 2) / 6 + x * (x - 1) + x; n               } else if (i == j || j == k || i == k) { n                 long long x = parts[i]; n                 long long y = parts[j]; n                 if (i == j) n                   y = parts[k]; n                 else if (j == k) n                   swap(x, y); n                 res += x * (x + 1) / 2 * y; n               } else { n                 res += parts[i] * parts[j] * parts[k]; n               } n             } n   return res; n } n int get_common_num(vector<int> &a, vector<int> &b) { n   int ia = 0, ib = 0; n   int res = 0; n   while (ia < a.size() && ib < b.size()) { n     if (a[ia] == b[ib]) { n       ++res; n       ++ia, ++ib; n     } else if (a[ia] < b[ib]) n       ++ia; n     else n       ++ib; n   } n   return res; n } n int get_common_num(vector<int> &a, vector<int> &b, vector<int> &c) { n   int ia = 0, ib = 0, ic = 0; n   int res = 0; n   while (ia < a.size() && ib < b.size() && ic < c.size()) { n     if (a[ia] == b[ib] && b[ib] == c[ic]) { n       ++res; n       ++ia, ++ib, ++ic; n     } else if (a[ia] <= min(b[ib], c[ic])) n       ++ia; n     else if (b[ib] <= min(a[ia], c[ic])) n       ++ib; n     else n       ++ic; n   } n   return res; n } n vector<int> dividers[100111]; n int main() { n   ios::sync_with_stdio(false); n   dividers[1].push_back(1); n   vector<int> smallest_fact(100111 + 1, 0); n   for (int i = 2; i < 100111; ++i) { n     if (!smallest_fact[i]) { n       smallest_fact[i] = i; n       for (long long j = i * (long long)i; j < 100111; j += i) { n         if (!smallest_fact[j]) smallest_fact[j] = i; n       } n     } n     int p = smallest_fact[i]; n     int x = i; n     while (x % p == 0) x /= p; n     int y = i; n     int pp = p; n     dividers[i] = dividers[x]; n     while (y % p == 0) { n       for (int d : dividers[x]) dividers[i].push_back(pp * d); n       pp *= p; n       y /= p; n     } n     sort(dividers[i].begin(), dividers[i].end()); n   } n   int t; n   cin >> t; n   while (t--) { n     int a, b, c; n     cin >> a >> b >> c; n     int x = dividers[a].size(); n     int y = dividers[b].size(); n     int z = dividers[c].size(); n     int xy = get_common_num(dividers[a], dividers[b]); n     int xz = get_common_num(dividers[a], dividers[c]); n     int yz = get_common_num(dividers[b], dividers[c]); n     int xyz = get_common_num(dividers[a], dividers[b], dividers[c]); n     long long ans = calc2(x, y, z, xy, xz, yz, xyz); n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double p, f[220][220]; n int x, k; n int main() { n   scanf( %d%d%lf , &x, &k, &p); n   p /= 100; n   for (int i = 0; i <= k; i++) n     for (int j = x + i; j % 2 == 0; j /= 2) f[0][i]++; n   for (int i = 0; i < k; i++) { n     for (int j = 0; j <= k; j++) { n       if (j) f[i + 1][j - 1] += (1 - p) * f[i][j]; n       if (j * 2 <= k) f[i + 1][j * 2] += p * (f[i][j] + 1); n     } n   } n   printf( %.10lf n , f[k][0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void inout() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n } n int main() { n   inout(); n   long long n, i, u, v; n   cin >> n; n   map<long long, long long> prv, nxt, cnt1, cnt2; n   vector<long long> ans(n + 5); n   for (i = 0; i < n; i++) { n     cin >> u >> v; n     prv[v] = u; n     nxt[u] = v; n     cnt1[u]++; n     cnt2[v]++; n   } n   for (auto k : cnt1) n     if (cnt1[k.first] > cnt2[k.first]) ans[1] = k.first; n   for (auto k : cnt2) n     if (cnt1[k.first] < cnt2[k.first]) ans[n] = k.first; n   i = 3; n   while (i <= n) { n     ans[i] = nxt[ans[i - 2]]; n     i += 2; n   } n   i = n + n % 2 - 2; n   while (i > 0) { n     ans[i] = prv[ans[i + 2]]; n     i -= 2; n   } n   for (i = 1; i <= n; i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 105000; n const int M = 105; n int n, m, p; n long long hill[N]; n long long a[N], s[N]; n long long dp[N]; n int top, tail; n long long duix[N], duiy[N]; n int getint() { n   char e = getchar(); n   int res = 0; n   while (e <  0  || e >  9 ) e = getchar(); n   while ( 0  <= e && e <=  9 ) { n     res = (res << 3) + (res << 1) + e -  0 ; n     e = getchar(); n   } n   return res; n } n int main() { n   int i, j; n   n = getint(); n   m = getint(); n   p = getint(); n   for (i = 2; i <= n; i++) hill[i] = getint(); n   for (i = 2; i <= n; i++) hill[i] += hill[i - 1]; n   for (i = 1; i <= m; i++) { n     a[i] = -hill[getint()]; n     a[i] += getint(); n   } n   sort(a + 1, a + m + 1); n   for (i = 1; i <= m; i++) s[i] = a[i] + s[i - 1]; n   for (i = 1; i <= m; i++) dp[i] = i * a[i]; n   for (j = 2; j <= p; j++) { n     top = 1; n     tail = 0; n     for (i = 1; i <= m; i++) { n       long long nowx = i; n       long long nowy = dp[i]; n       while (tail >= 2) { n         double xla = duiy[tail] - duiy[tail - 1]; n         xla /= duix[tail] - duix[tail - 1]; n         double xlb = nowy - duiy[tail - 1]; n         xlb /= nowx - duix[tail - 1]; n         if (xla - xlb > -1e-10) n           tail--; n         else n           break; n       } n       tail++; n       duix[tail] = nowx; n       duiy[tail] = nowy; n     } n     for (i = 1; i <= m; i++) { n       dp[i] = duiy[top] - a[i] * duix[top]; n       while (top < tail && duix[top + 1] <= i) { n         long long now = duiy[top + 1] - a[i] * duix[top + 1]; n         if (now <= dp[i]) { n           top++; n           dp[i] = now; n         } else n           break; n       } n       dp[i] += i * a[i]; n     } n   } n   printf( %I64d n , dp[m] - s[m]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, h; n int main() { n   scanf( %d %d , &n, &h); n   for (int i = 1; i < n; i++) { n     printf( %.10lf n , sqrt(1. * h * h * i / n)); n   } n } n


#include <bits/stdc++.h> n using namespace std; n void sprint(int x) { cout << x; } n void sprint(long x) { cout << x; } n void sprint(long long x) { cout << x; } n void sprint(unsigned x) { cout << x; } n void sprint(unsigned long x) { cout << x; } n void sprint(unsigned long long x) { cout << x; } n void sprint(float x) { cout << x; } n void sprint(double x) { cout << x; } n void sprint(long double x) { cout << x; } n void sprint(char x) { cout <<      << x <<     ; } n void sprint(const char *x) { cout <<     << x <<    ; } n void sprint(const string &x) { cout <<     << x <<    ; } n void sprint(bool x) { cout << (x ?  true  :  false ); } n template <typename T, typename V> n void sprint(const pair<T, V> &x); n template <typename T> n void sprint(const vector<T> &x); n template <typename T, typename V> n void sprint(const pair<T, V> &x) { n   cout <<  { ; n   sprint(x.first); n   cout <<  , ; n   sprint(x.second); n   cout <<  } ; n } n template <typename T> n void sprint(const vector<T> &x) { n   cout <<  { ; n   for (int u69 = 0; u69 < x.size(); u69++) { n     sprint(x[u69]); n     cout << (u69 + 1 == x.size() ?    :  , ); n   }; n   cout <<  } ; n } n template <typename T> n void sprint(const T &x) { n   int f = 0; n   cout <<  { ; n   for (auto &i : x) cout << (f++ ?  ,  :   ), sprint(i); n   cout <<  } ; n } n void sprint() { cout <<  ] n ; } n template <typename T, typename... V> n void sprint(T t, V... v) { n   sprint(t); n   if (sizeof...(v)) cout <<  ,  ; n   sprint(v...); n } n inline long long ceil2(long long a, long long b) { return (a + b - 1) / b; } n inline bool isPowerOfTwo(long long x) { return x && (!(x & (x - 1))); } n bool isPerfectSquare(long long x) { n   long long sr = sqrt(x); n   return ((sr * sr) == x); n } n bool isIn(string &s2, string &s1) { n   if (s1.find(s2) != string::npos) return true; n   return false; n } n bool isSorted(vector<int> &a) { n   for (int i = 0; i < (int)a.size() - 1; i++) n     if (a[i] > a[i + 1]) return false; n   return true; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   char a[n][m]; n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < m; j++) { n       cin >> a[i][j]; n     } n   } n   int count = 0; n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < m; j++) { n       if (a[i][j] ==  W  && ((a[i + 1][j] ==  P  && i + 1 < n) || n                              (a[i - 1][j] ==  P  && i - 1 >= 0) || n                              (a[i][j + 1] ==  P  && j + 1 < m) || n                              (a[i][j - 1] ==  P  && j - 1 >= 0))) { n         count++; n       } n     } n   } n   cout << count <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int l[100005], r[100005]; n int main(int argc, char* argv[]) { n   std::ios::sync_with_stdio(false); n   int n, k; n   scanf( %d%d , &n, &k); n   int sum = 0; n   for (int i = 0; i < n; i++) { n     scanf( %d%d , &l[i], &r[i]); n     sum += r[i] - l[i] + 1; n   } n   int res = 0; n   while (sum % k != 0) { n     sum++; n     res++; n   } n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T bigmod(T p, T e, T M) { n   long long ret = 1; n   for (; e > 0; e >>= 1) { n     if (e & 1) ret = (ret * p) % M; n     p = (p * p) % M; n   } n   return (T)ret; n } n template <class T> n inline T gcd(T a, T b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n template <class T> n inline T modinverse(T a, T M) { n   return bigmod(a, M - 2, M); n } n template <class T> n inline T lcm(T a, T b) { n   a = abs(a); n   b = abs(b); n   return (a / _gcd(a, b)) * b; n } n template <class T, class X> n inline bool getbit(T a, X i) { n   T t = 1; n   return ((a & (t << i)) > 0); n } n template <class T, class X> n inline T setbit(T a, X i) { n   T t = 1; n   return (a | (t << i)); n } n template <class T, class X> n inline T resetbit(T a, X i) { n   T t = 1; n   return (a & (~(t << i))); n } n inline long long power(long long a, long long b) { n   long long multiply = 1; n   for (int i = (0); i < (b); i++) { n     multiply *= a; n   } n   return multiply; n } n char given[505][505]; n long long dp[505][505]; n int n, m, modby; n long long calc(int x, int y) { n   long long &ret = dp[x][y]; n   if (x == 2 && y == 0) return 1; n   if (x == 0 && y == 1) return 0; n   if (ret != -1) return ret; n   ret = 0; n   long long a; n   if (x > 1) { n     a = calc(x - 2, y) % modby; n     a = (a * ((x * (x - 1)) / 2) % modby) % modby; n     ret += a % modby; n   } n   if (y > 1) { n     a = calc(x + 2, y - 2) % modby; n     a = (a * ((y * (y - 1)) / 2) % modby) % modby; n     ret += a % modby; n   } n   if (x != 0 && y != 0) { n     a = calc(x, y - 1) % modby; n     a = (a * (x * y) % modby) % modby; n     ret += a % modby; n   } n   return ret; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> m >> modby; n   memset(dp, -1, sizeof(dp)); n   for (int i = (0); i < (m); i++) { n     for (int j = (0); j < (n); j++) cin >> given[i][j]; n   } n   int zeros = 0, ones = 0; n   for (int i = (0); i < (n); i++) { n     int x = 0; n     for (int j = (0); j < (m); j++) { n       if (given[j][i] ==  1 ) x++; n     } n     if (x == 0) n       zeros++; n     else if (x == 1) n       ones++; n   } n   if (n == m) n     cout << 1 << endl; n   else n     cout << calc(ones, zeros) % modby << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:64000000 ) n using namespace std; n const int MAXN = -1; n const int MOD = 1; n const long double EPS = 1e-11; n long double sqr(long double x) { return x * x; } n bool ok(long double x) { return x >= -EPS && x <= 1 + EPS; } n void norm(long double &x) { n   if (x < 0) x = 0; n   if (x > 1) x = 1; n } n int main() { n   int n; n   while (scanf( %d , &n) == 1) { n     vector<long double> mn(n), mx(n); n     for (int i = 0; i < n; i++) { n       double o; n       scanf( %lf , &o); n       mx[i] = o; n     } n     for (int i = 0; i < n; i++) { n       double o; n       scanf( %lf , &o); n       mn[i] = o; n     } n     vector<double> a(n), b(n); n     long double sa = 0.0, sb = 0.0; n     for (int i = 0; i < n; i++) { n       long double g = mn[i] + mx[i]; n       long double A = -1; n       long double B = (sb + g - sa); n       long double C = g * sa - mx[i]; n       long double D = B * B - 4 * A * C; n       assert(D >= -EPS); n       D = fabs(D); n       long double na = (-B - sqrt(D)) / (2 * A); n       long double nb = g - na; n       assert(ok(na)); n       assert(ok(nb)); n       a[i] = na; n       b[i] = nb; n       sa += na; n       sb += nb; n     } n     for (int i = 0; i < n; i++) printf( %.15f%c , a[i],    n [i + 1 == n]); n     for (int i = 0; i < n; i++) printf( %.15f%c , b[i],    n [i + 1 == n]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 65; n char g[maxn][maxn]; n int r, c, row[maxn], col[maxn]; n void solve() { n   memset(row, 0, sizeof row); n   memset(col, 0, sizeof col); n   int sum = 0; n   for (int i = 1; i <= r; i++) { n     for (int j = 1; j <= c; j++) { n       if (g[i][j] ==  A ) col[i]++, row[j]++, sum++; n     } n   } n   if (sum == 0) n     printf( MORTAL n ); n   else if (sum == r * c) n     printf( 0 n ); n   else if (row[1] == r || row[c] == r || col[1] == c || col[r] == c) n     printf( 1 n ); n   else { n     int flag = 0; n     for (int i = 2; i < r; i++) n       if (col[i] == c) flag = 1; n     for (int i = 2; i < c; i++) n       if (row[i] == r) flag = 1; n     if (flag == 1 || g[1][1] ==  A  || g[r][1] ==  A  || g[1][c] ==  A  || n         g[r][c] ==  A ) n       printf( 2 n ); n     else if (row[1] > 0 || row[c] > 0 || col[1] > 0 || col[r] > 0) n       printf( 3 n ); n     else n       printf( 4 n ); n   } n } n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     scanf( %d%d , &r, &c); n     for (int i = 1; i <= r; i++) scanf( %s , g[i] + 1); n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int c[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; n char a[60][60], color[2510]; n int to[60][60], minx[2510], maxx[2510], miny[2510], maxy[2510], f[2510][4][2]; n void dfs(int x0, int y0, int value) { n   to[x0][y0] = value; n   minx[value] = min(minx[value], x0); n   miny[value] = min(miny[value], y0); n   maxx[value] = max(maxx[value], x0); n   maxy[value] = max(maxy[value], y0); n   for (int i = 0; i < 4; i++) { n     int x = x0 + c[i][0], y = y0 + c[i][1]; n     if (to[x][y] == 0 && a[x][y] == a[x0][y0]) dfs(x, y, value); n   } n } n int calc(int x, int y, int z) { n   int x1 = minx[x], x2 = maxx[x], y1 = miny[x], y2 = maxy[x]; n   if (y == 0) n     return (z == 0 ? to[x1][y2 + 1] : to[x2][y2 + 1]); n   else if (y == 1) n     return (z == 0 ? to[x2 + 1][y2] : to[x2 + 1][y1]); n   else if (y == 2) n     return (z == 0 ? to[x2][y1 - 1] : to[x1][y1 - 1]); n   else n     return (z == 0 ? to[x1 - 1][y1] : to[x1 - 1][y2]); n } n int main() { n   int n, K; n   scanf( %d%d , &n, &K); n   for (int i = 1; i <= n; i++) scanf( %s , a[i] + 1); n   int m = strlen(a[1] + 1), tot = 0; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (a[i][j] !=  0  && to[i][j] == 0) { n         tot++; n         color[tot] = a[i][j]; n         minx[tot] = miny[tot] = 1 << 30; n         maxx[tot] = maxy[tot] = 0; n         dfs(i, j, tot); n       } n   for (int i = 1; i <= tot; i++) n     for (int j = 0; j < 4; j++) n       for (int k = 0; k < 2; k++) f[i][j][k] = calc(i, j, k); n   int bp = 1, dp = 0, cp = 0; n   for (int i = 1; i <= K; i++) { n     int p = f[bp][dp][cp]; n     if (p == 0) n       if (cp == 0) n         cp = 1; n       else { n         cp = 0; n         dp = dp + 1 & 3; n       } n     else n       bp = p; n   } n   printf( %c n , color[bp]); n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const unsigned long long MAX = LONG_MAX; n void fasterIO() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n int sumDig(int a) { n   int sum = 0; n   while (a != 0) { n     sum += a % 10; n     a /= 10; n   } n   return sum; n } n int main() { n   int test; n   cin >> test; n   while (test--) { n     long long p, k; n     long long arr[150]; n     cin >> p >> k; n     for (unsigned long long i = 0; i < (p); i++) cin >> arr[i]; n     sort(arr, arr + p); n     if (abs((arr[0] + k) - arr[p - 1]) > k) n       cout <<  -1  << endl; n     else n       cout << arr[0] + k << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[18] = {0}; n long long int get(long long int n, int l) { n   if (!n) return 0; n   if (l == 1) return n; n   long long int t = a[l]; n   if (n == t) return l; n   if (n < t) n     return min(get(n, l - 1), l + get(t - n, l - 1)); n   else n     return min((n / t) * l + get(n % t, l - 1), n                ((1 + (n / t)) * l) + get(t - n % t, l - 1)); n } n int main() { n   long long int n, i, ans; n   scanf( %I64d , &n); n   for (int i = 1; i < 18; i++) a[i] = a[i - 1] * 10 + 1; n   for (i = 1; n > a[i]; i++) n     ; n   ans = get(n, i); n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, n, p, c; n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %d%d , &n, &p), c = 2 * n + p; n     for (int i = 1; i <= n; ++i) n       for (int j = i + 1; j <= n; ++j) n         if (c) printf( %d %d n , i, j), --c; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > g[500100]; n long long f[500100][2]; n int n, k; n void dfs(int u, int fa) { n   vector<long long> d; n   d.clear(); n   f[u][0] = f[u][1] = 0; n   for (int i = 0; i < g[u].size(); i++) { n     int v = g[u][i].first, w = g[u][i].second; n     if (v == fa) continue; n     dfs(v, u); n     f[u][0] += f[v][1]; n     f[u][1] += f[v][1]; n     d.push_back(-(w + f[v][0] - f[v][1])); n   } n   sort(d.begin(), d.end()); n   for (int i = 0; i < d.size() && i < k; i++) { n     long long D = -d[i]; n     if (D <= 0) break; n     if (i < k - 1) f[u][0] += D; n     f[u][1] += D; n   } n } n void ae(int x, int y, int z) { g[x].push_back(make_pair(y, z)); } n void solve() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) g[i].clear(); n   int x, y, z; n   for (int i = 1; i < n; i++) { n     scanf( %d%d%d , &x, &y, &z); n     ae(x, y, z); n     ae(y, x, z); n   } n   dfs(1, 0); n   printf( %lld n , f[1][1]); n } n int main() { n   int tc; n   scanf( %d , &tc); n   while (tc--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 10; n int a[MAXN], b[MAXN], f[260], n, k; n int main(void) { n   ios::sync_with_stdio(false); n   cin >> n >> k; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n     b[i] = a[i]; n   } n   memset(f, -1, sizeof f); n   for (int i = 1; i <= n; ++i) { n     if (f[a[i]] == -1) { n       int low = -1; n       for (int j = a[i]; j >= a[i] - k + 1 && j >= 0; --j) { n         if (f[j] != -1) { n           if (f[j] >= a[i] - k + 1) low = f[j]; n           break; n         } n         low = j; n       } n       for (int j = low; j <= a[i]; ++j) { n         f[j] = low; n       } n     } n     b[i] = f[a[i]]; n   } n   for (int i = 1; i <= n; ++i) { n     cout << b[i] <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[100005]; n int n, a[100005], ans[100005]; n inline bool find() { n   int i = 1, j = n; n   while (i <= j) { n     if (a[i] == a[j]) { n       i++; n       j--; n     } else if (a[i] == a[j] + 10) { n       a[j - 1]--; n       a[j] += 10; n     } else if ((a[i] == a[j] + 1) || (a[i] == a[j] + 11)) { n       a[i]--; n       a[i + 1] += 10; n     } else n       return false; n   } n   if ((i - 1 == j + 1) && n       ((a[i - 1] > 18) || (a[i - 1] < 0) || (a[i - 1] % 2 == 1))) n     return false; n   if (a[1] == 0) return false; n   i--; n   j++; n   while (i) { n     if ((a[i] > 18) || (a[i] < 0)) return false; n     ans[i] = (a[i] + 1) / 2; n     ans[j] = a[i] - ans[i]; n     i--; n     j++; n   } n   for (int i = 1; i <= n; i++) printf( %d , ans[i]); n   printf(  n ); n   return true; n } n int main() { n   scanf( %s , s + 1); n   for (int i = 1; i <= strlen(s + 1); i++) a[i] = s[i] -  0 ; n   n = strlen(s + 1); n   if (find()) return 0; n   if ((s[1] !=  1 ) || (n == 1)) { n     printf( 0 n ); n     return 0; n   } n   for (int i = 1; i <= n; i++) a[i - 1] = s[i] -  0 ; n   a[1] += 10; n   n--; n   if (find()) return 0; n   printf( 0 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ben[11]; n int n, k; n int mem[505][505 * 11]; n int arr[505 * 505]; n int freqFav[100005]; n int freqCards[100005]; n int solve(int num, int rem) { n   if (rem == 0 || num == 0) return 0; n   int &ret = mem[num][rem]; n   if (~ret) return ret; n   for (int i = 0; i < min(k, rem); i++) { n     ret = max(ret, ben[i] + solve(num - 1, rem - i - 1)); n   } n   return ret; n } n int main() { n   memset(mem, -1, sizeof(mem)); n   scanf( %d %d , &n, &k); n   for (int i = 0; i < k * n; i++) { n     scanf( %d , arr + i); n     freqCards[arr[i]]++; n   } n   for (int i = 0; i < n; i++) { n     int fav; n     scanf( %d , &fav); n     freqFav[fav]++; n   } n   for (int i = 0; i < k; i++) { n     scanf( %d , ben + i); n   } n   int res = 0; n   for (int i = 0; i < 100005; i++) { n     res += solve(freqFav[i], freqCards[i]); n   } n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int sz = 4e6 + 50; n int n, m; n string str; n int s[sz], e[sz], four[sz], seven[sz], non_dec[sz], non_inc[sz]; n vector<bool> lazy(sz, 0); n void calculate(int node) { n   int lc = node * 2; n   int rc = lc + 1; n   int f1 = four[lc], s1 = seven[lc], nd1 = non_dec[lc], ni1 = non_inc[lc]; n   int f2 = four[rc], s2 = seven[rc], nd2 = non_dec[rc], ni2 = non_inc[rc]; n   if (lazy[lc]) { n     swap(f1, s1); n     swap(nd1, ni1); n   } n   if (lazy[rc]) { n     swap(f2, s2); n     swap(nd2, ni2); n   } n   four[node] = f1 + f2; n   seven[node] = s1 + s2; n   non_dec[node] = max(f1 + nd2, nd1 + s2); n   non_inc[node] = max(s1 + ni2, ni1 + f2); n } n void modify(int node) { n   int lc = node * 2; n   int rc = lc + 1; n   four[node] = four[lc] + four[rc]; n   seven[node] = seven[lc] + seven[rc]; n   non_dec[node] = max(four[lc] + non_dec[rc], non_dec[lc] + seven[rc]); n   non_inc[node] = max(seven[lc] + non_inc[rc], non_inc[lc] + four[rc]); n } n void lazyPropagation(int node) { n   if (lazy[node]) { n     lazy[2 * node] = !lazy[2 * node]; n     lazy[2 * node + 1] = !lazy[2 * node + 1]; n   } n   lazy[node] = 0; n } n void build(int node, int l, int r) { n   s[node] = l; n   e[node] = r; n   if (l == r) { n     if (str[l] ==  4 ) { n       four[node] = 1; n       seven[node] = 0; n       non_dec[node] = 1; n       non_inc[node] = 1; n     } else { n       four[node] = 0; n       seven[node] = 1; n       non_dec[node] = 1; n       non_inc[node] = 1; n     } n     return; n   } n   int mid = (l + r) / 2; n   build(2 * node, l, mid); n   build(2 * node + 1, mid + 1, r); n   modify(node); n } n void update(int node, int l, int r) { n   if (s[node] > r || e[node] < l) return; n   if (l <= s[node] && e[node] <= r) { n     lazy[node] = !lazy[node]; n     return; n   } n   lazyPropagation(node); n   update(2 * node, l, r); n   update(2 * node + 1, l, r); n   calculate(node); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   string tt; n   int x, y; n   cin >> n >> m; n   cin >> str; n   str =  0  + str; n   build(1, 1, n); n   while (m--) { n     cin >> tt; n     if (tt[0] ==  c ) { n       if (!lazy[1]) n         cout << non_dec[1] <<   n ; n       else n         cout << non_inc[1] <<   n ; n     } else { n       cin >> x >> y; n       update(1, x, y); n     } n   } 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 int rnd(int l, int r) { return l + rng() % (r - l + 1); } n const int N = 200010; n int n; n long long ret; n int a[N], lz[N], b[N]; n int f[N][25]; n int que(int i, int j) { n   int t = lz[j - i + 1]; n   return max(f[i][t], f[j - (1 << t) + 1][t]); n } n void dnc(int l, int r) { n   if (l >= r) return; n   int x = que(l, r); n   int p = b[x]; n   if (p - l < r - p) n     for (int i = l, _c = p - 1; i <= _c; ++i) n       ret += r >= b[x - a[i]] && b[x - a[i]] > p; n   else n     for (int i = p + 1, _c = r; i <= _c; ++i) n       ret += l <= b[x - a[i]] && b[x - a[i]] < p; n   dnc(l, p - 1); n   dnc(p + 1, r); n } n int main() { n   n = ({ n     int x = 0; n     int c = getchar(), n = 0; n     for (; !isdigit(c); c = getchar()) n = (c ==  - ); n     for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ; n     n ? -x : x; n   }); n   for (int i = 1, _c = n; i <= _c; ++i) n     a[i] = f[i][0] = ({ n       int x = 0; n       int c = getchar(), n = 0; n       for (; !isdigit(c); c = getchar()) n = (c ==  - ); n       for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ; n       n ? -x : x; n     }), n     b[a[i]] = i, lz[i] = log2(i); n   for (int j = 1, _c = 20; j <= _c; ++j) n     for (int i = 1, _c = n; i <= _c; ++i) { n       if (i + (1 << j - 1) > n) break; n       f[i][j] = max(f[i][j - 1], f[i + (1 << j - 1)][j - 1]); n     } n   dnc(1, n); n   cout << ret; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[3010][3010], height[3010][3010]; n deque<pair<int, int> > mdq; n void push(int val, int pos) { n   while (!mdq.empty() && mdq.back().first > val) mdq.pop_back(); n   mdq.push_back(make_pair(val, pos)); n } n int front(int lim) { n   while (!mdq.empty() && mdq.front().second < lim) mdq.pop_front(); n   return mdq.front().first; n } n void clear() { mdq.clear(); } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int row, column, r1, c1; n   long long int g0, x, y, z; n   cin >> row >> column >> r1 >> c1; n   cin >> g0 >> x >> y >> z; n   long long int g = g0; n   for (int i = 0; i < row; i++) { n     for (int j = 0; j < column; j++) { n       arr[i][j] = g; n       g = (g * x + y) % z; n     } n   } n   for (int i = 0; i < row; i++) { n     clear(); n     for (int j = 0; j < c1; j++) push(arr[i][j], j); n     for (int j = 0; j + c1 <= column; j++) { n       height[i][j] = front(j); n       if (j + c1 < column) push(arr[i][j + c1], j + c1); n     } n   } n   for (int j = 0; j + c1 <= column; j++) { n     clear(); n     for (int i = 0; i < r1; i++) push(height[i][j], i); n     for (int i = 0; i + r1 <= row; i++) { n       height[i][j] = front(i); n       if (i + r1 < row) push(height[i + r1][j], i + r1); n     } n   } n   long long int ans = 0; n   for (int i = 0; i + r1 <= row; i++) n     for (int j = 0; j + c1 <= column; j++) ans += height[i][j]; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n double ans; n int n, cnt, C; n int head, tail; n int q[3005], d[3005], d2[3005]; n bool vis[3005]; n vector<int> e[3005]; n void dfs(int x) { n   vis[x] = 1; n   for (int i = 0; i < e[x].size(); i++) n     if (!vis[e[x][i]]) { n       d2[e[x][i]] = d2[x] + 1; n       if (!d[e[x][i]]) { n         d[e[x][i]] = d[x] + 1; n         ans += 1.0 / d[e[x][i]]; n       } else n         ans = n             ans + 1.0 / d2[e[x][i]] - 2.0 / (d[e[x][i]] + d2[e[x][i]] + C - 2); n       dfs(e[x][i]); n     } n   vis[x] = 0; n } n int main() { n   n = read(); n   for (int i = 1; i <= n; i++) { n     int u = read(), v = read(); n     u++; n     v++; n     e[u].push_back(v); n     e[v].push_back(u); n     d[u]++; n     d[v]++; n   } n   for (int i = 1; i <= n; i++) n     if (d[i] == 1) q[tail++] = i; n   while (head != tail) { n     int now = q[head]; n     head++; n     for (int i = 0; i < e[now].size(); i++) { n       d[e[now][i]]--; n       if (d[e[now][i]] == 1) q[tail++] = e[now][i]; n     } n   } n   C = n - tail; n   for (int i = 1; i <= n; i++) { n     memset(d, 0, sizeof(d)); n     memset(d2, 0, sizeof(d2)); n     d[i] = d2[i] = 1; n     dfs(i); n   } n   printf( %.10lf n , ans + n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void read(T& num) { n   bool start = false, neg = false; n   char c; n   num = 0; n   while ((c = getchar()) != EOF) { n     if (c ==  - ) n       start = neg = true; n     else if (c >=  0  && c <=  9 ) { n       start = true; n       num = num * 10 + c -  0 ; n     } else if (start) n       break; n   } n   if (neg) num = -num; n } n inline int Q(int x, int y) { n   printf( 0 %d %d n , x, y); n   fflush(stdout); n   scanf( %d , &x); n   return x; n } n set<int> c; n void dfs(int l, int r) { n   if (l > r) return; n   int mid = l + (r - l) / 2; n   int d = Q(mid, mid); n   if (!d) c.insert(mid), d++; n   dfs(l, mid - d); n   dfs(mid + d, r); n } n set<int> V, H; n int main() { n   int n = 0, m = 0; n   int mx = (int)(1e8); n   dfs(-mx, mx); n   int s = mx; n   for (; c.count(s); s--) n     ; n   for (set<int>::iterator it = c.begin(); it != c.end(); it++) { n     int x = (*it); n     if (!Q(x, s)) V.insert(x), ++n; n     if (!Q(s, x)) H.insert(x), ++m; n   } n   printf( 1 %d %d n , n, m); n   for (set<int>::iterator it = V.begin(); it != V.end(); it++) n     printf( %d  , (*it)); n   putchar(  n ); n   for (set<int>::iterator it = H.begin(); it != H.end(); it++) n     printf( %d  , (*it)); n   putchar(  n ); n   fflush(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000000 + 5]; n int b[1000000 + 5]; n int main() { n   ios_base::sync_with_stdio(false); n   int i, j, n, k; n   queue<pair<int, int> > q; n   cin >> n; n   for (i = 1; i <= n; i++) cin >> a[i]; n   for (i = 1; i <= n; i++) cin >> b[i]; n   for (i = 1; i <= n; i++) { n     if (a[i] != b[i]) { n       for (j = i + 1; j <= n; j++) { n         if (a[i] == b[j]) { n           k = j; n           break; n         } n       } n       for (j = k; j > i; j--) { n         q.push(make_pair(j - 1, j)); n         int t = b[j]; n         b[j] = b[j - 1]; n         b[j - 1] = t; n       } n     } n   } n   cout << q.size() << endl; n   while (q.empty() != 1) { n     cout << q.front().first <<     << q.front().second << endl; n     q.pop(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int bignumlen = 2200; n const int Blen = 8; n const long long base = 100000000; n struct bignum { n   int len; n   long long data[bignumlen]; n   long long &operator[](int x) { return (data[x]); } n   const long long &operator[](int x) const { return (data[x]); } n   bignum() { n     memset(data, 0, sizeof(data)); n     len = 0; n   } n   void clear() { n     for (int i = len; i >= 1; --i) data[i] = 0; n     len = 0; n   } n   int check(const bignum &a, const bignum &b) { n     if (a.len > b.len) return (0); n     if (b.len > a.len) return (1); n     for (int i = a.len; i >= 1; --i) { n       if (a.data[i] < b.data[i]) return (1); n       if (b.data[i] < a.data[i]) return (0); n     } n     return 2; n   } n   bool operator<(const bignum &b) { return (check(*this, b) == 1); } n   bool operator>(const bignum &b) { return (check(*this, b) == 0); } n   bool operator<=(const bignum &b) { return (check(*this, b) >= 1); } n   bool operator>=(const bignum &b) { return (check(*this, b) % 2 == 0); } n   bool operator!=(const bignum &b) { return (check(*this, b) != 2); } n   bool operator==(const bignum &b) { return (check(*this, b) == 2); } n   bignum operator=(const bignum &x) { n     for (int i = x.len + 1; i <= len; ++i) data[i] = 0; n     for (int i = 1; i <= x.len; ++i) data[i] = x.data[i]; n     len = x.len; n     return *this; n   } n   bignum operator=(long long x) { n     for (int i = len; i >= 0; --i) data[i] = 0; n     len = 0; n     while (x) { n       data[++len] = x % base; n       x /= base; n     } n     return *this; n   } n   bignum(long long x) { n     memset(data, 0, sizeof(data)); n     len = 0; n     (*this) = x; n   } n   bignum operator*(const bignum &b) { n     int i, j; n     bignum tmp; n     for (i = 1; i <= len; ++i) n       if (data[i] != 0) n         for (j = 1; j <= b.len; ++j) n           if (b.data[j] != 0) { n             tmp.data[i + j - 1] += data[i] * b.data[j]; n             tmp.data[i + j] += tmp.data[i + j - 1] / base; n             tmp.data[i + j - 1] %= base; n           } n     tmp.len = len + b.len - 1; n     while (tmp.data[tmp.len + 1]) tmp.len++; n     return tmp; n   } n   bignum operator*(long long x) { n     int i; n     bignum tmp; n     for (i = 1; i <= len; ++i) tmp[i] = data[i] * x; n     tmp.len = len; n     for (i = 1; i <= len; ++i) { n       tmp[i + 1] += tmp[i] / base, tmp[i] %= base; n       if (tmp[i + 1] && i + 1 > tmp.len) tmp.len++; n     } n     return tmp; n   } n   bignum operator/(long long x) { n     int i; n     bignum tmp; n     long long y = 0; n     for (i = len; i >= 1; --i) { n       y = y * base + data[i]; n       tmp[i] = y / x; n       y %= x; n     } n     tmp.len = len; n     while (tmp[tmp.len] == 0 && tmp.len > 1) tmp.len--; n     return tmp; n   } n   bignum operator/(const bignum &b) { n     if (b.len <= 1 && b[1] == 0) { n       printf( error! ?0?! ); n       for (;;) n         ; n     } n     int i, l1 = (len - 1) * Blen, l2 = (b.len - 1) * Blen; n     long long x = data[len], y = b[b.len]; n     while (x) x /= 10, l1++; n     while (y) y /= 10, l2++; n     bignum tmp, chu, B; n     chu = *this; n     B = b; n     for (i = 1; i * Blen <= l1 - l2; ++i) B *= base; n     for (i = 1; i <= (l1 - l2) % Blen; ++i) B *= 10; n     for (i = l1 - l2; i >= 0; --i) { n       x = 0; n       while (chu >= B) chu -= B, x++; n       tmp[i / Blen + 1] = tmp[i / Blen + 1] * 10 + x; n       B /= 10; n     } n     tmp.len = (l1 - l2) / Blen + 1; n     while (tmp.len >= 1 && !tmp[tmp.len]) tmp.len--; n     return tmp; n   } n   bignum operator+(const bignum &b) { n     bignum tmp; n     int i, l = max(len, b.len); n     for (i = 1; i <= l; ++i) tmp[i] = data[i] + b[i]; n     for (i = 1; i <= l; ++i) tmp[i + 1] += tmp[i] / base, tmp[i] %= base; n     tmp.len = l; n     if (tmp[tmp.len + 1]) tmp.len++; n     return tmp; n   } n   bignum operator+(long long x) { n     bignum tmp; n     tmp = *this; n     tmp[1] += x; n     for (int i = 1; i <= len && tmp[i] >= base; ++i) n       tmp[i + 1] += tmp[i] / base, tmp[i] %= base; n     while (tmp[tmp.len + 1]) tmp.len++; n     return tmp; n   } n   bignum operator-(const bignum &b) { n     int i; n     bignum tmp; n     for (i = 1; i <= len; ++i) tmp.data[i] = data[i] - b.data[i]; n     for (i = 1; i <= len; ++i) { n       if (tmp[i] < 0) tmp.data[i] += base, tmp.data[i + 1]--; n     } n     tmp.len = len; n     while (tmp[tmp.len] == 0 && tmp.len > 1) tmp.len--; n     return tmp; n   } n   bignum operator-(long long x) { n     bignum tmp; n     tmp = *this; n     tmp[1] -= x; n     for (int i = 1; i <= len && tmp[i] < 0; ++i) { n       tmp[i + 1] += (tmp[i] + 1) / base - 1; n       tmp[i] = (tmp[i] + 1) % base + base - 1; n     } n     while (!tmp[tmp.len] && tmp.len > 1) tmp.len--; n     return tmp; n   } n   long long operator%(long long x) { n     int i; n     long long y = 0; n     for (i = len; i >= 1; --i) y = (y * base + data[i]) % x; n     return y; n   } n   bignum operator%(const bignum &b) { n     if (b.len <= 1 && b[1] == 0) { n       printf( error! ?0 mod! ); n       for (;;) n         ; n     } n     int i, l1 = (len - 1) * Blen, l2 = (b.len - 1) * Blen; n     long long x = data[len], y = b[b.len]; n     while (x) x /= 10, l1++; n     while (y) y /= 10, l2++; n     bignum chu, B; n     chu = *this; n     B = b; n     for (i = 1; i * Blen <= l1 - l2; ++i) B *= base; n     for (i = 1; i <= (l1 - l2) % Blen; ++i) B *= 10; n     for (i = l1 - l2; i >= 0; --i) { n       while (chu >= B) chu -= B; n       B /= 10; n     } n     return chu; n   } n   bignum operator+=(const bignum &b) { return *this = (*this + b); } n   bignum operator*=(const bignum &b) { return *this = (*this * b); } n   bignum operator-=(const bignum &b) { return *this = (*this - b); } n   bignum operator/=(const bignum &b) { return *this = (*this / b); } n   bignum operator%=(const bignum &b) { return *this = (*this % b); } n   bignum operator*=(long long x) { return (*this = (*this * x)); } n   bignum operator+=(long long x) { return (*this = (*this + x)); } n   bignum operator-=(long long x) { return (*this = (*this - x)); } n   bignum operator/=(long long x) { return (*this = (*this / x)); } n   void read() { n     char c[bignumlen * Blen + 10]; n     scanf( %s , c + 1); n     int l = strlen(c + 1); n     (*this).clear(); n     long long x; n     for (int i = 1; i <= (l - 1) / Blen + 1; ++i) { n       x = 0; n       for (int j = l - Blen * i + 1; j <= l - Blen * i + Blen; ++j) n         if (j >= 1) x = x * 10 + c[j] - 48; n       data[++len] = x; n     } n   } n   void write() { n     printf( %lld , data[len]); n     for (int i = len - 1; i >= 1; --i) printf( %0*I64d , Blen, data[i]); n   } n } p, q, pp, qq; n bignum gcd(const bignum &A, const bignum &B) { n   bignum a = A, b = B, res = 1; n   while (!(a[1] & 1) && !(b[1] & 1)) a /= 2, b /= 2, res *= 2; n   for (;;) { n     if (a.len == 1 && a[1] == 0) return b * res; n     if (b.len == 1 && b[1] == 0) return a * res; n     while (!(a[1] & 1)) a /= 2; n     while (!(b[1] & 1)) b /= 2; n     if (a > b) n       a -= b; n     else n       b -= a; n   } n } n void YF(bignum &A, bignum &B) { n   bignum tmp = gcd(A, B); n   A /= tmp; n   B /= tmp; n } n int n, i, x, num; n int main() { n   scanf( %d , &n); n   while (n % 2 == 0) { n     n /= 2; n     num++; n   } n   if (n == 1) { n     printf( %d/%d n , num, 1); n     return 0; n   } n   p = 0; n   q = 1; n   pp = 1; n   qq = 1; n   for (i = n + 1;;) { n     if (i > n) { n       if (q % (i - n) == 0) n         q /= i - n; n       else n         p *= i - n; n       if (qq % (i - n) == 0) n         qq /= i - n; n       else n         pp *= i - n; n       if (p % i == 0) n         p /= i; n       else n         q *= i; n       if (pp % i == 0) n         pp /= i; n       else n         qq *= i; n     } else n       p += q; n     if (i == 1) break; n     if (i % 2 == 0) n       i /= 2; n     else n       i += n; n   } n   YF(p, q); n   p = p * qq; n   q = q * (qq - pp); n   p += q * num; n   bignum tmp = gcd(p, q); n   (p / tmp).write(); n   printf( / ); n   (q / tmp).write(); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10, MSQ = 1010, SQ = 10, inf = 2e9; n pair<pair<int, int>, int> p[maxn]; n map<int, vector<int> > mp; n vector<int> vec, vec2; n int n, k, sm[maxn], l[maxn], r[maxn]; n long long ans; n void add(int ind, int val) { n   while (ind < maxn) { n     sm[ind] += val; n     ind += (ind & -ind); n   } n } n int ask(int ind) { n   int ans = 0; n   while (ind > 0) { n     ans += sm[ind]; n     ind -= (ind & -ind); n   } n   return ans; n } n void Add(int id, int val) { n   if (val == 1) ans += ask(r[id]) - ask(l[id]); n   add(id + 1, val); n } n bool CMP(int a, int b) { return p[a].first.second > p[b].first.second; } n void Add(vector<int>& vec) { n   sort(vec.begin(), vec.end(), CMP); n   for (int id : vec) Add(id, 1); n   for (int id : vec) Add(id, -1); n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; i++) { n     scanf( %d%d%d , &p[i].first.first, &p[i].first.second, &p[i].second); n     vec.push_back(p[i].second); n   } n   sort(p, p + n); n   for (int i = 0; i < n; i++) { n     pair<pair<int, int>, int> PP1 = {{p[i].first.first - p[i].first.second, -1}, n                                      -1}, n                               PP2 = { n                                   {p[i].first.first + p[i].first.second, inf}, n                                   inf}; n     l[i] = lower_bound(p, p + n, PP1) - p; n     r[i] = upper_bound(p, p + n, PP2) - p; n   } n   for (int i = 0; i < n; i++) { n     mp[p[i].second].push_back(i); n   } n   sort(vec.begin(), vec.end()); n   vec.resize(unique(vec.begin(), vec.end()) - vec.begin()); n   for (int i = 0; i < int((vec).size()); i++) { n     long long bf = ans; n     Add(mp[vec[i]]); n     vec2.push_back(ans - bf); n   } n   for (int i = 0; i < int((vec).size()); i++) { n     for (int j = i + 1; j < int((vec).size()) && vec[j] - vec[i] <= k; j++) { n       vector<int> vv(int((mp[vec[i]]).size()) + int((mp[vec[j]]).size())), n           v1 = mp[vec[i]], v2 = mp[vec[j]]; n       merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vv.begin()); n       Add(vv); n       ans -= vec2[i] + vec2[j]; n     } n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long x, y, p, q; n     cin >> x >> y >> p >> q; n     if (p == q && x == y) { n       printf( 0 n ); n       continue; n     } n     if (p == 0 && x == 0) { n       printf( 0 n ); n       continue; n     } n     if (p == q || p == 0 || q == 0) { n       printf( -1 n ); n       continue; n     } n     long long i = max( n         (x - p * ((y + q - 1) / q) + p - 1) / p, n         (p * ((y + q - 1) / q) - (x + ((y + q - 1) / q) * q - y) + q - p - 1) / n             (q - p)); n     printf( %lld n , i * q + ((y + q - 1) / q) * q - y); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double sq = 1.73205080757; n int main() { n   long long k, k1; n   cin >> k; n   k1 = k * 2; n   k *= 2; n   k1 -= k1 % 3; n   k1++; n   if (k1 > k) k1 -= 3; n   long long h = 1, h1, an = 0; n   while (k1 > 0) { n     while (k1 * k1 + 3LL * h * h <= k * k && n            (k1 + 1) * (k1 + 1) + 3LL * (h - 1) * (h - 1) <= k * k) n       h++; n     h--; n     if ((k1 - 1) % 6 == 0) n       h1 = max(h - (h % 2 == 0), 0LL); n     else n       h1 = h - h % 2; n     an += h1; n     k1 -= 3; n   } n   cout << an * 2 - h1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void fast() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n int main() { n   fast(); n   long long int t = 1; n   while (t--) { n     long long int n, m; n     cin >> n >> m; n     long long int a[n][m], b[n][m]; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         cin >> a[i][j]; n       } n     } n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         cin >> b[i][j]; n       } n     } n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         if (a[i][j] > b[i][j]) { n           a[i][j] = b[i][j] + a[i][j]; n           b[i][j] = a[i][j] - b[i][j]; n           a[i][j] = a[i][j] - b[i][j]; n         } n       } n     } n     bool ans = 1; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m - 1; j++) { n         if (a[i][j] >= a[i][j + 1]) { n           ans = 0; n           break; n         } n         if (b[i][j] >= b[i][j + 1]) { n           ans = 0; n           break; n         } n       } n     } n     for (int i = 0; i < n - 1; i++) { n       for (int j = 0; j < m; j++) { n         if (a[i][j] >= a[i + 1][j]) { n           ans = 0; n           break; n         } n         if (b[i][j] >= b[i + 1][j]) { n           ans = 0; n           break; n         } n       } n     } n     if (!ans) n       cout <<  Impossible ; n     else n       cout <<  Possible ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const int inf = 0x3f3f3f3f; n const double eps = 1e-10; n const long long INF = (1LL << 60); n int main() { n   int a, b; n   scanf( %d%d , &a, &b); n   if (a == b) { n     printf( %d n , a); n     return 0; n   } n   double ans = -1; n   if (a >= b) { n     int k = (int)floor((a * 1.0 / b - 1) / 2); n     ans = (a - b) * 1.0 / (2 * k); n     ans = min(ans, (a + b) * 1.0 / (2 * k + 2)); n   } n   printf( %0.12f n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100000 + 10]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     long long k; n     cin >> k; n     for (int i = 1; i <= k; i++) cin >> a[i]; n     sort(a + 1, a + k + 1); n     int ans = 1; n     for (int i = 1; i < k; i++) { n       if (a[i] != a[i + 1]) { n         ans++; n         continue; n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char const *argv[]) { n   ios::sync_with_stdio(false); n   long long n, m, k; n   cin >> n >> m >> k; n   long long x, s; n   cin >> x >> s; n   pair<long long, long long> a[m]; n   for (int i = 0; i < m; i++) cin >> a[i].second; n   for (int i = 0; i < m; i++) cin >> a[i].first; n   sort(a, a + m); n   pair<long long, long long> c[k]; n   for (int i = 0; i < k; i++) cin >> c[i].first; n   for (int i = 0; i < k; i++) cin >> c[i].second; n   long long mn[m]; n   mn[0] = a[0].second; n   for (int i = 1; i < m; i++) { n     mn[i] = min(mn[i - 1], a[i].second); n   } n   long long ans = n * x; n   for (int i = 0; i < m; i++) { n     if (a[i].first <= s) ans = min(ans, n * a[i].second); n   } n   for (int i = 0; i < k; i++) { n     if (c[i].second > s) break; n     int t = c[i].first; n     int k = n         upper_bound(a, a + m, n                     pair<long long, long long>(s - c[i].second, 1LL << 60)) - n         a; n     k--; n     if (k >= 0) { n       ans = min(ans, (n - t) * min(x, mn[k])); n     } n     ans = min(ans, (n - t) * x); n   } n   ans = max(ans, 0LL); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n const int MAXN = 2e5 + 10; n using namespace std; n int T, N, cmp; n int p[MAXN], c[MAXN]; n vector<int> comp[MAXN]; n bool vis[MAXN]; n void dfs(int x) { n   vis[x] = true; n   comp[cmp].push_back(c[x]); n   if (!vis[p[x]]) dfs(p[x]); n } n int min_ans(int idx_comp) { n   int n_comp = (int)(comp[idx_comp].size()); n   vector<int> div; n   for (long long i = 1; i * i <= 1LL * n_comp; i++) n     if (n_comp % i == 0) { n       div.push_back((int)i); n       if (i * i != 1LL * n_comp) div.push_back((int)(n_comp / i)); n     } n   sort(div.begin(), div.end()); n   for (int d : div) n     for (int i = 0; i < d; i++) { n       int g = (i + d) % n_comp; n       bool found = true; n       while (g != i) { n         if (comp[idx_comp][g] != comp[idx_comp][i]) found = false; n         g = (g + d) % n_comp; n       } n       if (found) return d; n     } n } n inline void init() { n   cmp = 0; n   for (int i = 1; i < N + 1; i++) { n     vis[i] = false; n     comp[i].clear(); n   } n } n int main() { n   scanf( %d , &T); n   while (T--) { n     scanf( %d , &N); n     init(); n     for (int i = 1; i < N + 1; i++) scanf( %d , &p[i]); n     for (int i = 1; i < N + 1; i++) scanf( %d , &c[i]); n     for (int i = 1; i < N + 1; i++) n       if (!vis[i]) { n         cmp++; n         dfs(i); n       } n     int mn = N; n     for (int i = 1; i < cmp + 1; i++) mn = min(mn, min_ans(i)); n     printf( %d n , mn); n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool st[1000000]; n int main() { n   int N, K; n   cin >> N >> K; n   for (int i = 0; i < 1000000; i++) st[i] = false; n   int cnt = 0; n   for (int i = 0; i < N; i++) { n     int te1, te2; n     cin >> te1 >> te2; n     te1 += 100000, te2 += 100000; n     for (int j = te1; j <= te2; j++) { n       if (!st[j]) { n         st[j] = true; n         cnt++; n       } n     } n   } n   cout << ((cnt + K - 1) / K) * K - cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxint = -1u >> 1; n template <class T> n bool get_max(T& a, const T& b) { n   return b > a ? a = b, 1 : 0; n } n template <class T> n bool get_min(T& a, const T& b) { n   return b < a ? a = b, 1 : 0; n } n int main() { n   int f[21]; n   int a, b, c; n   cin >> a >> b >> c; n   f[0] = a; n   f[1] = b; n   for (int i = 2; i < 21; i++) f[i] = f[i - 1] + f[i - 2]; n   cout << f[c] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   int u; n   int v; n   long long cost; n   edge() {} n   edge(int u, int v, long long c) : u(u), v(v), cost(c) {} n }; n long long comb2(long long n) { return (n * (n - 1)) / 2; } n const int MAXN = 100100; n int n; n vector<int> G[MAXN]; n edge es[MAXN]; n int childNum[MAXN]; n long long usedNum[MAXN]; n int dfs(int v, int par) { n   if (childNum[v] >= 0) return childNum[v]; n   int ret = 1; n   for (int i = 0; i < (int)G[v].size(); i++) { n     int next = G[v][i]; n     if (next != par) ret += dfs(next, v); n   } n   return childNum[v] = ret; n } n int main(void) { n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     cin >> es[i].u >> es[i].v >> es[i].cost; n     G[es[i].u].push_back(es[i].v); n     G[es[i].v].push_back(es[i].u); n   } n   for (int i = 1; i <= n; i++) childNum[i] = -1; n   dfs(1, 0); n   double sum = 0; n   for (int i = 0; i < n - 1; i++) { n     int x = min(childNum[es[i].u], childNum[es[i].v]); n     usedNum[i] = 2 * (x * comb2(n - x) + (n - x) * comb2(x)); n     sum += (double)usedNum[i] * (double)es[i].cost; n   } n   int q; n   long long div = (long long)n * (long long)(n - 1) * (long long)(n - 2) / 6; n   cin >> q; n   while (q--) { n     int id; n     long long cost; n     cin >> id >> cost; n     id--; n     long long diff = es[id].cost - cost; n     es[id].cost = cost; n     sum -= (double)diff * (double)usedNum[id]; n     printf( %.10lf n , sum / (double)div); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int t = n % 4; n   int a = 0; n   for (int i = 0; i < 4; i++) { n     if ((i * 7) % 4 == t) a = i; n   } n   int b = -1, c; n   for (int j = (n - a) / 4 * 4 + a; j >= a; j -= 4) { n     if ((n - 7 * j) % 4 == 0 && (n - 7 * j >= 0)) { n       c = j; n       b = (n - 7 * j) / 4; n       break; n     } n   } n   if (b == -1) { n     cout << -1; n     return 0; n   } n   for (int i = 0; i < b; i++) cout << 4; n   for (int i = 0; i < c; i++) cout << 7; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class Solution { n  public: n   void solve(vector<pair<int, int>>& edges, vector<int>& xs, vector<int>& ys, n              int n) { n     vector<vector<int>> graph(n + 1); n     for (auto& p : edges) { n       int u = p.first, v = p.second; n       graph[u].push_back(v); n       graph[v].push_back(u); n     } n     cout <<  B   << ys[0] << endl; n     fflush(stdout); n     int root = -1; n     cin >> root; n     vector<int> dis(n + 1, INT_MAX); n     dis[root] = 0; n     queue<int> q; n     q.push(root); n     while (!q.empty()) { n       auto u = q.front(); n       q.pop(); n       int d = dis[u]; n       for (auto v : graph[u]) { n         if (dis[v] > d + 1) { n           dis[v] = d + 1; n           q.push(v); n         } n       } n     } n     int x = -1; n     for (auto v : xs) { n       if (x == -1 || dis[x] > dis[v]) { n         x = v; n       } n     } n     cout <<  A   << x << endl; n     fflush(stdout); n     int y = -1; n     cin >> y; n     bool found = false; n     for (auto v : ys) { n       if (v == y) { n         found = true; n         break; n       } n     } n     if (found) { n       cout <<  C   << x << endl; n     } else { n       cout <<  C   << -1 << endl; n     } n     fflush(stdout); n     return; n   } n }; n int main(int argc, char** argv) { n   ios::sync_with_stdio(false); n   cin.tie(0); n   Solution sol; n   int t; n   cin >> t; n   while (t-- > 0) { n     int n; n     cin >> n; n     vector<pair<int, int>> edges; n     edges.reserve(n - 1); n     for (int i = 0, u = 0, v = 0; i < n - 1; ++i) { n       cin >> u >> v; n       edges.emplace_back(u, v); n     } n     int k; n     cin >> k; n     vector<int> xs(k); n     for (int i = 0; i < k; ++i) { n       cin >> xs[i]; n     } n     cin >> k; n     vector<int> ys(k); n     for (int i = 0; i < k; ++i) { n       cin >> ys[i]; n     } n     sol.solve(edges, xs, ys, n); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000 * 1000 * 1000 + 9; n const int MAX_N = 1000 * 1000 + 10; n long long dp1[MAX_N]; n int main() { n   int n; n   scanf( %d , &n); n   n /= 2; n   if (n == 1) { n     printf( %d n , 10); n     return 0; n   } n   dp1[1] = 5; n   for (int i = 2; i <= n - 2; i++) dp1[i] = (3 + 2 * dp1[i - 1]) % MOD; n   long long dp2 = 1; n   for (int i = 0; i < (n - 2); i++) n     dp2 = (1 + dp1[n - 2 - i] * (3 + dp2)) % MOD; n   dp2 += 5; n   dp2 = 1 + dp2 * dp2; n   dp2 *= 2; n   dp2 %= MOD; n   printf( %d n , (int)dp2); n   return 0; n } n


#include <bits/stdc++.h> n int a[100100]; n int f[100100][3]; n int s[100100]; n char tmp[100100]; n int n; n void maximize(int &x, const int &y) { n   if (x < y) x = y; n } n void swap(int &a, int &b) { n   int sw; n   sw = a; n   a = b; n   b = sw; n } n void init(void) { n   scanf( %d , &n); n   int i; n   for (i = 1; i <= n; i = i + 1) scanf( %d , &a[i]); n   scanf( %s , tmp); n   for (i = 0; i < n; i = i + 1) s[i + 1] = tmp[i] - 48; n   for (i = 1; i <= n; i = i + 1) { n     if (i >= n + 1 - i) break; n     swap(a[i], a[n + 1 - i]); n     swap(s[i], s[n + 1 - i]); n   } n } n void optimize(void) { n   int i, j, k; n   memset(f, -1, sizeof f); n   f[0][0] = 0; n   for (i = 0; i < n; i = i + 1) n     for (j = 0; j < 2; j = j + 1) n       if (f[i][j] >= 0) n         for (k = 0; k < 2; k = k + 1) n           if (j || (k <= s[i + 1])) n             maximize(f[i + 1][j || (k < s[i + 1])], f[i][j] + a[i + 1] * k); n   if (f[n][0] > f[n][1]) n     printf( %d , f[n][0]); n   else n     printf( %d , f[n][1]); n } n int main(void) { n   init(); n   optimize(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long INF = 1e9 + 9; n const long long N = 500500; n char s[4][4]; n long long dot, cross, nought, first_wins, second_wins; n long long first_win_rows, first_win_col, second_win_row, second_win_col; n long long total; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   for (long long i = 1; i <= 3; i++) { n     for (long long j = 1; j <= 3; j++) { n       cin >> s[i][j]; n       if (s[i][j] ==  . ) dot++; n       if (s[i][j] ==  X ) cross++; n       if (s[i][j] ==  0 ) nought++; n     } n   } n   total += cross + nought; n   if (!(cross == nought || cross == nought + 1)) { n     cout <<  illegal ; n     return 0; n   } n   for (long long i = 1; i <= 3; i++) { n     set<char> st; n     for (long long j = 1; j <= 3; j++) { n       st.insert(s[i][j]); n     } n     if (st.size() == 1 && *st.begin() ==  X ) { n       first_wins++; n     } n     if (st.size() == 1 && *st.begin() ==  0 ) { n       second_wins++; n     } n   } n   for (long long j = 1; j <= 3; j++) { n     set<char> st; n     for (long long i = 1; i <= 3; i++) { n       st.insert(s[i][j]); n     } n     if (st.size() == 1 && *st.begin() ==  X ) { n       first_wins++; n     } n     if (st.size() == 1 && *st.begin() ==  0 ) { n       second_wins++; n     } n   } n   set<char> st; n   st.insert(s[1][1]); n   st.insert(s[2][2]); n   st.insert(s[3][3]); n   if (st.size() == 1 && *st.begin() ==  X ) { n     first_wins++; n   } n   if (st.size() == 1 && *st.begin() ==  0 ) { n     second_wins++; n   } n   st.clear(); n   st.insert(s[1][3]); n   st.insert(s[2][2]); n   st.insert(s[3][1]); n   if (st.size() == 1 && *st.begin() ==  X ) { n     first_wins++; n   } n   if (st.size() == 1 && *st.begin() ==  0 ) { n     second_wins++; n   } n   if (first_wins > 0 && second_wins > 0) { n     cout <<  illegal ; n     return 0; n   } n   if (first_wins > 0 || second_wins > 0) { n     if (first_wins == 1 && total & 1) { n       cout <<  the first player won ; n     } else if (second_wins == 1 && total % 2 == 0) { n       cout <<  the second player won ; n     } else if (first_wins == 2 && total & 1) { n       if (first_win_col == 2 || first_win_rows == 2) { n         cout <<  illegal ; n       } else { n         cout <<  the first player won ; n       } n     } else if (second_wins == 2 && total % 2 == 0) { n       if (second_win_col == 2 || second_win_row == 2) { n         cout <<  illegal ; n       } else { n         cout <<  the second player won ; n       } n     } else { n       cout <<  illegal ; n     } n     return 0; n   } n   if (dot > 0) { n     if (cross == nought) n       cout <<  first ; n     else n       cout <<  second ; n   } else { n     cout <<  draw ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int mx = 0, sum = 0; n int main() { n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     int a; n     cin >> a; n     mx = max(mx, a); n     sum += a; n   } n   sum *= 2; n   sum += n; n   sum /= n; n   cout << max(sum, mx) << endl; n   return 0; n } n


/*    Author : Prakhar Rai    */ n #include<bits/stdc++.h> n #define ll long long n #define ld long double n #define LB(x,num) lower_bound(x.begin(),x.end(),num) - x.begin() n #define UB(x,num) upper_bound(x.begin(),x.end(),num) - x.begin() n #define BS(x,num) binary_search(x.begin(),x.end(),num) n #define pb push_back n #define mp make_pair n #define fs first n #define sc second n #define vci vector<int> n #define vcll vector<ll> n #define vcd vector<long double> n #define line(x) sort(x.begin(),x.end()) n #define all(x) x.begin(),x.end() n #define newl   n  n #define vc vector n #define loop(i,a,b) for(int i = a; i < b; i++) n #define lol(i,a,b) for(ll i = a; i < b; i++) n #define lod(i,a,b) for(ld i = 0; i < b; i++) n #define mod 1000000007 n #define read(v,n) lol(i,0,n) {ll x; cin >> x; v.pb(x);} n #define run(a,x) for(auto x : a) n #define yes  YES  n #define no  NO  n ll cnt; n using namespace std; n // ll dp[10000000]; n // memset(dp, -1, sizeof(dp)); n  n void solve() { n  ll n; cin >> n; n  vcll v; n  read(v, n + 2); n  sort(all(v)); n  ll sum = 0; n  n  lol(i, 0, n) { n   sum += v[i]; n  } n  n  if (sum == v[n]) { n   lol(i, 0, n) { n    cout << v[i] <<    ; n   } n   cout << newl; n   return; n  } n  n  sum += v[n]; n  n  lol(i, 0, n + 1) { n   if (sum - v[i] == v[n + 1]) { n    lol(j, 0, n + 1) { n     if (i == j) { n      continue; n     } else { n      cout << v[j] <<    ; n     } n    } n    cout << newl; n    return; n   } n  } n  n  cout << -1 << newl; n  return; n } n  n // FILE WITH TEST CASE n  n void init() { n #ifndef ONLINE_JUDGGE n  freopen( input.txt ,  r , stdin); n  freopen( output.txt ,  w , stdout); n #endif n } n  n int main() { n //  init(); n  ios_base::sync_with_stdio(false); cin.tie(0); n  int t; cin >> t; n  while (t--) n   solve(); n } n /* This is my journey and i shall endure the work to reach the top .*/


#include <bits/stdc++.h> n using namespace std; n int play(int n) { n   int x = 1; n   for (;;) { n     if (x * 2 > n) n       break; n     else n       x *= 2; n   } n   return x / 2; n } n int main() { n   int n, b, p, k, x = 1, btl = 0, twl = 0; n   scanf( %d %d %d , &n, &b, &p); n   twl = n * p; n   while (1) { n     int pl = play(n); n     btl += (2 * b + 1) * pl; n     n = pl + n - pl * 2; n     if (n == 1) break; n   } n   cout << btl <<     << twl << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int h, w; n   cin >> h >> w; n   string line; n   char table[h + 2][w + 2]; n   memset(table,  0 , sizeof table); n   for (int i = 1; i <= h; i++) { n     cin >> line; n     line =     + line; n     for (int j = 1; j <= w; j++) { n       table[i][j] = line[j]; n     } n   } n   int dph[h + 3][w + 3]; n   int dpv[h + 3][w + 3]; n   int dp[h + 3][w + 3]; n   int c = 0; n   memset(dp, 0, sizeof dp); n   memset(dpv, 0, sizeof dpv); n   memset(dph, 0, sizeof dph); n   for (int y = 1; y <= h; y++) { n     for (int x = 1; x <= w; x++) { n       c = 0; n       if (table[y][x] ==  . ) { n         if (table[y + 1][x] ==  .  && y != h) { n           c += 1; n           dpv[y][x] = 1; n         } n         if (table[y][x + 1] ==  .  && x != w) { n           dph[y][x] = 1; n           c += 1; n         } n       } n       dp[y][x] = dp[y][x - 1] + c; n     } n   } n   int tt; n   cin >> tt; n   while (tt--) { n     int y1, x1, y2, x2, x, y, ans = 0; n     cin >> y1 >> x1 >> y2 >> x2; n     for (int y = y1; y < y2; y++) { n       ans += dp[y][x2 - 1] - dp[y][x1 - 1]; n     } n     for (int x = x1; x < x2; x++) { n       ans += dph[y2][x]; n     } n     for (int y = y1; y < y2; y++) { n       ans += dpv[y][x2]; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename _T, typename _T2> n string to_string(pair<_T, _T2> x) { n   return  <  + to_string(x.first) +  ,   + to_string(x.second) +  > ; n } n template <typename _T> n string to_string(vector<_T> vec) { n   string s =  [  ; n   for (auto el : vec) s += to_string(el) +  ,  ; n   s[s.size() - 1] =  ] ; n   s[s.size() - 2] =    ; n   return s; n } n template <typename _T> n string to_string(set<_T> vec) { n   string s =  {  ; n   for (auto el : vec) s += to_string(el) +  ,  ; n   s[s.size() - 1] =  } ; n   s[s.size() - 2] =    ; n   return s; n } n template <typename _T, typename _T2> n string to_string(map<_T, _T2> vec) { n   string s =  {  ; n   for (auto el : vec) n     s +=  mp[  + to_string(el.first) +  ]=   + to_string(el.second) +  ,  ; n   s[s.size() - 1] =  } ; n   s[s.size() - 2] =    ; n   return s; n } n template <typename _T> n inline void _DBG(const char *s, _T x) { n   cerr << s <<   =   << to_string(x) <<   n ; n } n template <typename _T, typename... args> n void _DBG(const char *s, _T x, args... a) { n   while (*s !=  , ) cerr << *s++; n   cerr <<   =   << to_string(x) <<   n ; n   _DBG(s + 1, a...); n } n mt19937 rng___(chrono::steady_clock::now().time_since_epoch().count()); n const long long MOD = 998244353; n const long long LLINF = 1e18 + 7; n long long d, m; n long long powe(long long n, long long k) { n   if (k == 0) return 1; n   long long pom = powe(n, k / 2); n   pom = (pom * pom) % m; n   if (k & 1) pom = (pom * n) % m; n   return pom; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   ; n   int t; n   cin >> t; n   while (t--) { n     cin >> d >> m; n     long long dp[32][32]; n     for (int i = 0; i < 32; i++) n       for (int j = 0; j < 32; j++) dp[i][j] = 0; n     dp[0][0] = 1; n     long long ans = 0; n     for (int i = 1; i < 32; i++) { n       for (int j = 1; j < 32; j++) { n         int b = j - 1; n         if ((1 << b) > d) break; n         long long summ = 0; n         for (int k = 0; k < j; k++) { n           summ += dp[i - 1][k]; n         } n         summ = summ % m; n         dp[i][j] = (summ * 1LL * min(d - (1 << b) + 1, 1LL << b)) % m; n         ans = (ans + dp[i][j]) % m; n       } n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   long long s[100005]{0}; n   pair<long long, long long> p[100005]; n   for (int i = 0; i < m; i++) { n     int u, v; n     long long d; n     cin >> u >> v >> d; n     u--; n     v--; n     s[u] -= d; n     s[v] += d; n   } n   for (int i = 0; i < n; i++) p[i] = pair<long long, long long>(s[i], i); n   sort(p, p + n); n   vector<int> u, v; n   vector<long long> d; n   int i = 0; n   int j = n - 1; n   while (i < j) { n     if (p[i].first + p[j].first < 0) { n       u.push_back(p[i].second); n       v.push_back(p[j].second); n       d.push_back(p[j].first); n       p[i].first += p[j].first; n       p[j].first = 0; n       j--; n     } else if (p[i].first + p[j].first > 0) { n       u.push_back(p[i].second); n       v.push_back(p[j].second); n       d.push_back(-p[i].first); n       p[j].first += p[i].first; n       p[i].first = 0; n       i++; n     } else { n       if (p[i].first == 0) { n         i++; n         j--; n         continue; n       } n       u.push_back(p[i].second); n       v.push_back(p[j].second); n       d.push_back(p[j].first); n       p[i].first = 0; n       p[j].first = 0; n       i++; n       j--; n     } n   } n   int l = u.size(); n   cout << l << endl; n   for (int i = 0; i < l; i++) n     cout << u[i] + 1 <<     << v[i] + 1 <<     << d[i] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline void read(int &res) { n   register char ch = getchar(); n   res = 0; n   register int g = 1; n   while (!isdigit(ch)) { n     if (ch ==  - ) g = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     res = (res << 3) + (res << 1) + (ch ^ 48); n     ch = getchar(); n   } n   res *= g; n } n priority_queue<int> q; n int n, m, a[202000], ans[202000], sum = 0, t = 0; n int main() { n   read(n); n   read(m); n   for (register int i = 1; i <= n; i++) { n     read(a[i]); n   } n   for (register int i = 1; i <= n; i++) { n     t = 0; n     sum = 0; n     while (!q.empty()) q.pop(); n     for (register int j = 1; j <= i - 1; j++) { n       t += a[j]; n       q.push(a[j]); n     } n     while (t + a[i] > m) { n       t -= q.top(); n       q.pop(); n       sum++; n     } n     printf( %d  , sum); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, i; n long long r, c = 1000000000000000000LL; n void solve(int n, long long x) { n   for (int i = 1; i * i <= n; i++) n     if (!(n % i)) { n       r = max(r, (i + 2LL) * (n / i + 2LL) * x); n       c = min(c, (i + 2LL) * (n / i + 2LL) * x); n     } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i * i <= n; i++) n     if ((n % i) == 0) { n       solve(i, n / i + 1LL); n       solve(n / i, i + 1LL); n     } n   printf( %I64d %I64d n , c - n, r - n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long i, t, n, m, c, d, a[100005]; n   cin >> n >> m; n   if (n == 1 && m == 0) { n     cout << 10000000; n     return 0; n   } n   if (m < n / 2 || n < 2) { n     cout << -1; n     return 0; n   } n   c = 1000000000LL; n   for (int i = n - 1; i > 1; i--) { n     a[i] = c--; n   } n   int tem; n   if (n % 2) n     m = m - (n - 3) / 2; n   else n     m = m - (n - 2) / 2; n   a[1] = m; n   a[0] = m * 2; n   for (__typeof(0) i = 0; i <= n - 1; i++) cout << a[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, string> p; n int ans = 0; n string tmp =  abcdef ; n void doit(int n, string s) { n   if (n == 0) { n     int f = 0; n     while (true) { n       if (s.length() == 2) { n         if (p.find(s) == p.end()) { n           f = 1; n           break; n         } n         if (p[s] !=  a ) { n           f = 1; n         } n         break; n       } n       string s1 = s.substr(0, 2); n       string s2 =   ; n       if (s.length() > 2) { n         s2 += s.substr(2, s.length() - 2); n       } n       if (p.find(s1) == p.end()) { n         f = 1; n         break; n       } else { n         s = p[s1] + s2; n       } n     } n     if (f == 0) ans++; n     return; n   } n   for (int i = 0; i < 6; i++) { n     string tmp1 = s; n     tmp1.push_back(tmp[i]); n     doit(n - 1, tmp1); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, q; n   cin >> n >> q; n   for (int i = 0; i < q; i++) { n     string s1, s2; n     cin >> s1 >> s2; n     p.insert(make_pair(s1, s2)); n   } n   for (int i = 0; i < 6; i++) { n     string s; n     s.push_back(tmp[i]); n     doit(n - 1, s); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long const Mod = 1000000007; n unsigned long long sq_sum(unsigned long long n) { n   unsigned long long ret = n; n   ret = (n * (n + 1)); n   ret /= 2; n   if (ret % 3 == 0) { n     return ((ret / 3) % Mod * (2 * n + 1)) % Mod; n   } else { n     return ((ret % Mod) * ((2 * n + 1) / 3)) % Mod; n   } n } n unsigned long long check(unsigned long long a, unsigned long long b) { n   unsigned long long ret = 0; n   for (unsigned long long x = b; x < 100000; ++x) { n     if (x % b != 0) { n       unsigned long long t1 = x / b; n       unsigned long long t2 = x % b; n       if (t1 % t2 == 0 && t1 / t2 <= a) ret += x; n     } n   } n   return ret; n } n int main() { n   unsigned long long a, b; n   while (cin >> a >> b) { n     cout << (((a * (a + 1) / 2) % Mod * b + a) % Mod * n              ((b * (b - 1) / 2) % Mod)) % n                 Mod n          << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long segs[] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6}; n int n, m, k; n long long a[200005]; n vector<pair<int, int> > q; n int res[200005]; n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < m; i++) { n     int r, t; n     cin >> r >> t; n     if (q.empty()) { n       q.push_back(make_pair(r, t - 1)); n     } else { n       while (!q.empty() && t - 1 >= q.back().second) { n         q.pop_back(); n       } n       q.push_back(make_pair(r, t - 1)); n     } n   } n   q.push_back(make_pair(1, 0)); n   int l = 0, r = n - 1, ind = n - 1; n   for (int i = 0; i < q.size(); i++) { n     if (i == q.size() - 1) { n       res[ind] = a[l]; n       break; n     } n     if (!i) { n       while (r > q[0].second) { n         res[r] = a[r]; n         r--; n         ind--; n       } n       sort(a, a + r + 1); n     } n     if (q[i].first == 1) { n       while (ind > q[i + 1].second) { n         res[ind] = a[r]; n         ind--; n         r--; n       } n     } else { n       while (ind > q[i + 1].second) { n         res[ind] = a[l]; n         ind--; n         l++; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     if (i) cout <<    ; n     cout << res[i]; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b, c; n long long exgcd(long long a, long long b, long long& x, long long& y) { n   if (b == 0) { n     x = 1; n     y = 0; n     return a; n   } n   long long d = exgcd(b, a % b, x, y); n   long long z = x; n   x = y; n   y = z - y * (a / b); n   return d; n } n int main(int argc, char const* argv[]) { n   cin >> a >> b >> c; n   long long x, y; n   long long d = exgcd(a, b, x, y); n   if (c % d) { n     cout << -1 << endl; n   } else { n     x = -c / d * x; n     y = -c / d * y; n     cout << x <<     << y << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long t, cn, n, nr, p10; n int main() { n   cin >> t; n   while (t--) { n     cin >> n; n     cn = n; n     nr = 0; n     p10 = 1; n     while (cn) { n       if (cn % 10) nr++; n       cn /= 10; n     } n     cout << nr <<   n ; n     while (n) { n       if (n % 10) cout << n % 10 * p10 <<    ; n       n /= 10; n       p10 *= 10; n     } n     cout <<   n ; 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   int n, amazing = 0, maxx, minn, k; n   cin >> n; n   cin >> k; n   maxx = k; n   minn = k; n   for (int i = 1; i < n; i++) { n     cin >> k; n     if (k > maxx) { n       maxx = k; n       amazing++; n     } else if (k < minn) { n       minn = k; n       amazing++; n     } n   } n   cout << amazing << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = INT_MAX; n const int N = 2 * 1e3 + 10; n const long double eps = 1e-9; n const int mod = 998244353; n char a[N][N]; n int d[N][N], v[N][N], n, m; n bool used[N][N]; n void nxt(char c, int &x, int &y) { n   if (c ==  U ) n     --x; n   else if (c ==  D ) n     ++x; n   else if (c ==  R ) n     ++y; n   else n     --y; n   return; n } n int cx, cy; n void fillcycle() { n   int x = cx, y = cy, cnt = 1; n   nxt(a[x][y], x, y); n   while (!(x == cx && y == cy)) { n     ++cnt; n     nxt(a[x][y], x, y); n   } n   x = cx, y = cy; n   d[x][y] = cnt; n   v[x][y] = 2; n   nxt(a[x][y], x, y); n   while (!(x == cx && y == cy)) { n     v[x][y] = 2; n     d[x][y] = cnt; n     nxt(a[x][y], x, y); n   } n   return; n } n void cycle(int x, int y) { n   while (!(x <= 0 || y <= 0 || x > n || y > m) && !used[x][y]) { n     v[x][y] = 0; n     used[x][y] = true; n     nxt(a[x][y], x, y); n   } n   if (x <= 0 || y <= 0 || x > n || y > m) return; n   if (v[x][y] != 0) return; n   cx = x; n   cy = y; n   fillcycle(); n   return; n } n void gg(int x, int y) { n   while (x > 0 && y > 0 && x <= n && y <= m && v[x][y] == 0) { n     v[x][y] = 1; n     nxt(a[x][y], x, y); n   } n   return; n } n void dfs(int x, int y) { n   int tx = x, ty = y; n   nxt(a[x][y], tx, ty); n   stack<pair<int, int> > vc; n   vc.push({x, y}); n   int c = 1; n   while (!(tx <= 0 || ty <= 0 || x > n || y > m) && d[tx][ty] == -1) { n     vc.push({tx, ty}); n     nxt(a[tx][ty], tx, ty); n   } n   if (tx <= 0 || ty <= 0 || x > n || y > m) n     c = 1; n   else n     c = d[tx][ty] + 1; n   while (!vc.empty()) { n     tx = vc.top().first; n     ty = vc.top().second; n     vc.pop(); n     d[tx][ty] = c; n     ++c; n   } n   return; n } n void solve() { n   cin >> n >> m; n   for (int i = 0; i <= n + 1; ++i) { n     fill(d[i], d[i] + m + 1, -1); n     fill(used[i], used[i] + m + 1, false); n     fill(v[i], v[i] + m + 1, -1); n     if (i > 0 && i <= n) { n       for (int j = 1; j <= m; ++j) cin >> a[i][j]; n     } n   } n   for (int i = 0; i <= n + 1; ++i) d[i][0] = d[i][m + 1] = 0; n   for (int i = 0; i <= m + 1; ++i) d[0][i] = d[n + 1][i] = 0; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       if (!used[i][j]) { n         cycle(i, j); n         gg(i, j); n       } n     } n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       if (d[i][j] == -1) dfs(i, j); n     } n   } n   int x = 1, y = 1, ans = -2; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       if (ans < d[i][j]) ans = d[i][j], x = i, y = j; n     } n   } n   cout << x <<     << y <<     << ans <<   n ; n   return; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[300005]; n string s, p; n vector<long long> v; n int main() { n   ios_base::sync_with_stdio(false); n   long long i, j, k, l, m, x, y, r, n, t; n   cin >> n >> m; n   for (i = 1; i <= n; i++) { n     cin >> x; n     if (x < 300005) a[x] = 1; n   } n   long long msd = 0; n   long long cnt = 0; n   for (i = 1; i <= 300000; i++) { n     if (msd >= m) { n       break; n     } n     if (a[i] == 0) { n       if ((msd + i) <= m) { n         cnt++; n         v.push_back(i); n         msd += i; n       } n     } n   } n   cout << cnt << endl; n   for (i = 0; i < v.size(); i++) { n     cout << v[i] <<    ; n   } n   return 0; n } n