#include <bits/stdc++.h> n using namespace std; n struct re { n   long long x[300300]; n   long long up[300300]; n   bool f[300300]; n }; n int o; n re rmq; n void f_read() { n   int i, n, a[100100]; n   cin >> n; n   for (i = 0; i < n; ++i) cin >> a[i]; n   o = 1; n   memset(rmq.x, 0, sizeof(rmq.x)); n   memset(rmq.up, 0, sizeof(rmq.up)); n   memset(rmq.f, 0, sizeof(rmq.f)); n   while (o < n) o *= 2; n   for (i = 0; i < n; ++i) rmq.x[i + o] = a[i]; n   for (i = o - 1; i > 0; --i) n     rmq.x[i] = ((rmq.x[i * 2]) < (rmq.x[i * 2 + 1]) ? (rmq.x[i * 2 + 1]) n                                                     : (rmq.x[i * 2])); n } n void RPUSH(int v) { n   if (!rmq.f[v]) return; n   rmq.f[v * 2] = 1; n   rmq.up[v * 2] = rmq.up[v]; n   rmq.f[v * 2 + 1] = 1; n   rmq.up[v * 2 + 1] = rmq.up[v]; n   rmq.x[v] = rmq.up[v]; n   rmq.f[v] = 0; n } n long long RMAX(int l, int r, int v = 1, int cl = 1, int cr = o) { n   if (l > r) return 0; n   if (l == cl && r == cr) { n     if (rmq.f[v]) n       return rmq.up[v]; n     else n       return rmq.x[v]; n   } n   RPUSH(v); n   int t = (cl + cr) >> 1; n   long long a = RMAX(l, ((r) > (t) ? (t) : (r)), v * 2, cl, t), n             b = RMAX(((t + 1) < (l) ? (l) : (t + 1)), r, v * 2 + 1, t + 1, cr); n   return ((a) < (b) ? (b) : (a)); n } n long long RADD(int l, int r, long long d, int v = 1, int cl = 1, int cr = o) { n   if (l > r) n     if (rmq.f[v]) n       return rmq.up[v]; n     else n       return rmq.x[v]; n   if (l == cl && r == cr) { n     rmq.f[v] = 1; n     rmq.up[v] = d; n     return d; n   } n   RPUSH(v); n   int t = (cl + cr) >> 1; n   long long a = RADD(l, ((t) > (r) ? (r) : (t)), d, v * 2, cl, t), n             b = RADD(((t + 1) < (l) ? (l) : (t + 1)), r, d, v * 2 + 1, t + 1, n                      cr); n   rmq.x[v] = ((a) < (b) ? (b) : (a)); n   return rmq.x[v]; n } n int main() { n   f_read(); n   int i, m, w, h; n   cin >> m; n   for (i = 0; i < m; ++i) { n     cin >> w >> h; n     long long t = RMAX(1, w); n     cout << t << endl; n     RADD(1, w, h + t); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 10; n const long long MOD = 1e9 + 7; n long long n, x[N], ans, pw[N], dp[N], num[N]; n inline long long add(long long a, long long b) { n   a += b; n   while (a >= MOD) a -= MOD; n   return a; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.setf(ios::fixed); n   cout.precision(20); n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> x[i]; n   if (n == 1) { n     cout << 0 <<   n ; n     return 0; n   } n   sort(x, x + n); n   pw[0] = 1; n   for (int i = 1; i <= n; ++i) { n     pw[i] = add(pw[i - 1], pw[i - 1]); n   } n   dp[1] = x[1] - x[0]; n   num[1] = 1; n   for (int i = 2; i < n; ++i) { n     long long now = x[i] - x[i - 1]; n     dp[i] = add(dp[i - 1], num[i - 1] * now); n     dp[i] = add(dp[i], dp[i]); n     dp[i] = add(dp[i], now); n     num[i] = add(num[i - 1], num[i - 1]); n     num[i] = add(num[i], 1); n   } n   for (int i = 0; i < n; ++i) ans = add(ans, dp[i]); n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n char ss[8][5] = { 1869 ,  6198 ,  1896 ,  1689 ,  1986 ,  1968 ,  1698 }; n string s; n int x, cnt, vis[1000010]; n int main() { n   cin >> s; n   int len = s.length(); n   for (register int i = 0; i < len; i++) { n     if (s[i] ==  0 ) n       cnt++; n     else if (!vis[s[i]] and n              (s[i] ==  1  or s[i] ==  6  or s[i] ==  8  or s[i] ==  9 )) n       vis[s[i]] = 1; n     else { n       x = (x << 3) + (x << 1) + (s[i] ^ 48); n       x %= 7; n       cout << s[i]; n     } n   } n   if (x == 0) n     cout << 1869; n   else if (x == 1) n     cout << 6198; n   else if (x == 2) n     cout << 1896; n   else if (x == 3) n     cout << 1689; n   else if (x == 4) n     cout << 1986; n   else if (x == 5) n     cout << 1968; n   else if (x == 6) n     cout << 1698; n   for (int i = 1; i <= cnt; i++) cout << 0; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <class A, class B> n A cvt(B x) { n   stringstream ss; n   ss << x; n   A y; n   ss >> y; n   return y; n } n typedef struct { n   string name; n   int c; n   int p[3]; n   int size; n   int room; n } item; n typedef struct { n   string name; n   int c; n   int bonus; n   string home; n } resident; n int n, k; n map<string, item> items; n map<string, vector<string> > itres; n map<string, resident> residents; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     string name, c; n     int a, d, r, s; n     cin >> name >> c >> a >> d >> r >> s; n     if (c ==  weapon ) items[name] = (item){name, 0, {a, d, r}, s, s}; n     if (c ==  armor ) items[name] = (item){name, 1, {a, d, r}, s, s}; n     if (c ==  orb ) items[name] = (item){name, 2, {a, d, r}, s, s}; n   } n   cin >> k; n   for (int i = 0; i < k; i++) { n     string name, c, home; n     int b; n     cin >> name >> c >> b >> home; n     if (c ==  gladiator ) residents[name] = (resident){name, 0, b, home}; n     if (c ==  sentry ) residents[name] = (resident){name, 1, b, home}; n     if (c ==  physician ) residents[name] = (resident){name, 2, b, home}; n     items[home].room--; n     itres[home].push_back(name); n   } n   int move = 0; n   for (typeof(items.begin()) it = items.begin(); it != items.end(); it++) n     if (it->second.room > 0) move = 1; n   if (!move) { n     vector<pair<int, pair<int, string> > > v; n     for (typeof(items.begin()) it = items.begin(); it != items.end(); it++) { n       string name = it->first; n       int cl = it->second.c; n       int val = it->second.p[cl]; n       for (typeof(itres[name].begin()) itr = itres[name].begin(); n            itr != itres[name].end(); itr++) { n         if (residents[*itr].c == cl) val += residents[*itr].bonus; n       } n       v.push_back(make_pair(cl, make_pair(-val, name))); n     } n     sort(v.begin(), v.end()); n     int i = 0; n     for (int t = 0; t < 3; t++) { n       while (v[i].first != t) i++; n       string name = v[i].second.second; n       cout << name <<     << itres[name].size(); n       for (typeof(itres[name].begin()) itr = itres[name].begin(); n            itr != itres[name].end(); itr++) n         cout <<     << *itr; n       cout << endl; n     } n   } else { n     vector<pair<int, string> > r[3]; n     for (typeof(residents.begin()) itr = residents.begin(); n          itr != residents.end(); itr++) { n       r[itr->second.c].push_back(make_pair(itr->second.bonus, itr->first)); n     } n     int sum[3][1005]; n     for (int t = 0; t < 3; t++) { n       sort(r[t].begin(), r[t].end()); n       reverse(r[t].begin(), r[t].end()); n       sum[t][0] = 0; n       for (int i = 1; i <= r[t].size(); i++) { n         sum[t][i] = sum[t][i - 1] + r[t][i - 1].first; n       } n     } n     pair<int, pair<int, int> > best = make_pair(-1, make_pair(-1, -1)); n     pair<string, vector<string> > out[3]; n     set<string> used; n     for (typeof(items.begin()) it0 = items.begin(); it0 != items.end(); it0++) n       if (it0->second.c == 0) n         for (typeof(items.begin()) it1 = items.begin(); it1 != items.end(); n              it1++) n           if (it1->second.c == 1) n             for (typeof(items.begin()) it2 = items.begin(); it2 != items.end(); n                  it2++) n               if (it2->second.c == 2) { n                 int sz0 = min(it0->second.size, (int)r[0].size()); n                 int sz1 = min(it1->second.size, (int)r[1].size()); n                 int sz2 = min(it2->second.size, (int)r[2].size()); n                 pair<int, pair<int, int> > rez = n                     make_pair(it0->second.p[0] + sum[0][sz0], n                               make_pair(it1->second.p[1] + sum[1][sz1], n                                         it2->second.p[2] + sum[2][sz2])); n                 if (rez > best) { n                   best = rez; n                   used.clear(); n                   vector<string> x0, x1, x2; n                   for (int i = 0; i < sz0; i++) { n                     x0.push_back(r[0][i].second); n                     used.insert(r[0][i].second); n                   } n                   for (int i = 0; i < sz1; i++) { n                     x1.push_back(r[1][i].second); n                     used.insert(r[1][i].second); n                   } n                   for (int i = 0; i < sz2; i++) { n                     x2.push_back(r[2][i].second); n                     used.insert(r[2][i].second); n                   } n                   out[0] = make_pair(it0->first, x0); n                   out[1] = make_pair(it1->first, x1); n                   out[2] = make_pair(it2->first, x2); n                 } n               } n     for (int t = 0; t < 3; t++) { n       for (typeof(residents.begin()) itr = residents.begin(); n            itr != residents.end(); itr++) { n         if (out[t].second.size() == items[out[t].first].size) break; n         if (used.count(itr->first) == 0) { n           out[t].second.push_back(itr->first); n           used.insert(itr->first); n         } n       } n       cout << out[t].first <<     << out[t].second.size(); n       for (int i = 0; i < out[t].second.size(); i++) n         cout <<     << out[t].second[i]; n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mxN = 1e5, M = 1e9 + 7, I3 = 333333336; n int n, m, eu[mxN], ev[mxN], ew[mxN], c2, px[mxN]; n vector<int> adj[mxN], c; n long long ans = 1, ans2, ca; n bool vis[mxN], vis2[mxN]; n void dfs(int u, int p) { n   vis[u] = 1; n   for (int e : adj[u]) { n     int v = eu[e] ^ ev[e] ^ u; n     if (v == p) continue; n     if (vis[v]) { n       if (vis2[e]) continue; n       vis2[e] = 1; n       if (!v) { n         c2 = px[u] ^ ew[e]; n       } else { n         c.push_back(px[u] ^ px[v] ^ ew[e]); n       } n     } else { n       px[v] = px[u] ^ ew[e]; n       dfs(v, u); n     } n   } n } n vector<int> gv(vector<int> &c) { n   vector<int> a; n   for (int i = 0; i < c.size(); ++i) { n     int j = 0; n     while (j < a.size()) { n       c[i] = min(c[i], a[j] ^ c[i]); n       ++j; n     } n     a.push_back(c[i]); n   } n   sort(a.begin(), a.end()); n   return a; n } n vector<array<int, 2>> bmv, s2mv, s1mv; n map<int, int> sm; n void iv(vector<int> &c, int d) { n   int bm = 0; n   for (int ci : c) bm |= 1 << ci; n   if (c.size() > 2) { n     bmv.push_back({bm, d}); n   } else { n     sm[bm] = (sm[bm] + d) % M; n   } n } n vector<int> av(vector<int> a, int b) { n   for (int i = 0; i < 32; ++i) n     if (b >> i & 1) { n       int j = 0, x = i; n       while (j < a.size()) { n         x = min(x, x ^ a[j]); n         ++j; n       } n       a.push_back(x); n       if (!x) swap(a[0], a.back()); n     } n   return a; n } n void dfs2(vector<int> &a, int i, int j) { n   ans2 = (ans2 + ca) % M; n   if (a.size() < 4) { n     for (; i < s2mv.size(); ++i) { n       vector<int> b = av(a, s2mv[i][0]); n       if (!b[0]) continue; n       long long da = ca; n       ca = ca * s2mv[i][1] % M; n       dfs2(b, i + 1, j); n       ca = da; n     } n   } n   if (a.size() < 5) { n     for (; j < s1mv.size(); ++j) { n       vector<int> b = av(a, s1mv[j][0]); n       if (!b[0]) continue; n       long long da = ca; n       ca = ca * s1mv[j][1] % M; n       dfs2(b, i, j + 1); n       ca = da; n     } n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   for (int i = 0; i < m; ++i) { n     cin >> eu[i] >> ev[i] >> ew[i], --eu[i], --ev[i]; n     adj[eu[i]].push_back(i); n     adj[ev[i]].push_back(i); n   } n   vis[0] = 1; n   for (int e : adj[0]) { n     int u = eu[e] ^ ev[e]; n     if (vis[u]) continue; n     c.clear(); n     c2 = -1; n     px[u] = ew[e]; n     dfs(u, 0); n     if (c2 == -1) { n       if (!c.size()) { n         ans = ans * 2 % M; n       } else { n         vector<int> a = gv(c); n         if (a[0]) iv(c, 1); n       } n     } else { n       if (c.size()) { n         vector<int> a = gv(c); n         if (a[0]) { n           iv(c, 2); n           c.push_back(c2); n           a = gv(c); n           if (a[0]) iv(c, 1); n         } n       } else { n         ans = ans * 3 % M; n         if (c2) { n           c.push_back(c2); n           iv(c, I3); n         } n       } n     } n   } n   for (pair<int, int> p : sm) { n     if (__builtin_popcount(p.first) == 1) n       s1mv.push_back({p.first, p.second}); n     else n       s2mv.push_back({p.first, p.second}); n   } n   for (array<int, 2> a : bmv) { n     vector<int> b; n     b = av(b, a[0]); n     ca = a[1]; n     dfs2(b, 0, 0); n   } n   vector<int> b; n   ca = 1; n   dfs2(b, 0, 0); n   cout << ans * ans2 % M; n } n


