#include <bits/stdc++.h>   using namespace std;   struct Dinic {     static const int MXN = (int)2e6;     struct Edge {       int v, f, re;     };     int n, s, t, level[MXN];     vector<Edge> E[MXN];     void init(int _n, int _s, int _t) {       n = _n;       s = _s;       t = _t;     }     void add_edge(int u, int v, int f) {       E[u].push_back({v, f, (int)E[v].size()});       E[v].push_back({u, 0, (int)E[u].size() - 1});     }     bool BFS() {       for (int i = 0; i < n; i++) level[i] = -1;       queue<int> que;       que.push(s);       level[s] = 0;       while (!que.empty()) {         int u = que.front();         que.pop();         for (auto &it : E[u]) {           if (it.f > 0 && level[it.v] == -1) {             level[it.v] = level[u] + 1;             que.push(it.v);           }         }       }       return level[t] != -1;     }     int DFS(int u, int nf) {       if (u == t) return nf;       int res = 0;       for (auto &it : E[u]) {         if (it.f > 0 && level[it.v] == level[u] + 1) {           int tf = DFS(it.v, min(nf, it.f));           res += tf;           nf -= tf;           it.f -= tf;           E[it.v][it.re].f += tf;           if (nf == 0) return res;         }       }       if (!res) level[u] = -1;       return res;     }     int flow(int res = 0) {       while (BFS()) res += DFS(s, 2147483647);       return res;     }   } flow;   struct Node {     int l, r, y, v;     bool operator<(const Node &rhs) const { return y < rhs.y; }   };   int n, q, s, t, tot;   vector<Node> seg;   namespace segtree {   static const int N = (int)1e4 + 5;   static const int inf = (int)1e9;   int id[N * 4], id2[N * 4], tag[N * 4];   void pull(int o, int lson, int rson) {     id[o] = tot++;     if (!tag[lson]) flow.add_edge(id[o], id[lson], inf);     if (!tag[rson]) flow.add_edge(id[o], id[rson], inf);   }   void build1(int l, int r, int o = 0) {     if (l + 1 == r) return void(id[o] = n + l);     int mid = (l + r) >> 1, lson = o * 2 + 1, rson = o * 2 + 2;     build1(l, mid, lson);     build1(mid, r, rson);     pull(o, lson, rson);   }   void build2(int l, int r, int o = 0) {     if (l + 1 == r) return void(id2[o] = l);     int mid = (l + r) >> 1, lson = o * 2 + 1, rson = o * 2 + 2;     id2[o] = tot++;     build2(l, mid, lson);     build2(mid, r, rson);     flow.add_edge(id2[lson], id2[o], inf);     flow.add_edge(id2[rson], id2[o], inf);   }   void build(int l, int r) {     build1(l, r);     build2(l, r);   }   void modify1(int l, int r, int ql, int qr, int v, int o = 0) {     if (ql <= l && r <= qr) return void(tag[o] += v);     int mid = (l + r) >> 1, lson = o * 2 + 1, rson = o * 2 + 2;     if (ql < mid) modify1(l, mid, ql, qr, v, lson);     if (mid < qr) modify1(mid, r, ql, qr, v, rson);     pull(o, lson, rson);   }   void modify2(int l, int r, int ql, int qr, int o = 0) {     if (ql <= l && r <= qr) return void(flow.add_edge(id2[o], id[0], inf));     int mid = (l + r) >> 1, lson = o * 2 + 1, rson = o * 2 + 2;     if (ql < mid) modify2(l, mid, ql, qr, lson);     if (mid < qr) modify2(mid, r, ql, qr, rson);   }   void modify(int l, int r, int ql1, int qr1, int v, int ql2, int qr2) {     if (!tag[0] && ql2 < qr2) modify2(l, r, ql2, qr2);     modify1(l, r, ql1, qr1, v);   }   }  // namespace segtree   void init() {     cin >> n >> q;     for (int i = 0; i < q; ++i) {       int x1, y1, x2, y2;       cin >> x1 >> y1 >> x2 >> y2;       seg.push_back({x1, x2 + 1, y1, 1});       seg.push_back({x1, x2 + 1, y2 + 1, -1});     }     sort(seg.begin(), seg.end());   }   void solve() {     tot = 2 * n + 2, s = 0, t = 2 * n + 1;     for (int i = 1; i <= n; ++i) {       flow.add_edge(s, i, 1);       flow.add_edge(i + n, t, 1);     }     segtree::build(1, n + 1);     int preY = 1;     for (auto [l, r, y, v] : seg) {       segtree::modify(1, n + 1, l, r, v, preY, y);       preY = y;     }     if (preY < n + 1) segtree::modify2(1, n + 1, preY, n + 1);     flow.init(tot, s, t);     cout << flow.flow() <<   n ;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0);     init();     solve();   }


#include <bits/stdc++.h>   using namespace std;   long long int power(long long int x, long long int n) {     if (x == 0) return 0;     if (n == 0) return 1;     if (n == 1) return x;     if (n % 2 == 0) {       long long int temp = power(x, n / 2);       return (temp % 1000000007 * temp % 1000000007) % 1000000007;     } else {       long long int temp = power(x, n / 2);       return (x % 1000000007 *               ((temp % 1000000007 * temp % 1000000007) % 1000000007) %               1000000007) %              1000000007;     }   }   long long int findroot(long long int i, long long int Arr[]) {     while (Arr[i] != i) {       Arr[i] = Arr[Arr[i]];       i = Arr[i];     }     return i;   }   void weightedunion(long long int i, long long int j, long long int arr[],                      long long int size[]) {     long long int x = findroot(i, arr);     long long int y = findroot(j, arr);     if (size[x] > size[y]) {       size[x] += size[y];       arr[y] = arr[x];     } else {       size[y] += size[x];       arr[x] = arr[y];     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int n, k, i, j, temp, ans = 0;     cin >> n >> k;     long long int arr[n];     set<long long int> s;     map<long long int, long long int> mp;     for (i = 0; i < n; i++) {       cin >> arr[i];       mp[arr[i]]++;     }     if (k == 1)       cout << mp.size() <<   n ;     else {       sort(arr, arr + n);       for (i = 0; i < n; i++) {         if (s.find(arr[i]) != s.end())           continue;         else {           s.insert(arr[i]);           vector<long long int> inc;           vector<long long int> ninc;           inc.push_back(mp[arr[i]]);           ninc.push_back(0);           for (temp = k * arr[i]; temp <= (long long int)1e9; temp = temp * k) {             s.insert(temp);             if (mp.find(temp) == mp.end()) {               inc.push_back(max(inc.back(), ninc.back()));               ninc.push_back(inc.back());             } else {               long long int x = inc.back(), y = ninc.back();               inc.push_back(mp[arr[i]] + ninc.back());               ninc.push_back(max(x, y));             }           }           ans += max(inc.back(), ninc.back());         }       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int ara[1000005], n, m, dp[1005][1005];   long long int func(long long int i, long long int j) {     if ((j + ara[i]) % m == 0) {       return 1;     }     if (i >= n - 1) {       if ((j + ara[i]) % m == 0) {         return 1;       } else         return 0;     }     if (dp[i][j] != -1) {       return dp[i][j];     }     long long int p1 = 0, p2 = 0;     p1 = func(i + 1, (j + ara[i]) % m);     p2 = func(i + 1, j);     dp[i][j] = p1 | p2;     return dp[i][j];   }   int main() {     long long int i, k, sum = 0;     scanf( %I64d %I64d , &n, &m);     for (i = 0; i < n; i++) {       scanf( %I64d , &ara[i]);     }     if (n > m) {       printf( YES n );       return 0;     }     memset(dp, -1, sizeof dp);     k = func(0, 0);     if (k == 1) {       printf( YES n );     } else {       printf( NO n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void fast() {     ios_base::sync_with_stdio(false);     cin.tie(0);     std::cout.tie(0);   }   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }   void solve() {     long long n;     cin >> n;     vector<long long> a(n);     vector<long long> cnt(n + 1);     for (long long(i) = 0; (i) < (n); (i)++) {       cin >> a[i];       cnt[a[i]]++;     }     vector<long long> ans(n + 1);     vector<long long> add;     long long i = 0;     for (; i < n + 1; i++) {       ans[i] = cnt[i];       if (cnt[i] == 0) break;       for (long long j = 0; j < cnt[i] - 1; j++) add.push_back(i);     }     i++;     for (; i < n + 1; i++) {       if (ans[i - 1] == -1) {         ans[i] = -1;         continue;       }       ans[i] = ans[i - 1] + cnt[i] - cnt[i - 1];       if (cnt[i - 1] == 0) {         if (add.empty()) {           ans[i] = -1;           continue;         }         ans[i] += (i - add.back() - 1);         add.pop_back();       }       for (long long j = 0; j < cnt[i] - 1; j++) add.push_back(i);     }     for (auto x : ans) cout << x <<    ;     cout <<   n ;   }   signed main() {     fast();     long long t = 1;     cin >> t;     for (long long(i) = 0; (i) < (t); (i)++) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1100;   int a[MAXN];   int main() {     int n, k;     while (scanf( %d%d , &n, &k) != EOF) {       for (int i = 0; i < n; i++) scanf( %d , &a[i]);       int res = 0;       int sum = (1 << 29);       for (int i = 0; i < n; i++) {         int tmp = 0, flag = true;         for (int j = 0; j < n; j++)           if (a[j] != a[i] + (j - i) * k) {             tmp++;             if (a[i] + (j - i) * k <= 0) flag = false;           }         if (flag && tmp < sum) sum = tmp, res = i;       }       printf( %d n , sum);       if (sum) {         for (int i = 0; i < n; i++) {           int tmp = a[res] + (i - res) * k;           if (a[i] < tmp)             printf( + %d %d n , i + 1, tmp - a[i]);           else if (a[i] > tmp)             printf( - %d %d n , i + 1, a[i] - tmp);         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long M = 1000000000000000003LL;   int n, m;   long long pow2[262144];   char S[262144];   char T[262144];   long long hS[262144][26];   long long hT[262144][26];   long long curh[26];   vector<int> ans;   bool v[26];   inline long long mulmod(long long x, long long y, long long mod) {     return (x * y) - mod * (long long)((long double)x * y / mod);   }   bool check(long long a[26], long long b[26]) {     memset(v, false, sizeof v);     for (int i = 0; i < 26; ++i) {       if (a[i] == 0) continue;       if (v[i]) continue;       if (a[i] == b[i]) {         v[i] = true;         continue;       }       for (int j = 0; j < 26; ++j)         if (a[i] == b[j]) {           if (v[j]) return false;           if (b[i] == a[j]) {             v[j] = true;             v[i] = true;             break;           } else             return false;         }       if (!v[i]) return false;     }     return true;   }   int main() {     pow2[0] = 1;     for (int i = 1; i < 262144; ++i) pow2[i] = pow2[i - 1] * 2ll % M;     scanf( %d%d , &n, &m);     scanf( %s , S + 1);     scanf( %s , T + 1);     for (int i = 1; i <= m; ++i) {       for (int j = 0; j < 26; ++j) hT[i][j] = hT[i - 1][j] * 2ll % M;       (hT[i][T[i] - 97] += 1ll) %= M;     }     for (int i = 1; i <= n; ++i) {       for (int j = 0; j < 26; ++j) hS[i][j] = hS[i - 1][j] * 2ll % M;       (hS[i][S[i] - 97] += 1ll) %= M;     }     for (int i = m; i <= n; ++i) {       for (int j = 0; j < 26; ++j) {         curh[j] = (hS[i][j] - mulmod(hS[i - m][j], pow2[m], M)) % M;         if (curh[j] < 0) curh[j] += M;       }       if (check(hT[m], curh)) ans.push_back(i - m + 1);     }     int sz = ans.size();     printf( %d n , sz);     for (int i = 0; i < sz; ++i) {       printf( %d , ans[i]);       if (i + 1 < sz)         printf(   );       else         printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = 1e15;   const long double pi = 3.141592;   const long long mod1 = 1e9 + 7;   int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }   int main() {     int n;     cin >> n;     vector<int> d(n + 1);     set<int> set;     int x = 1, y = 1;     int m = 1;     for (int i = 1; i <= n; i++) {       cin >> d[i];       m = max(m, d[i]);     }     for (int i = 1; i <= n; i++) {       if (m % d[i] == 0 && set.find(d[i]) == set.end()) {         set.insert(d[i]);         d[i] = -1;       }     }     for (int i = 1; i <= n; i++) {       y = max(y, d[i]);     }     cout << m <<     << y;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = ~(1 << 31);   int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); }   int main() {     int n;     cin >> n;     vector<int> v(n);     for (int i = 0; i < n; i++) cin >> v[i];     int m = 0, l = 0;     for (int i = 1; i < n; i++) {       if (v[i] < v[m] && m != 0) {         cout << 3 << endl;         cout <<  1   << m + 1 <<     << i + 1 << endl;         return 0;       }       if (v[i] > v[l] && l != 0) {         cout << 3 << endl;         cout <<  1   << l + 1 <<     << i + 1 << endl;         return 0;       }       if (v[i] > v[m]) {         m = i;       }       if (v[i] < v[l]) {         l = i;       }     }     cout << 0 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1000000000 + 7;   const int MAX = 100000;   map<pair<string, int>, int> F;   vector<pair<string, int>> v, ans;   pair<string, int> q;   set<string> s;   int n;   vector<int> g[300000];   vector<int> S[300000];   bool used[300000];   bool cmp(int i1, int i2) { return v[i1].second > v[i2].second; }   bool qw(int i1, int i2) { return i1 > i2; }   int main() {     cin >> n;     for (int i = (0); i < (n); i++) {       cin >> q.first >> q.second;       if (!F.count(q)) F[q] = v.size(), v.push_back(q);       int V = F[q];       int k;       cin >> k;       for (int j = (0); j < (k); j++) {         cin >> q.first >> q.second;         if (!F.count(q)) F[q] = v.size(), v.push_back(q);         g[V].push_back(F[q]);       }     }     S[0].push_back(0);     used[0] = 1;     s.insert(v[0].first);     for (int i = (0); i < (n); i++) {       sort(S[i].begin(), S[i].end(), cmp);       for (int j = (0); j < (S[i].size()); j++) {         if (i && !s.count(v[S[i][j]].first)) {           s.insert(v[S[i][j]].first);           ans.push_back(v[S[i][j]]);         } else if (i)           continue;         for (int k = (0); k < (g[S[i][j]].size()); k++)           if (!used[g[S[i][j]][k]])             S[i + 1].push_back(g[S[i][j]][k]), used[g[S[i][j]][k]] = 1;       }     }     sort(ans.begin(), ans.end());     cout << ans.size() <<   n ;     for (int i = (0); i < (ans.size()); i++)       cout << ans[i].first <<     << ans[i].second << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x, y, m{99999};     cin >> n;     int* A = new int[n];     for (int i{0}; i < n; i++) cin >> A[i];     for (int i{0}; i < n; i++) {       int s{0};       for (int j{0}; j < A[i]; j++) {         cin >> x;         s += x;       }       y = (15 * A[i]) + s * 5;       if (y < m) m = y;     }     cout << m;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1003, mod = 998244353;   template <typename T>   void read(T &x) {     int ch = getchar();     x = 0;     bool f = false;     for (; ch <  0  || ch >  9 ; ch = getchar()) f |= ch ==  - ;     for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ;     if (f) x = -x;   }   int n, fa[N];   bool vis[N];   vector<int> in[N], nxt[N], pos;   char str[N];   map<vector<int>, vector<bool> > buc;   vector<bool> query(const vector<int> &v) {     if (buc.count(v)) return buc[v];     putchar( ? );     for (int u : v) printf(  %d , u);     putchar(  n );     fflush(stdout);     scanf( %s , str);     vector<bool> res(n);     for (int i = 0; i < n; ++i) res[i] = (str[i] !=  0 );     return buc[v] = res;   }   int main() {     read(n);     in[0].emplace_back(0);     vis[0] = vis[n] = true;     for (int i = 1; i < n; ++i) nxt[0].emplace_back(i);     while (true) {       bool flg = true;       pos.resize(0);       for (int i = 0; i <= n; ++i)         if (vis[i]) pos.emplace_back(i);       for (int _ = 0; _ < 2; ++_) {         vector<int> d1(n, 0), d2(n, 0);         for (int i = _; i + 1 < pos.size(); i += 2) {           if (in[pos[i]].empty() || pos[i + 1] == pos[i] + 1) continue;           if (pos[i + 1] == pos[i] + 2) {             in[pos[i] + 1] = nxt[pos[i]];             nxt[pos[i]].resize(0);             vis[pos[i] + 1] = true;             continue;           }           flg = false;           int mid = pos[i] + pos[i + 1] + 1 >> 1;           for (int u : in[pos[i]]) {             d2[u] = mid - pos[i];             d1[u] = d2[u] - 1;           }         }         vector<bool> a1 = query(d1), a2 = query(d2);         for (int i = _; i + 1 < pos.size(); i += 2) {           if (in[pos[i]].empty() || pos[i + 1] <= pos[i] + 2) continue;           int mid = pos[i] + pos[i + 1] + 1 >> 1;           vector<int> tmp = nxt[pos[i]];           nxt[pos[i]].resize(0);           for (int x : tmp) {             if (a1[x])               nxt[pos[i]].emplace_back(x);             else if (a2[x])               in[mid].emplace_back(x);             else               nxt[mid].emplace_back(x);           }           vis[mid] = true;         }       }       if (flg) break;     }     for (int _ = 1; _ <= 3; ++_)       for (int i = 0; (1 << i) < n; ++i) {         vector<int> d(n, 0);         bool flg = true;         for (int x = _; x < n; x += 3)           if (!in[x].empty())             for (int v : in[x - 1])               if (v >> i & 1) {                 d[v] = 1;                 flg = false;               }         if (flg) continue;         vector<bool> res = query(d);         for (int x = _; x < n; x += 3)           if (!in[x].empty())             for (int v : in[x])               if (res[v]) fa[v] |= 1 << i;       }     puts( ! );     for (int i = 1; i < n; ++i) printf( %d %d n , fa[i] + 1, i + 1);   }


