#include <bits/stdc++.h> n using namespace std; n long long int a1, b1, a2, b2; n int main() { n   scanf( %lld %lld , &a1, &b1); n   scanf( %lld %lld , &a2, &b2); n   long long int p, q, ans = 0; n   p = (a1 * b1); n   q = (a2 * b2); n   long long int tp = 0, tq = 0, thp = 0, thq = 0; n   while ((p % 3) == 0) { n     p = (p / 3); n     thp++; n   } n   while ((q % 3) == 0) { n     q = (q / 3); n     thq++; n   } n   while ((p % 2) == 0) { n     p = (p / 2); n     tp++; n   } n   while ((q % 2) == 0) { n     q = (q / 2); n     tq++; n   } n   p = (a1 * b1); n   q = (a2 * b2); n   long long int x; n   if (thp > thq) { n     tp = tp + (thp - thq); n     ans = ans + (thp - thq); n     x = (thp - thq); n     while (x--) { n       if ((a1 % 3) == 0) n         a1 = ((a1 / 3) * 2); n       else n         b1 = ((b1 / 3) * 2); n     } n   } else if (thq > thp) { n     tq = tq + (thq - thp); n     ans = ans + (thq - thp); n     x = (thq - thp); n     while (x--) { n       if ((a2 % 3) == 0) n         a2 = ((a2 / 3) * 2); n       else n         b2 = ((b2 / 3) * 2); n     } n   } n   if (tp > tq) { n     ans = ans + (tp - tq); n     x = (tp - tq); n     while (x--) { n       if ((a1 % 2) == 0) n         a1 = (a1 / 2); n       else n         b1 = (b1 / 2); n     } n   } else { n     ans = ans + (tq - tp); n     x = (tq - tp); n     while (x--) { n       if ((a2 % 2) == 0) n         a2 = (a2 / 2); n       else n         b2 = (b2 / 2); n     } n   } n   if ((a1 * b1) == (a2 * b2)) { n     printf( %lld n , ans); n     printf( %lld %lld n , a1, b1); n     printf( %lld %lld n , a2, b2); n   } else n     printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n int t, k, l[1005], r[1005], lenl, lenr; n long long f[1005][1005][2]; n long long dfs(int *x, int Index, int d, int q, int lim) { n   if (Index < 1) return (long long)q; n   if (!lim && f[Index][d][q] != -1) return f[Index][d][q]; n   long long res = 0; n   int up = lim ? x[Index] : 9; n   for (int i = 0; i <= up; i++) { n     if (i != 4 && i != 7) n       res = (res + dfs(x, Index - 1, max(d - 1, 0), q, lim && i == up)) % mod; n     else n       res = (res + dfs(x, Index - 1, k, q || d, lim && i == up)) % mod; n   } n   if (!lim) f[Index][d][q] = res; n   return res; n } n long long solve(int *x, int len) { return dfs(x, len, 0, 0, 1); } n int main() { n   scanf( %d %d , &t, &k); n   memset(f, -1, sizeof f); n   while (t--) { n     char now[1005]; n     scanf( %s , now + 1); n     lenl = strlen(now + 1); n     for (int i = 1; i <= lenl; i++) l[lenl - i + 1] = now[i] - 48; n     scanf( %s , now + 1); n     lenr = strlen(now + 1); n     for (int i = 1; i <= lenr; i++) r[lenr - i + 1] = now[i] - 48; n     l[1]--; n     int Index = 1; n     while (l[Index] < 0) { n       l[Index] = 9; n       l[Index + 1]--; n       Index++; n     } n     if (Index == lenl && !l[Index]) lenl--; n     printf( %lld n , (solve(r, lenr) - solve(l, lenl) + mod) % mod); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   long long ans = 0; n   for (int i = (0); i < (n); ++i) { n     long long t, T, x, cost; n     cin >> t >> T >> x >> cost; n     if (t >= T) { n       ans += cost + m * x; n       continue; n     } n     long long aux1 = cost; n     if (m > (T - t)) aux1 += m * x; n     long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1; n     aux2 *= cost; n     ans += min(aux1, aux2); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<pair<int, int>, int> a[100001]; n pair<int, int> ans[100001]; n int n, m, i, j, k, l, r, t, last, b[100001], x, y; n map<int, pair<int, int> > mp; n int main() { n   cin >> n >> m; n   for (i = 0; i < m; i++) { n     cin >> a[i].first.first >> a[i].first.second; n     a[i].first.second = 1 - a[i].first.second; n     b[i] = a[i].first.first; n     a[i].second = i; n   } n   sort(a, a + m); n   if (a[0].first.second || a[1].first.second) { n     cout << -1; n     return 0; n   } n   k = 3; n   for (i = 2; i < m; i++) { n     if (a[i].first.second) { n       if (t == (k - 2) * (k - 1) / 2) { n         cout << -1; n         return 0; n       } n       t++; n       continue; n     } n     k++; n   } n   t = 2; n   for (i = 0; i < m; i++) { n     if (!a[i].first.second) { n       ans[a[i].second].first = t - 1; n       ans[a[i].second].second = t; n       t++; n     } n   } n   x = 3; n   y = 1; n   for (i = 0; i < m; i++) { n     if (!a[i].first.second) continue; n     ans[a[i].second].first = y; n     ans[a[i].second].second = x; n     y++; n     if (x == y + 1) { n       x++; n       y = 1; n     } n   } n   for (i = 0; i < m; i++) { n     cout << ans[i].first <<     << ans[i].second << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int inv = 1000000000; n const int minv = -inv; n const int max_n = 500000 + 5; n const int max_cn2 = max_n / 2 + 5; n int n, cn2, fn2; n int a[max_cn2]; n int x; n long long q[max_cn2]; n int main() { n   scanf( %d , &n); n   cn2 = (n + 1) / 2; n   fn2 = n / 2; n   long long suma = 0ll; n   for (int i = 0; i < cn2; ++i) { n     scanf( %d , &a[i]); n     suma += ((long long)(a[i])); n   } n   scanf( %d , &x); n   suma += ((long long)(fn2)) * ((long long)(x)); n   if (suma > 0ll) { n     printf( %d n , n); n     return 0; n   } n   if (x >= 0) { n     printf( %d n , -1); n     return 0; n   } n   q[0] = 0ll; n   for (int i = 1; i < n - fn2; ++i) n     q[i] = min(q[i - 1] + ((long long)(a[i - 1] - x)), 0ll); n   long long rsum = ((long long)(fn2)) * ((long long)(x)); n   for (int k = fn2 + 1; k < n; ++k) { n     rsum += ((long long)(a[n - k])); n     if (rsum + q[n - k] > 0ll) { n       printf( %d n , k); n       return 0; n     } n   } n   printf( %d n , -1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000 * 1000 * 1000 + 7; n const long long LINF = 1LL * INF * INF; n const int MAX = 1e5 + 7; n const double PI = acos(-1.); n const double EPS = 1e-7; n const int MOD = 998244353; n const int MAXF = 1000000000; n int dx[] = {1, -1, 0, 0}; n int dy[] = {0, 0, -1, 1}; n bool isInField(int x, int y) { n   return 1 <= x && x <= MAXF && 1 <= y && y <= MAXF; n } n bool isWhite(int x, int y) { return x % 2 == y % 2; } n void solve() { n   int black, white; n   cin >> black >> white; n   map<pair<int, int>, int> was; n   vector<pair<int, int> > ans; n   int sX = 200000, sY = 200000; n   if (black > white) { n     sX++; n     --black; n   } else { n     --white; n   } n   ans.emplace_back(sX, sY); n   was[{sX, sY}] = 1; n   while (black > 0 && white > 0) { n     --black; n     --white; n     ++sX; n     ans.emplace_back(sX, sY); n     was[{sX, sY}] = 1; n     ++sX; n     ans.emplace_back(sX, sY); n     was[{sX, sY}] = 1; n   } n   for (int i = 0; i < (((int)ans.size())); ++i) { n     int x = ans[i].first; n     int y = ans[i].second; n     for (int j = 0; j < (4); ++j) { n       int a = x + dx[j]; n       int b = y + dy[j]; n       if (black > 0) { n         if (!was.count({a, b}) && !isWhite(a, b)) { n           --black; n           ans.emplace_back(a, b); n           was[{a, b}] = 1; n         } n       } else if (white > 0) { n         if (!was.count({a, b}) && isWhite(a, b)) { n           --white; n           ans.emplace_back(a, b); n           was[{a, b}] = 1; n         } n       } n     } n   } n   if (black != 0 || white != 0) { n     cout <<  NO n ; n     return; n   } n   cout <<  YES n ; n   for (auto [x, y] : ans) { n     cout << x <<     << y << endl; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   int q; n   cin >> q; n   while (q--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef long long ll; n #define mod 1000000007 n  n int main(){ n     int n;cin>>n; n     int summ = 0; n     while(n>9){ n         int summ = 0; n         while(n>9){ n             summ = summ + n%10; n             n = n/10; n         } n         summ = summ + n; n         n = summ; n     } n     cout<<n; n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   double d, L, v1, v2; n   cin >> d >> L >> v1 >> v2; n   double distance; n   int i, r; n   cout << fixed << setprecision(20) << (L - d) / (v1 + v2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int key; n long long dis[1000]; n struct pt { n   long long x, y, z; n } f[8], point[8]; n long long meow(int a, int b) { n   long long ta, tb, tc; n   ta = f[a].x - f[b].x; n   tb = f[a].y - f[b].y; n   tc = f[a].z - f[b].z; n   return (ta * ta + tb * tb + tc * tc); n } n int chk() { n   int i, j, cnta, cntb, cntc, k; n   long long dis, sq2, sq3, ik; n   for (i = 0; i < 8; i++) { n     for (j = 0; j < 8; j++) { n       if (i != j && f[i].x == f[j].x && f[i].y == f[j].y && f[i].z == f[j].z) n         return false; n     } n   } n   for (i = 0; i < 8; i++) { n     for (j = 0; j < 8; j++) { n       dis = meow(i, j); n       cnta = cntb = cntc = 0; n       for (k = 0; k < 8; k++) { n         ik = meow(i, k); n         if (ik == dis) cnta++; n         if (ik == dis * 2) cntb++; n         if (ik == dis * 3) cntc++; n       } n       if (cnta == 3 && cntb == 3 && cntc == 1) break; n     } n     if (j == 8) return false; n   } n   return true; n } n void go(int r) { n   if (r == 8) { n     if (chk()) { n       key = 1; n       cout <<  YES  << endl; n       for (int i = 0; i < 8; i++) n         cout << f[i].x <<     << f[i].y <<     << f[i].z << endl; n     } n     return; n   } n   f[r] = point[r]; n   go(r + 1); n   if (key) return; n   swap(f[r].x, f[r].y); n   go(r + 1); n   if (key) return; n   swap(f[r].y, f[r].z); n   go(r + 1); n   if (key) return; n   swap(f[r].x, f[r].y); n   go(r + 1); n   if (key) return; n   swap(f[r].y, f[r].z); n   go(r + 1); n   if (key) return; n   swap(f[r].x, f[r].y); n   go(r + 1); n   return; n } n int main() { n   int i; n   for (i = 0; i < 8; i++) cin >> point[i].x >> point[i].y >> point[i].z; n   key = 0; n   f[0] = point[0]; n   go(1); n   if (!key) cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &x) { n   x = 0; n   char c; n   for (c = getchar(); !isdigit(c); c = getchar()) n     ; n   for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ; n } n const int N = 255, P = 998244353, oo = 0x3f3f3f3f; n int mul(int a, int b) { return 1ll * a * b % P; } n int add(int a, int b) { n   a += b; n   return a >= P ? a - P : a; n } n int sub(int a, int b) { n   a -= b; n   return a < 0 ? a + P : a; n } n int gcd(int a, int b) { return !b ? a : gcd(b, a % b); } n namespace Dinic { n const int S = N * N; n struct Edge { n   int d, cap, nxt; n } es[S << 2]; n int vn, g[S], lev[S], itr[S], q[S], h, t, en = 1; n void add(int s, int d, int cap) { n   es[++en] = (Edge){d, cap, g[s]}, g[s] = en; n   es[++en] = (Edge){s, 0, g[d]}, g[d] = en; n } n void bfs(int s) { n   int v; n   memset(lev, -1, sizeof(lev)); n   h = t = 0; n   q[t++] = s; n   lev[s] = 0; n   while (h < t) { n     v = q[h++]; n     for (int i = g[v], d = es[i].d; i; i = es[i].nxt, d = es[i].d) { n       if (es[i].cap > 0 && lev[d] < 0) { n         lev[d] = lev[v] + 1; n         q[t++] = es[i].d; n       } n     } n   } n } n int dfs(int v, int t, int f) { n   if (v == t) return f; n   for (int &i = itr[v]; i; i = es[i].nxt) { n     if (es[i].cap <= 0 || lev[es[i].d] <= lev[v]) continue; n     int a = dfs(es[i].d, t, min(es[i].cap, f)); n     if (a <= 0) continue; n     es[i].cap -= a; n     es[i ^ 1].cap += a; n     return a; n   } n   return 0; n } n int sol(int s, int t) { n   int i, res = 0, a; n   while (true) { n     bfs(s); n     if (lev[t] < 0) break; n     for (i = 0; i <= vn; ++i) itr[i] = g[i]; n     for (a = dfs(s, t, oo); a > 0; a = dfs(s, t, oo)) { n       res += a; n     } n   } n   return res; n } n }  // namespace Dinic n using namespace Dinic; n int n, m; n vector<int> xx, yy; n struct Rec { n   int x1, y1, x2, y2; n } rec[N]; n int f(vector<int> &a, int x) { n   return lower_bound(a.begin(), a.end(), x) - a.begin() + 1; n } n int main(int argc, char *argv[]) { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= (m); ++i) { n     int x1, y1, x2, y2; n     scanf( %d%d%d%d , &x1, &y1, &x2, &y2); n     --x1, --y1; n     xx.push_back(x1), xx.push_back(x2); n     yy.push_back(y1), yy.push_back(y2); n     rec[i] = (Rec){x1, y1, x2, y2}; n   } n   xx.push_back(0), yy.push_back(0); n   sort(xx.begin(), xx.end()), sort(yy.begin(), yy.end()); n   xx.erase(unique(xx.begin(), xx.end()), xx.end()); n   yy.erase(unique(yy.begin(), yy.end()), yy.end()); n   int sx = xx.size(), sxy = sx + yy.size(), src = sxy + m + 1, snk = src + 1; n   vn = snk; n   for (int i = 1; i < (xx.size()); ++i) { n     add(src, i + 1, xx[i] - xx[i - 1]); n   } n   for (int i = 1; i < (yy.size()); ++i) { n     add(sx + i + 1, snk, yy[i] - yy[i - 1]); n   } n   for (int i = 1; i <= (m); ++i) { n     int x1 = rec[i].x1, x2 = rec[i].x2, y1 = rec[i].y1, y2 = rec[i].y2; n     x1 = f(xx, x1); n     x2 = f(xx, x2); n     y1 = f(yy, y1); n     y2 = f(yy, y2); n     for (int j = x1 + 1; j <= x2; ++j) add(j, sxy + i, oo); n     for (int j = y1 + 1; j <= y2; ++j) add(sxy + i, sx + j, oo); n   } n   cout << sol(src, snk) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n const int MX = 3001; n map<pair<int, int>, bool> forbidden[MX]; n struct Graph { n   int d[MX][MX]; n   int parent[MX][MX]; n   vector<int> adj[MX]; n   void addEdge(int u, int v) { n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   vector<int> bfs(int s, int n) { n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < n; j++) { n         d[i][j] = INF; n         parent[i][j] = -1; n       } n     } n     d[n][s] = 0; n     deque<pair<int, int>> q; n     q.push_back({n, s}); n     while (!q.empty()) { n       int a = q.front().first; n       int b = q.front().second; n       q.pop_front(); n       for (int c : adj[b]) { n         if (d[b][c] == INF && !forbidden[a][{b, c}]) { n           d[b][c] = d[a][b] + 1; n           parent[b][c] = a; n           q.push_back({b, c}); n         } n       } n     } n     int minDistance = INF; n     for (int i = 0; i < n; i++) { n       minDistance = min(minDistance, d[i][n - 1]); n     } n     if (minDistance == INF) return {}; n     int prev = -1; n     for (int i = 0; i < n; i++) { n       if (d[i][n - 1] == minDistance) { n         prev = i; n         break; n       } n     } n     vector<int> path = {n}; n     int cur = n - 1; n     while (prev != n) { n       path.push_back(prev + 1); n       int aux = prev; n       prev = parent[prev][cur]; n       cur = aux; n     } n     reverse(path.begin(), path.end()); n     return path; n   } n } G; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, m, k; n   cin >> n >> m >> k; n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     u--; n     v--; n     G.addEdge(u, v); n   } n   for (int i = 0; i < k; i++) { n     int a, b, c; n     cin >> a >> b >> c; n     a--; n     b--; n     c--; n     forbidden[a][{b, c}] = true; n   } n   vector<int> path = G.bfs(0, n); n   cout << (int)path.size() - 1 <<   n ; n   for (int u : path) cout << u <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cout.tie(0); n   cin.tie(0); n   cout.precision(10); n   bool vozr = 1, ub = 1, same; n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (i != 0 && a[i] > a[i - 1]) ub = 0; n     if (i != 0 && a[i] < a[i - 1]) vozr = 0; n   } n   if (n <= 2 || (vozr && ub)) { n     cout << -1 <<   n ; n     return 0; n   } n   if (vozr) { n     same = 1; n     for (int i = 2; i < n - 1; i++) { n       if (a[i] != a[i - 1]) same = 0; n     } n     if (same) { n       if (a[n - 2] == a[n - 1]) n         cout << 1 <<     << n - 1 <<   n ; n       else n         cout << n - 1 <<     << n <<   n ; n     } else n       cout << 1 <<     << n <<   n ; n   } else if (ub) { n     same = 1; n     for (int i = 2; i < n - 1; i++) { n       if (a[i] != a[i - 1]) same = 0; n     } n     if (same) { n       if (a[n - 2] == a[n - 1]) n         cout << 1 <<     << 2 <<   n ; n       else n         cout << n - 1 <<     << n <<   n ; n     } else n       cout << 1 <<     << n <<   n ; n   } else { n     if (n == 3) { n       if (a[0] == a[2]) n         cout << -1 <<   n ; n       else n         cout << 1 <<     << 3 <<   n ; n     } else { n       int minn = 1100000000, maxx = -1, id1, id2; n       for (int i = 0; i < n; i++) { n         if (a[i] < minn) { n           minn = a[i]; n           id1 = i; n         } n         if (a[i] >= maxx) { n           maxx = a[i]; n           id2 = i; n         } n       } n       if (id1 == 0) { n         if (id2 == n - 1) n           cout << 1 <<     << n <<   n ; n         else { n           if (id2 == 1) n             cout << 2 <<     << 3 <<   n ; n           else n             cout << 2 <<     << id2 + 1 <<   n ; n         } n       } else { n         if (id1 == 1) n           cout << 2 <<     << 3 <<   n ; n         else n           cout << 2 <<     << id1 + 1 <<   n ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n; n   while (cin >> n) { n     string s; n     cin >> s; n     long long a0 = 0, a1 = 0, a2 = 0; n     for (long long i = 0; i < s.size(); i++) { n       if (s[i] ==  0 ) { n         a0++; n       } else if (s[i] ==  1 ) { n         a1++; n       } else { n         a2++; n       } n     } n     long long d = s.size() / 3; n     long long x = 0, y = 0, z = 0; n     for (long long i = s.size() - 1; i >= 0; i--) { n       if (s[i] ==  1 ) { n         if (a1 > d && a2 < d) { n           s[i] =  2 ; n           a2++; n           a1--; n         } n       } n       if (s[i] ==  0 ) { n         if (a0 > d && a2 < d) { n           s[i] =  2 ; n           a2++; n           a0--; n         } n       } n     } n     for (long long i = 0; i < s.size(); i++) { n       if (s[i] ==  1 ) { n         if (a1 > d && a0 < d) { n           s[i] =  0 ; n           a0++; n           a1--; n         } n       } n       if (s[i] ==  2 ) { n         if (a2 > d && a0 < d) { n           s[i] =  0 ; n           a0++; n           a2--; n         } n       } n     } n     for (long long i = 0; i < s.size(); i++) { n       if (s[i] ==  2 ) { n         if (a2 > d && a1 < d) { n           s[i] =  1 ; n           a1++; n           a2--; n         } n       } n       if (s[i] ==  0 ) { n         if (a0 > d && a1 < d && x >= d) { n           s[i] =  1 ; n           a1++; n           a0--; n         } n         x++; n       } n     } n     cout << s << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void err(istream_iterator<string> it) {} n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cerr << *it <<   =   << a << endl; n   err(++it, args...); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m, a; n   cin >> n >> m; n   vector<int> cost(m); n   for (int i = 0; i < m; i++) cin >> a >> cost[i]; n   sort(cost.begin(), cost.end(), greater<int>()); n   int k = 1; n   while (true) { n     int nxtk = k + 1; n     int need = nxtk * (nxtk - 1) / 2; n     if (nxtk % 2 == 0) need += nxtk / 2 - 1; n     if (need <= (n - 1) && nxtk <= m) n       k = nxtk; n     else n       break; n   } n   long long res = 0; n   for (int i = 0; i < k; i++) res += cost[i]; n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e+6 + 5; n int lp[N], ans = 1e+9; n set<int> factors; n void factorise(int n, int isv) { n   int cnt = 0, ans = 0, n1 = n; n   while (n % 2 == 0) { n     cnt++; n     n = n / 2; n   } n   if (cnt) { n     ans = max(ans, 2); n     if (isv) factors.insert(2); n   } n   for (int i = 3; i * i <= n; i++) { n     cnt = 0; n     while (n % i == 0) { n       cnt++; n       n = n / i; n     } n     if (cnt) { n       ans = max(ans, i); n       if (isv) factors.insert(i); n     } n   } n   if (n > 1) { n     ans = max(ans, n); n     if (isv) factors.insert(n); n   } n   lp[n1] = ans; n } n int main() { n   int x; n   cin >> x; n   for (int i = 2; i <= x; i++) factorise(i, i == x); n   for (auto it : factors) { n     int i = x - it + 1; n     for (i; i <= x; i++) { n       if (lp[i] != i) ans = min(ans, i - lp[i] + 1); n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int a, s, d, f, g, h, j, k, l, i, n, m; n int main() { n   cin >> n >> m >> k; n   l = n * m * k / 100; n   for (i = 0; i < n; i++) { n     if (l > m) { n       cout << m; n       l = l - m; n     } else if (l > 0) { n       cout << l; n       l = 0; n     } else n       cout << 0; n     cout <<    ; n   } n } n


