#include <bits/stdc++.h>   using namespace std;   vector<int> v[100009];   map<int, int> mp;   int main() {     int n, m, x, y, index = 1;     scanf( %d%d , &n, &m);     for (int i = 0; i < m; i++) {       scanf( %d%d , &x, &y);       if (mp[x] == 0) {         mp[x] = index;         v[index++].push_back(y);       } else         v[mp[x]].push_back(y);       if (mp[y] == 0) {         mp[y] = index;         v[index++].push_back(x);       } else         v[mp[y]].push_back(x);     }     int bus_out = 0, bus_in = 0, ring = 0, s, cen_c = 0, cen_out = 0;     for (int i = 1; i < index; i++) {       s = v[i].size();       if (s == 1) {         cen_out++;         bus_out++;       } else if (s == 2) {         bus_in++;         ring++;       } else {         cen_c++;       }     }     if (n < m) {       printf( unknown topology n );     } else if (n - 1 == m && bus_out == 2 && bus_in == n - 2) {       printf( bus topology n );     } else if (ring == n && n == m) {       printf( ring topology n );     } else if (n == m + 1 && cen_c == 1 && cen_out == n - 1) {       printf( star topology n );     } else {       printf( unknown topology n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mxn = 2e5 + 5;   long long a[1001][1001];   int main() {     int t;     cin >> t;     while (t--) {       int n, m;       cin >> n >> m;       for (int i = 1; i <= n; ++i) {         for (int j = 1; j <= m; ++j) {           cin >> a[i][j];           if ((i + j) & 1 && !(a[i][j] & 1))             ++a[i][j];           else if (!((i + j) & 1) && a[i][j] & 1)             ++a[i][j];           cout << a[i][j] <<    ;         }         cout << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long LINF = 0x3f3f3f3f3f3f3f3f;   const int MOD = (int)1e9 + 7;   const int N = (int)1e6 + 7;   struct node {     long long x, y, a;   } a[N];   int n;   long long dp[N];   struct P {     long long x, y;     P() {}     P(long long x, long long y) : x(x), y(y) {}     P operator-(const P &rhs) const { return P(x - rhs.x, y - rhs.y); }     bool operator^(const P &rhs) const {       return (double)x * rhs.y > (double)y * rhs.x;     }   };   void Init() {     for (int i = (1); i < (n + 1); ++i)       scanf( %I64d%I64d%I64d , &a[i].x, &a[i].y, &a[i].a);     sort(a + 1, a + n + 1, [&](node a, node b) { return a.x < b.x; });   }   int Solve() {     deque<P> Q;     Q.push_back(P((a[0].x), (dp[0])));     for (int i = (1); i < (n + 1); ++i) {       while ((int)(Q).size() > 1 &&              (Q[1].y - Q[0].y) > (Q[1].x - Q[0].x) * (a[i].y))         Q.pop_front();       dp[i] = Q[0].y - (a[i].y) * Q[0].x + a[i].x * a[i].y - a[i].a;       P aa((a[i].x), (dp[i]));       while ((int)(Q).size() > 1 && !((aa - Q[(int)(Q).size() - 2]) ^                                       (Q.back() - Q[(int)(Q).size() - 2])))         Q.pop_back();       Q.push_back(aa);     }     long long ans = 0;     for (int i = (1); i < (n + 1); ++i) ans = max(ans, dp[i]);     return printf( %I64d n , ans);   }   int main() {     while (~scanf( %d , &n)) {       Init();       Solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long LINF = 1001002003004005006ll;   int dx[] = {1, 0, -1, 0};   int dy[] = {0, 1, 0, -1};   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   template <class T>   bool chmax(T &a, const T &b) {     if (a < b) {       a = b;       return 1;     }     return 0;   }   template <class T>   bool chmin(T &a, const T &b) {     if (b < a) {       a = b;       return 1;     }     return 0;   }   signed main() {     cin.tie(0);     ios::sync_with_stdio(0);     long long n;     cin >> n;     string s;     cin >> s;     if (n == 1) {       cout << 0 << endl;       return 0;     }     vector<long long> v;     long long i = 0;     while (i < n) {       char tmp = s[i];       long long cnt = 0;       while (i + cnt < n and s[i + cnt] == tmp) {         cnt++;       }       v.push_back(cnt);       i += cnt;     }     long long m = v.size();     if (m == 1) {       long long ans = n * (n + 1) / 2 - n;       cout << ans << endl;       return 0;     }     long long diff = 0;     for (long long i = 0; i < m; i++) {       diff += 2 * v[i];     }     diff -= v[0];     diff -= v[m - 1];     diff -= m - 1;     long long ans = n * (n + 1) / 2 - diff - n;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long mypow3(long long p, long long mod) {     long long rez = 1, x = 3;     while (p > 0) {       if (p % 2 == 1) rez = (rez * x) % mod;       x = (x * x) % mod;       p /= 2;     }     return rez;   }   int main() {     long long n, m, ans;     ios_base::sync_with_stdio(false);     cin >> n >> m;     ans = mypow3(n, m) - 1;     if (ans == -1) ans = m - 1;     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a, b, c[101], d[101], mx = -1e9, cnt;   int main() {     cin >> a >> b;     for (int i = 1; i <= a; ++i) {       cin >> c[i];     }     for (int i = 1; i < a; ++i) {       d[i] = c[i] - c[i + 1] - b;     }     for (int i = 1; i <= a; ++i) {       mx = max(d[i], mx);     }     cout << mx;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   constexpr int inf = 1e9;   using P = pair<int, int>;   template <typename T>   void printv(const vector<T> &v) {     int sz = v.size();     for (int i = 0; i < sz; i++) {       cout << v[i] <<    n [i == sz - 1];     }   }   using Graph = vector<vector<int>>;   const int MAX_N = 3010;   Graph g(MAX_N);   vector<vector<int>> dist(MAX_N, vector<int>(MAX_N, inf));   vector<vector<int>> ch(MAX_N, vector<int>(MAX_N));   int dfs(int r, int cur, int pre) {     ch[r][cur] = 1;     for (auto nxt : g[cur]) {       if (nxt == pre) continue;       ch[r][cur] += dfs(r, nxt, cur);     }     return ch[r][cur];   }   int main() {     cin.tie(0);     ios::sync_with_stdio(false);     int n;     cin >> n;     vector<P> ps;     for (int i = 0; i < n - 1; i++) {       int u, v;       cin >> u >> v;       u--;       v--;       ps.push_back(P(u, v));       g[u].push_back(v);       g[v].push_back(u);     }     for (int i = 0; i < n; i++) {       queue<int> que;       que.push(i);       dist[i][i] = 0;       while (!que.empty()) {         int v = que.front();         que.pop();         for (auto nxt : g[v]) {           if (dist[i][nxt] != inf) continue;           dist[i][nxt] = dist[i][v] + 1;           que.push(nxt);         }       }     }     for (int i = 0; i < n; i++) {       dfs(i, i, -1);     }     vector<vector<P>> cs(n);     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         cs[dist[i][j]].push_back(P(i, j));       }     }     vector<vector<ll>> dp(n, vector<ll>(n));     for (int d = 0; d < n - 1; d++) {       for (auto p : cs[d]) {         int u = p.first, v = p.second;         for (auto nxt : g[u]) {           if (dist[u][nxt] + dist[u][v] != dist[v][nxt]) continue;           ll x = ch[nxt][v], y = ch[u][nxt];           dp[nxt][v] = max(dp[nxt][v], dp[u][v] + x * y);           dp[v][nxt] = max(dp[v][nxt], dp[u][v] + x * y);         }         for (auto nxt : g[v]) {           if (dist[v][nxt] + dist[u][v] != dist[u][nxt]) continue;           ll x = ch[nxt][u], y = ch[v][nxt];           dp[nxt][u] = max(dp[nxt][u], dp[u][v] + x * y);           dp[u][nxt] = max(dp[u][nxt], dp[u][v] + x * y);         }       }     }     ll ret = 0;     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++) ret = max(ret, dp[i][j]);     cout << ret << endl;   }


#include <bits/stdc++.h>   const long double pie = 3.1415926535;   using namespace std;   bool br1, br2, br;   long long n, k, x, y, z, mini, maxi, l, ind, ini, sum, t, len, r, q, imini, w,       m;   string s, s1, s2;   struct Point {     long long X, Y;     static long long getDir(Point a, Point b, Point c) {       return ((b.X - a.X) * (c.Y - a.Y) - (b.Y - a.Y) * (c.X - a.X));     }   };   void solve() {     Point A, B, C;     cin >> A.X >> A.Y >> B.X >> B.Y >> C.X >> C.Y;     m = A.getDir(A, B, C);     if (m == 0)       cout <<  TOWARDS  << endl;     else if (m > 0)       cout <<  LEFT  << endl;     else       cout <<  RIGHT  << endl;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 5e5 + 5, M = 65;   long long n, q, d = 60, fa[N], opt, x, s[M];   double f[N][M];   void update(long long v) {     long long top = 0, x = v;     for (long long i = 0; i <= d; i++) {       if (!x) break;       s[top++] = x, x = fa[x];     }     top--;     for (long long i = top; i >= 2; i--)       f[s[i]][i] /= 0.5 * (1 + f[s[i - 1]][i - 1]);     for (long long i = 1; i <= d; i++) f[v][i] = 1;     for (long long i = 1; i <= top; i++)       f[s[i]][i] *= 0.5 * (1 + f[s[i - 1]][i - 1]);   }   double query(long long v) {     double ans = 0;     for (long long i = 1; i < d; i++) ans += i * (f[v][i + 1] - f[v][i]);     return ans;   }   signed main() {     scanf( %lld , &q), n = 1;     for (long long i = 1; i <= d; i++) f[1][i] = 1;     while (q--) {       scanf( %lld%lld , &opt, &x);       if (opt == 1)         n++, fa[n] = x, update(n);       else         printf( %.10lf n , query(x));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MaxN = 2e5 + 10;   const int MOD = 1e9 + 7;   const int INF = 1e9;   int main() {     int L, n, p, t;     cin >> L >> n >> p >> t;     if (n == 0) {       cout << 0 <<   n ;       exit(0);     }     vector<pair<int, int> > seg(n);     for (int i = 0; i < n; ++i) {       cin >> seg[i].first >> seg[i].second;     }     sort(seg.begin(), seg.end());     set<pair<int, int> > st;     map<int, int> f;     for (int i = 0; i < n; ++i) {       st.insert(make_pair(seg[i].first, i));     }     int ans = 0;     while (!st.empty()) {       int x = st.begin()->first, where = st.begin()->second;       ans = max(ans, f[x]);       st.erase(st.begin());       int ex = seg[where].second;       if (where + 1 < (int)seg.size()) {         f[seg[where + 1].first] = max(f[seg[where + 1].first], f[x]);       }       int can = (ex - x) / p;       int val = f[x] + can;       ans = max(ans, f[x] + can);       if (where + 1 < n && f[seg[where + 1].first] >= val) {         continue;       }       x += (ex - x) / p * p + t;       if (where + 1 < n && seg[where + 1].first >= x) {         f[seg[where + 1].first] = max(f[seg[where + 1].first], val);         continue;       }       int pos = lower_bound(seg.begin(), seg.end(), make_pair(x, INF)) -                 seg.begin() - 1;       if (pos < n && seg[pos].first <= x && x <= seg[pos].second) {         st.insert(make_pair(x, pos));         f[x] = max(f[x], val);       } else if (pos + 1 != n) {         f[seg[pos + 1].first] = max(f[seg[pos + 1].first], val);       }     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   vector<string> cards;   map<vector<string>, bool> used;   void rec(vector<string>& x) {     if (x.size() == 1) {       puts( YES );       exit(0);     }     if (used[x] == true) return;     used[x] = true;     int p = x.size() - 1;     if (x[p][0] == x[p - 1][0] || x[p][1] == x[p - 1][1]) {       string s = x[p - 1];       string c = x[p];       x[p - 1] = x[p];       x.pop_back();       rec(x);       x[p - 1] = s;       x.push_back(c);     }     if (p > 2 && (x[p][0] == x[p - 3][0] || x[p][1] == x[p - 3][1])) {       string s = x[p - 3];       string c = x[p];       x[p - 3] = x[p];       x.pop_back();       rec(x);       x[p - 3] = s;       x.push_back(c);     }   }   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       string s;       cin >> s;       cards.push_back(s);     }     rec(cards);     puts( NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   using pii = pair<int, int>;   const int INF = (int)1e9;   struct Car {     int x = INF;     ll time = 0;     int id = -1;     Car() = default;     Car(int xx, ll t, int i) : x(xx), time(t), id(i) {}   };   Car get_best(const Car& a, const Car& b, int x) {     int d1 = abs(x - a.x);     int d2 = abs(x - b.x);     if (d1 != d2) return d1 < d2 ? a : b;     if (a.time != b.time) return a.time < b.time ? a : b;     return a.id < b.id ? a : b;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(nullptr);     map<int, set<pair<ll, int>>> waiting;     int n, k, m;     cin >> n >> k >> m;     for (int i = 0; i < k; i++) {       int x;       cin >> x;       waiting[x].insert({0, i});     }     ll curt = 0;     map<ll, vector<pii>> events;     for (int i = 0; i < m; i++) {       ll t, a, b;       cin >> t >> a >> b;       curt = max(curt, t);       while (events.size() && events.begin()->first <= curt) {         auto e = *events.begin();         events.erase(events.begin());         for (auto p : e.second) waiting[p.first].insert({e.first, p.second});       }       if (waiting.empty()) {         assert(events.size());         auto e = *events.begin();         events.erase(events.begin());         for (auto p : e.second) waiting[p.first].insert({e.first, p.second});         curt = e.first;       }       assert(waiting.size());       Car L;       Car R;       auto it = waiting.lower_bound(a);       if (it != waiting.end())         L = Car(it->first, it->second.begin()->first, it->second.begin()->second);       if (it != waiting.begin()) {         --it;         R = Car(it->first, it->second.begin()->first, it->second.begin()->second);       }       Car best = get_best(L, R, a);       assert(best.id != -1);       cout << best.id + 1 <<     << curt - t + abs(a - best.x) <<   n ;       waiting[best.x].erase({best.time, best.id});       if (waiting[best.x].empty()) waiting.erase(best.x);       events[curt + abs(a - best.x) + abs(a - b)].push_back({b, best.id});     }   }