#include <bits/stdc++.h>   using namespace std;   namespace fastIO {   bool IOerror = 0;   inline bool blank(char ch) {     return ch ==     || ch ==   n  || ch ==   r  || ch ==   t ;   }   inline long long read() {     bool sign = 0;     char ch = getchar();     long long x = 0;     for (; blank(ch); ch = getchar())       ;     if (IOerror) return 0;     if (ch ==  - ) sign = 1, ch = getchar();     for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ;     if (sign) x = -x;     return x;   }   };  // namespace fastIO   using namespace fastIO;   struct node {     long long l, r, len;     node(long long x, long long y) { l = x, r = y, len = y - x + 1; }     bool operator<(const node& bb) const {       return len != bb.len ? len > bb.len : l < bb.l;     }   };   set<node> s;   long long now = 0;   long long l[1000010], r[1000010], len[1000010], num[1000010], ans[2010][2010];   signed main() {     long long n = read(), maxx = 0;     s.insert(node(1, n + 1));     for (long long i = 1; i <= n; i++) {       long long x = read();       now = 0;       for (auto i : s) {         l[++now] = i.l, r[now] = i.r, len[now] = i.len;         num[now] = 0;       }       for (long long j = 1; j <= now; j++) {         num[j] = 1;         ans[l[j]][i] = 1;         maxx = max(maxx, l[j]);         x--;         if (!x) break;       }       if (x) {         for (long long j = 1; j <= now; j++) {           while (num[j] < len[j] - 1 && x) {             ans[l[j] + num[j]][i] = 1;             maxx = max(maxx, l[j] + num[j]);             num[j]++;             x--;           }         }       }       if (x) {         for (long long j = 1; j <= now; j++) {           if (x && num[j] != len[j])             ans[r[j]][i] = 1, x--, num[j]++, maxx = max(maxx, r[j]);         }       }       s.clear();       for (long long j = 1; j <= now; j++) {         if (num[j]) s.insert(node(l[j], l[j] + num[j] - 1));         if (num[j] != len[j]) s.insert(node(l[j] + num[j], r[j]));         if (len[j] == 1) s.insert(node(l[j], r[j]));       }     }     cout << maxx <<   n ;     for (long long i = 1; i <= maxx; i++) {       for (long long j = 1; j <= n; j++) cout << ans[i][j];       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   namespace mincostflow {   struct edge {     int to, cap, cost, rev;     edge(int to = 0, int cap = 0, int cost = 0, int rev = 0)         : to(to), cap(cap), cost(cost), rev(rev) {}   };   vector<edge> g[7000];   int dist[7000], q[7000 * 2];   int frome[7000], fromv[7000];   bool inq[7000];   int s, t;   void add_edge(int from, int to, int cap, int cost) {     g[from].push_back(edge(to, cap, cost, g[to].size()));     g[to].push_back(edge(from, 0, -cost, g[from].size() - 1));   }   bool dij() {     for (int i = 0; i <= t; i++) dist[i] = 1000000000;     dist[s] = 0;     q[0] = s;     inq[s] = 1;     int head = 0, tail = 1;     while (head != tail) {       int now = q[head++];       if (head == 7000 + 1) head = 0;       for (int i = 0; i < g[now].size(); i++) {         edge &e = g[now][i];         if (e.cap && dist[e.to] > dist[now] + e.cost) {           dist[e.to] = dist[now] + e.cost;           frome[e.to] = i;           fromv[e.to] = now;           if (!inq[e.to]) {             if (dist[e.to] < dist[q[head]]) {               head--;               if (head == -1) head = 7000;               q[head] = e.to;             } else {               q[tail++] = e.to;               if (tail == 7000 + 1) tail = 0;             }           }         }       }       inq[now] = 0;     }     return dist[t] != 1000000000;   }   int augment() {     int c = 1000000000, ret = 0;     for (int i = t; i != s; i = fromv[i]) c = min(c, g[fromv[i]][frome[i]].cap);     for (int i = t; i != s; i = fromv[i]) {       edge &e = g[fromv[i]][frome[i]];       ret += c * e.cost;       e.cap -= c;       g[e.to][e.rev].cap += c;     }     return ret;   }   int mcf() {     int ans = 0;     while (dij()) ans += augment();     return ans;   }   };  // namespace mincostflow   int a[100][100];   int id[100][100];   int n, m, cnt;   int main() {     using namespace mincostflow;     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         scanf( %d , &a[i][j]);         id[i][j] = ++cnt;       }     }     s = ++cnt;     t = ++cnt;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if ((i + j) & 1)           add_edge(s, id[i][j], 1, 0);         else           add_edge(id[i][j], t, 1, 0);       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if ((i + j) % 2 == 0) continue;         if (id[i][j - 1])           add_edge(id[i][j], id[i][j - 1], 1, (a[i][j] != a[i][j - 1]));         if (id[i][j + 1])           add_edge(id[i][j], id[i][j + 1], 1, (a[i][j] != a[i][j + 1]));         if (id[i - 1][j])           add_edge(id[i][j], id[i - 1][j], 1, (a[i][j] != a[i - 1][j]));         if (id[i + 1][j])           add_edge(id[i][j], id[i + 1][j], 1, (a[i][j] != a[i + 1][j]));       }     }     printf( %d n , mcf());     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<int, int> lc, rc, pr;   set<int> nodes;   set<int>::iterator it;   vector<int> v;   int main() {     int n;     cin >> n;     int root;     cin >> root;     nodes.insert(root);     for (int i = 1; i < n; ++i) {       int x;       cin >> x;       v.push_back(x);       it = nodes.upper_bound(x);       if (it == nodes.end()) {         it--;         rc[*it] = x;         pr[x] = (*it);       } else if (it == nodes.begin()) {         lc[*it] = x;         pr[x] = (*it);       } else {         int ul = (*it);         it--;         int ll = (*it);         if (lc[ul])           rc[ll] = x, pr[x] = ll;         else           lc[ul] = x, pr[x] = ul;       }       nodes.insert(x);     }     for (int i = 0; i < v.size(); ++i) {       cout << pr[v[i]] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int R[300010 * 2], L[300010 * 2], n, m, sum[300010], sz;   struct Fun {     int k, b;   } f[300010], id[300010 * 20], s[300010];   inline int Read() {     char c = getchar();     int num = 0, f = 1;     while ( 0  > c || c >  9 ) {       if (c ==  - ) f = -1;       c = getchar();     }     while ( 0  <= c && c <=  9 ) num = num * 10 + c -  0 , c = getchar();     return (num * f);   }   inline bool Pd(Fun c, Fun a, Fun b) {     return ((long long)(c.b - a.b) * (b.k - c.k) <=             (long long)(c.b - b.b) * (a.k - c.k));   }   inline void Ins(int rt, Fun x) {     id[++sz] = x;     if (!R[rt]) L[rt] = sz;     R[rt] = sz;     while (R[rt] - L[rt] >= 2 && Pd(id[R[rt] - 2], id[R[rt] - 1], id[R[rt]])) {       id[R[rt] - 1] = id[R[rt]];       id[R[rt]] = (Fun){0, 0};       sz--;       R[rt]--;     }   }   inline void Merge(int rt, int l, int mid, int r) {     int i = l, j = mid + 1, k = l - 1;     while (i <= mid && j <= r) {       if ((f[i].k < f[j].k) || (f[i].k == f[j].k && f[i].b < f[j].b))         s[++k] = f[i], i++;       else         s[++k] = f[j], j++;     }     for (; j <= r; j++) s[++k] = f[j];     for (; i <= mid; i++) s[++k] = f[i];     f[l] = s[l];     Ins(rt, f[l]);     for (int t = l + 1; t <= r; t++) {       f[t] = s[t];       if (f[t].k != f[t - 1].k) Ins(rt, f[t]);     }   }   inline void Build(int rt, int l, int r) {     int mid = (l + r) / 2;     if (l == r) {       Ins(rt, f[l]);       return;     }     Build(rt * 2, l, mid);     Build(rt * 2 + 1, mid + 1, r);     Merge(rt, l, mid, r);   }   inline int Cal(int l, int r, int x) {     while (r - l > 1) {       int mid = (l + r) / 2;       if (id[mid].k * x + id[mid].b < id[mid + 1].k * x + id[mid + 1].b) {         r = mid;         continue;       }       if (id[mid].k * x + id[mid].b > id[mid + 1].k * x + id[mid + 1].b) {         l = mid;         continue;       }       if (id[mid].k * x + id[mid].b < id[mid - 1].k * x + id[mid - 1].b) {         l = mid;         continue;       }       if (id[mid].k * x + id[mid].b > id[mid - 1].k * x + id[mid - 1].b) {         r = mid;         continue;       }     }     return (min(id[l].k * x + id[l].b, id[r].k * x + id[r].b));   }   inline int Find(int rt, int l, int r, int a, int b, int p) {     int mid = (l + r) / 2;     if (l == a && r == b) {       if (l <= 115 && 115 <= r) int deb = 0;       return (Cal(L[rt], R[rt], p));     }     if (b <= mid) return (Find(rt * 2, l, mid, a, b, p));     if (mid < a) return (Find(rt * 2 + 1, mid + 1, r, a, b, p));     if (a <= mid && mid < b) {       int L = Find(rt * 2, l, mid, a, mid, p);       int R = Find(rt * 2 + 1, mid + 1, r, mid + 1, b, p);       return (min(L, R));     }   }   int main() {     n = Read();     for (int i = 1; i <= n; i++) f[i].k = Read();     for (int i = 1; i <= n; i++)       sum[i] = sum[i - 1] + f[i].k, f[i].b = f[i].k * i - sum[i];     Build(1, 1, n);     m = Read();     for (int i = 1; i <= m; i++) {       int x = Read(), y = Read();       int Min = Find(1, 1, n, y - x + 1, y, x - y);       printf( %d n , min(sum[y] - sum[y - x], sum[y] + Min));     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     int n;     cin >> n;     queue<int> r, d;     char c;     for (int i = 0; i < n; i++) {       cin >> c;       if (c ==  D )         d.push(i);       else         r.push(i);     }     while (r.size() != 0 && d.size() != 0) {       if (r.front() < d.front()) {         d.pop();         r.push(r.front() + n);         r.pop();       } else {         r.pop();         d.push(d.front() + n);         d.pop();       }     }     if (r.size() == 0)       cout <<  D n ;     else       cout <<  R n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   bool checkmax(T &a, T b) {     return a < b ? a = b, true : false;   }   template <class T>   bool checkmin(T &a, T b) {     return a > b ? a = b, true : false;   }   template <class T>   void string_reader(string s, vector<T> &vec) {     istringstream sin(s);     copy(istream_iterator<T>(sin), istream_iterator<T>(), back_inserter(vec));   }   const int N = 200;   const int K = 7;   const int INF = N * N * 10000;   const int dx[] = {0, 0, 1, -1};   const int dy[] = {1, -1, 0, 0};   int matrix[N][N];   int dp[N][N][1 << K], inq[N][N][1 << K];   tuple<int, int, int> pre[N][N][1 << K];   tuple<int, int> nodes[K];   int n, m, k;   bool is_legal(int x, int y) { return 0 <= x && x < n && 0 <= y && y < m; }   char fig[N][N];   void dfs(int x, int y, int s) {     fig[x][y] =  X ;     int tmpx = get<0>(pre[x][y][s]), tmpy = get<1>(pre[x][y][s]),         tmps = get<2>(pre[x][y][s]);     if (tmpx == -1)       return;     else if (x == tmpx && y == tmpy) {       dfs(x, y, s ^ tmps);       dfs(x, y, tmps);     } else       dfs(tmpx, tmpy, tmps);   }   void DP() {     int als = 1 << k;     for (int i = 0; i < int(n); ++i)       for (int j = 0; j < int(m); ++j) {         fill(dp[i][j], dp[i][j] + als, INF);         memset(pre[i][j], -1, sizeof(pre[i][j]));         for (int kk = 0; kk < int(als); ++kk) {           get<0>(pre[i][j][kk]) = -1;           get<1>(pre[i][j][kk]) = -1;           get<2>(pre[i][j][kk]) = -1;         }       }     memset(inq, false, sizeof(inq));     queue<tuple<int, int, int> > q;     for (int i = 0; i < int(k); ++i) {       int x = get<0>(nodes[i]), y = get<1>(nodes[i]);       dp[x][y][1 << i] = matrix[x][y];       inq[x][y][1 << i] = true;       q.push(tuple<int, int, int>(x, y, 1 << i));     }     while (q.empty() == false) {       auto now = q.front();       q.pop();       int x = get<0>(now), y = get<1>(now), s = get<2>(now);       for (int kk = 0; kk < int(4); ++kk) {         int tmpx = x + dx[kk];         int tmpy = y + dy[kk];         if (is_legal(tmpx, tmpy) == false) continue;         if (checkmin(dp[tmpx][tmpy][s], dp[x][y][s] + matrix[tmpx][tmpy])) {           get<0>(pre[tmpx][tmpy][s]) = x;           get<1>(pre[tmpx][tmpy][s]) = y;           get<2>(pre[tmpx][tmpy][s]) = s;           if (!inq[tmpx][tmpy][s]) {             q.push(tuple<int, int, int>(tmpx, tmpy, s));             inq[tmpx][tmpy][s] = 1;           }         }       }       for (int kk = 0; kk < int(4); ++kk) {         int nxts = (als - 1) ^ s;         for (int i = nxts; i != 0; i = (i - 1) & nxts) {           if (checkmin(dp[x][y][i | s],                        dp[x][y][i] + dp[x][y][s] - matrix[x][y])) {             get<0>(pre[x][y][s | i]) = x;             get<1>(pre[x][y][s | i]) = y;             get<2>(pre[x][y][s | i]) = s;             if (!inq[x][y][i | s]) {               q.push(tuple<int, int, int>(x, y, i | s));               inq[x][y][i | s] = 1;             }           }         }       }       inq[x][y][s] = false;     }     int ans = INF;     pair<int, int> p;     for (int i = 0; i < int(n); ++i)       for (int j = 0; j < int(m); ++j)         if (checkmin(ans, dp[i][j][als - 1])) p = make_pair(i, j);     cout << ans << endl;     dfs(p.first, p.second, als - 1);     for (int i = 0; i < int(n); ++i)       for (int j = 0; j < int(m); ++j) {         cout << fig[i][j];         if (j == m - 1) cout << endl;       }   }   int main() {     while (cin >> n >> m >> k) {       for (int i = 0; i < int(n); ++i)         for (int j = 0; j < int(m); ++j) cin >> matrix[i][j];       memset(fig,  . , sizeof(fig));       for (int i = 0; i < int(k); ++i) {         cin >> get<0>(nodes[i]) >> get<1>(nodes[i]);         get<0>(nodes[i])--;         get<1>(nodes[i])--;       }       DP();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int Get() {     int x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while ( 0  <= ch && ch <=  9 ) {       x = (x << 1) + (x << 3) + ch -  0 ;       ch = getchar();     }     return x * f;   }   const long long mod = 1e9 + 7;   const long long Mod = 998244353;   int n, m;   long long f[2005][15];   struct road {     int to, next;   } s[2005 << 1];   int h[2005], cnt;   void add(int i, int j) {     s[++cnt] = (road){j, h[i]};     h[i] = cnt;   }   vector<int> e[2005];   long long fac[2005], ifac[2005];   long long ksm(long long t, long long x) {     long long ans = 1;     for (; x; x >>= 1, t = t * t % mod)       if (x & 1) ans = ans * t % mod;     return ans;   }   long long Hash[2005];   long long Hash2[2005];   const long long p = 666233, p2 = 134341;   unsigned long long p3 = 233333;   int size[2005];   bool cmp(int a, int b) {     if (size[a] != size[b]) return size[a] < size[b];     if (Hash[a] != Hash[b]) return Hash[a] < Hash[b];     return Hash2[a] < Hash2[b];   }   long long t[2005];   vector<int> sn[2005];   int dep[2005], mxdep[2005];   void dfs1(int v, int fr) {     size[v] = 1;     mxdep[v] = dep[v];     for (int i = 0; i < e[v].size(); i++) {       int to = e[v][i];       if (to == fr) continue;       sn[v].push_back(to);       dep[to] = dep[v] + 1;       dfs1(to, v);       mxdep[v] = max(mxdep[v], mxdep[to]);       size[v] += size[to];     }     sort(sn[v].begin(), sn[v].end(), cmp);     t[v] = 1;     for (int i = 0; i < sn[v].size(); i++) {       int j = i;       while (j + 1 < sn[v].size() && size[sn[v][i]] == size[sn[v][j + 1]] &&              Hash[sn[v][i]] == Hash[sn[v][j + 1]] &&              Hash2[sn[v][j]] == Hash2[sn[v][j + 1]])         j++;       t[v] = t[v] * ifac[j - i + 1] % mod;       i = j;     }     Hash2[v] = Hash[v] =  ( ;     long long now = 1, now2 = 1;     for (int i = 0; i < sn[v].size(); i++) {       Hash[v] = ((Hash[v] * now % mod) ^ Hash[sn[v][i]]) % mod;       Hash2[v] = ((Hash2[v] * now2 % Mod) ^ Hash2[sn[v][i]]) % Mod;       now = now * p % mod;       now2 = now2 * p2 % Mod;     }     Hash[v] = (Hash[v] * p +  ) ) % mod;     Hash2[v] = (Hash2[v] * p +  ) ) % Mod;   }   int st[2005], top;   int cal(int v, int u) {     if (!top) return 0;     static long long ans[1 << 13], g[1 << 13];     int num = sn[u].size();     for (int i = 0; i < 1 << num; i++) ans[i] = 0;     ans[0] = 1;     for (int i = 1; i <= top; i++) {       memcpy(g, ans, sizeof(g));       int now = st[i];       for (int j = 0; j < num; j++) {         if (!f[now][sn[u][j]]) continue;         int t = 1 << j;         for (int sta = 0; sta < 1 << num; sta++) {           if (!(sta & t))             g[sta | t] = (g[sta | t] + ans[sta] * f[now][sn[u][j]]) % mod;         }       }       memcpy(ans, g, sizeof(g));     }     return ans[(1 << num) - 1] * t[u] % mod;   }   long long ans;   int rt;   void solve(int v, int fr) {     for (int i = 1; i <= m; i++)       if (sn[i].size() == 0) f[v][i] = 1;     for (int i = h[v]; i; i = s[i].next) {       int to = s[i].to;       if (to == fr) continue;       solve(to, v);     }     top = 0;     for (int i = h[v]; i; i = s[i].next) {       if (s[i].to == fr) continue;       st[++top] = s[i].to;     }     for (int i = 1; i <= m; i++) {       if (sn[i].size()) {         f[v][i] = cal(v, i);       }     }     (ans += f[v][rt]) %= mod;   }   map<pair<int, int>, int> vis;   int work() {     for (int i = 1; i <= m; i++) sn[i].clear();     for (int i = 1; i <= m; i++) Hash[i] = Hash2[i] = 0;     dep[rt] = 1;     dfs1(rt, 0);     if (vis.find(make_pair(Hash[rt], Hash2[rt])) != vis.end()) return 0;     vis[make_pair(Hash[rt], Hash2[rt])] = 1;     memset(f, 0, sizeof(f));     ans = 0;     solve(1, 0);     return ans;   }   int main() {     n = Get();     int a, b;     for (int i = 1; i < n; i++) {       a = Get(), b = Get();       add(a, b), add(b, a);     }     m = Get();     fac[0] = 1;     for (int i = 1; i <= m; i++) fac[i] = fac[i - 1] * i % mod;     ifac[m] = ksm(fac[m], mod - 2);     for (int i = m - 1; i >= 0; i--) ifac[i] = ifac[i + 1] * (i + 1) % mod;     for (int i = 1; i < m; i++) {       a = Get(), b = Get();       e[a].push_back(b);       e[b].push_back(a);     }     long long tot = 0;     for (rt = m; rt >= 1; rt--) (tot += work()) %= mod;     cout << tot <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 310;   int l[maxn], c[maxn];   int n;   int f[1 << 9];   int g[maxn][10], cnt[maxn];   int main() {     cin >> n;     for (int i = 1; i <= n; i++) cin >> l[i];     for (int i = 1; i <= n; i++) cin >> c[i];     for (int i = 1; i <= n; i++) {       int x = l[i];       for (int j = 2; j * j <= x; j++) {         if (x % j == 0) {           g[i][++cnt[i]] = j;           while (x % j == 0) x /= j;         }       }       if (x > 1) {         g[i][++cnt[i]] = x;       }     }     int ans = 1e9 + 10;     for (int i = 1; i <= n; i++) {       memset(f, 0x3f, sizeof f);       int tot = (1 << cnt[i]) - 1;       f[tot] = c[i];       for (int j = 1; j <= n; j++) {         if (i == j) continue;         int s = 0;         for (int k = 1; k <= cnt[i]; k++) {           int flag = 0;           for (int x = 1; x <= cnt[j]; x++) {             if (g[i][k] == g[j][x]) {               flag = 1;               break;             }           }           if (flag) s |= (1 << (k - 1));         }         for (int k = 0; k <= tot; k++) f[k & s] = min(f[k & s], f[k] + c[j]);       }       ans = min(ans, f[0]);     }     if (ans == 1e9 + 10)       cout << -1;     else       cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e3 * 3 + 10;   int n, k, m;   vector<int> graph[2][MAXN];   int p[2][MAXN];   int sum;   void read_input() {     scanf( %d , &n);     scanf( %d , &k);     int v;     for (int i = 0; i < k - 1; i++) {       sum++;       scanf( %d , &v);       graph[0][n + i + 2].push_back(v + n);       graph[0][v + n].push_back(n + i + 2);       p[0][n + i + 2] = v + n;     }     for (int i = 0; i < n; i++) {       scanf( %d , &v);       graph[0][i + 1].push_back(v + n);       graph[0][v + n].push_back(i + 1);       p[0][i + 1] = n + v;     }     scanf( %d , &m);     for (int i = 0; i < m - 1; i++) {       sum++;       scanf( %d , &v);       graph[1][n + i + 2].push_back(v + n);       graph[1][v + n].push_back(n + i + 2);       p[1][n + i + 2] = v + n;     }     for (int i = 0; i < n; i++) {       scanf( %d , &v);       graph[1][i + 1].push_back(v + n);       graph[1][v + n].push_back(i + 1);       p[1][i + 1] = n + v;     }   }   int timer;   int in[2][MAXN], out[2][MAXN];   int depth[2][MAXN];   void dfs(int v, int p, int r, int dt = 0) {     in[r][v] = timer++;     depth[r][v] = dt;     for (auto x : graph[r][v]) {       if (in[r][x] == 0 && x != p) {         dfs(x, v, r, dt + 1);       }     }     out[r][v] = timer++;   }   int lca[2][MAXN][MAXN];   const int INF = 1e9;   int _abs(int f) {     if (f < 0) return -f;     return f;   }   int T[MAXN][MAXN];   int rec(int l, int r) {     int temp = max(l, r) + 1;     if (temp > n) return 0;     if (T[l][r]) return T[l][r];     int ans = INF;     int add = 0;     if (l != 0) {       int LCA = lca[0][l][temp];       add = _abs(depth[0][LCA] - depth[0][temp]) - 1;       ans = min(ans, rec(temp, r) + add);     } else {       ans = rec(temp, r) + depth[0][temp] - 1;     }     if (r != 0) {       int LCA = lca[1][r][temp];       add = _abs(depth[1][LCA] - depth[1][temp]) - 1;       ans = min(ans, rec(l, temp) + add);     } else {       ans = min(ans, rec(l, temp) + depth[1][temp] - 1);     }     return T[l][r] = ans;   }   void solve() {     timer = 1;     dfs(n + 1, n + 1, 0);     for (int i = 0; i < n; i++) {       int temp = i + 1;       int LCA = temp;       for (int j = i + 1; j < n; j++) {         int tr = j + 1;         while (in[0][tr] > out[0][LCA] || out[0][tr] < in[0][LCA])           LCA = p[0][LCA];         lca[0][temp][tr] = lca[0][tr][temp] = LCA;       }     }     timer = 1;     dfs(n + 1, n + 1, 1);     for (int i = 0; i < n; i++) {       int temp = i + 1;       int LCA = temp;       for (int j = i + 1; j < n; j++) {         int tr = j + 1;         while (in[1][tr] > out[1][LCA] || out[1][tr] < in[1][LCA])           LCA = p[1][LCA];         lca[1][temp][tr] = lca[1][tr][temp] = LCA;       }     }     printf( %d n , sum - rec(0, 0));   }   int main() {     read_input();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, k;     cin >> n >> k;     int a[n];     for (int i = 0; i < n; i++) cin >> a[i];     if (n > 1) sort(a, a + n);     int cont = 0;     if (k == n) {       cout << a[k - 1] <<   n ;     } else if (k == 0) {       if (a[0] - 1 >= 1)         cout << a[0] - 1 << endl;       else         puts( -1 );     } else {       k--;       if (a[k] == a[k + 1])         cout << -1 << endl;       else         cout << a[k] << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 2e5 + 5;   long long v[N];   signed main() {     long long n;     cin >> n;     long long p = 2 * n;     long long x = p * (p + 1);     if (x % 4 == 0)       cout <<  NO n ;     else {       cout <<  YES n ;       for (long long i = 1; i <= 2 * n; i++) v[i] = i;       for (long long i = 1; i <= n; i++) {         if (i & 1) {           cout << 2 * i - 1 <<    ;           v[2 * i - 1] = 0;         } else {           cout << 2 * i <<    ;           v[2 * i] = 0;         }       }       for (long long i = 1; i <= 2 * n; i++)         if (v[i] != 0) cout << v[i] <<    ;     }   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:16000000 )   using namespace std;   const int Maxn = 505;   const int Maxk = 11;   const int Maxd = 10;   const int Inf = 1000000000;   int n, k;   string s;   int w;   struct trie {     trie *ch[Maxd];     int cnt;     trie() {       for (int i = 0; i < Maxd; i++) ch[i] = nullptr;       cnt = 0;     }   };   trie *tr;   void Add(trie *tr, int lvl, const string &s, int add) {     if (lvl >= s.length()) {       tr->cnt += add;       return;     }     int dig = s[lvl] -  0 ;     if (!tr->ch[dig]) tr->ch[dig] = new trie();     Add(tr->ch[dig], lvl + 1, s, add);   }   void Solve(trie *tr, int lvl, int dp[][Maxk]) {     for (int i = 0; i <= lvl; i++) {       for (int j = 0; j <= k; j++) dp[i][j] = Inf;       dp[i][0] = (lvl - i) * tr->cnt;     }     int my[Maxk];     for (int j = 0; j <= k; j++) my[j] = Inf;     my[1] = 0;     int oth[Maxn][Maxk];     for (int i = 0; i < Maxd; i++)       if (tr->ch[i]) {         Solve(tr->ch[i], lvl + 1, oth);         for (int l = 0; l <= lvl; l++)           for (int j = k; j >= 0; j--) {             int ways = dp[l][j];             dp[l][j] = Inf;             for (int z = 0; j + z <= k; z++)               dp[l][j + z] = min(dp[l][j + z], ways + oth[l][z]);           }         for (int j = k; j > 0; j--) {           int ways = my[j];           my[j] = Inf;           for (int z = 0; j + z <= k; z++)             my[j + z] = min(my[j + z], ways + oth[lvl][z]);         }       }     for (int i = 0; i <= lvl; i++)       for (int j = 0; j <= k; j++) dp[i][j] = min(dp[i][j], my[j]);   }   int main() {     tr = new trie();     cin >> n >> k;     for (int i = 0; i < n; i++) {       cin >> s >> w;       Add(tr, 0, s, w);     }     int dp[Maxn][Maxk];     Solve(tr, 0, dp);     int res = Inf;     for (int j = 0; j <= k; j++) res = min(res, dp[0][j]);     printf( %d n , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, w, r, k, i, j, s, q, x, y, l, a[22000];   int main() {     ios_base::sync_with_stdio(0);     cin >> n;     for (i = 1; i <= n; i++) {       cin >> a[i], s += a[i];       if (a[i] < 0) k = 1;     }     if (k == 0) {       cout << s;       return 0;     }     for (l = 2; l <= n; l++)       if (n % l == 0 && n / l >= 3)         for (j = 1; j <= l; j++) {           m = 0;           for (i = j; i <= n; i += l) m += a[i];           s = max(s, m);         }     cout << s;   }


