#include <bits/stdc++.h>   using namespace std;   using namespace std::chrono;   template <typename A>   ostream &operator<<(ostream &cout, vector<A> const &v);   template <typename A, typename B>   ostream &operator<<(ostream &cout, pair<A, B> const &p) {     return cout <<  (  << p.first <<  ,   << p.second <<  ) ;   }   template <typename A>   ostream &operator<<(ostream &cout, vector<A> const &v) {     cout <<  [ ;     for (int i = 0; i < v.size(); i++) {       if (i) cout <<  ,  ;       cout << v[i];     }     return cout <<  ] ;   }   template <typename A, typename B>   istream &operator>>(istream &cin, pair<A, B> &p) {     cin >> p.first;     return cin >> p.second;   }   mt19937 rng(61378913);   void usaco(string filename) {     freopen((filename +  .in ).c_str(),  r , stdin);     freopen((filename +  .out ).c_str(),  w , stdout);   }   const long double pi = 3.14159265358979323846;   const long long mod = 1000000007;   namespace modop {   long long madd(long long a, long long b) { return (a + b) % mod; }   long long msub(long long a, long long b) {     return (((a - b) % mod) + mod) % mod;   }   long long mmul(long long a, long long b) {     return ((a % mod) * (b % mod)) % mod;   }   long long mpow(long long base, long long exp) {     long long res = 1;     while (exp) {       if (exp % 2 == 1) {         res = (res * base) % mod;       }       exp >>= 1;       base = (base * base) % mod;     }     return res;   }   long long minv(long long base) { return mpow(base, mod - 2); }   long long mdiv(long long a, long long b) { return mmul(a, minv(b)); }   }  // namespace modop   using namespace modop;   long long n, m, k, q, l, r, x, y, z;   long long a[1000005];   long long b[1000005];   long long c[1000005];   string second, t;   long long ans = 0;   map<long long, long long> cnt;   pair<long long, long long> vals[1000005];   void solve(int tc) {     cin >> n >> k;     cnt.clear();     for (int ele = 0; ele < n; ele++) cin >> a[ele];     ;     if (k == 1) {       cout << (n & 1) <<   n ;       return;     }     for (long long i = 0; i < (n); ++i) ++cnt[a[i]];     long long second = cnt.size();     int pos = 0;     for (pair<long long, long long> x : cnt) vals[pos++] = x;     reverse(vals, vals + second);     long long req = 0, last = vals[0].first, ans = 0;     bool fin = 0;     for (long long i = 0; i < (second); ++i) {       long long diff = last - vals[i].first;       last = vals[i].first;       for (long long j = 0; j < (diff); ++j) {         if (req == 0) break;         if (req > 1000000) {           fin = 1;           break;         }         req *= k;         if (req > 1000000) {           fin = 1;           break;         }       }       long long c = vals[i].second;       if (fin) {         ans = msub(ans, c * mpow(k, vals[i].first));         continue;       }       if (c <= req) {         req -= c;         ans = msub(ans, c * mpow(k, vals[i].first));       } else {         c -= req;         ans = 0;         req = 0;         if (c & 1) {           req = 1;           ans = (ans + mpow(k, vals[i].first)) % mod;         }       }     }     cout << (ans % mod) <<   n ;   }   int main() {     { ios_base::sync_with_stdio(false); }     {       cin.tie(NULL);       cout.tie(NULL);     }     int tc = 1;     cin >> tc;     for (int t = 0; t < tc; t++) solve(t);   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100010;   int i, j, k, n, m, vis[maxn], cnt, Q[maxn], top;   void dfs(int now) {     vis[now]++;     if (!vis[now * 2 % n]) dfs(now * 2 % n);     if (!vis[(now * 2 + 1) % n]) dfs((now * 2 + 1) % n);     Q[++top] = now;   }   int main() {     cin >> n;     if (n & 1)       cout << -1 << endl;     else {       dfs(0);       while (top) cout << Q[top--] <<    ;       cout << 0 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<long long int, long long int> > getfac(long long int n) {     vector<pair<long long int, long long int> > ret;     for (long long int i = 1; i < sqrt(n) + 1; i++) {       if (n % i == 0) {         ret.push_back({min({i, n / i}), max({i, n / i})});       }     }     return ret;   }   bool fun1(pair<long long int, long long int> a,             pair<long long int, long long int> b) {     return a.first + a.second < b.first + b.second;   }   bool isin(pair<long long int, long long int> a,             pair<long long int, long long int> b) {     return b.first <= a.first && a.second >= b.second;   }   long long int max(long long int a, long long int b) {     if (a <= b) return a;     return b;   }   long long int min(long long int a, long long int b) {     if (a <= b) return b;     return a;   }   int main() {     long long int a, b, c, d, ans = 1e17;     cin >> a >> b;     c = a;     d = b;     a = min({c, d});     b = max({c, d});     vector<pair<long long int, long long int> > af = getfac(a), bf = getfac(b),                                                 cf = getfac(a + b);     sort(cf.begin(), cf.end(), fun1);     for (int i = 0; i < cf.size(); i += 1) {       long long int p = cf[i].first, q = cf[i].second;       for (int j = 0; j < af.size(); j++) {         if (isin(cf[i], af[j])) {           ans = min({ans, 2 * (p + q)});         }       }       for (int j = 0; j < bf.size(); j++) {         if (isin(cf[i], bf[j])) {           ans = min({ans, 2 * (p + q)});         }       }     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = (2e5 + 42);   const int MAXLOG = 20;   const int64_t inf = (int64_t)1e18;   int n, m, S;   vector<pair<int, int> > G[MAXN];   void read() {     cin >> n >> m >> S;     for (int i = 0; i < m; i++) {       int u, v, w;       cin >> u >> v >> w;       G[u].push_back({v, w});       G[v].push_back({u, w});     }   }   bool in_queue[MAXN];   vector<int> pars[MAXN], adj[MAXN];   int64_t dist[MAXN];   int par[MAXN][MAXLOG], d[MAXN];   int lca(int u, int v) {     if (d[u] < d[v]) swap(u, v);     for (int lg = MAXLOG - 1; lg >= 0; lg--)       if (d[par[u][lg]] >= d[v]) u = par[u][lg];     if (u == v) return u;     for (int lg = MAXLOG - 1; lg >= 0; lg--)       if (par[u][lg] != par[v][lg]) u = par[u][lg], v = par[v][lg];     return par[u][0];   }   void spfa(int st) {     for (int i = 0; i <= n; i++) dist[i] = inf, adj[i].clear(), in_queue[i] = 0;     queue<int> Q;     in_queue[st] = 1;     dist[st] = 0;     Q.push(st);     while (!Q.empty()) {       int u = Q.front();       in_queue[u] = 0;       Q.pop();       for (auto e : G[u]) {         int v = e.first, w = e.second;         if (dist[v] > dist[u] + w) {           dist[v] = dist[u] + w;           if (!in_queue[v]) Q.push(v);           in_queue[v] = 1;         }       }     }     for (int i = 1; i <= n; i++)       for (auto e : G[i])         if (dist[e.first] == dist[i] + e.second) pars[e.first].push_back(i);   }   int tr_sz[MAXN];   void dfs(int u) {     tr_sz[u] = 1;     for (int v : adj[u]) {       dfs(v);       tr_sz[u] += tr_sz[v];     }   }   void solve() {     spfa(S);     int ans = 0;     vector<pair<int64_t, int> > order;     for (int i = 1; i <= n; i++)       if (dist[i] < inf) order.push_back({dist[i], i});     sort(order.begin(), order.end());     for (int i = 0; i < MAXLOG; i++) par[S][i] = S;     for (pair<int64_t, int> e : order) {       if (e.second == S) continue;       int u = e.second;       int l = pars[u][0];       for (int anc : pars[u]) l = lca(l, anc);       par[u][0] = l;       d[u] = d[l] + 1;       adj[l].push_back(u);       for (int lg = 1; lg < MAXLOG; lg++)         par[u][lg] = par[par[u][lg - 1]][lg - 1];     }     dfs(S);     for (int i = 1; i <= n; i++)       if (dist[i] < inf && i != S) ans = max(ans, tr_sz[i]);     cout << ans <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     read();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long c[(1 << 20)];   struct SuffixTree {     int s[(1 << 20)];     map<int, int> to[(1 << 20)];     int len[(1 << 20)] = {(1 << 30)}, fpos[(1 << 20)], link[(1 << 20)],                   id[(1 << 20)];     int node, pos, sz = 1, n = 0;     int make_node(int p, int l, int k) {       fpos[sz] = p;       len[sz] = l;       id[sz] = k;       return sz++;     }     void go_edge() {       while (pos > len[to[node][s[n - pos]]]) {         node = to[node][s[n - pos]];         pos -= len[node];       }     }     void add(int c, int k) {       s[n++] = c;       pos++;       int last = 0;       while (pos > 0) {         go_edge();         int edge = s[n - pos];         int& v = to[node][edge];         int t = s[fpos[v] + pos - 1];         if (v == 0) {           v = make_node(n - pos, (1 << 30), k);           link[last] = node;           last = 0;         } else if (t == c) {           link[last] = node;           return;         } else {           int u = make_node(fpos[v], pos - 1, k);           to[u][c] = make_node(n - 1, (1 << 30), k);           to[u][t] = v;           fpos[v] += pos - 1;           len[v] -= pos - 1;           v = u;           link[last] = u;           last = u;         }         if (node == 0)           pos--;         else           node = link[node];       }     }     long long rr;     int asd[(1 << 20)];     long long dfs(int x, int d) {       long long r = 0;       d += len[x];       if (!((int)(to[x]).size()))         r = c[id[x]];       else {         for (auto p : to[x]) r += dfs(p.second, d);       }       if (len[x]) rr = max(rr, r * d);       return r;     }     long long doit() {       for (int i = n - 1; i >= 0; --i) {         if (s[i] <= 0)           asd[i] = 0;         else           asd[i] = asd[i + 1] + 1;       }       for (int i = 1, ThxDem = sz; i < ThxDem; ++i)         len[i] = min(len[i], asd[fpos[i]]);       len[0] = 0;       rr = 0;       dfs(0, 0);       return rr;     }   };   int n;   SuffixTree t;   char _s[(1 << 20)];   int main() {     scanf( %d , &n);     for (int i = 0, ThxDem = n; i < ThxDem; ++i) {       scanf( %s , _s);       for (int j = 0; _s[j]; ++j) {         t.add(_s[j], i);       }       t.add(-i, i);     }     for (int i = 0, ThxDem = n; i < ThxDem; ++i) scanf( %lld , c + i);     printf( %lld n , t.doit());     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   long long a[100000], b[100000];   vector<pair<int, long long>> connections[100000];   pair<long long, long long> needed(int node) {     long long temp = a[node] - b[node];     long long temp2 = temp / 1000000007;     temp %= 1000000007;     for (const pair<int, int> edge : connections[node]) {       pair<long long, long long> curr = needed(edge.first);       if (curr.first > 0 || curr.first == 0 && curr.second >= 0) {         curr.first *= edge.second;         curr.second *= edge.second;       }       temp += curr.second;       temp2 += temp / 1000000007;       temp %= 1000000007;       temp2 += curr.first;     }     return make_pair(temp2, temp);   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (int i = 0; i < (n); i++) cin >> b[i];     for (int i = 0; i < (n); i++) cin >> a[i];     for (int i = 0; i < (n - 1); i++) {       int x;       long long k;       cin >> x >> k;       connections[--x].push_back(make_pair(i + 1, k));     }     pair<long long, long long> res = needed(0);     if (res.first <= 0 && res.second <= 0)       cout <<  YES n ;     else       cout <<  NO n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dx[] = {-1, 1, 0, 0, 0, 0, 0, 0};   int dy[] = {0, 0, 1, -1, 0, 0, 0, 0};   const int inf = 1000000000;   const double eps = 0.000000009;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int n;     cin >> n;     string a[n];     for (int i = 0; i < n; i++) cin >> a[i];     long long ans = 7;     for (int i = 0; i < n; i++) {       for (int j = i + 1; j < n; j++) {         int diff = 0;         for (int k = 0; k < 6; k++) {           if (a[i][k] != a[j][k]) diff++;         }         ans = min(ans, (long long)ceil(diff / 2.0));       }     }     cout << ans - 1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     multiset<long long> s;     long long now = 0;     for (int i = 1; i <= n; i++) {       long long a;       cin >> a;       a -= i;       s.insert(a);       s.insert(a);       now -= a;       long long x = (*s.rbegin());       s.erase(s.find(x));       now += x;     }     cout << now <<   n ;     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:1024000000,1024000000 )   using namespace std;   const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;   int n, m;   int u[N], v[N];   vector<int> G[N];   bool check(int m) {     for (int i = 1; i <= n; ++i) G[i].clear();     vector<int> in(n + 1, 0);     for (int i = 1; i <= m; ++i) {       G[u[i]].push_back(v[i]);       ++in[v[i]];     }     queue<int> q;     for (int i = 1; i <= n; ++i)       if (!in[i]) q.push(i);     while (q.size()) {       int u = q.front();       q.pop();       if (q.size()) return false;       for (int v : G[u])         if (--in[v] == 0) q.push(v);     }     return true;   }   int main() {     ios_base::sync_with_stdio(0);     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; ++i) scanf( %d%d , u + i, v + i);     int l = 1, r = m;     while (l <= r) {       int m = l + r >> 1;       if (check(m))         r = m - 1;       else         l = m + 1;     }     if (l == m + 1) l = -1;     printf( %d n , l);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 5;   const int MOD = 1e9 + 7;   const int INF = 1e9;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     int n, m;     cin >> n >> m;     int a[n][m], b[n][m];     vector<int> col[n + m], col2[n + m];     int i, j;     bool ans = true;     for (i = 0; i < n; i++) {       for (j = 0; j < m; j++) {         cin >> a[i][j];       }     }     for (i = 0; i < n; i++) {       for (j = 0; j < m; j++) {         cin >> b[i][j];       }     }     for (i = 0; i < n; i++) {       for (j = 0; j < m; j++) {         col[i + j].push_back(a[i][j]);         col2[i + j].push_back(b[i][j]);       }     }     for (i = 0; i < (n + m - 1); i++) {       sort(col[i].begin(), col[i].end());       sort(col2[i].begin(), col2[i].end());       if (col[i] != col2[i]) {         ans = false;       }     }     if (ans) {       cout <<  YES ;     } else {       cout <<  NO ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   int n;   int p[N];   pair<int, int> v[N];   void solve(int nx, int nnx) {     int curr = 1;     for (int i = 1; i <= n; i++) p[i] = 0;     p[curr] = nx;     p[nx] = nnx;     curr = nx;     bool okay = true;     for (int i = 2; i <= n; i++) {       if (v[curr].first == nnx) {         if (!p[nnx]) p[nnx] = v[curr].second;         nx = nnx;         nnx = v[curr].second;         curr = nx;       } else if (v[curr].second == nnx) {         if (!p[nnx]) p[nnx] = v[curr].first;         nx = nnx;         nnx = v[curr].first;         curr = nx;       } else {         okay = false;         break;       }     }     curr = 1;     for (int i = 1; i <= n; i++) {       curr = p[curr];     }     if (curr != 1) okay = false;     if (okay) {       curr = 1;       for (int i = 1; i <= n; i++) {         printf( %d  , curr);         curr = p[curr];       }       exit(0);     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       int x, y;       scanf( %d %d , &x, &y);       v[i].first = x;       v[i].second = y;     }     solve(v[1].first, v[1].second);     solve(v[1].second, v[1].first);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int tmp = 3;     int x = 1, y = 2;     int n;     cin >> n;     bool flag = true;     for (int i = 1; i <= n; i++) {       int q;       cin >> q;       if (q == tmp) {         flag = false;       }       if (q == x)         swap(tmp, y);       else         swap(tmp, x);     }     if (flag)       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename...>   static inline int getchar_unlocked(void) {     return getchar();   }   template <typename...>   static inline void putchar_unlocked(int c) {     putchar(c);   }   void reader(int& x) {     int k, m = 0;     x = 0;     for (;;) {       (k) = getchar_unlocked();       if (k ==  - ) {         m = 1;         break;       }       if ( 0  <= k && k <=  9 ) {         x = k -  0 ;         break;       }     }     for (;;) {       (k) = getchar_unlocked();       if (k <  0  || k >  9 ) break;       x = x * 10 + k -  0 ;     }     if (m) x = -x;   }   void reader(long long& x) {     int k, m = 0;     x = 0;     for (;;) {       (k) = getchar_unlocked();       if (k ==  - ) {         m = 1;         break;       }       if ( 0  <= k && k <=  9 ) {         x = k -  0 ;         break;       }     }     for (;;) {       (k) = getchar_unlocked();       if (k <  0  || k >  9 ) break;       x = x * 10 + k -  0 ;     }     if (m) x = -x;   }   int reader(char c[]) {     int i, s = 0;     for (;;) {       (i) = getchar_unlocked();       if (i !=     && i !=   n  && i !=   r  && i !=   t  && i != EOF) break;     }     c[s++] = i;     for (;;) {       (i) = getchar_unlocked();       if (i ==     || i ==   n  || i ==   r  || i ==   t  || i == EOF) break;       c[s++] = i;     }     c[s] =   0 ;     return s;   }   template <class T, class S>   void reader(T& x, S& y) {     reader(x);     reader(y);   }   template <class T, class S, class U>   void reader(T& x, S& y, U& z) {     reader(x);     reader(y);     reader(z);   }   template <class T, class S, class U, class V>   void reader(T& x, S& y, U& z, V& w) {     reader(x);     reader(y);     reader(z);     reader(w);   }   void writer(int x, char c) {     int s = 0, m = 0;     char f[10];     if (x < 0) m = 1, x = -x;     while (x) f[s++] = x % 10, x /= 10;     if (!s) f[s++] = 0;     if (m) putchar_unlocked( - );     while (s--) putchar_unlocked(f[s] +  0 );     putchar_unlocked(c);   }   void writer(long long x, char c) {     int s = 0, m = 0;     char f[20];     if (x < 0) m = 1, x = -x;     while (x) f[s++] = x % 10, x /= 10;     if (!s) f[s++] = 0;     if (m) putchar_unlocked( - );     while (s--) putchar_unlocked(f[s] +  0 );     putchar_unlocked(c);   }   void writer(const char c[]) {     int i;     for (i = 0; c[i] !=   0 ; i++) putchar_unlocked(c[i]);   }   void writer(const char x[], char c) {     int i;     for (i = 0; x[i] !=   0 ; i++) putchar_unlocked(x[i]);     putchar_unlocked(c);   }   template <class T>   void writerLn(T x) {     writer(x,   n );   }   template <class T, class S>   void writerLn(T x, S y) {     writer(x,    );     writer(y,   n );   }   template <class T, class S, class U>   void writerLn(T x, S y, U z) {     writer(x,    );     writer(y,    );     writer(z,   n );   }   template <class T>   void writerArr(T x[], int n) {     if (!n) {       putchar_unlocked(  n );       return;     }     for (int i = 0; i < (n - 1); i++) writer(x[i],    );     writer(x[n - 1],   n );   }   template <class T>   void chmax(T& l, const T r) {     l = max(l, r);   }   template <class T>   void chmin(T& l, const T r) {     l = min(l, r);   }   template <class T>   T gcd(T a, T b) {     return b ? gcd(b, a % b) : a;   }   template <class T>   T extgcd(T a, T b, T& x, T& y) {     for (T u = y = 1, v = x = 0; a;) {       T q = b / a;       swap(x -= q * u, u);       swap(y -= q * v, v);       swap(b -= q * a, a);     }     return b;   }   template <class T>   T mod_inv(T a, T m) {     T x, y;     extgcd(a, m, x, y);     return (m + x % m) % m;   }   long long mod_pow(long long a, long long n, long long mod) {     long long ret = 1;     long long p = a % mod;     while (n) {       if (n & 1) ret = ret * p % mod;       p = p * p % mod;       n >>= 1;     }     return ret;   }   struct P {     long double x, y;     P() : x(0), y(0) {}     P(const long double& x, const long double& y) : x(x), y(y) {}     P operator+(const P& a) const { return P(x + a.x, y + a.y); }     P& operator+=(const P& a) {       x += a.x;       y += a.y;       return *this;     }     P operator-(const P& a) const { return P(x - a.x, y - a.y); }     P& operator-=(const P& a) {       x -= a.x;       y -= a.y;       return *this;     }     P operator*(const long double a) const { return P(a * x, a * y); }     P& operator*=(const long double a) {       x *= a;       y *= a;       return *this;     }     P operator/(const long double a) const { return P(x / a, y / a); }     P& operator/=(const long double a) {       x /= a;       y /= a;       return *this;     }     bool operator<(const P& a) const {       return (x != a.x) ? (x < a.x) : (y < a.y);     }   };   const double PI = acos(-1);   P rot(const P& a, double theta) {     theta = -theta / 180 * PI;     return P(a.x * cos(theta) - a.y * sin(theta),              a.x * sin(theta) + a.y * cos(theta));   }   long double SQ(long double x) { return x * x; }   long double dist2(const P& a, const P& b) {     return SQ(a.x - b.x) + SQ(a.y - b.y);   }   long double abs2(const P& p) { return SQ(p.x) + SQ(p.y); }   long double dot(const P& a, const P& b) { return a.x * b.x + a.y * b.y; }   long double cross(const P& a, const P& b) { return a.x * b.y - a.y * b.x; }   long double abs(const P& p) { return sqrt(abs2(p)); }   double arg(const P& a) {     double t = atan2(a.y, a.x);     return t < 0 ? t + 2 * PI : t;   }   struct Q {     long double x, y;     int argsum;     static Q zero() {       Q ret = {0, 0, 0};       return ret;     }     bool is_zero() const { return x == 0 && y == 0; }     Q operator+(const Q& r) const {       if (is_zero())         return r;       else if (r.is_zero())         return *this;       P p1(x, y);       P p2(r.x, r.y);       p2 = rot(p2, argsum);       P cur = p1 + p2;       Q ret;       ret.x = cur.x;       ret.y = cur.y;       ret.argsum = argsum + r.argsum;       return ret;     }   };   class seg_tree {    public:     vector<int> len, args;     vector<Q> dat;     int n;     void propagate(int i) { dat[i] = dat[i * 2 + 1] + dat[i * 2 + 2]; }     void init(int size) {       n = 1;       len.resize(size, 1);       args.resize(size);       while (n < size) n <<= 1;       dat.resize(2 * n - 1);       fill(dat.begin(), dat.end(), Q::zero());       for (int i = 0; i < size; i++) dat[n - 1 + i].x = 1.0;       for (int i = n - 2; i >= 0; i--) propagate(i);     }     void update_k(int k) {       P cur(len[k], 0);       cur = rot(cur, args[k]);       k += n - 1;       dat[k].x = cur.x;       dat[k].y = cur.y;       dat[k].argsum = args[k - n + 1];     }     void update_len(int k, int len) {       this->len[k] += len;       update_k(k);       k += n - 1;       while (k > 0) {         k = (k - 1) / 2;         propagate(k);       }     }     void update_arg(int k, int arg) {       args[k] += arg;       update_k(k);       k += n - 1;       while (k > 0) {         k = (k - 1) / 2;         propagate(k);       }     }     void update_query(int t, int id, int val) {       id--;       if (t == 1) {         update_len(id, val);       } else {         update_arg(id, val);       }     }     Q query(int a, int b) { return query(a, b, 0, 0, n); }     Q query(int a, int b, int k, int l, int r) {       if (r <= a || b <= l) return Q::zero();       if (a <= l && r <= b) return dat[k];       int md = (l + r) / 2;       int nl = k * 2 + 1, nr = nl + 1;       Q lval = query(a, b, nl, l, md);       Q rval = query(a, b, nr, md, r);       return lval + rval;     }   };   P q2p(Q& q) { return P(q.x, q.y); }   void print(seg_tree& seg, int n) {     for (int i = 0; i < (n); i++) {       Q q = seg.dat[seg.n - 1 + i];       P p = q2p(q);       printf( %d : (%lf,%lf) n , i, p.x, p.y);     }   }   int main() {     int n, q;     reader(n, q);     seg_tree seg;     seg.init(n);     for (int i = 0; i < (q); i++) {       int t, a, b;       reader(t, a, b);       seg.update_query(t, a, b);       Q val = seg.query(0, n);       P cur = q2p(val);       printf( %.10lf %.10lf n , (double)cur.x, (double)cur.y);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int p[1000005];   int fnd(int x) { return p[x] = p[x] == x ? x : fnd(p[x]); }   void unn(int x, int y) { p[fnd(y)] = fnd(x); }   int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};   int gcd(int x, int y) { return x % y ? gcd(y, x % y) : y; }   int pq[5144];   vector<int> pr;   int net[100005];   int a[100005];   int main() {     int n;     scanf( %d , &n);     if (n == 1) return printf( 1 n ), 0;     for (int i = 2; i <= 10000; i++) {       if (net[i]) continue;       pr.push_back(i);       for (int j = i * i; j <= 100000; j += i) net[j] = 1;     }     for (int i = 0; i < n; i++) {       scanf( %d , a + i);       int mx = 0;       for (int j = 0; j < pr.size(); j++) {         if (a[i] % pr[j] == 0) {           mx = max(mx, pq[j]);         }       }       for (int j = 0; j < pr.size(); j++) {         if (a[i] % pr[j] == 0) {           pq[j] = max(mx + 1, pq[j]);         }       }     }     int mx = 1;     for (int i = 0; i < pr.size(); i++) mx = max(mx, pq[i]);     printf( %d , mx);   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 305;   const long long mode = 1e9 + 7;   const long long maxm = 1e5 + 10;   long long n, q, t, w[maxn], fa[maxn], nw[maxn], dp[maxm], son[maxn], z[maxn],       cur, ww[maxn];   long long u, v;   signed main() {     cin >> n >> q >> t;     for (long long i = 1; i <= n; ++i) cin >> w[i];     bool juge = true;     while (q--) {       cin >> u >> v;       if (fa[u] == v) {         juge = false;       }       fa[v] = u;       son[u] = v;     }     if (!juge) {       cout << 0;       return 0;     }     for (long long i = 1; i <= n; ++i) {       if (fa[i] == 0 && son[i]) {         long long sum = 0;         long long now = i;         while (now) {           ww[now] = sum + w[now];           nw[now] = nw[fa[now]] + sum + w[now];           sum += w[now];           now = son[now];         }       }       if (fa[i] == 0 && son[i] == 0) nw[i] = ww[i] = w[i];       if (son[i] == 0 && fa[i]) {         z[++cur] = i;       }     }     long long sum = 0;     for (long long i = 1; i <= n; ++i) {       if (son[i] == 0 && fa[i]) {         long long temp = 0, now = i;         while (now) {           temp += w[now];           now = fa[now];         }         sum += nw[i] - temp;       }     }     if (sum > t) {       cout << 0;       return 0;     } else if (sum == t) {       cout << 1;       return 0;     }     dp[sum] = 1;     for (long long i = 1; i <= n; ++i) {       for (long long j = ww[i]; j <= t; ++j) {         if (dp[j - ww[i]]) dp[j] += dp[j - ww[i]];         dp[j] %= mode;       }     }     cout << dp[t];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   T gcd(T a, T b) {     return b == 0 ? a : gcd(b, a % b);   }   template <typename T>   T modMul(T a, T b, T m) {     T x = 0, y = a % m;     while (b > 0) {       if (b % 2 == 1) x = (x + y) % m;       y = (y * 2) % m;       b /= 2;     }     return x % m;   }   template <typename T>   T pollard(T n) {     int i = 0, k = 2, d;     T x = 3, y = 3;     while (true) {       i++;       x = (modMul(x, x, n) + n - 1) % n;       d = gcd(abs(y - x), n);       if (d != 1 && d != n) return d;       if (i == k) y = x, k *= 2;     }   }   template <typename T>   T modExp(T a, T b, T m) {     if (b == 0) return (T)1;     T c = modExp(a, b / 2, m);     c = modMul(c, c, m);     if (b % 2 != 0) c = modMul(c, a, m);     return c;   }   bool miller(long long n) {     const int pn = 9;     const int p[] = {2, 3, 5, 7, 11, 13, 17, 19, 23};     for (int i = 0; i < pn; i++)       if (n % p[i] == 0) return n == p[i];     if (n < p[pn - 1]) return false;     long long s = 0, t = n - 1;     while (~t & 1) t >>= 1, ++s;     for (int i = 0; i < pn; ++i) {       long long pt = modExp((long long)p[i], t, n);       if (pt == 1) continue;       bool ok = false;       for (int j = 0; j < s && !ok; j++) {         if (pt == n - 1) ok = true;         pt = modMul(pt, pt, n);       }       if (!ok) return false;     }     return true;   }   long long arr[200009], mskcnt[1 << 16];   int main() {     long long n, x, y;     scanf( %lld %lld %lld , &n, &x, &y);     vector<long long> div;     int cnt_y[20], cnt_x[20];     long long d = y;     for (int i = 2; 1LL * i * i <= d && i <= 10000000; i++) {       if (d % i == 0) {         int cnt = 0;         while (d % i == 0) {           d /= i;           cnt++;         }         cnt_y[div.size()] = cnt;         div.push_back(i);       }     }     if (d > 1) {       if (miller(d)) {         cnt_y[div.size()] = 1;         div.push_back(d);       } else {         long long p = pollard(d);         if (p * p == d) {           cnt_y[div.size()] = 2;           div.push_back(p);         } else {           cnt_y[div.size()] = 1;           div.push_back(p);           cnt_y[div.size()] = 1;           div.push_back(d / p);         }       }     }     if (y % x != 0) {       puts( 0 );       return 0;     }     d = x;     for (int idx = 0; idx < div.size(); idx++) {       long long i = div[idx];       int cnt = 0;       while (d % i == 0) {         d /= i;         cnt++;       }       if (cnt == cnt_y[idx]) {         swap(div.back(), div[idx]);         swap(cnt_y[div.size() - 1], cnt_y[idx]);         idx--;         div.pop_back();       } else         cnt_x[idx] = cnt;     }     for (int i = 0; i < n; i++) scanf( %lld , &arr[i]);     for (int i = 0; i < n; i++) {       long long val = arr[i];       if (val % x == 0) {         int mask = 0;         d = val;         for (int idx = 0; idx < div.size(); idx++) {           long long i = div[idx];           int cnt = 0;           while (d % i == 0) {             d /= i;             cnt++;           }           if (cnt > cnt_x[idx]) {             mask |= (1 << idx);           }         }         mskcnt[mask]++;       }     }     for (int i = 0; i < div.size(); i++) {       for (int mask = 0; mask < (1 << div.size()); mask++) {         if (mask & (1 << i)) {           mskcnt[mask] += mskcnt[mask ^ (1 << i)];         }       }     }     long long ans = 0;     for (int i = 0; i < n; i++) {       long long val = arr[i];       if (y % val == 0) {         int mask = 0;         d = val;         for (int idx = 0; idx < div.size(); idx++) {           long long i = div[idx];           int cnt = 0;           while (d % i == 0) {             d /= i;             cnt++;           }           if (cnt < cnt_y[idx]) {             mask |= (1 << idx);           }         }         mask = (~mask) & ((1 << div.size()) - 1);         ans += mskcnt[mask];       }     }     printf( %lld n , ans);   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 100000;   int N, M, S;   vector<pair<int, int> > b;   vector<pair<pair<int, int>, int> > s;   vector<pair<int, int> > b2;   vector<pair<pair<int, int>, int> > s2;   int ans[MAX_N + 1];   priority_queue<pair<int, int>, vector<pair<int, int> >,                  greater<pair<int, int> > >       pq;   bool chk(int x) {     long long cost = 0;     while (!b.empty()) b.pop_back();     while (!s.empty()) s.pop_back();     for (int i = 0; i < b2.size(); i++) {       b.push_back(b2[i]);     }     for (int i = 0; i < s2.size(); i++) {       s.push_back(s2[i]);     }     while (!pq.empty()) pq.pop();     while (!b.empty()) {       while (!s.empty() && s.back().first.first >= b.back().first) {         pq.push({s.back().first.second, s.back().second});         s.pop_back();       }       if (pq.empty()) {         return false;       }       pair<int, int> now = pq.top();       pq.pop();       cost += (long long)now.first;       for (int i = 0; i < x; i++) {         if (b.empty()) break;         pair<int, int> p = b.back();         b.pop_back();         ans[p.second] = now.second;       }     }     return cost <= (long long)S;   }   int main() {     scanf( %d%d%d , &N, &M, &S);     for (int i = 0; i < M; i++) {       int x;       scanf( %d , &x);       b2.push_back({x, i});     }     for (int i = 0; i < N; i++) {       int x;       scanf( %d , &x);       s2.push_back({{x, 0}, i});     }     for (int i = 0; i < N; i++) {       int x;       scanf( %d , &x);       s2[i].first.second = x;     }     sort(b2.begin(), b2.end());     sort(s2.begin(), s2.end());     int st = 0, ed = 100000, m;     while (st < ed) {       m = (st + ed) / 2;       if (chk(m))         ed = m;       else         st = m + 1;     }     if (chk(st)) {       printf( YES n );       for (int i = 0; i < M; i++) {         printf( %d  , ans[i] + 1);       }     } else {       printf( NO );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool yes;   int t, n, m, len, l, sender[105];   map<string, int> msi;   map<int, string> mis;   string str, user[105], text[105];   set<int> mention[105];   int ans[105];   int dp[105][105];   char c;   bool solve(int pos, int idx) {     if (pos == m) return 1;     if (dp[pos][idx] != -1) return dp[pos][idx];     if (mention[pos].find(idx) != mention[pos].end()) return 0;     dp[pos][idx] = 0;     if (pos + 1 < m && sender[pos + 1] != -1) {       if (idx == sender[pos + 1]) return 0;       dp[pos][idx] = solve(pos + 1, sender[pos + 1]);     } else {       for (int i = 0; i < n; ++i) {         if (i != idx) {           dp[pos][idx] |= solve(pos + 1, i);           if (dp[pos][idx]) break;         } else if (pos == m - 1)           dp[pos][idx] = true;       }     }     if (dp[pos][idx]) ans[pos] = idx;     return dp[pos][idx];   }   int main() {     scanf( %d%c , &t, &c);     while (t--) {       scanf( %d%c , &n, &c);       memset(sender, -1, sizeof sender);       memset(dp, -1, sizeof dp);       for (int i = 0; i < n; ++i) {         cin >> user[i];         msi[user[i]] = i;         mis[i] = user[i];       }       scanf( %d%c , &m, &c);       for (int i = 0; i < m; ++i) {         getline(cin, str,  : );         if (str.compare( ? ) == 0)           sender[i] = -1;         else           sender[i] = msi[str];         getline(cin, text[i]);       }       for (int i = 0; i < m; ++i) {         len = text[i].size();         for (int j = 0; j < n; ++j) {           l = user[j].size();           yes = false;           for (int k = 0; k < len; ++k) {             if (text[i][k] == user[j][0] && k + l - 1 < len &&                 text[i].compare(k, l, user[j]) == 0) {               if (((k - 1 >= 0 && text[i][k - 1] !=  .  &&                     text[i][k - 1] !=  ,  && text[i][k - 1] !=  !  &&                     text[i][k - 1] !=  ?  && text[i][k - 1] !=    ) ||                    (k + l < len && text[i][k + l] !=  .  &&                     text[i][k + l] !=  ,  && text[i][k + l] !=  !  &&                     text[i][k + l] !=  ?  && text[i][k + l] !=    ))) {               } else {                 yes = true;                 break;               }             }           }           if (yes) mention[i].insert(msi[user[j]]);         }       }       l = 0;       if (sender[0] != -1)         l |= solve(0, sender[0]);       else {         for (int i = 0; i < n; ++i) {           l |= solve(0, i);           if (l) break;         }       }       if (!l)         cout <<  Impossible  << endl;       else {         for (int i = 0; i < m; ++i) {           cout << user[ans[i]] <<  :  << text[i] << endl;         }       }       msi.clear(), mis.clear();       for (int i = 0; i < m; ++i) mention[i].clear();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       vector<int> a(n), b(n);       for (auto &it : a) cin >> it;       for (auto &it : b) cin >> it;       int mna = *min_element(a.begin(), a.end());       int mnb = *min_element(b.begin(), b.end());       long long ans = 0;       for (int i = 0; i < n; ++i) {         ans += max(a[i] - mna, b[i] - mnb);       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1050;   int s[maxn][maxn];   int main(int argc, char const *argv[]) {     int n, a, b;     cin >> n >> a >> b;     if (a != 1 && b != 1) {       printf( NO n );       return 0;     }     if (n == 1) {       printf( YES n0 n );       return 0;     }     if (a == 1 && b == 1 && n <= 3) {       printf( NO n );       return 0;     }     printf( YES n );     if (a == 1 && b == 1) {       for (int i = 1; i < n; ++i) {         s[i][i + 1] = s[i + 1][i] = 1;       }       for (int i = 1; i <= n; ++i) {         for (int j = 1; j <= n; ++j) {           printf( %d , s[i][j]);         }         printf(  n );       }     } else if (a == 1) {       for (int i = b; i <= n; ++i) {         for (int j = b; j <= n; ++j) {           s[i][j] = 1;         }       }       for (int i = 1; i <= n; ++i) {         for (int j = 1; j <= n; ++j) {           s[i][j] = 1 - s[i][j];         }         s[i][i] = 0;       }       for (int i = 1; i <= n; ++i) {         for (int j = 1; j <= n; ++j) {           printf( %d , s[i][j]);         }         printf(  n );       }     } else {       for (int i = a; i <= n; ++i) {         for (int j = a; j <= n; ++j) {           s[i][j] = 1;         }       }       for (int i = 1; i <= n; ++i) s[i][i] = 0;       for (int i = 1; i <= n; ++i) {         for (int j = 1; j <= n; ++j) {           printf( %d , s[i][j]);         }         printf(  n );       }     }     return 0;   }