#include <bits/stdc++.h>   const int MX = 2e6 + 7;   const int N = 1e9 + 1;   const int mod = 998244353;   const double pi = acos(-1);   double esp = 1e-13;   using namespace std;   long long qpow(long long a, long long b, long long MOD = mod) {     for (long long ans = 1;; a = a * a % MOD, b >>= 1) {       if (b & 1) ans = ans * a % MOD;       if (!b) return ans;     }   }   long long inv(long long a, long long MOD = mod) {     return qpow(a, MOD - 2, MOD);   }   long long exgcd(long long a, long long b, long long &x, long long &y) {     if (b == 0) {       x = 1, y = 0;       return a;     }     long long ret = exgcd(b, a % b, y, x);     y -= a / b * x;     return ret;   }   long long getInv(int a, int mod) {     long long x, y;     long long d = exgcd(a, mod, x, y);     return d == 1 ? (x % mod + mod) % mod : -1;   }   long long add(long long a, long long b) {     long long ans = (a + b) % mod;     return ans >= 0 ? ans : ans + mod;   }   void mul(long long &a, long long b) { a = a % mod * (b % mod) % mod; }   int main() {     ios::sync_with_stdio(0), cin.tie(0);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       priority_queue<pair<int, int>> Q;       for (int i = 1; i <= n; i++) {         int x;         cin >> x;         Q.push(make_pair(x, i));       }       int tmp = 0, res = 0;       while (!Q.empty()) {         auto x = Q.top();         Q.pop();         if (x.second == res) {           if (Q.empty()) {             break;           } else {             tmp++;             auto y = Q.top();             Q.pop();             y.first--;             if (y.first != 0) Q.push(y);             res = y.second;             Q.push(x);           }         } else {           tmp++;           x.first--;           if (x.first != 0) Q.push(x);           res = x.second;         }       }       cout << (tmp % 2 == 0 ?  HL  :  T ) <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   int R[101];   double dp[100100], sum[100100];   int main() {     scanf( %d%d , &n, &m);     if (m == 1) {       puts( 1 );       return 0;     }     int score = 0;     dp[0] = 1;     double f = 1. / (m - 1);     for (int i = 0; i < n; ++i) {       scanf( %d , R + i);       score += R[i];     }     for (int i = 0; i < n; ++i) {       int r = R[i];       sum[0] = dp[0];       for (int j = 1; j <= min(score - 1, (i + 1) * m); ++j) {         sum[j] = sum[j - 1] + dp[j];       }       for (int j = min(score - 1, (i + 1) * m); j > 0; --j) {         dp[j] = sum[j - 1];         if (j - m - 1 >= 0) dp[j] -= sum[j - m - 1];         if (j - r >= 0) dp[j] -= dp[j - r];         dp[j] *= f;       }       dp[0] = 0;     }     double res = 1;     for (int i = 0; i < score; ++i) {       res += dp[i] * (m - 1);     }     printf( %.15lf n , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200010;   const int MOD = 1000000007;   const int INF = 0x3fffffff;   typedef struct Node {     long long v;     int p;     Node(long long v_, int p_) {       v = v_;       p = p_;     }     bool operator<(const Node& other) const {       return v < other.v || (v == other.v && p > other.p);     }   } Node;   long long a[N];   int main() {     ios::sync_with_stdio(false);     int n, k;     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; ++i) {       scanf( %I64d , &a[i]);       a[i] += a[i - 1];     }     priority_queue<Node> Q;     Q.push(Node(a[n] - a[n - k], n + 1 - k));     long long ans = 0;     int ansa, ansb;     int q = n + 1 - k;     for (int i = n + 1 - k - k; i > 0; --i) {       if (ans <= Q.top().v + a[i + k - 1] - a[i - 1]) {         ans = Q.top().v + a[i + k - 1] - a[i - 1];         ansa = i;         ansb = Q.top().p;       }       --q;       Q.push(Node(a[q + k - 1] - a[q - 1], q));     }     printf( %d %d n , ansa, ansb);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 105;   int n;   int a[N];   int sum = 0;   char s[N];   int main() {     scanf( %d , &n);     scanf( %s , s + 1);     for (int i = 1; i <= n; ++i) {       a[i] = s[i] -  0 ;       sum += a[i];     }     if (sum == 0) {       puts( YES );       return 0;     }     for (int i = 2; i <= sum; ++i) {       if (sum % i == 0) {         bool flag = 1;         int cnt = 0, tot = sum / i, ret = 0;         for (int j = 1; j <= n; ++j) {           ret += a[j];           if (ret > tot) {             flag = 0;             break;           } else if (ret == tot) {             ++cnt;             ret = 0;           }         }         if (cnt != i) flag = 0;         if (flag) {           printf( YES );           return 0;         }       }     }     puts( NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n;     scanf( %lld , &n);     long long sum[n + 5], ans[n + 5], a;     cout <<  ? 1 2  << endl;     cin >> sum[1];     cout <<  ? 2 3  << endl;     cin >> sum[2];     cout <<  ? 1 3  << endl;     cin >> sum[3];     ans[1] = (sum[1] + sum[3] - sum[2]) / 2;     ans[2] = sum[1] - ans[1];     ans[3] = sum[3] - ans[1];     for (int i = 4; i <= n; i++) {       cout <<  ?   << i - 1 <<     << i << endl;       cin >> sum[i];       ans[i] = sum[i] - ans[i - 1];     }     cout <<  ! ;     for (int i = 1; i <= n; i++) {       cout <<     << ans[i];     }     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   double const eps = 1e-7;   struct Edge {     int dest, flow, ctpt;     double cost;     Edge(int _dest, int _flow, double _cost, int _ctpt)         : dest(_dest), flow(_flow), cost(_cost), ctpt(_ctpt) {}   };   vector<Edge> edges[2004];   int source = 2002, sink = 2003, from[2004];   double dist[2004], p[2000], u[2000];   bool inq[2004];   vector<Edge>::iterator edge_from[2004];   inline void insert(int from, int to, int flow, double cost) {     edges[from].push_back(Edge(to, flow, cost, edges[to].size()));     edges[to].push_back(Edge(from, 0, -cost, edges[from].size() - 1));   }   inline bool can_augment() {     static queue<int> q;     vector<Edge>::iterator it;     int x;     memset(dist, 0xf0, sizeof dist);     dist[source] = 0.0;     inq[source] = true;     q.push(source);     while (!q.empty()) {       inq[x = q.front()] = false;       q.pop();       for (it = edges[x].begin(); it != edges[x].end(); ++it)         if (it->flow && dist[it->dest] < dist[x] + it->cost - eps) {           dist[it->dest] = dist[x] + it->cost;           from[it->dest] = x;           edge_from[it->dest] = it;           if (!inq[it->dest]) {             inq[it->dest] = true;             q.push(it->dest);           }         }     }     return dist[sink] > -1e60;   }   double augment() {     int i, flow = 0x3f3f3f3f;     double cost = 0.0;     for (i = sink; i != source; i = from[i]) flow = min(flow, edge_from[i]->flow);     for (i = sink; i != source; i = from[i]) {       cost += edge_from[i]->cost;       edge_from[i]->flow -= flow;       edges[i][edge_from[i]->ctpt].flow += flow;     }     return flow * cost;   }   int main() {     int n, a, b, i, A = 2000, B = 2001;     double ans = 0.0;     ios_base::sync_with_stdio(false);     cin >> n >> a >> b;     for (i = 0; i < n; ++i) cin >> p[i];     for (i = 0; i < n; ++i) cin >> u[i];     for (i = 0; i < n; ++i) {       insert(A, i, 1, p[i]);       insert(B, i, 1, u[i]);       insert(i, sink, 1, 0.0);       insert(i, sink, 1, -p[i] * u[i]);     }     insert(source, A, a, 0.0);     insert(source, B, b, 0.0);     while (can_augment()) ans += augment();     cout << fixed << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     vector<int> v = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47};     int pos = -1, cnt = 0;     for (int i = 0; i < v.size(); i++) {       cout << v[i] << endl;       string s;       cin >> s;       if (s ==  yes ) pos = i, cnt++;     }     int f = 0;     if (cnt > 1)       f = 1;     else if (pos != -1 && v[pos] * v[pos] <= 100) {       cout << v[pos] * v[pos] << endl;       string s;       cin >> s;       if (s ==  yes ) f = 1;     }     if (f)       cout <<  composite  << endl;     else       cout <<  prime  << endl;   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2010;   int a[maxn], b[maxn], tot, n, m;   pair<int, int> ans1, ans21, ans22;   pair<long long, pair<int, int> > two[maxn * maxn];   int main(void) {     long long s = 0, s2 = 1e18, s3 = 1e18;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]), s += a[i];     scanf( %d , &m);     for (int i = 1; i <= m; i++) scanf( %d , &b[i]), s -= b[i];     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         long long tem = abs(s + 2 * b[j] - 2 * a[i]);         if (tem < s2) s2 = tem, ans1 = make_pair(i, j);       }     for (int i = 1; i <= m; i++)       for (int j = i + 1; j <= m; j++)         two[++tot] = make_pair(2ll * b[i] + 2ll * b[j], make_pair(i, j));     sort(two + 1, two + 1 + tot);     for (int i = 1; i <= n; i++)       for (int j = i + 1; j <= n; j++) {         long long tmp = 2ll * a[i] + 2ll * a[j] - s;         int p =             lower_bound(two + 1, two + 1 + tot, make_pair(tmp, make_pair(0, 0))) -             two - 1;         for (int t = max(1, p - 2); t <= min(tot, p + 2); t++) {           long long tem = abs(s + two[t].first - 2ll * a[i] - 2ll * a[j]);           if (tem < s3) {             s3 = tem;             ans21 = make_pair(i, two[t].second.first);             ans22 = make_pair(j, two[t].second.second);           }         }       }     long long ans = min(min(abs(s), abs(s2)), abs(s3));     printf( %lld n , ans);     if (ans == s)       printf( 0 n );     else if (ans == abs(s2))       printf( 1 n%d %d n , ans1.first, ans1.second);     else       printf( 2 n%d %d n%d %d n , ans21.first, ans21.second, ans22.first,              ans22.second);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long LINF = 0x3f3f3f3f3f3f3f3fLL;   const double EPS = 1e-8;   const int MOD = 1000000007;   const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};   int main() {     cin.tie(0);     ios::sync_with_stdio(false);     int n;     cin >> n;     vector<long long> s(n + 1, 0);     for (int i = (1); i < (n + 1); ++i) cin >> s[i];     for (int i = (1); i < (n + 1); ++i) s[i] += s[i - 1];     int q;     cin >> q;     while (q--) {       int l, r;       cin >> l >> r;       --l;       --r;       cout << (s[r + 1] - s[l]) / 10 <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[100], k[100];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     int t, n;     cin >> t;     for (int i = 0; i < t; ++i) {       cin >> n;       n *= 2;       for (int j = 0; j < n; ++j) cin >> a[j];       for (int j = 0; j < n; ++j) {         if (k[a[j]] == 0) {           cout << a[j] <<    ;           ++k[a[j]];         }       }       cout <<   n ;       for (int j = 0; j <= 100; ++j) k[j] = 0;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 2e9;   const long double eps = 0.000001;   const long long mod = 1e9 + 7;   const long double PI = 3.14159265359;   template <class T>   struct MyVector : vector<T> {     MyVector() : vector<T>() {}     MyVector(int n) : vector<T>(n) {}     T &operator[](size_t i) { return vector<T>::at(i); }   };   string t;   string p;   vector<int> a;   bool check(int m) {     vector<int> u((int)t.size(), false);     for (int i = 0; i < m; i++) {       u[a[i]] = true;     }     string ns;     for (int i = 0; i < (int)t.size(); i++) {       if (u[i]) continue;       ns.push_back(t[i]);     }     int idx = 0;     for (int i = 0; i < (int)p.size(); i++) {       bool fl = false;       for (; idx < (int)ns.size(); idx++) {         if (ns[idx] == p[i]) {           idx++;           fl = true;           break;         }       }       if (!fl) return false;     }     return true;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     cin >> t >> p;     a.resize((int)t.size());     for (int i = 0; i < (int)t.size(); i++) {       cin >> a[i];       a[i]--;     }     int l = -1, r = (int)t.size() + 1;     while (r - l > 1) {       int m = (l + r) / 2;       if (check(m))         l = m;       else         r = m;     }     cout << l;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 50;   int b[maxn];   int cnt;   int main() {     std::ios::sync_with_stdio(false);     int t;     cin >> t;     while (t--) {       string a;       cin >> a;       cnt = 0;       bool flag = true;       for (int i = 0; i < a.size(); i++) {         int t = a[i] -  a ;         b[cnt++] = t;       }       sort(b, b + cnt);       for (int i = 1; i < cnt; i++) {         if (b[i] - b[i - 1] != 1) {           flag = false;         }       }       if (flag)         cout <<  YES  << endl;       else         cout <<  NO  << endl;     }     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 10;   int b[maxn];   char a[maxn];   int num[2 * maxn];   int main() {     int n;     scanf( %d , &n);     int count0 = 0, count1 = 0;     memset(num, -1, sizeof(num));     int len = 0, maxlen = 0;     scanf( %s , a + 1);     for (int i = 1; i <= n; i++) {       if (a[i] ==  0 )         count0++;       else         count1++;       b[i] = count1 - count0;       if (b[i] == 0) {         if (maxlen < i) maxlen = i;         continue;       }       if (num[b[i] + maxn] == -1)         num[b[i] + maxn] = i;       else {         len = i - num[b[i] + maxn];         if (maxlen < len) maxlen = len;       }     }     printf( %d n , maxlen);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long t, n, m, k, rem, each, x, y, i, j, ans;     cin >> t;     while (t--) {       cin >> n >> m >> x >> y;       string s[n];       long long one = 0, two = 0;       for (i = 0; i < n; i++) {         cin >> s[i];       }       for (i = 0; i < n; i++) {         for (j = 0; j < m; j++) {           if (s[i][j] ==  . ) one++;           if (j == 0) continue;           if (s[i][j] ==  .  && s[i][j - 1] ==  . ) {             two++;             s[i][j] =  $ ;             s[i][j - 1] =  $ ;           }         }       }       long long p1, p2;       p1 = x * one;       p2 = y * two + x * (one - two * 2);       ans = min(p1, p2);       cout << ans << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   constexpr long long mod = 1000000007;   pair<long long, long long> dfs(int v, const vector<vector<int>> &children,                                  const vector<int> &black) {     long long zero = 0;     long long one = 0;     (black[v] ? one : zero) = 1;     for (const auto &child : children[v]) {       const auto result = dfs(child, children, black);       long long t_zero = (zero * result.second + zero * result.first) % mod;       one =           (one * result.second + zero * result.second + one * result.first) % mod;       zero = t_zero;     }     return {zero, one};   }   int main() {     cin.tie(nullptr);     ios::sync_with_stdio(false);     int n;     cin >> n;     vector<vector<int>> children(n);     for (int i = 1; i < n; ++i) {       int p;       cin >> p;       children[p].emplace_back(i);     }     vector<int> black(n);     for (auto &e : black) cin >> e;     const auto ans = dfs(0, children, black);     cout << ans.second << endl;     return EXIT_SUCCESS;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 7;   int dp[80][80][80][2];   char c[80];   vector<int> V, K, a;   int cal(int id, vector<int> vv, int pos) {     int cnt = 0;     for (int i = id; i < ((int)vv.size()); i++)       if (vv[i] < pos) cnt++;     return cnt;   }   int cc(int i, int j, int k, int pos) {     return cal(i, V, pos) + cal(j, K, pos) + cal(k, a, pos);   }   void mi(int &a, int b) {     if (a > b) a = b;   }   int main() {     int n;     scanf( %d , &n);     scanf( %s , c);     for (int i = 0; i < n; i++) {       if (c[i] ==  V )         V.push_back(i);       else if (c[i] ==  K )         K.push_back(i);       else         a.push_back(i);     }     for (int i = 0; i <= ((int)V.size()); i++)       for (int j = 0; j <= ((int)K.size()); j++)         for (int k = 0; k <= ((int)a.size()); k++)           for (int l = 0; l < 2; l++) dp[i][j][k][l] = INF;     dp[0][0][0][0] = 0;     for (int i = 0; i <= ((int)V.size()); i++)       for (int j = 0; j <= ((int)K.size()); j++)         for (int k = 0; k <= ((int)a.size()); k++)           for (int l = 0; l < 2; l++) {             if (i + 1 <= ((int)V.size()))               mi(dp[i + 1][j][k][1], dp[i][j][k][l] + cc(i, j, k, V[i]));             if (j + 1 <= ((int)K.size()) && l == 0)               mi(dp[i][j + 1][k][0], dp[i][j][k][l] + cc(i, j, k, K[j]));             if (k + 1 <= ((int)a.size()))               mi(dp[i][j][k + 1][0], dp[i][j][k][l] + cc(i, j, k, a[k]));           }     printf( %d n , min(dp[((int)V.size())][((int)K.size())][((int)a.size())][0],                        dp[((int)V.size())][((int)K.size())][((int)a.size())][1]));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, m, k;     cin >> n >> m >> k;     long long l = 0, h = n * m + 2;     while (l < h) {       long long md = (l + h) / 2;       long long rs = 0;       for (int i = 1; i <= n; i++) rs += min(m, md / i);       if (rs < k)         l = md + 1;       else         h = md;     }     cout << l << endl;   }


