#include <bits/stdc++.h>   using namespace std;   int n;   int tree[1000005 << 2];   int mn[1000005 << 2];   int mx[1000005 << 2];   int pos;   char s[1000005];   void pushup(int rt) {     tree[rt] = tree[(2 * rt)] + tree[(2 * rt + 1)];     mn[rt] = min(mn[(2 * rt)], tree[(2 * rt)] + mn[(2 * rt + 1)]);     mx[rt] = max(mx[(2 * rt)], tree[(2 * rt)] + mx[(2 * rt + 1)]);   }   void update(int rt, int l, int r, int x, int v) {     if (l == r) {       tree[rt] = mn[rt] = mx[rt] = v;       return;     }     if (x <= ((l + r) / 2))       update((2 * rt), l, ((l + r) / 2), x, v);     else       update((2 * rt + 1), ((l + r) / 2) + 1, r, x, v);     pushup(rt);   }   int main() {     scanf( %d , &n);     pos = 1;     scanf( %s , s + 1);     char opt[5];     for (int i = 1; i <= n; ++i) {       opt[1] = s[i];       if (opt[1] ==  R )         ++pos;       else if (opt[1] ==  L ) {         if (pos > 1) --pos;       } else {         if (opt[1] ==  ( )           update(1, 1, n, pos, 1);         else if (opt[1] ==  ) )           update(1, 1, n, pos, -1);         else           update(1, 1, n, pos, 0);       }       if (tree[1] != 0 || mn[1] < 0)         printf( -1  );       else         printf( %d  , mx[1]);     }     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int f(long long x) {     long long i, j, cnt = 0;     for (i = 1; i <= x; i *= 2) {       for (j = i; j <= x; j *= 3) cnt++;     }     return cnt;   }   int main() {     int l, r;     cin >> l >> r;     cout << f(r) - f(l - 1);   }


#include <bits/stdc++.h>   using namespace std;   int prime[1000100];   void get_prime() {     for (int i = 2; i < 1000100; i++) {       if (!prime[i]) {         for (int j = i * 2; j < 1000100; j += i) {           prime[j] = i;         }       }     }     for (int i = 2; i < 1000100; i++) {       if (!prime[i]) {         prime[i] = 1;       }     }   }   int main() {     get_prime();     int x2;     cin >> x2;     int ans = 0x3f3f3f3f3f;     for (int i = x2 - prime[x2] + 1; i <= x2; i++) {       ans = min(ans, i - prime[i] + 1);     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t, a, b, x;     cin >> t;     while (t--) {       cin >> x;       if (x % 2 == 0) {         cout << x / 2 <<     << x / 2 << endl;       } else {         cout << 1 <<     << x - 1 << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   string FILE_NAME =  testcase.A ;   string NAME;   string itos(int n) {     stringstream ss;     ss << n;     return ss.str();   }   const int dx[] = {0, 1, 0, -1};   const int dy[] = {-1, 0, 1, 0};   bool is_right(vector<pair<int, int> > p) {     vector<int> len2(3, 0);     for (int i = 0; i < (3); i++) {       int x0 = p[i].first, y0 = p[i].second;       int x1 = p[(i + 1) % 3].first, y1 = p[(i + 1) % 3].second;       len2[i] = (x0 - x1) * (x0 - x1) + (y0 - y1) * (y0 - y1);     }     sort(len2.begin(), len2.end());     return ((len2[2] == len2[0] + len2[1]) &&             (sqrt(len2[2]) + 1e-8 < sqrt(len2[0]) + sqrt(len2[1])));   }   bool search_right_triangle(vector<pair<int, int> > p) {     for (int i = 0; i < (3); i++) {       for (int k = 0; k < (4); k++) {         vector<pair<int, int> > q = p;         q[i].first += dx[k];         q[i].second += dy[k];         if (is_right(q)) return true;       }     }     return false;   }   int main() {     vector<pair<int, int> > p(3);     for (int i = 0; i < (3); i++) {       cin >> p[i].first >> p[i].second;     }     if (is_right(p)) {       cout <<  RIGHT  << endl;     } else if (search_right_triangle(p)) {       cout <<  ALMOST  << endl;     } else {       cout <<  NEITHER  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   double const e = 0.000001;   int main() {     int n, a, d;     cin >> n >> a >> d;     int prevv = 10000000;     double prevt = 0;     for (int i = 0; i < n; i++) {       int t, v;       cin >> t >> v;       double t1 = (double)v / a;       double s1 = a * t1 * t1 / 2;       double t2 = 0;       if (s1 - d > e) {         t1 = sqrt((2.0 * d) / a);       } else {         double s2 = d - s1;         t2 = s2 / v;       }       double tt = t1 + t2;       if (t + tt - prevt > e) {         prevt = t + tt;         prevv = v;       }       printf( %f n , prevt);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     long long n;     long long m;     long long k;     cin >> n >> m >> k;     vector<long long> p(m);     for (long long i = 0; i < m; ++i) {       cin >> p[i];     }     long long i = 0;     long long r = ((p[0] + k - 1) / k) * k;     long long cur = 0;     long long cnt = 0;     long long ans = 0;     while (i < m) {       if (p[i] - cur > r && i < m) {         r = ((p[i] - cur + k - 1) / k) * k;       } else {         cnt = 0;         while (i < m && p[i] - cur <= r) {           ++cnt;           ++i;         }         ++ans;         cur += cnt;       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   double f(double x, double A, double B, double L) {     double y = sqrt(L * L - x * x);     return (A * x + B * y - x * y) / L;   }   int main(void) {     double A, B, L;     scanf( %lf%lf%lf , &A, &B, &L);     if (L <= B) {       printf( %.8f n , min(A, L));       return 0;     } else if (L <= A) {       printf( %.8f n , min(B, L));       return 0;     } else {       double left = 0.0, right = L, ma, mb;       for (int u = 0; u < 500; u++) {         ma = (left * 3 + right) / 4.0;         mb = (left + right * 3) / 4.0;         double fa = f(ma, A, B, L);         double fb = f(mb, A, B, L);         if (fa < fb)           right = mb;         else           left = ma;       }       double ff = f((left + right) / 2.0, A, B, L);       ff = min(ff, L);       ff = min(ff, A);       if (ff < 1e-8)         printf( My poor head =( n );       else         printf( %.8f n , ff);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     double d, l, v1, v2;     scanf( %lf %lf %lf %lf , &d, &l, &v1, &v2);     l -= d;     v1 += v2;     l /= v1;     printf( %f , l);   }


#include <bits/stdc++.h>   using namespace std;   void run_case() {     int n, k;     cin >> n >> k;     vector<int> A(n);     for (int &a : A) {       cin >> a;     }     vector<int> ans;     for (int i = 1; i < n; i++) {       ans.push_back(A[i - 1] - A[i]);     }     sort(ans.begin(), ans.end());     ;     int res = A[n - 1] - A[0];     for (int i = 0; i < k - 1; i++) {       res += ans[i];     }     cout << res;   }   int main() {     ios_base::sync_with_stdio(false);     int test = 1;     while (test--) {       run_case();       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     string s;     cin >> s;     string final;     long long int id = -1, count = 0, r = 0, l = 0;     for (long long int i = 0; i < (long long int)s.length(); i++) {       if (s[i] ==  # ) {         id = i;         count++;       }       if (s[i] ==  ( ) r++;       if (s[i] ==  ) ) l++;     }     vector<long long int> ans;     for (long long int i = 0; i < (long long int)s.length(); i++) {       if (i == id) {         long long int num = r - l - (count - 1);         if (num <= 0) {           cout << -1 << endl;           return;         }         ans.push_back(num);         while (num--) {           final +=  ) ;         }         i++;       }       if (s[i] ==  ) )         final +=  ) ;       else if (s[i] ==  ( )         final +=  ( ;       if (s[i] ==  # ) {         ans.push_back(1);         final +=  ) ;       }     }     l = 0, r = 0;     for (long long int i = 0; i < (long long int) final.length(); i++) {       if (l > r) {         cout << -1 << endl;         return;       }       if (final[i] ==  ( )         r++;       else         l++;     }     if (l != r) {       cout << -1 << endl;       return;     }     for (long long int i = 0; i < (long long int)ans.size(); i++) {       cout << ans[i] << endl;     }   }   int main() {     long long int t = 1;     for (long long int i = 1; i <= t; i++) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> cost[4];   vector<vector<int> > G;   void solve() {     long long n, p, w, d;     cin >> n >> p >> w >> d;     for (long long y = 0; y < w; y++) {       if (p < y * d) continue;       long long wx = p - y * d;       if (wx % w) continue;       long long x = wx / w;       if (x + y > n) continue;       cout << x <<     << y <<     << (n - x - y);       return;     }     cout << -1;   }   int main() {     ios_base::sync_with_stdio(false);     int T;     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1e6 + 10;   const long long inf = 1e18;   long long n;   long long arr[20];   bool flag = false;   void rec(long long i, long long s) {     if (i == n + 1 && s % 360 == 0) flag = true;     if (i > n) return;     rec(i + 1, s + arr[i]);     rec(i + 1, s - arr[i]);   }   int32_t main() {     cin >> n;     for (long long i = 1; i <= n; i++) cin >> arr[i];     rec(1, 0);     if (flag)       cout <<  YES ;     else       cout <<  NO ;   }


#include <bits/stdc++.h>   using namespace std;   long long a[1000006], s[1000006];   int main() {     ios_base::sync_with_stdio(false);     long long n, i, j, k;     string st;     cin >> st;     long long ln = st.size();     stack<long long> stk;     for (i = 0; i < ln; i++) {       if (st[i] ==  ( ) {         stk.push(i);       } else {         if (stk.empty()) {           s[i] = -1;         }         if (!stk.empty()) {           long long idx = stk.top();           stk.pop();           if (idx > 0 && st[idx - 1] ==  )  && s[idx - 1] >= 0)             s[i] = s[idx - 1];           else             s[i] = idx;           long long xx = i - s[i] + 1;           a[xx]++;         }       }     }     for (i = 1000005; i >= 0; i--) {       if (a[i] > 0) {         cout << i <<     << a[i] << endl;         return 0;       }     }     cout << 0 <<     << 1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, a[110][110], dp[110][110];   bool vis[110][110];   long long dfs(long long x, long long y, long long num) {     if (a[x][y] < num) return (long long)3000000000000000000;     if (x == n - 1 && y == m - 1) return a[x][y] - num;     if (dp[x][y] != -1) return dp[x][y];     long long ret = (long long)3000000000000000000;     if (y + 1 < m) {       ret = min(ret, dfs(x, y + 1, num + 1));     }     if (x + 1 < n) {       ret = min(ret, dfs(x + 1, y, num + 1));     }     return dp[x][y] = min((long long)3000000000000000000, ret + a[x][y] - num);   }   long long solve() {     long long ans = (long long)3000000000000000000;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (a[i][j] - i - j <= a[0][0]) {           memset(dp, -1, sizeof dp);           ans = min(ans, dfs(0, 0, a[i][j] - i - j));         }       }     }     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long t;     cin >> t;     while (t--) {       cin >> n >> m;       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           cin >> a[i][j];         }       }       cout << solve() <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   long long aa;   bool bb;   char ch;   long long scan() {     while (ch = getchar(), (ch <  0  || ch >  9 ) && ch !=  - )       ;     ch ==  -  ? (bb = 1, aa = 0) : (bb = 0, aa = ch - 48);     while (ch = getchar(), (ch >=  0  && ch <=  9 )) aa = aa * 10 + ch - 48;     return (bb ? (-aa) : (aa));   }   inline int Min(int u, int v) { return u < v ? u : v; }   const int INF = 2147483647;   int len_str;   char s[100000], str[100000];   int len[100000];   int pos[100000];   int g[100000][30] = {};   int fail[100000] = {}, sz = 0;   int K[100000], Q;   int n;   int ans[100000];   int set_num = 0, num[100000];   set<int> Set[100000];   queue<int> q;   struct E {     int to, next;   } e[100000];   int last[100000] = {}, tot = 0;   void add(int u, int v) {     e[++tot].next = last[u];     last[u] = tot;     e[tot].to = v;   }   void ins(int p) {     int i, u;     u = 0;     for (i = 0; i < len[p]; ++i) {       s[i] = s[i] -  a ;       if (!g[u][s[i]]) {         g[u][s[i]] = ++sz;       }       u = g[u][s[i]];     }     pos[u] = p;   }   void build_ac() {     int i, u, v;     for (i = 0; i < 26; ++i)       if (g[0][i]) q.push(g[0][i]);     while (!q.empty()) {       u = q.front();       q.pop();       for (i = 0; i < 26; ++i) {         if (g[u][i]) {           v = g[u][i];           q.push(v);           fail[v] = g[fail[u]][i];         } else if (u)           g[u][i] = g[fail[u]][i];       }     }   }   void build_tree() {     for (int i = 1; i <= sz; ++i) add(fail[i], i);   }   void dfs(int u) {     int max_node = u;     for (int i = last[u]; i; i = e[i].next) {       dfs(e[i].to);       if (Set[num[e[i].to]].size() > Set[num[max_node]].size())         max_node = e[i].to;     }     set<int>::iterator j;     for (int i = last[u]; i; i = e[i].next) {       if (e[i].to == max_node) continue;       for (j = Set[num[e[i].to]].begin(); j != Set[num[e[i].to]].end(); j++)         Set[num[max_node]].insert(*j);     }     if (max_node != u) {       for (j = Set[num[u]].begin(); j != Set[num[u]].end(); j++)         Set[num[max_node]].insert(*j);       num[u] = num[max_node];     }     if (pos[u]) {       vector<int> p;       for (j = Set[num[u]].begin(); j != Set[num[u]].end(); j++) p.push_back(*j);       int tmp = INF;       for (int k = K[pos[u]], h = k - 1; h < p.size(); ++h)         tmp = Min(tmp, p[h] - p[h - k + 1]);       if (tmp == INF)         ans[pos[u]] = -1;       else         ans[pos[u]] = tmp + len[pos[u]];     }   }   int main() {     scanf( %s , str);     len_str = strlen(str);     scanf( %d , &Q);     for (int i = 1; i <= Q; ++i) {       scanf( %d , &K[i]);       scanf( %s , s);       len[i] = strlen(s);       ins(i);     }     build_ac();     build_tree();     int u = 0;     for (int i = 0; i < len_str; ++i) {       u = g[u][str[i] -  a ];       if (!num[u]) num[u] = ++set_num;       Set[num[u]].insert(i);     }     dfs(0);     for (int i = 1; i <= Q; ++i) printf( %d n , ans[i]);     return 0;   }