#include <bits/stdc++.h> n using namespace std; n struct arr { n   int l, r, add, cnt, sum[50]; n } a[110005 * 4]; n int data[110005], i, n, m, opt, L, R; n const int P = 95542721; n inline int calc(int x) { n   while (x > 47) x -= 48; n   return x; n } n inline void down(int k) { n   if (!a[k].cnt || a[k].l == a[k].r) return; n   int W = a[k].cnt; n   a[k].cnt = 0; n   a[(k << 1)].add = calc(a[(k << 1)].add + W); n   a[(k << 1)].cnt = calc(a[(k << 1)].cnt + W); n   a[(k << 1 | 1)].add = calc(a[(k << 1 | 1)].add + W); n   a[(k << 1 | 1)].cnt = calc(a[(k << 1 | 1)].cnt + W); n } n inline void up(int k) { n   for (int i = 0; i < 48; i++) n     a[k].sum[i] = (a[(k << 1)].sum[calc(a[(k << 1)].add + i)] + n                    a[(k << 1 | 1)].sum[calc(a[(k << 1 | 1)].add + i)]) % n                   P; n } n void build(int k, int l, int r) { n   a[k].l = l; n   a[k].r = r; n   a[k].add = a[k].cnt = 0; n   if (l == r) { n     a[k].sum[0] = data[l]; n     for (int i = 1; i <= 47; i++) n       a[k].sum[i] = (a[k].sum[i - 1] * 1ll * a[k].sum[i - 1] % P * 1ll * n                      a[k].sum[i - 1] % P); n     return; n   } n   int mid = (l + r) >> 1; n   if (l <= mid) build((k << 1), l, mid); n   if (r > mid) build((k << 1 | 1), mid + 1, r); n   for (int i = 0; i < 48; i++) n     a[k].sum[i] = (a[(k << 1)].sum[i] + a[(k << 1 | 1)].sum[i]) % P; n } n void update(int k) { n   if (L <= a[k].l && a[k].r <= R) { n     a[k].cnt = calc(a[k].cnt + 1); n     a[k].add = calc(a[k].add + 1); n     return; n   } n   down(k); n   int mid = (a[k].l + a[k].r) >> 1; n   if (L <= mid) update((k << 1)); n   if (R > mid) update((k << 1 | 1)); n   a[k].add = 0; n   up(k); n } n int Query(int k) { n   if (L <= a[k].l && a[k].r <= R) return a[k].sum[calc(a[k].add)]; n   down(k); n   int res = 0; n   int mid = (a[k].l + a[k].r) >> 1; n   if (L <= mid) res = Query((k << 1)); n   if (R > mid) (res += Query((k << 1 | 1))) %= P; n   return res; n } n int main() { n   scanf( %d , &n); n   for (i = 1; i <= n; i++) scanf( %d , &data[i]); n   build(1, 1, n); n   scanf( %d , &m); n   while (m--) { n     scanf( %d%d%d , &opt, &L, &R); n     if (opt == 1) n       printf( %d n , Query(1)); n     else n       update(1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int _inf = 0xc0c0c0c0; n const long long INF = 0x3f3f3f3f3f3f3f3f; n const long long _INF = 0xc0c0c0c0c0c0c0c0; n const long long mod = (int)1e9 + 7; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n long long ksm(long long a, long long b, long long mod) { n   int ans = 1; n   while (b) { n     if (b & 1) ans = (ans * a) % mod; n     a = (a * a) % mod; n     b >>= 1; n   } n   return ans; n } n long long inv2(long long a, long long mod) { return ksm(a, mod - 2, mod); } n void exgcd(long long a, long long b, long long &x, long long &y, long long &d) { n   if (!b) { n     d = a; n     x = 1; n     y = 0; n   } else { n     exgcd(b, a % b, y, x, d); n     y -= x * (a / b); n   } n } n int dp[10025]; n int main() { n   int c, d, n, m, k, V, ans = inf; n   scanf( %d%d , &c, &d); n   scanf( %d%d , &n, &m); n   scanf( %d , &k); n   memset(dp, 0x3f, sizeof(dp)); n   dp[0] = 0; n   for (int j = n; j <= n * m; ++j) { n     dp[j] = min(dp[j], dp[j - n] + c); n   } n   for (int j = 1; j <= n * m; ++j) { n     dp[j] = min(dp[j], dp[j - 1] + d); n   } n   for (int i = n * m - k; i <= n * m; ++i) { n     ans = min(ans, dp[i]); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const string str =  VC ; n const int maxn = 300; n char s[2]; n int l, n, m; n char buf[maxn]; n int alpha[26]; n vector<int> G[maxn * 2]; n bool mark[maxn * 2]; n int stk[maxn * 2], top; n bool dfs(int u) { n   if (s[u & 1] == 0) return false; n   if (mark[u ^ 1]) return false; n   if (mark[u]) return true; n   while (top == 2 * maxn) n     ; n   mark[stk[top++] = u] = true; n   for (int v : G[u]) n     if (!dfs(v)) return false; n   return true; n } n string ans; n void check(string tmp) { n   if (ans ==    || tmp < ans) ans = tmp; n } n void solve(int st) { n   for (int i = 0; i < 2 * n; i++) mark[i] = 0; n   for (int i = 0; i < st; i++) { n     top = 0; n     if (!dfs(i << 1 | alpha[buf[i] -  a ])) return; n   } n   for (int i = 2 * st; i < 2 * n; i += 2) { n     if (mark[i] || mark[i ^ 1]) continue; n     top = 0; n     if (s[0] < s[1]) { n       if (!dfs(i)) { n         while (top) mark[stk[--top]] = false; n         if (!dfs(i ^ 1)) return; n       } n     } else { n       if (!dfs(i ^ 1)) { n         while (top) mark[stk[--top]] = false; n         if (!dfs(i)) return; n       } n     } n   } n   string tmp(buf, buf + st); n   for (int i = st; i < n; i++) tmp += s[mark[i << 1 | 1]]; n   check(tmp); n } n int main() { n   scanf( %s , buf); n   l = strlen(buf); n   for (int i = l - 1; i >= 0; i--) s[alpha[i] = str.find(buf[i])] =  a  + i; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < m; i++) { n     int x, y; n     scanf( %d %s , &x, buf); n     x--; n     x = x << 1 | str.find(buf[0]); n     scanf( %d %s , &y, buf); n     y--; n     y = y << 1 | str.find(buf[0]); n     G[x].push_back(y); n     G[y ^ 1].push_back(x ^ 1); n   } n   scanf( %s , buf); n   for (int i = 0; i <= n; i++) { n     if (i < n) { n       char ch = buf[i]; n       for (buf[i]++; buf[i] <  a  + l; buf[i]++) solve(i + 1); n       buf[i] = ch; n     } else n       solve(n); n   } n   if (ans !=   ) n     puts(ans.c_str()); n   else n     puts( -1 ); n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n int n, k, x, sum, a[24], BadPoint[2], dp[(1 << 24)]; n int32_t main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   cin >> k; n   for (int i = 0; i < k; i++) cin >> BadPoint[i]; n   dp[0] = 1; n   for (int mask = 1; mask < (1 << n); mask++) { n     sum = 0; n     for (int i = 0; i < n; i++) { n       if ((mask >> i) & 1) { n         dp[mask] += dp[(mask ^ (1 << i))]; n         if (dp[mask] >= MOD) dp[mask] -= MOD; n         sum += a[i]; n       } n     } n     for (int i = 0; i < k; i++) n       if (sum == BadPoint[i]) dp[mask] = 0; n   } n   cout << dp[(1 << n) - 1]; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   int a = 2, b = 3, c = 5, d = 7; n   int ab = 6, ac = 10, ad = 14, bc = 15, bd = 21, cd = 35; n   int abc = 30, bcd = 105, abd = 42, acd = 70; n   int abcd = 210; n   cout << n - (n / a + n / b + n / c + n / d) + n               (n / ab + n / ac + n / ad + n / bc + n / bd + n / cd) - n               (n / abc + n / bcd + n / abd + n / acd) + n / abcd; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> ar[110]; n set<int> se; n int findstart(int k) { n   while (ar[k].first != 0) k = ar[k].first; n   return k; n } n int findend(int k) { n   while (ar[k].second != 0) k = ar[k].second; n   return k; n } n void addalltoend(int i) { n   se.insert(ar[i].second); n   while (ar[i].second != 0) { n     i = ar[i].second; n     se.insert(ar[i].second); n   } n } n int main() { n   int n, en, nen; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> ar[i].first >> ar[i].second; n   } n   int i = 0; n   while (ar[i].first == 0 && ar[i].second == 0 && i != n + 1) { n     i++; n   } n   if (n == 1) { n     cout << 0 <<     << 0; n     return 0; n   } n   if (n != 1 && i == n + 1) { n     for (i = 1; i < n; i++) { n       cout << i - 1 <<     << i + 1 <<   n ; n     } n     cout << i - 1 <<     << 0 <<   n ; n     return 0; n   } n   i = findstart(i); n   en = findend(i); n   se.insert(i); n   addalltoend(i); n   se.erase(0); n   for (set<int>::const_iterator it = se.begin(); it != se.end(); it++) n     for (int i = 1; i <= n; i++) { n       if (ar[i].first == 0 && ar[i].second != 0 && n           se.find(ar[i].second) == se.end()) { n         ar[en].second = i; n         ar[i].first = en; n         i = findstart(i); n         addalltoend(i); n         en = findend(i); n       } n     } n   for (int i = 1; i <= n; i++) { n     if (ar[i].first == 0 && ar[i].second == 0) { n       ar[en].second = i; n       ar[i].first = en; n       en = i; n     } n   } n   for (int i = 1; i <= n; i++) n     cout << ar[i].first <<     << ar[i].second <<   n ; n   for (set<int>::const_iterator it = se.begin(); it != se.end(); it++) return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void calc(int n, int p) { n   int j = 1, k = 1; n   int cnt = 0; n   int here = 1; n   for (int i = 0; i < 2 * n + p; i++) { n     cout << j <<    ; n     j += k; n     if (j > n) j -= n; n     cout << j << endl; n     cnt++; n     int tmp = j + k; n     if (tmp > n) tmp -= n; n     tmp += k; n     if (tmp > n) tmp -= n; n     if (j == here || tmp == j) { n       if (cnt == n) { n         cnt = 0; n         k++; n         j = 1; n         here = 1; n       } else { n         j++; n         here++; n       } n     } n   } n } n int main() { n   int t; n   cin >> t; n   vector<pair<int, int> > out; n   for (int i = 0; i < t; i++) { n     int n, p; n     cin >> n >> p; n     calc(n, p); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     long long a[n]; n     set<long long> s; n     for (long long i = 0; i < n; i++) { n       cin >> a[i]; n       s.insert(a[i]); n     } n     cout << s.size() <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int_fast64_t viz[100001], ans; n vector<int_fast64_t> v(100001); n void dfs(int_fast64_t node) { n   ans++; n   viz[node] = 1; n   if (!viz[v[node]]) dfs(v[node]); n } n void solve() { n   int_fast64_t n; n   cin >> n; n   for (int_fast64_t i = 1; i <= n; i++) cin >> v[i]; n   vector<int_fast64_t> res; n   for (int_fast64_t i = 1; i <= n; i++) { n     ans = 0; n     if (!viz[i]) dfs(i); n     res.push_back(ans); n   } n   sort(res.begin(), res.end()); n   if (res.size() > 1) { n     int_fast64_t maxi = res[res.size() - 1] + res[res.size() - 2]; n     res.pop_back(); n     res.pop_back(); n     res.push_back(maxi); n     ans = 0; n     for (int_fast64_t i = 0; i < res.size(); i++) ans += res[i] * res[i]; n     cout << ans; n   } else n     cout << n * n; n } n signed main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(0); n   ; n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O2 ) n using namespace std; n void solve() { n   int n; n   cin >> n; n   set<int> s; n   for (int i = 0; i < n; i++) { n     int a; n     cin >> a; n     s.insert(a); n   } n   for (int cnt = 1; cnt <= 1023; cnt++) { n     bool vis[1025]; n     memset(vis, 0, sizeof(vis)); n     bool h = 1; n     for (auto x : s) { n       if (vis[x ^ cnt] || s.count(x ^ cnt) == 0) { n         h = 0; n         break; n       } n       vis[x ^ cnt] = 1; n     } n     if (h) { n       cout << cnt; n       return; n     } n   } n   cout << -1; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     solve(); n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n ifstream fin( A.in ); n ofstream fout( A.out ); n int n, m, a, b, v[100001], viz[100001]; n vector<int> G[100001]; n long long sum; n struct sor { n   int x, i; n } s[100001]; n bool cmp(sor a, sor b) { return a.x > b.x; } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) { n     cin >> v[i]; n     s[i].x = v[i]; n     s[i].i = i; n   } n   for (int i = 1; i <= m; ++i) { n     cin >> a >> b; n     G[a].push_back(b); n     G[b].push_back(a); n   } n   sort(s + 1, s + n + 1, cmp); n   for (int i = 1; i <= n; ++i) { n     viz[s[i].i] = 1; n     for (vector<int>::iterator it = G[s[i].i].begin(); it != G[s[i].i].end(); n          ++it) { n       if (!viz[*it]) sum += v[*it]; n     } n   } n   cout << sum; 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   int N; n   cin >> N; n   vector<int> a(N); n   stack<int> st; n   for (int i = 0; i < N; i++) { n     cin >> a[i]; n   } n   int ma = *max_element(a.begin(), a.end()); n   for (int i = 0; i < N; i++) { n     if (a[i] == ma) continue; n     int j = i; n     while (j < N && a[j] != ma) { n       if (st.empty() || st.top() > a[j]) n         st.push(a[j]); n       else if (!st.empty() && a[j] == st.top()) n         st.pop(); n       else { n         cout <<  NO  << endl; n         return 0; n       } n       j++; n     } n     if (!st.empty()) { n       cout <<  NO  << endl; n       return 0; n     } n     i = j; n   } n   cout <<  YES  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename... As> n struct tpl : public std::tuple<As...> { n   using std::tuple<As...>::tuple; n   tpl() {} n   tpl(std::tuple<As...> const& b) { std::tuple<As...>::operator=(b); } n   template <typename T = tuple<As...> > n   typename tuple_element<0, T>::type const& x() const { n     return get<0>(*this); n   } n   template <typename T = tuple<As...> > n   typename tuple_element<0, T>::type& x() { n     return get<0>(*this); n   } n   template <typename T = tuple<As...> > n   typename tuple_element<1, T>::type const& y() const { n     return get<1>(*this); n   } n   template <typename T = tuple<As...> > n   typename tuple_element<1, T>::type& y() { n     return get<1>(*this); n   } n   template <typename T = tuple<As...> > n   typename tuple_element<2, T>::type const& z() const { n     return get<2>(*this); n   } n   template <typename T = tuple<As...> > n   typename tuple_element<2, T>::type& z() { n     return get<2>(*this); n   } n   template <typename T = tuple<As...> > n   typename tuple_element<3, T>::type const& w() const { n     return get<3>(*this); n   } n   template <typename T = tuple<As...> > n   typename tuple_element<3, T>::type& w() { n     return get<3>(*this); n   } n }; n using lli = long long int; n using llu = long long unsigned; n using pii = tpl<lli, lli>; n using piii = tpl<lli, lli, lli>; n using piiii = tpl<lli, lli, lli, lli>; n using vi = vector<lli>; n using vii = vector<pii>; n using viii = vector<piii>; n using vvi = vector<vi>; n using vvii = vector<vii>; n using vviii = vector<viii>; n template <class T> n using min_queue = priority_queue<T, vector<T>, greater<T> >; n template <class T> n using max_queue = priority_queue<T>; n template <size_t... I> n struct my_index_sequence { n   using type = my_index_sequence; n   static constexpr array<size_t, sizeof...(I)> value = {{I...}}; n }; n namespace my_index_sequence_detail { n template <typename I, typename J> n struct concat; n template <size_t... I, size_t... J> n struct concat<my_index_sequence<I...>, my_index_sequence<J...> > n     : my_index_sequence<I..., (sizeof...(I) + J)...> {}; n template <size_t N> n struct make_index_sequence n     : concat<typename make_index_sequence<N / 2>::type, n              typename make_index_sequence<N - N / 2>::type>::type {}; n template <> n struct make_index_sequence<0> : my_index_sequence<> {}; n template <> n struct make_index_sequence<1> : my_index_sequence<0> {}; n }  // namespace my_index_sequence_detail n template <class... A> n using my_index_sequence_for = n     typename my_index_sequence_detail::make_index_sequence<sizeof...(A)>::type; n template <class T, size_t... I> n void print_tuple(ostream& s, T const& a, my_index_sequence<I...>) { n   using swallow = int[]; n   (void)swallow{0, (void(s << (I == 0 ?    :  ,  ) << get<I>(a)), 0)...}; n } n template <class T> n ostream& print_collection(ostream& s, T const& a); n template <class... A> n ostream& operator<<(ostream& s, tpl<A...> const& a); n template <class... A> n ostream& operator<<(ostream& s, tuple<A...> const& a); n template <class A, class B> n ostream& operator<<(ostream& s, pair<A, B> const& a); n template <class T, size_t I> n ostream& operator<<(ostream& s, array<T, I> const& a) { n   return print_collection(s, a); n } n template <class T> n ostream& operator<<(ostream& s, vector<T> const& a) { n   return print_collection(s, a); n } n template <class T, class U> n ostream& operator<<(ostream& s, multimap<T, U> const& a) { n   return print_collection(s, a); n } n template <class T> n ostream& operator<<(ostream& s, multiset<T> const& a) { n   return print_collection(s, a); n } n template <class T, class U> n ostream& operator<<(ostream& s, map<T, U> const& a) { n   return print_collection(s, a); n } n template <class T> n ostream& operator<<(ostream& s, set<T> const& a) { n   return print_collection(s, a); n } n template <class T> n ostream& print_collection(ostream& s, T const& a) { n   s <<  [ ; n   for (auto it = begin(a); it != end(a); ++it) { n     s << *it; n     if (it != prev(end(a))) s <<    ; n   } n   return s <<  ] ; n } n template <class... A> n ostream& operator<<(ostream& s, tpl<A...> const& a) { n   s <<  ( ; n   print_tuple(s, a, my_index_sequence_for<A...>{}); n   return s <<  ) ; n } n template <class... A> n ostream& operator<<(ostream& s, tuple<A...> const& a) { n   s <<  ( ; n   print_tuple(s, a, my_index_sequence_for<A...>{}); n   return s <<  ) ; n } n template <class A, class B> n ostream& operator<<(ostream& s, pair<A, B> const& a) { n   return s <<  (  << get<0>(a) <<  ,   << get<1>(a) <<  ) ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   lli m, n; n   cin >> n >> m; n   vi A(m); n   for (lli i = 0; i < (lli)(m); ++i) cin >> A[i]; n   lli left = n; n   vi B = A; n   vi cs; n   for (lli i = 0; i < (lli)(m); ++i) { n     if (left < B[i]) { n       B[i] -= left; n       B[i] %= n; n       left = n; n     } n     left -= B[i]; n     if (i != m - 1) cs.emplace_back(n - left); n   } n   cs.push_back(0); n   cs.push_back(n); n   sort(begin(cs), end(cs)); n   vi S; n   for (lli i = 0; i < (lli)(m); ++i) S.push_back(cs[i + 1] - cs[i]); n   int j = 0; n   vvi ans; n   ans.emplace_back(m); n   for (lli i = 0; i < (lli)(m); ++i) { n     while (A[i] > 0) { n       if (j == m) { n         j = 0; n         ans.emplace_back(m); n       } n       A[i] -= S[j]; n       ans.back()[j] = i; n       j++; n     } n   } n   cout << ans.size() <<   n ; n   for (lli i = 0; i < (lli)(m); ++i) cout << S[i] <<    ; n   cout <<   n ; n   for (lli t = 0; t < (lli)(ans.size()); ++t) { n     for (lli j = 0; j < (lli)(m); ++j) cout << ans[t][j] + 1 <<    ; n     cout <<   n ; n   } n   cout << flush; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef int matrix[130][130]; n long long n; n vector<int> v[130]; n int k, q[30], d, f[30], h[130], len[30], r[30], ans; n matrix a, b, t; n char c; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n void mul(matrix a, matrix b) { n   for (int i = 0; i < len[0]; i++) n     for (int j = 0; j < len[0]; j++) { n       t[i][j] = 0; n       for (int k = 0; k < len[0]; k++) n         t[i][j] = (t[i][j] + a[i][k] * b[k][j]) % 12345; n     } n   for (int i = 0; i < len[0]; i++) n     for (int j = 0; j < len[0]; j++) a[i][j] = t[i][j]; n } n void pow(long long n) { n   for (int i = 0; i < len[0]; i++) a[i][i] = 1; n   for (int i = 0; n >> i; i++) { n     if ((n >> i) & 1) mul(a, b); n     mul(b, b); n   } n } n int main() { n   scanf( %I64d%d , &n, &k); n   for (int i = 0; i < 26; i++) q[i] = 1; n   for (int i = 0; i < k; i++) n     scanf(  %c%d , &c, &d), c -=  A , q[c] *= d / gcd(d, q[c]), n         f[c] = 1, v[c].push_back(d); n   len[26] = 1; n   for (int j = 25; j >= 0; j--) len[j] = len[j + 1] * q[j]; n   for (int j = 0; j < len[0]; j++) { n     for (int i = 0; i < 26; i++) r[i] = j / len[i + 1] % q[i]; n     h[j] = 1; n     for (int i = 0; i < 26; i++) n       if (v[i].size()) { n         bool fg = 0; n         for (int d : v[i]) fg |= (r[i] % d == 0); n         h[j] &= fg; n       } n     for (int i = 0; i < 26; i++) n       if (f[i]) n         b[j][(r[i] + 1 == q[i]) ? j + len[i + 1] - len[i] : j + len[i + 1]]++; n   } n   pow(n); n   for (int i = 0; i < len[0]; i++) ans += h[i] * a[0][i]; n   printf( %d n , ans % 12345); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 7; n int n; n int cnt; n int sum; n int head[maxn]; n int size[maxn]; n int T[maxn]; n int top; n int temp[2]; n struct edge { n   int u, v, w, next; n } e[maxn * 2]; n void addedge(int u, int v, int w) { n   e[cnt].u = u; n   e[cnt].v = v; n   e[cnt].w = w; n   e[cnt].next = head[u]; n   head[u] = cnt++; n } n void add_edge(int u, int v, int w) { n   addedge(u, v, w); n   addedge(v, u, w); n } n void dfs(int u, int f) { n   size[u] = T[u]; n   for (int i = head[u]; ~i; i = e[i].next) { n     int v = e[i].v; n     if (v != f) { n       dfs(v, u); n       size[u] += size[v]; n     } n   } n   if (size[u] * 3 == sum && u != e[head[0]].v) { n     temp[top++] = u; n     size[u] = 0; n   } n } n int main() { n   cnt = 0; n   memset(head, -1, sizeof(head)); n   scanf( %d , &n); n   int a, b; n   sum = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &a, &b); n     add_edge(a, i, 1); n     T[i] = b; n     sum += b; n   } n   if (sum % 3) { n     cout << -1 << endl; n   } else { n     top = 0; n     temp[0] = -1; n     temp[1] = -1; n     dfs(e[head[0]].v, 0); n     if (temp[0] != -1 && temp[1] != -1) { n       cout << temp[0] <<     << temp[1] << endl; n     } else { n       cout << -1 << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2000 + 10; n int a[maxn][maxn]; n vector<int> G[maxn], R[maxn], topo; n bool v[maxn]; n void rdfs(int now) { n   v[now] = true; n   for (int u : R[now]) n     if (!v[u]) rdfs(u); n   topo.push_back(now); n } n void dfs(int now) { n   v[now] = true; n   for (int u : G[now]) n     if (!v[u]) dfs(u); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= n; ++j) { n       cin >> a[i][j]; n       if (a[i][j]) G[i].push_back(j), R[j].push_back(i); n     } n   for (int i = 1; i <= n; ++i) n     if (!v[i]) rdfs(i); n   reverse(topo.begin(), topo.end()); n   memset(v, false, sizeof(v)); n   dfs(topo[0]); n   for (int i = 1; i <= n; ++i) n     if (!v[i]) return cout <<  NO  << endl, 0; n   cout <<  YES  << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse4 ) n using namespace std; n int n; n int arr[8005]; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     cin >> n; n     multiset<int> ms; n     for (int i = 0; i < n; i++) { n       cin >> arr[i]; n       ms.insert(arr[i]); n     } n     int ans = 0; n     for (int i = 0; i < n; i++) { n       int sum = arr[i]; n       for (int j = i + 1; j < n; j++) { n         sum += arr[j]; n         while (ms.find(sum) != ms.end()) { n           ans++; n           ms.erase(ms.find(sum)); n         } n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> v[2]; n long long get(long long a, long long b) { return (a + b != 0); } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   string s; n   long long ans, x, y, a, b, c, d; n   long long minx, maxx, miny, maxy, maxl, maxr, maxu, maxd; n   while (t--) { n     cin >> s; n     x = 0; n     y = 0; n     minx = 0; n     maxx = 0; n     miny = 0; n     maxy = 0; n     maxl = 0; n     maxr = 0; n     maxu = 0; n     maxd = 0; n     for (auto it : s) { n       if (it ==  W ) y++; n       if (it ==  S ) y--; n       if (it ==  A ) x--; n       if (it ==  D ) x++; n       maxx = max(maxx, x); n       minx = min(minx, x); n       maxy = max(maxy, y); n       miny = min(miny, y); n       maxu = max(maxu, y - miny); n       maxd = max(maxd, maxy - y); n       maxr = max(maxr, x - minx); n       maxl = max(maxl, maxx - x); n     } n     a = max(maxl, maxr); n     b = max(get(maxl, maxr), a - (maxl != maxr)); n     c = max(maxu, maxd); n     d = max(get(maxu, maxd), c - (maxu != maxd)); n     a++; n     b++; n     c++; n     d++; n     ans = min(a * d, b * c); n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 1020; n int n, m, d, tmp; n int h[maxn], dis1[maxn], dis2[maxn]; n bool mark[maxn], isaffected[maxn]; n vector<int> adj[maxn]; n pair<int, int> temp; n void dfsa(int v) { n   for (int i = 0; i < adj[v].size(); i++) { n     int u = adj[v][i]; n     if (!mark[u]) { n       mark[u] = true; n       h[u] = h[v] + 1; n       dfsa(u); n       if (h[u] > tmp && isaffected[u]) { n         tmp = h[u]; n         temp.first = u; n       } n     } n   } n } n void dfsb(int v) { n   for (int i = 0; i < adj[v].size(); i++) { n     int u = adj[v][i]; n     if (!mark[u]) { n       mark[u] = true; n       h[u] = h[v] + 1; n       dfsb(u); n       if (h[u] > tmp && isaffected[u]) { n         tmp = h[u]; n         temp.second = u; n       } n     } n   } n } n void dfs1(int v) { n   for (int i = 0; i < adj[v].size(); i++) { n     int u = adj[v][i]; n     if (!mark[u]) { n       mark[u] = true; n       dis1[u] = dis1[v] + 1; n       dfs1(u); n     } n   } n } n void dfs2(int v) { n   for (int i = 0; i < adj[v].size(); i++) { n     int u = adj[v][i]; n     if (!mark[u]) { n       mark[u] = true; n       dis2[u] = dis2[v] + 1; n       dfs2(u); n     } n   } n } n int main() { n   cin >> n >> m >> d; n   for (int i = 1; i <= m; i++) { n     int x; n     cin >> x; n     isaffected[x] = true; n   } n   for (int i = 1; i < n; i++) { n     int a, b; n     cin >> a >> b; n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   mark[1] = true; n   dfsa(1); n   for (int i = 1; i <= n; i++) { n     h[i] = 0; n     mark[i] = false; n   } n   h[temp.first] = 0; n   tmp = 0; n   mark[temp.first] = true; n   dfsb(temp.first); n   for (int i = 1; i <= n; i++) { n     mark[i] = false; n   } n   dis1[temp.first] = 0; n   mark[temp.first] = true; n   dfs1(temp.first); n   for (int i = 1; i <= n; i++) { n     mark[i] = false; n   } n   dis2[temp.second] = 0; n   mark[temp.second] = true; n   dfs2(temp.second); n   for (int i = 1; i <= n; i++) { n     mark[i] = false; n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     if (max(dis1[i], dis2[i]) <= d) { n       ans++; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int c, v0, v1, a, l; n int main() { n   scanf( %d %d %d %d %d , &c, &v0, &v1, &a, &l); n   int s = 0, d = v0 - a; n   for (int i = 1;; i++) { n     d += a; n     if (d >= v1) d = v1; n     if (i == 1) n       s += d; n     else n       s += d - l; n     if (s >= c) { n       printf( %d n , i); n       return 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n std::pair<T, U> operator+(const std::pair<T, U>& l, const std::pair<T, U>& r) { n   return {l.first + r.first, l.second + r.second}; n } n typedef void (*callback_function)(void); n const long long ZERO = 0LL; n const long long INF64 = 1e18; n const long long INF32 = 1e9; n const long long MOD = 1e9 + 7; n const long double PI = acos(-1.0L); n const long double EPS = static_cast<long double>(1e-9); n inline long long Pow(long long a, long long k) { n   long long s = 1; n   for (; k; k >>= 1) { n     k& 1 ? s = 1LL * s * a % MOD : 0; n     a = 1LL * a * a % MOD; n   } n   return s; n } n const long long N = 1e6 + 7; n void input() {} n void preprocess() {} n void debug() { n   if (true) { n   } n } n void solve() { n   preprocess(); n   long long n, m; n   cin >> n >> m; n   long long a[n]; n   for (long long i = (0); i < (n); i++) { n     cin >> a[i]; n   } n   for (long long i = (0); i < (m); i++) { n     long long L, R, x; n     cin >> L >> R >> x; n     L--; n     x--; n     long long y = L; n     for (long long j = (L); j < (R); j++) { n       if (a[j] < a[x]) { n         y++; n       } n     } n     cout << (y == x ?  Yes  :  No ) << endl; n   } n } n void output() {} n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(); n   { n     input(); n     solve(); n     output(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> adj[500005]; n vector<pair<long long, long long>> vc[500005]; n string s; n bool ans[500005], hv[500005]; n long long level[500005], sz[500005], heavy[500005], cnt[500005], col[500005]; n void dfs(long long u, long long p, long long l = 1) { n   sz[u] = 1, heavy[u] = -1, level[u] = l; n   for (long long i = 0; i < adj[u].size(); i++) { n     long long v = adj[u][i]; n     if (v == p) continue; n     dfs(v, u, l + 1); n     sz[u] += sz[v]; n     if (heavy[u] == -1 || sz[v] > sz[heavy[u]]) heavy[u] = v; n   } n } n void Add(long long u, long long p) { n   cnt[level[u]] ^= (1 << col[u]); n   for (long long i = 0; i < adj[u].size(); i++) { n     long long v = adj[u][i]; n     if (v == p || hv[v]) continue; n     Add(v, u); n   } n } n void dsu(long long u, long long p, bool flg) { n   for (long long i = 0; i < adj[u].size(); i++) { n     long long v = adj[u][i]; n     if (v == p || v == heavy[u]) continue; n     dsu(v, u, 0); n   } n   if (~heavy[u]) dsu(heavy[u], u, 1), hv[heavy[u]] = 1; n   Add(u, p); n   for (auto v : vc[u]) { n     long long ht = v.first; n     long long id = v.second; n     if (__builtin_popcountll(cnt[ht]) <= 1) ans[id] = 1; n   } n   if (~heavy[u]) hv[heavy[u]] = 0; n   if (flg == 0) Add(u, p); n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long i, j, k, p, q, m, n; n   cin >> n >> m; n   for (i = 2; i <= n; i++) { n     cin >> p; n     adj[p].push_back(i); n     adj[i].push_back(p); n   } n   cin >> s; n   for (i = 0; i < s.size(); i++) { n     col[i + 1] = s[i] -  a ; n   } n   for (i = 1; i <= m; i++) { n     cin >> p >> q; n     vc[p].push_back({q, i}); n   } n   dfs(1, -1); n   dsu(1, -1, 0); n   for (i = 1; i <= m; i++) { n     if (ans[i]) n       cout <<  Yes  n            <<   n ; n     else n       cout <<  No  n            <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using llu = unsigned long long; n using ld = long double; n const double PI = acos(-1.0), g = 9.80665; n const int unseen = -1; n const ll inf = 1e18 + 505, ninf = -1e18 - 505; n const int MOD = 1000000007; n void solve(void) { n   int n, p; n   cin >> n >> p; n   int vrtc = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = i + 1; j <= n; j++) { n       cout << i <<     << j <<   n ; n       vrtc++; n       if (vrtc == (2 * n + p)) return; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int testcase = 1; n   cin >> testcase; n   for (int tc = 1; tc <= testcase; tc++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void read(int &x) { n   x = 0; n   int f = 1; n   char ch = getchar(); n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) f = -f; n   for (; isdigit(ch); ch = getchar()) x = x * 10 + ch -  0 ; n   x *= f; n } n void print(int x) { n   if (x < 0) putchar( - ), x = -x; n   if (!x) return; n   print(x / 10), putchar(x % 10 + 48); n } n void write(int x) { n   if (!x) n     putchar( 0 ); n   else n     print(x); n   putchar(  n ); n } n const int maxn = 1e6 + 10; n const int inf = 1e9; n const double eps = 1e-8; n const int mod = 1e9 + 7; n char s[maxn]; n vector<vector<int> > v; n stack<int> a, b; n int n; n int main() { n   scanf( %s , s + 1); n   n = strlen(s + 1); n   for (int i = 1; i <= n; i++) n     if (s[i] ==  1 ) { n       if (a.empty()) return puts( -1 ), 0; n       int x = a.top(); n       a.pop(); n       v[x].push_back(i); n       b.push(x); n     } else { n       if (b.empty()) n         v.push_back(vector<int>{i}), a.push(v.size() - 1); n       else { n         int x = b.top(); n         b.pop(); n         v[x].push_back(i); n         a.push(x); n       } n     } n   if (b.size()) return puts( -1 ), 0; n   write(v.size()); n   for (auto x : v) { n     printf( %d  , x.size()); n     for (auto a : x) printf( %d  , a); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:640000000 ) n using namespace std; n const long long INF = (1e15) + 4; n const int N = 1e6 + 5; n const int K = 26; n int a[N]; n int t[N]; n int n; n int nxt[N]; n int c[N]; n int sum(int r) { n   int result = 0; n   for (; r >= 0; r = (r & (r + 1)) - 1) result ^= t[r]; n   return result; n } n void inc(int i, int d) { n   for (; i < n; i = (i | (i + 1))) t[i] ^= d; n } n vector<pair<int, int>> z[N]; n int answer[N]; n int get_ans(int l, int r) { n   int answer1 = c[r]; n   if (l > 0) answer1 ^= c[l - 1]; n   int answer2 = sum(r); n   return (answer1 ^ answer2); n } n int main() { n   scanf( %d , &n); n   map<int, int> b; n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     nxt[i] = -1; n     c[i] = a[i]; n     if (i) c[i] ^= c[i - 1]; n   } n   for (int i = n - 1; i >= 0; i--) { n     if (b.count(a[i])) nxt[i] = b[a[i]]; n     b[a[i]] = i; n   } n   int m; n   scanf( %d , &m); n   for (int i = 0; i < m; i++) { n     int l, r; n     scanf( %d %d , &l, &r); n     l--; n     r--; n     z[l].push_back(make_pair(r, i)); n   } n   set<int> s; n   for (int i = 0; i < n; i++) { n     if (s.count(a[i]) == 0) inc(i, a[i]); n     s.insert(a[i]); n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < z[i].size(); j++) { n       answer[z[i][j].second] = get_ans(i, z[i][j].first); n     } n     inc(i, a[i]); n     if (nxt[i] != -1) inc(nxt[i], a[i]); n   } n   for (int i = 0; i < m; i++) { n     printf( %d n , answer[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace Flandle_Scarlet { n int n; n int a[51][51]; n char tmp[51]; n int s[51][51]; n void Input() { n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     scanf( %s , tmp); n     for (int j = 1; j <= n; ++j) { n       if (tmp[j - 1] ==  # ) n         a[i][j] = 1; n       else n         a[i][j] = 0; n     } n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= n; ++j) { n       s[i][j] = s[i][j - 1] + s[i - 1][j] - s[i - 1][j - 1] + a[i][j]; n     } n   } n } n int RSum(int a1, int b1, int a2, int b2) { n   return s[a2][b2] - s[a1 - 1][b2] - s[a2][b1 - 1] + s[a1 - 1][b1 - 1]; n } n int dp[51][51][51][51]; n int DFS(int a1, int b1, int a2, int b2) { n   if (dp[a1][b1][a2][b2]) { n     return dp[a1][b1][a2][b2]; n   } n   if (a1 == a2 and b1 == b2) { n     dp[a1][b1][a2][b2] = a[a1][b1]; n     return dp[a1][b1][a2][b2]; n   } n   if (RSum(a1, b1, a2, b2) == 0) { n     return (dp[a1][b1][a2][b2] = 0); n   } n   dp[a1][b1][a2][b2] = max(a2 - a1 + 1, b2 - b1 + 1); n   for (int i = a1; i < a2; ++i) { n     int newans = DFS(a1, b1, i, b2) + DFS(i + 1, b1, a2, b2); n     dp[a1][b1][a2][b2] = min(dp[a1][b1][a2][b2], newans); n   } n   for (int i = b1; i < b2; ++i) { n     int newans = DFS(a1, b1, a2, i) + DFS(a1, i + 1, a2, b2); n     dp[a1][b1][a2][b2] = min(dp[a1][b1][a2][b2], newans); n   } n   return dp[a1][b1][a2][b2]; n } n void Soviet() { n   DFS(1, 1, n, n); n   printf( %d n , dp[1][1][n][n]); n } n void IsMyWife() { n   if (0) { n     freopen(  ,  r , stdin); n     freopen(  ,  w , stdout); n   } n   Input(); n   Soviet(); n } n };  // namespace Flandle_Scarlet n int main() { n   Flandle_Scarlet::IsMyWife(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2005; n const long long inf = 1e18 + 5; n long long n, m, x; n long long a[maxn], b[maxn]; n long long ma[maxn * maxn], mb[maxn * maxn]; n void input() { n   cin >> n >> m; n   for (long long i = 1; i <= n; ++i) { n     cin >> a[i]; n     a[i] += a[i - 1]; n     ma[i] = inf; n   } n   for (long long i = 1; i <= m; ++i) { n     cin >> b[i]; n     b[i] += b[i - 1]; n     mb[i] = inf; n   } n   cin >> x; n } n void slove() { n   for (long long i = 1; i <= n; ++i) { n     for (long long j = 0; j < i; ++j) { n       ma[i - j] = min(ma[i - j], a[i] - a[j]); n     } n   } n   for (long long i = 1; i <= m; ++i) { n     for (long long j = 0; j < i; ++j) { n       mb[i - j] = min(mb[i - j], b[i] - b[j]); n     } n   } n   long long ans = 0; n   for (long long i = 1; i <= n; ++i) { n     for (long long j = 1; j <= m; ++j) { n       if (ma[i] * mb[j] <= x) { n         ans = max(ans, i * j); n       } n     } n   } n   cout << ans << endl; n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   input(); n   slove(); n   return 0; n } n