#include <bits/stdc++.h>   using namespace std;   const double EPS = 1e-9;   const int MOD = 1e9 + 7;   int dx[8] = {0, 0, 1, -1, -1, 1, -1, 1}, dy[8] = {1, -1, 0, 0, -1, -1, 1, 1};   void INPUT() {}   void OUTPUT() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);   }   int main() {     INPUT();     OUTPUT();     string s;     cin >> s;     string ans =   , s1 =   , s0 =   ;     for (int i = ((int)((s).size())) - 1; i >= 0; i--) {       if (s[i] ==  0 ) {         ans +=  0 ;         s0 +=  0 ;       } else if (s[i] ==  2 ) {         ans +=  2 ;         s0 =   ;       } else {         s1 +=  1 ;       }     }     cout << s0 + s1;     int i = ((int)((ans).size())) - 1;     while (ans[i] ==  0 ) {       i--;     }     while (i > -1) {       cout << ans[i];       i--;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve();   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     solve();     return 0;   }   void solve() {     long long a, b;     cin >> a >> b;     long long ct = 0;     while (a > 0 && b > 0) {       if (a > b) {         ct += a / b;         a = a % b;       } else {         ct += b / a;         b = b % a;       }     }     cout << ct;   }


#include <bits/stdc++.h>   using namespace std;   struct query {     long long l, r, x;   };   int n, a[109];   long long m;   int main() {     cin >> n >> m;     vector<query> v;     vector<long long> cp = {1LL << 60};     for (int i = 0; i < n; i++) {       cin >> a[i];       int pr = sqrt(a[i]);       for (int j = 1; j < pr; j++) {         v.push_back(query{j, j + 1, (a[i] + j - 1) / j});         cp.push_back(j);       }       cp.push_back(pr);       int r = (a[i] + pr - 1) / pr, ptr = pr;       for (int j = r; j > 1; j--) {         v.push_back(query{ptr, (a[i] + j - 2) / (j - 1), j});         ptr = v.back().r;         cp.push_back(ptr);       }       v.push_back(query{ptr, 1LL << (59 + 1), 1});     }     sort(cp.begin(), cp.end());     cp.erase(unique(cp.begin(), cp.end()), cp.end());     vector<long long> sum(cp.size());     for (query i : v) {       int pl = lower_bound(cp.begin(), cp.end(), i.l) - cp.begin();       int pr = lower_bound(cp.begin(), cp.end(), i.r) - cp.begin();       sum[pl] += i.x;       sum[pr] -= i.x;     }     for (int i = 1; i < cp.size(); i++) sum[i] += sum[i - 1];     long long ret = 0;     for (int i = 0; i < cp.size() - 1; i++) {       long long r = min(cp[i + 1] - 1, (m + sum[0]) / sum[i]);       if (cp[i] <= r) ret = max(ret, r);     }     cout << ret << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int mod = 1000000007;   const long long int inf = 0x3f3f3f3f3f3f3f3fLL;   const double eps = 1e-9;   const double PI = acos(-1);   long long int i, j, k;   void solve(void);   inline bool equal(double a, double b) { return fabs(a - b) < eps; }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout << fixed << setprecision(10);     int t = 1;     while (t--) solve();     return 0;   }   void solve() {     int n, a, b, T;     cin >> n >> a >> b >> T;     char c[n];     for (i = (0); i <= (n - 1); ++i) cin >> c[i];     int lo = 1, hi = n, mid, ans = 0, left[n], right[n];     left[0] = right[0] = 1 + (c[0] ==  w  ? b : 0);     for (i = (1); i <= (n - 1); ++i)       left[i] = left[i - 1] + a + (c[n - i] ==  w  ? b : 0) + 1;     for (i = (1); i <= (n - 1); ++i)       right[i] = right[i - 1] + a + (c[i] ==  w  ? b : 0) + 1;     while (lo <= hi) {       mid = (lo + hi) >> 1;       bool pos = 0;       for (i = (mid - 1); i >= (0); --i) {         long long int time1 = right[i] + left[mid - i - 1] - left[0] + i * a;         if (time1 <= T) pos = 1;         long long int time2 = left[i] + right[mid - i - 1] - right[0] + i * a;         if (time2 <= T) pos = 1;       }       if (pos) {         ans = ((ans) > (mid) ? (ans) : (mid));         lo = mid + 1;       } else         hi = mid - 1;     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t = 1;     cin >> t;     while (t-- > 0) {       int n;       cin >> n;       string srr[n];       for (int i = 0; i < n; i++) {         cin >> srr[i];       }       vector<pair<int, int>> afirst(n);       vector<pair<int, int>> bfirst(n);       vector<pair<int, int>> cfirst(n);       vector<pair<int, int>> dfirst(n);       vector<pair<int, int>> efirst(n);       for (int i = 0; i < n; i++) {         int countabc[5] = {0};         for (int j = 0; j < srr[i].size(); j++) {           countabc[srr[i][j] -  a ]++;         }         afirst[i].first = 2 * countabc[0] - srr[i].size();         bfirst[i].first = 2 * countabc[1] - srr[i].size();         cfirst[i].first = 2 * countabc[2] - srr[i].size();         dfirst[i].first = 2 * countabc[3] - srr[i].size();         efirst[i].first = 2 * countabc[4] - srr[i].size();         afirst[i].second = i;         bfirst[i].second = i;         cfirst[i].second = i;         dfirst[i].second = i;         efirst[i].second = i;       }       sort(afirst.begin(), afirst.end());       sort(bfirst.begin(), bfirst.end());       sort(cfirst.begin(), cfirst.end());       sort(dfirst.begin(), dfirst.end());       sort(efirst.begin(), efirst.end());       int maxi = 0;       int curr = 0;       int total = 0;       for (int i = n - 1; i >= 0; i--) {         curr += afirst[i].first;         if (curr >= 1) {           total++;         } else {           break;         }       }       maxi = max(maxi, total);       curr = 0;       total = 0;       for (int i = n - 1; i >= 0; i--) {         curr += bfirst[i].first;         if (curr >= 1) {           total++;         } else {           break;         }       }       maxi = max(maxi, total);       curr = 0;       total = 0;       for (int i = n - 1; i >= 0; i--) {         curr += cfirst[i].first;         if (curr >= 1) {           total++;         } else {           break;         }       }       maxi = max(maxi, total);       curr = 0;       total = 0;       for (int i = n - 1; i >= 0; i--) {         curr += dfirst[i].first;         if (curr >= 1) {           total++;         } else {           break;         }       }       maxi = max(maxi, total);       curr = 0;       total = 0;       for (int i = n - 1; i >= 0; i--) {         curr += efirst[i].first;         if (curr >= 1) {           total++;         } else {           break;         }       }       maxi = max(maxi, total);       cout << maxi << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long rd() {     long long x = 0;     char o, f = 1;     while (o = getchar(), o < 48)       if (o == 45) f = -f;     do x = (x << 3) + (x << 1) + (o ^ 48);     while (o = getchar(), o > 47);     return x * f;   }   const int maxn = 1e2 + 5;   const long long mod = 998244353;   int _;   int n, m, a[maxn][maxn];   long long ans;   void solve(int x, int y) {     vector<int> v;     v.push_back(a[x][y]);     if (n - x + 1 != x) v.push_back(a[n - x + 1][y]);     if (m - y + 1 != y) v.push_back(a[x][m - y + 1]);     if (n - x + 1 != x && m - y + 1 != y) v.push_back(a[n - x + 1][m - y + 1]);     sort(v.begin(), v.end());     int len = v.size();     len /= 2;     for (auto u : v) {       ans += abs(v[len] - u);     }   }   int main() {     for (_ = rd(); _; _--) {       ans = 0;       n = rd();       m = rd();       for (int i = 1; i <= n; i++)         for (int j = 1; j <= m; j++) a[i][j] = rd();       for (int i = 1; i <= (n + 1) / 2; i++) {         for (int j = 1; j <= (m + 1) / 2; j++) solve(i, j);       }       printf( %lld n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T1, typename T2>   inline void chkmin(T1& a, T2 b) {     if (a > b) a = b;   }   template <typename T1, typename T2>   inline void chkmax(T1& a, T2 b) {     if (a < b) a = b;   }   using ll = long long;   using ld = long double;   const string FILENAME =  input ;   using ll = long long;   using ld = long double;   struct Point {     ld x, y;     Point() {}     Point(ld _x, ld _y) : x(_x), y(_y) {}     ld len() { return sqrt(x * x + y * y); }   };   Point operator+(const Point& a, const Point& b) {     return Point(a.x + b.x, a.y + b.y);   }   Point operator-(const Point& a, const Point& b) {     return Point(a.x - b.x, a.y - b.y);   }   ld operator*(const Point& a, const Point& b) { return a.x * b.y - a.y * b.x; }   ld operator^(const Point& a, const Point& b) { return a.x * b.x + a.y * b.y; }   const double Pi = acos(-1);   const int MAXN = 100228;   int n, k;   Point p[MAXN];   bool ok(double r) {     vector<pair<double, int> > st;     int add = 0;     for (int i = 0; i < n; i++) {       double f = p[i].len();       if (f > 2 * r + 1e-9) {         continue;       }       if (abs(p[i].x) < 1e-9 && abs(p[i].y) < 1e-9) {         add++;         continue;       }       Point q = p[i];       swap(q.x, q.y);       q.x *= -1;       double fm = sqrt(q.x * q.x + q.y * q.y);       q.x /= fm;       q.y /= fm;       double h = sqrt(r * r - (f / 2) * (f / 2));       q.x *= h;       q.y *= h;       Point s = p[i];       s.x /= 2;       s.y /= 2;       Point e = s + q;       Point e1 = s - q;       double l = atan2(e.y, e.x) + 2 * Pi;       double rr = atan2(e1.y, e1.x) + 2 * Pi;       while (l < 0) {         l += 2 * Pi;       }       while (rr < 0) {         rr += 2 * Pi;       }       while (l >= 2 * Pi) {         l -= 2 * Pi;       }       while (rr >= 2 * Pi) {         rr -= 2 * Pi;       }       if (l > rr) {         swap(l, rr);       }       if (rr - l <= Pi + 1e-9) {         st.push_back(make_pair(l, 1));         st.push_back(make_pair(rr, -1));       } else {         st.push_back(make_pair(0, 1));         st.push_back(make_pair(l, -1));         st.push_back(make_pair(rr, 1));         st.push_back(make_pair(2 * Pi, -1));       }     }     sort((st).begin(), (st).end());     int cur = 0;     int tm = 0;     for (auto x : st) {       cur += x.second;       chkmax(tm, cur);     }     return tm + add >= k;   }   void solve() {     cin >> n >> k;     for (int i = 0; i < n; i++) {       int x, y;       cin >> x >> y;       p[i].x = x;       p[i].y = y;     }     double l = 0.0;     double r = 200228;     for (int it = 0; it < 36; it++) {       double mid = (l + r) / 2.0;       if (ok(mid)) {         r = mid;       } else {         l = mid;       }     }     cout << fixed << l <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1005;   long long sum[N][N];   int spt[11][N][N], mn[N][N], arr[N][N];   bool visit[N][N];   int n, m, a, b, x;   void buildSparse() {     int LN = log2(n) + 1, LM = log2(m) + 1;     for (int lg = 1; lg <= LM; lg++)       for (int i = 1; i <= n; i++)         for (int j = 1; j <= m; j++)           spt[lg][i][j] =               min(spt[lg - 1][i][j], spt[lg - 1][i][j + (1 << (lg - 1))]);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         sum[i][j] += sum[i - 1][j];         if (j >= b) {           int lg = log2(b), l = j - b + 1, r = j - (1 << lg) + 1;           mn[i][j] = min(spt[lg][i][l], spt[lg][i][r]);         }       }     for (int i = 1; i <= n; i++)       for (int j = b; j <= m; j++) spt[0][i][j] = mn[i][j];     for (int lg = 1; lg <= LN; lg++)       for (int i = 1; i <= n; i++)         for (int j = b; j <= m; j++)           spt[lg][i][j] =               min(spt[lg - 1][i][j], spt[lg - 1][i + (1 << (lg - 1))][j]);   }   long long getRemoved(int i, int j) {     int lg = log2(a), l = i - a + 1;     int r = i - (1 << lg) + 1;     int minn = min(spt[lg][l][j], spt[lg][r][j]);     long long res = sum[i][j] - sum[i - a][j] - sum[i][j - b] +                     sum[i - a][j - b] - 1LL * a * b * minn;     return res;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n >> m >> a >> b;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         cin >> arr[i][j];         spt[0][i][j] = arr[i][j];         sum[i][j] += sum[i][j - 1] + arr[i][j];       }     }     buildSparse();     vector<pair<long long, pair<int, int>>> v, ans;     for (int i = a; i <= n; i++)       for (int j = b; j <= m; j++) v.push_back({getRemoved(i, j), {i, j}});     sort(v.begin(), v.end());     for (int i = 0; i < v.size(); i++) {       int x = v[i].second.first - a + 1, y = v[i].second.second - b + 1;       if (!visit[x][y] && !visit[x + a - 1][y] && !visit[x][y + b - 1] &&           !visit[x + a - 1][y + b - 1]) {         ans.push_back({v[i].first, {x, y}});         for (int j = 0; j < a; j++)           for (int k = 0; k < b; k++) visit[x + j][y + k] = true;       }     }     cout << ans.size() << endl;     for (int i = 0; i < ans.size(); i++)       cout << ans[i].second.first <<     << ans[i].second.second <<                << ans[i].first <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100000 + 10;   struct Edge {     int from, to, w;   } edges[maxn];   int val[maxn];   long long ans;   int size[maxn], fa[maxn];   bool cmp(Edge a, Edge b) { return a.w > b.w; }   int Getfa(int x) {     if (fa[x] == x)       return x;     else       return fa[x] = Getfa(fa[x]);   }   int main() {     int n, m;     cin >> n >> m;     for (int i = 1; i <= n; ++i) scanf( %d , &val[i]);     for (int i = 1; i <= m; ++i) {       int u, v;       scanf( %d %d , &u, &v);       edges[i].from = u, edges[i].to = v;       edges[i].w = min(val[u], val[v]);     }     sort(edges + 1, edges + 1 + m, cmp);     for (int i = 1; i <= n; ++i) size[i] = 1, fa[i] = i;     ans = 0;     for (int i = 1; i <= m; ++i) {       int u = edges[i].from, v = edges[i].to;       int fx = Getfa(u), fy = Getfa(v);       if (fx == fy) continue;       ans += (long long)size[fx] * size[fy] * edges[i].w;       fa[fx] = fy;       size[fy] += size[fx];     }     printf( %.6lf n , (double)2.0 * ans / n / (n - 1));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int N, M;     char eat;     cin >> N >> M;     scanf( %c , &eat);     char temp[5000];     vector<vector<bool>> grid(N, vector<bool>(M));     for (int r = 0; r < N; r++) {       scanf( %s , temp);       for (int c = 0; c < M; c++) grid[r][c] = (temp[c] ==  1  ? 1 : 0);     }     int result = 0;     vector<vector<int>> memo(N, vector<int>(M));     for (int r = 0; r < N; r++) {       for (int c = M - 1; c >= 0; c--) {         if (grid[r][c] == 1) memo[r][c] = 1 + (c != M - 1 ? memo[r][c + 1] : 0);       }     }     for (int c = 0; c < M; c++) {       vector<int> width;       for (int r = 0; r < N; r++) {         if (memo[r][c] != 0) width.push_back(memo[r][c]);       }       sort(width.rbegin(), width.rend());       for (int i = 0; i < width.size(); i++) {         int area = (i + 1) * width[i];         result = max(result, area);       }     }     cout << result << endl;   }