#include <bits/stdc++.h>   int n, k, pos[1000005];   char s[1000005];   struct ac_automaton {     int ch[1000005][26], fail[1000005], arr[1000005], cnt[1000005], totc;     int fir[1000005], nxt[1000005], to[1000005], tot, in[1000005], out[1000005],         block;     void line(int x, int y) {       nxt[++tot] = fir[x];       fir[x] = tot;       to[tot] = y;     }     void update(int x, int d) {       for (int i = x; i <= block; i += i & (-i)) arr[i] += d;     }     int getsum(int x) {       int s = 0;       for (int i = x; i; i -= i & (-i)) s += arr[i];       return s;     }     void insert(char *s, int k) {       int r = 0, v;       for (int i = 0; s[i]; i++) {         v = s[i] -  a ;         if (!ch[r][v]) ch[r][v] = ++totc;         r = ch[r][v];       }       pos[k] = r;     }     int Q[1000005], head, tail;     void build() {       Q[head = 0] = 0, tail = 1;       int r, c;       while (head < tail) {         r = Q[head++];         for (int i = 0; i < 26; i++)           if (c = ch[r][i]) {             Q[tail++] = c;             fail[c] = r ? ch[fail[r]][i] : 0;           } else             ch[r][i] = ch[fail[r]][i];       }       for (int i = 1; i <= totc; i++) line(fail[i], i);     }     void dfs(int u) {       in[u] = ++block;       for (int i = fir[u]; i; i = nxt[i]) dfs(to[i]);       out[u] = block;     }     void modify(int x, int d) {       if (cnt[x] == d) return;       cnt[x] = d;       update(in[x], d);       update(out[x] + 1, -d);     }     int find(char *s) {       int r = 0, v, ans = 0;       for (int i = 0; s[i]; i++) {         v = s[i] -  a ;         r = ch[r][v];         ans += getsum(in[r]);       }       return ans;     }   } A;   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= k; i++) scanf( %s , s), A.insert(s, i);     A.build();     A.dfs(0);     for (int i = 1; i <= k; i++) A.modify(pos[i], 1);     int x;     char c;     for (int i = 1; i <= n; i++) {       getchar(), c = getchar();       if (c ==  ? )         scanf( %s , s), printf( %d n , A.find(s));       else         scanf( %d , &x), A.modify(pos[x], c ==  +  ? 1 : -1);     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = (4000000) + 7;   const int inf = (1e9) + 7;   const long long LLinf = (1e18) + 7;   const long long mod = 1e9 + 7;   int main() {     int n;     scanf( %d , &n);     n++;     n /= 2;     n--;     printf( %d , n);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e18 + 5;   const int mod = 1e9 + 7;   const int N = 3 * 1e5 + 5;   int n, c[N], maxOf[N];   int main() {     ios_base::sync_with_stdio(false);     cin >> n;     for (int i = 0; i < n; i++) {       cin >> c[i];       maxOf[c[i]] = i;     }     int last = c[n - 1];     int bb = -1;     for (int i = n - 1; i >= 0; i--) {       if (c[i] != last) {         bb = i;         break;       }     }     assert(bb != -1);     int ans = 0;     for (int i = 0; i < n; i++) {       if (c[i] != last) {         ans = max(ans, n - 1 - i);       } else {         ans = max(ans, bb - i);       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int M = 998244353;   void solve() {     long long int a;     cin >> a;     cout << ((a / 2) + 1) << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int t = 1;     cin >> t;     while (t--) solve();   }


