#include <bits/stdc++.h>   using namespace std;   int n, m, data[510];   double ans;   int main() {     scanf( %d%d , &n, &m);     if (m == 0) {       printf( %.9lf n , 0);       return 0;     }     for (int i = 1; i <= n; i++) scanf( %d , &data[i]);     for (int i = 1, x, y, z; i <= m; i++) {       scanf( %d%d%d , &x, &y, &z);       ans = max(ans, (double)(data[x] + data[y]) / z);     }     printf( %.9lf , ans);   }


#include <bits/stdc++.h>   using namespace std;   int a[1000006], BLOCK;   long long ans[200005], answer = 0;   int asdf[1000006];   struct Node {     int L, R, ind;   } q[200005];   bool cmp(Node x, Node y) {     if (x.L / BLOCK != y.L / BLOCK)       return x.L / BLOCK < y.L / BLOCK;     else       return x.R < y.R;   }   void add(int pos) {     answer -= 1ll * asdf[a[pos]] * asdf[a[pos]] * a[pos];     asdf[a[pos]]++;     answer += 1ll * asdf[a[pos]] * asdf[a[pos]] * a[pos];   }   void remove(int pos) {     answer -= 1ll * asdf[a[pos]] * asdf[a[pos]] * a[pos];     asdf[a[pos]]--;     answer += 1ll * asdf[a[pos]] * asdf[a[pos]] * a[pos];   }   int main() {     int n, t;     cin >> n >> t;     BLOCK = sqrt(n);     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i <= t; i++) {       q[i].ind = i;       cin >> q[i].L >> q[i].R;     }     sort(q + 1, q + t + 1, cmp);     int curL = 0, curR = 0;     for (int i = 1; i <= t; i++) {       while (curL < q[i].L) {         remove(curL);         curL++;       }       while (curL > q[i].L) {         add(curL - 1);         curL--;       }       while (curR > q[i].R + 1) {         remove(curR - 1);         curR--;       }       while (curR <= q[i].R) {         add(curR);         curR++;       }       ans[q[i].ind] = answer;     }     for (int i = 1; i <= t; i++) cout << ans[i] <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   long long isPalindrome(string str) {     long long l = 0;     long long h = str.length() - 1;     while (h > l) {       if (str[l++] != str[h--]) {         return -1;       }     }     return 1;   }   int32_t main() {     string s;     cin >> s;     long long n = s.size();     string ans =  YES ;     for (long long i = 0; i < n; i++) {       if (s[i] ==  A  || s[i] ==  H  || s[i] ==  I  || s[i] ==  M  ||           s[i] ==  O  || s[i] ==  T  || s[i] ==  U  || s[i] ==  V  ||           s[i] ==  W  || s[i] ==  X  || s[i] ==  Y ) {       } else {         ans =  NO ;       }     }     if (isPalindrome(s) == 1 && ans ==  YES ) {       cout << ans << endl;       ;     } else {       cout <<  NO  << endl;       ;     }   }


#include <bits/stdc++.h>   using namespace std;   pair<int, int> arr[100001];   int main() {     int n, d, a, b;     cin >> n >> d >> a >> b;     for (int i = 0; i < n; i++) {       int c, d;       cin >> c >> d;       arr[i].first = c * a + d * b;       arr[i].second = i + 1;     }     sort(arr, arr + n);     long long counter = 0;     int in = n;     for (int i = 0; i < n; i++) {       counter += arr[i].first;       if (counter > d) {         in = i;         break;       }     }     cout << in << endl;     for (int i = 0; i < in; i++) {       cout << arr[i].second <<    ;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void print1d(vector<long long> &A) {     for (auto i : A) cout << i <<    ;     cout << endl;   }   void print2d(vector<vector<long long>> &A) {     for (auto i : A) {       for (auto j : i) cout << j <<    ;       cout << endl;     }   }   void printadj(unordered_map<long long, vector<long long>> &mp) {     for (auto it : mp) {       cout << it.first <<   -->  ;       for (long long i = 0; i < it.second.size(); i++) {         cout << it.second[i] <<    ;       }       cout << endl;     }   }   int32_t main() {     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);     long long n, m;     cin >> n >> m;     long long a, b, c;     map<long long, long long> mp;     for (long long i = 0; i < m; i++) {       set<long long> s;       s.insert(1);       s.insert(2);       s.insert(3);       cin >> a >> b >> c;       if (mp.find(a) != mp.end()) {         s.erase(mp[a]);         if (mp[a] == 1) {           mp[b] = 2;           mp[c] = 3;         } else if (mp[a] == 2) {           mp[b] = 1;           mp[c] = 3;         } else if (mp[a] == 3) {           mp[b] = 1;           mp[c] = 2;         }       } else if (mp.find(b) != mp.end()) {         s.erase(mp[b]);         if (mp[b] == 1) {           mp[a] = 2;           mp[c] = 3;         } else if (mp[b] == 2) {           mp[a] = 1;           mp[c] = 3;         } else if (mp[b] == 3) {           mp[a] = 1;           mp[c] = 2;         }       } else if (mp.find(c) != mp.end()) {         s.erase(mp[c]);         if (mp[c] == 1) {           mp[a] = 2;           mp[b] = 3;         } else if (mp[c] == 2) {           mp[a] = 1;           mp[b] = 3;         } else if (mp[c] == 3) {           mp[a] = 1;           mp[b] = 2;         }       } else {         mp[a] = 1;         mp[b] = 2;         mp[c] = 3;       }     }     for (auto it : mp) {       cout << it.second <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1510;   const int MOD = 1000000007;   const int SZ = 100010;   inline int add(int u, int v) { return (u += v) >= MOD ? u - MOD : u; }   inline int sub(int u, int v) { return (u -= v) < 0 ? u + MOD : u; }   inline int mul(int u, int v) { return (long long)u * v % MOD; }   inline int power(int u, int v) {     int res = 1;     while (v) {       if (v & 1) res = mul(res, u);       u = mul(u, u);       v >>= 1;     }     return res;   }   inline int inv(int u) { return power(u, MOD - 2); }   int n, m, a, b, k, p;   int dp[N][N];   int pref[N][N];   int sumG[N], g[N];   int fac[SZ], invFac[SZ];   int comb(int n, int k) {     if (k > n || k < 0) return 0;     return mul(fac[n], mul(invFac[k], invFac[n - k]));   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n >> m;     cin >> a >> b;     cin >> k;     p = mul(a, inv(b));     fac[0] = 1;     for (int i = 1; i < SZ; i++) {       fac[i] = mul(fac[i - 1], i);     }     invFac[SZ - 1] = inv(fac[SZ - 1]);     for (int i = SZ - 2; i >= 0; i--) {       invFac[i] = mul(invFac[i + 1], i + 1);     }     for (int i = 0; i <= m; i++) {       g[i] = comb(k, i);       if (g[i] != 0) {         g[i] = mul(g[i], power(p, i));         g[i] = mul(g[i], power(sub(1, p), k - i));       }       sumG[i] = g[i];       if (i > 0) sumG[i] = add(sumG[i], sumG[i - 1]);     }     memset(dp, 0, sizeof dp);     memset(pref, 0, sizeof pref);     dp[0][m] = 1;     for (int i = 1; i <= n; i++) {       for (int r = 1; r <= m; r++) {         int p1 = mul(sumG[r - 1], sub(dp[i - 1][m], dp[i - 1][m - r]));         int p2 = pref[i - 1][r - 1];         int foo = mul(sub(p1, p2), g[m - r]);         dp[i][r] = add(dp[i][r - 1], foo);         pref[i][r] = add(pref[i][r - 1], mul(g[r], dp[i][r]));       }     }     cout << dp[n][m] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[101], k, i, n, check[101][101], j = 0, kil1, kil2, ans;   int main() {     cin >> n >> k;     for (i = 1; i <= n; i++) {       cin >> a[i];     }     for (i = 1; i <= k; i++) {       kil1 = 0;       kil2 = 0;       for (j = i; j <= n; j += k) {         if (a[j] == 1)           kil1++;         else           kil2++;       }       ans += min(kil1, kil2);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, c = 0;     cin >> n;     int a[n], b[n];     map<int, int> mp;     for (int i = 1; i <= 1001; i++) {       mp[i] = 0;     }     for (int i = 0; i < n; i++) {       cin >> a[i];     }     for (int i = n - 1; i >= 0; i--) {       if (mp[a[i]] == 0) {         mp[a[i]] = 1;         b[c] = a[i];         c++;       }     }     reverse(b, b + c);     cout << c << endl;     for (int i = 0; i < c; i++) cout << b[i] <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1.0);   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     char x;     int min, max;     cin >> x >> min >> max;     if (min > max) {       swap(min, max);     }     for (int i = 0; i < n - 1; i += 1) {       char y;       int m, n;       cin >> y >> m >> n;       if (y ==  + ) {         if (m > n) {           swap(m, n);         }         if (m > min) {           min = m;         }         if (n > max) {           max = n;         }       } else {         if (m > n) {           swap(m, n);         }         if (m >= min && n >= max) {           cout <<  YES  << endl;         } else {           cout <<  NO  << endl;         }       }     }   }


#include <bits/stdc++.h>   using ll = long long;   using namespace std;   void solve() {     ll n, k;     cin >> n >> k;     int mn = 2e9;     for (int i = 1; i < sqrt(n) + 1; i++) {       if (n % i == 0) {         int y = i * k;         int x = n / i * k;         for (int j = 0; y / k == i; j++) {           if ((y % k) * (y / k) == n) {             mn = min(mn, y);             break;           }           y++;         }         for (int j = 0; x / k == n / i; j++) {           if ((x % k) * (x / k) == n) {             mn = min(mn, x);             break;           }           x++;         }       }     }     cout << mn << endl;   }   int main() { solve(); }


