#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     long long int t;     cin >> t;     while (t--) {       long long int n;       cin >> n;       float a = 1;       float b = (float)360 / ((2 * n));       b /= 2;       b = (3.14159265358979323846264338327950288419716939937510582 / 180) * b;       float ans = 1 / tan(b);       cout << fixed << setprecision(15) << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename A, typename B, typename C>   struct triple {     A X;     B Y;     C Z;     triple(A a = 0, B b = 0, C c = 0) : X(a), Y(b), Z(c) {}   };   template <typename A, typename B, typename C>   triple<A, B, C> make_triple(A a = 0, B b = 0, C c = 0) {     return triple<A, B, C>(a, b, c);   }   template <typename A, typename B, typename C>   bool operator<(const triple<A, B, C>& a, const triple<A, B, C>& b) {     if (a.X != b.X) return a.X < b.X;     if (a.Y != b.Y) return a.Y < b.Y;     return a.Z < b.Z;   }   template <typename T, typename SS>   ostream& operator<<(ostream& ofs, const pair<T, SS>& p) {     ofs <<  (   << p.first <<   ,   << p.second <<   ) ;     return ofs;   }   template <typename T>   void print(T a) {     for (auto i : a) cout << i <<    ;     cout <<   n ;   }   template <typename T>   T max(T a, T b, T c) {     return max(a, max(b, c));   }   template <typename T>   T min(T a, T b, T c) {     return min(a, min(b, c));   }   struct custom_hash {     static uint64_t splitmix64(uint64_t x) {       x += 0x9e3779b97f4a7c15;       x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;       x = (x ^ (x >> 27)) * 0x94d049bb133111eb;       return x ^ (x >> 31);     }     size_t operator()(uint64_t x) const {       static const uint64_t FIXED_RANDOM =           chrono::steady_clock::now().time_since_epoch().count();       return splitmix64(x + FIXED_RANDOM);     }   };   signed main() {     srand(time(NULL));     cin.tie(0);     cout.tie(0);     ios_base::sync_with_stdio(false);     long long n;     cin >> n;     vector<long long> a(n);     for (long long i = 0; i < (n); i++) cin >> a[i];     long long ans = 0;     auto solve = [&](long long v) {       vector<long long> b(a);       long long pw = 1 << v;       for (long long i = 0; i < (n); i++) b[i] %= (pw * 2);       sort(b.begin(), b.end());       long long c = 0;       auto count = [&](long long l, long long r) -> long long {         return lower_bound(b.begin(), b.end(), r) -                lower_bound(b.begin(), b.end(), l);       };       for (long long i = 0; i < (n); i++)         c += (count(pw - b[i], pw * 2 - b[i]) +               count(pw * 3 - b[i], pw * 4 - b[i]));       for (long long i = 0; i < (n); i++)         if ((2 * b[i]) & pw) c--;       c >>= 1;       return c & 1;     };     for (long long i = 0; i < (25); i++)       if (solve(i)) ans += 1LL << i;     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   long long int gcd(long long int a, long long int b) {     if (b != 0)       return gcd(b, a % b);     else       return a;   }   long long int power(long long int a, long long int b) {     if (b == 0) return 1;     if (b == 1) return a;     if (b % 2 == 0) {       long long int t = power(a, b / 2);       return t * t;     } else {       long long int t = power(a, b / 2);       return a * t * t;     }   }   long long int powin(long long int a, long long int b) {     if (b == 0)       return 1;     else if (b == 1)       return a;     else if (b % 2 == 0) {       long long int t = powin(a, b / 2);       return (t * t) % 1000000007;     } else {       long long int t = powin(a, b / 2);       return (((t * t) % 1000000007) * a) % 1000000007;     }   }   long long int n, k;   long long int dp[509][10 * 509];   long long int h[20];   void solve() {     for (long long int i = 1; i < n + 1; i++) {       for (long long int j = 1; j < k * n + 1; j++) {         for (long long int p = 1; p < k + 1; p++) {           if (j >= p) dp[i][j] = max(dp[i][j], dp[i - 1][j - p] + h[p]);         }       }     }   }   long long int fr[100000 + 10];   long long int f[100000 + 10];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n >> k;     memset(dp, 0, sizeof dp);     memset(fr, 0, sizeof fr);     memset(f, 0, sizeof f);     for (long long int i = 0; i < n * k; i++) {       long long int t;       cin >> t;       fr[t]++;     }     for (long long int i = 0; i < n; i++) {       int p;       cin >> p;       f[p]++;     }     h[0] = 0;     for (long long int i = 1; i < k + 1; i++) {       cin >> h[i];       dp[1][i] = h[i];     }     solve();     long long int ans = 0;     for (long long int i = 1; i < 100000 + 1; i++) {       if (f[i] > 0) {         long long int temp = 0;         for (long long int j = 1; j < f[i] + 1; j++) {           temp = max(dp[j][fr[i]], temp);         }         ans += temp;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void read(long long &x) {     x = 0;     char c = getchar();     int f = 1;     while (!(c >=  0  && c <=  9 )) {       if (c ==  - ) f = -1;       c = getchar();     }     while ((c >=  0  && c <=  9 )) {       x = (x << 1) + (x << 3) + (c ^ 48);       c = getchar();     }     x *= f;   }   void output(long long x) {     if (x < 0) {       x = -x;       putchar( - );     }     if (x > 9) output(x / 10);     putchar(x % 10 +  0 );   }   struct node {     long long l, r;     mutable long long val;     node(long long L) : l(L) {}     node(long long L, long long R, long long VAL) : l(L), r(R), val(VAL) {}   };   bool operator<(node x, node y) { return x.l < y.l; }   set<node> s;   set<node>::iterator split(long long pos) {     set<node>::iterator it = s.lower_bound(node(pos));     if (it != s.end() && it->l == pos) return it;     it--;     long long l = it->l, r = it->r, val = it->val;     s.erase(it);     s.insert(node(l, pos - 1, val));     return s.insert(node(pos, r, val)).first;   }   void add(long long l, long long r, long long x) {     set<node>::iterator it2 = split(r + 1), it1 = split(l);     for (set<node>::iterator it = it1; it != it2; ++it) {       it->val += x;     }   }   long long kth(long long l, long long r, long long x) {     set<node>::iterator it2 = split(r + 1), it1 = split(l);     vector<pair<long long, long long> > v;     v.clear();     for (set<node>::iterator it = it1; it != it2; ++it) {       v.push_back(pair<long long, long long>(it->val, it->r - it->l + 1));     }     sort(v.begin(), v.end());     for (long long i = 0; i < v.size(); ++i) {       x -= v[i].second;       if (x <= 0) return v[i].first;     }   }   long long ksm(long long a, long long b, long long c) {     a %= c;     long long ans = 1;     while (b) {       if (b & 1) ans = (ans * a) % c;       a = (a * a) % c;       b >>= 1;     }     return ans;   }   long long query(long long l, long long r, long long x, long long y) {     long long ans = 0;     set<node>::iterator it2 = split(r + 1), it1 = split(l);     for (set<node>::iterator it = it1; it != it2; ++it) {       ans = (ans + (it->r - it->l + 1) * ksm(it->val, x, y) % y) % y;     }     return ans;   }   void assign(long long l, long long r, long long k) {     set<node>::iterator it2 = split(r + 1), it1 = split(l);     s.erase(it1, it2);     s.insert(node(l, r, k));   }   long long n, m, seed, vmax;   long long op, l, r, x, y;   long long rnd() {     long long ret = seed;     seed = (seed * 7 + 13) % 1000000007;     return ret;   }   void SWAP(long long &x, long long &y) {     x ^= y;     y ^= x;     x ^= y;   }   int main() {     read(n), read(m), read(seed), read(vmax);     for (long long i = 1; i <= n; ++i) {       x = (rnd() % vmax) + 1;       s.insert(node(i, i, x));     }     for (long long i = 1; i <= m; ++i) {       op = (rnd() % 4) + 1;       l = (rnd() % n) + 1;       r = (rnd() % n) + 1;       if (l > r) SWAP(l, r);       if (op == 1) {         x = (rnd() % vmax) + 1;         add(l, r, x);       } else if (op == 2) {         x = (rnd() % vmax) + 1;         assign(l, r, x);       } else if (op == 3) {         x = (rnd() % (r - l + 1)) + 1;         output(kth(l, r, x));         putchar(  n );       } else {         x = (rnd() % vmax) + 1;         y = (rnd() % vmax) + 1;         output(query(l, r, x, y));         putchar(  n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct ln {     long long m, k;     ln(long long mm, long long kk) {       m = mm;       k = kk;     }   };   deque<ln> cvt;   long long dp[100010][2];   long long sw[100010];   long long wt[100010];   long long hilldist[100010];   long long cs(long long x, int ci) { return x * cvt[ci].m + cvt[ci].k; }   bool ex(long long m, long long k) {     long long m1 = cvt[cvt.size() - 2].m;     long long k1 = cvt[cvt.size() - 2].k;     long long m2 = cvt[cvt.size() - 1].m;     long long k2 = cvt[cvt.size() - 1].k;     return (k1 - k2) * (m - m1) >= (k1 - k) * (m2 - m1);   }   int main() {     int n, m, p;     int d, h, t;     scanf( %d%d%d , &n, &m, &p);     for (int i = 2; i <= n; i++) {       scanf( %d , &d);       hilldist[i] = hilldist[i - 1] + d;     }     for (int i = 1; i <= m; i++) {       scanf( %d%d , &h, &t);       wt[i] = t - hilldist[h];     }     sort(wt + 1, wt + m + 1);     for (int i = 1; i <= m; i++) sw[i] = sw[i - 1] + wt[i];     int nx = 1, nw = 0;     for (int i = 1; i <= m; i++) {       dp[i][0] = wt[i] * i - sw[i];     }     int k1;     for (int k = 2; k <= p; k++) {       cvt.clear();       k1 = k - 1;       cvt.push_back(ln(-k1, sw[k1] + dp[k1][nw]));       for (int i = 0; i <= m; i++) dp[i][nx] = 0;       for (int i = k; i <= m; i++) {         while (cvt.size() > 1 && cs(wt[i], 0) >= cs(wt[i], 1)) cvt.pop_front();         dp[i][nx] = cs(wt[i], 0) + wt[i] * (i)-sw[i];         while (cvt.size() > 1 && ex(-i, sw[i] + dp[i][nw])) cvt.pop_back();         cvt.push_back(ln(-i, sw[i] + dp[i][nw]));       }       swap(nx, nw);     }     cout << dp[m][nw];   }