#include <bits/stdc++.h> n const int mod = 1000000007; n const int inf = 1000000009; n const long long INF = 1000000000000000009; n const long long big = 1000000000000000; n const long double eps = 1e-9; n using namespace std; n struct odcinek { n   int l, x, r, nr; n }; n int suma = 1; n odcinek T[100005]; n int D[1100005], M[1100005], S[1100005]; n bool cmp(odcinek a, odcinek b) { return a.x < b.x; } n void lazy(int u) { n   if (2 * u + 1 <= suma) { n     D[2 * u] += D[u]; n     D[2 * u + 1] += D[u]; n   } n   M[u] += D[u]; n   D[u] = 0; n } n void dodaj(int a, int b, int lo, int hi, int u, int c) { n   if (b <= a) return; n   lazy(u); n   if (a == lo && b == hi) { n     D[u] += c; n     return; n   } n   int mid = (lo + hi) / 2; n   dodaj(a, min(b, mid), lo, mid, 2 * u, c); n   dodaj(max(a, mid), b, mid, hi, 2 * u + 1, c); n   if (M[2 * u] + D[2 * u] >= M[2 * u + 1] + D[2 * u + 1]) { n     M[u] = M[2 * u] + D[2 * u]; n     S[u] = S[2 * u]; n   } else { n     M[u] = M[2 * u + 1] + D[2 * u + 1]; n     S[u] = S[2 * u + 1]; n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(); n   cout.tie(); n   int n; n   cin >> n; n   while (suma < 300000) suma *= 2; n   suma *= 2; n   suma--; n   for (int i = suma / 2 + 1; i <= suma; i++) S[i] = i - suma / 2; n   for (int i = suma / 2; i >= 1; i--) S[i] = S[2 * i]; n   for (int i = 1; i <= n; i++) { n     int a, b, c; n     cin >> a >> b >> c; n     T[i] = {a, b, c, i}; n   } n   sort(T + 1, T + n + 1, cmp); n   priority_queue<pair<int, int> > Q; n   int wynik = 0; n   pair<int, int> skad; n   for (int i = 1; i <= n; i++) { n     while (!Q.empty() && -Q.top().first < T[i].x) { n       pair<int, int> x = Q.top(); n       dodaj(T[x.second].l, T[x.second].x + 1, 1, suma / 2 + 2, 1, -1); n       Q.pop(); n     } n     dodaj(T[i].l, T[i].x + 1, 1, suma / 2 + 2, 1, 1); n     lazy(1); n     if (M[1] > wynik) { n       wynik = M[1]; n       skad = make_pair(S[1], T[i].x); n     } n     Q.push(make_pair(-T[i].r, i)); n   } n   cout << wynik <<   n ; n   for (int i = 1; i <= n; i++) { n     if (T[i].l <= skad.first && T[i].r >= skad.second && T[i].x >= skad.first && n         T[i].x <= skad.second) n       cout << T[i].nr <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   int c = 0; n   int a[2 * m]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < 2 * m; j++) cin >> a[j]; n     for (int j = 0; j < 2 * m - 1; j += 2) { n       if (a[j] == 1 || a[j + 1] == 1) c++; n     } n   } n   cout << c << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n = 0, ans = 0, mod = 1000000009LL; n   scanf( %I64d , &n); n   if (n >= 5) { n     vector<long long> lev(n + 1, 0); n     if (n >= 3) lev[3] = 1; n     if (n >= 4) lev[4] = 1; n     if (n >= 5) lev[5] = 5; n     for (int i = 6; i <= n; i++) n       if (i % 2 == 0) n         lev[i] = 1; n       else n         lev[i] = (2 * lev[i - 2] + 3) % mod; n     long long dob = 1; n     for (int i = 5; i <= n; i++) n       if (lev[i] > 1) { n         dob = (dob * lev[i]) % mod; n         ans = (ans + 4 * dob) % mod; n       } n     ans = (ans + 6) % mod; n     ans = (ans * ans) % mod; n     ans = (ans + mod - 4) % mod; n     ans = (2 * ans) % mod; n     ans = (ans + 10) % mod; n     printf( %I64d n , ans); n   } n   if (n == 1) printf( 2 n ); n   if (n == 2) printf( 10 n ); n   if (n == 3) printf( 34 n ); n   if (n == 4) printf( 74 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long b, long long y) { return b > 0 ? gcd(y % b, b) : y; } n long long lcm(long long b, long long y) { return b * y / gcd(b, y); } n double dis(double x1, double x2, double y1, double y2) { n   double ans = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); n   return sqrt(ans); n } n double random(double start, double en) { n   return start + (en - start) * rand() / (RAND_MAX + 1.0); n } n map<int, vector<int> > a; n map<int, int> b; n map<int, vector<int> >::iterator it; n void dfs(int x) { n   if (b[x]) return; n   b[x] = 1; n   cout << x <<    ; n   for (int i = 0; i < a[x].size(); i++) { n     dfs(a[x][i]); n   } n   return; n } n int main() { n   int n; n   while (cin >> n) { n     for (int i = 1; i <= n; i++) { n       int x, y; n       cin >> x >> y; n       a[x].push_back(y); n       a[y].push_back(x); n     } n     int t; n     for (it = a.begin(); it != a.end(); it++) { n       if (it->second.size() == 1) { n         t = it->first; n         break; n       } n     } n     dfs(t); n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<string> split(const string& s, char c) { n   vector<string> v; n   stringstream ss(s); n   string first; n   while (getline(ss, first, c)) v.emplace_back(first); n   return move(v); n } n void err(vector<string>::iterator it) {} n template <typename T, typename... Args> n void err(vector<string>::iterator it, T a, Args... args) { n   cerr << a <<    ; n   err(++it, args...); n } n const int M = 5000100, mod = 1000000007; n struct segment_tree { n   pair<int, int> T[M]; n   segment_tree() {} n   pair<int, int> find_min(pair<int, int> a, pair<int, int> b) { n     return a.first < b.first ? a : b; n   } n   void build(int* A, int i, int l, int r) { n     if (l == r) n       T[i] = make_pair(A[l], l); n     else { n       int m = (l + r) / 2; n       build(A, 2 * i, l, m); n       build(A, 2 * i + 1, m + 1, r); n       T[i] = find_min(T[2 * i], T[2 * i + 1]); n     } n   } n   void update(int i, int l, int r, int first) { n     if (l == r && r == first) n       T[i] = make_pair(1111111111, l); n     else if (first >= l && first <= r) { n       int m = (l + r) / 2; n       update(2 * i, l, m, first); n       update(2 * i + 1, m + 1, r, first); n       T[i] = find_min(T[2 * i], T[2 * i + 1]); n     } n   } n   pair<int, int> query(int i, int l, int r, int st, int ed) { n     if (l >= st && r <= ed) n       return T[i]; n     else if (l > ed || r < st) n       return make_pair(1111111111, -1); n     else { n       int m = (l + r) / 2; n       return find_min(query(2 * i, l, m, st, ed), n                       query(2 * i + 1, m + 1, r, st, ed)); n     } n   } n }; n struct BIT { n   vector<int> tree; n   BIT(int n) { tree.resize(n); } n   void update(int idx, int val) { n     while (idx < tree.size()) { n       tree[idx] += val; n       idx += idx & (-idx); n     } n   } n   int query(int idx) { n     int sum = 0; n     while (idx > 0) { n       sum += tree[idx]; n       idx -= idx & (-idx); n     } n     return sum; n   } n }; n int A[M], spec[M], n; n segment_tree tree; n BIT bit(M); n long long solve(int l, int r) { n   long long sum = 0; n   while (l <= r) { n     pair<int, int> temp = tree.query(1, 1, n, l, r); n     if (temp.first == 1111111111) n       break; n     else if (spec[temp.first]) { n       tree.update(1, 1, n, temp.second); n       bit.update(temp.second, -1); n       sum += solve(l, temp.second - 1); n       sum += solve(temp.second + 1, r); n       break; n     } else { n       sum += bit.query(r) - bit.query(l - 1); n       tree.update(1, 1, n, temp.second); n       bit.update(temp.second, -1); n     } n   } n   return sum; n } n int main() { n   ios_base::sync_with_stdio(false); n   int i, j, k, m; n   memset((spec), (false), sizeof(spec)); n   scanf( %d%d , &n, &m); n   for (i = 1; i <= n; i++) scanf( %d , &A[i]); n   for (j = 1; j <= m; j++) scanf( %d , &k), spec[k] = true; n   tree.build(A, 1, 1, n); n   for (i = 1; i <= n; i++) bit.update(i, 1); n   cout << solve(1, n) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long solve(long long h, long long n, bool left) { n   if (h == 1) return left ? n : 3 - n; n   long long p = 1ll << (h - 1); n   if (left) { n     if (n > p) n       return ((long long)1 << h) + solve(h - 1, n - p, left); n     else n       return 1 + solve(h - 1, n, false); n   } else { n     if (n > p) n       return 1 + solve(h - 1, n - p, true); n     else n       return ((long long)1 << h) + solve(h - 1, n, false); n   } n   return -1; n } n int main() { n   int h; n   cin >> h; n   long long n; n   cin >> n; n   cout << solve(h, n, true) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct edge_t { n   int u, dist; n }; n struct firstk_sum_t { n   multiset<long long> left, right; n   long long left_sum = 0LL; n   void insert(long long value) { n     if (!left.empty() && value <= *left.rbegin()) { n       left.insert(value); n       left_sum += value; n     } else { n       right.insert(value); n     } n   } n   void erase(long long value) { n     if (!left.empty() && value <= *left.rbegin()) { n       auto it = left.find(value); n       if (it != left.end()) { n         left_sum -= *it; n         left.erase(it); n       } n     } else { n       auto it = right.find(value); n       if (it != right.end()) right.erase(it); n     } n   } n   long long get_sum(int k) { n     while (!left.empty() && (int)left.size() > k) { n       auto it = left.end(); n       --it; n       right.insert(*it); n       left_sum -= *it; n       left.erase(it); n     } n     while (!right.empty() && (int)left.size() < k) { n       auto it = right.begin(); n       left.insert(*it); n       left_sum += *it; n       right.erase(it); n     } n     return left_sum; n   } n }; n struct res_t { n   int parent_cost; n   vector<long long> with; n   vector<long long> without_override; n   res_t *dup_prefix(int size) { n     res_t *result = new res_t; n     result->parent_cost = parent_cost; n     result->with = vector<long long>( n         with.begin(), with.begin() + min(size, (int)with.size())); n     result->without_override = vector<long long>( n         without_override.begin(), n         without_override.begin() + min(size, (int)without_override.size())); n     return result; n   } n   void set_without(int i, long long value) { n     int new_size = i + 1; n     without_override.reserve(new_size); n     while ((int)without_override.size() < new_size) { n       without_override.push_back(with[without_override.size()]); n     } n     without_override[i] = value; n   } n   long long get_with(int i) { n     if (i < (int)with.size()) n       return with[i]; n     else n       return 0LL; n   } n   long long get_without(int i) { n     if (i < (int)without_override.size()) n       return without_override[i]; n     else if (i < (int)with.size()) n       return with[i]; n     else n       return 0LL; n   } n }; n res_t *solve(int v, int p, int pc, const vector<vector<edge_t>> &adj) { n   vector<res_t *> children; n   for (const edge_t &ed : adj[v]) n     if (ed.u != p) children.push_back(solve(ed.u, v, ed.dist, adj)); n   if (children.empty()) { n     res_t *result = new res_t; n     result->parent_cost = pc; n     result->with = {LLONG_MAX}; n     result->without_override = {0LL}; n     return result; n   } else { n     sort(children.begin(), children.end(), n          [](const res_t *lhs, const res_t *rhs) { n            return lhs->with.size() > rhs->with.size(); n          }); n     int compute_until = (int)children.size() + 2; n     if ((int)children.size() >= 2) n       compute_until = max(compute_until, (int)children[1]->with.size()); n     res_t *children0start = children[0]->dup_prefix(compute_until); n     res_t *result = children[0]; n     children[0] = children0start; n     firstk_sum_t first_k; n     vector<long long> children_value(children.size()); n     int good_to_remove = 0; n     for (int i = 0; i < (int)children.size(); ++i) { n       children_value[i] = children[i]->parent_cost; n       good_to_remove += (children_value[i] < 0); n       first_k.insert(children_value[i]); n     } n     result->with.resize(max((int)result->with.size(), compute_until)); n     result->with[0] = LLONG_MAX; n     for (int j = compute_until - 1; j >= 1; --j) { n       result->with[j] = 0LL; n       for (int i = 0; i < (int)children.size(); ++i) { n         if ((int)children[i]->with.size() <= j) break; n         result->with[j] += children[i]->get_with(j); n       } n       int max_children = j - 1; n       int nremove = (int)children.size() - max_children; n       for (int i = 0; i < (int)children.size(); ++i) { n         if ((int)children[i]->with.size() <= j) break; n         first_k.erase(children_value[i]); n         good_to_remove -= (children_value[i] < 0); n         children_value[i] = children[i]->get_without(j) - n                             children[i]->get_with(j) + children[i]->parent_cost; n         good_to_remove += (children_value[i] < 0); n         first_k.insert(children_value[i]); n       } n       nremove = max(nremove, good_to_remove); n       long long total_cost = first_k.get_sum(nremove); n       result->with[j] += total_cost; n     } n     result->without_override.clear(); n     first_k.left.clear(); n     first_k.right.clear(); n     first_k.left_sum = 0LL; n     good_to_remove = 0; n     for (int i = 0; i < (int)children.size(); ++i) { n       children_value[i] = children[i]->parent_cost; n       good_to_remove += (children_value[i] < 0); n       first_k.insert(children_value[i]); n     } n     long long without0 = 0LL; n     for (int i = 0; i < (int)children.size(); ++i) { n       without0 += children[i]->get_without(0); n       without0 += children[i]->parent_cost; n     } n     result->set_without(0, without0); n     for (int j = compute_until - 1; j >= 1; --j) { n       long long children_cost = 0LL; n       for (int i = 0; i < (int)children.size(); ++i) { n         if ((int)children[i]->with.size() <= j) break; n         children_cost += children[i]->get_with(j); n       } n       int max_children = j; n       int nremove = (int)children.size() - max_children; n       for (int i = 0; i < (int)children.size(); ++i) { n         if ((int)children[i]->with.size() <= j) break; n         first_k.erase(children_value[i]); n         good_to_remove -= (children_value[i] < 0); n         children_value[i] = children[i]->get_without(j) - n                             children[i]->get_with(j) + children[i]->parent_cost; n         good_to_remove += (children_value[i] < 0); n         first_k.insert(children_value[i]); n       } n       nremove = max(nremove, good_to_remove); n       long long total_cost = first_k.get_sum(nremove); n       result->set_without(j, children_cost + total_cost); n     } n     if (pc != INT_MAX) { n       for (int j = 1; j < compute_until; ++j) { n         result->with[j] = n             min(result->with[j], result->without_override[j] + pc); n       } n     } n     result->parent_cost = pc; n     return result; n   } n } n int main() { n   int n; n   scanf( %d , &n); n   vector<vector<edge_t>> adj(n); n   for (int i = 0; i < n - 1; ++i) { n     int a, b, c; n     scanf( %d %d %d , &a, &b, &c), --a, --b; n     adj[a].push_back({b, c}); n     adj[b].push_back({a, c}); n   } n   res_t *result = solve(0, -1, INT_MAX, adj); n   for (int i = 0; i < n; ++i) { n     if (i > 0) printf(   ); n     printf( %lld , result->get_without(i)); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 233; n int a[N][N]; n int main() { n   ios::sync_with_stdio(false); n   int m, n, q, p, x, y, b; n   cin >> n >> m; n   for (int i = 0; i < n; ++i) cin >> a[i][0]; n   p = a[0][0]; n   for (int i = 0; i < m; ++i) cin >> a[0][i]; n   q = a[0][0]; n   x = y = b = 0; n   for (int i = 1; i < n; ++i) x ^= a[i][0]; n   for (int i = 1; i < m; ++i) y ^= a[0][i]; n   if ((y ^ p) != (x ^ q)) { n     printf( NO n ); n     return 0; n   } else { n     b = y ^ p; n     a[0][0] = b; n     printf( YES n ); n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < m; ++j) printf( %d  , a[i][j]); n       printf(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n void Letter() { n   char str[100000 + 1]; n   int Left[100000 + 1], Right[100000 + 1]; n   int i, j; n   int Len; n   scanf( %s , str); n   Len = strlen(str); n   Left[0] = Right[Len - 1] = 0; n   for (i = 1; i < Len; i++) n     if (str[i - 1] <=  z  && str[i - 1] >=  a ) n       Left[i] = Left[i - 1] + 1; n     else n       Left[i] = Left[i - 1]; n   for (i = Len - 2; i >= 0; i--) n     if (str[i + 1] <=  Z  && str[i + 1] >=  A ) n       Right[i] = Right[i + 1] + 1; n     else n       Right[i] = Right[i + 1]; n   int min = INT_MAX; n   for (i = 0; i < Len; i++) n     if (min > Left[i] + Right[i]) min = Left[i] + Right[i]; n   printf( %d n , min); n } n int main() { n   Letter(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, cnt; n int po[999994], cr[14]; n map<int, int> mert; n void calc(int L) { n   int x = 0; n   for (int i = 1; i <= L; i++) x = x * 10 + cr[i]; n   if (x == 0) n     return; n   else if (!mert.count(x)) { n     mert[x] = 1; n     po[++cnt] = x; n   } n } n void dfs(int cur, int limit) { n   if (cur > limit) n     calc(limit); n   else n     for (int i = 0; i <= 1; i++) { n       cr[cur] = i; n       dfs(cur + 1, limit); n     } n } n int main() { n   cin >> n; n   for (int i = 1; i <= 10; i++) dfs(1, i); n   sort(po + 1, po + cnt + 1); n   int i = 0; n   for (i = 1; po[i] <= n; i++) n     ; n   cout << i - 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, count(0); n   cin >> n >> m; n   for (int a = 0; a * a <= n; a++) { n     for (int b = 0; b * b <= m; b++) { n       if (a * a + b == n && a + b * b == m) count++; n     } n   } n   cout << count << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( inline ) n using namespace std; n const int N = 405; n const int MOD = 1000000007; n int T, lim, n, k, l, p, cnt, id[N]; n long long C[N][N], a[N], dp[N][N], fac[N] = {1}; n vector<int> G[N]; n bool Judge(long long x) { n   long long k = sqrt(x); n   return k * k == x; n } n void init() { n   C[0][0] = 1LL; n   for (int i = 1; i <= 350; i++) { n     C[i][0] = C[i][i] = 1; n     for (int j = 1; j < i; j++) C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD; n     fac[i] = fac[i - 1] * i % MOD; n   } n   id[1] = cnt = 1, G[1].push_back(1); n   for (int i = 2; i <= n; i++) { n     bool flag = true; n     for (int j = i - 1; j >= 1; j--) n       if (Judge(a[j] * a[i])) { n         G[id[j]].push_back(i); n         id[i] = id[j]; n         flag = false; n         break; n       } n     if (flag) { n       id[i] = ++cnt; n       G[cnt].push_back(i); n     } n   } n } n signed main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %lld , a + i); n   init(); n   dp[1][(int)G[1].size() - 1] = 1; n   lim = (int)G[1].size(); n   for (int i = 2; i <= cnt; i++) { n     int sz = G[i].size(); n     for (int k = 0; k < sz; k++) n       for (int j = 0; j < lim; j++) n         for (int p = 0; p <= min(j, k + 1); p++) { n           dp[i][j + sz - k - 1 - p] += dp[i - 1][j] * C[sz - 1][k] % MOD * n                                        C[j][p] % MOD * n                                        C[lim + 1 - j][k + 1 - p] % MOD; n           dp[i][j + sz - k - 1 - p] %= MOD; n         } n     lim += sz; n   } n   long long res = dp[cnt][0]; n   for (int i = 1; i <= cnt; i++) res = res * fac[(int)G[i].size()] % MOD; n   printf( %lld n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(false); n   ; n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     vector<set<long long>> lol(n - 1); n     long long cnt = 0; n     long long x; n     for (long long i = 0; i < n - 1; i++) { n       cin >> cnt; n       for (long long j = 0; j < cnt; j++) { n         cin >> x; n         lol[i].insert(x); n       } n     } n     for (long long i = 1; i <= n; i++) { n       bool ok = 1; n       vector<set<long long>> st(lol.begin(), lol.end()); n       long long need = i; n       for (auto &z : st) { n         z.erase(need); n       } n       vector<long long> ans; n       ans.emplace_back(i); n       for (long long j = 1; j < n; j++) { n         long long cur = -1; n         long long how = 0; n         for (auto &z : st) { n           if (z.size() == 1) { n             cur = (*z.begin()); n             how++; n           } n         } n         if (how != 1) { n           ok = 0; n           break; n         } n         ans.emplace_back(cur); n         for (auto &z : st) z.erase(cur); n       } n       if (!ok) continue; n       set<set<long long>> kavo(lol.begin(), lol.end()); n       for (long long k = 2; k < n; k++) { n         set<long long> now; n         now.insert(ans[k]); n         bool is = 0; n         for (long long j = k - 1; j >= 0; j--) { n           now.insert(ans[j]); n           if (kavo.count(now)) { n             kavo.erase(now); n             is = 1; n             break; n           } n         } n         if (!is) { n           ok = 0; n           break; n         } n       } n       if (ok) { n         for (auto z : ans) { n           cout << z <<    ; n         } n         cout <<   n ; n         break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long const lim = 1e5 + 5; n long long const mod = 1000000007; n vector<long long> adj[lim + 5]; n long long vis[lim + 5], n; n vector<long long> count_edge; n long long child[lim + 5]; n void all_clear() { n   count_edge.clear(); n   for (long long i = 1; i <= lim; i++) { n     adj[i].clear(); n     vis[i] = -1; n     child[i] = 0; n   } n } n void dfs(long long s) { n   vis[s] = 1; n   for (auto v : adj[s]) { n     if (vis[v] == -1) { n       dfs(v); n       child[s] += child[v]; n       count_edge.push_back(child[v] * (n - child[v])); n     } n   } n   child[s] += 1; n } n void solve() { n   cin >> n; n   all_clear(); n   for (int i = 1; i < n; i++) { n     long long a, b; n     cin >> a >> b; n     adj[a].push_back(b), adj[b].push_back(a); n   } n   long long m; n   cin >> m; n   vector<long long> fact; n   for (int i = 0; i < m; i++) { n     long long p; n     cin >> p; n     fact.push_back(p); n   } n   dfs(1); n   sort(count_edge.rbegin(), count_edge.rend()); n   sort(fact.rbegin(), fact.rend()); n   for (int i = 0; i < count_edge.size(); i++) count_edge[i] %= mod; n   long long ans = 0; n   if (fact.size() > count_edge.size()) { n     while (fact.size() > count_edge.size()) { n       long long val = fact[0]; n       fact.erase(fact.begin()); n       fact[0] = (fact[0] * val) % mod; n     } n     for (int i = 0; i < fact.size(); i++) { n       ans = (ans + (fact[i] * count_edge[i]) % mod) % mod; n     } n   } else { n     for (int i = 0; i < count_edge.size(); i++) { n       if (i >= fact.size()) { n         ans = (ans + count_edge[i]) % mod; n       } else { n         ans = (ans + (fact[i] * count_edge[i]) % mod) % mod; n       } n     } n   } n   cout << ans << endl; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   int t = 1, no = 0; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, c, d, cnt1, cnt2; n   cin >> n; n   pair<int, int> date[n]; n   for (i = 0; i < n; i++) { n     cin >> date[i].first >> date[i].second; n   } n   sort(date, date + n); n   c = date[0].first; n   d = date[0].second; n   if (c <= d) n     cnt1 = c; n   else n     cnt1 = d; n   for (i = 0; i < n; i++) { n     c = date[i].first; n     d = date[i].second; n     if (c <= d) n       cnt2 = c; n     else n       cnt2 = d; n     if (cnt1 <= cnt2) n       cnt1 = cnt2; n     else n       cnt1 = c; n   } n   cout << cnt1; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n long long v[100100]; n vector<int> pos, neg; n vector<int> X, Y; n vector<long long> V; n int main() { n   scanf( %d %d , &n, &m); n   int x, y; n   long long z; n   memset(v, 0, sizeof(v)); n   for (int i = 0; i < m; i++) { n     scanf( %d %d %I64d , &x, &y, &z); n     v[x] += z; n     v[y] -= z; n   } n   for (int i = 1; i <= n; i++) { n     if (v[i] > 0) pos.push_back(i); n     if (v[i] < 0) neg.push_back(i); n   } n   int i = 0, j = 0; n   while (i < pos.size() && j < neg.size()) { n     long long tem = v[pos[i]] > -v[neg[j]] ? -v[neg[j]] : v[pos[i]]; n     v[pos[i]] -= tem; n     v[neg[j]] += tem; n     X.push_back(pos[i]); n     Y.push_back(neg[j]); n     V.push_back(tem); n     if (v[pos[i]] <= 0) i++; n     if (v[neg[j]] >= 0) j++; n   } n   printf( %d n , X.size()); n   for (int i = 0; i < X.size(); i++) { n     cout << X[i] <<     << Y[i] <<     << V[i] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, p; n long long int a[100001]; n int is_good(long long int q) { n   int b[n]; n   for (int i = n - 1; i >= 0; i--) { n     long long int l = max(a[i] - q, 0ll); n     b[i] = n - l - (n - 1 - i); n     if (b[i] % p == 0) return 0; n   } n   return 1; n } n int main() { n   cin >> n >> p; n   long long int maxx = 0; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     maxx = max(maxx, a[i]); n   } n   sort(a, a + n); n   long long int t1 = 0; n   for (int i = 0; i < n; i++) { n     t1 = max(t1, a[i] - i); n   } n   long long int l = t1, r = 1000000001; n   while (r > l) { n     long long int mid = (r + l + 1) / 2; n     if (is_good(mid)) { n       l = mid; n     } else { n       r = mid - 1; n     } n   } n   if (l == t1 && !is_good(l)) { n     cout <<  0  n          <<   n ; n     cout <<   n ; n     exit(0); n   } n   cout << (l - t1 + 1) <<   n ; n   for (long long int i = t1; i <= l; i++) { n     cout << i <<    ; n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n char ss[1002][1002]; n int ak = -1, p = 0; n int main() { n   while (gets(ss[p])) { n     if ((int)strlen(ss[p]) > ak) ak = (int)strlen(ss[p]); n     p++; n   } n   for (int i = 0; i <= ak + 1; i++) printf( * ); n   printf(  n ); n   bool k = false; n   for (int i = 0; i < p; i++) { n     int len = strlen(ss[i]); n     if ((ak - len) % 2 != 0) k = !k; n     printf( * ); n     for (int j = 0; j < (ak - len + (!k)) / 2; j++) printf(   ); n     printf( %s , ss[i]); n     for (int j = 0; j < (ak - len + (k)) / 2; j++) printf(   ); n     printf( * n ); n   } n   for (int i = 0; i <= ak + 1; i++) printf( * ); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n const int N = 30; n int a[N]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i]); n   } n   int max = 0, sit; n   for (int i = 1; i <= 100000; ++i) { n     bool div = true; n     for (int j = 0; j < n; ++j) { n       if (a[j] % i) { n         div = false; n         break; n       } n     } n     if (!div) continue; n     int oddcnt = 0; n     for (int j = 0; j < n; ++j) { n       oddcnt += a[j] / i & 1; n     } n     if (oddcnt > 1) continue; n     int value = i * (oddcnt ? 1 : 2); n     if (max < value) { n       max = value; n       sit = i; n     } n   } n   printf( %d n , max); n   if (!max) { n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < a[i]; ++j) { n         putchar( a  + i); n       } n     } n     putchar(  n ); n     return 0; n   } n   for (int i = 0; i < n; ++i) { n     a[i] /= sit; n   } n   for (int times = 0; times < sit; ++times) { n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < a[i] >> 1; ++j) { n         putchar( a  + i); n       } n     } n     for (int i = 0; i < n; ++i) { n       if (a[i] & 1) { n         putchar( a  + i); n       } n     } n     for (int i = n - 1; i >= 0; --i) { n       for (int j = 0; j < a[i] >> 1; ++j) { n         putchar( a  + i); n       } n     } n   } n   putchar(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 9; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, s; n   cin >> n >> s; n   if (s < 2 * n) return cout <<  NO n , 0; n   cout <<  YES n ; n   for (int i = 1; i < n; i++) cout << 2 <<    ; n   cout << s - 2 * (n - 1) <<   n ; n   cout << 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double eps = 1e-9; n pair<int, int> get_closer(int a, vector<int> &v) { n   auto it = lower_bound(v.begin(), v.end(), a); n   int b = 2000000000, c = 2000000000; n   if (it != v.end()) b = *it; n   if (it != v.begin()) c = *(it - 1); n   return {c, b}; n } n unsigned long long int count(int a, int b, int c) { n   return (a - b) * 1LL * (a - b) + (a - c) * 1LL * (a - c) + n          (b - c) * 1LL * (b - c); n } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n1, n2, n3; n     scanf( %d %d %d , &n1, &n2, &n3); n     vector<int> r(n1), g(n2), b(n3); n     for (int i = 0; i < n1; i++) scanf( %d , &r[i]); n     for (int i = 0; i < n2; i++) scanf( %d , &g[i]); n     for (int i = 0; i < n3; i++) scanf( %d , &b[i]); n     sort(r.begin(), r.end()); n     sort(g.begin(), g.end()); n     sort(b.begin(), b.end()); n     unsigned long long int mn = count(r[0], b[0], g[0]); n     for (int i = 0; i < n1; i++) { n       int a = r[i]; n       pair<int, int> p1 = get_closer(a, g); n       pair<int, int> p2 = get_closer(a, b); n       pair<int, int> p3 = get_closer(p1.first, b); n       pair<int, int> p4 = get_closer(p1.second, b); n       mn = min(mn, count(a, p1.first, p2.first)); n       mn = min(mn, count(a, p1.second, p2.first)); n       mn = min(mn, count(a, p1.first, p2.second)); n       mn = min(mn, count(a, p1.second, p2.second)); n       mn = min(mn, count(a, p1.first, p3.first)); n       mn = min(mn, count(a, p1.first, p3.second)); n       mn = min(mn, count(a, p1.second, p4.first)); n       mn = min(mn, count(a, p1.second, p4.second)); n     } n     for (int i = 0; i < n2; i++) { n       int a = g[i]; n       pair<int, int> p1 = get_closer(a, r); n       pair<int, int> p2 = get_closer(a, b); n       pair<int, int> p3 = get_closer(p1.first, b); n       pair<int, int> p4 = get_closer(p1.second, b); n       mn = min(mn, count(a, p1.first, p2.first)); n       mn = min(mn, count(a, p1.second, p2.first)); n       mn = min(mn, count(a, p1.first, p2.second)); n       mn = min(mn, count(a, p1.second, p2.second)); n       mn = min(mn, count(a, p1.first, p3.first)); n       mn = min(mn, count(a, p1.first, p3.second)); n       mn = min(mn, count(a, p1.second, p4.first)); n       mn = min(mn, count(a, p1.second, p4.second)); n     } n     for (int i = 0; i < n3; i++) { n       int a = b[i]; n       pair<int, int> p1 = get_closer(a, g); n       pair<int, int> p2 = get_closer(a, r); n       pair<int, int> p3 = get_closer(p1.first, r); n       pair<int, int> p4 = get_closer(p1.second, r); n       mn = min(mn, count(a, p1.first, p2.first)); n       mn = min(mn, count(a, p1.second, p2.first)); n       mn = min(mn, count(a, p1.first, p2.second)); n       mn = min(mn, count(a, p1.second, p2.second)); n       mn = min(mn, count(a, p1.first, p3.first)); n       mn = min(mn, count(a, p1.first, p3.second)); n       mn = min(mn, count(a, p1.second, p4.first)); n       mn = min(mn, count(a, p1.second, p4.second)); n     } n     cout << mn <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   int a[10000]; n   while (scanf( %d , &n) != EOF) { n     for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n     for (int i = 1; i <= n; i++) { n       if (a[i] % 2 == 0) a[i] -= 1; n     } n     for (int i = 1; i <= n - 1; i++) printf( %d  , a[i]); n     printf( %d n , a[n]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   map<char, int> mp; n   mp.insert(pair<char, int>( A , 0)); n   mp.insert(pair<char, int>( B , 0)); n   mp.insert(pair<char, int>( C , 0)); n   int i = 3; n   while (i--) { n     string s; n     cin >> s; n     if (s[1] ==  > ) n       mp[s[0]]++; n     else n       mp[s[2]]++; n   } n   if (mp[ A ] == mp[ B ] && mp[ C ] == mp[ B ]) n     cout <<  Impossible ; n   else { n     if (mp[ A ] == 0) n       cout <<  A ; n     else if (mp[ B ] == 0) n       cout <<  B ; n     else if (mp[ C ] == 0) n       cout <<  C ; n     if (mp[ A ] == 1) n       cout <<  A ; n     else if (mp[ B ] == 1) n       cout <<  B ; n     else if (mp[ C ] == 1) n       cout <<  C ; n     if (mp[ A ] == 2) n       cout <<  A ; n     else if (mp[ B ] == 2) n       cout <<  B ; n     else if (mp[ C ] == 2) n       cout <<  C ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long GCD(long long u, long long v) { n   long long r; n   while (v) { n     r = u % v; n     u = v; n     v = r; n   } n   return u; n } n long long modpow(long long a, long long p, long long mod) { n   long long ret = 1; n   while (p) { n     if (p & 1) ret = (ret * a) % mod; n     a = (a * a) % mod; n     p /= 2; n   } n   return ret; n } n long long power(long long a, long long p) { n   long long ret = 1; n   while (p) { n     if (p & 1) ret = (ret * a); n     a = (a * a); n     p /= 2; n   } n   return ret; n } n int a[51]; n int main() { n   int i, j, tmp; n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   int n, k; n   cin >> n >> k; n   for (i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   if (k > n) n     cout << -1 << endl; n   else n     cout << a[n - k] <<     << a[n - k] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   if (n < 4) { n     cout <<  NO n ; n     return 0; n   } n   cout <<  YES n ; n   if (n >= 4 && !(n & 1)) { n     int ones = 0; n     for (int i = 5; i < n; i += 2) { n       printf( %d - %d = 1 n , i + 1, i); n       ones++; n     } n     cout <<  1 * 4 = 4 n ; n     cout <<  4 * 2 = 8 n ; n     cout <<  8 * 3 = 24 n ; n     while (ones > 0) { n       cout <<  24 * 1 = 24 n ; n       ones--; n     } n     return 0; n   } n   if (n > 4 && n & 1) { n     cout <<  5 - 1 = 4 n ; n     cout <<  4 - 2 = 2 n ; n     cout <<  4 * 2 = 8 n ; n     cout <<  8 * 3 = 24 n ; n     int ones = 0; n     for (int i = 6; i < n; i += 2) { n       printf( %d - %d = 1 n , i + 1, i); n       ones++; n     } n     while (ones > 0) { n       cout <<  24 * 1 = 24 n ; n       ones--; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, ai, m, xi, yi; n   cin >> n; n   vector<int> wire(100); n   for (int i = 1; i <= n; i++) { n     cin >> wire[i]; n   } n   cin >> m; n   for (int i = 1; i <= m; i++) { n     cin >> xi >> yi; n     wire[xi - 1] += (yi - 1); n     wire[xi + 1] += (wire[xi] - yi); n     wire[xi] = 0; n   } n   for (int i = 1; i <= n; i++) { n     cout << wire[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e6 + 3; n const int INFI = 1e9 * 2; n const long long LINFI = 1e17; n const double pi = acos(-1.0); n const double s2 = sqrt(2.0); n const int N = 222; n const int M = 11; n const int move[8][2] = {1, 0, -1, 0, 0, 1, 0, -1, 1, 1, 1, -1, -1, 1, -1, -1}; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n int main() { n   int a, b, c, d, p, q, m, x, y; n   while (scanf( %d%d%d%d , &a, &b, &c, &d) != EOF) { n     m = c * d / gcd(c, d); n     a *= m; n     b *= m; n     x = a / c; n     y = b / d; n     if (x >= y) { n       q = a; n       p = a - y * c; n     } else { n       q = b; n       p = b - x * d; n     } n     m = gcd(p, q); n     q /= m; n     p /= m; n     printf( %d/%d n , p, q); n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int t, n, *arr, i, a, odd, even; n   scanf( %d , &t); n   while (t--) { n     scanf( %d , &n); n     arr = (int*)malloc(n * sizeof(int)); n     for (i = 0; i < n; i++) scanf( %d , &arr[i]); n     even = 0; n     odd = 0; n     for (i = 0; i < n; i++) { n       if (arr[i] % 2 == 0) n         even++; n       else n         odd++; n     } n     a = 0; n     if (n % 2 == 0) { n       if (even == odd) { n         for (i = 0; i < n; i++) { n           if ((i % 2) != (arr[i] % 2)) a++; n         } n         printf( %d n , a / 2); n       } else n         printf( %d n , -1); n     } else { n       if (odd == (even - 1)) { n         for (i = 0; i < n; i++) { n           if ((i % 2) != (arr[i] % 2)) a++; n         } n         printf( %d n , a / 2); n       } else n         printf( %d n , -1); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[100][10]; n string s; n int ss[100]; n int main() { n   cin >> s; n   int n = ((int)(s).size()); n   for (int i = 0; i < n; i++) ss[i + 1] = s[i] -  0 ; n   for (int i = 0; i < 10; i++) dp[1][i] = 1; n   for (int i = 2; i <= n; i++) n     for (int j = 0; j < 10; j++) { n       long long cnt = 0; n       if (j * 2 - 1 >= ss[i] && j * 2 - 1 - ss[i] < 10) { n         int k = j * 2 - 1 - ss[i]; n         cnt += dp[i - 1][k]; n       } n       if (j * 2 >= ss[i] && j * 2 - ss[i] < 10) { n         int k = j * 2 - ss[i]; n         cnt += dp[i - 1][k]; n       } n       if (j * 2 + 1 >= ss[i] && j * 2 + 1 - ss[i] < 10) { n         int k = j * 2 + 1 - ss[i]; n         cnt += dp[i - 1][k]; n       } n       dp[i][j] = cnt; n     } n   long long can = 1; n   for (int i = 2; i <= n; i++) { n     if (((ss[i - 1] + ss[i]) / 2 == ss[i] || n          (ss[i - 1] + ss[i]) / 2 - 1 == ss[i]) || n         ((ss[i - 1] + ss[i]) / 2 + 1 == ss[i] && (ss[i - 1] + ss[i]) % 2 == 1)) n       continue; n     can = 0; n     break; n   } n   long long ccnt = 0; n   for (int i = 0; i < 10; i++) ccnt += dp[n][i]; n   cout << ccnt - can << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, c = 0, temp; n   int h[101], g[101]; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> h[i] >> g[i]; n   } n   for (int j = 0; j < n; j++) { n     temp = n - 1; n     while (temp > j) { n       if (h[j] == g[temp]) c++; n       if (h[temp] == g[j]) c++; n       temp--; n     } n   } n   cout << c; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t,n; n int a[100010]; n int main(){ n  cin>>t; n  while(t--){ n   cin>>n; n   for(int i=0;i<n;++i)cin>>a[i]; n   sort(a,a+n); n   long long ans=a[n-1],sum=0; n   for(int i=0;i<n;++i){ n    sum+=a[i]; n    ans+=sum-1ll*a[i]*(i+1); n   } n   cout<<ans<<  n ; n  } n } n