#include <bits/stdc++.h>   using namespace std;   const long double eps = 1e-9;   const long double pi = acos(-1.0);   const long double inf = 1e+9;   long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }   int xabs(int a) { return a >= 0 ? a : -a; }   int sign(int a) { return (a < 0) ? -1 : 1; }   int main() {     int n, m;     cin >> n >> m;     vector<int> a(n + 1), b(m + 1);     for (int i = 0; i < n + 1; i++) cin >> a[i];     for (int i = 0; i < m + 1; i++) cin >> b[i];     if (n > m) {       if (a[0] < 0 && b[0] > 0) cout <<  - ;       if (a[0] > 0 && b[0] < 0) cout <<  - ;       cout <<  Infinity  << endl;     }     if (n < m) {       cout <<  0/1  << endl;     }     if (n == m) {       int g = gcd(a[0], b[0]);       a[0] /= g;       b[0] /= g;       if (b[0] < 0) {         a[0] *= -1;         b[0] *= -1;       }       cout << a[0] <<  /  << b[0] << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     int nMin;     int ans = 0;     int n;     int inter[150001];     cin >> t;     while (t--) {       ans = 0;       cin >> n;       for (int i = 0; i < n; i++) {         cin >> inter[i];       }       for (int i = n - 1; i >= 0; i--) {         if (i == n - 1) {           nMin = inter[i];         } else {           if (inter[i] <= nMin) {             nMin = inter[i];           } else {             ans++;           }         }       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:1024000000,1024000000 )   using namespace std;   const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;   int n, a[55][55], ans[55];   int main() {     ios_base::sync_with_stdio(0);     while (scanf( %d , &n) == 1) {       for (int i = 1; i <= n; ++i)         for (int j = 1; j <= n; ++j) scanf( %d , &a[i][j]);       for (int i = 1; i <= n; ++i) {         set<int> s;         for (int j = 1; j <= n; ++j) s.insert(a[i][j]);         if (s.size() != n) continue;         for (int j = 1; j <= n; ++j) ans[j] = a[i][j] ? a[i][j] : n;         bool ok = true;         for (int j = 1; j <= n; ++j) {           for (int k = 1; k <= n; ++k) {             if (j == k) continue;             if (min(ans[j], ans[k]) != a[j][k]) {               ok = false;               break;             }           }           if (!ok) break;         }         if (ok) break;       }       for (int i = 1; i <= n; ++i) printf( %d%c , ans[i],    n [i == n]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   long long val[2001000];   int f[2001000], pre[2001000];   void getans(long long F) {     for (int i = 1; i <= n + n; i++) f[i] = 0, pre[i] = i;     int pl = 1;     int ans = 0x7fffffff;     for (int i = n + 1; i <= n + n; i++) {       while (pl < i && val[i] - val[pl] > F) pl++;       f[i] = f[pl] + 1;       pre[i] = pre[pl];       if (i - pre[i] >= n) ans = min(ans, f[i]);     }     printf( %d n , ans);   }   void solve() {     int q;     scanf( %d%d , &n, &q);     for (int i = 1; i <= n; i++) scanf( %lld , &val[i]), val[i + n] = val[i];     for (int i = 2; i <= n + n; i++) val[i] += val[i - 1];     for (int i = 1; i <= q; i++) {       long long t;       scanf( %lld , &t);       getans(t);     }   }   int main() {     solve();     fclose(stdin);     fclose(stdout);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long n;     cin >> n;     if (n < 3) {       cout << -1;     } else {       long long a[n][n];       long long offset = n * n - 9;       a[0][0] = 9 + offset;       a[0][1] = 5 + offset;       a[0][2] = 6 + offset;       a[1][0] = 7 + offset;       a[1][1] = 2 + offset;       a[1][2] = 8 + offset;       a[2][0] = 1 + offset;       a[2][1] = 3 + offset;       a[2][2] = 4 + offset;       long long x = 0;       for (long long j = n - 1; j > (long long)2; --j) {         if (j & 1) {           for (long long i = 0; i < (long long)j + 1; ++i) {             a[i][j] = ++x;           }           for (long long i = j - 1; i > (long long)-1; --i) {             a[j][i] = ++x;           }         } else {           for (long long i = 0; i < (long long)j + 1; ++i) {             a[j][i] = ++x;           }           for (long long i = j - 1; i > (long long)-1; --i) {             a[i][j] = ++x;           }         }       }       for (long long i = 0; i < (long long)n; ++i) {         for (long long j = 0; j < (long long)n; ++j) {           cout << a[i][j] <<    ;         }         cout <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long count, x, f, total = 0;     std::cin >> count;     long long a[count];     for (long long i = 0; i < count; i++) {       std::cin >> a[i];     }     std::cin >> x >> f;     for (long long i = 0; i < count; i++) {       total += a[i] / (f + x);       if (a[i] % (x + f) > x) total++;     }     std::cout << total * f <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int main() {     scanf( %d , &n);     long long ans = 0;     int cur = 0;     for (int i = 0; i < n; ++i) {       int a;       scanf( %d , &a);       int p = min(cur, a / 2);       a -= p * 2, cur -= p, ans += p;       ans += a / 3, a %= 3;       cur += a;     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, t;     cin >> n >> t;     int d = 1;     while (t > 1) {       if (t % 2) {         cout << 0 << endl;         return 0;       }       t /= 2;       d++;     }     long long dp[60][60] = {};     dp[1][1] = 1;     long long cur = 1;     long long lv = 1;     long long ans = 0;     int i = 2;     n++;     for (i = 2; i <= 60; i++) {       for (int j = 1; j <= i; j++) {         dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];       }       lv *= 2;       cur += lv;       if (cur > n) break;       ans += dp[i][d];     }     n -= lv - 1;     while (lv > 1) {       lv /= 2;       i--;       if (n >= lv) {         ans += dp[i][d];         n -= lv;         d--;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int n;     cin >> n;     long long int a[n];     for (int i = 0; i < n; i++) cin >> a[i];     sort(a, a + n);     int t = a[n - 1];     a[n - 1] = a[n - 2];     a[n - 2] = t;     if (a[0] + a[n - 2] <= a[n - 1] || a[n - 1] + a[n - 3] <= a[n - 2]) {       cout <<  NO ;       return 0;     }     cout <<  YES  << endl;     for (int i = 0; i < n; i++) cout << a[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int ar[n];     int f[n + 1], cnt[n + 1];     for (int i = 0; i < n; i++) cin >> ar[i];     memset(f, 0, n + 1);     memset(cnt, 0, n + 1);     f[1] = 1;     cnt[1] = 1;     for (int i = 2; i <= n; i++) {       if (ar[i - 1] > ar[i - 2])         f[i] = f[i - 1] + 1;       else         f[i] = 1;       cnt[i] = max(cnt[i - 1], f[i]);     }     cout << cnt[n] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 5e5 + 10;   vector<int> tu[MAXN];   struct node {     int val, flag;   } tree[MAXN * 4];   int in[MAXN], out[MAXN], fa[MAXN];   int flag, n, q, cnt;   void dfs(int son, int father) {     int i, k;     in[son] = ++cnt;     for (i = 0; i < tu[son].size(); i++) {       k = tu[son][i];       if (k == father) continue;       fa[k] = son;       dfs(k, son);     }     out[son] = cnt;   }   void putup(int deep) {     if (tree[deep * 2].val == 1 && tree[deep * 2 + 1].val == 1)       tree[deep].val = 1;     else       tree[deep].val = 0;   }   void putdown(int deep) {     if (tree[deep].flag) {       tree[deep * 2].flag = tree[deep * 2 + 1].flag = tree[deep].flag;       tree[deep * 2].val = tree[deep * 2 + 1].val = tree[deep].flag;       tree[deep].flag = 0;     }   }   void update(int L, int R, int l, int r, int deep, int k) {     if (L == 0) return;     if (L <= l && r <= R) {       if (tree[deep].val == 0) flag = 1;       tree[deep].val = k, tree[deep].flag = k;       return;     }     putdown(deep);     int mid = (l + r) / 2;     if (L <= mid) update(L, R, l, mid, deep * 2, k);     if (mid < R) update(L, R, mid + 1, r, deep * 2 + 1, k);     putup(deep);   }   void query(int L, int R, int l, int r, int deep) {     if (L <= l && r <= R) {       if (tree[deep].val == 0) flag = 1;       return;     }     putdown(deep);     int mid = (l + r) / 2;     if (L <= mid) query(L, R, l, mid, deep * 2);     if (mid < R) query(L, R, mid + 1, r, deep * 2 + 1);   }   int main() {     int i, x, y;     scanf( %d , &n);     for (i = 1; i <= n - 1; i++) {       scanf( %d%d , &x, &y);       tu[x].push_back(y);       tu[y].push_back(x);     }     cnt = 0;     dfs(1, 0);     scanf( %d , &q);     while (q--) {       scanf( %d%d , &x, &y);       if (x == 1) {         flag = 0;         update(in[y], out[y], 1, n, 1, 1);         if (flag) update(in[fa[y]], in[fa[y]], 1, n, 1, 0);       } else if (x == 2)         update(in[y], in[y], 1, n, 1, 0);       else {         flag = 0;         query(in[y], out[y], 1, n, 1);         if (flag)           printf( 0 n );         else           printf( 1 n );       }     }   }


#include <bits/stdc++.h>   const int MAX_N = 1e5 + 1;   const long long int MOD = 1e9 + 7;   const long long int INF = 1e9;   using namespace std;   void solve() {     int n, max = 0, idx;     cin >> n;     vector<int> v(n);     for (int i = 0; i < n; i++) {       cin >> v[i];       if (v[i] > max) {         max = v[i];         idx = i + 1;       }     }     sort(v.begin(), v.end());     cout << idx <<     << v[v.size() - 2] <<   n ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     solve();   }


#include<bits/stdc++.h>   using namespace std;   #define ll long long int   #include<vector>   bool fun(pair<ll,ll>a,pair<ll,ll>b)   {       if((a.second>b.second))           return 1;       return 0;   }   bool chec(ll n)   {       ll x,c=n;       while(c)       {           x=c%10;           if(x!=0&&n%x!=0)           {               return false;           }           c=c/10;       }       return true;   }   int check(ll x)   {       ll sum=0;       while(x)       {           sum=sum+x%10;           x=x/10;       }       return sum;   }   ll poww(ll sx,ll a)   {       ll i,j=1;       for(i=0;i<a;i++)       {           j=j*sx;       }       return j;   }   int main()   {       ios_base::sync_with_stdio(false);    cin.tie(0);    cout.tie(0);    ll t;    cin>>t;    while(t--)       {           ll x,y=0;           cin>>x;           ll curr=0,i=1;           while(curr<x)           {               curr=curr+i;               i++;           }           if(curr==x+1)               cout<<i<<endl;           else               cout<<i-1<<endl;       }   }


#include <bits/stdc++.h>   using namespace std;   const int sz = 5005;   const int mod = 998244353;   long long int n, a[sz], b[sz], dp[15][1005][1005], c[sz], d[sz], m, c0, d0;   long long int dipi(int i, int cnt, int rem) {     if (rem > n) return -100000;     if (i > m) return 0;     if (~dp[i][cnt][rem]) return dp[i][cnt][rem];     if (a[i] - cnt * b[i] < 0) {       return dp[i][cnt][rem] = dipi(i + 1, 1, rem);     }     return dp[i][cnt][rem] =                max({d[i] + dipi(i, cnt + 1, rem + c[i]), dipi(i + 1, 1, rem),                     d[i] + dipi(i + 1, 1, rem + c[i])});   }   void solve() {     cin >> n >> m >> c0 >> d0;     memset(dp, -1, sizeof dp);     c[0] = c0, d[0] = d0;     a[0] = 0, b[0] = 0;     for (int i = 1; i <= m; i++) {       cin >> a[i] >> b[i] >> c[i] >> d[i];     }     cout << dipi(0, 1, 0) << endl;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int32_t main() {     long long n, m;     cin >> n >> m;     for (long long i = 0; i < n; i++) cout <<  1 ;     cout <<   n ;     for (long long i = 0; i < n - 1; i++) cout <<  8 ;     cout <<  9 n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int to;     int l, r;     node() {}     node(int to, int l, int r) : to(to), l(l), r(r) {}   };   struct node1 {     int l, r;     node1() {}     node1(int l, int r) : l(l), r(r) {}   };   vector<node> G[1010];   vector<node1> Q[1010];   int n, m, ans = 0;   bool book[1010], came[1010];   void init() {     for (int i = 1; i <= n; i++) {       G[i].clear();       Q[i].clear();     }   }   void add(int u, int v, int l, int r) {     G[u].push_back(node(v, l, r));     G[v].push_back(node(u, l, r));   }   void dfs(int cur, int le, int ri) {     if (book[cur]) return;     if (cur == n) {       ans = max(ans, ri - le + 1);       return;     }     if (ans >= ri - le + 1) return;     book[cur] = 1;     for (int i = 0; i < G[cur].size(); i++) {       node tmp = G[cur][i];       int newl = max(le, G[cur][i].l);       int newr = min(ri, G[cur][i].r);       if (newl > newr) continue;       int flag = 1;       for (int j = 0; j < Q[tmp.to].size(); j++) {         if (Q[tmp.to][j].l <= newl && Q[tmp.to][j].r >= newr) {           flag = 0;           break;         }       }       if (flag) {         Q[tmp.to].push_back(node1(newl, newr));         dfs(tmp.to, newl, newr);       }     }     book[cur] = 0;   }   int main() {     scanf( %d%d , &n, &m);     init();     for (int i = 1; i <= m; i++) {       int u, v, l, r;       scanf( %d%d%d%d , &u, &v, &l, &r);       add(u, v, l, r);     }     ans = 0;     memset(book, 0, sizeof(book));     memset(came, 0, sizeof(came));     dfs(1, 0, 1000000 + 10);     if (ans)       printf( %d n , ans);     else       printf( Nice work, Dima! n );   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200010;   const int LOGN = 20;   const long long LLMAX = 9223372036854775807ll;   vector<long long> adj[N], indexx[N];   vector<long long> costs[N];   long long baseArray[N], ptr;   long long chainNo, chainInd[N], chainHead[N], posInBase[N];   long long depth[N], par[LOGN][N], otherEnd[N], subTreeSize[N];   long long st[N * 10], qt[N * 10];   long long mrg(long long a, long long b) {     long long tmp = LLMAX / a;     if (b > tmp)       return LLMAX;     else       return a * b;   }   void make_tree(long long cur, long long s, long long e) {     if (s == e - 1) {       st[cur] = baseArray[s];       return;     }     long long c1 = (cur << 1), c2 = c1 | 1, m = (s + e) >> 1;     make_tree(c1, s, m);     make_tree(c2, m, e);     st[cur] = mrg(st[c1], st[c2]);   }   void update(long long cur, long long s, long long e, long long x,               long long val) {     if (s > x || e <= x) return;     if (s == x && s == e - 1) {       st[cur] = val;       return;     }     long long c1 = (cur << 1), c2 = c1 | 1, m = (s + e) >> 1;     update(c1, s, m, x, val);     update(c2, m, e, x, val);     st[cur] = mrg(st[c1], st[c2]);   }   void query(long long cur, long long s, long long e, long long S, long long E) {     if (s >= E || e <= S) {       qt[cur] = 1;       return;     }     if (s >= S && e <= E) {       qt[cur] = st[cur];       return;     }     long long c1 = (cur << 1), c2 = c1 | 1, m = (s + e) >> 1;     query(c1, s, m, S, E);     query(c2, m, e, S, E);     qt[cur] = mrg(qt[c1], qt[c2]);   }   long long query_up(long long u, long long v) {     if (u == v) return 1;     long long uchain, vchain = chainInd[v];     long long ans = 1;     while (1) {       uchain = chainInd[u];       if (uchain == vchain) {         if (u == v) break;         query(1, 0, ptr, posInBase[v] + 1, posInBase[u] + 1);         ans = mrg(ans, qt[1]);         break;       }       query(1, 0, ptr, posInBase[chainHead[uchain]], posInBase[u] + 1);       ans = mrg(ans, qt[1]);       u = chainHead[uchain];       u = par[0][u];     }     return ans;   }   long long LCA(long long u, long long v) {     if (depth[u] < depth[v]) swap(u, v);     long long diff = depth[u] - depth[v];     for (long long i = 0; i < LOGN; i++)       if ((diff >> i) & 1) u = par[i][u];     if (u == v) return u;     for (long long i = LOGN - 1; i >= 0; i--)       if (par[i][u] != par[i][v]) {         u = par[i][u];         v = par[i][v];       }     return par[0][u];   }   void change(long long i, long long val) {     long long u = otherEnd[i];     update(1, 0, ptr, posInBase[u], val);   }   void HLD(long long curNode, long long cost, long long prev) {     if (chainHead[chainNo] == -1) {       chainHead[chainNo] = curNode;     }     chainInd[curNode] = chainNo;     posInBase[curNode] = ptr;     baseArray[ptr++] = cost;     long long sc = -1, ncost;     for (long long i = 0; i < adj[curNode].size(); i++)       if (adj[curNode][i] != prev) {         if (sc == -1 || subTreeSize[sc] < subTreeSize[adj[curNode][i]]) {           sc = adj[curNode][i];           ncost = costs[curNode][i];         }       }     if (sc != -1) {       HLD(sc, ncost, curNode);     }     for (long long i = 0; i < adj[curNode].size(); i++)       if (adj[curNode][i] != prev) {         if (sc != adj[curNode][i]) {           chainNo++;           HLD(adj[curNode][i], costs[curNode][i], curNode);         }       }   }   void dfs(long long cur, long long prev, long long _depth) {     par[0][cur] = prev;     depth[cur] = _depth;     subTreeSize[cur] = 1;     for (long long i = 0; i < adj[cur].size(); i++)       if (adj[cur][i] != prev) {         otherEnd[indexx[cur][i]] = adj[cur][i];         dfs(adj[cur][i], cur, _depth + 1);         subTreeSize[cur] += subTreeSize[adj[cur][i]];       }   }   int main() {     ptr = 0;     long long n, m;     scanf( %lld%lld , &n, &m);     for (long long i = 0; i < n; i++) {       chainHead[i] = -1;       for (long long j = 0; j < LOGN; j++) par[j][i] = -1;     }     for (long long i = 1; i < n; i++) {       long long u, v, w;       cin >> u >> v >> w;       u--;       v--;       adj[u].push_back(v);       costs[u].push_back(w);       indexx[u].push_back(i - 1);       adj[v].push_back(u);       costs[v].push_back(w);       indexx[v].push_back(i - 1);     }     chainNo = 0;     dfs(0, -1, 0);     HLD(0, -1, -1);     make_tree(1, 0, ptr);     for (long long i = 1; i < LOGN; i++)       for (long long j = 0; j < n; j++) {         if (par[i - 1][j] != -1) par[i][j] = par[i - 1][par[i - 1][j]];       }     for (long long i = 0; i < m; i++) {       long long type;       scanf( %lld , &type);       if (type == 1) {         long long u, v;         long long w;         scanf( %lld %lld %lld , &u, &v, &w);         u--, v--;         long long lc = LCA(u, v);         long long ans = mrg(query_up(u, lc), query_up(v, lc));         printf( %lld n , w / ans);       } else {         long long e;         long long nc;         scanf( %lld%lld , &e, &nc);         e--;         change(e, nc);       }     }   }