#include <bits/stdc++.h>   const int MaxN = 300000;   using namespace std;   long long arr[MaxN + 5], a[MaxN + 5], n;   struct Segment {     int l, r, lx, mx, rx;     int Sign(long long x) { return x > 0 ? 1 : -1; }     void Update(Segment A, Segment B) {       lx = A.lx, rx = B.rx;       mx = max(A.mx, B.mx);       if (a[A.r] && a[B.l] && Sign(a[A.r]) >= Sign(a[B.l])) {         mx = max(mx, A.rx + B.lx);         if (A.mx == A.r - A.l + 1) lx = A.mx + B.lx;         if (B.mx == B.r - B.l + 1) rx = B.mx + A.rx;       }       mx = max(mx, max(lx, rx));       l = A.l, r = B.r;     }   } seg[MaxN * 4 + 5];   void Update(int l, int r, int t, int x, int w) {     if (l == r) {       a[x] += w;       if (a[x] != 0)         seg[t] = (Segment){l, r, 1, 1, 1};       else         seg[t] = (Segment){l, r, 0, 0, 0};       return;     }     int mid = (l + r) >> 1;     if (x <= mid)       Update(l, mid, t * 2, x, w);     else       Update(mid + 1, r, t * 2 + 1, x, w);     seg[t].Update(seg[t * 2], seg[t * 2 + 1]);   }   int main() {     while (~scanf( %d , &n)) {       for (int i = 1; i <= n; i++) scanf( %d , &arr[i]);       for (int i = 1; i <= n; i++) a[i] = arr[i + 1] - arr[i];       for (int i = 1; i < n; i++) Update(1, n - 1, 1, i, 0);       int m;       scanf( %d , &m);       for (int i = 1; i <= m; i++) {         int l, r, w;         scanf( %d%d%d , &l, &r, &w);         if (l - 1 >= 1) Update(1, n - 1, 1, l - 1, w);         if (r <= n - 1) Update(1, n - 1, 1, r, -w);         printf( %d n , seg[1].mx + 1);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2.5e5 + 10;   template <typename T>   inline T read() {     T ans = 0, flag = 1;     char c = getchar();     while (!isdigit(c)) {       if (c ==  - ) flag = -1;       c = getchar();     }     while (isdigit(c)) {       ans = ans * 10 + (c - 48);       c = getchar();     }     return ans * flag;   }   int n, k, p;   long long power_pow(long long base, int b, const long long MOD = p) {     long long ans = 1;     while (b) {       if (b & 1) (ans *= base) %= MOD;       (base *= base) %= MOD;       b >>= 1;     }     return ans;   }   int fac[maxn], Inv[maxn];   void init() {     const int MOD = p;     fac[0] = 1;     for (register int i(1), end_i(n); i <= end_i; i++)       fac[i] = 1ll * i * fac[i - 1] % MOD;     Inv[n] = power_pow(fac[n], MOD - 2);     for (register int i(n - 1), end_i(0); i >= end_i; i--)       Inv[i] = (i + 1ll) * Inv[i + 1] % MOD;   }   int f[26][maxn];   int dp(int x, int S) {     if (~f[x][S]) return f[x][S];     const int MOD = p;     if (!S) {       long long res = fac[n];       for (register int i(1), end_i(x); i <= end_i; i++)         res = res * (k - i + 1) % MOD;       return res;     }     long long res = 0;     int U = S & (S - 1);     for (int U = S & (S - 1), T = U;; T = (T - 1) & U) {       res = (res + 1ll * Inv[S - T] * dp(x + 1, T)) % MOD;       if (!T) break;     }     return f[x][S] = res;   }   int main() {     cin >> n >> k >> p;     long long ans = power_pow(k, n);     const int MOD = p;     if (n % 2 == 0) {       init();       memset(f, -1, sizeof f);       ans = (ans + MOD - dp(0, n)) % MOD;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   namespace XX {   template <template <typename> class Compare, typename T>   inline T& UP(T& x, const T& y) {     if (Compare<T>()(y, x)) x = y;     return x;   }   template <typename Compare, typename T>   inline T& UP(T& x, const T& y, Compare comp) {     if (comp(y, x)) x = y;     return x;   }   template <typename T>   inline T& GT(T& x, const T& y) {     return UP<std::greater>(x, y);   }   template <typename T>   inline T& LS(T& x, const T& y) {     return UP<std::less>(x, y);   }   template <typename T>   struct Mapper {     int operator[](const T& v) {       int& ret = table[v];       if (!ret) rtable[ret = table.size()] = v;       return ret - 1;     }     template <typename... Args>     int operator()(Args... args) {       return (*this)[T(args...)];     }     T rev(int idx) { return rtable[idx + 1]; }     std::map<T, int> table;     std::map<int, T> rtable;   };   template <typename T, int S>   struct ReferenceArray {     struct It {       typename std::array<T*, S>::iterator it;       T& operator*() { return **it; }       void operator++() { it++; }       bool operator!=(const It& other) { return it != other.it; }     };     int size() const { return _ptr.size(); }     It begin() const { return {_ptr.begin()}; }     It end() const { return {_ptr.end()}; }     T& operator[](int idx) const { return *_ptr[idx]; }     mutable std::array<T*, S> _ptr;   };   template <typename T, typename... Args>   ReferenceArray<T, sizeof...(Args) + 1> MAKEV(T& arg1, Args&... args) {     return {&arg1, &args...};   }   struct Range {     struct It {       int num, step;       int operator*() { return num; }       void operator++() { num += step; }       bool operator!=(const It& other) { return num != other.num; }     };     Range(int ee) : b(0), e(ee) {}     Range(int bb, int ee) : b(bb), e(ee) {}     It begin() { return {b, (b < e ? 1 : -1)}; }     It end() { return {e, 0}; }     int b, e;   };   }  // namespace XX   template <typename T>   struct ScanfSpecifier {};   template <>   struct ScanfSpecifier<int> {     static constexpr const char* value =  %d ;   };   template <>   struct ScanfSpecifier<double> {     static constexpr const char* value =  %lf ;   };   template <>   struct ScanfSpecifier<float> {     static constexpr const char* value =  %f ;   };   template <>   struct ScanfSpecifier<char> {     static constexpr const char* value =  %c ;   };   template <>   struct ScanfSpecifier<const char*> {     static constexpr const char* value =  %s ;   };   template <>   struct ScanfSpecifier<unsigned long> {     static constexpr const char* value =  %lu ;   };   template <>   struct ScanfSpecifier<char*> {     static constexpr const char* value =  %s ;   };   template <>   struct ScanfSpecifier<unsigned int> {     static constexpr const char* value =  %u ;   };   template <>   struct ScanfSpecifier<long long int> {     static constexpr const char* value =  %lld ;   };   template <typename T>   int RD(T& arg) {     return std::scanf(ScanfSpecifier<T>::value, &arg);   }   template <int S>   int RD(char (&arg)[S]) {     return std::scanf( %s , arg);   }   template <>   int RD<char*>(char*& arg) {     return std::scanf( %s , arg);   }   template <>   int RD<char>(char& arg) {     return std::scanf(  %c , &arg);   }   template <typename T, typename... Args>   int RD(T& arg1, Args&... args) {     return RD(arg1) + RD(args...);   }   template <typename T>   T RD() {     T ret;     RD(ret);     return ret;   }   template <typename It>   void RDV(It begin, It end) {     while (begin != end) RD(*begin++);   }   template <typename C>   void RDV(C& c) {     RDV(std::begin(c), std::end(c));   }   template <typename... Args>   void WT(Args... args) {     int alc = 0;     int dummy[] = {((alc++ ? std::printf(   ) : 0),                     std::printf(ScanfSpecifier<Args>::value, args), 0)...};   }   template <typename... Args>   void WTL(Args... args) {     WT(args...);     std::printf(  n );   }   template <typename It>   void WTV(It begin, It end) {     int alc = 0;     while (begin != end) (alc++ ? std::printf(   ) : 0), WT(*begin++);   }   template <typename C>   void WTV(const C& c) {     WTV(std::begin(c), std::end(c));   }   template <typename It>   void WTVL(It begin, It end) {     WTV(begin, end);     std::printf(  n );   }   template <typename C>   void WTVL(const C& c) {     WTVL(std::begin(c), std::end(c));   }   template <typename T>   T& UMAX(T& x, T y) {     return XX::UP<std::greater>(x, y);   }   template <typename T>   T& UMIN(T& x, T y) {     return XX::UP<std::less>(x, y);   }   using XX::UP;   using RG = XX::Range;   using XX::MAKEV;   using XX::Mapper;   using namespace std;   map<pair<long long int, long long int>, long long int> cost;   vector<pair<long long int, long long int> > road(long long int u,                                                    long long int v) {     vector<pair<long long int, long long int> > ret;     while (u != v) {       if (u > v) swap(u, v);       ret.emplace_back(v, v / 2);       v /= 2;     }     return ret;   }   int main() {     int Q;     RD(Q);     while (Q--) {       int t;       RD(t);       if (t == 1) {         long long int u, v, w;         RD(u, v, w);         for (pair<long long int, long long int> r : road(u, v)) cost[r] += w;       } else {         long long int u, v;         RD(u, v);         long long int ans = 0;         for (pair<long long int, long long int> r : road(u, v)) ans += cost[r];         WTL(ans);       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     int aa[2010];     scanf( %d%d , &n, &k);     for (int i = 0; i < n; i++) scanf( %d , &aa[i]);     sort(aa, aa + n);     int p = n - 1;     int ans = 0;     while (p >= 0) {       int temp = aa[p];       ans += (temp - 1) * 2;       p -= k;     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   int d[1000];   void Qsort(int, int);   bool Checked(int, int);   int main(int argc, char** argv) {     char a[10000];     std ::cin >> a;     int id = 0;     int k = 0;     while (a[k] !=   0 ) {       int num = 0;       while ((a[k] !=   0 ) && (a[k] !=  , )) {         num = (num * 10) + (a[k] -  0 );         k++;       }       k++;       if (Checked(num, id) == true) d[id++] = num;     }     bool flag = false;     Qsort(0, id - 1);     int cnt = 0;     int index = 0;     for (int i = 1; i <= id; i++) {       if (d[i - 1] + 1 == d[i]) {         if (cnt == 0) index = i - 1;         cnt++;       } else {         if (cnt != 0) {           if (flag == true) {             std ::cout <<  ,  << d[index] <<  -  << d[index + cnt];           } else {             std ::cout << d[index] <<  -  << d[index + cnt];             flag = true;           }           cnt = 0;         } else {           if (flag == true) {             std ::cout <<  ,  << d[i - 1];           } else {             std ::cout << d[i - 1];             flag = true;           }           cnt = 0;         }       }     }     if (flag == true) {       if (cnt != 0) std ::cout <<  ,  << d[index] <<  -  << d[index + cnt];     } else {       if (cnt != 0) std ::cout << d[index] <<  -  << d[index + cnt];     }     return 0;   }   void Qsort(int l, int r) {     if (l > r) return;     int i = l;     int j = r;     int m = d[(l + r) / 2];     while (i <= j) {       while (d[i] < m) i++;       while (d[j] > m) j--;       if (i <= j) {         int temp = d[i];         d[i] = d[j];         d[j] = temp;         i++;         j--;       }     }     Qsort(l, j);     Qsort(i, r);   }   bool Checked(int key, int n) {     for (int i = 0; i < n; i++) {       if (d[i] == key) return false;     }     return true;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 10;   int n, vis[N];   int Log2[N], a[N], st[N][20], lim[N];   void pre() {     Log2[1] = 0, st[1][0] = a[1];     for (int i = 2; i <= n; ++i) {       Log2[i] = Log2[i - 1];       if (i == 1 << Log2[i - 1] + 1) Log2[i]++;       st[i][0] = a[i];     }     for (int i = n; i >= 1; --i)       for (int j = 1; i + (1 << j) - 1 <= n; ++j) {         st[i][j] = max(st[i][j - 1], st[i + (1 << j - 1)][j - 1]);       }   }   int ask(int l, int r) {     int x = Log2[r - l + 1];     return max(st[l][x], st[r - (1 << x) + 1][x]);   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     pre();     lim[n + 1] = n;     for (int i = n; i >= 1; i--) {       int x = a[i];       if (vis[x])         lim[i] = min(lim[i + 1], vis[x] - 1);       else         lim[i] = lim[i + 1];       vis[x] = i;     }     long long res = 0;     for (int i = 1; i <= n; i++) {       int j = i;       while (j <= lim[i]) {         int ma = ask(i, j);         if (ma == (j - i + 1))           res++, j++;         else           j = i + ma - 1;       }     }     printf( %lld , res);   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_SIZE = 1e6 + 10, INF = 2 * 1e9, MAX_N = 7e3 + 5, MAX_K = 3e4 + 5;   const long long SuperINF = 1e18;   const double eps = 0.5, PI = 20 / 7;   void files() {     freopen( input.txt ,  r , stdin);     freopen( output.txt ,  w , stdout);   }   int tests;   int main() {     std::ios::sync_with_stdio(0);     cin >> tests;     for (int tt = 0; tt < tests; tt++) {       int n, k;       cin >> n >> k;       string s;       cin >> s;       set<int> open, close;       for (int i = 0; i < s.size(); i++) {         if (s[i] ==  ( )           open.insert(i);         else           close.insert(i);       }       vector<pair<int, int> > indexes;       bool need_open = false;       int cnt = 2 * (k - 1);       int ost = (n - 2 * (k - 1)) / 2;       int now_ost = 0;       bool flag = false;       for (int i = 0; i < s.size(); i++) {         if (cnt > 0)           need_open ^= true;         else {           flag = true;           if (ost > 0) {             need_open = true;           } else             break;         }         if (need_open) {           if (s[i] !=  ( ) {             if (open.size() == 0) return 1;             int f = *open.begin();             close.insert(f);             close.erase(i);             if (open.find(f) == open.end()) return 1;             open.erase(f);             indexes.push_back(make_pair(i + 1, f + 1));             swap(s[i], s[f]);           } else {             if (open.find(i) == open.end()) return 1;             open.erase(i);           }         } else {           if (s[i] !=  ) ) {             if (close.size() == 0) return 1;             int f = *close.begin();             open.insert(f);             open.erase(i);             if (close.find(f) == close.end()) return 1;             close.erase(f);             indexes.push_back(make_pair(i + 1, f + 1));             swap(s[i], s[f]);           } else {             if (close.find(i) == close.end()) return 1;             close.erase(i);           }         }         cnt--;         if (flag) ost--;       }       cout << indexes.size() <<   n ;       for (auto x : indexes) cout << x.first <<     << x.second <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int q;   bool read() {     if (scanf( %d , &q) < 1) {       return 0;     }     return 1;   }   int get_dig(char c) {     if ( 0  <= c && c <=  9 ) {       return c -  0 ;     }     if ( a  <= c && c <=  f ) {       return c -  a  + 10;     }     return -1;   }   unsigned long long read_num() {     string res;     char c;     while (1) {       c = getchar();       int d = get_dig(c);       if (d == -1) {         if (!res.empty()) {           break;         }         continue;       }       res += c;     }     unsigned long long coef = 1;     unsigned long long ans = 0;     for (int i = int(((int)(res).size())) - 1; i >= 0; --i) {       int d = get_dig(res[i]);       ans += coef * d;       coef *= 16;     }     return ans;   }   bool bit(int x, int i) { return (x >> i) & 1; }   unsigned long long dp[20][2][2][2];   unsigned long long solve(unsigned long long r) {     if (!r) {       return 0;     }     unsigned long long old = r;     vector<int> R;     for (int it = 0; it < int(16); ++it) {       R.push_back(r % 16);       r /= 16;     }     reverse(begin(R), end(R));     r = old;     unsigned long long ans = 0;     for (int hb = 1; hb < 16; ++hb) {       memset(dp, 0, sizeof dp);       dp[0][0][0][0] = 1;       for (int i = 0; i < int(16); ++i)         for (int was16 = 0; was16 < int(2); ++was16)           for (int was2 = 0; was2 < int(2); ++was2)             for (int ls = 0; ls < int(2); ++ls) {               long long cur_dp = dp[i][was16][was2][ls];               if (!cur_dp) {                 continue;               }               for (int d = 0; d < int(16); ++d) {                 if (d > hb) {                   continue;                 }                 if (!ls && d > R[i]) {                   continue;                 }                 int nwas16 = was16 || d == hb;                 int nwas2 = was2;                 if (hb / 4 == 15 - i && bit(d, hb % 4)) {                   nwas2 = 1;                 }                 int nls = ls || d < R[i];                 dp[i + 1][nwas16][nwas2][nls] += cur_dp;               }             }       unsigned long long cur = 0;       cur += dp[16][1][1][0];       cur += dp[16][1][1][1];       ans += cur;     }     return ans;   }   void solve() {     for (int it = 0; it < int(q); ++it) {       unsigned long long l, r;       l = read_num();       r = read_num();       unsigned long long ans = solve(r);       if (l > 0) {         ans -= solve(l - 1);       }       printf( %llu n , ans);     }   }   int main() {     while (read()) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i, sum = 0;     scanf( %d , &n);     int a[n];     for (i = 0; i < n; i++) {       scanf( %d , &a[i]);     }     sort(a, a + n);     for (i = 0; i < n; i++) {       sum += a[i];     }     printf( %d n , a[n - 1] * n - sum);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   int len(const T &c) {     return (int)c.size();   }   template <class T>   void cmin(T &a, T b) {     if (b < a) a = b;   }   template <class T>   void cmax(T &a, T b) {     if (b > a) a = b;   }   vector<pair<int, int> > v;   int main() {     int n, t1, t2;     cin >> n;     for (int i(0), _n(n); i < _n; ++i) {       cin >> t1 >> t2;       if (t1 > t2) swap(t1, t2);       v.push_back(make_pair(t1, t2));     }     vector<int> ans;     vector<pair<int, int> > tem;     bool b;     for (int i(0), _n(n); i < _n; ++i) {       b = 1;       tem.clear();       for (int j(0), _n(n); j < _n; ++j)         if (j != i) tem.push_back(v[j]);       sort(tem.begin(), tem.end());       for (int j(0), _n(n - 2); j < _n; ++j) {         if (tem[j].second > tem[j + 1].first) {           b = 0;           break;         }       }       if (b) ans.push_back(i + 1);     }     cout << len(ans) << endl;     for (int i(0), _n(len(ans)); i < _n; ++i) cout << ans[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   const int step[2] = {-1, 1}, mn = 101000;   int n, m, xs, ys, kx, ky, x, y, fx, fy, cnt;   long long ans;   string st;   set<int> T[mn];   void fix(int x, int y, int& fx, int& fy) {     if (x == 1)       fx = 1;     else if (x == n)       fx = 0;     if (y == 1)       fy = 1;     else if (y == m)       fy = 0;   }   int run(int& x, int& y, int& fx, int& fy) {     int l = n;     if (fx == 0)       l = min(l, x - 1);     else       l = min(l, n - x);     if (fy == 0)       l = min(l, y - 1);     else       l = min(l, m - y);     x += l * step[fx], y += l * step[fy];     fix(x, y, fx, fy);     return l;   }   int main() {     cin >> n >> m >> xs >> ys >> st;     kx = st[0] ==  D , ky = st[1] ==  R ;     fix(xs, ys, kx, ky);     x = xs, y = ys, fx = kx, fy = ky;     ans = cnt = 1;     T[x].insert(y);     for (;;) {       ans += run(x, y, fx, fy);       if (!T[x].count(y)) {         ++cnt;         T[x].insert(y);       }       if (cnt == n + m - 2) break;       if (x == xs && y == ys && fx == kx && fy == ky) break;     }     if (cnt == n + m - 2)       cout << ans << endl;     else       cout << -1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 10005;   bool dp[N][2];   char s[N];   set<string> ans;   inline bool ck(int x, int y) {     bool same = true;     for (int i = 0; x + i < y; ++i) {       same &= s[x + i] == s[y + i];     }     return !same;   }   int main() {     scanf( %s , s);     int n = strlen(s);     dp[n][0] = dp[n][1] = true;     for (int i = n - 2; i >= 5; --i) {       dp[i][0] = dp[i + 2][1] || (dp[i + 2][0] && ck(i, i + 2));       dp[i][1] = dp[i + 3][0] || (dp[i + 3][1] && ck(i, i + 3));       if (dp[i][0]) ans.insert(string(s + i, s + i + 2));       if (dp[i][1]) ans.insert(string(s + i, s + i + 3));     }     printf( %d n , ans.size());     for (auto &x : ans) {       printf( %s n , x.c_str());     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bitset<300000005> pr;   int uke[20000005];   int anti_uke(int l, int r) {     int ans = 0;     for (register int i = 5; i <= r; i += 4) {       if (i >= l && pr[i] == 0) ++ans;     }     if (l <= 2 && r >= 2) ++ans;     return ans;   }   int main() {     int l, r;     scanf( %d %d , &l, &r);     pr[0] = pr[1] = 1;     int cnt = 0;     for (register int i = 2; i <= r; i++) {       if (pr[i] == 0) uke[++cnt] = i;       for (register int j = 1; j <= cnt && i * uke[j] <= r; j++) {         pr[i * uke[j]] = 1;         if (i % uke[j] == 0) break;       }     }     printf( %d n , anti_uke(l, r));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 5;   char str[MAXN];   int main() {     scanf( %s , str);     int n = strlen(str);     printf( 3 nL 2 nR 2 nR %d , 2 * n - 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int prim[100000];   int main() {     int a, b;     cin >> a >> b;     bool ok = false;     int x = 0;     for (int i = 3; i <= a; i++) {       for (int j = 2; j < i / 2; j++) {         if (i % j == 0) {           ok = true;           break;         }       }       if (ok == false) {         prim[x] = i;         x++;       }       ok = false;     }     int sum = 0;     int xx = 0;     for (int i = 0; i < x; i++) {       sum = prim[i] + prim[i + 1] + 1;       for (int j = 0; j < x; j++) {         if (sum == prim[j]) {           xx++;           break;         }       }     }     if (xx >= b) {       cout <<  YES ;       return 0;     } else {       cout <<  NO ;       return 0;     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 5e5 + 10, mod = 1e9 + 9, base = 41, INF = 1e9 + 10, sq = 502;   int main() {     ios_base::sync_with_stdio(false), cin.tie(NULL);     long long n, b;     cin >> n >> b;     long long a[n];     for (long long i = 0; i < (n); ++i) cin >> a[i];     long long maximum = b;     for (long long i = 0; i < (n); ++i) {       for (int j = i; j < n; j++) {         long long buy = b / a[i];         long long more = b % a[i];         long long sell = a[j] * buy + more;         if (sell > maximum) maximum = sell;       }     }     cout << maximum << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 2000000000;   const long long infLL = 9000000000000000000;   template <typename first, typename second>   ostream& operator<<(ostream& os, const pair<first, second>& p) {     return os <<  (  << p.first <<  ,   << p.second <<  ) ;   }   template <typename T>   ostream& operator<<(ostream& os, const vector<T>& v) {     os <<  { ;     for (auto it = v.begin(); it != v.end(); ++it) {       if (it != v.begin()) os <<  ,  ;       os << *it;     }     return os <<  } ;   }   template <typename T>   ostream& operator<<(ostream& os, const set<T>& v) {     os <<  [ ;     for (auto it = v.begin(); it != v.end(); ++it) {       if (it != v.begin()) os <<  , ;       os << *it;     }     return os <<  ] ;   }   template <typename T>   ostream& operator<<(ostream& os, const multiset<T>& v) {     os <<  [ ;     for (auto it = v.begin(); it != v.end(); ++it) {       if (it != v.begin()) os <<  ,  ;       os << *it;     }     return os <<  ] ;   }   template <typename first, typename second>   ostream& operator<<(ostream& os, const map<first, second>& v) {     os <<  [ ;     for (auto it = v.begin(); it != v.end(); ++it) {       if (it != v.begin()) os <<  ,  ;       os << it->first <<   =   << it->second;     }     return os <<  ] ;   }   void faltu() { cerr <<   n ; }   template <typename T>   void faltu(T a[], int n) {     for (int i = 0; i < n; ++i) cerr << a[i] <<    ;     cerr <<   n ;   }   template <typename T, typename... hello>   void faltu(T arg, const hello&... rest) {     cerr << arg <<    ;     faltu(rest...);   }   int n, m;   vector<pair<int, int> > vec1[105], vec2[105];   int x[1005], y[1005];   int cnt1[105], cnt2[105];   vector<pair<int, int> > store[105];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     cin >> n >> m;     for (int i = 1; i <= n; ++i) cnt1[i] = cnt2[i] = 1;     for (int i = 0; i < m; ++i) {       cin >> x[i] >> y[i];       if (x[i] > y[i]) swap(x[i], y[i]);       ++cnt1[x[i]];       ++cnt2[y[i]];     }     int r1 = 1e6, c1 = 1e6;     int r2 = r1 - 1, c2 = c1 + 1;     for (int i = 1; i <= n; ++i) {       for (int j = 0; j < cnt1[i]; ++j) {         vec1[i].push_back(make_pair(r1, c1));         store[i].push_back(make_pair(r1, c1));         ++r1;       }       for (int j = 0; j < cnt2[i]; ++j) {         vec2[i].push_back(make_pair(r2, c2));         store[i].push_back(make_pair(r2, c2));         ++c2;       }       reverse(vec1[i].begin(), vec1[i].end());       reverse(vec2[i].begin(), vec2[i].end());       store[i].push_back(make_pair(vec1[i].back().first, vec2[i].back().second));       vec1[i].pop_back();       vec2[i].pop_back();       --c1;       --r2;     }     for (int i = 0; i < m; ++i) {       assert(!vec1[x[i]].empty());       assert(!vec2[y[i]].empty());       store[x[i]].push_back(           make_pair(vec1[x[i]].back().first, vec2[y[i]].back().second));       vec1[x[i]].pop_back();       vec2[y[i]].pop_back();     }     for (int i = 1; i <= n; ++i) {       cout << (int)store[i].size() <<   n ;       for (auto x : store[i]) cout << x.first <<     << x.second <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int mm = 1111;   struct point {     double x, y;     point() {}     point(double _x, double _y) : x(_x), y(_y) {}   } g[mm];   point MakeVector(point &P, point &Q) { return point(Q.x - P.x, Q.y - P.y); }   double CrossProduct(point P, point Q) { return P.x * Q.y - P.y * Q.x; }   double MultiCross(point P, point Q, point R) {     return CrossProduct(MakeVector(Q, P), MakeVector(Q, R));   }   struct halfPlane {     point s, t;     double angle;     halfPlane() {}     halfPlane(point _s, point _t) : s(_s), t(_t) {}     halfPlane(double sx, double sy, double tx, double ty)         : s(sx, sy), t(tx, ty) {}     void GetAngle() { angle = atan2(t.y - s.y, t.x - s.x); }   } hp[mm], q[mm];   point IntersectPoint(halfPlane P, halfPlane Q) {     double a1 = CrossProduct(MakeVector(P.s, Q.t), MakeVector(P.s, Q.s));     double a2 = CrossProduct(MakeVector(P.t, Q.s), MakeVector(P.t, Q.t));     return point((P.s.x * a2 + P.t.x * a1) / (a2 + a1),                  (P.s.y * a2 + P.t.y * a1) / (a2 + a1));   }   bool cmp(halfPlane P, halfPlane Q) {     if (fabs(P.angle - Q.angle) < 1e-8) return MultiCross(P.s, P.t, Q.s) > 0;     return P.angle < Q.angle;   }   bool IsParallel(halfPlane P, halfPlane Q) {     return fabs(CrossProduct(MakeVector(P.s, P.t), MakeVector(Q.s, Q.t))) < 1e-8;   }   void HalfPlaneIntersect(int n, int &m) {     sort(hp, hp + n, cmp);     int i, l = 0, r = 1;     for (m = i = 1; i < n; ++i)       if (hp[i].angle - hp[i - 1].angle > 1e-8) hp[m++] = hp[i];     n = m;     m = 0;     q[0] = hp[0], q[1] = hp[1];     for (i = 2; i < n; ++i) {       if (IsParallel(q[r], q[r - 1]) || IsParallel(q[l], q[l + 1])) return;       while (l < r &&              MultiCross(hp[i].s, hp[i].t, IntersectPoint(q[r], q[r - 1])) > 0)         --r;       while (l < r &&              MultiCross(hp[i].s, hp[i].t, IntersectPoint(q[l], q[l + 1])) > 0)         ++l;       q[++r] = hp[i];     }     while (l < r &&            MultiCross(q[l].s, q[l].t, IntersectPoint(q[r], q[r - 1])) > 0)       --r;     while (l < r &&            MultiCross(q[r].s, q[r].t, IntersectPoint(q[l], q[l + 1])) > 0)       ++l;     q[++r] = q[l];     for (i = l; i < r; ++i) g[m++] = IntersectPoint(q[i], q[i + 1]);   }   int main() {     int n, m;     point a, b;     while (cin >> n) {       for (int i = 0; i < n; i++) cin >> g[i].x >> g[i].y;       for (int i = 0; i < n; i++) {         hp[i] = halfPlane(g[(i + 1) % n], g[i]);         hp[i].GetAngle();       }       a = g[0], b = g[1];       HalfPlaneIntersect(n, m);       if (m < 3) {         cout << 0 << endl;         continue;       }       int ans = 0;       if (a.x > b.x) swap(a, b);       int f2 = 0, f1 = 0;       for (int i = 0; i < m; i++)         if (g[i].y == a.y && g[(i + 1) % m].y == a.y) {           f2 = 1;           a = g[i], b = g[(i + 1) % m];           break;         } else if (g[i].y == a.y)           f1 = 1;       if (f1 && !f2)         cout << 1 << endl;       else if (f2) {         if (a.x > b.x) swap(a, b);         cout << int(b.x - a.x) + 1 << endl;       } else         cout << 0 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int a[200000 + 5];   int d[200000 + 5];   set<pair<int, int> > s;   set<pair<int, int> >::iterator it;   void djikstra() {     int u, v;     fill(d, d + n + 2, (int)100000000);     d[1] = 0;     s.insert(make_pair(d[1], 1));     while (!s.empty()) {       it = s.begin();       u = (*it).second;       s.erase(it);       v = a[u];       if (d[v] > (d[u] + 1)) {         if (d[v] != (int)100000000) s.erase(s.find(make_pair(d[v], v)));         d[v] = d[u] + 1;         s.insert(make_pair(d[v], v));       }       v = u - 1;       if (v >= 1 && d[v] > (d[u] + 1)) {         if (d[v] != (int)100000000) s.erase(s.find(make_pair(d[v], v)));         d[v] = d[u] + 1;         s.insert(make_pair(d[v], v));       }       v = u + 1;       if (v <= n && d[v] > (d[u] + 1)) {         if (d[v] != (int)100000000) s.erase(s.find(make_pair(d[v], v)));         d[v] = d[u] + 1;         s.insert(make_pair(d[v], v));       }     }   }   int main() {     int i;     scanf( %d , &n);     for (i = 1; i <= n; i++) scanf( %d , &a[i]);     djikstra();     for (i = 1; i <= n; i++) printf( %d  , d[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000000007;   void sync_stdio() {     cin.tie(NULL);     ios_base::sync_with_stdio(false);   }   struct Sync_stdio {     Sync_stdio() {       cin.tie(NULL);       ios_base::sync_with_stdio(false);     }   } _sync_stdio;   vector<int> v;   vector<double> pref;   vector<double> inv;   vector<double> sum;   double get(int x, int y) {     return sum[y] - sum[x - 1] - (inv[y] - inv[x - 1]) * pref[x - 1];   }   vector<vector<double>> dp;   const double EPS = 1e-7;   void solve(int i, int l, int r, int optl, int optr) {     int mid = (l + r) / 2;     int optm = 0;     for (int j = (optl); j < (optr + 1); ++j) {       if (dp[i][mid] + EPS > dp[i - 1][j] + get(j + 1, mid)) {         dp[i][mid] = dp[i - 1][j] + get(j + 1, mid);         optm = j;       }     }     if (l <= mid - 1) {       solve(i, l, mid - 1, optl, optm);     }     if (mid + 1 <= r) {       solve(i, mid + 1, r, optm, optr);     }   }   int main() {     int n, k;     cin >> n >> k;     v.resize(n);     for (int __i = 0; __i < (n); ++__i) cin >> v[__i];     ;     pref.resize(n + 1);     inv.resize(n + 1);     sum.resize(n + 1);     for (int i = (1); i < (n + 1); ++i) {       pref[i] = pref[i - 1] + v[i - 1];       inv[i] = inv[i - 1] + 1.0 / v[i - 1];       sum[i] = sum[i - 1] + pref[i] / v[i - 1];     }     dp.assign(k + 1, vector<double>(n + 1, 1e18));     vector<vector<long long>> prev(k + 1, vector<long long>(n + 1, -1));     dp[0][0] = 0;     cout << fixed << setprecision(10);     for (int i = (1); i < (k + 1); ++i) {       solve(i, 1, n, 0, n);     }     cout << dp[k][n];     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize(2)   using namespace std;   const int mod = 1e9 + 7;   const double eps = 1e-6;   const int N = 1e6 + 7;   int tr[N][26], id[N], idx;   char s[100007][1003], str[10007];   bool flag;   int n, m, len[N];   int res[N], cnt;   int trans(char c) {     if (c >=  A  && c <=  Z ) c = c -  A  +  a ;     return c;   }   void insert(int first) {     int p = 0;     for (int i = len[first] - 1; i >= 0; i--) {       int t = trans(s[first][i]) -  a ;       if (!tr[p][t]) tr[p][t] = ++idx;       p = tr[p][t];     }     id[p] = first;   }   void dfs(int first) {     if (flag) return;     if (first == n) {       for (int i = 0; i < cnt; i++) cout << s[res[i]] <<    ;       puts(  );       flag = true;       return;     }     int p = 0;     for (int i = first; i < n; i++) {       int t = str[i] -  a ;       if (!tr[p][t]) break;       p = tr[p][t];       if (id[p] != -1) {         res[cnt++] = id[p];         dfs(first + len[id[p]]);         cnt--;       }     }   }   int main() {     ios::sync_with_stdio(false);     memset(id, -1, sizeof(id));     cin >> n >> str >> m;     for (int i = 0; i <= m - 1; i++) {       cin >> s[i];       len[i] = strlen(s[i]);       insert(i);     }     dfs(0);     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:200000000 )   using namespace std;   const int cs = 0x186A1;   const int up = 1001;   int n, ans;   int u[1005];   vector<int> p, s;   int main() {     for (int i = 2; i < up; i++)       if (!u[i])         for (int j = i * i; j < up; j += i) u[j] = 1;     for (int i = 2; i < up; i++)       if (!u[i]) p.push_back(i);     cin >> n;     for (auto i : p) {       int cur = i;       while (cur <= n) {         s.push_back(cur);         cur *= i;       }     }     cout << s.size() << endl;     for (auto i : s) cout << i <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     long long n, x, y;     cin >> n >> x >> y;     long long arr[n];     for (long long i = 0; i < n; i++) {       cin >> arr[i];     }     long long an = -1;     for (long long i = 0; i < n; i++) {       bool ans = true;       for (long long j = i - 1; j >= 0 && j >= i - x; j--) {         if (arr[j] < arr[i]) {           ans = false;           break;         }       }       for (long long j = i + 1; j < n && j <= i + y; j++) {         if (arr[j] < arr[i]) {           ans = false;           break;         }       }       if (ans) {         an = i;         break;       }     }     cout << an + 1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int p = 0;     char ch =  0 , var;     pair<int, char> sam[4];     string str[4];     for (int i = 0; i < 4; i++) {       cin >> str[i];       sam[i].first = str[i].size() - 2;       sam[i].second = (char)65 + i;     }     sort(sam, sam + 4);     if (sam[0].first <= sam[1].first / 2) {       var = sam[0].second;       p++;     }     if (sam[3].first >= sam[2].first * 2) {       var = sam[3].second;       p++;     }     if (p == 2 or p == 0)       cout <<  C ;     else       cout << var;   }