#include <bits/stdc++.h>   using namespace std;          #define pb push_back   #define MP make_pair   #define ll long long   #define lb lower_bound   #define ub upper_bound   #define bs binary_search      #define fo(i,a,b) for(int i=a;i<b;i++)   #define rfo(i,b,a) for(int i=b;i>=a;i--)      typedef vector<ll> vi;   typedef vector<vi> vvi;       string solve(){      ll n;      cin>>n;      vi a(n);      map<int,int> m;            fo(i,0,n){          cin>>a[i];          m[a[i]] = m[a[i]]  + (i%2);      }      sort(a.begin(), a.end());      fo(i,0,n){          m[a[i]] = m[a[i]]-(i%2);      }      map<int,int> :: iterator itr;      for(itr = m.begin() ; itr!= m.end() ; itr++){          if(itr->second != 0) return  NO ;      }      return  YES ;          }         int main()   {    ios_base::sync_with_stdio(false);       cin.tie(NULL);       int t;    cin>>t;    while(t--)    {     cout<<solve()<<endl;    }    return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct t {     char c;     int a, b;     t(char c, int a, int b) : c(c), a(a), b(b) {}     t() {}   };   int n;   vector<t> e;   double temp;   double t0 = 1000;   double gen_rand() {     int x = rand() + 1, y = rand() + 1;     x %= y;     return x / (double)y;   }   int get_ans() {     int r = 0, b = 0;     int R = 0, B = 0;     int ans = 0;     for (int i = 0; i < n; i++) {       int need =           max(max(0, max(0, e[i].a - R) - r), max(0, max(0, e[i].b - B) - b));       ans += need;       r -= max(0, max(0, e[i].a - R));       b -= max(0, max(0, e[i].b - B));       r += need;       b += need;       ans++;       if (e[i].c ==  R ) {         R++;       } else {         B++;       }     }     return ans;   }   int main() {     srand(time(NULL));     cin >> n;     for (int i = 0; i < n; i++) {       char c;       int r, b;       cin >> c >> r >> b;       e.push_back({c, r, b});     }     random_shuffle(e.begin(), e.end());     temp = t0;     int ans = INT_MAX;     for (int qwe = 0; qwe < 70000; ++qwe) {       int cur = get_ans();       ans = min(ans, cur);       vector<t> was = e;       for (int i = 0; i < n * temp / t0; i++) {         swap(e[rand() % n], e[rand() % n]);       }       int now = get_ans();       ans = min(ans, now);       if (now < cur) {         cur = now;       } else {         double go = exp(-(now - cur) / temp);         if (gen_rand() <= go) {           cur = now;         } else {           e = was;         }       }       temp *= 0.999;     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   T gcd(T a, T b) {     return (b ? __gcd(a, b) : a);   }   template <typename T>   T lcm(T a, T b) {     return (a * (b / gcd(a, b)));   }   template <class T>   ostream &operator<<(ostream &os, vector<T> V) {     os <<  [  ;     for (auto v : V) os << v <<    ;     return os <<  ] ;   }   template <class T>   ostream &operator<<(ostream &os, set<T> S) {     os <<  {  ;     for (auto s : S) os << s <<    ;     return os <<  } ;   }   template <class L, class R>   ostream &operator<<(ostream &os, pair<L, R> P) {     return os <<  (  << P.first <<  ,  << P.second <<  ) ;   }   template <class L, class R>   ostream &operator<<(ostream &os, map<L, R> M) {     os <<  {  ;     for (auto m : M) os <<  (  << m.first <<  :  << m.second <<  )  ;     return os <<  } ;   }   const int N = 5e5 + 5;   long long ar[N];   long long l[N], r[N], ans[N];   int main() {     cin.sync_with_stdio(0);     cin.tie(0);     cin.exceptions(cin.failbit);     int n;     cin >> n;     for (int i = 1; i <= n; ++i) cin >> ar[i];     vector<long long> lft(1, 0), rt(1, n + 1);     for (int i = 1; i <= n; ++i) {       while (ar[lft.back()] > ar[i]) {         lft.pop_back();       }       l[i] = l[lft.back()] + ar[i] * (i - lft.back());       lft.push_back(i);     }     for (int i = n; i; --i) {       while (ar[rt.back()] > ar[i]) rt.pop_back();       r[i] = r[rt.back()] + ar[i] * (rt.back() - i);       rt.push_back(i);     }     for (int i = 1; i <= n; ++i) {       ans[i] = l[i] + r[i] - ar[i];     }     long long maxa = max_element(ans + 1, ans + n + 1) - ans;     for (int i = maxa - 1; i; --i) {       if (ar[i] > ar[i + 1]) ar[i] = ar[i + 1];     }     for (int i = maxa + 1; i <= n; ++i)       if (ar[i - 1] < ar[i]) ar[i] = ar[i - 1];     for (int i = 1; i <= n; ++i) cout << ar[i] <<    ;     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string getbiner(long long n) {     string ret =   ;     while (n) {       if (n % 2)         ret +=  1 ;       else         ret +=  0 ;       n /= 2;     }     return ret;   }   long long getnum(string s) {     long long ret = 0;     long long now = 1;     cout <<   t  << ( s ) <<   =   << (s) <<   n ;     for (long long(i) = (0); (i) < (s.length()); (i)++) {       ret += ((s[i] -  0 ) * now);       now *= 2;     }     return ret;   }   int main() {     ios::sync_with_stdio(NULL);     cin.tie(NULL);     cout.tie(NULL);     long long n, m;     cin >> n >> m;     if (n < m) {       cout << 0 <<   n ;       return 0;     }     if ((n - m) % 2) {       cout << 0 <<   n ;       return 0;     }     long long k = (n - m) / 2;     string a = getbiner(m);     string b = getbiner(k);     long long mx = max(a.length(), b.length());     long long diffa = mx - a.length();     long long diffb = mx - b.length();     for (long long(i) = (0); (i) < (diffa); (i)++) a +=  0 ;     for (long long(i) = (0); (i) < (diffb); (i)++) b +=  0 ;     reverse(a.begin(), a.end());     reverse(b.begin(), b.end());     long long counter = 0;     int flag = 0;     for (long long(i) = (0); (i) < (a.length()); (i)++) {       if (a[i] ==  1  && b[i] ==  0 ) {         counter++;       } else if (a[i] ==  1  && b[i] ==  1 ) {         flag = 1;       }     }     if (flag) {       cout << 0 <<   n ;       return 0;     }     long long ans = pow(2, counter);     if (n == m) ans -= 2;     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2 * 100 * 1000 + 10;   bool f1[maxn], f2[maxn];   int n, dis[maxn], dis2[maxn], barg[maxn], mxdis, mxdis2, jj1, j2, h1, h2;   vector<int> e[maxn], e2[maxn];   vector<pair<int, int> > v1, v2;   void barg2(int v, int p, int t) {     f2[v] = true;     dis2[v] = t;     for (int i = 0; i < e[v].size(); i++) {       int u = e[v][i];       if (u != p && u != h2) barg2(u, v, t + 1);     }   }   void barg1(int v, int p, int t) {     f1[v] = true;     dis[v] = t;     for (int i = 0; i < e[v].size(); i++) {       int u = e[v][i];       if (u != p && u != h1) barg1(u, v, t + 1);     }   }   int hazf2(int v, int p) {     if (v == jj1) return jj1;     for (int i = 0; i < e2[v].size(); i++) {       int u = e2[v][i];       if (u != p) {         int x = hazf2(u, v);         if (x != -1) return u;       }     }     return -1;   }   int hazf1(int v, int p) {     if (v == j2) return j2;     for (int i = 0; i < e2[v].size(); i++) {       int u = e2[v][i];       if (u != p) {         int x = hazf1(u, v);         if (x != -1) return u;       }     }     return -1;   }   int dfs4(int v, int p) {     int mx1 = 0, mx2 = 0;     for (int i = 0; i < e[v].size(); i++) {       int u = e[v][i];       if (u != p) {         int mx3 = dfs4(u, v);         if (mx3 > mx2) mx2 = mx3;         if (mx2 > mx1) swap(mx2, mx1);       }     }     barg[v] = mx2 + mx1;     return barg[v] + 1;   }   void dfs3(int v, int p, int t) {     dis2[v] = t;     for (int i = 0; i < e2[v].size(); i++) {       int u = e2[v][i];       if (u != p) dfs3(u, v, t + 1);     }     return;   }   void dfs2(int v, int p, int t) {     dis[v] = t;     for (int i = 0; i < e2[v].size(); i++) {       int u = e2[v][i];       if (u != p) dfs2(u, v, t + 1);     }     return;   }   bool dfs(int v, int p) {     bool f = true;     if (e[v].size() > 2) f = false;     for (int i = 0; i < e[v].size(); i++) {       int u = e[v][i];       if (u != p)         if (dfs(u, v) == false) {           f = false;           e2[v].push_back(u);           e2[u].push_back(v);         }     }     return f;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = 0; i < n; i++) f1[i] = false, f2[i] = false;     for (int i = 0; i < n - 1; i++) {       int v, u;       cin >> v >> u;       v--, u--;       e[v].push_back(u);       e[u].push_back(v);     }     int v;     for (int i = 0; i < n; i++)       if (e[i].size() > 2) {         v = i;         break;       }     dfs(v, -1);     for (int i = 0; i < n; i++)       if (e2[i].size() > 0) {         v = i;         break;       }     dfs2(v, -1, 0);     dfs4(v, -1);     mxdis = dis[v];     for (int i = 0; i < n; i++)       if (e2[i].size() > 0 && dis[i] > mxdis) {         mxdis = dis[i];         v = i;       }     for (int i = 0; i < n; i++)       if (e2[i].size() > 0 && dis[i] == mxdis) v1.push_back({barg[i], i});     dfs3(v, -1, 0);     mxdis2 = dis2[v];     for (int i = 0; i < n; i++)       if (e2[i].size() > 0 && dis2[i] > mxdis2) mxdis2 = dis2[i];     for (int i = 0; i < n; i++)       if (e2[i].size() > 0 && dis2[i] == mxdis2) v2.push_back({barg[i], i});     sort(&v1[0], &v1[v1.size()]);     sort(&v2[0], &v2[v2.size()]);     reverse(&v1[0], &v1[v1.size()]);     reverse(&v2[0], &v2[v2.size()]);     if (v1[0].second == v2[0].second)       if (v2.size() == 1 || v1[1].first > v2[1].first)         jj1 = v1[1].second, j2 = v2[0].second;       else         jj1 = v1[0].second, j2 = v2[1].second;     else       jj1 = v1[0].second, j2 = v2[0].second;     h1 = hazf1(jj1, -1);     h2 = hazf2(j2, -1);     barg1(jj1, -1, 0);     barg2(j2, -1, 0);     mxdis = -1;     for (int i = 0; i < n; i++)       if (f1[i] == true && dis[i] > mxdis) {         mxdis = dis[i];         v = i;       }     f1[v] = false;     cout << v + 1 <<    ;     mxdis = -1;     for (int i = 0; i < n; i++)       if (f2[i] == true && dis2[i] > mxdis) {         mxdis = dis2[i];         v = i;       }     f2[v] = false;     cout << v + 1 <<   n ;     mxdis = -1;     for (int i = 0; i < n; i++)       if (f1[i] == true && e[i].size() == 1 && dis[i] > mxdis) {         mxdis = dis[i];         v = i;       }     cout << v + 1 <<    ;     mxdis = -1;     for (int i = 0; i < n; i++)       if (f2[i] == true && e[i].size() == 1 && dis2[i] > mxdis) {         mxdis = dis2[i];         v = i;       }     cout << v + 1 <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int a[200005];   string ans =   ;   int main() {     ios_base::sync_with_stdio(0);     ;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];     }     int l = 0, r = n - 1;     int mini = 0;     while (l <= r) {       if (l == r) {         if (a[l] > mini) {           ans +=  L ;         }         break;       }       if (a[l] > mini || a[r] > mini) {         if (a[l] > mini && a[r] > mini) {           if (a[l] < a[r]) {             ans +=  L ;             mini = a[l];             l++;           } else {             ans +=  R ;             mini = a[r];             r--;           }         } else if (a[l] > mini) {           ans +=  L ;           mini = a[l];           l++;         } else {           ans +=  R ;           mini = a[r];           r--;         }       } else {         break;       }     }     cout << ans.size() << endl;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   int n, a[100100], m, x, rs = 1e9;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     for (int i = 1; i <= m; i++) cin >> x, a[x]++;     for (int i = 1; i <= n; i++) rs = min(rs, a[i]);     cout << rs;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> a, b;   int x[50];   int main() {     int n, l;     scanf( %d%d , &n, &l);     for (int i = 0; i < (int)n; ++i) scanf( %d , x + i);     for (int i = 1; i < (int)n; ++i) a.push_back(x[i] - x[i - 1]);     a.push_back(x[0] + l - x[n - 1]);     for (int i = 0; i < (int)n; ++i) scanf( %d , x + i);     for (int i = 1; i < (int)n; ++i) b.push_back(x[i] - x[i - 1]);     b.push_back(x[0] + l - x[n - 1]);     for (int i = 0; i < (int)n; ++i)       if (a == b)         printf( YES n ), exit(0);       else {         int t = a[0];         for (int j = 1; j < (int)n; ++j) a[j - 1] = a[j];         a[n - 1] = t;       }     printf( NO n );   }