#include<bits/stdc++.h>   using namespace std;   int main()   {    long long t,n,x;    cin>>t;    while(t--)    {     cin>>n>>x;     long long a[n+1],m=30,j,s=0,k,c;     for (long long i = 0; i < n; ++i)     {      cin>>a[i];      k=a[i];      c=0;      while(k%x==0)      {       k/=x;       c++;      }      if (m>c)       m=c,j=i;     }     //cout<<m<<   <<j<<endl;     for (long long i = 0; i < n; ++i)     {      s+=(m+1)*a[i];     }     for (long long i = 0; i < j; ++i)     {      s+=a[i];     }     cout<<s<<endl;    }   }


#include <bits/stdc++.h>   using namespace std;   void setIO(string name =   ) {     ios_base::sync_with_stdio(0);     cin.tie(0);     if (!name.empty()) {       freopen((name +  .in ).c_str(),  r , stdin);       freopen((name +  .out ).c_str(),  w , stdout);     }   }   long long calc(long long a, long long b) { return (a + a * b) * b / 2; }   int main() {     setIO();     long long a, b;     cin >> a >> b;     if (b == 1) {       cout << a;       return 0;     }     long long cur;     if (b % 2)       cur = (b + 1) / 2 * b;     else       cur = b / 2 * (b + 1);     if (a <= b || a < cur || b >= 400000) {       cout << -1;       return 0;     }     vector<long long> divs;     for (long long i = 1; i * i <= a; i++) {       if (a % i == 0) {         if (i == a / i)           divs.push_back(i);         else {           divs.push_back(i);           divs.push_back(a / i);         }       }     }     sort(divs.begin(), divs.end());     for (int i = (int)divs.size() - 1; i >= 0; i--) {       if (calc(divs[i], b) <= a && (calc(divs[i], b) % divs[i] == 0)) {         long long cur = divs[i];         for (int j = 1; j <= b; j++) {           if (j != b) {             cout << cur * j <<    ;             a -= cur * j;           } else {             cout << a;             return 0;           }         }       }     }     cout << -1;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 4e5 + 2;   signed main() {     ios::sync_with_stdio(false);     cin.tie(0);     srand(time(NULL));     int N;     long long sa = 0, sb = 0;     cin >> N;     vector<int> A(N), B(N), ind(N);     iota(ind.begin(), ind.end(), 0);     for (int i = 0; i < N; ++i) {       cin >> A[i];       sa += A[i];     }     for (int i = 0; i < N; ++i) {       cin >> B[i];       sb += B[i];     }     int ok = (N >> 1) + 1;     while (1) {       mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());       long long x = 0, y = 0;       random_shuffle(ind.begin(), ind.end());       for (int i = 0; i < ok; ++i) {         x += A[ind[i]];         y += B[ind[i]];       }       if (x << 1LL > sa && y << 1LL > sb) {         cout << ok <<   n ;         for (int i = 0; i < ok; ++i) {           cout << ind[i] + 1 <<    ;         }         exit(0);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool lucky(int n) {     n = abs(n);     if (n == 8) return true;     while (n) {       int x = n % 10;       if (x == 8) return true;       n /= 10;     }     return false;   }   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= 100; i++) {       int x = n + i;       if (lucky(x)) {         printf( %d n , i);         return 0;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   void chkmax(T &x, T y) {     x = max(x, y);   }   template <typename T>   void chkmin(T &x, T y) {     x = min(x, y);   }   template <typename T>   void read(T &x) {     x = 0;     int f = 1;     char c = getchar();     for (; !isdigit(c); c = getchar())       if (c ==  - ) f = -f;     for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ;     x *= f;   }   const int N = 2e5 + 10;   int a[N];   map<int, long long> mp;   signed main() {     int _;     read(_);     while (_--) {       mp.clear();       int n;       read(n);       long long s = 0, ans = 0;       for (int i = (1); i <= (n); i++) read(a[i]), s += a[i], mp[a[i]]++;       if (s * 2 % n != 0) {         puts( 0 );         continue;       }       long long mid = s * 2 / n;       for (int i = (1); i <= (n); i++)         if (a[i] < mid - a[i]) ans += mp[mid - a[i]];       if (mid % 2 == 0) ans += mp[mid / 2] * (mp[mid / 2] - 1) / 2;       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int p[2005], c[2005];   int n, root = -1;   vector<int> v[2005];   int d[2005], vis[2005];   int ans[2005];   struct cmp {     bool operator()(int x, int y) { return d[x] > d[y]; }   };   priority_queue<int, vector<int>, cmp> q;   void dfs(int x) {     if (c[x] == 0) q.push(x);     for (auto y : v[x]) {       if (vis[y]) continue;       vis[y] = 1;       d[y] = d[x] + 1;       dfs(y);     }   }   int main() {     ios::sync_with_stdio(false);     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> p[i] >> c[i];       if (!p[i]) {         root = i;         continue;       }       v[i].push_back(p[i]);       v[p[i]].push_back(i);     }     vis[root] = 1;     dfs(root);     bool flag = true;     int count = 1;     while (!q.empty()) {       int _top = q.top();       q.pop();       ans[_top] = count++;       int head = p[_top];       while (head) {         c[head]--;         if (c[head] == 0) q.push(head);         head = p[head];       }     }     for (int i = 1; i <= n; i++) {       if (c[i] > 0) {         flag = false;         break;       }     }     if (flag) {       cout <<  YES  << endl;       for (int i = 1; i <= n; i++) cout << ans[i] <<    ;       cout << endl;     } else {       cout <<  NO  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a, b;     vector<pair<int, int> > g;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a >> b;       g.push_back(make_pair(a, b));     }     sort(g.begin(), g.end());     int mx = 0, ans = 0;     for (int i = 0; i < n; i++) {       if (g[i].second < mx) ans++;       mx = max(mx, g[i].second);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) {     if (a == 0) return b;     return gcd(b % a, a);   }   int spf[15000005], ans[15000002];   void sieve() {     spf[1] = 1;     for (int i = 2; i < 15000002; i++) spf[i] = i;     for (int i = 4; i < 15000002; i += 2) spf[i] = 2;     for (int i = 3; i * i < 15000002; i++) {       if (spf[i] == i) {         for (int j = i * i; j < 15000002; j += i)           if (spf[j] == j) spf[j] = i;       }     }   }   int main() {     int i, n, ans1 = 1, k = 0;     scanf( %d , &n);     int a[300005];     sieve();     for (i = 1; i <= n; i++) scanf( %d , &a[i]);     ans1 = a[1];     for (i = 1; i <= n; i++) ans1 = gcd(ans1, a[i]);     for (i = 1; i <= n; i++) a[i] = (a[i] / ans1);     for (i = 1; i <= n; i++) {       int x = a[i];       set<int> q;       while (x != 1) {         q.insert(spf[x]);         x = x / spf[x];       }       set<int>::iterator it1;       for (it1 = q.begin(); it1 != q.end(); ++it1) ans[*it1]++;     }     int p = 0;     for (i = 1; i <= 15000000; i++) p = max(p, ans[i]);     if (p == 0)       printf( -1 n );     else       printf( %d n , n - p);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     string name;     ostringstream convert;     for (int i = 1; i <= n; i++) {       convert << i;       name = convert.str();     }     cout << name[n - 1];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int fx[] = {1, 0, 0, -1};   int fy[] = {0, -1, 1, 0};   int n, m;   int data[1002][1002], dist[1002][1002];   void bfs(int sx, int sy) {     for (int i = 0; i < 1002; i++) {       for (int j = 0; j < 1002; j++) dist[i][j] = INT_MAX;     }     queue<int> q;     q.push(sx);     q.push(sy);     dist[sx][sy] = 0;     while (!q.empty()) {       int u = q.front();       q.pop();       int v = q.front();       q.pop();       for (int i = 0; i < 4; i++) {         int x = u + fx[i];         int y = v + fy[i];         if (x >= 0 && x < n && y >= 0 && y < m && data[x][y] == 0 &&             dist[x][y] > dist[u][v] + 1) {           dist[x][y] = dist[u][v] + 1;           q.push(x);           q.push(y);         }       }     }   }   int main() {     int k;     scanf( %d , &n);     scanf( %d , &m);     scanf( %d , &k);     int sx, sy;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         char ch;         cin >> ch;         if (ch ==  * )           data[i][j] = 1;         else if (ch ==  X ) {           sx = i;           sy = j;         }       }     }     if (k % 2 != 0) {       cout <<  IMPOSSIBLE n ;       return 0;     }     bfs(sx, sy);     vector<char> res;     while (k > 0) {       if (sx < n - 1 && dist[sx + 1][sy] < k) {         sx++;         res.push_back( D );       } else if (sy > 0 && dist[sx][sy - 1] < k) {         sy--;         res.push_back( L );       } else if (sy < m - 1 && dist[sx][sy + 1] < k) {         sy++;         res.push_back( R );       } else if (sx > 0 && dist[sx - 1][sy] < k) {         sx--;         res.push_back( U );       } else {         cout <<  IMPOSSIBLE n ;         return 0;       }       k--;     }     for (int i = 0; i < res.size(); i++) {       cout << res[i];     }     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mxN = 210;   int n, a[mxN], pos[mxN][3];   int next(int i) {     if (pos[a[i]][0] == i) return pos[a[i]][1];     return pos[a[i]][0];   }   void upd(int u, int v) {     if (pos[a[u]][0] == u)       pos[a[u]][0] = v;     else       pos[a[u]][1] = v;   }   bool gd() {     for (int i = 1; i < 2 * n; i += 2) {       if (a[i] != a[i - 1]) return false;     }     return true;   }   void prnt() {     cout <<  Array  << endl;     for (int i = 0; i < 2 * n; i++) {       cout << a[i] <<    ;     }     cout << endl << endl;   }   int main() {     memset(pos, -1, sizeof pos);     cin >> n;     for (int i = 0; i < 2 * n; i++) {       cin >> a[i];       if (pos[a[i]][0] == -1)         pos[a[i]][0] = i;       else         pos[a[i]][1] = i;     }     int cnt = 0;     while (!gd()) {       for (int i = 0; i < 2 * n; i++) {         if (i + 1 < 2 * n && a[i] == a[i + 1]) continue;         if (i && a[i] == a[i - 1]) continue;         if (i + 1 < 2 * n && next(i) > i && next(i + 1) < next(i)) {           upd(i, i + 1);           upd(i + 1, i);           swap(a[i], a[i + 1]);           cnt++;         } else if (i && next(i) < i && next(i - 1) > next(i)) {           upd(i, i - 1);           upd(i - 1, i);           swap(a[i], a[i - 1]);           cnt++;         }       }     }     cout << cnt << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100010;   int f[256], b[8];   vector<pair<int, int>> a;   int main() {     int n, m, k;     scanf( %d%d%d , &n, &m, &k);     for (int i = 1; i <= n; i++) {       int l, r;       scanf( %d%d , &l, &r);       a.push_back({l, i});       a.push_back({r + 1, -i});     }     sort(a.begin(), a.end());     for (int i = 0; i < 2 * n; i++) {       if (i > 0) {         int ms = 0;         for (int p = 0; p < 8; p++) {           if (b[p]) ms += 1 << p;         }         for (int s = 0; s < (1 << k); s++) {           int cnt = __builtin_popcount(s & ms);           if (cnt & 1) f[s] += a[i].first - a[i - 1].first;         }       }       int j = i;       while (j < 2 * n && a[j].first == a[i].first) {         int y = a[j].second;         if (y < 0) {           y = -y;           for (int p = 0; p < 8; p++) {             if (b[p] == y) {               for (int s = 0; s < (1 << k); s++)                 f[s] = max(f[s], f[s ^ (1 << p)]);               b[p] = 0;               break;             }           }         } else {           for (int p = 0; p < 8; p++) {             if (b[p] == 0) {               b[p] = y;               break;             }           }         }         j++;       }       i = j - 1;     }     int ans = 0;     for (int i = 0; i < (1 << k); i++) ans = max(ans, f[i]);     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   const int N = 3e5 + 100;   int a[N], n;   long long ans;   void update(int i, int c) {     if (i <= 0 || i > n) return;     if (a[i] > a[i - 1] && a[i] > a[i + 1]) ans += a[i] * c;     if (a[i] < a[i - 1] && a[i] < a[i + 1]) ans -= a[i] * c;   }   int main() {     int w;     cin >> w;     while (w--) {       int q;       scanf( %d%d , &n, &q);       a[0] = a[n + 1] = 0;       for (int i = 1; i <= n; i++) scanf( %d , a + i);       ans = 0;       for (int i = 1; i <= n; i++) update(i, 1);       printf( %lld n , ans);       while (q--) {         int l, r;         scanf( %d%d , &l, &r);         set<int> st;         for (int i = -1; i <= 1; i++) {           st.insert(l + i);           st.insert(r + i);         }         for (int i : st) update(i, -1);         swap(a[l], a[r]);         for (int i : st) update(i, 1);         printf( %lld n , ans);       }     }     return 0;   }


