#include <bits/stdc++.h> n void __check(const char* expr, const char* file, unsigned int line) { n   printf( %s:%u: failed check `%s  n , file, line, expr); n   abort(); n } n using namespace std; n template <typename T> n bool uin(T& a, T b) { n   if (b < a) { n     a = b; n     return true; n   } n   return false; n } n template <typename T> n bool uax(T& a, T b) { n   if (a < b) { n     a = b; n     return true; n   } n   return false; n } n template <typename T> n int sz(const T& t) { n   return static_cast<int>(t.size()); n } n template <typename T> n void shrink(std::vector<T>& v) { n   std::vector<T>(v).swap(v); n } n const bool DBG = false; n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   vector<int> v(1 + n); n   for (int i = 0; i < (int)(n); ++i) { n     cin >> v[i]; n     v[i]--; n   } n   v[n] = v[n - 1] ^ 1; n   int prev = v[0]; n   array<int, 2> a; n   a.fill(0); n   a[prev]++; n   int res = 0; n   for (int i = 1; i <= n; ++i) { n     int value = v[i]; n     if (value != prev) { n       if (a[value] > 0) { n         int opposite = value ^ 1; n         auto local_res = min(a[opposite], a[value]) * 2; n         res = max(local_res, res); n         a[value] = 0; n       } n     } n     a[value]++; n     prev = value; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Player { n   string name; n   long int points; n   map<int, int> places; n }; n Player compSecond(Player a, Player b) { n   if (a.points > b.points) { n     return a; n   } else if (a.points < b.points) { n     return b; n   } n   for (int i = 1; i < 70; i++) { n     if (a.places[i] > b.places[i]) { n       return a; n     } n     if (a.places[i] < b.places[i]) { n       return b; n     } n   } n } n Player compFirst(Player a, Player b) { n   if (a.places[1] > b.places[1]) { n     return a; n   } else if (a.places[1] < b.places[1]) { n     return b; n   } n   if (a.points > b.points) { n     return a; n   } else if (a.points < b.points) { n     return b; n   } n   for (int i = 2; i < 70; i++) { n     if (a.places[i] > b.places[i]) { n       return a; n     } n     if (a.places[i] < b.places[i]) { n       return b; n     } n   } n } n map<string, Player> parts; n vector<int> points = {25, 18, 15, 12, 10, 8, 6, 4, 2, 1}; n int main() { n   int n; n   cin >> n; n   set<Player> st; n   for (int i = 0; i < n; i++) { n     int players; n     cin >> players; n     for (int j = 1; j <= players; j++) { n       string name; n       cin >> name; n       parts[name].points += (j <= 10) ? points[j - 1] : 0; n       parts[name].places[j]++; n       parts[name].name = name; n     } n   } n   Player min; n   Player first; n   Player second; n   first.points = 0; n   second.points = 0; n   for (auto i : parts) { n     first = compSecond(i.second, first); n     second = compFirst(i.second, second); n   } n   cout << first.name << endl << second.name << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c; n   cin >> a; n   b = 1; n   while ((b * b + 1) / 2 < a) b += 2; n   cout << (a == 3 ? 5 : b); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n pair<int, int> a[111]; n int m; n long long p[111]; n long long an; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d%d , &a[i].second, &a[i].first); n   sort(a + 1, a + 1 + n); n   scanf( %d , &m); n   for (int i = 1; i <= m; ++i) scanf( %I64d , &p[i]); n   p[++m] = 1e12; n   long long cur = 0; n   int ps = 1; n   for (int i = 1; i <= n; ++i) { n     while (a[i].second) { n       if (cur + a[i].second > p[ps]) { n         a[i].second -= p[ps] - cur; n         an += 1ll * ps * (p[ps] - cur) * a[i].first; n         cur = p[ps++]; n       } else { n         an += 1ll * a[i].second * ps * a[i].first; n         cur += a[i].second; n         a[i].second = 0; n       } n     } n   } n   printf( %I64d , an); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 4e5 + 5; n struct Edge { n   int v, nxt; n   Edge() {} n   Edge(int _v, int _nxt) : v(_v), nxt(_nxt) {} n }; n Edge E[MAXN * 2]; n int head[MAXN], ans[MAXN], sub[MAXN], mxsub[MAXN]; n int tot = 0, fuc, n, x, y; n void addedge(int u, int v) { n   E[tot] = Edge(v, head[u]); n   head[u] = tot++; n } n void dfs1(int now, int fa) { n   sub[now] = 1; n   mxsub[now] = 1; n   int tmp = 1; n   for (int i = head[now]; ~i; i = E[i].nxt) { n     int v = E[i].v; n     if (v != fa) { n       dfs1(v, now); n       sub[now] += sub[v]; n       mxsub[now] = max(mxsub[now], mxsub[v]); n       tmp += sub[v]; n     } n   } n   if (tmp <= fuc) { n     mxsub[now] = tmp; n   } n } n void dfs2(int now, int fa, int subfa, int mxfa) { n   int cnt = subfa > fuc; n   int id; n   if (cnt) { n     id = -1; n   } n   int tmp = n; n   int mx1, mx2; n   int id1, id2; n   mx1 = mx2 = -1; n   for (int i = head[now]; ~i; i = E[i].nxt) { n     int v = E[i].v; n     if (v != fa) { n       if (id1 == -1 || mx1 < mxsub[v]) { n         mx2 = mx1; n         id2 = id1; n         mx1 = mxsub[v]; n         id1 = v; n       } else if (id2 == -1 || mx2 < mxsub[v]) { n         mx2 = mxsub[v]; n         id2 = v; n       } n     } n   } n   for (int i = head[now]; ~i; i = E[i].nxt) { n     int v = E[i].v; n     if (v != fa) { n       dfs2(v, now, sub[now] - sub[v] + subfa, n            max(tmp - sub[v] <= fuc ? tmp - sub[v] : 0, n                max(mxfa, id1 == v ? mx2 : mx1))); n       if (sub[v] > fuc) { n         cnt++; n         id = v; n       } n     } n   } n   if (cnt > 1) { n     ans[now] = 0; n   } else if (cnt == 0) { n     ans[now] = 1; n   } else if (id != -1) { n     ans[now] = (sub[id] - mxsub[id] <= fuc); n   } else { n     ans[now] = (subfa - mxfa <= fuc); n   } n } n int main() { n   memset(head, -1, sizeof(head)); n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     scanf( %d%d , &x, &y); n     addedge(x, y); n     addedge(y, x); n   } n   fuc = n / 2; n   dfs1(1, 1); n   dfs2(1, 1, 0, 0); n   for (int i = 1; i <= n; i++) { n     printf( %d  , ans[i]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 0.0000001; n const double PI = acos(-1); n const long long INFLL = 0x7FFFFFFFFFFFFFFF; n const int INF = 0x7FFFFFFF; n template <typename T> n inline void next(T &num) { n   char c; n   num = 0; n   do { n     c = getchar_unlocked(); n   } while (c != EOF && c ==     && c ==   n  && c ==   t ); n   int sign = (c ==  -  ? -1 : 1); n   if (c !=  - ) num += (c -  0 ); n   while ((c = getchar_unlocked()) != EOF && c !=   n  && c !=   t  && n          c !=    ) { n     num *= 10; n     num += (c -  0 ); n   } n   num *= sign; n } n inline string getstr() { n   string str; n   char k; n   while ((k = getchar_unlocked()) ==     || k ==   n ) { n     k = getchar_unlocked(); n     if (k ==     || k ==   n ) n       continue; n     else n       break; n   } n   str.push_back(k); n   while ((k = getchar_unlocked()) != EOF && k !=   n  && k !=   t  && n          k !=   v  && k !=   0  && k !=    ) n     str.push_back(k); n   return str; n } n const int M = 50005; n long long n, a[M]; n long long ans; n void dfs(long long n, int p, long long now) { n   if (now > ans) return; n   if (!n) { n     ans = min(ans, now); n     return; n   } n   if (!p) return; n   int need = n / a[p]; n   dfs(n - need * a[p], p - 1, now + p * need); n   dfs((need + 1) * a[p] - n, p - 1, now + p * (need + 1)); n } n int main() { n   cin >> n; n   for (int i = int(1), _b = int(16); i <= _b; i++) a[i] = a[i - 1] * 10 + 1; n   ans = n; n   dfs(n, 16, 0); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-8; n const int INF = 1e9; n const int N = 150010; n int n, m, s, k; n int a[110][110]; n int main() { n   scanf( %d%d%d , &n, &m, &k); n   int s0 = 0, s1 = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       scanf( %d , &a[i][j]); n       if (a[i][j] == 0) n         s0++; n       else n         s1++; n     } n   } n   int ans = min(s0, s1); n   int b[110], sum, tmp; n   memset(b, 0, sizeof(b)); n   for (int i = 0; i < n; i++) { n     sum = 0, tmp = 0; n     for (int j = 0; j < m; j++) { n       b[j] = a[i][j]; n     } n     for (int j = 0; j < n; j++) { n       if (j == i) continue; n       tmp = 0; n       for (int t = 0; t < m; t++) { n         if (a[j][t] != b[t]) tmp++; n       } n       tmp = min(tmp, m - tmp); n       sum += tmp; n     } n     ans = min(ans, sum); n   } n   memset(b, 0, sizeof(b)); n   for (int j = 0; j < m; j++) { n     sum = 0, tmp = 0; n     for (int i = 0; i < n; i++) { n       b[i] = a[i][j]; n     } n     for (int i = 0; i < m; i++) { n       if (i == j) continue; n       tmp = 0; n       for (int t = 0; t < n; t++) { n         if (a[t][i] != b[t]) tmp++; n       } n       tmp = min(tmp, n - tmp); n       sum += tmp; n     } n     ans = min(ans, sum); n   } n   if (ans > k) n     puts( -1 ); n   else n     printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long modulo = 1000000007; n int choose(int n, int r) { n   if (n < r) { n     return 0; n   } n   if (n == r || r == 0) { n     return 1; n   } n   return choose(n - 1, r - 1) + choose(n - 1, r); n } n int get_factors(int n) { n   int numFactors = 0; n   for (int i = 1; i <= sqrt(n); i++) { n     bool divisible = n % i == 0; n     if (!divisible) { n       continue; n     } n     int quotient = n / i; n     numFactors++; n     if (quotient != i) { n       numFactors++; n     } n   } n   return numFactors; n } n int sumDigits(int num) { n   int sum = 0; n   while (num > 0) { n     sum += num % 10; n     num /= 10; n   } n   return sum; n } n int gcd(int a, int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n vector<int> dfs(int i, vector<int> graph[], vector<bool>& visited) { n   vector<int> group = {i}; n   vector<int> connected = graph[i]; n   visited[i] = true; n   for (int j : connected) { n     if (!visited[j]) { n       vector<int> vals = dfs(j, graph, visited); n       for (int val : vals) { n         group.push_back(val); n       } n     } n   } n   return group; n } n bool comparePairs(pair<long long, long long> p1, n                   pair<long long, long long> p2) { n   if (p1.first < p2.first) { n     return true; n   } else if (p1.first > p2.first) { n     return false; n   } else { n     return p1.second < p2.second; n   } n } n vector<int> primes(int n) { n   vector<int> p(n + 1); n   for (int i = 0; i <= n; i++) { n     p[i] = i; n   } n   p[0] = -1; n   p[1] = -1; n   for (int i = 2; i <= n; i++) { n     if (p[i] == -1) { n       continue; n     } n     int temp = i * i; n     while (temp <= n) { n       p[temp] = -1; n       temp += i; n     } n   } n   return p; n } n vector<long long> nextPrimes(long long n) { n   vector<long long> p(n + 1, 0); n   for (long long i = 0; i <= n; i++) { n     p[i] = i; n   } n   p[0] = -1; n   p[1] = -1; n   for (long long i = 2; i <= n; i++) { n     if (p[i] == -1) { n       continue; n     } n     long long temp = i * i; n     while (temp <= n) { n       p[temp] = -1; n       temp += i; n     } n   } n   long long last = -1; n   for (long long i = n; i >= 0; i--) { n     if (p[i] == -1) { n       p[i] = last; n     } else { n       last = p[i]; n     } n   } n   return p; n } n bool comparePairs(const pair<int, int>& lhs, const pair<int, int>& rhs) { n   return abs(lhs.first - lhs.second) < abs(rhs.first - lhs.second); n } n void solve() { n   int n, k; n   cin >> n >> k; n   vector<int> want(k + 1); n   vector<bool> taken(n * k + 1, false); n   for (int i = 1; i < k + 1; i++) { n     int iOfWant; n     cin >> iOfWant; n     want[i] = iOfWant; n     taken[iOfWant] = true; n   } n   int curr = 1; n   for (int i = 1; i < k + 1; i++) { n     cout << want[i] <<    ; n     for (int j = 0; j < n - 1; j++) { n       while (taken[curr]) { n         curr++; n       } n       cout << curr <<    ; n       curr++; n     } n     cout <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t = 1; n   while (t-- > 0) { n     solve(); n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, K; n long long B; n long long A[100005], Ac[100005]; n int main() { n   cin >> N >> K >> B; n   map<long long, int> ct; n   for (int i = 0; i < N; i++) { n     cin >> A[i]; n     ct[A[i]]++; n   } n   ct[A[N - 1]]--; n   for (int i = 0; i < N - 1; i++) Ac[i] = A[i]; n   sort(Ac, Ac + N - 1); n   reverse(Ac, Ac + N - 1); n   if (K >= N) K = N - 1; n   long long presum = 0; n   long long nth = Ac[K - 1]; n   for (int i = 0; i < K; i++) presum += Ac[i]; n   for (int i = 0; i < N - 1; i++) { n     long long s = presum - max(nth, A[i]); n     if (s + A[i] > B) { n       cout << i + 1 <<   n ; n       return 0; n     } n   } n   cout << N <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int nax = 2e5; n int par[nax], dp[nax]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   map<int, int> prev; n   int ma = -1, mai; n   for (int i = 0; i < n; i++) { n     int a; n     cin >> a; n     dp[i] = 1; n     par[i] = -1; n     if (prev.count(a - 1)) { n       par[i] = prev[a - 1]; n       dp[i] = dp[prev[a - 1]] + 1; n     } n     if (dp[i] > ma) { n       ma = dp[i]; n       mai = i; n     } n     prev[a] = i; n   } n   cout << ma << endl; n   int p = mai; n   vector<int> path; n   while (p != -1) { n     path.push_back(p); n     p = par[p]; n   } n   reverse(path.begin(), path.end()); n   for (int i : path) cout << i + 1 <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a; n   cin >> a; n   while (a--) { n     int b, c; n     cin >> b >> c; n     int n = abs(b - c); n     n = n / 5 + (n % 5) / 2 + (n % 5) % 2; n     cout << n <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n multiset<long long> buckets[30]; n long long bsum[30]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int q, tot = 0; n   cin >> q; n   while (q--) { n     char c; n     long long x; n     cin >> c >> x; n     int p = 31 - __builtin_clz(x), ans = 0; n     if (c ==  + ) n       buckets[p].insert(x), bsum[p] += x, tot++; n     else n       buckets[p].erase(buckets[p].find(x)), bsum[p] -= x, tot--; n     long long sm = 0; n     for (int i = 0; i < 30; i++) n       if (buckets[i].size()) { n         if (*buckets[i].begin() > 2 * sm) ans++; n         sm += bsum[i]; n       } n     cout << tot - ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ull = unsigned long long; n int n; n int cnt[1 << 20]; n int cnt2[1 << 20]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   cin >> n; n   ll res = 0; n   ++cnt2[0]; n   for (int pref = 0, a, i = 1; i <= n; ++i) { n     cin >> a; n     pref ^= a; n     if (i & 1) { n       res += cnt[pref]; n       ++cnt[pref]; n     } else { n       res += cnt2[pref]; n       ++cnt2[pref]; n     } n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unordered_set<int> si; n int fff = 0; n int fj = 2; n void mmin(int &t) { n   for (int j = fj; 1;) { n     int fflag = 0; n     vector<int> pr; n     for (int i = 2; i < sqrt(j) + 1; i++) { n       if (j % i == 0) { n         if (si.find(i) == si.end() && si.find(j / i) == si.end()) { n           pr.push_back(i); n           pr.push_back(j / i); n         } else { n           fflag = 2; n           break; n         } n       } n     } n     if (fflag == 0 && si.find(j) == si.end()) { n       for (auto tt : pr) si.insert(tt); n       si.insert(j); n       t = j; n       fj = j + 1; n       fff = 1; n       break; n     } n     if (j % 2 == 0) n       j++; n     else n       j += 2; n   } n } n void check(int &t) { n   vector<int> prime; n   int flag = 0; n   for (int i = 2; i < sqrt(t) + 1; i++) { n     if (t % i == 0) { n       if (si.find(i) == si.end()) n         prime.push_back(i); n       else { n         flag = 1; n         break; n       } n       if (si.find(t / i) == si.end()) n         prime.push_back(t / i); n       else { n         flag = 1; n         break; n       } n     } n   } n   if (si.find(t) == si.end()) n     prime.push_back(t); n   else { n     flag = 1; n   } n   if (flag) { n     for (int j = t + 1; 1; j++) { n       int fflag = 0; n       for (int i = 2; i < sqrt(j) + 1; i++) { n         if (j % i == 0) { n           if (si.find(i) == si.end() && si.find(j / i) == si.end()) n             ; n           else { n             fflag = 2; n             break; n           } n         } n       } n       if (fflag == 0 && si.find(j) == si.end()) { n         for (int i = 2; i < max((int)sqrt(j) + 1, j); i++) n           if (j % i == 0) { n             si.insert(i); n             si.insert(j / i); n           } n         si.insert(j); n         t = j; n         fff = 1; n         break; n       } n     } n   } else { n     for (auto tt : prime) si.insert(tt); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   vector<int> ans; n   for (int i = 0; i < n; i++) { n     int t; n     cin >> t; n     if (t == 1) { n       ans.push_back(t); n       continue; n     } n     if (fff == 1) n       mmin(t); n     else n       check(t); n     ans.push_back(t); n   } n   for (auto t : ans) cout << t <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 2000005; n const int INF = 0x3f3f3f3f; n struct Num { n   int a, b; n   Num() {} n   Num(int x, int y) : a(x), b(y) {} n   bool operator<(const Num &y) const { return a == y.a ? b < y.b : a < y.a; } n   bool operator>(const Num &y) const { return a == y.a ? b > y.b : a > y.a; } n   bool operator==(const Num &y) const { return a == y.a && b == y.b; } n   Num operator+(const Num &y) const { return Num(a + y.a, b + y.b); } n }; n struct State { n   Num n; n   int base; n   long long route; n   bool operator<(const State &b) const { return n < b.n; } n   bool operator==(const State &b) const { return n == b.n; } n }; n bool cmp(const State &a, const State &b) { n   return a.n == b.n ? a.base > b.base : a.n < b.n; n } n State sta[MaxN]; n int cou; n int N; n Num rem[30][3]; n int rnum[30]; n int bbb; n long long lll; n void dfs1(int d, Num tn, int b, long long l) { n   if (d > ((N + 1) >> 1)) { n     sta[cou].n = tn; n     sta[cou].base = b; n     sta[cou++].route = l; n     return; n   } n   for (int i = 0; i < 3; ++i) n     dfs1(d + 1, tn + rem[d][i], b + (i ? rnum[d] : 0), (l << 2) | i); n } n void dfs2(int d, Num tn, int b, long long l) { n   if (d > N) { n     State ts; n     ts.n.a = -tn.a; n     ts.n.b = -tn.b; n     int p = lower_bound(sta, sta + cou, ts) - sta; n     if (p < cou && sta[p] == ts && sta[p].base + b > bbb) { n       bbb = sta[p].base + b; n       lll = (sta[p].route << (2 * (N / 2))) | l; n     } n     return; n   } n   for (int i = 0; i < 3; ++i) n     dfs2(d + 1, tn + rem[d][i], b + (i ? rnum[d] : 0), (l << 2) | i); n } n void show() { n   int rrr[30]; n   for (int i = 0; i < N; ++i) { n     rrr[i] = lll & 3; n     lll >>= 2; n   } n   for (int i = N - 1; i >= 0; --i) n     if (rrr[i] == 0) n       puts( MW ); n     else if (rrr[i] == 1) n       puts( LW ); n     else n       puts( LM ); n } n int main() { n   int a, b, c; n   scanf( %d , &N); n   for (int i = 1; i <= N; ++i) { n     scanf( %d %d %d , &a, &b, &c); n     rnum[i] = a; n     rem[i][0] = Num(b, c); n     rem[i][1] = Num(-a, c - a); n     rem[i][2] = Num(b - a, -a); n   } n   cou = 0; n   bbb = -INF; n   dfs1(1, Num(0, 0), 0, 0); n   sort(sta, sta + cou, cmp); n   cou = unique(sta, sta + cou) - sta; n   dfs2((N + 1) / 2 + 1, Num(0, 0), 0, 0); n   if (bbb == -INF) n     puts( Impossible ); n   else n     show(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 2; n long long gcd(long long a, long long b) { n   if (b > a) { n     return gcd(b, a); n   } n   if (b == 0) { n     return a; n   } n   return gcd(b, a % b); n } n long long expo(long long a, long long b, long long mod) { n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = (res * a) % mod; n     a = (a * a) % mod; n     b = b >> 1; n   } n   return res; n } n long long mminvprime(long long a, long long b) { return expo(a, b - 2, b); } n vector<long long> sieve(int n) { n   int* arr = new int[n + 1](); n   vector<long long> vect; n   for (int i = 2; i <= n; i++) n     if (arr[i] == 0) { n       vect.push_back(i); n       for (int j = 2 * i; j <= n; j += i) arr[j] = 1; n     } n   return vect; n } n long long mod_add(long long a, long long b, long long m) { n   a = a % m; n   b = b % m; n   return (((a + b) % m) + m) % m; n } n long long mod_mul(long long a, long long b, long long m) { n   a = a % m; n   b = b % m; n   return (((a * b) % m) + m) % m; n } n long long mod_sub(long long a, long long b, long long m) { n   a = a % m; n   b = b % m; n   return (((a - b) % m) + m) % m; n } n long long mod_div(long long a, long long b, long long m) { n   a = a % m; n   b = b % m; n   return (mod_mul(a, mminvprime(b, m), m) + m) % m; n } n int fact[N]; n void precalc() { n   fact[0] = 1; n   for (int i = 1; i < N; i++) { n     fact[i] = mod_mul(fact[i - 1], i, 1000000007); n   } n } n long long mod(long long x) { n   return ((x % 1000000007 + 1000000007) % 1000000007); n } n long long inv(long long x) { return expo(x, 1000000007 - 2, 1000000007); } n long long divide(long long a, long long b) { n   return mod_mul(a, inv(b), 1000000007); n } n long long nCr(long long n, long long r) { n   return divide(fact[n], mod_mul(fact[r], fact[n - r], 1000000007)); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     map<char, long long> m; n     long long cnt = 0; n     for (int i = 0; i < s.length(); i++) { n       m[s[i]]++; n     } n     for (auto x : m) { n       if (x.second <= 2) n         cnt += x.second; n       else n         cnt += 2; n     } n     if (cnt % 2 != 0) cnt--; n     cout << cnt / 2 << (  n ); n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:100000000000000 ) n using namespace std; n const long long int INF = 2e18; n char mode[5002 / 2][5002]; n short int cup[4][5002 / 2][5002]; n short int up[4][5002 / 2][5002]; n int best = 0, cnt = 0; n inline int geti(int type, int x, int y) { n   if (up[type][x][y] == -1 || (type >= 2 && up[type][x][y] == x) || n       (type < 2 && up[type][x][y] == y)) n     return up[type][x][y]; n   int nex = up[type][x][y]; n   if (type >= 2) return up[type][x][y] = geti(type, nex, y); n   return up[type][x][y] = geti(type, x, nex); n } n inline void upd(int x, int y) { n   for (int i = 0; i < 2; i++) { n     int plus = (!i ? -1 : 1); n     up[i][x][y] = y + plus; n   } n   for (int i = 0; i < 2; i++) { n     int plus = (!i ? -1 : 1); n     up[i + 2][x][y] = x + plus; n   } n } n int n, m; n int dfs(int i, int j) { n   char c = mode[i][j]; n   upd(i, j); n   int out = 1; n   if (c ==  L ) { n     int nex = geti(0, i, j); n     if (nex != -1 && nex < m) out = dfs(i, nex) + 1; n   } n   if (c ==  R ) { n     int nex = geti(1, i, j); n     if (nex != -1 && nex < m) out = dfs(i, nex) + 1; n   } n   if (c ==  U ) { n     int nex = geti(2, i, j); n     if (nex != -1 && nex < n) out = dfs(nex, j) + 1; n   } n   if (c ==  D ) { n     int nex = geti(3, i, j); n     if (nex != -1 && nex < n) out = dfs(nex, j) + 1; n   } n   if (out > best) { n     best = out; n     cnt = 0; n   } n   if (out == best) cnt++; n   return out; n } n int main() { n   cin >> n >> m; n   char c; n   scanf(  n ); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       scanf( %c , &c); n       if (n < m) n         mode[i][j] = c; n       else { n         if (c ==  U ) n           c =  R ; n         else if (c ==  R ) n           c =  D ; n         else if (c ==  D ) n           c =  L ; n         else if (c !=  . ) n           c =  U ; n         mode[j][n - 1 - i] = c; n       } n       if (c ==  . ) continue; n     } n     scanf(  n ); n   } n   if (n >= m) swap(n, m); n   for (int i = 0; i <= n; i++) { n     for (int j = 0; j <= m; j++) { n       cup[1][i][j] = m; n       cup[3][i][j] = n; n     } n   } n   vector<int> pre(m, -1); n   for (int i = 0; i < n; i++) { n     int pp = -1; n     for (int j = 0; j < m; j++) { n       if (mode[i][j] !=  . ) { n         pre[j] = i; n         pp = j; n       } n       cup[0][i][j] = pp; n       cup[2][i][j] = pre[j]; n     } n   } n   pre.assign(m, n); n   for (int i = n - 1; i >= 0; i--) { n     int pp = m; n     for (int j = m - 1; j >= 0; j--) { n       if (mode[i][j] !=  . ) { n         pre[j] = i; n         pp = j; n       } n       cup[1][i][j] = pp; n       cup[3][i][j] = pre[j]; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       for (int k = 0; k < 4; k++) { n         for (int i2 = 0; i2 <= n; i2++) { n           for (int j2 = 0; j2 <= m; j2++) { n             up[k][i2][j2] = cup[k][i2][j2]; n           } n         } n       } n       if (mode[i][j] !=  . ) dfs(i, j); n     } n   } n   cout << best <<     << cnt; n } n