#include <bits/stdc++.h>   using namespace std;   int a[100], b[100], path[11];   int res;   bool check(int pos, int l1, int l2) {     if (pos == 0) {       return 1;     }     if (!l1 && !l2) return 1;     int l = l1 ? a[pos] : 0;     int r = l2 ? b[pos] : 9;     for (int i = l; i <= r; i++) {       if (path[i]) {         path[i]--;         if (check(pos - 1, l1 && l == i, l2 && r == i)) {           path[i]++;           return 1;         }         path[i]++;       }     }     return 0;   }   int c;   int solve(int u, int pos) {     int res = 0;     if (u == 9) {       path[u] = pos;       if (check(c, 1, 1)) return 1;       return 0;     }     for (int i = 0; i <= pos; i++) {       path[u] += i;       res += solve(u + 1, pos - i);       path[u] -= i;     }     return res;   }   int dig(int *p, long long x) {     int len = 0;     while (x) {       p[++len] = x % 10;       x /= 10;     }     c = len;     return len;   }   int main() {     long long l, r;     cin >> l >> r;     int pos = dig(a, l);     pos = dig(b, r);     cout << solve(0, pos) <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   vector<int> pri[3005];   long long ans;   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) {       int par, mon;       cin >> par >> mon;       pri[par].push_back(mon);     }     for (int i = 1; i <= m; i++) sort(pri[i].begin(), pri[i].end());     ans = 1e18 + 5;     for (int i = 1; i <= n; i++) {       long long cost = 0;       int need = i - pri[1].size();       vector<int> U;       for (int j = 2; j <= m; j++) {         int staIn = max(0, (int)pri[j].size() - (i - 1));         for (int k = 0; k < staIn; k++) {           cost += pri[j][k];           need--;         }         for (int k = staIn; k < pri[j].size(); k++) {           U.push_back(pri[j][k]);         }       }       sort(U.begin(), U.end());       if (need > 0) {         for (int j = 0; j < need; j++) cost += U[j];       }       ans = min(cost, ans);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool p(string s) {     int n = s.length();     for (int i = 0; i < n / 2; i++) {       if (s[i] != s[n - i - 1]) return 0;     }     return 1;   }   int main() {     std::ios::sync_with_stdio(false);     string s;     cin >> s;     int n = s.length();     for (char c =  a ; c <=  z ; c++) {       string tmp = s;       for (int i = 0; i <= n; i++) {         tmp.insert(tmp.begin() + i, c);         if (p(tmp)) {           cout << tmp << endl;           return 0;         }         tmp = s;       }     }     cout <<  NA  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     void solution();     solution();     return 0;   }   struct item {     int v;     int i;   };   bool operator<(item a, item b) { return a.v < b.v; }   const int maxn = 200000;   item a[maxn];   int c[maxn];   void solution() {     int n;     scanf( %i , &n);     set<int> s;     bool bad = false;     for (int i = 0; i < n; i++) {       scanf( %i , &a[i].v);       a[i].v += i;       a[i].i = i;       if (s.find(a[i].v) != s.end()) {         bad = true;       }       s.insert(a[i].v);     }     if (!bad) {       sort(a, a + n);       for (int i = 0; i < n; i++) {         c[i] = a[i].v - i;         if (c[i] < 0) {           bad = true;         }       }     }     if (!bad) {       for (int i = 0; i < n; i++) {         printf( %i  , c[i]);       }     }     if (bad) {       printf( :( );     }     printf(  n );   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int a, b, c, d, e;     cin >> a >> b >> c >> d >> e;     int f[5] = {a, b, c, d, e}, mn = a + b + c + d + e, ans = a + b + c + d + e;     sort(f, f + 5);     map<int, int> mp;     for (int i = 0; i < 5; i++) {       mp[f[i]]++;       if (mp[f[i]] == 2)         ans = min(ans, mn - f[i] * 2);       else if (mp[f[i]] == 3)         ans = min(ans, mn - f[i] * 3);     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long int fast_exp(long long int base, long long int exp1) {     long long int res = 1;     while (exp1 > 0) {       if (exp1 & 1) res = (res * base) % 1000000007;       base = (base * base) % 1000000007;       exp1 /= 2;     }     return res % 1000000007;   }   long long int pr[100001] = {0};   void isprime() {     pr[0] = 1;     pr[1] = 1;     for (long long int a = 2; a <= 100001; a++) {       if (!pr[a]) {         for (long long int b = a; b < 100001; b += a) {           pr[b] = a;         }       }     }   }   long long int comp = 1, tim = 0;   struct vertex {     vector<long long int> adj;     long long int vis = 0;     long long int parent = -1;     long long int dist = 0;     long long int component = 0;     long long int in = 0;     long long int out = 0;     long long int low = 0;     long long int child = 0;     long long int indeg = 0;     long long int outdeg = 0;     bool AP = false;   };   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     isprime();     long long int n;     cin >> n;     long long int x[n];     long long int dp[100001] = {0};     for (long long int a = 0; a < n; a++) {       long long int b;       cin >> b;       long long int k = b;       while (k > 1) {         long long int p = pr[k];         dp[b] = max(dp[b], dp[p] + 1);         while (k % p == 0) k /= p;       }       k = b;       while (k > 1) {         long long int p = pr[k];         dp[p] = max(dp[p], dp[b]);         while (k % p == 0) k /= p;       }       if (b == 1) dp[1] = 1;     }     cout << *max_element(dp, dp + 100001) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double eps = 1e-12;   const int N = 2067;   int n, t;   long double f[N][N], ft[N][N];   struct Node {     int ss, sl, ts, tl;     long double p;   } node[N];   bool comp(Node c1, Node c2) {     return c1.tl * (1 - c1.p) * c2.p < c2.tl * (1 - c2.p) * c1.p;   }   bool equals(long double a, long double b) { return abs(a - b) < eps; }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n >> t;     for (int i = 1; i <= n; i++) {       cin >> node[i].ss >> node[i].sl >> node[i].ts >> node[i].tl >> node[i].p;       node[i].p = 1 - node[i].p;     }     sort(node + 1, node + n + 1, comp);     for (int i = 1; i <= n; i++) {       for (int j = 0; j <= t; j++) f[i][j] = f[i - 1][j], ft[i][j] = ft[i - 1][j];       for (int j = 0; j <= t - node[i].ts; j++) {         int ct = j + node[i].ts;         if (f[i][ct] < f[i - 1][j] + node[i].ss) {           f[i][ct] = f[i - 1][j] + node[i].ss;           ft[i][ct] = ft[i - 1][j] + node[i].ts;         } else if (equals(f[i][ct], f[i - 1][j] + node[i].ss)) {           ft[i][ct] = min(ft[i][ct], ft[i - 1][j] + node[i].ts);         }       }       for (int j = 0; j <= t - node[i].ts - node[i].tl; j++) {         int ct = j + node[i].ts + node[i].tl;         long double cs = f[i - 1][j] + node[i].ss + node[i].sl * node[i].p;         long double cp =             (ft[i - 1][j] + node[i].ts) * (1 - node[i].p) + ct * node[i].p;         if (f[i][ct] < cs) {           f[i][ct] = cs;           ft[i][ct] = cp;         } else if (equals(f[i][ct], cs)) {           ft[i][ct] = min(ft[i][ct], cp);         }       }     }     long double rs = 0;     long double rp = 0;     for (int i = 0; i <= t; i++) {       if (rs < f[n][i]) {         rs = f[n][i];         rp = ft[n][i];       } else if (equals(rs, f[n][i])) {         rp = min(rp, ft[n][i]);       }     }     cout << setprecision(12) << fixed << rs <<     << rp << endl;   }