#include <bits/stdc++.h>   template <typename T>   T power(T x, T y, T mod) {     T res = 1;     while (y > 0) {       if (y & 1) res = (res * x) % mod;       y /= 2;       x = (x * x) % mod;     }     return res;   }   using namespace std;   long long int a[2005][2005];   long long int d1[4005];   long long int d2[4005];   long long int ele[2005][2005];   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int n;     cin >> n;     for (long long int i = 0; i < n; i++)       for (long long int j = 0; j < n; j++) cin >> a[i][j];     for (long long int i = 0; i < n; i++) {       for (long long int j = 0; j < n; j++) {         long long int b = a[i][j];         d1[j - i + n] += b;         d2[n + n - i - j - 1] += b;       }     }     long long int max1 = 0;     long long int max2 = 0;     long long int x1, y1, x2, y2;     x1 = 1;     x2 = 1;     y1 = 1;     y2 = 2;     for (long long int i = 0; i < n; i++) {       for (long long int j = 0; j < n; j++) {         ele[i][j] = d1[j - i + n] + d2[2 * n - i - j - 1] - a[i][j];         if ((i + j) % 2) {           if (max1 < ele[i][j]) {             max1 = ele[i][j];             x1 = i + 1;             y1 = j + 1;           }         } else {           if (max2 < ele[i][j]) {             max2 = ele[i][j];             x2 = i + 1;             y2 = j + 1;           }         }       }     }     cout << max1 + max2 <<   n ;     cout << x1 <<     << y1 <<     << x2 <<     << y2 <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) {       int tmp;       cin >> tmp;       if (tmp % 2)         cout << tmp <<      ;       else         cout << tmp - 1 <<      ;     }     cin.get();     cin.get();   }


