#include <bits/stdc++.h> n using namespace std; n const int maxn = 2000; n int pre[maxn]; n int countlen(int i) { n   int ans = 0, p; n   for (p = i; p != -2; p = pre[p]) ans++; n   return ans; n } n int main() { n   int i, n; n   cin >> n; n   for (i = 0; i < n; i++) cin >> pre[i], pre[i]--; n   int ans = 0; n   for (i = 0; i < n; i++) ans = max(ans, countlen(i)); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100004; n long long int a[MAXN]; n long long int k, n; n vector<long long int> b(MAXN, 0); n long long int f(double num) { n   long long int res = 0; n   for (int i = 0; i < n; i++) { n     if (num > a[i] + 0.25) { n       b[i] = 0; n       continue; n     } n     double tem = sqrt(12 * (a[i] - num) - 3); n     b[i] = (long long int)(tem / 6 + 0.5); n     if (b[i] > a[i]) b[i] = a[i]; n     res += b[i]; n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> a[i]; n   double mid; n   double lo = -3e18, hi = 1e9; n   for (int i = 0; i < 200; i++) { n     mid = (lo + hi) / 2; n     if (f(mid) >= k) n       lo = mid; n     else n       hi = mid; n   } n   long long int tot = 0; n   priority_queue<pair<long long int, int>, vector<pair<long long int, int> >, n                  greater<pair<long long int, int> > > n       pq; n   priority_queue<pair<long long int, int> > pq1; n   for (int i = 0; i < n; i++) { n     double tem = sqrt(12 * (a[i] - lo) - 3); n     b[i] = min((long long int)(tem / 6 + 0.5), a[i]); n     tot += b[i]; n     if (b[i]) pq.push({a[i] - 3 * b[i] * b[i] + 3 * b[i] - 1, i}); n     if (b[i] != a[i]) pq1.push({a[i] - 3 * b[i] * b[i] - 3 * b[i] - 1, i}); n   } n   while (tot - k > 0) { n     pair<long long int, int> tem = pq.top(); n     pq.pop(); n     int i = tem.second; n     b[i]--; n     tem.first = a[i] - 3 * b[i] * b[i] + 3 * b[i] - 1; n     pq.push(tem); n     tot--; n   } n   for (int i = 0; i < n; i++) { n     int bu = b[i]; n     printf( %d  , bu); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int bl = 400, sz = 4e5 + 10; n int pr[sz], bor[sz][26], cnt[sz], an[sz], q = 1; n char prsy[sz]; n vector<pair<int, int> > sp[sz]; n vector<int> sv[sz]; n void dfs(int v) { n   int u = v, s = 0; n   for (int a = 0; (a < bl and u != 0 and s != -1); a++) { n     s = bor[s][prsy[u] -  a ]; n     if (s != -1) cnt[s]++; n     u = pr[u]; n   } n   for (int a = 0; a < sp[v].size(); a++) { n     int s = sp[v][a].first, nu = sp[v][a].second; n     an[nu] = cnt[s]; n   } n   for (int a = 0; a < sv[v].size(); a++) { n     dfs(sv[v][a]); n   } n   u = v, s = 0; n   for (int a = 0; (a < bl and u != 0 and s != -1); a++) { n     s = bor[s][prsy[u] -  a ]; n     if (s != -1) cnt[s]--; n     u = pr[u]; n   } n } n int f(string &s, int le) { n   int n = s.size(), re = 0; n   int zf[n], l = 0, r = -1; n   zf[0] = 0; n   for (int a = 1; a < n; a++) { n     zf[a] = 0; n     if (r >= a) zf[a] = min(zf[a - l], r - a + 1); n     while (a + zf[a] < n) { n       if (s[zf[a]] == s[a + zf[a]]) n         zf[a]++; n       else n         break; n     } n     if (a + zf[a] - 1 > r) l = a, r = a + zf[a] - 1; n     if (zf[a] == le) re++; n   } n   return re; n } n int main() { n   int n; n   cin >> n; n   for (int a = 1; a <= n; a++) { n     int ty; n     scanf( %d , &ty); n     if (ty == 2) scanf( %d , &pr[a]); n     scanf( %s , prsy + a); n     sv[pr[a]].push_back(a); n   } n   int m; n   cin >> m; n   for (int a = 0; a < 26; a++) bor[0][a] = -1; n   for (int a = 0; a < m; a++) { n     int v; n     scanf( %d , &v); n     string st; n     cin >> st; n     reverse(st.begin(), st.end()); n     if (st.size() < bl) { n       int s = 0; n       for (int b = 0; b < st.size(); b++) { n         int ne = st[b] -  a ; n         if (bor[s][ne] == -1) { n           bor[s][ne] = q++; n           for (int i = 0; i < 26; i++) bor[q - 1][i] = -1; n         } n         s = bor[s][ne]; n       } n       sp[v].push_back({s, a}); n     } else { n       int l = st.size(); n       st.push_back( # ); n       while (v) { n         st.push_back(prsy[v]), v = pr[v]; n       } n       an[a] = f(st, l); n     } n   } n   dfs(0); n   for (int a = 0; a < m; a++) printf( %d n , an[a]); n } n