#include <bits/stdc++.h>   #pragma GCC optimize(3)   using namespace std;   using ll = long long;   namespace io {   const int SIZE = (1 << 21) + 1;   char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1;   char getc() {     return (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),                         (iS == iT ? EOF : *iS++))                      : *iS++);   }   void flush() {     fwrite(obuf, 1, oS - obuf, stdout);     oS = obuf;   }   void putc(char x) {     *oS++ = x;     if (oS == oT) flush();   }   template <class T>   void read(T &x) {     char ch;     int f = 1;     x = 0;     while (isspace(ch = getc()))       ;     if (ch ==  - ) ch = getc(), f = -1;     do x = x * 10 + (ch -  0 );     while (isdigit(ch = getc()));     x *= f;   }   template <class T, class... A>   void read(T &x, A &...args) {     read(x);     read(args...);   }   template <class T>   void write(T x) {     static char stk[128];     int top = 0;     if (x == 0) {       putc( 0 );       return;     }     if (x < 0) putc( - ), x = -x;     while (x) stk[top++] = x % 10, x /= 10;     while (top) putc(stk[--top] +  0 );   }   template <class T, class... A>   void write(T x, A... args) {     write(x);     putc(   );     write(args...);   }   void space() { putc(   ); }   void endl() { putc(  n ); }   struct _f {     ~_f() { flush(); }   } __f;   }  // namespace io   using io::endl;   using io::flush;   using io::getc;   using io::putc;   using io::read;   using io::space;   using io::write;   const int B = 17;   const int N = 100005;   const ll INF = 0x3f3f3f3f3f3f3f3f;   int phi[N], p[N], pc = 0;   bool np[N];   void sieve(int n) {     phi[1] = 1;     for (int i = 2; i <= n; ++i) {       if (!np[i]) p[++pc] = i, phi[i] = i - 1;       for (int j = 1; j <= pc && i * p[j] <= n; ++j) {         np[i * p[j]] = true;         if (i % p[j] == 0) {           phi[i * p[j]] = phi[i] * p[j];           break;         }         phi[i * p[j]] = phi[i] * (p[j] - 1);       }     }   }   struct node {     ll mn[B], tag;   } t[N * 4];   void up(int x) {     for (int i = 0; i < B; ++i)       t[x].mn[i] = min(t[x << 1].mn[i], t[x << 1 | 1].mn[i]) + t[x].tag;   }   void tag(int x, int val) {     for (int i = 0; i < B; ++i) t[x].mn[i] += val;     t[x].tag += val;   }   void modify(int x, int l, int r, int ql, int qr, int v) {     if (ql <= l && r <= qr) {       tag(x, v);       return;     }     int mid = (l + r) >> 1;     if (ql <= mid) modify(x << 1, l, mid, ql, qr, v);     if (qr > mid) modify(x << 1 | 1, mid + 1, r, ql, qr, v);     up(x);   }   void assign(int x, int l, int r, int p, const ll *v, int tag = 0) {     if (l == r) {       for (int i = 0; i < B; ++i) t[x].mn[i] = v[i] - tag;       return;     }     int mid = (l + r) >> 1;     tag += t[x].tag;     p <= mid ? assign(x << 1, l, mid, p, v, tag)              : assign(x << 1 | 1, mid + 1, r, p, v, tag);     up(x);   }   void build(int x, int l, int r) {     memset(t[x].mn, 0x3f, sizeof(t[x].mn));     if (l == 0) t[x].mn[0] = 0;     t[x].tag = 0;     if (l == r) return;     int mid = (l + r) >> 1;     build(x << 1, l, mid);     build(x << 1 | 1, mid + 1, r);   }   ll dp[N][B];   vector<int> di[N];   void prework(int n) {     sieve(n);     for (int i = 1; i <= n; ++i)       for (int j = i; j <= n; j += i) di[j].push_back(i);     build(1, 0, n);     for (int i = 1; i <= n; ++i) {       for (int d : di[i]) modify(1, 0, n, 0, d - 1, phi[i / d]);       dp[i][0] = INF;       for (int j = 0; j < B - 1; ++j) dp[i][j + 1] = t[1].mn[j];       assign(1, 0, n, i, dp[i]);     }   }   int main() {     int qc;     read(qc);     prework(100000);     while (qc--) {       int n, k;       read(n, k);       if (k >= B)         write(n);       else         write(dp[n][k]);       endl();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool win(long long s, long long e) {     if (e % 2 == 1) {       return s % 2 == 0;     }     if (s > e / 2) {       return s % 2 == 1;     }     if (s > e / 4) return true;     return win(s, e / 4);   }   bool lose(long long s, long long e) {     if (s >= e / 2 + 1) {       return true;     }     return win(s, e / 2);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int t;     cin >> t;     int nw = 0, nl = 0;     vector<pair<long long, long long>> V(t);     for (int i = 0; i < t; ++i) cin >> V[i].first >> V[i].second;     reverse(V.begin(), V.end());     bool w = win(V[0].first, V[0].second);     bool l = lose(V[0].first, V[0].second);     for (int i = 1; i < V.size(); ++i) {       bool thisw = win(V[i].first, V[i].second);       bool thisl = lose(V[i].first, V[i].second);       if (w) {         if (!thisl) w = false;       } else {         if (thisw) w = true;       }       if (l) {         if (!thisl) l = false;       } else {         if (thisw) l = true;       }     }     cout << w <<     << l;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long q1 = 1000000007, q2 = 100003;   long long p1 = 1299827, p2 = 41;   bool v[100009];   long long sp[100009];   void sieve() {     for (long long i = 2; i < 100009; i += 2) sp[i] = 2;     for (long long i = 3; i < 100009; i += 2) {       if (!v[i]) {         sp[i] = i;         for (long long j = i; (j * i) < 100009; j += 2) {           if (!v[j * i]) v[j * i] = true, sp[j * i] = i;         }       }     }   }   set<long long> factorize(long long k) {     set<long long> ans;     while (k > 1) {       ans.insert(sp[k]);       k /= sp[k];     }     return ans;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     vector<long long> used(100009, -1);     for (int i = 0; i < n; i++) {       long long x, y;       cin >> x >> y;       long long ans = 0;       for (long long j = 1; j * j <= x; j++) {         if (x % j == 0) {           if (used[j] == -1 || i - used[j] > y) {             ans++;           }           used[j] = i;           if (x / j != j && used[x / j] == -1 || i - used[x / j] > y) {             ans++;           }           used[x / j] = i;         }       }       if (used[x] == -1 || i - used[x] > y) {         ans++;       }       used[x] = i;       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = (long long)1e9;   long long max(long long a, long long b, long long c) {     return max(a, max(b, c));   }   long long n;   long long house[200011];   long long mn[200011][2][2];   long long mx[200011][2][2];   long long f(long long i, long long j, long long k) {     if (i == -1) return 0;     if (mn[i][j][k] != -1) return mn[i][j][k];     long long res = INF;     if (house[i] == 0)       res = f(i - 1, 0, j);     else if (j + k == 0)       res = 1 + f(i - 1, 1, 0);     else if (j == 1)       res = f(i - 1, 0, 1);     else       res = f(i - 1, 0, 0);     return mn[i][j][k] = res;   }   long long g(long long i, long long j, long long k) {     if (i == -1) return 0;     if (mx[i][j][k] != -1) return mx[i][j][k];     long long res = 0;     if (house[i] == 0)       res = g(i - 1, 0, j);     else if (j + k == 2)       res = 1 + g(i - 1, 1, 1);     else if (j + k == 0) {       if (house[i] == 1)         res = 1 + max(g(i - 1, 1, 0), g(i - 1, 0, 1), g(i - 1, 0, 0));       else if (house[i] == 2)         res = 2 + max(g(i - 1, 1, 1), g(i - 1, 1, 0), g(i - 1, 0, 1));       else         res = 3 + g(i - 1, 1, 1);     } else if (j == 1) {       if (house[i] == 1)         res = 1 + max(g(i - 1, 1, 1), g(i - 1, 0, 1));       else         res = 2 + g(i - 1, 1, 1);     } else {       if (house[i] == 1)         res = 1 + max(g(i - 1, 0, 1), g(i - 1, 1, 0));       else         res = 2 + g(i - 1, 1, 1);     }     return mx[i][j][k] = res;   }   signed main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     memset(mn, -1, sizeof mn);     memset(mx, -1, sizeof mx);     cin >> n;     for (long long i = 0; i < n; i++) {       long long x;       cin >> x;       house[--x]++;     }     cout << f(n - 1, 0, 0) <<     << g(n - 1, 0, 0);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int G[23][23], n, m;   bool check() {     for (int i = 1; i <= n; ++i) {       int cnt = 0;       for (int j = 1; j <= m; ++j) cnt += G[i][j] != j;       if (cnt > 2) return false;     }     return true;   }   int main(int argc, char const *argv[]) {     cin >> n >> m;     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j) scanf( %d , &G[i][j]);     if (check()) {       cout <<  YES  << endl;       return 0;     }     for (int i = 1; i <= m; ++i)       for (int j = i + 1; j <= m; ++j) {         for (int k = 1; k <= n; ++k) swap(G[k][i], G[k][j]);         if (check()) {           cout <<  YES  << endl;           return 0;         }         for (int k = 1; k <= n; ++k) swap(G[k][i], G[k][j]);       }     cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> adj[100005];   map<int, int> mp;   map<int, int> mp1;   vector<int> rt;   int c[100005];   bool vis[100005];   bool bfs(int nd) {     int col = c[nd];     int fg = 0;     queue<int> q;     vis[nd] = true;     q.push(nd);     while (!q.empty()) {       int node = q.front();       q.pop();       for (int i = 0; i < adj[node].size(); i++) {         if (!vis[adj[node][i]]) {           if (c[adj[node][i]] != col) {             fg = 1;           }           vis[adj[node][i]] = true;           q.push(adj[node][i]);         }       }     }     if (fg == 0)       return true;     else       return false;   }   int ua[100005], va[100005];   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n - 1; i++) {       int u, v;       scanf( %d %d , &u, &v);       adj[u].push_back(v);       adj[v].push_back(u);       ua[i] = u;       va[i] = v;     }     for (int i = 1; i <= n; i++) {       scanf( %d , &c[i]);     }     for (int i = 0; i < n - 1; i++) {       if (c[ua[i]] != c[va[i]]) {         memset(vis, false, sizeof(vis));         int fg = 0;         vis[ua[i]] = true;         for (int j = 0; j < adj[ua[i]].size(); j++) {           bool x1 = bfs(adj[ua[i]][j]);           if (x1 == false) {             fg = 1;             break;           }         }         if (fg == 0) {           printf( YES n );           printf( %d n , ua[i]);           return 0;         } else {           memset(vis, false, sizeof(vis));           vis[va[i]] = true;           fg = 0;           for (int j = 0; j < adj[va[i]].size(); j++) {             bool x2 = bfs(adj[va[i]][j]);             if (x2 == false) {               fg = 1;               break;             }           }           if (fg == 0) {             printf( YES n );             printf( %d n , va[i]);             return 0;           } else {             printf( NO n );             return 0;           }         }       }     }     printf( YES n1 n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 1e5 + 10, tsz = 5 * maxn, offset = 1e5;   vector<int> graph[maxn];   int sz[maxn];   bool big[maxn];   int a[maxn], b[maxn];   long long dp[maxn];   struct Line {     int m;     long long c;   } tree[tsz];   int Left[tsz], Right[tsz], idn = 1, track[tsz], idt = 0;   bool exist[tsz];   inline long long f(Line &line, long long x) {     return (long long)line.m * (x - offset) + line.c;   }   void add_line(long long lo, long long hi, Line line, long long node) {     if (!exist[node]) track[idt++] = node;     exist[node] = 1;     if (lo == hi) {       if (f(line, lo) < f(tree[node], lo)) tree[node] = line;       return;     }     long long mid = lo + hi >> 1;     bool l = f(line, lo) < f(tree[node], lo);     bool m = f(line, mid) < f(tree[node], mid);     if (m) swap(tree[node], line);     if (l != m) {       if (!Left[node]) Left[node] = ++idn;       add_line(lo, mid, line, Left[node]);     } else {       if (!Right[node]) Right[node] = ++idn;       add_line(mid + 1, hi, line, Right[node]);     }   }   long long query(long long lo, long long hi, long long p, long long node) {     if (lo == hi) return f(tree[node], p);     long long mid = lo + hi >> 1, ret = f(tree[node], p);     if (p <= mid && Left[node])       ret = min(ret, query(lo, mid, p, Left[node]));     else if (p > mid && Right[node])       ret = min(ret, query(mid + 1, hi, p, Right[node]));     return ret;   }   void szdfs(long long u, long long p) {     sz[u] = 1;     for (long long i = 0; i < graph[u].size(); i++) {       long long nd = graph[u][i];       if (nd == p) continue;       szdfs(nd, u);       sz[u] += sz[nd];     }   }   void add(long long u, long long p) {     for (auto v : graph[u]) {       if (v != p && !big[v]) {         add_line(0, maxn + offset, {b[v], dp[v]}, 1);         add(v, u);       }     }   }   void dfs(long long u, long long p, bool keep) {     long long mx = -1, bigChild = -1;     for (auto v : graph[u])       if (v != p && sz[v] > mx) mx = sz[v], bigChild = v;     for (auto v : graph[u])       if (v != p && v != bigChild) dfs(v, u, 0);     if (bigChild != -1) {       dfs(bigChild, u, 1);       big[bigChild] = 1;     }     add(u, p);     if (graph[u].size() == 1 && u != 1)       dp[u] = 0;     else       dp[u] = query(0, maxn + offset, a[u] + offset, 1);     if (keep) add_line(0, maxn + offset, {b[u], dp[u]}, 1);     if (bigChild != -1) big[bigChild] = 0;     if (keep == 0) {       idn = 1;       while (idt) {         int node = track[--idt];         tree[node] = {0, (1LL << 62)};         exist[node] = Left[node] = Right[node] = 0;       }     }   }   int main() {     for (long long i = 0; i < tsz; ++i) tree[i] = {0, (1LL << 62)};     long long n;     cin >> n;     for (long long i = 1; i <= n; ++i) scanf( %d , &a[i]);     for (long long i = 1; i <= n; ++i) scanf( %d , &b[i]);     for (long long i = 1; i < n; i++) {       long long u, v;       scanf( %lld , &u), scanf( %lld , &v);       graph[u].push_back(v);       graph[v].push_back(u);     }     szdfs(1, -1);     dfs(1, -1, 0);     for (long long i = 1; i <= n; ++i) {       if (i != 1) printf(   );       printf( %lld , dp[i]);     }     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = int64_t;   using ull = uint64_t;   inline void solve() {}   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     string s;     cin >> s;     int lucky = 0;     for (auto const &c : s) lucky += c ==  4  || c ==  7 ;     bool is_lucky = lucky > 0;     while (lucky) {       int x = lucky % 10;       if (!(x == 4 || x == 7)) is_lucky = false;       lucky /= 10;     }     cout << (is_lucky ?  YES  :  NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const int mod = 1e9 + 7;   const double eps = 1e-6;   const int N = 1 << 17;   char S[N], T[N];   int AS[N], AT[N], BS[N], BT[N];   int main() {     cin >> (S + 1) >> (T + 1);     int n = strlen(S + 1), m = strlen(T + 1);     for (int i = 1; i <= n; i++) {       AS[i] = S[i] ==  A  ? AS[i - 1] + 1 : 0;       BS[i] = BS[i - 1] + (S[i] ==  B  || S[i] ==  C );     }     for (int i = 1; i <= m; i++) {       AT[i] = T[i] ==  A  ? AT[i - 1] + 1 : 0;       BT[i] = BT[i - 1] + (T[i] ==  B  || T[i] ==  C );     }     int q;     cin >> q;     while (q--) {       static int a, b, c, d;       cin >> a >> b >> c >> d;       int ss = BS[b] - BS[a - 1];       int tt = BT[d] - BT[c - 1];       if (ss > tt || (ss & 1) ^ (tt & 1))         cout << 0;       else {         int x = min(b - a + 1, AS[b]), y = min(d - c + 1, AT[d]);         if (x < y)           cout << 0;         else if (x > y && (x - y) % 3 && ss + 2 > tt)           cout << 0;         else if (x == y && x == b - a + 1 && y != d - c + 1)           cout << 0;         else           cout << 1;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int N, P, M, T;     cin >> N >> P >> M >> T;     deque<int> dq(N, 1);     for (int x = 0; x < T; x++) {       int typ, pos;       cin >> typ >> pos;       if (typ) {         dq.push_back(1);         if (pos <= P) P++;         cout << dq.size() <<     << P <<   n ;       } else {         int seg1 = pos;         if (P <= seg1) {           int Ind = dq.size() - seg1;           while (Ind > 0) {             dq.pop_back();             Ind--;           }           cout << dq.size() <<     << P <<   n ;         } else {           P -= seg1;           while (seg1 > 0) {             dq.pop_front();             seg1--;           }           cout << dq.size() <<     << P <<   n ;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100100;   int segTree[N * 4], n, x, y, z, l, r, val;   map<int, int> TMp, NMp;   vector<pair<int, int>> vv;   vector<int> v, numm[N];   struct Query {     int type, tim, num, ind;   };   Query q[N];   void update(int i, int j, int pos) {     if (i > r || j < l) return;     if (i == j) {       if (val == 0)         segTree[pos] = 0;       else         segTree[pos] += val;       return;     }     int mid = (i + j) / 2;     update(i, mid, pos * 2);     update(mid + 1, j, pos * 2 + 1);     segTree[pos] = segTree[pos * 2] + segTree[pos * 2 + 1];   }   int get(int i, int j, int pos) {     if (i > r || j < l) {       return 0;     }     if (l <= i && r >= j) {       return segTree[pos];     }     int mid = (i + j) / 2;     return get(i, mid, pos * 2) + get(mid + 1, j, pos * 2 + 1);   }   int main() {     scanf( %d , &n);     int c = 1;     for (int i = 0; i < n; i++) {       scanf( %d%d%d , &x, &y, &z);       if (NMp[z] == 0) {         NMp[z] = c;         c++;       }       numm[NMp[z]].push_back(i);       q[i].type = x;       q[i].tim = y;       q[i].num = NMp[z];       q[i].ind = i;       v.push_back(y);     }     sort(v.begin(), v.end());     int co = 1;     for (int i = 0; i < v.size(); i++) {       if (TMp[v[i]] == 0) {         TMp[v[i]] = co;         co++;       }     }     for (int j = 1; j < c; j++) {       for (int i = 0; i < numm[j].size(); i++) {         int x = numm[j][i];         if (q[x].type == 1) {           l = TMp[q[x].tim];           r = TMp[q[x].tim];           val = 1;           update(1, N, 1);         } else if (q[x].type == 2) {           l = TMp[q[x].tim];           r = TMp[q[x].tim];           val = -1;           update(1, N, 1);         } else if (q[x].type == 3) {           l = 1;           r = TMp[q[x].tim];           int ans = get(1, N, 1);           vv.push_back(make_pair(q[x].ind, ans));         }       }       for (int i = 0; i < numm[j].size(); i++) {         l = TMp[q[numm[j][i]].tim];         r = TMp[q[numm[j][i]].tim];         val = 0;         update(1, N, 1);       }     }     sort(vv.begin(), vv.end());     for (int i = 0; i < vv.size(); i++) {       printf( %d n , vv[i].second);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 10001;   vector<int> g[maxn];   int res;   int color[maxn];   int vis[maxn];   void solve(int root, int c) {     vis[root] = 1;     if (color[root] != c) res++;     for (int i = 0; i < g[root].size(); i++) {       int to = g[root][i];       if (vis[to]) continue;       solve(to, color[root]);     }   }   int main() {     int n;     scanf( %d , &n);     memset(vis, 0, sizeof(vis));     for (int i = 2; i <= n; i++) {       int t;       scanf( %d , &t);       g[i].push_back(t);       g[t].push_back(i);     }     for (int i = 1; i <= n; i++) scanf( %d , &color[i]);     res = 1;     solve(1, color[1]);     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     node *arr[27];     int ind;     node() {       memset(arr, 0, sizeof arr);       ind = 0;     }   };   const int N = 1e5 + 5;   int dp[N][2];   node *root;   int nodes = 0;   void insert(string &s) {     node *cur = root;     for (int i = 0; i < s.size(); i++) {       int x = s[i] -  a ;       if (!cur->arr[x]) cur->arr[x] = new node(), cur->arr[x]->ind = ++nodes;       cur = cur->arr[x];     }   }   bool solve(node *cur, bool f) {     int &ret = dp[cur->ind][f];     if (ret != -1) return ret;     bool flag = 0, flag1 = 0, flag2 = 1;     for (int i = 0; i < 26; i++) {       if (cur->arr[i]) {         flag = 1;         flag1 |= solve(cur->arr[i], !f);         flag2 &= solve(cur->arr[i], !f);       }     }     if (!flag) return ret = !f;     if (f) return ret = flag1;     return ret = flag2;   }   int dp2[N][2];   bool solve2(node *cur, bool f) {     int &ret = dp2[cur->ind][f];     if (ret != -1) return ret;     bool flag = 0, flag1 = 0, flag2 = 1;     for (int i = 0; i < 26; i++) {       if (cur->arr[i]) {         flag = 1;         flag1 |= solve2(cur->arr[i], !f);         flag2 &= solve2(cur->arr[i], !f);       }     }     if (!flag) return ret = !f;     if (f) return ret = flag2;     return ret = flag1;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     memset(dp, -1, sizeof dp);     memset(dp2, -1, sizeof dp2);     root = new node();     int n, k;     cin >> n >> k;     for (int i = 0; i < n; i++) {       string s;       cin >> s;       insert(s);     }     node *cur = root;     if (solve(cur, 1) && !solve2(cur, 1))       cout <<  First ;     else if (solve(cur, 1)) {       if (k % 2 == 1)         cout <<  First ;       else         cout <<  Second ;     } else       cout <<  Second ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int px, py;   int tx, ty;   int decide(int x, int y, int x2, int y2) {     px = tx;     py = ty;     px -= x;     py -= y;     x2 -= x;     y2 -= y;     int dis = x2 * py - y2 * px;     return dis >= 0;   }   int main() {     int n, d;     cin >> n >> d;     int ele;     cin >> ele;     while (ele--) {       int cnt = 0;       cin >> tx >> ty;       cnt += decide(d, 0, n, n - d);       cnt += decide(n, n - d, n - d, n);       cnt += decide(n - d, n, 0, d);       cnt += decide(0, d, d, 0);       if (cnt == 4)         printf( YES n );       else         printf( NO n );     }   }