#include <bits/stdc++.h>   using namespace std;   int n, a[1005], b[1005];   int main() {     bool same = 1;     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       scanf( %d%d , a + i, b + i);       same &= a[i] == b[i];     }     if (!same) {       puts( rated );       return 0;     }     for (int i = 2; i <= n; ++i) {       if (a[i] > a[i - 1]) {         puts( unrated );         return 0;       }     }     puts( maybe );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long fpow(long long x, long long y, long long m) {     long long temp;     if (y == 0 || x == 1) return 1;     temp = fpow(x, y / 2, m);     if (y % 2 == 0)       return ((temp) * (temp)) % m;     else       return (((x) % m) * ((temp * temp) % m)) % m;   }   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   void sieve(long long n) {     bool prime[5 * 100005];     memset(prime, true, sizeof(prime));     for (long long p = 2; p * p <= n; p++)       if (prime[p] == true)         for (long long i = p * p; i <= n; i += p) prime[i] = false;     prime[1] = 0;   }   long long cnt, sum, mid, mx = -1e17, mn = 1e17, a[2 * 100005];   long long n, m, d, t, i, j, k, l, r, x, y, z;   bool f, f1, f2;   string s;   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     vector<long long> dp(100005);     for ((i) = 1; (i) <= (n); (i)++) {       cin >> x >> y;       cnt = 0;       for (j = 1; j * j <= x; j++) {         if (x % j == 0) {           if (dp[j] < i - y) {             cnt++;           }           dp[j] = i;           if (dp[x / j] < i - y) {             cnt++;           }           dp[x / j] = i;         }       }       cout << cnt;       cout <<   n ;     }   }


