#include <bits/stdc++.h> n using namespace std; n int cal(int a, int b, int ba) { n   int x[100], xl, y[100], yl, i, z[100]; n   memset(x, 0, sizeof x); n   memset(y, 0, sizeof y); n   memset(z, 0, sizeof z); n   xl = yl = 0; n   while (a > 0) { n     x[xl++] = a % 10; n     a /= 10; n   } n   while (b > 0) { n     y[yl++] = b % 10; n     b /= 10; n   } n   for (i = 0; i < max(xl, yl); i++) { n     z[i] += x[i] + y[i]; n     if (z[i] >= ba) { n       z[i + 1] += z[i] / ba; n       z[i] %= ba; n     } n   } n   i = max(xl, yl) + 10; n   while (z[i] == 0) i--; n   return i + 1; n } n int main() { n   int a, b, i, t, m, ans; n   while (scanf( %d %d , &a, &b) != EOF) { n     m = 0; n     t = a; n     while (t > 0) { n       if (t % 10 > m) m = t % 10; n       t /= 10; n     } n     t = b; n     while (t > 0) { n       if (t % 10 > m) m = t % 10; n       t /= 10; n     } n     ans = 0; n     for (i = m + 1; i <= 1000; i++) { n       t = cal(a, b, i); n       if (t > ans) ans = t; n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 50; n const long long oo = 1e18; n const long long mod = 1e9 + 7; n int a[N], sum[2 * N]; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int cur = n; n   long long ans = 0; n   sum[cur] = 1; n   long long q = 0; n   for (int i = 0; i < n; i++) { n     if (a[i] < m) { n       cur--; n       q -= sum[cur]; n     } else { n       q += sum[cur]; n       cur++; n     } n     ans += q; n     sum[cur]++; n   } n   memset(sum, 0, sizeof(sum)); n   cur = n; n   sum[cur] = 1; n   q = 0; n   for (int i = 0; i < n; i++) { n     if (a[i] < m + 1) { n       cur--; n       q -= sum[cur]; n     } else { n       q += sum[cur]; n       cur++; n     } n     ans -= q; n     sum[cur]++; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool umin(T& a, T b) { n   if (a > b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n bool umax(T& a, T b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n int arr[300009], b[300009]; n vector<pair<int, int> > adj[300009], query[300009]; n const int N = 500; n long long ans[300009], par[300009]; n int main() { n   int n, q, k; n   scanf( %d%d%d , &n, &q, &k); n   deque<pair<int, int> > deq; n   for (int i = 1; i <= n; i++) { n     scanf( %d , arr + i); n     while (!deq.empty() and deq.back().first >= arr[i]) deq.pop_back(); n     while (!deq.empty() and i - deq.front().second > k) deq.pop_front(); n     deq.push_back(make_pair(arr[i], i)); n     b[i] = deq.front().first; n   } n   for (int i = 1; i <= n; i++) adj[i % k].push_back(make_pair(i, b[i])); n   if (k > N) { n     while (q--) { n       int l, r; n       scanf( %d%d , &l, &r); n       int mn = 1000000007; n       long long res = arr[l]; n       int pos = upper_bound(adj[l % k].begin(), adj[l % k].end(), n                             make_pair(l, 1000000007)) - n                 adj[l % k].begin(); n       while (pos < (int)adj[l % k].size() and adj[l % k][pos].first <= r) n         umin(mn, adj[l % k][pos++].second), res += mn; n       printf( %lld n , res); n     } n     return 0; n   } n   for (int i = 1; i <= q; i++) { n     int l, r; n     scanf( %d%d , &l, &r); n     query[l].push_back(make_pair(r, i)); n     ans[i] = arr[l]; n   } n   for (int i = 0; i < k; i++) { n     memset(par, 0, sizeof par); n     int sz = adj[i].size(); n     vector<pair<int, int> > v; n     for (int j = sz - 1; j >= 0; j--) { n       int l = adj[i][j].first, val = adj[i][j].second; n       if (!v.empty()) { n         for (int h = 0; h < int(query[l].size()); h++) { n           int r = query[l][h].first; n           int idx = query[l][h].second; n           int R = upper_bound(adj[i].begin(), adj[i].end(), n                               make_pair(r, 1000000007)) - n                   adj[i].begin(); n           int st = 0, en = v.size() - 1; n           while (st + 1 < en) { n             int mid = (st + en) >> 1; n             if (v[mid].second < R) n               en = mid; n             else n               st = mid; n           } n           if (v[st].second < R) en = st; n           int who = v[en].second; n           int me = v[en].first; n           if (who < R) n             ans[idx] += par[v.back().second] - par[who] + (R - who) * 1LL * me; n         } n       } n       while (!v.empty() and v.back().first >= val) v.pop_back(); n       if (!v.empty()) { n         int next_ind = v.back().second; n         par[j] = par[next_ind] + (next_ind - j) * 1LL * val; n       } n       v.push_back(make_pair(val, j)); n     } n   } n   for (int i = 1; i <= q; i++) printf( %lld n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n int main() { n   int n, k, sol = 0; n   cin >> n >> k; n   vector<int> a(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a.begin(), a.end()); n   int maxB = (a[n - 1] - a[0]) / (k - 1); n   vector<int> ble(maxB + 2); n   vector<vector<int>> b(n + 1); n   for (int i = 1; i <= maxB; i++) { n     b[0] = vector<int>(k + 1); n     b[0][0] = 1; n     int p = 0; n     for (int j = 1; j <= n; j++) { n       while (a[p] <= a[j - 1] - i) p++; n       b[j] = b[j - 1]; n       for (int l = 0; l < k; l++) { n         b[j][l + 1] = (b[j][l + 1] + b[p][l]) % mod; n         if (b[j][l + 1] == 0) break; n       } n     } n     ble[i] = b[n][k]; n   } n   for (int i = 0; i <= maxB; i++) n     sol = (sol + i * (long long)(ble[i] - ble[i + 1]) + mod) % mod; n   cout << sol << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ans; n long long arr[300005][8]; n long long a, b; n bool ok(long long mid, long long n, long long m) { n   long long mx = (1LL << m) - 1; n   long long cnt[mx + 1]; n   for (int i = 0; i <= mx; i++) cnt[i] = 0; n   for (int i = 0; i < n; i++) { n     long long tem = 0; n     for (int j = 0; j < m; j++) { n       if (arr[i][j] >= mid) { n         tem = tem | (1LL << j); n       } n     } n     cnt[tem] = i + 1; n   } n   for (int i = 0; i <= mx; i++) { n     for (int j = i; j <= mx; j++) { n       if (cnt[i] && cnt[j]) { n         long long tem = i | j; n         if (tem == mx) { n           a = cnt[i]; n           b = cnt[j]; n           return true; n         } n       } n     } n   } n   return false; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(nullptr); n   ; n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) cin >> arr[i][j]; n   } n   long long lo = 0; n   long long hi = 1e9; n   a = 1; n   b = 1; n   while (lo <= hi) { n     long long mid = (lo + hi) / 2; n     if (ok(mid, n, m)) { n       ans = mid; n       lo = mid + 1; n     } else { n       hi = mid - 1; n     } n   } n   cout << a <<     << b << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long best; n int n, m, k; n int main() { n   cin >> n >> m >> k; n   if (k > n + m - 2) n     cout << -1 << endl; n   else { n     for (int area = 1; area <= (int)sqrt(n); area++) { n       int x = n / area; n       if (n / x == area) { n         int cut = min(k, x - 1); n         int remainCut = k - cut; n         if (remainCut + 1 != 0) n           best = max(best, (long long)area * (long long)(m / (remainCut + 1))); n       } n       int _area = n / area; n       x = n / _area; n       if (n / x == _area) { n         int cut = min(k, x - 1); n         int remainCut = k - cut; n         if (remainCut + 1 != 0) n           best = max(best, (long long)_area * (long long)(m / (remainCut + 1))); n       } n     } n     cout << best << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n long long dyn[5001][5001]; n int A[300000]; n int main() { n   int n, k; n   scanf( %d , &(n)); n   scanf( %d , &(k)); n   for (int(i) = 0; (i) < n; (i)++) scanf( %d , &(A[i])); n   sort(A, A + n); n   int rem = n % k; n   int q = n / k; n   for (int i = 0; i <= k; i++) { n     if (i == 0) { n       dyn[0][0] = 0; n       dyn[0][1] = -1; n       continue; n     } n     int j = min(i, rem); n     for (int t = 0; t <= j; t++) { n       dyn[i][t] = 2000000000; n       if (t > 0) { n         dyn[i][t] = min(dyn[i][t], dyn[i - 1][t - 1] + A[i * q + t - 1] - n                                        A[(i - 1) * q + t - 1]); n       } n       if (dyn[i - 1][t] != -1) { n         dyn[i][t] = min(dyn[i][t], n                         dyn[i - 1][t] + A[i * q + t - 1] - A[(i - 1) * q + t]); n       } n     } n     dyn[i][j + 1] = -1; n   } n   printf( %I64d , dyn[k][rem]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const int MAX = 1e9 + 5; n long long i, j, k, n, m, RIGHT[N], LEFT[N], ans, a[N]; n long long _RIGHT[N], _LEFT[N]; n int main() { n   scanf( %lld , &n); n   for (int i = 1; i <= n - 1; i++) scanf( %lld , &a[i]); n   for (int i = n; i >= 1; i--) { n     if (a[i] != 1) RIGHT[i] = RIGHT[i + 1] + a[i] - (a[i] & 1); n     _RIGHT[i] = max(RIGHT[i + 1], _RIGHT[i + 1]) + a[i] - (!(a[i] & 1)); n   } n   for (int i = 1; i <= n; i++) { n     if (a[i - 1] != 1) LEFT[i] = LEFT[i - 1] + a[i - 1] - (a[i - 1] & 1); n     _LEFT[i] = max((a[i] != 1) * LEFT[i - 1], (_LEFT[i - 1])) + a[i - 1] - n                (!(a[i - 1] & 1)); n   } n   for (int i = 1; i <= n; i++) { n     ans = max(ans, max(_LEFT[i], LEFT[i]) + RIGHT[i]); n     ans = max(ans, max(_RIGHT[i], RIGHT[i]) + LEFT[i]); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m, c0, d0; n   cin >> n >> m >> c0 >> d0; n   long long a[m + 4], b[m + 5], c[m + 5], d[m + 5]; n   for (int i = 1; i <= m; i++) { n     cin >> a[i] >> b[i] >> c[i] >> d[i]; n     a[i] /= b[i]; n   } n   c[0] = c0; n   a[0] = 1005; n   long long dp[n + 4][m + 4]; n   memset(dp, 0, sizeof(dp)); n   for (long long i = 1; i <= n; i++) { n     for (long long j = 0; j <= m; j++) { n       for (long long k = 0; k <= a[j]; k++) { n         if (i - c[j] * k < 0) break; n         if (j == 0) { n           dp[i][j] = k * d0; n         } else { n           dp[i][j] = max(dp[i - c[j] * k][j - 1] + k * d[j], dp[i][j]); n         } n       } n     } n   } n   cout << dp[n][m] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int mohin, a, b, c; n   cin >> a >> b >> c; n   mohin = 0; n   for (int i = 1; i <= c; i++) { n     mohin += 2 * ((a - 4 * (i - 1)) + (b - 4 * (i - 1))) - 4; n   } n   cout << mohin << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, c, l; n long long Ans; n int main() { n   cin >> a >> b >> c >> l; n   for (int i = 0; i <= l; i++) { n     long long lim = (a + b + c + i - 1) / 2; n     if (a <= lim && b <= lim && c <= lim) { n       Ans += (long long)(i + 2) * (i + 1) / 2; n       if (a + i > lim) { n         Ans -= (long long)(i + a - lim + 1) * (i + a - lim) / 2; n       } n       if (b + i > lim) { n         Ans -= (long long)(i + b - lim + 1) * (i + b - lim) / 2; n       } n       if (c + i > lim) { n         Ans -= (long long)(i + c - lim + 1) * (i + c - lim) / 2; n       } n     } n   } n   cout << Ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char n[1000100]; n char b[1000100]; n int main() { n   long long result, last, i, B, c, BB[10]; n   int re, C; n   while (scanf( %s %s %d , b, n, &C) != EOF) { n     c = C; n     B = 0; n     for (i = 0; b[i]; i++) B = (B * 10 + b[i] -  0 ) % c; n     if (B == 0) B = c; n     BB[0] = 1; n     for (i = 1; i < 10; i++) BB[i] = (BB[i - 1] * B) % c; n     for (i = strlen(n) - 1; i >= 0; i--) n       if (n[i] !=  0 ) break; n     n[i]--; n     for (i++; n[i]; i++) n[i] =  9 ; n     result = 1; n     for (i = 0; n[i]; i++) { n       last = result; n       result = (result * result) % c; n       result = (result * result) % c; n       result = (result * result) % c; n       result = (result * last) % c; n       result = (result * last) % c; n       result = (result * BB[n[i] -  0 ]) % c; n     } n     result = (result * (B - 1)) % c; n     if (result == 0) result = c; n     re = result; n     printf( %d n , re); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n const int N = 100005; n int i, n, k, x, y, lvl[N]; n vector<int> g[N]; n queue<int> q; n int main() { n   ios_base::sync_with_stdio(0); n   memset(lvl, -1, sizeof(lvl)); n   cin >> n >> k; n   for (i = 1; i < n; ++i) { n     cin >> x >> y; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   for (i = 1; i <= n; ++i) n     if (g[i].size() == 1) q.push(i), lvl[i] = 0; n   for (; !q.empty(); q.pop()) { n     x = q.front(); n     if (lvl[x] == k) continue; n     vector<int> now; n     for (int to : g[x]) n       if (lvl[to] == -1 || lvl[to] == lvl[x] + 1) now.push_back(to); n     if (now.size() > 1) return cout <<  No n , 0; n     if (!now.size()) return cout <<  No n , 0; n     if (g[now[0]].size() < 4 - (lvl[x] + 1 == k)) return cout <<  No n , 0; n     if (lvl[now[0]] == -1) lvl[now[0]] = lvl[x] + 1, q.push(now[0]); n   } n   int cnt = 0; n   for (i = 1; i <= n; ++i) { n     if (lvl[i] > k) return cout <<  No n , 0; n     if (lvl[i] == k) ++cnt; n     if (cnt > 1) return cout <<  No n , 0; n   } n   cout << (!cnt ?  No  :  Yes ) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double ans = 0.0F; n int n, d, m[101] = {0}, r[101] = {0}; n int tmp = 0; n int main(int argc, char** argv, char** env) { n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> m[i]; n   } n   for (int i = 1; i <= n; ++i) { n     cin >> r[i]; n   } n   for (int i = 0; i < (int)1e6; ++i) { n     for (int j = 1; j <= n; ++j) { n       if (i % m[j] == r[j]) { n         tmp++; n         break; n       } n     } n   } n   ans = tmp * 1.0F / 1e6; n   cout << fixed << setprecision(6) << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, x[200001], y[200001], tag[200001], fi[200001 << 1], nt[200001 << 1], n     go[200001 << 1], root; n int cnt, top, low[200001], dfn[200001], id[200001], deg[200001], st[200001], n     ans, cntx, sum, maxx; n void add(int u, int v) { n   nt[++cnt] = fi[u]; n   fi[u] = cnt; n   go[cnt] = v; n } n void tarjan(int u, int fa) { n   dfn[u] = ++cnt; n   low[u] = dfn[u]; n   st[++top] = u; n   int t = 0; n   for (int i = fi[u]; i; i = nt[i]) { n     int v = go[i]; n     if (v == fa && !t) { n       t++; n       continue; n     } n     if (dfn[v]) { n       low[u] = min(low[u], low[v]); n       continue; n     } n     tarjan(v, u); n     low[u] = min(low[u], low[v]); n   } n   if (low[u] == dfn[u]) { n     int y; n     while (y = st[top--]) { n       tag[y] = u; n       if (y == u) break; n     } n   } n } n void dfs1(int u, int dep) { n   dfn[u] = 1; n   for (int i = fi[u]; i; i = nt[i]) { n     int v = go[i]; n     if (dfn[v]) continue; n     dfs1(v, dep + 1); n   } n   if (dep > maxx) root = u, maxx = dep; n } n void dfs2(int u, int dep, int fa) { n   for (int i = fi[u]; i; i = nt[i]) { n     int v = go[i]; n     if (v == fa) continue; n     dfs2(v, dep + 1, u); n   } n   if (dep >= maxx) maxx = dep; n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d %d , &x[i], &y[i]); n     add(x[i], y[i]); n     add(y[i], x[i]); n   } n   for (int i = 1; i <= n; i++) { n     top = 0; n     cnt = 0; n     if (!dfn[i]) tarjan(i, 0); n   } n   memset(fi, 0, sizeof fi); n   cnt = 0; n   top = 0; n   for (int i = 1; i <= n; i++) n     if (tag[i] == i) id[i] = ++top; n   for (int i = 1; i <= m; i++) { n     if (tag[x[i]] != tag[y[i]]) { n       add(id[tag[x[i]]], id[tag[y[i]]]), add(id[tag[y[i]]], id[tag[x[i]]]); n       deg[id[tag[x[i]]]]++; n       deg[id[tag[y[i]]]]++; n     } n   } n   for (int i = 1; i <= top; i++) { n     if (deg[i] == 1) ans++; n   } n   memset(dfn, 0, sizeof dfn); n   for (int i = 1; i <= top; i++) { n     if (!dfn[i] && deg[i] == 0) sum++, ans++; n     if (!dfn[i] && deg[i] != 0) { n       sum++; n       dfs1(i, 1), dfs2(root, 1, 0); n       root = 0; n       ans += maxx - 2; n       maxx = 0; n     } n   } n   sum--; n   ans = sum + n - ans; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline bool cmin(T& a, const T& b) { n   return a > b ? a = b, 1 : 0; n } n template <typename T> n inline bool cmax(T& a, const T& b) { n   return a < b ? a = b, 1 : 0; n } n const int MaxN = 205; n unsigned int n, d, M, c[MaxN][MaxN], f[MaxN][MaxN], g[MaxN]; n int main() { n   cin >> n >> d >> M; n   c[0][0] = 1; n   for (int i = 1; i <= n; i++) { n     c[i][0] = 1; n     for (int j = 1; j <= i; j++) c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % M; n   } n   f[1][0] = 1; n   for (int i = 2; i <= n; i++) { n     for (int k = 0; k < d; k++) (g[i - 1] += f[i - 1][k]) %= M; n     for (int j = 1; j < i; j++) n       for (int k = 0; k < d; k++) n         f[i][k + 1] = (f[i][k + 1] + n                        (long long)f[i - j][k] * g[j] % M * c[i - 2][j - 1]) % n                       M; n   } n   int ans = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j <= d; j++) n       for (int k = 0; j + k <= d; k++) { n         if (k == 1) continue; n         ans = (ans + (long long)f[i + 1][j] * f[n - i][k]) % M; n       } n   ans = (long long)ans * 2 * n * (n - 1) % M; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, a, b, i, j; n   cin >> n >> a >> b; n   for (i = 0; i <= 10000000; i++) { n     if (n - a * i >= 0) { n       if ((n - a * i) % b == 0) { n         cout <<  YES  n              <<   n ; n         cout << i <<     << (n - a * i) / b; n         return 0; n       } n     } n   } n   cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 3e5 + 5; n int N, M, K; n int A[MAXN]; n long long P, H; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> N; n   for (int i = 1; i <= N; i++) { n     cin >> P; n     A[i] = A[i - 1] + __builtin_popcountll(P); n     M = 0; n     for (int j = i - 1; j >= 0; j--) { n       if (A[i] - A[j] >= 118) { n         break; n       } n       M = max(M, A[j + 1] - A[j]); n       if ((A[i] - A[j]) & 1) { n         continue; n       } n       if (A[i] - A[j] < 2 * M) { n         H--; n       } n     } n     if (A[i] & 1) { n       H += K; n       K++; n     } else { n       H += i - K; n     } n   } n   cout << H <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool exceed(long long x, long long y, long long m) { return x >= m / y + 1; } n long long n, k; n string s; n long long dp[1005][2005]; n int zero = 1002; n int main(void) { n   ios::sync_with_stdio(0); n   cin >> n >> k >> s; n   s =  #  + s; n   for (long long(i) = (0); (i) <= (n); (i)++) n     for (long long(j) = (-k); (j) <= (k); (j)++) dp[i][zero + j] = -1e18; n   dp[0][zero + 0] = 0; n   for (long long(i) = (0); (i) <= (n - 1); (i)++) { n     for (long long(j) = (-k); (j) <= (k); (j)++) { n       for (long long(l) = (-1); (l) <= (1); (l)++) { n         if (dp[i][zero + j] < -1e18 / 2) continue; n         if (s[i + 1] !=  ? ) { n           if (s[i + 1] !=  W  && l == 1) continue; n           if (s[i + 1] !=  L  && l == -1) continue; n           if (s[i + 1] !=  D  && l == 0) continue; n         } n         long long nj = j + l; n         if (abs(nj) > k) continue; n         if (i + 1 != n && abs(nj) == k) continue; n         dp[i + 1][zero + nj] = j; n       } n     } n   } n   long long p = -1e18; n   if (dp[n][zero + k] > -1e18 / 2) p = k; n   if (dp[n][zero - k] > -1e18 / 2) p = -k; n   if (p < -1e18 / 2) { n     cout <<  NO  << endl; n     return 0; n   } n   for (int i = n; i >= 1; i--) { n     if (dp[i][zero + p] == p) s[i] =  D ; n     if (dp[i][zero + p] < p) s[i] =  W ; n     if (dp[i][zero + p] > p) s[i] =  L ; n     p = dp[i][zero + p]; n   } n   cout << s.substr(1) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n int n, log2n, q, C[N], L[N], R[N], tot = 0, f[N][25], up[N], de2[N]; n long long de[N]; n struct node { n   long long min, sign; n } a[N * 4]; n pair<int, int> E[2 * N]; n vector<pair<int, int> > e[N]; n void add1(int x, int k) { n   for (; x <= n; x += x & (-x)) de[x] += k; n } n long long ask1(int x) { n   long long res = 0; n   for (; x; x -= x & (-x)) res += de[x]; n   return res; n } n void build(int l, int r, int now) { n   if (l == r) { n     a[now].min = ask1(l) + up[C[l]]; n     return; n   } n   int mid = (l + r) >> 1; n   build(l, mid, now << 1); n   build(mid + 1, r, now << 1 | 1); n   a[now].min = min(a[now << 1].min, a[now << 1 | 1].min); n } n void update(int l, int r, int now, int k) { n   a[now].min += k; n   a[now].sign += k; n } n void down(int l, int r, int now) { n   int mid = (l + r) >> 1; n   update(l, mid, now << 1, a[now].sign); n   update(mid + 1, r, now << 1 | 1, a[now].sign); n   a[now].sign = 0; n } n void add2(int l, int r, int now, int s, int t, int k) { n   if (s <= l && r <= t) { n     update(l, r, now, k); n     return; n   } n   down(l, r, now); n   int mid = (l + r) >> 1; n   if (s <= mid) add2(l, mid, now << 1, s, t, k); n   if (t > mid) add2(mid + 1, r, now << 1 | 1, s, t, k); n   a[now].min = min(a[now << 1].min, a[now << 1 | 1].min); n } n long long ask2(int l, int r, int now, int s, int t) { n   if (s <= l && r <= t) return a[now].min; n   down(l, r, now); n   int mid = (l + r) >> 1; n   long long res = 0x3f3f3f3f3f3f3f3f; n   if (s <= mid) res = min(ask2(l, mid, now << 1, s, t), res); n   if (t > mid) res = min(ask2(mid + 1, r, now << 1 | 1, s, t), res); n   return res; n } n void dfs1(int u, int fa) { n   f[u][0] = fa; n   C[++tot] = u; n   L[u] = tot; n   for (int i = 0; i < e[u].size(); i++) { n     int v = e[u][i].first; n     if (v == fa) continue; n     dfs1(v, u); n   } n   R[u] = tot; n } n void dfs2(int u, int fa) { n   for (int i = 0; i < e[u].size(); i++) { n     int v = e[u][i].first, w = e[u][i].second; n     if (v == fa) continue; n     de2[v] = de2[u] + 1; n     add1(L[v], w); n     add1(R[v] + 1, -w); n     dfs2(v, u); n   } n } n bool on(int u, int v) { n   swap(u, v); n   long long k = de2[u] - de2[v]; n   if (k < 0) return false; n   for (int i = 0; k; i++, k >>= 1) n     if (k & 1) u = f[u][i]; n   return (u == v); n } n int main() { n   std::ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> q; n   log2n = log2(n); n   for (int i = 1; i <= 2 * n - 2; i++) { n     int u, v, w; n     cin >> u >> v >> w; n     if (i < n) n       E[i] = {w, v}; n     else n       E[i] = {w, u}, up[u] = w; n     if (i >= n) continue; n     e[u].push_back({v, w}); n     e[v].push_back({u, w}); n   } n   dfs1(1, 0); n   dfs2(1, 0); n   for (int j = 1; j <= log2n; j++) n     for (int i = 1; i <= n; i++) f[i][j] = f[f[i][j - 1]][j - 1]; n   build(1, n, 1); n   for (int i = 0; i < q; i++) { n     int c, x, y; n     cin >> c >> x >> y; n     if (c == 1) { n       if (x < n) { n         add1(L[E[x].second], -E[x].first + y); n         add1(R[E[x].second] + 1, E[x].first - y); n         add2(1, n, 1, L[E[x].second], R[E[x].second], -E[x].first + y); n       } else n         add2(1, n, 1, L[E[x].second], L[E[x].second], -E[x].first + y); n       E[x].first = y; n     } else { n       if (on(x, y)) n         cout << ask1(L[y]) - ask1(L[x]) << endl; n       else n         cout << ask2(1, n, 1, L[x], R[x]) - ask1(L[x]) + ask1(L[y]) << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n const long long pinf = ((long long)2e18); n const long long ninf = ((long long)-2e18); n const long long mod = 1000000007; n using namespace std; n long long power(long long x, long long n, long long M) { n   if (n == 0) n     return 1; n   else if (n % 2 == 0) n     return power((x * x) % M, n / 2, M); n   else n     return (x * power((x * x) % M, (n - 1) / 2, M)) % M; n } n bool vowel(char c) { n   return (c ==  a  or c ==  e  or c ==  i  or c ==  o  or c ==  u  or n           c ==  A  or c ==  E  or c ==  I  or c ==  U  or c ==  O ); n } n int32_t main() { n   ; n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     vector<long long> a(n), b(n); n     map<long long, long long> make_pair; n     for (long long i = 0; i < n; i++) { n       cin >> a[i]; n       make_pair[a[i]]++; n       b[i] = a[i]; n     } n     sort(b.begin(), b.end()); n     long long p = 0; n     long long ans = 0; n     long long l = 0, r = n - 1; n     while (l < r) { n       if (make_pair[b[p]]) { n         if (a[l] == b[p]) { n           make_pair[a[l]]--; n           p++; n           l++; n         } else if (a[r] == b[p]) { n           make_pair[a[r]]--; n           r--; n           p++; n         } else { n           make_pair[a[l]]--; n           l++; n           ans = l; n         } n       } else { n         p++; n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:256000000 ) n #pragma gcc optimize( O3 ) n #pragma gcc target( sse4 ) n using namespace std; n mt19937 rng(2391); n mt19937 gnr(chrono::high_resolution_clock::now().time_since_epoch().count()); n template <typename A> n istream& operator>>(istream& fin, vector<A>& v) { n   for (auto it = v.begin(); it != v.end(); ++it) fin >> *it; n   return fin; n } n template <typename A, typename B> n istream& operator>>(istream& fin, pair<A, B>& p) { n   fin >> p.first >> p.second; n   return fin; n } n template <typename A, typename B> n pair<A, B> operator+(const pair<A, B>& a, const pair<A, B>& b) { n   return make_pair(a.first + b.first, a.second + b.second); n } n template <typename A, typename B> n pair<A, B> operator+=(pair<A, B>& a, const pair<A, B>& b) { n   a.first += b.first; n   a.second += b.second; n   return a; n } n template <typename A, typename B> n pair<A, B> operator-(const pair<A, B>& a, const pair<A, B>& b) { n   return make_pair(a.first - b.first, a.second - b.second); n } n template <typename A, typename B> n pair<A, B> operator-(const pair<A, B>& a) { n   return make_pair(-a.first, -a.second); n } n template <typename A, typename B> n pair<A, B>& operator++(pair<A, B>& a) { n   ++a.first; n   ++a.second; n   return a; n } n template <typename A, typename B> n pair<A, B>& operator--(pair<A, B>& a) { n   --a.first; n   --a.second; n   return a; n } n template <typename A> n vector<A>& operator++(vector<A>& a) { n   for (auto it = a.begin(); it != a.end(); ++it) ++*it; n   return a; n } n template <typename A> n vector<A>& operator--(vector<A>& a) { n   for (auto it = a.begin(); it != a.end(); ++it) --*it; n   return a; n } n template <typename A, typename B> n pair<A, B> operator++(pair<A, B>& a, int) { n   pair<A, B> b = a; n   ++a; n   return b; n } n template <typename A, typename B> n pair<A, B> operator--(pair<A, B>& a, int) { n   pair<A, B> b = a; n   --a; n   return b; n } n template <typename A> n vector<A>& operator++(vector<A>& a, int) { n   vector<A> b = a; n   ++a; n   return b; n } n template <typename A> n vector<A>& operator--(vector<A>& a, int) { n   vector<A> b = a; n   --a; n   return b; n } n vector<vector<int>> adjlist_from_edgelist(const vector<pair<int, int>>& e, n                                           const int& n) { n   vector<vector<int>> g(n); n   for (auto it = e.begin(); it != e.end(); ++it) { n     g[it->first].push_back(it->second); n     g[it->second].push_back(it->first); n   } n   return g; n } n template <typename A, typename B> n pair<A, B> operator-=(pair<A, B>& a, const pair<A, B>& b) { n   a.first -= b.first; n   a.second -= b.second; n   return a; n } n template <typename A> n A operator*(const pair<A, A>& p, const pair<A, A>& q) { n   return p.first * q.first + p.second * q.second; n } n template <typename A> n pair<A, A> operator*(const pair<A, A>& p, const A& q) { n   return make_pair(p.first * q, p.second * q); n } n template <typename A> n A operator%(const pair<A, A>& p, const pair<A, A>& q) { n   return p.first * q.second - p.second * q.first; n } n template <typename A> n A sq_len(const pair<A, A>& p) { n   return p * p; n } n template <typename A> n A sq_dist(const pair<A, A>& p, const pair<A, A>& q) { n   return sq_len(p - q); n } n template <typename A> n double len(const pair<A, A>& p) { n   return sqrt(sq_len(p)); n } n template <typename A> n double dist(const pair<A, A>& p, const pair<A, A>& q) { n   return len(p - q); n } n template <typename A> n bool is_rhombus(const pair<A, A>& a, const pair<A, A>& b, const pair<A, A>& c, n                 const pair<A, A>& d) { n   A x = sq_dist(a, b); n   A y = sq_dist(b, c); n   A z = sq_dist(c, d); n   A t = sq_dist(d, a); n   return ((x == y) && (y == z) && (z == t)); n } n template <typename A> n bool is_rectangle(const pair<A, A>& a, const pair<A, A>& b, const pair<A, A>& c, n                   const pair<A, A>& d) { n   pair<A, A> x = a - b, y = b - c, z = c - d, t = d - a; n   return ((x * y == 0) && (y * z == 0) && (z * t == 0) && (t * x == 0)); n } n template <typename A> n bool are_parallel(const pair<A, A>& a, const pair<A, A>& b) { n   return (a % b == 0); n } n template <typename A> n bool are_orthogonal(const pair<A, A>& a, const pair<A, A>& b) { n   return (a * b == 0); n } n template <typename A> n bool are_codirected(const pair<A, A>& a, const pair<A, A>& b) { n   return (are_parallel(a, b) && (a * b >= 0)); n } n template <typename A> n bool is_parallelogram(const pair<A, A>& a, const pair<A, A>& b, n                       const pair<A, A>& c, const pair<A, A>& d) { n   return ((a - b) == (d - c)); n } n template <typename A> n bool is_trapezoid(const pair<A, A>& a, const pair<A, A>& b, const pair<A, A>& c, n                   const pair<A, A>& d) { n   pair<A, A> x = a - b, z = d - c; n   return are_codirected(x, z); n } n template <typename A> n bool is_convex_polygon(const pair<A, A>& a, const pair<A, A>& b, n                        const pair<A, A>& c, const pair<A, A>& d) { n   pair<A, A> x = a - b, y = b - c, z = c - d, t = d - a; n   A p = x % y, q = y % z, r = z % t, s = t % x; n   return (((p >= 0) && (q >= 0) && (r >= 0) && (s >= 0)) || n           ((p <= 0) && (q <= 0) && (r <= 0) && (s <= 0))); n } n template <typename A> n bool nprcs(const pair<A, A>& a, const pair<A, A>& c) { n   return ((a % c) >= 0); n } n template <typename A> n bool nprcs(const pair<A, A>& a, const pair<A, A>& b, const pair<A, A>& c) { n   return nprcs(a - b, a - c); n } n template <typename A> n bool npocs(const pair<A, A>& a, const pair<A, A>& c) { n   return ((a % c) <= 0); n } n template <typename A> n bool npocs(const pair<A, A>& a, const pair<A, A>& b, const pair<A, A>& c) { n   return npocs(a - b, a - c); n } n template <typename A> n bool prcs(const pair<A, A>& a, const pair<A, A>& c) { n   return ((a % c) > 0); n } n template <typename A> n bool prcs(const pair<A, A>& a, const pair<A, A>& b, const pair<A, A>& c) { n   return prcs(a - b, a - c); n } n template <typename A> n bool pocs(const pair<A, A>& a, const pair<A, A>& c) { n   return ((a % c) < 0); n } n template <typename A> n bool pocs(const pair<A, A>& a, const pair<A, A>& b, const pair<A, A>& c) { n   return pocs(a - b, a - c); n } n template <typename A> n bool different_sides(const pair<A, A>& a, const pair<A, A>& b, n                      const pair<A, A>& c, const pair<A, A>& d) { n   pair<A, A> x = b - a; n   A p = x % (c - b), q = x % (d - b); n   return (((p >= 0) && (q <= 0)) || ((p <= 0) && (q >= 0))); n } n template <typename A> n bool sharply_different_sides(const pair<A, A>& a, const pair<A, A>& b, n                              const pair<A, A>& c, const pair<A, A>& d) { n   pair<A, A> x = b - a; n   A p = x % (c - b), q = x % (d - b); n   return (((p > 0) && (q < 0)) || ((p < 0) && (q > 0))); n } n template <typename A> n pair<A, A> rot_90(const pair<A, A>& p) { n   return make_pair(-p.second, p.first); n } n template <typename A> n pair<A, A> rot_90(const pair<A, A>& p, const pair<A, A>& c) { n   return c + rot_90(p - c); n } n template <typename A> n bool intersecting_segments(const pair<A, A>& a, const pair<A, A>& b, n                            const pair<A, A>& c, const pair<A, A>& d) { n   return different_sides(a, b, c, d) && different_sides(c, d, a, b); n } n template <typename A> n bool sharply_intersecting_segments(const pair<A, A>& a, const pair<A, A>& b, n                                    const pair<A, A>& c, const pair<A, A>& d) { n   return sharply_different_sides(a, b, c, d) && n          sharply_different_sides(c, d, a, b); n } n template <typename A> n pair<pair<A, A>, A> line_with_normal_vector_through_point( n     const pair<A, A>& normal, const pair<A, A>& point) { n   return make_pair(normal, -(normal * point)); n } n template <typename A> n pair<pair<A, A>, A> serper(const pair<A, A>& a, const pair<A, A>& b) { n   pair<A, A> p = b - a; n   return make_pair(p + p, -(p * (a + b))); n } n pair<double, double> ints(const pair<double, double>& p, const double& a, n                           const pair<double, double>& q, const double& b) { n   double D = p % q; n   double E = n       pair<double, double>(-a, p.second) % pair<double, double>(-b, q.second); n   double F = n       pair<double, double>(p.first, -a) % pair<double, double>(q.first, -b); n   return pair<double, double>(E / D, F / D); n } n pair<double, double> circumcenter(const pair<double, double>& x, n                                   const pair<double, double>& y, n                                   const pair<double, double>& z) { n   auto p1 = serper(x, y), p2 = serper(x, z); n   return ints(p1.first, p1.second, p2.first, p2.second); n } n template <typename A> n pair<pair<A, A>, A> l_th_2(const pair<A, A>& p, const pair<A, A>& q) { n   return make_pair(make_pair(q.second - p.second, p.first - q.first), -p % q); n } n template <typename A> n vector<pair<double, double>> circle_intersection(const pair<pair<A, A>, A>& a, n                                                  const pair<pair<A, A>, A>& b) { n   pair<A, A> c = b.first - a.first; n   A rr1 = a.second * a.second, rr2 = b.second * b.second, cc = c * c, n     rrrr1 = rr1 * rr1, rrrr2 = rr2 * rr2, cccc = cc * cc, n     D = 2 * (rr1 * rr2 + rr2 * cc + cc * rr1) - (rrrr1 + rrrr2 + cccc); n   if (D >= 0) { n     double E = (((double)(rr1 - rr2)) / cc + 1) / 2; n     pair<double, double> baza = n         pair<double, double>(a.first.first, a.first.second) + n         pair<double, double>(c.first, c.second) * E; n     if (D) { n       double lll = sqrt((double)(D)) / (2 * cc); n       pair<A, A> cr = rot_90(c); n       pair<double, double> pmm = n           pair<double, double>(cr.first, cr.second) * lll; n       return {baza + pmm, baza - pmm}; n     } else n       return vector<pair<double, double>>(1, baza); n   } else n     return vector<pair<double, double>>(); n } n template <typename A, typename B> n pair<A, B> sopr(const pair<A, B>& p) { n   return make_pair(p.first, -p.second); n } n template <typename A> n bool on_segment(const pair<A, A>& a, const pair<A, A>& b, const pair<A, A>& c) { n   return are_codirected(b - a, c - b); n } n template <typename A> n pair<pair<A, A>, A> perpendicular(const pair<A, A>& line, n                                   const pair<A, A>& point) { n   return line_with_normal_vector_through_point(rot_90(line), point); n } n pair<double, double> projection(const pair<pair<double, double>, double>& line, n                                 const pair<double, double>& point) { n   pair<pair<double, double>, double> temp = perpendicular(line.first, point); n   return ints(line.first, line.second, temp.first, temp.second); n } n pair<double, double> height(const pair<double, double>& a, n                             const pair<double, double>& b, n                             const pair<double, double>& c) { n   return projection(l_th_2(a, c), b); n } n pair<double, double> unitvector(const pair<double, double>& a) { n   return a * (1 / len(a)); n } n template <typename T> n vector<int> z_function(const vector<T>& s) { n   int n = ((int)(s.size())); n   vector<int> z(n); n   int l = 0, r = 1; n   for (int i = 1; i < n; ++i) { n     z[i] = max(0, min(r - i, z[i - l])); n     while (i + z[i] < n && (s[i + z[i]] == s[z[i]])) ++z[i]; n     if (r < i + z[i]) { n       l = i; n       r = i + z[i]; n     } n   } n   return z; n } n pair<int, int> euc(const int& m, const int& n) { n   if (n == 0) return pair<int, int>((m >= 0) ? 1 : -1, 0); n   int t = m / n; n   pair<int, int> ans1 = euc(n, m - t * n); n   return pair<int, int>(ans1.second, ans1.first - ans1.second * t); n } n int prod(const int& a, const int& b, const int& M) { n   return ((long long)(a)) * b % M; n } n int sum(const int& a, const int& b, const int& M) { n   int c = a + b; n   return c >= M ? c - M : c; n } n int raz(const int& a, const int& b, const int& M) { n   int c = a - b; n   return c < 0 ? c + M : c; n } n const int LITTLE_BORDER = 400; n struct factorizator { n   int N; n   vector<int> pr; n   vector<int> md; n   vector<int> pw; n   vector<int> without_md; n   factorizator(const int& n) : N(n), md(n), pw(n), without_md(n, 1) { n     for (int i = 2; i < N; ++i) md[i] = i; n     for (int i = 2; i < N; ++i) { n       if (md[i] == i) pr.push_back(i); n       bool worth = true; n       for (int j = 0; worth && (j < ((int)(pr.size()))) && (pr[j] <= md[i]); n            ++j) { n         long long temp = ((long long)(pr[j])) * i; n         if (temp < N) n           md[((int)temp)] = pr[j]; n         else n           worth = false; n       } n     } n     for (int i = 2; i < N; ++i) { n       int t = md[i], s = i / t; n       if (md[s] == t) { n         pw[i] = 1 + pw[s]; n         without_md[i] = without_md[s]; n       } else { n         pw[i] = 1; n         without_md[i] = s; n       } n     } n   } n   void known_factorization(int n, vector<pair<unsigned long long, int>>& v) { n     while (n > 1) { n       v.emplace_back(md[n], pw[n]); n       n = without_md[n]; n     } n   } n   vector<pair<unsigned long long, int>> pollardRho(const unsigned long long& n); n   vector<pair<unsigned long long, int>> factor(unsigned long long n); n   template <typename T> n   vector<pair<T, int>> merge(const vector<pair<T, int>>& p1, n                              const vector<pair<T, int>>& p2) { n     vector<pair<T, int>> ans; n     int m = ((int)(p1.size())); n     int n = ((int)(p2.size())); n     int i = 0; n     int j = 0; n     while ((i < m) || (j < n)) { n       if (i < m) { n         if (j < n) { n           if (p1[i].first < p2[j].first) n             ans.push_back(p1[i++]); n           else if (p1[i].first > p2[j].first) n             ans.push_back(p2[j++]); n           else { n             ans.emplace_back(p1[i].first, p1[i].second + p2[j].second); n             ++i; n             ++j; n           } n         } else n           ans.push_back(p1[i++]); n       } else n         ans.push_back(p2[j++]); n     } n     return ans; n   } n }; n factorizator fac(LITTLE_BORDER); n unsigned long long experimental_prodll(const unsigned long long& a, n                                        const unsigned long long& b, n                                        const unsigned long long& M, n                                        const int& helper) { n   if (M == 0) return 0; n   unsigned long long q = ((long double)a) * b / M; n   unsigned long long a1 = a * b - q * M; n   unsigned long long a2 = a1; n   int rm = M % helper; n   int ra = a % helper; n   int rb = b % helper; n   int rq = q % helper; n   int r1 = raz(prod(ra, rb, helper), prod(rq, rm, helper), helper); n   int r2 = r1; n   while (true) { n     if (a1 % helper == r1 && a1 < M) return a1; n     a1 -= M; n     r1 = raz(r1, rm, helper); n     a2 += M; n     r2 = sum(r2, rm, helper); n     if (a2 % helper == r2 && a2 < M) return a2; n   } n } n inline int find_helper(const unsigned long long& M) { n   int i = 30; n   while (M % fac.pr[i] == 0) ++i; n   return fac.pr[i]; n } n unsigned long long prodll(const unsigned long long& a, n                           const unsigned long long& b, n                           const unsigned long long& M) { n   if (M == 0) return 0; n   unsigned long long helper = find_helper(M); n   return experimental_prodll(a, b, M, helper); n } n unsigned long long sumll(const unsigned long long& a, n                          const unsigned long long& b, n                          const unsigned long long& M) { n   if (a < M - b) n     return a + b; n   else n     return a + b - M; n } n unsigned long long razll(const unsigned long long& a, n                          const unsigned long long& b, n                          const unsigned long long& M) { n   if (a >= b) n     return a - b; n   else n     return a + M - b; n } n template <typename T> n T diff(const T& a, const T& b) { n   return (a > b) ? (a - b) : (b - a); n } n template <typename A> n bool angdis_cmp(const pair<A, A>& a, const pair<A, A>& b) { n   A p; n   if (p = a % b) n     return (p > 0); n   else n     return sq_len(a) < sq_len(b); n } n template <typename T> n int find_min_idx(const vector<T>& v) { n   int ans = 0, n = ((int)(v.size())); n   for (int i = 1; i < n; ++i) n     if (v[i] < v[ans]) ans = i; n   return ans; n } n template <typename T> n vector<int> convex_hull(vector<pair<T, T>>& a) { n   int n = ((int)(a.size())); n   if (n) { n     int mt = find_min_idx(a); n     pair<T, T> d = a[mt]; n     for (int i = 0; i < n; ++i) a[i] -= d; n     vector<int> idx(n); n     for (int i = 0; i < n; ++i) idx[i] = i; n     sort(idx.begin(), idx.end(), n          [&](const int& l, const int& r) { return angdis_cmp(a[l], a[r]); }); n     vector<int> h(1, idx.front()); n     for (auto it = idx.begin() + 1; it != idx.end(); ++it) { n       auto temp = a.begin() + *it; n       if (((int)(h.size())) >= 2) { n         if (are_parallel(a[h.back()], *temp)) h.pop_back(); n         while ((((int)(h.size())) >= 3) && n                npocs(a[h[((int)(h.size())) - 2]], a[h.back()], *temp)) n           h.pop_back(); n       } n       h.push_back(*it); n     } n     for (int i = 0; i < n; ++i) a[i] += d; n     return h; n   } else n     return vector<int>(); n } n pair<int, int> cool_gcd(const int& a, const int& b) { n   if (b) { n     int c = a / b; n     pair<int, int> ans1 = cool_gcd(b, a - b * c); n     return pair<long long, long long>(ans1.second, n                                       ans1.first - ans1.second * c); n   } else n     return pair<int, int>(1, 0); n } n pair<long long, long long> cool_gcdll(const long long& a, const long long& b) { n   if (b) { n     long long c = a / b; n     pair<long long, long long> ans1 = cool_gcdll(b, a - b * c); n     return pair<long long, long long>(ans1.second, n                                       ans1.first - ans1.second * c); n   } else n     return pair<long long, long long>(1ll, 0ll); n } n template <typename T> n T gcd(T a, T b) { n   while (b) { n     a %= b; n     swap(a, b); n   } n   return a; n } n long long pr_p(const long long& a, const long long& b, const long long& p) { n   if (b < 1000000) return (a * b) % p; n   long long temp = pr_p(a, b >> 1ll, p); n   temp = sumll(temp, temp, p); n   if (b & 1ll) n     return sumll(temp, a, p); n   else n     return temp; n } n long long po_p(const long long& a, const long long& b, const long long& p) { n   if (b < 2) { n     if (b == 0) n       return 1; n     else n       return a; n   } n   long long temp = po_p(a, b >> 1ll, p); n   temp = pr_p(temp, temp, p); n   if (b & 1ll) n     return pr_p(temp, a, p); n   else n     return temp; n } n int pow_mod(const int& a, const int& b, const int& p) { n   if (b < 2) { n     if (b == 0) n       return 1; n     else n       return a; n   } n   int temp = pow_mod(a, b >> 1, p); n   temp = prod(temp, temp, p); n   if (b & 1) n     return prod(temp, a, p); n   else n     return temp; n } n long long pow_modll(const long long& a, const long long& b, n                     const long long& p) { n   if (b < 2) { n     if (b == 0) n       return 1; n     else n       return a; n   } n   long long temp = pow_modll(a, b >> 1ll, p); n   temp = prodll(temp, temp, p); n   if (b & 1ll) n     return prodll(temp, a, p); n   else n     return temp; n } n int inverse(int a, int n) { n   int c = cool_gcd(a, n).first; n   if (c < 0) c += n; n   return c; n } n long long inversell(long long a, long long n) { n   long long c = cool_gcdll(a, n).first; n   if (c < 0) c += n; n   return c; n } n template <typename A> n pair<int, int> equal_elements(const vector<A>& u, const vector<A>& v) { n   pair<int, int> ans(INT_MAX, INT_MAX); n   int m = ((int)(u.size())), n = ((int)(v.size())); n   vector<int> id_u(m); n   for (int i = 1; i < m; ++i) id_u[i] = i; n   vector<int> id_v(n); n   for (int i = 1; i < n; ++i) id_v[i] = i; n   sort(id_u.begin(), id_u.end(), n        [&](const int& x, const int& y) { return u[x] < u[y]; }); n   sort(id_v.begin(), id_v.end(), n        [&](const int& x, const int& y) { return v[x] < v[y]; }); n   int i = 0; n   int j = 0; n   while ((i < m) && (j < n)) { n     if (u[id_u[i]] < v[id_v[j]]) n       ++i; n     else if (v[id_v[j]] < u[id_u[i]]) n       ++j; n     else { n       ans = min(ans, pair<int, int>(id_v[j], id_u[i])); n       ++j; n     } n   } n   if (ans.first == INT_MAX) n     return pair<int, int>(-1, -1); n   else n     return pair<int, int>(ans.second, ans.first); n } n long long discr_log(long long a, long long b, long long n) { n   int k = ((int)(sqrt((long double)n))); n   long long a1 = inversell(a, n); n   int l = k + 20; n   long long a2 = po_p(a1, k, n); n   vector<long long> seq1(k), seq2(l); n   seq1.front() = 1; n   for (int i = 1; i < k; ++i) seq1[i] = pr_p(seq1[i - 1], a, n); n   seq2.front() = b; n   for (int i = 1; i < l; ++i) seq2[i] = pr_p(seq2[i - 1], a2, n); n   pair<long long, long long> e = equal_elements(seq1, seq2); n   if (e.first == -1) n     return -1; n   else n     return e.first + e.second * k; n } n long long common_discr_log(long long a, long long b, long long n) { n   const int C = 70; n   a %= n; n   b %= n; n   if (gcd(n, a) != 1) { n     for (int i = 0; i < C; ++i) { n       if (po_p(a, i, n) == b) { n         return i; n       } n     } n     long long multp = po_p(a, C, n); n     long long g = gcd(multp, n); n     if (b % g) { n       return -1; n     } else { n       b /= g; n       n /= g; n       multp /= g; n       long long pop_back = inversell(multp, n); n       b = pr_p(b, pop_back, n); n       long long ans = discr_log(a, b, n); n       if (ans == -1) n         return -1; n       else n         return ans + C; n     } n   } else n     return discr_log(a, b, n); n } n const unsigned long long minimum_composites[] = {1ull, n                                                  2046ull, n                                                  1373652ull, n                                                  25326000ull, n                                                  3215031750ull, n                                                  2152302898746ull, n                                                  3474749660382ull, n                                                  341550071728320ull, n                                                  341550071728320ull, n                                                  3825123056546413050ull, n                                                  3825123056546413050ull, n                                                  18446744073709551615ull}; n const int little_primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}; n bool miller_rabin_check(const unsigned long long& n, n                         const unsigned long long& prime, n                         const unsigned long long& odd, n                         const unsigned long long& pot, n                         const unsigned long long& nmo) { n   long long x = pow_modll(prime, odd, n); n   if ((x == nmo) || (x == 1)) return true; n   for (int i = 1; i < pot; ++i) { n     x = prodll(x, x, n); n     if (x == nmo) return true; n     if (x == 1) return false; n   } n   return false; n } n bool is_prime(const unsigned long long& n) { n   if (n <= 2) return (n == 2); n   unsigned long long odd, pot, nmo; n   nmo = n - 1; n   odd = nmo; n   pot = 0; n   while ((odd & 1ll) == 0) { n     odd >>= 1ll; n     ++pot; n   } n   int i = 0; n   while (n > minimum_composites[i]) { n     if (miller_rabin_check(n, little_primes[i], odd, pot, nmo) == false) n       return false; n     ++i; n   } n   return true; n } n vector<pair<unsigned long long, int>> factorizator::pollardRho( n     const unsigned long long& n) { n   if (is_prime(n)) n     return vector<pair<unsigned long long, int>>( n         1, pair<unsigned long long, int>(n, 1)); n   int step = 1; n   while (true) { n     step <<= 1; n     unsigned long long a = rng(); n     unsigned long long b = a; n     unsigned long long d = 1; n     for (int j = 0; j < step; ++j) { n       a = sumll(1, prodll(a, a, n), n); n       b = sumll(1, prodll(b, b, n), n); n       b = sumll(1, prodll(b, b, n), n); n       d = gcd(diff(a, b), n); n       if (d > 1) { n         break; n       } n     } n     if (1 < d && d < n) return merge(pollardRho(d), pollardRho(n / d)); n   } n   return vector<pair<unsigned long long, int>>(); n } n vector<pair<unsigned long long, int>> factorizator::factor( n     unsigned long long n) { n   vector<pair<unsigned long long, int>> ans; n   for (int i = 0; n >= N && i < ((int)(pr.size())) && pr[i] < LITTLE_BORDER; n        ++i) n     while (n % pr[i] == 0) { n       if (((int)(ans.size())) && ans.back().first == pr[i]) n         ++ans.back().second; n       else n         ans.emplace_back(pr[i], 1); n       n /= pr[i]; n     } n   if (n < N) { n     known_factorization(n, ans); n     return ans; n   } else n     return merge(ans, pollardRho(n)); n } n template <typename T> n T phi(const vector<pair<T, int>>& v) { n   T ans = 1; n   for (int i = 0; i < ((int)(v.size())); ++i) { n     ans *= v[i].first - 1; n     for (int j = 1; j < v[i].second; ++j) ans *= v[i].first; n   } n   return ans; n } n unsigned long long phi(const unsigned long long& n) { n   return phi(fac.factor(n)); n } n bool check_primitive_root(const int& ph, const vector<int>& to_check, n                           const int& r, const int& n) { n   for (int i = 0; i < ((int)(to_check.size())); ++i) n     if (pow_mod(r, to_check[i], n) == 1) return false; n   return (pow_mod(r, ph, n) == 1); n } n int primitive_root(const int& n) { n   if (n < 3) return n - 1; n   int p = phi(n); n   vector<pair<unsigned long long, int>> f = fac.factor(p); n   vector<int> to_check(((int)(f.size()))); n   for (int i = 0; i < ((int)(f.size())); ++i) to_check[i] = p / f[i].first; n   for (int i = 2; i < n; ++i) n     if (check_primitive_root(p, to_check, i, n)) return i; n   return -1; n } n int unite_mod(const int& a, const int& p, const int& b, const int& q) { n   pair<int, int> c = cool_gcd(p, q); n   int pr = p * q; n   int ans = ((a * c.second * q + b * c.first * p) % pr + pr) % pr; n   return ans; n } n long long unite_modll(const long long& a, const long long& p, n                       const long long& b, const long long& q) { n   pair<long long, long long> c = cool_gcdll(p, q); n   long long pr = p * q; n   long long ans = ((a * c.second * q + b * c.first * p) % pr + pr) % pr; n   return ans; n } n pair<int, int> power_v(int n, const int& p) { n   int ans = 0; n   while (n % p == 0) { n     n /= p; n     ++ans; n   } n   return pair<int, int>(ans, n); n } n int square_root_prime_modulo(int c, int n, const int& pr, const int& k) { n   c %= n; n   if (c) { n     pair<int, int> kek = power_v(c, pr); n     int l = kek.first; n     if (l & 1) return -1; n     if (l > 0) { n       int pwl = 1; n       for (int i = 0; i < l; ++i) pwl *= pr; n       n /= pwl; n       c /= pwl; n       int ans1 = square_root_prime_modulo(c, n, pr, k - l); n       if (ans1 == -1) return -1; n       for (int i = 0; i < (l >> 1); ++i) ans1 *= pr; n       return ans1; n     } else { n       int primitive; n       if (n & 1) n         primitive = primitive_root(n); n       else n         primitive = 5; n       int u = ((int)discr_log(primitive, c, n)); n       if (u == -1) return -1; n       if (u & 1) return -1; n       return pow_mod(primitive, u >> 1, n); n     } n   } else n     return 0; n } n int square_root_modulo(const int& c, const int& n) { n   vector<pair<unsigned long long, int>> f = fac.factor(n); n   int a = 0, p = 1; n   for (int i = 0; i < ((int)(f.size())); ++i) { n     int q = 1; n     for (int j = 0; j < f[i].second; ++j) q *= f[i].first; n     int b = square_root_prime_modulo(c, q, f[i].first, f[i].second); n     if (b == -1) return -1; n     a = unite_mod(a, p, b, q); n     p *= q; n   } n   return a; n } n namespace BGF { n inline int cntd(unsigned x) { n   int ans = 0; n   while (x) { n     if (x & 1) ++ans; n     x >>= 1; n   } n   return ans; n } n inline long long cntdll(unsigned long long x) { n   long long ans = 0; n   while (x) { n     if (x & 1) ++ans; n     x >>= 1; n   } n   return ans; n } n inline int clzd(unsigned x) { n   int ans = 32; n   while (x) { n     --ans; n     x >>= 1; n   } n   return ans; n } n inline long long clzdll(unsigned long long x) { n   long long ans = 64; n   while (x) { n     --ans; n     x >>= 1; n   } n   return ans; n } n inline int ctzd(unsigned x) { n   if (x == 0) return 32; n   int ans = 0; n   while ((x & 1) == 0) { n     ++ans; n     x >>= 1; n   } n   return ans; n } n inline long long ctzdll(unsigned long long x) { n   if (x == 0) return 64; n   long long ans = 0; n   while ((x & 1) == 0) { n     ++ans; n     x >>= 1; n   } n   return ans; n } n }  // namespace BGF n namespace BGF { n const unsigned BHC = 65535u; n const unsigned long long BHCL = 65535ull; n const unsigned BHN = 65536u; n const unsigned BHL = 16u; n const unsigned LH = 4294901760u; n const unsigned RH = 65535u; n const unsigned long long BHLL1 = 16ull; n const unsigned long long BHLL2 = 32ull; n const unsigned long long BHLL3 = 48ull; n const unsigned long long LHL = 18446744069414584320ull; n const unsigned long long RHL = 4294967295ull; n int CNT[BHN]; n int CLZ[BHN]; n int CTZ[BHN]; n inline void initialize() { n   CNT[0] = 0; n   CLZ[0] = BHL + BHL; n   CTZ[0] = BHL; n   for (unsigned i = 1; i < BHN; ++i) { n     CNT[i] = cntd(i); n     CLZ[i] = clzd(i); n     CTZ[i] = ctzd(i); n   } n } n inline int cntc(const unsigned& x) { n   return CNT[x & BHC] + CNT[(x >> BHL) & BHC]; n } n inline long long cntcll(const unsigned long long& x) { n   return CNT[x & BHCL] + CNT[(x >> BHLL1) & BHCL] + CNT[(x >> BHLL2) & BHCL] + n          CNT[(x >> BHLL3) & BHCL]; n } n inline int clzc(const unsigned& x) { n   if (x & LH) n     return CLZ[x >> BHL] - BHL; n   else n     return CLZ[x]; n } n inline long long clzcll(const unsigned long long& x) { n   if (x & LHL) n     return clzc(x >> BHLL2); n   else n     return clzc(x) + BHLL2; n } n inline int ctzc(const unsigned& x) { n   if (x & RH) n     return CTZ[x & RH]; n   else n     return CTZ[x >> BHL] + BHL; n } n inline long long ctzcll(const unsigned long long& x) { n   if (x & RHL) n     return ctzc(x); n   else n     return ctzc(x >> BHLL2) + BHLL2; n } n }  // namespace BGF n const string INPUT_FILE =  input.txt ; n const string OUTPUT_FILE =  output.txt ; n void rel(long long& a, const long long& b) { n   if (a > b) a = b; n } n int another_main() { n   const int INF = 1e9 + 1; n   const long long SINF = 1e12 + 2; n   const long long SSINF = 1e18 + 2; n   const int MINF = -INF; n   int t; n   cin >> t; n   for (int I = 0; I < t; ++I) { n     int n, m; n     cin >> n >> m; n     vector<pair<int, int>> u; n     for (int i = 0; i < n; ++i) { n       int x; n       cin >> x; n       u.emplace_back(x, -MINF); n     } n     for (int i = 0; i < m; ++i) { n       pair<int, int> p; n       cin >> p; n       u.push_back(pair<int, int>(p.first, -p.second)); n     } n     sort(u.begin(), u.end()); n     vector<pair<int, int>> w; n     for (pair<int, int> p : u) { n       p.second = -p.second; n       while (((int)(w.size())) && n              (w.back().second >= p.first && w.back().second >= p.second)) n         w.pop_back(); n       w.push_back(p); n     } n     int q = 0; n     int mxp = MINF; n     vector<long long> L, R; n     vector<int> into(1); n     vector<pair<int, int>> z; n     for (pair<int, int> p : w) n       if (p.second == MINF) { n         mxp = max(mxp, p.first); n         ++into[q]; n       } else { n         z.push_back(p); n         if (mxp != MINF) n           L.push_back(p.first - mxp); n         else n           L.push_back(SINF); n         ++q; n         into.emplace_back(); n       } n     R.resize(q); n     int mnp = INF; n     for (int i = ((int)(w.size())) - 1; i >= 0; --i) { n       pair<int, int> p = w[i]; n       if (p.second == MINF) n         mnp = min(mnp, p.first); n       else { n         --q; n         if (mnp != INF) n           R[q] = mnp - p.second; n         else n           R[q] = SINF; n       } n     } n     q = ((int)(L.size())); n     vector<vector<long long>> dp(4, vector<long long>(q + 1, SSINF)); n     for (int j = 0; j < 4; ++j) dp[j][0] = 0; n     for (int i = 0; i < q; ++i) { n       if (into[i] == 0) { n         if (i) { n           rel(dp[0][i + 1], n               ((long long)z[i].first - z[i - 1].first) + dp[0][i]); n           rel(dp[1][i + 1], n               2ll * ((long long)z[i].first - z[i - 1].first) + dp[1][i]); n           rel(dp[2][i + 1], dp[2][i]); n           rel(dp[3][i + 1], dp[3][i]); n         } n       } n       rel(dp[2][i + 1], 2 * R[i] + dp[0][i]); n       rel(dp[2][i + 1], 2 * R[i] + dp[1][i]); n       rel(dp[3][i + 1], R[i] + dp[0][i]); n       rel(dp[3][i + 1], R[i] + dp[1][i]); n       if (into[i]) { n         rel(dp[1][i + 1], 2 * L[i] + dp[0][i]); n         rel(dp[1][i + 1], 2 * L[i] + dp[1][i]); n         rel(dp[0][i + 1], L[i] + dp[0][i]); n         rel(dp[0][i + 1], L[i] + dp[1][i]); n         rel(dp[2][i + 1], 2 * R[i] + dp[0][i]); n         rel(dp[2][i + 1], 2 * R[i] + dp[1][i]); n         rel(dp[2][i + 1], 2 * R[i] + dp[2][i]); n         rel(dp[2][i + 1], 2 * R[i] + dp[3][i]); n         rel(dp[3][i + 1], R[i] + dp[0][i]); n         rel(dp[3][i + 1], R[i] + dp[1][i]); n         rel(dp[3][i + 1], R[i] + dp[2][i]); n         rel(dp[3][i + 1], R[i] + dp[3][i]); n         if (into[i] == 1) { n           rel(dp[0][i + 1], L[i] + dp[2][i]); n           rel(dp[1][i + 1], 2 * L[i] + dp[3][i]); n         } else { n           rel(dp[0][i + 1], L[i] + dp[3][i]); n         } n       } n       i = i; n     } n     long long ans = SSINF; n     for (int i = 0; i < 4; ++i) ans = min(ans, dp[i][q]); n     cout << ans <<   n ; n   } n   return 0; n } n int main() { n   BGF::initialize(); n   int AMR = another_main(); n   return AMR; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, Magic, md, pd; n int a[99], b[99], cnt[99], row[99], col[99]; n int t[99][99]; n void giveans() { n   printf( %d n , Magic); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) printf( %d%c , t[i][j],    n [j + 1 == n]); n   exit(0); n } n void dfs2(int, int); n void dfs(int x, int y) { n   if (x == n) { n     if (row[n - 1] == Magic && md == Magic && pd == Magic) giveans(); n   } else if (x == 2 && y == 0) n     dfs(x, y + 1); n   else if (x == 2 && y == 3) n     dfs(x + 1, 0); n   else if (x == n - 1) { n     int tmp, tmp1, tmp2; n     if (y == 0) n       tmp = lower_bound(b, b + m, Magic - pd) - b; n     else n       tmp = lower_bound(b, b + m, Magic - col[y]) - b; n     if ((y == 0 && b[tmp] != Magic - pd) || n         (y != 0 && b[tmp] != Magic - col[y])) n       return; n     if (!cnt[tmp]) return; n     cnt[tmp]--; n     t[x][y] = b[tmp]; n     if (y == n - 1) md += b[tmp]; n     if (!y) pd += b[tmp]; n     row[x] += b[tmp]; n     col[y] += b[tmp]; n     bool ok = 1, need = 0; n     if (y == 0 && ok) { n       tmp1 = lower_bound(b, b + m, Magic - col[0]) - b; n       if (b[tmp1] != Magic - col[0] || !cnt[tmp1]) n         ok = 0; n       else { n         cnt[tmp1]--; n         col[0] += b[tmp1]; n         row[2] += b[tmp1]; n         t[2][0] = b[tmp1]; n         tmp2 = lower_bound(b, b + m, Magic - row[2]) - b; n         if (b[tmp2] != Magic - row[2] || !cnt[tmp2]) { n           ok = 0; n           col[0] -= b[tmp1]; n           row[2] -= b[tmp1]; n           cnt[tmp1]++; n         } else { n           cnt[tmp2]--; n           t[2][3] = b[tmp2]; n           col[3] += b[tmp2]; n           row[2] += b[tmp2]; n           need = 1; n         } n       } n     } n     if (ok) { n       if (y == n - 1) n         dfs(x + 1, 0); n       else n         dfs(x, y + 1); n     } n     if (need) { n       cnt[tmp1]++; n       col[0] -= b[tmp1]; n       row[2] -= b[tmp1]; n       cnt[tmp2]++; n       col[3] -= b[tmp2]; n       row[2] -= b[tmp2]; n     } n     if (!y) pd -= b[tmp]; n     if (y == n - 1) md -= b[tmp]; n     col[y] -= b[tmp]; n     row[x] -= b[tmp]; n     cnt[tmp]++; n   } else if (y == n - 1) { n     int tmp = lower_bound(b, b + m, Magic - row[x]) - b; n     if (b[tmp] != Magic - row[x]) return; n     if (!cnt[tmp]) return; n     cnt[tmp]--; n     t[x][y] = b[tmp]; n     if (!x) pd += b[tmp]; n     col[y] += b[tmp]; n     row[x] += b[tmp]; n     dfs(x + 1, 0); n     col[y] -= b[tmp]; n     row[x] -= b[tmp]; n     if (!x) pd -= b[tmp]; n     cnt[tmp]++; n   } else { n     for (int i = 0; i < m; i++) n       if (cnt[i]) { n         cnt[i]--; n         t[x][y] = b[i]; n         row[x] += b[i]; n         col[y] += b[i]; n         if (x == y) md += b[i]; n         if (x + y == n - 1) pd += b[i]; n         dfs(x, y + 1); n         if (x + y == n - 1) pd -= b[i]; n         if (x == y) md -= b[i]; n         col[y] -= b[i]; n         row[x] -= b[i]; n         cnt[i]++; n       } n   } n } n int main() { n   srand(13451); n   scanf( %d , &n); n   srand(rand() * 10 + rand() + n); n   for (int i = 0; i < n * n; i++) n     scanf( %d , a + i), Magic += a[i], b[i] = a[i]; n   if (n == 1) { n     printf( %d n%d n , a[0], a[0]); n     return 0; n   } n   sort(b, b + n * n); n   m = unique(b, b + n * n) - b; n   b[m] = 1000000000; n   for (int i = 0; i < n * n; i++) cnt[lower_bound(b, b + m, a[i]) - b]++; n   Magic /= n; n   if (n < 4) dfs2(0, 0); n   int p[200]; n   for (int i = 0; i < m; i++) p[i] = i; n   random_shuffle(p, p + m); n   for (int i = 0; i < m; i++) { n     cnt[i]--; n     t[0][0] = b[i]; n     row[0] += b[i]; n     col[0] += b[i]; n     md += b[i]; n     dfs(0, 1); n     row[0] -= b[i]; n     col[0] -= b[i]; n     md -= b[i]; n     cnt[i]++; n   } n   return 0; n } n void dfs2(int x, int y) { n   if (x == n) { n     if (row[n - 1] == Magic && md == Magic && pd == Magic) giveans(); n   } else if (x == n - 1) { n     int tmp = lower_bound(b, b + m, Magic - col[y]) - b; n     if (b[tmp] != Magic - col[y]) return; n     if (!cnt[tmp]) return; n     cnt[tmp]--; n     t[x][y] = b[tmp]; n     if (y == n - 1) md += b[tmp]; n     if (!y) pd += b[tmp]; n     row[x] += b[tmp]; n     if (pd == Magic) { n       if (y == n - 1) n         dfs2(x + 1, 0); n       else n         dfs2(x, y + 1); n     } n     if (!y) pd -= b[tmp]; n     if (y == n - 1) md -= b[tmp]; n     row[x] -= b[tmp]; n     cnt[tmp]++; n   } else if (y == n - 1) { n     int tmp = lower_bound(b, b + m, Magic - row[x]) - b; n     if (b[tmp] != Magic - row[x]) return; n     if (!cnt[tmp]) return; n     cnt[tmp]--; n     t[x][y] = b[tmp]; n     if (!x) pd += b[tmp]; n     col[y] += b[tmp]; n     dfs2(x + 1, 0); n     col[y] -= b[tmp]; n     if (!x) pd -= b[tmp]; n     cnt[tmp]++; n   } else { n     for (int i = 0; i < m; i++) n       if (cnt[i]) { n         cnt[i]--; n         t[x][y] = b[i]; n         row[x] += b[i]; n         col[y] += b[i]; n         if (x == y) md += b[i]; n         if (x + y == n - 1) pd += b[i]; n         dfs2(x, y + 1); n         if (x + y == n - 1) pd -= b[i]; n         if (x == y) md -= b[i]; n         col[y] -= b[i]; n         row[x] -= b[i]; n         cnt[i]++; n       } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x0FFFFFFF; n const long long INFLL = 0x0FFFFFFFFFFFFFFFll; n const int MAXN = 300005; n int n, m, x, y; n vector<int> g[MAXN]; n int ans[MAXN]; n int getenemies(int i) { n   int c = 0; n   for (size_t j = 0; j < g[i].size(); j++) n     if (ans[g[i][j]] == ans[i]) c++; n   return c; n } n void solve(int p) { n   if (getenemies(p) > 1) { n     ans[p] = 1 - ans[p]; n     for (size_t j = 0; j < g[p].size(); j++) solve(g[p][j]); n   } n } n int main() { n   cin >> n >> m; n   while (m--) { n     cin >> x >> y; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   for (int i = 1; i <= n; i++) solve(i); n   for (int i = 1; i <= n; i++) cout << ans[i]; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300010; n map<pair<int, int>, int> mp; n struct point { n   int first, second; n   int l, r; n   point() {} n   point(int first, int second, int l, int r) n       : first(first), second(second), l(l), r(r) {} n }; n long long ans[N << 1]; n long long sum; n int cnt; n int cx[N << 1], cy[N << 1]; n int pre[N << 1]; n int sz[N << 1]; n int find(int first) { n   if (!pre[first]) return first; n   return find(pre[first]); n } n struct store { n   int first; n   int cx, cy; n   int sz; n   int pre; n   store() {} n   store(int first, int cx, int cy, int sz, int pre) n       : first(first), cx(cx), cy(cy), sz(sz), pre(pre) {} n }; n stack<store> stk; n void recovery(store a) { n   sz[a.first] = a.sz; n   cx[a.first] = a.cx; n   cy[a.first] = a.cy; n   pre[a.first] = a.pre; n   sum += 1LL * a.cx * a.cy; n } n void unions(int first, int second) { n   first = find(first); n   second = find(second); n   if (first == second) return; n   if (sz[first] > sz[second]) swap(first, second); n   cnt += 2; n   stk.push(store(first, cx[first], cy[first], sz[first], pre[first])); n   stk.push(store(second, cx[second], cy[second], sz[second], pre[second])); n   sum -= 1LL * cx[first] * cy[first]; n   sum -= 1LL * cx[second] * cy[second]; n   sz[second] += sz[first]; n   cx[second] += cx[first]; n   cy[second] += cy[first]; n   sz[second] += sz[first]; n   pre[first] = second; n   sum += 1LL * cx[second] * cy[second]; n } n void rollback() { n   cnt -= 2; n   store second = stk.top(); n   stk.pop(); n   store first = stk.top(); n   stk.pop(); n   sum -= 1LL * cx[second.first] * cy[second.first]; n   recovery(second); n   recovery(first); n } n void solve(int l, int r, vector<point> &v) { n   if (l == r) { n     int tmp = cnt; n     for (int i = 0; i < v.size(); i++) { n       int first = v[i].first, second = v[i].second; n       unions(first, second); n     } n     ans[l] = sum; n     while (cnt > tmp) rollback(); n     return; n   } n   int tmp = cnt; n   vector<point> vl, vr; n   int mid = (l + r) >> 1; n   for (int i = 0; i < v.size(); i++) { n     if (v[i].l == l && v[i].r == r) { n       int first = v[i].first, second = v[i].second; n       unions(first, second); n     } else if (v[i].r <= mid) n       vl.push_back(v[i]); n     else if (v[i].l > mid) n       vr.push_back(v[i]); n     else { n       vl.push_back(point(v[i].first, v[i].second, v[i].l, mid)); n       vr.push_back(point(v[i].first, v[i].second, mid + 1, v[i].r)); n     } n   } n   solve(l, mid, vl); n   solve(mid + 1, r, vr); n   while (cnt > tmp) { n     rollback(); n   } n } n int main() { n   ios::sync_with_stdio(false); n   vector<point> v; n   int n; n   cin >> n; n   for (int i = 1; i <= 300000; i++) { n     cx[i] = 1; n     cy[i + 300000] = 1; n   } n   for (int i = 1; i <= 600000; i++) sz[i] = 1; n   for (int i = 1; i <= n; i++) { n     int first, second; n     cin >> first >> second; n     second += 300000; n     if (mp.count(make_pair(first, second))) { n       int st = mp[make_pair(first, second)]; n       int ed = i - 1; n       v.push_back(point(first, second, st, ed)); n       mp.erase(make_pair(first, second)); n     } else { n       mp[make_pair(first, second)] = i; n     } n   } n   for (map<pair<int, int>, int>::iterator it = mp.begin(); it != mp.end(); n        it++) { n     int first = it->first.first; n     int second = it->first.second; n     int st = it->second; n     int ed = n; n     v.push_back(point(first, second, st, ed)); n   } n   solve(1, n, v); n   for (int i = 1; i <= n; i++) cout << ans[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int maxn = 10105; n int n, b, q, st, ed; n int First[maxn], cnt, dis[maxn]; n struct edge { n   int to, Next, flow, c; n } Tree[maxn * 100]; n void init() { n   cnt = 0; n   memset(First, -1, sizeof(First)); n } n void addedge(int from, int to, int flow) { n   Tree[cnt].to = to; n   Tree[cnt].Next = First[from]; n   Tree[cnt].flow = flow; n   First[from] = cnt++; n   Tree[cnt].to = from; n   Tree[cnt].Next = First[to]; n   Tree[cnt].flow = 0; n   First[to] = cnt++; n } n bool bfs(int st, int ed) { n   memset(dis, 0, sizeof(dis)); n   queue<int> Q; n   dis[st] = 1; n   Q.push(st); n   while (!Q.empty()) { n     int u = Q.front(); n     Q.pop(); n     for (int i = First[u]; i != -1; i = Tree[i].Next) { n       int v = Tree[i].to; n       int flow = Tree[i].flow; n       if (flow && !dis[v]) { n         dis[v] = dis[u] + 1; n         if (v == ed) return true; n         Q.push(v); n       } n     } n   } n   return false; n } n int dfs(int u, int minf) { n   if (u == ed) { n     return minf; n   } n   if (minf == 0) return minf; n   int f = 0; n   for (int i = First[u]; i != -1; i = Tree[i].Next) { n     int v = Tree[i].to; n     if (Tree[i].flow && dis[v] == dis[u] + 1) { n       int tmp = dfs(v, min(Tree[i].flow, minf)); n       Tree[i].flow -= tmp; n       Tree[i ^ 1].flow += tmp; n       f += tmp; n       minf -= tmp; n       if (minf == 0) return f; n     } n   } n   return f; n } n int dinic(int st, int ed) { n   int res = 0; n   while (bfs(st, ed)) res += dfs(st, INF); n   return res; n } n struct node { n   int up, num; n   bool operator<(const node& A) const { n     if (A.up != up) return A.up > up; n     return A.num > num; n   } n } ax[10005]; n int sum[10]; n int main() { n   init(); n   scanf( %d%d%d , &n, &b, &q); n   st = 0, ed = q + 5 + 10; n   for (int i = 1; i <= q; i++) scanf( %d%d , &ax[i].up, &ax[i].num); n   sort(ax + 1, ax + 1 + q); n   ax[q + 1].up = b, ax[q + 1].num = n; n   for (int i = 1; i <= q + 1; i++) { n     int nowv = ax[i].up - ax[i - 1].up; n     int nowb = ax[i].num - ax[i - 1].num; n     if (ax[i].num > n || nowb > nowv || nowb < 0) { n       puts( unfair ); n       return 0; n     } n     for (int j = ax[i - 1].up + 1; j <= ax[i].up; j++) { n       sum[j % 5]++; n     } n     for (int j = 0; j < 5; j++) addedge(i, q + j + 2, sum[j]); n     addedge(st, i, nowb); n     memset(sum, 0, sizeof(sum)); n   } n   for (int i = 1; i <= 5; i++) addedge(q + i + 1, ed, n / 5); n   int max1 = dinic(st, ed); n   if (max1 == n) n     puts( fair ); n   else n     puts( unfair ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long arr[10000001]; n int main() { n   long long d = 1; n   arr[0] = 0; n   for (int i = 1; i < 10000001; i++) { n     arr[i] = arr[i - 1] + d; n     d += 2; n   } n   int t; n   cin >> t; n   for (int i = 0; i < t; i++) { n     int n, k; n     cin >> n >> k; n     if (n % 2 == 0 && k % 2 != 0) { n       cout <<  NO  << endl; n       continue; n     } n     if (n % 2 != 0 && k % 2 == 0) { n       cout <<  NO  << endl; n       continue; n     } n     long long x = arr[k]; n     if (n >= x) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   long long i; n   long long n; n   cin >> n; n   cin >> s; n   long long arr[27]; n   for (i = 0; i < 26; i++) { n     arr[i] = 0; n   } n   for (i = 0; i < s.length(); i++) { n     arr[s[i] -  a ]++; n   } n   long long cnt = 0; n   if (n == 1) { n     cout <<  YES ; n     return 0; n   } n   for (i = 0; i < 26; i++) { n     if (arr[i] > 1) { n       cout <<  YES ; n       return 0; n     } n   } n   cout <<  NO ; n   return 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n long long dp[511][511]; n long long tol[511]; n int main() { n   int n, k, i, j, p; n   scanf( %d%d , &n, &k); n   for (i = 1; i <= n; ++i) dp[0][i] = 1; n   for (i = 1; i <= n; ++i) { n     for (j = 1; j <= n; ++j) { n       for (p = max(i - j, 0); p <= i - 1; ++p) { n         dp[i][j] = dp[i][j] + dp[p][j]; n         dp[i][j] %= mod; n       } n     } n   } n   for (i = 1; i <= n; ++i) { n     tol[i] = dp[n][i] - dp[n][i - 1]; n   } n   long long ans = 0; n   for (i = 1; i <= n; ++i) { n     for (j = 1; j <= n; ++j) { n       if (i * j < k) { n         ans += tol[i] * tol[j]; n         ans %= mod; n       } n     } n   } n   ans *= 2; n   cout << (ans % mod + mod) % mod << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main(int argc, const char* argv[]) { n   int n; n   std::cin >> n; n   int step = 0; n   int prev = 0; n   int first = 0, second = 0; n   bool prog = true; n   int x = 0; n   for (int i = 0; i < n; i++) { n     std::cin >> x; n     if (i == 0) n       first = x; n     else if (i == 1) { n       second = x; n       step = second - first; n       prev = x; n     } else { n       if ((x - prev) != step) prog = false; n       prev = x; n     } n   } n   if (prog) n     std::cout << x + step; n   else n     std::cout << x; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long N, W; n   cin >> N >> W; n   long long lowest = 0, highest = W; n   long long current = 0, sum = 0; n   while (N--) { n     cin >> current; n     sum += current; n     lowest = max(lowest, -sum); n     highest = min(highest, W - sum); n   } n   cout << max(0ll, highest - lowest + 1) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class team { n  public: n   int x, y, z; n }; n int search(team *, int, int); n int main() { n   int m, n, t; n   cin >> n >> m; n   int *arr1, *arr2; n   arr1 = new int[m]; n   arr2 = new int[m]; n   for (int i = 0; i < m; i++) cin >> arr1[i] >> arr2[i]; n   team *res = new team[n / 3]; n   for (int i = 0; i < n / 3; i++) { n     res[i].x = 0; n     res[i].y = 0; n     res[i].z = 0; n   } n   bool *tak = new bool[n + 1]; n   bool resf = false; n   for (int i = 0; i < n + 1; i++) tak[i] = false; n   tak[0] = true; n   for (int i = 0; i < m; i++) { n     if ((!tak[arr1[i]]) && (!tak[arr2[i]])) { n       bool f = true; n       for (int j = 0; j < n / 3; j++) { n         if (res[j].x == 0) { n           res[j].x = arr1[i]; n           res[j].y = arr2[i]; n           tak[arr1[i]] = true; n           tak[arr2[i]] = true; n           f = false; n           break; n         } n       } n       if (f) { n         resf = true; n         goto End; n       } n     } else if (tak[arr1[i]] && tak[arr2[i]]) { n       if (search(res, arr1[i], n / 3) == search(res, arr2[i], n / 3)) n         continue; n       else { n         resf = true; n         goto End; n       } n     } else if (tak[arr1[i]]) { n       t = search(res, arr1[i], n / 3); n       if (res[t].z == 0) { n         res[t].z = arr2[i]; n         tak[arr2[i]] = true; n       } else { n         resf = true; n         goto End; n       } n     } else if (tak[arr2[i]]) { n       t = search(res, arr2[i], n / 3); n       if (res[t].z == 0) { n         res[t].z = arr1[i]; n         tak[arr1[i]] = true; n       } else { n         resf = true; n         goto End; n       } n     } n   } n   for (int i = 0; i < n / 3; i++) { n     if (res[i].x == 0) { n       for (int j = 1; j < n + 1; j++) n         if (!tak[j]) { n           res[i].x = j; n           tak[j] = true; n           break; n         } n     } n     if (res[i].y == 0) { n       for (int j = 1; j < n + 1; j++) n         if (!tak[j]) { n           res[i].y = j; n           tak[j] = true; n           break; n         } n     } n     if (res[i].z == 0) { n       for (int j = 1; j < n + 1; j++) n         if (!tak[j]) { n           res[i].z = j; n           tak[j] = true; n           break; n         } n     } n   } n End: n   if (resf) n     cout << -1; n   else { n     for (int i = 0; i < n / 3; i++) n       cout << res[i].x <<     << res[i].y <<     << res[i].z << endl; n   } n   return 0; n } n int search(team *input, int val, int size) { n   for (int i = 0; i < size; i++) { n     if (input[i].x == val) return i; n     if (input[i].y == val) return i; n     if (input[i].z == val) return i; n   } n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   long long n, k; n   cin >> n >> k; n   vector<long long> a(n); n   vector<long long> b(n); n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     a[i]--; n   } n   for (long long i = 0; i < n; i++) { n     cin >> b[i]; n   } n   long long cntZero = 0; n   vector<long long> cnt(k); n   for (long long i = 0; i < n; i++) { n     cnt[a[i]]++; n   } n   for (long long i = 0; i < k; i++) { n     if (cnt[i] == 0) cntZero++; n   } n   set<pair<long long, long long>> s; n   for (long long i = 0; i < n; i++) { n     s.insert({b[i], i}); n   } n   long long ans = 0; n   while (cntZero) { n     auto pp = *s.begin(); n     s.erase(s.begin()); n     if (cnt[a[pp.second]] > 1) { n       cntZero--; n       cnt[a[pp.second]]--; n       ans += pp.first; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long l1, r1, l2, r2, k, ans; n int main() { n   cin >> l1 >> r1 >> l2 >> r2 >> k; n   if (r1 < l2 || r2 < l1) { n     ans = 0; n   } else { n     long long r = min(r1, r2), l = max(l1, l2); n     ans = r - l + 1; n     if (k >= l && k <= r) ans--; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, sg, m, k; n   cin >> n >> sg >> m >> k; n   vector<int> a(n), starts(n, n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < sg; i++) { n     int l, r; n     cin >> l >> r; n     l--; n     r--; n     starts[r] = min(starts[r], l); n   } n   auto bldPrefCnt = [&](int x) { n     vector<int> res(n); n     int cur = 0; n     for (int i = 0; i < n; i++) { n       if (a[i] <= x) { n         cur++; n       } n       res[i] = cur; n     } n     return res; n   }; n   auto check = [&](int x) { n     auto prefs = bldPrefCnt(x); n     vector<int> sufMem(n, -1); n     map<int, int> sufL; n     for (int i = 0; i < n; i++) { n       int l = starts[i]; n       if (l == n) { n         continue; n       } n       auto itL = sufL.lower_bound(l); n       if (itL != sufL.end()) { n         sufMem[i] = itL->first; n       } n       while (sufL.size() > 0 && l <= sufL.rbegin()->second) { n         sufL.erase(prev(sufL.end())); n       } n       sufL[i] = l; n     } n     vector<vector<int>> dp(n, vector<int>(m + 1, 0)); n     int best = 0; n     vector<int> prefMx(n); n     for (int j = 1; j <= m; j++) { n       vector<int> nxtPrefMx(n); n       for (int i = 0; i < n; i++) { n         auto &cur = dp[i][j]; n         if (starts[i] < n) { n           int l = starts[i]; n           if (l > 0) { n             cur = (prefs[i] - prefs[l - 1]) + prefMx[l - 1]; n           } else { n             cur = prefs[i]; n           } n           int u = sufMem[i]; n           if (u != -1) { n             int take = prefs[i] - prefs[u]; n             cur = max(cur, take + dp[u][j - 1]); n           } n         } n         nxtPrefMx[i] = n             (i > 0 && nxtPrefMx[i - 1] > cur) ? nxtPrefMx[i - 1] : cur; n         best = max(best, cur); n       } n       prefMx = nxtPrefMx; n     } n     return best >= k; n   }; n   int l = 0, r = 1e9 + 10; n   int ans = -1; n   if (check(r)) { n     while (l < r) { n       int mid = (l + r) / 2; n       if (check(mid)) { n         r = mid; n       } else { n         l = mid + 1; n       } n     } n     ans = r; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int w[40]; n int main() { n   int m, n; n   cin >> m >> n; n   for (int i = 1; i <= n; i++) { n     cout << 1 << endl; n     cin >> w[i]; n     if (w[i] == 0) { n       return 0; n     } n   } n   int l = 1, r = m; n   int index = 1; n   while (l <= r) { n     int mid = (l + r) / 2; n     cout << mid << endl; n     int q; n     cin >> q; n     if (q == 0) return 0; n     if (q == w[(index - 1) % n + 1]) { n       l = mid + 1; n     } else { n       r = mid; n     } n     index++; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, int> mp; n const int INF = 0x3F3F3F3F; n const int MOD = 1000003; n const int dx[] = {-1, 0, 1, 0}; n const int dy[] = {0, 1, 0, -1}; n vector<vector<char> > mat; n int n, m; n bool isvalid(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; } n int main() { n   cin >> n >> m; n   mat.resize(n); n   for (int i = 0; i < n; i++) { n     mat[i].resize(m); n     for (int j = 0; j < m; j++) { n       cin >> mat[i][j]; n     } n   } n   bool flag = 0; n   int num = 0; n   for (int i = 0; i < n; i++) { n     if (flag) break; n     bool l[2] = {0}; n     for (int j = 0; j < m; j++) { n       if (mat[i][j] ==  . ) continue; n       if (mat[i][j] ==  1  || mat[i][j] ==  2 ) { n         l[j & 1] = 1; n       } else { n         l[(j + 1) & 1] = 1; n       } n     } n     if (l[0] && l[1]) flag = 1; n     if (!l[0] && !l[1]) num++; n   } n   for (int j = 0; j < m; j++) { n     if (flag) break; n     bool u[2] = {0}; n     for (int i = 0; i < n; i++) { n       if (mat[i][j] ==  . ) continue; n       if (mat[i][j] ==  1  || mat[i][j] ==  4 ) { n         u[i & 1] = 1; n       } else { n         u[(i + 1) & 1] = 1; n       } n     } n     if (u[0] && u[1]) flag = 1; n     if (!u[0] && !u[1]) num++; n   } n   if (flag) { n     cout << 0 << endl; n     return 0; n   } n   int ans = 1; n   for (int i = 0; i < num; i++) { n     ans = (ans * 2) % MOD; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, i, cnt = 0; n   cin >> n; n   long long ar[n]; n   for (i = 0; i < n; i++) cin >> ar[i]; n   for (i = 0; i < n; i++) { n     if (ar[i] % 2 != 0) cnt++; n   } n   cout << min(cnt, n - cnt); n } n