#include <bits/stdc++.h>   using namespace std;   const int cstMax = 1e6 + 100;   const int cstAlphaSize = 26;   char a[cstMax], b[cstMax];   int c[cstMax][cstAlphaSize];   long long getGcd(long long a, long long b) {     long long r;     while (b != 0) r = a % b, a = b, b = r;     return a;   }   int main() {     long long n, m;     long long alen, blen, gcd, lcm;     long long cycleValue, nCicles;     while (cin >> n >> m) {       cin >> a >> b;       alen = strlen(a);       blen = strlen(b);       gcd = getGcd(alen, blen);       lcm = alen / gcd * blen;       cycleValue = lcm;       nCicles = n * alen / lcm;       memset(c, 0, sizeof c);       for (int i = (0); (i) < int(alen); ++i) ++c[i % gcd][int(a[i] -  a )];       for (int i = (0); (i) < int(blen); ++i)         cycleValue -= c[i % gcd][int(b[i] -  a )];       cout << (cycleValue * nCicles) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     std::vector<long long> x0, x1, y0, y1;     long long a[n][4];     for (long long int i = 0; i < n; i++) {       cin >> a[i][0] >> a[i][1] >> a[i][2] >> a[i][3];       x0.push_back(a[i][0]);       x1.push_back(a[i][2] + 1);       y0.push_back(a[i][1]);       y1.push_back(a[i][3] + 1);     }     sort((x0).begin(), (x0).end());     sort((x1).begin(), (x1).end());     sort((y0).begin(), (y0).end());     sort((y1).begin(), (y1).end());     long long lx = x0[n - 1];     long long ly = y0[n - 1];     long long ux = x1[0];     long long uy = y1[0];     for (long long int i = 0; i < n; i++) {       long long xi = a[i][0];       long long xf = a[i][2] + 1;       long long yi = a[i][1];       long long yf = a[i][3] + 1;       long long x00 = lx;       long long x11 = ux;       long long y00 = ly;       long long y11 = uy;       if (xi == lx) {         x00 = x0[n - 2];       }       if (xf == ux) {         x11 = x1[1];       }       if (yi == ly) {         y00 = y0[n - 2];       }       if (yf == uy) {         y11 = y1[1];       }       if (x00 < x11 && y00 < y11) {         cout << x00 <<     << y00;         return 0;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   char x[1001][1001];   int check(int i, int j) {     if (x[i][j] ==  . ) {       for (int a = -1; a <= 1; a++) {         for (int k = -1; k <= 1; k++) {           if (i + a >= 0 && i + a < n && j + k >= 0 && j + k < m &&               x[i + a][j + k] ==  * ) {             return 1;           }         }       }     } else {       int cnt = 0;       for (int a = -1; a <= 1; a++) {         for (int k = -1; k <= 1; k++) {           if (i + a >= 0 && i + a < n && j + k >= 0 && j + k < m &&               x[i + a][j + k] ==  * ) {             cnt++;           }         }       }       if (cnt != x[i][j] -  0 ) {         return 1;       }     }     return 0;   }   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) {       scanf( %s , x[i]);     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (x[i][j] !=  *  && check(i, j)) {           printf( NO n );           return 0;         }       }     }     printf( YES n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100000 + 100;   int n, m, k, ans;   int num[maxn];   int main() {     ios::sync_with_stdio(false);     while (scanf( %d%d%d , &n, &m, &k) != EOF) {       ans = 0;       for (int i = 0; i < n; ++i) {         scanf( %d , &num[i]);       }       ans = num[n - 1] - num[0] + 1;       for (int i = 0; i < n; ++i) {         num[i] = num[i + 1] - num[i] - 1;       }       sort(num, num + n - 1);       for (int i = 0; i < k - 1; ++i) {         ans -= num[n - 2 - i];       }       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, k, m;   long long t[300005];   long long a[300005];   long long s;   long long res[300005], ans[300005];   long long cnt[300005];   long long pre[300005], p[300005], pp[300005];   struct query {     long long l;     long long r;     long long id;   } q[300005];   bool cmp(query a, query b) {     if (a.l / s != b.l / s) return a.l / s < b.l / s;     return a.r / s < b.r / s;   }   void in() {     scanf( %lld%lld , &n, &k);     s = sqrt(n);     for (long long i = 1; i <= n; i++) {       scanf( %lld , &t[i]);     }     pre[0] = 0;     for (long long i = 1; i <= n; i++) {       scanf( %lld , &a[i]);       if (t[i] == 1) {         pre[i] = pre[i - 1] + a[i];       } else {         pre[i] = pre[i - 1] - a[i];       }     }     set<long long> al;     map<long long, long long> idx;     long long cnt = 0;     for (long long i = 0; i <= n; i++) {       if (al.empty() || al.find(pre[i] - k) == al.end()) {         al.insert(pre[i] - k);         cnt++;         idx[pre[i] - k] = cnt;         p[i] = cnt;       } else {         p[i] = idx[pre[i] - k];       }       if (al.empty() || al.find(pre[i] + k) == al.end()) {         al.insert(pre[i] + k);         cnt++;         idx[pre[i] + k] = cnt;         pp[i] = cnt;       } else {         pp[i] = idx[pre[i] + k];       }       if (al.empty() || al.find(pre[i]) == al.end()) {         al.insert(pre[i]);         cnt++;         idx[pre[i]] = cnt;         pre[i] = cnt;       } else {         pre[i] = idx[pre[i]];       }     }     scanf( %lld , &m);     for (long long i = 1; i <= m; i++) {       scanf( %lld%lld , &q[i].l, &q[i].r);       q[i].id = i;     }   }   void solve() {     sort(q + 1, q + m + 1, cmp);     long long l = 1, r = 0;     long long rf = 0;     for (long long i = 1; i <= m; i++) {       long long cur = 0;       while (l < q[i].l) {         cnt[pre[l]]--;         cur -= cnt[pp[l]];         l++;       }       while (l > q[i].l) {         l--;         cur += cnt[pp[l]];         cnt[pre[l]]++;       }       while (r < q[i].r) {         r++;         cur += cnt[p[r]];         cnt[pre[r]]++;       }       while (r > q[i].r) {         cnt[pre[r]]--;         cur -= cnt[p[r]];         r--;       }       rf += cur;       res[q[i].id] = rf + cnt[pp[l - 1]];     }   }   void ou() {     for (long long i = 1; i <= m; i++) {       printf( %lld n , res[i]);     }   }   signed main() {     in();     solve();     ou();   }


