#include<bits/stdc++.h>   #define fi first   #define se second         #define all(x) (x).begin(), (x).end()   #define rall(x) (x).rbegin(), (x).rend()   #define pb push_back   #define len(x) (int)(x).size()   typedef long long ll;   typedef long double ld;   using namespace std;      const int N = 1001;   const int mod = 998244353;   int g[N][N];   int perm[N][N];   int cnt[N][N];   int n;   void add(int id, int val){       for(int x = 0; x < n; ++x){           cnt[x][perm[id][x]] += val;       }   }   void md(int &a){       if(a >= mod)           a-=mod;   }   void solve(){       cin >> n;       for(int i = 0;  i < 2*n; ++i){           for(int j = 0 ; j < n; ++j){               cin >> perm[i][j];           }           add(i , +1);       }       vector < bool > active(2*n, 1);       int ans = 1;       vector < int > answer;       while(true){           bool have = 0;           for(int x = 0; x < 2*n; ++x)               if(active[x])have = 1;           if(!have)               break;           int found = -1;           for(int x = 0 ;x < 2 * n; ++x){               if(active[x]){                   for(int i = 0 ;i  < n; ++i){                       if(cnt[i][perm[x][i]] == 1){                           found = x;                           break;                       }                   }               }           }           if(found == -1){               ans *= 2, md(ans);               for(int x = 0; x < 2*n; ++x)                   if(active[x])                       found = x;              }           answer.pb(found);           /// dell all shit           for(int j = 0; j < 2*n;++j){               if(active[j]){                   bool del = 0;                   for(int z = 0; z < n; ++z){                       if(perm[found][z] == perm[j][z])                           del = 1;                   }                   if(del)                       active[j] = 0, add(j , -1);               }           }       }       cout << ans <<   n ;       for(auto u : answer)           cout << u + 1 <<    ;       cout <<   n ;   }   signed main()   {       ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);       int t;cin >> t;       while(t--){           solve();       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxN = 105;   int n, s, c[maxN], sum1, sum2;   vector<int> d;   bool cmp(int A, int B) { return A > B; }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> s;       for (int j = 1; j <= s; j++) cin >> c[j];       for (int j = 1; j <= s / 2; j++) sum1 += c[j];       for (int j = (s + 1) / 2 + 1; j <= s; j++) sum2 += c[j];       if (s % 2 == 1) d.push_back(c[(s + 1) / 2]);     }     sort(d.begin(), d.end(), cmp);     for (int i = 0; i < d.size(); i++)       if (i % 2 == 0)         sum1 += d[i];       else         sum2 += d[i];     cout << sum1 <<     << sum2;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long n, ans = 0;     cin >> n;     vector<int> a(n + 5), b(n + 5), c(n + 5);     map<long long, long long> m;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i <= n; i++) cin >> b[i], m[b[i]] = i;     for (int i = 0; i < n; i++) c[i + 1] = m[a[i + 1]];     for (int i = 0; i < n - 1; i++)       if (c[i + 2] > c[i + 1])         ans++;       else         break;     cout << n - ans - 1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   bool uin(T &a, T b) {     return a <= b ? true : false;   }   template <class T>   bool uax(T &a, T b) {     return a >= b ? true : false;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cout.precision(10);     cout << fixed;     long long H;     int n;     cin >> H >> n;     vector<long long> a(n);     long long sum = 0;     long long gap = 0;     long long h = H;     for (int i = 0; i < n; i++) {       cin >> a[i];       sum -= a[i];       h += a[i];       if (h <= 0) {         cout << i + 1 << endl;         return 0;       }       gap = max(gap, sum);     }     if (sum <= 0) {       cout << -1 << endl;       return 0;     }     long long whole = (H - gap) / sum;     H -= whole * sum;     long long result = whole * n;     for (int i = 0;; i++) {       H += a[i % n];       result++;       if (H <= 0) {         cout << result << endl;         break;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long arr[1002][1002];   long long store[2][1000002];   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long n, m, k, p;     cin >> n >> m >> k >> p;     for (long long i = 1; i <= n; i++)       for (long long j = 1; j <= m; j++) cin >> arr[i][j];     multiset<long long> s1, s2;     for (long long i = 1; i <= n; i++) {       long long s = 0;       for (long long j = 1; j <= m; j++) s += arr[i][j];       s1.insert(s);     }     for (long long i = 1; i <= m; i++) {       long long s = 0;       for (long long j = 1; j <= n; j++) s += arr[j][i];       s2.insert(s);     }     for (long long i = 1; i <= k; i++) {       long long a = *s1.rbegin();       store[0][i] += store[0][i - 1] + a;       s1.erase(s1.find(a));       s1.insert(a - m * p);       a = *s2.rbegin();       store[1][i] += store[1][i - 1] + a;       s2.erase(s2.find(a));       s2.insert(a - n * p);     }     long long maxi = -1e18;     for (long long i = 0; i <= k; i++)       maxi = max(maxi, store[0][i] + store[1][k - i] - i * (k - i) * p);     cout << maxi;   }


#include <bits/stdc++.h>   using namespace std;   long long int arr[10000];   int main() {     std::ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int a, b, c, i, j, k, n, m, state = 0;     for (i = 0; i < 6; i++) cin >> arr[i];     sort(arr, arr + 6);     for (i = 0; i < 3; i++) {       if (arr[i] == arr[i + 1] && arr[i + 1] == arr[i + 2] &&           arr[i + 2] == arr[i + 3] && state == 0) {         state = 1;         arr[i] = 0;         arr[i + 1] = 0;         arr[i + 2] = 0;         arr[i + 3] = 0;         break;       }     }     if (state == 0) {       cout <<  Alien  << endl;       return 0;     }     long long int t1 = 0, t2 = 0;     for (i = 0; i < 6; i++) {       if (arr[i] != 0 && t1 == 0)         t1 = arr[i];       else if (arr[i] != 0)         t2 = arr[i];     }     if (t1 == t2)       cout <<  Elephant  << endl;     else       cout <<  Bear  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   T Max(const T &a, const T &b) {     return a > b ? a : b;   }   const int SN = 100000 + 10;   long long w[SN], f[SN][2], down[SN], g[SN], h[SN], head[SN];   long long u, v, n, num;   bool vis[SN];   struct Edge {     int from, to, next;   } E[SN << 2];   void Read(long long &x) {     long long in = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch =  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       in = in * 10 + ch -  0 ;       ch = getchar();     }     x = in * f;   }   void Add(int u, int v) {     E[++num].from = u;     E[num].to = v;     E[num].next = head[u];     head[u] = num;   }   void dfs(int u) {     vis[u] = 1;     g[u] = f[u][0] = f[u][1] = down[u] = w[u];     for (int i = head[u]; i; i = E[i].next) {       int to = E[i].to;       if (vis[to]) continue;       dfs(to);       f[u][0] = Max(f[u][0], f[to][0]);       f[u][0] = Max(f[u][0], f[u][1] + f[to][1]);       f[u][0] = Max(f[u][0], g[to] + down[u]);       f[u][0] = Max(f[u][0], down[to] + g[u]);       f[u][1] = Max(f[u][1], f[to][1]);       f[u][1] = Max(f[u][1], down[u] + down[to]);       g[u] = Max(g[u], g[to] + w[u]);       g[u] = Max(g[u], down[u] + f[to][1]);       g[u] = Max(g[u], down[to] + w[u] + h[u]);       h[u] = Max(h[u], f[to][1]);       down[u] = Max(down[u], down[to] + w[u]);     }   }   int main() {     Read(n);     for (int i = 1; i <= n; i++) Read(w[i]);     for (int i = 1; i < n; i++) {       Read(u), Read(v);       Add(u, v), Add(v, u);     }     dfs(1);     printf( %lld n , f[1][0]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long n;     cin >> n;     vector<pair<long long, long long> > a(n);     for (auto &x : a) cin >> x.first;     for (auto &x : a) cin >> x.second;     long long ans = 0;     sort(a.begin(), a.end());     priority_queue<long long> pq;     long long sum = 0;     for (long long i = 0; i < n; ++i) {       if (!pq.empty()) {         long long dif = a[i].first - a[i - 1].first - 1;         long long j = 0;         while (!pq.empty()) {           if (j >= dif) break;           long long top = pq.top();           pq.pop();           ans += top * j;           sum -= top;           ++j;         }         assert(sum >= 0);         ans += sum * dif;       }       long long j = i;       while (j < n and a[i].first == a[j].first) {         pq.push(a[j].second);         sum += a[j].second;         ++j;       }       long long top = pq.top();       pq.pop();       sum -= top;       assert(sum >= 0);       ans += sum;       i = j - 1;     }     long long j = 0;     while (!pq.empty()) {       ans += pq.top() * j;       ++j;       pq.pop();     }     cout << ans <<   n ;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long t = 1;     for (long long i = 0; i < t; ++i) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename P, typename Q>   ostream& operator<<(ostream& os, pair<P, Q> p) {     os <<  (  << p.first <<  ,  << p.second <<  ) ;     return os;   }   template <typename P, typename Q>   istream& operator>>(istream& is, pair<P, Q>& p) {     is >> p.first >> p.second;     return is;   }   template <typename T>   ostream& operator<<(ostream& os, vector<T> v) {     os <<  ( ;     for (auto& i : v) os << i <<  , ;     os <<  ) ;     return os;   }   template <typename T>   istream& operator>>(istream& is, vector<T>& v) {     for (auto& i : v) is >> i;     return is;   }   int f(string s) {     int cnt = 0;     for (int i = 0; i < s.size(); ++i) {       for (int j = 0; j <= s.size(); ++j) {         string t;         for (int k = i; k < j; ++k) {           t += s[k];         }         if (t.size()) {           string u = t;           reverse(u.begin(), u.end());           cnt += (t == u);         }       }     }     return cnt;   }   void fn() {     cout << f( ghghghg ) <<     << f( gggghhh ) << endl;     cout << f( llooo ) <<     << f( ololo ) << endl;     cout << f( gagadbcgghhchbdf ) <<     << f( abccbaghghghgdfd ) <<              << f( aabbccddfgggghhh ) << endl;   }   int main(int argc, char* argv[]) {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     string s;     while (cin >> n >> s) {       sort(s.begin(), s.end());       cout << s << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 105;   int n;   string s[MAXN];   string ans =   ;   vector<int> g[MAXN];   bool has[MAXN];   bool used[MAXN];   vector<int> tp;   void dfs(int v) {     used[v] = true;     for (int i = 0; i < g[v].size(); i++) {       int to = g[v][i];       if (!used[to]) dfs(to);     }     tp.push_back(v);   }   void dfs2(int v) {     used[v] = true;     ans.append(1, (char)( a  + v));     for (int i = 0; i < (int)g[v].size(); i++) {       int to = g[v][i];       if (!used[to]) dfs2(to);     }   }   int main() {     scanf( %d n , &n);     for (int i = 1; i <= n; i++) {       getline(cin, s[i]);     }     for (int i = 1; i <= n; i++) {       for (int j = 0; j < s[i].length(); j++) {         has[s[i][j] -  a ] = true;         if (j + 1 < s[i].length()) {           int from = s[i][j] -  a ;           int to = s[i][j + 1] -  a ;           g[from].push_back(to);         }       }     }     for (int i = 0; i < 26; i++) {       if (has[i] && !used[i]) {         dfs(i);       }     }     reverse(tp.begin(), tp.end());     memset(used, false, sizeof(used));     for (int i = 0; i < (int)tp.size(); i++) {       int cur = tp[i];       if (!used[cur]) dfs2(cur);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   int n;   long long l[100001], r[100001], ans = 0;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %I64d%I64d , &l[i], &r[i]);     std::sort(l + 1, l + n + 1);     std::sort(r + 1, r + n + 1);     for (int i = 1; i <= n; i++) {       ans += l[i] > r[i] ? l[i] : r[i];     }     printf( %I64d n , ans + 1ll * n);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a, b, men, may;   vector<pair<int, int> > arr;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     men = 1000000009;     for (int i = 0; i < n; i++) {       cin >> a >> b;       arr.push_back(make_pair(a, b));       men = min(a, men);       may = max(may, b);     }     for (int i = 0; i < n; i++) {       if (arr[i].first == men && arr[i].second == may) {         cout << i + 1;         return 0;       }     }     cout << -1;   }