#include <bits/stdc++.h>   int main() {     int n, i, sum = 0;     char str[51];     scanf( %s , &str);     n = strlen(str);     for (i = 0; i < n; i = i + 1) {       if (str[i] ==  a ) {         sum++;       }     }     if (sum > n / 2) {       printf( %d n , n);     } else {       printf( %d n , 2 * sum - 1);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, k;     vector<long long int> v;     cin >> n >> k;     long long int r;     for (long long int i = 0; i < n; i++) {       long long int a;       cin >> a;       v.push_back(a);     }     r = v[0] % k;     long long int mn = 10000000000000;     for (long long int i = 0; i < n; i++) {       if (v[i] % k != r) {         cout <<  -1 n ;         return 0;       }       mn = min(mn, v[i]);     }     long long int count = 0;     for (int i = 0; i < n; i++) {       count += (v[i] - mn) / k;     }     cout << count <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   string NumberToString(T Number) {     std::ostringstream second;     second << Number;     return second.str();   }   long long qpow(long long p, long long q) {     long long f = 1;     while (q) {       if (q & 1) f = f * p % 1000000007LL;       p = p * p % 1000000007LL;       q >>= 1;     }     return f;   }   inline int read() {     int x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   const long long mod7 = 998244353LL;   inline long long add(long long _x, long long _y, long long _mod = mod7) {     _x += _y;     return _x >= _mod ? _x - _mod : _x;   }   inline long long sub(long long _x, long long _y, long long _mod = mod7) {     _x -= _y;     return _x < 0 ? _x + _mod : _x;   }   inline long long mul(long long _x, long long _y, long long _mod = mod7) {     _x *= _y;     return _x >= _mod ? _x % _mod : _x;   }   int const N = 553;   long long t[N][N], cum[N][N];   int main() {     for (auto ik = 0; ik < (501); ++ik) cum[0][ik] = 1LL;     for (int in = 1; in <= 500; ++in) {       for (int ik = 1; ik <= in; ++ik) {         for (int before = 0; (in - ik - before) >= 0; ++before) {           t[in][ik] =               add(t[in][ik],                   mul(cum[before][min(before, ik - 1)],                       cum[(in - ik - before)][min((in - ik - before), ik)]));         }         cum[in][ik] = add(cum[in][ik - 1], t[in][ik]);       }     }     int n, k;     cin >> n >> k;     long long sol = 0LL;     for (int a = 1; a < min(k, n + 1); ++a) {       if (k % a == 0)         sol = add(sol, mul(t[n][a], cum[n][min(n, (k - 1) / a)]));       else         sol = add(sol, mul(t[n][a], cum[n][min(n, k / a)]));     }     cout << mul(sol, 2LL);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }   inline int gcd(int a, int b) {     if (b == 0) return a;     return gcd(b, a % b);   }   template <typename T>   string to_str(T str) {     stringstream stream;     stream << str;     return stream.str();   }   template <typename T>   int to_int(T num) {     int val;     stringstream stream;     stream << num;     stream >> val;     return val;   }   vector<string> split(const string& s, char delim) {     vector<string> elems;     stringstream ss(s);     string item;     while (getline(ss, item, delim)) elems.push_back(item);     return elems;   }   const int dr[] = {-1, -1, 0, 1, 1, 1, 0, -1};   const int dc[] = {0, 1, 1, 1, 0, -1, -1, -1};   const int T2 = 105;   const int T3 = 1005;   const int T4 = 10005;   const int T5 = 100005;   int a[T3], b[T3];   const int M = 475;   bool check(int s, int p) {     int i = (s / 50) % M;     int loop = 25;     while (loop--) {       i = (i * 96 + 42) % M;       if (26 + i == p) {         return true;       }     }     return false;   }   int main() {     ios_base::sync_with_stdio(0);     int p, x, y;     cin >> p >> x >> y;     int s = x;     bool f = 0;     while (s >= y) {       if (check(s, p)) {         f = 1;         break;       }       s -= 50;     }     if (!f) {       s = x;       while (1) {         if (check(s, p)) {           f = 1;           break;         }         s += 50;       }     }     int diff = s - x;     if (diff < 0)       cout <<  0 n ;     else       cout << ceil(diff * 1.0 / 100) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long double esp = 1e-11;   const long double PI = acos(-1.0);   const long long int INF = 0x3f3f3f3f;   const long long int MOD = 1000000007;   const int maxn = 10010;   const int NUM = 25;   long long int wn[NUM];   long long int mu(long long int a, long long int b, long long int P) {     long long int ans = 1;     a %= P;     while (b) {       if (b & 1) ans = ans * a % P;       a = a * a % P;       b >>= 1;     }     return ans;   }   void GetWn(long long int G, long long int P) {     long long int t;     for (int i = 0; i < NUM; i++) {       t = 1ll << i;       wn[i] = mu(G, (P - 1) / t, P);     }   }   void Rader(long long int a[], int len) {     int j = len >> 1, k;     for (int i = 1; i < len - 1; i++) {       if (i < j) swap(a[i], a[j]);       k = len >> 1;       while (j >= k) {         j -= k;         k >>= 1;       }       if (j < k) j += k;     }   }   void NTT(long long int a[], int len, int on, long long int P) {     Rader(a, len);     long long int id = 0, w, u, t;     for (int h = 2; h <= len; h <<= 1) {       id++;       for (int j = 0; j < len; j += h) {         w = 1;         for (int k = j; k < j + h / 2; k++) {           u = a[k] % P;           t = w * a[k + h / 2] % P;           a[k] = (u + t) % P;           a[k + h / 2] = (u - t + P) % P;           w = w * wn[id] % P;         }       }     }     if (on == -1) {       for (int i = 1; i < len / 2; i++) swap(a[i], a[len - i]);       long long int inv = mu(len, P - 2, P);       for (int i = 0; i < len; i++) a[i] = a[i] * inv % P;     }   }   long long int a[1ll << 20];   int main() {     int n, k, tem;     scanf( %d%d , &n, &k);     memset(a, 0, sizeof(a));     for (int x = 1; x <= n; x++) {       scanf( %d , &tem);       a[tem] = 1;     }     long long int N = 1ll << 20, G = 3, P = 469762049ll;     GetWn(G, P);     NTT(a, N, 1, P);     for (int i = 0; i < N; i++) a[i] = mu(a[i], k, P);     NTT(a, N, -1, P);     for (int i = 0; i < N; i++)       if (a[i]) printf( %d  , i);     return 0;   }