#include <bits/stdc++.h> n using ll = long long; n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   vector<int> arr(n); n   for (int i = 0; i < n; i++) cin >> arr[i]; n   sort(arr.begin(), arr.end()); n   vector<int> fst; n   for (int i = 0; i < (n / 2); i++) fst.push_back(arr[i]); n   vector<int> snd; n   for (int i = (n / 2); i < n; i++) snd.push_back(arr[i]); n   int hidden = 0; n   int j = 0; n   for (int i = 0; i < fst.size(); i++) { n     while (j < snd.size() && snd[j] < 2 * fst[i]) j++; n     if (j == snd.size()) break; n     hidden++; n     j++; n   } n   cout << n - hidden <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> nxt[1 << 13], G[15]; n vector<pair<int, int> > lca[13]; n bool a[14][14], ok[14][1 << 13]; n long long int dp[14][1 << 13][2]; n int n; n long long int solve(int root, int mask, int check) { n   if (check == 0) { n     int i; n     for (i = 0; i < lca[root].size(); i++) { n       int x = lca[root][i].first, y = lca[root][i].second; n       if ((mask & (1 << x)) == 0 && x != root) return 0; n       if ((mask & (1 << y)) == 0 && y != root) return 0; n     } n   } n   long long int &ret = dp[root][mask][check]; n   if (ret != -1) return ret; n   if (mask == 0) return ret = 1; n   ret = 0; n   int i, j, k; n   for (i = 0; i < nxt[mask].size(); i++) { n     int newmask = nxt[mask][i]; n     if (!ok[root][newmask]) continue; n     int msb = -1, ch = -1; n     for (k = 0; k < n; k++) { n       if (mask & (1 << k)) msb = k; n       if ((newmask & (1 << k)) && a[root][k]) { n         if (ch == -1) n           ch = k; n         else n           ch = -2; n       } n     } n     if (ch == -2 || (newmask & (1 << msb)) == 0) continue; n     if (ch == -1) { n       for (k = 0; k < n; k++) { n         if (newmask & (1 << k)) n           ret += n               solve(k, newmask ^ (1 << k), 0) * solve(root, mask ^ newmask, 1); n       } n     } else n       ret += solve(ch, newmask ^ (1 << ch), 0) * solve(root, mask ^ newmask, 1); n   } n   return ret; n } n int main() { n   int i, j, m, k, k1, q; n   scanf( %d%d , &n, &m); n   ; n   scanf( %d , &q); n   ; n   while (m--) { n     int x, y; n     scanf( %d%d , &x, &y); n     ; n     x--; n     y--; n     G[x].push_back(y); n     G[y].push_back(x); n     a[x][y] = 1; n     a[y][x] = 1; n   } n   while (q--) { n     int x, y, z; n     scanf( %d%d , &x, &y); n     ; n     scanf( %d , &z); n     ; n     x--; n     y--; n     z--; n     lca[z].push_back(make_pair(x, y)); n   } n   for (i = 0; i < 1 << n; i++) n     for (j = 0; j < i + 1; j++) n       if ((i & j) == j) nxt[i].push_back(j); n   for (i = 0; i < n; i++) { n     for (j = 0; j < 1 << n; j++) { n       bool f = 1; n       if (j & (1 << i)) continue; n       for (k = 0; k < n; k++) { n         if ((j & (1 << k)) == 0) continue; n         for (k1 = 0; k1 < G[k].size(); k1++) n           if ((j & (1 << G[k][k1])) == 0 && G[k][k1] != i) f = 0; n       } n       for (k = 0; k < lca[i].size(); k++) { n         int x = lca[i][k].first, y = lca[i][k].second; n         if ((j & (1 << x)) && (j & (1 << y))) f = 0; n       } n       ok[i][j] = f; n     } n   } n   memset(dp, -1, sizeof(dp)); n   cout << solve(0, ((1 << (n - 1)) - 1) << 1, 0) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using vi = vector<int>; n using db = double; n const int mxn = 300006; n const ll mod = 1E9L + 7; n int n; n ll a[mxn]; n ll p[mxn]; n int main() { n   cin >> n; n   for (int i = 0; i < n; ++i) scanf( %lld , &a[i]); n   sort(a, a + n); n   p[0] = 1; n   for (int i = 1; i <= n; ++i) p[i] = p[i - 1] * 2 % mod; n   ll ans = 0; n   for (int i = 0; i < n; ++i) { n     ans -= a[i] * p[n - 1 - i]; n     ans += a[i] * p[i]; n     ans %= mod; n   } n   cout << (ans + mod) % mod << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int inf = 0x3f3f3f3f3f3f3f3fll; n const int mx = 15000005; n vector<string> adj[2000005]; n vector<bool> vis; n vector<pair<string, int>> pa; n int ind = 0, mxx = 0; n void dfs(int d, int ti) { n   mxx = max(mxx, d); n   adj[d].push_back(pa[ind].first); n   while (ti--) { n     ind++; n     if (ind < pa.size()) dfs(d + 1, pa[ind].second); n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   string s; n   cin >> s; n   std::istringstream is(s); n   std::string token; n   int now = 1; n   while (std::getline(is, token,  , )) { n     if (now) { n       pa.push_back({token, 0}); n     } else { n       int val = 0; n       for (char c : token) { n         val = val * 10 + (c -  0 ); n       } n       pa.back().second = val; n     } n     now = now ^ 1; n   } n   vis.assign(pa.size() + 1, false); n   while (ind < pa.size()) { n     dfs(0, pa[ind].second); n     ind++; n   } n   int i = 0; n   cout << mxx + 1 << endl; n   while (100) { n     if (adj[i].size() == 0) break; n     for (string u : adj[i]) cout << u <<    ; n     cout << endl; n     i++; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> times; n const int INF = 0x3fffffff; n struct Node { n   int l, r; n   int v; n   Node() { l = -1, r = -1; } n   Node(int _l, int _r) { l = _l, r = _r; } n } node[120000]; n int fa[120000]; n int n, v, ls, rs, root; n int ans; n int Findroot(int t) { n   if (fa[t] != t) return Findroot(fa[t]); n   return t; n } n void dfs(int mi, int ma, int now) { n   if (node[now].v <= mi || node[now].v >= ma) { n     if (times[node[now].v] != -1) { n       ans++; n       times[node[now].v]++; n     } n   } else { n     ans -= times[node[now].v]; n     times[node[now].v] = -1; n   } n   if (node[now].l ^ (~0)) dfs(mi, min(ma, node[now].v), node[now].l); n   if (node[now].r ^ (~0)) dfs(max(mi, node[now].v), ma, node[now].r); n   return; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) fa[i] = i; n   for (int i = 1; i <= n; i++) { n     scanf( %d%d%d , &v, &ls, &rs); n     if (ls ^ (~0)) fa[ls] = i; n     if (rs ^ (~0)) fa[rs] = i; n     node[i].l = ls; n     node[i].r = rs; n     node[i].v = v; n   } n   root = Findroot(1); n   dfs(-INF, INF, root); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 400000 + 10; n const double eps = 1e-9; n const double pi = acos(-1); n int n; n long long k; n struct Vec { n   int x, y; n   double d() { return x * x + y * y; } n } p[N]; n vector<double> v; n int id(double x) { return lower_bound(v.begin(), v.end(), x) - v.begin() + 1; } n int bit[N]; n void add(int x, int k) { n   while (x < N) { n     bit[x] += k; n     x += x & (-x); n   } n } n int sum(int x) { n   int ans = 0; n   while (x > 0) { n     ans += bit[x]; n     x -= x & (-x); n   } n   return ans; n } n long long cal(vector<pair<double, double> > vec) { n   memset(bit, 0, sizeof(bit)); n   sort(vec.begin(), vec.end()); n   v.clear(); n   for (auto p : vec) { n     v.push_back(p.first); n     v.push_back(p.second); n   } n   sort(v.begin(), v.end()); n   v.erase(unique(v.begin(), v.end()), v.end()); n   long long res = 0; n   for (auto p : vec) { n     res += sum(id(p.second)) - sum(id(p.first)); n     add(id(p.second), 1); n   } n   return res; n } n double check(double x) { n   vector<pair<double, double> > vec; n   long long tot = 1LL * n * (n - 1) / 2, cnt = 0; n   for (int i = 1; i <= n; i++) { n     if (p[i].d() > x * x - eps) { n       double dis = sqrt(p[i].d()); n       double theta = acos(x / dis); n       double mid = atan2(p[i].y, p[i].x); n       int pre = vec.size(); n       if (mid < 0) mid += 2 * pi; n       if (mid + theta > 2 * pi) { n         vec.push_back(make_pair(mid + theta - 2 * pi, mid - theta)); n       } else if (mid - theta < 0) { n         vec.push_back(make_pair(mid + theta, mid - theta + 2 * pi)); n       } else { n         vec.push_back(make_pair(mid - theta, mid + theta)); n       } n       int cur = vec.size(); n     } n   } n   tot -= cal(vec); n   return tot >= k; n } n int main() { n   scanf( %d%lld , &n, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &p[i].x, &p[i].y); n   } n   double lef = 0, rig = 2e4; n   for (int i = 1; i <= 100; i++) { n     double mid = (lef + rig) / 2; n     if (check(mid)) n       rig = mid; n     else n       lef = mid; n   } n   printf( %.8f n , rig); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10); n   int m, x, y; n   cin >> m; n   vector<int> M(m); n   for (int i = 0; i < (m); ++i) cin >> M[i]; n   int sum = accumulate(M.begin(), M.end(), 0); n   cerr << sum << endl; n   cin >> x >> y; n   int val = 0; n   for (int i = 0; i < (m); ++i) { n     val += M[i]; n     if (val > y) { n       cout << 0 << endl; n       exit(0); n     } n     if (val >= x && sum - val >= x && sum - val <= y) { n       cout << i + 2 << endl; n       exit(0); n     } n   } n   cout << 0 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[5000005]; n void solve() { n   for (long long i = 1; i <= 5000000; i++) dp[i] = i * (i - 1) / 2; n   for (long long i = 2; i <= 5000000 / 2; i++) { n     for (long long j = 2; j <= i; j++) { n       if (i * j > 5000000) break; n       dp[i * j] = min(dp[i * j], dp[i] + j * (j - 1) / 2 * i); n       dp[i * j] = min(dp[i * j], dp[j] + i * (i - 1) / 2 * j); n     } n   } n   long long t, l, r; n   cin >> t >> l >> r; n   long long tt = 1, ans = 0; n   while (l <= r) { n     dp[l] %= (long long)(1e9 + 7); n     ans = (ans + tt * dp[l]) % (long long)(1e9 + 7); n     tt = (tt * t) % (long long)(1e9 + 7); n     l++; n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct sak { n   int x, y; n   sak(int x1, int y1) { n     x = x1; n     y = y1; n   } n }; n vector<sak> A, B; n vector<int> AA, BB; n int findA(int k) { n   int t1 = -1; n   for (int i = 0; i < A.size(); i++) n     if (A[i].x <= k && AA[i] == 0 && (t1 == -1 || A[t1].y < A[i].y)) { n       t1 = i; n     } n   return t1; n } n int findB(int k) { n   int t1 = -1; n   for (int i = 0; i < B.size(); i++) n     if (B[i].x <= k && BB[i] == 0 && (t1 == -1 || B[t1].y < B[i].y)) { n       t1 = i; n     } n   return t1; n } n int main() { n   int n, k; n   while (scanf( %d %d , &n, &k) == 2) { n     int x, y, z; n     A.clear(); n     B.clear(); n     AA.clear(); n     BB.clear(); n     for (int i = 0; i < n; i++) { n       scanf( %d %d %d , &x, &y, &z); n       if (x == 0) { n         A.push_back(sak(y, z)); n         AA.push_back(0); n       } else { n         B.push_back(sak(y, z)); n         BB.push_back(0); n       } n     } n     int maxi = 0; n     int sak1 = findA(k); n     if (A.size() > 0 && sak1 != -1) { n       int cnt = 1; n       x = 1; n       y = 0; n       int kk = k + A[sak1].y; n       AA[sak1] = 1; n       while (1) { n         sak1 = findB(kk); n         if (sak1 == -1) break; n         cnt++; n         kk += B[sak1].y; n         BB[sak1] = 1; n         y++; n         sak1 = findA(kk); n         if (sak1 == -1) break; n         cnt++; n         kk += A[sak1].y; n         AA[sak1] = 1; n         x++; n       } n       if (cnt > maxi) maxi = cnt; n     } n     for (int i = 0; i < AA.size(); i++) AA[i] = 0; n     for (int i = 0; i < BB.size(); i++) BB[i] = 0; n     sak1 = findB(k); n     if (B.size() > 0 && sak1 != -1) { n       int cnt = 1; n       x = 0; n       y = 1; n       int kk = k + B[sak1].y; n       BB[sak1] = 1; n       while (1) { n         sak1 = findA(kk); n         if (sak1 == -1) break; n         cnt++; n         kk += A[sak1].y; n         AA[sak1] = 1; n         x++; n         sak1 = findB(kk); n         if (sak1 == -1) break; n         cnt++; n         kk += B[sak1].y; n         BB[sak1] = 1; n         y++; n       } n       if (cnt > maxi) maxi = cnt; n     } n     printf( %d n , maxi); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcdExtended(long long a, long long b, long long *x, long long *y) { n   if (a == 0) { n     *x = 0; n     *y = 1; n     return b; n   } n   long long x1, y1; n   long long gcd = gcdExtended(b % a, a, &x1, &y1); n   *x = y1 - (b / a) * x1; n   *y = x1; n   return gcd; n } n int main() { n   long long x, y, a, b, c; n   cin >> a >> b >> c; n   c = -c; n   long long g = gcdExtended(a, b, &x, &y); n   if (c % g == 0) n     cout << c * x / g <<     << c * y / g << endl; n   else n     cout <<  -1  << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:256000000 ) n using namespace std; n inline int readChar(); n template <class T = int> n inline T readInt(); n template <class T> n inline void writeInt(T x, char end = 0); n inline void writeChar(int x); n inline void writeWord(const char *s); n static const int buf_size = 4096; n inline int getChar() { n   static char buf[buf_size]; n   static int len = 0, pos = 0; n   if (pos == len) pos = 0, len = fread(buf, 1, buf_size, stdin); n   if (pos == len) return -1; n   return buf[pos++]; n } n inline int readChar() { n   int c = getChar(); n   while (c <= 32) c = getChar(); n   return c; n } n template <class T> n inline T readInt() { n   int s = 1, c = readChar(); n   T x = 0; n   if (c ==  - ) s = -1, c = getChar(); n   while ( 0  <= c && c <=  9 ) x = x * 10 + c -  0 , c = getChar(); n   return s == 1 ? x : -x; n } n static int write_pos = 0; n static char write_buf[buf_size]; n inline void writeChar(int x) { n   if (write_pos == buf_size) n     fwrite(write_buf, 1, buf_size, stdout), write_pos = 0; n   write_buf[write_pos++] = x; n } n template <class T> n inline void writeInt(T x, char end) { n   if (x < 0) writeChar( - ), x = -x; n   char s[24]; n   int n = 0; n   while (x || !n) s[n++] =  0  + x % 10, x /= 10; n   while (n--) writeChar(s[n]); n   if (end) writeChar(end); n } n inline void writeWord(const char *s) { n   while (*s) writeChar(*s++); n } n struct Flusher { n   ~Flusher() { n     if (write_pos) fwrite(write_buf, 1, write_pos, stdout), write_pos = 0; n   } n } flusher; n const int M = 1e5 + 5; n int n; n pair<pair<int, int>, int> b[M]; n vector<int> cop; n long long t[8 * M]; n long long dp[M]; n void update(int v, int vl, int vr, int l, int r, long long val) { n   if (l > vr || r < vl) return; n   if (l <= vl && vr <= r) n     t[v] = max(t[v], val); n   else { n     int vm = (vl + vr) / 2; n     update(v * 2 + 1, vl, vm, l, r, val); n     update(v * 2 + 2, vm + 1, vr, l, r, val); n     t[v] = max(t[v * 2 + 1], t[v * 2 + 2]); n   } n } n long long get(int v, int vl, int vr, int l, int r) { n   if (l > vr || r < vl) return 0; n   if (l <= vl && vr <= r) return t[v]; n   int vm = (vl + vr) / 2; n   return max(get(v * 2 + 1, vl, vm, l, r), get(v * 2 + 2, vm + 1, vr, l, r)); n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d%d%d , &b[i].first.second, &b[i].first.first, &b[i].second); n     cop.push_back(b[i].first.first), cop.push_back(b[i].first.second); n   } n   sort(cop.begin(), cop.end()); n   for (int i = 0; i < n; ++i) { n     b[i].first.first = n         lower_bound(cop.begin(), cop.end(), b[i].first.first) - cop.begin() + 1; n     b[i].first.second = lower_bound(cop.begin(), cop.end(), b[i].first.second) - n                         cop.begin() + 1; n   } n   sort(b, b + n); n   dp[n - 1] = b[n - 1].second; n   update(0, 0, 2 * M, b[n - 1].first.second, b[n - 1].first.second, dp[n - 1]); n   long long mx = dp[n - 1]; n   for (int i = n - 2; i >= 0; --i) { n     long long x = get(0, 0, 2 * M, 0, b[i].first.first - 1); n     dp[i] = x + b[i].second; n     mx = max(mx, dp[i]); n     update(0, 0, 2 * M, b[i].first.second, b[i].first.second, dp[i]); n   } n   cout << mx; n } n


#include <bits/stdc++.h> n using namespace std; n long long t, a, b, d; n bool fun(long long mid) { n   long long total = mid * (mid + 1) / 2; n   if (total >= d) { n     return true; n   } n   return false; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> t; n   while (t--) { n     cin >> a >> b; n     d = abs(a - b); n     long long s = 0, e = 1000000007, mid; n     while (s < e) { n       mid = (s + e) / 2; n       if (fun(mid)) { n         e = mid; n       } else { n         s = mid + 1; n       } n     } n     long long total = s * (s + 1) / 2; n     while (total % 2 != d % 2) { n       s++; n       total = s * (s + 1) / 2; n     } n     cout << s << endl; n   } n   return 0; n } n