#include <bits/stdc++.h>   using namespace std;   const long long Max = 2e5 + 9, inf = 1e18;   long long A[Max], B[Max], d[Max];   int find_nxt(long long id, long long x) {     if (A[id] - B[id] >= x) return B[id] += x, id;     d[id] = find_nxt(d[id], x - A[id] + B[id]), B[id] = A[id];     return d[id];   }   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     long long n, m, a, id, x;     cin >> n;     for (int i = 1; i <= n; i++) cin >> A[i], d[i] = i + 1;     A[n + 1] = inf;     cin >> m;     while (m--) {       cin >> a >> id;       if (a == 1) {         cin >> x;         find_nxt(id, x);       } else         cout << B[id] <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100005;   int c1[MAXN], c2[MAXN];   vector<pair<int, int> > ans;   int main() {     int n;     scanf( %d , &n);     int in, winner;     for (int i = 1; i <= n; i++) {       scanf( %d , &in);       if (i == n) winner = in;       c1[i] = c1[i - 1] + (in == 1);       c2[i] = c2[i - 1] + (in == 2);     }     for (int t = 1; t <= n; t++) {       int w1 = 0, w2 = 0, loc = 0;       while (loc < n) {         int loc1 = lower_bound(c1, c1 + n + 1, c1[loc] + t) - c1;         int loc2 = lower_bound(c2, c2 + n + 1, c2[loc] + t) - c2;         loc = min(loc1, loc2);         if (loc > n) break;         if (loc1 < loc2)           w1++;         else           w2++;       }       if (loc == n)         if ((winner == 1 && w1 > w2) || (winner == 2 && w1 < w2))           ans.push_back(make_pair(max(w1, w2), t));     }     sort(ans.begin(), ans.end());     printf( %d n , ans.size());     for (int i = 0; i < ans.size(); i++)       printf( %d %d n , ans[i].first, ans[i].second);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Rect {     int x1, y1, x2, y2;     long long area() { return (long long)(x2 - x1) * (y2 - y1); }   };   long long intersect(const Rect&, const Rect&);   Rect intersectingRect(const Rect&, const Rect&);   int main() {     Rect white, black1, black2, b1Inside, b2Inside;     long long b1Cover, b2Cover, totalCover;     cin >> white.x1 >> white.y1 >> white.x2 >> white.y2;     cin >> black1.x1 >> black1.y1 >> black1.x2 >> black1.y2;     cin >> black2.x1 >> black2.y1 >> black2.x2 >> black2.y2;     b1Cover = intersect(white, black1);     b2Cover = intersect(white, black2);     b1Inside = intersectingRect(white, black1);     b2Inside = intersectingRect(white, black2);     totalCover = b1Cover + b2Cover - intersect(b1Inside, b2Inside);     if (white.area() - totalCover == 0)       cout <<  NO ;     else       cout <<  YES ;   }   long long intersect(const Rect& rect1, const Rect& rect2) {     long long x = max(0, min(rect1.x2, rect2.x2) - max(rect1.x1, rect2.x1));     long long y = max(0, min(rect1.y2, rect2.y2) - max(rect1.y1, rect2.y1));     return x * y;   }   Rect intersectingRect(const Rect& rect1, const Rect& rect2) {     Rect rect;     if (intersect(rect1, rect2)) {       rect.x1 = max(rect1.x1, rect2.x1);       rect.x2 = min(rect1.x2, rect2.x2);       rect.y1 = max(rect1.y1, rect2.y1);       rect.y2 = min(rect1.y2, rect2.y2);     } else {       rect.x1 = 0;       rect.x2 = 0;       rect.y1 = 0;       rect.y2 = 0;     }     return rect;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const int N = 2e5 + 10;   const int INF = 0x3f3f3f3f;   const long long LINF = 0x3f3f3f3f3f3f3f3f;   inline int read() {     static char buf[1000000], *p1 = buf, *p2 = buf;     register int x = false;     register char ch =         p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2)             ? EOF             : *p1++;     ;     register bool sgn = false;     while (ch !=  -  && (ch <  0  || ch >  9 ))       ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2)                ? EOF                : *p1++;     ;     if (ch ==  - )       sgn = true,       ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2)                ? EOF                : *p1++;     ;     while (ch >=  0  && ch <=  9 )       x = (x << 1) + (x << 3) + (ch ^ 48),       ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2)                ? EOF                : *p1++;     ;     return sgn ? -x : x;   }   long long fpow(long long a, int b, int mod) {     long long res = 1;     for (; b > 0; b >>= 1) {       if (b & 1) res = res * a % mod;       a = a * a % mod;     }     return res;   }   long long h[N][2];   char s[N];   long long base[N];   int cnt[N];   long long calc(int L, int R) {     return (h[R][L & 1] - h[L - 1][L & 1] * base[cnt[R] - cnt[L - 1]] % mod +             mod) %            mod;   }   int main() {     int n, m;     cin >> n;     scanf( %s , s + 1);     cin >> m;     base[0] = 1;     for (int i = 1; i <= n; i++) {       base[i] = base[i - 1] * 998244353 % mod;       cnt[i] = cnt[i - 1];       h[i][0] = h[i - 1][0];       h[i][1] = h[i - 1][1];       if (s[i] ==  0 ) {         cnt[i] = cnt[i - 1] + 1;         h[i][0] = (h[i - 1][0] * 998244353 % mod + (i & 1) + 1) % mod;         h[i][1] = (h[i - 1][1] * 998244353 % mod + (i & 1 ^ 1) + 1) % mod;       }     }     while (m--) {       int x, y, z;       cin >> x >> y >> z;       if (calc(x, x + z - 1) == calc(y, y + z - 1))         puts( YES );       else         puts( NO );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int const maxn = 4e6;   int tr01[maxn][2], x;   int len, check[maxn], n, tot;   void insert(int x) {     int ex, v = 0;     for (int i = 29; i >= 0; i--) {       ex = !!(x & (1 << i));       if (tr01[v][ex])         v = tr01[v][ex];       else {         tr01[v][ex] = ++tot;         v = tot;       }     }   }   void init() {     tot = 0;     memset(tr01, 0, sizeof(tr01));   }   int work(int x, int wei) {     if (!tr01[x][0] && !tr01[x][1]) return 0;     if (tr01[x][0] == 0) return work(tr01[x][1], wei - 1);     if (tr01[x][1] == 0) return work(tr01[x][0], wei - 1);     return (1 << wei) + min(work(tr01[x][1], wei - 1), work(tr01[x][0], wei - 1));   }   int main() {     scanf( %d , &n);     init();     for (int i = 1; i <= n; i++) {       scanf( %d , &x);       insert(x);     }     printf( %d n , work(0, 29));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s;   inline void YES() {     puts( Yes );     exit(0);   }   inline void NO() {     puts( No );     exit(0);   }   int main() {     cin >> s;     int n = (int)s.length();     string baru =   ;     int score = 0;     int pl = -1, mn = 1;     for (int i = n - 1; i >= 0; i--) {       if (s[i] ==  - )         score += pl;       else         score += mn;       swap(mn, pl);     }     if (score == 0)       YES();     else       NO();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int l, r, a;     cin >> l >> r >> a;     int total = 0;     while (l > 0 && r > 0) {       l--, r--, total += 2;     }     while (l > 0 && a > 0) {       l--, a--, total += 2;     }     while (r > 0 && a > 0) {       r--, a--, total += 2;     }     if (a > 0)       if (a % 2 == 0)         total += a;       else         total += a - 1;     cout << total;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long mod(long int a) { return (a > 0 ? a : (-a)); }   long min(long int a, long int b) { return (a) > (b) ? (b) : (a); }   long max(long int a, long int b) { return (a) < (b) ? (b) : (a); }   bool sortbyff(const pair<int, int> &a, const pair<int, int> &b) {     return (a.first < b.first);   }   long num[1000001], t[5001][2];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     long n, m, k, i, j, l, r, a = -1, b = -1, c, d, s, u, w, x = 0, y, z, sum = 0,                               p = 0, ans = 0, M = 10000000;     cin >> n;     cout << n <<    ;     for (i = 1; i < n; i++) {       cout << i <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200 + 5;   int n, m, p[N];   double dp[N][N][N];   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cout << fixed << setprecision(10);     cin >> n >> m;     for (int i = 0; i < n; i++) cin >> p[i];     for (int i = 0; i < n; i++)       for (int j = i + 1; j < n; j++) dp[j][i][0] = 1;     long double pi = n * (n + 1) / 2;     pi = 1 / pi;     for (int k = 1; k <= m; k++) {       for (int i = 0; i < n; i++)         for (int j = 0; j < n; j++) {           if (i == j) continue;           for (int L = 0; L < n; L++)             for (int R = L; R < n; R++) {               int x = i, y = j;               if (L <= x && x <= R) x = L + R - x;               if (L <= y && y <= R) y = L + R - y;               dp[i][j][k] += dp[x][y][k - 1] * pi;             }         }     }     long double ans = 0;     for (int i = 0; i < n; i++)       for (int j = i + 1; j < n; j++) {         if (p[i] > p[j])           ans += dp[j][i][m];         else           ans += dp[i][j][m];       }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   char s[100002];   int main() {     cin.getline(s, 100002);     int l = strlen(s), flag;     for (int i = 0; i < l; i++) {       char c = s[i];       if (s[i] == s[l - i - 1] &&           (c ==  A  || c ==  H  || c ==  I  || c ==  M  || c ==  O  || c ==  T  ||            c ==  U  || c ==  V  || c ==  W  || c ==  X  || c ==  Y ))         flag = 1;       else {         flag = 0;         break;       }     }     if (l % 2 == 1 && flag) {       char c = s[l / 2];       if (c ==  A  || c ==  H  || c ==  I  || c ==  M  || c ==  O  || c ==  T  ||           c ==  U  || c ==  V  || c ==  W  || c ==  X  || c ==  Y )         flag = 1;       else         flag = 0;     }     if (flag)       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct res {     double l, r, w;   } T[800000];   double x[210000], s[210000], p, c, ans, w;   int N, M, i, l, r;   int init(int i, int l, int r, int x, double w) {     if (x < l || r < x) return 0;     if (l == r) {       T[i].w = T[i].l = T[i].r = w;       return 0;     }     int mid = l + r >> 1;     init(i * 2, l, mid, x, w);     init(i * 2 + 1, mid + 1, r, x, w);   }   int build(int i, int l, int r) {     if (l == r) return 0;     int mid = l + r >> 1, ls = i << 1, rs = (i << 1) + 1;     build(ls, l, mid);     build(rs, mid + 1, r);     T[i].l = max(T[ls].l, s[mid] - s[l - 1] + T[rs].l);     T[i].r = max(T[rs].r, T[ls].r + s[r] - s[mid]);     T[i].w =         max(T[ls].r + T[rs].l, max(max(T[ls].w, T[rs].w), max(T[i].l, T[i].r)));   }   res get(int i, int l, int r, int x, int y) {     if (x <= l && r <= y) return T[i];     int mid = l + r >> 1, ls = i << 1, rs = (i << 1) + 1;     res X;     if (y <= mid)       X = get(ls, l, mid, x, y);     else if (mid < x)       X = get(rs, mid + 1, r, x, y);     else {       res L = get(ls, l, mid, x, mid);       res R = get(rs, mid + 1, r, mid + 1, y);       X.w = max(max(L.w, R.w), L.r + R.l);       X.l = max(L.l, s[mid] - s[x - 1] + R.l);       X.r = max(R.r, s[y] - s[mid] + L.r);     }     return X;   }   int main() {     scanf( %d%d%lf , &N, &M, &c);     for (i = 1; i <= N; i++) scanf( %lf , &x[i]);     --N;     for (i = 1; i <= N; i++) {       scanf( %lf , &p);       w = (x[i + 1] - x[i]) / 2.0 - p * c / 100.0;       s[i] = s[i - 1] + w;       init(1, 1, N, i, w);     }     build(1, 1, N);     while (M--) {       scanf( %d%d , &l, &r);       res delta = get(1, 1, N, l, r - 1);       ans += max(0.0, delta.w);     }     printf( %.8lf n , ans);   }