#include <bits/stdc++.h>   using namespace std;   complex<double> r() {     double a, b;     cin >> a >> b;     return {a, b};   }   double cross(complex<double> a, complex<double> b) {     return (conj(a) * b).imag();   }   double orient(complex<double> a, complex<double> b, complex<double> c) {     return cross(b - a, c - a);   }   void up(vector<complex<double> > &st, complex<double> p) {     while ((long long)(st).size() >= 2 && orient(st.end()[-2], st.back(), p) >= 0)       st.pop_back();     st.push_back(p);   }   struct line {     complex<double> d;     double c;     line(complex<double> a, complex<double> b) : d(b - a), c(cross(d, a)) {}   };   complex<double> inter(line l1, line l2) {     return (l1.c * l2.d - l2.c * l1.d) / cross(l1.d, l2.d);   }   signed main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     long long n;     complex<double> obj;     cin >> n;     obj = r();     vector<complex<double> > ps(n);     double mx = 0, my = 0;     for (long long i = 0; i < (n); i++) {       ps[i] = r();       mx = max(mx, (ps[i].real()));       my = max(my, (ps[i].imag()));     }     ps.push_back({0.0, my});     sort(ps.begin(), ps.end(), [](complex<double> a, complex<double> b) {       return make_tuple(a.real(), -a.imag()) < make_tuple(b.real(), -b.imag());     });     vector<complex<double> > hull;     for (complex<double> p : ps) up(hull, p);     hull.push_back({mx, 0.0});     long long i = upper_bound(hull.begin(), hull.end(), obj,                               [](complex<double> a, complex<double> b) {                                 return cross(a, b) < 0;                               }) -                   hull.begin() - 1;     complex<double> a = inter(line(0, obj), line(hull[i], hull[i + 1]));     cout << fixed << setprecision(8) << obj.real() / a.real() <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 110;   const int dx[4] = {0, 0, 1, -1};   const int dy[4] = {1, -1, 0, 0};   int n, m, ret;   char G[maxn][maxn];   bool ok(int x, int y) { return x >= 0 && y >= 0 && x < n && y < m; }   void dfs(int x, int y) {     if (G[x][y] ==  . ) return;     G[x][y] =  . ;     for (int i = 0; i < (4); i++) {       int tx = x + dx[i];       int ty = y + dy[i];       if (ok(tx, ty) && G[tx][ty] ==  B ) {         dfs(tx, ty);       }     }   }   signed main() {     while (~scanf( %d , &n) && ~scanf( %d , &m)) {       memset((G), 0, sizeof(G));       ret = 0;       for (int i = 0; i < (n); i++) scanf( %s , G[i]);       for (int i = 0; i < (n); i++) {         for (int j = 0; j < (m); j++) {           if (G[i][j] ==  B ) {             dfs(i, j);             ret++;           }         }       }       printf( %d n , ret);     }     return 0;   }