#include <bits/stdc++.h> n using namespace std; n bool vis[101]; n vector<int> a[101]; n void dfs(int x) { n   vis[x] = true; n   for (int i = 0; i < a[x].size(); i++) n     if (!vis[a[x][i]]) dfs(a[x][i]); n } n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     a[x].push_back(y); n     a[y].push_back(x); n   } n   dfs(1); n   if (*min_element(vis + 1, vis + n + 1) && m == n) n     cout <<  FHTAGN! ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n const int MOD = 1000000007; n const int Inv2 = 500000004; n inline int mul(int x, int y) { return (long long)x * (long long)y % MOD; } n inline int add(int x, int y) { n   int r = x + y; n   if (r >= MOD) r -= MOD; n   return r; n } n inline int qpow(int a, int p) { n   int x = a, ans = 1; n   while (p) { n     if (p & 1) ans = mul(ans, x); n     x = mul(x, x); n     p >>= 1; n   } n   return ans; n } n using namespace std; n template <typename T> n inline void read(T &x) { n   char ch; n   while ((ch = getchar()), (ch <  0  || ch >  9 )) n     ; n   x = ch -  0 ; n   while ((ch = getchar()), (ch >=  0  && ch <=  9 )) x = x * 10 + (ch -  0 ); n } n inline int readbin(int *str) { n   char ch; n   int len = 1; n   while ((ch = getchar()), (ch <  0  || ch >  1 )) n     ; n   *(str++) = ch -  0 ; n   while ((ch = getchar()), (ch >=  0  && ch <=  1 )) *(str++) = ch -  0 , len++; n   return len; n } n void fwt(int A[], int n) { n   int i, h, k, A0, A1; n   for (h = 2; h <= n; h <<= 1) { n     for (i = 0; i < n; i += h) { n       for (k = i; k < i + (h / 2); k++) { n         A0 = A[k]; n         A1 = A[k + (h / 2)]; n         A[k] = (A0 + A1) % MOD; n         A[k + (h / 2)] = (A0 - A1) % MOD; n       } n     } n   } n } n void ifwt(int A[], int n) { n   int i, h, k, A0, A1; n   for (h = 2; h <= n; h <<= 1) { n     for (i = 0; i < n; i += h) { n       for (k = i; k < i + (h / 2); k++) { n         A0 = A[k]; n         A1 = A[k + (h / 2)]; n         A[k] = mul(Inv2, A0 + A1); n         A[k + (h / 2)] = mul(Inv2, A0 - A1); n       } n     } n   } n } n const int MAXN = 5 + (1 << 20); n int A[MAXN], P[MAXN]; n int N, NB; n int main() { n   int i; n   read(NB); n   N = readbin(A); n   fwt(A, N); n   for (i = 0; i < N; i++) P[i] = mul(A[i], A[i]); n   ifwt(P, N); n   int Ans = 0; n   for (i = 0; i < N; i++) n     Ans = add(Ans, mul(P[i], 1 << (NB - __builtin_popcount(i)))); n   Ans = mul(Ans, 3); n   printf( %d , Ans); n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int t, n, m, i, j, k, sum = 0; n   long long int p, w, d; n   cin >> n >> p >> w >> d; n   long long int l = 0; n   long long int r = n; n   for (i = 0; i <= w; i++) { n     long long int temp = p - i * d; n     if (temp < 0) break; n     if ((temp) % w != 0) continue; n     long long int q = temp / w; n     if (i + q <= n) { n       cout << q <<     << i <<     << n - q - i <<   n ; n       return 0; n     } n   } n   cout << -1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int q; n   int a[510][2]; n   int m = 0; n   int l1, r1, l2, r2; n   int flag; n   scanf( %d , &q); n   for (int i = 0; i < q; i++) { n     scanf( %d%d%d%d , &l1, &r1, &l2, &r2); n     flag = 0; n     for (int j = l1; j <= r1; j++) { n       a[m][0] = j; n       for (int k = l2; k <= r2; k++) { n         if (j != k) { n           flag = 1; n           a[m][1] = k; n           break; n         } n       } n       if (flag == 1) { n         m++; n         break; n       } n     } n   } n   for (int i = 0; i < q; i++) printf( %d %d n , a[i][0], a[i][1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long modpow(long long a, long long b, n                  long long mod = (long long)(1e9 + 7)) { n   if (!b) return 1; n   a %= mod; n   return modpow(a * a % mod, b / 2, mod) * (b & 1 ? a : 1) % mod; n } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const int mxn = 1e6 + 4, mxk = 20; n int n, q; n int d[mxn], parent[mxn][mxk]; n void add(int i, int p) { n   parent[i][0] = p; n   d[i] = d[p] + 1; n   for (int j = 0; j < mxk; j++) n     if (j) parent[i][j] = parent[parent[i][j - 1]][j - 1]; n } n int lca(int i, int j) { n   if (d[i] > d[j]) swap(i, j); n   int diff = d[j] - d[i]; n   for (int k = mxk - 1; k >= 0; k--) n     if (diff & (1 << k)) j = parent[j][k]; n   if (i == j) return i; n   for (int k = mxk - 1; k >= 0; k--) n     if (parent[i][k] != parent[j][k]) i = parent[i][k], j = parent[j][k]; n   return parent[i][0]; n } n int dist(int i, int j) { return d[i] + d[j] - 2 * d[lca(i, j)]; } n void solve() { n   for (int j = 0; j < mxk; j++) parent[0][j] = 0; n   d[0] = 0; n   add(1, 0); n   add(2, 0); n   add(3, 0); n   scanf( %d , &q); n   n = 4; n   stringstream ss; n   int a = 1, b = 2; n   int best = dist(a, b); n   for (int _ = 0; _ < q; _++) { n     int x; n     scanf( %d , &x); n     x--; n     add(n++, x); n     add(n++, x); n     int d1 = dist(a, n - 1), d2 = dist(b, n - 1); n     if (max(d1, d2) > best) { n       best = max(d1, d2); n       if (d1 > d2) n         b = n - 1; n       else n         a = n - 1; n     } n     ss << best << endl; n   } n   cout << ss.str(); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int k; n     string s; n     cin >> k >> s; n     s +=  A ; n     vector<int> a; n     for (int i = 0; i < s.size(); i++) { n       if (s[i] ==  A ) a.push_back(i); n     } n     int ans = 0; n     for (int i = 0; i + 1 < a.size(); i++) { n       ans = max(ans, a[i + 1] - a[i] - 1); n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1e9 + 7; n long long x, k; n long long qow(long long a, long long b) { n   long long ans = 1; n   a = a % mod; n   while (b) { n     if (b % 2 == 1) { n       ans = (ans * a) % mod; n     } n     a = (a * a) % mod; n     b /= 2; n   } n   return ans; n } n int main() { n   while (~scanf( %lld %lld , &x, &k)) { n     if (x == 0) { n       printf( 0 n ); n       continue; n     } n     x = x % mod; n     cout << (((2 * x - 1) * qow(2, k) + mod) % mod + 1 + mod) % mod << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n struct node { n   long long x, y; n } s, t, dist[100005]; n int dx[4] = {0, 0, -1, 1}; n int dy[4] = {1, -1, 0, 0}; n char wind[100005]; n string mov =  UDLR ; n long long pos, l, r, mid; n bool check(long long mid) { n   long long zq = mid / n, sy = mid % n; n   long long x = s.x + zq * dist[n].x + dist[sy].x; n   long long y = s.y + zq * dist[n].y + dist[sy].y; n   long long dis = abs(t.x - x) + abs(t.y - y); n   return dis <= mid; n } n int main() { n   cin >> s.x >> s.y >> t.x >> t.y; n   cin >> n >> wind; n   for (int i = 0; i < n; i++) { n     pos = -1; n     for (int j = 0; j < 4; j++) { n       if (wind[i] == mov[j]) pos = j; n     } n     dist[i + 1].x = dist[i].x + dx[pos]; n     dist[i + 1].y = dist[i].y + dy[pos]; n   } n   l = 0, r = 1e15; n   while (l <= r) { n     mid = (l + r) >> 1; n     if (check(mid)) { n       r = mid - 1; n     } else { n       l = mid + 1; n     } n   } n   if (l > 1e15) n     cout << -1 << endl; n   else n     cout << l << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void read(T &x) { n   x = 0; n   char c = getchar(); n   bool flag = false; n   while (!isdigit(c)) { n     if (c ==  - ) flag = true; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = (x << 1) + (x << 3) + (c ^ 48); n     c = getchar(); n   } n   if (flag) x = -x; n } n int n, lenth, las = 1, root = 1, tot = 1; n long long ans; n int ch[1000010][26], fa[1000010], len[1000010], siz[1000010][15], l[1000010], n     r[1000010]; n char s[1000010], str[1000010]; n struct edge { n   int to, nxt; n } e[1000010]; n int head[1000010], edge_cnt; n void add(int from, int to) { n   e[++edge_cnt] = (edge){to, head[from]}; n   head[from] = edge_cnt; n } n void insert(int c, int id) { n   int p = las, np = las = ++tot; n   len[np] = len[p] + 1, siz[np][id] = 1; n   while (p && !ch[p][c]) ch[p][c] = np, p = fa[p]; n   if (!p) n     fa[np] = root; n   else { n     int q = ch[p][c]; n     if (len[q] == len[p] + 1) n       fa[np] = q; n     else { n       int nq = ++tot; n       memcpy(ch[nq], ch[q], sizeof(ch[q])); n       len[nq] = len[p] + 1, fa[nq] = fa[q], fa[q] = fa[np] = nq; n       while (ch[p][c] == q) ch[p][c] = nq, p = fa[p]; n     } n   } n } n void dfs(int x) { n   for (int i = head[x]; i; i = e[i].nxt) { n     int y = e[i].to; n     dfs(y); n     for (int j = 0; j <= n; ++j) siz[x][j] += siz[y][j]; n   } n } n void calc(int x) { n   if (!siz[x][0]) return; n   for (int i = 1; i <= n; ++i) n     if (siz[x][i] < l[i] || siz[x][i] > r[i]) return; n   ans += len[x] - len[fa[x]]; n } n int main() { n   scanf( %s , str + 1), lenth = strlen(str + 1); n   for (int i = 1; i <= lenth; ++i) insert(str[i] -  a , 0); n   read(n); n   for (int i = 1; i <= n; ++i) { n     las = 1, scanf( %s , s + 1), lenth = strlen(s + 1), read(l[i]), read(r[i]); n     for (int j = 1; j <= lenth; ++j) insert(s[j] -  a , i); n   } n   for (int i = 2; i <= tot; ++i) add(fa[i], i); n   dfs(root); n   for (int i = 2; i <= tot; ++i) calc(i); n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1e5 + 69; n const long long Inf = 1e13; n long long n, newa[maxn], ans[maxn], newb[maxn]; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (long long i = 2; i <= n; i += 2) cin >> ans[i]; n   for (long long i = 2; i <= n; i += 2) { n     long long found = 0; n     for (long long j = sqrt(ans[i]); j >= 1; j--) n       if (ans[i] % j == 0) { n         long long newj = ans[i] / j; n         if (!((j + newj) & 1)) { n           newa[i] = (newj + j) / 2; n           newb[i] = (newj - j) / 2; n           if (newb[i] <= newa[i - 2]) continue; n           found = 1; n           break; n         } n       } n     if (!found) return cout <<  No , 0; n   } n   for (long long i = 1; i <= n; i++) { n     if (i & 1) { n       ans[i] = (newb[i + 1] - newa[i - 1]) * (newb[i + 1] + newa[i - 1]); n       if (ans[i] > Inf) return cout <<  No , 0; n     } n   } n   cout <<  Yes  <<   n ; n   for (long long i = 1; i <= n; i++) cout << ans[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int la, lb; n int a[2000000]; n int b[2000000]; n int bpos[1000001]; n void deb(int* x, int xlow, int xhigh) { n   cout <<  [ ; n   for (int i = xlow; i < xhigh; i++) { n     cout <<     << x[i]; n   } n   cout <<   ] ; n } n int seen[1000001]; n int solve() { n   fill(bpos, bpos + 1000001, -1); n   for (int i = 0; i < lb; i++) { n     bpos[b[i]] = i; n   } n   int res = 0; n   for (int i = la; i < la * 2; i++) { n     a[i] = a[i - la]; n   } n   int alow = -1; n   int ahigh = alow + la; n   int blow = -1; n   int last = -1; n   int len = 0; n   fill(seen, seen + 1000001, 0); n   int steps = 0; n   for (int i = 0; i < la; i++) { n     alow++; n     ahigh++; n     len = max(len - 1, 0); n     if (len == 0) { n       if (bpos[a[alow]] != -1) { n         blow = bpos[a[alow]]; n       } else { n         blow += lb - 1; n         if (blow >= lb) { n           blow -= lb; n         } n       } n       last = -1; n     } else { n       blow = bpos[a[alow]]; n     } n     while (len < la) { n       int y = bpos[a[alow + len]]; n       bool good = false; n       if ((y != -1) && (last != -1)) { n         if (y < blow) { n           if (last < blow) { n             good = (y > last); n           } else { n             good = (y > last - lb); n           } n         } else { n           if (last >= blow) { n             good = (y > last); n           } else { n             good = (y - lb > last); n           } n         } n       } else if (y != -1) { n         good = true; n       } n       if (good) { n         seen[a[alow + len]]++; n         assert(seen[a[alow + len]] <= 2); n         last = y; n         steps++; n         assert(steps < 4000000); n         len++; n       } else { n         break; n       } n     } n     res = std::max(res, len); n   } n   return res; n } n inline void init() {} n int main() { n   init(); n   ios_base::sync_with_stdio(0); n   bool prev = false; n   while (cin >> la >> lb) { n     if (prev) { n       cout << endl; n     } n     prev = true; n     for (int i = 0; i < la; i++) { n       cin >> a[i]; n     } n     for (int i = 0; i < lb; i++) { n       cin >> b[i]; n     } n     cout << solve() << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct data { n   string name; n   int score; n   data() {} n   data(string name, int score) : name(name), score(score) {} n   const bool operator<(const data& other) const { n     if (score != other.score) return score > other.score; n     return name < other.name; n   } n   const bool operator==(const data& other) const { return name == other.name; } n }; n data vasya; n int n, m, b[100005]; n int best(vector<data> v) { n   sort(b, b + m); n   reverse(b, b + m); n   v.erase(find(v.begin(), v.end(), vasya)); n   sort(v.rbegin(), v.rend()); n   v.insert(v.begin(), vasya); n   for (int i = 0; i < m; i++) v[i].score += b[i]; n   sort(v.begin(), v.end()); n   return find(v.begin(), v.end(), vasya) - v.begin() + 1; n } n int worst(vector<data> v) { n   multiset<int> S; n   multiset<int>::iterator it; n   int idx; n   for (int i = 0; i < m; i++) S.insert(b[i]); n   idx = find(v.begin(), v.end(), vasya) - v.begin(); n   if (m == n) v[idx].score += *S.begin(), S.erase(S.begin()); n   sort(v.begin(), v.end()); n   idx = find(v.begin(), v.end(), vasya) - v.begin(); n   for (int i = idx + 1; i < (int)v.size() && S.size(); i++) { n     int add; n     if (v[i].name < vasya.name) n       add = v[idx].score - v[i].score; n     else n       add = v[idx].score - v[i].score + 1; n     it = S.lower_bound(add); n     if (it == S.end()) continue; n     v[i].score += *it; n     S.erase(it); n   } n   sort(v.begin(), v.end()); n   return find(v.begin(), v.end(), vasya) - v.begin() + 1; n } n int main() { n   cin >> n; n   vector<data> v(n); n   for (int i = 0; i < n; i++) cin >> v[i].name >> v[i].score; n   cin >> m; n   for (int i = 0; i < m; i++) cin >> b[i]; n   cin >> vasya.name; n   vasya.score = v[find(v.begin(), v.end(), vasya) - v.begin()].score; n   int place1 = best(v); n   int place2 = worst(v); n   cout << place1 <<     << place2 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n vector<vector<vector<int> > > dp; n vector<vector<int> > tmp; n vector<vector<int> > base; n int get_dig(int pos, int k) { n   return (pos < k) ? pos : (get_dig(pos / k, k) + pos % k) % k; n } n inline int get_val(const vector<vector<int> > &a, int i, int j, int shift, n                    int k) { n   if (i == k && j < k) { n     return a[k][(j + k - shift) % k]; n   } n   return a[(i + k - shift) % k][(j + k - shift) % k]; n } n vector<vector<int> > shift(const vector<vector<int> > &a, int shift) { n   int k = int(a.size()) - 1; n   vector<vector<int> > res = a; n   for (int i = 0; i <= k; ++i) { n     for (int j = 0; j < k; ++j) { n       res[i][j] = get_val(a, i, j, shift, k); n     } n   } n   return res; n } n vector<vector<int> > operator*(const vector<vector<int> > &a, n                                const vector<vector<int> > &b) { n   int n = int(a.size()), k = int(a[0].size()), m = int(b[0].size()); n   vector<vector<int> > res(n, vector<int>(m, 0)); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       for (int l = 0; l < k; ++l) { n         res[i][j] = (res[i][j] + 1ll * a[i][l] * b[l][j]) % MOD; n       } n     } n   } n   return res; n } n int main() { n   long long n; n   int k; n   cin >> n >> k; n   int max_depth = 0; n   { n     long long tmp = n; n     while (tmp != 0) { n       ++max_depth; n       tmp /= k; n     } n   } n   dp.resize(max_depth); n   { n     dp[0].assign(k + 1, vector<int>(k + 1, 0)); n     for (int i = 0; i <= k; ++i) { n       for (int j = 0; j <= k; ++j) { n         dp[0][i][j] = (i == j || j == 0); n       } n     } n     vector<vector<int> > e(k + 1, vector<int>(k + 1, 0)); n     for (int i = 0; i <= k; ++i) { n       e[i][i] = 1; n     } n     for (int depth = 1; depth < max_depth; ++depth) { n       dp[depth] = e; n       vector<vector<int> > bs = dp[depth - 1]; n       vector<vector<int> > &res = dp[depth]; n       int curx = k; n       for (int i = 0; (1 << i) <= curx; ++i) { n         if (curx & (1 << i)) { n           res = bs * shift(res, (1 << i)); n           curx ^= (1 << i); n         } n         bs = bs * shift(bs, (1 << i)); n       } n     } n   } n   base.assign(1, vector<int>(k + 1, 0)); n   base[0][k] = 1; n   int sl = 0; n   long long x = n; n   vector<int> vec; n   while (x != 0) { n     vec.push_back(x % k); n     x /= k; n   } n   int len = int(vec.size()); n   for (int cur_depth = len - 1; cur_depth >= 0; --cur_depth) { n     int rest = vec[cur_depth]; n     for (int it = 0; it < rest; ++it, sl = (sl + 1) % k) { n       base = base * shift(dp[cur_depth], sl); n     } n   } n   { n     int res = 0; n     for (int i = 0; i <= k; ++i) { n       res = (res + base[0][i]) % MOD; n     } n     cout << res <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 10001; n long long a[MAXN]; n int f[MAXN]; n long long c[MAXN], b[MAXN]; n int n; n int main() { n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   for (int i = 0; i < n; ++i) { n     long long X = a[i], cnt = 0; n     while (!(X & 1)) { n       ++cnt; n       X >>= 1; n     } n     b[i] = X; n     c[i] = cnt; n   } n   int ans = 0; n   for (int i = 0; i < n; ++i) { n     f[i] = 1; n     for (int j = 0; j < i; ++j) { n       if (a[j] % a[i] == 0 && a[i] % 2 == 1) f[i] = max(f[i], f[j] + 1); n       if (a[i] % 2 == 0) { n         long long t1 = b[j], t2 = b[i]; n         if (t1 % t2 == 0) { n           if (c[i] - c[j] == i - j || c[i] <= i - j - 1) n             f[i] = max(f[i], f[j] + 1); n         } n       } n     } n     if (f[i] > ans) ans = f[i]; n   } n   cout << n - ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int inf = (1 << 30) - 1, n, X, x, y, z, f[510][510], c[510], R, g[510][510], n     size[510], h[2][510], H[2][510][510], q[510], L[510]; n int Lng[510]; n double a[510][510]; n vector<int> E[510]; n void read(int &x) { n   char ch = getchar(); n   int mark = 1; n   for (; ch !=  -  && (ch <  0  || ch >  9 ); ch = getchar()) n     ; n   if (ch ==  - ) mark = -1, ch = getchar(); n   for (x = 0; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch - 48; n   x *= mark; n } n double Solve(int m, int n) { n   bool flag = 1; n   for (; flag;) { n     flag = 0; n     for (int i = 1; i <= n; i++) n       if (a[0][i] > 0) { n         double Min = inf; n         int l; n         for (int j = 1; j <= m; j++) n           if (a[j][i] < 0) { n             double tmp = -a[j][0] / a[j][i]; n             if (Min > tmp) Min = tmp, l = j; n           } n         if (Min == inf) continue; n         flag = 1; n         double tmp = -a[l][i]; n         a[l][i] = -1; n         for (int j = 0; j <= n; j++) a[l][j] /= tmp; n         for (int j = 0; j <= m; j++) n           if (a[j][i] && j != l) { n             for (int k = 0; k <= n; k++) n               if (k != i) a[j][k] += a[j][i] * a[l][k]; n             a[j][i] *= a[l][i]; n           } n       } n   } n   return a[0][0]; n } n void dfs(int u, int pre) { n   int cnt = 0; n   for (int i = 0; i < E[u].size(); i++) n     if (E[u][i] != pre) { n       dfs(E[u][i], u); n       ++cnt; n       Lng[u] = max(Lng[u], min(Lng[E[u][i]] + f[u][E[u][i]], inf)); n     } n   size[u] = 1; n   int A = 0; n   for (int j = 0; j <= R; j++) h[A][j] = inf; n   for (int Bg = 0; Bg <= cnt; Bg++) n     for (int j = 0; j <= R; j++) H[A][Bg][j] = inf; n   H[A][0][0] = inf - 1; n   h[A][1] = 0; n   cnt = 0; n   L[0] = -inf; n   for (int i = 0; i < E[u].size(); i++) n     if (E[u][i] != pre) { n       int v = E[u][i]; n       q[++cnt] = v; n       L[cnt] = min(inf, f[u][v] + Lng[v]); n       A ^= 1; n       for (int j = 0; j <= R; j++) h[A][j] = inf; n       for (int Bg = 0; Bg <= cnt; Bg++) n         for (int j = 0; j <= R; j++) H[A][Bg][j] = inf; n       for (int k = 0; k <= size[u] && k <= R; k++) n         for (int j = 0; j <= size[v] && j + k <= R; j++) { n           if (j == 0 && max(h[A ^ 1][k], Lng[v] + f[u][v]) <= X) n             h[A][j + k] = 0; n           if (j && g[v][j] != inf && n               max(h[A ^ 1][k], min(g[v][j], f[u][v])) <= X) n             h[A][j + k] = 0; n         } n       for (int Bg = 0; Bg < cnt; Bg++) n         for (int k = 0; k <= size[u] && k <= R; k++) n           if (H[A ^ 1][Bg][k] != inf) { n             for (int j = 1; j + k <= R && j <= size[v]; j++) n               if (g[v][j] <= X) n                 H[A][Bg][j + k] = min(H[A][Bg][j + k], n                                       min(H[A ^ 1][Bg][k], g[v][j] + f[u][v])); n             if (L[cnt] > L[Bg]) n               H[A][cnt][k] = min(H[A][cnt][k], H[A ^ 1][Bg][k]); n             else n               H[A][Bg][k] = min(H[A][Bg][k], H[A ^ 1][Bg][k]); n           } n       size[u] += size[v]; n     } n   for (int j = 0; j <= R; j++) g[u][j] = h[A][j]; n   for (int Bg = 0; Bg <= cnt; Bg++) n     for (int j = 0; j <= R; j++) n       if (H[A][Bg][j] + L[Bg] <= X) g[u][j] = min(g[u][j], H[A][Bg][j]); n } n bool check() { n   for (int i = 1; i <= n; i++) n     for (int j = 0; j <= n; j++) g[i][j] = inf; n   dfs(1, 0); n   return (g[1][R] <= X); n } n int main() { n   read(n); n   read(X); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) f[i][j] = inf; n     f[i][i] = 0; n     read(c[i]); n     R += c[i]; n   } n   for (int i = 1; i < n; i++) { n     read(x); n     read(y); n     read(z); n     f[x][y] = f[y][x] = z; n     E[x].push_back(y); n     E[y].push_back(x); n   } n   for (int k = 1; k <= n; k++) n     for (int i = 1; i <= n; i++) n       if (i ^ k) n         for (int j = 1; j <= n; j++) n           if ((i ^ j) && (k ^ j)) f[i][j] = min(f[i][k] + f[k][j], f[i][j]); n   if (!check()) { n     printf( -1 n ); n     return 0; n   } n   a[0][n + 1] = -R; n   for (int i = 1; i <= n; i++) { n     a[0][i] = 1; n     a[i][n + 1] = 1; n     a[i][0] = c[i] ^ 1; n     for (int j = 1; j <= n; j++) a[i][j] = -(f[i][j] <= X); n   } n   printf( %.0lf n , Solve(n, n + 1)); n   return 0; n } n