#include <bits/stdc++.h>   int n;   int m;   char str[3000100];   int preveri(int x) {     int stevec = 0;     if (x - 1 >= 0 && str[x - 1] ==  . ) {       stevec++;     }     if (x + 1 < n && str[x + 1] ==  . ) {       stevec++;     }     return stevec;   }   int main() {     scanf( %d , &n);     scanf( %d , &m);     scanf( %s , str);     int vsota = 0;     for (int i = 1; i < n; i++) {       if (str[i - 1] ==  .  && str[i] ==  . ) {         vsota++;       }     }     for (int i = 0; i < m; i++) {       int x;       char c;       scanf( %d %c , &x, &c);       int a = preveri(x - 1);       if (c ==  .  && str[x - 1] !=  . ) {         vsota = vsota + a;       } else if (c !=  .  && str[x - 1] ==  . ) {         vsota = vsota - a;       }       str[x - 1] = c;       printf( %d n , vsota);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 10, Mod = 1e9 + 7;   int n, m, k, A[N], P[N];   map<int, int> M;   long long tot;   int main() {     scanf( %d%d , &n, &k);     m = (1 << k) - 1;     for (int i = 1; i <= n; i++)       scanf( %d , &A[i]), P[i] = P[i - 1] ^ A[i], M[min(P[i], m - P[i])]++;     M[0]++;     for (auto X : M) {       int a = X.second >> 1;       int b = X.second - a;       tot += 1ll * a * (a - 1) / 2;       tot += 1ll * b * (b - 1) / 2;     }     tot = 1ll * (n + 1) * n / 2 - tot;     printf( %lld , tot);     return (0);   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1000000007;   struct Interval {     long long a, b, ind;     bool operator<(const Interval& ano) const { return b < ano.b; }   } a[200010];   bool used[200010];   set<long long> st;   struct SEG {     long long sum[1600010], tg1[1600010], tg2[1600010];     inline void pushup(long long u) {       sum[u] = (sum[u << 1] + sum[u << 1 | 1]) % mod;     }     inline void pushdown(long long u, long long l, long long r) {       long long mid = (l + r) >> 1;       if (tg1[u]) {         tg1[u << 1] = tg1[u], tg2[u << 1] = 0;         sum[u << 1] = tg1[u] * (mid - l + 1) % mod;         tg1[u << 1 | 1] = tg1[u], tg2[u << 1 | 1] = 0;         sum[u << 1 | 1] = tg1[u] * (r - mid) % mod;         tg1[u] = 0;       }       if (tg2[u]) {         tg2[u << 1] = (tg2[u << 1] + tg2[u]) % mod;         sum[u << 1] = (sum[u << 1] + (mid - l + 1) * tg2[u]) % mod;         tg2[u << 1 | 1] = (tg2[u << 1 | 1] + tg2[u]) % mod;         sum[u << 1 | 1] = (sum[u << 1 | 1] + (r - mid) * tg2[u]) % mod;         tg2[u] = 0;       }     }     void Build(long long u, long long l, long long r) {       if (l == r) {         sum[u] = tg1[u] = tg2[u] = 0;         return;       }       long long mid = (l + r) >> 1;       Build(u << 1, l, mid);       Build(u << 1 | 1, mid + 1, r);       pushup(u);     }     void Add(long long u, long long l, long long r, long long L, long long R,              long long ad) {       if (r < L || R < l) return;       if (L <= l && r <= R) {         tg2[u] = (tg2[u] + ad) % mod;         sum[u] = (sum[u] + (r - l + 1) * ad) % mod;         return;       }       pushdown(u, l, r);       long long mid = (l + r) >> 1;       Add(u << 1, l, mid, L, R, ad);       Add(u << 1 | 1, mid + 1, r, L, R, ad);       pushup(u);     }     void Assign(long long u, long long l, long long r, long long L, long long R,                 long long as) {       if (r < L || R < l) return;       if (L <= l && r <= R) {         tg1[u] = as, tg2[u] = 0;         sum[u] = (r - l + 1) * as % mod;         return;       }       pushdown(u, l, r);       long long mid = (l + r) >> 1;       Assign(u << 1, l, mid, L, R, as);       Assign(u << 1 | 1, mid + 1, r, L, R, as);       pushup(u);     }     long long Query(long long u, long long l, long long r, long long L,                     long long R) {       if (r < L || R < l) return 0;       if (L <= l && r <= R) return sum[u];       pushdown(u, l, r);       long long mid = (l + r) >> 1, ans = 0;       ans = Query(u << 1, l, mid, L, R);       ans = (ans + Query(u << 1 | 1, mid + 1, r, L, R)) % mod;       pushup(u);       return ans;     }   } S1, S2;   signed main() {     long long n;     scanf( %lld , &n);     for (long long i = 1; i <= n; i++)       scanf( %lld %lld , &a[i].a, &a[i].b), a[i].ind = i;     sort(a + 1, a + n + 1);     long long T;     scanf( %lld , &T);     while (T--) {       long long x;       scanf( %lld , &x);       used[x] = true;     }     S1.Build(1, 1, n << 1), S2.Build(1, 1, n << 1);     st.insert(2 * n);     for (long long i = 1; i <= n; i++) {       long long pos = *(st.lower_bound(a[i].a));       long long s1 = S1.Query(1, 1, n << 1, pos, pos),                 s2 = s1 - S2.Query(1, 1, n << 1, pos, pos);       S1.Assign(1, 1, n << 1, a[i].a, a[i].a, (s1 * 2 + 1) % mod);       if (used[a[i].ind])         S2.Assign(1, 1, n << 1, a[i].a, a[i].a, (s1 - s2 + mod) % mod);       else         S2.Assign(1, 1, n << 1, a[i].a, a[i].a, (s1 * 2 + 1 - s2 + mod) % mod);       if (used[a[i].ind]) S2.Assign(1, 1, n << 1, 1, a[i].a - 1, mod - s2 - 1);       S1.Add(1, 1, n << 1, 1, a[i].a - 1, (s1 + 1) % mod);       S2.Add(1, 1, n << 1, 1, a[i].a - 1, (s1 + 1) % mod);       st.insert(a[i].a);     }     printf( %lld n ,            (S1.Query(1, 1, n << 1, 1, 1) - S2.Query(1, 1, n << 1, 1, 1) + mod) %                mod);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   class TaskD {    public:     void solve(std::istream &in, std::ostream &out) {       long long n;       in >> n;       vector<long long> a(n), b(n), sa(n), sb(n);       for (auto &i : a) in >> i;       for (auto &i : b) in >> i;       for (long long i = 0; i < n; ++i) {         sa[i] = a[i] - b[i];         sb[i] = b[i] - a[i];       }       sort((sa).begin(), (sa).end());       sort((sb).begin(), (sb).end());       long long ans = 0;       for (long long i = 0; i < n; ++i) {         ans += (long long)(sa.end() -                            lower_bound((sa).begin(), (sa).end(), sb[i] + 1));         if (sb[i] < 0) ans -= 1;       }       out << ans / 2 << endl;     }   };   signed main() {     TaskD solver;     std::istream &in(std::cin);     std::ostream &out(std::cout);     solver.solve(in, out);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[400][400];   int main() {     int n;     cin >> n;     long long k = n * n;     for (int j = 1; j <= n; j++) {       if (j % 2 != 0) {         for (int i = 1; i <= n; i++) {           a[i][j] = k--;         }       } else {         for (int i = n; i >= 1; i--) {           a[i][j] = k--;         }       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         cout << a[i][j] <<    ;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[20][110], b[20][110], c[20][110];   vector<int> res;   int main() {     int n, m, k;     scanf( %d%d%d , &n, &m, &k);     for (int i = 0; i < n; i++) {       char st[15];       scanf( %s , st);       for (int j = 0; j < m; j++) scanf( %d%d%d , &a[i][j], &b[i][j], &c[i][j]);     }     int ans = 0;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         if (i == j) continue;         res.clear();         for (int h = 0; h < m; h++)           for (int ik = 0; ik < c[i][h]; ik++) res.push_back(b[j][h] - a[i][h]);         sort(res.begin(), res.end());         reverse(res.begin(), res.end());         int v = 0;         for (int kp = 0; kp < k && kp < (int)res.size(); kp++)           v += max(res[kp], 0);         ans = max(ans, v);       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> sorted;   vector<set<int> > v;   set<int> solve(int lf, int rg) {     if (lf == rg) {       return v[lf];     }     int md = (lf + rg) >> 1;     set<int> x = solve(lf, md);     set<int> y = solve(md + 1, rg);     for (int val : y) {       v[md + 1].insert(val);     }     for (int val : x) {       v[md].insert(val);     }     if ((int)(x).size() < (int)(y).size()) {       for (int val : x) {         v[md + 1].insert(val);         y.insert(val);       }       return y;     } else {       for (int val : y) {         v[md].insert(val);         x.insert(val);       }       return x;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     vector<pair<int, int> > a(n);     for (int i = 0; i < n; ++i) {       int x, y;       cin >> x >> y;       sorted.push_back(x);       a[i] = {x, y};     }     sort((sorted).begin(), (sorted).end());     sorted.resize(unique((sorted).begin(), (sorted).end()) - sorted.begin());     v.resize((int)(sorted).size());     for (int i = 0; i < n; ++i) {       a[i].first = lower_bound((sorted).begin(), (sorted).end(), a[i].first) -                    sorted.begin();       v[a[i].first].insert(a[i].second);     }     solve(0, (int)(v).size() - 1);     vector<pair<int, int> > ans;     for (int i = 0; i < (int)(v).size(); ++i) {       for (int y : v[i]) {         ans.push_back({sorted[i], y});       }     }     cout << (int)(ans).size() <<   n ;     for (int i = 0; i < (int)(ans).size(); ++i) {       cout << ans[i].first <<     << ans[i].second <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 5;   const int N = 1e12;   string a[105];   int dp1[105], dp2[105], dp3[105];   int main() {     int n, m, i, j, k;     cin >> n >> m;     for (i = 0; i < n; i++) {       cin >> a[i];       dp1[i] = maxn;       dp2[i] = maxn;       dp3[i] = maxn;     }     for (i = 0; i < n; i++) {       for (j = 0; j < m; j++) {         if (a[i][j] >=  a  && a[i][j] <=  z )           dp1[i] = min(dp1[i], min(j, m - j));         else if (a[i][j] >=  0  && a[i][j] <=  9 )           dp2[i] = min(dp2[i], min(j, m - j));         else           dp3[i] = min(dp3[i], min(j, m - j));       }     }     int ans = INT_MAX;     for (i = 0; i < n; i++) {       for (j = 0; j < n; j++) {         for (k = 0; k < n; k++) {           if (i != j && j != k && i != k)             ans = min(ans, dp1[i] + dp2[j] + dp3[k]);         }       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   struct T {     int l, h, a;     void scan() { scanf( %d%d%d , &l, &h, &a); }   };   const int INF = 1e9;   T Map[6][6];   int F[6];   int minF, maxC;   int cost;   int N;   void check() {     for (int i = 1; i < N - 1; ++i) {       if (F[i] != 0) return;     }     if (F[N - 1] < minF || F[N - 1] == minF && cost > maxC) {       minF = F[N - 1];       maxC = cost;     }   }   void rec(int from, int to) {     if (F[0] < INF - minF) return;     if (from == N - 1 && to == N) {       check();     }     if (from - 1 > 0 && F[from - 1] > 0) return;     int nextF, nextT;     nextT = to == N - 1 ? from + 2 : to + 1;     nextF = to == N - 1 ? from + 1 : from;     for (int i = Map[from][to].l; i <= Map[from][to].h && i <= F[from]; ++i) {       int d;       if (i == 0)         d = 0;       else {         d = Map[from][to].a + i * i;       }       cost += d;       F[from] -= i;       F[to] += i;       rec(nextF, nextT);       cost -= d;       F[from] += i;       F[to] -= i;     }   }   int main() {     scanf( %d , &N);     F[0] = INF;     for (int i = 0; i < N * (N - 1) / 2; ++i) {       int from, to;       scanf( %d%d , &from, &to);       --from;       --to;       Map[from][to].scan();     }     minF = INF;     maxC = -1;     cost = 0;     rec(0, 1);     if (minF == INF) {       printf( -1 -1 );       return 0;     }     printf( %d %d , minF, maxC);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long A[1000000];   long long ncr(int n, int k) {     long long ans = 1;     k = k > n - k ? n - k : k;     int j = 1;     for (; j <= k; j++, n--) {       if (n % j == 0) {         ans *= n / j;       } else if (ans % j == 0) {         ans = ans / j * n;       } else {         ans = (ans * n) / j;       }     }     return ans;   }   int main() {     ios::sync_with_stdio(false);     long long n, m, t;     cin >> n >> m >> t;     long long sum = 0;     for (long long i = 4; i <= n; i++)       for (long long j = 1; j <= m; j++)         if (i + j == t) sum += ncr(n, i) * ncr(m, j);     cout << sum << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MX = 1000001;   const int MI = 100001;   const int MT = 10001;   int main() {     long long int n, ans = 0;     cin >> n;     ans = (n * (n + 1)) / 2;     n--;     for (int i = 0; i < n; ++i) {       int x;       scanf( %d , &x);       ans -= x;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   const long long inf = 1e15;   const int N = 1e6 + 6;   int n, q, b[N], t;   queue<int> even, odd, ans;   int main() {     scanf( %d%d , &n, &q);     for (int i = 1; i <= n; i++) {       if (i % 2)         odd.push(i);       else         even.push(i);     }     int len, Lipoter = 1, flag = 1;     for (int i = 1; i <= q; i++) {       scanf( %d , &t);       if (t == 1) {         scanf( %d , &len);         flag = (flag + len + n) % n;         if (flag == 0) flag = n;       } else {         if (flag % 2) {           if (Lipoter == 0) {             even.push(even.front());             even.pop();           } else             Lipoter = 0;         } else {           if (Lipoter == 1) {             odd.push(odd.front());             odd.pop();           } else             Lipoter = 1;         }         if (flag % 2)           flag++;         else           flag--;         if (flag == 0) flag = n;       }     }     if (Lipoter) {       while (!even.empty() && !odd.empty()) {         ans.push(odd.front());         odd.pop();         ans.push(even.front());         even.pop();       }     } else {       while (!even.empty() && !odd.empty()) {         ans.push(even.front());         even.pop();         ans.push(odd.front());         odd.pop();       }     }     while (1) {       if (ans.front() == 1)         break;       else {         ans.push(ans.front());         ans.pop();       }     }     while (flag <= n) {       b[flag++] = ans.front();       ans.pop();     }     int cnt = 1;     while (!ans.empty()) {       b[cnt++] = ans.front();       ans.pop();     }     for (int i = 1; i <= n; i++) printf( %d  , b[i]);     return 0;   }