#include <bits/stdc++.h>   using namespace std;   int64_t b, s, c;   int64_t pr[3];   int64_t m;   int64_t req[] = {0, 0, 0};   bool binary_search(int64_t mid) {     int64_t to[3];     for (int64_t i = 0; i < 3; i++) {       to[i] = req[i] * mid;     }     to[0] -= b;     to[1] -= s;     to[2] -= c;     int64_t cost = 0;     for (int64_t i = 0; i < 3; i++)       if (to[i] > 0) cost += to[i] * pr[i];     if (cost <= m) return true;     return false;   }   signed main() {     string ss;     cin >> ss;     for (char i : ss) {       if (i ==  B )         req[0]++;       else if (i ==  S )         req[1]++;       else         req[2]++;     }     cin >> b >> s >> c;     for (int64_t i = 0; i < 3; i++) cin >> pr[i];     cin >> m;     int64_t st = 0;     int64_t en = 1e14;     int64_t ans = 0;     while (st <= en) {       int64_t mid = st + (en - st) / 2;       if (binary_search(mid)) {         ans = mid;         st = mid + 1;       } else {         en = mid - 1;       }     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1.0);   const int mod = 1000000007;   const int inf = 0x3f3f3f3f;   const int maxn = 200005;   void task() {     int r1, c1, r2, c2;     cin >> r1 >> c1 >> r2 >> c2;     int Rook = 0, Bishop = 0, King = 0;     if (r1 == r2 or c1 == c2)       Rook = 1;     else       Rook = 2;     if ((r1 + c1) == (r2 + c2) or (r2 - r1) == (c2 - c1))       Bishop = 1;     else if ((r1 + c1) % 2 == (r2 + c2) % 2)       Bishop = 2;     else       Bishop = 0;     King = max(abs(r1 - r2), abs(c1 - c2));     cout << Rook <<     << Bishop <<     << King <<   n ;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int T = 1;     while (T--) {       task();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int ans = 0;   int m;   void dfs(int parent, int s, int cnt, vector<int> &cats,            vector<vector<int> > &adj) {     if (cats[s] == 1)       cnt++;     else       cnt = 0;     if (cnt > m) return;     if (s != 1 && adj[s].size() == 1) {       ans++;       return;     }     for (int i : adj[s]) {       if (i != parent) dfs(s, i, cnt, cats, adj);     }   }   int main() {     int n;     int x, y;     cin >> n >> m;     vector<int> cats(n + 1);     for (int i = 1; i <= n; i++) cin >> cats[i];     vector<vector<int> > adj(n + 1);     for (int i = 0; i < n - 1; i++) {       cin >> x >> y;       adj[x].push_back(y);       adj[y].push_back(x);     }     dfs(0, 1, 0, cats, adj);     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, a[1000006], ans[1000006];   bool b[1000006];   stack<pair<int, int> > s1, s2;   int main() {     int i, j;     scanf( %d , &n);     for (i = 1; i <= n; i++) scanf( %d , &a[i]);     scanf( %d , &m);     for (i = 1; i <= m; i++) {       scanf( %d , &j);       b[j] = true;     }     for (i = 1; i <= n; i++) {       if (b[i]) {         while (s1.size()) {           if (s1.top().first == a[i] && s2.size() == 0) {             break;           } else if (s2.size() && s1.top().first == s2.top().first) {             ans[s1.top().second] = 1;             ans[s2.top().second] = -1;             s1.pop();             s2.pop();           } else {             s2.push(s1.top());             s1.pop();           }         }         if (s1.size() == 0) return cout <<  NO  << endl, 0;         ans[s1.top().second] = 1;         ans[i] = -1;         s1.pop();       } else {         s1.push({a[i], i});       }     }     while (s1.size()) {       if (s2.size() == 0) {         s2.push(s1.top());         s1.pop();       } else if (s1.top().first == s2.top().first) {         ans[s1.top().second] = 1;         ans[s2.top().second] = -1;         s1.pop();         s2.pop();       } else {         s2.push(s1.top());         s1.pop();       }     }     if (s2.size()) return cout <<  NO  << endl, 0;     printf( YES n );     for (i = 1; i <= n; i++) printf( %d  , a[i] * ans[i]);   }