#include <bits/stdc++.h>   using namespace std;   int x[100];   int a[10010000];   int main() {     int a, b, n;     scanf( %d%d%d , &a, &b, &n);     if (b >= n - 1)       puts( 2 );     else {       int f = 0;       for (int i = 1; i < n && i <= a; i++) {         int x = (long long)i * 1000000000 % n;         if (x > 0 && x <= n - b - 1) {           f = 1;           printf( 1 %09d n , i);           break;         }       }       if (!f) puts( 2 );     }   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-6;   const int mod = 1e9 + 7;   const int maxn = 2e6 + 100;   const int maxm = 2e6 + 100;   const int inf = 0x3f3f3f3f;   const double pi = acos(-1.0);   int t;   pair<long long, long long> a[maxn];   long long s;   int n;   int vis[maxn];   bool cmp2(pair<long long, long long> a, pair<long long, long long> b) {     return a.second < b.second;   }   vector<long long> mm, rr, lll;   int ck(long long x) {     int L = 0, R = 0, M = 0;     lll.clear();     rr.clear();     mm.clear();     for (int i = 1; i <= n; i++) {       if (a[i].first <= x && a[i].second >= x) {         mm.push_back(a[i].first);         M++;         continue;       }       if (a[i].first > x) {         R++;         rr.push_back(a[i].first);       }       if (a[i].second < x) {         L++;         lll.push_back(a[i].first);       }     }     if (L > n / 2) return false;     long long res = 0;     sort(mm.begin(), mm.end());     if (L < n / 2) {       int to = M;       for (int i = 0; i < to; i++) {         lll.push_back(mm[i]);         L++;         M--;         if (L == n / 2) break;       }     }     if (R < n / 2 + 1) {       if (R + M != n / 2 + 1) return false;       res += 1ll * M * x;     }     for (int i = 0; i < (int)lll.size(); i++) {       res += lll[i];     }     for (int i = 0; i < (int)rr.size(); i++) {       res += max(rr[i], x);     }     return res <= s;   }   int main() {     scanf( %d , &t);     while (t--) {       long long mx = 0;       scanf( %d %lld , &n, &s);       for (int i = 1; i <= n; i++) {         scanf( %lld %lld , &a[i].first, &a[i].second);         mx = max(mx, a[i].second);       }       long long ans = 0;       sort(a + 1, a + 1 + n);       long long l = a[n / 2 + 1].first;       sort(a + 1, a + 1 + n, cmp2);       long long r = a[n / 2 + 1].second;       while (l <= r) {         long long mid = l + r >> 1;         if (ck(mid)) {           l = mid + 1;           ans = mid;         } else           r = mid - 1;       }       printf( %lld n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int max(long long int a, long long int b) {     if (a > b)       return a;     else       return b;   }   void solve() {     int n;     cin >> n;     vector<int> a(2 * n);     for (int i = 0; i < 2 * n; i++) {       cin >> a[i];     }     int din = 0;     for (int i = 0; i < 2 * n; i++) {       if (a[i] == 1)         din += 1;       else         din -= 1;     }     if (din == 0) {       cout << 0 << endl;       return;     }     map<int, int> m;     int dr = 0;     m[0] = n - 1;     for (int i = n; i < 2 * n; i++) {       if (a[i] == 1)         dr += 1;       else         dr -= 1;       if (!m.count(dr)) {         m[dr] = i;       }     }     int ans = 1e9 + 7;     int dl = 0;     if (m.count(din)) {       ans = min(ans, m[din] - n + 1);     }     for (int i = n - 1; i >= 0; i--) {       if (a[i] == 1)         dl += 1;       else         dl -= 1;       int q = din - dl;       if (m.count(q)) {         ans = min(ans, m[q] - i + 1);       }     }     cout << ans << endl;   }   int main() {     int t;     cin >> t;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   const long double PI = acos(-1.0);   const double EPS = 1E-6;   const int INF = 2 * (1e+9) + 1;   const int MAXN = 3 * 100000001;   const int MOD = (1e+9) + 7;   int n, m;   map<int, int> m1, m2;   map<int, int> u, cnt;   int main() {     cin >> n;     for (int i = 0; i < (n); ++i) {       int t1, t2;       cin >> t1 >> t2;       m1[t1] = t2;       m2[t2] = t1;       cnt[t1]++;       cnt[t2]++;     }     int cur = 0;     vector<int> ans(n);     int inx = 1;     while (inx < n) {       cur = m1[cur];       ans[inx] = cur;       u[cur] = 1;       inx += 2;       if (!cur) break;     }     cur = 0, inx = n - 2;     while (inx >= 0) {       cur = m2[cur];       if (!cur) break;       ans[inx] = cur;       u[cur] = 1;       inx -= 2;     }     if (n % 2 == 0) {       for (map<int, int>::iterator it = m1.begin(); it != m1.end(); it++) {         if (!u[it->first] && it->first) {           ans[0] = it->first;         }       }     } else {       for (map<int, int>::iterator it = m1.begin(); it != m1.end(); it++) {         if (cnt[it->first] == 1 && (!u[it->first])) {           ans[0] = it->first;           cur = ans[0], inx = 2;           while (inx < n) {             cur = m1[cur];             ans[inx] = cur;             u[cur] = 1;             inx += 2;             if (!cur) break;           }           break;         }       }     }     for (int i = 0; i < ((int)(ans).size()); ++i) {       cout << ans[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 1e9 + 7;   vector<vector<pair<string, int>>> adl;   vector<int> dist(2000, inf);   map<pair<string, int>, int> nums;   map<string, pair<int, int>> mins;   void bfs(string& poly, int version) {     vector<string> order;     order.push_back(poly);     mins[poly] = {0, version};     dist[1] = 0;     for (int i = 0; i < order.size(); i++) {       int u = nums[{order[i], mins[order[i]].second}];       for (auto j : adl[u]) {         int v = nums[j], ver = j.second;         string& s = j.first;         if (dist[v] > dist[u] + 1) {           dist[v] = dist[u] + 1;           order.push_back(s);           if (mins.find(s) == mins.end()) mins[s] = {dist[v], ver};           if (dist[v] < mins[s].first) {             mins[s] = {dist[v], ver};           } else if (dist[v] == mins[s].first) {             mins[s].second = max(mins[s].second, ver);           }         }       }     }   }   int main() {     int n;     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     adl.resize(2 * n);     int m = 1, v = -1;     string poly;     for (int i = 0; i < n; i++) {       int num, ind, vers;       string s, s1;       cin >> s >> num;       if (i == 0) {         poly = s;         v = num;       }       if (nums[{s, num}] == 0) {         nums[{s, num}] = m;         m++;       }       ind = nums[{s, num}];       int k;       cin >> k;       for (int j = 0; j < k; j++) {         cin >> s1 >> vers;         if (s == s1) continue;         adl[ind].push_back({s1, vers});       }     }     bfs(poly, v);     cout << (int)mins.size() - 1 <<   n ;     for (auto p : mins)       if (p.first != poly) cout << p.first <<     << p.second.second <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   using nagai = long long;   const int N = 100;   int matr[N][N];   const int K = 12;   int de[N][K];   const int oo = 0x3f3f3f3f;   mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n, k;     cin >> n >> k;     for (int i = 0; i < n; ++i)       for (int j = 0; j < n; ++j) cin >> matr[i][j];     int ans = oo;     for (int i = 0; i < 30000; ++i) {       memset(de, 0x3f, sizeof de);       vector<int> col(n);       for (int i = 0; i < n; ++i) col[i] = rnd() % 2;       de[0][0] = 0;       for (int l = 0; l < k; ++l)         for (int a = 0; a < n; ++a)           for (int b = 0; b < n; ++b)             if (col[a] != col[b])               de[b][l + 1] = min(de[b][l + 1], de[a][l] + matr[a][b]);       ans = min(ans, de[0][k]);     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   const int mod = 1000000007, maxn = 200005, maxk = 105;   int n, m;   int a[maxn], fac[maxn], nfac[maxn], inv[maxn], sum[maxn][maxk];   inline int C(int a, int b) {     return 1ll * fac[a] * nfac[b] % mod * nfac[a - b] % mod;   }   int main() {     fac[0] = fac[1] = nfac[0] = nfac[1] = inv[1] = 1;     for (int i = 2; i <= 200000; i++)       fac[i] = 1ll * fac[i - 1] * i % mod,       inv[i] = mod - 1ll * (mod / i) * inv[mod % i] % mod,       nfac[i] = 1ll * nfac[i - 1] * inv[i] % mod;     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int i = 1; i <= m; i++) {       int l, r, k;       scanf( %d%d%d , &l, &r, &k);       for (int j = 0; j <= k; j++) {         sum[l][j] = (sum[l][j] + C(k, k - j)) % mod;         sum[r + 1][j] = (sum[r + 1][j] - C(k + r - l + 1, k - j) + mod) % mod;       }     }     for (int i = 1; i <= n; i++)       for (int j = 100; j >= 0; j--)         sum[i][j] = (sum[i][j] + (sum[i - 1][j + 1] + sum[i - 1][j]) % mod) % mod;     for (int i = 1; i <= n; i++)       printf( %d%c , (a[i] + sum[i][0]) % mod, i == n ?   n  :    );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 500010;   long long n, sx, sy, x, y, res[6];   int main() {     scanf( %lld , &n);     scanf( %lld%lld , &sx, &sy);     for (int i = 0; i < n; i++) {       scanf( %lld%lld , &x, &y);       if (x < sx) res[1]++;       if (y < sy) res[2]++;       if (x > sx) res[3]++;       if (y > sy) res[4]++;     }     long long mx = 0;     for (int i = 1; i <= 4; i++) mx = max(mx, res[i]);     for (int i = 1; i <= 4; i++) {       if (mx == res[i]) {         long long a, b;         if (i == 1) {           a = sx - 1, b = sy;         } else if (i == 2) {           a = sx, b = sy - 1;         } else if (i == 3) {           a = sx + 1, b = sy;         } else           a = sx, b = sy + 1;         printf( %lld n , mx);         printf( %lld %lld , a, b);         return 0;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   bool uin(T &a, T b) {     return (a > b ? a = b, true : false);   }   template <class T>   bool uax(T &a, T b) {     return (a < b ? a = b, true : false);   }   int const X = 8;   int const L = 26;   int const N = 41;   long long const INF = 3e18 + 41;   int x, k, n, q;   int c[N];   int p[N], w[N];   long long d[(1 << X)][(1 << X)][L];   long long ans;   bool bit(int m, int i) { return (m & (1 << i)); }   int xorbit(int m, int i) { return (m ^ (1 << i)); }   int qbit(int m) {     int res = 0;     while (m) {       res += (m & 1);       m /= 2;     }     return res;   }   int dx[(1 << X)][(1 << X)];   unordered_map<int, int> a;   vector<pair<int, long long>> e[(1 << X)][L];   void build() {     for (int i = 0; i < (1 << X); ++i)       for (int j = 0; j < (1 << X); ++j)         for (int o = 0; o < L; ++o) d[i][j][o] = INF;     memset(dx, 255, sizeof(dx));     for (int i = 0; i < (1 << k); ++i) {       if (!bit(i, 0)) {         int m0 = i;         int m1 = (m0 >> 1);         uin(d[m0][m1][0], 0ll);       } else {         int m0 = i;         for (int j = 1; j < k + 1; ++j) {           if (bit(m0, j)) continue;           int m1 = xorbit(m0, j);           m1 = xorbit(m1, 0);           m1 >>= 1;           if (uin(d[m0][m1][0], (long long)c[j - 1])) dx[m0][m1] = j;         }       }     }     for (int o = 1; o < L; ++o) {       for (int t = 0; t < (1 << k); ++t) {         if (qbit(t) != x) continue;         for (int i = 0; i < (1 << k); ++i) {           if (qbit(i) != x) continue;           for (int j = 0; j < (1 << k); ++j) {             if (qbit(j) != x) continue;             uin(d[i][j][o], d[i][t][o - 1] + d[t][j][o - 1]);           }         }       }     }     for (int o = 0; o < L; ++o) {       for (int i = 0; i < (1 << X); ++i)         for (int j = 0; j < (1 << X); ++j)           if (d[i][j][o] < INF) {             if (qbit(i) != x) continue;             e[i][o].push_back(make_pair(j, d[i][j][o]));           }     }   }   long long f[(1 << X)], nf[(1 << X)];   void solve() {     int p1 = 0;     int x0 = 0;     for (int i = 0; i < (1 << X); ++i) f[i] = INF;     f[(1 << x) - 1] = 0;     while (x0 < n - x) {       while (p1 < q && x0 > p[p1]) ++p1;       int s;       for (int i = L - 1; i >= 0; --i) {         s = i;         if (x0 + (1 << i) + k - 1 > n - 1) continue;         if (p1 != q && x0 + (1 << i) + k - 1 >= p[p1]) continue;         break;       }       for (int i = 0; i < (1 << k); ++i) nf[i] = INF;       for (int i = 0; i < (1 << k); ++i) {         if (f[i] == INF) continue;         for (pair<int, long long> p : e[i][s]) {           long long v = f[i] + p.second;           if (s == 0 && dx[i][p.first] != -1 &&               a.find(x0 + dx[i][p.first]) != a.end()) {             v += a[x0 + dx[i][p.first]];           }           uin(nf[p.first], v);         }       }       memcpy(f, nf, sizeof(f));       x0 += (1 << s);     }     ans = f[(1 << x) - 1];   }   int main() {     scanf( %d %d %d %d , &x, &k, &n, &q);     for (int i = 0; i < k; ++i) scanf( %d , &c[i]);     for (int i = 0; i < q; ++i) {       scanf( %d %d , &p[i], &w[i]);       --p[i];       a[p[i]] = w[i];     }     for (int i = 0; i < q; ++i)       for (int j = i + 1; j < q; ++j)         if (p[j] < p[i]) {           swap(p[j], p[i]);           swap(w[j], w[i]);         }     build();     solve();     printf( %I64d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int const N = 1e5 + 2;   int n, m;   void solve() {     string n, m;     cin >> n >> m;     sort((n).begin(), (n).end());     if (n[0] ==  0  && n.size() > 1) swap(n[0], n[1]);     if (n == m)       cout <<  OK  << endl;     else       cout <<  WRONG_ANSWER  << endl;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     solve();   }