#pragma GCC optimize( O3 )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx )       #include <iostream>   #include <cstdio>   #include <vector>   #include <set>   #include <map>   #include <string>   #include <cstring>   #include <cstdlib>   #include <algorithm>   #include <cassert>   #include <cmath>   #include <queue>   #include <ctime>   #include <unordered_map>   #include <unordered_set>   #include <sstream>       using namespace std;       #define REP(i, n) for(int (i) = 0; (i) < (n); (i)++)   #define SZ(a) (int)(a).size()   #define ALL(a) (a).begin(), (a).end()      const int N = 5005;   long long a[N], b[N], d[N];   short e[N][N];   vector<long long> c[2 * N];       int main() {    ios::sync_with_stdio(false);    cin.tie(nullptr);    int n;    cin >> n;    REP(i, n) cin >> a[i];    REP(i, n) cin >> b[i];    REP(i, n) {     int aidx = i, bidx = 0, cidx = i;     c[cidx].reserve(i + 2);     c[cidx].push_back(0);     while (aidx >= 0) {      e[aidx][bidx] = SZ(c[cidx]);      c[cidx].push_back(c[cidx].back() + a[aidx] * b[bidx]);      --aidx, ++bidx;     }    }    for (int i = 1; i < n; i++) {     int aidx = n - 1, bidx = i, cidx = n - 1 + i;     c[cidx].reserve(n - i + 1);     c[cidx].push_back(0);     while (bidx < n) {      e[aidx][bidx] = SZ(c[cidx]);      c[cidx].push_back(c[cidx].back() + a[aidx] * b[bidx]);      --aidx, ++bidx;     }    }    d[0] = 0;    for (int i = 1; i <= n; i++) {     d[i] = d[i - 1] + a[i - 1] * b[i - 1];    }    long long res = d[n];    REP(L, n) {     for (int R = L; R < n; R++) {      long long fs = d[n] - d[R + 1] + d[L];      int st = e[R][L], fn = e[L][R];      long long sc = c[L + R][fn] - c[L + R][st - 1];      res = max(res, fs + sc);     }    }    cout << res << endl;    return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( 3 ,  inline ,  Ofast )   using namespace std;   template <class T>   inline void read(T &x) {     x = 0;     T f = 1;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();     x *= f;   }   inline void Setfile(string Str =   ) {     if (Str ==   ) return;     freopen((Str +  .in ).c_str(),  r , stdin);     freopen((Str +  .out ).c_str(),  w , stdout);   }   const int maxn = 500005, mod = 998244353;   int n, f[maxn][2], pw2[maxn], sz[maxn], ans;   vector<int> G[maxn];   inline int Add(int x, int y) { return (x += y) >= mod ? x - mod : x; }   inline int Mul(int x, int y) { return 1ll * x * y % mod; }   inline void dfs(int u = 1, int p = 0) {     f[u][0] = sz[u] = 1;     for (int i = 0; i < (int)G[u].size(); ++i) {       int v = G[u][i];       if (v == p) continue;       dfs(v, u), sz[u] += sz[v],           f[u][1] = Add(Mul(f[u][1], Mul(2, Add(f[v][0], f[v][1]))),                         Mul(f[u][0], f[v][0])),           f[u][0] = Mul(f[u][0], Add(Mul(f[v][1], 2), f[v][0]));     }     ans = Add(ans, Mul(f[u][1], pw2[n - sz[u] + 1]));   }   int main() {     Setfile();     pw2[0] = 1;     for (int i = 1; i < maxn; ++i) pw2[i] = Add(pw2[i - 1], pw2[i - 1]);     read(n);     for (int i = 1, x, y; i < n; ++i)       read(x), read(y), G[x].push_back(y), G[y].push_back(x);     dfs(), printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     ios::sync_with_stdio(NULL);     cin.tie(0);     cout.tie(0);     int a, b, c;     cin >> a >> b >> c;     c -= 2;     b -= 1;     int ans = min({a, b, c});     if (ans < 0) {       cout << 0;     } else {       ans = ans * 3 + 3;       cout << ans;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   pair<int, int> _e[600005], ex[600005];   vector<int> ee[600005];   int nt, rr = 0, dn = 1, colnum, dfnn, _enum, nn, mm, n, m, ca, idnum, num, top,           hd[600005], dfn[600005], low[600005], instk[600005], a[600005],           b[600005], col[600005], dffn[600005], dep[600005], fa[600005][20],           lg[600005], vis[600005], stk[600005], id[600005];   struct enode {     int to, nxt;   } e[600005];   int rt(int element) {     element = (element + rr) % nt;     if (!element) element = nt;     return element;   }   void add(int u, int v) {     e[++dn] = (enode){v, hd[u]};     hd[u] = dn;   }   void tarjan(int x, int lst) {     int i, v;     instk[x] = 1, dfn[x] = low[x] = ++dfnn;     for (i = hd[x]; i; i = e[i].nxt) {       v = e[i].to;       if (i == lst || i == (lst ^ 1)) continue;       if (!dfn[v]) {         tarjan(v, i);         if (low[x] > low[v]) low[x] = low[v];       } else if (instk[v]) {         if (low[x] > dfn[v]) low[x] = dfn[v];       }     }     for (i = hd[x]; i; i = e[i].nxt) {       v = e[i].to;       if (i == lst || i == (lst ^ 1)) continue;       if (low[v] <= dfn[x]) {         ee[x].push_back(v);         ee[v].push_back(x);       } else         _e[++_enum] = make_pair(x, v);     }     instk[x] = 0;   }   void dfs(int x) {     col[x] = colnum;     for (auto v : ee[x])       if (!col[v]) dfs(v);   }   void dfs1(int x, int father) {     int i;     dffn[x] = ++dfnn;     dep[x] = dep[father] + 1;     fa[x][0] = father;     for (i = 1; i <= lg[dep[x]]; i++) fa[x][i] = fa[fa[x][i - 1]][i - 1];     for (i = hd[x]; i; i = e[i].nxt)       if (!dffn[e[i].to]) dfs1(e[i].to, x);   }   int getlca(int x, int y) {     int i;     if (dep[x] < dep[y]) swap(x, y);     for (i = lg[dep[x]]; i >= 0; i--)       if (dep[fa[x][i]] >= dep[y]) x = fa[x][i];     if (x == y) return x;     for (i = lg[dep[x]]; i >= 0; i--)       if (fa[x][i] != fa[y][i]) {         x = fa[x][i];         y = fa[y][i];       }     return fa[x][0];   }   void add2(int u, int v) {     add(u, v);     add(v, u);   }   void dfss(int x) {     vis[x] = 1;     for (auto v : ee[x])       if (!vis[v]) dfss(v);   }   void solve(int t) {     int i, flag = 1;     for (i = 1; i <= mm; i++) add2(ex[i].first, ex[i].second);     _enum = dfnn = 0;     tarjan(a[1], 0);     dfss(a[1]);     for (i = 1; i <= nn; i++)       if (!vis[a[i]]) flag = 0;     puts(flag ?  YES  :  NO );     if (flag) rr = (rr + t) % nt;     dn = 1;     for (i = 1; i <= idnum; i++) {       hd[id[i]] = vis[id[i]] = low[id[i]] = dfn[id[i]] = 0;       ee[id[i]].clear();     }   }   void build() {     int i, lca;     top = 0, idnum = 0;     for (i = 1; i <= num; i++) {       if (!top) {         stk[top = 1] = b[i];         id[++idnum] = b[i];         continue;       }       lca = getlca(stk[top], b[i]);       if (!lca) {         while (top > 1) {           add2(stk[top - 1], stk[top]);           top--;         }         stk[top = 1] = b[i];         id[++idnum] = b[i];         continue;       }       while (top > 1 && dep[stk[top - 1]] > dep[lca]) {         add2(stk[top - 1], stk[top]);         top--;       }       if (dep[stk[top]] > dep[lca]) add2(lca, stk[top--]);       if (stk[top] != lca) {         stk[++top] = lca;         id[++idnum] = lca;       }       stk[++top] = b[i];       id[++idnum] = b[i];     }     while (top > 1) {       add2(stk[top - 1], stk[top]);       top--;     }   }   int main() {     int i, u, v, t, xx, yy;     cin >> n >> m >> ca;     nt = n;     for (i = 1; i <= m; i++) {       cin >> u >> v;       if (u != v) add2(u, v);     }     for (i = 1; i <= n; i++)       if (!dfn[i]) tarjan(i, 0);     for (i = 1; i <= n; i++)       if (!col[i]) {         colnum++;         dfs(i);       }     dn = 1;     for (i = 1; i <= n; i++) hd[i] = 0;     for (i = 1; i <= _enum; i++) add2(col[_e[i].first], col[_e[i].second]);     n = colnum;     dep[0] = -1;     lg[1] = 0;     dfnn = 0;     for (i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1;     for (i = 1; i <= n; i++)       if (!dffn[i]) dfs1(i, 0);     dn = 1;     for (i = 1; i <= n; i++) {       hd[i] = dfn[i] = low[i] = 0;       ee[i].clear();     }     for (t = 1; t <= ca; t++) {       cin >> nn >> mm;       num = nn;       for (i = 1; i <= nn; i++) {         cin >> xx;         a[i] = rt(xx);       }       for (i = 1; i <= mm; i++) {         cin >> xx >> yy;         ex[i].first = rt(xx);         ex[i].second = rt(yy);       }       for (i = 1; i <= nn; i++) a[i] = b[i] = col[a[i]];       for (i = 1; i <= mm; i++) {         ex[i] = make_pair(col[ex[i].first], col[ex[i].second]);         b[++num] = ex[i].first;         b[++num] = ex[i].second;       }       sort(a + 1, a + nn + 1, [&](int x, int y) { return dffn[x] < dffn[y]; });       nn = unique(a + 1, a + nn + 1) - a - 1;       sort(b + 1, b + num + 1, [&](int x, int y) { return dffn[x] < dffn[y]; });       num = unique(b + 1, b + num + 1) - b - 1;       build();       solve(t);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int mod = 998244353;   const long long int mxn = 1e5 + 7;   long long int a[mxn];   long long int power(long long int a, long long int b);   long long int flcm(long long int n, long long int m);   long long int fgcd(long long int n, long long int m);   long long int modinv(long long int x);   long long int max_val(long long int start, long long int end) {     long long int pres_max, global_max;     for (long long int i = start; i <= end; i++) {       if (i == start) {         pres_max = a[i];         global_max = a[i];       } else {         pres_max = max(pres_max + a[i], a[i]);         global_max = max(global_max, pres_max);       }     }     return global_max;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int n, m, k, u, v, t = 1, temp, ans = 0, flag = 1, c = 0, min_el,                                  max_el, sum_el;     string s, s1, s2;     cin >> t;     while (t--) {       ans = 0;       c = 0;       flag = 1;       sum_el = 0;       cin >> n;       memset(a, 0, sizeof(a));       for (long long int i = 0; i < n; i++) cin >> a[i];       max_el = max(max_val(0, n - 2), max_val(1, n - 1));       if (max_el < max_val(0, n - 1))         cout <<  YES  << endl;       else         cout <<  NO  << endl;     }     return 0;   }   long long int power(long long int a, long long int b) {     long long int res = 1;     while (b) {       if (b % 2) {         res = (res * a) % mod;       }       a = (a * a) % mod;       b /= 2;     }     return res;   }   long long int flcm(long long int n, long long int m) {     long long int temp = fgcd(n, m);     return (n * m) / temp;   }   long long int fgcd(long long int n, long long int m) {     long long int temp;     while (m != 0) {       temp = n;       n = m;       m = temp % m;     }     return n;   }   long long int modinv(long long int x) { return power(x, mod - 2); }