#include <iostream> n #include <vector> n #include <numeric> n #include <algorithm> n  n using namespace std; n   n int main(){ n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n     cout.tie(NULL); n     int t; n     cin >> t; n     while(t--){ n         int n; n         cin >> n; n         vector<long long> a(n); n         for (int i = 0; i < n; ++i){ n             cin >> a[i]; n         } n         sort(a.begin(), a.end()); n         long long ans = 0; n         for (int k = 1; k < n; ++k){ n             ans += a[k] * (2 * k + 1 - n); n         } n         ans -= a[n-1]; n          n         cout << -ans << endl; n     } n }


#include <bits/stdc++.h> n using namespace std; n struct line { n   int l, r; n } p[510], q[510]; n struct Point { n   long double x, y; n } Q[10], tmp[10]; n int vis[10]; n long double A, B, C; n int n, H, F, tot, cnt; n void read(int &x) { n   char ch = getchar(); n   int mark = 1; n   for (; ch !=  -  && (ch <  0  || ch >  9 ); ch = getchar()) n     ; n   if (ch ==  - ) mark = -1, ch = getchar(); n   for (x = 0; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch - 48; n   x *= mark; n } n inline bool cmp(line a, line b) { n   if (a.l == b.l) return a.r > b.r; n   return a.l < b.l; n } n inline long double Get(long double x, long double y) { n   return A * x + B * y + C; n } n inline long double Abs(long double x) { n   if (x < 0) return -x; n   return x; n } n inline long double dis(Point X, long double A, long double B, long double C) { n   return Abs(X.x * A + X.y * B + C) / sqrt(A * A + B * B); n } n inline long double Getsum(long double x1, long double y1, long double x2, n                           long double y2) { n   return x1 * y2 - x2 * y1; n } n Point Getp(Point X, Point Y, long double A, long double B, long double C) { n   long double d1 = dis(X, A, B, C), d2 = dis(Y, A, B, C); n   Point Ans; n   Ans.x = (d2 * X.x + d1 * Y.x) / (d1 + d2); n   Ans.y = (d2 * X.y + d1 * Y.y) / (d1 + d2); n   return Ans; n } n void solve() { n   int tot = 0; n   int s1 = 0, s2 = 0; n   for (int k = 1; k <= cnt; k++) { n     double t = Get(Q[k].x, Q[k].y); n     if (t == 0) n       vis[k] = -1, s2++; n     else if (t > 0) n       vis[k] = 1, s1++; n     else n       vis[k] = -1, s2++; n   } n   if (s1 == 0) { n     cnt = 0; n     return; n   } else if (s2 == 0) n     return; n   Q[cnt + 1] = Q[1]; n   vis[cnt + 1] = vis[1]; n   for (int i = 1; i <= cnt; i++) { n     if (vis[i] == 1) tmp[++tot] = Q[i]; n     if (vis[i] * vis[i + 1] < 0) { n       tmp[++tot] = Getp(Q[i], Q[i + 1], A, B, C); n     } n   } n   cnt = tot; n   for (int i = 1; i <= cnt; i++) Q[i] = tmp[i]; n } n int main() { n   read(n); n   read(H); n   read(F); n   for (int i = 1; i <= n; i++) read(p[i].l), read(p[i].r); n   sort(p + 1, p + n + 1, cmp); n   q[++tot] = p[1]; n   for (int i = 2; i <= n; i++) { n     if (p[i].l > q[tot].r) n       q[++tot] = p[i]; n     else n       q[tot].r = max(q[tot].r, p[i].r); n   } n   long double sum = 0; n   for (int i = 1; i <= tot; i++) { n     long double d = (long double)(F + H) / (F - H) * (q[i].r - q[i].l); n     sum += (d + q[i].r - q[i].l) * H * 2; n   } n   for (int i = 1; i <= tot; i++) { n     for (int j = 1; j <= tot; j++) { n       cnt = 4; n       Q[1].x = q[i].l; n       Q[1].y = -H; n       Q[2].x = q[i].r; n       Q[2].y = -H; n       Q[3].x = (long double)(F + H) / (F - H) * q[i].r; n       Q[3].y = H; n       Q[4].x = (long double)(F + H) / (F - H) * q[i].l; n       Q[4].y = H; n       A = H - F; n       B = -q[j].l; n       C = F * q[j].l; n       A = -A; n       B = -B; n       C = -C; n       solve(); n       if (cnt <= 2) continue; n       A = H - F; n       B = -q[j].r; n       C = F * q[j].r; n       solve(); n       if (cnt <= 2) continue; n       long double over = 0; n       for (int k = 2; k < cnt; k++) n         over += Getsum(Q[k].x - Q[1].x, Q[k].y - Q[1].y, Q[k + 1].x - Q[1].x, n                        Q[k + 1].y - Q[1].y) / n                 2; n       sum -= over; n     } n   } n   printf( %.7lf n , (double)sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m1, q; n int m[102][102]; n struct query { n   int t; n   int a; n   int b; n   int c; n }; n void rote(int type, int val) { n   if (type == 1) { n     int aux = m[val][m1]; n     for (int i = m1 - 1; i >= 1; i--) m[val][i + 1] = m[val][i]; n     m[val][1] = aux; n   } else { n     int aux = m[n][val]; n     for (int i = n - 1; i >= 1; i--) m[i + 1][val] = m[i][val]; n     m[1][val] = aux; n   } n } n int main() { n   cin >> n >> m1 >> q; n   stack<query> st; n   for (int i = 0; i < q; i++) { n     int in; n     cin >> in; n     if (in == 3) { n       int a, b, c; n       cin >> a >> b >> c; n       query qu; n       qu.t = in; n       qu.a = a; n       qu.b = b; n       qu.c = c; n       st.push(qu); n     } else { n       int a; n       cin >> a; n       query qu; n       qu.t = in; n       qu.a = a; n       st.push(qu); n     } n   } n   memset(m, 0, sizeof(m)); n   while (!st.empty()) { n     int type = st.top().t; n     if (type == 3) { n       int x, y, v; n       x = st.top().b; n       y = st.top().a; n       v = st.top().c; n       st.pop(); n       m[y][x] = v; n     } else { n       int a = st.top().a; n       st.pop(); n       rote(type, a); n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m1; j++) cout << m[i][j] <<    ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n const int maxn = 1005; n int n, m; n int a[maxn], b[maxn]; n bool solve(double v) { n   for (int i = 0; i < n; i++) { n     if ((m + v) / a[i] <= v) n       v -= (m + v) / a[i]; n     else n       return false; n     if ((m + v) / b[i] <= v) n       v -= (m + v) / b[i]; n     else n       return false; n   } n   if (v >= 0) return true; n   return false; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n   } n   scanf( %d , &b[n - 1]); n   for (int i = 0; i < n - 1; i++) { n     scanf( %d , &b[i]); n   } n   double left = -1, right = 1e9 + 1; n   while (left + 0.000001 < right) { n     double mid = (left + right) / 2; n     if (solve(mid)) { n       right = mid; n     } else { n       left = mid; n     } n   } n   if (right > 1e9 + 0.00001) n     puts( -1 ); n   else n     printf( %f n , right); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct info { n   long long int val, in; n }; n long long int arr[100010]; n info tree[100010 * 4], temp; n void init(long long int node, long long int b, long long int e) { n   if (b == e) { n     tree[node].val = arr[b]; n     tree[node].in = b; n     return; n   } n   long long int Left = node * 2; n   long long int Right = node * 2 + 1; n   long long int mid = (b + e) / 2; n   init(Left, b, mid); n   init(Right, mid + 1, e); n   if (tree[Left].val >= tree[Right].val) { n     tree[node].val = tree[Right].val; n     tree[node].in = tree[Right].in; n   } else { n     tree[node].val = tree[Left].val; n     tree[node].in = tree[Left].in; n   } n } n info query(long long int node, long long int b, long long int e, n            long long int i, long long int j) { n   if (i > e || j < b) { n     temp.in = temp.val = 30; n     return temp; n   } n   if (b >= i && e <= j) return tree[node]; n   long long int Left = node * 2; n   long long int Right = node * 2 + 1; n   long long int mid = (b + e) / 2; n   info p1 = query(Left, b, mid, i, j); n   info p2 = query(Right, mid + 1, e, i, j); n   if (p1.val >= p2.val) n     return p2; n   else n     return p1; n } n int main() { n   long long int i, j, len, mn = 30, m, last = 1, in; n   long long int used = -1; n   string s; n   vector<char> v, ans; n   char mxx =  a  - 1; n   cin >> m >> s; n   len = s.length(); n   for (i = 0; i < s.length(); i++) { n     arr[i + 1] = s[i] -  a  + 1; n   } n   init(1, 1, len); n   for (i = 1; i <= len - m + 1; i++) { n     info ret = query(1, 1, len, i, i + m - 1); n     if (used == -1) { n       used = ret.in; n       i = ret.in; n       last = ret.val; n       v.push_back(last - 1 +  a ); n       mxx = max(mxx, (char)(last - 1 +  a )); n     } else { n       if (ret.val == last && used == ret.in) { n       } else { n         used = ret.in; n         i = ret.in; n         last = ret.val; n         v.push_back(last - 1 +  a ); n         mxx = max(mxx, (char)(last - 1 +  a )); n       } n     } n   } n   if (v.size() == 0) v.push_back(s[0]); n   sort(v.begin(), v.end()); n   long long int inx = v.size() - 1; n   long long int cnt = 0; n   for (i = 0; i < v.size(); i++) { n     if (v[i] == v[inx]) cnt++; n   } n   for (i = 0; i < s.length(); i++) { n     if (s[i] < v[inx]) ans.push_back(s[i]); n   } n   for (i = 1; i <= cnt; i++) { n     ans.push_back(v[inx]); n   } n   sort(ans.begin(), ans.end()); n   for (i = 0; i < ans.size(); i++) { n     cout << ans[i]; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 5e3 + 3; n const int INF = 2000000000; n inline long long ab(long long x) { return x < 0 ? -x : x; } n long long a[MAXN]; n int c_a[MAXN]; n int l[MAXN], r[MAXN], x[MAXN], t[MAXN]; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     a[i] = INF; n   } n   for (int q = 0; q < m; q++) { n     cin >> t[q] >> l[q] >> r[q] >> x[q]; n     --l[q], --r[q]; n   } n   for (int q = m - 1; q >= 0; q--) { n     if (t[q] == 1) { n       for (int i = l[q]; i <= r[q]; i++) { n         a[i] -= x[q]; n       } n     } else { n       bool found = false; n       for (int i = l[q]; i <= r[q]; i++) { n         if (a[i] >= x[q]) { n           a[i] = x[q]; n           found = true; n         } n       } n       if (!found) { n         cout <<  NO ; n         return 0; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     if (a[i] < 0) { n       if (ab(a[i]) > INF / 2) a[i] = -INF / 2; n     } else { n       if (a[i] > INF / 2) a[i] = INF / 2; n     } n   } n   for (int i = 0; i < n; i++) { n     c_a[i] = a[i]; n   } n   for (int q = 0; q < m; q++) { n     if (t[q] == 1) { n       for (int i = l[q]; i <= r[q]; i++) { n         a[i] += x[q]; n       } n     } else { n       long long maxv = -INF; n       for (int i = l[q]; i <= r[q]; i++) { n         maxv = max(maxv, a[i]); n       } n       if (maxv != x[q]) { n         cout <<  NO ; n         return 0; n       } n     } n   } n   cout <<  YES n ; n   for (int i = 0; i < n; i++) { n     if (i > 0) cout <<    ; n     cout << c_a[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 2000000000000000000LL; n const int inf = 0x3f3f3f3f; n const long double EPS = 1e-9; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   long long x, i, j, n = 0, t; n   cin >> x; n   while (x / 2 != 0) { n     if (x % 2 == 1) n++; n     x = x / 2; n   } n   cout << n + 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 105; n long long g[N][N], f[N][N], a[N], d; n int n; n pair<int, int> p[N]; n void FloydWarshall() { n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) f[i][j] = g[i][j]; n     f[i][i] = 0; n   } n   for (int k = 1; k <= n; k++) { n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n; j++) { n         f[i][j] = min(f[i][j], f[i][k] + f[k][j]); n       } n     } n   } n } n int main() { n   scanf( %d%lld , &n, &d); n   for (int i = 2; i < n; i++) scanf( %lld , &a[i]); n   for (int i = 1; i <= n; i++) scanf( %d%d , &p[i].first, &p[i].second); n   for (int i = 2; i < n; i++) { n     for (int j = 1; j <= n; j++) { n       if (i == j) continue; n       g[i][j] -= a[i]; n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (i == j) continue; n       g[i][j] += n           1ll * d * n           (abs(p[i].first - p[j].first) + abs(p[i].second - p[j].second)); n     } n   } n   FloydWarshall(); n   printf( %lld , f[1][n]); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int lpd(int n) { n   int ans = -1; n   for (int i = 2; i * i <= n; i++) { n     if (n % i == 0) { n       ans = i; n       while (n % i == 0) n /= i; n     } n   } n   if (n > 1) ans = n; n   return ans; n } n int x, p[1000006], pd, t, ans; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   p[0] = p[1] = 1; n   for (int i = 2; i * i <= 1e6; i++) { n     if (p[i] == 0) { n       for (int j = i * i; j <= 1e6; j += i) { n         p[j] = 1; n       } n     } n   } n   cin >> x; n   ans = 1e9; n   pd = lpd(x); n   t = x / pd; n   t -= 1; n   if (p[pd * t + 1] == 0 && (pd * t + 2 == x)) { n     cout << (x - 1) <<   n ; n     return 0; n   } n   for (int i = pd * t + 1; i < x; i++) { n     if (p[i] == 0) continue; n     pd = lpd(i); n     t = i / pd; n     t -= 1; n     ans = min(ans, pd * t + 1); n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long int dp[4011][4011]; n long long int b[2000000]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int n; n   cin >> n; n   long long int a[n]; n   set<long long int> st; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     st.insert(a[i]); n   } n   long long int k = 0; n   for (auto u : st) { n     b[u] = k; n     k++; n   } n   for (int i = 0; i < n; i++) { n     a[i] = b[a[i]]; n   } n   for (int i = 0; i < n; i++) { n     long long int f = 0; n     for (int j = i - 1; j >= 0; j--) { n       if (a[i] == a[j]) { n         if (f == 1) continue; n         f = 1; n         dp[a[i]][a[j]]++; n       } else { n         dp[a[i]][a[j]] = max(dp[a[i]][a[j]], 1 + dp[a[j]][a[i]]); n       } n     } n   } n   long long int ans = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) ans = max(ans, dp[i][j]); n   } n   cout << ans + 1; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a = 1, b, c = 0, d = 0, e = 0, f, i = 0, k = 0, j = 0, n = 0, t = 0, n       l = 0, m = 0, h[3000] = {0}; n   string s, x; n   cin >> t; n   n = 1000000; n   for (i = 0; i < t; i++) { n     cin >> b; n     j = max(j, min(b - 1, 1000000 - b)); n   } n   cout << j; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k; n long long cp1[102]; n long long cp2[102]; n long long dp[102][10002]; n long long comb[102][102]; n bool mark[102][10002]; n long long Calculate_Pow(long long x, long long y) { n   long long val = 1; n   for (long long i = 1, t = x; i <= y; i <<= 1, t = (t * t) % 1000000007) n     if (y & i) val = (val * t) % 1000000007; n   return val; n } n void Calculate_Combination() { n   for (long long i = 0; i <= n; i++) comb[i][0] = 1; n   for (long long i = 1; i <= n; i++) n     for (long long j = 1; j <= i; j++) n       comb[i][j] = (comb[i - 1][j - 1] + comb[i - 1][j]) % 1000000007; n } n long long f(long long col, long long last) { n   if (last < 0) return 0; n   if (col == n && last) return 0; n   if (col == n) return 1; n   if (mark[col][last]) return dp[col][last]; n   mark[col][last] = true; n   if (col >= m % n) { n     for (long long i = 0; i <= n; i++) n       dp[col][last] = n           (dp[col][last] + cp1[i] * f(col + 1, last - i)) % 1000000007; n   } else { n     for (long long i = 0; i <= n; i++) n       dp[col][last] = n           (dp[col][last] + cp2[i] * f(col + 1, last - i)) % 1000000007; n   } n   return dp[col][last]; n } n int main() { n   scanf( %I64d%I64d%I64d , &n, &m, &k); n   Calculate_Combination(); n   for (long long i = 0; i <= n; i++) { n     cp1[i] = Calculate_Pow(comb[n][i], m / n); n     cp2[i] = Calculate_Pow(comb[n][i], m / n + 1); n   } n   printf( %I64d n , f(0, k)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 5; n const int mod = 1e9 + 7; n vector<int> G[maxn]; n char s[maxn]; n int vis[maxn]; n map<string, int> mp; n long long a[maxn], n, L, R, t; n void solve1() { n   L = 0, R = n; n   for (int i = 0; i <= n - 2; i++) { n     if (s[i] ==  < ) n       L++; n     else { n       for (int i = R - L; i <= R; i++) printf( %d  , i); n       R = R - L - 1, L = 0; n     } n   } n   for (int i = R - L; i <= R && i >= 1; i++) printf( %d  , i); n   printf(  n ); n } n void solve2() { n   L = 0, R = 1; n   for (int i = 0; i <= n - 2; i++) { n     if (s[i] ==  > ) n       L++; n     else { n       for (int i = R + L; i >= R; i--) printf( %d  , i); n       R = R + L + 1, L = 0; n     } n   } n   for (int i = R + L; i >= R && i <= n; i--) printf( %d  , i); n   printf(  n ); n } n int main() { n   cin >> t; n   while (t--) { n     scanf( %lld%s , &n, s); n     solve1(); n     solve2(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int C = 26; n void add(int &x, int y) { n   x += y; n   while (x >= MOD) x -= MOD; n   while (x < 0) x += MOD; n } n int fix(int x) { n   while (x >= MOD) x -= MOD; n   while (x < 0) x += MOD; n   return x; n } n int pw(int a, int b) { n   int ret = 1; n   while (b) { n     if (b & 1) ret = 1ll * ret * a % MOD; n     b >>= 1; n     a = 1ll * a * a % MOD; n   } n   return ret; n } n const int MAXN = 1e6 + 10; n int a[MAXN], ans[MAXN], n, K, sz; n vector<int> who[MAXN], vec[2][2]; n void solve() { n   string s; n   cin >> s; n   n = s.size(); n   for (int i = 0; i < n; i++) a[i] = s[i] ==  L ; n   set<int> avail[2]; n   for (int i = 0; i < n; i++) { n     if (avail[a[i] ^ 1].empty()) { n       avail[a[i]].insert(K); n       who[K].push_back(i); n       K++; n     } else { n       int x = *avail[a[i] ^ 1].begin(); n       avail[a[i] ^ 1].erase(x); n       who[x].push_back(i); n       avail[a[i]].insert(x); n     } n   } n   for (int i = 0; i < K; i++) { n     vec[a[who[i][0]]][a[who[i].back()]].push_back(i); n   } n   int cur = vec[0][0].size() < vec[1][1].size(); n   int last = vec[0][0].size() > vec[1][1].size(); n   while ((int)max(vec[0][0].size(), vec[1][1].size()) > 1) { n     int i = vec[cur][cur].back(); n     vec[cur][cur].pop_back(); n     for (int x : who[i]) ans[sz++] = x; n     cur ^= 1; n   } n up: n   if (vec[0][0].size() && vec[1][1].size()) { n     if (last) { n       for (int i : vec[1][1]) n         for (int x : who[i]) ans[sz++] = x; n       for (int i : vec[0][1]) n         for (int x : who[i]) ans[sz++] = x; n       for (int i : vec[0][0]) n         for (int x : who[i]) ans[sz++] = x; n       for (int i : vec[1][0]) n         for (int x : who[i]) ans[sz++] = x; n     } else { n       for (int i : vec[0][0]) n         for (int x : who[i]) ans[sz++] = x; n       for (int i : vec[1][0]) n         for (int x : who[i]) ans[sz++] = x; n       for (int i : vec[1][1]) n         for (int x : who[i]) ans[sz++] = x; n       for (int i : vec[0][1]) n         for (int x : who[i]) ans[sz++] = x; n     } n   } else { n     if (vec[0][0].size()) { n       for (int i : vec[0][1]) n         for (int x : who[i]) ans[sz++] = x; n       for (int i : vec[0][0]) n         for (int x : who[i]) ans[sz++] = x; n       for (int i : vec[1][0]) n         for (int x : who[i]) ans[sz++] = x; n     } else if (vec[1][1].size()) { n       for (int i : vec[1][0]) n         for (int x : who[i]) ans[sz++] = x; n       for (int i : vec[1][1]) n         for (int x : who[i]) ans[sz++] = x; n       for (int i : vec[0][1]) n         for (int x : who[i]) ans[sz++] = x; n     } else if (!!vec[0][1].size() ^ !!vec[1][0].size()) { n       for (int i : vec[1][0]) n         for (int x : who[i]) ans[sz++] = x; n       for (int i : vec[0][1]) n         for (int x : who[i]) ans[sz++] = x; n     } else { n       int i = vec[0][1].back(); n       vec[0][1].pop_back(); n       int j = vec[1][0].back(); n       for (int k = 1; k < (int)who[i].size(); k++) who[j].push_back(who[i][k]); n       while ((int)who[i].size() > 1) who[i].pop_back(); n       vec[0][0].push_back(i); n       goto up; n     } n   } n   cout << K - 1 <<   n ; n   for (int i = 0; i < n; i++) cout << ans[i] + 1 <<    ; n   cout <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int te = 1; n   for (int w = 1; w <= te; w++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<int> a[2002]; n int b[2002]; n vector<int> ans[4]; n void Work() { n   int l; n   int maxT = 0; n   int specialNum = -1; n   for (int i = 0; i < n; i++) { n     cin >> l; n     a[l].push_back(i + 1); n     if (l > maxT) maxT = l; n     b[l]++; n     if (b[l] >= 3) specialNum = l; n   } n   int check = 0; n   for (int i = 0; i <= maxT; i++) { n     if (b[i] >= 2) check++; n     if (b[i] >= 3) check++; n     if (check == 2) break; n   } n   if (check < 2) { n     cout <<  NO ; n     return; n   } n   for (int i = 1; i <= maxT; i++) { n     for (int j = 0; j < a[i].size(); j++) { n       if (a[i][j] > 0) { n         ans[1].push_back(a[i][j]); n       } n     } n   } n   for (int i = 1; i <= maxT; i++) { n     for (int j = a[i].size() - 1; j >= 0; j--) { n       if (a[i][j] > 0) { n         ans[2].push_back(a[i][j]); n       } n     } n   } n   bool tt = false; n   if (specialNum == -1) n     for (int i = 1; i <= maxT; i++) { n       if (b[i] >= 2 && !tt) { n         for (int j = a[i].size() - 1; j >= 0; j--) { n           if (a[i][j] > 0) { n             ans[3].push_back(a[i][j]); n             tt = true; n           } n         } n       } else { n         for (int j = 0; j < a[i].size(); j++) { n           if (a[i][j] > 0) { n             ans[3].push_back(a[i][j]); n           } n         } n       } n     } n   else { n     for (int i = 1; i <= specialNum; i++) { n       if (i != specialNum) n         for (int j = a[i].size() - 1; j >= 0; j--) { n           if (a[i][j] > 0) { n             ans[3].push_back(a[i][j]); n             tt = true; n           } n         } n     } n     for (int i = 0; i < a[specialNum].size() / 2; i++) { n       if (a[specialNum][i] > 0) { n         ans[3].push_back(a[specialNum][i]); n       } n     } n     for (int i = a[specialNum].size() - 1; i >= a[specialNum].size() / 2; i--) { n       if (a[specialNum][i] > 0) { n         ans[3].push_back(a[specialNum][i]); n       } n     } n     for (int i = specialNum + 1; i <= maxT; i++) { n       if (i != specialNum) n         for (int j = a[i].size() - 1; j >= 0; j--) { n           if (a[i][j] > 0) { n             ans[3].push_back(a[i][j]); n             tt = true; n           } n         } n     } n   } n   cout <<  YES  << endl; n   for (int i = 1; i <= 3; i++) { n     for (int j = 0; j < ans[i].size(); j++) { n       cout << ans[i][j]; n       if (j < ans[i].size() - 1) cout <<    ; n     } n     if (i != 3) cout << endl; n   } n } n int main() { n   cin >> n; n   Work(); n } n