#include <bits/stdc++.h> n using namespace std; n long long totalSum; n long long check(long long a, long long b, long long c, long long r) { n   if (b + c > a) { n     return 0; n   } n   long long d = min(r, a - b - c); n   return (d + 2) * (d + 1) / 2; n } n int main() { n   long long a, b, c, l, total; n   cin >> a >> b >> c >> l; n   totalSum = a + b + c + l; n   total = (l + 3) * (l + 2) * (l + 1) / 6; n   for (long long i = 0; i <= l; i++) { n     total -= check(a + i, b, c, l - i); n     total -= check(b + i, a, c, l - i); n     total -= check(c + i, b, a, l - i); n   } n   cout << total <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const double alpha = 0.9; n struct Node { n   int size, key, nodeCount; n   bool isExist; n   Node *lc, *rc; n   unordered_map<int, int> mp; n   bool isBad() { n     return lc->nodeCount > alpha * nodeCount + 5 || n            rc->nodeCount > alpha * nodeCount + 5; n   } n   void merge() { n     size = isExist; n     size += lc->size; n     size += rc->size; n     nodeCount = 1; n     nodeCount += lc->nodeCount; n     nodeCount += rc->nodeCount; n     mp.clear(); n     for (auto e : lc->mp) { n       mp[e.first] += e.second; n     } n     for (auto e : rc->mp) { n       mp[e.first] += e.second; n     } n     if (isExist) mp[key]++; n   } n } pool[N * 2], *C = pool + 1, *root, *null; n Node *newNode(int key) { n   C->size = 1; n   C->key = key; n   C->mp.clear(); n   C->mp[key] = 1; n   C->isExist = 1; n   C->lc = C->rc = null; n   return C++; n } n int n, q; n Node *buildTree(int *a, int l, int r) { n   if (l >= r) return null; n   int mid = (l + r) / 2; n   Node *p = newNode(a[mid]); n   p->lc = buildTree(a, l, mid); n   p->rc = buildTree(a, mid + 1, r); n   p->merge(); n   return p; n } n void dfs(Node *p, vector<Node *> &v) { n   if (p == null) return; n   dfs(p->lc, v); n   v.push_back(p); n   dfs(p->rc, v); n } n Node *divide(vector<Node *> &v, int l, int r) { n   if (l >= r) return null; n   int mid = (l + r) / 2; n   Node *p = v[mid]; n   p->lc = divide(v, l, mid); n   p->rc = divide(v, mid + 1, r); n   p->merge(); n   return p; n } n void rebuild(Node *&p) { n   vector<Node *> v; n   dfs(p, v); n   p = divide(v, 0, v.size()); n } n int erase(Node *p, int idx) { n   if (p->isExist && idx == p->lc->size + 1) { n     p->isExist = 0; n     p->mp[p->key]--; n     p->size--; n     return p->key; n   } n   int res; n   p->size--; n   res = idx <= p->lc->size ? erase(p->lc, idx) n                            : erase(p->rc, idx - p->lc->size - p->isExist); n   p->mp[res]--; n   return res; n } n Node **insert(Node *&p, int idx, int val) { n   if (p == null) { n     p = newNode(val); n     return &null; n   } else { n     p->nodeCount++; n     p->size++; n     p->mp[val]++; n     Node **res = idx <= p->lc->size + 1 n                      ? insert(p->lc, idx, val) n                      : insert(p->rc, idx - p->lc->size - p->isExist, val); n     if (p->isBad()) res = &p; n     return res; n   } n } n int getNumber(Node *p, int l, int r, int w) { n   if (p == null) return 0; n   if (l <= 1 && p->size <= r) { n     return p->mp.count(w) ? p->mp[w] : 0; n   } else { n     int res = 0; n     if (l <= p->lc->size) res += getNumber(p->lc, l, r, w); n     if (r > p->lc->size + p->isExist) n       res += getNumber(p->rc, l - p->lc->size - p->isExist, n                        r - p->lc->size - p->isExist, w); n     if (p->isExist && l <= p->lc->size + 1 && p->lc->size + 1 <= r && n         p->key == w) { n       res++; n     } n     return res; n   } n } n int main() { n   null = pool; n   null->size = 0; n   null->nodeCount = 0; n   null->key = 0; n   null->mp.clear(); n   null->isExist = 0; n   null->lc = null->rc = null; n   static int a[N]; n   int lastans = 0; n   scanf( %d , &n); n   for (int i = 0; i < (int)(n); i++) { n     scanf( %d , &a[i]); n   } n   root = buildTree(a, 0, n); n   scanf( %d , &q); n   for (int i = 0; i < (int)(q); i++) { n     int op, l, r, w; n     scanf( %d%d%d , &op, &l, &r); n     l = (l + lastans - 1 + n) % n + 1; n     r = (r + lastans - 1 + n) % n + 1; n     if (l > r) swap(l, r); n     if (op == 1) { n       int value = erase(root, r); n       Node **p = insert(root, l, value); n       if (*p != null) rebuild(*p); n     } else if (op == 2) { n       scanf( %d , &w); n       w = (w + lastans - 1 + n) % n + 1; n       lastans = getNumber(root, l, r, w); n       printf( %d n , lastans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<long long, long long> A[55]; n double pos[55][55]; n bool comp(pair<long long, long long> a, pair<long long, long long> b) { n   if (a.first > b.first) n     return 1; n   else if (a.first < b.first) n     return 0; n   else { n     return a.second <= b.second; n   } n } n bool vis[55][55]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> A[i].first; n   for (int i = 1; i <= n; ++i) cin >> A[i].second; n   sort(A + 1, A + n + 1, comp); n   long long low = 0; n   long long high = 10000000000000; n   int count = 10000; n   while (--count) { n     long long mid = low + high; n     mid /= 2.0; n     for (int i = 0; i <= n; ++i) { n       for (int j = 0; j <= n; ++j) { n         vis[i][j] = 0; n       } n     } n     pos[0][0] = 0; n     vis[0][0] = 1; n     for (int i = 1; i <= n; ++i) { n       long long st = A[i].first; n       int u = i - 1; n       while (i <= n && A[i].first == st) ++i; n       --i; n       int coun = i - u; n       long long sum[55]; n       for (int j = u + 1; j <= i; ++j) sum[j - u] = 0; n       sum[0] = 0; n       for (int j = u + 1; j <= i; ++j) n         sum[j - u] = sum[j - u - 1] + A[j].second; n       long long sum2[55]; n       for (int j = u + 1; j <= i; ++j) sum2[j - u] = 0; n       sum2[0] = 0; n       for (int j = u + 1; j <= i; ++j) n         sum2[j - u] = sum2[j - u - 1] + A[j].first; n       for (int j = 0; j <= u; ++j) { n         for (int l = 0; l <= min(coun, j); ++l) { n           if (vis[u][j] == 0) continue; n           int rem = j - l + coun - l; n           if (vis[i][rem] == 0) { n             vis[i][rem] = 1; n             pos[i][rem] = pos[u][j] - (sum[coun] - sum[l]) * mid / 1000.0 + n                           (sum2[coun] - sum2[l]); n           } else n             pos[i][rem] = min(pos[i][rem], n                               pos[u][j] - (sum[coun] - sum[l]) * mid / 1000.0 + n                                   (sum2[coun] - sum2[l])); n         } n       } n     } n     bool flag = 0; n     for (int j = 0; j <= n; ++j) { n       if (pos[n][j] <= 0 && vis[n][j]) { n         flag = 1; n       } n     } n     if (flag) { n       high = mid; n     } else { n       low = mid + 1; n     } n   } n   cout << high << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long n, m, i, j, mn, mx; n   scanf( %ld %ld , &n, &m); n   long a[n][m]; n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       scanf( %ld , &a[i][j]); n     } n   } n   mx = 0; n   for (i = 0; i < n; i++) { n     mn = LONG_MAX; n     for (j = 0; j < m; j++) { n       mn = min(a[i][j], mn); n     } n     mx = max(mn, mx); n   } n   printf( %ld , mx); n } n