#include <bits/stdc++.h>   using namespace std;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   const int N = 3123;   int n, m;   vector<tuple<int, int, int>> g[N];   vector<int> lpos, rpos;   bool vis[N];   bool dfs(int u, int l, int r) {     vis[u] = 1;     if (u == n) return 1;     bool ret = 0;     for (auto t : g[u]) {       int v, tl, tr;       tie(v, tl, tr) = t;       if (tl > l or tr < r or vis[v]) continue;       ret |= dfs(v, l, r);     }     return ret;   }   bool has_path(int l, int r) {     memset(vis, 0, sizeof vis);     return dfs(1, l, r);   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n >> m;     for (int i = 0; i < m; i++) {       int a, b, l, r;       cin >> a >> b >> l >> r;       g[a].emplace_back(b, l, r);       g[b].emplace_back(a, l, r);       lpos.emplace_back(l);       rpos.emplace_back(r);     }     sort(begin(lpos), end(lpos));     sort(begin(rpos), end(rpos));     lpos.resize(unique(begin(lpos), end(lpos)) - lpos.begin());     rpos.resize(unique(begin(rpos), end(rpos)) - rpos.begin());     int ans = 0;     for (int l : lpos) {       int lo = lower_bound(begin(rpos), end(rpos), l) - rpos.begin(),           hi = rpos.size();       while (lo < hi) {         int mi = (lo + hi) / 2;         if (has_path(l, rpos[mi]))           lo = mi + 1;         else           hi = mi;       }       if (lo - 1 >= 0) ans = max(ans, rpos[lo - 1] - l + 1);     }     if (ans == 0) {       cout <<  Nice work, Dima!  << endl;       return 0;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, sum = 0;     string s;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> s;       if (s ==  Tetrahedron ) {         sum += 4;       } else if (s ==  Cube ) {         sum += 6;       } else if (s ==  Octahedron ) {         sum += 8;       } else if (s ==  Dodecahedron ) {         sum += 12;       } else if (s ==  Icosahedron ) {         sum += 20;       }     }     cout << sum << endl;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> start_years;   vector<long long> start_numbers;   vector<long long> mods;   void init() {     long long start = 1989;     start_years.resize(10);     start_numbers.resize(10);     mods.resize(10);     start_years[0] = start;     start_numbers[0] = 9;     mods[0] = 10;     long long p = 10;     for (int i = 1; i < 10; ++i) {       start_years[i] = start_years[i - 1] + p;       p *= 10;       start_numbers[i] = start_years[i] % p;       mods[i] = p;     }   }   long long solve(int i, long long num) {     long long pos = num - start_numbers[i] + mods[i];     pos %= mods[i];     return start_years[i] + pos;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     init();     int n = 0;     cin >> n;     for (int i = 0; i < (n); ++i) {       string str;       cin >> str;       int len = str.size() - 4;       stringstream ss(str.substr(4, len));       long long num;       ss >> num;       cout << solve(len - 1, num) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using int64 = long long;   constexpr int64 P = 1000000007;   struct FiniteField {    private:     int64 x;       public:     FiniteField(int64 x) : x(x) {       if (x > P || x < 0) {         cerr <<  Invalied FiniteField!  << endl;         exit(1);       }     }     FiniteField() : x(0) {}     int64 Value() const { return x; }     inline FiniteField operator+(FiniteField o) const {       FiniteField r(*this);       r += o;       return r;     }     inline FiniteField operator-(FiniteField o) const {       FiniteField r(*this);       r -= o;       return r;     }     inline FiniteField operator*(FiniteField o) const {       FiniteField r(*this);       r *= o;       return r;     }     inline FiniteField operator/(FiniteField o) const {       FiniteField r(*this);       r /= o;       return r;     }     inline void operator+=(FiniteField o) { x = (x + o.x) % P; }     inline void operator-=(FiniteField o) { x = (x + P - o.x) % P; }     inline void operator*=(FiniteField o) { x = (x * o.x) % P; }     void operator/=(FiniteField o) {       int64 p = P - 2;       while (p) {         if (p % 2) {           *this *= o;         }         o *= o;         p /= 2;       }     }   };   template <typename T>   vector<vector<T>> operator*(const vector<vector<T>>& a1,                               const vector<vector<T>>& a2) {     int n = a1.size();     int m = a1[0].size();     if (a2.size() != m) {       cerr <<  Matrix dimensions don t match for multiplication!  << endl;       exit(1);     }     int l = a2[0].size();     vector<vector<T>> result(n, vector<T>(l));     for (int i = 0; i < n; i++) {       for (int j = 0; j < l; j++) {         for (int k = 0; k < m; k++) {           result[i][j] += a1[i][k] * a2[k][j];         }       }     }     return result;   }   template <typename T>   vector<T> operator*(const vector<vector<T>>& matrix, const vector<T>& v) {     int n = matrix.size();     int m = matrix[0].size();     if (v.size() != m) {       cerr <<  Matrix and vector dimensions don t match for multiplication!             << endl;       exit(1);     }     vector<T> result(n);     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         result[i] += matrix[i][j] * v[j];       }     }     return result;   }   template <typename T>   vector<vector<T>> Make2DVector(int d1, int d2, T default_value) {     return vector<vector<T>>(d1, vector<T>(d2, default_value));   }   template <typename T>   vector<vector<vector<T>>> Make3DVector(int d1, int d2, int d3,                                          T default_value) {     return vector<vector<vector<T>>>(         d1, vector<vector<T>>(d2, vector<T>(d3, default_value)));   }   constexpr int DEBUG = 0;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int64 n;     int d;     cin >> n >> d;     auto shifts = Make3DVector<FiniteField>(d, d + 1, d + 1, 0);     for (int k = 0; k < d; k++) {       for (int i = 0; i < d; i++) {         shifts[k][i][(i + k) % d] = 1;       }       shifts[k][d][d] = 1;     }     auto units = Make3DVector<FiniteField>(d, d + 1, d + 1, 0);     for (int k = 0; k < d; k++) {       for (int i = 0; i < d + 1; i++) {         units[k][i][i] = 1;       }       for (int i = 0; i < d + 1; i++) {         units[k][k][i] = 1;       }     }     auto identity = Make2DVector<FiniteField>(d + 1, d + 1, 0);     for (int i = 0; i < d + 1; i++) {       identity[i][i] = 1;     }     map<int64, vector<vector<FiniteField>>> cache;     function<vector<vector<FiniteField>>(int64)> find_matrix =         [&](int64 p) -> vector<vector<FiniteField>> {       if (cache.find(p) != cache.end()) {         return cache.find(p)->second;       }       if (p <= d) {         auto matrix = identity;         for (int k = 0; k < p; k++) {           matrix = units[k] * matrix;         }         return matrix;       }       int64 child_p = d;       while ((p + child_p - 1) / child_p > d) child_p *= d;       auto child = find_matrix(child_p);       auto matrix = child;       int64 remaining_p = p;       remaining_p -= child_p;       for (int k = 1; k < d; k++) {         if (remaining_p >= child_p) {           matrix = shifts[d - k] * child * shifts[k] * matrix;           remaining_p -= child_p;         } else if (remaining_p > 0) {           matrix = shifts[d - k] * find_matrix(remaining_p) * shifts[k] * matrix;           remaining_p = 0;         } else {           break;         }       }       cache[p] = matrix;       return matrix;     };     if (DEBUG) cerr <<  PHASE 1  << endl;     auto matrix = find_matrix(n);     if (DEBUG) cerr <<  PHASE 2  << endl;     vector<FiniteField> base_vector(d + 1);     base_vector[d] = 1;     auto final_vector = matrix * base_vector;     FiniteField ans = 0;     for (const auto& x : final_vector) ans += x;     cout << ans.Value() << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string com;     map<string, bool> mp;     int ans = 0;     while (getline(cin, com)) {       if (com[0] ==  + ) {         com.erase(com.begin(), com.begin() + 1);         mp[com] = 1;       } else if (com[0] ==  - ) {         com.erase(com.begin(), com.begin() + 1);         mp[com] = 0;       } else {         int len = com.size() - com.find( : ) - 1, cnt = 0;         for (map<string, bool>::iterator it = mp.begin(); it != mp.end(); it++) {           if (it->second) cnt++;         }         ans += cnt * len;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   namespace {   using namespace std;   template <typename T>   istream &operator>>(istream &is, vector<T> &v) {     for (T &t : v) is >> t;     return is;   }   template <typename T>   bool space(vector<T>) {     return false;   }   template <typename T>   bool space(T) {     return true;   }   bool space(char) { return false; }   template <typename T>   ostream &operator<<(ostream &os, const vector<T> &v) {     for (const T &t : v) {       os << t << (space(t) ?     :   );     }     os <<   n ;     return os;   }   long long gcd(long long a, long long b) {     while (b) {       long long mod = a % b;       a = b;       b = mod;     }     return a;   }   template <typename T>   void incr_sort(vector<T> &v) {     stable_sort(v.begin(), v.end());   }   template <typename T>   void decr_sort(vector<T> &v) {     stable_sort(v.rbegin(), v.rend());   }   long long sum(const vector<int> &v) {     long long s = 0;     for (long long value : v) s += value;     return s;   }   vector<long long> prefix_sum(const vector<int> &v) {     vector<long long> res(v.size());     res[0] = v[0];     for (int i = 1; i < v.size(); ++i) res[i] = res[i - 1] + v[i];     return res;   }   }  // namespace   void solve();   int main() {     cin.tie(nullptr);     cout.tie(nullptr);     ios::sync_with_stdio(false);     int t = 1;     cin >> t;     for (int i = 0; i < t; ++i) solve();     return 0;   }   void solve() {     int h, n;     cin >> h >> n;     vector<int> p(n);     cin >> p;     p.push_back(0);     long long ans = 0;     int i = 0;     while (p[i] > 2) {       if (p[i + 1] < p[i] - 1) p[i] = p[i + 1] + 1;       if (p[i] <= 2) break;       if (p[i + 2] == p[i + 1] - 1)         i += 2;       else {         ++ans;         p[i + 1] = p[i + 2] + 1;         i++;       }     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, v, k, cnt;   signed main() {     scanf( %lld%lld%lld , &n, &m, &v), k = (v == 1) ? 2 : 1;     if (m < n - 1 || m > n - 1 + (n - 2) * (n - 3) / 2) puts( -1 ), exit(0);     for (long long i = 1; i <= n; i++)       if (i != v) printf( %lld %lld n , i, v);     cnt = n - 1;     for (long long i = 1; i <= n; i++) {       if (i == v || i == k) continue;       for (long long j = i + 1; j <= n; j++) {         if (j == v || j == k) continue;         if (cnt + 1 <= m) cnt++, printf( %lld %lld n , i, j);         if (cnt == m) break;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long fac(long long n) {     long long m = 1;     for (long long i = 1; i <= n; i++) {       m = m * i;     }     return m;   }   long long gcd(long long x, long long y) {     if (x > y) swap(x, y);     long long md;     while (1) {       md = y % x;       if (md == 0) break;       y = x;       x = md;     }     return x;   }   long long nCr(long long n, long long r) {     long long rs = (fac(n)) / (fac(r) * fac(n - r));     return rs;   }   long long nPr(long long n, long long r) {     long long rs = fac(n) / fac(n - r);     return rs;   }   bool flag, fl;   long long i, j;   long long pw(long long n, long long p) {     long long mn = 1;     if (p == 0) return 1;     for (long long z = 0; z < p; z++) {       mn *= n;     }     return mn;   }   int main() {     long long n;     scanf( %lld , &n);     long long num = 0;     vector<int> bn;     long long rsl;     for (i = 1; i <= 1024; i++) {       bn.clear();       j = i;       num = 0;       while (j > 0) {         bn.push_back(j % 2);         j = j / 2;       }       long long ln = bn.size();       for (j = 0; j < ln; j++) {         num += (bn[j] * pw(10, j));       }       if (num <= n) {         rsl = i;       } else         break;     }     printf( %lld n , rsl);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = int(1e6) + 10;   int n;   char buffer[MAX_N];   int main() {     gets(buffer);     n = strlen(buffer);     reverse(buffer, buffer + n);     vector<pair<char, int> > v;     for (int i = 0, j, l = -1; i < n; i = j) {       for (j = i; j < n && buffer[j] ==  1 ; j++)         ;       if (j > i + 1) {         if (l == i) {           v.pop_back();         }         v.push_back(make_pair( - , i));         v.push_back(make_pair( + , j));         buffer[l = j--] =  1 ;       } else if (j == i + 1 && l != i) {         v.push_back(make_pair( + , i));       }       j++;     }     cout << (int)v.size() << endl;     for (__typeof((v).begin()) e = (v).begin(); e != (v).end(); ++e) {       printf( %c2^%d n , e->first, e->second);     }   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int N;     cin >> N;     string s1, s2;     cin >> s1 >> s2;     int num_different = 0;     vector<pair<bool, int>> differences;     int num_s1_has_a = 0;     int num_s1_has_b = 0;     for (int i = 0; i < N; ++i) {       if (s1[i] != s2[i]) {         bool s1_has_a = (s1[i] ==  a );         if (s1_has_a) {           ++num_s1_has_a;         } else {           ++num_s1_has_b;         }         differences.push_back({s1_has_a, i});       }     }     if ((num_s1_has_a + num_s1_has_b) % 2 != 0) {       cout << -1 << endl;       return;     } else {       sort(differences.begin(), differences.end());       int answer = (num_s1_has_a + num_s1_has_b) / 2;       if (num_s1_has_a % 2 != 0) {         ++answer;       }       cout << answer << endl;       for (int i = 0; i < differences.size(); i += 2) {         if (differences[i].first != differences[i + 1].first) {           cout << differences[i].second + 1 <<     << differences[i].second + 1                << endl;         }         cout << differences[i].second + 1 <<     << differences[i + 1].second + 1              << endl;       }     }   }   int main() { solve(); }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e9 + 7;   int main() {     ios::sync_with_stdio(0);     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, m;     cin >> n >> m;     vector<pair<int, int> > ans;     if (n == 1) {       if (m == 1) {         printf( 1 1 );         return 0;       }       for (int j = 0; j < m / 2; j++) {         printf( 1 %d n , j + 1);         printf( 1 %d n , m - (j + 1) + 1);       }       if (m % 2 != 0) {         printf( 1 %d , m / 2 + 1);       }       return 0;     }     for (int i = 0; i < n / 2; i++) {       for (int j = 0; j < m; j++) {         printf( %d %d n , i + 1, j + 1);         printf( %d %d n , n - i, m - j);       }     }     if (n % 2 != 0) {       if (m == 1) {         printf( %d %d n , n / 2 + 1, 1);         return 0;       }       for (int j = 0; j < m / 2; j++) {         printf( %d %d n , n / 2 + 1, j + 1);         printf( %d %d n , n / 2 + 1, m - j);       }       if (m % 2 != 0) {         printf( %d %d n , n / 2 + 1, m / 2 + 1);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct __s {     __s() {       srand(time(NULL));       if (1) {         ios_base::Init i;         cin.sync_with_stdio(0);         cin.tie(0);       }     }     ~__s() {       if (!1)         fprintf(stderr,  Execution time: %.3lf s. n ,                 (double)clock() / CLOCKS_PER_SEC);       int n;       cin >> n;     }   } __S;   struct vertex {     int go[26];     int link;     int ans;     int p;     int pch;     bool root;     bool isAns;     vertex(int p, int pch, bool root) : p(p), pch(pch), root(root) {       memset(go, 255, sizeof(go));       link = -1;       ans = 0;       isAns = false;     };   };   vector<vertex> t;   void add(string &s, int v) {     for (int i = 0; i < (int)(s.size()); i++) {       int c = s[i] -  a ;       if (t[v].go[c] == -1) {         t[v].go[c] = t.size();         t.push_back(vertex(v, c, false));       }       v = t[v].go[c];     }     t[v].ans++;   }   int getGo(int v, int c);   int getLink(int v) {     if (t[v].link == -1) {       if (t[t[v].p].root)         return t[v].link = t[v].p;       else         return t[v].link = getGo(getLink(t[v].p), t[v].pch);     }     return t[v].link;   }   int getGo(int v, int c) {     if (t[v].go[c] == -1) {       if (t[v].root)         return t[v].go[c] = v;       else         return t[v].go[c] = getGo(getLink(v), c);     }     return t[v].go[c];   }   int getAns(int v) {     if (t[v].isAns) return t[v].ans;     t[v].isAns = true;     return t[v].ans += getAns(getLink(v));   }   int getAns(string &s, int v) {     int res = 0;     for (int i = 0; i < (int)(s.size()); i++) {       int c = s[i] -  a ;       v = getGo(v, c);       res += getAns(v);     }     return res;   }   int n[2];   int root[2][20];   queue<string> p[2][20];   queue<string> q[2];   void build() {     for (int i = 0; i < (int)(2); i++) {       int N = n[i] + q[i].size();       for (int j = 0; j < (int)(20); j++) {         bool a = ((n[i] >> j) & 1);         bool b = ((N >> j) & 1);         if (a == b) continue;         if (a) {           root[i][j] = 0;           while (p[i][j].size()) {             q[i].push(p[i][j].front());             p[i][j].pop();           }           continue;         }         root[i][j] = t.size();         t.push_back(vertex(root[i][j], 0, true));         for (int k = 0; k < (int)(1LL << j); k++) {           add(q[i].front(), root[i][j]);           p[i][j].push(q[i].front());           q[i].pop();         }       }       n[i] = N;     }   }   int get(string &s, int i) {     int res = 0;     for (int j = 0; j < (int)(20); j++) {       if (root[i][j] == 0) continue;       res += getAns(s, root[i][j]);     }     return res;   }   int get(string &s) { return get(s, 0) - get(s, 1); }   int main(void) {     t.push_back(vertex(0, 0, false));     int n;     cin >> n;     for (int _ = 0; _ < (int)(n); _++) {       int t;       string s;       cin >> t >> s;       if (t == 1 || t == 2) {         q[t - 1].push(s);       } else {         build();         cout << get(s) <<   n ;         cout.flush();       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int _ = 2e5 + 7;   const int __ = 8e5 + 7;   const long long inf = 1e17;   int n, m, q;   long long wgt[2][_], cst[_], minx[__], tag[__];   struct edge {     int x, y;     long long w;   } e[_];   void build(int k, int l, int r, bool id) {     tag[k] = 0;     if (l == r) {       if (!id) {         minx[k] = wgt[0][l] + cst[l];       } else         minx[k] = wgt[1][l];       return;     }     int mid = (l + r) >> 1;     build(k << 1, l, mid, id);     build(k << 1 | 1, mid + 1, r, id);     minx[k] = min(minx[k << 1], minx[k << 1 | 1]);   }   bool rule_x(edge a, edge b) { return a.x < b.x; }   void init() {     cin >> n >> m >> q;     n--;     for (int i = 1; i <= n; i++) scanf( %lld%lld , &wgt[0][i], &wgt[1][i]);     for (int i = 1; i <= m; i++) scanf( %d%d%lld , &e[i].x, &e[i].y, &e[i].w);     sort(e + 1, e + 1 + m, rule_x);     build(1, 0, n, 1);   }   void upd(int k, long long w) {     minx[k] += w;     tag[k] += w;   }   void psd(int k) {     if (!tag[k]) return;     upd(k << 1, tag[k]);     upd(k << 1 | 1, tag[k]);     tag[k] = 0;   }   void modify(int k, int l, int r, int x, int y, long long w) {     if (l >= x && r <= y) {       upd(k, w);       return;     }     psd(k);     int mid = (l + r) >> 1;     if (x <= mid) modify(k << 1, l, mid, x, y, w);     if (y > mid) modify(k << 1 | 1, mid + 1, r, x, y, w);     minx[k] = min(minx[k << 1], minx[k << 1 | 1]);   }   long long query(int k, int l, int r, int x, int y) {     if (l >= x && r <= y) return minx[k];     psd(k);     int mid = (l + r) >> 1;     long long t1 = inf, t2 = inf;     if (x <= mid) t1 = query(k << 1, l, mid, x, y);     if (y > mid) t2 = query(k << 1 | 1, mid + 1, r, x, y);     return min(t1, t2);   }   void pre() {     int p = 1;     for (int i = 1; i <= n + 1; i++) {       while (p <= m && e[p].x <= i) {         modify(1, 0, n, 0, e[p].y - 1, e[p].w);         p++;       }       if (i == n + 1)         cst[0] = query(1, 0, n, 0, n);       else         cst[i] = query(1, 0, n, 0, n);     }     build(1, 0, n, 0);   }   void run() {     printf( %lld n , query(1, 0, n, 0, n));     int x;     long long w;     for (int i = 1; i <= q; i++) {       scanf( %d%lld , &x, &w);       modify(1, 0, n, x, x, -wgt[0][x] + w);       wgt[0][x] = w;       printf( %lld n , query(1, 0, n, 0, n));     }   }   int main() {     init();     pre();     run();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline void file_handling() {     ios_base::sync_with_stdio(0);     cin.tie(0);   }   void solve() {     string dish;     cin >> dish;     long long b, c, s;     b = s = c = 0;     for (long long i = 0; i < dish.size(); ++i)       if (dish[i] ==  B )         b++;       else if (dish[i] ==  S )         s++;       else         c++;     long long nb, nc, ns;     cin >> nb >> ns >> nc;     long long pb, pc, ps;     cin >> pb >> ps >> pc;     long long rubles;     cin >> rubles;     long long left = 0;     long long right = 1e15;     long long ans = 0;     while (left <= right) {       long long mid = (right + left) / 2;       long long cost = 0;       if (mid * b >= nb) cost += (mid * b - nb) * pb;       if (mid * c >= nc) cost += (mid * c - nc) * pc;       if (mid * s >= ns) cost += (mid * s - ns) * ps;       if (cost <= rubles) {         ans = mid;         left = mid + 1;       } else         right = mid - 1;     }     cout << ans <<   n ;   }   signed main() {     file_handling();     long long testCases = 1;     while (testCases--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int mxn = 0;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int t = 1;     while (t--) {       long long int i, j, k, n, m, x;       cin >> n >> x >> k;       vector<long long int> a(n);       for (i = 0; i < n; i++) cin >> a[i];       sort(a.begin(), a.end());       long long int ans = 0;       for (i = 0; i < n; i++) {         long long int l = a[i] / x;         if (a[i] % x) {           ans += lower_bound(a.begin(), a.end(), x * (l + k + 1)) -                  lower_bound(a.begin(), a.end(), max((l + k) * x, a[i]));         } else {           ans += lower_bound(a.begin(), a.end(), (l + k) * x) -                  lower_bound(a.begin(), a.end(), max((l + k - 1) * x, a[i]));         }       }       cout << ans;     }     return 0;   }