#include<bits/stdc++.h> n using namespace std; n #define ll long long n template <typename Arg1> n //print with endl n void pn(Arg1&& arg1) n { cout<<arg1<<  n ;} n template <typename Arg1, typename... Args> n void pn(Arg1&& arg1, Args&&... args) n { cout<<arg1<<  n ; pn(args...); } n  //print with space n template <typename Arg1> n void ps(Arg1&& arg1) n { cout<<arg1<<   ;} n template <typename Arg1, typename... Args> n void ps(Arg1&& arg1, Args&&... args) n { cout<<arg1<<   ; ps(args...); } n   n template <typename Arg1> n void pvs(Arg1&& arg1, ll n) n { for(ll i=0;i<n;i++)ps(arg1[i]); } n template <typename Arg1> n void pvn(Arg1&& arg1, ll n) n { for(ll i=0;i<n;i++)pn(arg1[i]); } n  n ll sum(ll n){ n     ll v=0; n     while(n!=0){ n         v+=(n%10); n         n/=10; n     } n     return v; n } n   n ll log(ll n, ll b) n { ll c=0; n   while(n>(b-1)) n   { c++; n/=b; } return c; } n #define fast  ios::sync_with_stdio(false); cin.tie(NULL);  cout.tie(NULL); n #define mod 1000000007 //10^9+7 n #define st string n #define ull unsigned long long n #define pb push_back n #define mp make_pair n #define vi vector <int> n #define vll vector <long long> n #define f0(i,a) for(i=0;i<a;i++) n #define f1(i,a) for(i=1;i<=n;i++) n #define all(a) a.begin(),a.end() n #define sz(a) a.size() n #define lb lower_bound n #define ub upper_bound n #define pl pair<ll,ll> n #define vpl vector<pl> n #define ff first n #define ss second n #define um unordered_map n #define dsort(v) sort(v); reverse(v); n #define maxall(v) *max_element(all(v)) n #define minall(v) *min_element(all(v)) n #define gcd(a,b)        __gcd(a,b) n #define lcm(a,b)        a/__gcd(a,b)*b n #define Y                YES  n #define N                NO  n #define endl              n  n #define debug(x)        cout << #x <<   :   << x << nl; n const ll MOD = 998244353; n const ll inf = 1e18; n const ll MAX = 2e5 + 1; n ll GCD(ll a,ll b){ n   if(b==0) n     return a; n   else  n     return GCD(b,a%b); n } n ll mulmod(ll x,ll n,ll M) n { n     ll result=1; n     while(n>0) n     { n         if(n% 2 ==1) n             result=(result * x)%M; n         x=(x*x)%M; n         n=n/2; n     } n     return result; n } n  n ///////////////////////// n bool isPowerOfTwo(ll n)  n {  n     if (n == 0)  n         return 0;  n     while (n != 1)  n     {  n         if (n%2 != 0)  n             return 0;  n         n = n/2;  n     }  n     return 1;  n } n void solve(){ n    ll i; n    ll n; n    cin>>n; n    vll v(n); n    f0(i,n) cin>>v[i]; n    f0(i,n){ n     if(v[i]&1) n       cout<<v[i]<<   ; n    } n    f0(i,n){ n     if(!(v[i]&1)) n       cout<<v[i]<<   ; n    } n cout<<endl; n     n     n     n } n int main(){   n   fast; n   #ifdef Local n     freopen( inp.txt ,  r , stdin); n    // freopen( out.txt ,  w , stdin); n    #endif n   // solve();/* n   ll t; n   cin>>t; n   for(ll tt=1;tt<=t;tt++) n   { n     // cout<< Case # <<tt<< :  ; n     solve(); n   } n   //*/ n } n  n  n  n  n  n