#include <bits/stdc++.h>   const int MOD = int(1E9) + 7;   const int MAXN = 100000;   int pw[MAXN + 5], cnt[MAXN + 5];   void init() {     pw[0] = 1;     for (int i = 1; i <= MAXN; i++) pw[i] = 2LL * pw[i - 1] % MOD;   }   int main() {     init();     int n, x;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &x);       if (x != -1) cnt[x]++;     }     int ans = pw[n - 1] - 1;     for (int i = 1; i <= n; i++) ans = (ans - 1LL * (pw[cnt[i]] - 1) % MOD) % MOD;     printf( %d n , (ans + MOD) % MOD);   }


#include <bits/stdc++.h>   using namespace std;   void fre() {     freopen( c://test//input.in ,  r , stdin);     freopen( c://test//output.out ,  w , stdout);   }   template <class T1, class T2>   inline void gmax(T1 &a, T2 b) {     if (b > a) a = b;   }   template <class T1, class T2>   inline void gmin(T1 &a, T2 b) {     if (b < a) a = b;   }   const int N = 5005, M = 0, Z = 1e9 + 7;   const int inf = 0x3f3f3f3f;   template <class T1, class T2>   inline void gadd(T1 &a, T2 b) {     a = (a + b) % Z;   }   int casenum, casei;   int n, m;   int f[N][N];   int d[N][N];   int v[N];   int c[N];   vector<int> a[N];   int sz[N];   void dfs(int x) {     f[x][0] = d[x][0] = sz[x] = 0;     for (auto y : a[x]) {       dfs(y);       for (int i = sz[x]; ~i; --i) {         for (int j = 1; j <= sz[y]; ++j) {           gmin(f[x][i + j], f[x][i] + f[y][j]);           gmin(f[x][i + j], f[x][i] + d[y][j]);           gmin(d[x][i + j], d[x][i] + d[y][j]);         }       }       sz[x] += sz[y];     }     ++sz[x];     for (int i = sz[x]; i; --i) {       f[x][i] = min(f[x][i - 1] + v[x] - c[x], inf);       gmin(d[x][i], d[x][i - 1] + v[x]);     }   }   int main() {     while (~scanf( %d%d , &n, &m)) {       memset(f, 63, sizeof(f));       memset(d, 63, sizeof(d));       for (int i = 1; i <= n; ++i) {         a[i].clear();         scanf( %d%d , &v[i], &c[i]);         if (i > 1) {           int fa;           scanf( %d , &fa);           a[fa].push_back(i);         }       }       dfs(1);       int ans = 0;       for (int i = 1; i <= n; ++i) {         if (f[1][i] <= m || d[1][i] <= m) {           gmax(ans, i);         }       }       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1000003;   const int maxn = 500010;   int n, m;   char str[maxn];   struct node {     int lr, ud;   } ma[maxn];   inline char s(int i, int j) { return str[(i - 1) * m + (j - 1)]; }   int tran(int i, int j) { return (i - 1) * m + (j - 1); }   int solve() {     int ret = 1;     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         int ind = tran(i, j);         char cha = s(i, j);         if (cha >=  1  && cha <=  4 ) {           if (cha ==  1 ) {             if (ma[ind].lr != 0 && ma[ind].lr != 1) return -1;             if (ma[ind].ud != 0 && ma[ind].ud != 1) return -1;             ma[ind].lr = 1;             ma[ind].ud = 1;             for (int k = i - 1; k >= 1; --k) {               int tp = tran(k, j);               if (ma[tp].ud == 0)                 ma[tp].ud = ((i - k) % 2 ? 2 : 1);               else {                 if (ma[tp].ud != ((i - k) % 2 ? 2 : 1))                   return -1;                 else                   break;               }             }             for (int k = i + 1; k <= n; ++k) {               int tp = tran(k, j);               if (ma[tp].ud == 0)                 ma[tp].ud = ((k - i) % 2 ? 2 : 1);               else {                 if (ma[tp].ud != ((k - i) % 2 ? 2 : 1))                   return -1;                 else                   break;               }             }             for (int k = j - 1; k >= 1; --k) {               int tp = tran(i, k);               if (ma[tp].lr == 0)                 ma[tp].lr = ((j - k) % 2 ? 2 : 1);               else {                 if (ma[tp].lr != ((j - k) % 2 ? 2 : 1))                   return -1;                 else                   break;               }             }             for (int k = j + 1; k <= m; ++k) {               int tp = tran(i, k);               if (ma[tp].lr == 0)                 ma[tp].lr = ((k - j) % 2 ? 2 : 1);               else {                 if (ma[tp].lr != ((k - j) % 2 ? 2 : 1))                   return -1;                 else                   break;               }             }           } else if (cha ==  2 ) {             if (ma[ind].lr != 0 && ma[ind].lr != 1) return -1;             if (ma[ind].ud != 0 && ma[ind].ud != 2) return -1;             ma[ind].lr = 1;             ma[ind].ud = 2;             for (int k = i - 1; k >= 1; --k) {               int tp = tran(k, j);               if (ma[tp].ud == 0)                 ma[tp].ud = ((i - k) % 2 ? 1 : 2);               else {                 if (ma[tp].ud != ((i - k) % 2 ? 1 : 2))                   return -1;                 else                   break;               }             }             for (int k = i + 1; k <= n; ++k) {               int tp = tran(k, j);               if (ma[tp].ud == 0)                 ma[tp].ud = ((k - i) % 2 ? 1 : 2);               else {                 if (ma[tp].ud != ((k - i) % 2 ? 1 : 2))                   return -1;                 else                   break;               }             }             for (int k = j - 1; k >= 1; --k) {               int tp = tran(i, k);               if (ma[tp].lr == 0)                 ma[tp].lr = ((j - k) % 2 ? 2 : 1);               else {                 if (ma[tp].lr != ((j - k) % 2 ? 2 : 1))                   return -1;                 else                   break;               }             }             for (int k = j + 1; k <= m; ++k) {               int tp = tran(i, k);               if (ma[tp].lr == 0)                 ma[tp].lr = ((k - j) % 2 ? 2 : 1);               else {                 if (ma[tp].lr != ((k - j) % 2 ? 2 : 1))                   return -1;                 else                   break;               }             }           } else if (cha ==  3 ) {             if (ma[ind].lr != 0 && ma[ind].lr != 2) return -1;             if (ma[ind].ud != 0 && ma[ind].ud != 2) return -1;             ma[ind].lr = 2;             ma[ind].ud = 2;             for (int k = i - 1; k >= 1; --k) {               int tp = tran(k, j);               if (ma[tp].ud == 0)                 ma[tp].ud = ((i - k) % 2 ? 1 : 2);               else {                 if (ma[tp].ud != ((i - k) % 2 ? 1 : 2))                   return -1;                 else                   break;               }             }             for (int k = i + 1; k <= n; ++k) {               int tp = tran(k, j);               if (ma[tp].ud == 0)                 ma[tp].ud = ((k - i) % 2 ? 1 : 2);               else {                 if (ma[tp].ud != ((k - i) % 2 ? 1 : 2))                   return -1;                 else                   break;               }             }             for (int k = j - 1; k >= 1; --k) {               int tp = tran(i, k);               if (ma[tp].lr == 0)                 ma[tp].lr = ((j - k) % 2 ? 1 : 2);               else {                 if (ma[tp].lr != ((j - k) % 2 ? 1 : 2))                   return -1;                 else                   break;               }             }             for (int k = j + 1; k <= m; ++k) {               int tp = tran(i, k);               if (ma[tp].lr == 0)                 ma[tp].lr = ((k - j) % 2 ? 1 : 2);               else {                 if (ma[tp].lr != ((k - j) % 2 ? 1 : 2))                   return -1;                 else                   break;               }             }           } else {             if (ma[ind].lr != 0 && ma[ind].lr != 2) return -1;             if (ma[ind].ud != 0 && ma[ind].ud != 1) return -1;             ma[ind].lr = 2;             ma[ind].ud = 1;             for (int k = i - 1; k >= 1; --k) {               int tp = tran(k, j);               if (ma[tp].ud == 0)                 ma[tp].ud = ((i - k) % 2 ? 2 : 1);               else {                 if (ma[tp].ud != ((i - k) % 2 ? 2 : 1))                   return -1;                 else                   break;               }             }             for (int k = i + 1; k <= n; ++k) {               int tp = tran(k, j);               if (ma[tp].ud == 0)                 ma[tp].ud = ((k - i) % 2 ? 2 : 1);               else {                 if (ma[tp].ud != ((k - i) % 2 ? 2 : 1))                   return -1;                 else                   break;               }             }             for (int k = j - 1; k >= 1; --k) {               int tp = tran(i, k);               if (ma[tp].lr == 0)                 ma[tp].lr = ((j - k) % 2 ? 1 : 2);               else {                 if (ma[tp].lr != ((j - k) % 2 ? 1 : 2))                   return -1;                 else                   break;               }             }             for (int k = j + 1; k <= m; ++k) {               int tp = tran(i, k);               if (ma[tp].lr == 0)                 ma[tp].lr = ((k - j) % 2 ? 1 : 2);               else {                 if (ma[tp].lr != ((k - j) % 2 ? 1 : 2))                   return -1;                 else                   break;               }             }           }         }       }     }     map<int, bool> row, col;     map<int, bool>::iterator it;     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         int ind = tran(i, j);         if (!ma[ind].lr) row[i] = 1;         if (!ma[ind].ud) col[j] = 1;       }     }     for (int i = 1; i <= row.size(); ++i) ret = ret * 2 % mod;     for (int i = 1; i <= col.size(); ++i) ret = ret * 2 % mod;     return ret;   }   int main() {     while (~scanf( %d%d , &n, &m)) {       int ans = 0;       memset(ma, 0, sizeof(ma));       for (int i = 1; i <= n; ++i) scanf( %s , str + (i - 1) * m);       int ret = solve();       if (ret == -1)         puts( 0 );       else         printf( %d n , ret);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     long long k, x;     cin >> t;     while (t--) {       cin >> k >> x;       cout << x + ((k - 1) * 9) << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   long long n, t, i, j, a[300006], q;   int main() {     scanf( %lld , &t);     while (t--) {       long long ans = 0;       memset(a, 0, sizeof a);       scanf( %lld%lld , &n, &q);       for (i = 1; i <= n; i++) {         scanf( %lld , &a[i]);       }       long long flag = 0;       for (i = 1; i <= n; i++) {         if (a[i] > a[i - 1] && a[i] > a[i + 1] && flag == 0) {           ans += a[i];           flag = 1;         } else if (a[i] < a[i - 1] && a[i] < a[i + 1] && flag == 1) {           ans -= a[i];           flag = 0;         }       }       printf( %lld n , ans);     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n, k1, k2;       cin >> n >> k1 >> k2;       bool ya = 0;       while (k1--) {         int a;         cin >> a;         if (a == n) {           ya = 1;         }       }       while (k2--) {         int a;         cin >> a;       }       if (ya) {         cout <<  YES  << endl;       } else {         cout <<  NO  << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2010;   vector<int> edges[MAXN];   queue<int> q;   int visited[MAXN];   int main() {     int n, m, a, b;     char x;     cin >> n >> m;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         cin >> x;         if (x ==  # ) {           edges[i].push_back(n + j);           edges[n + j].push_back(i);         }       }     }     q.push(n - 1);     q.push(0);     while (!q.empty()) {       a = q.front();       q.pop();       b = q.front();       q.pop();       if (a == 0) {         cout << b;         return 0;       }       for (int i : edges[a]) {         if (!visited[i]) {           q.push(i);           q.push(b + 1);           visited[i] = true;         }       }     }     cout << -1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 5;   int n, q;   int a[maxn], b[maxn], f[maxn], g[maxn][30];   int c(int l, int r) {     int k = (int)log2(r - l + 1);     return max(g[l][k], g[r - (1 << k) + 1][k]);   }   void solve() {     cin >> n;     for (int i = 1; i <= n; ++i) cin >> a[i];     cin >> q;     for (int i = 1; i <= q; ++i) {       int id, p, first;       cin >> id;       if (id == 1) {         cin >> p >> first;         a[p] = first;         b[p] = i;       } else         cin >> f[i];     }     for (int i = 1; i <= q; ++i) g[i][0] = f[i];     int m = (int)log2(q) + 1;     for (int j = 1; j <= m; ++j)       for (int i = 1; i + (1 << (j - 1)) - 1 <= q; ++i)         g[i][j] = max(g[i][j - 1], g[i + (1 << (j - 1))][j - 1]);     for (int i = 1; i <= n; ++i) cout << max(a[i], c(max(1, b[i]), q)) <<    ;   }   int main() { solve(); }