#include <bits/stdc++.h> n using namespace std; n long long pwr(long long a, long long b, long long mod) { n   a %= mod; n   if (a < 0) a += mod; n   long long ans = 1; n   while (b) { n     if (b & 1) ans = (ans * a) % mod; n     a = (a * a) % mod; n     b /= 2; n   } n   return ans; n } n long long pwr(long long a, long long b) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans *= a; n     a *= a; n     b /= 2; n   } n   return ans; n } n long long gcd(long long a, long long b) { n   while (b) { n     long long temp = a; n     a = b; n     b = temp % b; n   } n   return a; n } n long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; } n long long modularInverse(long long a, long long m) { n   assert(false); n   return pwr(a, m - 2, m); n } n int main(int argc, char const *argv[]) { n   std::ios::sync_with_stdio(false); n   int n, m, num1, num2; n   cin >> n >> m; n   if (n > m) { n     num1 = n; n     num2 = m; n   } else { n     num1 = m; n     num2 = n; n   } n   long long sum = 0; n   while (num2 != 0 && num1 >= 2) { n     sum++; n     num2--; n     num1 = num1 - 2; n     if (num1 < num2) { n       swap(num1, num2); n     } n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using lpair = pair<ll, ll>; n const ll MOD = 1e9 + 7; n const ll INF = 8e18; n void print() {} n template <class H, class... T> n void print(H&& h, T&&... t) { n   cout << h <<    n [sizeof...(t) == 0]; n   print(forward<T>(t)...); n } n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   ll t; n   cin >> t; n   while (t--) { n     ll nr, ng, nb; n     cin >> nr >> ng >> nb; n     vector<ll> r(nr), g(ng), b(nb); n     for (ll i = (0); i < (nr); i++) cin >> r[i]; n     for (ll i = (0); i < (ng); i++) cin >> g[i]; n     for (ll i = (0); i < (nb); i++) cin >> b[i]; n     sort(r.begin(), r.end()); n     sort(g.begin(), g.end()); n     sort(b.begin(), b.end()); n     ll ans = INF; n     vector<lpair> lp; n     for (ll i = (0); i < (nr); i++) { n       lp.push_back({r[i], 0}); n     } n     for (ll i = (0); i < (ng); i++) { n       lp.push_back({g[i], 1}); n     } n     for (ll i = (0); i < (nb); i++) { n       lp.push_back({b[i], 2}); n     } n     sort(lp.begin(), lp.end()); n     vector<ll> pr, pg, pb; n     for (ll i = (0); i < (nr + ng + nb); i++) { n       if (lp[i].second == 0) { n         pr.push_back(i); n       } else if (lp[i].second == 1) { n         pg.push_back(i); n       } else { n         pb.push_back(i); n       } n     } n     for (ll i = (1); i < (nr + ng + nb - 1); i++) { n       if (lp[i].second == 0) { n         ll val_g = -1, val_b = -1; n         ll pos = lower_bound(pg.begin(), pg.end(), i) - pg.begin(); n         if (pos != 0) { n           val_g = g[pos - 1]; n         } n         pos = lower_bound(pb.begin(), pb.end(), i) - pb.begin(); n         if (pos != nb) { n           val_b = b[pos]; n         } n         if (val_g != -1 && val_b != -1) { n           ll val_r = lp[i].first; n           ll res = (val_r - val_g) * (val_r - val_g) + n                    (val_r - val_b) * (val_r - val_b) + n                    (val_b - val_g) * (val_b - val_g); n           ans = min(ans, res); n         } n         val_b = -1, val_g = -1; n         pos = lower_bound(pb.begin(), pb.end(), i) - pb.begin(); n         if (pos != 0) { n           val_b = b[pos - 1]; n         } n         pos = lower_bound(pg.begin(), pg.end(), i) - pg.begin(); n         if (pos != ng) { n           val_g = g[pos]; n         } n         if (val_g != -1 && val_b != -1) { n           ll val_r = lp[i].first; n           ll res = (val_r - val_g) * (val_r - val_g) + n                    (val_r - val_b) * (val_r - val_b) + n                    (val_b - val_g) * (val_b - val_g); n           ans = min(ans, res); n         } n       } else if (lp[i].second == 1) { n         ll val_r = -1, val_b = -1; n         ll pos = lower_bound(pr.begin(), pr.end(), i) - pr.begin(); n         if (pos != 0) { n           val_r = r[pos - 1]; n         } n         pos = lower_bound(pb.begin(), pb.end(), i) - pb.begin(); n         if (pos != nb) { n           val_b = b[pos]; n         } n         if (val_r != -1 && val_b != -1) { n           ll val_g = lp[i].first; n           ll res = (val_r - val_g) * (val_r - val_g) + n                    (val_r - val_b) * (val_r - val_b) + n                    (val_b - val_g) * (val_b - val_g); n           ans = min(ans, res); n         } n         val_r = -1, val_b = -1; n         pos = lower_bound(pb.begin(), pb.end(), i) - pb.begin(); n         if (pos != 0) { n           val_b = b[pos - 1]; n         } n         pos = lower_bound(pr.begin(), pr.end(), i) - pr.begin(); n         if (pos != nr) { n           val_r = r[pos]; n         } n         if (val_r != -1 && val_b != -1) { n           ll val_g = lp[i].first; n           ll res = (val_r - val_g) * (val_r - val_g) + n                    (val_r - val_b) * (val_r - val_b) + n                    (val_b - val_g) * (val_b - val_g); n           ans = min(ans, res); n         } n       } else { n         ll val_r = -1, val_g = -1; n         ll pos = lower_bound(pr.begin(), pr.end(), i) - pr.begin(); n         if (pos != 0) { n           val_r = r[pos - 1]; n         } n         pos = lower_bound(pg.begin(), pg.end(), i) - pg.begin(); n         if (pos != ng) { n           val_g = g[pos]; n         } n         if (val_r != -1 && val_g != -1) { n           ll val_b = lp[i].first; n           ll res = (val_r - val_g) * (val_r - val_g) + n                    (val_r - val_b) * (val_r - val_b) + n                    (val_b - val_g) * (val_b - val_g); n           ans = min(ans, res); n         } n         val_g = -1, val_r = -1; n         pos = lower_bound(pg.begin(), pg.end(), i) - pg.begin(); n         if (pos != 0) { n           val_g = g[pos - 1]; n         } n         pos = lower_bound(pr.begin(), pr.end(), i) - pr.begin(); n         if (pos != nr) { n           val_r = r[pos]; n         } n         if (val_r != -1 && val_g != -1) { n           ll val_b = lp[i].first; n           ll res = (val_r - val_g) * (val_r - val_g) + n                    (val_r - val_b) * (val_r - val_b) + n                    (val_b - val_g) * (val_b - val_g); n           ans = min(ans, res); n         } n       } n     } n     print(ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; n const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; n const int INF = 1000000000; n const long long LINF = 1000000000000000000; n const long long MOD = 1000000007; n const double PI = acos(-1.0); n const double EPS = 1e-10; n template <class T> n inline bool chmax(T& a, T b) { n   if (a < b) { n     a = b; n     return true; n   } n   return false; n } n template <class T> n inline bool chmin(T& a, T b) { n   if (a > b) { n     a = b; n     return true; n   } n   return false; n } n template <class T> n inline void add(T& a, T b) { n   a = ((a + b) % MOD + MOD) % MOD; n }; n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   long long n, z; n   cin >> n >> z; n   vector<long long> a(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a.begin(), a.end()); n   int ans = 0; n   int i = 0, j = n / 2; n   while (i < n / 2 && j < n) { n     if (a[j] - a[i] >= z) { n       ans++; n       i++; n       j++; n     } else n       j++; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline int setBit(int N, int pos) { return N = N | (1 << pos); } n inline int resetBit(int N, int pos) { return N = N & ~(1 << pos); } n inline bool checkBit(int N, int pos) { return (bool)(N & (1 << pos)); } n int main() { n   long long n, k, x, sum = 0; n   scanf( %lld %lld , &n, &k); n   while (n--) { n     scanf( %lld , &x); n     sum += (x / k) + (bool)(x % k); n   } n   printf( %lld n , sum / 2 + (bool)(sum % 2)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n long long power(long long a, long long b, long long m = mod) { n   long long x = 1; n   while (b) { n     if (b & 1) { n       x = 1ll * x * a % m; n     } n     a = 1ll * a * a % m; n     b /= 2; n   } n   return x; n } n const int N = 5e5 + 9; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int tt; n   cin >> tt; n   while (tt--) { n     int n; n     cin >> n; n     vector<vector<int>> g(n + 1); n     map<pair<int, int>, vector<int>> gg; n     vector<vector<int>> a(n + 1, vector<int>(3)); n     map<pair<int, int>, int> ed1; n     for (int i = 1; i <= n - 2; i++) { n       for (int j = 0; j < 3; j++) { n         cin >> a[i][j]; n       } n       for (int j = 0; j < 3; j++) { n         int u = a[i][j]; n         int v = a[i][(j + 1) % 3]; n         if (u > v) swap(u, v); n         g[u].push_back(v); n         g[v].push_back(u); n         gg[{u, v}].push_back(i); n         ed1[{u, v}]++; n       } n     } n     vector<vector<int>> gp(n + 1); n     for (auto it : ed1) { n       if (it.second == 1) { n         int u = it.first.first, v = it.first.second; n         gp[u].push_back(v); n         gp[v].push_back(u); n       } n     } n     vector<int> poly; n     vector<int> vis(n + 1); n     map<pair<int, int>, int> ed; n     vis[1] = 1; n     poly.push_back(1); n     for (int i = 1;;) { n       int pi = i; n       for (int v : gp[i]) { n         if (vis[v] == 0) { n           vis[v] = 1; n           i = v; n           ed[{min(pi, i), max(pi, i)}]++; n           break; n         } n       } n       if (i == pi) { n         ed[{1, pi}]++; n         break; n       } n       poly.push_back(i); n     } n     vector<int> cnt(n + 2); n     for (int i : poly) cout << i <<    ; n     cout <<   n ; n     if (n == 3) { n       cout <<  1 n ; n       continue; n     } n     function<pair<int, int>(int)> get_corner = [&](int i) { n       int cc = 0, tu; n       for (int j = 0; j < 3; j++) cnt[a[i][j]] = 0; n       for (int j = 0; j < 3; j++) { n         int u = a[i][j]; n         int v = a[i][(j + 1) % 3]; n         if (u > v) swap(u, v); n         if (ed.count({u, v})) { n           cc++; n           cnt[u]++; n           cnt[v]++; n           if (cnt[u] > cnt[v]) n             tu = u; n           else n             tu = v; n         } n       } n       return make_pair(tu, cc); n     }; n     int cur = 1; n     set<pair<int, int>> ss; n     for (int i = 1; i <= n - 2; i++) { n       auto r = get_corner(i); n       if (r.second == 2) { n         cur = i; n         ss.insert({-r.second, i}); n       } n     } n     while ((int)ss.size() > 1) { n       auto it = *ss.begin(); n       ss.erase(ss.begin()); n       cur = it.second; n       auto r = get_corner(cur); n       cout << cur <<    ; n       int x = -1, y = -1; n       for (int j = 0; j < 3; j++) { n         int v = a[cur][j]; n         if (v != r.first) { n           if (x == -1) n             x = v; n           else n             y = v; n         } n       } n       if (x > y) swap(x, y); n       int ind = 0; n       auto tmp = gg[{x, y}]; n       if (tmp.empty()) { n         return 0; n       } n       if (tmp[ind] == cur) ind++; n       ed[{x, y}]++; n       cur = tmp[ind]; n       r = get_corner(cur); n       ss.erase({-r.second + 1, cur}); n       ss.insert({-r.second, cur}); n     } n     cout << ss.begin()->second <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void in() {} n int main() { n   in(); n   int n; n   cin >> n; n   int arr[n + 10], ps[n + 10]; n   for (int i = 0; i < n; ++i) { n     scanf( %d , arr + i); n     ps[arr[i]] = i + 1; n   } n   ps[0] = ps[n + 1] = -1; n   int a = 0, pre = 1; n   for (int i = 1; i <= n + 1; ++i) { n     if (ps[i - 1] < ps[i]) continue; n     a = max(a, i - pre); n     pre = i; n   } n   cout << (n - a) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cout.tie(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     string a, b; n     cin >> a >> b; n     vector<int> ans; n     int l = 0, r = n - 1, x = 0; n     for (int i = n - 1; i >= 0; --i) { n       int cur = (a[r] -  0 ) ^ x; n       if (cur == (b[i] -  0 )) { n         if (l < r) n           --r; n         else n           ++r; n         continue; n       } n       cur = (a[l] -  0 ) ^ x; n       if (cur == (b[i] -  0 )) { n         ans.push_back(1); n       } n       ans.push_back(i + 1); n       x ^= 1; n       swap(l, r); n       if (l < r) n         --r; n       else n         ++r; n     } n     cout << ans.size() <<    ; n     for (auto i : ans) cout << i <<    ; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:102400000,102400000 ) n using namespace std; n const int N = 3e5 + 100; n const double PI = acos(-1.0); n void fre() { n   freopen( /Users/luras/Desktop/in.txt ,  r , stdin); n   freopen( /Users/luras/Desktop/out.txt ,  w , stdout); n } n int n, m, i, j, ans, p; n char s[N], ch; n int main() { n   while (~scanf( %d%d , &n, &m)) { n     scanf( %s , s + 1); n     ans = 0; n     for (i = 2; i <= n; i++) { n       if (s[i] ==  .  && s[i - 1] ==  . ) ans++; n     } n     for (i = 1; i <= m; i++) { n       scanf( %d %c , &p, &ch); n       if ((s[p] ==  .  && ch ==  . ) || (s[p] !=  .  && ch !=  . )) { n         printf( %d n , ans); n         continue; n       } n       if (s[p] ==  . ) { n         if (p == 1) { n           if (s[p + 1] ==  . ) { n             ans--; n           } n         } else { n           if (s[p + 1] ==  . ) ans--; n           if (s[p - 1] ==  . ) ans--; n         } n       } else { n         if (p == 1) { n           if (s[p + 1] ==  . ) ans++; n         } else { n           if (s[p + 1] ==  . ) ans++; n           if (s[p - 1] ==  . ) ans++; n         } n       } n       s[p] = ch; n       printf( %d n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int N = 3 * 100002; n int a[N], pr[N]; n ll cnt[N]; n map<int, int> s[N]; n int main() { n   int q; n   cin >> q; n   while (q--) { n     ll ans = 0; n     int n; n     cin >> n; n     for (int i = 0; i <= n; i++) { n       pr[i] = -1; n       s[i].clear(); n     } n     for (int i = 1; i <= n; i++) { n       cnt[i] = 0; n       cin >> a[i]; n       if (i != 1) { n         if (pr[i - 1] != -1) { n           swap(s[i], s[pr[i - 1]]); n           s[i][a[pr[i - 1] - 1]] = pr[i - 1] - 1; n         } n         if (s[i][a[i]]) n           pr[i] = s[i][a[i]]; n         else n           pr[i] = -1; n         if (a[i] == a[i - 1]) pr[i] = i - 1; n       } n       if (pr[i] != -1) cnt[i] = cnt[pr[i] - 1] + 1; n       ans += cnt[i]; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using   namespace  std ; n using LL  =  long long ; n  n short int mark[500005] ; n  n class cmp{ n public:  n     bool operator () (const pair<int, int> &p1, const pair<int, int> &p2){ n         return ((p1.first < p2.first) || (p1.first == p2.first && p1.second > p2.second)) ; n     } n } ; n  n int main(){ n  n     ios_base::sync_with_stdio(false); n     cin.tie(0); n      n     int q ; n     cin >>  q ; n  n     int id = 0 ; n     queue<int> a ; n     priority_queue<pair<int, int> , vector<pair<int, int> > , cmp > b ; n  n     for(int i = 1 ; i <= q ; i++){ n         int x ; n         cin >> x ; n  n         if(x == 1){ n             int m ; n             cin >> m ; n             a.push(++id) ; n             b.push(make_pair(m, id)) ; n         } n         else if(x == 2){ n             while(mark[a.front()]) n                 a.pop() ; n             cout << a.front() <<     ; n             mark[a.front()] = 1 ; n             a.pop() ; n         } n         else if(x == 3) { n             while(mark[b.top().second]) n                 b.pop() ; n             cout << b.top().second <<     ; n             mark[b.top().second] = 1 ; n             b.pop() ; n         } n     } n     cout << endl ; n  n     return 0; 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; n   int k; n   cin >> n >> k; n   int ar[n]; n   for (int i = 0; i < n; i++) { n     cin >> ar[i]; n   } n   sort(ar, ar + n); n   long long lista[n]; n   int cantidad[n]; n   int index = 0; n   for (int i = 0; i < n; i++) { n     if (i == 0) { n       lista[i] = ar[i]; n       cantidad[index] = 1; n     } else if (ar[i] == ar[i - 1]) { n       cantidad[index]++; n     } else { n       index++; n       cantidad[index] = 1; n       lista[index] = ar[i]; n     } n   } n   int diferentes = index + 1; n   long long L[diferentes]; n   long long R[diferentes]; n   int LeftAmount[diferentes]; n   int RightAmount[diferentes]; n   L[0] = 0; n   LeftAmount[0] = 0; n   R[diferentes - 1] = 0; n   RightAmount[diferentes - 1] = 0; n   for (int i = 1; i < diferentes; i++) { n     LeftAmount[i] = LeftAmount[i - 1] + cantidad[i - 1]; n     L[i] = L[i - 1] + (lista[i] - 1 - lista[i - 1]) * cantidad[i - 1]; n     L[i] += (lista[i] - 1 - (lista[i - 1] - 1)) * LeftAmount[i - 1]; n   } n   for (int i = diferentes - 2; i >= 0; i--) { n     RightAmount[i] = RightAmount[i + 1] + cantidad[i + 1]; n     R[i] = R[i + 1] + (lista[i + 1] - (lista[i] + 1)) * cantidad[i + 1]; n     R[i] += (lista[i + 1] - lista[i]) * RightAmount[i + 1]; n   } n   long long ans = 1000000000000000005; n   for (int i = 0; i < diferentes; i++) { n     if (cantidad[i] >= k) { n       ans = 0; n     } else { n       int needed = k - cantidad[i]; n       if ((LeftAmount[i] >= needed) && (RightAmount[i] >= needed)) { n         ans = min(min(L[i], R[i]) + needed, ans); n       } else if (LeftAmount[i] >= needed) { n         ans = min(L[i] + needed, ans); n       } else if (RightAmount[i] >= needed) { n         ans = min(R[i] + needed, ans); n       } else { n         ans = min(L[i] + R[i] + needed, ans); n       } n     } n   } n   cout << ans; 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(NULL); n   long long t; n   cin >> t; n   while (t--) { n     long long n, k; n     cin >> n >> k; n     vector<vector<long long>> a(n, vector<long long>(3)); n     vector<long long> b(k); n     vector<int> make_pair(n + 1); n     for (int i = 0; i < n; ++i) { n       cin >> a[i][0]; n       make_pair[a[i][0]] = i; n       a[i][1] = i - 1; n       a[i][2] = i < n - 1 ? i + 1 : -1; n     } n     vector<int> st(n + 1, false); n     for (auto &i : b) { n       cin >> i; n       st[i] = true; n     } n     long long ans = 1; n     for (long long i = 0; i < k; ++i) { n       int pos = make_pair[b[i]]; n       int cnt = 0; n       if (a[pos][1] != -1) { n         int nbr = a[pos][1]; n         if (!st[a[nbr][0]]) cnt++; n       } n       if (a[pos][2] != -1) { n         int nbr = a[pos][2]; n         if (!st[a[nbr][0]]) cnt++; n       } n       st[b[i]] = false; n       int l = a[pos][1]; n       int r = a[pos][2]; n       if (l != -1) a[l][2] = r; n       if (r != -1) a[r][1] = l; n       if (!cnt) { n         ans = 0; n         break; n       } n       ans = (ans * cnt) % ((long long)(998244353)); n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void bfs(long long root, list<long long> adj[], vector<long long> &level) { n   level[root] = 0; n   queue<long long> qu; n   qu.push(root); n   while (!qu.empty()) { n     long long cur = qu.front(); n     qu.pop(); n     for (auto it : adj[cur]) { n       if (level[it] == -1) { n         level[it] = level[cur] + 1; n         qu.push(it); n       } n     } n   } n } n void solve() { n   long long V, E, a, b; n   cin >> V >> E; n   list<long long> adj[V + 1]; n   vector<long long> level(V + 1, -1); n   for (long long i = 0; i < E; i++) { n     cin >> a >> b; n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   bfs(1, adj, level); n   map<long long, vector<long long> > mp; n   for (int i = 1; i <= V; i++) { n     mp[level[i]].push_back(i); n   } n   a = mp.size(); n   vector<long long> ans1, ans2; n   for (int i = 0; i < a; i += 2) { n     vector<long long> hold = mp[i]; n     for (auto it : hold) ans1.push_back(it); n   } n   for (int i = 1; i < a; i += 2) { n     vector<long long> hold = mp[i]; n     for (auto it : hold) ans2.push_back(it); n   } n   if (ans1.size() < ans2.size()) { n     cout << ans1.size() << endl; n     for (auto it : ans1) cout << it <<    ; n     cout << endl; n   } else { n     cout << ans2.size() << endl; n     for (auto it : ans2) cout << it <<    ; n     cout << endl; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long q = 1; n   cin >> q; n   while (q--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fast() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n int main() { n   int n, c = 0; n   char x; n   vector<pair<int, int> > v; n   cin >> n; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       cin >> x; n       if (x ==  C ) { n         v.push_back(make_pair(i, j)); n       } n     } n   } n   sort(v.begin(), v.end()); n   for (int i = 0; i < v.size(); i++) { n     for (int j = i + 1; j < v.size(); j++) { n       if (v[i].first == v[j].first) c++; n       if (v[i].second == v[j].second) c++; n     } n   } n   cout << c; n   return 0; n } n