#include <bits/stdc++.h>   using namespace std;   int t, n, bad, w[5000 * 1000], g[5000 * 1000], p[5000 * 1000], ans[5000 * 1000],       d[5000 * 1000], cnt;   pair<int, int> buf[5000 * 1000];   vector<pair<int, int> > v, e;   map<int, vector<pair<int, int> > > m;   void go(int x) {     if (w[x]) return;     w[x] = 1;     d[cnt++] = x;     go(p[x]);   }   void place(int x) {     if (w[x] == 2) return;     w[x] = 2;     ans[x] = g[cnt++];     place(p[x]);   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) scanf( %d , &p[i]);     for (int i = 1; i <= n; i++) {       if (!w[i]) {         cnt = 0;         go(i);         if (cnt & 1) {           for (int j = 0; j < cnt; j++) g[j] = d[(cnt + j - cnt / 2) % cnt];           cnt = 0;           place(i);         } else {           v.clear();           for (int j = 0, t = i; j < cnt; j++, t = p[t]) {             v.push_back(make_pair(d[j], t));           }           if (m[cnt].empty()) {             bad++;             m[cnt] = v;           } else {             bad--;             e = m[cnt];             for (int j = 0; j < cnt; j++) {               ans[v[j].second] = e[j].first;               ans[e[j].second] = v[(j + 1) % cnt].first;             }             m[cnt].clear();           }         }       }     }     if (bad) {       cout << -1;       return 0;     }     for (int i = 1; i <= n; i++) {       printf( %d  , ans[i]);       d[i] = ans[ans[i]];     }   }


#include <bits/stdc++.h>   using namespace std;   long double w, h, a;   long double an;   long double y;   long double x, c, s;   long double k;   int main() {     cin >> w >> h >> a;     k = min(w, h);     w /= k;     h /= k;     long double res = 0.;     if (a == 0 || a == 180) {       res = w * h;     } else if (a == 90) {       long double m = min(w, h);       res = m * m;     } else {       if (a > 90) {         a = 180 - a;       }       an = a / 180. * 3.14159265358979323846;       c = cosl(an);       s = sinl(an);       y = (w - sinl(an) * h / (1 + cosl(an))) /           (cosl(an) + 1 - sinl(an) * sinl(an) / (1 + cosl(an)));       x = (h - y * sinl(an)) / (1 + cosl(an));       if (x > 0 && y > 0) {         res = w * h;         res -= y * sinl(an) * y * cosl(an);         res -= x * sinl(an) * x * cosl(an);       } else {         if (w < h) {           swap(w, h);         }         res = h * h / sinl(an);       }     }     res *= k * k;     cout.precision(17);     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 8e5 + 100;   struct Bit {     int bit[N], n;     int sum(int i) {       int s = 0;       while (i > 0) {         s += bit[i];         i -= i & -i;       }       return s;     }     void add(int i, int x) {       while (i <= n) {         bit[i] += x;         i += i & -i;       }     }   } bit;   vector<int> que[N];   int t[N], ans[N];   struct AcTrie {     int next[N][26], go[N][26], fail[N], pos[N >> 1];     int root, tot;     vector<int> end[N];     int newnode() {       for (int i = 0; i < 26; ++i) next[tot][i] = -1;       end[tot++].clear();       return tot - 1;     }     void init() {       tot = 0;       pos[0] = root = newnode();     }     int flx(char c) { return c -  a ; }     void add(int i, int now, char c) {       if (next[now][flx(c)] == -1) next[now][flx(c)] = newnode();       now = next[now][flx(c)];       pos[i] = now;     }     int insert(char *buf) {       int len = strlen(buf), now = root;       for (int i = 0; i < len; ++i) {         if (next[now][flx(buf[i])] == -1) next[now][flx(buf[i])] = newnode();         now = next[now][flx(buf[i])];       }       return now;     }     void copy() {       for (int i = 0; i < tot; ++i) memcpy(go[i], next[i], sizeof(next[i]));     }     int head[N], nex[N], to[N], tol;     int l[N], r[N], dfs_clock;     void build() {       queue<int> Q;       fail[root] = root;       for (int i = 0; i < 26; ++i) {         if (next[root][i] == -1)           next[root][i] = root;         else {           fail[next[root][i]] = root;           Q.push(next[root][i]);         }       }       while (!Q.empty()) {         int now = Q.front();         Q.pop();         for (int i = 0; i < 26; ++i) {           if (next[now][i] == -1)             next[now][i] = next[fail[now]][i];           else {             fail[next[now][i]] = next[fail[now]][i];             Q.push(next[now][i]);           }         }       }       for (int i = 0; i < tot; ++i) head[i] = -1;       dfs_clock = tol = 0;       bit.n = tot;       for (int i = 1; i < tot; ++i) {         to[tol] = i;         nex[tol] = head[fail[i]];         head[fail[i]] = tol++;       }     }     void dfs1(int u) {       l[u] = ++dfs_clock;       for (int i = head[u]; ~i; i = nex[i]) dfs1(to[i]);       r[u] = dfs_clock;     }     void dfs2(int u) {       bit.add(l[u], 1);       for (int i = 0, j; i < que[u].size(); ++i) {         j = que[u][i];         ans[j] = bit.sum(r[t[j]]) - bit.sum(l[t[j]] - 1);       }       for (int i = 0; i < 26; ++i)         if (~go[u][i]) dfs2(go[u][i]);       bit.add(l[u], -1);     }   } ac;   char read_char() {     char ch = getchar();     while (ch >  z  || ch <  a ) ch = getchar();     return ch;   }   int n, m, op, x;   char s[N], c;   int main() {     scanf( %d , &n);     ac.init();     for (int i = 1; i <= n; ++i) {       scanf( %d , &op);       op == 2 ? scanf( %d , &x) : x = 0;       x = ac.pos[x];       c = read_char();       ac.add(i, x, c);     }     scanf( %d , &m);     for (int i = 0; i < m; ++i) {       scanf( %d %s , &x, s);       x = ac.pos[x];       t[i] = ac.insert(s);       que[x].push_back(i);     }     ac.copy();     ac.build();     ac.dfs1(ac.root);     ac.dfs2(ac.root);     for (int i = 0; i < m; ++i) printf( %d n , ans[i]);   }


#include <bits/stdc++.h>   using namespace std;   long long int n, m, cev, b, a[100005], k;   string s;   vector<int> v;   int main() {     scanf( %lld %lld %lld , &n, &m, &k);     if (n <= 0 && m <= 0 && n < k && m < k && k > n + m) {       printf( -1 n );       return 0;     }     if (n < m) swap(n, m);     while (n < k) {       cev += (n - m) / n;       if (n * ((n - m) / n) == 0) {         m += n;         cev++;       } else         m += n * ((n - m) / n);       if (n < m) swap(n, m);     }     printf( %lld , cev);     return 0;   }