#include <bits/stdc++.h>   using namespace std;   string itos(int a) {     if (!a) return  0 ;     string res;     while (a) {       res += (char)( 0  + (a % 10));       a /= 10;     }     reverse((res).begin(), (res).end());     return res;   }   int main() {     int n;     int a[10], can = 0;     ;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];       can |= 1 << a[i];     }     if (n > 6) {       cout <<  0  << endl;       return 0;     }     vector<string> v;     vector<int> mask;     for (int i = 0; i < 256; i++) {       string s = itos(i);       int tmp = 0;       for (int j = 0; j < s.size(); j++) {         tmp |= 1 << (int)(s[j] -  0 );       }       if ((tmp & can) == tmp) {         v.push_back(s);         mask.push_back(tmp);       }     }     vector<string> ans;     for (int i = 0; i < (int)(v.size()); i++)       for (int j = 0; j < (int)(v.size()); j++) {         int size = min(v[i].size(), v[j].size());         bool f = true;         for (int k = 0; k < size; k++) {           if (v[i][k] != v[j][v[j].size() - 1 - k]) {             f = false;             break;           }         }         if (!f) continue;         for (int k = 0; k < (int)(v.size()); k++)           for (int l = 0; l < (int)(v.size()); l++) {             if ((mask[i] | mask[j] | mask[k] | mask[l]) != can) continue;             string s = v[i] + v[k] + v[l] + v[j];             bool f = true;             for (int x = 0; 2 * x < s.size(); x++) {               if (s[x] != s[s.size() - 1 - x]) {                 f = false;                 break;               }             }             if (f) ans.push_back(v[i] +  .  + v[k] +  .  + v[l] +  .  + v[j]);           }       }     cout << ans.size() << endl;     for (int i = 0; i < ans.size(); i++) cout << ans[i] << endl;   }


#include <bits/stdc++.h>   #pragma GCC target( avx2 )   #pragma GCC optimization( O3 )   #pragma GCC optimization( unroll-loops )   using namespace std;   long long fn(long long x, long long rn[]) {     if (x == rn[x])       return x;     else       return rn[x] = fn(rn[x], rn);   }   bool un(long long x, long long y, long long rn[], long long sz[]) {     x = fn(x, rn);     y = fn(y, rn);     if (x == y) return false;     if (sz[x] < sz[y]) swap(x, y);     sz[x] += sz[y];     rn[y] = x;     return true;   }   long long gcd(long long a, long long b) {     if (!b) return a;     return gcd(b, a % b);   }   long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }   long long MOD = 998244353;   long long mod = 1000000007;   long long power(long long x, long long y, long long p) {     long long res = 1;     x %= p;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }   long long inv(long long val, long long MODx = MOD) {     return power(val, MODx - 2, MODx);   }   vector<long long> fac, ifac;   void preFac(long long sz) {     fac.resize(sz + 1), ifac.resize(sz + 1);     fac[0] = 1;     for (int i = 1; i <= sz; i++) {       fac[i] = (i * fac[i - 1]) % MOD;     }     ifac[sz] = inv(fac[sz]);     for (int i = sz - 1; i >= 0; i--) {       ifac[i] = ((i + 1) * ifac[i + 1]) % MOD;     }   }   long long nCr(long long N, long long R) {     if (R <= N and R >= 0) {       return ((fac[N] * ifac[R]) % MOD * ifac[N - R]) % MOD;     }     return 0;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n, k;     cin >> n >> k;     map<long long, long long> pos;     long long p[n + 1], q[n + 1];     for (long long i = 1; i <= n; i++) {       cin >> p[i];     }     for (long long i = 1; i <= n; i++) {       cin >> q[i];       pos[q[i]] = i;     }     long long pt = 1;     long long pt_cons = 1;     vector<vector<long long>> comp;     vector<long long> vec;     while (pt <= n) {       vec.push_back(p[pt]);       pt_cons = max(pt_cons, pos[p[pt]]);       if (pt == pt_cons) {         comp.push_back(vec);         vec.clear();         pt_cons = 1;       }       pt++;     }     if (!vec.empty()) {       comp.push_back(vec);     }     if (comp.size() < k) {       cout <<  NO ;       return 0;     }     string ans(n + 1,  * );     char x =  a ;     for (auto u : comp) {       for (auto v : u) {         ans[v] = x;       }       x++;       if (x == ( z  + 1)) {         x =  z ;       }     }     cout <<  YES n ;     for (long long i = 1; i <= n; i++) {       cout << ans[i];     }   }


#include <bits/stdc++.h>   using namespace std;   int n, s;   int c[3];   void init() {     int score;     scanf( %d%d , &n, &s);     for (int i = 0; i < n; i++) {       scanf( %d , &score);       c[score - 3]++;     }   }   int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }   void extended_gcd(int a, int b, int c, int &x, int &y) {     if (!b) {       x = c / a, y = 0;       return;     }     extended_gcd(b, a % b, c, y, x);     y -= (a / b) * x;   }   void solve() {     int k[3], ret = 0x7FFFFFFF;     int d = gcd(c[1], c[2]);     for (int i = 0; i <= s; i++) {       int A = c[1], B = c[2], C = s - i * c[0], x, y;       if (C < 0) break;       if (C % d != 0) continue;       extended_gcd(A, B, C, x, y);       int delta1 = B / d, delta2 = A / d;       if (y > x && delta2 != 0) {         int delta = (y - x) / delta2;         y -= delta2 * delta;         x += delta1 * delta;       }       for (int step = 17; step >= 0; step--) {         if (y >= x) break;         if (y + delta2 * (1 << step) <= x - delta1 * (1 << step)) {           y += delta2 * (1 << step);           x -= delta1 * (1 << step);         }       }       if (c[1] * x > c[2] * y) {         for (int step = 17; step >= 0; step--)           if (x - delta1 * (1 << step) >= i) {             if (c[1] * (x - delta1 * (1 << step)) >=                 c[2] * (y + delta2 * (1 << step))) {               x -= delta1 * (1 << step);               y += delta2 * (1 << step);             }           }       }       if (y < i && delta2 != 0) {         int delta = (i - y - 1) / delta2 + 1;         y += delta2 * delta;         x -= delta1 * delta;       }       for (int j = 0;; j++) {         if (x >= i && y >= x &&             abs(c[0] * i - c[1] * x) + abs(c[1] * x - c[2] * y) < ret) {           ret = abs(c[0] * i - c[1] * x) + abs(c[1] * x - c[2] * y);           k[0] = i, k[1] = x, k[2] = y;         }         if (y >= x && c[2] * y > c[1] * x) {           break;         }         y += delta2;         x -= delta1;         if (x < i) break;       }     }     if (ret == 0x7FFFFFFF) {       printf( %d n , -1);     } else       printf( %d %d %d n , k[0], k[1], k[2]);   }   int main() {     init();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   const int M = 1e9 + 7;   long long a[N];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long a, b, x, y, z;     cin >> a >> b >> x >> y >> z;     cout << max(0ll, y - (a - 2 * x)) + max(0ll, y - (b - 3 * z));   }


#include <bits/stdc++.h>   using namespace std;   template <typename Arg1>   void __f(const char* name, Arg1&& arg1) {     std::cerr << name <<   :   << arg1 << endl;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     std::cerr.write(names, comma - names) <<   :   << arg1 <<   |  ;     __f(comma + 1, args...);   }   const long long MOD = 998244353;   const long long INFLL = 0x3f3f3f3f3f3f3f3f;   const int INF = 0x3f3f3f3f;   const long long MAXN = 1e+5 + 7;   const int m = 998244353;   const int nax = 1e6;   long long fatorial[nax + 1];   long long binary_pow(long long a, long long b) {     a = a % m;     long long ans = 1;     while (b > 0) {       if (b & 1) {         ans = (ans * a) % m;       }       a = (a * a) % m;       b = b >> 1;     }     return ans;   }   long long modular_inverse(long long a) { return binary_pow(a, m - 2); }   void calcula_fatorial() {     fatorial[0] = 1;     for (int i = 1; i <= nax; i++) {       fatorial[i] = (fatorial[i - 1] * i) % m;     }   }   long long binomial_coefficient(int n, int k) {     return fatorial[n] * modular_inverse(fatorial[k]) % m *            modular_inverse(fatorial[n - k]) % m;   }   void solve() {     long long n, k;     cin >> n >> k;     set<long long> evento;     map<long long, long long> entrada, saida;     for (int i = 0; i < n; ++i) {       long long a, b;       cin >> a >> b;       evento.insert(a);       evento.insert(b);       entrada[a]++;       saida[b]++;     }     long long ans = 0, sz = 0;     for (auto e : evento) {       sz += entrada[e];       for (int i = 0; i < saida[e]; ++i) {         if (sz - 1 < k - 1) {           sz--;           continue;         }         ans += binomial_coefficient(sz - 1, k - 1);         ans %= m;         sz--;       }     }     cout << ans <<   n ;   }   int main() {     calcula_fatorial();     ios_base::sync_with_stdio(0);     cin.tie(0);     long long i, j, n;     int t = 1;     while (t--) solve();     return 0;   }


#include<bits/stdc++.h>   using namespace std;      #define INF 1234567890   #define ll long long      int T;   int N, M, P;   vector<int> g[400401], h[400401];   bool vis[400401], reach[400401], loop[400401];   vector<int> st;   int myscc[400401], sz[400401], dp[400401];      void DFS(int n)   {    vis[n] = true;    for(int next : g[n])    {     if (vis[next]) continue;     DFS(next);    }    st.push_back(n);   }   void DFS2(int n)   {    vis[n] = true;    for(int next : h[n])    {     if (vis[next]) continue;     DFS2(next);    }    myscc[n] = P;    sz[P]++;   }   void DFS3(int n)   {    vis[n] = true;    for(int next : g[n])    {     if (vis[next]) continue;     DFS3(next);    }   }      bool isScc(int i)   {    return (sz[myscc[i]] >= 2 || loop[i]);   }      int main()   {    ios::sync_with_stdio(0); cin.tie(0);    cin.exceptions(ios::badbit | ios::failbit);    cin >> T;    while(T--)    {     cin >> N >> M;     // clear     P = 0;     for(int i=1; i<=N; i++)     {      g[i].clear();      h[i].clear();     }     memset(vis, 0, sizeof(bool)*(N+1));     memset(reach, 0, sizeof(bool)*(N+1));     memset(loop, 0, sizeof(bool)*(N+1));     st.clear();        for(int i=0; i<M; i++)     {      int a, b;      cin >> a >> b;      if (a == b) { loop[a] = true; continue; }      g[a].push_back(b);      h[b].push_back(a);     }        DFS(1);     for(int i=1; i<=N; i++)      if (vis[i])       reach[i] = true;        for(int i=1; i<=N; i++)      if (!vis[i])       DFS(i);        memset(vis, 0, sizeof(bool)*(N+1));     reverse(st.begin(), st.end()); // DFS 종료 역순 = 위상정렬순     for(int n : st)     {      if (vis[n]) continue;      sz[P] = 0;      DFS2(n);      P++;     }        memset(vis, 0, sizeof(bool)*(N+1));     for(int i=1; i<=N; i++)      if (reach[i] && isScc(i) && !vis[i])       DFS3(i); // 1 -> scc -> x가 가능한가?        memset(dp, 0, sizeof(int)*(N+1)); // 0,1,2+     dp[1] = 1;     for(int n : st)     {      if (dp[n] > 2) dp[n] = 2;      for(int next : g[n])       dp[next] += dp[n];     }        for(int i=1; i<=N; i++)     {      if (reach[i] == false) cout <<  0  ;      else if (vis[i]) cout <<  -1  ;      else if (dp[i] >= 2) cout <<  2  ;      else cout <<  1  ;     }     cout <<   n ;    }    return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     int t;     cin >> t;     vector<int> a(7);     while (t--) {       long long k;       cin >> k;       int sum = 0;       for (int i = 0; i < 7; i++) {         cin >> a[i];         sum += a[i];       }       long long ans = (k / sum) * 7;       long long rem = k % sum;       if (rem == 0) {         ans -= 7;         rem = sum;       }       int min = 7;       int count = 0, count_sum = 0;       for (int i = 0; i < 7; i++) {         count = 0;         count_sum = 0;         for (int j = 0; j < 7; j++) {           count++;           if (a[(i + j) % 7]) {             count_sum++;           }           if (count_sum == rem) {             if (count < min) {               min = count;               break;             }           }         }       }       cout << ans + min << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int go[2], sum;     node(int x0, int x1, int _sum) { go[0] = x0, go[1] = x1, sum = _sum; }   };   node root(-1, -1, 0);   vector<node> t = {root};   int get_sum(int v) { return v == -1 ? 0 : t[v].sum; }   int go(int v, int id) {     if (t[v].go[id] == -1) {       t[v].go[id] = t.size();       t.push_back(node(-1, -1, 0));     }     return t[v].go[id];   }   void add(int a) {     int v = 0;     for (int bit = 29; bit >= 0; bit--) {       int x = (a >> bit) & 1;       v = go(v, x);       t[v].sum++;     }   }   int get(int a, int k) {     int v = 0, ans = 0, bit = 29;     while (bit >= 0) {       int x = (a >> bit) & 1, y = (k >> bit) & 1;       if (x > y) ans += get_sum(t[v].go[0]);       if ((x ^ 1) > y) ans += get_sum(t[v].go[1]);       if (t[v].go[x ^ y] == -1) break;       v = go(v, x ^ y);       bit--;     }     if (bit == -1) ans += t[v].sum;     return ans;   }   void solve() {     int n, k;     cin >> n >> k;     add(0);     long long pref = 0, ans = 0;     for (int i = 1; i <= n; i++) {       int v;       cin >> v;       pref = pref ^ v;       ans += get(pref, k);       add(pref);     }     cout << ans <<   n ;   }   int main() {     ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);     cout.setf(ios::fixed), cout.precision(20);     solve();   }