#include <bits/stdc++.h> n const int kM = 4e4 + 5; n const int kN = 55; n int M, n, m, k; n long long T[kM << 3], tag[kM << 3], a[kN][kM], pre[kM], s[kM], f[kN][kM]; n void Update(int cur) { T[cur] = std::max(T[cur << 1], T[cur << 1 | 1]); } n void PushTag(int cur) { n   T[cur << 1] += tag[cur]; n   T[cur << 1 | 1] += tag[cur]; n   tag[cur << 1] += tag[cur]; n   tag[cur << 1 | 1] += tag[cur]; n   tag[cur] = 0; n } n void Build(int cur, int l, int r) { n   if (l < r) { n     int mid = (l + r) >> 1; n     Build(cur << 1, l, mid); n     Build(cur << 1 | 1, mid + 1, r); n     Update(cur); n   } else n     T[cur] = s[l]; n } n void Modify(int cur, int l, int r, int ql, int qr, long long v) { n   if (l > qr || r < ql) return; n   if (ql <= l && r <= qr) n     T[cur] += v, tag[cur] += v; n   else { n     int mid = (l + r) >> 1; n     PushTag(cur); n     if (ql <= mid) Modify(cur << 1, l, mid, ql, qr, v); n     if (qr > mid) Modify(cur << 1 | 1, mid + 1, r, ql, qr, v); n     Update(cur); n   } n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) scanf( %d , &a[i][j]); n   for (int i = 1; i <= m + k; ++i) pre[i] = pre[i - 1] + a[1][i]; n   for (int i = 1; i <= m; ++i) f[1][i] = pre[i + k - 1] - pre[i - 1]; n   for (int i = 2; i <= n; ++i) { n     for (int j = 1; j <= m + k; ++j) pre[j] = pre[j - 1] + a[i][j]; n     for (int j = 1; j <= m; ++j) n       s[j] = pre[j + k - 1] - pre[std::max(k, j - 1)] + f[i - 1][j]; n     memset(tag, 0, sizeof(tag)); n     Build(1, 1, m); n     for (int j = 1; j <= m; ++j) { n       f[i][j] = T[1] + pre[j + k - 1] - pre[j - 1]; n       Modify(1, 1, m, j - k + 1, j, a[i][j]); n       Modify(1, 1, m, j + 1, j + k, -a[i][j + k]); n     } n   } n   long long ans = 0; n   for (int i = 1; i <= m; ++i) ans = std::max(ans, f[n][i]); n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7, mxn = 2e5 + 5; n int n; n int a[200005]; n int mp1[200005], mp2[200005]; n map<int, int> mp; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     mp[a[i]]++; n   } n   int ele, ma = 0; n   for (auto ii : mp) { n     if (ma < ii.second) { n       ma = ii.second; n       ele = ii.first; n     } n   } n   int an = 0; n   for (int i = 1; i <= 100; i++) { n     if (ele == i) continue; n     memset(mp1, -1, sizeof(mp1)); n     memset(mp2, -1, sizeof(mp2)); n     int su = 0; n     mp1[0] = 0; n     for (int j = 0; j < n; j++) { n       if (a[j] == ele) n         su++; n       else if (a[j] == i) n         su--; n       if (su >= 0) { n         if (mp1[su] != -1) { n           int idx = mp1[su]; n           int tm = j + 1 - idx; n           an = max(tm, an); n         } else { n           mp1[su] = j + 1; n         } n       } else { n         if (mp2[-1 * su] != -1) { n           int idx = mp2[-1 * su]; n           int tm = j + 1 - idx; n           an = max(tm, an); n         } else { n           mp2[-1 * su] = j + 1; n         } n       } n     } n   } n   cout << an; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   char c = getchar(); n   while (!isdigit(c)) c = getchar(); n   int num = 0; n   while (isdigit(c)) num = num * 10 + c -  0 , c = getchar(); n   return num; n } n void write(int num) { n   if (num >= 10) write(num / 10); n   putchar(num % 10 +  0 ); n } n int u[50001], v[50001], c[50001], t[50001]; n vector<int> e[50001]; n bool cmp(int a, int b) { return c[a] < c[b]; } n int head[500001], ver[2000001], nxt[2000001], sz; n void addedge(int u, int v) { ver[++sz] = v, nxt[sz] = head[u], head[u] = sz; } n int dfn[500001], low[500001]; n int vis[500001], stk[500001]; n int now, top, cnt, scc[500001]; n void tarjan(int x) { n   dfn[x] = low[x] = ++now; n   stk[++top] = x, vis[x] = 1; n   for (int i = head[x]; i; i = nxt[i]) n     if (!vis[ver[i]]) n       tarjan(ver[i]), low[x] = min(low[x], low[ver[i]]); n     else if (vis[ver[i]] == 1) n       low[x] = min(low[x], dfn[ver[i]]); n   if (dfn[x] == low[x]) { n     scc[x] = ++cnt, vis[x] = 2; n     while (stk[top] != x) { n       scc[stk[top]] = cnt; n       vis[stk[top]] = 2; n       --top; n     } n     --top; n   } n } n bool _2sat(int n) { n   now = cnt = 0; n   for (int i = 1; i <= n * 2; i++) vis[i] = 0; n   for (int i = 1; i <= n * 2; i++) n     if (!vis[i]) tarjan(i); n   for (int i = 1; i <= n; i++) n     if (scc[i] == scc[i + n]) return 0; n   return 1; n } n int main() { n   int n, m; n   n = read(), m = read(); n   for (int i = 1; i <= m; i++) { n     u[i] = read(), v[i] = read(), c[i] = read(), t[i] = read(); n     e[u[i]].push_back(i), e[v[i]].push_back(i); n   } n   int tot = m; n   for (int i = 1; i <= n; i++) { n     int tmp = 0; n     sort(e[i].begin(), e[i].end(), cmp); n     for (int j = 1; j < e[i].size(); j++) n       if (c[e[i][j]] == c[e[i][j - 1]]) { n         if (tmp) { n           puts( No ); n           return 0; n         } n         tmp = j; n       } n     if (tmp) { n       addedge(e[i][tmp], m * 5 + e[i][tmp - 1]); n       addedge(e[i][tmp - 1], m * 5 + e[i][tmp]); n       addedge(m * 5 + e[i][tmp - 1], e[i][tmp]); n       addedge(m * 5 + e[i][tmp], e[i][tmp - 1]); n       for (int j = 0; j < e[i].size(); j++) n         if (j != tmp && j != tmp - 1) addedge(e[i][j], m * 5 + e[i][j]); n     } else { n       for (int j = 1; j < e[i].size(); j++) { n         addedge(j + tot, j + 1 + tot); n         addedge(j + 1 + tot + e[i].size(), j + tot + e[i].size()); n         addedge(j + 1 + tot + m * 5, j + tot + m * 5); n         addedge(j + tot + e[i].size() + m * 5, n                 j + 1 + tot + e[i].size() + m * 5); n       } n       for (int j = 0; j < e[i].size(); j++) { n         addedge(e[i][j], j + 1 + tot); n         addedge(e[i][j], j + 1 + tot + e[i].size()); n         addedge(j + 1 + tot + m * 5, e[i][j] + m * 5); n         addedge(j + 1 + tot + e[i].size() + m * 5, e[i][j] + m * 5); n       } n       for (int j = 1; j < e[i].size(); j++) { n         addedge(e[i][j], j + tot + m * 5); n         addedge(e[i][j - 1], j + 1 + tot + e[i].size() + m * 5); n         addedge(j + tot, e[i][j] + m * 5); n         addedge(j + 1 + tot + e[i].size(), e[i][j - 1] + m * 5); n       } n     } n     tot += e[i].size() * 2; n   } n   int tmp = sz; n   int l = 0, r = 1000000001; n   while (l < r) { n     int mid = (l + r) / 2; n     for (int i = 1; i <= m * 10; i++) n       while (head[i] > tmp) head[i] = nxt[head[i]]; n     sz = tmp; n     for (int i = 1; i <= m; i++) n       if (t[i] > mid) addedge(i, i + m * 5); n     if (_2sat(m * 5)) n       r = mid; n     else n       l = mid + 1; n   } n   if (l == 1000000001) { n     puts( No ); n     return 0; n   } n   puts( Yes ); n   write(l), putchar(   ); n   for (int i = 1; i <= m * 10; i++) n     while (head[i] > tmp) head[i] = nxt[head[i]]; n   sz = tmp; n   for (int i = 1; i <= m; i++) n     if (t[i] > l) addedge(i, i + m * 5); n   _2sat(m * 5); n   int res = 0; n   for (int i = 1; i <= m; i++) n     if (scc[i] < scc[i + m * 5]) ++res; n   write(res), putchar(  n ); n   for (int i = 1; i <= m; i++) n     if (scc[i] < scc[i + m * 5]) write(i), putchar(   ); n   putchar(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, dist[110][110]; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) scanf( %d , &dist[i][j]); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       for (int k = 1; k <= n; k++) n         dist[j][k] = dist[j][k] > dist[j][i] + dist[i][k] n                          ? dist[j][i] + dist[i][k] n                          : dist[j][k]; n   int max = 0; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) max = max > dist[i][j] ? max : dist[i][j]; n   cout << max << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long n; n   cin >> n; n   cout << 2 * n - 1 <<     << 2 <<   n ; n   cout << 1 <<     << 2 <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, i, j, x, y, z, ans2, ans; n int ans1[101]; n int main() { n   cin >> n >> m; n   for (i = 1; i <= m; i++) { n     y = -1; n     for (j = 1; j <= n; j++) { n       cin >> x; n       if (x > y) { n         y = x; n         z = j; n       } n     } n     ans1[z]++; n   } n   for (i = 1; i <= 101; i++) { n     if (ans1[i] > ans2) { n       ans2 = ans1[i]; n       ans = i; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   int u, v, w, id; n } e[500001]; n int f[500001], last[500001]; n int Left[500001]; n struct que { n   int x, y, next; n } q[1000001]; n int head[500001]; n int cnt; n void init() { n   cnt = 0; n   memset(head, -1, sizeof(head)); n } n void add_edge(int w, int i, int id) { n   q[cnt].x = i; n   q[cnt].y = id; n   q[cnt].next = head[w]; n   head[w] = cnt++; n } n int fa[500001]; n int stid[500001 << 3], stfa[500001 << 3], top = 0; n bool cmp(edge a, edge b) { n   if (a.w == b.w) return a.id < b.id; n   return a.w < b.w; n } n int Find(int u) { n   if (u == fa[u]) { n     return u; n   } else { n     int v = Find(fa[u]); n     ++top; n     stid[top] = u, stfa[top] = fa[u]; n     fa[u] = v; n     return v; n   } n } n bool add(int id) { n   int u = e[id].u, v = e[id].v; n   u = Find(u), v = Find(v); n   if (u == v) return false; n   ++top; n   stid[top] = u, stfa[top] = fa[u]; n   fa[u] = v; n   return true; n } n void back(int ret) { n   while (top > ret) { n     fa[stid[top]] = stfa[top]; n     top--; n   } n } n int main() { n   init(); n   int n, m, Q; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d%d , &e[i].u, &e[i].v, &e[i].w); n     e[i].id = i; n   } n   sort(e + 1, e + 1 + m, cmp); n   for (int i = 1; i <= m; i++) f[e[i].id] = i; n   scanf( %d , &Q); n   for (int i = 1; i <= Q; i++) { n     int qn; n     scanf( %d , &qn); n     for (int j = 1; j <= qn; j++) { n       int id; n       scanf( %d , &id); n       id = f[id]; n       if (last[e[id].w] != i) { n         last[e[id].w] = i; n         Left[i]++; n       } n       add_edge(e[id].w, i, id); n     } n   } n   for (int i = 1; i <= n; i++) fa[i] = i; n   for (int i = 1; i <= m; i++) { n     if (e[i].w != e[i - 1].w) { n       int w = e[i].w; n       int rec = top; n       for (int j = head[w]; ~j; j = q[j].next) { n         bool ok = true; n         while (q[j].next != -1 && q[j].x == q[q[j].next].x) { n           ok &= add(q[j].y); n           j = q[j].next; n         } n         ok &= add(q[j].y); n         if (ok) { n           Left[q[j].x]--; n         } n         back(rec); n       } n     } n     for (; e[i].w == e[i + 1].w; i++) { n       add(i); n     } n     add(i); n   } n   for (int i = 1; i <= Q; i++) { n     if (!Left[i]) n       printf( YES n ); n     else n       printf( NO n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 55; n int read() { n   int w = 0, f = 0; n   char c = getchar(); n   while ((c <  0  || c >  9 ) && c !=  - ) c = getchar(); n   if (c ==  - ) f = 1, c = getchar(); n   while (c >=  0  && c <=  9 ) w = w * 10 + c -  0 , c = getchar(); n   return f ? -w : w; n } n int n; n int a[N][N], b[N][N]; n int ask(int x1, int y1, int x2, int y2) { n   printf( ? %d %d %d %d n , x1, y1, x2, y2); n   fflush(stdout); n   int tmp; n   scanf( %d , &tmp); n   return tmp; n } n void Ans(int res) { n   puts( ! ); n   if (res == 1) { n     for (int i = 1; i <= n; ++i) n       for (int j = 1; j <= n; ++j) { n         printf( %d , a[i][j]); n         if (j == n) puts(  ); n       } n   } else { n     for (int i = 1; i <= n; ++i) { n       for (int j = 1; j <= n; ++j) { n         printf( %d , b[i][j]); n         if (j == n) puts(  ); n       } n     } n   } n } n bool check(vector<int>& v) { n   vector<int> tmp = v; n   reverse(tmp.begin(), tmp.end()); n   for (size_t i = 0; i < v.size(); ++i) n     if (v[i] != tmp[i]) return 0; n   return 1; n } n int tx = -1, ty, sx, sy; n bool r1, r2; n vector<int> p1, p2; n void work(int t, int x, int y) { n   if (!t) { n     bool b1 = check(p1), b2 = check(p2); n     if (b1 != b2) { n       tx = x, ty = y; n       r1 = b1, r2 = b2; n     } n     return; n   } n   if (tx != -1) return; n   if (x + 1 <= n) { n     p1.push_back(a[x + 1][y]); n     p2.push_back(b[x + 1][y]); n     work(t - 1, x + 1, y); n     p1.pop_back(); n     p2.pop_back(); n     if (tx != -1) return; n   } n   if (y + 1 <= n) { n     p1.push_back(a[x][y + 1]); n     p2.push_back(b[x][y + 1]); n     work(t - 1, x, y + 1); n     p1.pop_back(); n     p2.pop_back(); n   } n } n int main() { n   memset(a, -1, sizeof a); n   memset(b, -1, sizeof b); n   n = read(); n   for (int i = 1; i <= n; i += 2) { n     for (int j = 1; j <= n; j += 2) { n       if (i == 1 && j == 1) n         a[i][j] = 1; n       else { n         if (j > 2) n           a[i][j] = ask(i, j - 2, i, j) ^ a[i][j - 2] ^ 1; n         else n           a[i][j] = ask(i - 2, j, i, j) ^ a[i - 2][j] ^ 1; n       } n       b[i][j] = a[i][j]; n     } n   } n   for (int i = 2; i <= n; i += 2) { n     for (int j = 2; j <= n; j += 2) { n       if (i == 2 && j == 2) n         a[2][2] = ask(1, 1, 2, 2); n       else { n         if (j > 2) n           a[i][j] = ask(i, j - 2, i, j) ^ a[i][j - 2] ^ 1; n         else n           a[i][j] = ask(i - 2, j, i, j) ^ a[i - 2][j] ^ 1; n       } n       b[i][j] = a[i][j]; n     } n   } n   for (int i = 1; i <= n; i += 2) { n     for (int j = 2; j <= n; j += 2) { n       if (i == 1 && j == 2) n         a[1][2] = 0; n       else { n         if (j > 2) n           a[i][j] = ask(i, j - 2, i, j) ^ a[i][j - 2] ^ 1; n         else n           a[i][j] = ask(i - 2, j, i, j) ^ a[i - 2][j] ^ 1; n       } n       b[i][j] = a[i][j] ^ 1; n     } n   } n   for (int i = 2; i <= n; i += 2) { n     for (int j = 3; j <= n; j += 2) { n       if (i == 2 && j == 3) n         a[2][3] = ask(1, 2, 2, 3) ^ a[1][2] ^ 1; n       else { n         if (j > 3) n           a[i][j] = ask(i, j - 2, i, j) ^ a[i][j - 2] ^ 1; n         else n           a[i][j] = ask(i - 2, j, i, j) ^ a[i - 2][j] ^ 1; n       } n       b[i][j] = a[i][j] ^ 1; n     } n   } n   for (int i = 2; i <= n; i += 2) { n     a[i][1] = ask(i, 1, i, 3) ^ a[i][3] ^ 1; n     b[i][1] = a[i][1] ^ 1; n   } n   for (int t = 3; tx == -1; ++t) { n     for (int i = 1; i <= n; ++i) { n       for (int j = 1; j <= n; ++j) { n         p1.clear(), p2.clear(); n         p1.push_back(a[i][j]), p2.push_back(b[i][j]); n         work(t, i, j); n         if (tx != -1) { n           sx = i, sy = j; n           break; n         } n       } n       if (tx != -1) break; n     } n   } n   int tmp = ask(sx, sy, tx, ty); n   Ans(tmp == r1); n   return 0; n } n