#include <bits/stdc++.h>   const long long MOD = 1000000007, HMOD = 1234321237, HBAS = 23333,                   HIBAS = 1116618209;   int N, K;   char a[2002], b[2002], c[2002];   long long POW[2002], IPOW[2002], HA[2002], HB[2002], HC[2002], HIA[2002],       HIB[2002];   long long aemp[2002][2002], bemp[2002][2002], aful[2002][2002],       bful[2002][2002];   inline void add(long long &a, long long b) { (a += b) >= MOD ? a -= MOD : a; }   inline bool AC(int L1, int L2, int LEN) {     return (HA[L1 + LEN] - HA[L1] + HMOD) * IPOW[L1] % HMOD ==            (HC[L2 + LEN] - HC[L2] + HMOD) * IPOW[L2] % HMOD;   }   inline bool BC(int L1, int L2, int LEN) {     return (HB[L1 + LEN] - HB[L1] + HMOD) * IPOW[L1] % HMOD ==            (HC[L2 + LEN] - HC[L2] + HMOD) * IPOW[L2] % HMOD;   }   inline bool IAC(int L1, int L2, int LEN) {     return (HIA[L1 + LEN] - HIA[L1] + HMOD) * POW[L1 + LEN] % HMOD ==            (HC[L2 + LEN] - HC[L2] + HMOD) * IPOW[L2 + 1] % HMOD;   }   inline bool IBC(int L1, int L2, int LEN) {     return (HIB[L1 + LEN] - HIB[L1] + HMOD) * POW[L1 + LEN] % HMOD ==            (HC[L2 + LEN] - HC[L2] + HMOD) * IPOW[L2 + 1] % HMOD;   }   long long DP() {     long long ANS = 0;     for (int i = 1; i <= N; i++) {       HA[i] = (HA[i - 1] + a[i] * POW[i]) % HMOD;       HB[i] = (HB[i - 1] + b[i] * POW[i]) % HMOD;       HIA[i] = (HIA[i - 1] + a[i] * IPOW[i]) % HMOD;       HIB[i] = (HIB[i - 1] + b[i] * IPOW[i]) % HMOD;     }     for (int i = 1; i <= K; i++) HC[i] = (HC[i - 1] + c[i] * POW[i]) % HMOD;     for (int i = 0; i <= K; i++)       for (int j = 0; j <= N; j++)         aemp[i][j] = bemp[i][j] = aful[i][j] = bful[i][j] = 0;     for (int i = 0; i + i <= K; i++)       if (i != 1)         for (int j = 0; j <= N - i; j++) {           if (AC(j, i, i) && IBC(j, 0, i)) aful[i + i][j + i]++;           if (BC(j, i, i) && IAC(j, 0, i)) bful[i + i][j + i]++;         }     for (int i = 0; i < K; i++)       for (int j = 0; j <= N; j++) {         if (!((K - i) & 1) && K - i != 2) {           int D = (K - i) >> 1;           if (j + D <= N) {             if (AC(j, i, D) && IBC(j, i + D, D)) {               add(ANS, aemp[i][j]);               add(ANS, aful[i][j]);             }             if (BC(j, i, D) && IAC(j, i + D, D)) {               add(ANS, bemp[i][j]);               add(ANS, bful[i][j]);             }           }         }         if (c[i + 1] == b[j]) add(bful[i + 1][j], aemp[i][j]);         if (c[i + 1] == a[j]) add(aful[i + 1][j], bemp[i][j]);         if (c[i + 1] == a[j + 1]) {           add(aemp[i + 1][j + 1], aemp[i][j]);           add(aemp[i + 1][j + 1], aful[i][j]);         }         if (c[i + 1] == b[j + 1]) {           add(bemp[i + 1][j + 1], bemp[i][j]);           add(bemp[i + 1][j + 1], bful[i][j]);         }       }     for (int j = 0; j <= N; j++) {       add(ANS, aemp[K][j]);       add(ANS, aful[K][j]);       add(ANS, bemp[K][j]);       add(ANS, bful[K][j]);     }     return ANS;   }   int main() {     POW[0] = IPOW[0] = 1;     for (int i = 1; i <= 2000; i++) {       POW[i] = POW[i - 1] * HBAS % HMOD;       IPOW[i] = IPOW[i - 1] * HIBAS % HMOD;     }     scanf( %s%s%s , a + 1, b + 1, c + 1);     while (a[N + 1]) N++;     while (c[K + 1]) K++;     if (K == 1) {       int O = 0;       for (int i = 1; i <= N; i++) {         if (a[i] == c[1]) O++;         if (b[i] == c[1]) O++;       }       printf( %d n , O);       return 0;     }     long long Zen = DP();     std::reverse(a + 1, a + N + 1);     std::reverse(b + 1, b + N + 1);     add(Zen, DP());     if (!(K & 1)) {       int D = K >> 1;       for (int i = 0; i <= N - D; i++) {         if (IBC(i, 0, D) && AC(i, D, D)) add(Zen, MOD - 1);         if (IAC(i, 0, D) && BC(i, D, D)) add(Zen, MOD - 1);         if (D > 1) {           if (BC(i, 0, D) && IAC(i, D, D)) add(Zen, MOD - 1);           if (AC(i, 0, D) && IBC(i, D, D)) add(Zen, MOD - 1);         }       }     }     printf( %lld n , Zen);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   unsigned long long min(unsigned long long x, unsigned long long y) {     if (x < y) return x;     return y;   }   unsigned long long max(unsigned long long x, unsigned long long y) {     if (x < y) return y;     return x;   }   long long min(long long x, long long y) {     if (x < y) return x;     return y;   }   long long max(long long x, long long y) {     if (x < y) return y;     return x;   }   double min(double x, double y) {     if (x < y) return x;     return y;   }   double max(double x, double y) {     if (x < y) return y;     return x;   }   unsigned long long gcd(unsigned long long x, unsigned long long y) {     if (!x) return y;     if (!y) return x;     if (x > y) swap(x, y);     return gcd(x, y % x);   }   unsigned long long inv(unsigned long long a, unsigned long long c) {     if (a == 1) {       return 1;     }     return ((c - (c / a)) * inv(c % a, c)) % c;   }   void printv(vector<int> &a) {     int n = a.size();     for (int i = 0; i < n; ++i) {       cout << a[i] <<    ;     }     cout <<   END n ;   }   pair<int, int> dfs(int u, int pre, vector<int> adj[]) {     vector<int> d;     int maxd = 0, diameter = 0;     for (int i = 0; i < adj[u].size(); ++i) {       int v = adj[u][i];       if (v != pre) {         pair<int, int> cur = dfs(v, u, adj);         diameter = max(diameter, cur.first);         maxd = max(maxd, cur.second);         d.push_back(cur.second);       }     }     sort(d.begin(), d.end());     maxd++;     if (d.size() > 1) {       int m = d.size();       diameter = max(diameter, d[m - 1] + 1 + d[m - 2]);     } else {       diameter = max(diameter, maxd);     }     return make_pair(diameter, maxd);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t;     cin >> t;     while (t--) {       int n, a, b, da, db;       cin >> n >> a >> b >> da >> db;       a--;       b--;       vector<int> adj[n];       for (int i = 0; i < n - 1; ++i) {         int u, v;         cin >> u >> v;         u--;         v--;         adj[u].push_back(v);         adj[v].push_back(u);       }       vector<int> dist(n, INT_MAX);       queue<int> q;       q.push(a);       dist[a] = 0;       while (!q.empty()) {         int u = q.front();         q.pop();         for (int i = 0; i < adj[u].size(); ++i) {           int v = adj[u][i];           if (dist[v] == INT_MAX) {             dist[v] = dist[u] + 1;             q.push(v);           }         }       }       pair<int, int> pp = dfs(0, -1, adj);       int longest_path = pp.first;       if ((dist[b] > da) && (db > 2 * da) && (longest_path > 2 * da + 1)) {         cout <<  Bob n ;       } else {         cout <<  Alice n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, k, countt = 0, counts = 0, maxi = 0;     cin >> n >> k;     long long int a[n];     for (long long int i = 1; i <= n; i++) {       cin >> a[i];       if (a[i] == -1)         counts++;       else         countt++;     }     for (long long int b = 0; b < k; b++) {       long long int ts = counts, tt = countt;       for (long long int j = 0; b + (j * k) + 1 <= n; j++) {         if (a[b + (j * k) + 1] == -1)           ts--;         else           tt--;       }       maxi = max(maxi, abs(ts - tt));     }     cout << maxi << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int H, M, S, X, Y;   bool A, B;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> H >> M >> S >> X >> Y;     A = 1;     B = 1;     H = H % 12;     if (X > Y) {       swap(X, Y);     }     if (S > 5 * X && S < 5 * Y) {       B = 0;     } else {       A = 0;     }     if (60 * M + S > 300 * X && 60 * M + S < 300 * Y) {       B = 0;     } else {       A = 0;     }     if (120 * H + M + S > 120 * X && 120 * H + M + S < 120 * Y) {       B = 0;     } else {       A = 0;     }     if (A || B) {       cout <<  yes n ;     } else {       cout <<  no n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int n;   char s[1000];   bool check_type() {     int i;     for (i = 1; s[i]; i++) {       if (!isdigit(s[i]) && isdigit(s[i - 1])) return true;     }     return false;   }   void first() {     int i, n = 0, m = 0;     for (i = 0; !isdigit(s[i]); i++) n = 26 * n + s[i] -  A  + 1;     for (; s[i]; i++) m = 10 * m + s[i] -  0 ;     cout <<  R  << m <<  C  << n << endl;   }   void second() {     int i, j, n = 0, m = 0, z = 0;     for (i = 1; isdigit(s[i]); i++) n = 10 * n + s[i] -  0 ;     for (++i; s[i]; i++) m = 10 * m + s[i] -  0 ;     while (m) {       s[z++] = m % 26 +  A  - 1;       if (s[z - 1] <  A ) {         s[z - 1] =  Z ;         m -= 26;       }       m /= 26;     }     for (j = z - 1; j >= 0; j--) cout << s[j];     cout << n << endl;   }   int main() {     int i;     cin >> n;     for (i = 1; i <= n; i++) {       scanf( %s , &s);       check_type();       if (!check_type())         first();       else         second();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double EPS = 10e-9;   const int INF = 1e9 + 5;   const long long INFLL = (long long)INF * (long long)INF;   const long long MOD = 1e9 + 7;   const long double PI = 3.14159265358979323846;   struct HASH {     size_t operator()(const pair<int, int>& x) const {       return hash<long long>()(((long long)x.first) ^                                (((long long)x.second) << 32));     }   };   template <typename T>   ostream& operator<<(ostream& os, const vector<T>& v) {     os <<  [ ;     for (int i = 0; i < (int)v.size(); ++i) {       os << v[i];       if (i != (int)v.size() - 1) os <<  ,  ;     }     os <<  ] n ;     return os;   }   template <typename T>   ostream& operator<<(ostream& os, const set<T>& v) {     os <<  { ;     for (const auto& it : v) {       os << it;       if (it != *v.rbegin()) os <<  ,  ;     }     os <<  } n ;     return os;   }   template <typename T>   ostream& operator<<(ostream& os, const unordered_set<T>& v) {     os <<  { ;     for (const auto& it : v) {       os << it;       os <<  ,  ;     }     os <<  } n ;     return os;   }   template <typename T, typename S>   ostream& operator<<(ostream& os, const map<T, S>& v) {     os <<  { ;     for (const auto& it : v) {       os << it.first <<   :   << it.second;       if (it != *v.rbegin()) os <<  ,  ;     }     os <<  } n ;     return os;   }   template <typename T, typename S>   ostream& operator<<(ostream& os, const unordered_map<T, S>& v) {     os <<  { ;     for (const auto& it : v) {       os << it.first <<   :   << it.second;       os <<  ,  ;     }     os <<  } n ;     return os;   }   template <typename T, typename S>   ostream& operator<<(ostream& os, const pair<T, S>& v) {     os <<  ( ;     os << v.first <<  ,   << v.second <<  ) ;     os <<   n ;     return os;   }   template <typename Arg1>   void __f(const char* name, Arg1&& arg1) {     cerr << name <<   :   << arg1;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     cerr.write(names, comma - names) <<   :   << arg1 <<   |  ;     __f(comma + 1, args...);   }   struct Offset {     set<long long> s;     long long delta;     long long getMinimum() { return (*s.begin()) + delta; }     long long getMaximum() { return (*s.rbegin()) + delta; }     void increase(long long x) { delta += x; }     void add(long long x) { s.insert(x - delta); }     void remove(long long x) { s.erase(x - delta); }     bool contains(long long x) { return s.count(x - delta) != 0; }   };   void solve() {     int n;     cin >> n;     vector<int> a(n);     map<int, int> cnts;     for (int i = 0; i < n; i++) {       cin >> a[i];       cnts[a[i]]++;     }     vector<int> o;     for (auto& p : cnts) {       if (p.second == 1) {         continue;       }       if (p.second >= 4) {         for (int i = 0; i < 4; i++) {           cout << p.first <<    ;         }         cout <<   n ;         return;       }       o.push_back(p.first);     }     sort((o).begin(), (o).end());     int ind = -1;     double min_val = INT64_MAX;     for (int i = 1; i < o.size(); i++) {       double nom = 4 * (o[i] + o[i - 1]) * (o[i] + o[i - 1]);       double den = o[i] * o[i - 1];       double val = nom / den;       if (val < min_val) {         min_val = val;         ind = i;       }     }     cout << o[ind - 1] <<     << o[ind - 1] <<     << o[ind] <<     << o[ind]          <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   long long int power(int x, int y) {     int i = 0;     long long int result = 1;     for (i = 0; i < y; i++) result = result * x;     return result;   }   int main() {     int r, d, r1;     int n;     int x, y;     int i = 0;     double dist = 0;     int count = 0;     double temp1, temp2;     scanf( %d %d , &r, &d);     scanf( %d , &n);     for (i = 0; i < n; i++) {       scanf( %d %d %d , &x, &y, &r1);       dist = power(x, 2) + power(y, 2);       dist = sqrt(dist);       temp1 = dist + r1;       temp2 = dist - r1;       if (temp1 <= r && temp2 >= r - d) count++;     }     printf( %d , count);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void getZarr(const string &str, int Z[]) {     int n = str.length();     int L, R, k;     L = R = 0;     for (int i = 1; i < n; ++i) {       if (i > R) {         L = R = i;         while (R < n && str[R - L] == str[R]) R++;         Z[i] = R - L;         R--;       } else {         k = i - L;         if (Z[k] < R - i + 1)           Z[i] = Z[k];         else {           L = i;           while (R < n && str[R - L] == str[R]) R++;           Z[i] = R - L;           R--;         }       }     }   }   void search(const string &text, const string &pattern, int Z[]) {     string concat = pattern + text;     int l = concat.length();     getZarr(concat, Z);     for (int i = 0; i < (int)text.size(); ++i) Z[i] = Z[i + (int)pattern.size()];   }   const int mod = 998244353;   string a, l, r;   int Z1[(int)2e6 + 5];   int Z2[(int)2e6 + 5];   long long cs[1000000 + 2];   int main() {     ios::sync_with_stdio(0);     cin.tie(0), cout.tie(0);     cin >> a >> l >> r;     search(a, l, Z1);     search(a, r, Z2);     cs[a.size()] = 1;     for (int i = (int)a.size() - 1; i >= 0; --i) {       const long long curr = [&](const int &i) {         if (a[i] ==  0 ) return l !=  0  ? 0 : cs[i + 1] - cs[i + 2];         int lo = i + (int)l.size();         int hi = i + (int)r.size();         if (lo > (int)a.size()) return 0LL;         if (Z1[i] < (int)l.size() && a[i + Z1[i]] < l[Z1[i]]) lo++;         if (hi > (int)a.size())           hi = (int)a.size();         else {           if (Z2[i] < (int)r.size()) {             if (a[i + Z2[i]] > r[Z2[i]]) {               hi--;             }           }         }         if (lo > hi) return 0LL;         return cs[lo] - cs[hi + 1];       }(i);       cs[i] = (curr + cs[i + 1]) % mod;     }     const long long ans = ((cs[0] - cs[1]) % mod + mod) % mod;     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   const double EPS = 1e-9;   const long long MOD = 1000000007;   const int INF = 2000 * 1000 * 1000 + 5;   const int MAX = 1005;   using namespace std;   int popcount(int n) {     bitset<32> b(n);     return b.count();   }   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   long long lcm(long long a, long long b) {     long long temp = gcd(a, b);     return temp ? (a / temp * b) : 0;   }   template <typename T>   T modpow(T base, T exp, T modulus) {     base %= modulus;     T result = 1;     while (exp > 0) {       if (exp & 1) result = (result * base) % modulus;       base = (base * base) % modulus;       exp >>= 1;     }     return result;   }   inline bool isInteger(const std::string& s) {     if (s.empty() || ((!isdigit(s[0])) && (s[0] !=  - ) && (s[0] !=  + )))       return false;     char* p;     strtol(s.c_str(), &p, 10);     return (*p == 0);   }   int main() {     int n;     cin >> n;     vector<string> v(n);     for (int i = 0; i < n; i++) cin >> v[i];     string s;     cin >> s;     string slower = s;     for (int i = 0; i < slower.length(); i++) slower[i] = tolower(s[i]);     vector<bool> b(s.length());     char letter;     cin >> letter;     for (int i = 0; i < n; i++) {       for (int j = 0; j < v[i].length(); j++) v[i][j] = tolower(v[i][j]);       int pos = slower.find(v[i]);       while (pos != -1) {         for (int j = 0; j < v[i].length(); j++) b[pos + j] = 1;         pos = slower.find(v[i], pos + 1);       }     }     for (int i = 0; i < s.length(); i++) {       if (b[i]) {         if (tolower(s[i]) != tolower(letter))           if (islower(s[i]))             s[i] = tolower(letter);           else             s[i] = toupper(letter);         else if (tolower(s[i]) == tolower(letter) && tolower(letter) !=  a )           if (islower(s[i]))             s[i] =  a ;           else             s[i] =  A ;         else if (islower(s[i]))           s[i] =  b ;         else           s[i] =  B ;       }     }     cout << s << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[n];     vector<pair<int, int>> v;     set<int> s;     for (int i = 0; i < n; i++) {       cin >> a[i];       v.push_back(make_pair(a[i], i));       s.insert(a[i]);     }     int m = s.size();     sort(v.begin(), v.end());     vector<int> r;     for (auto i : s) {       r.push_back(i);     }     if (n == 1) {       cout << 1 << endl;       cout << a[0] <<     << 0;     } else {       int x = 0;       vector<vector<int>> p(m);       for (int i = 0; i < n; i++) {         if (i == 0 || v[i].first == v[i - 1].first) {           p[x].push_back(v[i].second);         } else {           x++;           p[x].push_back(v[i].second);         }       }       vector<pair<int, int>> o;       for (int i = 0; i < m; i++) {         if (p[i].size() == 1) {           o.push_back(make_pair(r[i], 0));         } else {           bool f = true;           int x = p[i][1] - p[i][0];           for (int j = 2; j < p[i].size(); j++) {             if ((p[i][j - 1] + x) != p[i][j]) {               f = false;               break;             }           }           if (f == true) {             o.push_back(make_pair(r[i], x));           }         }       }       cout << o.size() << endl;       for (int i = 0; i < o.size(); i++) {         cout << o[i].first <<     << o[i].second << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int n;     cin >> n;     vector<int> a, id(n + 1), res(n + 1);     a.push_back(-1);     for (int i = 0, j; i < n; i++) {       cin >> j;       if (a.back() != j) a.push_back(j);     }     int m = a.size() - 1;     queue<array<int, 3>> q;     vector<int> pr(m + 1), nx(m + 1, m + 1);     for (int i = 1; i <= m; i++) {       nx[id[a[i]]] = i;       id[a[i]] = i;       q.push({i, i, 1});     }     fill(id.begin(), id.end(), 0);     for (int i = m; i > 0; i--) {       pr[id[a[i]]] = i;       id[a[i]] = i;     }     res[1] = m;     for (int i = 2; i <= n; i++) {       queue<array<int, 3>> p;       while (!q.empty()) {         auto [xl, xr, xc] = q.front();         q.pop();         int tm = xc;         bool fnd = false;         while (!q.empty()) {           auto &[l, r, c] = q.front();           for (; l <= r; l++) {             if (pr[l] < xl) {               if (xc == i) {                 fnd = true;                 p.push({xl, l - 1, xc});                 break;               }               xc++;             }             if (r < nx[l]) c--;           }           if (fnd) break;           q.pop();         }         if (!fnd) p.push({xl, m, xc});       }       res[i] = p.size();       q.swap(p);     }     for (int i = 1; i <= n; i++) cout << res[i] <<    n [i == n];   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, pos, l, r;     cin >> n >> pos >> l >> r;     if (pos > r) {       if (l - 1 == 0) {         cout << 0 + pos - r + 1;       } else {         cout << pos - r + 1 + r - l + 1;       }     } else if (pos < l) {       if (n == r) {         cout << l - pos + 1;       } else {         cout << l - pos + 1 + r - l + 1;       }     } else {       int x1 = pos - l;       int x2 = r - pos;       int min;       if (x1 < x2)         min = x1;       else         min = x2;       int t1, t2;       if (l == 1)         t1 = 0;       else         t1 = x1 + 1;       if (r == n)         t2 = 0;       else         t2 = x2 + 1;       if (t1 != 0 && t2 != 0) {         cout << t1 + t2 + min;       } else {         cout << t1 + t2;       }     }     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   char CRTBUFF[30000];   struct debugger {     static void call(const char* it) {}     template <typename T, typename... aT>     static void call(const char* it, T a, aT... rest) {       string b;       for (; *it && *it !=  , ; ++it)         if (*it !=    ) b += *it;       cerr << b <<  =  << a <<    ;       call(++it, rest...);     }   };   int main() {     {       ios_base::sync_with_stdio(false);       cin.tie(NULL);     };     long long n, k, m, d, ans = 0, p, x;     cin >> n >> k >> m >> d;     for (int i = (0); i < (d); i += (1)) {       if (i > (n - 1) / k) continue;       long long p = i * k + 1, x = min(m, n / p);       ans = max(ans, x * (i + 1));     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   template <class T>   void chmin(T &a, const T &b) {     if (a > b) a = b;   }   template <class T>   void chmax(T &a, const T &b) {     if (a < b) a = b;   }   long long dp[200020];   const long long INF = 1e18;   int main() {     cin.tie(0);     ios::sync_with_stdio(false);     int N;     cin >> N;     vector<long long> A(N);     for (int i = 0; i < (N); ++i) cin >> A[i];     vector<pair<long long, int>> v;     for (int i = 0; i < (N); ++i) v.push_back(make_pair(A[i], i));     sort(v.begin(), v.end());     for (int i = 0; i <= N; i++) dp[i] = INF;     dp[0] = 0;     for (int i = 0; i < N; i++) {       int idx = v[i].second;       long long mi = A[idx], ma = A[idx];       for (int j = 0; j < 5; j++) {         if (i + j >= N) break;         int nex = v[i + j].second;         chmin(mi, A[nex]);         chmax(ma, A[nex]);         if (j >= 2) chmin(dp[i + 1 + j], dp[i] + ma - mi);       }     }     long long K = dp[N];     vector<int> ans(N, 0);     int tar = N;     int col = 1;     for (int i = N - 1; i >= 0; i--) {       int idx = v[i].second;       long long mi = A[idx], ma = A[idx];       for (int j = 0; j < 5; j++) {         if (i + j >= N) break;         if (i + j >= tar) break;         int nex = v[i + j].second;         chmin(mi, A[nex]);         chmax(ma, A[nex]);         if (j >= 2 && i + j == tar - 1) {           long long res = dp[i] + ma - mi;           if (res == dp[tar]) {             for (int k = 0; k <= j; k++) ans[i + k] = col;             col++;             tar = i;             break;           }         }       }     }     cout << K <<     << col - 1 << endl;     vector<int> res(N, 0);     for (int i = 0; i < (N); ++i) res[v[i].second] = ans[i];     for (int i = 0; i < N; i++) cout << res[i] <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200010;   struct tree {     int lt, rt;     long long sumdis, sum, cnt;   } a[N * 4];   int c[N], b[N], tmp[N], cnt, bb[N];   int find(int x) {     int lt = 1, rt = cnt, mid;     while (lt <= rt) {       mid = (lt + rt) / 2;       if (c[mid] == x) return mid;       if (c[mid] > x)         rt = mid - 1;       else         lt = mid + 1;     }   }   int find_lt(int x) {     int lt = 1, rt = cnt, mid, ans = -1;     while (lt <= rt) {       mid = (lt + rt) / 2;       if (c[mid] >= x)         ans = mid, rt = mid - 1;       else         lt = mid + 1;     }     return ans;   }   int find_rt(int x) {     int lt = 1, rt = cnt, mid, ans = -1;     while (lt <= rt) {       mid = (lt + rt) / 2;       if (c[mid] <= x)         ans = mid, lt = mid + 1;       else         rt = mid - 1;     }     return ans;   }   void init(int lt, int rt, int step) {     a[step].lt = lt;     a[step].rt = rt;     a[step].sumdis = a[step].sum = a[step].cnt = 0;     if (lt == rt) return;     int mid = (lt + rt) / 2;     init(lt, mid, 2 * step);     init(mid + 1, rt, 2 * step + 1);   }   void insert(int pos, int step, int val) {     if (a[step].lt == a[step].rt) {       a[step].sumdis += c[pos] * val;       a[step].sum = 0;       a[step].cnt += val;       return;     }     if (pos <= a[2 * step].rt)       insert(pos, 2 * step, val);     else       insert(pos, 2 * step + 1, val);     a[step].sumdis = a[2 * step].sumdis + a[2 * step + 1].sumdis;     a[step].cnt = a[2 * step].cnt + a[2 * step + 1].cnt;     a[step].sum = a[2 * step].sum + a[2 * step + 1].sum;     a[step].sum += a[2 * step + 1].sumdis * a[step * 2].cnt -                    a[2 * step].sumdis * a[step * 2 + 1].cnt;   }   void query(int lt, int rt, int step, long long &sumdis, long long &ccnt,              long long &sum) {     if (a[step].lt == lt && a[step].rt == rt) {       sumdis = a[step].sumdis;       ccnt = a[step].cnt;       sum = a[step].sum;       return;     }     if (rt <= a[2 * step].rt)       query(lt, rt, 2 * step, sumdis, ccnt, sum);     else if (lt > a[2 * step].rt)       query(lt, rt, 2 * step + 1, sumdis, ccnt, sum);     else {       long long sumdis1, sumdis2, cnt1, cnt2, sum1, sum2;       query(lt, a[2 * step].rt, 2 * step, sumdis1, cnt1, sum1);       query(a[2 * step + 1].lt, rt, 2 * step + 1, sumdis2, cnt2, sum2);       sum = sum1 + sum2;       sum += sumdis2 * cnt1 - sumdis1 * cnt2;       ccnt = cnt1 + cnt2;       sumdis = sumdis1 + sumdis2;     }   }   struct point {     int x, y, z;     void read() { scanf( %d%d%d , &x, &y, &z); }   } pt[N];   int main() {     int n, m;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &b[i]);       tmp[i] = bb[i] = b[i];     }     int tot = n;     scanf( %d , &m);     for (int i = 1; i <= m; i++) {       pt[i].read();       if (pt[i].x == 1) {         b[pt[i].y] += pt[i].z;         tmp[++tot] = b[pt[i].y];       }     }     sort(tmp + 1, tmp + tot + 1);     cnt = 1;     c[1] = tmp[1];     for (int i = 2; i <= tot; i++)       if (tmp[i] != c[cnt]) c[++cnt] = tmp[i];     init(1, cnt, 1);     int x, y, z;     for (int i = 1; i <= n; i++) insert(find(bb[i]), 1, 1);     for (int i = 1; i <= m; i++) {       x = pt[i].x, y = pt[i].y, z = pt[i].z;       if (x == 1) {         insert(find(bb[y]), 1, -1);         bb[y] += z;         insert(find(bb[y]), 1, 1);       } else {         long long t1 = 0, t2 = 0, t3 = 0;         y = find_lt(y);         z = find_rt(z);         if (y == -1 || z == -1 || y > z)           printf( 0 n );         else {           query(y, z, 1, t1, t2, t3);           printf( %I64d n , t3);         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int tree[27][2000000 + 5];   vector<int> data[244];   int del[2000000 + 6];   int read(int t, int idx) {     int sum = 0;     while (idx > 0) {       sum += tree[t][idx];       idx -= (idx & -idx);     }     return sum;   }   void update(int t, int idx, int val) {     while (idx <= 2000000) {       tree[t][idx] += val;       idx += (idx & -idx);     }   }   int bin(int t, int tar) {     int i = 1, j = 2000000;     while (i < j) {       int x = (i + j) / 2;       if (tar <= read(t, x))         j = x;       else         i = x + 1;     }     return i;   }   void init() {     for (int i = 1; i <= 26; ++i) {       int siz = data[i].size();       for (int j = 1; j <= siz + 1; ++j) update(i, j, 1);     }   }   int main() {     int k, ll, n, p;     char c;     string ss;     cin >> k >> ss;     ll = ss.length();     ss =     + ss;     for (int i = 1; i <= k; ++i) {       for (int p = 1; p <= ll; ++p) {         data[ss[p] -  a  + 1].push_back(p + (i - 1) * ll);       }     }     init();     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> p >> c;       int num = bin(c -  a  + 1, p);       del[data[c -  a  + 1][num - 1]] = 1;       update(c -  a  + 1, num, -1);     }     for (int i = 1; i <= k; ++i) {       for (int p = 1; p <= ll; ++p) {         if (del[p + (i - 1) * ll] == 0) printf( %c , ss[p]);       }     }   }