#include <bits/stdc++.h>   using namespace std;   map<int, long long> ans, re, now;   int n, h[200005], tot, mp[200005], siz[200005], mz[200005], rem[200005], cnt;   bool vis[200005];   struct DX {     int v, p;   } dx[400005];   int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }   void add(int u, int v) {     dx[tot].p = h[u];     dx[tot].v = v;     h[u] = tot++;   }   void dfs_G(int u, int fx) {     siz[u] = 1;     rem[++cnt] = u;     mz[u] = 0;     for (int i = h[u]; ~i; i = dx[i].p) {       int v = dx[i].v;       if (v == fx || vis[v]) continue;       dfs_G(v, u);       siz[u] += siz[v];       mz[u] = max(mz[u], siz[v]);     }   }   int fd_G(int u) {     cnt = 0;     dfs_G(u, u);     for (int i = 1; i <= cnt; i++)       if (mz[rem[i]] <= siz[u] / 2 && siz[u] - siz[rem[i]] <= siz[u] / 2)         return rem[i];   }   void dfs(int u, int fx, int gc) {     re[gc = gcd(gc, mp[u])]++;     for (int i = h[u]; ~i; i = dx[i].p) {       int v = dx[i].v;       if (v == fx || vis[v]) continue;       dfs(v, u, gc);     }   }   void sol(int u) {     u = fd_G(u);     vis[u] = 1;     now[mp[u]] = 1;     ans[mp[u]] += 1;     for (int i = h[u]; ~i; i = dx[i].p) {       int v = dx[i].v;       if (vis[v]) continue;       dfs(v, 0, mp[u]);       for (map<int, long long>::iterator it = now.begin(); it != now.end(); it++)         for (map<int, long long>::iterator ti = re.begin(); ti != re.end(); ti++)           ans[gcd(it->first, ti->first)] += it->second * ti->second;       for (map<int, long long>::iterator ti = re.begin(); ti != re.end(); ti++)         now[ti->first] += ti->second;       re.clear();     }     now.clear();     for (int i = h[u]; ~i; i = dx[i].p) {       int v = dx[i].v;       if (vis[v]) continue;       sol(v);     }   }   int main() {     memset(h, -1, sizeof(h));     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &mp[i]);     int a, b;     for (int i = 1; i < n; i++) {       scanf( %d%d , &a, &b);       add(a, b);       add(b, a);     }     sol(1);     for (map<int, long long>::iterator it = ans.begin(); it != ans.end(); it++)       printf( %d %lld n , it->first, it->second);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int m;     cin >> m;     for (int i = 1; i <= m; i++) {       if ((i * (i + 1)) / 2 == m) {         cout <<  YES  << endl;         return 0;       }       if ((i * (i + 1)) / 2 > m) {         cout <<  NO  << endl;         return 0;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 150000 + 50;   int n, m, h, vec[MAXN], sec[MAXN], ans, need[4 * MAXN], ex[4 * MAXN];   void plant(int v, int b, int e) {     need[v] = e - b;     if (e - b == 1) return;     int mid = (b + e) / 2;     plant(v << 1, b, mid);     plant(v << 1 ^ 1, mid, e);   }   void water(int v, int b, int e, int x, int val) {     if (x == e) return;     if (e - b == 1) {       need[v] -= val;       ex[v] = max(0, -need[v]);       return;     }     int mid = (b + e) / 2;     if (x < mid)       water(v << 1, b, mid, x, val);     else       water(v << 1 ^ 1, mid, e, x, val);     need[v] = max(0, need[v << 1]) + max(0, need[v << 1 ^ 1] - ex[v << 1]);     ex[v] = ex[v << 1 ^ 1] + max(0, ex[v << 1] - max(0, need[v << 1 ^ 1]));   }   int main() {     scanf( %d %d %d , &n, &m, &h);     for (int i = 0; i < m; i++) scanf( %d , &sec[i]);     sort(sec, sec + m);     plant(1, 0, m);     for (int i = 0; i < n; i++) {       if (i >= m)         water(1, 0, m, lower_bound(sec, sec + m, h - vec[i - m]) - sec, -1);       scanf( %d , &vec[i]);       water(1, 0, m, lower_bound(sec, sec + m, h - vec[i]) - sec, 1);       ans += bool(need[1] == 0);     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k;   char s[200];   char p[200];   char ans[200];   bool used[200][30];   bool brute(int pos) {     if (pos == n) {       puts(ans);       exit(0);     }     if (ans[pos] != 0) {       if (pos >= strlen(s) - 1) {         if (p[pos - strlen(s) + 1] ==  0 ) {           bool ok = true;           for (int j = strlen(s) - 1; j >= 0; j--) {             if (ans[pos - j] != s[strlen(s) - 1 - j]) {               ok = false;               break;             }           }           if (ok) return false;         }       }       return brute(pos + 1);     }     memset(used[pos], 0, sizeof used[pos]);     int cnt = k;     bool need_check = false;     if (pos >= strlen(s) - 1) {       need_check = true;       for (int i = strlen(s) - 1; i > 0; i--) {         if (ans[pos - i] != s[strlen(s) - 1 - i]) {           need_check = false;           break;         }       }     }     for (int t = 0; t < k; t++) {       ans[pos] = (char)( a  + t);       if (!need_check) {         if (brute(pos + 1)) return true;       } else {         if (t != s[strlen(s) - 1] -  a ) {           if (brute(pos + 1)) return true;         }       }     }     ans[pos] = 0;     return false;   }   int main() {     scanf( %d %d n , &n, &k);     gets(s);     gets(p);     memset(ans, 0, sizeof ans);     for (int i = 0; i < strlen(p); i++) {       if (p[i] ==  1 ) {         for (int j = 0; j < strlen(s); j++) {           if (ans[i + j] == 0 || ans[i + j] == s[j]) {             ans[i + j] = s[j];           } else {             puts( No solution );             return 0;           }         }       }     }     for (int i = 0; i < strlen(p); i++) {       if (p[i] ==  0 ) {         bool ok = true;         for (int j = 0; j < strlen(s); j++) {           if (ans[i + j] != s[j]) {             ok = false;             break;           }         }         if (ok) {           puts( No solution );           return 0;         }       }     }     for (int i = 0; i < k; i++) {       bool ok = true;       for (int j = 0; j < strlen(s); j++) {         if (s[j] -  a  == i) {           ok = false;           break;         }       }       if (ok) {         for (int j = 0; j < n; j++) {           if (ans[j] == 0) ans[j] = (char)( a  + i);         }         puts(ans);         return 0;       }     }     brute(0);     puts( No solution );   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     if (k > n) {       cout <<  -1 ;       return 0;     }     if (n == 1) {       cout <<  a ;       return 0;     }     if (n > 1 && k == 1) {       cout <<  -1 ;       return 0;     }     string str;     char c1 =  a , c2 =  b ;     for (int i = 0; i < n; i++) {       str += c1;       if (i % 2 == 0)         c1 =  b ;       else         c1 =  a ;     }     char ch =  a  + k - 1;     int l = str.size() - 1;     for (int i = 0; i < k - 2; i++) str[l--] = ch--;     cout << str;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, dfsCont, dfs_num[2005], dfs_low[2005], numComp;   vector<vector<int> > lista;   string ans[2005];   vector<int> pila, comp;   map<string, int> names;   bool vis[2005];   void tarjan(int u) {     dfs_low[u] = dfs_num[u] = dfsCont++;     vis[u] = 1;     pila.push_back(u);     for (int i = 0; i < lista[u].size(); i++) {       int v = lista[u][i];       if (dfs_num[v] == -1) tarjan(v);       if (vis[v]) dfs_low[u] = min(dfs_low[u], dfs_low[v]);     }     if (dfs_low[u] == dfs_num[u]) {       while (true) {         int v = pila.back();         comp.push_back(v);         pila.pop_back();         vis[v] = 0;         if (u == v) break;       }       numComp++;     }   }   int main() {     scanf( %d , &n);     lista.assign(n, vector<int>());     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++) {         int x;         scanf( %d , &x);         if (x) lista[i].push_back(j);       }     pila.clear();     memset(dfs_low, 0, sizeof dfs_low);     memset(dfs_num, -1, sizeof dfs_num);     memset(vis, 0, sizeof vis);     dfsCont = 0;     numComp = 0;     tarjan(0);     if (numComp == 1 && comp.size() == n)       printf( YES n );     else       printf( NO n );   }


#include <bits/stdc++.h>   using namespace std;   template <typename X>   void MA(X &a, X b) {     a = max(a, b);   }   template <typename X>   void MI(X &a, X b) {     a = min(a, b);   }   int DEBUG = 0, MULTI = 0, GCJ = 0;   struct solver_t;   solver_t *solver;   struct solver_t {     vector<int> A;     vector<vector<int> > EDG;     struct lcu_t {       int N;       vector<int> CNT, PAR;       lcu_t(int N) : N(N) {         PAR.resize(N);         CNT.resize(N);         for (int64_t i = (0); i < (N); ++i) PAR[i] = i;         for (int64_t i = (0); i < (N); ++i) CNT[i] = 1;       }       int parent(int i) {         if (PAR[i] == i)           return i;         else           return PAR[i] = parent(PAR[i]);       }       int64_t get_cnt(int64_t x) { return x * (x - 1) / 2; }       int64_t join(int a, int b) {         int pa = parent(a), pb = parent(b);         if (pa == pb) return 0;         if (!(CNT[pb] >= CNT[pa])) swap(pa, pb);         int64_t diff = -get_cnt(CNT[pa]) - get_cnt(CNT[pb]);         PAR[pa] = pb;         CNT[pb] += CNT[pa];         diff += get_cnt(CNT[pb]);         return diff;       }     };     void solve() {       int N;       cin >> N;       A.resize(N + 1);       for (int64_t i = (1); i <= (N); ++i) cin >> A[i];       EDG.resize(N + 1);       for (int64_t i = (0); i < (N - 1); ++i) {         int X, Y;         cin >> X >> Y;         EDG[X].push_back(Y);         EDG[Y].push_back(X);       }       vector<array<int64_t, 2> > order;       for (int64_t i = (1); i <= (N); ++i) order.push_back({A[i], i});       sort((order).begin(), (order).end());       int64_t res[2] = {};       for (int64_t mode = (0); mode < (2); ++mode) {         lcu_t lcu(N + 1);         int64_t prev_cnt = 0;         int64_t &sum = res[mode];         vector<int> vis(N + 1);         for (int i = 0; i < (int)order.size();) {           int64_t diff = 0;           int j;           {             if (DEBUG)               cerr << 81 <<                        <<                         <<                        <<    << endl;           };           for (j = i; j < (int)order.size(); j++) {             if (order[i][0] != order[j][0]) break;             int u = order[j][1];             vis[u] = 1;             {               if (DEBUG)                 cerr << 86 <<                          <<   vis   << u                       <<                          <<  vis   << u << endl;             };             for (int v : EDG[u])               if (vis[v]) {                 diff += lcu.join(u, v);                 {                   if (DEBUG)                     cerr << 90 <<                              <<  u <<     << v <<     << diff                           <<     << u <<     << v <<     << diff << endl;                 };               }           }           {             if (DEBUG)               cerr << 93 <<                        <<  order[i][0] <<     << diff                     <<     << order[i][0] <<     << diff << endl;           };           sum += order[i][0] * diff;           prev_cnt += diff;           i = j;         }         reverse((order).begin(), (order).end());         {           if (DEBUG)             cerr << 99 <<                      <<  sum                   <<     << sum << endl;         };         {           if (DEBUG)             cerr << 100 <<                      <<   !!                    <<                      <<  !!  << endl;         };       }       cout << abs(res[0] - res[1]) <<   n ;     }     void gen() {}     void brute() {}   };   int main(int argc, char **argv) {     for (int64_t i = (1); i <= (argc - 1); ++i)       for (int j = 0; argv[i][j]; j++)         if (argv[i][j] ==  . ) freopen(argv[i],  r , stdin);     for (int64_t i = (1); i <= (argc - 1); ++i)       if (argv[i] == string( q )) DEBUG = 1 << 30;     for (int64_t i = (1); i <= (argc - 1); ++i)       if (argv[i] == string( gen )) {         (solver = new solver_t())->gen();         exit(0);       }     for (int64_t i = (1); i <= (argc - 1); ++i)       if (argv[i] == string( brute )) {         (solver = new solver_t())->brute();         exit(0);       }     ios::sync_with_stdio(false), cin.tie(0);     cout.setf(ios::fixed), cout.precision(10);     int t;     if (MULTI || GCJ)       cin >> t;     else       t = 1;     for (int64_t i = (1); i <= (t); ++i) {       if (DEBUG) cerr << 129 <<     << i << endl;       if (GCJ) cout <<  Case #  << i <<  :  ;       solver = new solver_t();       solver->solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9;   const long long INFLL = 1e18 + 1;   const int MAX = 100001;   const long long MOD = 1000000007;   const int SZ = 300100;   const double PI = 3.14159265358979323846264338327;   long long inq(long long k, long long q, long long mod) {     if (q == 0) return 1;     long long l = inq(k, q / 2, mod);     if (q % 2 == 0)       return l * l % mod;     else       return l * l % mod * k % mod;   }   long long gcd(long long a, long long b) {     if (a < b) swap(a, b);     if (b == 0) return a;     return gcd(b, a % b);   }   long long cubr(long long a) {     long long l = -1, r = 1e6 + 2;     while (l < r - 1) {       long long mid = (l + r) / 2;       if (mid * mid * mid > a)         r = mid;       else         l = mid;     }     return l;   }   long long max(long long a, long long b) {     if (a > b) return a;     return b;   }   long long min(long long a, long long b) { return -1 * max(-a, -b); }   long long possible(long long q) {     if (q == INF) return -1;     return q;   }   bool correct(int x, int xx) {     if (x < 0) return 0;     if (x >= xx) return 0;     return 1;   }   long long dsumm(long long x, long long k) {     long long y = 1;     long long z = 0;     for (int i = 0; y < 1e18; i += 1) {       z += x / y % k;       y *= k;     }     return z;   }   long long dcount(long long x) {     long long y = 1;     long long z = 0;     int c[100];     for (int i = 0; i < 10; i += 1) c[i] = 0;     for (int i = 0; x > 0; i += 1) {       if (c[x / y % 10] == 0) z++;       c[x / y % 10] = 1;       x /= 10;     }     return z;   }   long long lg10(long long x) {     if (10000 <= x && x < 100000) return 5;     if (x == 0) return 0;     return lg10(x / 10) + 1;   }   long long g(long long x, long long mod) {     if (x == 0) return 0;     return x / mod + g(x / mod, mod);   }   bool is_digit(char c) { return ( 0  <= c && c <=  9 ); }   string s;   int n, p;   int a[50][50];   int b[3000000];   int marks[(1 << 17)][20][20];   int r[3000000];   int dp[3000000];   int len[3000000];   int cnt[50];   void mark(int mask, int i, int j) {     if (marks[mask][i][j] == 0) return;     marks[mask][i][j] = 0;     for (int k = 0; k < p; k++) {       if (((1 << k) & mask) == 0 && k != i && k != j) {         int m2 = (mask + (1 << k));         if (marks[m2][i][j] == 1) mark(m2, i, j);       }     }   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> p;     cin >> s;     for (int(i) = 0; (i) != (n); i += 1) b[i] = s[i] -  a ;     for (int(i) = 0; (i) != (n); i += 1) cnt[b[i]]++;     for (int i = 0; i < p; i++) {       for (int j = 0; j < p; j++) {         cin >> a[i][j];       }     }     for (int i = 0; i < (1 << p); i++) {       r[i] = 1;       for (int j = 0; j < p; j++) {         for (int k = 0; k < p; k++) {           marks[i][j][k] = 1;         }       }     }     for (int i = 0; i < p; i++) {       for (int j = 0; j < p; j++) {         if (a[i][j] == 0) {           long long calc = 0;           long long mask = 0;           for (int k = 0; k < n; k++) {             if (b[k] == j) {               if (calc == 1) {                 mark(mask, i, j);               }               calc = 0;               mask = 0;             }             if (b[k] == i) {               calc = 1;               mask = 0;             }             if (b[k] != i && b[k] != j && calc == 1) mask |= (1 << b[k]);           }         }       }     }     for (int i = 0; i < (1 << p); i++) {       r[i] = 1;       for (int j = 0; j < p; j++) {         for (int k = 0; k < p; k++) {           r[i] = min(r[i], marks[i][j][k]);         }       }     }     dp[0] = 1;     int ans = n;     for (int i = 1; i < (1 << p); i++) {       for (int j = 0; j < p; j++) {         if (i & (1 << j)) {           int ii = i - (1 << j);           if (dp[ii] && r[i]) {             dp[i] = 1;             int first = n;             for (int k = 0; k < p; k++) {               if ((1 << k) & i) {                 first -= cnt[k];               }             }             ans = min(first, ans);           }         }       }     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int n, m;     cin >> n >> m;     vector<string> a(n);     for (int i = 0; i < n; i++) {       cin >> a[i];     }     int ans = 1 << 30;     for (int k = 2; k < 100; k++) {       int nn = (n + k - 1) / k * k;       int nm = (m + k - 1) / k * k;       vector<string> b = a;       b.resize(nn,  0 );       for (int i = 0; i < nn; i++) {         b[i].resize(nm,  0 );       }       int cur = 0;       for (int i = 0; i + k <= nn; i += k) {         for (int j = 0; j + k <= nm; j += k) {           int ca = 0;           int cb = 0;           for (int w = 0; w < k; w++) {             for (int h = 0; h < k; h++) {               ca += (b[i + w][j + h] ==  0  ? 0 : 1);               cb += (b[i + w][j + h] ==  1  ? 0 : 1);             }           };           cur += min(ca, cb);         }       };       ans = min(ans, cur);     }     cout << ans <<   n ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     solve();     return 0;   }