#include <bits/stdc++.h> n inline int read() { n   int x; n   char c; n   while ((c = getchar()) <  0  || c >  9 ) n     ; n   for (x = c -  0 ; (c = getchar()) >=  0  && c <=  9 ;) x = x * 10 + c -  0 ; n   return x; n } n int a[100000 + 5]; n void solve(int n) { n   for (int i = 20, x; i; --i) n     if (n & (1 << i)) { n       x = ((1 << i + 1) - 1) ^ n; n       if (x > 1) solve(x - 1); n       while (n >= x) printf( %d  , n--); n       return; n     } n } n int main() { n   int n = read(), i; n   if (n & 1) n     puts( NO ); n   else n     puts( YES ), solve(n), puts(  ); n   if (n < 6 || n == (n & -n)) n     puts( NO ); n   else { n     puts( YES ); n     a[1] = 3; n     a[2] = 6; n     a[3] = 2; n     a[4] = 5; n     a[5] = 1; n     a[6] = 4; n     for (i = 7; i <= n; ++i) n       if (i == (i & -i)) n         a[i] = i + 1, a[i + 1] = i, ++i; n       else n         a[i] = a[i & -i], a[i & -i] = i; n     for (i = 1; i <= n; ++i) printf( %d  , a[i]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 5004; n const int MAXNN = MAXN * 2 + 10; n const int INF = 0x3f3f3f3f; n struct Edge { n   int u, v, f, cap; n   Edge() { f = 0; } n   Edge(int uu, int vv, int cc) : u(uu), v(vv), f(0), cap(cc) {} n   inline int other(int w) { return w == u ? v : u; } n   inline int flow(int from) { return from == u ? cap - f : f; } n   inline void augment(int from, int val) { n     if (from == u) n       f += val; n     else n       f -= val; n   } n }; n struct Dinic { n   int N; n   vector<int> par, L, pt, eIdx; n   vector<Edge> E; n   vector<vector<int>> a; n   Dinic(int n) : N(n), par(n), L(n), pt(n), eIdx(n), a(n) {} n   Dinic() : N(0) {} n   void addEdge(int u, int v, int c) { n     if (u != v) { n       E.emplace_back(u, v, c); n       a[u].push_back(E.size() - 1); n       a[v].push_back(E.size() - 1); n     } n   } n   void clearFlow() { n     for (Edge &e : E) e.f = 0; n   } n   bool BFS(int source, int target) { n     const int INF = 0x3f3f3f3f; n     queue<int> Q; n     fill(L.begin(), L.end(), INF); n     L[source] = 0; n     Q.push(source); n     while (Q.size()) { n       int u = Q.front(); n       Q.pop(); n       for (int ei : a[u]) { n         Edge &e = E[ei]; n         int v = e.other(u); n         if (L[v] > L[u] + 1 && e.flow(u)) { n           L[v] = L[u] + 1; n           if (v == target) return true; n           Q.push(v); n         } n       } n     } n     return false; n   } n   int DFS(int source, int target) { n     stack<int> S; n     int ans = 0; n     S.push(source); n     while (S.size()) { n       int u = S.top(); n       if (u == target) { n         int f = 1e9, bottleneck = -1; n         for (int cur = target; cur != source; cur = par[cur]) n           f = min(f, E[eIdx[cur]].flow(par[cur])); n         for (int cur = target; cur != source; cur = par[cur]) { n           Edge &e = E[eIdx[cur]]; n           e.augment(par[cur], f); n           if (!e.flow(par[cur])) bottleneck = par[cur]; n         } n         ans += f; n         while (S.top() != bottleneck) S.pop(); n       } else { n         if (pt[u]) { n           int &i = pt[u]; n           while (--i >= 0) { n             int ei = a[u][i]; n             Edge &e = E[ei]; n             int v = e.other(u); n             if (pt[v] > 0 && L[u] + 1 == L[v] && e.flow(u)) { n               par[v] = u; n               eIdx[v] = ei; n               S.push(v); n               break; n             } n           } n         } n         if (S.top() == u) { n           pt[u] = 0; n           S.pop(); n         } n       } n     } n     return ans; n   } n   int maxFlow(int source, int target) { n     int mf = 0, f; n     clearFlow(); n     while (BFS(source, target)) { n       for (int i = 0; i < N; ++i) pt[i] = (int)a[i].size(); n       while (pt[source]) { n         f = DFS(source, target); n         if (!f) break; n         mf += f; n       } n     } n     return mf; n   } n }; n bool vis[MAXNN]; n int n, m, S, T; n inline void init(Dinic &F, int val) { n   for (int ei : F.a[T]) { n     F.E[ei].cap = val; n   } n } n void bs(Dinic &F) { n   int hi = n, lo = 0, ans = 0, mid; n   while (hi >= lo) { n     mid = (hi + lo) >> 1; n     init(F, mid); n     if (F.maxFlow(S, T) == m) { n       ans = mid; n       hi = mid - 1; n     } else { n       lo = mid + 1; n     } n   } n   init(F, ans); n   F.maxFlow(S, T); n   printf( %d n , ans); n   for (int i = 1, from = -1, to = -1; i <= m; ++i) { n     for (int ei : F.a[i]) { n       Edge &e = F.E[ei]; n       if (e.other(i) == S) continue; n       if (e.flow(i)) n         to = e.other(i); n       else n         from = e.other(i); n     } n     printf( %d %d n , from - m, to - m); n   } n } n int main() { n   scanf( %d%d , &n, &m); n   S = 0; n   T = n + m + 1; n   Dinic F(T + 1); n   for (int i = 1, x, y; i <= m; ++i) { n     scanf( %d%d , &x, &y); n     F.addEdge(i, m + x, 1); n     F.addEdge(i, m + y, 1); n     F.addEdge(S, i, 1); n   } n   for (int i = 1; i <= n; ++i) F.addEdge(m + i, T, MAXN); n   bs(F); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double cp(const complex<double> &a, const complex<double> &b) { n   return a.real() * b.imag() - b.real() * a.imag(); n } n inline complex<double> line_inter(const complex<double> &a, n                                   const complex<double> &b, n                                   const complex<double> &c, n                                   const complex<double> &d) { n   return a + cp(c - a, d - c) / cp(b - a, d - c) * (b - a); n } n inline double lp_dist(const complex<double> &a, const complex<double> &b, n                       const complex<double> &p) { n   return cp(b - a, p - a) / abs(b - a); n } n int main() { n   int y1, y2, yw, xb, yb, r; n   cin >> y1 >> y2 >> yw >> xb >> yb >> r; n   yw -= r; n   y1 += r; n   y2 -= r; n   if (y1 == y2) n     cout << -1 << endl; n   else { n     double cc = yw + (yw - (y1 + 0.00001)); n     complex<double> a(0, cc), b(xb, yb), c(0, yw), d(xb, yw); n     complex<double> e(0, yw + yw - (y2 + r)); n     if (lp_dist(b, a, e) <= r) n       cout << -1 << endl; n     else { n       complex<double> s = line_inter(a, b, c, d); n       cout << fixed << setprecision(12) << s.real() << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline int read(T& x) { n   x = 0; n   char c = getchar(), f = 1; n   while (c >  9  || c <  0 ) { n     if (c ==  - ) f = -1; n     if (c == -1) return -1; n     c = getchar(); n   } n   while (c <=  9  && c >=  0 ) n     x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); n   x *= f; n   return 1; n } n template <typename T, typename... Args> n inline int read(T& x, Args&... args) { n   return read(x) + read(args...); n } n template <typename T> n inline int write(T x) { n   if (x < 0) return putchar( - ) & write((~x) + 1); n   if (x / 10) return write(x / 10) & putchar(x % 10 | 48); n   return putchar(x | 48); n } n template <typename T> n inline int write(T x, char c) { n   return write(x) + putchar(c); n } n int n, m; n struct edge { n   int u, v, nxt; n } e[1000005 << 1]; n int f[1000005], tot = 1; n inline void addEdge(int u, int v) { n   tot++, e[tot].u = u, e[tot].v = v, e[tot].nxt = f[u], f[u] = tot; n } n int dfn[1000005], low[1000005], idx; n int bel[1000005], siz[1000005], scc; n bool ins[1000005]; n int sta[1000005], tp; n inline void tarjan(int x, int fa) { n   dfn[x] = low[x] = ++idx; n   sta[++tp] = x; n   ins[x] = 1; n   for (int i = f[x]; i; i = e[i].nxt) { n     int v = e[i].v; n     if (!dfn[v]) { n       tarjan(v, x); n       low[x] = ((low[x]) < (low[v]) ? (low[x]) : (low[v])); n     } else if (ins[v]) n       low[x] = ((low[x]) < (dfn[v]) ? (low[x]) : (dfn[v])); n   } n   if (dfn[x] == low[x]) { n     int v; n     scc++; n     do { n       v = sta[tp--]; n       bel[v] = scc; n       siz[scc]++; n       ins[v] = 0; n     } while (v != x); n   } n } n signed main() { n   int t; n   read(t); n   while (t--) { n     read(n, m); n     tot = idx = scc = 0; n     for (int i = 1; i <= n; i++) bel[i] = dfn[i] = low[i] = f[i] = siz[i] = 0; n     for (int i = 1; i <= m; i++) { n       int u, v; n       read(u, v); n       if (u != v) addEdge(u, v); n     } n     for (int i = 1; i <= n; i++) n       if (!dfn[i]) tarjan(i, -1); n     if (scc == 1) { n       puts( No ); n       continue; n     } n     puts( Yes ); n     write(siz[1],    ), write(n - siz[1],   n ); n     for (int i = 1; i <= n; i++) n       if (bel[i] == 1) write(i,    ); n     putchar(10); n     for (int i = 1; i <= n; i++) n       if (bel[i] != 1) write(i,    ); n     putchar(10); n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool cmp(pair<int, int> a, pair<int, int> b) { n   return abs(a.first) + abs(a.second) < abs(b.first) + abs(b.second); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   vector<pair<int, int>> q1; n   vector<pair<int, int>> q2; n   vector<pair<int, int>> q3; n   vector<pair<int, int>> q4; n   for (int i = 0; i < n; i++) { n     int x, y; n     cin >> x >> y; n     if (x > 0 && y >= 0) { n       q1.push_back(make_pair(x, y)); n     } n     if (x <= 0 && y > 0) { n       q2.push_back(make_pair(x, y)); n     } n     if (x < 0 && y <= 0) { n       q3.push_back(make_pair(x, y)); n     } n     if (x >= 0 && y < 0) { n       q4.push_back(make_pair(x, y)); n     } n   } n   sort(q1.begin(), q1.end(), cmp); n   sort(q2.begin(), q2.end(), cmp); n   sort(q3.begin(), q3.end(), cmp); n   sort(q4.begin(), q4.end(), cmp); n   int ans = 0; n   for (int i = 0; i < q1.size(); i++) { n     ans += 4; n     if (q1[i].second != 0) { n       ans += 2; n     } n   } n   for (int i = 0; i < q2.size(); i++) { n     ans += 4; n     if (q2[i].first != 0) { n       ans += 2; n     } n   } n   for (int i = 0; i < q3.size(); i++) { n     ans += 4; n     if (q3[i].second != 0) { n       ans += 2; n     } n   } n   for (int i = 0; i < q4.size(); i++) { n     ans += 4; n     if (q4[i].first != 0) { n       ans += 2; n     } n   } n   cout << ans << endl; n   for (int i = 0; i < q1.size(); i++) { n     cout <<  1   << abs(q1[i].first) <<   R n ; n     if (q1[i].second != 0) { n       cout <<  1   << abs(q1[i].second) <<   U n ; n     } n     cout << 2 << endl; n     if (q1[i].second != 0) { n       cout <<  1   << abs(q1[i].second) <<   D n ; n     } n     cout <<  1   << abs(q1[i].first) <<   L n ; n     cout << 3 << endl; n   } n   for (int i = 0; i < q2.size(); i++) { n     cout <<  1   << abs(q2[i].second) <<   U n ; n     if (q2[i].first != 0) { n       cout <<  1   << abs(q2[i].first) <<   L n ; n     } n     cout << 2 << endl; n     if (q2[i].first != 0) { n       cout <<  1   << abs(q2[i].first) <<   R n ; n     } n     cout <<  1   << abs(q2[i].second) <<   D n ; n     cout << 3 << endl; n   } n   for (int i = 0; i < q3.size(); i++) { n     cout <<  1   << abs(q3[i].first) <<   L n ; n     if (q3[i].second != 0) { n       cout <<  1   << abs(q3[i].second) <<   D n ; n     } n     cout << 2 << endl; n     if (q3[i].second != 0) { n       cout <<  1   << abs(q3[i].second) <<   U n ; n     } n     cout <<  1   << abs(q3[i].first) <<   R n ; n     cout << 3 << endl; n   } n   for (int i = 0; i < q4.size(); i++) { n     cout <<  1   << abs(q4[i].second) <<   D n ; n     if (q4[i].first != 0) { n       cout <<  1   << abs(q4[i].first) <<   R n ; n     } n     cout << 2 << endl; n     if (q4[i].first != 0) { n       cout <<  1   << abs(q4[i].first) <<   L n ; n     } n     cout <<  1   << abs(q4[i].second) <<   U n ; n     cout << 3 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2007; n set<pair<int, int> > E[N]; n struct edge { n   int x, y; n } e[N]; n int n, rk[N], mxrk, mxid; n void adde(int x, int y, int i) { n   E[x].insert(make_pair(y, i)), E[y].insert(make_pair(x, i)); n } n void dele(int i) { n   int x = e[i].x, y = e[i].y; n   E[x].erase(make_pair(y, i)), E[y].erase(make_pair(x, i)); n   printf( ? %d %d n , x, y), fflush(stdout); n } n bool check(vector<pair<__int128, int> > &ve, __int128 x) { n   priority_queue<pair<__int128, int> > q; n   for (auto u : ve) q.push(u); n   for (int i = n - 1; i >= 0; i--) n     if (x >> i & 1) { n       pair<__int128, int> v = q.top(); n       q.pop(); n       v.first -= __int128(1) << i, rk[v.second] = i; n       q.push(v); n     } n   return q.top().first < 0; n } n __int128 produce(vector<pair<__int128, int> > &ve) { n   if (!ve.size()) return 0; n   __int128 z = (__int128(1) << n) - 1; n   for (int i = n - 1; i >= 0; i--) n     if (check(ve, z - (__int128(1) << i))) z -= __int128(1) << i; n   check(ve, z); n   return z; n } n __int128 dfs(int v, int fa) { n   vector<pair<__int128, int> > ve; n   for (auto x : E[v]) { n     int u = x.first; n     if (u == fa) continue; n     ve.push_back(make_pair(dfs(u, v), x.second)); n   } n   return produce(ve); n } n void find(int v, int fa) { n   for (auto x : E[v]) { n     int u = x.first, i = x.second; n     if (u == fa) continue; n     if (rk[i] > mxrk) mxrk = rk[i], mxid = i; n     find(u, v); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     adde(x, y, i), adde(y, x, i); n     e[i] = {x, y}; n   } n   dfs(1, 0); n   int rt = 1; n   while (1) { n     mxrk = -1, mxid = 0; n     find(rt, 0); n     if (mxrk == -1) break; n     dele(mxid); n     scanf( %d , &rt); n   } n   printf( ! %d n , rt), fflush(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long bin_pow(long long a, long long b) { n   if (b == 0) return 1; n   if (b % 2 == 0) { n     long long t = bin_pow(a, b / 2); n     return t * t % 1000000007; n   } else n     return a * bin_pow(a, b - 1) % 1000000007; n } n vector<long long> a; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long t = 1, n, m, k = 0, x = 0, y = 0, z = 0, sum = 0, l = 0, r = 0, n             ans = 0, mn = LLONG_MAX, mx = LLONG_MIN; n   cin >> n; n   a.resize(n); n   vector<pair<long long, long long> > d; n   stack<long long> s; n   for (int i = 0; i < n; i++) cin >> a[i]; n   t = 0; n   for (int i = 0; i < n; i++) { n     if (a[i] < t) { n       while (a[i] != t) { n         x = s.top(); n         s.pop(); n         d.push_back({x, i}); n         t--; n       } n     } else { n       while (a[i] != t) { n         s.push(i + 1); n         t++; n       } n     } n   } n   while (!s.empty()) { n     x = s.top(); n     d.push_back({x, n}); n     s.pop(); n   } n   cout << d.size() <<   n ; n   for (auto u : d) cout << u.first <<     << u.second <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> graph[5001]; n bool subset[5001][5001]; n set<pair<int, int>> ans_pairs; n vector<int> child_nodes(5001, -1); n int total_nodes; n void insert_into_answer(int network1, int network2) { n   if (network1 != 0 && network2 != 0) { n     ans_pairs.insert({network1, network2}); n     ans_pairs.insert({network2, network1}); n   } n   return; n } n bool isSubsetSum(vector<int> &set, int n, int sum) { n   for (int i = 0; i <= set.size(); i++) subset[i][0] = true; n   for (int j = 1; j <= sum; j++) subset[0][j] = false; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= sum; j++) { n       if (j < set[i - 1]) subset[i][j] = subset[i - 1][j]; n       if (j >= set[i - 1]) n         subset[i][j] = subset[i - 1][j] || subset[i - 1][j - set[i - 1]]; n     } n   } n   return subset[n][sum]; n } n int dfs(int id, int parent) { n   if (child_nodes[id] != -1) { n     return child_nodes[id]; n   } n   if (graph[id].size() == 0) { n     return child_nodes[id] = 1; n   } n   int temp = 0; n   for (long long i = 0; i < graph[id].size(); i++) { n     if (graph[id][i] != parent) { n       temp += dfs(graph[id][i], id); n     } n   } n   return child_nodes[id] = temp + 1; n } n void solve(int id, int parent) { n   insert_into_answer(child_nodes[id] - 1, total_nodes - child_nodes[id]); n   vector<int> childs_to_adjacent; n   for (long long i = 0; i < graph[id].size(); i++) { n     if (graph[id][i] != parent) { n       childs_to_adjacent.push_back(child_nodes[graph[id][i]]); n     } n   } n   isSubsetSum(childs_to_adjacent, childs_to_adjacent.size(), child_nodes[id]); n   int child_length = childs_to_adjacent.size(); n   for (long long i = 1; i < child_nodes[id]; i++) { n     if (subset[child_length][i]) { n       insert_into_answer(i, total_nodes - i - 1); n     } n   } n   for (long long i = 0; i < graph[id].size(); i++) { n     if (graph[id][i] != parent) { n       solve(graph[id][i], id); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> total_nodes; n   int x, y; n   for (long long i = 1; i < total_nodes; i++) { n     cin >> x >> y; n     graph[x - 1].push_back(y - 1); n     graph[y - 1].push_back(x - 1); n   } n   int a = dfs(0, -1); n   solve(0, -1); n   cout << ans_pairs.size() << endl; n   for (auto it : ans_pairs) { n     cout << it.first <<     << it.second << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, d; n   cin >> a >> b >> c >> d; n   int e = max(c, d); n   if (e > 2 * c || e > 2 * d || e >= b) { n     cout << -1; n     return 0; n   } n   cout << 2 * a << endl << 2 * b << endl << e; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     string str; n     int n; n     cin >> n; n     cin.ignore(); n     getline(cin, str); n     int l = 0, r = n - 1; n     while (l < n && str[l] ==  0 ) { n       l++; n     } n     while (r >= 0 && str[r] ==  1 ) { n       r--; n     } n     if (l >= r) { n       cout << str << endl; n     } else { n       cout << str.substr(0, l) <<  0  << str.substr(r + 1, n) << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, k, l; n     cin >> n >> k; n     vector<int> barrel; n     for (int i = 0; i < n; i++) { n       cin >> l; n       barrel.push_back(l); n     } n     sort(barrel.begin(), barrel.end()); n     long long int resp = 0; n     k++; n     while (k--) { n       resp += barrel.back(); n       barrel.pop_back(); n     } n     cout << resp << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005; n char buf[100000]; n vector<pair<int, string> > vp; n int b[maxn], n, m; n pair<int, string> me; n int getL() { n   int tx = 0, ret = 1; n   if (m == n) { n     me.first -= b[0]; n     tx = 1; n   } n   for (int i = 0; i < (int)vp.size(); i++) { n     if (vp[i] < me) { n       ret++; n       continue; n     } n     while (tx < m) { n       pair<int, string> tmp = vp[i]; n       tmp.first -= b[tx]; n       tx++; n       if (tmp < me) { n         ret++; n         break; n       } n     } n   } n   if (m == n) me.first += b[0]; n   return ret; n } n int getH() { n   me.first -= b[m - 1]; n   int tx = m - 2, ret = 1; n   vector<pair<int, string> > vp2; n   for (int i = 0; i < (int)vp.size(); i++) { n     if (vp[i] < me) { n       if (tx >= 0) tx--; n       ret++; n     } else n       vp2.push_back(vp[i]); n   } n   for (int i = (int)vp2.size() - 1; i >= 0; i--) { n     while (tx >= 0) { n       pair<int, string> tmp = vp2[i]; n       tmp.first -= b[tx]; n       tx--; n       if (tmp < me) { n         ret++; n       } else n         break; n     } n   } n   while (tx >= 0) { n     ret++; n     tx--; n   } n   me.first += b[m - 1]; n   return ret; n } n int main() { n   while (scanf( %d , &n) != EOF) { n     vp.clear(); n     for (int i = 0; i < n; i++) { n       int tmp; n       scanf( %s%d , buf, &tmp); n       vp.push_back(make_pair(-tmp, string(buf))); n     } n     scanf( %d , &m); n     for (int i = 0; i < m; i++) { n       scanf( %d , &b[i]); n     } n     sort(b, b + m); n     scanf( %s , buf); n     string s(buf); n     for (int i = 0; i < (int)vp.size(); i++) { n       if (vp[i].second == s) { n         me = vp[i]; n         swap(vp[i], vp[vp.size() - 1]); n         vp.pop_back(); n         break; n       } n     } n     sort(vp.begin(), vp.end()); n     printf( %d %d n , getH(), getL()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   int x, y, d, r; n   bool operator<(edge const &T) const { return d < T.d; } n }; n int n, t, ed, x, y; n int v[100005], b[100005], p[100005], s[100005], l[100005], qd[100005 + 100005], n     qn[100005 + 100005], qr[100005 + 100005]; n long long int sum, mx, sz[100005], res[100005]; n edge a[100005]; n void bd(int x, int y, int r) { n   qd[++ed] = y, qn[ed] = l[x], qr[ed] = r, l[x] = ed; n } n void precal(int x) { n   sum += s[x]; n   v[x] = 1; n   for (int i = l[x]; i; i = qn[i]) n     if (!v[qd[i]]) precal(qd[i]); n } n void dfs(int x, int y) { n   sz[x] = s[x]; n   for (int i = l[x]; i; i = qn[i]) { n     if (qd[i] == y) continue; n     dfs(qd[i], x); n     res[qr[i]] = (sum - sz[qd[i]]) * sz[qd[i]]; n     sz[x] += sz[qd[i]]; n   } n } n int find(int x) { return x == p[x] ? x : p[x] = find(p[x]); } n int main() { n   scanf( %d , &n); n   for (int i = (0); i < (n - 1); i++) n     scanf( %d%d%d , &a[i].x, &a[i].y, &a[i].d), a[i].r = i + 1; n   for (int i = (1); i <= (n); i++) p[i] = i, s[i] = 1; n   sort(a, a + n - 1); n   for (int i = 0, j = 0; i < n - 1; i = j) { n     ed = t = 0; n     while (j < n - 1 && a[i].d == a[j].d) { n       b[t++] = x = find(a[j].x); n       b[t++] = y = find(a[j].y); n       bd(x, y, a[j].r), bd(y, x, a[j].r); n       j++; n     } n     sort(b, b + t); n     t = unique(b, b + t) - b; n     for (int k = (0); k < (t); k++) v[b[k]] = 0; n     for (int k = (0); k < (t); k++) { n       if (v[b[k]]) continue; n       sum = 0; n       precal(b[k]); n       dfs(b[k], -1); n     } n     for (int k = (0); k < (t); k++) l[b[k]] = 0; n     for (int k = i; k < j; k++) { n       x = find(a[k].x), y = find(a[k].y); n       p[x] = y; n       s[y] += s[x]; n     } n   } n   mx = 0; n   for (int i = (1); i <= (n - 1); i++) mx = max(mx, res[i]); n   sum = 0; n   for (int i = (1); i <= (n - 1); i++) sum += res[i] == mx; n   printf( %I64d %I64d n , mx << 1, sum); n   for (int i = (1); i <= (n - 1); i++) n     if (res[i] == mx) printf( %d  , i); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > v; n int promena[3002]; n long long d[3001], s[3002][3002], pom; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int j, n, i, x, p; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> x >> p; n     v.push_back(make_pair(x, p)); n   } n   sort(v.begin(), v.end()); n   d[n - 1] = v[n - 1].second; n   for (i = 0; i < n - 1; i++) n     for (j = i + 1; j < n; j++) n       s[i][j] = s[i][j - 1] + (long long)v[j].first - v[i].first; n   for (i = n - 2; i >= 0; i--) { n     for (j = i; j < n; j++) { n       pom = d[j + 1] + s[i][j] + (long long)v[i].second; n       if (promena[i] == 0) { n         d[i] = pom; n         promena[i] = 1; n       } else { n         if (d[i] > pom) d[i] = pom; n       } n     } n   } n   cout << d[0]; 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   cout.tie(0); n   ; n   long long int n; n   cin >> n; n   long long int a[n]; n   map<long long int, long long int> mp; n   for (long long int i = 0; i < n; i++) { n     cin >> a[i]; n     mp[a[i]] = i; n   } n   long long int prev = mp[1]; n   long long int mx = 1; n   for (long long int i = 2; i <= n; i++) { n     if (mp[i] > mp[i - 1]) { n       long long int j = i; n       long long int cnt = 1; n       while (mp[j] > mp[j - 1]) { n         cnt++; n         j++; n       } n       mx = max(mx, cnt); n       i = j - 1; n     } n   } n   long long int ans = n - mx; n   cout << ans <<   n ; n   cerr <<   nTime elapsed:   << 1000 * clock() / CLOCKS_PER_SEC <<  ms n ; n } n