#include <bits/stdc++.h>   using namespace std;   long long mod = 1000000009;   long long A[200000];   long long root(long long a) {     long long i = a;     if (A[i] != i) {       A[i] = root(A[i]);     }     return A[a];   }   void unio(long long a, long long b) {     long long ra = root(a);     long long rb = root(b);     if (ra != rb) {       A[ra] = rb;     }   }   signed main() {     long long n, m;     cin >> n >> m;     long long val = 1;     for (long long i = 1; i <= n; i++) {       A[i] = i;     }     for (long long i = 1; i <= m; i++) {       long long a, b;       cin >> a >> b;       if (root(a) == root(b)) {         val = (val * 2LL) % mod;       } else {         unio(a, b);       }       val--;       val = (val + mod) % mod;       cout << val << endl;       val++;     }   }


#include <bits/stdc++.h>   const int maxn = 200005, K = 37, mod = 1e9 + 7;   int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }   int dec(int a, int b) { return a - b < 0 ? a - b + mod : a - b; }   int mul(int a, int b) { return 1ll * a * b % mod; }   int ksm(int a, int b = mod - 2) {     int ret = 1;     for (; b; b >>= 1, a = mul(a, a))       if (b & 1) ret = mul(ret, a);     return ret;   }   int n, a[maxn];   std::tuple<int, int, int> p[maxn];   int f[maxn], c[maxn], mx, ans;   void ins(int x, int v) {     for (; x <= mx; x += x & -x) c[x] = std::max(c[x], v);   }   int ask(int x) {     int res = 0xcfcfcfcf;     for (; x; x -= x & -x) res = std::max(res, c[x]);     return res;   }   int main() {     memset(c, 0xcf, sizeof(c));     memset(f, 0xcf, sizeof(f));     scanf( %d , &n);     for (int i = 1; i <= n; i++)       scanf( %d , a + i), p[i] = std::make_tuple(i - a[i], a[i], i),                           mx = std::max(mx, a[i]);     mx++;     std::sort(p + 1, p + 1 + n);     ins(1, 0);     for (int i = 1; i <= n; i++) {       if (std::get<2>(p[i]) >= std::get<1>(p[i]))         f[i] = ask(std::get<1>(p[i])) + 1;       ins(std::get<1>(p[i]) + 1, f[i]);       ans = std::max(ans, f[i]);     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = (1 << 30) - 1;   const long long lINF = (1LL << 62) - 1;   const double dINF = 1e40;   const int MAXT = 1 << 22, MAXN = 1001000;   struct seg_tree {     int sum[MAXT], len;     void build(int n) {       for (len = 1; len < n; len <<= 1)         ;     }     template <int val>     void update(int pos) {       for (int x = pos + len; x > 0; x >>= 1) sum[x] += val;     }     int find(int k) {       int x;       for (x = 1; x < len;) {         x <<= 1;         if (sum[x] < k) {           k -= sum[x];           x ^= 1;         }       }       return x - len;     }   } ds;   int pos[MAXT], val[MAXN];   bool app[MAXN];   int main() {     int n, m;     scanf( %d%d , &n, &m);     ds.build(n + m);     for (int i = 0; i < n; i++) {       pos[m + i] = i;       ds.update<1>(m + i);     }     memset(val, -1, sizeof(val));     bool flag = true;     for (int i = m - 1; i >= 0; i--) {       int k, mark;       scanf( %d%d , &mark, &k);       int p = ds.find(k), op = pos[p];       ds.update<-1>(p);       pos[i] = op;       ds.update<1>(i);       if (val[op] == -1)         val[op] = mark;       else if (val[op] != mark) {         flag = false;         break;       }     }     memset(app, false, sizeof(app));     for (int i = 0; i < n; i++)       if (val[i] != -1) {         if (app[val[i]]) {           flag = false;           break;         }         app[val[i]] = true;       }     if (flag) {       for (int i = 0, v = 1; i < n; i++) {         if (val[i] == -1) {           for (; app[v]; ++v)             ;           app[v] = true;           val[i] = v;         }         if (i == n - 1)           printf( %d n , val[i]);         else           printf( %d  , val[i]);       }     } else       puts( -1 );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = (long long)-1e18;   const int N = 200 * 1000 + 100;   vector<int> g[N];   vector<int> w[N];   vector<pair<long long, long long> > cur;   long long add[N];   long long ans[N];   int a[N];   long long dfs2(int v) {     ans[v] = add[v];     for (int i = 0; i < g[v].size(); i++) ans[v] += dfs2(g[v][i]);     return ans[v];   }   void dfs(int v, int par, long long h) {     if (v != par) {       long long dist = h - a[v];       vector<pair<long long, long long> >::iterator it = lower_bound(           cur.begin(), cur.end(), pair<long long, long long>(dist, INF));       add[par]++;       if (it != cur.begin()) {         it--;         add[it->second]--;       }     }     cur.push_back(pair<long long, long long>(h, v));     for (int i = 0; i < g[v].size(); i++) dfs(g[v][i], v, h + w[v][i]);     cur.pop_back();   }   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];     }     for (int i = 1; i < n; i++) {       int p, l;       cin >> p >> l;       p--;       g[p].push_back(i);       w[p].push_back(l);     }     dfs(0, 0, 0);     dfs2(0);     for (long long i = 0; i < n; i++) cout << ans[i] <<    ;     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   std::mt19937 rnd(       (int)std::chrono::steady_clock::now().time_since_epoch().count());   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   const int NDIG = 6;   const int MAXQ = 100000;   const int MAXSUM = 999999;   int nvals;   int gain[NDIG];   int nq;   int q[MAXQ];   long long qans[MAXQ];   long long dp[NDIG + 1][MAXSUM + 1];   long long tmp[MAXSUM + 1];   pair<long long, int> qq[MAXSUM + 1];   int qhead, qtail;   void solve() {     for (int ndig = (0); ndig <= (NDIG); ++ndig)       for (int sum = (0); sum <= (MAXSUM); ++sum)         dp[ndig][sum] = ndig == 0 && sum == 0 ? 0 : LLONG_MIN;     for (int ndig = (1); ndig <= (NDIG); ++ndig) {       int curgain = gain[NDIG - ndig];       long long vargain = (long long)10 * curgain;       for (int sum = (0); sum <= (MAXSUM); ++sum)         tmp[sum] = dp[ndig - 1][sum] == LLONG_MIN                        ? LLONG_MIN                        : dp[ndig - 1][sum] - (sum / 3) * vargain;       for (int mod = (0); mod < (30); ++mod)         if (mod <= 9 * nvals) {           long long fixedgain = (long long)(mod / 3) * curgain;           qhead = qtail = 0;           for (int sum = mod; sum <= MAXSUM; sum += 10) {             int fst = (sum - mod) / 10;             int qmx = min((sum - mod) / 30, (9 * nvals - mod) / 30);             long long now = tmp[fst];             while (qtail < qhead && now >= qq[qhead - 1].first) --qhead;             qq[qhead++] = make_pair(now, fst);             while (qtail < qhead && qq[qtail].second < fst - (qmx - 10)) ++qtail;             long long best = qtail < qhead ? qq[qtail].first : LLONG_MIN;             if (best != LLONG_MIN) {               dp[ndig][sum] =                   max(dp[ndig][sum], best + fixedgain + (fst / 3) * vargain);             }             for (int q = (max(0, qmx - 10)); q <= (qmx); ++q) {               long long opt = dp[ndig - 1][fst - 3 * q];               if (opt == LLONG_MIN) continue;               int nthree = mod / 3 + 10 * q;               opt += fixedgain + q * vargain;               if (nthree >= 3 * (nvals - 1) && mod % 3 != 0)                 opt -= (long long)(nthree - 3 * (nvals - 1)) * curgain;               dp[ndig][sum] = max(dp[ndig][sum], opt);             }           }         }     }     for (int i = (0); i < (nq); ++i) qans[i] = dp[NDIG][q[i]];   }   void run() {     scanf( %d , &nvals);     for (int i = (0); i < (NDIG); ++i) scanf( %d , &gain[i]);     scanf( %d , &nq);     for (int i = (0); i < (nq); ++i) scanf( %d , &q[i]);     solve();     for (int i = (0); i < (nq); ++i) printf( %lld n , qans[i]);   }   long long qansstupid[MAXQ];   void solvestupid() {     auto calc = [&](int x) {       long long ret = 0;       for (int i = 0; x > 0; ++i, x /= 10)         if (x % 10 % 3 == 0) ret += (long long)(x % 10 / 3) * gain[i];       return ret;     };     for (int i = (0); i < (nq); ++i) {       int sum = q[i];       qansstupid[i] = LLONG_MIN;       if (nvals == 4) {         for (int a = (0); a <= (sum); ++a)           for (int b = (0); b <= (sum - a); ++b)             for (int c = (0); c <= (sum - a - b); ++c) {               int d = sum - a - b - c;               long long cur = calc(a) + calc(b) + calc(c) + calc(d);               qansstupid[i] = max(qansstupid[i], cur);             }       } else if (nvals == 3) {         for (int a = (0); a <= (sum); ++a)           for (int b = (0); b <= (sum - a); ++b) {             int c = sum - a - b;             long long cur = calc(a) + calc(b) + calc(c);             qansstupid[i] = max(qansstupid[i], cur);           }       } else if (nvals == 2) {         for (int a = (0); a <= (sum); ++a) {           int b = sum - a;           long long cur = calc(a) + calc(b);           qansstupid[i] = max(qansstupid[i], cur);         }       } else if (nvals == 1) {         int a = sum;         long long cur = calc(a);         qansstupid[i] = max(qansstupid[i], cur);       } else {         assert(false);       }     }   }   void stress() {     for (int rep = (0); rep < (1000); ++rep) {       nvals = rnd() % 4 + 1;       int sum = rnd() % (nvals == 4 ? 100 : 10000) + 1;       for (int i = (0); i < (NDIG); ++i) gain[i] = rnd() % 1000 + 1;       nq = 1, q[0] = sum;       solve();       solvestupid();       bool ok = true;       for (int i = (0); i < (nq); ++i)         if (qans[i] != qansstupid[i]) ok = false;       if (ok) {         printf( . );         continue;       }       printf( err have=%lld want=%lld n , qans[0], qansstupid[0]);       printf( %d n , nvals);       for (int i = (0); i < (NDIG); ++i) {         if (i != 0) printf(   );         printf( %d , gain[i]);       }       puts(  );       printf( %d n , nq);       for (int i = (0); i < (nq); ++i) printf( %d n , q[i]);     }   }   int main() {     run();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool isprime(int a) {     for (int i = 2; i * i <= a; i++) {       if (a % i == 0) return false;     }     return true;   }   bool v[10010];   int gcd(int a, int b) {     if (a % b == 0) return b;     return gcd(b, a % b);   }   int main() {     int T;     cin >> T;     for (int i = 2; i < 10010; i++) {       v[i] = isprime(i);     }     while (T--) {       int a;       int b;       cin >> a >> b;       if (a == 1 || b == 1) {         cout <<  Finite  << endl;         continue;       }       if (gcd(a, b) == 1) {         cout <<  Finite  << endl;         continue;       } else {         cout <<  Infinite  << endl;         continue;       }     }     return 0;   }