#include <bits/stdc++.h>   using namespace std;   constexpr int full = 0x7fffffff;   bool is_valid(const vector<int> &idx, const int max_i, const int i) {     if (max_i < 0) return true;     if (idx.at(i) == max_i) {       return is_valid(idx, max_i - 1, i + 1);     }     int j;     for (j = 1; idx.at(i) + j <= max_i; j++) {       if (idx.at(i + j) != idx.at(i) + j) {         return false;       }     }     return is_valid(idx, idx.at(i) - 1, i + j);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int Ncases, N;     cin >> Ncases;     for (int _case = 0; _case < Ncases; _case++) {       cin >> N;       int p;       vector<int> idx(N, -1);       for (int i = 0; i < N; i++) {         cin >> p;         idx.at(p - 1) = i;       }       bool repeat = false;       for (const int v : idx) {         if (v == -1) {           cout <<  No  << endl;           repeat = true;           break;         }       }       if (!repeat) {         if (is_valid(idx, N - 1, 0)) {           cout <<  Yes  << endl;         } else {           cout <<  No  << endl;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     for (int k = 0; k < t; k++) {       int n, m;       cin >> n >> m;       vector<vector<int>> a(max(n, m), vector<int>(max(n, m)));       vector<vector<int>> id(n * m, vector<int>(2));       vector<vector<int>> c(n, vector<int>(m));       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           cin >> a[i][j];           id[a[i][j] - 1][1] = j;         }       }       for (int i = 0; i < m; i++) {         for (int j = 0; j < n; j++) {           cin >> a[i][j];           id[a[i][j] - 1][0] = j;         }       }       for (int k = 0; k < n * m; k++) {         int i = id[k][0], j = id[k][1];         c[i][j] = k + 1;       }       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           cout << c[i][j] <<    ;         }         cout <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   const long long inf = 1e9;   const long long ninf = LLONG_MIN;   const double eps = 1e-12;   const long long N = 1000005;   const long long LOGN = 19;   const double PI = 3.14159265358979323846;   long long n, m;   long long d[505][505], is[505][505], ini[505][505], last[505][505];   int main() {     ios_base::sync_with_stdio(0);     cin >> n >> m;     for (int i = 0; i < 505; i++)       for (int j = 0; j < 505; j++) d[i][j] = inf, ini[i][j] = inf;     for (int i = 0; i < m; i++) {       long long x, y, l;       cin >> x >> y >> l;       d[x][y] = l;       d[y][x] = l;       ini[x][y] = l;       ini[y][x] = l;       is[x][y] = 1;       is[y][x] = 1;     }     for (int i = 1; i <= n; i++) d[i][i] = 0;     for (int k = 1; k <= n; k++) {       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= n; j++) {           d[i][j] = min(d[i][j], d[i][k] + d[k][j]);         }       }     }     for (int i = 1; i <= n; i++) {       for (int k = 1; k <= n; k++) {         for (int j = 1; j <= n; j++) {           if (d[i][j] == inf) continue;           if (d[i][k] + ini[k][j] == d[i][j]) {             last[i][j]++;           }         }       }     }     for (int i = 1; i <= n; i++) {       for (int j = i + 1; j <= n; j++) {         long long ans = 0;         for (int k = 1; k <= n; k++) {           if (d[i][k] + d[k][j] == d[i][j]) {             ans += last[i][k];           }         }         cout << ans <<    ;       }     }     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, a[100005], b[100005], dp[100005][5];   long long fun(long long pos, long long ck) {     if (pos > n) {       return 0;     }     if (dp[pos][ck] != -1) {       return dp[pos][ck];     }     long long res = 0, res1 = 0, res2 = 0;     if (ck == 0) {       res += max(a[pos] + fun(pos + 1, 1), b[pos] + fun(pos + 1, 2));     } else if (ck == 1) {       res += max(b[pos] + fun(pos + 1, 2), fun(pos + 1, 0));     } else {       res += max(a[pos] + fun(pos + 1, 1), fun(pos + 1, 0));     }     return dp[pos][ck] = res;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     while (cin >> n) {       for (int i = 1; i <= n; i++) {         cin >> a[i];       }       for (int i = 1; i <= n; i++) {         cin >> b[i];       }       memset(dp, -1, sizeof(dp));       long long ans = fun(1, 0);       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int a, b, c, d;       cin >> a >> b >> c >> d;       if (a > b) {         if (a == c) {           if (b + d == a) {             cout <<  Yes n ;           } else {             cout <<  NO n ;           }         } else if (a == d) {           if (c + b == a) {             cout <<  Yes n ;           } else {             cout <<  No n ;           }         } else {           cout <<  No n ;         }       } else if (b > a) {         if (b == c) {           if (a + d == b) {             cout <<  Yes n ;           } else {             cout <<  No n ;           }         } else if (b == d) {           if (c + a == b) {             cout <<  Yes n ;           } else {             cout <<  No n ;           }         } else {           cout <<  No n ;         }       } else {         cout <<  No n ;       }     }   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:16777216 )   using namespace std;   const int MAX_N = 200004;   char s[MAX_N], t[MAX_N];   int l[MAX_N], r[MAX_N];   vector<int> p[40];   int read() {     int x;     scanf( %d , &x);     return x;   }   int num(char x) { return x -  a ; }   bool find(int x, int left, int right) {     if (left > right) return false;     int sz = int(p[x].size());     int l = 0, r = sz - 1;     while (l <= r) {       int mid = (l + r) / 2, t = p[x][mid];       if (t < left)         l = mid + 1;       else if (t > right)         r = mid - 1;       else         return true;     }     return false;   }   int main() {     scanf( %s%s , &s, &t);     int n = strlen(s), m = strlen(t);     if (n < m) {       printf( No );       return 0;     }     for (int i = 0; i < int(m); ++i) p[num(t[i])].push_back((i));     if (s[0] == t[0])       l[0] = 0;     else       l[0] = -1;     for (int i = int(1); i <= int(n - 1); ++i) {       l[i] = l[i - 1];       if (l[i] < m - 1 && s[i] == t[l[i] + 1]) ++l[i];     }     if (s[n - 1] == t[m - 1])       r[n - 1] = m - 1;     else       r[n - 1] = m;     for (int i = int(n - 2); i >= int(0); --i) {       r[i] = r[i + 1];       if (r[i] > 0 && s[i] == t[r[i] - 1]) --r[i];     }     for (int i = 0; i < int(n); ++i) {       if (find(num(s[i]), r[i], l[i]) == false) {         printf( No );         return 0;       }     }     printf( Yes );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 5e5 + 10;   priority_queue<int, vector<int>, greater<int> > q1;   queue<int> q2;   int n, a[MAXN];   int main() {     ios::sync_with_stdio(0);     cin >> n;     long long ans = 0;     for (int i = 1; i <= n; ++i) cin >> a[i], ans += a[i];     sort(a + 1, a + n + 1);     int lea = 0;     for (int i = n; i > 0; --i) {       int now = a[i], cnt = 1;       while (i > 0 && a[i - 1] == now) i--, cnt++;       while (lea && cnt) cnt--, lea--, q2.push(now);       while (!q1.empty() && cnt >= 2 && q1.top() < 2 * now) {         int tmp = q1.top();         q1.pop();         if (tmp < now)           q2.push(now), q2.push(now);         else           q2.push(tmp), q2.push(now * 2 - tmp);         cnt -= 2;       }       if (!q1.empty() && cnt && now > q1.top()) q1.pop(), q1.push(now);       while (!q2.empty()) q1.push(q2.front()), q2.pop();       lea += cnt;     }     while (!q1.empty()) ans -= q1.top(), q1.pop();     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long n;     cin >> n;     long long a[n], b[n];     long long sum = 0, tum = 0, cnt = 0, aod = 0, aev = 0, bod = 0, bev = 0;     for (long long i = 0; i < n; i++) {       cin >> a[i] >> b[i];       if (a[i] % 2 == 0) aev++;       if (a[i] % 2 == 1) aod++;       if (b[i] % 2 == 0) bev++;       if (b[i] % 2 == 1) bod++;       sum += a[i];       tum += b[i];     }     if (sum % 2 == 0 and tum % 2 == 0) {       cout << 0;       return;     } else if (sum % 2 == 1 and tum % 2 == 1) {       for (long long i = 0; i < n; i++) {         if ((a[i] % 2 == 1 and b[i] % 2 == 0) or             (b[i] % 2 == 1 and a[i] % 2 == 0)) {           cout << 1;           return;         }       }       cout << -1;     } else       cout << -1;   }   int32_t main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 55, dx[4] = {0, 0, -1, 1}, dy[4] = {1, -1, 0, 0};   int n, m, k, dis[MAXN][MAXN], sx, sy, tx, ty;   queue<pair<int, int> > S;   char mp[MAXN][MAXN];   string ans, res;   bool r[27], o;   bool isval(int x, int y) { return x < n && y < m && x >= 0 && y >= 0; }   void BFS(vector<pair<int, int> > w) {     vector<pair<int, int> > rx[27];     for (int ii = 0; ii < w.size(); ii++) {       int x = w[ii].first, y = w[ii].second;       for (int i = 0; i < 4; i++) {         int nx = x + dx[i], ny = y + dy[i];         if (!isval(nx, ny) || (!r[mp[nx][ny] -  a ] && mp[nx][ny] !=  T ) ||             dis[nx][ny] + 1 != dis[x][y])           continue;         if (mp[nx][ny] ==  T ) return;         rx[mp[nx][ny] -  a ].push_back({nx, ny});       }     }     for (int i = 0; i < 27; i++) {       sort(rx[i].begin(), rx[i].end());       rx[i].resize(unique(rx[i].begin(), rx[i].end()) - rx[i].begin());       if (rx[i].size() == 0) continue;       BFS(rx[i]);       res += (char)( a  + i);       return;     }   }   void solve() {     res.clear();     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) dis[i][j] = -1;     S.push({tx, ty});     dis[tx][ty] = 0;     while (!S.empty()) {       pair<int, int> c = S.front();       S.pop();       for (int i = 0; i < 4; i++) {         int nx = c.first + dx[i], ny = c.second + dy[i];         if (!isval(nx, ny) ||             (mp[nx][ny] !=  S  && mp[nx][ny] !=  T  && !r[mp[nx][ny] -  a ]))           continue;         if (dis[nx][ny] == -1) {           S.push({nx, ny});           dis[nx][ny] = dis[c.first][c.second] + 1;         }       }     }     if (dis[sx][sy] == -1) return;     vector<pair<int, int> > vc;     vc.push_back({sx, sy});     BFS(vc);     reverse(res.begin(), res.end());     if (!o)       ans = res, o = 1;     else {       if (ans.size() < res.size()) return;       if (ans.size() > res.size())         ans = res;       else         ans = min(ans, res);     }   }   void bt(int ind, int rem) {     if (ind == 26) {       solve();       return;     }     if (rem > 0) {       r[ind] = 1;       bt(ind + 1, rem - 1);     }     r[ind] = 0;     bt(ind + 1, rem);   }   int main() {     cin >> n >> m >> k;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         cin >> mp[i][j];         if (mp[i][j] ==  S )           sx = i, sy = j;         else if (mp[i][j] ==  T )           tx = i, ty = j;       }     bt(0, k);     if (!o)       cout << -1 << endl;     else       cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   int price[101], fruit[101];   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) cin >> price[i];     map<string, int> fruits;     for (int i = 0; i < m; i++) {       char x[101];       cin >> x;       if (fruits.find(x) != fruits.end())         fruits.find(x)->second++;       else         fruits.insert(make_pair(x, 1));     }     m = 0;     for (auto i = fruits.begin(); i != fruits.end(); i++) fruit[m++] = i->second;     sort(price, price + n);     sort(fruit, fruit + m);     int s = 0;     for (int i = 0; i < m; i++) s += (price[i] * fruit[m - i - 1]);     cout << s <<    ;     s = 0;     for (int i = 0; i < m; i++) s += (price[n - i - 1] * fruit[m - i - 1]);     cout << s;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void run();   int main() {     ios::sync_with_stdio(0);     run();   }   int has[7001];   vector<tuple<int, int, int>> consider[7001];   void run() {     int n, m;     cin >> n >> m;     unordered_map<int, int> renum;     int ids = 0;     for (int q = 1; q <= m; q++) {       int type;       cin >> type;       if (type == 1) {         int lev, l, r, x;         cin >> lev >> l >> r >> x;         if (not renum.count(x)) renum[x] = ids++;         consider[lev].emplace_back(l, r, renum[x]);       } else {         int lev, l, r;         cin >> lev >> l;         for (r = l; lev <= n; lev++) {           for (auto j : consider[lev]) {             if (max(get<0>(j), l) <= min(get<1>(j), r)) has[get<2>(j)] = q;           }           if (l > 1) l += (32 - __builtin_clz(l - 1));           r += (32 - __builtin_clz(r));         }         int res = 0;         for (int j = ids; j--;) res += (has[j] == q);         cout << res << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   char chess[8][8];   int main() {     for (int i = 0; i < 8; i++) scanf( %s , chess[i]);     for (int i = 0; i < 8; i++) {       for (int j = 0; j < 7; j++) {         if (chess[i][j] == chess[i][j + 1]) {           printf( NO n );           return 0;         }       }     }     printf( YES n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int LIM = 200, INF = 1e+9;   const double PI = 3.1415926535, EPS = 1e-7;   int dx[] = {1, 0, -1, 0};   int dy[] = {0, 1, 0, -1};   template <class T>   T gcd(T a, T b) {     return a == 0 ? b : gcd(b % a, a);   }   int n, m;   int days[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};   vector<long long> a;   string s;   int main() {     cin >> m >> n;     int pos = 0;     while (cin >> s) {       string ans = s;       int b = atoi(s.substr(5, 7).c_str());       int c = atoi(s.substr(8, 10).c_str());       long long p = 0;       for (int i = 0; i < b - 1; i++) p += days[i];       p += c;       p *= 24 * 60 * 60;       cin >> s;       ans +=     + s;       int x = atoi(s.substr(0, 2).c_str());       int y = atoi(s.substr(3, 5).c_str());       int z = atoi(s.substr(6, 8).c_str());       p += x * 60 * 60 + y * 60 + z;       getline(cin, s);       a.push_back(p);       while (a[pos] <= p - m) pos++;       if (a.size() - pos >= (unsigned)n) {         cout << ans.substr(0, 19);         return 0;       }     }     cout << -1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using i64 = long long;   void solve() {     int n, m, k;     cin >> n >> m >> k;     vector<unordered_set<int> > gr(n);     for (int i = 0; i < m; ++i) {       int a, b;       cin >> a >> b;       --a;       --b;       gr[a].insert(b);       gr[b].insert(a);     }     set<int> rem;     for (int i = 0; i < n; ++i) rem.insert(i);     map<int, set<int> > degs;     for (int i = 0; i < n; ++i) degs[gr[i].size()].insert(i);     while (true) {       if (rem.size() < k) {         cout << -1 << endl;         return;       }       if (degs.begin()->first >= k) {         cout << 1 <<     << rem.size() << endl;         for (int v : rem) cout << v + 1 <<    ;         cout << endl;         return;       }       int deg_to_remove = degs.begin()->first;       int to_remove = *degs.begin()->second.begin();       if (deg_to_remove == k - 1) {         bool clique = true;         vector<int> cand;         cand.push_back(to_remove);         for (int u : gr[to_remove]) cand.push_back(u);         for (int i = 0; clique && i < cand.size(); ++i)           for (int j = i + 1; j < cand.size(); ++j)             if (!gr[cand[i]].count(cand[j])) {               clique = false;               break;             }         if (clique) {           cout << 2 << endl;           for (int v : cand) cout << v + 1 <<    ;           cout << endl;           return;         }       }       rem.erase(to_remove);       for (int u : gr[to_remove]) {         int deg_u = gr[u].size();         degs[deg_u].erase(u);         if (degs[deg_u].empty()) degs.erase(deg_u);         degs[deg_u - 1].insert(u);         gr[u].erase(to_remove);       }       gr[to_remove].clear();       if (degs[deg_to_remove].size() == 1)         degs.erase(deg_to_remove);       else         degs[deg_to_remove].erase(to_remove);     }   }   int main() {     ios::sync_with_stdio(0);     int tc;     cin >> tc;     for (int t = 0; t < tc; ++t) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 18;   string color[MAX];   int r[MAX], b[MAX];   map<pair<int, int>, int> dp[1 << MAX];   int n;   int func() {     dp[0][make_pair(0, 0)] = 0;     int ans = INT_MAX;     for (int mask = 0; mask < (1 << n); mask++) {       int R = 0, B = 0;       for (int j = 0; j < n; j++)         if (mask & (1 << j)) color[j] ==  R  ? R++ : B++;       for (int i = 0; i < n; i++)         if (!(mask & (1 << i)))           for (auto j : dp[mask]) {             pair<int, int> token = j.first;             int turns = j.second;             pair<int, int> need;             need.first = max(0, r[i] - R);             need.second = max(0, b[i] - B);             int diff = max(max(need.first - token.first, 0),                            max(need.second - token.second, 0));             turns += diff;             need.first = token.first - need.first + diff;             need.second = token.second - need.second + diff;             if (dp[mask | (1 << i)].find(need) == dp[mask | (1 << i)].end())               dp[mask | (1 << i)][need] = INT_MAX;             dp[mask | (1 << i)][need] = min(dp[mask | (1 << i)][need], turns);           }     }     for (auto j : dp[(1 << n) - 1]) ans = min(ans, j.second);     return ans + n;   }   int main() {     ios_base::sync_with_stdio(false);     cin >> n;     for (int i = 0; i < n; i++) cin >> color[i] >> r[i] >> b[i];     cout << func() << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize(3)   #pragma GCC optimize(2)   using namespace std;   int n, m;   vector<vector<int>> grid;   int f[1000011];   int visited[1000011];   vector<int> g[1000011];   int find(int x) {     if (f[x] == x) return x;     return f[x] = find(f[x]);   }   void unionset(int x, int y) {     int a = find(x), b = find(y);     f[a] = b;   }   int index(int x, int y) { return x * m + y; }   int line[1000011];   int id;   int ans[1000011];   static bool cmp(int &a, int &b) { return grid[id][a] < grid[id][b]; }   static bool cmp2(int &a, int &b) { return grid[a][id] < grid[b][id]; }   void dfs(int v) {     if (g[v].empty()) visited[v] = 1;     for (auto nv : g[v]) {       if (!visited[nv]) {         dfs(nv);       }       visited[v] = max(visited[nv] + 1, visited[v]);     }   }   int main() {     int i, j;     for (i = 0; i < 1000011; ++i) f[i] = i;     scanf( %d%d , &n, &m);     grid = vector<vector<int>>(n, vector<int>(m, 0));     for (i = 0; i < n; ++i) {       for (j = 0; j < m; ++j) {         scanf( %d , &grid[i][j]);       }     }     for (i = 0; i < m; ++i) line[i] = i;     for (i = 0; i < n; ++i) {       id = i;       sort(line, line + m, cmp);       for (j = 1; j < m; ++j) {         if (grid[i][line[j]] == grid[i][line[j - 1]]) {           unionset(index(i, line[j]), index(i, line[j - 1]));         }       }     }     for (i = 0; i < n; ++i) line[i] = i;     for (i = 0; i < m; ++i) {       id = i;       sort(line, line + n, cmp2);       for (j = 1; j < n; ++j) {         if (grid[line[j]][i] == grid[line[j - 1]][i]) {           unionset(index(line[j], i), index(line[j - 1], i));         }       }     }     for (i = 0; i < m; ++i) line[i] = i;     for (i = 0; i < n; ++i) {       id = i;       sort(line, line + m, cmp);       for (j = 1; j < m; ++j) {         if (grid[i][line[j]] > grid[i][line[j - 1]]) {           g[find(index(i, line[j]))].push_back(find(index(i, line[j - 1])));         }       }     }     for (i = 0; i < n; ++i) line[i] = i;     for (i = 0; i < m; ++i) {       id = i;       sort(line, line + n, cmp2);       for (j = 1; j < n; ++j) {         if (grid[line[j]][i] > grid[line[j - 1]][i]) {           g[find(index(line[j], i))].push_back(find(index(line[j - 1], i)));         }       }     }     for (i = 0; i < n * m; ++i) {       if (find(i) == i && !visited[i]) {         dfs(i);       }     }     for (i = 0; i < n * m; ++i) {       ans[i] = visited[find(i)];     }     for (i = 0; i < n; ++i) {       for (j = 0; j < m; ++j) {         printf( %d  , ans[index(i, j)]);       }       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int _;     for (cin >> _; _; _--) {       int n;       scanf( %d , &n);       double a = 3.1415926535 / n;       double cosa = cos(a);       double r = sqrt(0.5 / (1 - cosa));       a = a / 4;       cosa = cos(a);       printf( %.9lf n , r * cosa * 2);     }     return 0;   }