#include <bits/stdc++.h> n using namespace std; n bool SR(int &_x) { return scanf( %d , &_x) == 1; } n bool SR(long long &_x) { return scanf( %lld , &_x) == 1; } n bool SR(double &_x) { return scanf( %lf , &_x) == 1; } n bool SR(char *_s) { return scanf( %s , _s) == 1; } n bool RI() { return true; } n template <typename I, typename... T> n bool RI(I &_x, T &..._tail) { n   return SR(_x) && RI(_tail...); n } n void SP(const int _x) { printf( %d , _x); } n void SP(const long long _x) { printf( %lld , _x); } n void SP(const double _x) { printf( %.16lf , _x); } n void SP(const char *s) { printf( %s , s); } n void PL() { puts(  ); } n template <typename I, typename... T> n void PL(const I _x, const T... _tail) { n   SP(_x); n   if (sizeof...(_tail)) putchar(   ); n   PL(_tail...); n } n const int maxn = 55; n struct Solver { n   int wid, hei; n   char mp[maxn][maxn]; n   void rot(int x, int y) { n     if (mp[x][y] ==  L ) { n       for (int i : {x, x + 1}) n         for (int j : {y, y + 1}) mp[i][j] =  UD [i - x]; n     } else { n       for (int i : {x, x + 1}) n         for (int j : {y, y + 1}) mp[i][j] =  LR [j - y]; n     } n   } n   vector<pair<int, int> > solve() { n     assert(wid % 2 == 0); n     vector<pair<int, int> > ret; n     for (int i = 0; i < int(hei); i++) n       for (int j = 0; j < wid; j += 2) { n         vector<pair<int, int> > cur; n         int x = i, y = j, f = 0; n         while (1) { n           if ((f == 0 && mp[x][y] ==  L ) || (f == 1 && mp[x][y] ==  U )) break; n           cur.push_back({x, y}); n           if (f == 0) n             y++; n           else n             x++; n           f ^= 1; n         } n         reverse((cur).begin(), (cur).end()); n         for (auto p : cur) rot(p.first, p.second); n         ret.insert(ret.end(), (cur).begin(), (cur).end()); n       } n     return ret; n   } n } sol0, sol1; n int n, m; n char f0[maxn][maxn], f1[maxn][maxn]; n void read() { n   RI(n, m); n   for (int i = 0; i < int(n); i++) RI(f0[i]); n   for (int i = 0; i < int(n); i++) RI(f1[i]); n } n bool flip; n void build() { n   if (m % 2) { n     flip = 1; n     auto rev = [&](char c) { n       if (c ==  L ) return  U ; n       if (c ==  R ) return  D ; n       if (c ==  U ) return  L ; n       if (c ==  D ) return  R ; n       assert(0); n     }; n     { n       sol0.hei = m; n       sol0.wid = n; n       for (int i = 0; i < int(n); i++) n         for (int j = 0; j < int(m); j++) sol0.mp[j][i] = rev(f0[i][j]); n       sol1.hei = m; n       sol1.wid = n; n       for (int i = 0; i < int(n); i++) n         for (int j = 0; j < int(m); j++) sol1.mp[j][i] = rev(f1[i][j]); n     } n   } else { n     sol0.hei = n; n     sol0.wid = m; n     for (int i = 0; i < int(n); i++) n       for (int j = 0; j < int(m); j++) sol0.mp[i][j] = f0[i][j]; n     sol1.hei = n; n     sol1.wid = m; n     for (int i = 0; i < int(n); i++) n       for (int j = 0; j < int(m); j++) sol1.mp[i][j] = f1[i][j]; n   } n } n void sol() { n   auto res0 = sol0.solve(), res1 = sol1.solve(); n   reverse((res1).begin(), (res1).end()); n   res0.insert(res0.end(), (res1).begin(), (res1).end()); n   if (flip) n     for (auto &p : res0) swap(p.first, p.second); n   PL(((int)(res0).size())); n   for (auto p : res0) PL(p.first + 1, p.second + 1); n } n int main() { n   read(); n   build(); n   sol(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int setBit(int N, int pos) { return N = N | (1 << pos); } n inline int resetBit(int N, int pos) { return N = N & ~(1 << pos); } n inline bool checkBit(int N, int pos) { return (bool)(N & (1 << pos)); } n int A[400000 + 10]; n vector<int> nxt[400000 + 10]; n int idx[400000 + 10]; n struct data { n   int id, p; n }; n bool operator<(data a, data b) { return a.p < b.p; } n priority_queue<data> Q; n int holding[400000 + 10]; n void validate(int i) { n   while (Q.size() && Q.top().p <= i) Q.pop(); n } n int main() { n   int n, k; n   scanf( %d %d , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %d , &A[i]), nxt[A[i]].push_back(i); n   for (int i = 1; i <= n; i++) nxt[i].push_back(n + 1), idx[i] = 1; n   int ans = 0; n   int have = 0; n   for (int i = 1; i <= n; i++) { n     if (holding[A[i]] == true) { n       Q.push({A[i], nxt[A[i]][idx[A[i]]]}); n       idx[A[i]]++; n     } else { n       if (have < k) { n         have++; n         ans++; n         Q.push({A[i], nxt[A[i]][idx[A[i]]]}); n         idx[A[i]]++; n         holding[A[i]] = true; n       } else { n         validate(i); n         ans++; n         holding[Q.top().id] = false; n         Q.pop(); n         Q.push({A[i], nxt[A[i]][idx[A[i]]]}); n         holding[A[i]] = true; n         idx[A[i]]++; n       } n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 100 + 3; n int n, m, k, ns[MAX]; n long long dp[MAX][MAX][MAX], cs[MAX][MAX]; n long long solve(int cur = 0, int pre = 0, int rem = k) { n   if (cur == n && rem == 0) return 0; n   if (rem < 0 || (cur == n && rem != 0)) return 1e18; n   long long &ret = dp[cur][pre][rem]; n   if (ret != -1) return ret; n   if (ns[cur] == 0) { n     ret = 1e18; n     for (int i = 1; i <= m; i++) n       ret = min(ret, solve(cur + 1, i, rem - (pre != i)) + cs[cur][i - 1]); n   } else { n     if (ns[cur] == pre) n       ret = solve(cur + 1, pre, rem); n     else n       ret = solve(cur + 1, ns[cur], rem - 1); n   } n   return ret; n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   memset(dp, -1, sizeof(dp)); n   for (int i = 0; i < n; i++) { n     scanf( %d , &ns[i]); n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       scanf( %d , &cs[i][j]); n     } n   } n   long long ans = solve(); n   if (ans == 1e18) n     printf( -1 ); n   else n     printf( %lld , ans); n   return 0; n } n