#include <bits/stdc++.h> n int main() { n   uint64_t a, b, x; n   std::cin >> a >> b; n   a < b | a - b & 1 n       ? std::cout << -1 n       : (x = a - b >> 1, n          x ^ a - x ^ b ? std::cout << -1 : std::cout << x <<     << a - x); n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   char ch = getchar(); n   int w = 1, c = 0; n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) w = -1; n   for (; isdigit(ch); ch = getchar()) c = (c << 3) + (c << 1) + (ch ^ 48); n   return w * c; n } n const int mod = 998244353, M = 1e6 + 10; n int n; n long long fac[M], inv[M]; n long long C(int x, int y) { n   if (y > x || y < 0) return 0; n   return fac[x] * inv[y] % mod * inv[x - y] % mod; n } n char s[M]; n long long fast(long long x, long long p) { n   long long ret = 1; n   for (; p; p >>= 1, x = x * x % mod) n     if (p & 1) ret = ret * x % mod; n   return ret; n } n int main() { n   scanf( %s , s + 1); n   n = strlen(s + 1); n   fac[0] = 1; n   for (int i = 1; i <= n; ++i) fac[i] = fac[i - 1] * i % mod; n   inv[n] = fast(fac[n], mod - 2); n   for (int i = n; i >= 1; --i) inv[i - 1] = inv[i] * i % mod; n   int l = 0, x = 0, y = 0, r = 0; n   for (int i = 1; i <= n; ++i) { n     if (s[i] ==  ( ) l++; n     if (s[i] ==  ? ) x++; n   } n   long long ans = 0; n   for (int i = n; i >= 1; --i) { n     if (s[i] ==  ( ) l--; n     if (s[i] ==  ) ) r++; n     if (s[i] ==  ? ) x--, y++; n     ans = n         (ans + C(x + y, y + r - l) * l + C(x + y - 1, y + r - l - 1) * x) % mod; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000010; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n int lcm(int a, int b) { return a * b / gcd(a, b); } n int main(void) { n   int c, d, n, m, k, i, j, ans = 10000; n   scanf( %d %d %d %d %d , &c, &d, &n, &m, &k); n   for (i = 0; i <= 10000; i++) n     for (j = 0; j <= 10000; j++) n       if (i * n + j >= n * m - k) ans = min(ans, i * c + j * d); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n static const int INF = 0x3f3f3f3f; n static const long long INFL = 0x3f3f3f3f3f3f3f3fLL; n const int lim = 2001; n int A[lim]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %d , &A[i]); n   long long int total = 0ll; n   for (int i = 0; i < n; ++i) { n     for (int j = i + 1; j < n; ++j) { n       if (A[i] > A[j]) total = total + 1ll; n     } n   } n   int q; n   scanf( %d , &q); n   while (q--) { n     int a, b; n     scanf( %d %d , &a, &b); n     total = total + ((b - a + 1) * 1ll * (b - a)) / 2ll; n     if (total % 2 == 1ll) n       printf( odd n ); n     else n       printf( even n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int p[105][105]; n char s[105][105]; n int n, m; n long long k; n void color(int x, int y, char c) { n   while (x < n && y >= 0) { n     s[x][y] = c; n     x++; n     y--; n   } n } n vector<pair<pair<int, int>, pair<int, int> > > v; n vector<int> vals; n long long mem[305][305]; n int vis[305][305]; n int id; n long long add(long long x, long long y) { n   if (x < 0 || y < 0) return -1; n   long long z = x + y; n   if (z >= k) return -1; n   return z; n } n long long solve(int ind, int r) { n   if (r < 0) return 0; n   if (ind >= int(vals.size())) return r == 0; n   if (vis[ind][r] == id) return mem[ind][r]; n   vis[ind][r] = id; n   long long ans = 0; n   if (vals[ind] != 1) ans = add(ans, solve(ind + 1, r + 1)); n   if (vals[ind] != 0) ans = add(ans, solve(ind + 1, r - 1)); n   return mem[ind][r] = ans; n } n void gen(int ind) { n   if (ind >= int(vals.size())) return; n   vals[v[ind].first.second] = 0; n   id++; n   long long tmp = solve(0, 0); n   if (tmp == -1) { n     color(v[ind].second.first, v[ind].second.second,  ( ); n     gen(ind + 1); n   } else { n     vals[v[ind].first.second] = 1; n     k -= tmp; n     color(v[ind].second.first, v[ind].second.second,  ) ); n     gen(ind + 1); n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   cin >> k; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) cin >> p[i][j]; n   for (int j = 0; j < m; j++) { n     int x = 0, y = j, pr = n * m + 1; n     while (x < n && y >= 0) { n       pr = min(pr, p[x][y]); n       x++; n       y--; n     } n     v.push_back(make_pair(make_pair(pr, v.size()), make_pair(0, j))); n     vals.push_back(-1); n   } n   for (int i = 1; i < n; i++) { n     int x = i, y = m - 1, pr = n * m + 1; n     while (x < n && y >= 0) { n       pr = min(pr, p[x][y]); n       x++; n       y--; n     } n     v.push_back(make_pair(make_pair(pr, v.size()), make_pair(i, m - 1))); n     vals.push_back(-1); n   } n   sort(v.begin(), v.end()); n   gen(0); n   for (int i = 0; i < n; i++) cout << s[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n int lcm(int a, int b) { return a / gcd(a, b) * b; } n long long powmod(long long a, long long b, long long MOD) { n   long long ans = 1; n   while (b) { n     if (b % 2) ans = ans * a % MOD; n     a = a * a % MOD; n     b /= 2; n   } n   return ans; n } n int main() { n   int t; n   int n, x, y, d; n   ios::sync_with_stdio(false); n   string a; n   cin >> n; n   cin >> a; n   int l = a.size(); n   vector<int> v; n   v.push_back(-1); n   int fa = n + 12, fb = -23; n   for (int i = 0; i < l; i++) { n     if (a[i] ==  S ) v.push_back(i); n     if (fa == n + 12 && a[i] ==  G ) fa = i; n     if (a[i] ==  G ) fb = i; n   } n   v.push_back(n); n   if (v.size() == 2) { n     cout << n << endl; n     return 0; n   } else { n     int ma = 0; n     for (int i = 1; i < v.size() - 1; i++) { n       int xa = v[i + 1] - v[i - 1] - 1; n       if (v[i + 1] > fb && v[i - 1] < fa) xa--; n       if (ma < xa) ma = xa; n     } n     cout << ma << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int mod = 1e9 + 7; n const double eps = 1e-6; n const int maxn = 1 << 17; n int a[maxn]; n void merge(int l, int r, int from, int to, int& res) { n   if (res <= 0 || l + 1 == r) { n     for (int i = l, j = from; i <= r; i++, j++) a[i] = j; n     return; n   } n   res -= 2; n   int m = (l + r) >> 1; n   int mid = (from + to + 1) >> 1; n   merge(l, m, mid, to, res); n   merge(m, r, from, mid, res); n } n int main() { n   int n, k; n   while (cin >> n >> k) { n     k--; n     merge(0, n, 0, n, k); n     if (k) n       cout << -1 << endl; n     else { n       for (int i = 0; i < n; i++) { n         if (i) cout <<    ; n         cout << a[i] + 1; n       } n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1e4 + 9; n long long a[maxn]; n long long n; n void solve() { n   cin >> n; n   long long sum = 0; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     sum += a[i]; n   } n   if (sum % n != 0) { n     cout <<  -1 n ; n     return; n   } n   sum /= n; n   long long j = -1; n   for (long long i = n - 1; i >= 1; i--) { n     if (a[i] >= i + 1) { n       j = i; n       break; n     } n   } n   vector<pair<pair<long long, long long>, long long> > ans; n   if (j != -1) { n     ans.emplace_back(make_pair(j, 0), 1); n     a[j] -= j + 1; n     a[0] += j + 1; n     for (long long e = j; e >= 1; e--) { n       if (a[e] % (e + 1) != 0) { n         long long x = (e + 1) - a[e] % (e + 1); n         ans.emplace_back(make_pair(0, e), x); n         a[0] -= x; n         a[e] += x; n       } n       ans.emplace_back(make_pair(e, 0), a[e] / (e + 1)); n       a[0] += a[e]; n       a[e] = 0; n     } n   } else { n     j = 0; n   } n   vector<pair<long long, long long> > kek; n   for (long long e = j + 1; e < n; e++) { n     kek.emplace_back((e + 1) - a[e], e); n   } n   sort(kek.begin(), kek.end()); n   for (auto x : kek) { n     if (a[0] >= x.first) { n       ans.emplace_back(make_pair(0, x.second), x.first); n       ans.emplace_back(make_pair(x.second, 0), 1); n       a[0] += a[x.second]; n       a[x.second] = 0; n     } else { n       cout <<  -1 n ; n       return; n     } n   } n   for (long long i = 1; i < n; i++) { n     ans.emplace_back(make_pair(0, i), sum); n   } n   if ((long long)ans.size() > 3 * n) { n     cout <<  -1 n ; n     return; n   } n   cout << ans.size() <<   n ; n   for (auto x : ans) n     cout << x.first.first + 1 <<     << x.first.second + 1 <<     << x.second n          <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) solve(); n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   srand(time(NULL)); n   ; n   long long t = 1; n   while (t--) { n     long long n; n     cin >> n; n     long long a[n], b[n]; n     for (long long i = 0; i < n; i++) cin >> a[i]; n     multiset<long long> s; n     for (long long i = 0; i < n; i++) cin >> b[i], s.insert(b[i]); n     vector<long long> c(n); n     for (long long i = 0; i < n; i++) { n       multiset<long long>::iterator it = s.begin(); n       long long ans = (a[i] + *it) % n, num = *it; n       it = s.lower_bound(n - a[i]); n       if (it != s.end() && (a[i] + *it) % n < ans) { n         ans = (a[i] + *it) % n; n         num = *it; n       } n       c[i] = ans; n       s.erase(s.find(num)); n     } n     for (long long i = 0; i < n; i++) cout << c[i] <<    ; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int ans = 9999; n string s, t; n int main() { n   cin >> s >> t; n   for (int i = 0; i < t.size(); i++) s =  .  + s +  . ; n   for (int i = 0; i + t.size() - 1 < s.size(); i++) { n     int temp = 0; n     for (int j = 0; j < t.size(); j++) temp += (s[i + j] != t[j]); n     if (ans > temp) ans = temp; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int a[200005]; n vector<long long int> g[200005]; n vector<long long int> c[200005]; n multiset<long long int> st[200005]; n long long int idx[200005], toAdd[200005], ans[200005]; n void mergeTree(long long int id1, long long int id2) { n   multiset<long long int>::iterator it; n   long long int x; n   for (it = st[id2].begin(); it != st[id2].end(); it++) { n     x = *it + toAdd[id2] - toAdd[id1]; n     st[id1].insert(x); n   } n   return; n } n void dfs(long long int u) { n   idx[u] = u; n   long long int i, j, k, mx; n   mx = st[idx[u]].size(); n   long long int id = idx[u]; n   for (i = 0; i < g[u].size(); i++) { n     long long int v = g[u][i]; n     dfs(v); n     if (st[idx[v]].size() > mx) { n       mx = st[idx[v]].size(); n       id = idx[v]; n     } n     toAdd[idx[v]] -= c[u][i]; n   } n   idx[u] = id; n   for (i = 0; i < g[u].size(); i++) { n     long long int v = g[u][i]; n     if (id != idx[v]) mergeTree(id, idx[v]); n     st[id].insert((a[v] - c[u][i]) - toAdd[id]); n   } n   multiset<long long int>::iterator it; n   while (!st[id].empty()) { n     it = st[id].begin(); n     if ((*it + toAdd[id]) < 0) n       st[id].erase(it); n     else n       break; n   } n   ans[u] = (long long int)st[id].size(); n   return; n } n int main() { n   long long int i, j, k, l, m, n; n   cin >> n; n   for (i = 1; i <= n; i++) scanf( %lld , &a[i]); n   for (i = 2; i <= n; i++) { n     scanf( %lld %lld , &k, &l); n     g[k].push_back(i); n     c[k].push_back(l); n   } n   memset(toAdd, 0, sizeof(toAdd)); n   dfs(1); n   for (i = 1; i <= n; i++) { n     printf( %lld , ans[i]); n     if (i < n) n       printf(   ); n     else n       printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mas[100500]; n int main() { n   int n, k, p = 1, t; n   bool cur = false; n   char q; n   string s =   , s1; n   cin >> n >> k; n   mas[1] = 1; n   for (int i = 2; i <= n; i++) { n     if (p < k) { n       if (!cur) n         mas[i] = n - mas[i - 1] + 1; n       else n         mas[i] = mas[i - 2] + 1; n       if (!cur) n         cur = true; n       else n         cur = false; n       p++; n     } else { n       if (!cur) { n         mas[i] = mas[i - 1] + 1; n       } else { n         mas[i] = mas[i - 1] - 1; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     s1 =   ; n     while (mas[i] > 0) { n       q = mas[i] % 10 +  0 ; n       mas[i] /= 10; n       s1 = q + s1; n     } n     s += s1 +    ; n   } n   cout << s; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gr[1000005]; n int was[1000005]; n long long GetPow(long long val, int deg) { n   long long ret = 1; n   for (int i = 0; i < deg; ++i) { n     ret *= val; n   } n   return ret; n } n long long arr[100000]; n int main() { n   long long sq4 = 2, sq2 = 2, val4 = 16, val2 = 4; n   int least = 0; n   for (int i = 4; i < 1000005; ++i) { n     if (val2 == i) { n       was[gr[sq2]]++; n       ++sq2; n       val2 = GetPow(sq2, 2); n       while (was[least]) { n         ++least; n       } n     } n     gr[i] = least; n     if (val4 == i) { n       was[gr[sq4]]--; n       if (!was[gr[sq4]] && gr[sq4] < least) { n         least = gr[sq4]; n       } n       ++sq4; n       val4 = GetPow(sq4, 4); n     } n   } n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> arr[i]; n   } n   sort(arr, arr + n); n   sq2 = 1; n   sq4 = 1; n   val2 = 1; n   val4 = 1; n   long long xr = 0, cur = 0; n   memset(was, 0, sizeof was); n   least = 0; n   for (int i = 0; i < n; ++i) { n     if (arr[i] == 1) { n       continue; n     } n     while (val2 <= arr[i]) { n       was[gr[sq2]]++; n       ++sq2; n       val2 = GetPow(sq2, 2); n     } n     while (val4 < arr[i]) { n       was[gr[sq4]]--; n       if (!was[gr[sq4]] && least > gr[sq4]) { n         least = gr[sq4]; n       } n       ++sq4; n       val4 = GetPow(sq4, 4); n     } n     while (was[least]) { n       ++least; n     } n     xr ^= least; n   } n   if (xr) { n     puts( Furlo ); n   } else { n     puts( Rublo ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18; n const int32_t M = 1e9 + 7; n const int32_t MM = 998244353; n long long m, n; n void solve() { n   cin >> n >> m; n   bool visited[n + 5]; n   memset(visited, 0, sizeof(visited)); n   for (long long i = 0; i < m; i++) { n     long long a, b, c; n     cin >> a >> b >> c; n     visited[b] = 1; n   } n   long long node = -1; n   for (long long i = 1; i < n + 1; i++) n     if (!visited[i]) { n       node = i; n       break; n     } n   for (long long i = 1; i < n + 1; i++) { n     if (i == node) continue; n     cout << node <<     << i <<   n ; n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n long long solve(long long n, string str1, string str2) { n   long long t1 = 0, t2 = 0; n   for (long long i = 0; i < n; i++) { n     if (str1[i] != str2[i]) { n       if (str1[i] ==  1 ) n         t1++; n       else n         t2++; n     } n   } n   if (t1 != t2) n     return -1; n   else n     return 2 * t1; n } n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     string str1, str2; n     cin >> n; n     cin >> str1 >> str2; n     long long a1 = solve(n, str1, str2); n     long long ind = -1; n     for (long long i = 0; i < n; i++) { n       if (str1[i] ==  1  && str2[i] ==  1 ) { n         ind = i; n         break; n       } n     } n     long long a2 = -1; n     string temp = str1; n     if (ind != -1) { n       for (long long i = 0; i < n; i++) { n         if (str1[i] ==  1  && i != ind) n           str1[i] =  0 ; n         else if (str1[i] ==  0 ) n           str1[i] =  1 ; n       } n       a2 = solve(n, str1, str2); n     } n     long long a3 = -1; n     str1 = temp; n     long long ind2 = -1; n     for (long long i = 0; i < n; i++) { n       if (str1[i] ==  1  && str2[i] ==  0 ) { n         ind2 = i; n         break; n       } n     } n     if (ind2 != -1) { n       for (long long i = 0; i < n; i++) { n         if (str1[i] ==  1  && i != ind2) n           str1[i] =  0 ; n         else if (str1[i] ==  0 ) n           str1[i] =  1 ; n       } n       a3 = solve(n, str1, str2); n     } n     if (a1 == -1 && a2 == -1 && a3 == -1) { n       cout <<  -1  << endl; n     } else { n       if (a1 == -1 && a2 == -1) n         cout << (a3 + 1) << endl; n       else if (a1 == -1 && a3 == -1) n         cout << (a2 + 1) << endl; n       else if (a2 == -1 && a3 == -1) n         cout << a1 << endl; n       else if (a1 == -1) n         cout << min(a2 + 1, a3 + 1) << endl; n       else if (a2 == -1) n         cout << min(a1, a3 + 1) << endl; n       else if (a3 == -1) n         cout << min(a1, a2 + 1) << endl; n       else { n         long long b = min(a2 + 1, a3 + 1); n         long long ans = min(b, a1); n         cout << ans << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double PI = acos(-1); n vector<int> adj[100005]; n bool vis[100005]; n map<pair<int, int>, long long> edge; n map<pair<int, int>, pair<long long, int>> weight; n long long mod = 1e9 + 7; n int n, k; n long long dfs(int node) { n   vis[node] = 1; n   long long ans = 1; n   for (auto v : adj[node]) { n     if (!vis[v]) { n       long long a = dfs(v); n       edge[{v, node}] = a; n       edge[{node, v}] = a; n       ans += a; n     } n   } n   if (ans > 1) ans--; n   return ans; n } n void clc() { n   for (int i = 0; i <= n; i++) { n     adj[i].clear(); n     vis[i] = 0; n   } n   edge.clear(); n   weight.clear(); n } n void test_case() { n   long long sum = 0, ans = 1e8, s; n   cin >> n >> s; n   pair<int, int> arr[n]; n   vector<long long> v1, v2; n   v1.push_back(0), v2.push_back(0); n   for (int i = 0; i < n - 1; i++) { n     int x, y, c, w; n     cin >> x >> y >> c >> w; n     adj[x].push_back(y), adj[y].push_back(x); n     arr[i] = {x, y}; n     weight[{x, y}] = {c, w}, weight[{y, x}] = {c, w}; n   } n   dfs(1); n   for (int i = 0; i < n; i++) { n     long long x = edge[{arr[i].first, arr[i].second}], n               y = weight[{arr[i].first, arr[i].second}].first; n     sum += x * y; n     while (y) { n       if (weight[{arr[i].first, arr[i].second}].second == 1) n         v1.push_back(x * y - x * (y / 2)); n       else n         v2.push_back(x * y - x * (y / 2)); n       y /= 2; n     } n   } n   sort(v1.begin() + 1, v1.end()); n   sort(v2.begin() + 1, v2.end()); n   reverse(v1.begin() + 1, v1.end()); n   reverse(v2.begin() + 1, v2.end()); n   for (int i = 1; i < v1.size(); i++) v1[i] += v1[i - 1]; n   for (int i = 1; i < v2.size(); i++) v2[i] += v2[i - 1]; n   for (int i = 0; i < v2.size(); i++) { n     long long pos = n         lower_bound(v1.begin(), v1.end(), sum - s - v2[i]) - v1.begin(); n     if (pos == v1.size()) continue; n     ans = min(ans, pos + 2 * i); n   } n   cout << ans <<   n ; n   clc(); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     test_case(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long min(long long x, long long y) { return (x < y) ? x : y; } n long long max(long long x, long long y) { return (x > y) ? x : y; } n const int N = 1000009; n const int E = 1000000; n int x, y, n, m, e, i, u, w, mx, z, sum, l, r, best, cnt, g, j, k, t; n int a[N], flag[N], mark[N]; n string s; n vector<int> pos, ans; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     flag[a[i]] = true; n   } n   for (int i = 1; i <= E / 2; i++) n     if (!flag[i] && !flag[E - i + 1]) pos.push_back(i); n   for (int i = 0; i < n; i++) { n     if (mark[a[i]]) continue; n     mark[a[i]] = true; n     k = a[i]; n     if (!flag[E - k + 1]) n       ans.push_back(E - k + 1); n     else { n       w = pos[cnt]; n       cnt++; n       ans.push_back(w); n       ans.push_back(E - w + 1); n       mark[E - k + 1] = true; n     } n   } n   cout << ans.size() << endl; n   for (int i = 0; i < ans.size(); i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int ma = -1, mi = 1000000000000000; n inline long long int gcd(long long int a, long long int b) { n   return b ? gcd(b, a % b) : a; n } n inline long long int lcm(long long int a, long long int b, long long int MOD) { n   return a / gcd(a, b) * b % MOD; n } n long long int power(long long int x, unsigned int y) { n   if (y == 0) n     return 1; n   else if (y % 2 == 0) n     return power(x, y / 2) * power(x, y / 2); n   else n     return x * power(x, y / 2) * power(x, y / 2); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int i, j, k, a[100009] = {0}, b[100009] = {0}, c = 0, d = 0; n   string n, m; n   cin >> n >> m; n   if (n ==  monday ) { n     if (m ==  monday  || m ==  wednesday  || m ==  thursday ) { n       cout <<  YES  << endl; n       return 0; n     } n   } n   if (n ==  tuesday ) { n     if (m ==  tuesday  || m ==  friday  || m ==  thursday ) { n       cout <<  YES  << endl; n       return 0; n     } n   } n   if (n ==  wednesday ) { n     if (m ==  friday  || m ==  wednesday  || m ==  saturday ) { n       cout <<  YES  << endl; n       return 0; n     } n   } n   if (n ==  thursday ) { n     if (m ==  saturday  || m ==  sunday  || m ==  thursday ) { n       cout <<  YES  << endl; n       return 0; n     } n   } n   if (n ==  friday ) { n     if (m ==  friday  || m ==  sunday  || m ==  monday ) { n       cout <<  YES  << endl; n       return 0; n     } n   } n   if (n ==  saturday ) { n     if (m ==  saturday  || m ==  monday  || m ==  tuesday ) { n       cout <<  YES  << endl; n       return 0; n     } n   } n   if (n ==  sunday ) { n     if (m ==  sunday  || m ==  wednesday  || m ==  tuesday ) { n       cout <<  YES  << endl; n       return 0; n     } n   } n   cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void rr(vector<vector<long long>> &m, long long r, long long nr, long long nc) { n   long long t = m[r - 1][nc - 1]; n   for (long long i = nc - 2; i >= 0; i--) m[r - 1][i + 1] = m[r - 1][i]; n   m[r - 1][0] = t; n   return; n } n void rc(vector<vector<long long>> &m, long long c, long long nr, long long nc) { n   long long t = m[nr - 1][c - 1]; n   for (long long i = nr - 2; i >= 0; i--) m[i + 1][c - 1] = m[i][c - 1]; n   m[0][c - 1] = t; n   return; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, m, q, t; n   cin >> n >> m >> q; n   vector<vector<long long>> mat(n, vector<long long>(m, 0)); n   vector<vector<long long>> op; n   for (long long i = 0; i < q; i++) { n     cin >> t; n     if (t == 1 || t == 2) { n       long long roc; n       cin >> roc; n       op.push_back({t, roc}); n     } else { n       long long r, c, x; n       cin >> r >> c >> x; n       op.push_back({t, r, c, x}); n     } n   } n   bool f3 = false; n   for (long long i = op.size() - 1; i >= 0; i--) { n     if (!f3) { n       if (op[i][0] == 3) { n         f3 = true; n         mat[op[i][1] - 1][op[i][2] - 1] = op[i][3]; n       } n     } else { n       if (op[i][0] == 1) { n         rr(mat, op[i][1], n, m); n       } else if (op[i][0] == 2) { n         rc(mat, op[i][1], n, m); n       } else { n         mat[op[i][1] - 1][op[i][2] - 1] = op[i][3]; n       } n     } n   } n   for (auto i : mat) { n     for (auto j : i) cout << j <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 10004; n long long a[N], b[N], c, sa = 0, mb = 0, M; n int n; n bool ok1(long long x) { n   long long s = 0; n   for (int i = 1; i <= n; i++) { n     if (a[i] > 0 && x / b[i] > c / a[i]) return 0; n     s += a[i] * x / b[i] + 1; n     if (s >= c) return 0; n   } n   return 1; n } n bool ok2(long long x) { n   long long s = 0; n   for (int i = 1; i <= n; i++) { n     if (a[i] > 0 && x / b[i] > c / a[i]) return 1; n     s += a[i] * x / b[i] + 1; n     if (s > c) return 1; n   } n   return 0; n } n int main() { n   scanf( %d %lld , &n, &c); n   for (int i = 1; i <= n; i++) { n     scanf( %lld %lld , &a[i], &b[i]); n     sa += a[i]; n     mb = max(mb, b[i]); n   } n   long long u, v; n   M = c * mb; n   if (sa == 0) return puts(n == c ?  -1  :  0 ), 0; n   long long l = 0, r = M; n   while (l < r) { n     long long mid = (l + r + 1) / 2; n     if (ok1(mid)) n       l = mid; n     else n       r = mid - 1; n   } n   u = l; n   l = 0, r = M; n   while (l < r) { n     long long mid = (l + r) / 2; n     if (ok2(mid)) n       r = mid; n     else n       l = mid + 1; n   } n   v = r; n   printf( %lld n , max(v - u - 1, 0ll)); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1005; n long long ara[N]; n int main() { n   long long n, m; n   cin >> n >> m; n   for (long long i = 0; i < m; ++i) { n     long long x; n     cin >> x; n     ++ara[x]; n   } n   vector<long long> v; n   for (long long i = 1; i <= 100; ++i) { n     if (ara[i] != 0) v.push_back(ara[i]); n   } n   long long mx = 0; n   for (long long i = 1; i <= 100; ++i) { n     long long s = 0; n     for (long long j = 0; j < v.size(); ++j) { n       s += v[j] / i; n     } n     if (s >= n) mx = max(mx, i); n   } n   cout << mx; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100; n int main() { n   long long a[maxn], b[maxn]; n   int n, m; n   while (cin >> n >> m) { n     for (int i = 1; i <= n; ++i) { n       cin >> a[i]; n     } n     for (int i = 1; i <= m; ++i) { n       cin >> b[i]; n     } n     sort(a + 1, a + n + 1); n     sort(b + 1, b + m + 1); n     long long ans1 = a[1] * b[1], ans2 = a[n] * b[m], ans3 = a[1] * b[m], n               ans4 = a[n] * b[1]; n     long long maxx = max(ans1, max(ans2, max(ans3, ans4))); n     if (maxx == ans1) n       cout << max(a[2] * b[1], max(ans2, max(a[2] * b[m], ans4))) << endl; n     else if (maxx == ans2) n       cout << max(a[n - 1] * b[m], max(ans1, max(ans3, a[n - 1] * b[1]))) n            << endl; n     else if (maxx == ans3) n       cout << max(a[2] * b[1], max(ans2, max(a[2] * b[m], ans4))) << endl; n     else n       cout << max(ans1, max(a[n - 1] * b[m], max(ans3, a[n - 1] * b[1]))) n            << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool debug = true; n long long n, m, a, b; n int main(int argc, char* argv[]) { n   cin >> n >> m >> a >> b; n   long long r = n % m; n   cout << min(r * b, (m - r) * a); n   while (clock() <= 300) { n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct seq { n   int num; n   char color; n }; n int main() { n   string s; n   cin >> s; n   int i = 1, N = 0, j; n   seq temp; n   vector<seq> arr; n   temp.num = 1; n   temp.color = s[0]; n   while (s[i] !=   0 ) { n     if (s[i] == temp.color) { n       temp.num++; n     } else { n       arr.push_back(temp); n       temp.num = 1; n       temp.color = s[i]; n       N++; n     } n     i++; n   } n   arr.push_back(temp); n   N++; n   if (N % 2 == 0) { n     cout << 0 << endl; n     return 0; n   } n   i = 0; n   j = N - 1; n   while (i < j) { n     if (arr[i].color == arr[j].color) { n       if (arr[i].num + arr[j].num >= 3) { n         i++; n         j--; n       } else { n         break; n       } n     } else { n       break; n     } n   } n   if (i == j) { n     if (arr[i].num + 1 >= 3) { n       cout << arr[i].num + 1 << endl; n     } else { n       cout << 0 << endl; n     } n   } else { n     cout << 0 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   char tramp; n   cin >> tramp; n   string rank =  6789TJQKA ; n   char c1_s, c1_r; n   cin >> c1_r >> c1_s; n   char c2_s, c2_r; n   cin >> c2_r >> c2_s; n   if (c1_s == tramp && c2_s != tramp) { n     cout <<  YES  << endl; n     return 0; n   } n   if (c1_s == c2_s && rank.find(c1_r) > rank.find(c2_r)) { n     cout <<  YES  << endl; n     return 0; n   } n   cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int true_mod(int A, int N) { n   A %= N; n   if (A < 0) A += N; n   return A; n } n const long long MOD = 1000LL * 1000LL * 1000LL + 7LL; n const int MX = 109; n struct matrix { n   long long A[MX][MX]; n }; n int x; n void output(const matrix& A) { n   for (int i = 0; i < x; i++) { n     for (int j = 0; j < x; j++) cout << A.A[i][j] <<    ; n     cout << endl; n   } n } n matrix operator*(const matrix& A, const matrix& B) { n   matrix R; n   for (int i = 0; i < x; i++) { n     for (int k = 0; k < x; k++) { n       long long t = 0; n       for (int j = 0; j < x; j++) t += A.A[i][j] * B.A[j][k] % MOD; n       R.A[i][k] = t % MOD; n     } n   } n   return R; n } n matrix unity() { n   matrix R; n   for (int i = 0; i < x; i++) n     for (int j = 0; j < x; j++) R.A[i][j] = (i == j ? 1 : 0); n   return R; n } n matrix power(const matrix& A, int N) { n   matrix R = unity(), A_pow = A; n   while (N != 0) { n     if (N % 2 == 1) R = R * A_pow; n     A_pow = A_pow * A_pow; n     N /= 2; n   } n   return R; n } n int n, b, k; n const long long B = 10; n int cnt_D[MX] = {0}; n matrix get_A() { n   matrix R; n   for (int i = 0; i < x; i++) { n     for (int j = 0; j < x; j++) { n       int smth = true_mod(j - 10 * i, x); n       R.A[i][j] = cnt_D[smth]; n     } n   } n   return R; n } n int main() { n   cin >> n >> b >> k >> x; n   for (int i = 0; i < n; i++) { n     int a; n     cin >> a; n     cnt_D[a % x]++; n   } n   matrix A = get_A(); n   matrix A_pow = power(A, b); n   cout << A_pow.A[0][k]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, m; n char arr[2004][2004]; n pair<int, int> v[2004][2004]; n long long int dist[8000003]; n const long long int oo = 1000000007; n const int dx[] = {1, -1, 0, 0}; n const int dy[] = {0, 0, 1, -1}; n int get_node(int x, int y) { return x * m + y; } n void bfs(int r, int c) { n   for (int i = 0; i < n * m; i++) dist[i] = oo; n   dist[get_node(r, c)] = 0; n   deque<pair<int, int>> dq; n   dq.push_back({r, c}); n   v[r][c] = {0, 0}; n   while (!dq.empty()) { n     int x = dq[0].first; n     int y = dq[0].second; n     dq.pop_front(); n     for (int i = 0; i < 4; i++) { n       int xx = x + dx[i]; n       int yy = y + dy[i]; n       if (xx < 0 || xx >= n || yy < 0 || yy >= m) continue; n       if (arr[xx][yy] ==  * ) continue; n       int flag = 0; n       if (yy < y) flag = 1; n       int node = get_node(xx, yy); n       int u = get_node(x, y); n       if (dist[node] > dist[u] + flag) { n         dist[node] = dist[u] + flag; n         v[xx][yy] = v[x][y]; n         if (i == 2) v[xx][yy].second++; n         if (i == 3) v[xx][yy].first++; n         if (flag) n           dq.push_back({xx, yy}); n         else n           dq.push_front({xx, yy}); n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   long long int r, c; n   cin >> r >> c; n   r--; n   c--; n   long long int le, ri; n   cin >> le >> ri; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) cin >> arr[i][j], v[i][j] = {oo, oo}; n   bfs(r, c); n   long long int ans = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (arr[i][j] ==  * ) continue; n       if (v[i][j].first <= le && v[i][j].second <= ri) ans++; n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxmn = 505; n long long a[maxmn][maxmn]; n long long luu1[maxmn][maxmn]; n bool k[1000005]; n long long maxx = 1e9; n void readquick() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n int main() { n   readquick(); n   vector<long long> luu; n   memset(k, true, sizeof k); n   for (int i = 2; i <= sqrt(1000000); i++) { n     if (k[i] == true) { n       for (int j = i * i; j <= 1000000; j += i) { n         k[j] = false; n       } n     } n   } n   for (int i = 2; i <= 1000000; i++) { n     if (k[i] == true) luu.push_back(i); n   } n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cin >> a[i][j]; n       int w = lower_bound(luu.begin(), luu.end(), a[i][j]) - luu.begin(); n       luu1[i][j] = luu[w] - a[i][j]; n     } n   } n   for (int i = 1; i <= n; i++) { n     long long tong = 0; n     for (int j = 1; j <= m; j++) { n       tong += luu1[i][j]; n     } n     if (tong >= 0) { n       maxx = min(maxx, tong); n     } n   } n   for (int i = 1; i <= m; i++) { n     long long tong = 0; n     for (int j = 1; j <= n; j++) { n       tong += luu1[j][i]; n     } n     if (tong >= 0) { n       maxx = min(maxx, tong); n     } n   } n   cout << maxx; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n vector<vector<int> > v(N); n int col[N]; n int par[N]; n int n; n int in[N], out[N]; n int timer; n bool all_same[N]; n bool can[N]; n int euler[2 * N]; n int tree[8 * N]; n int answer = -1; n void dfs(int node) { n   all_same[node] = true; n   can[node] = true; n   timer++; n   in[node] = timer; n   euler[timer] = col[node]; n   for (int to : v[node]) { n     if (to == par[node]) continue; n     par[to] = node; n     dfs(to); n     can[node] &= (all_same[to]); n     all_same[node] &= (all_same[to] and (col[node] == col[to])); n   } n   out[node] = timer; n } n void build(int id, int lo, int hi) { n   if (lo == hi) { n     tree[id] = euler[lo]; n     return; n   } n   int mid = (lo + hi) >> 1; n   build(2 * id, lo, mid); n   build(2 * id + 1, mid + 1, hi); n   if (tree[2 * id + 1] == tree[2 * id]) tree[id] = tree[2 * id]; n } n int query(int id, int lo, int hi, int l, int r) { n   if (r < l) return -2; n   if (l > hi or r < lo) return -2; n   if (l <= lo and r >= hi) { n     if (tree[id] == 0) return -1; n     return (tree[id]); n   } n   int mid = (lo + hi) >> 1; n   int tmp1 = query(2 * id, lo, mid, l, r); n   int tmp2 = query(2 * id + 1, mid + 1, hi, l, r); n   if (tmp1 == tmp2) { n     return tmp1; n   } n   if (tmp1 == -2) { n     return tmp2; n   } n   if (tmp2 == -2) { n     return tmp1; n   } n   return -1; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n - 1; i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     v[x].emplace_back(y); n     v[y].emplace_back(x); n   } n   for (int i = 1; i <= n; i++) { n     scanf( %d , &col[i]); n   } n   dfs(1); n   build(1, 1, timer); n   for (int i = 1; i <= n; i++) { n     if (can[i]) { n       int node = i; n       int tmp1 = query(1, 1, timer, 1, in[node] - 1); n       int tmp2 = query(1, 1, timer, out[node] + 1, timer); n       if ((tmp1 == tmp2 and tmp1 != -1) or (tmp1 == -2 and tmp2 != -1) or n           (tmp1 != -1 and tmp2 == -2)) { n         puts( YES ); n         printf( %d n , i); n         return 0; n       } n     } n   } n   puts( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n clock_t time_p = clock(); n void ktj() { n   time_p = clock() - time_p; n   cerr <<  Time elapsed :   << (float)(time_p) / CLOCKS_PER_SEC <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, i, j, k; n   cin >> n; n   vector<long long> a(n); n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   if (n % 2) { n     for (i = 0; i < n; i++) { n       if (a[i] >= 0) { n         a[i] = -a[i] - 1; n       } n     } n     auto mn = min_element(a.begin(), a.end()); n     a[mn - a.begin()] = -a[mn - a.begin()] - 1; n     for (i = 0; i < n; i++) { n       cout << a[i] <<    ; n     } n     cout <<   n ; n   } else { n     for (i = 0; i < n; i++) { n       if (a[i] >= 0) n         cout << -a[i] - 1 <<    ; n       else n         cout << a[i] <<    ; n     } n     cout <<   n ; n   } n   ktj(); n } n


