#include <bits/stdc++.h>   using namespace std;   struct LCT {     int ch[555555][2], f[555555], tag[555555], key[555555], sum[555555],         st[555555], ls[555555], rs[555555];     LCT() {       for (int i = 1; i <= 550000; i++) {         sum[i] = ch[i][0] = ch[i][1] = f[i] = tag[i] = key[i] = 0;       }     }     bool isrt(int x) {       if (!f[x]) return 1;       return (ch[f[x]][0] != x && ch[f[x]][1] != x);     }     int get(int x) { return (ch[f[x]][1] == x); }     void pushup(int x) {       sum[x] = key[x];       sum[x] += sum[ch[x][0]];       sum[x] += sum[ch[x][1]];     }     void push(int x) {       tag[x] ^= 1;       swap(ch[x][0], ch[x][1]);     }     void pushdown(int x) {       if (tag[x]) {         push(ch[x][0]);         push(ch[x][1]);         tag[x] = 0;       }     }     void rotate(int x) {       int fa = f[x], gfa = f[fa];       int wh = get(x);       ch[fa][wh] = ch[x][wh ^ 1];       f[ch[x][wh ^ 1]] = fa;       ch[x][wh ^ 1] = fa;       if (!isrt(fa)) ch[gfa][ch[gfa][1] == fa] = x;       f[fa] = x;       f[x] = gfa;       pushup(fa);     }     void splay(int x) {       int y = x, z = 0;       st[++z] = y;       while (!isrt(y)) {         y = f[y];         st[++z] = y;       }       while (z) pushdown(st[z--]);       for (int fa; (fa = f[x]) && !isrt(x); rotate(x)) {         if (!isrt(fa)) {           if (get(fa) == get(x))             rotate(fa);           else             rotate(x);         }       }       pushup(x);     }     void access(int x) {       for (int y = 0; x; y = x, x = f[x]) {         splay(x);         ch[x][1] = y;         pushup(x);       }     }     void makeroot(int x) {       access(x);       splay(x);       push(x);     }     int findroot(int x) {       access(x);       splay(x);       int pos = x;       while (ch[pos][0]) {         pushdown(pos);         pos = ch[pos][0];       }       splay(pos);       return pos;     }     void split(int x, int y) {       makeroot(x);       access(y);       splay(x);     }     void link(int x, int y) {       makeroot(x);       if (findroot(y) != x) f[x] = y;     }     void cut(int x, int y) {       makeroot(x);       if (findroot(y) != x || f[y] != x || ch[y][0]) return;       ch[x][1] = 0;       f[y] = 0;       pushup(x);     }   } lct;   int n, pa[333333], x, y, deg[333333];   vector<int> g[333333];   queue<int> q;   void dfs(int i, int fa) {     pa[i] = fa;     for (int j = 0; j < g[i].size(); j++) {       int to = g[i][j];       if (to == fa) continue;       dfs(to, i);     }   }   int find(int x) {     int pos = x;     while (pos) {       lct.pushdown(pos);       if (!lct.ch[pos][0] || !lct.sum[lct.ch[pos][0]]) {         if (lct.key[pos]) return pos;         pos = lct.ch[pos][1];       } else         pos = lct.ch[pos][0];     }     return pos;   }   int main() {     scanf( %d , &n);     for (int i = 1; i < n; i++) {       scanf( %d%d , &x, &y);       g[x].push_back(y);       g[y].push_back(x);     }     for (int i = 1; i < n; i++) {       scanf( %d%d , &x, &y);       lct.key[n + i] = 1;       lct.ls[n + i] = x;       lct.rs[n + i] = y;       lct.link(x, n + i);       lct.link(y, n + i);     }     printf( %d n , n - 1);     dfs(1, 0);     for (int i = 2; i <= n; i++) deg[pa[i]]++;     for (int i = 1; i <= n; i++) {       if (!deg[i]) q.push(i);     }     for (int i = 1; i < n; i++) {       x = q.front();       q.pop();       lct.split(x, pa[x]);       y = find(x);       printf( %d %d %d %d n , x, pa[x], lct.ls[y], lct.rs[y]);       lct.cut(y, lct.ls[y]);       lct.cut(y, lct.rs[y]);       lct.ls[y] = x;       lct.rs[y] = pa[x];       lct.link(y, lct.ls[y]);       lct.link(y, lct.rs[y]);       lct.splay(y);       lct.key[y] = 0;       lct.pushup(y);       if (!--deg[pa[x]]) q.push(pa[x]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxS = 1 << 15;   const int mod = 1e9 + 7;   int n;   int a[65];   int d[65];   int s[15], tots;   int t[65], tott;   int in[65], cnt[maxS + 5];   int f[maxS + 5][65];   int ans, st;   namespace usual {   int C[65][65];   int add(int ta, int tb) { return ta + tb >= mod ? ta + tb - mod : ta + tb; }   int sub(int ta, int tb) { return ta < tb ? ta - tb + mod : ta - tb; }   void make_C() {     C[0][0] = 1;     for (int i = 1; i <= n; i++) {       C[i][0] = 1;       for (int j = 1; j <= n; j++) C[i][j] = add(C[i - 1][j], C[i - 1][j - 1]);     }   }   }  // namespace usual   using namespace usual;   namespace funi {   int fn[65];   void make_fn() {     for (int i = 1; i <= n; i++) fn[i] = i;   }   int find(int x) { return fn[x] == x ? x : fn[x] = find(fn[x]); }   void uni(int u, int v) {     int fu = find(u), fv = find(v);     if (u == v) return;     fn[fu] = fv;   }   }  // namespace funi   using namespace funi;   bool issub(int ta, int tb) { return (ta & tb) == ta; }   bool isint(int ta, int tb) { return (ta & tb) > 0; }   void clean() {     tots = tott = 0;     memset(in, 0, sizeof in);     memset(cnt, 0, sizeof cnt);     memset(f, 0, sizeof f);   }   int dp(int fir) {     clean();     for (int i = 1; i <= n; i++) {       if (find(i) != fir) continue;       if (d[i] == 0)         s[++tots] = i;       else         t[++tott] = i;     }     if (tott == 0) return 1;     for (int i = 1; i <= tott; i++)       for (int j = 1; j <= tots; j++)         if (a[t[i]] % a[s[j]] == 0) in[i] |= (1 << (j - 1));     for (int i = 0; i < (1 << (tots + 1 - 1)); i++)       for (int j = 1; j <= tott; j++)         if (issub(in[j], i)) cnt[i]++;     for (int i = 1; i <= tott; i++) f[in[i]][1]++;     for (int S = 0; S < (1 << (tots + 1 - 1)); S++)       for (int k = 1; k < tott; k++) {         if (f[S][k] == 0) continue;         for (int i = 1; i <= tott; i++)           if (!issub(in[i], S) && isint(in[i], S))             f[S | in[i]][k + 1] = add(f[S | in[i]][k + 1], f[S][k]);         f[S][k + 1] = add(f[S][k + 1], 1ll * f[S][k] * (cnt[S] - k) % mod);       }     return f[(1 << (tots + 1 - 1)) - 1][tott];   }   int main() {     scanf( %d , &n);     make_C();     make_fn();     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++)         if (a[i] % a[j] == 0 && i != j) {           uni(i, j);           d[i]++;         }     ans = 1;     for (int i = 1; i <= n; i++)       if (find(i) == i) {         ans = 1ll * ans * dp(i) % mod;         if (tott == 0) continue;         ans = 1ll * ans * C[st + tott - 1][tott - 1] % mod;         st += tott - 1;       }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void maz(T &a, T b) {     if (a < b) a = b;   }   template <class T>   inline void miz(T &a, T b) {     if (a > b) a = b;   }   template <class T>   inline T abs(T a) {     return a > 0 ? a : -a;   }   const int N = 100100, INF = 1e9;   const double EPS = 1e-7;   const int mod = 1e9 + 7;   long long mul(long long a, long long b, long long MOD) { return a * b % MOD; }   long long Pow(long long a, long long x, long long MOD) {     long long res = 1, tmp = a;     while (x) {       if (x & 1) res = mul(res, tmp, MOD);       tmp = mul(tmp, tmp, MOD);       x >>= 1;     }     return res;   }   int main() {     int p;     scanf( %d , &p);     int k;     scanf( %d , &k);     if (k == 0) {       cout << Pow(p, p - 1, mod) << endl;     } else if (k == 1) {       cout << Pow(p, p, mod) << endl;     } else {       long long now = k;       int ct = 1;       while (now != 1) {         now = mul(now, k, p), ct++;       }       cout << Pow(p, (p - 1) / ct, mod) << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b;     cin >> a >> b;     if (b > a) {       swap(a, b);     }     cout << b <<    ;     int r = a - b;     cout << r / 2;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     string s;     cin >> s;     int n = (int)s.size();     vector<vector<int>> a(26, vector<int>(n + 1));     for (int i = 1; i < n + 1; ++i) {       a[s[i - 1] -  a ][i] += 1;     }     for (int i = 0; i < 26; ++i) {       for (int j = 1; j < n + 1; ++j) {         a[i][j] += a[i][j - 1];       }     }     int q;     cin >> q;     while (q--) {       int l, r;       cin >> l >> r;       if (l == r) {         cout <<  Yes  <<   n ;         continue;       }       int cnt = 0;       for (int i = 0; i < 26; ++i) {         cnt += !!(a[i][r] - a[i][l - 1]);       }       if (cnt == 1) {         cout <<  No  <<   n ;         continue;       }       if (cnt == 2) {         cout << (s[l - 1] == s[r - 1] ?  No  :  Yes ) <<   n ;         continue;       }       cout <<  Yes  <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long t[505];   long long p[505][505];   long long dp[505][505];   const long long oo = 4557430888798830399LL;   int main() {     ios_base::sync_with_stdio(0), cin.tie(0);     int n, m, k;     cin >> n >> m >> k;     vector<string> s(n);     for (auto &i : s) cin >> i;     memset(p, 63, sizeof p);     memset(dp, 63, sizeof dp);     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) t[j + 1] = t[j] + s[i][j] -  0 ;       for (int j = 1; j <= m; ++j)         if (s[i][j - 1] ==  1 )           for (int l = j; l <= m; ++l)             if (s[i][l - 1] ==  1 )               p[i][t[m] - t[l] + t[j - 1]] =                   min(p[i][t[m] - t[l] + t[j - 1]], (long long)l - j + 1);       p[i][t[m]] = 0;     }     dp[0][0] = 0;     for (int i = 0; i < n; ++i)       for (int j = 0; j <= k; ++j)         if (dp[i][j] != oo)           for (int l = k - j; l >= 0; --l)             if (p[i][l] != oo)               dp[i + 1][j + l] = min(dp[i + 1][j + l], dp[i][j] + p[i][l]);     long long ans = oo;     for (int i = 0; i <= k; ++i) ans = min(ans, dp[n][i]);     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dp[41][1 << 7][1 << 7], num[1 << 7], vis[3][30], n, m;   void init() {     int i, j, k;     for (i = 0; i <= 40; i++) {       for (j = 0; j < 1 << 7; j++) {         for (k = 0; k < 1 << 7; k++) {           dp[i][j][k] = -11111111;         }       }     }   }   int get_zero(int x) {     int sum = 0, len = 0;     while (x) {       if (x % 2 == 0) sum++;       x /= 2;       len++;     }     return sum + m - len;   }   void gao(int index, int x) {     int ans = 0;     while (x) {       vis[index][ans++] = x % 2;       x /= 2;     }   }   int check(int up, int mid, int down, int a) {     int i, j, flag = 1;     memset(vis, 0, sizeof(vis));     gao(0, up), gao(1, mid), gao(2, down);     for (i = 0; i < m; i++) {       if (vis[1][i] == 1) continue;       if (i + 1 <= m - 1 && vis[1][i + 1] == 1) continue;       if (i - 1 >= 0 && vis[1][i - 1] == 1) continue;       if (vis[0][i] == 1) continue;       if (vis[2][i] == 1) continue;       flag = 0;       break;     }     return flag;   }   int main() {     init();     int up, mid, i, j, k, x;     scanf( %d %d , &n, &m);     if (n < m) swap(n, m);     for (i = 0; i <= (1 << m); i++) {       dp[0][0][i] = 0;       num[i] = get_zero(i);     }     int r = (1 << m);     for (i = 0; i < n; i++) {       for (j = 0; j < r; j++) {         for (k = 0; k < r; k++) {           for (x = 0; x < r; x++) {             if (check(j, k, x, i)) {               dp[i + 1][k][x] = max(dp[i + 1][k][x], dp[i][j][k] + num[k]);             }           }         }       }     }     int maxnum = -1;     for (i = 0; i < r; i++) {       maxnum = max(maxnum, dp[n][i][0]);     }     printf( %d n , maxnum);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-8;   int dcmp(double x) {     if (fabs(x) < eps) return 0;     return (x > 0) ? 1 : -1;   }   const unsigned long long hash1 = 201326611;   const unsigned long long hash2 = 50331653;   const int N = 1000000 + 10;   const int M = 1000 + 10;   const int inf = 0x3f3f3f3f;   const long long mod = 1000000000 + 7;   int n, m, k, sx, sy, ex, ey;   char mp[M][M];   int vis[M][M], dis[M][M];   int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};   struct node {     int x, y;   };   int check(int tx, int ty) {     if (tx < 1 || tx > n || ty < 1 || ty > m) return 0;     if (mp[tx][ty] ==  # ) return 0;     return 1;   }   int bfs() {     queue<pair<int, int> > qu;     qu.push(make_pair(sx, sy));     vis[sx][sy] = (1 << 4) - 1;     dis[sx][sy] = 0;     while (!qu.empty()) {       pair<int, int> now = qu.front();       qu.pop();       if (now.first == ex && now.second == ey) return dis[ex][ey];       for (int i = 0; i < 4; i++) {         int tmp = k, tx = now.first, ty = now.second;         while (tmp--) {           tx += dx[i], ty += dy[i];           if (!check(tx, ty)) break;           if ((vis[tx][ty] >> i) & 1) break;           vis[tx][ty] |= (1 << i);           if (dis[tx][ty] != 0)             dis[tx][ty] = min(dis[tx][ty], dis[now.first][now.second] + 1);           else             dis[tx][ty] = dis[now.first][now.second] + 1;           qu.push(make_pair(tx, ty));         }       }     }     return -1;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0), cout.tie(0);     cin >> n >> m >> k;     for (int i = 1; i <= n; i++) {       cin >> mp[i] + 1;     }     cin >> sx >> sy >> ex >> ey;     cout << bfs() << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, k;   long long pow2(int x) {     if (x == 0) return 1;     if (x == 1) return 2;     long long h = pow2(x / 2);     return (h * h * (x & 1 ? 2 : 1)) % 1000000009;   }   int main() {     cin >> n >> m >> k;     int x = n / k;     int y = n % k;     int z = m - (x * (k - 1) + y);     if (z > 0) {       long long v = ((pow2(z + 1) + 1000000009 - 2 - z) * k) % 1000000009;       long long res = (v + m) % 1000000009;       cout << res << endl;     } else {       cout << m << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 50100;   unsigned long long arr[maxn];   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     unsigned long long s = 0, res = 0;     long long n;     cin >> n;     for (int i = 0; i < n; i++) cin >> arr[i], s += arr[i];     s = s / n;     for (int i = 0; i + 1 < n; i++) {       long long tmp = arr[i] - s;       res += abs(tmp);       arr[i + 1] += tmp;     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool prime[1000009];   vector<long long> pri;   void SieveOfEratosthenes() {     memset(prime, true, sizeof(prime));     for (long long p = 2; p * p <= 1000000; p++) {       if (prime[p] == true) {         for (long long i = p * p; i <= 1000000; i += p) prime[i] = false;       }     }     for (long long i = 2; i <= 1000000; i++) {       if (prime[i]) {         pri.push_back(i);       }     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t;     cin >> t;     SieveOfEratosthenes();     while (t--) {       long long n;       cin >> n;       long long counter = 0;       int i = 0;       long long sz = pri.size();       long long st = 0;       long long end = sz - 1;       long long idx;       if (n == 1) {         printf( 1 n );         continue;       }       while (st < end) {         long long md = st + (end - st) / 2;         if (pri[md] * pri[md] > n) {           end = md;           idx = md;         } else {           st = md + 1;         }       }       st = 0;       end = sz - 1;       long long id = upper_bound(pri.begin(), pri.end(), n) - pri.begin();       id--;       long long ans = id - idx + 2;       printf( %lld n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 105, maxm = 1000005;   const int inf = 0x3f3f3f3f;   int f[maxn][maxn];   int p[maxm], d[maxm], pre[maxm], nxt[maxm];   int main() {     int n, m;     char ch;     cin >> n;     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= n; ++j) {         cin >> ch;         f[i][j] = ch -  0 ;         if (f[i][j] == 0) f[i][j] = inf;       }     }     for (int i = 1; i <= n; ++i) f[i][i] = 0;     for (int k = 1; k <= n; ++k) {       for (int i = 1; i <= n; ++i) {         for (int j = 1; j <= n; ++j) {           f[i][j] = min(f[i][j], min(inf, f[i][k] + f[k][j]));         }       }     }     cin >> m;     nxt[0] = 1, pre[m + 1] = m;     for (int i = 1; i <= m; ++i) {       cin >> p[i];       d[i] = 1;       pre[i] = i - 1;       nxt[i] = i + 1;     }     int cnt = 0;     for (int i = 2; i < m; ++i) {       if (d[pre[i]] + d[i] == f[p[pre[i]]][p[nxt[i]]]) {         d[pre[i]] += d[i];         nxt[pre[i]] = nxt[i];         pre[nxt[i]] = pre[i];         ++cnt;       }     }     cout << m - cnt << endl;     for (int i = nxt[0]; i != m + 1; i = nxt[i]) {       cout << p[i] <<    ;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int a[210000];   long long sum[210000];   long long calc_move(int from, int to) {     return a[from] * 1ll * (to - from) - (sum[to] - sum[from]);   }   long long crossover(int from, int to) {     long long headstart = calc_move(from, to);     if (headstart < 0) return to;     return to + 1 + headstart / (a[to] - a[from]);   }   long long solve() {     long long ret = 0;     deque<int> q;     for (int(i) = 0; (i) < (int)(n); (i)++)       sum[i] = (i == 0 ? 0 : sum[i - 1]) + a[i];     int largest_so_far = -100000000;     for (int(i) = 0; (i) < (int)(n); (i)++) {       if (a[i] > largest_so_far) {         while (q.size() >= 2 && crossover(q[0], q[1]) >= crossover(q[0], i))           q.pop_back();         q.push_back(i);         largest_so_far = a[i];       }       while (q.size() >= 2 && crossover(q[0], q[1]) <= i) q.pop_front();       ret = max(ret, calc_move(q[0], i));     }     return ret;   }   int main() {     long long start_char = 0;     scanf( %d , &n);     for (int(i) = 0; (i) < (int)(n); (i)++) {       scanf( %d , &a[i]);       start_char += (i + 1) * 1ll * a[i];     }     long long best_delta = solve();     reverse(a, a + n);     for (int(i) = 0; (i) < (int)(n); (i)++) a[i] = -a[i];     best_delta = max(best_delta, solve());     printf( %lld n , start_char + best_delta);   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   inline int Max(int a, int b) { return a > b ? a : b; }   const int maxn = 2e5 + 20;   int maxy[maxn << 2], mark[maxn], x[maxn], y[maxn];   set<int> s[maxn];   int n, m;   char op[20];   vector<int> v;   void build(int l, int r, int rt) {     if (l == r) {       maxy[rt] = -1;       return;     } else {       int mid = (l + r) >> 1;       build(l, mid, rt << 1);       build(mid + 1, r, rt << 1 | 1);     }   }   void pushup(int rt) { maxy[rt] = Max(maxy[rt << 1], maxy[rt << 1 | 1]); }   void update(int pos, int val, int l, int r, int rt) {     if (l == r) {       maxy[rt] = val;       return;     }     int mid = (l + r) >> 1;     if (pos <= mid)       update(pos, val, l, mid, rt << 1);     else       update(pos, val, mid + 1, r, rt << 1 | 1);     pushup(rt);   }   int query(int L, int R, int val, int l, int r, int rt) {     if (maxy[rt] <= val) return -1;     if (l == r) return l;     int mid = (l + r) >> 1;     int ans = -1;     if (L <= mid && maxy[rt << 1] > val) {       ans = query(L, R, val, l, mid, rt << 1);       if (ans != -1) return ans;     }     if (R > mid && maxy[rt << 1 | 1] > val)       ans = query(L, R, val, mid + 1, r, rt << 1 | 1);     return ans;   }   int main() {     scanf( %d , &n);     for (int i = (0); i < (n); i++) {       scanf( %s%d%d , op, &x[i], &y[i]);       if (op[0] ==  a )         mark[i] = 1;       else if (op[0] ==  r )         mark[i] = 2;       else         mark[i] = 3;       if (op[0] ==  a ) v.push_back(x[i]);     }     sort(v.begin(), v.end());     v.erase(unique(v.begin(), v.end()), v.end());     m = v.size();     for (int i = (0); i < (n); i++) {       if (mark[i] == 1 || mark[i] == 2) {         int pos = lower_bound(v.begin(), v.end(), x[i]) - v.begin() + 1;         if (mark[i] == 1)           s[pos].insert(y[i]);         else           s[pos].erase(y[i]);         int val;         if (s[pos].empty())           val = -INF;         else           val = *(--s[pos].end());         update(pos, val, 1, m, 1);       } else {         int pos = upper_bound(v.begin(), v.end(), x[i]) - v.begin() + 1;         int z = query(pos, m, y[i], 1, m, 1);         if (z == -1 || pos == m + 1) {           puts( -1 );           continue;         }         int ans = *(s[z].upper_bound(y[i]));         printf( %d %d n , v[z - 1], ans);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int32_t main() {     long long q;     cin >> q;     for (long long i = 0; i < q; i++) {       long long a, b, c;       cin >> a >> b >> c;       long long ans = 0;       long long num = min(min(a, b), c);       ans += num;       a -= num;       b -= num;       c -= num;       if (a > b) {         swap(a, b);       }       num = min(a, (a + b) / 3);       ans += num;       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   int main() {     cin.tie(0), cout.tie(0);     ios::sync_with_stdio(false);     int t;     cin >> t;     while (t--) {       int n, l, r, now = 1;       cin >> n;       for (int i = 0; i < n; i++) {         cin >> l >> r;         if (now < l) {           now = l;         }         if (now <= r) {           cout << now <<    ;           now++;         } else {           cout << 0 <<    ;         }       }       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   int nd, na;   int nm;   int atq[124], def[124], meu[124];   int foi[124];   int killAll() {     memset(foi, 0, sizeof(foi));     for (int i = 0; i < nd; i++) {       int j = 0;       while (j < nm && (meu[j] <= def[i] || foi[j] == 1)) j++;       if (j == nm)         return 0;       else         foi[j] = 1;     }     int ans = 0;     for (int i = 0; i < na; i++) {       int j = 0;       while (j < nm && (meu[j] < atq[i] || foi[j])) j++;       if (j == nm) return 0;       if (meu[j] >= atq[i] && !foi[j]) {         ans += meu[j] - atq[i];         foi[j] = 1;       }     }     for (int j = 0; j < nm; j++)       if (!foi[j]) ans += meu[j];     return ans;   }   int killAtq() {     int me = min(nm, na);     int ans = 0;     for (int k = 1; k <= me; k++) {       int j = nm - k;       int aux = 0;       for (int i = 0; i < k; i++, j++) {         if (meu[j] >= atq[i]) {           aux += meu[j] - atq[i];         }       }       ans = max(ans, aux);     }     return ans;   }   int main() {     char s[124];     scanf(  %d %d , &n, &m);     nd = na = nm = 0;     int p;     for (int i = 0; i < n; i++) {       scanf(  %s %d , s, &p);       if (!strcmp( ATK , s)) atq[na++] = p;       if (!strcmp( DEF , s)) def[nd++] = p;     }     for (int i = 0; i < m; i++) {       scanf(  %d , &p);       meu[nm++] = p;     }     sort(atq, atq + na);     sort(def, def + nd);     sort(meu, meu + nm);     printf( %d n , max(killAll(), killAtq()));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int power(long long int x, long long int n) {     if (n == 0)       return 1;     else if (n % 2 == 0)       return power(x * x, n / 2);     else       return x * power(x * x, (n - 1) / 2);   }   long long int highestPowerof2(long long int n) {     long long int p = (long long int)log2(n);     return (long long int)pow(2, p);   }   long long int powermod(long long int x, long long int y, long long int p) {     long long int res = 1;     x = x % p;     if (x == 0) return 0;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int n, m, i, j, k, count;     bool ok = false;     cin >> n >> m;     long long int a[n][m];     long long int arr[n][m];     memset(arr, -1, sizeof(arr));     for (i = 0; i < n; i++) {       for (j = 0; j < m; j++) cin >> a[i][j];     }     for (i = 0; i < n; i++) {       for (j = 0; j < m; j++) {         if (a[i][j] == 0) {           for (k = 0; k < n; k++) arr[k][j] = 0;           for (k = 0; k < m; k++) arr[i][k] = 0;         }       }     }     for (i = 0; i < n; i++) {       for (j = 0; j < m; j++) {         if (arr[i][j] == -1) arr[i][j] = 1;       }     }     for (i = 0; i < n; i++) {       for (j = 0; j < m; j++) {         count = 0;         if (a[i][j] == 1) {           for (k = 0; k < n; k++)             if (arr[k][j] == 1) count++;           for (k = 0; k < m; k++)             if (arr[i][k] == 1) count++;           if (count == 0) {             ok = true;             break;           }         } else if (a[i][j] == 0) {           for (k = 0; k < n; k++)             if (arr[k][j] == 1) count++;           for (k = 0; k < m; k++)             if (arr[i][k] == 1) count++;           if (count != 0) {             ok = true;             break;           }         }       }       if (ok) break;     }     if (ok)       cout <<  NO  << endl;     else {       cout <<  YES  << endl;       for (i = 0; i < n; i++) {         for (j = 0; j < m; j++) {           cout << arr[i][j] <<    ;         }         cout << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 5;   const int INF = 1e9 + 7;   vector<int> adjList[MAXN];   vector<int> vec;   bool visited[MAXN];   void dfs(int start) {     visited[start] = true;     for (auto u : adjList[start]) {       if (!visited[u]) {         dfs(u);       }     }     vec.push_back(start);   }   int main() {     int n, m;     cin >> n >> m;     for (int i = 0; i < m; ++i) {       int c, d;       cin >> c >> d;       adjList[c].push_back(d);     }     for (int i = 1; i <= n; ++i) {       if (!visited[i]) {         vec.clear();         dfs(i);         for (auto u : vec) {           cout << u <<    ;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int x = 1, y = 1;     int k = 0;     cin >> k;     bool fl = false;     for (int i = 1; i <= n; i++) {       int t;       if (i != n)         cin >> t;       else         t = 1 - k;       if (t == k && fl == false) {         x++;       } else if (t != k) {         if (y != x && fl == true) {           cout <<  NO ;           return 0;         }         fl = true;         y = 1;         k = t;       } else if (t == k) {         y++;       }     }     cout <<  YES ;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 200 + 5;   int mp[maxn][maxn], dp[maxn][maxn], n, m;   int main() {     int t;     scanf( %d , &t);     while (t--) {       memset(dp, 0, sizeof dp);       scanf( %d%d , &n, &m);       for (int i = 1; i <= n; i++)         for (int j = 1; j <= m; j++) scanf( %d , &mp[i][j]);       for (int i = 1; i <= m; i++) {         memset(dp[i & 1], 0, sizeof dp[i & 1]);         for (int k = 0; k < n; k++) {           for (int s = 0; s < (1 << n); s++) {             int tmp = 0;             for (int j = 1; j <= n; j++) {               if (s & (1 << (j - 1))) tmp += mp[j][i];             }             for (int x = 0; x < (1 << n); x++) {               if (!(s & x))                 dp[i & 1][s | x] = max(dp[i & 1][s | x], dp[!(i & 1)][x] + tmp);             }           }           for (int j = 1; j <= n; j++) mp[j - 1][i] = mp[j][i];           mp[n][i] = mp[0][i];         }       }       printf( %d n , dp[m & 1][(1 << n) - 1]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[654321];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n, k;     cin >> n >> k;     int a, b, c, d;     cin >> a >> b >> c >> d;     map<int, int> mapka;     mapka[1] = a;     mapka[2] = c;     mapka[n - 1] = d;     mapka[n] = b;     set<int> st;     for (int i = 1; i <= n; ++i) st.insert(i);     st.erase(a);     st.erase(b);     st.erase(c);     st.erase(d);     if (n == 4 || k <= n) {       cout << -1;       return 0;     }     int cc = 3;     for (set<int>::iterator it = st.begin(); it != st.end(); ++it)       mapka[cc++] = *it;     for (int i = 1; i <= n; ++i) cout << mapka[i] <<    ;     cout << endl;     cout << mapka[2] <<     << mapka[1] <<    ;     for (int i = 3; i <= n - 2; ++i) cout << mapka[i] <<    ;     cout << mapka[n] <<     << mapka[n - 1] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 1e9;   template <class T>   int getbit(T x, int pos) {     return (x >> (pos - 1)) & 1;   }   template <class T>   void turn_on(T &x, int pos) {     x = x | ((T)1 << (pos - 1));   }   template <class T>   void turn_off(T &x, int pos) {     x = x & ~((T)1 << (pos - 1));   }   template <class T>   T sqr(T a) {     return a * a;   }   int main() {     int n;     scanf( %d , &n);     long long g = 0;     for (int i = 1; i <= n; ++i) {       long long x = 0;       scanf( %I64d , &x);       if (x < 4)         g ^= 0;       else if (x < 16)         g ^= 1;       else if (x < 82)         g ^= 2;       else if (x < 6723)         g ^= 0;       else if (x < 50626)         g ^= 3;       else if (x < 2562991876)         g ^= 1;       else         g ^= 2;     }     if (g == 0)       printf( Rublo );     else       printf( Furlo );   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 100;   struct Node {     int a, b, id;     Node() {}     Node(int a, int b, int id) : a(a), b(b), id(id) {}     bool operator<(const Node& th) const { return a < th.a; }   } node[N];   int vis[N];   int main() {     int n, m;     while (scanf( %d%d , &n, &m) != EOF) {       for (int i = 0; i < m; i++) {         int a, b;         scanf( %d%d , &a, &b);         b += a - 1;         node[i] = Node(a, b, i);       }       sort(node, node + m);       int l = 0;       int i = 0;       memset(vis, 0, sizeof(vis));       int cnt = 0;       while (i < m) {         l = max(l, node[i].a - 1);         int r = node[i].b;         int id = i;         while (i < m && node[i].a <= l + 1) {           if (node[i].b > r) {             id = i;             r = node[i].b;           }           i++;         }         if (r > l) {           cnt++;           vis[node[id].id] = 1;         }         l = max(l, r);       }       cnt = m - cnt;       printf( %d n , cnt);       for (int i = 0; i < m; i++)         if (!vis[i]) {           cnt--;           printf( %d%c , i + 1, cnt == 0 ?   n  :    );         }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1000 * 100;   int n;   pair<int, int> sora[maxn + 100];   int lpos = 0;   bool seen[maxn + 100];   bool comp(pair<int, int> a, pair<int, int> b) {     if (a.first < b.first)       return true;     else {       if (a.first == b.first)         return a.second < b.second;       else         return false;     }   }   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       int temp;       scanf( %d , &temp);       sora[i] = make_pair(temp, i);     }     sort(sora, sora + n, comp);     int co = 0;     for (int i = 0; i < n; i++) {       seen[sora[i].second] = true;       while (lpos < n && seen[lpos]) lpos++;       if (lpos > i || lpos == n) {         co++;       }     }     cout << co << endl;   }


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1.0);   double fRand(double fMin, double fMax) {     double f = (double)rand() / RAND_MAX;     return fMin + f * (fMax - fMin);   }   template <class T>   T min(T a, T b, T c) {     return min(a, min(b, c));   }   template <class T>   T max(T a, T b, T c) {     return max(a, max(b, c));   }   struct matrix {     vector<vector<int> > a;     int m, n;     matrix(int _m, int _n) {       m = _m;       n = _n;       a.resize(m);       for (int i = (0); i <= (m - 1); ++i) a[i].resize(n);     }     bool inline operator==(const matrix &o) const {       if (m != o.m || n != o.n) return false;       for (int i = (0); i <= (m - 1); ++i) {         for (int j = (0); j <= (n - 1); ++j) {           if (a[i][j] != o.a[i][j]) return false;         }       }       return true;     }     void inline operator=(const matrix &o) {       m = o.m;       n = o.n;       for (int i = (0); i <= (m - 1); ++i) {         for (int j = (0); j <= (n - 1); ++j) a[i][j] = o.a[i][j];       }     }     matrix inline operator*(const matrix &o) const {       matrix c(m, o.n);       for (int i = (0); i <= (m - 1); ++i) {         for (int j = (0); j <= (o.n - 1); ++j) {           c.a[i][j] = 0;           for (int k = (0); k <= (n - 1); ++k)             c.a[i][j] = (c.a[i][j] + 1LL * a[i][k] * o.a[k][j]) % 1000000007;         }       }       return c;     }     void print() {       for (int i = (0); i <= (m - 1); ++i) {         for (int j = (0); j <= (n - 1); ++j) printf( %d  , a[i][j]);         puts(  );       }       puts(  );     }   };   matrix Identity(int n) {     matrix I(n, n);     for (int i = (0); i <= (n - 1); ++i) I.a[i][i] = 1;     return I;   }   matrix matPow(matrix A, int n) {     if (n == 0) return Identity(A.m);     if (n % 2 == 1) return A * matPow(A, n - 1);     matrix B = matPow(A, n / 2);     return B * B;   }   int n, l, M, c[3][1000005], dpp[105], last[105];   int main() {     scanf( %d%d%d , &n, &l, &M);     for (int i = (0); i <= (2); ++i)       for (int j = (1); j <= (n); ++j) scanf( %d , &c[i][j]);     matrix A(1, M);     for (int i = (1); i <= (n); ++i) ++A.a[0][c[0][i] % M];     for (int i = (1); i <= (n); ++i) ++dpp[c[1][i] % M];     matrix B(M, M);     for (int i = (0); i <= (M - 1); ++i)       for (int j = (0); j <= (M - 1); ++j) B.a[i][j] = dpp[(j - i + M) % M];     matrix C = A * matPow(B, l - 2);     for (int i = (1); i <= (n); ++i) {       ++last[(c[1][i] + c[2][i]) % M];     }     int ans = 0;     for (int i = (0); i <= (M - 1); ++i) {       ans = (ans + 1LL * last[i] * C.a[0][(M - i) % M]) % 1000000007;     }     printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a[5005], b[5005], ans[5005], s, q[10005], x[10005];   map<int, int> m;   void ask(int x, int y) {     printf( ? %d %d n , x, y);     fflush(stdout);   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) ask(i, 0), scanf( %d , a + i), x[a[i]] = i;     for (int i = 0; i < n; i++) ask(x[0], i), scanf( %d , b + i);     for (int i = 0; i < n; i++) {       memset(q, -1, sizeof(q));       for (int j = 0; j < n; j++) q[a[j] ^ i] = j;       bool ok = 1;       for (int j = 0; j < n; j++)         if (q[j] != (b[j] ^ i)) ok = 0, j = n;       if (ok) ans[++s] = i;     }     printf( ! n );     printf( %d n , s);     s = ans[1];     for (int i = 0; i < n - 1; i++) printf( %d  , a[i] ^ s);     printf( %d n , a[n - 1] ^ s);     fflush(stdout);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int32_t main() {     long long n, k;     cin >> n >> k;     vector<long long> a(n);     vector<long long> b(n);     vector<long long> c(n);     long long sum1 = 0;     for (long long i = 0; i < n; i++) {       cin >> a[i];     }     for (long long i = 0; i < n; i++) {       cin >> b[i];       sum1 += b[i];     }     for (long long i = 0; i < n; i++) {       c[i] = b[i] - a[i];     }     sort(c.begin(), c.end());     reverse(c.begin(), c.end());     long long sum2 = 0;     for (long long i = 0; i < k; i++) {       sum2 += c[i];     }     for (long long i = k; i < n; i++) {       if (c[i] > 0) sum2 += c[i];     }     cout << sum1 - sum2 << endl;   }


#include <bits/stdc++.h>   using namespace std;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   const long long MOD = (long long)1e9 + 7;   long long add(long long x, long long y) {     x += y;     if (x >= MOD) return x - MOD;     return x;   }   long long sub(long long x, long long y) {     x -= y;     if (x < 0) return x + MOD;     return x;   }   long long mult(long long x, long long y) { return (x * y) % MOD; }   const int N = (int)1e6 + 7;   long long f[N], rf[N];   long long pw1[N], pw2[N];   int n, m;   int main() {     rf[0] = rf[1] = 1;     for (long long x = 2; x < N; x++) rf[x] = sub(0, mult(MOD / x, rf[MOD % x]));     for (int i = 2; i < N; i++) rf[i] = mult(rf[i - 1], rf[i]);     f[0] = 1;     for (int i = 1; i < N; i++) f[i] = mult(f[i - 1], i);     scanf( %d%d , &n, &m);     pw1[0] = pw2[0] = 1;     for (int i = 1; i < N; i++) {       pw1[i] = mult(pw1[i - 1], n);       pw2[i] = mult(pw2[i - 1], m);     }     long long ans = 0;     for (int k = 1; k < n - 1 && k <= m; k++) {       long long cur = k + 1;       cur = mult(cur, pw1[n - k - 2]);       cur = mult(cur, f[n - 2]);       cur = mult(cur, rf[n - k - 1]);       cur = mult(cur, f[m - 1]);       cur = mult(cur, rf[k - 1]);       cur = mult(cur, rf[m - k]);       cur = mult(cur, pw2[n - 1 - k]);       ans = add(ans, cur);     }     if (n - 1 <= m) {       long long cur = f[n - 2];       cur = mult(cur, f[m - 1]);       cur = mult(cur, rf[n - 2]);       cur = mult(cur, rf[m - n + 1]);       ans = add(ans, cur);     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 55;   const int maxm = 2e4 + 5;   int a[maxn][maxm], dp[maxm], nxt[maxm], sum[maxn][maxm];   int pre[maxm], suff[maxm];   int n, m, k;   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cin >> n >> m >> k;     for (int i = 0; i <= n - 1; i++)       for (int j = 0; j <= m - 1; j++) cin >> a[i][j];     for (int i = 0; i <= n - 1; i++)       for (int j = 0; j <= k - 1; j++) sum[i][0] += a[i][j];     for (int i = 0; i <= n - 1; i++)       for (int j = 1; j <= m - k; j++)         sum[i][j] = sum[i][j - 1] + a[i][j + k - 1] - a[i][j - 1];     for (int i = 0; i <= m - k; i++) nxt[i] = sum[0][i] + sum[1][i];     for (int i = 1; i <= n - 1; i++) {       for (int j = 0; j <= m - k; j++) dp[j] = nxt[j], nxt[j] = 0;       pre[0] = dp[0];       for (int i = 1; i <= m - k; i++) pre[i] = max(pre[i - 1], dp[i]);       suff[m - k] = dp[m - k];       for (int i = m - k - 1; i >= 0; i--) suff[i] = max(suff[i + 1], dp[i]);       for (int j = 0; j <= m - k; j++) {         if (j - k >= 0) nxt[j] = max(pre[j - k], nxt[j]);         if (j + k <= m - k) nxt[j] = max(suff[j + k], nxt[j]);       }       int tmp = 0;       deque<pair<int, int> > que;       for (int j = 0; j <= m - k; j++) {         int now = dp[j] - tmp - sum[i][j];         while (que.size() && que.front().first < now) que.pop_front();         que.push_front(pair<int, int>(now, j));         if (j - k >= 0 && que.size() && que.back().second == j - k)           que.pop_back();         nxt[j] = max(nxt[j], que.back().first + tmp);         tmp += a[i][j];       }       tmp = 0;       que.clear();       for (int j = m - k; j >= 0; j--) {         int now = dp[j] - tmp - sum[i][j];         while (que.size() && que.front().first < now) que.pop_front();         que.push_front(pair<int, int>(now, j));         if (j + k <= m - k && que.size() && que.back().second == j + k)           que.pop_back();         nxt[j] = max(nxt[j], que.back().first + tmp);         tmp += a[i][j + k - 1];       }       for (int j = 0; j <= m - k; j++) nxt[j] += sum[i][j] + sum[i + 1][j];     }     int res = 0;     for (int i = 0; i <= m - k; i++) res = max(res, nxt[i]);     cout << res <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   char a[51][51], b[51][51];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int ax, ay, bx, by, xx, yy, s = 0, ans = 0;     cin >> ax >> ay;     for (int i = 1; i <= ax; i++) {       for (int j = 1; j <= ay; j++) cin >> a[i][j];     }     cin >> bx >> by;     for (int i = 1; i <= bx; i++) {       for (int j = 1; j <= by; j++) cin >> b[i][j];     }     for (int x = -50; x <= 50; x++) {       for (int y = -50; y <= 50; y++) {         s = 0;         for (int i = 1; i <= ax; i++) {           for (int j = 1; j <= ay; j++) {             if (((i + x >= 1) && (i + x <= bx)) &&                 ((j + y >= 1) && (j + y <= by))) {               s += (a[i][j] -  0 ) * (b[i + x][j + y] -  0 );             }           }         }         if (s >= ans) {           ans = s;           xx = x;           yy = y;         }       }     }     cout << xx <<     << yy;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void sol() {     int n, m, k;     cin >> n >> m >> k;     int a[n + 1][m + 1];     for (int i = 1; i <= n; i++)       for (int ii = 1; ii <= m; ii++) cin >> a[i][ii];     for (int i = 1; i <= k; i++) {       int x, y;       cin >> y;       x = 1;       while (x != n + 1) {         if (a[x][y] == 1)           a[x][y] = 2, y++;         else if (a[x][y] == 2)           x++;         else           a[x][y] = 2, y--;       }       cout << y <<    ;     }     cout <<   n ;   }   int main() {     std::ios_base::sync_with_stdio(0);     std::cin.tie(0);     std::cout.tie(0);     ;     int tc = 1;     while (tc--) sol();     return 0;   }


#include<bits/stdc++.h>   using namespace std;      #define pii             pair<int,int>   #define vi              vector<int>   #define mii             map<int,int>   #define pqs             priority_queue<int,vi,greater<int> >   #define mod             1000000007   #define int             long long   #define pb              push_back   #define inf             1e18   #define sp(x,y)         fixed<<setprecision(y)<<x   #define mk(arr,n,type)  type *arr=new type[n];   #define w(t)            int t; cin>>t; while(t--)   #define mp              make_pair   #define endl              n    #define pqb             priority_queue<int>   #define ceil(a , b)     ((a / b) + (a%b && 1))   #define rep(i,a,b)      for(int i=a;i<(int)b;i++)   #define repr(i,a,b)     for(int i=a;i>=(int)b;i--)   #define fastio          ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);   #define setbits(x)      __builtin_popcountll(x)   #define zrobits(x)      __builtin_ctzll(x)   #define all(v)          v.begin(), v.end()   #define ff              first   #define ss              second   template<typename... T> void pn(T... args) { ((cout << args <<    ), ...); cout<<  n ; }   template<typename... T> void ps(T... args) { ((cout << args <<    ), ...); cout<<  ; }   // void start()   // {   //     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);   // #ifndef ONLINE_JUDGE   //     freopen( input.txt ,  r , stdin);   //     freopen( output.txt ,  w , stdout);   // #endif   // }   int power(int a, int b,int P=mod) {       int res = 1;       for (; b; b >>= 1, a = 1ll * a * a % P)           if (b & 1)               res = 1ll * res * a % P;       return res;   }      void solve()   {       int n;       cin>>n;       int arr[n];       int sum=0;       vector<int>temp;       int ans=n-1;       for(int i=0; i<n; i++)       {           cin>>arr[i];           sum+=arr[i];           temp.pb(sum);       }       for(auto j:temp)       {           int i=0,cnt=0;           sum=0;           while(i<n)           {               sum+=arr[i];               if(sum==j)               {                   cnt++;                   sum=0;               }               else if(sum>j)               {                   break;               }               i++;           }           if(i==n&&sum==0)           {               ans=min(ans,n-cnt);           }       }       cout<<ans<<endl;      }   int32_t main()   {       fastio;       w(t)       {           solve();       }          return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = (long long)1e9 + 7;   const long long MOD1 = 2286661337;   const long long MOD2 = 998244353;   const int INF = (int)1e9 + 7;   const double EPS = 1e-7;   const int N = (int)2e5;   int n;   long long a[N], b[N];   bool is_same_() {     for (int i = 0; i < n; ++i) {       if (a[i] != b[i]) {         return false;       }     }     return true;   }   bool is_up() {     for (int i = 1; i < n; ++i) {       if (b[i] <= b[i - 1]) {         return false;       }     }     return true;   }   bool is_down() {     for (int i = 1; i < n; ++i) {       if (b[i] >= b[i - 1]) {         return false;       }     }     return true;   }   bool is_bad() {     for (int i = 0; i < n; ++i) {       if (b[i] <= 0) {         return true;       }     }     return false;   }   bool is_rev_same() {     for (int i = 0; i < n; ++i) {       if (a[i] != b[n - 1 - i]) {         return false;       }     }     return true;   }   bool is_uncomp() {     for (int i = 0; i < n; ++i) {       if (a[i] > b[i]) {         return true;       }     }     return false;   }   void solve() {     int cnt = 0;     string ans;     int prev_ = -1;     while (!is_same_()) {       if (is_rev_same()) {         ans.push_back( R );         break;       }       if (is_bad()) {         cnt = -1;         break;       }       if (is_up()) {         for (int i = n - 1; i >= 0; --i) {           b[i] = (i ? b[i] - b[i - 1] : b[i]);         }         if (prev_ != 0) {           if (prev_ != -1) {             ans.push_back( R );           }           prev_ = 0;         }         ans.push_back( P );       } else if (is_down()) {         for (int i = 0; i < n; ++i) {           b[i] = (i == n - 1 ? b[i] : b[i] - b[i + 1]);         }         if (prev_ != 1) {           ans.push_back( R );           prev_ = 1;         }         ans.push_back( P );       } else {         cnt = -1;         break;       }       ++cnt;     }     if (prev_ == 1) {       ans.push_back( R );     }     reverse(ans.begin(), ans.end());     if (cnt == -1) {       printf( IMPOSSIBLE );     } else if (cnt > N) {       printf( BIG n%d , cnt);     } else {       printf( SMALL n%d n%s , ans.size(), ans.c_str());     }   }   signed main() {     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       scanf( %lld , &a[i]);     }     for (int i = 0; i < n; ++i) {       scanf( %lld , &b[i]);     }     if (n == 1) {       if (a[0] == b[0]) {         printf( SMALL n0 );       } else {         printf( IMPOSSIBLE );       }     } else if (n == 2) {       long long cnt = 0;       int prev_ = -1;       string ans =   ;       while (!is_same_()) {         if (is_rev_same()) {           ans.push_back( R );           break;         }         if (is_uncomp()) {           ans.push_back( R );           reverse(b, b + n);           if (is_uncomp()) {             cnt = -1;             break;           }         }         long long x = b[0], y = b[1];         if (x <= 0 || y <= 0) {           cnt = -1;           break;         }         if (x < y) {           if (prev_ != 0) {             if (prev_ != -1) {               ans.push_back( R );             }             prev_ = 0;           }           long long cur_cnt = y / x;           if (x == a[0]) {             cur_cnt = min(cur_cnt, (y - a[1] + x - 1) / x);           }           if (ans.size() + cur_cnt <= N) {             for (int i = 0; i < cur_cnt; ++i) {               ans.push_back( P );             }           }           cnt += cur_cnt;           y -= x * cur_cnt;         } else if (x > y) {           if (prev_ != 1) {             ans.push_back( R );             prev_ = 1;           }           long long cur_cnt = x / y;           if (y == a[1]) {             cur_cnt = min(cur_cnt, (x - a[0] + y - 1) / y);           }           if (ans.size() + cur_cnt <= N) {             for (int i = 0; i < cur_cnt; ++i) {               ans.push_back( P );             }           }           cnt += cur_cnt;           x -= y * cur_cnt;         } else {           cnt = -1;           break;         }         b[0] = x, b[1] = y;       }       if (prev_ == 1) {         ans.push_back( R );       }       reverse(ans.begin(), ans.end());       if (cnt == -1) {         printf( IMPOSSIBLE );       } else if (cnt > N) {         printf( BIG n%lld , cnt);       } else {         printf( SMALL n%d n%s , ans.size(), ans.c_str());       }     } else {       solve();     }     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:268435456,268435456 )   using namespace std;   int above[41][41], dp[41][41][41][41];   char s[42];   int main() {     int n, m, q, s1, s2, s3, s4;     scanf( %d %d %d , &n, &m, &q);     for (int i = 1; i <= n; i++) {       scanf( %s , s + 1);       for (int j = 1; j <= m; j++)         if (s[j] ==  0 ) above[i][j] += above[i - 1][j] + 1;     }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         for (int k = i; k <= n; k++)           for (int l = j; l <= m; l++) {             dp[i][j][k][l] =                 dp[i][j][k - 1][l] + dp[i][j][k][l - 1] - dp[i][j][k - 1][l - 1];             int temp = min(above[k][l], k - i + 1);             dp[i][j][k][l] += temp;             for (int z = l - 1; z >= j; z--) {               temp = min(temp, above[k][z]);               if (temp == 0)                 break;               else                 dp[i][j][k][l] += temp;             }           }     for (int i = 0; i < q; i++)       scanf( %d %d %d %d , &s1, &s2, &s3, &s4),           printf( %d n , dp[s1][s2][s3][s4]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   vector<int> vec[300000];   int mn(int x);   int mx(int x) {     if (!vec[x].size()) return 0;     int ret = n;     for (int i = (0); i < (vec[x].size()); i++) ret = min(ret, mn(vec[x][i]));     return ret;   }   int mn(int x) {     if (!vec[x].size()) return 0;     int ret = 0;     for (int i = (0); i < (vec[x].size()); i++) ret += mx(vec[x][i]) + 1;     return ret - 1;   }   int main() {     scanf( %d , &n);     for (int i = (0); i < (n - 1); i++) {       int x, y;       scanf( %d%d , &x, &y);       x--;       y--;       vec[x].push_back(y);     }     int leaf = 0;     for (int i = (0); i < (n); i++)       if (!vec[i].size()) leaf++;     printf( %d %d n , leaf - mx(0), 1 + mn(0));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int T, n, m, sa[40010], sb[40010], ls, du[201], ans;   bool g[201][201];   int getint() {     int w = 0;     bool q = 0;     char c = getchar();     while ((c >  9  || c <  0 ) && c !=  - ) c = getchar();     if (c ==  - ) c = getchar(), q = 1;     while (c >=  0  && c <=  9 ) w = w * 10 + c -  0 , c = getchar();     return q ? -w : w;   }   void dfs(int u) {     while (du[u]) {       for (int i = 1; i <= n; i++)         if (g[u][i]) {           ls++;           sa[ls] = u;           sb[ls] = i;           du[u]--, du[i]--;           g[u][i] = g[i][u] = 0;           u = i;           break;         }     }   }   int main() {     T = getint();     while (T--) {       ans = n = getint();       m = getint();       for (int i = 1, x, y; i <= m; i++) {         x = getint();         y = getint();         g[x][y] = g[y][x] = 1;         du[x]++;         du[y]++;       }       for (int i = 1; i <= n; i++) ans -= du[i] & 1;       printf( %d n , ans);       for (int i = 1; i <= n; i++)         if (du[i] & 1)           while (du[i]) dfs(i);       for (int i = 1; i <= n; i++)         while (du[i]) dfs(i);       for (int i = 1; i <= ls; i++) printf( %d %d n , sa[i], sb[i]);       ls = 0;     }   }


#include <bits/stdc++.h>   using namespace std;   long long cross(pair<long long, long long> a, pair<long long, long long> b,                   pair<long long, long long> c) {     return abs((b.first - a.first) * (c.second - a.second) -                (b.second - a.second) * (c.first - a.first));   }   vector<pair<long long, long long> > P;   int n;   long long s;   int main() {     cin >> n >> s;     for (int i = 1; i <= n; i++) {       pair<long long, long long> p;       cin >> p.first >> p.second;       P.push_back(p);     }     sort(P.begin(), P.end());     pair<long long, long long> a, b, c;     a = P[0], b = P[1], c = P[2];     long long ans = cross(a, b, c);     int flag = 1;     while (flag) {       flag = 0;       for (int i = 0; i < P.size(); i++) {         long long tmp = cross(P[i], b, c);         if (tmp > ans) {           ans = tmp;           a = P[i];           flag = 1;         }       }       for (int i = 0; i < P.size(); i++) {         long long tmp = cross(a, P[i], c);         if (tmp > ans) {           ans = tmp;           b = P[i];           flag = 1;         }       }       for (int i = 0; i < P.size(); i++) {         long long tmp = cross(a, b, P[i]);         if (tmp > ans) {           ans = tmp;           c = P[i];           flag = 1;         }       }     }     cout << a.first + b.first - c.first <<     << a.second + b.second - c.second          << endl;     cout << a.first + c.first - b.first <<     << a.second + c.second - b.second          << endl;     cout << b.first + c.first - a.first <<     << b.second + c.second - a.second          << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     s +=  0 ;     int c = 1;     char sc = s[0];     bool xd = false;     string res =   ;     for (int i = 1; i < s.size(); i++) {       if (sc != s[i]) {         if (c == 1) {           res += sc;           xd = false;         } else if (c >= 2 && xd) {           xd = false;           res += sc;         } else {           xd = true;           res += sc;           res += sc;         }         c = 1;         sc = s[i];       } else         c++;     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int64_t mod = 1e9 + 7;   int64_t mod1 = 1e9 + 6;   int64_t power(int64_t a, int64_t b) {     if (b == 0)       return 1;     else if (b % 2 == 0)       return power((((a % mod) * (a % mod)) % mod), b / 2) % mod;     else       return ((a % mod) * (power((((a % mod) * (a % mod)) % mod), b / 2) % mod)) %              mod;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int64_t n;     cin >> n;     int64_t a[n + 1], b[n + 1], c[n + 1], d[n + 1], e[n + 1];     for (int64_t i = 1; i <= n; ++i) cin >> a[i] >> b[i] >> c[i] >> d[i] >> e[i];     if (n <= 2) {       cout << n <<   n ;       for (int64_t i = 1; i <= n; ++i) cout << i <<   n ;       return 0;     }     bool vis[n + 1];     memset(vis, false, sizeof(vis));     for (int64_t i = 1; i <= n; ++i) {       if (vis[i]) continue;       int64_t ch = 0;       for (int64_t j = 1; j <= n; ++j) {         if (i == j) continue;         for (int64_t k = j + 1; k <= n; ++k) {           if (i == k or k == j) continue;           int64_t s =               (a[j] - a[i]) * (a[k] - a[i]) + (b[j] - b[i]) * (b[k] - b[i]) +               (c[j] - c[i]) * (c[k] - c[i]) + (d[j] - d[i]) * (d[k] - d[i]) +               (e[j] - e[i]) * (e[k] - e[i]);           if (s > 0) {             ch = 1;             break;           }         }         if (ch) break;       }       if (ch == 0) {         cout << 1 <<   n ;         cout << i;         return 0;       }     }     cout << 0;   }


#define rep(i,n) for(int i=0;i<(int)(n);i++)   #define ALL(v) v.begin(),v.end()   typedef long long ll;       #include<bits/stdc++.h>   using namespace std;       int main(){     ios::sync_with_stdio(false);     std::cin.tie(nullptr);          int t;     cin>>t;     while(t--){       int xa,ya,xb,yb,xf,yf;       cin>>xa>>ya>>xb>>yb>>xf>>yf;              if(xa==xb && xf==xa && (yf-ya)*(yf-yb)<0) cout<<abs(ya-yb)+2<<endl;       else if(ya==yb && yf==ya && (xf-xa)*(xf-xb)<0) cout<<abs(xa-xb)+2<<endl;       else cout<<abs(xa-xb)+abs(ya-yb)<<endl;                                 }                      return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 400;   int n, m, dp[MAXN + 5][MAXN + 5];   char s[MAXN + 5], t[MAXN + 5];   inline void ckmax(int &x, int y) { x = x > y ? x : y; }   int main() {     int T;     scanf( %d , &T);     while (T--) {       scanf( %s%s , s + 1, t + 1);       n = strlen(s + 1);       m = strlen(t + 1);       bool ok = 0;       for (int i = 1; i <= m; ++i) {         memset(dp, -1, sizeof(dp));         dp[0][0] = 0;         for (int j = 1; j <= n; ++j) {           for (int k = 0; k <= i; ++k) {             if (dp[j - 1][k] != -1) {               ckmax(dp[j][k], dp[j - 1][k] + (i + dp[j - 1][k] + 1 <= m &&                                               s[j] == t[i + dp[j - 1][k] + 1]));             }             if (k >= 1 && s[j] == t[k]) {               ckmax(dp[j][k], dp[j - 1][k - 1]);             }           }         }         if (dp[n][i] == m - i) {           ok = 1;           break;         }       }       if (ok)         puts( YES );       else         puts( NO );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     string s1, s2;     cin >> s1 >> s2;     if (s1.size() > s2.size())       cout << s1.size();     else if (s2.size() > s1.size())       cout << s2.size();     else if (s1 == s2)       cout <<  -1 ;     else       cout << s1.size();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 5e5 + 5;   int n, k, par[N];   int64_t f[N], g[N];   vector<pair<int, int> > ke[N];   vector<int64_t> c[N];   void dfs(int u) {     int64_t sum = 0;     for (auto e : ke[u])       if (e.second != par[u]) {         int v = e.second, w = e.first;         par[v] = u;         dfs(v);         sum += f[v];         c[u].push_back(g[v] + w - f[v]);       }     sort(c[u].begin(), c[u].end(), greater<int64_t>());     for (int i = 0; i < k - 1 && i < (int)c[u].size() && c[u][i] > 0; ++i)       sum += c[u][i];     f[u] = g[u] = sum;     if (k <= (int)c[u].size() && c[u][k - 1] > 0) f[u] += c[u][k - 1];   }   int main() {     int T;     scanf( %d , &T);     while (T--) {       scanf( %d%d , &n, &k);       for (int i = 1; i < n; ++i) {         int u, v, w;         scanf( %d%d%d , &u, &v, &w);         ke[u].push_back(make_pair(w, v));         ke[v].push_back(make_pair(w, u));       }       dfs(1);       printf( %I64d n , f[1]);       for (int i = 1; i <= n; ++i) ke[i].clear(), c[i].clear(), par[i] = 0;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 5;   namespace SBT {   const int _N = N << 2 | 1;   int lc[_N], rc[_N], siz[_N], val[_N], rp[_N], rsiz[_N];   int tp, dl[_N], tot, q[_N], qc[_N];   const double alpha = 0.55;   inline int _new(int v, int ct) {     int u = tp ? dl[tp--] : ++tot;     lc[u] = rc[u] = 0, val[u] = v, siz[u] = rp[u] = ct, rsiz[u] = 1;     return u;   }   inline void upt(int x) {     siz[x] = siz[lc[x]] + siz[rc[x]] + rp[x];     rsiz[x] = rsiz[lc[x]] + rsiz[rc[x]] + 1;   }   inline void zig(int &u) {     int v = lc[u];     lc[u] = rc[v];     rc[v] = u;     upt(u);     upt(v);     u = v;   }   inline void zag(int &u) {     int v = rc[u];     rc[u] = lc[v];     lc[v] = u;     upt(u);     upt(v);     u = v;   }   inline void ins(int &u, int v, int ct = 1) {     if (!u) {       u = _new(v, ct);       return;     }     siz[u] += ct;     if (val[u] == v)       rp[u] += ct;     else if (v < val[u]) {       ins(lc[u], v, ct);       if (rsiz[lc[u]] > rsiz[u] * alpha) zig(u);     } else {       ins(rc[u], v, ct);       if (rsiz[rc[u]] > rsiz[u] * alpha) zag(u);     }     upt(u);   }   inline void del(int &u, int v) {     if (!u) return;     if (val[u] == v) {       if (rp[u] > 1)         --rp[u], --siz[u];       else if (!lc[u] || !rc[u])         dl[++tp] = u, u = lc[u] | rc[u];       else if (rsiz[lc[u]] > rsiz[rc[u]])         zig(u), del(u, v);       else         zag(u), del(u, v);       if (u) upt(u);       return;     }     if (v < val[u])       del(lc[u], v);     else       del(rc[u], v);     upt(u);   }   int rank(int u, int v) {     if (!u) return 0;     if (val[u] == v) return siz[lc[u]] + rp[u];     if (v < val[u]) return rank(lc[u], v);     return rank(rc[u], v) + siz[lc[u]] + rp[u];   }   inline int range(int u, int l, int r) { return rank(u, r) - rank(u, l - 1); }   void us_dfs(int u) {     if (lc[u]) us_dfs(lc[u]);     q[++q[0]] = val[u], qc[q[0]] = rp[u];     dl[++tp] = u;     if (rc[u]) us_dfs(rc[u]);   }   inline void mge(int &u, int &v) {     if (rsiz[u] < rsiz[v]) swap(u, v);     q[0] = 0;     us_dfs(v);     v = 0;     for (int i = 1; i <= q[0]; ++i) ins(u, q[i], qc[i]);   }   }  // namespace SBT   using SBT::del;   using SBT::ins;   using SBT::mge;   using SBT::range;   int n, root, tot;   int fi[N], ne[N], to[N];   int fa[N], siz[N], ans[N];   inline int read() {     int s = 0, w = 1;     char ch = getchar();     for (; !isdigit(ch); ch = getchar())       if (ch ==  - ) w = -1;     for (; isdigit(ch); ch = getchar()) s = (s << 1) + (s << 3) + (ch ^ 48);     return s * w;   }   void print(int x) {     if (x < 0) {       putchar( - );       x = -x;     }     if (x > 9) print(x / 10);     putchar(x % 10 +  0 );   }   inline void add(int x, int y) {     ne[++tot] = fi[x], fi[x] = tot, to[tot] = y;     ne[++tot] = fi[y], fi[y] = tot, to[tot] = x;   }   void pre_dfs(int x) {     siz[x] = 1;     for (int i = fi[x], v = to[i]; i; v = to[i = ne[i]])       if (v != fa[x]) {         pre_dfs(v);         siz[x] += siz[v];       }   }   int Rt, rt_path, rt[N];   inline bool check(int u, int mn, int mx, int maxson, int lim) {     int l = mx - lim, r = lim - mn;     if (l > r) return false;     if (maxson != fa[u]) return range(rt[maxson], l, r) > 0;     int s1 = range(Rt, l, r);     int s2 = range(rt[u], l, r);     int s3 = range(rt_path, l + siz[u], r + siz[u]);     return s1 + s3 > s2;   }   inline void solve_dfs(int x) {     del(Rt, siz[x]);     if (x != root) ins(rt_path, siz[x]);     int mn = 1e9, mx = 0, submx = 0, mxson = 0;     for (int i = fi[x], v = to[i]; i; v = to[i = ne[i]]) {       if (v != fa[x]) solve_dfs(v);       int sz = v == fa[x] ? n - siz[x] : siz[v];       mn = min(mn, sz);       if (sz > mx) {         submx = mx, mx = sz, mxson = v;       } else if (sz > submx)         submx = sz;     }     ins(Rt, siz[x]);     ins(rt[x], siz[x]);     if (x != root) del(rt_path, siz[x]);     if (mxson == fa[x])       for (int i = fi[x], v = to[i]; i; v = to[i = ne[i]])         if (v != fa[x]) mge(rt[x], rt[v]);     int l = submx == 0 ? mx : submx, r = mx, mid = 0;     while (l < r)       check(x, mn, mx, mxson, mid = l + r >> 1) ? r = mid : l = mid + 1;     ans[x] = l;     if (mxson != fa[x])       for (int i = fi[x], v = to[i]; i; v = to[i = ne[i]])         if (v != fa[x]) mge(rt[x], rt[v]);   }   int main() {     n = read();     for (int i = 1; i <= n; ++i) {       int x = read(), y = read();       if (!x)         root = y;       else         fa[y] = x, add(x, y);     }     pre_dfs(root);     for (int i = 1; i <= n; ++i) ins(Rt, siz[i]);     solve_dfs(root);     for (int i = 1; i <= n; ++i) print(ans[i]), puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, k, M, D, N, sol, x, mid, f, l, T;   long long up(long long a, long long b) {     long long tmp = a / b;     if (a % b != 0) tmp = tmp + 1ll;     return tmp;   }   long long rep(long long a) {     long long tmp = n / a;     tmp = up(tmp, k);     return tmp;   }   int main() {     cin >> n >> k >> M >> D;     sol = rep(M) * M;     for (long long p = 2ll; p <= D; p++) {       f = 1ll, l = M;       mid = (f + l + 1ll) / 2;       T = -1;       while (l > f) {         mid = (f + l + 1ll) / 2;         if (rep(mid) == p && rep(mid + 1ll) != p) {           T = mid;           break;         }         if (rep(mid) >= p) {           f = mid;         } else if (rep(mid) < p) {           l = mid - 1ll;         }         if (l - f <= 1) {           if (rep(f) == p && rep(l + 1ll) > p) {             T = f;             break;           }           if (rep(l) == p && rep(l + 1ll) > p) {             T = l;             break;           }         }       }       if (T != -1) {         x = T;         T = rep(x) * x;         sol = max(sol, T);       }     }     cout << sol;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int max(int a, int b) { return a > b ? a : b; }   inline int min(int a, int b) { return a < b ? a : b; }   map<int, int> mp;   int main() {     int n, x;     cin >> n;     mp.clear();     for (int i(0); i < (n); ++i) {       cin >> x;       ++mp[x];     }     long long ans(0);     for (map<int, int>::iterator it = mp.begin(); it != mp.end() && it->first < 0;          ++it)       ans += (long long)it->second * (long long)mp[-it->first];     ans += (long long)mp[0] * ((long long)mp[0] - 1ll) / 2ll;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   long long a[305];   int prt[305], num[305] = {0}, ha[305] = {0};   long long f[305][605] = {0};   long long c[305][305], jc[305];   int n;   bool pd(int i, int j) {     long long ha = sqrt(a[i] * a[j]);     if (ha * ha == a[i] * a[j]) return true;     return false;   }   int getfa(int x) {     if (x == prt[x]) return x;     return prt[x] = getfa(prt[x]);   }   void init() {     c[0][0] = 1;     for (int i = 1; i <= 300; i++) {       c[i][0] = 1;       for (int j = 1; j <= i; j++)         c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod;     }     jc[0] = 1;     for (int i = 1; i <= 300; i++) jc[i] = jc[i - 1] * i % mod;   }   void solve() {     int i, j, k, h;     int sum = num[1];     f[1][num[1] - 1] = 1;     for (i = 2; i <= num[0]; i++) {       for (j = 0; j <= sum - 1; j++)         for (k = 1; k <= num[i]; k++)           for (h = 0; h <= j && h <= k; h++) {             f[i][j - h + num[i] - k] += f[i - 1][j] * c[j][h] % mod *                                         c[num[i] - 1][k - 1] % mod *                                         c[sum + 1 - j][k - h] % mod;             f[i][j - h + num[i] - k] %= mod;           }       sum += num[i];     }     long long ans = f[num[0]][0];     for (int i = 1; i <= num[0]; i++) ans = ans * jc[num[i]] % mod;     printf( %lld n , ans);   }   int main() {     init();     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %lld , &a[i]);     for (int i = 1; i <= n; i++) prt[i] = i;     for (int i = 1; i <= n; i++)       for (int j = i + 1; j <= n; j++)         if (pd(i, j)) {           int f1, f2;           f1 = getfa(i);           f2 = getfa(j);           if (f1 != f2) prt[f1] = f2;         }     for (int i = 1; i <= n; i++) prt[i] = getfa(i);     for (int i = 1; i <= n; i++) {       if (ha[prt[i]] == 0) {         num[0]++;         ha[prt[i]] = num[0];         num[num[0]]++;       } else         num[ha[prt[i]]]++;     }     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};   const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};   const int Mod = 1000000000 + 0;   template <typename T>   inline T sq(T x) {     return x * x;   }   template <typename T>   inline T In() {     T x;     cin >> x;     return x;   }   template <typename T>   inline void Out(T& x) {     cout << x;   }   template <typename T>   inline void sort(T& v) {     sort(begin(v), end(v));   }   template <typename T>   inline void revs(T& v) {     reverse(begin(v), end(v));   }   template <typename T>   inline void uniq(T& v) {     sort(v);     v.erase(unique(begin(v), end(v)), end(v));   }   template <typename T>   inline int ubnd(T& v, typename T::value_type& x) {     return upper_bound(begin(v), end(v), x) - begin(v);   }   template <typename T>   inline int lbnd(T& v, typename T::value_type& x) {     return lower_bound(begin(v), end(v), x) - begin(v);   }   template <typename T, typename F>   inline void inpt(T& v, int n, F f) {     for (v.reserve(n); n--; v.emplace_back(f()))       ;   }   template <typename T, typename F>   inline void show(T& v, F f, string d =    , string e =   n ) {     int i = 0;     for (auto& x : v) i++ && (cout << d), f(x);     cout << e;   }   template <typename T>   inline typename T::iterator minel(T& v) {     return min_element(begin(v), end(v));   }   template <typename T>   inline typename T::iterator maxel(T& v) {     return max_element(begin(v), end(v));   }   inline void fast_io() {     ios::sync_with_stdio(0);     cin.tie(0);   }   inline int in() {     int x;     scanf( %d , &x);     return x;   }   inline long long pow_mod(long long a, long long k, long long m) {     long long r = 1;     for (; k > 0; a = a * a % m, k >>= 1)       if (k & 1) r = r * a % m;     return r;   }   inline long long mod_inv(long long a, long long p) {     return pow_mod(a, p - 2, p);   }   int main() {     vector<long long> xs, ys;     int n = in();     for (int i = 0; i < n; i++) {       long long x, y;       cin >> x >> y;       xs.emplace_back(x);       ys.emplace_back(y);     }     sort(xs);     sort(ys);     long long xx = abs(xs[0] - xs[n - 1]);     long long yy = abs(ys[0] - ys[n - 1]);     cout << sq(max(xx, yy)) << endl;     return 0;   }


#include <bits/stdc++.h>   const long long mod = (long long)1e9 + 7;   const int maxn = (int)4e5 + 2;   long long inf = 1LL << 60;   const double eps = 1e-9;   using namespace std;   template <class N>   inline void read(N &n) {     N x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = 10 * x + ch -  0 ;       ch = getchar();     }     n = x * f;   }   long long a[2001], b[2001];   int main() {     ios_base::sync_with_stdio(0);     int n, m;     cin >> n;     long long s1 = 0, s2 = 0;     for (int i = 0; i < n; i++) {       cin >> a[i];       s1 += a[i];     }     cin >> m;     for (int i = 0; i < m; i++) {       cin >> b[i];       s2 += b[i];     }     long long diff = abs(s1 - s2);     pair<int, int> ans1 = {-1, -1};     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         long long bs = s2 - b[j] + a[i];         long long as = s1 - a[i] + b[j];         if (diff > abs(as - bs)) {           diff = abs(as - bs);           ans1 = {i, j};         }       }     }     map<long long, pair<int, int> > mpa, mpb;     for (int i = 0; i < n; i++) {       for (int j = i + 1; j < n; j++) {         mpa[a[i] + a[j]] = {i, j};       }     }     long long diff1 = inf;     pair<int, int> ans[2];     for (int i = 0; i < m; i++) {       for (int j = i + 1; j < m; j++) {         long long d = b[i] + b[j];         auto it1 = mpa.lower_bound((s1 - s2 + 2 * d) / 2);         if (it1 != mpa.begin()) it1--;         int jj = 0;         while (it1 != mpa.end() && jj <= 2) {           long long diff2 = abs((s1 - it1->first + d) - (s2 - d + it1->first));           if (diff1 > diff2) {             diff1 = diff2;             ans[1] = {i, j};             ans[0] = it1->second;           }           jj++;           it1++;         }       }     }     if (diff1 < diff) {       cout << diff1 << endl;       cout << 2 << endl;       cout << ans[0].first + 1 <<     << ans[1].first + 1 << endl;       cout << ans[0].second + 1 <<     << ans[1].second + 1 << endl;     } else {       cout << diff << endl;       if (ans1.first == -1)         cout << 0;       else {         cout << 1 << endl;         cout << ans1.first + 1 <<     << ans1.second + 1;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   const int iinf = 1 << 29;   const long long llinf = 1ll << 60;   const double PI = 3.14159265;   mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());   long long mrand(long long B) { return (unsigned long long)rng() % B; }   long long cross(pair<long long, long long> p1, pair<long long, long long> p2) {     return p1.first * p2.second - p2.first * p1.second;   }   bool online(pair<long long, long long> a, pair<long long, long long> b,               pair<long long, long long> c) {     pair<long long, long long> p1{b.first - a.first, b.second - a.second};     pair<long long, long long> p2{c.first - a.first, c.second - a.second};     return cross(p1, p2) == 0;   }   void z_func(vector<int> &z, vector<int> &s, int n) {     int L = 0, R = 0;     for (int i = 1; i < n; i++) {       if (i > R) {         L = R = i;         while (R < n && s[R - L] == s[R]) R++;         z[i] = R - L;         R--;       } else {         int k = i - L;         if (z[k] < R - i + 1)           z[i] = z[k];         else {           L = i;           while (R < n && s[R - L] == s[R]) R++;           z[i] = R - L;           R--;         }       }     }   }   const int MXN = 1e5 + 1;   int n, m;   bool roots[MXN];   vector<int> g[MXN];   int listt[MXN];   vector<int> ans;   void fail() {     cout <<  -1 n ;     exit(0);   }   int dfs(int node) {     int request = -1;     for (auto &v : g[node]) {       int req = dfs(v);       if (req != -1 && request != -1 && req != request) fail();       if (req != -1) request = req;     }     if (request != -1 && listt[node] != request) fail();     if (request == -1) {       if (listt[node] == node) {         ans.push_back(node);         return -1;       } else {         return listt[node];       }     } else {       if (node == listt[node]) {         ans.push_back(node);         return -1;       }       return listt[node];     }   }   void work() {     cin >> n >> m;     for (int i = 0; i < m; ++i) {       int u, v;       cin >> u >> v;       roots[v] = true;       g[u].push_back(v);     }     for (int i = 1; i <= n; ++i) cin >> listt[i];     for (int i = 1; i <= n; ++i)       if (!roots[i]) dfs(i);     cout << ((int)(ans).size()) <<   n ;     for (auto &e : ans) cout << e <<   n ;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     work();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k;   struct Poi {     int x, y, ff;     bool operator<(const Poi &A) const {       return x < A.x || (x == A.x && y < A.y);     }   } pp[(200100)];   int row[(200100)], cntr;   int cnt[(200100)], pre[(200100)];   long long ans[(200100)];   inline void solve() {     sort(pp + 1, pp + 1 + n * 2);     for (int i = 1; i <= n * 2; ++i) {       int y = lower_bound(row + 1, row + 1 + cntr, pp[i].y) - row;       for (int j = y; row[j] < row[y] + k; ++j) {         if (cnt[j] > 0)           ans[cnt[j]] += (long long)(row[j + 1] - row[j]) * (pp[i].x - pre[j]);         pre[j] = pp[i].x;         cnt[j] += pp[i].ff;       }     }     for (int i = 1; i <= n; ++i) cout << ans[i] <<    ;   }   inline void In() {     scanf( %d %d , &n, &k);     for (int i = 1; i <= n; ++i) {       int i1 = i * 2 - 1, i2 = i * 2;       scanf( %d %d , &pp[i1].x, &pp[i1].y);       pp[i1].ff = 1;       row[i1] = pp[i1].y;       row[i2] = row[i1] + k;       pp[i2].y = pp[i1].y;       pp[i2].ff = -1;       pp[i2].x = pp[i1].x + k;     }     sort(row + 1, row + 1 + n * 2);     cntr = 1;     for (int i = 2; i <= n * 2; ++i)       if (row[i] != row[cntr]) row[++cntr] = row[i];   }   int main() {     In();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int gcd(long long int x, long long int y) {     if (y == 0) return x;     return gcd(y, x % y);   }   long long exp(long long a, long long b) {     if (b == 0) return 1;     long long res = exp(a, b / 2);     if (b % 2)       return res * res * a;     else       return res * res;   }   int prime(long long x) {     if (x % 2 == 0)       return 0;     else {       for (int i = 3; i * i <= x; i += 2) {         if (x % i == 0) return 0;       }     }     return 1;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     int n, m, k;     map<int, int> ma;     cin >> n;     int arr[n];     for (int i = 0; i < n; i++) cin >> arr[i];     cin >> m;     int brr[m];     for (int i = 0; i < m; i++) cin >> brr[i];     for (int i = 0; i < n; i++) {       for (int j = m - 1; j >= 0; j--) {         if (brr[j] % arr[i] == 0) {           k = brr[j] / arr[i];           if (ma.find(k) == ma.end())             ma.insert({k, 1});           else             ma[k]++;         }       }     }     map<int, int>::iterator it;     it = ma.end();     it--;     cout << it->second <<   n ;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f, N = 1e3 + 5, MOD = 1e9 + 7;   int T, cas = 0;   int n, m;   bool vis[N];   vector<int> primes;   void Init_Primes() {     memset(vis, 0, sizeof(vis));     int m = sqrt(N + 0.5);     for (int i = (2); i <= (m); i++)       if (!vis[i])         for (int j = i * i; j <= N; j += i) vis[j] = 1;     for (int i = (2); i < (N); i++)       if (!vis[i]) primes.push_back(i);   }   int main() {     Init_Primes();     while (scanf( %d , &n) != EOF) {       int a[N], k = 0;       for (int i = 2; i <= n; i++) {         if (!vis[i]) {           int t = 1;           while (t * i <= n) {             t *= i;             a[k++] = t;           }         }       }       printf( %d n , k);       for (int i = 0; i < (k - 1); i++) printf( %d  , a[i]);       if (k > 0) printf( %d n , a[k - 1]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int d1[5], c1[5], d2[5], c2[5];   int min2[5], min1[5];   int main() {     int n;     cin >> n;     for (int i = 1; i <= 4; i++) {       cin >> c1[i] >> d1[i] >> c2[i] >> d2[i];       min1[i] = min(c1[i], d1[i]);       min2[i] = min(c2[i], d2[i]);     }     int tmp = 1e9;     int it;     for (int i = 1; i <= 4; i++)       if (min1[i] + min2[i] < tmp) {         tmp = min1[i] + min2[i];         it = i;       }     if (tmp > n)       cout << -1;     else       cout << it <<     << min1[it] <<     << n - min1[it];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a, x, y, xx, yy;   int main() {     cin >> a >> x >> y >> xx >> yy;     if (abs(x - xx) == a)       cout << a + min(2 * a - y - yy, y + yy);     else if (abs(y - yy) == a)       cout << a + min(2 * a - x - xx, x + xx);     else       cout << abs(xx - x) + abs(yy - y);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long a, b, c;     cin >> a >> b >> c;     long long d = b * b - 4 * a * c;     if (a != 0) {       double x = a, y = b;       if (d < 0) cout << 0;       if (d == 0) {         cout << 1 << endl;         printf( %lf , -y / (2 * x));       }       if (d > 0) {         cout << 2 << endl;         if (x > 0)           printf( %lf n%lf , (-y - sqrt(d)) / (2 * x), (-y + sqrt(d)) / (2 * x));         else           printf( %lf n%lf , (-y + sqrt(d)) / (2 * x), (-y - sqrt(d)) / (2 * x));       }     } else {       if (b == 0) {         if (c == 0)           cout << -1;         else           cout << 0;       } else {         double s = c, t = b;         cout << 1 << endl;         printf( %lf , -s / t);       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200005;   bitset<N> vis;   int n, m, d[N], bound;   vector<int> g[N];   set<int> h[N];   unsigned long long a, b, c, ans, sum[N];   inline unsigned long long get(int u, int v) {     if (u > v) swap(u, v);     unsigned long long x = a * u * 1LLU * (u - 1) / 2 + u * (b * u + c * v);     unsigned long long y = b * (v * 1LLU * (v - 1) - u * 1LLU * (u + 1)) / 2 +                            (v - u - 1) * (a * u + c * v);     unsigned long long z = c * (n * 1LLU * (n - 1) - v * 1LLU * (v + 1)) / 2 +                            (n - v - 1) * (a * u + b * v);     return x + y + z;   }   inline unsigned long long val(int u, int v, int w) {     int x[3] = {u, v, w};     sort(x, x + 3);     return a * x[0] + b * x[1] + c * x[2];   }   vector<pair<int, int> > edges;   bitset<N> isHeavy;   vector<int> heavy;   inline bool isEdge(int u, int v) { return h[u].find(v) != h[u].end(); }   void triangles() {     for (int i = 0; i < n; ++i) {       if (d[i] > bound) {         heavy.push_back(i);         isHeavy[i] = 1;       }     }     for (int u : heavy) {       for (pair<int, int> e : edges) {         int v = e.first, w = e.second;         if (isHeavy[v] and v < u) continue;         if (isHeavy[w] and w < u) continue;         if (isEdge(u, v) and isEdge(u, w)) {           ans -= val(u, v, w);         }       }     }     for (pair<int, int> e : edges) {       int u = e.first, v = e.second;       if (u > v) swap(u, v);       if (isHeavy[u] or isHeavy[v]) continue;       for (int i = 0, j = 0; i < g[u].size() and j < g[v].size();) {         if (g[u][i] == g[v][j]) {           int w = g[u][i];           if (!isHeavy[w] and w > v) {             ans -= val(u, v, w);           }           ++i;         } else if (g[u][i] < g[v][j]) {           ++i;         } else {           ++j;         }       }     }   }   int main() {     scanf( %d %d %llu %llu %llu , &n, &m, &a, &b, &c);     bound = 700;     for (unsigned long long k = 0; k < n; ++k) {       unsigned long long x = k * (k - 1) / 2;       unsigned long long y = k * (a + 2 * b + 3 * c) - (2 * a + b);       if (x % 3 == 0)         x /= 3;       else         y /= 3;       ans += x * y;     }     while (m--) {       int u, v;       scanf( %d %d , &u, &v);       ++d[u], ++d[v];       edges.emplace_back(u, v);       ans -= get(u, v);       g[u].push_back(v);       g[v].push_back(u);       h[u].insert(v);       h[v].insert(u);     }     for (int i = 0; i < n; ++i) {       sort(g[i].begin(), g[i].end());       int sz = g[i].size();       for (int j = 0; j < sz; ++j) {         sum[j + 1] = sum[j] + g[i][j];       }       int at = 0;       while (at < sz and g[i][at] < i) {         ++at;       }       for (int j = 0; j < sz; ++j) {         unsigned long long cur;         int u = g[i][j];         if (u > i) {           cur = (sz - 1 - j) * (a * i + b * u) + c * (sum[sz] - sum[j + 1]);           ans += cur;         }         if (u < i) {           cur = (sz - at) * (a * u + b * i) + c * (sum[sz] - sum[at]);           ans += cur;           cur = j * (b * u + c * i) + a * sum[j];           ans += cur;         }       }     }     triangles();     printf( %llu n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int arr[200][200];     int n, m, ok = 1000000000, ans = 0;     scanf( %d %d , &n, &m);     for (int i = 0; i < n; i++) {       ok = 1000000000;       for (int j = 0; j < m; j++) {         scanf( %d , &arr[i][j]);         if (ok > arr[i][j]) ok = arr[i][j];       }       ans = max(ans, ok);     }     printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int a;     cin >> a;     if (a <= 1) {       cout << a;     } else if (a == 2) {       cout << 2;     } else if (a == 3) {       cout << 6;     } else if (a == 4) {       cout << 20;     } else if (a == 5) {       cout << 70;     } else if (a == 6) {       cout << 252;     } else if (a == 7) {       cout << 924;     } else if (a == 9) {       cout << 12870;     } else if (a == 10) {       cout << 48620;     } else if (a == 8) {       cout << 3432;     }   }


#include <bits/stdc++.h>   using namespace std;   long long a[200001], b[200001];   int main() {     int t;     int i, j;     cin >> t;     while (t--) {       long long n, m;       cin >> n >> m;       for (i = 0; i < n; i++) cin >> a[i] >> b[i];       long long A = 0, B = 0, Ap = 0, Bp = 0;       long long tot = 0, sum = 0;       for (i = 0; i < n; i++) {         A += a[i] - (m - min(m, b[i]));         Ap += m - min(m, b[i]);         B += b[i] - (m - min(m, a[i]));         Bp += m - min(m, a[i]);         sum += m;         tot += a[i] + b[i] - m;       }       if (A + A < tot) {         cout << tot - (A + A) << endl;         for (i = 0; i < n; i++) {           cout << m - min(m, b[i]) <<     << min(m, b[i]) << endl;         }       } else if (B + B < tot) {         cout << tot - (B + B) << endl;         for (i = 0; i < n; i++) {           cout << min(m, a[i]) <<     << m - min(m, a[i]) << endl;         }       } else {         long long togo = A - tot / 2;         cout << (tot & 1) << endl;         for (i = 0; i < n; i++) {           long long x = min(togo, min(m, b[i]));           x = min(x, a[i] - (m - min(m, b[i])));           togo -= x;           cout << m - min(m, b[i]) + x <<     << min(m, b[i]) - x << endl;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     long long mx = 0, x = 0;     cin >> n;     for (int i = 0; i < n; i++) {       long long y;       cin >> y;       y += mx;       mx += max(0LL, x - y), x = max(y, x);     }     cout << mx << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct fastIO {     inline fastIO operator>>(int& num) {       num = 0;       char c = getchar();       while (c !=  -  && (c <  0  || c >  9 )) c = getchar();       bool foo = 0;       if (c ==  - ) {         foo = 1;         c = getchar();       }       while (c >=  0  && c <=  9 ) {         num = (num << 3) + (num << 1) + c -  0 ;         c = getchar();       }       if (foo) num = -num;       return *this;     }     inline fastIO operator>>(long long& num) {       num = 0;       char c = getchar();       while (c !=  -  && (c <  0  || c >  9 )) c = getchar();       bool foo = 0;       if (c ==  - ) {         foo = 1;         c = getchar();       }       while (c >=  0  && c <=  9 ) {         num = (num << 3) + (num << 1) + c -  0 ;         c = getchar();       }       if (foo) num = -num;       return *this;     }     inline fastIO operator>>(float& num) {       scanf( %f , &num);       return *this;     }     inline fastIO operator>>(double& num) {       scanf( %lf , &num);       return *this;     }     inline fastIO operator>>(long double& num) {       scanf( %Lf , &num);       return *this;     }     inline fastIO operator>>(char& num) {       num = getchar();       while (num ==     || num ==   n ) num = getchar();       return *this;     }     inline fastIO operator>>(char* num) {       int cnt = 0;       char c = getchar();       while (c ==     || c ==   n ) c = getchar();       while (c !=     && c !=   n ) {         num[cnt++] = c;         c = getchar();       }       num[cnt] = 0;       return *this;     }     inline fastIO operator>>(string& num) {       cin >> num;       return *this;     }     inline void printInt(const int& num) {       if (num < 10) {         putchar(num +  0 );         return;       }       printInt(num / 10);       putchar((num % 10) +  0 );     }     inline void printINT(const long long& num) {       if (num < 10) {         putchar(num +  0 );         return;       }       printINT(num / 10);       putchar((num % 10) +  0 );     }     inline fastIO operator<<(const int& num) {       if (num < 0)         putchar( - ), printInt(-num);       else         printInt(num);       return *this;     }     inline fastIO operator<<(const long long& num) {       if (num < 0)         putchar( - ), printINT(-num);       else         printINT(num);       return *this;     }     inline fastIO operator<<(const float& num) {       printf( %.10f , num);       return *this;     }     inline fastIO operator<<(const double& num) {       printf( %.16lf , num);       return *this;     }     inline fastIO operator<<(const long double& num) {       printf( %.20Lf , num);       return *this;     }     inline fastIO operator<<(const char& num) {       putchar(num);       return *this;     }     inline fastIO operator<<(const char* num) {       for (int i = 0; num[i]; i++) putchar(num[i]);       return *this;     }     inline fastIO operator<<(const string& num) {       cout << num;       return *this;     }   } fio;   int n, m, k;   int fuck[17][17];   int shit[17][17][17];   int dy[17];   int ans = 0;   void dfs(int x, int y, int mx) {     if (x == n) {       for (int i = 1; i <= mx; i++)         for (int j = i + 1; j <= mx; j++)           if (dy[i] && dy[j] && dy[i] == dy[j]) return;       int fri = 0, bb = k;       for (int i = 1; i <= mx; i++) {         if (!dy[i])           fri++;         else           bb--;       }       int fk = 1;       for (int i = 0; i < fri; i++) {         fk *= bb;         bb--;       }       ans += fk;       if (ans > 1000000007) ans -= 1000000007;       return;     }     for (int i = 1; i <= mx + 1; i++) {       if (shit[x][y][i]) continue;       for (int ii = x; ii < n; ii++)         for (int jj = y; jj < m; jj++) shit[ii][jj][i]++;       bool fau = 1;       if (fuck[x][y] && dy[i] && fuck[x][y] != dy[i]) fau = 0;       if (fau) {         bool dyed = 0;         if (!dy[i]) {           dy[i] = fuck[x][y];           dyed = 1;         }         if (y < m - 1)           dfs(x, y + 1, min(max(mx, i), k - 1));         else           dfs(x + 1, 0, min(max(mx, i), k));         if (dyed) dy[i] = 0;       }       for (int ii = x; ii < n; ii++)         for (int jj = y; jj < m; jj++) shit[ii][jj][i]--;     }   }   int main() {     fio >> n >> m >> k;     if (n + m - 1 > k) {       fio <<  0 n ;       return 0;     }     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) fio >> fuck[i][j];     dfs(0, 0, 0);     fio << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC target( avx,avx2,fma )   using namespace std;   const long long MOD = 1e9 + 7;   const long long mxN = 1e5 + 3;   vector<long long> g[mxN];   long long good[mxN];   long long total[mxN], people[mxN];   long long h[mxN];   bool ok[mxN];   void dfs1(long long u, long long par) {     long long sum = 0;     for (long long i : g[u]) {       if (i != par) {         dfs1(i, u);         sum += total[i];       }     }     total[u] = sum + people[u];   }   bool dfs2(long long u, long long par) {     bool ret = true;     if (ok[u] == false) return false;     long long cnt = 0;     for (long long i : g[u]) {       if (i != par) {         cnt++;         ret = (ret && dfs2(i, u));       }     }     if (ret == true) {       long long su = 0;       for (long long ii : g[u]) {         if (ii != par) {           su += good[ii];         }       }       if (su > good[u])         return false;       else         return true;     }     return false;   }   void solve() {     long long n;     cin >> n;     for (long long i = 0; i <= n; ++i) {       g[i].clear();       good[i] = 0;       total[i] = 0;       people[i] = 0;       h[i] = 0;       ok[i] = true;     }     long long m;     cin >> m;     for (long long i = 1; i <= n; ++i) {       cin >> people[i];     }     for (long long i = 1; i <= n; ++i) {       cin >> h[i];     }     long long u, v;     for (long long i = 1; i < n; ++i) {       cin >> u >> v;       g[u].push_back(v);       g[v].push_back(u);     }     dfs1(1, -1);     for (long long i = 1; i <= n; ++i) {       long long g2 = (total[i] + h[i]);       long long b2 = (total[i] - h[i]);       if (g2 & 1) {         ok[i] = false;         continue;       }       if (b2 & 1) {         ok[i] = false;         continue;       }       long long go = g2 / 2;       long long b = b2 / 2;       if (go > total[i] or b > total[i] or go < 0 or b < 0) {         ok[i] = false;         continue;       }       good[i] = go;     }     bool ans = dfs2(1, -1);     if (ans)       cout <<  YES             <<   n ;     else       cout <<  NO             <<   n ;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long nT = 1;     cin >> nT;     for (long long i = 1; i <= nT; ++i) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100;   int l[N], r[N], t[N], c[N];   int main() {     int n, m;     scanf( %d %d , &n, &m);     for (int i = 0; i < m; ++i) {       scanf( %d %d %d %d , l + i, r + i, t + i, c + i);     }     int ans = 0;     for (int i = 1; i <= n; ++i) {       int win = 0, min = -1;       for (int j = 0; j < m; ++j) {         if (l[j] <= i && i <= r[j]) {           if (min == -1 || min > t[j]) {             min = t[j];             win = c[j];           }         }       }       ans += win;     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1000000007;   int n, m;   int clauses[123123][2];   vector<int> uses[123123];   bool vis[123123];   vector<int> chain[123123];   int counter;   vector<pair<long long, long long>> dips;   long long ans;   int rightc(int cnr) {     int xnr = abs(clauses[cnr][1]);     if (uses[xnr].size() == 2) {       return uses[xnr][0] == cnr ? uses[xnr][1] : uses[xnr][0];     } else {       return 0;     }   }   void load() {     cin >> n >> m;     int k;     for (int i = 1; i <= n; ++i) {       cin >> k;       for (int j = 0; j < k; ++j) {         cin >> clauses[i][j];         uses[abs(clauses[i][j])].push_back(i);       }     }   }   void dfs(int cnr, int from) {     if (vis[cnr]) {       return;     }     vis[cnr] = 1;     if (abs(clauses[cnr][1]) == from) {       swap(clauses[cnr][0], clauses[cnr][1]);     }     chain[counter].push_back(cnr);     dfs(rightc(cnr), abs(clauses[cnr][1]));   }   void dfs2(int cnr, int from, int f) {     if (vis[cnr]) {       return;     }     vis[cnr] = 1;     if (!clauses[cnr][1]) {       if (!f) {         swap(clauses[cnr][0], clauses[cnr][1]);       }     } else {       if (abs(clauses[cnr][1]) == from) {         swap(clauses[cnr][0], clauses[cnr][1]);       }     }     chain[counter].push_back(cnr);     dfs2(rightc(cnr), abs(clauses[cnr][1]), 1);   }   void buildChains() {     vis[0] = 1;     for (int i = 1; i <= m; ++i)       if (uses[i].size() == 1 && !vis[uses[i][0]]) {         dfs(uses[i][0], i);         ++counter;       }     for (int i = 1; i <= m; ++i)       if (uses[i].size() == 2) {         if (!vis[uses[i][0]] && !clauses[uses[i][0]][1]) {           dfs2(uses[i][0], i, 0);           ++counter;         }         if (!vis[uses[i][1]] && !clauses[uses[i][1]][1]) {           dfs2(uses[i][1], i, 0);           ++counter;         }       }     for (int i = 1; i <= n; ++i)       if (!vis[i]) {         dfs(i, abs(clauses[i][0]));         ++counter;       }   }   long long dp[123123][2][2][2];   int val(int i, int j, int nr) {     int v1 = i;     if (clauses[nr][0] < 0) {       v1 = 1 - v1;     }     if (clauses[nr][0] == 0) {       v1 = 0;     }     int v2 = j;     if (clauses[nr][1] < 0) {       v2 = 1 - v2;     }     if (clauses[nr][1] == 0) {       v2 = 0;     }     return (v1 | v2);   }   void cleaar(int si) {     for (int i = 0; i < si; ++i)       for (int j = 0; j < 2; ++j)         for (int l = 0; l < 2; ++l)           for (int k = 0; k < 2; ++k) dp[i][j][l][k] = 0;   }   void consider(int nr) {     if (chain[nr].size() == 1 &&         abs(clauses[chain[nr][0]][0]) == abs(clauses[chain[nr][0]][1])) {       if (clauses[chain[nr][0]][0] == clauses[chain[nr][0]][1]) {         dips.push_back(make_pair(1, 1));       } else {         dips.push_back(make_pair(0, 2));       }       return;     }     cleaar(chain[nr].size());     int fi = chain[nr][0];     for (int i = 0; i < 2; ++i)       for (int j = 0; j < 2; ++j) dp[0][i][j][val(i, j, fi)] = 1;     for (int i = 0; i < chain[nr].size() - 1; ++i)       for (int j = 0; j < 2; ++j)         for (int l = 0; l < 2; ++l)           for (int k = 0; k < 2; ++k) {             dp[i + 1][j][0][val(l, 0, chain[nr][i + 1]) ^ k] += dp[i][j][l][k];             dp[i + 1][j][0][val(l, 0, chain[nr][i + 1]) ^ k] %= mod;             dp[i + 1][j][1][val(l, 1, chain[nr][i + 1]) ^ k] += dp[i][j][l][k];             dp[i + 1][j][1][val(l, 1, chain[nr][i + 1]) ^ k] %= mod;           }     int si = chain[nr].size();     if (!clauses[chain[nr][0]][0] && !clauses[chain[nr][si - 1]][1]) {       dips.push_back(make_pair(dp[si - 1][0][0][0], dp[si - 1][0][0][1]));       return;     }     if (!clauses[chain[nr][si - 1]][1]) {       long long r0 = (dp[si - 1][0][0][0] + dp[si - 1][1][0][0]) % mod;       long long r1 = (dp[si - 1][0][0][1] + dp[si - 1][1][0][1]) % mod;       dips.push_back(make_pair(r0, r1));       return;     }     if (abs(clauses[chain[nr][0]][0]) != abs(clauses[chain[nr][si - 1]][1])) {       long long r0 = (dp[si - 1][0][0][0] + dp[si - 1][1][1][0] +                       dp[si - 1][0][1][0] + dp[si - 1][1][0][0]) %                      mod;       long long r1 = (dp[si - 1][0][0][1] + dp[si - 1][1][1][1] +                       dp[si - 1][0][1][1] + dp[si - 1][1][0][1]) %                      mod;       dips.push_back(make_pair(r0, r1));       return;     }     long long r0 = (dp[si - 1][0][0][0] + dp[si - 1][1][1][0]) % mod;     long long r1 = (dp[si - 1][0][0][1] + dp[si - 1][1][1][1]) % mod;     dips.push_back(make_pair(r0, r1));   }   void makeAns() {     long long a = dips[0].first;     long long b = dips[0].second;     for (int i = 1; i < counter; ++i) {       long long c = (a * dips[i].first + b * dips[i].second) % mod;       long long d = (a * dips[i].second + b * dips[i].first) % mod;       a = c;       b = d;     }     ans = b;     for (int i = 1; i <= m; ++i)       if (uses[i].empty()) {         ans *= 2;         ans %= mod;       }   }   void debug() {     return;     for (int i = 0; i < counter; ++i) {       cerr << i <<   :  ;       for (int ind : chain[i]) {         cerr <<  (  << clauses[ind][0] <<  ,   << clauses[ind][1] <<  )  ;       }       cerr << endl;     }     for (auto tmp : dips) {       cerr << tmp.first <<     << tmp.second << endl;     }   }   int main() {     ios_base::sync_with_stdio(0);     load();     buildChains();     for (int i = 0; i < counter; ++i) {       consider(i);     }     debug();     makeAns();     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int MX = 3005;   int n, a[MX], mem[MX][MX], ind[MX];   vector<int> pos[MX];   int dp(int i, int j) {     if (i > j) return 0;     int &res = mem[i][j];     if (res != -1) return res;     res = dp(i, j - 1);     for (int k = ind[j] - 1, x; k >= 0 && (x = pos[a[j]][k]) >= i; k--)       res = max(res, dp(i, x) + dp(x + 1, j - 1) + 1);     return res;   }   void main_() {     cin >> n;     for (int i = 0; i < int(n + 1); i++) pos[i].clear();     for (int i = 0; i < int(n); i++) {       cin >> a[i];       ind[i] = pos[a[i]].size();       pos[a[i]].push_back(i);     }     for (int i = 0; i < int(n); i++)       for (int j = 0; j < int(n); j++) mem[i][j] = -1;     cout << n - 1 - dp(0, n - 1) <<   n ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t = 1;     cin >> t;     while (t--) main_();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long solve() {     long long k, a, b;     cin >> k >> a >> b;     if (a > b) swap(a, b);     if (a == 0) {       if (b % k == 0) return b / k;       return -1;     }     long long ans = a / k + b / k;     if (b % k != 0 && a < k) return -1;     if (a % k != 0 && b < k) return -1;     if (ans == 0) return -1;     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout << solve() << endl;     return 0;   }