#include <bits/stdc++.h> n using namespace std; n char str[40], ans[2][40], p[40]; n int pre[40], sum[40]; n int main() { n   scanf( %s , str); n   int L = 0, R = 0; n   bool mark = true; n   memset(pre, -1, sizeof(pre)); n   for (int i = 0; i < 27; i++) { n     int a = str[i] -  A ; n     sum[a]++; n     if (pre[a] == -1) n       pre[a] = i; n     else { n       L = pre[a]; n       R = i; n       if (R - 1 == L) { n         mark = false; n       } n     } n   } n   int k = 0; n   for (int i = 0; i < 26; i++) { n     if (sum[i] == 0 || sum[i] > 2) mark = false; n     if (sum[i] == 2) k++; n   } n   if (k != 1 || !mark) { n     printf( Impossible n ); n     return 0; n   } n   R = 26 - R; n   int cnt = 0; n   if (L > R) { n     int o = 0; n     for (int i = 26; i >= 0; i--) { n       p[o++] = str[i]; n     } n     for (int i = 0; i < 27; i++) str[i] = p[i]; n     swap(L, R); n   } n   int num = R - L; n   if (num & 1) { n     num = (num / 2) + 1; n     int i; n     for (i = num;; i++) { n       if (cnt == L) break; n       ans[0][i] = str[cnt++]; n     } n     int flag = i - 1; n     ans[1][i - 1] = str[cnt++]; n     for (; i < 13; i++) ans[0][i] = str[cnt++]; n     for (int i = 12; i > flag; i--) { n       ans[1][i] = str[cnt++]; n     } n     cnt++; n     for (int i = flag - 1; i >= 0; i--) ans[1][i] = str[cnt++]; n     if (num) { n       for (int i = 0; i < num; i++) ans[0][i] = str[cnt++]; n     } n   } else { n     num = num / 2; n     int i; n     for (i = num;; i++) { n       if (cnt == L) break; n       ans[0][i] = str[cnt++]; n     } n     int flag = i; n     ans[1][i] = str[cnt++]; n     for (; i < 13; i++) ans[0][i] = str[cnt++]; n     for (int i = 12; i > flag; i--) { n       ans[1][i] = str[cnt++]; n     } n     cnt++; n     for (int i = flag - 1; i >= 0; i--) ans[1][i] = str[cnt++]; n     if (num) { n       for (int i = 0; i < num; i++) ans[0][i] = str[cnt++]; n     } n   } n   if (L == 0 && R == 0) { n     cnt = 0; n     for (int i = 0; i < 13; i++) ans[0][i] = str[cnt++]; n     for (int i = 12; i >= 0; i--) ans[1][i] = str[cnt++]; n   } n   for (int i = 0; i < 2; i++) { n     for (int j = 0; j < 13; j++) printf( %c , ans[i][j]); n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int N; n vector<int> V; n bool Do() { n   while (V.size() > 0 && V.back() % 2 == 0) V.pop_back(); n   if (V.size() == 0) return false; n   int sum = 0; n   int p = -1; n   for (int i = 0; i < V.size(); ++i) { n     if (V[i] % 2 && i < V.size() - 1) p = i; n     sum += V[i]; n   } n   if (sum % 2 == 0) n     return true; n   else if (p == -1) n     return false; n   else { n     for (int i = p; i + 1 < V.size(); ++i) V[i] = V[i + 1]; n     V.pop_back(); n     return true; n   } n } n int main() { n   int nr_t, nr; n   char c; n   cin >> nr_t; n   for (int i = 1; i <= nr_t; ++i) { n     cin >> N; n     for (int j = 1; j <= N; ++j) { n       cin >> c; n       V.push_back(c -  0 ); n     } n     if (!Do()) n       cout <<  -1  <<   n ; n     else { n       for (int i = 0; i < V.size(); ++i) cout << V[i]; n       cout <<   n ; n     } n     V.clear(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void toogle(vector<vector<int> >& v, int i, int j) { n   if (v[i][j] == 1) { n     v[i][j] = 0; n   } else if (v[i][j] == 0) { n     v[i][j] = 1; n   } n } n int main() { n   vector<vector<int> > v(3, vector<int>(3, 1)); n   int i, j, e; n   for (i = 0; i < 3; i++) { n     for (j = 0; j < 3; j++) { n       cin >> e; n       if (e % 2 == 0) { n         continue; n       } else { n         toogle(v, i, j); n         if (i - 1 >= 0) toogle(v, i - 1, j); n         if (j - 1 >= 0) toogle(v, i, j - 1); n         if (i + 1 < 3) toogle(v, i + 1, j); n         if (j + 1 < 3) toogle(v, i, j + 1); n       } n     } n   } n   for (i = 0; i < 3; i++) { n     for (j = 0; j < 3; j++) { n       cout << v[i][j]; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   int to, next; n } e[200010]; n int n, m, q, cnt, father[100010], fa[100010][30], a[100010], dis[100010], n     deep[100010], head[100010]; n inline void add(int u, int v) { n   cnt++; n   e[cnt].to = v; n   e[cnt].next = head[u]; n   head[u] = cnt; n } n inline int find(int x) { n   return father[x] == x ? x : father[x] = find(father[x]); n } n inline void dfs(int u) { n   deep[u] = deep[fa[u][0]] + 1; n   for (int i = head[u]; i; i = e[i].next) { n     int v = e[i].to; n     if (!deep[v]) { n       fa[v][0] = u; n       dfs(v); n       if (find(u) == find(v)) a[u] |= a[v]; n     } else if (deep[v] < deep[u] - 1) { n       if ((deep[u] + deep[v] + 1) & 1) a[u] = 1; n       for (int x = find(u); deep[x] > deep[v] + 1; x = find(x)) n         father[x] = fa[x][0]; n     } n   } n } n inline void dfs2(int u) { n   dis[u] += a[u]; n   for (int i = head[u]; i; i = e[i].next) { n     int v = e[i].to; n     if (deep[v] != deep[u] + 1) continue; n     if (find(u) == find(v)) a[v] |= a[u]; n     dis[v] = dis[u]; n     dfs2(v); n   } n } n inline int LCA(int x, int y) { n   if (deep[x] > deep[y]) swap(x, y); n   for (int i = 20; i >= 0; i--) n     if (deep[fa[y][i]] >= deep[x]) y = fa[y][i]; n   if (x == y) return x; n   for (int i = 20; i >= 0; i--) n     if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i]; n   return fa[x][0]; n } n inline bool check(int u, int v) { n   if (!LCA(u, v)) return 0; n   if ((deep[u] + deep[v]) & 1) return 1; n   if (dis[u] + dis[v] - 2 * dis[LCA(u, v)]) return 1; n   return 0; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) father[i] = i; n   for (int i = 1, x, y; i <= m; i++) { n     scanf( %d%d , &x, &y); n     add(x, y); n     add(y, x); n   } n   for (int i = 1; i <= n; i++) n     if (!deep[i]) dfs(i), dfs2(i); n   for (int i = 1; i <= 20; i++) n     for (int j = 1; j <= n; j++) fa[j][i] = fa[fa[j][i - 1]][i - 1]; n   scanf( %d , &q); n   while (q--) { n     int x, y; n     scanf( %d%d , &x, &y); n     if (check(x, y)) n       puts( Yes ); n     else n       puts( No ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int n, x; n   vector<int> a, b, c; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x; n     if (x == 1) n       a.push_back(i + 1); n     else if (x == 2) n       b.push_back(i + 1); n     else n       c.push_back(i + 1); n   } n   int res = min({a.size(), b.size(), c.size()}); n   cout << res << endl; n   for (int i = 0; i < res; i++) { n     cout << a[i] <<     << b[i] <<     << c[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n const int N = 1e6 + 100; n pair<int, int> seg[4 * N]; n int lazy[4 * N]; n void shift(int nod) { n   lazy[nod * 2] += lazy[nod]; n   lazy[nod * 2 + 1] += lazy[nod]; n   seg[nod * 2].first += lazy[nod]; n   seg[nod * 2 + 1].first += lazy[nod]; n   seg[nod * 2].second += lazy[nod]; n   seg[nod * 2 + 1].second += lazy[nod]; n   lazy[nod] = 0; n } n void upd(int nod, int l, int r, int L, int R, int val) { n   if (l >= R || L >= r) { n     return; n   } n   if (l >= L && r <= R) { n     seg[nod].first += val; n     seg[nod].second += val; n     lazy[nod] += val; n     return; n   } n   shift(nod); n   int mid = (r + l) / 2; n   upd(nod * 2, l, mid, L, R, val); n   upd(nod * 2 + 1, mid, r, L, R, val); n   seg[nod].first = max(seg[nod * 2].first, seg[nod * 2 + 1].first); n   seg[nod].second = min(seg[nod * 2].second, seg[nod * 2 + 1].second); n } n int a[N]; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   int cnt = 1; n   int to = 0, tc = 0; n   for (int i = 0; i < n; i++) { n     char c; n     cin >> c; n     if (c ==  ( ) { n       if (a[cnt] == 1) { n         if (to != tc || seg[1].second < 0) { n           cout << -1 <<   n ; n         } else { n           cout << seg[1].first <<   n ; n         } n         continue; n       } n       if (a[cnt] == 2) { n         upd(1, 1, N, cnt, N, +2); n         tc--; n       } else { n         upd(1, 1, N, cnt, N, 1); n       } n       a[cnt] = 1; n       to++; n     } n     if (c ==  ) ) { n       if (a[cnt] == 2) { n         if (to != tc || seg[1].second < 0) { n           cout << -1 <<   n ; n         } else { n           cout << seg[1].first <<   n ; n         } n         continue; n       } n       if (a[cnt] == 1) { n         upd(1, 1, N, cnt, N, -2); n         to--; n       } else { n         upd(1, 1, N, cnt, N, -1); n       } n       tc++; n       a[cnt] = 2; n     } n     if (c ==  R ) { n       cnt++; n     } n     if (c ==  L ) { n       cnt--; n     } n     if (c >=  a  && c <=  z ) { n       if (a[cnt] == 1) { n         a[cnt] = 0; n         upd(1, 1, N, cnt, N, -1); n         to--; n       } n       if (a[cnt] == 2) { n         a[cnt] = 0; n         upd(1, 1, N, cnt, N, +1); n         tc--; n       } n     } n     cnt = max(cnt, (int)1); n     if (to != tc || seg[1].second < 0) { n       cout << -1 <<   n ; n     } else { n       cout << seg[1].first <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000001] = {0}, n; n int main() { n   cin >> n; n   long long sum = 0; n   long s = 0, s1 = 0, s2 = 0, s3 = 0; n   for (int k = 1; k <= n; ++k) { n     cin >> a[k], sum += a[k]; n     if (a[k] == 1) n       ++s; n     else if (a[k] == 2) n       ++s1; n     else if (a[k] == 3) n       ++s3; n     else if (a[k] == 4) n       s2++; n   } n   s3 += min(s, s1); n   ; n   if (sum == 1 || sum == 2 || sum == 5) { n     cout << -1; n     return 0; n   } n   sum = 0; n   sum += min(s, s1); n   s1 = s1 - s; n   if (s1 < 0) n     s = -s1, s1 = 0; n   else n     s = 0; n   if (s > 0) { n     sum += s - s / 3; n     if (s == 1 && s3 == 0) ++sum; n   } n   if (s1 > 0) { n     sum += s1 - s1 / 3; n     if (s1 % 3 == 1 && s2 == 0) ++sum; n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   long long k; n   cin >> n >> k; n   k -= (long long)n * (n + 1) / 2; n   if (k < 0) { n     cout << -1 << endl; n     return 0; n   } n   long long sum = (long long)n * (n + 1) / 2; n   vector<int> ans; n   for (int i = n - 1; i >= 0; i--) { n     int add = max(0, n - 1 - (n - 1 - i) * 2); n     if (k > add) { n       ans.push_back(i); n       ans[n - 1 - i] = i; n       k -= add; n       sum += add; n     } else { n       for (int j = 0; j < i; j++) { n         ans.push_back(j); n       } n       ans.insert(ans.begin() + (n - 1 - i) + (add - k), i); n       sum += k; n       break; n     } n   } n   cout << sum << endl; n   for (int i = 0; i < n; i++) { n     cout << i + 1; n     if (i < n - 1) { n       cout <<    ; n     } n   } n   cout << endl; n   for (int i = 0; i < n; i++) { n     cout << ans[i] + 1; n     if (i < n - 1) { n       cout <<    ; n     } n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e4 + 10; n int cnt[100 + 2][N + 2]; n string s[100 + 2]; n set<int> on[100 + 2], of[100 + 2]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     cin >> s[i]; n     s[i] =  #  + s[i] +  # ; n     for (int j = 1; j <= m; j++) { n       if (s[i][j] ==  . ) n         of[i].insert(j); n       else n         on[i].insert(j); n     } n     on[i].insert(0); n     on[i].insert(m + 1); n     of[i].insert(-1); n     of[i].insert(m + 2); n   } n   long long ans = 0; n   int a = 1, b = 1, d = 1; n   while (true) { n     if (clock() > CLOCKS_PER_SEC * 0.8) cout <<  Never n , exit(0); n     if (a == n) break; n     if (d) { n       int cr = *on[a].lower_bound(b); n       int nx = *of[a + 1].lower_bound(b); n       if (nx < cr) { n         ans += nx - b + 1; n         a++; n         b = nx; n       } else { n         ans += cr - b; n         b = cr - 1; n         d ^= 1; n         if (s[a][cr] ==  + ) { n           on[a].erase(cr); n           s[a][cr] =  . ; n           of[a].insert(cr); n         } n       } n     } else { n       auto it1 = on[a].upper_bound(b); n       auto it2 = of[a + 1].upper_bound(b); n       it1--, it2--; n       int cr = *it1, nx = *it2; n       if (nx > cr) { n         ans += b - nx + 1; n         a++; n         b = nx; n       } else { n         ans += b - cr; n         b = cr + 1; n         d ^= 1; n         if (s[a][cr] ==  + ) { n           on[a].erase(cr); n           s[a][cr] =  . ; n           of[a].insert(cr); n         } n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 998244353; n struct bucket { n   long long size, minm; n   vector<long long> off; n   vector<long long> dps; n   vector<long long> pre; n   bucket(long long b) { n     size = b; n     off = vector<long long>(b); n     pre = vector<long long>(b); n     dps = vector<long long>(b); n     minm = 0; n   } n   void updoff(long long p, long long l, long long r) { n     assert(l >= 0 && l < size && r >= 0 && r < size && l <= r); n     long long nmnm = INT_MAX; n     for (long long j = l; j <= r; ++j) nmnm = min(nmnm, minm + off[j] + p); n     for (long long j = 0; j < l; ++j) nmnm = min(nmnm, minm + off[j]); n     for (long long j = r + 1; j < size; ++j) nmnm = min(nmnm, minm + off[j]); n     for (long long j = 0; j < l; ++j) off[j] = off[j] + minm - nmnm; n     for (long long j = l; j <= r; ++j) off[j] = off[j] + minm + p - nmnm; n     for (long long j = r + 1; j < size; ++j) off[j] = off[j] + minm - nmnm; n     minm = nmnm; n     pre = vector<long long>(size); n     for (long long j = 0; j < size; ++j) n       pre[off[j]] = (pre[off[j]] + dps[j]) % mod; n     for (long long j = 1; j < size; ++j) pre[j] = (pre[j] + pre[j - 1]) % mod; n   } n   void upddp(long long idx, long long newdp) { n     dps[idx] = newdp; n     pre = vector<long long>(size); n     for (long long j = 0; j < size; ++j) n       pre[off[j]] = (pre[off[j]] + dps[j]) % mod; n     for (long long j = 1; j < size; ++j) pre[j] = (pre[j] + pre[j - 1]) % mod; n   } n }; n void update_offset(vector<bucket> &ds, long long n, long long l, long long r, n                    long long p, long long b) { n   assert(l <= r); n   long long j = r / b; n   ds[j].updoff(p, max(l, j * b) - j * b, r - j * b); n   j--; n   while (j * b > l) { n     ds[j].minm += p; n     j--; n   } n   if (l >= j * b && l < (j + 1) * b) ds[j].updoff(p, l - j * b, b - 1); n } n long long query(vector<bucket> &ds, long long n, long long r, long long b, n                 long long k) { n   long long j = r / b; n   long long ret = 0; n   while (j >= 0) { n     long long idx = k - ds[j].minm; n     if (idx >= 0) ret = (ret + ds[j].pre[min(idx, b - 1)]) % mod; n     j--; n   } n   return ret; n } n int32_t main() { n   long long n, k; n   cin >> n >> k; n   long long b = sqrt(n); n   long long m = ceil((double)n / b); n   vector<long long> a(n); n   for (auto &x : a) cin >> x; n   vector<bucket> ds; n   for (long long i = 0; i < m; ++i) { n     bucket B = bucket(b); n     ds.push_back(B); n   } n   vector<long long> lo(n + 1, -1); n   vector<long long> llo(n + 1, -1); n   vector<long long> dp(n); n   dp[0] = 1; n   lo[a[0]] = 0; n   ds[0].updoff(1, 0, 0); n   ds[0].upddp(0, 1); n   if ((1 / b) < m) ds[1 / b].upddp(1 - (1 / b) * b, 1); n   for (long long i = 1; i < n; ++i) { n     if (lo[a[i]] != -1) update_offset(ds, n, llo[a[i]] + 1, lo[a[i]], -1, b); n     update_offset(ds, n, lo[a[i]] + 1, i, 1, b); n     dp[i] = query(ds, n, i, b, k); n     long long j = (i + 1) / b; n     if (j < m) ds[j].upddp((i + 1) - j * b, dp[i]); n     llo[a[i]] = lo[a[i]]; n     lo[a[i]] = i; n   } n   cout << dp[n - 1]; n } n