#include<bits/stdc++.h>   using namespace std;   const int maxn=1e5+10;   int a[maxn],b[maxn],c[maxn];   vector<int> v1[maxn],v2[maxn],app[maxn];   int ans[maxn];   int main()   {    int t;    cin>>t;    while(t--)    {     memset(ans,-1,sizeof(ans));     int n,m;     cin>>n>>m;     for(int i=1;i<=n;i++)     {      v1[i].clear();      v2[i].clear();      app[i].clear();      cin>>a[i];     }     for(int i=1;i<=n;i++)     {      cin>>b[i];      if(a[i]!=b[i])      {       v1[b[i]].push_back(i);      }      else      {       app[a[i]].push_back(i);      }     }     for(int i=1;i<=m;i++)     {      cin>>c[i];      v2[c[i]].push_back(i);     }     bool flag=true;     for(int i=1;i<=n;i++)     {      if((int)v1[i].size()>(int)v2[i].size())      {       puts( NO );       flag=false;       break;      }      int pp=-1;      if(!v1[i].empty())      {       pp=v1[i].back();      }      for(int j=0;j<v2[i].size();j++)      {       if(v1[i].empty())       {        if(pp!=-1)        {         ans[v2[i][j]]=pp;        }        else if(!app[i].empty())        {         ans[v2[i][j]]=app[i].back();        }       }       else       {        ans[v2[i][j]]=v1[i].back();        v1[i].pop_back();       }      }     }     if(!flag)     {      continue;     }     if(ans[m]==-1)     {      puts( NO );      continue;     }     puts( YES );     for(int i=1;i<=m;i++)     {      if(ans[i]==-1)      {       ans[i]=ans[m];      }      cout<<ans[i]<<   ;     }     cout<<endl;    }    return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, step, smallest, count = 0;     bool possible = true;     cin >> n >> step;     vector<long long> p(n);     cin >> p[0];     smallest = p[0];     for (int i = 1; i < n; i++) {       cin >> p[i];       if (p[i] < smallest) {         smallest = p[i];       }     }     for (int i = 0; i < n; i++) {       if ((p[i] - smallest) % step != 0) {         cout << -1 << endl;         possible = false;         break;       } else {         count += (p[i] - smallest) / step;       }     }     if (possible == true) {       cout << count << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int dx[] = {0, 1, 0, -1};   const int dy[] = {1, 0, -1, 0};   inline int read() {     int x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   inline long long read1() {     long long x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   const int N = 200010;   const int P = 1e9 + 7;   int dp[110][110][110];   int pw[110];   int Add(int a, int b) {     a += b;     return a >= P ? a - P : a;   }   char ch[110];   int main() {     int n, x;     n = read();     x = read();     scanf( %s , ch + 1);     for (int i = 1; i <= n; i++) dp[ch[i] -  0 ][i][i] = 1;     pw[0] = pw[1] = 2;     for (int i = 2; i <= x; i++) {       pw[i] = (long long)(pw[i - 1]) * pw[i - 2] % P;       for (int l = 1; l <= n; l++)         for (int r = l; r <= n; r++) {           dp[i][l][r] =               Add((long long)(dp[i - 1][l][r]) * (r == n ? pw[i - 2] : 1) % P,                   (long long)(dp[i - 2][l][r]) * (l == 1 ? pw[i - 1] : 1) % P);           for (int m = l; m <= r - 1; m++)             dp[i][l][r] = Add(dp[i][l][r], (long long)(dp[i - 1][l][m]) *                                                dp[i - 2][m + 1][r] % P);         }     }     cout << dp[x][1][n] << endl;     return 0;   }