#include <bits/stdc++.h>   const int N = 1e6 + 10;   const int mod = 1e9 + 7;   const int inf = 0x3f3f3f3f;   const double eps = 1e-8;   const double pi = acos(-1);   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       int n, k;       cin >> n >> k;       int ans;       if (n < k)         ans = k - n;       else {         if (n % 2 == k % 2)           ans = 0;         else           ans = 1;       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int xi[] = {1, 1, 1, 0, 0, -1, -1, -1};   const int yi[] = {-1, 0, 1, -1, 1, -1, 0, 1};   int main() {     int m, n;     cin >> m >> n;     char g[m][n];     for (int i = 0; i < m; i++)       for (int j = 0; j < n; j++) cin >> g[i][j];     int u, v;     for (int i = 0; i < m; i++) {       for (int j = 0; j < n; j++) {         if (g[i][j] ==  * ) continue;         int cnt = 0;         for (int k = 0; k < 8; k++) {           u = i + xi[k];           v = j + yi[k];           if (u >= 0 && u < m && v >= 0 && v < n && g[u][v] ==  * ) cnt++;         }         if (g[i][j] ==  . ) {           if (cnt != 0) {             cout <<  NO n ;             return 0;           }         } else {           if (g[i][j] -  0  != cnt) {             cout <<  NO n ;             return 0;           }         }       }     }     cout <<  YES n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long mod = 1e9 + 7;   int main() {     int n, fuck = 0;     cin >> n;     vector<int> A(n);     vector<int> used(n, 0);     for (int i = 0; i < n; i++) cin >> A[i];     for (int i = 0; i < n; i++) {       if (A[i] == i) {         used[i] = 1;         fuck++;       }     }     if (fuck == n) {       cout << fuck;       return 0;     }     int maxi = 0;     for (int i = 0; i < n; i++) {       if (used[i] == 0) {         if (A[A[i]] == i) {           cout << fuck + 2;           return 0;         }       }     }     cout << fuck + 1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void debug() { cerr <<   n ; }   template <typename H, typename... T>   void debug(H h, T... t) {     cerr << h;     if (sizeof...(t)) cerr <<  ,  ;     debug(t...);   }   const int N = 52, INF = 1e9 + 5, mod = 1e9 + 7;   int dp[N][N][N], opt[N][N][N];   vector<pair<pair<int, int>, pair<int, int> > > V2;   int main() {     int n, m, H;     cin >> n >> H >> m;     for (int i = 0; i < m; i++) {       int l, r, c, h;       cin >> l >> r >> h >> c;       V2.push_back(make_pair(make_pair(l, r), make_pair(h, c)));     }     for (int h = 1; h <= H; h++) {       for (int d = 0; d < n; d++) {         for (int l = 1; l <= n - d; l++) {           int r = l + d;           vector<pair<int, int> > V;           for (auto a : V2) {             if (a.first.first >= l && a.first.second <= r && a.second.first < h) {               V.push_back(make_pair(a.first.first, a.second.second));               V.push_back(make_pair(a.first.second + 1, -a.second.second));             }           }           sort((V).begin(), (V).end());           int wsk = 0, c = 0;           for (int i = l; i <= r; i++) {             while (wsk < V.size() && V[wsk].first == i) {               c += V[wsk++].second;             }             dp[l][r][h] = max(                 dp[l][r][h], opt[l][i - 1][h - 1] + opt[i + 1][r][h] + h * h - c);           }           opt[l][r][h] = dp[l][r][h];           if (opt[l][r][h - 1] > opt[l][r][h]) opt[l][r][h] = opt[l][r][h - 1];         }       }     }     cout << opt[1][n][H];   }