#include <bits/stdc++.h>   int res[1000008];   using namespace std;   int main() {     bool pr[1000008 + 1];     memset(pr, true, sizeof(pr));     int cnt = 0;     for (int p = 2; p * p <= 1000008; p++) {       if (pr[p] == true) {         for (int j = p * p; j <= 1000008; j += p) pr[j] = false;       }     }     for (int i = 2; i <= 1000008; i++) {       if (pr[i]) {         cnt++;       }       res[i] = cnt;     }     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int k = sqrt(n);       int r = res[n] - res[k];       cout << r + 1 <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   long long ans[1000005];   long long n;   struct cht {     vector<pair<long long, long long> > v;     long long pt = 0;     long long val(long long pos, long long x) {       return v[pos].first * x + v[pos].second;     }     bool lmao(pair<long long, long long> p0, pair<long long, long long> p1,               pair<long long, long long> p2) {       double lel = 1.0 * (p1.second - p0.second) * (p1.first - p2.first) -                        1.0 * (p2.second - p1.second) * (p0.first - p1.first) >=                    1e-7;       if (lel > 0) return true;       return false;     }     void add(long long first, long long second) {       for (pair<long long, long long> hic = {first, second};            v.size() > 1 && lmao(hic, v[v.size() - 1], v[v.size() - 2]);            v.pop_back())         ;       v.push_back({first, second});     }     long long get(long long x) {       for (pt = min(pt, (long long)v.size() - 1);            pt + 1 < (long long)v.size() && val(pt, x) < val(pt + 1, x); pt++)         ;       return val(pt, x);     }   } C;   pair<long long, pair<long long, long long> > first[1000005];   signed main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n;     C.add(0, 0);     for (long long i = 1; i <= n; i++) {       cin >> first[i].first >> first[i].second.first >> first[i].second.second;     }     sort(first + 1, first + n + 1);     long long hihi = -1000000000000000000;     for (long long i = 1; i <= n; i++) {       ans[i] = C.get(first[i].second.first) - first[i].second.second +                first[i].first * first[i].second.first;       C.add(-first[i].first, ans[i]);       hihi = max(hihi, ans[i]);     }     cout << hihi;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   using uint = unsigned int;   using ull = unsigned long long;   using ii = pair<int, int>;   using pli = pair<ll, int>;   using vi = vector<int>;   using vll = vector<ll>;   using pll = pair<ll, ll>;   using pil = pair<int, ll>;   using vvi = vector<vi>;   using vpi = vector<ii>;   const int N = 100100;   ll mod = 1000000007;   vvi adj;   vi p, h;   int n, m;   vi vis;   ii solve(int i) {     int tot_people = p[i];     int good = 0;     vis[i] = true;     for (auto x : adj[i]) {       if (vis[x]) continue;       auto temp = solve(x);       if (temp.first == -1) {         return temp;       }       tot_people += temp.first;       good += temp.second;     }     int good_calculated = (tot_people + h[i]) / 2;     if (good_calculated > tot_people || (tot_people + h[i]) % 2 ||         good > good_calculated) {       return make_pair(-1, -1);     } else {       return make_pair(tot_people, good_calculated);     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int tt;     cin >> tt;     while (tt--) {       int n, m;       cin >> n >> m;       adj.resize(n);       for (int i = 0; i <= n - 1; i++) adj[i].clear();       p.clear();       p.resize(n);       vis.clear();       vis.resize(n);       for (int i = 0; i <= n - 1; i++) cin >> p[i];       assert(accumulate(p.begin(), p.end(), 0) == m);       h.resize(n);       for (int i = 0; i <= n - 1; i++) {         cin >> h[i];       }       for (int i = 0; i <= n - 2; i++) {         int x, y;         cin >> x >> y;         x--, y--;         adj[x].push_back(y);         adj[y].push_back(x);       }       auto ans = solve(0);       if (ans.first != -1) {         cout <<  YES               <<   n ;       } else {         cout <<  NO               <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   const int N = 1e6 + 5;   int a[N], m[N];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int n, b;     cin >> n >> b;     for (int i = 1; i <= n; ++i) cin >> a[i];     for (int i = n; i >= 0; --i) m[i] = max(m[i + 1], a[i]);     int ans = b;     for (int i = 1; i <= n; ++i) {       ans = max(ans, b % a[i] + b / a[i] * m[i + 1]);     }     cout << (n == 1 ? b : ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[1005];   int main() {     int n, maxp, mx2p;     scanf( %d , &n);     for (register int i = 1; i <= n; ++i) {       scanf( %d , a + i);     }     if (a[1] > a[2]) {       maxp = 1;       mx2p = 2;     } else {       maxp = 2;       mx2p = 1;     }     for (register int i = 3; i <= n; ++i) {       if (a[i] > a[maxp]) {         mx2p = maxp;         maxp = i;       } else if (a[i] > a[mx2p]) {         mx2p = i;       }     }     printf( %d %d , maxp, a[mx2p]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     if (n == 2) {       cout <<  white n1 2 n ;     } else if (n % 2 == 0) {       cout <<  white n1 2 n ;     } else {       cout <<  black n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       int l, r;       cin >> l >> r;       cout << l <<     << l * 2 <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long add(long long x, long long y) {     x += y;     if (x >= 1000000007) x -= 1000000007;     return x;   }   inline long long sub(long long x, long long y) {     x -= y;     if (x < 0) x += 1000000007;     return x;   }   inline long long mul(long long x, long long y) {     return ((x % 1000000007) * (y % 1000000007)) % 1000000007;   }   inline long long pwr(long long a, long long b) {     long long x = 1LL;     while (b) {       if (b & 1) x = mul(x, a);       a = mul(a, a);       b >>= 1;     }     return x;   }   bool CHK(long long n) {     if (n == 1) return 0;     if (n == 2 or n == 3) return 1;     if (n % 2 == 0 or n % 3 == 0) return 0;     for (long long i = 5; i * i <= n; i += 6)       if (n % i == 0 or n % (i + 2) == 0) return 0;     return 1;   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(NULL);     long long n = 0;     cin >> n;     string s;     cin >> s;     long long cnt = 0;     for (long long i = 0; i < n - 1; i++) {       if (s[i] ==  * ) continue;       if (s[i] == s[i + 1]) cnt++, s[i + 1] =  * ;     }     cout << cnt <<   n ;     for (long long i = 0; i < n; i++) {       if (s[i] ==  *  and i != n - 1) {         if (s[i - 1] ==  R  and s[i + 1] ==  G )           s[i] =  B ;         else if (s[i - 1] ==  G  and s[i + 1] ==  B )           s[i] =  R ;         else if (s[i - 1] ==  R  and s[i + 1] ==  B )           s[i] =  G ;         else if (s[i - 1] ==  R  and s[i + 1] ==  R )           s[i] =  G ;         else if (s[i - 1] ==  B  and s[i + 1] ==  B )           s[i] =  G ;         else if (s[i - 1] ==  G  and s[i + 1] ==  G )           s[i] =  R ;         else if (s[i - 1] ==  G  and s[i + 1] ==  R )           s[i] =  B ;         else if (s[i - 1] ==  B  and s[i + 1] ==  G )           s[i] =  R ;         else if (s[i - 1] ==  B  and s[i + 1] ==  R )           s[i] =  G ;       } else if (s[i] ==  *  and i == n - 1) {         if (s[i - 1] ==  R )           s[i] =  G ;         else if (s[i - 1] ==  B )           s[i] =  G ;         else if (s[i - 1] ==  G )           s[i] =  B ;       }     }     for (auto u : s) cout << u;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 3e5;   set<int> v[MAXN];   int last = -1, lastx = -1;   vector<int> f;   int ans = 0;   int t[MAXN], x_[MAXN];   bool find(vector<int> &d, int k) {     if (d.empty()) return false;     int l = 0, r = int(d.size());     while (r - l > 1) {       int m = (l + r) / 2;       if (d[m] <= k)         l = m;       else         r = m;     }     return d[l] == k;   }   int main() {     ios_base::sync_with_stdio(false);     int n, q;     cin >> n >> q;     for (int i = 0; i < q; i++) {       int type, x;       cin >> type >> x;       x--;       t[i] = type, x_[i] = x;       if (type == 1) {         v[x].insert(i);         f.push_back(i);         ans++;       } else if (type == 2) {         ans -= (int)v[x].size();         v[x].clear();       } else if (type == 3)         if (last < f[x]) {           for (int j = lastx + 1; j <= x; j++) {             int id = f[j];             if (v[x_[id]].find(id) != v[x_[id]].end()) v[x_[id]].erase(id), ans--;           }           last = f[x], lastx = x;         }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, t1, t2, t3, t4, t5, dif, yes, rep, cnt[1005], last[1005],       ans[1005][1005], ma[1005][1005];   vector<int> nod, re, v[1005];   vector<pair<int, int> > path, more;   vector<pair<pair<int, int>, int> > sol;   bool vis[1005];   pair<int, int> pl, pr;   bool ok;   void clr() {     for (int i = 1; i <= n; i++) vis[i] = 0;     return;   }   int any(int mo) {     vis[mo] = 1;     if (v[mo].size() == 1)       return mo;     else {       for (int i = 0; i < v[mo].size(); i++) {         if (!vis[v[mo][i]]) {           return any(v[mo][i]);         }       }     }   }   pair<int, int> exp(int mo) {     clr();     vis[t4] = 1, vis[t5] = 1;     vector<int> ret;     for (int i = 0; i < v[mo].size(); i++) {       if (v[mo][i] != t4 && v[mo][i] != t5) {         ret.push_back(any(v[mo][i]));         if (ret.size() > 1) {           return make_pair(ret[0], ret[1]);         }       }     }   }   void dfs(int from, int to, int val) {     ok = 0;     vis[from] = 1;     if (from == to) {       ok = 1, yes = from;       return;     }     for (int i = 0; i < v[from].size(); i++) {       if (!vis[v[from][i]] && !ok) {         dfs(v[from][i], to, val);       }     }     if (ok) {       ma[from][yes] += val;       ma[yes][from] += val;       yes = from;       return;     }   }   int main() {     cin >> n;     for (int i = 0; i < n - 1; i++) {       cin >> t1 >> t2 >> t3;       path.push_back(make_pair(t1, t2));       cnt[t1]++, cnt[t2]++;       last[t1] = t3, last[t2] = t3;       v[t1].push_back(t2);       v[t2].push_back(t1);       ans[t1][t2] = t3;     }     for (int i = 1; i <= n; i++) {       if (cnt[i] == 2) {         cout <<  NO  << endl;         return 0;       }       if (cnt[i] == 1 && last[i] != 0) {         nod.push_back(i);       }     }     for (int i = 0; i < path.size(); i++) {       if (cnt[path[i].first] > 1 && cnt[path[i].second] > 1) {         more.push_back(path[i]);       }     }     sort(nod.begin(), nod.end());     while (nod.size() > 1) {       sol.push_back(make_pair(make_pair(nod[0], nod[1]), last[nod[0]]));       clr();       dfs(nod[0], nod[1], last[nod[0]]);       last[nod[1]] -= last[nod[0]];       if (re.size() < 2) re.push_back(nod[0]);       nod.erase(nod.begin());     }     if (nod.size() == 1 && n > 2) {       sol.push_back(make_pair(make_pair(re[1], nod[0]), last[nod[0]] / 2));       clr();       dfs(re[1], nod[0], last[nod[0]] / 2);       sol.push_back(make_pair(make_pair(re[0], re[1]), last[nod[0]] / -2));       clr();       dfs(re[0], re[1], last[nod[0]] / -2);       sol.push_back(make_pair(make_pair(re[0], nod[0]), last[nod[0]] / 2));       clr();       dfs(re[0], nod[0], last[nod[0]] / 2);     }     for (int i = 0; i < more.size(); i++) {       t4 = more[i].first, t5 = more[i].second;       if (ma[t4][t5] != ans[t4][t5]) {         pl = exp(t4), pr = exp(t5), dif = ans[t4][t5] - ma[t4][t5];         sol.push_back(make_pair(make_pair(pl.first, pr.first), dif / 2));         sol.push_back(make_pair(make_pair(pl.second, pr.second), dif / 2));         sol.push_back(make_pair(make_pair(pl.first, pl.second), dif / -2));         sol.push_back(make_pair(make_pair(pr.first, pr.second), dif / -2));       }     }     cout <<  YES  << endl << sol.size() << endl;     for (int i = 0; i < sol.size(); i++) {       cout << sol[i].first.first <<     << sol[i].first.second <<                << sol[i].second << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a, b;     cin >> n >> a >> b;     int r = 0;     for (int i = 1; i < 300; i++) {       int x1 = a / i;       int x2 = b / i;       if (x1 + x2 >= n && x1 != 0 && x2 != 0) r = i;     }     cout << r;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5;   long long dmod(long long x, long long MOD) { return x % MOD; }   long long m;   long long fe(long long x, long long p) {     if (p == 0) return 1;     long long ret = fe(x, p / 2);     ret = dmod(ret * ret, m);     if (p & 1) ret = dmod(ret * x, m);     return ret;   }   int n;   long long li[N + 5];   set<long long> ada;   long long get_count(long long x) {     long long ret = 0;     for (int i = 1; i <= n; i++) {       long long cur = (li[i] + x) % m;       if (ada.find(cur) != ada.end()) ret++;     }     return ret;   }   bool proc() {     if (2 * n < m) return 0;     set<long long> tmp;     long long cur = 0;     for (int i = 1; i <= n; i++) {       while (cur != li[i]) tmp.insert(cur++);       cur++;     }     while (cur != m) tmp.insert(cur++);     int id = 0;     for (auto x : tmp) {       id++;       li[id] = x;     }     n = id;     return 1;   }   int main() {     scanf( %d%d , &m, &n);     for (int i = 1; i <= n; i++) scanf( %lld , &li[i]);     if (n == 1) {       printf( %lld 0 n , li[1]);       return 0;     } else if (m == 2 || n == m) {       printf( 0 1 n );       return 0;     } else if (n == m - 1) {       sort(li + 1, li + n + 1);       for (int i = 1; i <= n; i++) {         if (li[i] != (i - 1)) {           printf( %d 1 n , i);           return 0;         }       }       printf( 0 1 n );       return 0;     }     sort(li + 1, li + n + 1);     bool tukar = proc();     for (int i = 1; i <= n; i++) ada.insert(li[i]);     long long dif = li[2] - li[1];     long long cnt = get_count(dif);     long long inv = fe(n - cnt, m - 2);     dif = dmod(dif * inv, m);     deque<long long> dq;     dq.push_front(li[1]);     while (1) {       long long now = dq.front();       now = dmod(now - dif + m, m);       if (ada.find(now) != ada.end())         dq.push_front(now);       else         break;     }     while (1) {       long long now = dq.back();       now = dmod(now + dif, m);       if (ada.find(now) != ada.end())         dq.push_back(now);       else         break;     }     if (dq.size() != n) {       printf( -1 n );       return 0;     }     if (tukar) {       printf( %lld %lld n , dmod(dq.back() + dif, m), dif);     } else {       printf( %lld %lld n , dq.front(), dif);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long dp[300300];   int a[300300], h[300300], d[300300];   vector<int> ans;   inline void calc(int i) {     int mn = min(a[i], a[i + 1]);     if (!mn) return;     ans.push_back(i);     a[i] -= mn, a[i + 1] -= mn;   }   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , a + i);     for (int i = 1; i <= 2; i++) dp[i] = a[i], d[i] = 1;     for (int i = 3; i <= n; i++) {       if (dp[i - 2] + a[i] < dp[i - 3] + max(a[i], a[i - 1]))         dp[i] = dp[i - 2] + a[i], d[i] = 1;       else         dp[i] = dp[i - 3] + max(a[i], a[i - 1]), d[i] = 2;     }     for (int i = n - (dp[n] >= dp[n - 1]); i > 0; i -= d[i] + 1) {       h[i] = h[i - d[i] + 1] = 1;     }     for (int i = 1; i <= n; i++)       if (h[i]) {         if (h[i + 1]) calc(i);         calc(i - 1), calc(i);       }     printf( %d n , ans.size());     for (int x : ans) printf( %d n , x);   }