#include <bits/stdc++.h> n long int temp, i, j, k, T; n long int ABS(int test) { n   if (test < 0) n     return test * -1; n   else n     return test; n } n int x8[] = {1, 1, 0, -1, -1, -1, 0, 1}; n int y8[] = {0, 1, 1, 1, 0, -1, -1, -1}; n int x4[] = {1, 0, -1, 0}; n int y4[] = {0, 1, 0, -1}; n long int arr[1000000]; n long int buff[1000000]; n long int memo[1000000]; n using namespace std; n int main() { n   char a[100001]; n   char b[100001]; n   int jawaban = 0, panjang, tujuh1 = 0, tujuh2 = 0, empat1 = 0, empat2 = 0; n   memset(memo, 0, sizeof memo); n   cin >> a; n   cin >> b; n   panjang = strlen(a); n   for (i = 0; i < panjang; i++) { n     if (a[i] ==  7 ) tujuh1++; n     if (a[i] ==  4 ) empat1++; n     if (b[i] ==  7 ) tujuh2++; n     if (b[i] ==  4 ) empat2++; n   } n   for (i = 0; i < panjang; i++) { n     if (a[i] == b[i]) { n       if (a[i] ==  7 ) { n         tujuh1--; n         tujuh2--; n       } n       if (a[i] ==  4 ) { n         empat1--; n         empat2--; n       } n     } n   } n   jawaban = max(tujuh1, tujuh2); n   cout << jawaban << endl; n   { n     fflush(stdin); n     getchar(); n   }; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[2][300004]; n long long sf[2][300004]; n long long ca[2][300004]; n int w33ha() { n   memset(sf, 0, sizeof(sf)); n   memset(ca, 0, sizeof(ca)); n   for (int t = 0; t <= 1; t++) n     for (int i = 1; i <= n; i++) scanf( %d , &a[t][i]); n   for (int t = 0; t <= 1; t++) { n     sf[t][0] = 0; n     for (int i = 1; i <= n; i++) { n       sf[t][i] = sf[t][i - 1] + a[t][i]; n     } n   } n   for (int t = 0; t <= 1; t++) { n     ca[t][n + 1] = 0; n     for (int i = n; i >= 1; i--) { n       long long nob = ((n - i + 1LL) << 1) - 1; n       ca[t][i] = ca[t][i + 1]; n       ca[t][i] += nob * a[1 - t][i]; n       ca[t][i] += sf[t][n] - sf[t][i] + sf[1 - t][n] - sf[1 - t][i]; n     } n   } n   long long ans = 0, now = 0; n   for (int i = 1; i <= n; i++) { n     long long delta = n         ((i - 1LL) << 1) * (sf[0][n] - sf[0][i - 1] + sf[1][n] - sf[1][i - 1]); n     if (i & 1) { n       ans = max(ans, now + delta + ca[0][i]); n       now += 1LL * ((i - 1LL) << 1) * a[0][i] + n              1LL * ((i - 1LL) << 1 | 1) * a[1][i]; n     } else { n       ans = max(ans, now + delta + ca[1][i]); n       now += 1LL * ((i - 1LL) << 1) * a[1][i] + n              1LL * ((i - 1LL) << 1 | 1) * a[0][i]; n     } n   } n   printf( %lld n , ans); n   return 0; n } n int main() { n   while (scanf( %d , &n) != EOF) w33ha(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, t, i; n   cin >> t; n   for (i = 0; i < t; i++) { n     cin >> a; n     cout << a / 2 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   queue<unsigned long long> q; n   vector<unsigned long long> v; n   unsigned long long temp; n   q.push(0); n   while (!q.empty()) { n     temp = q.front(); n     v.push_back(temp * 10 + 4); n     v.push_back(temp * 10 + 7); n     if (temp * 10 + 7 >= 4444444444) break; n     q.pop(); n     q.push(temp * 10 + 4); n     q.push(temp * 10 + 7); n   } n   unsigned long long left, right, sum = 0; n   cin >> left >> right; n   int i = 0; n   for (i = 0; i < v.size(); i++) n     if (v[i] >= left) break; n   while (left <= right) { n     sum = sum + v[i] * (min(v[i], right) - left + 1); n     left = v[i] + 1; n     i++; n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, b; n struct chel { n   int cost; n   int mas; n   int coun; n }; n chel f[103]; n chel tem[103]; n void mergesort(chel *a, chel *b, int n) { n   int i, i0, i1, iRight, iEnd, width, j; n   for (width = 1; width < n; width *= 2) n     for (i = 0; i < n; i += width * 2) { n       i0 = i; n       i1 = iRight = min(i + width, n); n       iEnd = min(i + width * 2, n); n       for (j = i; j < iEnd; j++) n         if (i0 < iRight && (i1 == iEnd || a[i0].coun < a[i1].coun)) n           b[j] = a[i0++]; n         else n           b[j] = a[i1++]; n       for (j = i; j < iEnd; j++) a[j] = b[j]; n     } n } n long long dp[(1 << 20)]; n int main() { n   int i, j; n   scanf( %d %d %d , &n, &m, &b); n   dp[0] = 0; n   for (i = 1; i < (1 << m); i++) dp[i] = (long long)1000000000 * 1000000000 * 2; n   long long ans = (long long)1000000000 * 1000000000 * 2; n   int x, t; n   for (i = 0; i < n; i++) { n     scanf( %d %d %d , &f[i].cost, &f[i].coun, &t); n     for (j = 0; j < t; j++) { n       scanf( %d , &x); n       f[i].mas |= (1 << (x - 1)); n     } n   } n   mergesort(f, tem, n); n   for (i = 0; i < n; i++) { n     for (j = 0; j < (1 << m); j++) n       dp[(j | f[i].mas)] = min(dp[(j | f[i].mas)], dp[j] + f[i].cost); n     ans = min(ans, dp[(1 << m) - 1] + (long long)b * f[i].coun); n   } n   if (ans == (long long)1000000000 * 1000000000 * 2) n     printf( -1 n ); n   else n     printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int md = 1e9 + 7; n long long int __gcd(long long int a, long long int b) { n   if (b == 0) return a; n   return __gcd(b, a % b); n } n long long int poww(long long int a, long long int b) { n   long long int res = 1; n   while (b) { n     if (b & 1) { n       res *= a; n       res %= md; n     } n     a *= a; n     a %= md; n     b >>= 1; n   } n   return res; n } n void ainp(long long int arr[], long long int n) { n   for (long long int i = 1; i <= n; i++) cin >> arr[i]; n } n const long long int N = 2e5 + 5; n void solve() { n   long long int n; n   cin >> n; n   long long int t = n; n   vector<long long int> bits; n   while (n) { n     bits.push_back(n % 3); n     n /= 3; n   } n   bits.push_back(0); n   long long int cnt2 = -1; n   for (long long int i = 0; i < bits.size() - 1; i++) { n     if (bits[i] == 2) { n       cnt2 = i; n     } n   } n   if (cnt2 == -1) n     cout << t <<   n ; n   else { n     for (long long int i = 0; i < cnt2; i++) { n       bits[i] = 0; n     } n     for (long long int i = cnt2; i < bits.size() - 1; i++) { n       if (bits[i] == 2) { n         bits[i] = 0; n         bits[i + 1] += 1; n       } n     } n     long long int ans = 0; n     for (long long int i = 0; i < bits.size(); i++) { n       ans += bits[i] * (long long int)pow(3, i); n     } n     cout << ans <<   n ; n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int t; n   cin >> t; n   while (t-- > 0) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, g = 0; n   cin >> t; n   for (int i = 0; i < t; i++) { n     vector<int> p; n     int n, x; n     g = 0; n     cin >> n; n     if (n == 1) { n       cin >> x; n       cout <<  YES  << endl; n       continue; n     } n     for (int l = 0; l < n; l++) { n       int k; n       cin >> k; n       p.push_back(k); n     } n     sort(p.begin(), p.end()); n     for (int j = 0; j < p.size() - 1; j++) { n       if (abs(p[j] - p[j + 1]) > 1) { n         cout <<  NO  << endl; n         g = 1; n         break; n       } n     } n     if (g == 0) cout <<  YES  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char const *argv[]) { n   int n; n   bool check, lucky; n   string s; n   cin >> n; n   for (int i = 4; i < n + 1; i++) { n     check = false; n     s = to_string(i); n     for (int j = 0; j < s.size(); j++) n       if ((s[j] ==  4 ) || (s[j] ==  7 )) n         check = true; n       else { n         check = false; n         break; n       } n     if (check) n       if (n % i == 0) { n         lucky = true; n         break; n       } else { n         lucky = false; n       } n   } n   if (lucky) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve(int d) { n   cout << fixed << setprecision(9); n   if (d == 0) { n     cout <<  Y   << 0.0 <<     << 0.0; n   } else if (d >= 1 && d < 4) n     cout <<  N ; n   else { n     double D = sqrt(d * (d - 4)); n     double a = (d + D) / 2.0; n     double b = (d - D) / 2.0; n     cout <<  Y   << a <<     << b; n   } n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     int d; n     cin >> d; n     solve(d); n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n; n     scanf( %d , &n); n     vector<int> a(n, 0); n     for (int i = 0; i < n; i++) scanf( %d , &a[i]); n     int m; n     scanf( %d , &m); n     vector<pair<int, int> > h(m, {0, 0}); n     for (int i = 0; i < m; i++) scanf( %d%d , &h[i].first, &h[i].second); n     sort(h.begin(), h.end()); n     vector<int> suf(m, 0); n     suf[m - 1] = h[m - 1].second; n     for (int i = m - 2; i >= 0; i--) suf[i] = max(suf[i + 1], h[i].second); n     int ans = 0; n     int bad = 0; n     for (int i = 0; i < n;) { n       int j = i; n       int mx = 0; n       while (j < n) { n         mx = max(mx, a[j]); n         int p = lower_bound(h.begin(), h.end(), make_pair(mx, -1)) - h.begin(); n         if (p < m && suf[p] >= j - i + 1) n           j++; n         else n           break; n       } n       if (j <= i) { n         bad = 1; n         break; n       } n       ans++; n       i = j; n     } n     if (bad) n       printf( -1 n ); n     else n       printf( %d n , ans); 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; n     cin >> n; n     int arr[n + 1]; n     int i, s = 0; n     for (i = 0; i < n; i++) { n       cin >> arr[i]; n     } n     int j; n     for (i = 1; i < n - 1; i++) { n       if (arr[i - 1] < arr[i] && arr[i + 1] < arr[i]) { n         cout <<  YES  << endl << i <<     << i + 1 <<     << i + 2 << endl; n         s = 1; n         break; n       } n     } n     if (s != 1) cout <<  NO  << endl; n   } n   return 0; n } n