#include <bits/stdc++.h> n using namespace std; n char s[100005]; n int main() { n   int n; n   cin >> n; n   int k = min(n, 4); n   for (int i = 0; i < k; i++) n     for (int j = i; j < n; j += 4) s[j] = i +  a ; n   s[n] =   0 ; n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, j, s = 0, o; n   cin >> n; n   char *a[n + 1], str[n + 1]; n   for (i = 0; i < n; i++) { n     cin >> str; n     a[i] = (char *)malloc(n + 1); n     strcpy(a[i], str); n   } n   for (i = 0; i < n && s == 0; i++) n     for (j = 0; j < n && s == 0; j++) { n       o = 0; n       if (j != 0 && a[i][j - 1] ==  o ) o++; n       if ((j != n - 1) && a[i][j + 1] ==  o ) o++; n       if (i != 0 && a[i - 1][j] ==  o ) o++; n       if ((i != n - 1) && a[i + 1][j] ==  o ) o++; n       if (o % 2 != 0) s = 1; n     } n   if (s) n     cout <<  NO ; n   else n     cout <<  YES ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int SIZE = 1e6 + 10; n vector<pair<int, int> > headd[10][2]; n bitset<10000000> used; n int ad[10000000]; n bool valid(long long x) { n   int lt = x % 10; n   x /= 10; n   int flag = 0; n   while (x) { n     int tmp = x % 10; n     x /= 10; n     if (tmp < lt) { n       if (flag == -1) return false; n       flag = -1; n     } else if (tmp > lt) { n       if (flag == 1) return false; n       flag = 1; n     } else n       return false; n     lt = tmp; n   } n   return true; n } n int check(int st, long long N, int ud, long long& K, long long need) { n   if (need >= 10000000) return -1; n   int ker = lower_bound((headd[st][ud]).begin(), (headd[st][ud]).end(), n                         make_pair((int)need, 0)) - n             headd[st][ud].begin(); n   int ker2 = lower_bound((headd[st][ud]).begin(), (headd[st][ud]).end(), n                          make_pair((int)need + 1, 0)) - n              headd[st][ud].begin(); n   if (ker2 - ker < K) { n     K -= ker2 - ker; n     return -1; n   } n   for (int i = (ker); i < (((int)(headd[st][ud]).size())); ++i) { n     K--; n     if (K == 0) return headd[st][ud][i].second; n   } n   return -1; n } n int main() { n   long long N, K; n   cin >> N >> K; n   if (N > (1e14 + 3) / K) { n     puts( -1 ); n     return 0; n   } n   if (N > 10000000) { n     for (long long now = N; now < 100000000000000LL; now += N) { n       if (valid(now)) { n         K--; n         if (!K) { n           cout << now << endl; n           return 0; n         } n       } n     } n     cout << -1 << endl; n     return 0; n   } n   int tmp[8] = {}; n   for (int i = (1); i < (10000000); ++i) { n     tmp[0]++; n     int now = 0; n     while (tmp[now] == 10) { n       tmp[now] = 0; n       now++; n       tmp[now]++; n     } n     bool err = 0; n     for (int j = (1); j < (6); ++j) { n       if ((tmp[j] - tmp[j - 1]) * (tmp[j] - tmp[j + 1]) <= 0) { n         err = 1; n         break; n       } n     } n     if (!err) { n       headd[tmp[6]][(tmp[6] < tmp[5]) ? 1 : 0].push_back(make_pair(i % N, i)); n     } n   } n   for (int i = 0; i < (10); ++i) n     for (int j = 0; j < (2); ++j) n       sort((headd[i][j]).begin(), (headd[i][j]).end()); n   for (long long now = 1; now < 10000000; now++) { n     if ((now / 10) % 10 == now % 10) continue; n     int ha = ((now / 10) % 10) < (now % 10) ? 1 : -1; n     if (now < 10 || (used[now / 10] && (ad[now / 10] != ha))) { n       if (now < 10) ha = 0; n       if (now % N == 0) { n         K--; n         if (K == 0) { n           cout << now << endl; n           return 0; n         } n       } n       ad[now] = ha; n       used[now] = 1; n     } n   } n   for (int i = (1); i < (10000000); ++i) { n     if (used[i]) { n       int tail = i % 10; n       int flag = 0; n       if (i >= 10) { n         if ((i / 10) % 10 < tail) n           flag = 1; n         else n           flag = -1; n       } n       long long r = (i * 10000000LL) % N; n       long long need = N - r; n       if (need == N) need = 0; n       if (flag != -1) { n         for (int j = 0; j < (tail); ++j) { n           int res = check(j, N, 1, K, need); n           if (res >= 0) { n             printf( %d%07d n , i, res); n             return 0; n           } n         } n       } n       if (flag != 1) n         for (int j = (tail + 1); j < (10); ++j) { n           int res = check(j, N, 0, K, need); n           if (res >= 0) { n             printf( %d%07d n , i, res); n             return 0; n           } n         } n     } n   } n   puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int foot = n / 36; n   float b = n % 36; n   int kkk = b / 3; n   double kk = kkk + 0.5; n   float inch = b / 3; n   if (inch >= kk) n     inch = ceil(inch); n   else if (inch < kk) n     inch = floor(inch); n   int sss = (float)(inch); n   if (sss % 12 == 0) { n     inch = 0; n     foot += (sss / 12); n   } n   cout << foot <<     << inch; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = 3.14159265359; n long long max(long long a, long long 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 bool valid(int i, int j, long long n, long long m) { n   if (i >= 0 && i < n && j >= 0 && j < m) n     return 1; n   else n     return 0; n } n void yes() { cout <<  YES  <<   n ; } n void no() { cout <<  NO  <<   n ; } n vector<unsigned long long> divisor(10000007); n void printDivisors(long long n) { n   for (int i = 1; i <= sqrt(n); i++) { n     if (n % i == 0) { n       if (n / i == i) { n         divisor[n] += i; n       } else { n         divisor[n] += i; n         divisor[n] += n / i; n       } n     } n   } n } n bool prime[1200000]; n map<long long, long long> primes; n void SieveOfEratosthenes(long long n = 1200000) { n   memset(prime, true, sizeof(prime)); n   prime[1] = false; n   for (int p = 2; p * p < n; p++) { n     if (prime[p] == true) { n       for (int i = p * 2; i < n; i += p) prime[i] = false; n     } n   } n   for (int i = 1; i < n + 1; i++) n     if (prime[i]) { n       primes[i]++; n     } n } n int gcd(long long a, long long b) { n   if (!b) return a; n   return gcd(b, a % b); n } n string toBinary(long long n) { n   string r; n   while (n != 0) { n     r = (n % 2 == 0 ?  0  :  1 ) + r; n     n /= 2; n   } n   return r; n } n bool isPrime(long long n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (int i = 5; i * i <= n; i = i + 6) n     if (n % i == 0 || n % (i + 2) == 0) return false; n   return true; n } n map<long long, long long> PF; n void primeFactors(long long n) { n   while (n % 2 == 0) { n     PF[2]++; n     n = n / 2; n   } n   for (int i = 3; i <= sqrt(n); i = i + 2) { n     while (n % i == 0) { n       PF[i]++; n       n = n / i; n     } n   } n   if (n > 2) PF[n]++; n } n long double intlog(long long base, long long x) { n   long double ans1 = log10(x), ans2 = log10(base); n   return (long double)(ans1 / ans2); n } n bool IsPowerOfTwo(long long x) { return (x & (x - 1)) == 0; } n map<long long, long long> LN; n void GetLuckyNumbers(string s) { n   if (s.size() > 10) return; n   GetLuckyNumbers(s +  4 ); n   if (s !=   ) LN[stoll(s)]++; n   GetLuckyNumbers(s +  7 ); n   if (s !=   ) LN[stoll(s)]++; n } n unsigned long long toDecimal(unsigned long long n) { n   unsigned long long num = n; n   unsigned long long dec_value = 0; n   unsigned long long base = 1; n   int temp = num; n   while (temp) { n     unsigned long long last_digit = temp % 10; n     temp = temp / 10; n     dec_value += last_digit * base; n     base = base * 2; n   } n   return dec_value; n } n long double power(long double b, long long p) { n   if (p == 0) n     return 1; n   else if (p == 1) n     return b; n   long double a = power(b, p / 2); n   if (p % 2 == 0) return (a) * (a); n   return (b) * (a) * (a); n } n long largest_power_of_two(long N) { n   N = N | (N >> 1); n   N = N | (N >> 2); n   N = N | (N >> 4); n   N = N | (N >> 8); n   return (N + 1) >> 1; n } n long long cnt(string s) { n   long long c = 0; n   for (int i = 0; i < s.size(); i++) c += s[i] -  0 ; n   return c; n } n bool ispal(string s) { n   for (int i = 0; i < s.size() / 2; i++) { n     if (s[i] != s[s.size() - i - 1]) return false; n   } n   return true; n } n map<string, long long> substrings; n void subString(string s, long long n) { n   for (int i = 0; i < n; i++) n     for (int len = 1; len <= n - i; len++) { n       substrings[s.substr(i, len)]++; n     } n } n bool check_divisabilty_prefix(string str, long long a) { n   long long len = str.size(); n   vector<int> lr(len + 1, 0); n   lr[0] = (str[0] -  0 ) % a; n   for (int i = 1; i < len; i++) n     lr[i] = ((lr[i - 1] * 10) % a + (str[i] -  0 )) % a; n   if (lr[len - 1] == 0) return 1; n   return 0; n } n bool check_divisabilty_suffix(string str, long long b) { n   long long len = str.size(); n   vector<long long> rl(len + 1, 0); n   rl[len - 1] = (str[len - 1] -  0 ) % b; n   int power10 = 10; n   for (int i = len - 2; i >= 0; i--) { n     rl[i] = (rl[i + 1] + (str[i] -  0 ) * power10) % b; n     power10 = (power10 * 10) % b; n   } n   if (rl[0] == 0) return 1; n   return 0; n } n bool isSubSeq(int idx1, int idx2, string s1, string s2) { n   if (idx2 == s2.size()) return true; n   if (idx1 == s1.size()) return false; n   if (s1[idx1] == s2[idx2]) n     return isSubSeq(idx1 + 1, idx2 + 1, s1, s2); n   else n     return isSubSeq(idx1 + 1, idx2, s1, s2); n } n vector<long long> v; n long long bs(long long st, long long en, long long rq, long long val) { n   long long ans; n   while (st <= en) { n     long long mid = (st + en) / 2; n     long long aa = v[mid] - v[rq]; n     if (aa <= val) { n       st = mid + 1; n       ans = mid; n     } else n       en = mid - 1; n   } n   return abs(ans - rq); n } n bool isPerfectSquare(long double x) { n   if (x >= 0) { n     long long sr = sqrt(x); n     return (sr * sr == x); n   } n   return false; n } n long long fn(long long s) { n   if (s < 10) return s; n   long long pr = 1; n   while (s) { n     if (s % 10) pr *= (s % 10); n     s /= 10; n   } n   return fn(pr); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   vector<vector<long long> > v(11, vector<long long>(1e6 + 1, 0)); n   for (int i = 1; i <= 1000000; i++) { n     v[fn(i)].push_back(i); n   } n   int n; n   long long x; n   cin >> x; n   while (x--) { n     long long start, endd, nm; n     cin >> start >> endd >> nm; n     long long lower = n         lower_bound(v[nm].begin(), v[nm].end(), start) - v[nm].begin(); n     long long upper = n         lower_bound(v[nm].begin(), v[nm].end(), endd) - v[nm].begin(); n     if (v[nm][upper] == endd) { n       cout << (upper - lower) + 1 << endl; n     } else { n       cout << upper - lower << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string a, b; n int f1[100005], f2[100005], res, p1, p2, k, dp2[100005]; n int main() { n   cin >> a >> b; n   int n = a.length(); n   int m = b.length(); n   a =  #  + a; n   b =  #  + b; n   int p = 1; n   for (int i = 1; i <= m; i++) { n     if (p > n) break; n     while (a[p] != b[i] && p <= n) p++; n     if (p <= n) f1[i] = p; n     p++; n   } n   p = n; n   for (int i = 1; i <= max(m, n); i++) f2[i] = -1; n   for (int i = m; i >= 1; i--) { n     if (p < 1) break; n     while (a[p] != b[i] && p >= 1) p--; n     if (p >= 1) f2[i] = p; n     p--; n   } n   for (int i = 1; i <= m; i++) n     if (res < i && f1[i] > 0) { n       res = i; n       p1 = 1; n       p2 = i; n       k = 1; n     } n   for (int i = 1; i <= max(m, n); i++) dp2[i] = n + 1; n   p = n; n   for (int i = m; i >= 1; i--) { n     if (p < 1) break; n     while (a[p] != b[i] && p >= 1) p--; n     if (p >= 1) dp2[i] = p; n     p--; n   } n   for (int i = 1; i <= m; i++) n     if (res < (m - i + 1) && dp2[i] < n + 1) { n       res = (m - i + 1); n       p1 = i; n       p2 = m; n       k = 1; n     } n   int z = 0; n   int x = 0; n   for (int i = 1; i <= m; i++) { n     z = max(z, i + 1); n     while (x <= n && a[x] != b[i]) x++; n     if (x > n) break; n     x++; n     while (f2[z] < x) z++; n     int ans = i + 1 + m - z; n     if (res < ans) { n       res = ans; n       p1 = i; n       p2 = z; n       k = 2; n     } n   } n   if (k == 1) n     for (int i = p1; i <= p2; i++) cout << b[i]; n   else if (k == 2) { n     for (int i = 1; i <= p1; i++) cout << b[i]; n     for (int i = p2; i <= m; i++) cout << b[i]; n   } else n     cout <<  - ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename INT> n struct TripleDXY { n   INT d, x, y; n   TripleDXY(INT _d = 0, INT _x = 0, INT _y = 0) : d(_d), x(_x), y(_y) {} n }; n template <typename INT> n TripleDXY<INT> egcd(INT a, INT b) { n   bool neg_a = a < 0; n   if (neg_a) a = -a; n   bool neg_b = b < 0; n   if (neg_b) b = -b; n   INT x = 0, lastx = 1; n   INT y = 1, lasty = 0; n   while (b != 0) { n     INT t = b; n     INT q = a / b; n     b = a % b; n     a = t; n     t = x; n     x = lastx - q * x; n     lastx = t; n     t = y; n     y = lasty - q * y; n     lasty = t; n   } n   return TripleDXY<INT>(a, neg_a ? -lastx : lastx, neg_b ? -lasty : lasty); n } n long long ChooseMod(int n, int k, int M) { n   long long res = 1; n   for (int i = 1; i <= k; ++i, --n) { n     TripleDXY<int> R = egcd(i, M); n     assert(R.d == 1); n     res = (res * n) % M; n     res = (res * R.x) % M; n     if (res < 0) res += M; n   } n   return res; n } n int N, K; n long long A[2004]; n long long R[2004]; n long long F[2004]; n int main(int argc, char* argv[]) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> N >> K; n   F[0] = 1; n   for (int i = 1; i < N; ++i) { n     F[i] = ChooseMod(K - 1 + i, i, 1000000007); n   } n   for (int i = 0; i < N; ++i) cin >> A[i]; n   R[0] = A[0] % 1000000007; n   for (int i = 1; i < N; ++i) { n     R[i] = 0; n     for (int j = 0; j <= i; ++j) { n       R[i] += (A[j] * F[i - j]); n       R[i] %= 1000000007; n     } n   } n   for (int i = 0; i < N; ++i) cout << R[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   string s1; n   cin >> s1; n   string s2; n   cin >> s2; n   string s3; n   cin >> s3; n   vector<pair<char, int> > cnt(26); n   for (int i = int(0); i < int(int(s1.size())); i++) { n     char c = s1[i]; n     int pos = tolower(c) -  a ; n     cnt[pos].second += 0; n     cnt[pos].first = c; n   } n   for (int i = int(0); i < int(int(s2.size())); i++) { n     char c = s2[i]; n     int pos = tolower(c) -  a ; n     cnt[pos].second += 0; n     cnt[pos].first = c; n   } n   for (int i = int(0); i < int(int(s3.size())); i++) { n     char c = s3[i]; n     int pos = tolower(c) -  a ; n     cnt[pos].second += 1; n     cnt[pos].first = c; n   } n   for (int i = int(0); i < int(int(s1.size())); i++) { n     for (int j = int(0); j < int(26); j++) { n       if (s1[i] == cnt[j].first) { n         cnt[j].second -= 1; n       } n     } n   } n   for (int i = int(0); i < int(int(s2.size())); i++) { n     for (int j = int(0); j < int(26); j++) { n       if (s2[i] == cnt[j].first) { n         cnt[j].second -= 1; n       } n     } n   } n   for (int i = int(0); i < int(26); i++) { n     if (cnt[i].second < 0 || cnt[i].second > 0) { n       cout <<  NO ; n       return 0; n     } n   } n   cout <<  YES ; n } n