#include <bits/stdc++.h>   int n, sum, ans[6050];   bool isprime(int x) {     if (x < 2 || (x & 1) == 0) {       return 0;     }     for (int i = 2; i * i <= x; ++i) {       if (x % i == 0) {         return 0;       }     }     return 1;   }   int main() {     scanf( %d , &n);     sum = n * (n + 1) / 2;     if (isprime(sum)) {       for (int i = 1; i <= n; ++i) {         ans[i] = 1;       }     } else if ((sum & 1) == 0) {       for (int i = 3; i <= n; ++i) {         if (isprime(i) && isprime(sum - i)) {           ans[i] = 2;           break;         }       }       for (int i = 1; i <= n; ++i) {         if (!ans[i]) {           ans[i] = 1;         }       }     } else {       if (isprime(sum - 2)) {         for (int i = 1; i <= n; ++i) {           ans[i] = 1;         }         ans[2] = 2;       } else {         sum -= 3;         ans[3] = 3;         for (int i = 5; i <= n; ++i) {           if (isprime(i) && isprime(sum - i)) {             ans[i] = 2;             break;           }         }         for (int i = 1; i <= n; ++i) {           if (!ans[i]) {             ans[i] = 1;           }         }       }     }     for (int i = 1; i <= n; ++i) {       printf( %d  , ans[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s[2005];   char str[2005][2005];   int a[2005][2005];   int v[2005][2005];   long long dp[8005][2005];   int pool[8005], top = 0;   int child[8005];   int ID = 0, n;   int getID() { return pool[--top]; }   void pre() {     for (int i = 8000; i >= 0; i--) pool[top++] = i;   }   void delID(int x) { pool[top++] = x; }   int K;   int combine(int me, int you, int D) {     int ret = getID();     child[ret] = min(K, child[me] + child[you]);     for (int i = 0; i <= child[ret]; i++) dp[ret][i] = 0;     for (int i = 0; i <= child[me]; i++)       for (int j = 0; j <= child[you] && i + j <= K; j++)         dp[ret][i + j] = max(dp[ret][i + j], dp[me][i] + dp[you][j] + i * j * D);     return ret;   }   int go(int L, int R, int D) {     int me = getID();     int c = 0;     while (L <= R && str[L][D] ==   0 ) ++c, ++L;     child[me] = c;     for (int i = 0; i <= c; i++) dp[me][i] = i * (i - 1) / 2 * D;     for (int i = L, j = L; i <= R; i = j) {       for (j = i; j <= R && str[i][D] == str[j][D]; j++)         ;       int you = go(i, j - 1, D + 1);       int nxt = combine(me, you, D);       delID(me);       delID(you);       me = nxt;     }     return me;   }   int main(void) {     pre();     scanf( %d%d , &n, &K);     for (int i = 1; i <= n; i++) cin >> s[i];     sort(s + 1, s + 1 + n);     for (int i = 1; i <= n; i++) strcpy(str[i], s[i].c_str());     int me = go(1, n, 0);     printf( %I64d n , dp[me][K]);     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:256000000 )   using namespace std;   const int maxN = 110;   int n, k, a[maxN];   void solve() {     vector<vector<double> > d[2];     int u = 0, v = 1;     d[u].assign(maxN, vector<double>(maxN, 0.0));     for (int i = 1; i <= n; ++i) {       for (int j = i + 1; j <= n; ++j) {         if (a[i] < a[j]) {           d[u][i][j] = 1.0;         } else {           d[u][j][i] = 1.0;         }       }     }     double p = 2.0 / (double)(n * (n + 1));     for (int steps = 0; steps < k; ++steps, swap(u, v)) {       d[v].assign(maxN, vector<double>(maxN, 0.0));       for (int i = 1; i <= n; ++i) {         for (int j = 1; j <= n; ++j) {           if (i == j) {             continue;           }           for (int a = 1; a <= n; ++a) {             for (int b = a; b <= n; ++b) {               int ni = i;               if (a <= i && i <= b) {                 ni = a + b - i;               }               int nj = j;               if (a <= j && j <= b) {                 nj = a + b - j;               }               d[v][ni][nj] += p * d[u][i][j];             }           }         }       }     }     double res = 0.0;     for (int i = 1; i <= n; ++i) {       for (int j = 1; j < i; ++j) {         res += d[u][i][j];       }     }     printf( %.10lf n , res);   }   int main() {     cin >> n >> k;     for (int i = 1; i <= n; ++i) {       cin >> a[i];     }     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int q[100010], t, n;   bool cmp(int a, int b) { return a < b; }   int main() {     scanf( %d , &t);     for (int i = 1; i <= t; i++) {       scanf( %d , &n);       for (int j = 1; j <= n; j++) {         scanf( %d , &q[j]);       }       int flag = 1;       sort(q + 1, q + n + 1, cmp);       for (int k = n - 2; k >= 1; k--) {         if (q[n - 1] >= k + 1) {           printf( %d n , k);           flag = 0;           break;         }       }       if (flag) printf( 0 n );     }   }


#include <bits/stdc++.h>   int main() {     long int n, m;     int t;     scanf( %d , &t);     while (t--) {       scanf( %ld%ld , &n, &m);       if (n == 1)         printf( 0 n );       else if (n == 2) {         printf( %ld n , m);       } else {         printf( %ld n , 2 * m);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   struct particle {     int x, v;   } particles[500002];   bool possible(long double time) {     long double maxRightyX = -1e19;     for (int i = 1; i <= n; i++) {       if (particles[i].v > 0) {         maxRightyX = max(particles[i].x + particles[i].v * time, maxRightyX);       } else if (maxRightyX >= particles[i].x + particles[i].v * time) {         return true;       }     }     return false;   }   void bin() {     long double l = 0, r = 1000000000;     while (l + 0.0000000001 < r) {       long double mid = (l + r) / 2;       if (possible(mid))         r = mid;       else         l = mid + 0.0000000001;     }     cout << fixed << setprecision(10);     if (possible(l)) {       cout << l;     } else if (possible(r)) {       cout << r;     } else {       cout <<  -1 ;     }     cout <<   n ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (int i = 1; i <= n; i++) cin >> particles[i].x >> particles[i].v;     bin();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<vector<int>> adj;   vector<int> a;   vector<int> dep;   vector<int> ret;   unordered_map<int, vector<int>> pmp;   unordered_map<int, vector<int>> mp;   vector<int> primeFactors(int num) {     vector<int> ret;     for (int i = 2; i * i <= num; i++) {       while (num % i == 0) {         ret.push_back(i);         num /= i;       }     }     if (num != 1) ret.push_back(num);     return ret;   }   void dfs(int idx, int prev, int level) {     dep[idx] = level;     for (int i : pmp[a[idx]]) {       if (mp.count(i) && !mp[i].empty()) {         if (ret[idx] == -1 || dep[ret[idx]] < dep[mp[i].back()])           ret[idx] = mp[i].back();       }     }     for (int i : pmp[a[idx]]) {       mp[i].push_back(idx);     }     for (auto nidx : adj[idx]) {       if (nidx == prev) continue;       dfs(nidx, idx, level + 1);     }     for (int i : pmp[a[idx]]) {       mp[i].pop_back();     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int n, q;     cin >> n >> q;     a = vector<int>(n + 1, 0);     dep = vector<int>(n + 1, 0);     ret = vector<int>(n + 1, -1);     for (int i = 1; i <= n; i++) cin >> a[i];     adj = vector<vector<int>>(n + 1);     for (int i = 0; i < n - 1; i++) {       int x, y;       cin >> x >> y;       adj[x].push_back(y);       adj[y].push_back(x);     }     for (int i = 1; i <= n; i++) {       if (pmp.count(a[i])) continue;       pmp[a[i]] = primeFactors(a[i]);     }     dfs(1, 0, 1);     while (q--) {       int type, v, w;       cin >> type;       if (type == 1) {         cin >> v;         cout << ret[v] << endl;       } else {         cin >> v >> w;         a[v] = w;         if (pmp.count(a[v])) continue;         pmp[a[v]] = primeFactors(a[v]);         ret = vector<int>(n + 1, -1);         dfs(1, 0, 1);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool prime(int x) {     if (x % 2 == 0) return 0;     for (size_t i = 3; i * i <= x; i += 2) {       if (x % i == 0) return 0;     }     return 1;   }   void solve() {     int n, x = 3;     cin >> n;     while (n) {       if (prime(x)) {         cout << x <<    ;         n--;       }       x += 2;     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     solve();   }