#include <bits/stdc++.h>   using namespace std;   vector<string> a;   long long i, j, k, sumA = 0, sumB = 0, min_sumA = 8, min_sumB = 8;   string t;   int main() {     for (i = 0; i < 8; i++) {       cin >> t;       a.push_back(t);     }     for (i = 0; i < 8; i++)       for (j = 0; j < 8; j++) {         sumA = sumB = 0;         if (a[i][j] ==  W ) {           for (k = i - 1; k >= 0; k--)             if (a[k][j] !=  B )               ++sumA;             else {               sumA = min_sumA;               break;             }           sumB = min_sumB;         } else if (a[i][j] ==  B ) {           for (k = i + 1; k < 8; k++)             if (a[k][j] !=  W )               ++sumB;             else {               sumB = min_sumB;               break;             }           sumA = min_sumA;         } else {           sumA = min_sumA;           sumB = min_sumB;         }         if (sumA < min_sumA) min_sumA = sumA;         if (sumB < min_sumB) min_sumB = sumB;       }     if (min_sumA <= min_sumB)       printf( A );     else       printf( B );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int Maxn = 3e5 + 10;   const int lim = 1000;   int prime[Maxn], M[Maxn], H[Maxn], cnt;   bool vis[Maxn];   struct node {     int v, c, l;   } a[Maxn];   int cmp(node A, node B) { return A.l < B.l; }   int n, k;   int main() {     int cnt = 0;     vis[0] = vis[1] = 1;     for (int i = 2; i <= 3e5; i++) {       if (!vis[i]) prime[++cnt] = i;       for (int j = 1; j <= cnt && i * prime[j] <= 3e5; j++) {         vis[i * prime[j]] = 1;         if (i % prime[j] == 0) break;       }     }     scanf( %d %d , &n, &k);     for (int i = 1; i <= n; i++) scanf( %d %d %d , &a[i].v, &a[i].c, &a[i].l);     sort(a + 1, a + n + 1, cmp);     for (int i = 1; i <= n; i++) M[a[i].l] = i;     int ans = 0, sum, top;     for (int i = 1; i <= n; i++) {       if (!M[i]) continue;       for (int h = 0; h < lim; h++) {         random_shuffle(a + 1, a + M[i] + 1);         top = 0, sum = 0;         for (int j = 1; j <= M[i]; j++) {           bool _ = 1;           for (int q = 0; q < top; q++)             if (!vis[a[j].c + a[H[q]].c]) {               _ = 0;               break;             }           if (_) {             H[top++] = j;             sum += a[j].v;           }         }         if (sum >= k) return 0 * printf( %d n , i);       }     }     puts( -1 );   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100000 + 10;   struct TreeNode {     int L, R;     pair<double, double> val;   } tree[maxn << 2];   int a[maxn], b[maxn], n, q;   void pushUp(int root) {     tree[root].val.first =         tree[root << 1].val.first * tree[root << 1 | 1].val.first;     tree[root].val.second =         tree[root << 1].val.second +         tree[root << 1].val.first * tree[root << 1 | 1].val.second;   }   void build(int L, int R, int root) {     tree[root].L = L;     tree[root].R = R;     if (L == R) {       return;     }     int mid = (L + R) >> 1;     build(L, mid, root << 1);     build(mid + 1, R, root << 1 | 1);   }   void update(int pos, pair<double, double> val, int root) {     int L = tree[root].L, R = tree[root].R;     if (L == R) {       tree[root].val = val;       return;     }     int mid = (L + R) >> 1;     if (pos <= mid) {       update(pos, val, root << 1);     } else {       update(pos, val, root << 1 | 1);     }     pushUp(root);   }   pair<double, double> query(int ql, int qr, int root) {     int L = tree[root].L, R = tree[root].R;     if (L >= ql && R <= qr) return tree[root].val;     int mid = (L + R) >> 1;     pair<double, double> a = make_pair(1.0, 0.0), b = make_pair(1.0, 0.0), c;     if (ql <= mid) {       a = query(ql, qr, root << 1);     }     if (qr > mid) {       b = query(ql, qr, root << 1 | 1);     }     c.first = a.first * b.first;     c.second = a.second + a.first * b.second;     return c;   }   int main() {     cin >> n >> q;     build(1, n, 1);     for (int i = 1; i <= n; i++) {       double x, y;       cin >> x >> y;       double p = x / y;       update(i, make_pair((1 - p) / p, (1 - p) / p), 1);     }     while (q--) {       int type;       cin >> type;       if (type == 1) {         int i, x, y;         scanf( %d%d%d , &i, &x, &y);         double p = double(x) / y;         update(i, make_pair((1.0 - p) / p, (1.0 - p) / p), 1);       } else {         int l, r;         scanf( %d%d , &l, &r);         pair<double, double> t = query(l, r, 1);         double p = t.second;         if (p < 1e20)           printf( %.6lf n , 1.0 / (1 + p));         else           cout <<  0.0000000  << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a, b, minn = 1000;     long long res = 0;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d%d , &a, &b);       minn = min(minn, b);       res += minn * a;     }     printf( %d , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using namespace std::chrono;   bool mysort(pair<long long, long long> A, pair<long long, long long> B) {     return A.second > B.second;   }   long long moduloMultiplication(long long a, long long b, long long mod) {     long long res = 0;     a %= mod;     while (b) {       if (b & 1) res = (res + a) % mod;       a = (2 * a) % mod;       b >>= 1;     }     return res;   }   void solve() {     long long n, k, i, j;     cin >> n >> k;     if (k == 0) {       if (n % 2 == 0)         cout <<  0 n ;       else         cout <<  1 n ;     } else {       if (k <= n) {         if (n == 1)           cout << n - k <<   n ;         else {           if ((n + k) % 2 == 0) {             cout <<  0 n ;           } else {             cout <<  1 n ;           }         }       } else {         cout << k - n <<   n ;       }     }   }   int32_t main() {     std::ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t;     cin >> t;     while (t--) {       auto start = high_resolution_clock::now();       solve();       auto stop = high_resolution_clock::now();       auto duration = duration_cast<seconds>(stop - start);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int k, a, b, v, nr = 0;     cin >> k >> a >> b >> v;     while (a > 0) {       ++nr;       if (b + 1 >= k) {         b = b + 1 - k;         a = a - k * v;       } else {         a = a - (b + 1) * v;         b = 0;       }     }     cout << nr;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int m;   char s[15];   int ans[1005];   bool num[15];   bool dfs(int suml, int sumr, int cot, int pre) {     if (cot == m + 1) return true;     if (cot & 1) {       for (int i = 1; i <= 10; i++)         if (num[i] && i != pre && suml + i > sumr) {           ans[cot] = i;           if (dfs(suml + i, sumr, cot + 1, i)) return true;         }     } else {       for (int i = 1; i <= 10; i++) {         if (num[i] && i != pre && sumr + i > suml) {           ans[cot] = i;           if (dfs(suml, sumr + i, cot + 1, i)) return true;         }       }     }     return false;   }   int main() {     scanf( %s , s + 1);     for (int i = 1; i <= 10; i++)       if (s[i] ==  1 ) num[i] = 1;     scanf( %d , &m);     if (!dfs(0, 0, 1, 0))       printf( NO n );     else {       printf( YES n );       for (int i = 1; i <= m; i++) printf( %d  , ans[i]);       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 998244353;   inline int qpow(int a, int p) {     int ans = 1;     while (p) {       if (p & 1) ans = (long long)ans * a % MOD;       a = (long long)a * a % MOD;       p >>= 1;     }     return ans;   }   inline int add(const int& x, const int& y) {     return x + y >= MOD ? x + y - MOD : x + y;   }   inline int dec(const int& x, const int& y) {     return x < y ? x - y + MOD : x - y;   }   int rt[2][24], r[262144], l, lim;   inline void init() {     lim = 1 << l;     for (int i = 0; i < lim; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) << (l - 1));   }   inline void NTT(int* a, int type) {     for (int i = 0; i < lim; i++)       if (i < r[i]) swap(a[i], a[r[i]]);     for (int L = 0; L < l; L++) {       int mid = 1 << L, len = mid << 1;       long long Wn = rt[type][L + 1];       for (int s = 0; s < lim; s += len)         for (int k = 0, w = 1; k < mid; k++, w = w * Wn % MOD) {           int x = a[s + k], y = (long long)w * a[s + mid + k] % MOD;           a[s + k] = add(x, y), a[s + mid + k] = dec(x, y);         }     }     if (type) {       int t = qpow(lim, MOD - 2);       for (int i = 0; i < lim; i++) a[i] = (long long)a[i] * t % MOD;     }   }   int fac[262144], finv[262144];   int f[262144], g[262144], t[262144];   void solve(int n) {     if (n == 0) return (void)(f[0] = 1);     int m = n >> 1;     solve(m);     for (l = 0; (1 << l) <= (m << 1); l++)       ;     init();     for (int i = 0; i <= m; i++)       t[i] = f[i], f[i] = (long long)f[i] * fac[i] % MOD;     g[m] = 1;     for (int i = m - 1; i >= 0; i--) g[i] = (long long)g[i + 1] * m % MOD;     for (int i = 0; i <= m; i++) g[i] = (long long)g[i] * finv[m - i] % MOD;     for (int i = m + 1; i < lim; i++) g[i] = 0;     NTT(f, 0), NTT(g, 0);     for (int i = 0; i < lim; i++) g[i] = (long long)f[i] * g[i] % MOD;     NTT(g, 1);     for (int i = 0; i <= m; i++) g[i] = (long long)g[i + m] * finv[i] % MOD;     for (int i = m + 1; i < lim; i++) g[i] = 0;     for (int i = 0; i <= m; i++) f[i] = t[i];     for (int i = m + 1; i < lim; i++) f[i] = 0;     NTT(f, 0), NTT(g, 0);     for (int i = 0; i < lim; i++) f[i] = (long long)f[i] * g[i] % MOD;     NTT(f, 1);     if (n & 1) {       for (int i = n; i >= 1; i--) f[i] = ((n - 1ll) * f[i] + f[i - 1]) % MOD;       f[0] = (n - 1ll) * f[0] % MOD;     }   }   int main() {     rt[0][23] = qpow(3, 119), rt[1][23] = qpow(rt[0][23], MOD - 2);     for (int i = 22; i >= 0; i--) {       rt[0][i] = (long long)rt[0][i + 1] * rt[0][i + 1] % MOD;       rt[1][i] = (long long)rt[1][i + 1] * rt[1][i + 1] % MOD;     }     int n, a, b;     scanf( %d%d%d , &n, &a, &b);     --n, --a, --b;     if (a < 0 || b < 0 || n < a + b) return puts( 0 ), 0;     fac[0] = 1;     for (int i = 1; i <= n; i++) fac[i] = (long long)fac[i - 1] * i % MOD;     finv[n] = qpow(fac[n], MOD - 2);     for (int i = n - 1; i >= 0; i--)       finv[i] = (long long)finv[i + 1] * (i + 1) % MOD;     solve(n);     int ans =         (long long)f[a + b] * fac[a + b] % MOD * finv[a] % MOD * finv[b] % MOD;     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char s[600010];   int a[600010];   int main() {     int n;     cin >> n >> s + 1;     for (int i = n + 1; i < 2 * n; i++) s[i] = s[i - n];     for (int i = 1; i < 2 * n; i++) {       a[i] = a[i - 1] + (s[i] ==  (  ? 1 : -1);     }     if (a[n] != 0) {       cout << 0 << endl;       cout <<  1 1  << endl;       return 0;     }     int cnt = 0, minn = 0;     for (int i = 1; i <= n; i++) {       minn = min(minn, a[i]);     }     int now = 0;     for (int i = 1; i <= n; i++) {       if (a[i] == minn) cnt++, now = i;     }     int ans = cnt;     int l = 1, r = 1;     int nowl = now, tmp = 0;     for (int i = now; i <= now + n - 1; i++) {       if (a[i] < minn + 2) {         if (cnt + tmp > ans) {           ans = tmp + cnt;           l = (nowl + 1 + n - 1) % n + 1, r = (i + n - 1) % n + 1;         }         nowl = i, tmp = 0;       } else if (a[i] == minn + 2)         tmp++;     }     tmp = 0, nowl = now;     for (int i = now; i <= now + n; i++) {       if (i == now + n || a[i] == minn) {         if (tmp > ans) {           ans = tmp;           l = (nowl + 1 + n - 1) % n + 1, r = (i + n - 1) % n + 1;         }         nowl = i, tmp = 0;       } else if (a[i] == minn + 1)         tmp++;     }     cout << ans << endl;     cout << l <<     << r << endl;   }