#include <bits/stdc++.h> n using namespace std; n long long ar[5000005]; n int main() { n   string s; n   cin >> s; n   long long sz = s.size(); n   long long cnt = 0; n   for (int i = 0; i < sz; i++) { n     long long l = 0, w = 0; n     for (int j = i; j < sz; j++) { n       if (s[j] ==  ( ) n         l++; n       else if (s[j] ==  ) ) n         l--; n       else n         w++; n       if (w > l && w != 0) { n         w--; n         l++; n       } n       if (l < 0) break; n       if (w == l) cnt++; n     } n   } n   cout << cnt << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void __print(int x) { cerr << x; } n void __print(long x) { cerr << x; } n void __print(long long x) { cerr << x; } n void __print(unsigned x) { cerr << x; } n void __print(unsigned long x) { cerr << x; } n void __print(unsigned long long x) { cerr << x; } n void __print(float x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(char x) { cerr <<      << x <<     ; } n void __print(const char* x) { cerr <<     << x <<    ; } n void __print(const string& x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V>& x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  , ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T& x) { n   int f = 0; n   cerr <<  { ; n   for (auto& i : x) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n template <class A> n void read(vector<A>& v); n template <class A, size_t S> n void read(array<A, S>& a); n template <class T> n void read(T& x) { n   cin >> x; n } n void read(double& d) { n   string t; n   read(t); n   d = stod(t); n } n void read(long double& d) { n   string t; n   read(t); n   d = stold(t); n } n template <class H, class... T> n void read(H& h, T&... t) { n   read(h); n   read(t...); n } n template <class A> n void read(vector<A>& x) { n   for (auto& a : x) read(a); n } n template <class A, size_t S> n void read(array<A, S>& x) { n   for (auto& a : x) read(a); n } n const int MAXN = 2e5 + 5; n long long mod = 1e9 + 7; n vector<long long> gr[200005]; n void solve() { n   long long n; n   cin >> n; n   vector<long long> a(n); n   read(a); n   long long val = 0; n   for (long long i = int(1); i < int(n); i++) { n     if (a[i] < a[i - 1]) { n       val += a[i - 1] - a[i]; n     } n     if (val > a[0]) { n       cout <<  NO  n            <<   n ; n       return; n     } n   } n   cout <<  YES  n        <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long test; n   test = 1; n   cin >> test; n   while (test--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n #define il inline n #define ll long long n const int N=2e5+5,B=316; n  n int n,q,b[N],c[N]; ll a[N],lz[N]; n  n il int jump(int x){return x?std::max(a[x]-lz[x/B],0ll):-1;} n  n il void rebuild(int x,int v) n { n  if (c[x]+v>B) return ; c[x]+=v; int i; n  for (i=x*B; i<x*B+B; i++) b[i]=jump(i)<x*B?i:b[jump(i)]; n } n  n il void add(int x,int y,int v) n { n  int i; for (i=x/B*B; i<x; i++) a[i]+=v; n  for (i=y+1; i<y/B*B+B; i++) a[i]+=v; n  for (i=x/B; i<=y/B; i++) lz[i]+=v,rebuild(i,i!=x/B&&i!=y/B); n } n  n il int LCA(int x,int y) n { n  for ( ; b[x]!=b[y]; x=jump(b[x])) if (jump(b[x])<jump(b[y])) std::swap(x,y); n  for ( ; x!=y; x=jump(x)) if (x<y) std::swap(x,y); return x; n } n  n int main() n { n  scanf( %d%d ,&n,&q),a[0]=-1; int i,x,y,k; n  for (i=1; i<n; i++) scanf( %d ,a+i),a[i]--; n   n  for (add(0,n-1,0); q; q--) n   if (scanf( %d%d%d ,&i,&x,&y),i>1) printf( %d n ,LCA(x-1,y-1)+1); n   else scanf( %d ,&k),add(x-1,y-1,k); n   n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n struct pi { n   bool dir; n   int node; n   int edge; n }; n const int mxn = 200005; n int n; n vector<pi> graph[mxn]; n int dp1[mxn]; n vector<int> vv; n int ans = 1000000000; n void dfs1(int v, int p) { n   for (int i = 0; i < graph[v].size(); i++) { n     pi u = graph[v][i]; n     if (u.node == p) continue; n     dfs1(u.node, v); n     dp1[v] += dp1[u.node]; n     if (u.dir) dp1[v]++; n   } n } n void dfs2(int v, int p, int l) { n   int temp = l + dp1[v]; n   if (ans == (n - 1) - temp) n     vv.push_back(v); n   else if (ans > (n - 1) - temp) { n     vv.clear(); n     vv.push_back(v); n     ans = (n - 1) - temp; n   } n   for (int i = 0; i < graph[v].size(); i++) { n     pi u = graph[v][i]; n     if (u.node == p) continue; n     int something = temp - dp1[u.node]; n     if (u.dir) n       something--; n     else n       something++; n     dfs2(u.node, v, something); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     pi temp; n     temp.dir = 1; n     temp.node = y; n     temp.edge = i; n     graph[x].push_back(temp); n     temp.dir = 0; n     temp.node = x; n     graph[y].push_back(temp); n   } n   dfs1(1, 1); n   dfs2(1, 1, 0); n   printf( %d n , ans); n   sort(vv.begin(), vv.end()); n   printf( %d , vv[0]); n   for (int i = 1; i < vv.size(); i++) printf(  %d , vv[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pi = pair<ll, ll>; n const ll inf = 0x3f3f3f3f3f3f3f3f; n const ll minf = 0xc0c0c0c0c0c0c0c0; n ll A[400400]; n ll B[400400]; n inline bool between(ll le, ll ri, ll first) { n   return le <= first && first <= ri; n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   ll n; n   cin >> n; n   for (ll i = 1; i <= n; i++) cin >> A[i]; n   ll res = 0; n   for (ll bit = 0; bit <= 24; bit++) { n     memcpy(B, A, sizeof(ll) * (n + 5)); n     ll mask = (1 << (bit + 1)) - 1; n     for (ll i = 1; i <= n; i++) { n       B[i] &= mask; n     } n     sort(B + 1, B + n + 1); n     ll cnt = 0; n     for (ll i = 1; i <= n; i++) { n       cnt += (upper_bound(B + 1, B + n + 1, (1 << bit + 1) - 1 - B[i]) - B) - n              (lower_bound(B + 1, B + n + 1, (1 << bit) - B[i]) - B); n       if ((1 << bit) - B[i] <= B[i] && B[i] <= (1 << bit + 1) - 1 - B[i]) --cnt; n       cnt += n           (upper_bound(B + 1, B + n + 1, (1 << bit + 2) - 2 - B[i]) - B) - n           (lower_bound(B + 1, B + n + 1, (1 << bit) + (1 << bit + 1) - B[i]) - n            B); n       if (((1 << bit) + (1 << bit + 1)) - B[i] <= B[i] && n           B[i] <= (1 << bit + 2) - 2 - B[i]) n         --cnt; n     } n     assert(cnt % 2 == 0); n     cnt /= 2; n     if (cnt & 1) res |= (1 << bit); n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void work(); n int main() { n   work(); n   return 0; n } n char S[222222]; n char T[222222]; n int t[256][256]; n void work() { n   int n; n   while (scanf( %d , &n) == 1) { n     memset(t, 0, sizeof(t)); n     int ans = 0; n     scanf( %s%s , S + 1, T + 1); n     for (int i = (1), END = (n); i <= END; i++) n       if (S[i] != T[i]) { n         ans++; n         t[S[i]][T[i]] = i; n       } n     bool two = false; n     for (int i = ( a ), END = ( z ); i <= END; i++) n       for (int j = (i + 1), END = ( z ); j <= END; j++) n         if (t[i][j] && t[j][i]) { n           two = true; n           printf( %d n%d %d n , ans - 2, t[i][j], t[j][i]); n           i = 256; n           j = 256; n         } n     if (two) continue; n     for (int i = ( A ), END = ( Z ); i <= END; i++) n       for (int j = (i + 1), END = ( Z ); j <= END; j++) n         if (t[i][j] && t[j][i]) { n           two = true; n           printf( %d n%d %d n , ans - 2, t[i][j], t[j][i]); n           i = 256; n           j = 256; n         } n     if (two) continue; n     bool one = false; n     for (int i = ( a ), END = ( z ); i <= END; i++) { n       int pos = -1; n       for (int j = ( a ), END = ( z ); j <= END; j++) n         if (t[i][j]) pos = t[i][j]; n       if (pos == -1) continue; n       for (int j = ( a ), END = ( z ); j <= END; j++) n         if (t[j][i]) { n           one = true; n           printf( %d n%d %d n , ans - 1, pos, t[j][i]); n           i = j = 256; n         } n     } n     if (one) continue; n     for (int i = ( A ), END = ( Z ); i <= END; i++) { n       int pos = -1; n       for (int j = ( A ), END = ( Z ); j <= END; j++) n         if (t[i][j]) pos = t[i][j]; n       if (pos == -1) continue; n       for (int j = ( A ), END = ( Z ); j <= END; j++) n         if (t[j][i]) { n           one = true; n           printf( %d n%d %d n , ans - 1, pos, t[j][i]); n           i = j = 256; n         } n     } n     if (one) continue; n     printf( %d n%d %d n , ans, -1, -1); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int h; n int seq[200010]; n int par[200010]; n int cur, cur_par, rem = -1, rem_par; n int main() { n   scanf( %d , &(h)); n   for (int i = 0; i < h + 1; i++) scanf( %d , &(seq[i])); n   cur_par = 1; n   cur = 1; n   for (int i = 0; i < h; i++) { n     if (rem == -1 && seq[i] > 1 && seq[i + 1] > 1) { n       puts( ambiguous ); n       rem = cur; n       rem_par = cur_par; n     } n     for (int j = 0; j < seq[i + 1]; j++) par[cur + j] = cur_par; n     cur += seq[i + 1]; n     cur_par = cur; n   } n   if (rem == -1) { n     puts( perfect ); n     return 0; n   } n   for (int i = 0; i < cur; i++) printf( %d  , par[i]); n   puts(  ); n   par[rem] = rem_par - 1; n   for (int i = 0; i < cur; i++) printf( %d  , par[i]); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5 + 10; n long long a[MAXN], dp[MAXN]; n int main() { n   long long n, ans = 0; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = n - 2; i >= 0; i--) { n     dp[i] = dp[i + 1]; n     if (a[i + 1] == 0) dp[i]++; n   } n   for (int i = 0; i < n; i++) n     if (a[i] == 1) ans += dp[i]; n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n void FastIO() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.precision(20); n } n int main() { n   FastIO(); n   int A, B; n   while (cin >> A >> B) { n     int M = min(A, B); n     int N = max(A - M, B - M); n     cout << M <<     << N / 2 << endl; n   } n   return 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n int n, m, a[16][11234], b[16][16], c[16][16], dp[1 << 16][16]; n void solve(int u) { n   for (int i = 0; i < 1 << n; ++i) n     for (int j = 0; j < n; ++j) dp[i][j] = 0; n   dp[1 << u][u] = inf; n   for (int i = 0; i < 1 << n; ++i) n     for (int j = 0; j < n; ++j) n       if (dp[i][j]) n         for (int k = 0; k < n; ++k) n           if (!(i >> k & 1)) n             dp[i | (1 << k)][k] = n                 max(dp[i | (1 << k)][k], min(dp[i][j], b[j][k])); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) scanf( %d , &a[i][j]); n   if (n == 1) { n     int res = inf; n     for (int j = 0; j < m - 1; ++j) res = min(res, abs(a[0][j] - a[0][j + 1])); n     printf( %d n , res); n     return 0; n   } n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < n; ++j) n       if (i != j) { n         b[i][j] = c[i][j] = inf; n         for (int k = 0; k < m; ++k) n           b[i][j] = min(b[i][j], abs(a[i][k] - a[j][k])); n         for (int k = 0; k < m - 1; ++k) n           c[i][j] = min(c[i][j], abs(a[i][k] - a[j][k + 1])); n       } n   int res = 0; n   for (int i = 0; i < n; ++i) { n     solve(i); n     for (int j = 0; j < n; ++j) n       if (i != j) res = max(res, min(c[i][j], dp[(1 << n) - 1][j])); n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long m, loc, t; n long long now; n class SegTree { n  private: n   long long F2(long long k) { return 1 << (long long)ceil(log2(k)); } n   vector<long long> seg; n   void Merge(long long cur) { seg[cur] = seg[2 * cur] + seg[2 * cur + 1]; } n  n  public: n   SegTree(long long n) { n     m = F2(n); n     for (long long i = 0; i < 2 * m + 2; i++) seg.push_back(0); n   } n   void Add(long long loc, long long cur = 1, long long l = 1, long long r = m) { n     if (r < l || r < loc || loc < l) n       return; n     else if (l == r) n       return void(seg[cur]++); n     long long mid = (l + r) / 2; n     Add(loc, 2 * cur, l, mid); n     Add(loc, 2 * cur + 1, mid + 1, r); n     Merge(cur); n   } n   long long Get(long long a, long long b, long long cur = 1, long long l = 1, n                 long long r = m) { n     if (r < l || r < a || b < l) n       return 0; n     else if (a <= l && r <= b) n       return seg[cur]; n     long long mid = (l + r) / 2; n     long long x = Get(a, b, 2 * cur, l, mid); n     long long y = Get(a, b, 2 * cur + 1, mid + 1, r); n     return x + y; n   } n   long long BinarySearch(long long a, long long l = 1, long long r = now) { n     if (r < l) return a; n     long long mid = (l + r) / 2; n     if (!Get(a + 1, mid)) return max(mid, BinarySearch(a, mid + 1, r)); n     return BinarySearch(a, l, mid - 1); n   } n }; n vector<pair<long long, long long>> v; n int main() { n   long long q; n   cin >> q; n   while (q--) { n     long long n; n     cin >> n; n     now = n; n     loc = 0; n     long long a[n + 1]; n     v.clear(); n     SegTree s(n); n     for (long long i = 0; i < n; i++) { n       cin >> t; n       if (t < loc) s.Add(i + 1); n       loc = t; n       a[i + 1] = t; n     } n     loc = 2; n     long long ans = 1; n     long long last = 1, cur = 0; n     while (loc <= n) { n       if (last == 0) swap(last, cur), ans++; n       last--; n       long long now = s.BinarySearch(loc + 1); n       if (loc == n) n         now = loc; n       else if (a[loc + 1] < a[loc]) n         now = loc; n       cur += now - loc + 1; n       loc = now + 1; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, T, t[1005] = {0}, v; n int main() { n   std::ios::sync_with_stdio(false); n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &v); n     t[v]++; n   } n   scanf( %d , &T); n   int ans = 0, sum = 0; n   for (int i = 1; i <= 1000; i++) { n     sum += t[i]; n     if (i > T + 1) sum -= t[i - T - 1]; n     if (sum > ans) ans = sum; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int q; n   cin >> q; n   while (q--) { n     int n, k; n     cin >> n >> k; n     int s[n]; n     char c; n     for (int i = 0; i < n; i++) { n       cin >> c; n       if (c ==  R ) n         s[i] = 0; n       else if (c ==  G ) n         s[i] = 1; n       else n         s[i] = 2; n     } n     int counter1 = 0; n     int counter2 = 0; n     int counter3 = 0; n     int myMin = k; n     for (int l = 0; l < k; l++) { n       if (s[l] != l % 3) { n         counter1++; n       } n       if (s[l] != (l + 1) % 3) { n         counter2++; n       } n       if (s[l] != (l + 2) % 3) { n         counter3++; n       } n     } n     myMin = min(myMin, min(counter1, min(counter2, counter3))); n     for (int j = 0; j < n - k; j++) { n       if (j % 3 != s[j]) { n         counter1--; n       } n       if ((k + j) % 3 != s[j + k]) { n         counter1++; n       } n       if ((j + 1) % 3 != s[j]) { n         counter2--; n       } n       if ((k + j + 1) % 3 != s[j + k]) { n         counter2++; n       } n       if ((j + 2) % 3 != s[j]) { n         counter3--; n       } n       if ((k + j + 2) % 3 != s[j + k]) { n         counter3++; n       } n       myMin = min(myMin, min(counter1, min(counter2, counter3))); n     } n     cout << myMin << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int a, b; n   scanf( %d %d , &a, &b); n   int fc, uc, df, sum = 0; n   fc = a; n   uc = 0; n   df = b; n   while (1) { n     sum += fc; n     if (fc == 0 && uc < df) break; n     uc += fc; n     fc = uc / df; n     uc = uc % df; n   } n   printf( %d n , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long N; n bool ask(long long n) { n   cout <<  ?   << n << endl; n   bool res; n   cin >> res; n   return res; n } n void solve() { n   cin >> N; n   long long l = 1; n   long long r = N - 1; n   long long delta = 0; n   long long start = N; n   bool t = false; n   while (l <= r) { n     long long m = (l + r) >> 1; n     delta += t ? -m : m; n     start = min(start, N - delta); n     l = m + 1; n     t = !t; n   } n   l = 1; n   r = N - 1; n   t = false; n   long long ans = N; n   ask(start); n   while (l <= r) { n     long long m = (l + r) >> 1; n     long long ns = start + (t ? -m : m); n     if (ask(ns)) { n       ans = m; n       r = m - 1; n     } else { n       l = m + 1; n     } n     t = !t; n     start = ns; n   } n   cout <<  =   << ans << endl; n   return; n } n int main() { n   int T; n   cin >> T; n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   int in; n   cin >> n >> m; n   vector<int> v(n + 1, 0); n   for (int i = 1; i <= n; ++i) { n     cin >> in; n     v[i] = in; n   } n   int a, b; n   double c = 0; n   double ret = 0; n   for (int i = 0; i < m; ++i) { n     cin >> a >> b >> c; n     ret = max((double)((v[a] + v[b]) / c), ret); n   } n   cout << fixed << std::setprecision(10) << ret << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b, c = 0, ans = 0; n   scanf( %d%d%d , &n, &a, &b); n   for (int i = 0; i < n; i++) { n     int x; n     scanf( %d , &x); n     if (x == 1) { n       if (a > 0) n         a--; n       else if (b > 0) { n         b--; n         c++; n       } else if (c > 0) n         c--; n       else n         ans++; n     } else if (b > 0) n       b--; n     else n       ans += 2; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int MAXN = 405; n int n, ans = -1; n int c[MAXN]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &c[i]); n   for (int i = 2; i <= n; i++) { n     for (int j = i; j <= n; j++) { n       int p = c[i - 1] - 1, cnt = 1, w = c[j]; n       for (int k = i; k <= j; k++) { n         cnt += p / c[k]; n         w += p / c[k] * c[k]; n         p %= c[k]; n       } n       p = w; n       for (int k = 1; k <= n; k++) { n         cnt -= p / c[k]; n         p %= c[k]; n       } n       if (cnt < 0 && (ans == -1 || w < ans)) ans = w; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 100 + 13; n int N, M; n int s[maxN][maxN]; n void enlarge(int x, int y, int l) { n   int col = s[x][y]; n   for (int i = x; i <= x + l; i++) n     for (int j = y; j <= y + l; j++) s[i][j] = col; n   return; n } n void paint(int x, int y) { n   int col = 0; n   for (int i = 1; i <= 26; i++) { n     if (i != s[x - 1][y] && i != s[x][y - 1] && i != s[x][y + 1]) { n       col = i; n       break; n     } n   } n   s[x][y] = col; n   int r = M; n   for (int i = y + 1; i <= M; i++) { n     if (s[x][i] == col || s[x - 1][i] == col) { n       r = i - 1; n       break; n     } n     if (s[x][i]) { n       r = i - 1; n       break; n     } n     bool flag(false); n     for (int j = 1; j < col; j++) { n       if (j != s[x - 1][i]) { n         flag = true; n         r = i - 1; n         break; n       } n     } n     if (flag) break; n   } n   r -= y; n   r = min(N - x, r); n   enlarge(x, y, r); n   return; n } n int main() { n   scanf( %d%d , &N, &M); n   for (int i = 0; i <= N; i++) s[i][0] = 30; n   for (int i = 0; i <= M; i++) s[0][i] = 30; n   for (int i = 1; i <= N; i++) n     for (int j = 1; j <= M; j++) n       if (s[i][j] == 0) paint(i, j); n   for (int i = 1; i <= N; i++) { n     for (int j = 1; j <= M; j++) cout << (char)(s[i][j] - 1 +  A ); n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class TaskA { n  public: n   void solve(istream& in, ostream& out) { n     int n, m; n     in >> n >> m; n     int cell = m - 1; n     for (int i = 0; i < n; ++i) { n       if (i & 1) { n         for (int j = 0; j < m; ++j) out << (j == cell ?  #  :  . ); n         cell = (cell == m - 1 ? 0 : m - 1); n       } else n         for (int j = 0; j < m; ++j) out <<  # ; n       out <<   n ; n     } n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   TaskA solver; n   std::istream& in(std::cin); n   std::ostream& out(std::cout); n   solver.solve(in, out); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n vector<int> ps[200000]; n int sum[200000]; n int main() { n   int T; n   cin >> T; n   for (int t = 0; t < T; t++) { n     int n; n     cin >> n; n     for (int i = 0; i < n; i++) ps[i].clear(); n     for (int i = 0; i < n; i++) { n       int p, m; n       cin >> m >> p; n       ps[m].push_back(p); n     } n     sum[0] = 0; n     for (int i = 0; i + 1 < n; i++) { n       sum[i + 1] = sum[i] + ps[i].size(); n     } n     priority_queue<int, vector<int>, greater<int>> Q; n     int cnt = 0; n     long long ans = 0; n     for (int i = n - 1; i >= 0; i--) { n       for (int p : ps[i]) Q.push(p); n       int k = max(i - sum[i] - cnt, 0); n       for (int j = 0; j < k; j++) { n         ans += Q.top(); n         Q.pop(); n       } n       cnt += k; n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n pair<long long int, pair<int, bool> > p[200005 << 1]; n long long int a[200005], tree[200005 << 2][2]; n long long int get(int n, int b, int e, int i, int j, bool c) { n   if (i > j || j < b || e < i) return 0; n   if (i <= b && j >= e) return tree[n][c]; n   int l, r, m; n   m = (b + e) >> 1; n   l = (n << 1) + 1; n   r = l + 1; n   return max(get(l, b, m, i, j, c), get(r, m + 1, e, i, j, c)); n } n void update(int n, int b, int e, int i, long long int v, bool c) { n   if (i < b || i > e) return; n   if (b == e) { n     tree[n][c] = v; n     return; n   } n   int l, r, m; n   m = (b + e) >> 1; n   l = (n << 1) + 1; n   r = l + 1; n   update(l, b, m, i, v, c); n   update(r, m + 1, e, i, v, c); n   tree[n][c] = max(tree[l][c], tree[r][c]); n } n int main() { n   int n; n   long long int t; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %lld , &a[i]); n     p[i] = make_pair(a[i] - i, make_pair(i, 1)); n     if (i > 0) p[i + n - 1] = make_pair(a[i] - i + 1, make_pair(i - 1, 0)); n   } n   int m = 2 * n - 1; n   sort(p, p + m); n   for (int i = 0; i < m; i++) { n     t = get(0, 0, n - 1, 0, p[i].second.first - 1, 1); n     if (!p[i].second.second) n       t = max(t, get(0, 0, n - 1, 0, p[i].second.first - 1, 0)); n     update(0, 0, n - 1, p[i].second.first, t + 1, p[i].second.second); n   } n   printf( %lld n , max(0ll, n - 1 - max(tree[0][1], tree[0][0]))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long i, j, k, n, m, res, c1, c0; n long long a[200009]; n string s; n set<long long> st; n queue<long long> q; n int main() { n   int nt; n   cin >> nt; n   while (nt--) { n     cin >> s; n     s =  R  + s; n     s +=  R ; n     long long last = 0, res = 0; n     for (i = 0; i < s.length(); i++) { n       if (s[i] ==  R ) res = max(res, i - last), last = i; n     } n     cout << res <<   n ; n   } n   exit(0); n } n