#include <bits/stdc++.h>   using namespace std;   const int N = 210000;   int n, root, opt, Max[N << 6], lson[N << 6], rson[N << 6], lazy[N << 6];   void pushdown(int k, int l, int r) {     if (!lazy[k]) return;     if (l != r) {       if (!lson[k]) lson[k] = ++opt;       if (!rson[k]) rson[k] = ++opt;       lazy[lson[k]] += lazy[k];       lazy[rson[k]] += lazy[k];       Max[lson[k]] += lazy[k];       Max[rson[k]] += lazy[k];     }     lazy[k] = 0;   }   void insert(int &k, int l, int r, int L, int R) {     if (l > R || r < L) return;     if (!k) k = ++opt;     if (l >= L && r <= R) return (void)(Max[k]++, lazy[k]++);     pushdown(k, l, r);     int mid = l + r >> 1;     if (L <= mid) insert(lson[k], l, mid, L, R);     if (R > mid) insert(rson[k], mid + 1, r, L, R);     Max[k] = max(Max[lson[k]], Max[rson[k]]);   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       int x, y;       scanf( %d%d , &x, &y);       insert(root, 0, 1000000000, x, y);       if (Max[1] > 2) {         puts( NO );         return 0;       }     }     puts( YES );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 6e5;   const int oo = 1e9 + 5;   int du[] = {-1, 0, 0, 1};   int dv[] = {0, -1, 1, 0};   const long long mod = 1e9 + 7;   long long sqr(long long x) { return x * x; }   int n, x, a[N + 5], ans;   bool vis[N + 5];   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> x;     vis[x] = true;     for (int i = 1; i < (1 << n); i++) {       if (!vis[i]) a[ans++] = i;       vis[i ^ x] = vis[i] = true;     }     cout << ans << endl;     for (int i = 1; i <= ans; i++) cout << (a[i] ^ a[i - 1]) <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 5 * 100 * 1000 + 10;   int arr[MAXN], nx[MAXN], bf[MAXN];   stack<int> st;   pair<int, int> pr[MAXN];   int main() {     int n, m, p;     cin >> n >> m >> p;     string second, w;     cin >> second >> w;     second =     + second;     int cntr = 1;     for (int i = 1; i < second.size(); i++) {       if (second[i] ==  ( ) {         arr[i] = cntr;         pr[cntr].first = i;         st.push(cntr);         cntr++;       } else {         arr[i] = st.top();         pr[arr[i]].second = i;         st.pop();       }       nx[i] = i + 1;       bf[i] = i - 1;     }     nx[0] = 1;     for (int i = 0; i < w.size(); i++) {       if (w[i] ==  R )         p = nx[p];       else if (w[i] ==  L )         p = bf[p];       else {         int sr = pr[arr[p]].first;         int er = pr[arr[p]].second;         nx[bf[sr]] = nx[er];         bf[nx[er]] = bf[sr];         if (nx[er] < second.size())           p = nx[er];         else           p = bf[sr];       }     }     int i = nx[0];     while (i < second.size()) {       cout << second[i];       i = nx[i];     }     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   int l, r, a, b, dif, t, x, lit, poz;   bool uz[27];   char s[1001];   int main() {     cin >> a >> b >> l >> r;     dif = min(500, r - l);     l = l % (a + b);     if (l == 0) l = a + b;     r = l + dif;     if (l <= a) {       for (int i = l; i <= min(a, r); i++) s[++poz] = i;       l = a + 1;       t = 1;     } else {       for (int i = l; i <= min(a + b, r); i++) s[++poz] = 1;       l = a + b + 1;       memset(uz, 0, sizeof(uz));       for (int i = poz; i >= max(1, poz - a + 1); i--) uz[s[i]] = 1;       lit = 1;       t = 2;     }     x = 0;     while (l <= r) {       if (t == 1) {         x++;         s[++poz] = s[poz - 1];         l++;         if (x == b) {           memset(uz, 0, sizeof(uz));           for (int i = poz; i >= max(1, poz - a + 1); i--) uz[s[i]] = 1;           lit = 1;           t = 2;           x = 0;         }       } else {         x++;         while (uz[lit] == 1) lit++;         s[++poz] = lit;         l++;         uz[lit] = 1;         if (x == a) {           t = 1;           x = 0;         }       }     }     memset(uz, 0, sizeof(uz));     for (int i = 1; i <= poz; i++) {       uz[s[i]] = 1;     }     int nr = 0;     for (int i = 1; i <= 26; i++) nr += uz[i];     cout << nr;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long pwr(long long base, long long p, long long mod = (1000000007LL)) {     long long ans = 1;     while (p) {       if (p & 1) ans = (ans * base) % mod;       base = (base * base) % mod;       p /= 2;     }     return ans;   }   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     string a, b;     cin >> a >> b;     int cnt1 = 0;     for (int i = 0; i < (int)a.length(); i++) cnt1 += a[i] -  0 ;     int cnt2 = 0;     for (int i = 0; i < (int)b.length(); i++) cnt2 += b[i] -  0 ;     if (cnt1 + (cnt1 & 1) >= cnt2)       cout <<  YES ;     else       cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long iinf = 1e9 + 10;   const long long inf = 1ll << 60;   const long long MOD = 998244353;   void GG() {     cout <<  0 n ;     exit(0);   }   const long long maxn = 1e5 + 5;   vector<pair<long long, long long> > here[maxn];   template <long long mod>   struct Modint {     long long val;     Modint(long long _val = 0) { val = _val; }     operator long long() const { return (*this).val; }     Modint operator+(const Modint& o) const {       return val + o.val >= mod ? Modint(val + o.val - mod) : Modint(val + o.val);     }     Modint operator-() const { return val ? Modint(mod - val) : *this; }     Modint operator-(const Modint& o) const {       return val - o.val < 0 ? Modint(val - o.val + mod) : Modint(val - o.val);     }     Modint operator*(const Modint& o) const { return Modint(val * o.val % mod); }     Modint operator+=(const Modint& o) {       *this = (*this) + o;       return *this;     }     Modint operator-=(const Modint& o) {       *this = (*this) - o;       return *this;     }     Modint operator*=(const Modint& o) {       *this = (*this) * o;       return *this;     }     Modint Pow(long long b) const {       Modint tmp(val), ret(1);       while (b) {         if (b & 1) ret *= tmp;         b >>= 1;         tmp *= tmp;       }       return ret;     }     Modint Pow(const Modint& a, long long b) const { return a.Pow(b); }     inline Modint inv() const { return (*this).Pow(mod - 2); }     Modint operator/(const Modint& o) const { return *this * o.inv(); }     bool operator==(const Modint& o) const { return val == o.val; }   };   Modint<MOD> mpow(Modint<MOD> a, long long n, long long mo = MOD) {     Modint<MOD> re = 1;     while (n > 0) {       if (n & 1) re = re * a % mo;       a = a * a % mo;       n >>= 1;     }     return re;   }   Modint<MOD> inv(long long b, long long mo = MOD) {     if (b == 1) return b;     return (mo - mo / b) * inv(mo % b, mo) % mo;   }   const Modint<MOD> i4 = inv(4);   const Modint<MOD> i2 = inv(2);   Modint<MOD> go(long long x) {     ;     Modint<MOD> oth2 = 0;     Modint<MOD> oth1 = 0;     Modint<MOD> oth0 = 1;     Modint<MOD> siga = 0;     long long sigf = 0;     for (pair<long long, long long>& p : here[x]) {       Modint<MOD> a = p.first, first = p.second;       Modint<MOD> mf = mpow(2, first);       oth2 = mf * oth2 +              oth0 * a * a * mf * i4 * first * (first + (Modint<MOD>)1) +              oth1 * (Modint<MOD>)2 * a * mf * i2 * first;       siga += a * first;       sigf += first;       sigf %= (MOD - 1);       Modint<MOD> m2 = mpow(2, sigf);       oth1 = siga * m2 * i2;       oth0 = m2;     }     Modint<MOD> mpowsigf = mpow(2, sigf);     ;     Modint<MOD> re = 0;     for (pair<long long, long long>& p : here[x]) {       Modint<MOD> a = p.first, first = p.second;       Modint<MOD> have1 = siga * mpowsigf * i4 + a * mpowsigf * inv(4);       Modint<MOD> two = a * a * mpowsigf * i2 + oth2 + oth1 * (Modint<MOD>)2 * a -                         (Modint<MOD>)2 * a * have1;       two *= i2;       ;       re += two * first;     };     return re - oth2;   }   long long mob[maxn];   bool np[maxn];   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     ;     long long n;     cin >> n;     for (long long i = 0; i < n; ++i) {       long long a, first;       cin >> a >> first;       for (long long j = 1; j <= sqrt(a); ++j) {         if (a % j == 0) {           here[j].push_back({a, first});           if (j * j != a) {             here[a / j].push_back({a, first});           }         }       }     }     fill(mob, mob + maxn - 1, 1);     for (long long i = 2; i < maxn; ++i) {       if (!np[i]) {         mob[i] = -1;         for (long long j = i + i; j < maxn; j += i) {           mob[j] = -mob[j];           np[j] = 1;         }         if (i * i <= maxn)           for (long long j = i * i; j < maxn; j += i * i) {             mob[j] = 0;           }       }     }     long long re = 0;     for (long long i = 0; i < maxn; ++i) {       if (mob[i] && (long long)here[i].size()) {         re += mob[i] * go(i);         re %= MOD;       }     }     cout << (re + MOD) % MOD <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void amin(T &first, const T &second) {     if (second < first) first = second;   }   template <class T>   inline void amax(T &first, const T &second) {     if (first < second) first = second;   }   void solve() {     int n;     cin >> n;     vector<pair<int, int> > a(n);     map<int, int> home;     for (int i = 0; i < n; ++i) {       cin >> a[i].first >> a[i].second;       home[a[i].first]++;     }     vector<int> ret(n);     for (int i = 0; i < n; ++i) {       ret[i] = n - 1;       ret[i] += home[a[i].second];     }     for (int i = 0; i < n; ++i) {       cout << ret[i] <<     << 2 * (n - 1) - ret[i] <<   n ;     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     long long q;     cin >> q;     for (long long t = 0; t < q; t++) {       long long n, k;       cin >> n >> k;       vector<vector<long long>> adj((size_t)n);       map<pair<long long, long long>, long long> weight;       for (long long i = 0; i < n - 1; i++) {         long long u, v, w;         cin >> u >> v >> w;         u--, v--;         adj[u].push_back(v);         adj[v].push_back(u);         weight[{u, v}] = w;         weight[{v, u}] = w;       }       vector<vector<long long>> dp((size_t)n, vector<long long>((size_t)2));       function<void(long long, long long)> visit = [&](long long u, long long v) {         dp[v][0] = 0;         dp[v][1] = 0;         vector<long long> g;         for (long long w : adj[v]) {           if (w != u) {             visit(v, w);             dp[v][0] += dp[w][1];             dp[v][1] += dp[w][1];             g.push_back(max(dp[w][0] + weight[{v, w}], dp[w][1]) - dp[w][1]);           }         }         sort(g.rbegin(), g.rend());         dp[v][0] += accumulate(             g.begin(), g.begin() + min((long long)g.size(), k - 1), (long long)0);         dp[v][1] += accumulate(g.begin(), g.begin() + min((long long)g.size(), k),                                (long long)0);       };       visit(-1, 0);       cout << dp[0][1] << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct pt {     unsigned long long x, y, v;   };   int n;   vector<unsigned long long> a, b;   pt c[200005];   vector<vector<unsigned long long>> d;   void sq(vector<unsigned long long>& v) {     sort(v.begin(), v.end());     v.erase(unique(v.begin(), v.end()), v.end());   }   template <class T>   T gcd(T a, T b) {     T t;     while (a) {       t = a;       a = b % a;       b = t;     }     return b;   }   const unsigned long long MOD1 = 1000000007;   const unsigned long long MOD2 = 1000000009;   struct frac {     unsigned long long p, q;     bool operator==(const frac& b) const {       unsigned long long v11 = (p % MOD1 * (b.q % MOD1)) % MOD1;       unsigned long long v12 = (q % MOD1 * (b.p % MOD1)) % MOD1;       if (v11 != v12) return false;       unsigned long long v21 = (p % MOD2 * (b.q % MOD2)) % MOD2;       unsigned long long v22 = (q % MOD2 * (b.p % MOD2)) % MOD2;       if (v21 != v22) return false;       return p * b.q == q * b.p;     }   };   bool deli(unsigned long long a, unsigned long long b, unsigned long long c) {     unsigned long long g = gcd(a, b);     a /= g;     g = gcd(a, c);     return a == g;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     cerr.tie(nullptr);     cin >> n;     for (int i = 0; i < n; i++) {       unsigned long long x, y, v;       cin >> x >> y >> v;       c[i] = {x, y, v};       a.push_back(x);       b.push_back(y);     }     sq(a);     sq(b);     int rows = a.size();     int cols = b.size();     if (rows * 1ll * cols != n) {       cout <<  0 n ;       return 0;     }     d.assign(rows, vector<unsigned long long>(cols, 0));     for (int i = 0; i < n; i++) {       int rr = lower_bound(a.begin(), a.end(), c[i].x) - a.begin();       int cc = lower_bound(b.begin(), b.end(), c[i].y) - b.begin();       d[rr][cc] = c[i].v;     }     bool ok = true;     for (int i = 0; i < rows - 1; i++) {       for (int j = 0; j < cols - 1; j++) {         frac a = {d[i][j], d[i + 1][j]};         frac b = {d[i][j + 1], d[i + 1][j + 1]};         if (!(a == b)) {           ok = false;         }       }     }     if (!ok) {       cout <<  0 n ;       return 0;     }     unsigned long long sol = 0;     unsigned long long v0 = d[0][0];     unsigned long long grow = 0;     for (int j = 0; j < cols; j++) grow = gcd(grow, d[0][j]);     unsigned long long gcol = 0;     for (int i = 0; i < rows; i++) gcol = gcd(gcol, d[i][0]);     set<unsigned long long> divs;     for (unsigned long long i = 1; i * i <= grow; i++) {       if (grow % i == 0) {         divs.insert(i);         divs.insert(grow / i);       }     }     for (unsigned long long g : divs) {       if (deli(v0, gcol, g)) {         sol++;       }     }     cout << sol <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     string s;     cin >> s;     map<char, int> mp;     for (char c : s) mp[c]++;     cout << 2 * min(mp[ L ], mp[ R ]) + 2 * min(mp[ U ], mp[ D ]) << endl;   }


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