#include<bits/stdc++.h>   #define int long long   using namespace std;   int32_t main()   {   ios::sync_with_stdio(0);   cin.tie(0);   int t;   cin>>t;   while(t--)       {int n,k;        cin>>n>>k;        int ok=1;        int poss=0;        for(int i=0;i<n-1;i++)           {ok=ok*2;            if(ok>=k){poss=1;break;}           }       if(ok<k){cout<< -1 n ;continue;}       vector<int> v;       k--;       for(int i=0;i<n-1;i++)           {//Should I take i+1            int poss=0;            int z=1;            for(int j=0;j<n-i-2;j++)               {z=z*2;                if(z>k){break;}               }           if(z>k)               {v.push_back(i+1);               }           else               {k-=z;               }           }       v.push_back(n);       int visited[n];       memset(visited,0,sizeof(visited));       for(auto x:v)           {               visited[x-1]=1;               cout<<x<<   ;               for(int i=x-1;i>0;i--)                   {                       if(visited[i-1]==1){break;}                       cout<<i<<   ;                   }           }       cout<<  n ;       }   }


#include <bits/stdc++.h>   using namespace std;   vector<int> g[100005];   int ans = 0;   int csize[100005];   int dfs(int u, int fa) {     csize[u] = 1;     for (int i = 0; i < g[u].size(); i++) {       int v = g[u][i];       if (v == fa) continue;       dfs(v, u);       csize[u] += csize[v];       if (csize[v] % 2 == 0) ans++;     }   }   int main() {     int n;     while (cin >> n) {       ans = 0;       for (int i = 0; i <= n; i++) {         g[i].clear();       }       for (int i = 0; i < n - 1; i++) {         int x, y;         cin >> x >> y;         g[x].push_back(y);         g[y].push_back(x);       }       if (n % 2)         cout <<  -1 n ;       else {         dfs(1, 0);         cout << ans <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 11;   const long long MOD = 1e18;   long long a[N], b[N];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long x, y, l, r;     cin >> x >> y >> l >> r;     vector<long long> v;     long long k = 1, kk = 1;     for (int i = 0; i <= 60; i++) {       a[i] = k;       b[i] = kk;       if (k != -1 && r / k >= x && k * x <= r)         k *= x;       else         k = -1;       if (kk != -1 && r / kk >= y && kk * y <= r)         kk *= y;       else         kk = -1;     }     for (int i = 0; i <= 60; i++)       for (int j = 0; j <= 60; j++)         if (a[i] != -1 && b[j] != -1) {           long long dd = a[i] + b[j];           if (dd >= l && dd <= r) v.push_back(dd);         }     v.push_back(l - 1);     v.push_back(r + 1);     sort(v.begin(), v.end());     long long ans = 0;     for (int i = 1; i < v.size(); i++) ans = max(ans, v[i] - v[i - 1] - 1);     cout << ans << endl;   }


#include <bits/stdc++.h>   int main(void) {     int b_cost, money, b_number;     scanf( %d %d %d , &b_cost, &money, &b_number);     int total_cost;     int loan = 0;     total_cost = b_cost;     for (int i = 2; i <= b_number; i++) {       total_cost += i * b_cost;     }     if (money < total_cost) {       loan = total_cost - money;     }     printf( %d , loan);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 10;   const long long mod = 1e9 + 7;   struct node {     int s, e, next;   } edge[maxn];   int head[maxn], len;   void add(int s, int e) {     edge[len].e = e;     edge[len].next = head[s];     head[s] = len++;   }   void init() {     memset(head, -1, sizeof(head));     len = 0;   }   double dp[maxn];   void dfs(int x, int fa) {     dp[x] = 0;     int ans = 0;     for (int i = head[x]; i != -1; i = edge[i].next) {       int y = edge[i].e;       if (y == fa) continue;       dfs(y, x);       dp[x] += dp[y];       ans++;     }     if (ans == 0)       dp[x] = 0;     else       dp[x] = dp[x] / ans + 1;   }   int main() {     int n;     while (scanf( %d , &n) != EOF) {       init();       int x, y;       for (int i = 1; i < n; i++) {         scanf( %d%d , &x, &y);         add(x, y);         add(y, x);       }       dfs(1, -1);       printf( %.8f n , dp[1]);     }   }


#include <bits/stdc++.h>   using namespace std;   long long a[200100], sum[200100], qs[200100];   bool bad(pair<long long, long long> &a, pair<long long, long long> &b,            pair<long long, long long> second) {     return (a.second - second.second) * (b.first - a.first) >            (a.second - b.second) * (second.first - a.first);   }   long long at(pair<long long, long long> a, long long x) {     return a.first * x + a.second;   }   int back = 0;   pair<long long, long long> l[200100];   void add(long long first, long long second) {     if (back >= 2 and bad(l[back - 2], l[back - 1], {first, second})) back--;     l[back++] = {first, second};   }   long long query(long long x, int L = 0, int R = back - 1) {     if (L + 1 == R) return max(at(l[L], x), at(l[R], x));     if (L == R) return at(l[L], x);     int mid = (L + R) / 2;     if (at(l[mid], x) < at(l[mid - 1], x)) return query(x, L, mid - 1);     if (at(l[mid], x) < at(l[mid + 1], x)) return query(x, mid + 1, R);     return at(l[mid], x);   }   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++)       scanf( %lld , &a[i]), qs[i] = a[i] + qs[i - 1],                             sum[i] = a[i] * i + sum[i - 1];     long long ans = 0;     add(0, 0);     for (int i = 1; i <= n; i++) {       ans = max(ans, sum[i] + query(qs[i]));       add(-i, -sum[i] + i * qs[i]);     }     printf( %lld , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, t = 0, c = 0;     cin >> a >> b;     t += a;     while (b <= a) {       c = a % b;       a /= b;       t += a;       a += c;     }     cout << t << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1000000007;   const long long INFLL = 1000000000000000000LL;   const int MAXN = 200009;   int m, x;   vector<int> weights;   stack<int> order;   void print() {     if (order.empty()) return;     int x = order.top();     order.pop();     print();     cout << x <<    ;   }   void solve(int bal, int step) {     if (step == m) {       cout <<  YES  << endl;       print();       exit(0);     }     if (abs(bal) >= 10) return;     for (int w : weights) {       if (step % 2 == 0) {         if (bal + w > 0 && (order.empty() || w != order.top())) {           order.push(w);           solve(bal + w, step + 1);           order.pop();         }       } else {         if (bal - w < 0 && (order.empty() || w != order.top())) {           order.push(w);           solve(bal - w, step + 1);           order.pop();         }       }     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     for (int i = 1; i <= 10; i++) {       scanf( %1d , &x);       if (x == 1) weights.push_back(i);     }     scanf( %d , &m);     solve(0, 0);     cout <<  NO  << endl;   }


#include <bits/stdc++.h>   int N, head[51], next[99], from[99], to[99], len[99], E, root, M, q[51], fa[51],       count_e[99], f[51][99][51], vi[51][99];   void BFS() {     int H = 0, T = 1, u;     q[1] = root;     while (H < T)       for (int e = head[u = q[++H]]; e; e = next[e])         if (to[e] != fa[u]) fa[q[++T] = to[e]] = u;   }   void F(int n, int e) {     if (vi[n][e]) return;     vi[n][e] = 1;     f[n][e][0] = 1 << 30;     if (!next[head[to[e]]]) {       for (int i = 1; i < n; i++) {         F(n - i, ((e - 1) ^ 1) + 1);         f[n][e][i] = f[n - i][((e - 1) ^ 1) + 1][n - i] + len[e];       }       f[n][e][n] = 0;     } else {       for (int g = head[to[e]]; g; g = next[g])         if (to[g] != from[e]) F(n, g);       for (int i = 1; i <= n; i++) {         int l = 0, r = (1 << 30) - 1;         while (l < r) {           int m = l + r + 1 >> 1;           int count = 0;           for (int g = head[to[e]]; g; g = next[g])             if (to[g] != from[e])               count += std::upper_bound(f[n][g], f[n][g] + n + 1, m - len[g],                                         [](int x, int y) { return x > y; }) -                        f[n][g] - 1;           if (count >= i)             l = m;           else             r = m - 1;         }         f[n][e][i] = l;       }     }   }   int main() {     scanf( %d , &N);     for (int i = 1, u, v, l; i < N; i++) {       scanf( %d%d%d , &u, &v, &l);       next[++E] = head[u], from[E] = u, to[E] = v, len[E] = l, head[u] = E;       next[++E] = head[v], from[E] = v, to[E] = u, len[E] = l, head[v] = E;     }     scanf( %d%d , &root, &M);     BFS();     for (int i = 1, x; i <= M; i++) {       scanf( %d , &x);       while (fa[x] != root) x = fa[x];       for (int e = head[root]; e; e = next[e])         if (to[e] == x) count_e[e]++;     }     int O = 1 << 30;     for (int e = head[root]; e; e = next[e]) {       F(M, e);       O = std::min(O, f[M][e][count_e[e]] + len[e]);     }     printf( %d n , O);     return 0;   }