#include <bits/stdc++.h>   using namespace std;   inline const char nl =   n ;   inline const long long MOD = 1000000007;   inline const long double pi = 3.141592653589793238;   struct query {     int a, b;   } q[105];   bool vis[105];   int n, cnt;   void dfs(int x) {     vis[x] = 1;     for (int i = 1; i <= cnt; i++) {       query y = q[i];       if (vis[i]) continue;       if ((y.a < q[x].a && q[x].a < y.b) || (y.a < q[x].b && q[x].b < y.b))         dfs(i);     }   }   int main() {     cin >> n;     while (n--) {       int op, x, y;       cin >> op >> x >> y;       if (op == 1) q[++cnt].a = x, q[cnt].b = y;       if (op == 2) {         for (int i = 1; i <= 100; i++) vis[i] = 0;         dfs(x);         if (vis[y])           cout <<  YES n ;         else           cout <<  NO n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   ifstream inp( inp.txt );   ofstream out( out.txt );   const int N = 1e5 + 5;   const long long B = 73;   int builtin_popcount(int &mask) {     int kol = 0;     for (int bit = 0; bit < 20; bit++) kol += int(bool(mask & (1 << bit)));     return kol;   }   signed main() {     ios::sync_with_stdio(0);     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     string a;     cin >> a;     int up = (1 << 20);     vector<int> dp(up, 0);     vector<int> masks;     for (int i = 0; i < ((int)a.size()); i++) {       int mask = 0;       for (int j = i; j < min(((int)a.size()), i + 20); j++) {         int bit = a[j] -  a ;         if (mask & (1 << bit)) break;         mask |= (1 << bit);         masks.push_back(mask);         dp[mask] = builtin_popcount(mask);       }     }     for (int mask = 1; mask < up; mask++)       for (int bit = 0; bit < 20; bit++)         if (mask & (1 << bit)) dp[mask] = max(dp[mask], dp[mask ^ (1 << bit)]);     int ans = 0;     for (auto &mask : masks)       ans = max(ans, builtin_popcount(mask) + dp[mask ^ (up - 1)]);     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename A, typename B>   string to_string(pair<A, B> p);   template <typename A, typename B, typename C>   string to_string(tuple<A, B, C> p);   template <typename A, typename B, typename C, typename D>   string to_string(tuple<A, B, C, D> p);   string to_string(const string& s) { return     + s +    ; }   string to_string(const char* s) { return to_string((string)s); }   string to_string(bool b) { return (b ?  true  :  false ); }   string to_string(vector<bool> v) {     bool first = true;     string res =  { ;     for (int i = 0; i < static_cast<int>(v.size()); i++) {       if (!first) {         res +=  ,  ;       }       first = false;       res += to_string(v[i]);     }     res +=  } ;     return res;   }   template <size_t N>   string to_string(bitset<N> v) {     string res =   ;     for (size_t i = 0; i < N; i++) {       res += static_cast<char>( 0  + v[i]);     }     return res;   }   template <typename A>   string to_string(A v) {     bool first = true;     string res =  { ;     for (const auto& x : v) {       if (!first) {         res +=  ,  ;       }       first = false;       res += to_string(x);     }     res +=  } ;     return res;   }   template <typename A, typename B>   string to_string(pair<A, B> p) {     return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ;   }   template <typename A, typename B, typename C>   string to_string(tuple<A, B, C> p) {     return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   +            to_string(get<2>(p)) +  ) ;   }   template <typename A, typename B, typename C, typename D>   string to_string(tuple<A, B, C, D> p) {     return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   +            to_string(get<2>(p)) +  ,   + to_string(get<3>(p)) +  ) ;   }   void debug_out() { cerr << endl; }   template <typename Head, typename... Tail>   void debug_out(Head H, Tail... T) {     cerr <<     << to_string(H);     debug_out(T...);   }   class dsu {    public:     vector<int> p;     int n;     dsu(int _n) : n(_n) {       p.resize(n);       iota(p.begin(), p.end(), 0);     }     inline int get(int x) { return (x == p[x] ? x : (p[x] = get(p[x]))); }     inline bool unite(int x, int y) {       x = get(x);       y = get(y);       if (x != y) {         p[x] = y;         return true;       }       return false;     }   };   const int MAX = 100010;   int bound[MAX];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int tt;     cin >> tt;     assert(1 <= tt && tt <= 1000);     int x = 0;     while (tt--) {       int n;       cin >> n;       assert(n >= 1);       vector<int> v(n);       for (int i = 0; i < n; i++) {         cin >> v[i];       }       sort(v.begin(), v.end());       v.resize(unique(v.begin(), v.end()) - v.begin());       for (int i = 0; i < n - 1; i++) {         assert(v[i] < v[i + 1]);       }       assert(1 <= v[0]);       assert(v[n - 1] <= 100000);       {         int ptr = -1;         for (int val = 0; val < MAX; val++) {           if (ptr + 1 < n && v[ptr + 1] == val) {             ++ptr;           }           bound[val] = ptr;         }       }       vector<vector<int>> who(1, vector<int>(n, -1));       vector<dsu> d(1, dsu(n + 1));       vector<bool> seen;       for (int i = n - 1; i >= 0; i--) {         int start = 0;         int j = 0;         while (start < i) {           if (j == (int)who.size()) {             who.emplace_back(n, -1);             d.emplace_back(n + 1);           }           int finish = i - 1;           int tmp = who[j][i];           if (tmp != -1) {             int val = v[i] - (v[tmp] - v[i]);             finish = (val <= 0 ? -1 : bound[val]);           }           if (start <= finish) {             int pos = d[j].get(start);             while (pos <= finish) {               who[j][pos] = i;               d[j].unite(pos, pos + 1);               pos = d[j].get(pos);             }             start = finish + 1;           }           ++j;         }         int g = 0;         while (g < (int)who.size() && who[g][i] != -1) {           ++g;         }         if (g >= (int)seen.size()) {           seen.resize(g + 1);         }         seen[g] = true;       }       int g = 0;       while (g < (int)seen.size() && seen[g]) {         ++g;       }       x ^= g;       42;     }     cout << (x > 0 ?  YES  :  NO ) <<   n ;     return 0;   }