#include <bits/stdc++.h>   int main() {     char c, str[100];     int i;     scanf( %c , &c);     getchar();     gets(str);     if (c ==  L ) {       for (i = 0; i < strlen(str); i++) {         if (str[i] ==  q ) {           str[i] =  w ;         } else if (str[i] ==  w ) {           str[i] =  e ;         } else if (str[i] ==  r ) {           str[i] =  t ;         } else if (str[i] ==  t ) {           str[i] =  y ;         } else if (str[i] ==  y ) {           str[i] =  u ;         } else if (str[i] ==  u ) {           str[i] =  i ;         } else if (str[i] ==  i ) {           str[i] =  o ;         } else if (str[i] ==  o ) {           str[i] =  p ;         } else if (str[i] ==  a ) {           str[i] =  s ;         } else if (str[i] ==  s ) {           str[i] =  d ;         } else if (str[i] ==  d ) {           str[i] =  f ;         } else if (str[i] ==  f ) {           str[i] =  g ;         } else if (str[i] ==  g ) {           str[i] =  h ;         } else if (str[i] ==  h ) {           str[i] =  j ;         } else if (str[i] ==  j ) {           str[i] =  k ;         } else if (str[i] ==  k ) {           str[i] =  l ;         } else if (str[i] ==  l ) {           str[i] =  ; ;         } else if (str[i] ==  z ) {           str[i] =  x ;         } else if (str[i] ==  x ) {           str[i] =  c ;         } else if (str[i] ==  c ) {           str[i] =  v ;         } else if (str[i] ==  v ) {           str[i] =  b ;         } else if (str[i] ==  b ) {           str[i] =  n ;         } else if (str[i] ==  n ) {           str[i] =  m ;         } else if (str[i] ==  m ) {           str[i] =  , ;         } else if (str[i] ==  , ) {           str[i] =  . ;         } else if (str[i] ==  . ) {           str[i] =  / ;         } else if (str[i] ==  e ) {           str[i] =  r ;         }       }     } else if (c ==  R ) {       for (i = 0; i < strlen(str); i++) {         if (str[i] ==  w ) {           str[i] =  q ;         } else if (str[i] ==  e ) {           str[i] =  w ;         } else if (str[i] ==  r ) {           str[i] =  e ;         } else if (str[i] ==  t ) {           str[i] =  r ;         } else if (str[i] ==  y ) {           str[i] =  t ;         } else if (str[i] ==  u ) {           str[i] =  y ;         } else if (str[i] ==  i ) {           str[i] =  u ;         } else if (str[i] ==  o ) {           str[i] =  i ;         } else if (str[i] ==  p ) {           str[i] =  o ;         } else if (str[i] ==  s ) {           str[i] =  a ;         } else if (str[i] ==  d ) {           str[i] =  s ;         } else if (str[i] ==  f ) {           str[i] =  d ;         } else if (str[i] ==  g ) {           str[i] =  f ;         } else if (str[i] ==  h ) {           str[i] =  g ;         } else if (str[i] ==  j ) {           str[i] =  h ;         } else if (str[i] ==  k ) {           str[i] =  j ;         } else if (str[i] ==  l ) {           str[i] =  k ;         } else if (str[i] ==  ; ) {           str[i] =  l ;         } else if (str[i] ==  x ) {           str[i] =  z ;         } else if (str[i] ==  c ) {           str[i] =  x ;         } else if (str[i] ==  v ) {           str[i] =  c ;         } else if (str[i] ==  b ) {           str[i] =  v ;         } else if (str[i] ==  n ) {           str[i] =  b ;         } else if (str[i] ==  m ) {           str[i] =  n ;         } else if (str[i] ==  , ) {           str[i] =  m ;         } else if (str[i] ==  . ) {           str[i] =  , ;         } else if (str[i] ==  / ) {           str[i] =  . ;         }       }     }     printf( %s , str);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 2e5 + 5;   inline long long read() {     long long x = 0, f = 1;     char ch = getchar();     while (ch >  9  || ch <  0 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   long long sum, n, q;   long long a[16];   long long P[16];   long long fac[16];   bool vis[16];   void get(long long sum) {     long long res = 0;     for (long long i = 1; i < n; i++) res = (res + a[i]) * (n - i);     for (long long i = 1; i <= 15; i++) {       a[i] = sum / fac[15 - i];       sum %= fac[15 - i];     }     memset(vis, 0, sizeof(vis));     for (long long i = 1; i <= 15; i++) {       long long cnt = 0;       for (long long j = 1; j <= 15; j++) {         if (!vis[j]) cnt++;         if (cnt == a[i] + 1) {           P[i] = j;           vis[j] = 1;           break;         }       }     }   }   signed main() {     n = read();     q = read();     fac[0] = 1;     for (long long i = 1; i <= 15; i++) fac[i] = fac[i - 1] * i;     while (q--) {       long long opt;       opt = read();       if (opt == 1) {         long long l, r;         l = read();         r = read();         long long ans = 0;         get(sum);         if (r > n - 15) {           if (l > n - 15) {             for (long long i = l - n + 15; i <= r - n + 15; i++) ans += P[i];             ans += (r - l + 1) * (n - 15);           } else {             ans += (l + n - 15) * (n - 14 - l) / 2;             for (long long i = 1; i <= r - n + 15; i++) ans += P[i];             ans += (r - n + 15) * (n - 15);           }         } else           ans = (l + r) * (r - l + 1) / 2;         printf( %lld n , ans);       }       if (opt == 2) {         long long x;         x = read();         sum += x;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long modpow(long long a, long long b, long long m = 1000000007) {     a %= m;     long long res = 1;     while (b) {       if (b & 1) res = res * a % m;       a = a * a % m;       b >>= 1;     }     return res;   }   long long bpow(long long a, long long b) {     long long res = 1;     while (b) {       if (b & 1) res = res * a;       a = a * a;       b >>= 1;     }     return res;   }   long long modinv(long long a) { return modpow(a, 1000000007 - 2, 1000000007); }   void graph(vector<vector<long long> > &adj, long long m) {     long long x, y;     for (long long i = 0; i < m; i++) {       cin >> x >> y;       adj[x - 1].push_back(y - 1);       adj[y - 1].push_back(x - 1);     }   }   const int mxN = 2e5 + 5;   void solve() {     int n;     cin >> n;     string s;     cin >> s;     set<int> color[26];     std::vector<int> out(n);     for (long long i = 0; i < s.size(); i++) {       int x = s[i] -  a ;       for (long long j = 1; j <= 26; j++) {         bool ok = true;         for (long long k = x + 1; k < 26; k++) {           ok &= !(color[k].count(j));         }         if (ok) {           color[x].insert(j);           out[i] = j;           break;         }       }     }     set<int> distinct;     for (auto &k : out) distinct.insert(k);     cout << distinct.size() <<   n ;     for (auto &k : out) cout << k <<    ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t = 1;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   template <class T1, class T2>   bool chmin(T1 &a, T2 b) {     return b < a && (a = b, true);   }   template <class T1, class T2>   bool chmax(T1 &a, T2 b) {     return a < b && (a = b, true);   }   template <class T1, class T2>   void sortPair(vector<T1> &v1, vector<T2> &v2) {     vector<pair<T1, T2>> u(v1.size());     for (int i = 0; i < u.size(); i++) u[i] = make_pair(v1[i], v2[i]);     sort(u.begin(), u.end());     for (int i = 0; i < u.size(); i++) tie(v1[i], v2[i]) = u[i];   }   long long solve() {     long long d, n, m;     cin >> d >> n >> m;     vector<long long> x(m), p(m);     for (int i = 0; i < (m); i++) scanf( %lld %lld , &x[i], &p[i]);     x.insert(x.begin(), 0);     p.insert(p.begin(), 0);     x.push_back(d);     p.push_back(0);     m += 2;     sortPair(x, p);     vector<long long> next(m, -1);     stack<int> s;     for (int i = (m)-1; i >= 0; i--) {       while (!s.empty() && p[i] <= p[s.top()]) s.pop();       next[i] = s.empty() ? -1 : s.top();       s.push(i);     }     long long full = n;     long long ans = 0;     int i = 0;     while (i < m - 1) {       if (x[i + 1] - x[i] > full) return -1;       long long use, dist, ni;       if (x[next[i]] - x[i] > full) {         dist = x[i + 1] - x[i];         use = full - n;         ni = i + 1;       } else {         dist = x[next[i]] - x[i];         use = max(0ll, dist - n);         ni = next[i];       }       ans += use * p[i];       n += use - dist;       i = ni;     }     return ans;   }   int main() {     cout << solve() << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline void read(T &x) {     char c;     bool nega = 0;     while ((!isdigit(c = getchar())) && (c !=  - ))       ;     if (c ==  - ) {       nega = 1;       c = getchar();     }     x = c - 48;     while (isdigit(c = getchar())) x = x * 10 + c - 48;     if (nega) x = -x;   }   template <typename T>   inline void writep(T x) {     if (x > 9) writep(x / 10);     putchar(x % 10 + 48);   }   template <typename T>   inline void write(T x) {     if (x < 0) {       putchar( - );       x = -x;     }     writep(x);   }   template <typename T>   inline void writeln(T x) {     write(x);     putchar(  n );   }   int n, m, q;   char a[2 * 1003][2 * 1003];   long long sum[2 * 1003][2 * 1003], sum1[2 * 1003][2 * 1003], ret, cn_n, cm_n;   long long mx, my;   long long sx1, sy1, sx2, sy2;   void dcy(long long left, long long right, long long lx, long long rx) {     if (left > sy2 || right < sy1) return;     if (left >= sy1 && right <= sy2) {       ret += (right - left + 1) * (rx - lx + 1) / 2;     } else if (right - left + 1 == m) {       long long ly = max(sy1, left), ry = min(sy2, right);       long long x = (lx - 1) / n, y = (ly - 1) / m;       int k = ((x) ^ (y));       int k1 = 0;       while (k) k1 += k & 1, k >>= 1;       lx = (lx - 1) % n + 1, ly = (ly - 1) % m + 1, rx = (rx - 1) % n + 1,       ry = (ry - 1) % m + 1;       if (k1 & 1)         ret += sum1[rx][ry] + sum1[lx - 1][ly - 1] - sum1[lx - 1][ry] -                sum1[rx][ly - 1];       else         ret +=             sum[rx][ry] + sum[lx - 1][ly - 1] - sum[lx - 1][ry] - sum[rx][ly - 1];     } else {       long long mid = (left + right) >> 1;       dcy(left, mid, lx, rx), dcy(mid + 1, right, lx, rx);     }   }   void dcx(long long left, long long right) {     if (left > sx2 || right < sx1) return;     if (left >= sx1 && right <= sx2)       ret += (sy2 - sy1 + 1) * (right - left + 1) / 2;     else if (right - left + 1 == n)       dcy(1, my, max(left, sx1), min(right, sx2));     else {       int mid = (left + right) >> 1;       dcx(left, mid), dcx(mid + 1, right);     }   }   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int i, j;     scanf( %d%d%d , &n, &m, &q);     for (i = 1; i <= n; i++) scanf( %s , a[i] + 1);     for (i = 1; i <= n; i++)       for (j = 1; j <= m; j++) {         sum[i][j] = sum[i + n][j + m] = a[i][j] -  0 ;         sum[i + n][j] = sum[i][j + m] = sum[i][j] ^ 1;       }     n <<= 1, m <<= 1;     for (i = 1; i <= n; i++) {       for (j = 1; j <= m; j++) {         sum[i][j] += sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];         sum1[i][j] = i * j - sum[i][j];       }     }     mx = n;     while (mx < 1e9) {       mx *= 2;     }     my = m;     while (my < 1e9) {       my *= 2;     }     while (q--) {       scanf( %d%d%d%d , &sx1, &sy1, &sx2, &sy2);       ret = 0;       dcx(1, mx);       cout << ret << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int mark[111111];   int a[111111];   int b[111111];   int n;   int noEle, cnt;   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];       b[i] = a[i];     }     sort(b, b + n + 1);     for (int i = 1; i <= n; i++) {       if (b[i] != a[i]) {         noEle++;         if (noEle % 2 != 0) cnt++;         if (cnt > 1) {           cout <<  NO ;           return 0;         }       }     }     cout <<  YES ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, t = 2;     cin >> n;     string s;     cin >> s;     for (int i = 0; i < n;) {       cout << s.at(i);       i += t;       t++;     }     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1000000000;   struct Point {     int x, y;     Point(int x_ = 0, int y_ = 0) : x(x_), y(y_) {}     Point(std::istream &in) {       in >> x >> y;       x *= 2;       y *= 2;     }   };   bool operator<(const Point &a, const Point &b) {     return (a.x < b.x) || (a.x == b.x && a.y < b.y);   }   bool cmp_sum(const Point &a, const Point &b) { return (a.x + a.y < b.x + b.y); }   bool cmp_diff(const Point &a, const Point &b) {     return (a.x - a.y < b.x - b.y);   }   int n, k;   vector<Point> gnome, metro;   vector<int> sorted_x, sorted_y;   vector<int> tree;   int curk;   void upd_val(int x, int val, const vector<int> &coords) {     x = lower_bound(coords.begin(), coords.end(), x) - coords.begin();     x += curk;     tree[x] = min(tree[x], val);     x /= 2;     while (x != 1) {       tree[x] = min(tree[x * 2], tree[x * 2 + 1]);       x /= 2;     }   }   int get_min_val(int l, int r, const vector<int> &coords) {     l = lower_bound(coords.begin(), coords.end(), l) - coords.begin();     r = lower_bound(coords.begin(), coords.end(), r) - coords.begin();     l += curk;     r += curk;     int res = INF;     while (l < r) {       res = min(res, min(tree[l], tree[r]));       l = (l + 1) / 2;       r = (r - 1) / 2;     }     if (l == r) {       res = min(res, tree[l]);     }     return res;   }   void shrink(vector<int> &coord) {     sort(coord.begin(), coord.end());     coord.resize(unique(coord.begin(), coord.end()) - coord.begin());   }   template <typename T>   std::vector<int> argsort(const std::vector<T> &vals, bool reverse = false) {     vector<int> result(vals.size());     for (int i = 0; i < int(vals.size()); ++i) {       result[i] = i;     }     sort(result.begin(), result.end(),          [&](int a, int b) -> bool { return (vals[a] < vals[b]); });     if (reverse) {       std::reverse(result.begin(), result.end());     }     return result;   }   void get_min_dist(const std::vector<Point> &gnome, std::vector<Point> metro,                     std::vector<int> &result) {     sorted_x.clear();     sorted_y.clear();     for (auto &x : gnome) {       sorted_x.push_back(x.x);       sorted_y.push_back(x.y);     }     for (auto &x : metro) {       sorted_x.push_back(x.x);       sorted_y.push_back(x.y);     }     shrink(sorted_x);     shrink(sorted_y);     int nn = gnome.size();     result.assign(nn, INF);     auto indices = argsort(gnome);     sort(metro.begin(), metro.end());     curk = 2;     while (int(sorted_y.size()) > curk) curk *= 2;     {       tree.assign(2 * curk, INF);       int l = 0, r = 0;       while (l < nn) {         int ind = indices[l];         while (r < k && metro[r].x <= gnome[ind].x) {           upd_val(metro[r].y, -metro[r].x - metro[r].y, sorted_y);           ++r;         }         result[ind] = min(result[ind],                           get_min_val(sorted_y.front(), gnome[ind].y, sorted_y) +                               gnome[ind].x + gnome[ind].y);         ++l;       }     }     {       tree.assign(2 * curk, INF);       int l = 0, r = 0;       while (l < nn) {         int ind = indices[l];         while (r < k && metro[r].x <= gnome[ind].x) {           upd_val(metro[r].y, -metro[r].x + metro[r].y, sorted_y);           ++r;         }         result[ind] = min(result[ind],                           get_min_val(gnome[ind].y, sorted_y.back(), sorted_y) +                               gnome[ind].x - gnome[ind].y);         ++l;       }     }     reverse(metro.begin(), metro.end());     reverse(indices.begin(), indices.end());     {       tree.assign(2 * curk, INF);       int l = 0, r = 0;       while (l < nn) {         int ind = indices[l];         while (r < k && metro[r].x >= gnome[ind].x) {           upd_val(metro[r].y, metro[r].x - metro[r].y, sorted_y);           ++r;         }         result[ind] = min(result[ind],                           get_min_val(sorted_y.front(), gnome[ind].y, sorted_y) -                               gnome[ind].x + gnome[ind].y);         ++l;       }     }     {       tree.assign(2 * curk, INF);       int l = 0, r = 0;       while (l < nn) {         int ind = indices[l];         while (r < k && metro[r].x >= gnome[ind].x) {           upd_val(metro[r].y, metro[r].x + metro[r].y, sorted_y);           ++r;         }         result[ind] = min(result[ind],                           get_min_val(gnome[ind].y, sorted_y.back(), sorted_y) -                               gnome[ind].x - gnome[ind].y);         ++l;       }     }   }   int find_dist(pair<pair<int, int>, pair<int, int> > rect, const Point &pnt) {     int sum = pnt.x + pnt.y;     int diff = pnt.x - pnt.y;     int sum_dist =         max(0, -min(rect.first.second, sum) + max(rect.first.first, sum));     int diff_dist =         max(0, -min(rect.second.second, diff) + max(rect.second.first, diff));     return max(sum_dist, diff_dist);   }   void get_all_min_dist(vector<pair<pair<int, int>, pair<int, int> > > rect,                         vector<Point> metro, vector<int> &dist) {     dist.assign(n, INF);     vector<Point> ok_points;     for (int i = 0; i < n; ++i) {       const auto &cr = rect[i];       int a, b, c, d;       tie(a, b) = cr.first;       tie(c, d) = cr.second;       ok_points.emplace_back((a + c) / 2, (a - c) / 2);       ok_points.emplace_back((a + d) / 2, (a - d) / 2);       ok_points.emplace_back((b + c) / 2, (b - c) / 2);       ok_points.emplace_back((b + d) / 2, (b - d) / 2);     }     vector<int> tmp;     get_min_dist(ok_points, metro, tmp);     for (int i = 0; i < n * 4; ++i) {       dist[i / 4] = min(dist[i / 4], tmp[i]);     }     vector<int> sorted_sum;     vector<int> sorted_diff;     for (auto &pnt : rect) {       sorted_sum.push_back(pnt.first.first);       sorted_sum.push_back(pnt.first.second);       sorted_diff.push_back(pnt.second.first);       sorted_diff.push_back(pnt.second.second);     }     for (auto &pnt : metro) {       sorted_sum.push_back(pnt.x + pnt.y);       sorted_diff.push_back(pnt.x - pnt.y);     }     shrink(sorted_sum);     shrink(sorted_diff);     sort(metro.begin(), metro.end(), cmp_sum);     curk = 2;     while (curk < int(sorted_diff.size())) curk *= 2;     vector<pair<pair<int, int>, pair<int, int> > > segment(n);     for (int i = 0; i < n; ++i) {       segment[i] = make_pair(make_pair(rect[i].first.first, i), rect[i].second);     }     sort(segment.begin(), segment.end());     {       tree.assign(curk * 2, INF);       int nn = segment.size();       int l = 0, r = 0;       while (l < nn) {         while (r < k && metro[r].x + metro[r].y <= segment[l].first.first) {           upd_val(metro[r].x - metro[r].y, -metro[r].y - metro[r].x, sorted_diff);           ++r;         }         int ind = segment[l].first.second;         dist[ind] =             min(dist[ind], get_min_val(segment[l].second.first,                                        segment[l].second.second, sorted_diff) +                                segment[l].first.first);         ++l;       }     }     reverse(metro.begin(), metro.end());     for (int i = 0; i < n; ++i) {       segment[i] = make_pair(make_pair(rect[i].first.second, i), rect[i].second);     }     sort(segment.rbegin(), segment.rend());     {       tree.assign(curk * 2, INF);       int nn = segment.size();       int l = 0, r = 0;       while (l < nn) {         while (r < k && metro[r].x + metro[r].y >= segment[l].first.first) {           upd_val(metro[r].x - metro[r].y, metro[r].x + metro[r].y, sorted_diff);           ++r;         }         int ind = segment[l].first.second;         dist[ind] =             min(dist[ind], get_min_val(segment[l].second.first,                                        segment[l].second.second, sorted_diff) -                                segment[l].first.first);         ++l;       }     }     sort(metro.begin(), metro.end(), cmp_diff);     for (int i = 0; i < n; ++i) {       segment[i] = make_pair(make_pair(rect[i].second.first, i), rect[i].first);     }     sort(segment.begin(), segment.end());     curk = 2;     while (curk < int(sorted_sum.size())) curk *= 2;     {       tree.assign(curk * 2, INF);       int nn = segment.size();       int l = 0, r = 0;       while (l < nn) {         while (r < k && metro[r].x - metro[r].y <= segment[l].first.first) {           upd_val(metro[r].x + metro[r].y, metro[r].y - metro[r].x, sorted_sum);           ++r;         }         int ind = segment[l].first.second;         dist[ind] =             min(dist[ind], get_min_val(segment[l].second.first,                                        segment[l].second.second, sorted_sum) +                                segment[l].first.first);         ++l;       }     }     reverse(metro.begin(), metro.end());     for (int i = 0; i < n; ++i) {       segment[i] = make_pair(make_pair(rect[i].second.second, i), rect[i].first);     }     sort(segment.rbegin(), segment.rend());     {       tree.assign(curk * 2, INF);       int nn = segment.size();       int l = 0, r = 0;       while (l < nn) {         while (r < k && metro[r].x - metro[r].y >= segment[l].first.first) {           upd_val(metro[r].x + metro[r].y, metro[r].x - metro[r].y, sorted_sum);           ++r;         }         int ind = segment[l].first.second;         dist[ind] =             min(dist[ind], get_min_val(segment[l].second.first,                                        segment[l].second.second, sorted_sum) -                                segment[l].first.first);         ++l;       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> k;     for (int i = 0; i < n; ++i) {       gnome.emplace_back(std::cin);     }     for (int i = 0; i < k; ++i) {       metro.emplace_back(std::cin);     }     int min_x = INF, max_x = -INF;     int min_y = INF, max_y = -INF;     for (const auto &pnt : gnome) {       min_x = min(min_x, pnt.x + pnt.y);       max_x = max(max_x, pnt.x + pnt.y);       min_y = min(min_y, pnt.x - pnt.y);       max_y = max(max_y, pnt.x - pnt.y);     }     int result = (max(max_x - min_x, max_y - min_y) + 1) / 2;     if (n > 1 && k) {       vector<int> dist;       get_min_dist(gnome, metro, dist);       auto indices = argsort(dist, true);       vector<pair<pair<int, int>, pair<int, int> > > all_rect;       vector<int> add_dist;       min_x = INF, max_x = -INF;       min_y = INF, max_y = -INF;       for (int i = 0; i < n; ++i) {         const auto &pnt = gnome[indices[i]];         min_x = min(min_x, pnt.x + pnt.y);         max_x = max(max_x, pnt.x + pnt.y);         min_y = min(min_y, pnt.x - pnt.y);         max_y = max(max_y, pnt.x - pnt.y);         int dist = (max(max_x - min_x, max_y - min_y) + 1) / 2;         add_dist.push_back(dist);         all_rect.emplace_back(make_pair(max_x - dist, min_x + dist),                               make_pair(max_y - dist, min_y + dist));       }       result = min(result, dist[indices[0]]);       vector<int> new_dist;       get_all_min_dist(all_rect, metro, new_dist);       for (int i = 0; i < n - 1; ++i) {         int t1 = add_dist[i];         int t2 = new_dist[i];         int t3 = dist[indices[i + 1]];         int x = (max(0, min(2 * t2, t3 + t2 - t1)) + 1) / 2;         result = min(result, max(t1 + x, t3 + t2 - x));       }     }     result = (result + 1) / 2;     cout << result <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int i, j, w, h;     char a[101][101];     cin >> w >> h;     for (i = 0; i < h; i++) {       for (j = 0; j < w; j++) {         char x;         cin >> x;         a[j][i] = x;       }     }     for (j = 0; j < w; j++) {       for (int k = 0; k < 2; k++) {         for (i = 0; i < h; i++) {           cout << a[j][i];           cout << a[j][i];         }         cout << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   #define ll long long   #define f(i, x, n) for (int i = x; i < (int)(n); ++i)   const int N = 1e6 + 10, mod = 998244353;   const double eps = 1e-7;      inline int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }   inline int mul(int a, int b) { return (ll)a * b % mod; }      int n;   string s;      ll fact[1000001], inv[1000001];// mod inv = n ^ mod-2   ll FastPower(int a, int b)   {       if(b == 0)           return 1;       ll res = FastPower(a, b/2);       if(b%2)           return res * res % mod * a % mod;       return res * res % mod;   }   ll nCk(int n, int k)   {    if(n < k)     return 0;       return fact[n] * inv[k] % mod * inv[n-k] % mod;   }      int main()   {    ios_base::sync_with_stdio(0);    cin.tie(0);   #ifndef ONLINE_JUDGE    freopen( input.txt ,  r , stdin);   #endif       fact[0] = 1;       inv[0] = FastPower(1, mod-2);       for(int i=1; i<=1000000; ++i)       {           fact[i] = fact[i-1] * i % mod;           inv[i] = FastPower(fact[i], mod-2);       }    int t;    cin >> t;    while(t--)       {     cin >> n >> s;     vector<int> a;     for(int i=0; i<n; ++i){      if(i < n-1 && s[i] ==  1  && s[i+1] ==  1 ){       a.push_back(2);       ++i;       continue;      }      a.push_back(s[i]- 0 );     }     n = a.size();     ll ans = 1;     int space = 0, nof2 = 0;     for(int i=0; i<n; ++i){      //cout << a[i] <<    ;      nof2 += a[i] == 2;      space += a[i] == 0;     }     ans = nCk(nof2+space, space);     cout << ans <<   n ;       }          }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   const double eps = 0.00000001;   void pr() { cout << endl; }   template <typename T1, typename... T2>   void pr(const T1& arg, const T2&... args) {     cout << arg <<    ;     pr(args...);   }   const int N = 5e5 + 10;   vector<int> v[N], mp[N], l_u[N], l_d[N], r_u[N], r_d[N];   int dui[N], n, m;   void init() {     for (int i = 1; i <= n; ++i)       v[i].clear(), mp[i].clear(), l_u[i].clear(), l_d[i].clear(), r_u[i].clear(),           r_d[i].clear();   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     int T;     cin >> T;     while (T--) {       cin >> n >> m;       vector<pair<int, int> > temp;       for (int i = 1; i <= n; ++i) {         v[i].push_back(233);         for (int j = 1; j <= m; ++j) {           int x;           cin >> x;           v[i].push_back(x);         }       }       for (int i = 1; i <= n; ++i) {         temp.push_back(pair<int, int>(v[i][1], i));       }       sort(temp.begin(), temp.end());       int cnt = 0;       for (int i = 0; i < temp.size(); ++i) {         int fir = temp[i].second;         mp[++cnt].push_back(233);         dui[cnt] = temp[i].second;         l_u[cnt].push_back(233);         l_d[cnt].push_back(233);         r_u[cnt].push_back(233);         r_d[cnt].push_back(233);         for (int j = 1; j <= m; ++j) {           mp[cnt].push_back(v[fir][j]);           l_u[cnt].push_back(v[fir][j]);           l_d[cnt].push_back(v[fir][j]);           r_u[cnt].push_back(v[fir][j]);           r_d[cnt].push_back(v[fir][j]);         }       }       for (int i = 1; i <= n; ++i) {         for (int j = 1; j <= m; ++j) {           l_u[i][j] = max({i - 1 >= 1 ? l_u[i - 1][j] : 0,                            j - 1 >= 1 ? l_u[i][j - 1] : 0, l_u[i][j]});         }       }       for (int i = n; i >= 1; --i) {         for (int j = 1; j <= m; ++j) {           l_d[i][j] = min({i + 1 <= n ? l_d[i + 1][j] : inf,                            j - 1 >= 1 ? l_d[i][j - 1] : inf, l_d[i][j]});         }       }       for (int i = 1; i <= n; ++i) {         for (int j = m; j >= 1; --j) {           r_u[i][j] = min({i - 1 >= 1 ? r_u[i - 1][j] : inf,                            j + 1 <= m ? r_u[i][j + 1] : inf, r_u[i][j]});         }       }       for (int i = n; i >= 1; --i) {         for (int j = m; j >= 1; --j) {           r_d[i][j] = max({i + 1 <= n ? r_d[i + 1][j] : 0,                            j + 1 <= m ? r_d[i][j + 1] : 0, r_d[i][j]});         }       }       bool flag = 1;       for (int i = 1; i < n; ++i) {         for (int j = 1; j < m; ++j) {           if (l_u[i][j] < l_d[i + 1][j] && r_u[i][j + 1] > r_d[i + 1][j + 1]) {             flag = 0;             cout <<  YES  << endl;             bool op[N];             memset(op, 0, sizeof(op));             for (int k = 1; k <= i; ++k) {               op[dui[k]] = 1;             }             for (int k = 1; k <= n; ++k) {               if (op[k])                 cout <<  B ;               else                 cout <<  R ;             }             cout <<     << j << endl;           }           if (!flag) break;         }         if (!flag) break;       }       if (flag) cout <<  NO  << endl;       init();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n, m;     cin >> n >> m;     map<int, set<pair<int, string>, greater<pair<int, string> > > > mp;     for (int i = 0; i < n; i++) {       string s;       int a, b;       cin >> s >> a >> b;       mp[a].insert(pair<int, string>(b, s));     }     for (int i = 1; i <= m; i++) {       pair<int, string> a, b, c;       auto it = mp[i].begin();       a = *it;       it++;       b = *it;       if (mp[i].size() > 2)         it++;       else {         cout << a.second <<     << b.second << endl;         continue;       }       c = *it;       if ((b.first == c.first)) {         cout <<  ?  << endl;       } else {         cout << a.second <<     << b.second << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3fffffff;   const long long oo = 3000000000000000000LL;   int tes, n;   long long ans[3], mn[4], mx[4], ll[4], rr[4], lt[4], rt[4], ret[3];   bool get(long long s, long long x, long long y, long long z) {     if (s <= x) {       ret[0] = s;       ret[1] = 0;       ret[2] = 0;       return 1;     } else if (s <= x + y) {       ret[0] = x;       ret[1] = s - x;       ret[2] = 0;       return 1;     } else if (s <= x + y + z) {       ret[0] = x;       ret[1] = y;       ret[2] = s - x - y;       return 1;     }     return 0;   }   long long shr1(long long x) { return (x - (x & 1)) / 2; }   bool jd(long long t) {     int i, j;     long long s, x, y, z;     for (i = 0; i <= 3; i++) {       if (mn[i] + t < mx[i] - t) return 0;       ll[i] = mx[i] - t;       rr[i] = mn[i] + t;     }     for (i = 0; i < 2; i++) {       lt[0] = shr1(ll[0] - 3 * i + 1);       rt[0] = shr1(rr[0] - 3 * i);       for (j = 1; j <= 3; j++) {         lt[j] = shr1(ll[j] - i + 1);         rt[j] = shr1(rr[j] - i);       }       if (lt[0] <= rt[0] && lt[1] <= rt[1] && lt[2] <= rt[2] && lt[3] <= rt[3] &&           lt[1] + lt[2] + lt[3] <= rt[0]) {         if (lt[1] + lt[2] + lt[3] >= lt[0]) {           ret[0] = lt[1];           ret[1] = lt[2];           ret[2] = lt[3];           ans[0] = ret[1] + ret[2] + i;           ans[1] = ret[0] + ret[2] + i;           ans[2] = ret[0] + ret[1] + i;           return 1;         }         if (get(lt[0] - lt[1] - lt[2] - lt[3], rt[1] - lt[1], rt[2] - lt[2],                 rt[3] - lt[3])) {           ret[0] += lt[1];           ret[1] += lt[2];           ret[2] += lt[3];           ans[0] = ret[1] + ret[2] + i;           ans[1] = ret[0] + ret[2] + i;           ans[2] = ret[0] + ret[1] + i;           return 1;         }       }     }     return 0;   }   int main() {     long long x, y, z, s, l, r, mid;     int i;     scanf( %d , &tes);     while (tes--) {       scanf( %d , &n);       for (i = 0; i <= 3; i++) {         mx[i] = -oo;         mn[i] = oo;       }       for (i = 1; i <= n; i++) {         scanf( %I64d%I64d%I64d , &x, &y, &z);         s = x + y + z;         mx[0] = max(mx[0], s);         mn[0] = min(mn[0], s);         mx[1] = max(mx[1], s - x - x);         mn[1] = min(mn[1], s - x - x);         mx[2] = max(mx[2], s - y - y);         mn[2] = min(mn[2], s - y - y);         mx[3] = max(mx[3], s - z - z);         mn[3] = min(mn[3], s - z - z);       }       l = 0;       r = oo;       while (l < r) {         mid = (l + r) >> 1;         if (jd(mid))           r = mid;         else           l = mid + 1;       }       jd(l);       printf( %I64d %I64d %I64d n , ans[0], ans[1], ans[2]);     }   }


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


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, a, b, i, j, arr[1005] = {0};     cin >> n >> m;     for (i = 1; i <= m; i++) {       cin >> a >> b;       arr[a] = 1;       arr[b] = 1;     }     for (i = 1; i <= n; i++) {       if (arr[i] == 0) break;     }     cout << n - 1 << endl;     for (j = 1; j <= n; j++) {       if (i != j) {         cout << i <<     << j << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct block {     string str;     int pos;   };   static bool compare(block b1, block b2) {     int n = b1.str.length();     for (int i = 0; i < n; i++) {       if (b1.str[i] != b2.str[i]) {         if ((i + 1) % 2) {           return (b1.str[i] < b2.str[i]);         } else {           return (b1.str[i] > b2.str[i]);         }       }     }   }   int main() {     int n, m;     cin >> n >> m;     vector<block> arr(n);     for (int i = 0; i < n; i++) {       cin >> arr[i].str;       arr[i].pos = i + 1;     }     sort(arr.begin(), arr.end(), compare);     for (int i = 0; i < n; i++) {       cout << arr[i].pos <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   double ans[3], temp[3];   int a[200200];   int n;   bool f(double d) {     double x = 0;     int cnt = 0;     for (int i = 0; i < n; i++) {       if (x < a[i]) {         if (cnt == 3) return false;         x = a[i] + 2 * d;         temp[cnt++] = a[i] + d;       }     }     for (int i = 0; i < 3; i++) ans[i] = temp[i];     return true;   }   int main() {     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     sort(a, a + n);     double ul = 0, ur = a[n - 1];     for (int k = 0; k < 400; k++) {       double mid = (ul + ur) / 2;       if (f(mid))         ur = mid;       else         ul = mid;     }     printf( %.6f n , ul);     printf( %.6f %.6f %.6f n , ans[0], ans[1], ans[2]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const int maxn = 3e6;   const double eps = 0.00000001;   long long ksm(long long a, long long n) {     long long ans = 1;     while (n) {       if (n & 1) ans = ans * a % mod;       a = a * a % mod;       n >>= 1;     }     return ans;   }   int main() {     long long c, n, ans;     std::ios::sync_with_stdio(false);     cin >> n;     c = 1;     for (int i = 1; i <= n; i++) c = c * i % mod;     ans = (c + mod - ksm(2, n - 1)) % mod;     cout << ans << endl;   }


#include <bits/stdc++.h>   /*#include <iostream>   #include <algorithm>   #include <math.h>   #include <iomanip>   #include <string>   #include <vector>   #include <set>   #include <sstream>*/      #define ll long long   #define fop(i,m,n) for(int i=m; i<n; i++)   #define fastIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)   #define X first   #define Y second   #define pb push_back   #define mp make_pair   #define all(v) v.begin(),v.end()   #define ep emplace_back      using namespace std;      const long long MOD = 1e9+7;   const long long N = 3e5+5;   const long long Nlog = 17;   const long long Hash = 257;      const double PI = 2.0*acos(0.0);//<cmath>   //const double PI = 3.141592653;   const double E = 2.718281828;      struct bot{       int p, w;   }x[N];      bool cmp(int a, int b){       return x[a].p < x[b].p;   }      void solve(){              int n, m;       cin >> n >> m;       fop(i,0,n) cin >> x[i].p;       fop(i,0,n){           char in;           cin >> in;           if(in ==  R ) x[i].w = 1;           else x[i].w = -1;       }              vector<int> v[2];       fop(i,0,n){           v[x[i].p%2].pb(i);       }       fop(i,0,2) sort(all(v[i]), cmp);              vector<int> ans(n, -1);              fop(t,0,2){           vector<int> s;           for(int i : v[t]){               if(x[i].w == 1){                   s.pb(i);               }else{                   if(s.empty()){                       s.pb(i);                   }else{                       int j = s.back();                       s.pop_back();                                              if(x[j].w == 1) ans[i] = ans[j] = (x[i].p-x[j].p)/2;                       else ans[i] = ans[j] = (x[i].p+x[j].p)/2;                       //cout << i <<     << j <<   :   << ans[i] <<   n ;                   }               }           }                      while(s.size() > 1){               int i = s.back();               s.pop_back();               int j = s.back();               s.pop_back();               if(x[j].w == 1) ans[i] = ans[j] = m - (x[i].p + x[j].p)/2;               else ans[i] = ans[j] = m - (x[i].p - x[j].p)/2;               //cout << i <<     << j <<   :   << ans[i] <<   n ;           }       }              fop(i,0,n){           cout << ans[i] <<    ;       }       cout <<   n ;          }         int main()   {       fastIO;          int t=1;       cin >> t;       while(t--) solve();          return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1000000007;   inline void add(int& x, int y) {     x += y;     if (x >= mod) x -= mod;     if (x < 0) x += mod;   }   inline int mul(int x, int y) { return ((long long)x * y) % mod; }   int gcd(int a, int b) {     if (b) return gcd(b, a % b);     return a;   }   int power(int a, int p) {     int ret = 1;     while (p) {       if (p & 1) ret = mul(ret, a);       a = mul(a, a);       p /= 2;     }     return ret;   }   int phi(int n) {     int ret = n;     int i = 2;     if (n % i == 0) {       ret -= ret / i;       while (n % i == 0) n /= i;     }     for (i = 3; i * i <= n; i++)       if (n % i == 0) {         ret -= ret / i;         while (n % i == 0) n /= i;       }     if (n > 1) ret -= ret / n;     return ret;   }   bool E[400][400];   int D[2][400][400];   int main() {     int N;     scanf( %d , &N);     int M;     scanf( %d , &M);     memset(D, 31, sizeof(D));     queue<pair<pair<int, int>, int>> Q;     int inf = D[0][0][0];     ;     D[0][0][0] = 0;     while (M--) {       int x, y;       scanf( %d , &x);       scanf( %d , &y);       E[x - 1][y - 1] = E[y - 1][x - 1] = true;     }     Q.push({{0, 0}, 0});     while (!Q.empty()) {       int ty = Q.front().second;       pair<int, int> a = Q.front().first;       Q.pop();       int nd = D[ty][a.first][a.second] + 1;       int x = a.first;       int y = a.second;       if (ty == 0) {         if (x != N - 1) {           for (int i = 0; i < N; i++)             if (i != x and E[x][i] == true and D[1][i][y] == inf) {               D[1][i][y] = nd;               Q.push({{i, y}, 1});               ;             }         } else if (D[1][x][y] == inf) {           D[1][x][y] = nd;           Q.push({{x, y}, 1});         }       }       if (ty == 1) {         if (x == N - 1 and E[y][N - 1] == false) {           cout << nd / 2 << endl;           return 0;         }         if (y != N - 1) {           for (int i = 0; i < N; i++)             if (i != x and i != y and E[y][i] == false and D[0][x][i] == inf) {               D[0][x][i] = nd;               Q.push({{x, i}, 0});               ;             }         } else if (D[0][x][y] == inf) {           D[0][x][y] = nd;           Q.push({{x, y}, 0});         }       }     }     cout << -1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int inf = 1e9 + 10;   const ll inf_ll = 1e18 + 10;   template <ll M>   struct modint {     static ll reduce(ll n) {       constexpr static ll b = -1ull / M;       ll r = n - (ll)(__uint128_t(b) * n >> 64) * M;       return r >= M ? r - M : r;     }     static ll inv(ll n) {       ll r = 1;       for (ll k = M - 2; k > 0; k >>= 1, n = reduce(n * n))         if (k & 1) r = reduce(r * n);       return r;     }     ll v;     modint(ll n = 0) : v(reduce(n)) { v += (M & 0 - (v < 0)); }     friend string to_string(const modint n) { return to_string(n.v); }     friend istream& operator>>(istream& i, modint& n) { return i >> n.v; }     friend ostream& operator<<(ostream& o, const modint n) { return o << n.v; }     template <typename T>     explicit operator T() {       return T(v);     }     friend bool operator==(const modint n, const modint m) { return n.v == m.v; }     friend bool operator!=(const modint n, const modint m) { return n.v != m.v; }     friend bool operator<(const modint n, const modint m) { return n.v < m.v; }     friend bool operator<=(const modint n, const modint m) { return n.v <= m.v; }     friend bool operator>(const modint n, const modint m) { return n.v > m.v; }     friend bool operator>=(const modint n, const modint m) { return n.v >= m.v; }     modint& operator+=(const modint n) {       v += n.v;       v -= (M & 0 - (v >= M));       return *this;     }     modint& operator-=(const modint n) {       v -= n.v;       v += (M & 0 - (v < 0));       return *this;     }     modint& operator*=(const modint n) {       v = reduce(v * n.v);       return *this;     }     modint& operator/=(const modint n) {       v = reduce(v * inv(n.v));       return *this;     }     friend modint operator+(const modint n, const modint m) {       return modint(n) += m;     }     friend modint operator-(const modint n, const modint m) {       return modint(n) -= m;     }     friend modint operator*(const modint n, const modint m) {       return modint(n) *= m;     }     friend modint operator/(const modint n, const modint m) {       return modint(n) /= m;     }     modint& operator++() { return *this += 1; }     modint& operator--() { return *this -= 1; }     modint operator++(int) {       modint t = *this;       return *this += 1, t;     }     modint operator--(int) {       modint t = *this;       return *this -= 1, t;     }     modint operator+() { return *this; }     modint operator-() { return modint(0) -= *this; }     modint pow(const ll k) const {       if (k < 1) return k == 0 ? modint(1) : modint(inv(this->v)).pow(-k);       return k & 1 ? *this * (this->pow(k - 1)) : (*this * *this).pow(k >> 1);     }   };   using mod = modint<int(1e9 + 7)>;   const int N = 2e5 + 5;   ll l[N], r[N];   mod p[N], w[N], s;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ll n;     cin >> n;     mod ans = n * n;     for (int i = 0; i < n; i++) cin >> l[i];     for (int i = 0; i < n; i++) cin >> r[i];     for (int i = 0; i < n; i++) w[i] = r[i] - l[i] + 1;     for (int i = 0; i < n - 1; i++) {       p[i] = mod(max(0ll, min(r[i], r[i + 1]) - max(l[i], l[i + 1]) + 1)) /              (w[i] * w[i + 1]);       if (i >= 2) s += p[i - 2];       ans += p[i] * (2 * s + 1 - 2 * n);     }     for (int i = 0; i < n - 2; i++) {       mod x = mod(max(0ll, min({r[i], r[i + 1], r[i + 2]}) -                                max({l[i], l[i + 1], l[i + 2]}) + 1)) /               (w[i] * w[i + 1] * w[i + 2]);       ans += 2 * x;     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long N, K;     cin >> N >> K;     vector<long long> P(N);     for (long long i = 0; i < N; i++) {       cin >> P[i];     }     sort(P.begin(), P.end());     long long min = P[0], vid = 0;     for (long long i = 1; i < P.size(); i++) {       if ((P[i] - P[0]) % K == 0) {         vid += abs(P[i] - P[0]) / K;       } else {         cout << -1 << endl;         return 0;       }     }     cout << vid << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = (int)1e9;   const long long INFLL = (long long)1e16;   const int MOD = (int)1e9 + 7;   template <typename T>   void out(T x) {     cout << x << endl;     exit(0);   }   bool bysec(const pair<int, int> &a, const pair<int, int> &b) {     return a.second < b.second;   }   const int maxn = (int)2e5 + 5;   int n, a[maxn];   vector<int> zero, pos;   set<pair<int, int> > neg;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     bool deleted = false;     cin >> n;     for (int i = 1; i <= n; i++) {       int x;       cin >> x;       if (x > 0) {         pos.push_back(i);       } else if (x < 0) {         neg.insert(make_pair(x, i));       } else {         zero.push_back(i);       }     }     if (pos.size() == 0 && neg.size() == 1 && zero.size() > 0) {       if (zero.size() > 1) {         for (int i = 1; i < (int)zero.size(); i++) {           cout <<  1   << zero[i] <<     << zero[0] << endl;         }       }       cout <<  2   << neg.begin()->second << endl;       return 0;     }     if (pos.size() == 0 && neg.size() == 0 && zero.size() > 0) {       if (zero.size() > 1) {         for (int i = 1; i < (int)zero.size(); i++) {           cout <<  1   << zero[i] <<     << zero[0] << endl;         }       }       return 0;     }     if (zero.size() > 1) {       for (int i = 1; i < (int)zero.size(); i++) {         cout <<  1   << zero[i] <<     << zero[0] << endl;       }     }     if (pos.size() > 1) {       for (int i = 1; i < (int)pos.size(); i++) {         cout <<  1   << pos[i] <<     << pos[0] << endl;       }     }     if (neg.size() % 2 == 1) {       int ind = neg.rbegin()->second;       if (zero.size() > 0) {         cout <<  1   << ind <<     << zero[0] << endl;       } else {         deleted = true;         cout <<  2   << ind << endl;       }     }     int sz = neg.size();     if (sz > 1) {       int end = sz % 2 == 1 ? sz - 1 : sz;       int f = neg.begin()->second;       int i = 0;       for (auto &p : neg) {         if (i == 0) {           i++;           continue;         }         if (i == end) break;         int cur = p.second;         cout <<  1   << cur <<     << f << endl;         i++;       }     }     if (zero.size() > 0 && !deleted) {       cout <<  2   << zero[0] << endl;       deleted = true;     }     if (pos.size() > 0 && sz > 1) {       cout <<  1   << pos[0] <<     << neg.begin()->second << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int x, y;     cin >> x >> y;     if (x > y) {       int t = x;       x = y;       y = t;     }     cout << x <<     << (y - x) / 2 << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 5;   string s;   int main() {     cin >> s;     int i = 0, cnt = 0;     int len = s.size();     while ((i = s.find( VK )) != -1) {       s[i] = s[i + 1] =  . ;       cnt++;     }     if (s.find( VV ) != -1 || s.find( KK ) != -1) cnt++;     printf( %d n , cnt);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-8;   const double PI = acos(-1.0);   int a[100005], s[100005], dp1[100005], dp2[100005], b[100005], ans[100005];   int main() {     int n, i, maxlen, k;     scanf( %d , &n);     for (i = 1; i <= n; i++) scanf( %d , &a[i]), b[n - i + 1] = -a[i];     for (i = 0; i <= n; i++) s[i] = 0x3f3f3f3f;     maxlen = 0;     for (i = 1; i <= n; i++) {       k = lower_bound(s, s + n, a[i]) - s;       s[k] = a[i];       dp1[i] = k + 1;       maxlen = max(maxlen, dp1[i]);     }     for (i = 0; i <= n; i++) s[i] = 0x3f3f3f3f;     for (i = 1; i <= n; i++) {       k = lower_bound(s, s + n, b[i]) - s;       s[k] = b[i];       dp2[n - i + 1] = k + 1;     }     k = 0;     memset(ans, -1, sizeof(ans));     for (i = 1; i <= n; i++) {       if (dp1[i] + dp2[i] - 1 < maxlen)         ans[i] = 1;       else         s[k++] = dp1[i];     }     sort(s, s + k);     for (i = 1; i <= n; i++) {       if (ans[i] == 1)         printf( 1 );       else {         if (upper_bound(s, s + k, dp1[i]) - lower_bound(s, s + k, dp1[i]) > 1)           printf( 2 );         else           printf( 3 );       }     }     puts(  );   }


#include <bits/stdc++.h>   using namespace std;   int n, m, q, tot, rec[100010], cnt[100010][360], num[360], Limit, W,       tmp[100010];   long long delta[360], sum[360], a[100010];   struct Set {     int k, l, r;   } s[100010];   long long AskBig(int now) {     int pos = tmp[now];     long long ans = sum[pos];     for (int i = 1; i <= W; i++) ans += (long long)cnt[now][i] * delta[i];     return ans;   }   long long AskSmall(int now) {     long long ans = 0;     for (int i = s[now].l; i <= s[now].r; i++) ans += a[rec[i]];     for (int i = 1; i <= W; i++) ans += (long long)cnt[now][i] * delta[i];     return ans;   }   void ChangeSmall(int now, int v) {     for (int i = s[now].l; i <= s[now].r; i++) a[rec[i]] += v;     for (int i = 1; i <= W; i++) sum[i] += (long long)cnt[now][i] * v;   }   int main() {     scanf( %d%d%d , &n, &m, &q);     Limit = ceil(sqrt(n));     for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]);     for (int i = 1; i <= m; i++) {       int k;       scanf( %d , &k);       s[i].k = k;       s[i].l = tot + 1;       for (int j = 1; j <= k; j++) {         int x;         scanf( %d , &x);         rec[++tot] = x;       }       s[i].r = tot;       if (k > Limit) num[++W] = i;     }     for (int i = 1; i <= W; i++) {       int u = num[i];       for (int j = s[u].l; j <= s[u].r; j++) {         ++tmp[rec[j]];         sum[i] += a[rec[j]];       }       for (int j = 1; j <= m; j++)         for (int k = s[j].l; k <= s[j].r; k++) cnt[j][i] += tmp[rec[k]];       for (int j = s[u].l; j <= s[u].r; j++) --tmp[rec[j]];     }     for (int i = 1; i <= W; i++) tmp[num[i]] = i;     for (int i = 1; i <= q; i++) {       char c = getchar();       int x, v;       while (c !=  ?  && c !=  + ) c = getchar();       if (c ==  ? ) {         scanf( %d , &x);         if (s[x].k > Limit)           printf( %I64d n , AskBig(x));         else           printf( %I64d n , AskSmall(x));       } else {         scanf( %d%d , &x, &v);         if (s[x].k > Limit)           delta[tmp[x]] += v;         else           ChangeSmall(x, v);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 1e5 + 5;   long long n, a[maxn], dp[maxn][2], f[maxn][2], d[maxn];   long long head[maxn], ecnt;   struct edge {     long long to, next;   } e[maxn << 1];   void adde(long long u, long long v) {     e[++ecnt].to = v;     e[ecnt].next = head[u];     head[u] = ecnt;   }   long long read() {     long long res = 0, f = 1;     char ch;     do {       ch = getchar();       if (ch ==  - ) f = -1;     } while (!isdigit(ch));     do {       res = res * 10 + ch -  0 ;       ch = getchar();     } while (isdigit(ch));     return res * f;   }   void dfs1(long long u, long long fa) {     dp[u][0] = dp[u][1] = f[u][0] = d[u] = a[u];     for (long long i = head[u]; i; i = e[i].next) {       long long v = e[i].to;       if (v == fa) continue;       dfs1(v, u);       dp[u][0] = max(dp[u][0], dp[v][0]);       dp[u][0] = max(dp[u][0], dp[u][1] + dp[v][1]);       dp[u][0] = max(dp[u][0], d[u] + f[v][0]);       dp[u][0] = max(dp[u][0], d[v] + f[u][0]);       dp[u][1] = max(dp[u][1], dp[v][1]);       dp[u][1] = max(dp[u][1], d[u] + d[v]);       f[u][0] = max(f[u][0], a[u] + f[v][0]);       f[u][0] = max(f[u][0], d[u] + dp[v][1]);       f[u][0] = max(f[u][0], a[u] + d[v] + f[u][1]);       f[u][1] = max(f[u][1], dp[v][1]);       d[u] = max(d[u], d[v] + a[u]);     }   }   signed main() {     n = read();     for (long long i = (1); i <= (n); ++i) a[i] = read();     for (long long i = (1); i <= (n - 1); ++i) {       long long u = read(), v = read();       adde(u, v);       adde(v, u);     }     dfs1(1, 0);     printf( %lld n , dp[1][0]);   }


#include<bits/stdc++.h>   #define FOR(i,a,b) for( int i=(a); i<(b); ++i)   #define REP(i,a,b) for( int i=(a); i<=(b); ++i)   #define SHOW(x) for(auto i:(x)){ cout << i <<    ;} cout << endl;   #define MAXN 25   using namespace std;      typedef long long LL;   typedef pair<int,int> PII;   typedef vector<int> VI;   void solve(){       string s;cin >> s;       bool f1 = false,f2 = false;       for(int i = 1; i < s.size(); i++){           if(s[i] == s[i-1] && s[i] ==  1 ){               f1 = true;           }           if(f1 && s[i] == s[i-1] && s[i] ==  0 ){               f2 = true;               break;           }       }       if(f2) cout <<  NO  << endl;       else cout <<  YES  << endl;   }   int main(){       ios::sync_with_stdio(false);       cin.tie(0);       int n; cin >> n;       while(n--){           solve();       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2000000 + 77, Mod = 1000000007, L = 30;   int n, m, k, dp[N], T[L], P[N], Ps[N], SS[L];   char S[N];   int main() {     scanf( %d %d , &n, &k);     scanf( %s , S + 1);     m = strlen(S + 1);     dp[0] = Ps[0] = 1;     for (int i = 1; i <= m; ++i) {       P[i] = T[S[i] -  a ], T[S[i] -  a ] = i;       dp[i] = (Ps[i - 1] - SS[S[i] -  a ] + Mod) % Mod;       Ps[i] = (Ps[i - 1] + dp[i]) % Mod;       SS[S[i] -  a ] = (SS[S[i] -  a ] + dp[i]) % Mod;     }     for (int i = m + 1; i <= n + m; ++i) {       int Mn = 0;       for (int j = 1; j < k; ++j)         if (T[j] < T[Mn]) Mn = j;       P[i] = T[Mn], T[Mn] = i;       dp[i] = (Ps[i - 1] - SS[Mn] + Mod) % Mod;       Ps[i] = (Ps[i - 1] + dp[i]) % Mod;       SS[Mn] = (SS[Mn] + dp[i]) % Mod;     }     printf( %d n , Ps[n + m]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   unordered_map<string, int> mp;   char ans[2][1005];   bool x[5005];   int oprd[2][5005];   char orig[5005][1005];   char op[5005][20];   char input[4][1005];   char line[4 * 1005];   int main() {     int n, m;     while (scanf( %d%d  , &n, &m) != EOF) {       mp.clear();       for (int i = (1); i <= (n); i++) {         fgets(line, 4 * 1005, stdin);         int num = sscanf(line,  %s := %s %s %s , input[0], input[1], input[2],                          input[3]);         mp[string(input[0])] = i;         if (num == 4) {           if (input[1][0] ==  ? )             oprd[0][i] = -1;           else             oprd[0][i] = mp[string(input[1])];           strcpy(op[i], input[2]);           if (input[3][0] ==  ? )             oprd[1][i] = -1;           else             oprd[1][i] = mp[string(input[3])];         } else           op[i][0] =  # , strcpy(orig[i], input[1]);       }       for (int i = (0); i <= (m - 1); i++) {         int val[2];         val[0] = val[1] = 0;         for (int j = (0); j <= (1); j++) {           for (int k = (1); k <= (n); k++) {             if (op[k][0] ==  # )               x[k] = orig[k][i] -  0 ;             else {               int oprd1 = (oprd[0][k] == -1 ? j : x[oprd[0][k]]);               int oprd2 = (oprd[1][k] == -1 ? j : x[oprd[1][k]]);               if (op[k][0] ==  X )                 x[k] = oprd1 ^ oprd2;               else if (op[k][0] ==  O )                 x[k] = oprd1 | oprd2;               else                 x[k] = oprd1 & oprd2;             }             val[j] += x[k];           }         }         ans[0][i] = (val[0] <= val[1] ?  0  :  1 );         ans[1][i] = (val[0] < val[1] ?  1  :  0 );       }       ans[0][m] = ans[1][m] =   0 ;       printf( %s n , ans[0]);       printf( %s n , ans[1]);     }     return 0;   }


#include <bits/stdc++.h>   const int inf = 0x3f3f3f3f;   const int maxn = 500050;   const int mod = 1e9 + 7;   using namespace std;   int main() {     ios::sync_with_stdio(0);     int n, a, b;     cin >> n >> a >> b;     int c = 0, ans = 0;     for (int i = 1; i <= n; i++) {       int x;       cin >> x;       if (x == 1) {         if (a > 0)           a--;         else {           if (b > 0) {             b--;             c++;           } else if (c > 0)             c--;           else             ans++;         }       } else {         if (b > 0)           b--;         else           ans += 2;       }     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 2e5 + 2;   string s;   long long n;   long long func(vector<long long> a) {     stack<long long> st;     long long max_ = 0;     for (long long i = 0; i < n; i++) {       while (!st.empty() && a[i] > st.top()) {         long long cur = st.top();         st.pop();         max_ = max(cur ^ a[i], max_);       }       st.push(a[i]);     }     return max_;   }   void solve() {     cin >> n;     vector<long long> a(n);     for (long long i = 0; i < n; i++) {       cin >> a[i];     }     long long ans = func(a);     reverse(a.begin() + 0, a.end());     ans = max(ans, func(a));     cout << ans;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     solve();     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     for (int i = 1; i <= 400; i++) s += to_string(i);     int n;     cin >> n;     cout << s[n - 1];   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, b, g, ans = 0;     cin >> b >> g >> n;     if (n < b && n < g)       ans = n + 1;     else if (n < b && n > g)       ans = g + 1;     else if (n < g && n > b)       ans = b + 1;     else {       ans = min(b, g) + 1 - (n - max(b, g));     }     cout << ans << endl;   }