#include <bits/stdc++.h>   using namespace std;   long long EPS = 1000000007;   const int INF = 1000000000;   double PI = 3.14159265358979323846;   const int MAXN = 1000040;   long long abss(long long h) { return max(h, -h); }   double fabss(double h) { return max(h, -h); }   long long ceill(long long x, long long y) {     if (x % y != 0) return (x / y) + 1;     return x / y;   }   string itos(long long num) {     string str =   ;     if (num == 0) return  0 ;     while (num != 0) {       str += ((num % 10) +  0 );       num /= 10;     }     reverse(str.begin(), str.end());     return str;   }   long long sstoi(string num) {     long long count = 1;     for (int i = 0; i < (int)num.size() - 1; i++) {       count *= 10;     }     long long ans = 0;     for (int i = 0; i < (int)num.size(); i++) {       ans += (num[i] -  0 ) * count;       count /= 10;     }     return ans;   }   long long n, m, k, h;   pair<char, int> a[MAXN];   int b[MAXN];   int c[MAXN];   pair<char, int> t[MAXN];   void build(int v, int tl, int tr) {     if (tl == tr) {       t[v].first = a[tl].first;       t[v].second = a[tl].second;     } else {       int tm = (tl + tr) / 2;       build(v * 2, tl, tm);       build(v * 2 + 1, tm + 1, tr);       if (t[v * 2].first > t[v * 2 + 1].first) {         t[v] = t[v * 2 + 1];       } else if (t[v * 2].first < t[v * 2 + 1].first) {         t[v] = t[v * 2];       } else if (t[v * 2].first == t[v * 2 + 1].first &&                  t[v * 2].second > t[v * 2 + 1].second) {         t[v] = t[v * 2];       } else {         t[v] = t[v * 2 + 1];       }     }   }   pair<char, int> sum(int v, int tl, int tr, int l, int r) {     if (l > r) return make_pair( z , -1);     if (l == tl && r == tr) return t[v];     int tm = (tl + tr) / 2;     pair<char, int> q1 = sum(v * 2, tl, tm, l, min(r, tm));     pair<char, int> q2 = sum(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);     if (q1.first > q2.first) {       return q2;     } else if (q1.first < q2.first) {       return q1;     } else if (q1.first == q2.first && q1.second > q2.second) {       return q1;     } else {       return q2;     }   }   int counter[30];   int main() {     int m;     cin >> m;     string str;     cin >> str;     int n = str.size();     for (int i = 0; i < n; i++) {       a[i].first = str[i];       a[i].second = i;       counter[str[i] -  a ]++;     }     build(1, 0, n - 1);     int index = 0;     vector<char> answer;     char maxChar =  a ;     while (true) {       pair<char, int> w = sum(1, 0, n - 1, index, index + m - 1);       answer.push_back(w.first);       maxChar = max(maxChar, w.first);       index = w.second;       if (index + m - 1 >= n - 1) {         break;       }       index++;     }     sort(answer.begin(), answer.end());     for (char i =  a ; i <=  z ; i++) {       if (i != maxChar) {         for (int j = 0; j < counter[i -  a ]; j++) {           cout << i;         }       } else {         for (int j = 0; j < answer.size(); j++) {           if (answer[j] == maxChar) {             cout << maxChar;           }         }         break;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) {     if (a == 0) return b;     return (b % a, a);   }   int binarysearch(int a[], int ele, int n) {     int beg = 0, end = n, mid, ans;     while (beg <= end) {       mid = beg + (end - beg) / 2;       if (a[mid] > ele) {         ans = mid;         end = mid - 1;       } else         beg = mid + 1;     }     return ans;   }   bool checkpallin(string a) {     int i = 0, j = a.length() - 1;     while (i < j) {       if (a[i] != a[j]) return false;       i++, j--;     }     return true;   }   bool comp(string a, string b) { return a.length() < b.length(); }   int main(void) {     int n, a;     cin >> n >> a;     if (a & 1)       cout << 1 + a / 2;     else       cout << 1 + (n - a) / 2;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 505;   int dis[N][N];   int vis[N], arr[N];   long long ans[N];   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= n; ++j) {         cin >> dis[i][j];       }     }     for (int i = 1; i <= n; ++i) {       cin >> arr[i];     }     for (int i = n; i > 0; --i) {       int x = arr[i];       vis[x] = 1;       for (int j = 1; j <= n; ++j) {         for (int k = 1; k <= n; ++k) {           dis[j][k] = min(dis[j][k], dis[j][x] + dis[x][k]);           if (vis[j] && vis[k]) ans[i] += dis[j][k];         }       }     }     for (int i = 1; i <= n; ++i) cout << ans[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200 * 1000 + 20;   int p[N], q[N], n, ans[N], cnt[N], seg[N << 2];   void build(int id = 1, int st = 0, int en = n) {     if (en - st == 1) {       seg[id] = 1;       return;     }     int mid = st + en >> 1;     build(id << 1, st, mid);     build(id << 1 | 1, mid, en);     seg[id] = seg[id << 1] + seg[id << 1 | 1];   }   void update(int idx, int id = 1, int st = 0, int en = n) {     if (en - st == 1) {       seg[id] = 0;       return;     }     int mid = st + en >> 1;     if (idx < mid)       update(idx, id << 1, st, mid);     else       update(idx, id << 1 | 1, mid, en);     seg[id] = seg[id << 1] + seg[id << 1 | 1];   }   int get(int l, int r, int id = 1, int st = 0, int en = n) {     if (l <= st && en <= r) return seg[id];     if (r <= st || en <= l) return 0;     int mid = st + en >> 1;     return get(l, r, id << 1, st, mid) + get(l, r, id << 1 | 1, mid, en);   }   int get2(int val, int id = 1, int st = 0, int en = n) {     if (en - st == 1) return st;     int mid = st + en >> 1;     if (val < seg[id << 1])       return get2(val, id << 1, st, mid);     else       return get2(val - seg[id << 1], id << 1 | 1, mid, en);   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cin >> n;     for (int i = 0; i < n; i++) cin >> p[i];     for (int i = 0; i < n; i++) cin >> q[i];     build();     for (int i = 0; i < n; i++) {       cnt[n - i - 1] = get(0, p[i]);       update(p[i]);     }     build();     for (int i = 0; i < n; i++) {       cnt[n - i - 1] += get(0, q[i]);       update(q[i]);     }     for (int i = 0; i < n; i++) {       cnt[i + 1] += cnt[i] / (i + 1);       cnt[i] %= i + 1;     }     build();     for (int i = n - 1; ~i; i--) {       ans[n - i - 1] = get2(cnt[i]);       update(ans[n - i - 1]);     }     for (int i = 0; i < n; i++) cout << ans[i] <<    ;     return cout <<   n , 0;   }