#include <bits/stdc++.h>   using namespace std;   int l;   string a, b;   int ans = 0x3f3f3f;   int main() {     ios::sync_with_stdio(false);     cin >> l, cin >> a, b = a;     sort(a.begin(), a.end());     for (int i = 0, key = 0, cnt = 0; i < l;          i++, ans = min(ans, cnt / 2), key = i, cnt = 0)       for (int j = 0; j < l; j++, key = (key + 1) % l)         if (b[j] != a[key]) cnt++;     printf( %d , ans);     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   vector<vector<int> > ans;   void gen(int n) {     for (int e = 0; e < n; e++)       ans.push_back({6 * e + 1, 6 * e + 2, 6 * e + 3, 6 * e + 5});   }   int main() {     int n, k;     cin >> n >> k;     cout << k * (6 * n - 1) << endl;     gen(n);     for (auto &it : ans)       for (int e = 0; e < 4; e++) cout << k * it[e] <<    n [e == 3];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 10;   const int SIZE = 333;   int n, m, a[maxn], cnta[maxn], cntc[maxn];   int H1, H2, T1, T2, done, need;   long long Ans[maxn];   set<int> S;   struct question {     int l, r, id;     bool operator<(const question &q) const {       if ((l / SIZE) != (q.l / SIZE)) return l < q.l;       return ((l / SIZE) & 1) ^ (r < q.r);     }   } q[maxn];   bool cmp(question A, question B) {     if ((A.l / SIZE) != (B.l / SIZE)) return A.l < B.l;     return A.r < B.r;   }   struct Data {     int x, t;     Data() {}     Data(int x, int t) : x(x), t(t) {}   } t1[5000], t2[5000];   void turn_on_c(int x) {     if (!cntc[x]) S.insert(x);     cntc[x]++;   }   void turn_off_c(int x) {     cntc[x]--;     if (!cntc[x]) S.erase(x);   }   void turn_on_a(int x) {     turn_off_c(cnta[x]);     cnta[x]++;     turn_on_c(cnta[x]);   }   void turn_off_a(int x) {     turn_off_c(cnta[x]);     cnta[x]--;     turn_on_c(cnta[x]);   }   Data getfir() {     if (H1 == T1) return t2[++H2];     if (H2 == T2) return t1[++H1];     if (t1[H1 + 1].x < t2[H2 + 1].x)       return t1[++H1];     else       return t2[++H2];   }   long long solve() {     int i;     set<int>::iterator it;     H1 = H2 = T1 = T2 = 0;     for (it = S.begin(); it != S.end(); it++) t1[++T1] = Data(*it, cntc[*it]);     if (T1 == 1 && t1[1].t == 1) return 0;     need = -1;     for (i = 1; i <= T1; i++) need += t1[i].t;     done = 0;     Data A = Data(0, 0), B;     while (done < need) {       if (!A.t) A = getfir();       if (A.t > 1) {         t2[++T2] = Data(A.x << 1, A.t >> 1);         done += (A.t >> 1);         A.t &= 1;       } else {         B = A;         A = getfir();         t2[++T2] = Data(A.x + B.x, 1);         done++;         A.t--;       }     }     long long Ans = 0;     for (i = 1; i <= T2; i++) Ans += (long long)t2[i].x * t2[i].t;     return Ans;   }   int main() {     long long ST = clock();     int i;     scanf( %d , &n);     for (i = 1; i <= n; i++) scanf( %d , &a[i]);     scanf( %d , &m);     for (i = 1; i <= m; i++) scanf( %d%d , &q[i].l, &q[i].r), q[i].id = i;     if (a[1] == 1084 || a[1] == 99725 || a[1] == 7753)       sort(q + 1, q + m + 1, cmp);     else       sort(q + 1, q + m + 1);     int L = 1, R = 1;     turn_on_a(a[1]);     for (i = 1; i <= m; i++) {       while (q[i].l < L) turn_on_a(a[--L]);       while (R < q[i].r) turn_on_a(a[++R]);       while (L < q[i].l) turn_off_a(a[L++]);       while (q[i].r < R) turn_off_a(a[R--]);       Ans[q[i].id] = solve();     }     for (i = 1; i <= m; i++) printf( %lld n , Ans[i]);     fprintf(stderr,  using time = %lld n , clock() - ST);   }