#include <bits/stdc++.h> n using namespace std; n struct BIT { n   int a[400010], size, n, dl, dr, dir, len; n   inline void set(int x, int v) { n     int k = v - query(x, x); n     for (; x <= size; x += ((x) & (-(x)))) a[x] += k; n   } n   inline void add(int x, int v) { n     for (; x <= size; x += ((x) & (-(x)))) a[x] += v; n   } n   inline int query(int x) { n     int res = 0; n     for (; x; x -= ((x) & (-(x)))) res += a[x]; n     return res; n   } n   inline int query(int l, int r) { return query(r) - query(l - 1); } n   inline void ope1(int x) { n     if (x > len - x) dir = -dir, x = len - x; n     if (dir == 1) { n       dl += x; n       for (int i = 1; i <= x; i++) { n         add(dl + i, query(dl - i + 1, dl - i + 1)); n         set(dl - i + 1, 0); n       } n     } else { n       dr += x; n       for (int i = 1; i <= x; i++) { n         add(n - (dr + i) + 1, query(n - (dr - i), n - (dr - i))); n         set(n - (dr - i), 0); n       } n     } n     len = max(x, len - x); n   } n   inline void ope2(int l, int r) { n     if (dir == 1) n       printf( %d n , query(l + dl + 1, r + dl)); n     else n       printf( %d n , query(n - dr - r + 1, n - dr - l)); n   } n   void init(int N) { n     for (size = 1; size < N; size <<= 1) n       ; n     dl = dr = 0, dir = 1, len = n = N; n     for (int i = 1; i <= n; i++) set(i, 1); n   } n   void debug() { n     printf( =============== n ); n     for (int i = 1; i <= n; i++) printf( %d  , query(i, i)); n     printf(  n ); n   } n } a; n int n, q; n int main() { n   scanf( %d%d , &n, &q); n   a.init(n); n   for (int i = 1, ope, l, r, p; i <= q; i++) { n     scanf( %d , &ope); n     if (ope == 1) { n       scanf( %d , &p); n       a.ope1(p); n     } else { n       scanf( %d%d , &l, &r); n       a.ope2(l, r); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, nrE; n struct Dish { n   int p, s, b; n } d[100002]; n struct Person { n   int inc, pref; n } v[100002]; n struct Event { n   int x, y, z, t; n   bool operator<(const Event &ot) const { n     if (x == ot.x) return z > ot.z; n     return x < ot.x; n   } n } E[100002 * 3]; n unordered_map<int, int> Bit[2]; n int ans[100002]; n void Add(int t, int i, int val) { n   for (; i < 2 * 1000000005 && i > 0; i += (i & (-i))) Bit[t][i] += val; n } n int Sum(int t, int i) { n   int ret = 0; n   for (; i > 0; i -= (i & (-i))) ret += Bit[t][i]; n   return ret; n } n void readDish() { n   for (int i = 0; i < n; ++i) scanf( %d , &d[i].p); n   for (int i = 0; i < n; ++i) scanf( %d , &d[i].s); n   for (int i = 0; i < n; ++i) scanf( %d , &d[i].b); n } n void readPers() { n   for (int i = 0; i < m; ++i) scanf( %d , &v[i].inc); n   for (int i = 0; i < m; ++i) scanf( %d , &v[i].pref); n } n void solveEvents() { n   sort(E, E + nrE); n   for (int i = 0; i < nrE; ++i) { n     if (E[i].z <= 0) { n       int idx = -E[i].z; n       ans[idx] = Sum(0, v[idx].inc + v[idx].pref) + n                  Sum(1, v[idx].pref - v[idx].inc + 1000000005); n     } else { n       Add(0, E[i].x + E[i].y, 1 * E[i].t); n       Add(1, E[i].z - E[i].x + 1000000005, -1 * E[i].t); n     } n   } n } n int main() { n   scanf( %d%d , &n, &m); n   readDish(); n   readPers(); n   for (int i = 0; i < n + m; ++i) { n     if (i < n) { n       E[nrE++] = {d[i].p, d[i].b, d[i].b + 1, 1}; n       E[nrE++] = {d[i].s + 1, d[i].b - (d[i].s - d[i].p + 1), n                   d[i].b + (d[i].s - d[i].p + 1) + 1, -1}; n     } else n       E[nrE++] = {v[i - n].inc, v[i - n].pref, n - i, 0}; n   } n   solveEvents(); n   for (int i = 0; i < m; ++i) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void smin(T &a, U b) { n   if (a > b) a = b; n } n template <typename T, typename U> n inline void smax(T &a, U b) { n   if (a < b) a = b; n } n template <class T> n inline void gn(T &first) { n   char c, sg = 0; n   while (c = getchar(), (c >  9  || c <  0 ) && c !=  - ) n     ; n   for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ; n        c = getchar()) n     first = (first << 1) + (first << 3) + c -  0 ; n   if (sg) first = -first; n } n template <class T, class T1> n inline void gn(T &first, T1 &second) { n   gn(first); n   gn(second); n } n template <class T, class T1, class T2> n inline void gn(T &first, T1 &second, T2 &z) { n   gn(first); n   gn(second); n   gn(z); n } n template <class T> n inline void print(T first) { n   if (first < 0) { n     putchar( - ); n     return print(-first); n   } n   if (first < 10) { n     putchar( 0  + first); n     return; n   } n   print(first / 10); n   putchar(first % 10 +  0 ); n } n template <class T> n inline void printsp(T first) { n   print(first); n   putchar(   ); n } n template <class T> n inline void println(T first) { n   print(first); n   putchar(  n ); n } n template <class T, class U> n inline void print(T first, U second) { n   printsp(first); n   println(second); n } n template <class T, class U, class V> n inline void print(T first, U second, V z) { n   printsp(first); n   printsp(second); n   println(z); n } n int power(int a, int b, int m, int ans = 1) { n   for (; b; b >>= 1, a = 1LL * a * a % m) n     if (b & 1) ans = 1LL * ans * a % m; n   return ans; n } n const int C = 333333; n vector<int> adj[C]; n int root[C], sz[C], d[C], src; n pair<int, int> f; n int find_root(int u) { n   return (root[u] == u ? u : root[u] = find_root(root[u])); n } n void add(int u, int v) { n   int uu = find_root(u), vv = find_root(v); n   if (uu == vv) return; n   sz[vv] += sz[uu]; n   root[uu] = vv; n   smax(d[vv], max(d[uu], (1 + d[uu]) / 2 + (1 + d[vv]) / 2 + 1)); n } n void dfs(int u, int dp = 0) { n   root[u] = src; n   sz[src]++; n   if (dp > f.second) { n     f.second = dp; n     f.first = u; n   } n   for (int i = 0; i < adj[u].size(); i++) { n     int v = adj[u][i]; n     if (root[v]) continue; n     dfs(v, dp + 1); n   } n } n void rdfs(int u, int dp = 0, int fa = 0) { n   smax(d[src], dp); n   for (int i = 0; i < adj[u].size(); i++) { n     int v = adj[u][i]; n     if (v == fa) continue; n     rdfs(v, dp + 1, u); n   } n } n void solve_C() { n   int n, m, q, u, v; n   gn(n, m, q); n   for (int i = 0; i < m; i++) { n     gn(u, v); n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   for (int i = 1; i <= n; i++) n     if (!root[i]) { n       f.first = i; n       f.second = 0; n       src = i; n       dfs(i); n       rdfs(f.first); n     } n   while (q--) { n     int t; n     gn(t); n     if (t == 1) { n       gn(u); n       u = find_root(u); n       println(d[u]); n     } else { n       gn(u, v); n       add(u, v); n     } n   } n   exit(0); n } n int main() { n   solve_C(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n const long long MOD = 1000000007LL, INF = 1e9, LINF = 1e18; n const long double PI = 3.141592653589793116, EPS = 1e-9, n                   GOLD = ((1 + sqrt(5)) / 2); n template <class T> n int getbit(T s, int i) { n   return (s >> 1) & 1; n } n template <class T> n T onbit(T s, int i) { n   return s | (T(1) << i); n } n template <class T> n T offbit(T s, int i) { n   return s & (~(T(1) << i)); n } n template <class T> n int cntbit(T s) { n   return __builtin_popcount(s); n } n string s; n long long n; n vector<long long> cc(26, 0); n void VarInput() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cin >> s; n   n = s.size(); n } n void ProSolve() { n   for (long long i = 0; i < n; i++) cc[s[i] -  a ]++; n   long long appear = 0, onlyOne = 0; n   for (long long i = 0; i < 26; i++) { n     if (cc[i] > 0) appear++; n     if (cc[i] == 1) onlyOne++; n   } n   if (appear > 4) { n     cout <<  No ; n     return; n   } n   if (appear < 2) { n     cout <<  No ; n     return; n   } n   if (appear == 2 && onlyOne > 0) { n     cout <<  No ; n     return; n   } n   if (appear == 3 && onlyOne > 2) { n     cout <<  No ; n     return; n   } n   cout <<  Yes ; n } n int main() { n   VarInput(); n   ProSolve(); n } n