#include <bits/stdc++.h> n using namespace std; n int n, a, b, x, y, f[1000001]; n void make(int l, int r) { n   for (int i = l; i < r; i++) f[i] = i + 1; n   f[r] = l; n } n int main() { n   scanf( %d%d%d , &n, &a, &b); n   for (x = 0; x * a <= n; x++) n     if ((n - x * a) % b == 0) { n       y = (n - x * a) / b; n       break; n     } n   if (a * x + b * y != n) return printf( -1 n ), 0; n   for (int i = 1; i <= x; i++) make(i * a - a + 1, i * a); n   for (int i = 1; i <= y; i++) make(a * x + i * b - b + 1, a * x + i * b); n   for (int i = 1; i <= n; i++) printf( %d%c , f[i], i < n ?     :   n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mx[] = {0, 2, 3, 1}; n long long get_base(long long n) { n   long long b = 4; n   for (;;) { n     if (b > n) return b / 4; n     b <<= 2; n   } n   return 0; n } n long long func1(long long n) { n   long long b = get_base(n); n   return (n - b) / 3 + b; n } n long long func2(long long n) { n   long long b = get_base(n); n   long long m = (n - b - 1) / 3; n   long long bb = b; n   vector<int> v; n   while (bb >= 4) { n     v.push_back(m % 4); n     m /= 4; n     bb /= 4; n   } n   long long r = b + b; n   bb = b / 4; n   for (int i = v.size() - 1; i >= 0; i--) { n     r += bb * mx[v[i]]; n     bb /= 4; n   } n   if (n == 2) return 2; n   return r; n } n int main() { n   int t; n   long long n, ans; n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> t; n   while (t--) { n     cin >> n; n     if (n % 3 == 0) n       ans = func1(n - 2) ^ func2(n - 1); n     else if (n % 3 == 1) n       ans = func1(n); n     else n       ans = func2(n); n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3 * 1e5 + 10, D = 313, mod = 1e9 + 7; n long long hsh[maxn], n, ht, ans, tz, p[maxn]; n string t, s[maxn]; n vector<pair<int, int> > nei[maxn]; n void find_p() { n   p[0] = 1; n   for (int i = 1; i < maxn; i++) p[i] = (p[i - 1] * D) % mod; n } n long long get_hash(int st, int en) { n   return ((hsh[en] - hsh[st] * p[en - st]) % mod + mod) % mod; n } n void st_hash() { n   for (int i = 0; i < tz; i++) hsh[i + 1] = ((hsh[i] * D) + t[i]) % mod; n   ht = hsh[tz]; n   memset(hsh, 0, sizeof hsh); n } n void dfs(int v, int par, int ptr) { n   for (auto u : nei[v]) n     if (u.first != par) { n       for (auto i : s[u.second]) { n         hsh[ptr + 1] = ((hsh[ptr] * D) + i) % mod; n         ptr++; n         if (ptr - tz >= 0 && get_hash(ptr - tz, ptr) == ht) ans++; n       } n       dfs(u.first, v, ptr); n       ptr -= (int)s[u.second].size(); n     } n } n int main() { n   ios::sync_with_stdio(0), cout.tie(0), cin.tie(0); n   int a; n   string b; n   cin >> n; n   for (int i = 1; i < n; i++) { n     cin >> a >> b; n     a--; n     s[i] = b; n     nei[i].push_back({a, i}); n     nei[a].push_back({i, i}); n   } n   cin >> t; n   tz = (int)t.size(); n   find_p(); n   st_hash(); n   dfs(0, -1, 0); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t, n; n   cin >> t; n   while (t--) { n     cin >> n; n     long long int c = 0; n     string s =   ; n     cin >> s; n     string h = s; n     sort(h.begin(), h.end()); n     for (int j = 0; j < n; j++) n       if (h.at(j) != s.at(j)) c++; n     cout << c << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 3; n int n, m, i, j, k, l, r, s, t, d, u, ii, jj, x, y, z, lz[4 * N], h[N]; n char c[100]; n pair<pair<int, int>, pair<int, int> > first[4 * N]; n void init(int root, int bas, int son) { n   if (bas == son) { n     if (h[bas] == 4) n       first[root].first.first = 1; n     else n       first[root].first.second = 1; n     first[root].second.first = first[root].second.second = 1; n     return; n   } n   init((root + root), bas, (bas + son >> 1)); n   init(((root + root) + 1), (bas + son >> 1) + 1, son); n   first[root].first.first = n       first[(root + root)].first.first + first[((root + root) + 1)].first.first; n   first[root].first.second = first[(root + root)].first.second + n                              first[((root + root) + 1)].first.second; n   first[root].second.first = max(first[(root + root)].second.first + n                                      first[((root + root) + 1)].first.second, n                                  first[(root + root)].first.first + n                                      first[((root + root) + 1)].second.first); n   first[root].second.second = max(first[(root + root)].second.second + n                                       first[((root + root) + 1)].first.first, n                                   first[(root + root)].first.second + n                                       first[((root + root) + 1)].second.second); n } n void push(int root, int bas, int son) { n   if (lz[root] and bas != son) { n     lz[((root + root) + 1)] = 1 - lz[((root + root) + 1)]; n     lz[(root + root)] = 1 - lz[(root + root)]; n     swap(first[(root + root)].first.first, first[(root + root)].first.second); n     swap(first[((root + root) + 1)].first.first, n          first[((root + root) + 1)].first.second); n     swap(first[((root + root) + 1)].second.first, n          first[((root + root) + 1)].second.second); n     swap(first[(root + root)].second.first, first[(root + root)].second.second); n   } n   lz[root] = 0; n } n void g(int root, int bas, int son) { n   push(root, bas, son); n   if (x > son or y < bas) return; n   if (x <= bas and y >= son) { n     swap(first[root].first.first, first[root].first.second); n     swap(first[root].second.first, first[root].second.second); n     lz[root] = 1; n     return; n   } n   g((root + root), bas, (bas + son >> 1)); n   g(((root + root) + 1), (bas + son >> 1) + 1, son); n   first[root].first.first = n       first[(root + root)].first.first + first[((root + root) + 1)].first.first; n   first[root].first.second = first[(root + root)].first.second + n                              first[((root + root) + 1)].first.second; n   first[root].second.first = max(first[(root + root)].second.first + n                                      first[((root + root) + 1)].first.second, n                                  first[(root + root)].first.first + n                                      first[((root + root) + 1)].second.first); n   first[root].second.second = max(first[(root + root)].second.second + n                                       first[((root + root) + 1)].first.first, n                                   first[(root + root)].first.second + n                                       first[((root + root) + 1)].second.second); n } n int main() { n   cin >> n >> m; n   for (i = 1; i <= n; i++) { n     scanf(  %c , &c[0]); n     if (c[0] ==  4 ) h[i] = 4; n   } n   init(1, 1, n); n   while (m--) { n     scanf( %s , c); n     if (c[0] ==  c ) n       printf( %d n , first[1].second.first); n     else { n       scanf( %d%d , &x, &y); n       g(1, 1, n); n     } n   } n } n