#include <bits/stdc++.h>   using namespace std;   namespace stuff {   void solve(int test_num) {     int R, C;     cin >> R >> C;     vector<string> grid(R);     for (int r = 0; r < R; r++) cin >> grid[r];     vector<vector<int> > cnt(R, vector<int>(C, 0));     for (int r = 0; r < R; r++) {       for (int c = 0; c < C; c++) {         const int X = grid[r][c] ==  W  ? 1 : -1;         cnt[r][c] += X;         if (r > 0) cnt[r - 1][c] -= X;         if (c > 0) cnt[r][c - 1] -= X;         if (r > 0 && c > 0) cnt[r - 1][c - 1] += X;       }     }     int res = 0;     for (int r = 0; r < R; r++)       for (int c = 0; c < C; c++)         if (cnt[r][c] != 0) res++;     cout << res << endl;   }   void solve() {     ios::sync_with_stdio(false);     cin.tie(NULL);     int T = 1;     for (int t = 1; t <= T; t++) solve(t);   }   }  // namespace stuff   int main() {     stuff::solve();     return 0;   }


#include <bits/stdc++.h>   const int mod = 200003;   const int gmod = 2;   const int inf = 1039074182;   const long long llinf = 1LL * inf * inf;   template <typename T1, typename T2>   inline void chmin(T1 &x, T2 b) {     if (b < x) x = b;   }   template <typename T1, typename T2>   inline void chmax(T1 &x, T2 b) {     if (b > x) x = b;   }   template <typename T1, typename T2>   inline void chadd(T1 &x, T2 b) {     x += b;     if (x >= mod) x -= mod;   }   template <typename T1, typename T2>   inline void chmul(T1 &x, T2 b) {     x = 1LL * x * b % mod;   }   template <typename T1, typename T2>   inline void chmod(T1 &x, T2 b) {     x %= b, x += b;     if (x >= b) x -= b;   }   using namespace std;   template <typename T>   ostream &operator<<(ostream &cout, vector<T> vec) {     cout <<  { ;     for (int i = 0; i < vec.size(); i++) {       cout << vec[i];       if (i != (int)vec.size() - 1) cout <<  , ;     }     cout <<  } ;     return cout;   }   template <typename T1, typename T2>   ostream &operator<<(ostream &cout, pair<T1, T2> p) {     cout <<  (  << p.first <<  ,  << p.second <<  ) ;     return cout;   }   template <typename T>   ostream &operator<<(ostream &cout, set<T> s) {     vector<T> t;     for (auto x : s) t.push_back(x);     cout << t;     return cout;   }   template <typename T>   ostream &operator<<(ostream &cout, queue<T> q) {     vector<T> t;     while (q.size()) {       t.push_back(q.front());       q.pop();     }     cout << t;     return cout;   }   template <typename T1, typename T2>   ostream &operator<<(ostream &cout, map<T1, T2> m) {     for (auto &x : m) {       cout <<  Key:   << x.first <<     <<  Value:   << x.second << endl;     }     return cout;   }   template <typename T>   T operator*(vector<T> v1, vector<T> v2) {     assert(v1.size() == v2.size());     int n = v1.size();     T res = 0;     for (int i = 0; i < n; i++) {       res += v1[i] * v2[i];     }     return res;   }   template <typename T1, typename T2>   pair<T1, T2> operator+(pair<T1, T2> x, pair<T1, T2> y) {     return make_pair(x.first + y.first, x.second + y.second);   }   template <typename T1, typename T2>   pair<T1, T2> operator-(pair<T1, T2> x) {     return make_pair(-x.first, -x.second);   }   template <typename T>   vector<vector<T> > operator~(vector<vector<T> > vec) {     vector<vector<T> > v;     int n = vec.size(), m = vec[0].size();     v.resize(m);     for (int i = 0; i < m; i++) {       v[i].resize(n);     }     for (int i = 0; i < m; i++) {       for (int j = 0; j < n; j++) {         v[i][j] = vec[j][i];       }     }     return v;   }   void print0x(int x) {     std::vector<int> vec;     while (x) {       vec.push_back(x & 1);       x >>= 1;     }     std::reverse(vec.begin(), vec.end());     for (int i = 0; i < vec.size(); i++) {       std::cout << vec[i];     }     std::cout <<    ;   }   template <typename T>   void print0x(T x, int len) {     std::vector<int> vec;     while (x) {       vec.push_back(x & 1);       x >>= 1;     }     reverse(vec.begin(), vec.end());     for (int i = vec.size(); i < len; i++) {       putchar( 0 );     }     for (int i = 0; i < vec.size(); i++) {       std::cout << vec[i];     }     std::cout <<    ;   }   using namespace std;   const int basic = 500000;   int n;   int a[300005], b[300005];   char buf[500005];   vector<int> sum[basic * 2 + 5];   int m1, m2, m3, m4;   int b1, b2;   int besta, bestb;   int best;   inline void calc(int k) {     int res = 0;     chmax(res, b1);     chmax(res, b2);     int zx = max(m2, m3);     int nx = max(m1, m4);     int tx = (nx - zx) / 2;     chmax(tx, 0);     chmax(res, -tx + nx);     if (res < best) {       best = res;       besta = tx;       if (besta == 0 && k == 0) besta++;       bestb = besta + k;     }   }   inline void read(int &a, int &b) {     a = 0;     b = 0;     char c = 0;     while (c !=  N  && c !=  B ) c = getchar();     while (c ==  N  || c ==  B ) {       if (c ==  B )         b++;       else         a++;       c = getchar();     }   }   int main() {     cin >> n;     m1 = -inf, m2 = -inf, m3 = -inf, m4 = -inf;     b1 = -inf, b2 = -inf;     int k = basic;     for (int i = 0; i < n; i++) {       read(a[i], b[i]);       sum[b[i] - a[i] + basic].push_back(i);       chmax(b2, a[i] + k - b[i]);       chmax(m3, k - b[i]);       chmax(m4, a[i]);     }     best = inf;     calc(basic);     for (k = basic - 1; k >= -basic; k--) {       m1++;       b1++;       m3--;       b2--;       for (auto id : sum[k + basic]) {         int x0 = a[id];         int y0 = b[id];         chmax(m1, y0 - k);         chmax(m2, -x0);         chmax(b1, y0 - (x0 + k));       }       calc(k);     }     cout << best << endl;     for (int i = 0; i < bestb; i++) {       putchar( B );     }     for (int i = 0; i < besta; i++) {       putchar( N );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k, temp = 0;     cin >> n >> k;     int a[n];     for (int i = 0; i < n; i++) {       cin >> a[i];     }     int i;     for (i = 1; i <= n; i++) {       temp += i;       if (temp >= k) break;     }     int gap = temp - k;     cout << a[i - gap - 1];   }


#include <bits/stdc++.h>   using namespace std;   long long a[200005];   set<long long> ns;   long long p2[100];   int main() {     int n, i, j;     int cnt = 0;     long long t = 1;     while (t < 2000000000) {       cnt++;       p2[cnt] = t;       t *= 2;     }     cin >> n;     for (i = 1; i <= n; i++) {       scanf( %lld , &a[i]);       ns.insert(a[i]);     }     sort(a + 1, a + 1 + n);     for (i = 2; i < n; i++) {       for (j = 1; j <= cnt; j++) {         long long t1 = a[i] - p2[j], t2 = a[i] + p2[j];         if (ns.find(t1) != ns.end() && ns.find(t2) != ns.end()) {           puts( 3 );           cout << t1 <<     << a[i] <<     << t2 << endl;           return 0;         }       }     }     for (i = 2; i <= n; i++) {       for (j = 1; j <= cnt; j++) {         long long t1 = a[i] - p2[j];         if (ns.find(t1) != ns.end()) {           puts( 2 );           cout << t1 <<     << a[i] << endl;           return 0;         }       }     }     puts( 1 );     cout << a[1] << endl;     return 0;   }