#include <bits/stdc++.h>   const int mxn = 100000 + 10;   using namespace std;   string s;   string soln;   int main() {     cin >> s;     int n = s.size();     for (int i = 0; i < n; i++)       if (s[i] ==  . )         printf( 0 );       else {         if (s[i + 1] ==  - )           printf( 2 );         else           printf( 1 );         i++;       }     puts(  );   }


//CLOCKS_PER_SEC   using namespace std;   #include<bits/stdc++.h>   #define sqr(x) 1ll*(x)*(x)   //#define sort stable_sort   #define ll long long   #define mk make_pair   #define pb push_back   #define in insert   #define mtr(x,y,z) mk(mk(x,y),z)   #define fi first   #define se second   #define lch(x) ((x)<<1)   #define rch(x) (((x)<<1)|1)   #define all(x) (x).begin(),(x).end()   #define titose CLOCKS_PER_SEC   #define fpi(x) freopen(x, r ,stdin);   #define fpo(x) freopen(x, w ,stdout);   #define fprio fpi( in.txt );fpo( out.txt );   #define fast ios_base::sync_with_stdio(false);   inline void read(int &x){int v=0,f=1;char c=getchar();while (!isdigit(c)&&c!= - ) c=getchar();if (c== - ) f=-1; else v=(c&15);while (isdigit(c=getchar())) v=(v<<1)+(v<<3)+(c&15);x=v*f;}   inline void read(ll &x){ll v=0ll,f=1ll;char c=getchar();while (!isdigit(c)&&c!= - ) c=getchar();if (c== - ) f=-1; else v=(c&15);while (isdigit(c=getchar())) v=(v<<1)+(v<<3)+(c&15);x=v*f;}   inline void readc(char &x){char c;while (((c=getchar())==   )||c==  n );x=c;}   #define pii pair<int,int>   #define pll pair<ll,ll>   #define vi vector<int>   #define vl vector<ll>   #define si set<int>   //#define sl set<ll>   #define mii map<int,int>   #define mll map<ll,ll>   #define msi map<string,int>   #define msl map<string,ll>   #define piii pair<int,pii >   #define piipi pair<pii,int>   #define plll pair<ll,pll >   #define pllpl pair<pll,ll>   #define pqi priority_queue<int>   #define pql priority_queue<ll>   #define npqi priority_queue<int,vector<int>,greater<int> >   #define npql priority_queue<ll,vector<ll>,greater<ll> >   #define forup(i,a,b) for ((i)=(a);(i)<=(b);(i)++)   #define fordo(i,a,b) for ((i)=(a);(i)>=(b);(i)--)   #define rep(i,x) forup ((i),1,(x))   #define repd(i,x) fordo ((i),(x),1)   #define rep0(i,x) forup ((i),0,((int)(x))-1)   #define rep0d(i,x) fordo ((i),((int)(x))-1,0)   #define itr iterator   #define fe(itcalc,c) for(__typeof((c).begin()) itcalc=(c).begin();itcalc!=(c).end();itcalc++)   #define NO {cout<< NO ;return 0;}   #define YES {cout<< YES ;return 0;}   #define y0 y000000000000000000000000000   #define y1 y111111111111111111111111111   #define j0 j000000000000000000000000000   #define j1 j111111111111111111111111111   #define cl0(a) memset((a),(0),(sizeof((a))))   #define clz(a) memset((a),(0x16),(sizeof((a))))   #define clf(a) memset((a),(-(0x16)),(sizeof((a))))   #define inf 0x3bbbbbbb   #define lnf 0x2bbbbbbbbbbbbbbbll   //#define sqrt divi   #define p2(i) (1ll<<(i))   #define readi read   #define readll read   /*************************************************/   int n,m,i,j,s=0,t=2001,px[1005],py[1005],dis[2005],inq[2005];   map<pair<int,int>,int> mp;   struct edg   {    int y;ll z;   }ee[1000005];   vector<int> e[2005];int tot;   ll sum;   void add(int x,int y,ll z)   {   // cerr<<x<<   <<y<<   <<z<<endl;    tot++;    e[x].pb(tot+tot-2);e[y].pb(tot+tot-1);    ee[tot+tot-2]=(edg){y,z};ee[tot+tot-1]=(edg){x,0};   }   bool bfs(int s,int t)   {    queue<int> qx;qx.push(s);    clz(dis);dis[s]=0;    while(!qx.empty()){     int x=qx.front();qx.pop();inq[x]=0;     fe(it,e[x])if(ee[*it].z){      if(dis[ee[*it].y]>dis[x]+1){       dis[ee[*it].y]=dis[x]+1;       if(!inq[ee[*it].y]){        qx.push(ee[*it].y);        inq[ee[*it].y]=1;       }      }     }    }    return dis[t]<dis[t+1];   }   ll dfs(int x,int tt,ll flw)   {    if(x==tt||flw==0)return flw;    ll res=0;    fe(it,e[x])if(ee[*it].z&&dis[ee[*it].y]==dis[x]+1){     ll t=dfs(ee[*it].y,tt,min(ee[*it].z,flw));     res+=t;flw-=t;ee[*it].z-=t;ee[(*it)^1].z+=t;     if(!flw)break;    }    if(flw) dis[x]=-1;    return res;   }   ll dinic(int s,int t)   {    ll ans=0;    while(bfs(s,t)){     ans+=dfs(s,t,lnf);    }    return ans;   }   int main()   {    read(n);    rep(i,n){     int z;read(py[i]);read(px[i]);read(z);     sum+=z;     add(i,i+n,z);mp[mk(px[i],py[i])]=i;    }    rep(i,n){     int a=abs(px[i]&1),b=abs(py[i]&1);     if(a==0){      if(b==1){       add(s,i,lnf);       if(mp.count(mk(px[i],py[i]-1))){        add(i+n,mp[mk(px[i],py[i]-1)],lnf);       }       if(mp.count(mk(px[i],py[i]+1))){        add(i+n,mp[mk(px[i],py[i]+1)],lnf);       }      }      else{       if(mp.count(mk(px[i]-1,py[i]))){        add(i+n,mp[mk(px[i]-1,py[i])],lnf);       }       if(mp.count(mk(px[i]+1,py[i]))){        add(i+n,mp[mk(px[i]+1,py[i])],lnf);       }      }     }     if(a==1){      if(b==0){       if(mp.count(mk(px[i],py[i]-1))){        add(i+n,mp[mk(px[i],py[i]-1)],lnf);       }       if(mp.count(mk(px[i],py[i]+1))){        add(i+n,mp[mk(px[i],py[i]+1)],lnf);       }      }      else{       add(i+n,t,lnf);      }     }    }    cout<<sum-dinic(s,t)<<endl;    return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:16777216 )   using namespace std;   const double eps = 1e-9;   const double pi = acos(-1.0);   int main() {     long long n;     cin >> n;     unsigned long long p = 1;     unsigned long long sq = (unsigned long long)(sqrt(n + 0.0) + eps + 1);     bool was = false;     while (p - 1 <= n) {       unsigned long long lf = 1;       unsigned long long rg = min(sq, n / p) + 5;       while (lf < rg - 1) {         unsigned long long mm = (lf + rg) >> 1;         unsigned long long m = 2 * mm - 1;         if ((p - 1) * m + m * (m - 1) / 2 <= n) {           lf = mm;         } else {           rg = mm;         }       }       lf = 2 * lf - 1;       if ((p - 1) * lf + lf * (lf - 1) / 2 == n) {         cout << p * lf << endl;         was = true;       }       p = p * 2;     }     if (!was) puts( -1 );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long t1, n, zero, one, cnt, cnt1, i, j;     string s;     cin >> t1;     while (t1--) {       cin >> n;       cnt = 0, zero = 0, one = 0;       for (i = 0; i < n; i++) {         cin >> s;         for (j = 0; j < s.length(); j++) {           if (s[j] ==  0 )             zero++;           else             one++;         }         if (s.length() % 2 != 0) cnt++;       }       cnt1 = 0;       if (zero % 2 != 0) cnt1++;       if (one % 2 != 0) cnt1++;       if (cnt >= cnt1)         cout << n << endl;       else         cout << n - 1 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     long long a[n], b[n];     for (long long i = 0; i < n; i++) {       cin >> a[i] >> b[i];     }     for (long long i = 0; i < n; i++) {       if (a[i] != b[i]) {         cout <<  rated ;         return 0;       }     }     for (long long i = 0; i < n - 1; i++) {       if (a[i + 1] > a[i]) {         cout <<  unrated ;         return 0;       }     }     cout <<  maybe ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int Mmax(long long int a, long long int b) {     if (a > b) return a;     return b;   }   long long int Mmin(long long int a, long long int b) {     if (a < b) return a;     return b;   }   long long int nod(long long int a, long long int b) {     while (a && b) {       if (a > b)         a %= b;       else         b %= a;     }     return Mmax(a, b);   }   long long int nok(long long int a, long long int b) {     return a * b / nod(a, b);   }   bool IsPrime(long long int x) {     if (x < 2) return false;     long long int X = sqrt(x), i;     for (i = 2; i <= X; i++)       if (x % i == 0) return false;     return true;   }   void hanoi(int n, int A, int C, int B) {     if (n == 1) {       cout << n <<     << A <<     << C << endl;     } else {       hanoi(n - 1, A, B, C);       cout << n <<     << A <<     << C << endl;       hanoi(n - 1, B, C, A);     }   }   string pr2(string a, int d) {     if (d == 0) return  0 ;     string b;     long long int sz = a.size(), i, prenos = 0;     for (i = 0; i < sz; i++) {       b[i] = a[i];     }     for (i = sz - 1; i > -1; i--) {       a[i] = ((b[i] -  0 ) * d + prenos) % 10 +  0 ;       prenos = ((b[i] -  0 ) * d + prenos) / 10;     }     if (prenos) a = char(prenos +  0 ) + a;     return a;   }   string sum(string a, string b) {     bool carry = false;     long long int i, sz1, sz2, maxsz, minsz;     string c, d;     sz1 = a.size();     sz2 = b.size();     maxsz = max(sz1, sz2);     minsz = min(sz1, sz2);     while (sz1 < maxsz) {       sz1++;       a =  0  + a;     }     while (sz2 < maxsz) {       sz2++;       b =  0  + b;     }     for (i = maxsz - 1; i > -1; i--) {       d = char((a[i] + b[i] - 96 + carry) % 10 + 48) + d;       if (a[i] + b[i] - 96 + carry > 9)         carry = true;       else         carry = false;     }     if (carry == true) d = char( 1 ) + d;     return d;   }   string pr(string a, string b) {     string res =  0 , p, p2;     int sz = a.size(), x = 0;     for (sz = a.size(); sz > 0; sz--, x++) {       int d = a[sz - 1] -  0 ;       a = a.erase(sz - 1, 1);       p2 = pr2(b, d);       p2 += p;       res = sum(res, p2);       p +=  0 ;     }     return res;   }   bool vesokosna(long long int x) {     return (x % 4 == 0 && x % 100 || x % 400 == 0);   }   long long int reverse(long long int x) {     long long int mirror = 0;     while (x) {       mirror = mirror * 10 + x % 10;       x /= 10;     }     return mirror;   }   long long int ost(string x, long long int k) {     long long int num = 0, i, sz = x.size();     for (i = 0; i < sz; i++) {       num = num * 10;       num += x[i] -  0 ;       num %= k;     }     return num;   }   int n;   int a[131072];   int b[131072];   vector<int> v;   int main() {     cin >> n;     int i;     for (i = 0; i < n; i++) cin >> a[i];     for (i = 0; i < n; i++) b[i] = a[i];     sort(b, b + n);     for (i = 0; i < n; i++) {       if (b[i] != a[i]) v.push_back(i);     }     if (v.size() > 2) {       cout <<  NO  << endl;     } else if (v.size() == 0) {       cout <<  YES  << endl;     } else {       swap(a[v[0]], a[v[1]]);       for (i = 0; i < n; i++) {         if (a[i] < a[i - 1]) {           cout <<  NO  << endl;           return 0;         }       }       cout <<  YES  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     string s1, s2;     cin >> s1 >> s2;     int flag = 0;     if (n % 2 != 0) flag = 1;     int mid;     if (flag)       mid = (n + 1) / 2;     else       mid = n / 2;     int cnt = 0;     for (int i = 0; i < n / 2; i++) {       int l = i, r = n - i - 1;       int a[30];       fill(a, a + 30, 0);       a[s1[l] -  a ]++;       a[s1[r] -  a ]++;       a[s2[l] -  a ]++;       a[s2[r] -  a ]++;       int k = 0;       int b[4];       int counts = 0;       for (int j = 0; j < 30; j++) {         if (a[j]) k++;         if (a[j]) {           b[counts++] = a[j];         }       }       sort(b, b + k);       if (k == 4) {         cnt += 2;         continue;       }       if (k == 3) {         if (s1[l] == s1[r])           cnt += 2;         else           cnt++;       }       if (k == 2) {         if (b[1] == 3) {           cnt++;         }       }     }     if (flag)       if (s1[mid - 1] != s2[mid - 1]) cnt++;     cout << cnt;   }


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


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   int isprime[1010];   int mp[256];   int main() {     isprime[0] = isprime[1] = 1;     for (int i = 2; i <= 1000; i++)       if (!isprime[i])         for (int j = i * 2; j <= 1000; j += i) isprime[j] = 1;     char s[1100], str[1010];     scanf( %s , s);     int l = strlen(s);     for (int i = 0; i < l; i++) {       mp[s[i]]++;       str[i] = 0;     }     str[l] = 0;     int flag = 0;     for (int i = 1; i <= l; i++) {       if (isprime[i]) continue;       int tmp = 0;       for (int j = 1; j <= l; j++) {         if (j % i == 0 && str[j]) {           tmp = str[j];           break;         }       }       if (tmp) {         int k = 1;         while (k * i <= l) {           if (str[k * i] == 0) {             mp[tmp]--;             if (mp[tmp] < 0) {               flag = 1;               break;             }           }           str[k * i] = tmp;           k++;         }       } else {         int p = max_element(mp, mp + 256) - mp;         int k = 1;         while (k * i <= l) {           if (str[k * i] == 0) {             mp[p]--;             if (mp[p] < 0) {               flag = 1;               break;             }           }           str[k * i] = p;           k++;         }       }       if (flag) break;     }     if (flag)       printf( NO n );     else {       printf( YES n );       for (int i = 1; i <= l; i++) {         if (!str[i]) {           int j =  a ;           while (mp[j] == 0) {             j++;           }           str[i] = j;           mp[j]--;         }       }       for (int i = 1; i <= l; i++) printf( %c , str[i]);       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void chkmin(T &a, T b) {     if (a > b) a = b;   }   template <class T>   inline void chkmax(T &a, T b) {     if (a < b) a = b;   }   inline int read() {     int s = 0, f = 1;     char ch = getchar();     while (!isdigit(ch) && ch !=  - ) ch = getchar();     if (ch ==  - ) ch = getchar(), f = -1;     while (isdigit(ch)) s = s * 10 + ch -  0 , ch = getchar();     return ~f ? s : -s;   }   const int maxn = 1e5 + 20;   int n, q, t;   long long now;   int fa[maxn], to[maxn];   int a[maxn];   int f[maxn], vis[maxn];   inline void init() {     n = read();     q = read();     t = read();     for (int i = (1), _end_ = (n); i <= _end_; i++) a[i] = read();     for (int i = (1), _end_ = (q); i <= _end_; i++) {       int x = read(), y = read();       to[x] = y;       fa[y] = x;     }     for (int i = (1), _end_ = (n); i <= _end_; i++)       if (!fa[i]) {         int x = i;         while (to[x]) {           if (vis[x]) puts( 0 ), exit(0);           vis[x] = 1;           now += a[x];           a[to[x]] += a[x];           x = to[x];         }         vis[x] = 1;       }     for (int i = (1), _end_ = (n); i <= _end_; i++)       if (!vis[i]) puts( 0 ), exit(0);   }   const int mod = 1e9 + 7;   inline void pls(int &a, int b) {     a += b;     a -= a >= mod ? mod : 0;   }   inline void doing() {     f[0] = 1;     for (int j = (1), _end_ = (n); j <= _end_; j++) {       for (int i = a[j]; i <= t - now; i++) pls(f[i], f[i - a[j]]);     }     if (now <= t)       printf( %d n , f[t - now]);     else       puts( 0 );   }   int main() {     init();     doing();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 35000, K = 50;   struct seg_node {     seg_node *lc, *rc;     int l, r, f, m;     seg_node(seg_node *lc, seg_node *rc, int l, int r)         : lc(lc), rc(rc), l(l), r(r), f(), m() {}     static seg_node *build(int l, int r) {       if (l == r - 1) return new seg_node(nullptr, nullptr, l, r);       int mid = (l + r) / 2;       return new seg_node(build(l, mid), build(mid, r), l, r);     }     void add(int val) {       f += val;       m += val;     }     void push_down() {       if (f > 0) {         lc->add(f);         rc->add(f);         f = 0;       }     }     void maintain() { m = max({m, lc->m, rc->m}); }     void add(int ql, int qr, int val) {       if (ql == l && qr == r)         add(val);       else {         push_down();         int mid = (l + r) / 2;         if (qr <= mid)           lc->add(ql, qr, val);         else if (ql >= mid)           rc->add(ql, qr, val);         else {           lc->add(ql, mid, val);           rc->add(mid, qr, val);         }         maintain();       }     }     int query(int ql, int qr) {       if (ql == l && qr == r)         return m;       else {         push_down();         int mid = (l + r) / 2;         if (qr <= mid)           return lc->query(ql, qr);         else if (ql >= mid)           return rc->query(ql, qr);         else           return max(lc->query(ql, mid), rc->query(mid, qr));       }     }   } * t[K + 1];   int a[N], last[N];   int main() {     ios::sync_with_stdio(false);     int n, k;     cin >> n >> k;     int cnt = 0;     t[1] = seg_node::build(0, n);     memset(last, 0xff, sizeof last);     for (int i = 0; i < n; i++) {       cin >> a[i];       a[i]--;       if (last[a[i]] == -1) {         cnt++;         last[a[i]] = i;       }       t[1]->add(i, i + 1, cnt);     }     for (int i = 2; i <= k; i++) {       t[i] = seg_node::build(0, n);       memset(last, 0xff, sizeof last);       for (int j = i - 1; j < n; j++) {         t[i - 1]->add(last[a[j]] == -1 ? i - 2 : last[a[j]], j, 1);         t[i]->add(j, j + 1, t[i - 1]->query(i - 2, j));         last[a[j]] = j;       }     }     cout << t[k]->query(n - 1, n);   }


#include <bits/stdc++.h>   using namespace std;   int n;   int a[222222];   set<int> s;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       s.insert(i);     }     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       if (a[i] != 0) {         s.erase(a[i]);       }     }     int idx = n;     int iidx = 0;     int iiidx = -1;     for (auto &x : s) {       while (a[idx] != 0) {         idx--;       }       a[idx] = x;       iidx = idx;       if (iiidx == -1) {         iiidx = idx;       }     }     for (int i = 1; i <= n; i++) {       if (a[i] == i) {         if (i == iidx) {           a[i] = a[iiidx];           a[iiidx] = i;         } else {           a[i] = a[iidx];           a[iidx] = i;         }         break;       }     }     for (int i = 1; i <= n; i++) {       printf( %d  , a[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     long long ans = 0;     int t = n;     for (int i = 2; i * i <= t; ++i) {       while (n % i == 0) {         ans += n;         n /= i;       }     }     ans += n == 1 ? 1 : n + 1;     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int freq[101];   int main() {     int n, x, max = 0, count = 0;     cin >> n;     string s;     if (n == 1) {       cin >> s;       sort(s.begin(), s.end());       for (int i = 0; i < s.length(); i++) {         if (s[i] == s[i + 1]) count++;       }       cout << count << endl;     } else if (n > 1) {       int arr[101] = {};       for (int i = 1; i <= n; i++) {         cin >> x;         freq[x]++;       }       for (int i = 0; i < 101; i++) {         if (freq[i] > max) max = freq[i];       }       cout << max;       return 0;     }   }


#include <bits/stdc++.h>   using namespace std;   const int AND = 0;   const int OR = 1;   const int NAND = 2;   const int NOR = 3;   const int N = 111;   int n, m, k;   string s;   int a[N][3];   int b[N][3];   vector<int> g[N];   bool st[N];   int ord[N];   int ordSz;   int col[N];   int C;   bool used[N];   bool oldUsed[N];   long long G[N];   int readType() {     cin >> s;     if (s ==  and ) return AND;     if (s ==  or ) return OR;     if (s ==  nand ) return NAND;     if (s ==  nor ) return NOR;     throw;   }   void read() {     cin >> n >> m >> k;     for (int i = 0; i < m; i++) {       a[i][0] = readType();       cin >> s;       int p = 1;       for (int j = 0; j < n; j++) {         if (s[j] ==  x ) {           a[i][p++] = j;         }       }     }     for (int i = 0; i < k; i++) {       b[i][0] = readType();       cin >> s;       int p = 1;       for (int j = 0; j < m; j++) {         if (s[j] ==  x ) {           b[i][p++] = j;         }       }     }   }   void addEdge(int v, int u) {     g[v ^ 1].push_back(u);     g[u ^ 1].push_back(v);   }   void addGraph(int v, int tp) {     tp = a[v][0] ^ (tp << 1);     if (tp == 0) {       st[2 * a[v][1] + 1] = 1;       st[2 * a[v][2] + 1] = 1;     } else if (tp == 1) {       addEdge(2 * a[v][1] + 1, 2 * a[v][2] + 1);     } else if (tp == 2) {       addEdge(2 * a[v][1], 2 * a[v][2]);     } else if (tp == 3) {       st[2 * a[v][1]] = 1;       st[2 * a[v][2]] = 1;     }   }   void dfsMark(int v) {     used[v] = 1;     for (int u : g[v]) {       if (!used[u]) dfsMark(u);     }   }   bool checkUsed() {     for (int i = 0; i < n; i++)       if (used[2 * i] && used[2 * i + 1]) return false;     return true;   }   bool checkSAT(long long MASK) {     for (int i = 0; i < 2 * n; i++) {       g[i].clear();       st[i] = 0;       used[i] = 0;     }     for (int i = 0; i < k; i++) {       if (((MASK >> i) & 1) == 0) continue;       for (int j = 1; j < 3; j++) addGraph(b[i][j], (int)(b[i][0] < 2));     }     for (int i = 0; i < 2 * n; i++)       if (st[i]) dfsMark(i);     if (!checkUsed()) return false;     for (int i = 0; i < n; i++) {       if (used[2 * i] || used[2 * i + 1]) continue;       for (int j = 0; j < 2 * n; j++) oldUsed[j] = used[j];       dfsMark(2 * i);       if (checkUsed()) continue;       for (int j = 0; j < 2 * n; j++) used[j] = oldUsed[j];       dfsMark(2 * i + 1);       if (!checkUsed()) return false;     }     return checkUsed();   }   int intVar[10];   int cntVar;   bool oper(int t, bool v1, bool v2) {     bool res = 0;     if (t >= 2) {       t ^= 2;       res ^= 1;     }     if (t == AND)       return res ^ (v1 && v2);     else       return res ^ (v1 || v2);   }   bool getValConst(int v, int MASK) {     for (int i = 0; i < cntVar; i++) {       if (intVar[i] == v) {         return (MASK >> i) & 1;       }     }     throw;   }   bool getValA1(int v, int MASK) {     return oper(a[v][0], getValConst(a[v][1], MASK), getValConst(a[v][2], MASK));   }   bool getValA2(int v, int MASK) {     return oper(a[v][0] ^ 2, getValConst(a[v][1], MASK),                 getValConst(a[v][2], MASK));   }   bool getValB1(int v, int MASK) {     return oper(b[v][0], getValA1(b[v][1], MASK), getValA1(b[v][2], MASK));   }   bool getValB2(int v, int MASK) {     return oper(b[v][0] ^ 2, getValA2(b[v][1], MASK), getValA2(b[v][2], MASK));   }   bool solve(int id1, int id2) {     cntVar = 0;     for (int i = 1; i < 3; i++)       for (int j = 1; j < 3; j++) {         intVar[cntVar++] = a[b[id1][i]][j];         intVar[cntVar++] = a[b[id2][i]][j];       }     sort(intVar, intVar + cntVar);     cntVar = unique(intVar, intVar + cntVar) - intVar;     for (int MASK = 0; MASK < (1 << cntVar); MASK++) {       if (getValB1(id1, MASK) && getValB2(id2, MASK)) return true;     }     return false;   }   bool isGoodMask(long long MASK) { return !checkSAT(MASK); }   void eraseFromG(int v, int u) {     if ((G[v] >> u) & 1) G[v] ^= 1LL << u;   }   int ANS = 0;   void brute(int curAns, long long curMask, long long cand, long long already) {     if (cand == 0) {       if (isGoodMask(curMask)) ANS = max(ANS, curAns);       return;     }     bool ok = true;     for (int i = 0; ok && i < k; i++) {       if (((already >> i) & 1) == 0) continue;       if ((G[i] & cand) == cand) ok = false;     }     if (!ok) return;     for (int v = 0; (1LL << v) <= cand; v++) {       if (((cand >> v) & 1) == 0) continue;       brute(curAns + 1, curMask | (1LL << v), (cand & G[v]) ^ (1LL << v),             already & G[v]);       already |= 1LL << v;       cand ^= 1LL << v;     }   }   int main() {     read();     for (int i = 0; i < k; i++) G[i] = (1LL << k) - 1;     for (int i = 0; i < k; i++)       for (int j = 0; j < k; j++) {         if (solve(i, j)) {           eraseFromG(i, j);           eraseFromG(j, i);         }       }     long long all = 0;     for (int i = 0; i < k; i++) {       if (((G[i] >> i) & 1) == 0) continue;       all |= 1LL << i;     }     brute(0, 0, all, 0);     if (ANS == 0)       cout << -1 << endl;     else       cout << k - ANS << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = INT_MAX;   const signed long long int LINF = LLONG_MAX;   const double EPS = 1e-9;   const int MOD = 1000000007;   const int MAXN = 100001;   const int ALPH = 26;   const int DALPH = 52;   string s;   vector<int> z;   vector<pair<int, int> > ans;   vector<int> add, sum;   int main() {     cin >> s;     z.resize((int)(s).size());     int l = 0, r = 0;     z[0] = (int)(s).size();     for (int i = 1; i < (int)(s).size(); ++i) {       int j = 0;       if (i <= r) {         int ind = i - l;         j = min(z[ind], r - i + 1);       }       for (; i + j < (int)(s).size(); ++j) {         if (s[j] != s[i + j]) {           break;         }       }       z[i] = j;       if (i + j - 1 > r) {         r = i + j - 1;         l = i;       }     }     add.resize((int)(s).size());     for (int i = 1; i < (int)(s).size(); ++i) {       add[0]++;       add[z[i]]--;     }     sum.resize((int)(s).size());     int cnt = 0;     for (int i = 0; i < (int)(s).size(); ++i) {       cnt += add[i];       sum[i] = cnt + 1;     }     for (int i = (int)(s).size() - 1; i >= 0; --i) {       if (i + z[i] != (int)(s).size()) {         continue;       }       ans.push_back(make_pair((z[i]), (sum[z[i] - 1])));     }     printf( %d n , (int)(ans).size());     for (int i = 0; i < (int)(ans).size(); ++i) {       printf( %d %d n , ans[i].first, ans[i].second);     }   }


#include <bits/stdc++.h>   using namespace std;   const int INF = (int)1e9;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     vector<int> xx(n);     vector<int> yy(n);     vector<int> diff(n);     for (int i = 0; i < n; i++) {       string s;       cin >> s;       xx[i] = count(s.begin(), s.end(),  B );       yy[i] = (int)s.size() - xx[i];       diff[i] = yy[i] - xx[i];     }     int maxX = *max_element(xx.begin(), xx.end());     int minX = *min_element(xx.begin(), xx.end());     int maxY = *max_element(yy.begin(), yy.end());     int minY = *min_element(yy.begin(), yy.end());     int max_diff = *max_element(diff.begin(), diff.end());     int min_diff = *min_element(diff.begin(), diff.end());     auto Works = [&](int dist) -> pair<int, int> {       int x1 = maxX - dist;       int x2 = minX + dist;       int y1 = maxY - dist;       int y2 = minY + dist;       int d1 = max_diff - dist;       int d2 = min_diff + dist;       auto Inside = [&](int x, int y) {         int d = y - x;         return (x1 <= x && x <= x2 && y1 <= y && y <= y2 && d1 <= d && d <= d2 &&                 x + y >= 1);       };       {         int x = x2;         int y = d1 + x;         y = max(y, y1);         y = min(y, y2);         if (Inside(x, y)) {           return {x, y};         }       }       {         int y = y2;         int x = y - d1;         x = max(x, x1);         x = min(x, x2);         if (Inside(x, y)) {           return {x, y};         }       }       return {-1, -1};     };     int low = 0, high = 5e5;     while (low < high) {       int mid = (low + high) / 2;       if (Works(mid).first != -1) {         high = mid;       } else {         low = mid + 1;       }     }     auto [k0, k1] = Works(low);     cout << low <<   n ;     cout << string(k0,  B ) + string(k1,  N ) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100100;   const int INF = 1000000000;   int n, m, numV;   int head[MAXN], e_cnt, s, t;   bool volunt[MAXN];   struct Edg {     int v, next;   } edg[MAXN * 4];   inline void add(int a, int b) {     edg[e_cnt].v = b;     edg[e_cnt].next = head[a];     head[a] = e_cnt++;   }   int dis[MAXN];   bool inq[MAXN];   bool spfa(int x) {     for (int i = 1; i <= n; ++i) dis[i] = INF;     dis[s] = 0;     queue<int> q;     q.push(s);     while (!q.empty()) {       int u = q.front();       q.pop();       inq[u] = 0;       if (volunt[u]) dis[u] = 0;       for (int i = head[u]; i != -1; i = edg[i].next) {         int v = edg[i].v;         if (dis[v] > dis[u] + 1 && dis[u] + 1 <= x) {           dis[v] = dis[u] + 1;           if (!inq[v]) q.push(v);           inq[v] = 1;         }       }     }     return dis[t] != INF;   }   int main() {     while (scanf( %d%d%d , &n, &m, &numV) != EOF) {       memset(volunt, 0, sizeof(volunt));       e_cnt = 0;       memset(head, -1, sizeof(head));       for (int i = 0, j; i < numV; ++i) {         scanf( %d , &j);         volunt[j] = 1;       }       for (int i = 0; i < m; ++i) {         int ta, tb;         scanf( %d%d , &ta, &tb);         add(ta, tb);         add(tb, ta);       }       scanf( %d%d , &s, &t);       int rt(m), lt(1), ans(-1);       while (lt <= rt) {         int mid = (lt + rt) >> 1;         if (spfa(mid)) {           rt = mid - 1;           ans = mid;         } else           lt = mid + 1;       }       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   vector<pair<int, int> > edges;   vector<int> conn[201];   bool v1[201], v2[201];   int mx = 0;   void setEdge(int a, int b, int v) {     for (int s = 0; s < conn[a].size(); s++) {       if (conn[a][s] == b || conn[a][s] == -1) conn[a][s] = (v == 1 ? b : -1);     }     for (int s = 0; s < conn[b].size(); s++) {       if (conn[b][s] == a || conn[b][s] == -1) conn[b][s] = (v == 1 ? a : -1);     }   }   int getHeight(int i) {     v2[i] = 1;     int ret = 0;     for (int j = 0; j < conn[i].size(); j++) {       if (conn[i][j] != -1 && !v2[conn[i][j]]) {         ret = max(ret, 1 + getHeight(conn[i][j]));       }     }     v2[i] = 0;     return ret;   }   int longestPath(int i) {     v1[i] = 1;     int ret = 0;     int cnt = 0;     for (int j = 0; j < conn[i].size(); j++) {       if (conn[i][j] != -1 && !v1[conn[i][j]]) {         ret = max(ret, longestPath(conn[i][j]));         cnt++;       }     }     if (cnt == 0) ret = max(ret, getHeight(i));     v1[i] = 0;     return ret;   }   int main() {     cin >> n;     memset(v1, 0, sizeof v1);     memset(v2, 0, sizeof v2);     int a, b;     for (int i = 0; i < n - 1; i++) {       cin >> a >> b;       edges.push_back(make_pair(a - 1, b - 1));       conn[a - 1].push_back(b - 1);       conn[b - 1].push_back(a - 1);     }     for (int i = 0; i < edges.size(); i++) {       a = edges[i].first;       b = edges[i].second;       setEdge(a, b, 0);       mx = max(mx, longestPath(a) * longestPath(b));       setEdge(a, b, 1);     }     cout << mx << endl;   }


#include<bits/stdc++.h>   using namespace std;       typedef long long ll;   typedef vector< ll > vl;   typedef vector< vector< ll > > vvl;   #define ff first   #define ss second   #define boost ios_base::sync_with_stdio(false); cin.tie(NULL);   cout.tie(NULL);   #define pb push_back   #define all(c) c.begin(), c.end()   #define tr(c,it) for(auto it=(c).begin(); it!=(c).end(); it++)   #define ref(i,n)    for(i=0;i<n;i++)   #define mp make_pair   #define endl   n    #define MOD 1000000007   ll power(ll a, ll b){//a^b       ll res=1;       a=a%MOD;       while(b>0){           if(b&1){res=(res*a)%MOD;b--;}           a=(a*a)%MOD;           b>>=1;       }       return res;   }   ll fermat_inv(ll y){return power(y,MOD-2);}   ll gcd(ll a, ll b){return (b==0)?a:gcd(b,a%b);}      int main()   {       boost       ll t;       cin>>t;       while(t--)       {           ll n,x=0,y=0,flag=0,m,i;           cin>>n;           string s;           cin>>s;           ref(i,n)           {               if(s[i]== T )                   x++;               else               {                   y++;               }               if(y>x)               {                   flag=1;                   break;               }           }           if(flag==1 || x!=2*y)           {               cout<< NO <<endl;               continue;           }           x=y=flag=0;           for(i=n-1;i>=0;i--)           {               if(s[i]== T )                   x++;               else               {                   y++;               }               if(y>x)               {                   flag=1;                   break;               }           }           if(flag)               cout<< NO <<endl;           else               cout<< YES <<endl;       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio();     cin.tie();     cout.tie();     int n;     cin >> n;     int weight[n];     for (int i = 0; i < n; ++i) cin >> weight[i];     int p;     cin >> p;     vector<pair<pair<int, int>, int> > data;     for (int i = 0; i < p; ++i) {       int a, b;       cin >> a >> b;       --a;       data.push_back({{b, a}, i});     }     sort(data.begin(), data.end());     vector<pair<int, long long> > answer;     long long dp[n];     int prev = -1;     for (int i = 0; i < p; ++i) {       int b = data[i].first.first, a = data[i].first.second;       if (b >= 500) {         long long sum = 0;         for (; a < n; a += b) sum += weight[a];         answer.push_back({data[i].second, sum});         prev = -1;         continue;       }       if (prev == -1 || b != prev) {         for (int j = n - 1; j >= 0; --j) {           if (j + b > n - 1)             dp[j] = weight[j];           else             dp[j] = weight[j] + dp[j + b];         }       }       answer.push_back({data[i].second, dp[a]});       prev = b;     }     sort(answer.begin(), answer.end());     for (int i = 0; i < p; ++i) cout << answer[i].second <<   n ;     return 0;   }


#define _CRT_SECURE_NO_WARNINGS   #include <iostream>   #include<time.h>   #include <cstdio>   #include <cstring>   #include <cstdlib>   #include <cmath>   #include <algorithm>   #include <queue>   #include <stack>   #include <unordered_map>   #include <unordered_set>   #include <stdio.h>   #include <deque>   #include <map>   #include <set>   #include <string>   #define ll long long   typedef long long LL;   typedef unsigned long long ULL;   //typedef long double ld;   using namespace std;   const int MAXN = 2e6 + 5000;   const long double eps = (1e-12);   const int logn = 21;   const double Pi = acos(-1.0);   const LL pp = 1000000000 + 9;   int lowbit(int x) { return x & (-x); }   #define MP(i,j) make_pair(i,j)   #define M(x) ((((x)%pp)+pp)%pp)   //#define int LL   #define P pair<int,int>   const int INF = 1e9;      int toright[505][505];   int todown[505][505];      int dx[5] = { 0,0,0,-1,1 };   int dy[5] = { 0,-1,1,0,0 };   int dp[505][505][25];      int getww(int x1, int y1, int x2, int y2) {       if (x2 == x1 + 1) {           return todown[x1][y1];       }       else if (x2 == x1 - 1) {           return todown[x2][y1];       }       else if (y2 == y1 + 1) {           return toright[x1][y1];       }       else if (y2 == y1 - 1) {           return toright[x1][y2];       }       return -1;   }      signed main() {       int n, m, k;       scanf( %d%d%d , &n, &m, &k);       for (int i = 1; i <= n; i++) {           for (int j = 1; j <= m - 1; j++) {               scanf( %d , &toright[i][j]);           }       }       for (int i = 1; i <= n - 1; i++) {           for (int j = 1; j <= m; j++) {               scanf( %d , &todown[i][j]);           }       }       if (k % 2 == 1) {           for (int i = 1; i <= n; i++) {               for (int j = 1; j <= m; j++) {                   printf( -1  );               }               printf(  n );           }       }       else {           k /= 2;              for (int kk = 1; kk <= k; kk++) {               for (int i = 1; i <= n; i++) {                   for (int j = 1; j <= m; j++) {                       dp[i][j][kk] = INF;                   }               }           }              for (int kk = 1; kk <= k; kk++) {               for (int i = 1; i <= n; i++) {                   for (int j = 1; j <= m; j++) {                       for (int l = 1; l <= 4; l++) {                           int x = i + dx[l]; int y = j + dy[l];                           if (x<1 || x>n || y<1 || y>m) continue;                           int w = getww(i, j, x, y);                           dp[i][j][kk] = min(dp[i][j][kk], w + dp[x][y][kk - 1]);                       }                   }               }           }              for (int i = 1; i <= n; i++) {               for (int j = 1; j <= m; j++) {                   printf( %d  , dp[i][j][k] * 2);               }               printf(  n );           }       }             return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 300000 + 5;   int a[maxn], dp[maxn];   int n, k;   bool ok_hack(int x) {     memset(dp, 0, sizeof(dp));     int last = 0;     for (int i = k; i <= n; i++) {       if (a[i] - a[dp[i - k] + 1] <= x) last = i;       dp[i] = last;     }     return dp[n] == n;   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     sort(a + 1, a + n + 1);     a[0] = 0;     int l = 0, r = a[n] - a[1];     while (l < r) {       int mid = r + l >> 1;       if (ok_hack(mid))         r = mid;       else         l = mid + 1;     }     printf( %d n , l);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC optimize( unroll-loops )   #pragma GCC target( sse,sse2,sse3,ssse3,abm,mmx,tune=native )   using namespace std;   const int64_t INF = (int64_t)(2e18);   const int maxn = 500 * 1000 + 100;   chrono::time_point<chrono::steady_clock> cl;   double current_time() {     return (double)(chrono::steady_clock::now() - cl).count() / 1e9;   }   void no() {     cout <<  0 ;     exit(0);   }   void inf() {     cout <<  inf ;     exit(0);   }   void ans(long long a) {     cout << a;     exit(0);   }   int32_t main() {     cl = chrono::steady_clock::now();     ios_base::sync_with_stdio(false);     cout << fixed << setprecision(10);     cin.tie(nullptr);     long long x, y, z;     cin >> x >> y >> z;     if (x == y && y != z) no();     if (x != y && y == z) ans(1);     if (x == z && y != z) no();     if (x == y && y == z) {       if (x == 1) inf();       if (x != 1) ans(2);     }     if (x > y) no();     if (y > z) no();     bool flag = false;     if (x == 1ll) {       flag = true;       long long zz = z;       while (z > 1) {         if (z % y != 0) flag = false;         z /= y;       }       z = zz;     }     vector<int> a;     long long zcp = z;     while (zcp > 0) {       a.push_back(zcp % y);       zcp /= y;     }     long long cur = 0;     long long curx = 1;     for (auto& it : a) {       cur += it * curx;       curx *= x;     }     int res = 0;     if (cur == y) ++res;     if (flag) ++res;     cout << bool(res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ii = pair<ll, ll>;   int main() {     char rep[256], a[256], b[256], d[256];     while (scanf( %[0-9].%[0-9]e%[0-9] n , a, d, b) == 3) {       auto len = strlen(d);       if (b[0] ==  0 ) {         if (len == 1 and d[0] ==  0 )           printf( %s n , a);         else           printf( %s.%s n , a, d);         continue;       }       if (a[0] ==  0 ) a[0] = 0;       while (len > 0 and d[len - 1] ==  0 ) --len;       d[len] = 0;       int digits = 0;       for (auto p = a; *p; ++p) rep[digits++] = *p;       int n = atoi(b);       auto p = d;       for (; *p and n; ++p) {         rep[digits++] = *p;         --n;       }       rep[digits] = 0;       if (n == 0) {         printf( %s , rep);         if (*p) {           printf( . );           while (*p) {             printf( %c , *p);             ++p;           }         }         printf(  n );       } else {         printf( %s , rep);         while (n--) printf( 0 );         printf(  n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long M = 1e9 + 7;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     long long t;     cin >> t;     while (t--) {       long long n;       cin >> n;       long long a[n];       map<long long, long long> m;       map<long long, long long> m1;       map<long long, long long> m2;       map<long long, long long> m3;       vector<pair<long long, long long>> v;       for (long long i = 0; i < n; i++) {         cin >> a[i];         m2[a[i]]++;       }       for (auto it = m2.begin(); it != m2.end(); it++) {         m3[it->second]++;       }       for (long long i = 0; i < n - 1; i++) {         long long x = ++m[a[i]];         m1[x]++;         m1[x - 1]--;         long long y = m2[a[i]];         if (y == 1) {           m2.erase(m2.find(a[i]));         } else           m2[a[i]]--;         m3[y]--;         m3[y - 1]++;         auto it = m.end();         it--;         x = it->first;         it = m2.end();         it--;         y = it->first;         if (x == m.size() && y == m2.size() && m1[1] == x && m3[1] == y) {           v.push_back({x, y});         }       }       cout << v.size() <<   n ;       for (long long i = 0; i < (long long)v.size(); i++) {         cout << v[i].first <<     << v[i].second <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int x, h, id;   } a[100005];   int n, i, j, f[100005], ans[100005];   bool cmp(node x, node y) {     if (x.x != y.x)       return x.x < y.x;     else       return x.h < y.h;   }   int main() {     scanf( %d , &n);     for (i = 1; i <= n; i++) {       scanf( %d%d , &a[i].x, &a[i].h);       a[i].id = i;     }     sort(a + 1, a + n + 1, cmp);     for (i = n; i >= 1; i--) {       f[i] = 1;       for (j = i + 1; j <= n; j += f[j]) {         if (a[i].x + a[i].h - 1 >= a[j].x)           f[i] += f[j];         else           break;       }       ans[a[i].id] = f[i];     }     for (i = 1; i <= n; i++) printf( %d  , ans[i]);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     map<char, int> ma;     char a;     int b;     cin >> b;     for (int i = 0; i < b; i++) {       cin >> a;       ma[a]++;     }     for (auto i : ma)       for (int j = 0; j < i.second; j++) cout << i.first;   }


#include <bits/stdc++.h>   using namespace std;   double x;   int N, M, ar[5005], res;   int dn[5005];   int main() {     scanf( %d %d , &N, &M);     for (int i = 0; i < N; i++) {       scanf( %d %lf , &ar[i], &x);       ar[i]--;     }     for (int i = 0; i < N; i++) {       for (int j = ar[i]; j >= 0; j--) {         dn[ar[i]] = max(dn[ar[i]], 1 + dn[j]);       }     }     for (int i = 0; i < M; i++) {       res = max(res, dn[i]);     }     printf( %d n , N - res);   }


#include <bits/stdc++.h>   const int MN = 1e3 + 10;   const int K = 20;   int N, M, T, id[MN], t[MN], ctr, m[1 << K];   bool conn[MN][MN];   char s[10];   std::vector<int> a[MN], on[K + 1];   struct Mod {    public:     char c;     int a, b;     void out() const { printf( %c %d %d n , c, a, b); }   };   std::vector<Mod> f;   void flip(int a, int b) {     f.push_back({conn[a][b] ?  -  :  + , a, b});     conn[a][b] ^= 1;   }   void dfs(int n) {     for (int x : a[n])       if (!~id[x]) dfs(x);     id[n] = ctr;     t[ctr] = n;     ++ctr;   }   int main() {     memset(id, -1, sizeof id);     scanf( %d%d%d , &N, &M, &T);     for (int i = 0, u, v; i < M; ++i)       scanf( %d%d , &u, &v), a[u].push_back(v), conn[u][v] = 1;     ctr = 0;     for (int i = 1; i <= N; ++i)       if (!~id[i]) dfs(i);     for (int i = std::min(N, K) - 1; i; --i)       for (int j = i - 1; j >= 0; --j)         if (!conn[t[i]][t[j]]) {           a[t[i]].push_back(t[j]);           conn[t[i]][t[j]] = 1;           f.push_back({ + , t[i], t[j]});         }     for (int i = 0; i < 1 << K; ++i)       if (__builtin_popcount(i) <= 3) on[__builtin_popcount(i)].push_back(i);     memset(m, -1, sizeof m);     for (int i = K; i < N; ++i) {       f.push_back({ + , t[i], t[i]});       int n = t[i], v = 0;       for (int x : a[n])         if (id[x] < K) v |= 1 << id[x];       bool ok = 0;       for (int j = 0; !ok; ++j) {         assert(j <= 3);         for (int k : on[j])           if (!~m[v ^ k]) {             m[v ^ k] = n;             for (int b = 0; b < K; ++b)               if (k >> b & 1) flip(n, t[b]);             ok = 1;             break;           }       }     }     printf( %u n , f.size());     for (int i = 0; i < f.size(); ++i) f[i].out();     fflush(stdout);     for (int i = 0; i < T; ++i) {       int v = 0;       bool lose = 0;       for (int j = 0; j < std::min(N, K); ++j) {         printf( ? 1 %d n , t[j]);         fflush(stdout);         scanf(  %s , s);         if (s[0] ==  S ) return 0;         if (s[0] ==  W ) v |= 1 << j;         if (s[0] ==  L ) {           lose = 1, v = j;           break;         }       }       if (lose)         printf( ! %d n , t[v]);       else         printf( ! %d n , m[v]);       fflush(stdout);       scanf(  %s , s);       if (s[0] ==  W ) return 0;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n;   long long b[500010], a[500010];   void solve() {     cin >> n;     for (int i = 0; i < n; i++) cin >> b[i];     long long sum = 0, flag = 1;     for (int i = 0; i < n; i++) sum += b[i];     if (sum % (n * (n + 1) / 2)) flag = 0;     sum /= n * (n + 1) / 2;     for (int i = 0; i < n; i++) {       long long dx = sum - b[(i + 1) % n] + b[i];       if (dx % n || dx <= 0)         flag = 0;       else         a[(i + 1) % n] = dx / n;     }     if (flag) {       cout <<  YES  <<   n ;       for (int i = 0; i < n; i++) cout << a[i] <<    ;       cout <<   n ;     } else       cout <<  NO  <<   n ;   }   int main() {     std::ios::sync_with_stdio(false);     std::cin.tie(0);     std::cout.tie(0);     int _;     cin >> _;     while (_--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 3e5;   const int INF = 1e9;   int n;   int ct[MAXN][2];   vector<int> ok(int d) {     vector<int> bad(2, -1);     int loX = 0, hiX = INF;     int loY = 0, hiY = INF;     int loD = -INF, hiD = INF;     for (int i = 0; i < n; i++) {       int x = ct[i][0], y = ct[i][1];       loX = max(loX, x - d);       hiX = min(hiX, x + d);       loY = max(loY, y - d);       hiY = min(hiY, y + d);       loD = max(loD, y - x - d);       hiD = min(hiD, y - x + d);     }     if (loX > hiX || loY > hiY || loD > hiD) return bad;     auto check1 = [&](int x) {       int lo = max(loY, loD + x), hi = min(hiY, hiD + x);       if (lo > hi) return bad;       if (x || hi) return vector<int>{x, hi};       return bad;     };     auto check2 = [&](int y) {       int lo = max(loX, y - hiD), hi = min(hiX, y - loD);       if (lo > hi) return bad;       if (y || hi) return vector<int>{hi, y};       return bad;     };     if (bad != check1(loX)) return check1(loX);     if (bad != check1(hiX)) return check1(hiX);     if (bad != check2(loY)) return check2(loY);     if (bad != check2(hiY)) return check2(hiY);     return bad;   }   void run_case() {     cin >> n;     for (int i = 0; i < n; i++) {       string s;       cin >> s;       for (char &j : s) {         ct[i][j ==  N ]++;       }     }     int lo = -1;     int hi = 1e6;     while (lo + 1 < hi) {       int mid = (lo + hi) / 2;       if (ok(mid)[0] != -1) {         hi = mid;       } else {         lo = mid;       }     }     vector<int> res;     res = ok(hi);     cout << hi <<   n ;     string ans = string(res[0],  B ) + string(res[1],  N );     cout << ans <<   n ;   }   int main() {     ios::sync_with_stdio(false), cin.tie(0);     if (fopen( input.in ,  r ))       freopen( input.in ,  r , stdin), freopen( output.out ,  w , stdout);     int t;     t = 1;     while (t--) {       run_case();     }   }