#include <bits/stdc++.h> n int n, k; n int count; n int x; n std::vector<int> negTemps; n std::priority_queue<int, std::vector<int>, std::greater<int> > intervals; n int main(void) { n   scanf(  %d %d , &n, &k); n   for (int i = 0; i <= n - 1; ++i) { n     scanf(  %d , &x); n     if (x < 0) { n       negTemps.push_back(i); n     } n   } n   if (!negTemps.size()) { n     printf( 0 n ); n     return 0; n   } n   if (k < negTemps.size()) { n     printf( -1 n ); n     return 0; n   } n   int count = negTemps.size() << 1; n   int rem = k - negTemps.size(); n   auto it1 = negTemps.begin(); n   auto it2 = it1 + 1; n   while (it2 != negTemps.end()) { n     intervals.push(*it2 - 1 - *it1); n     ++it1; n     ++it2; n   } n   while (intervals.size() && intervals.top() <= rem) { n     count -= 2; n     rem -= intervals.top(); n     intervals.pop(); n   } n   if (n - *it1 - 1 <= rem) --count; n   printf( %d n , count); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e6 + 10; n long long a[maxn], sum[maxn], n, m, big = 0, tmp; n int main() { n   scanf( %lld , &n); n   for (int i = 0; i < n; i++) { n     scanf( %lld , &m); n     a[m]++; n     big = max(big, m); n   } n   for (int i = 0; i <= big; i++) { n     for (int j = 0; j <= big; j++) { n       if ((long long)i * j > maxn - 10) break; n       sum[i * j] += a[i] * a[j]; n       if (i == j) sum[i * j] -= a[i]; n     } n   } n   for (int i = 1; i < maxn; i++) sum[i] += sum[i - 1]; n   scanf( %lld , &m); n   for (int i = 0; i < m; i++) { n     scanf( %lld , &tmp); n     printf( %lld n , n * (n - 1) - sum[tmp - 1]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 2e6 + 2e5; n const int N = M >> 1; n const int MX = 2e6; n const int D = 1e6; n double L[M], R[M]; n int g[M]; n struct pnt { n   int x, y; n   void input() { scanf( %d %d , &x, &y); } n } p[N]; n int n; n double ans; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     p[i].input(); n     p[i].x += D, p[i].y += D; n   } n   function<void(int, double)> update = [&](int y, double x) { n     L[y] = min(L[y], x); n     R[y] = max(R[y], x); n   }; n   for (int step = 0; step < 2; step++) { n     for (int i = 0; i <= MX; i++) L[i] = M, R[i] = -1; n     for (int i = 1; i <= n; i++) { n       update(p[i].y, p[i].x); n       int ni = i == n ? 1 : i + 1; n       if (p[i].y == p[ni].y) continue; n       double d = (p[ni].x - p[i].x) * 1.0 / (p[ni].y - p[i].y); n       double cur; n       cur = p[i].x; n       for (int j = p[i].y + 1; j < p[ni].y; j++) { n         cur += d; n         update(j, cur); n       } n       cur = p[ni].x; n       for (int j = p[ni].y + 1; j < p[i].y; j++) { n         cur += d; n         update(j, cur); n       } n     } n     long long tot = 0; n     for (int i = 0; i <= MX; i++) { n       int lx = L[i] + 0.9999999; n       int rx = R[i] + 0.0000001; n       g[i] = rx >= lx ? rx - lx + 1 : 0; n       tot += g[i]; n     } n     double c = 0, d = 0, dd = 0; n     for (int i = 0; i < MX; i++) { n       double r = g[i] * 1.0 / (tot - 1); n       d = d + dd + r; n       dd = dd + r + r; n       c = c + d; n       ans += 1.0 * g[i + 1] / tot * c; n     } n     for (int i = 1; i <= n; i++) swap(p[i].x, p[i].y); n   } n   cout << fixed << setprecision(10) << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n static int v1[100001]; n static int v2[100001]; n int comp(const void *elem1, const void *elem2) { n   return *((const int *)elem2) - *((const int *)elem1); n } n int main(void) { n   int k, r, n; n   scanf( %d %d %d , &n, &k, &r); n   k--; n   r--; n   for (int i = 0; i < n; ++i) { n     int a; n     scanf( %d , &a); n     v1[i] = a; n   } n   for (int i = 0; i < n; ++i) { n     int a; n     scanf( %d , &a); n     v2[i] = a; n   } n   qsort(v1 + k, r - k + 1, sizeof(int), comp); n   qsort(v2 + k, r - k + 1, sizeof(int), comp); n   for (int i = 0; i < n; ++i) { n     if (v1[i] != v2[i]) { n       printf( LIE ); n       return 0; n     } n   } n   printf( TRUTH ); 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 r, g, b, w; n     cin >> r >> g >> b >> w; n     int e = 0; n     if (r % 2 == 0) e++; n     if (g % 2 == 0) e++; n     if (b % 2 == 0) e++; n     if (e == 3) { n       cout <<  YES n ; n       continue; n     } else if (e == 2) { n       if (w % 2 == 0) { n         cout <<  YES n ; n         continue; n       } else { n         cout <<  NO n ; n         continue; n       } n     } else if (e == 1) { n       if (r > 0 && b > 0 && g > 0 && w % 2) { n         cout <<  YES n ; n         continue; n       } else { n         cout <<  NO n ; n         continue; n       } n     } else { n       cout <<  YES n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n long long powm(long long a, long long n) { n   long long base = 1; n   a %= MOD; n   while (n) { n     if (n & 1) base = base * a % MOD; n     a = a * a % MOD; n     n >>= 1; n   } n   return base; n } n int main() { n   long long a, b, t; n   cin >> a >> b >> t; n   if ((a & 1) != (b & 1) && t == -1) { n     cout << 0 << endl; n     return 0; n   } n   long long ans = powm(2, a - 1); n   ans = powm(ans, b - 1); n   cout << ans << endl; n } n