#include <bits/stdc++.h> n using namespace std; n const long long int maxn = 1e3 + 2; n const long long int mod = 1e3 + 2; n vector<long long int> d; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n, x, ans = INT_MAX; n   cin >> n >> x; n   long long int minx = x, maxx = x, a[maxn]; n   memset(a, 0, sizeof(a)); n   for (long long int i = 0; i < n; i++) { n     long long int start, stop; n     cin >> start >> stop; n     if (stop < start) swap(start, stop); n     for (long long int j = start; j <= stop; j++) ++a[j]; n   } n   for (long long int i = 0; i <= 1003; i++) n     if (a[i] == n) ans = min(abs(x - i), ans); n   if (ans == INT_MAX) n     cout <<  -1  << endl; n   else n     cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   int a, n2 = 0, n1 = 0; n   for (int i = 0; i < n; i++) cin >> a, a - 1 ? n2++ : n1++; n   if (n2) cout << 2 <<    ; n   if (n1) cout << 1 <<    ; n   for (int i = 0; i < n2 - 1; i++) cout << 2 <<    ; n   for (int i = 0; i < n1 - 1; i++) cout << 1 <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int>> res, res2, res3; n int x[10005], y[10005], id[35], b[35], vis[10005], r[10005]; n bitset<35> c[35], d[35], tmp; n int insert(int x, int y, bitset<35> now) { n   for (int i = 0; i <= 30; i++) n     if (x & (1 << i)) { n       if (!b[i]) { n         b[i] = x; n         id[i] = y; n         vis[y] = 1; n         d[i] = now; n         d[i][i] = 1; n         return i; n       } n       x ^= b[i]; n       now ^= d[i]; n     } n   return -1; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) n     scanf( %d , &x[i]), r[i] = insert(x[i], i, bitset<35>()); n   for (int i = 1; i <= n; i++) scanf( %d , &y[i]); n   for (int i = 1; i <= n; i++) { n     int now = y[i]; n     for (int j = 0; j <= 30; j++) n       if (now & (1 << j)) { n         now ^= b[j]; n         if (~r[i]) c[r[i]] ^= d[j]; n       } n     if (now) { n       puts( -1 ); n       return 0; n     } n   } n   for (int i = 1; i <= n; i++) n     if (!vis[i]) { n       int now = y[i] ^ x[i]; n       tmp.reset(); n       for (int j = 0; j <= 30; j++) n         if (now & (1 << j)) now ^= b[j], tmp ^= d[j]; n       for (int j = 0; j <= 30; j++) n         if (tmp[j]) res.push_back({i, id[j]}); n     } n   for (int i = 0; i <= 30; i++) { n     if (!c[i][i]) n       for (int j = 0; j <= 30; j++) n         if (c[j][i] && ((!c[j][j]) || j > i)) { n           res2.push_back({id[i], id[j]}); n           res2.push_back({id[j], id[i]}); n           res2.push_back({id[i], id[j]}); n           swap(c[i], c[j]); n           break; n         } n     if (!c[i][i]) continue; n     for (int j = 0; j <= 30; j++) n       if (i != j && c[j][i]) { n         c[j] ^= c[i]; n         res2.push_back({id[j], id[i]}); n       } n   } n   for (int i = 1; i <= 30; i++) n     for (int j = 0; j < i; j++) n       if (c[j][i]) res3.push_back({id[j], id[i]}); n   for (int i = 0; i <= 30; i++) n     if ((id[i]) && (!(c[i][i]))) res2.push_back({id[i], id[i]}); n   printf( %d n , (int)(res.size() + res2.size() + res3.size())); n   reverse(res2.begin(), res2.end()); n   for (auto i : res) printf( %d %d n , i.first, i.second); n   for (auto i : res3) printf( %d %d n , i.first, i.second); n   for (auto i : res2) printf( %d %d n , i.first, i.second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void out(T x) { n   cout << x << endl; n   exit(0); n } n using ll = long long; n using ld = long double; n const int maxn = 1e6 + 5; n int n, r; n ll c[maxn]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout << fixed << setprecision(10); n   ll sum = 0; n   cin >> n >> r; n   for (int i = 0; i < 1 << n; i++) { n     cin >> c[i]; n     sum += c[i]; n   } n   ld res = 1.0 * sum / (1 << n); n   cout << res <<   n ; n   while (r--) { n     int i, g; n     cin >> i >> g; n     sum -= c[i]; n     c[i] = g; n     sum += c[i]; n     ld res = 1.0 * sum / (1 << n); n     cout << res <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, row, column; n int a[5005]; n int cnt[1005]; n int main() { n   int i, j, k, l; n   int x, y; n   int tc, t; n   int res = 0; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     cnt[a[i]]++; n   } n   j = *(max_element(cnt, cnt + 1001)); n   if (j > (n + 1) / 2) n     puts( NO ); n   else n     puts( YES ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5010, Q = 100010; n int n, q, ac; n int a[N][N * 2]; n int ans[Q]; n vector<pair<pair<int, int>, pair<int, int>>> seq; n void ch(int j, int k, int x) { n   for (; j <= n; j += j & -j) n     for (int l = k; l <= n * 2; l += l & -l) a[j][l] += x; n } n int qry(int j, int k) { n   int ret = 0; n   for (; j; j &= j - 1) n     for (int l = k; l; l &= l - 1) ret += a[j][l]; n   return ret; n } n void add1(int j, int k, int x) { n   seq.emplace_back(make_pair(j, k), make_pair(1, x)); n } n void add2(int j, int k, int x) { n   seq.emplace_back(make_pair(j, k + j), make_pair(2, x)); n } n void add3(int j, int k, int x) { n   seq.emplace_back(make_pair(j, k - j + n), make_pair(3, x)); n } n int main() { n   scanf( %d%d , &n, &q); n   while (q--) { n     int opt; n     scanf( %d , &opt); n     if (opt == 1) { n       int d, x, y, l; n       scanf( %d%d%d%d , &d, &x, &y, &l); n       if (d == 1) { n         add1(x, y, 1); n         add2(x, y + l + 1, -1); n         add2(x + l + 1, y, 1); n         add1(x + l + 1, y, -1); n       } else if (d == 2) { n         add3(x, y - l, 1); n         add1(x, y + 1, -1); n         add1(x + l + 1, y + 1, 1); n         add3(x + l + 1, y + 1, -1); n       } else if (d == 3) { n         add1(x - l, y, 1); n         add3(x - l, y + 1, -1); n         add1(x + 1, y, -1); n         add3(x + 1, y + l + 2, 1); n       } else { n         add2(x - l, y, 1); n         add1(x - l, y + 1, -1); n         add2(x + 1, y - l - 1, -1); n         add1(x + 1, y + 1, 1); n       } n     } else { n       int x, y; n       scanf( %d%d , &x, &y); n       ++ac; n       seq.emplace_back(make_pair(x, y), make_pair(-1, ac)); n       seq.emplace_back(make_pair(x, y + x), make_pair(-2, ac)); n       seq.emplace_back(make_pair(x, y - x + n), make_pair(-3, ac)); n     } n   } n   for (int id = 1; id <= 3; ++id) { n     memset(a, 0, sizeof(a)); n     for (const auto& pr : seq) n       if (pr.second.first == id) n         ch(pr.first.first, pr.first.second, pr.second.second); n       else if (pr.second.first == -id) n         ans[pr.second.second] += qry(pr.first.first, pr.first.second); n   } n   for (int i = 1; i <= ac; ++i) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int p[105]; n   int n, m, i, j, t; n   memset(p, 0, sizeof(p)); n   scanf( %d%d , &n, &m); n   for (i = 0; i < m; i++) { n     scanf( %d , &t); n     for (j = t; p[j] == 0; j++) p[j] = t; n   } n   for (i = 1; i < n; i++) printf( %d  , p[i]); n   printf( %d n , p[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long int> odds; n int main() { n   int n; n   scanf( %d , &n); n   long long int sum = 0; n   int length_odds = 0; n   for (int x = 0; x < n; x++) { n     long long int num; n     scanf( %lld , &num); n     if (num % 2 == 0) { n       sum += num; n     } else { n       odds.push_back(num); n       length_odds++; n     } n   } n   sort(odds.begin(), odds.end()); n   reverse(odds.begin(), odds.end()); n   for (int x = 1; x < length_odds;) { n     sum += odds[0] + odds[1]; n     odds.erase(odds.begin()); n     odds.erase(odds.begin()); n     x += 2; n   } n   printf( %lld , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef struct _Box { n   int pos, weight; n } Box; n Box arr[200010]; n long long int bit1[200010], bit2[200010]; n const int mod = 1e9 + 7; n int num; n int getLowbit(int n) { return n & (-n); } n void add(long long int* bitArr, int pos, long long int val, bool shouldMod) { n   if (shouldMod) val = (val + mod) % mod; n   for (int i = pos; i <= num; i += getLowbit(i)) { n     bitArr[i] += val; n     if (shouldMod) bitArr[i] = (bitArr[i] + mod) % mod; n   } n } n long long int getPrefixSum(long long int* bitArr, int pos, bool shouldMod) { n   long long int ans = 0; n   for (int i = pos; i > 0; i -= getLowbit(i)) { n     ans += bitArr[i]; n     if (shouldMod) ans = (ans + mod) % mod; n   } n   return ans; n } n long long int getRangeSum(long long int* bitArr, int qLeftPt, int qRightPt, n                           bool shouldMod) { n   if (qRightPt < qLeftPt) return 0; n   long long int ans = getPrefixSum(bitArr, qRightPt, shouldMod) - n                       getPrefixSum(bitArr, qLeftPt - 1, shouldMod); n   if (shouldMod) ans = (ans + mod) % mod; n   return ans; n } n int getMidPos(int qLeftPt, int qRightPt) { n   int leftPt = qLeftPt, rightPt = qRightPt; n   int ans = rightPt; n   while (leftPt <= rightPt) { n     int midPt = (leftPt + rightPt) >> 1; n     if (getRangeSum(bit1, qLeftPt, midPt, false) >= n         getRangeSum(bit1, midPt + 1, qRightPt, false)) { n       ans = midPt; n       rightPt = midPt - 1; n     } else { n       leftPt = midPt + 1; n     } n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   memset(bit1, 0, sizeof(bit1)); n   memset(bit2, 0, sizeof(bit2)); n   int qNum; n   cin >> num >> qNum; n   for (int i = 1; i <= num; i++) { n     cin >> arr[i].pos; n   } n   for (int i = 1; i <= num; i++) { n     cin >> arr[i].weight; n     add(bit1, i, arr[i].weight, false); n     add(bit2, i, (long long int)arr[i].weight * (arr[i].pos - i), true); n   } n   while (qNum--) { n     int x, y; n     cin >> x >> y; n     if (x < 0) { n       x = -x; n       add(bit1, x, y - arr[x].weight, false); n       add(bit2, x, ((long long int)y - arr[x].weight) * (arr[x].pos - x), true); n       arr[x].weight = y; n     } else { n       if (x == y) { n         cout << 0 << endl; n         continue; n       } n       int midPos = getMidPos(x, y); n       long long int leftAns = (getRangeSum(bit1, x, midPos, true) * n                                    abs(arr[midPos].pos - midPos) % mod - n                                getRangeSum(bit2, x, midPos, true) + mod) % n                               mod; n       long long int rightAns = (getRangeSum(bit1, midPos, y, true) * n                                     abs(arr[midPos].pos - midPos) % mod - n                                 getRangeSum(bit2, midPos, y, true) + mod) % n                                mod; n       long long int ans = (leftAns - rightAns + mod) % mod; n       cout << ans << endl; n     } n   } n   return 0; n } n