#include <bits/stdc++.h> n const int MAXN = 2000 + 10; n int n; n char s[MAXN]; n struct Bitset { n   static const int SIZE = 2048 / 64; n   unsigned long long a[SIZE]; n   int cnt; n   int high; n   Bitset() { n     memset(a, 0, sizeof a); n     cnt = 0; n     high = 0; n   } n   inline bool at(int x) { return (a[x >> 6] >> (x & 63)) & 1; } n   inline void setone(int x) { n     cnt += !at(x); n     high = std::max(high, x >> 6); n     a[x >> 6] |= (1ull << (x & 63)); n   } n   inline int count() const { return cnt; } n   void operator^=(const Bitset &rhs) { n     cnt = 0; n     high = std::max(high, rhs.high); n     for (int i = 0; i <= high; i++) { n       a[i] ^= rhs.a[i]; n       cnt += __builtin_popcountll(a[i]); n     } n   } n }; n Bitset a[MAXN], b[MAXN]; n Bitset x; n void insert(int cur) { n   Bitset ans; n   ans.setone(cur); n   for (register int j = 2000; j >= 0; j--) { n     if (!x.at(j)) continue; n     if (b[j].count() == 0) { n       puts( 0 ); n       std::swap(a[j], x); n       std::swap(b[j], ans); n       return; n     } n     ans ^= b[j]; n     x ^= a[j]; n     if (x.count() == 0) break; n   } n   printf( %d , ans.count() - 1); n   for (int i = 1; i < cur; i++) { n     if (ans.at(i)) { n       printf(  %d , i - 1); n     } n   } n   printf(  n ); n } n Bitset to_binary() { n   int n = strlen(s + 1); n   static int num[MAXN]; n   std::reverse(s + 1, s + n + 1); n   const int B = 8; n   int cnt = 1, p = 1; n   for (int i = 1; i <= n; i++) { n     num[cnt] += (s[i] -  0 ) * p; n     p *= 10; n     if (i % B == 0) { n       cnt++; n       p = 1; n     } n   } n   int cur = 0; n   Bitset ans; n   if (num[cnt] == 0) cnt--; n   n = cnt; n   while (n) { n     if (num[1] & 1) ans.setone(cur); n     cur++; n     int res = 0; n     for (int i = n; i >= 1; i--) { n       num[i] += res * 100000000; n       res = num[i] % 2; n       num[i] /= 2; n     } n     while (n > 0 && num[n] == 0) n--; n   } n   return ans; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %s , s + 1); n     x = to_binary(); n     insert(i); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int N; n string get_block(const string &str) { n   string res; n   if (str.length() < 4) res.append(4 - str.length(),  0 ); n   res.append(str); n   return res; n } n string process(const string &addr) { n   string res; n   int all = count(addr.begin(), addr.end(),  : ); n   int i = 0, j = 0; n   int num = 0; n   while (j = addr.find( : , i + 1), j != (int)string::npos) { n     if (addr[i] ==  :  && addr[j] ==  : ) { n       for (int k = 0; k < 8 - all + 1; k++) res.append( 0000: ); n       i++; n     } n     res.append(get_block(addr.substr(i, j - i)) +  : ); n     num++; n     i = j + 1; n   } n   return string(res.begin(), res.end() - 1); n } n int main() { n   cin >> N; n   for (int t = 0; t < N; t++) { n     string addr; n     cin >> addr; n     if (addr ==  :: ) addr =  0000::0000 ; n     addr.push_back( : ); n     cout << process(addr) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int inp() { n   char c = getchar(); n   int neg = 1; n   while (c <  0  || c >  9 ) { n     if (c ==  - ) neg = -1; n     c = getchar(); n   } n   int sum = 0; n   while (c >=  0  && c <=  9 ) { n     sum = sum * 10 + c -  0 ; n     c = getchar(); n   } n   return sum * neg; n } n int idx = 0; n struct SEG { n   int l; n   int r; n   int ls; n   int rs; n   int sum; n   int len; n }; n struct Node { n   int x; n   int y; n   int l; n   int id; n } q[100010]; n bool cmp(Node a, Node b) { return a.l < b.l; } n int f[100010], w[100010], size[100010], top[100010], fa[100010], depth[100010], n     son[100010], dfn[100010], id[100010]; n int head[100010], nxt[200010], end[200010], ans[100010], value[200010]; n int cou = 0; n void link(int a, int b, int v) { n   nxt[++cou] = head[a]; n   head[a] = cou; n   end[cou] = b; n   value[cou] = v; n } n bool cmp2(int a, int b) { return w[a] < w[b]; } n SEG operator+(SEG a, SEG b) { n   if (a.l == -1) return b; n   if (b.l == -1) return a; n   SEG ret; n   ret.ls = a.ls; n   ret.l = a.l; n   ret.rs = b.rs; n   ret.r = b.r; n   ret.len = a.len + b.len; n   ret.sum = a.sum + b.sum; n   ret.sum -= f[a.rs]; n   ret.sum -= f[b.ls]; n   ret.sum += f[a.rs + b.ls]; n   if (ret.ls == a.len) ret.ls += b.ls; n   if (ret.rs == b.len) ret.rs += a.rs; n   return ret; n } n struct SEG_Tree { n   SEG t[400010]; n   void push_up(int cur) { t[cur] = t[((cur) << 1)] + t[((cur) << 1 | 1)]; } n   void build(int cur, int l, int r) { n     t[cur].l = l; n     t[cur].r = r; n     t[cur].len = r - l + 1; n     if (l == r) { n       t[cur].ls = t[cur].rs = 1; n       t[cur].sum = f[1]; n       return; n     } n     int mid = (l + r) >> 1; n     build(((cur) << 1), l, mid); n     build(((cur) << 1 | 1), mid + 1, r); n     push_up(cur); n   } n   void modify(int cur, int x) { n     if (t[cur].l == t[cur].r) { n       t[cur].sum = 0; n       t[cur].ls = t[cur].rs = 0; n       return; n     } n     if (x <= t[((cur) << 1)].r) n       modify(((cur) << 1), x); n     else n       modify(((cur) << 1 | 1), x); n     push_up(cur); n   } n   SEG query(int cur, int l, int r) { n     if (t[cur].l > r || t[cur].r < l) return (SEG){-1, -1, -1, -1, -1, -1}; n     if (t[cur].l >= l && t[cur].r <= r) { n       if (t[cur].l == 1) printf( Sukun AKIOI n ); n       return t[cur]; n     } n     return query(((cur) << 1), l, r) + query(((cur) << 1 | 1), l, r); n   } n } t; n void dfs1(int cur, int last) { n   fa[cur] = last; n   size[cur] = 1; n   depth[cur] = depth[last] + 1; n   for (int x = head[cur]; x != -1; x = nxt[x]) { n     if (end[x] != last) { n       w[end[x]] = value[x]; n       dfs1(end[x], cur); n       size[cur] += size[end[x]]; n       if (size[end[x]] > size[son[cur]]) son[cur] = end[x]; n     } n   } n } n void dfs2(int cur, int last, int t) { n   dfn[cur] = ++idx; n   top[cur] = t; n   if (son[cur]) dfs2(son[cur], cur, t); n   for (int x = head[cur]; x != -1; x = nxt[x]) n     if (end[x] != last && end[x] != son[cur]) dfs2(end[x], cur, end[x]); n } n int query(int cur, int x, int y) { n   bool t0 = false, t1 = true; n   std::vector<SEG> vec[2]; n   while (top[x] != top[y]) { n     if (depth[top[x]] < depth[top[y]]) { n       std::swap(x, y); n       std::swap(t0, t1); n     } n     vec[t0].push_back(t.query(1, dfn[top[x]], dfn[x])); n     x = fa[top[x]]; n   } n   if (depth[x] > depth[y]) { n     std::swap(x, y); n     std::swap(t0, t1); n   } n   SEG ans = (SEG){-1, -1, -1, -1, -1}; n   for (int i = 0; i < vec[0].size(); i++) { n     std::swap(vec[0][i].ls, vec[0][i].rs); n     ans = ans + vec[0][i]; n   } n   SEG mid = t.query(1, dfn[x] + 1, dfn[y]); n   if (t0) std::swap(mid.ls, mid.rs); n   ans = ans + mid; n   for (int i = vec[1].size() - 1; i >= 0; i--) { n     ans = ans + vec[1][i]; n   } n   return ans.sum; n } n int main() { n   memset(head, -1, sizeof(head)); n   int n = inp(); n   int m = inp(); n   for (int i = 1; i < n; i++) f[i] = inp(); n   for (int i = 1; i < n; i++) { n     int u = inp(); n     int v = inp(); n     int w = inp(); n     link(u, v, w); n     link(v, u, w); n   } n   dfs1(1, 0); n   dfs2(1, 0, 1); n   t.build(1, 1, n); n   for (int i = 1; i < n; i++) id[i] = i + 1; n   std::sort(id + 1, id + n, cmp2); n   for (int i = 1; i <= m; i++) { n     q[i].x = inp(); n     q[i].y = inp(); n     q[i].l = inp(); n     q[i].id = i; n   } n   std::sort(q + 1, q + m + 1, cmp); n   int cur = 1; n   for (int i = 1; i <= m; i++) { n     while (cur < n && w[id[cur]] < q[i].l) { n       t.modify(1, dfn[id[cur]]); n       cur++; n     } n     ans[q[i].id] = query(1, q[i].x, q[i].y); n   } n   for (int i = 1; i <= m; i++) printf( %d n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int ret = 0, f = 1; n   char ch = getchar(); n   while (ch >  9  || ch <  0 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     ret = (ret << 1) + (ret << 3) + ch -  0 ; n     ch = getchar(); n   } n   return ret * f; n } n int n, m, x, y, deg[25]; n bool edge[25][25], vis[25]; n int main() { n   n = read(); n   m = read(); n   for (register int i = 1; i <= m; i++) { n     x = read(); n     y = read(); n     if (x == y) continue; n     if (edge[x][y]) continue; n     deg[x]++; n     deg[y]++; n     edge[x][y] = 1; n     edge[y][x] = 1; n   } n   for (register int i = 1; i <= n; i++) { n     int st = i, j; n     memset(vis, 0, sizeof(vis)); n     vis[st] = 1; n     for (j = 1; j < n; j++) { n       int minn = 0, de = 0; n       for (register int k = 1; k <= n; k++) { n         if (edge[st][k] && !vis[k]) { n           if (de == 0 || minn > deg[k]) { n             de = k; n             minn = deg[k]; n           } n         } n       } n       if (de == 0) break; n       st = de; n       vis[st] = 1; n     } n     if (j == n) { n       printf( Yes n ); n       return 0; n     } n   } n   printf( No n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long a, b; n int main() { n   cin >> a >> b; n   if ((a + b) % 2 == 0) n     cout << (a - b) / 2 <<     << (a + b) / 2 << endl; n   else n     cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100; n int n, x[maxn], y[maxn], r; n void write(int x) { n   if (x <= 50) n     printf( %d , x); n   else { n     printf( (50+ ); n     write(x - 50); n     printf( ) ); n   } n } n void work() { n   for (int i = 1; i < n; i++) printf( ( ); n   for (int i = 1; i <= n; i++) { n     if (i > 1) printf( + ); n     printf( ( ); n     write(x[i]); n     printf( *((1-abs((t- ); n     write(i - 1); n     printf( )))+abs((abs((t- ); n     write(i - 1); n     printf( ))-1)))) ); n     if (i > 1) printf( ) ); n   } n   printf(  n ); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d%d , x + i, y + i, &r); n     x[i] /= 2; n     y[i] /= 2; n   } n   work(); n   memcpy(x, y, sizeof(y)); n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long C[2333][2333]; n int d, MOD, p = 0; n long long co[23333]; n vector<int> operator-(vector<int> a, vector<int> b) { n   vector<int> s; n   int u = max(a.size(), b.size()); n   a.resize(u); n   b.resize(u); n   for (int i = 0; i < u; ++i) s.push_back((a[i] - b[i]) % MOD); n   return s; n } n long long qp(long long a, long long b) { n   long long x = 1; n   a %= MOD; n   while (b) { n     if (b & 1) x = x * a % MOD; n     a = a * a % MOD; n     b >>= 1; n   } n   return x; n } n int add(int a, int b) { n   ++p; n   cout <<  +   << a <<     << b <<     << p << endl; n   co[p] = (co[a] + co[b]) % MOD; n   return p; n } n int po(int a) { n   ++p; n   cout <<  ^   << a <<     << p << endl; n   co[p] = qp(co[a], d); n   return p; n } n int mul(int a, int k) { n   k = (k % MOD + MOD) % MOD; n   if (!k) k += MOD; n   int s = a; n   --k; n   while (k) { n     if (k & 1) s = add(s, a); n     a = add(a, a); n     k >>= 1; n   } n   return s; n } n int neg(int a) { return mul(a, MOD - 1); } n int minus_(int a, int b) { return add(a, neg(b)); } n void op(int a) { n   cout <<  f   << a << endl; n   cerr << co[a] << endl; n } n int l = 3, z; n long long show(vector<int>& v, long long t) { n   long long x = 1, s = 0; n   for (int i = 0; i < v.size(); ++i) n     s += x * v[i] % MOD, s %= MOD, x = x * t % MOD; n   return (s % MOD + MOD) % MOD; n } n int p2(int a) { n   if (d == 2) return po(a); n   int n = d + 1; n   static vector<int> u[233], ss[233]; n   static int w[233], ww[233]; n   ww[0] = a; n   for (int i = 1; i <= n; ++i) { n     u[i].clear(); n     ss[i].clear(); n     for (int w = 0; w <= d; ++w) u[i].push_back(C[d][w] * qp(i, d - w) % MOD); n     ww[i] = add(ww[i - 1], l); n     w[i] = po(ww[i]); n     ss[i].resize(n + 1); n     ss[i][i] = 1; n   } n   for (int s = 0; s <= d; ++s) { n     long long x = u[s + 1][s] % MOD; n     if (!x) { n       for (int q = s + 2; q <= n; ++q) n         if (u[q][s] % MOD) { n           x = u[q][s] % MOD; n           for (int i = 1; i <= n; ++i) swap(ss[i][s + 1], ss[i][q]); n           swap(u[s + 1], u[q]); n           break; n         } n     } n     if (!x) throw  GG ; n     long long rv = qp(x, MOD - 2); n     for (auto& w : u[s + 1]) w = w % MOD * rv % MOD; n     for (auto& w : ss[s + 1]) w = w % MOD * rv % MOD; n     for (int q = 1; q <= n; ++q) n       if (q != s + 1 && u[q][s] % MOD) { n         long long v = u[q][s] % MOD; n         for (int e = 1; e <= n; ++e) n           ss[q][e] = (ss[q][e] - ss[s + 1][e] * v) % MOD; n         for (int e = 0; e <= d; ++e) n           u[q][e] = (u[q][e] - u[s + 1][e] * v) % MOD; n       } n   } n   int aa = z; n   for (int i = 1; i <= n; ++i) n     if (ss[3][i]) aa = add(aa, mul(w[i], ss[3][i])); n   return aa; n } n int main() { n   cin >> d >> MOD; n   p = 4; n   for (int i = 0; i < 23333; ++i) co[i] = 1; n   for (int i = 0; i < 2333; ++i) { n     C[i][0] = 1; n     for (int j = 1; j <= i; ++j) n       C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD; n   } n   co[1] = 5; n   co[2] = 3; n   z = minus_(l, l); n   op(mul(minus_(minus_(p2(add(1, 2)), p2(1)), p2(2)), (MOD + 1) / 2)); n } n


#include <bits/stdc++.h> n using namespace std; n int n; n double A[1010], B[1010]; n double p[1010][1010]; n double dis(int u) { return (B[u] + 1) / (1 - A[u]); } n int mark[1010]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) { n       scanf( %lf , &p[i][j]); n       p[i][j] /= 100; n     } n   for (int i = 1; i <= n - 1; i++) A[i] = 1; n   A[n] = 0, B[n] = -1; n   for (int k = 1; k <= n; k++) { n     int u = -1; n     for (int i = 1; i <= n; i++) { n       if ((u == -1 || dis(i) < dis(u) && !mark[i])) u = i; n     } n     mark[u] = 1; n     for (int v = 1; v <= n; v++) { n       if (mark[v]) continue; n       B[v] += A[v] * p[v][u] * dis(u); n       A[v] *= (1 - p[v][u]); n     } n   } n   printf( %.10f n , dis(1)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve(const int &test_id) { n   int n, p, k; n   cin >> n >> p >> k; n   vector<pair<long long, int> > aud(n); n   for (int i = 0; i < n; i++) cin >> aud[i].first, aud[i].second = i; n   sort(aud.rbegin(), aud.rend()); n   vector<int> twos(p + 1); n   for (int i = 0; i < p + 1; i++) twos[i] = (1 << i); n   vector<vector<long long> > skill(n, vector<long long>(twos[p])); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < p; j++) cin >> skill[i][j]; n   vector<vector<long long> > dp(n + 1, vector<long long>(twos[p])); n   for (int i = 1; i < n + 1; i++) { n     for (int j = 0; j < twos[p]; j++) { n       int curr_aud_count = i - 1 - __builtin_popcount(j); n       if (curr_aud_count < k && curr_aud_count > -1) n         dp[i][j] = max(dp[i][j], dp[i - 1][j] + aud[i - 1].first); n       else n         dp[i][j] = max(dp[i][j], dp[i - 1][j]); n       for (int k = 0; k < p; k++) n         if (j & twos[k]) n           dp[i][j] = max(dp[i][j], n                          dp[i - 1][j ^ twos[k]] + skill[aud[i - 1].second][k]); n     } n   } n   cout << dp[n][twos[p] - 1] << endl; n } n void solve_cases() { n   int test_cases = 1; n   for (int i = 1; i <= test_cases; i++) solve(i); n } n void fast_io() { n   ios::sync_with_stdio(false); n   srand(time(NULL)); n   cin.tie(0); n   cout.tie(0); n   cout << fixed << setprecision(15); n   cerr << fixed << setprecision(15); n } n int main() { n   fast_io(); n   solve_cases(); n   return EXIT_SUCCESS; n } n


#include <iostream> n #include <cmath> n #include <algorithm> n #include <vector> n #include <set> n #include <unordered_set> n #include <queue> n #include <deque> n #include <string> n #include <sstream> n #include <iomanip> n #include <map> n #include <unordered_map> n #include <stack> n #include <cstdio> n #include <climits> n #include <tuple> n #include <ctime> n #include <cstring> n #include <numeric> n #include <functional> n #include <chrono> n #include <cassert> n #include <bitset> n using namespace std; n using ll = long long; n const ll INF = 2e9; n vector <vector<int>> gp; n vector<int> vis; n vector<int> dist; n vector<int> v; n string fibWord(int n) n { n     string Sn_1 =  0 ; n     string Sn =  01 ; n     string tmp; n     for (int i = 2; i <= n; i++) n     { n         tmp = Sn; n         Sn += Sn_1; n         Sn_1 = tmp; n     } n  n     return Sn; n } n  n void solution() { n     int n; cin >> n; n     cout << 2 - n * n << endl; n } n  n int main() n { n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n     //int T; cin >> T; n     //while (T--){ n     solution(); n     //} n }


#include <bits/stdc++.h> n using namespace std; n int maps[501][501]; n char str[501]; n int dx[11] = {-1, 0, 1, 0, -1, -1, 1, 1, -1, -1}; n int dy[11] = {0, 1, 0, -1, 0, 1, 1, -1, -1, 1}; n int dp[550][550][10]; n int n, m; n int pan(int x, int y) { n   if (x < 1 || x > n || y < 1 || y > m) return 1; n   if (maps[x][y]) return 1; n   return 0; n } n int main() { n   while (~scanf( %d%d , &n, &m)) { n     for (int i = 1; i <= n; i++) { n       scanf( %s , str); n       for (int j = 1; j <= m; j++) { n         if (str[j - 1] ==  0 ) n           maps[i][j] = 0; n         else n           maps[i][j] = 1; n       } n     } n     int ans = 0; n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= m; j++) { n         if (maps[i][j]) continue; n         for (int q = 0; q < 9; q++) { n           dp[i][j][q] = 1; n           if (q == 4) continue; n           for (int k = 1;; k++) { n             int xx = i + dx[q] * k; n             int yy = j + dy[q] * k; n             if (pan(xx, yy)) break; n             dp[i][j][q]++; n           } n         } n       } n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= m; j++) { n         if (maps[i][j]) continue; n         for (int q = 0; q < 9; q++) { n           if (q == 4) continue; n           for (int k = 1;; k++) { n             int xx = i + dx[q] * k; n             int yy = j + dy[q] * k; n             if (pan(xx, yy) || pan(i + dx[q + 1] * k, j + dy[q + 1] * k)) { n               break; n             } n             if (q == 0 && dp[xx][yy][6] >= k + 1) ans++; n             if (q == 1 && dp[xx][yy][7] >= k + 1) ans++; n             if (q == 2 && dp[xx][yy][8] >= k + 1) ans++; n             if (q == 3 && dp[xx][yy][5] >= k + 1) ans++; n             if (q == 5 && dp[xx][yy][2] >= k * 2) ans++; n             if (q == 6 && dp[xx][yy][3] >= k * 2) ans++; n             if (q == 7 && dp[xx][yy][0] >= k * 2) ans++; n             if (q == 8 && dp[xx][yy][1] >= k * 2) ans++; n           } n         } n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const char nl =   n ; n const int MAX_N = 100011; n const long long INF = (1 << 29) + 123; n const long long MOD = 998244353; n const long double PI = 4 * atan((long double)1); n template <typename T> n bool ckmin(T& a, const T& b) { n   return a > b ? a = b, 1 : 0; n } n template <typename T> n bool ckmax(T& a, const T& b) { n   return b > a ? a = b, 1 : 0; n } n void dbg_out() { cerr << endl; } n template <typename Head, typename... Tail> n void dbg_out(Head H, Tail... T) { n   cerr << H <<    ; n   dbg_out(T...); n } n template <typename T> n class SafeVector : public vector<T> { n  public: n   using vector<T>::vector; n   typename vector<T>::reference operator[](size_t n) { n     return vector<T>::at(n); n   } n   typename vector<T>::const_reference operator[](size_t n) const { n     return vector<T>::at(n); n   } n }; n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n const int MX = 2e5 + 5; n long long a[MX][3]; n vector<long long> x; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < 3; j++) cin >> a[i][j]; n   for (int i = 0; i < n; i++) x.push_back(a[i][0]); n   long long dp[n]; n   for (int i = 0; i < n; i++) dp[i] = 0; n   long long pref[n + 1]; n   for (int i = 0; i < n + 1; i++) pref[i] = 0; n   dp[0] = a[0][0] - a[0][1]; n   pref[1] = dp[0]; n   for (int i = 1; i <= n - 1; i++) { n     int idx = lower_bound(x.begin(), x.end(), a[i][1]) - x.begin(); n     dp[i] = ((pref[i] - pref[idx]) % MOD + MOD + a[i][0] - a[i][1]) % MOD; n     pref[i + 1] = (pref[i] + dp[i]) % MOD; n   } n   long long ans = 0; n   for (int i = 0; i < n; i++) ans = ((a[i][2] * dp[i]) % MOD + ans) % MOD; n   cout << (ans + x[n - 1] + 1) % MOD << nl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, k; n   cin >> n >> k; n   long long ar[n]; n   for (long long i = 0; i < n; i++) { n     cin >> ar[i]; n   } n   map<long long, long long> mp1, mp2; n   for (long long i = 0; i < n; i++) { n     mp2[ar[i]]++; n   } n   mp1[ar[0]]++; n   mp2[ar[0]]--; n   long long ans = 0; n   for (long long i = 1; i < n - 1; i++) { n     mp2[ar[i]]--; n     if (ar[i] % k == 0) ans += mp1[ar[i] / k] * mp2[ar[i] * k]; n     mp1[ar[i]]++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   char c = getchar(); n   int tot = 1; n   while ((c <  0  || c >  9 ) && c !=  - ) c = getchar(); n   if (c ==  - ) { n     tot = -1; n     c = getchar(); n   } n   int sum = 0; n   while (c >=  0  && c <=  9 ) { n     sum = sum * 10 + c -  0 ; n     c = getchar(); n   } n   return sum * tot; n } n inline void wr(int x) { n   if (x < 0) { n     putchar( - ); n     wr(-x); n     return; n   } n   if (x >= 10) wr(x / 10); n   putchar(x % 10 +  0 ); n } n inline void wrn(int x) { n   wr(x); n   putchar(  n ); n } n inline void wri(int x) { n   wr(x); n   putchar(   ); n } n int n, m, c; n double ans; n struct p { n   double x, y; n   double z; n } z[200005]; n inline double get_dis(p a, p b) { n   return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); n } n inline double check(p a) { return get_dis(a, z[0]) - get_dis(a, z[n + 1]); } n bool cmp(p a, p b) { return a.z < b.z; } n inline double get_ans(p x, p y) { n   double wx = z[0].x, wy = z[0].y; n   if (x.x == y.x) n     wx = 2 * x.x - wx; n   else { n     double k = 1.0 * (y.y - x.y) / (y.x - x.x), b = x.y - k * x.x, n            kk = -1.0 / k; n     double d = n         2 * n         abs((z[0].x - x.x) * (z[0].y - y.y) - (z[0].y - x.y) * (z[0].x - y.x)) / n         sqrt((x.x - y.x) * (x.x - y.x) + (x.y - y.y) * (x.y - y.y)); n     double tx = d / sqrt(kk * kk + 1), ty = tx * kk; n     if ((k * wx + b > wy) ^ (k * (wx + tx) + b > (wy + ty))) n       wx = wx + tx, wy = wy + ty; n     else n       wx = wx - tx, wy = wy - ty; n   } n   return sqrt((z[n + 1].x - wx) * (z[n + 1].x - wx) + n               (z[n + 1].y - wy) * (z[n + 1].y - wy)); n } n signed main() { n   n = read(); n   c = read(); n   z[0].x = c; n   z[0].y = 0; n   for (int i = 1; i <= n; i++) { n     z[i].x = read(); n     z[i].y = read(); n     z[i].z = fabs(sqrt(((z[i].x - c) * (z[i].x - c)) + ((z[i].y) * (z[i].y))) - n                   sqrt(((z[i].x + c) * (z[i].x + c)) + ((z[i].y) * (z[i].y)))); n   } n   sort(z + 1, z + 1 + n, cmp); n   n = min(n, 850); n   z[n + 1].x = -c; n   z[n + 1].y = 0; n   ans = c * 2; n   for (int i = 1; i < n; i++) { n     if (fabs(check(z[i])) > ans) continue; n     for (int j = i + 1; j <= n; j++) { n       if (fabs(check(z[j])) > ans) continue; n       ans = min(ans, get_ans(z[i], z[j])); n     } n   } n   printf( %.10lf , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long N, f[3001][3001]; n struct Node { n   long long x, val; n } P[3001]; n inline bool cmp(const Node &a, const Node &b) { return a.x < b.x; } n int main() { n   scanf( %I64d , &N); n   for (long long i = 1; i <= N; i++) scanf( %I64d%I64d , &P[i].x, &P[i].val); n   sort(P + 1, P + N + 1, cmp); n   memset(f, 0x7f, sizeof(f)); n   f[1][1] = P[1].val; n   for (long long i = 2; i <= N; i++) n     for (long long j = 1; j <= i; j++) { n       f[i][j] = min(f[i][j], f[i - 1][j] + P[i].x - P[j].x); n       f[i][i] = min(f[i][i], f[i - 1][j] + P[i].val); n     } n   long long ans = (unsigned)(-1) >> 1; n   for (long long i = 1; i <= N; i++) ans = min(ans, f[N][i]); n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ax[4], ay[4], bx[4], by[4]; n int xmin = 1000, xmax = -1000, ymin = 1000, ymax = -1000; n bool f() { n   for (int i = 0; i < 4; i++) { n     if (bx[i] >= xmin && bx[i] <= xmax && by[i] >= ymin && by[i] <= ymax) { n       return 1; n     } n   } n   double X = ((double)bx[0] + (double)bx[3]) / 2, n          Y = ((double)by[0] + (double)by[3]) / 2; n   if (X >= xmin && X <= xmax && Y >= ymin && Y <= ymax) { n     return 1; n   } n   return 0; n } n void rotat() { n   int X, Y; n   xmin = ymin = 1000; n   xmax = ymax = -1000; n   for (int i = 0; i < 4; i++) { n     X = ax[i] + ay[i]; n     Y = ax[i] - ay[i]; n     ax[i] = X; n     ay[i] = Y; n     X = bx[i] + by[i]; n     Y = bx[i] - by[i]; n     bx[i] = X; n     by[i] = Y; n     swap(ax[i], bx[i]); n     swap(by[i], ay[i]); n     xmin = min(xmin, ax[i]); n     xmax = max(xmax, ax[i]); n     ymin = min(ymin, ay[i]); n     ymax = max(ymax, ay[i]); n   } n } n int main() { n   for (int i = 0; i < 4; i++) { n     cin >> ax[i] >> ay[i]; n     xmin = min(xmin, ax[i]); n     xmax = max(xmax, ax[i]); n     ymin = min(ymin, ay[i]); n     ymax = max(ymax, ay[i]); n   } n   for (int i = 0; i < 4; i++) { n     cin >> bx[i] >> by[i]; n   } n   if (f()) { n     cout <<  Yes n ; n     return 0; n   } n   rotat(); n   if (f()) { n     cout <<  Yes n ; n     return 0; n   } n   cout <<  No n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int N, M; n   cin >> N >> M; n   vector<int> ceff(N * 2); n   for (int i = 0; i < N; ++i) { n     cin >> ceff[i * 2]; n   } n   cin >> ceff[N * 2 - 1]; n   for (int i = 1; i < N; ++i) { n     cin >> ceff[i * 2 - 1]; n   } n   double cur = M; n   for (int i = N * 2 - 1; i >= 0; --i) { n     if (ceff[i] == 1) { n       cout << -1 << endl; n       return 0; n     } n     cur = cur / (ceff[i] - 1) + cur; n   } n   cout << setprecision(10) << fixed; n   cout << cur - M << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf(1010101010); n struct uf { n   int par[5005], rank[5005]; n   int ff(int x) { return par[x] == x ? x : par[x] = ff(par[x]); } n   void uu(int x, int y) { n     x = ff(x), y = ff(y); n     if (x == y) return; n     if (rank[x] > rank[y]) n       par[y] = x; n     else if (rank[x] < rank[y]) n       par[x] = y; n     else n       rank[x]++, par[y] = x; n   } n   uf(int n) { n     for (int i(0); i < (n); i++) par[i] = i, rank[i] = 0; n   } n }; n int n, m, k; n int wt[100005]; n pair<int, int> e[100005]; n int perm[100005]; n struct mycmp { n   bool operator()(int i, int j) const { return wt[i] < wt[j]; } n }; n void kruskal_met_0(vector<int>& ret) { n   uf G(n); n   for (int i(0); i < (m); i++) { n     int eid(perm[i]), x(e[eid].first), y(e[eid].second); n     if (x == 0 || y == 0) { n       ret.push_back(eid); n       G.uu(x, y); n     } n   } n   for (int i(0); i < (m); i++) { n     int eid(perm[i]), x(e[eid].first), y(e[eid].second), xr(G.ff(x)), n         yr(G.ff(y)); n     if (xr - yr) { n       ret.push_back(eid); n       G.uu(x, y); n     } n   } n   for (int i(0); i < (n); i++) n     if (G.ff(i) != G.ff(0)) { n       puts( -1 ); n       exit(0); n     } n } n void kruskal_wo_0(vector<int>& ret) { n   uf G(n); n   for (int i(0); i < (m); i++) { n     int eid(perm[i]), x(e[eid].first), y(e[eid].second), xr(G.ff(x)), n         yr(G.ff(y)); n     if (x == 0 || y == 0) continue; n     if (xr - yr) { n       ret.push_back(eid); n       G.uu(x, y); n     } n   } n } n int koppel[5005]; n int main() { n   cin >> n >> m >> k; n   int deg0(0); n   for (int i(0); i < (m); i++) { n     int x, y, w; n     cin >> x >> y >> w; n     x--, y--; n     wt[i] = w; n     e[i].first = x, e[i].second = y; n     if (x == 0 || y == 0) deg0++; n   } n   if (deg0 < k) { n     puts( -1 ); n     exit(0); n   } n   for (int i(0); i < (m); i++) perm[i] = i; n   sort(perm, perm + m, mycmp()); n   vector<int> mst_met, mst_wo; n   kruskal_met_0(mst_met); n   kruskal_wo_0(mst_wo); n   uf G(n); n   for (int i(0); i < (n); i++) koppel[i] = inf; n   vector<int> doen; n   for (__typeof((mst_met).end()) it((mst_met).begin()); it != (mst_met).end(); n        ++it) { n     int eid(*it), x(e[eid].first), y(e[eid].second), xr(G.ff(x)), yr(G.ff(y)); n     if (x == 0) n       koppel[yr] = wt[eid]; n     else if (y == 0) n       koppel[xr] = wt[eid]; n     else { n       int mi(min(koppel[xr], koppel[yr])); n       koppel[xr] = koppel[yr] = mi; n       G.uu(x, y); n       doen.push_back(eid); n     } n   } n   for (int iteratie(0); iteratie < (deg0 - k); iteratie++) { n     pair<int, int> opt(inf, 0); n     for (__typeof((mst_wo).end()) it((mst_wo).begin()); it != (mst_wo).end(); n          ++it) { n       int eid(*it), x(e[eid].first), y(e[eid].second), xr(G.ff(x)), yr(G.ff(y)); n       if (xr == yr) continue; n       opt = min(opt, make_pair(wt[eid] - max(koppel[xr], koppel[yr]), eid)); n     } n     if (opt.first == inf) { n       puts( -1 ); n       return 0; n     } n     int eid(opt.second), x(e[eid].first), y(e[eid].second), xr(G.ff(x)), n         yr(G.ff(y)); n     int mik(min(koppel[xr], koppel[yr])); n     koppel[xr] = koppel[yr] = mik; n     G.uu(x, y); n     doen.push_back(eid); n   } n   uf T(n); n   for (__typeof((doen).end()) it((doen).begin()); it != (doen).end(); ++it) { n     int eid(*it), x(e[eid].first), y(e[eid].second); n     assert(T.ff(x) != T.ff(y)); n     T.uu(x, y); n   } n   if (((int)(doen).size()) != n - 1 - k) { n     puts( -1 ); n     return 0; n   } n   for (int i(0); i < (m); i++) { n     int eid(perm[i]), x(e[eid].first), y(e[eid].second), xr(T.ff(x)), n         yr(T.ff(y)); n     if (x == 0 || y == 0) { n       if (xr - yr) { n         doen.push_back(eid); n         T.uu(x, y); n       } n     } n   } n   cout << ((int)(doen).size()) << endl; n   int som(0); n   for (__typeof((doen).end()) it((doen).begin()); it != (doen).end(); ++it) { n     cout << (*it + 1) <<    ; n     som += wt[*it]; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 5; n const int ZERO = 2e5 + 5000; n int n, w, h; n struct node { n   int id, x, t; n   node() {} n   node(int _id, int _x, int _t) : id(_id), x(_x), t(_t) {} n   bool operator<(const node &a) const { return x < a.x; } n } X[MAXN]; n vector<node> Y[MAXN * 4]; n int cnt[MAXN * 4]; n pair<int, int> ans[MAXN]; n int main() { n   scanf( %d%d%d , &n, &w, &h); n   int tot = 0; n   for (int i = 1; i <= n; i++) { n     int g, p, t; n     scanf( %d%d%d , &g, &p, &t); n     if (g == 1) { n       X[++tot] = node(i, p, t); n     } else { n       Y[p - t + ZERO].push_back(node(i, p, t)); n     } n   } n   sort(X + 1, X + 1 + tot); n   for (int i = 0; i < 4 * MAXN; i++) { n     sort(Y[i].begin(), Y[i].end()); n   } n   for (int i = 1; i <= tot; i++) { n     int pos = X[i].x - X[i].t + ZERO; n     int sz = Y[pos].size(); n     if (sz) { n       int en = ((sz - cnt[pos] - 1) % sz + sz) % sz; n       swap(Y[pos][en].id, X[i].id); n       cnt[pos]++; n     } n     ans[X[i].id] = make_pair(X[i].x, h); n   } n   for (int i = 0; i < 4 * MAXN; i++) { n     int sz = Y[i].size(); n     for (int j = 0; j < sz; j++) { n       int pos = (((sz - cnt[i] - 1) % sz + sz) % sz + j + 1) % sz; n       ans[Y[i][pos].id] = make_pair(w, Y[i][j].x); n     } n   } n   for (int i = 1; i <= n; i++) { n     printf( %d %d n , ans[i].first, ans[i].second); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char ans[1000005], tmp[1000005]; n int wa; n int n, x; n void go(int x, int y) { n   int len = 0; n   while (x != y) { n     if (x < y) { n       y -= x; n       tmp[len++] =  B ; n     } else { n       x -= y; n       tmp[len++] =  T ; n     } n     if (len >= n) return; n   } n   tmp[len++] =  T ; n   int w = 0; n   if (x > 1 || len != n) return; n   for (int i = 0; i + 1 < len; i++) n     if (tmp[i] == tmp[i + 1]) w++; n   if (wa > w) { n     wa = w; n     for (int i = 0; i < n; i++) ans[i] = tmp[i]; n   } n } n int main() { n   while (scanf( %d%d , &n, &x) == 2) { n     wa = 10000000; n     for (int i = 1; i <= x; i++) { n       go(i, x); n       go(x, i); n     } n     if (wa == 10000000) n       puts( IMPOSSIBLE ); n     else { n       reverse(ans, ans + n); n       printf( %d n , wa); n       for (int i = 0; i < n; i++) putchar(ans[i]); n       puts(  ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n const int N = 5e5 + 10, M = 1e7 + 10; n const long long mod = 1e9 + 7; n using namespace std; n map<pair<int, int>, set<int>> mp; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n int x[N], y[N]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) scanf( %d%d , &x[i], &y[i]); n   long long cnt = 0, res = 0; n   for (int i = 1; i < n; i++) { n     for (int j = i + 1; j <= n; j++) { n       int a = y[i] - y[j], b = x[i] - x[j]; n       int c = y[i] * x[j] - y[j] * x[i]; n       int g = gcd(a, b); n       a /= g; n       b /= g; n       c /= g; n       if (a < 0 || (a == 0 && b < 0)) a = -a, b = -b; n       pair<int, int> p = make_pair(a, b); n       if (mp[p].find(c) == mp[p].end()) { n         cnt++; n         mp[p].insert(c); n         res += cnt - mp[p].size(); n       } n     } n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int aa[105][105]; n pair<int, int> pos[105][105]; n int main() { n   int n, m, q; n   scanf( %d %d %d , &n, &m, &q); n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       pos[i][j] = {i, j}; n       aa[i][j] = INT_MAX; n     } n   } n   for (int l = 1; l <= q; ++l) { n     int a; n     scanf( %d , &a); n     if (a == 3) { n       int r, c, x; n       scanf( %d %d %d , &r, &c, &x); n       aa[pos[r][c].first][pos[r][c].second] = x; n     } else { n       int x; n       scanf( %d , &x); n       if (a == 1) { n         pair<int, int> temp = pos[x][1]; n         for (int i = 1; i <= m; ++i) { n           pos[x][i] = pos[x][i + 1]; n         } n         pos[x][m] = temp; n       } else { n         pair<int, int> temp = pos[1][x]; n         for (int i = 1; i <= n; ++i) { n           pos[i][x] = pos[i + 1][x]; n         } n         pos[n][x] = temp; n       } n     } n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       if (aa[i][j] == INT_MAX) { n         printf( 1  ); n       } else { n         printf( %d  , aa[i][j]); n       } n     } n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   int n; n   cin >> n; n   int A[12]; n   int B[12]; n   for (int i = 0; i < 10; i++) A[i] = true; n   for (int i = 0; i < 10; i++) B[i] = false; n   for (int i = 0; i < n; i++) { n     char x; n     int c; n     cin >> x >> c; n     for (int j = 0; j < 10; j++) { n       if (x ==  & ) { n         A[j] &= (c % 2); n         B[j] &= (c % 2); n       } n       if (x ==  | ) { n         A[j] |= (c % 2); n         B[j] |= (c % 2); n       } n       if (x ==  ^ ) { n         A[j] ^= (c % 2); n         B[j] ^= (c % 2); n       } n       c /= 2; n     } n   } n   int AAA = 0; n   int BBB = 0; n   for (int i = 0; i < 10; i++) { n     int a_ = 0, b_ = 0; n     for (int a = 0; a < 2; a++) { n       for (int b = 0; b < 2; b++) { n         int AA = (((true | a) ^ b)); n         int BB = (((false | a) ^ b)); n         if (AA == A[i] && BB == B[i]) { n           a_ = a; n           b_ = b; n         } n       } n     } n     AAA += a_ * (1ll << i); n     BBB += b_ * (1ll << i); n   } n   cout << 2 <<   n ; n   cout <<  |   << AAA <<   n ; n   cout <<  ^   << BBB <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int mxN = 500, M = 1e9 + 7; n int n, a[mxN]; n long long dp[mxN][mxN]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> a[i], --a[i]; n   dp[0][0] = 1; n   for (int i = 1; i < n; ++i) dp[i][i] = dp[i - 1][i] = 1; n   for (int i = n - 1; ~i; --i) { n     for (int j = i + 2; j < n; ++j) { n       dp[i][j] = dp[i + 1][j]; n       for (int k = i + 1; k < j; ++k) n         if (a[k + 1] > a[i + 1]) n           dp[i][j] = (dp[i][j] + dp[i + 1][k] * dp[k][j]) % M; n     } n   } n   cout << dp[0][n - 1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n constexpr int inf = 1e9; n int main() { n   int n, m; n   cin >> n >> m; n   vector<int> d(m), h(m); n   for (int i = 0; i < m; ++i) { n     cin >> d[i] >> h[i]; n   } n   int ans = max(h[0] + d[0] - 1, h.back() + (n - d.back())); n   for (int i = 0; i + 1 < m; ++i) { n     if (abs(h[i] - h[i + 1]) > d[i + 1] - d[i]) { n       ans = -1; n       break; n     } n     ans = max(ans, (d[i + 1] - d[i] - abs(h[i] - h[i + 1])) / 2 + n                        max(h[i], h[i + 1])); n   } n   if (ans == -1) { n     cout <<  IMPOSSIBLE  << endl; n   } else { n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const int mod = 1e9 + 7; n void show2d(const vector<vector<int>>& adj) { n   for (auto x : adj) { n     for (auto y : x) { n       cout << y <<    ; n     } n     cout <<   n ; n   } n } n long long int modexp(long long int a, long long int b, long long int mod) { n   long long int x = 1; n   while (b) { n     if (b & 1) x = ((x % mod) * (a % mod)) % mod; n     a = ((a % mod) * (a % mod)) % mod; n     b = b / 2; n   } n   return x; n } n void rf() {} n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, m, k; n   cin >> n >> m >> k; n   int o = 0; n   int tt = 0; n   for (int i = 0; i < n; ++i) { n     int ch; n     cin >> ch; n     if (ch == 1) n       ++o; n     else n       ++tt; n   } n   tt = max(tt - k, 0); n   cout << max((o + tt) - m, 0) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void print(T a) { n   for (auto i = a.begin(); i != a.end(); i++) cout << *i <<    ; n   cout << endl; n } n bool isVowel(char c) { n   if (c ==  a  || c ==  e  || c ==  i  || c ==  o  || c ==  u ) return true; n   return false; n } n void sync() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n vector<long long int> G[1000000]; n map<long long int, bool> ck; n bool dfs(long long int a, long long int b) { n   if (a == b) return true; n   ck[a] = true; n   for (long long int j : G[a]) { n     if (ck[j] == true) continue; n     bool d = dfs(j, b); n     if (d == true) return true; n   } n   return false; n } n int main() { n   sync(); n   long long int t; n   cin >> t; n   long long int j = 0; n   vector<pair<long long int, long long int> > edges; n   for (long long int testcase = 1; testcase <= t; testcase++) { n     long long int a, b, c; n     cin >> a >> b >> c; n     if (a == 2) { n       b--, c--; n       ck.clear(); n       bool d = dfs(b, c); n       if (d == true) n         cout <<  YES  << endl; n       else n         cout <<  NO  << endl; n     } else { n       edges.push_back({b, c}); n       if (j == 0) { n       } else { n         for (long long int i = 0; i < j; i++) { n           long long int x1 = edges[i].first; n           long long int y1 = edges[i].second; n           if ((x1 > b && x1 < c) || (y1 > b && y1 < c)) { n             G[i].push_back(j); n           } n           if ((b > x1 && b < y1) || (c > x1 && c < y1)) { n             G[j].push_back(i); n           } n         } n       } n       j++; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 10000; n const int M = 1000000; n const int inf = 1e9; n struct Edge { n   int v, w, cost, next; n } g[M]; n int head[N], st, ed, flow, cost, cnt; n void init() { n   memset(head, -1, sizeof(head)); n   flow = cost = cnt = 0; n } n void addEdge(int u, int v, int w, int cost) { n   g[cnt].v = v, g[cnt].w = w, g[cnt].cost = cost, g[cnt].next = head[u], n   head[u] = cnt++; n   g[cnt].v = u, g[cnt].w = 0, g[cnt].cost = -cost, g[cnt].next = head[v], n   head[v] = cnt++; n } n bool spfaFlow(int n) { n   vector<int> dis(n + 1, inf); n   vector<bool> vis(n + 1, false); n   vector<int> path(n + 1, -1); n   dis[st] = 0; n   vis[st] = true; n   queue<int> q; n   q.push(st); n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     vis[u] = false; n     for (int i = head[u]; ~i; i = g[i].next) { n       int v = g[i].v; n       if (g[i].w && dis[v] > dis[u] + g[i].cost) { n         dis[v] = dis[u] + g[i].cost; n         path[v] = i; n         if (!vis[v]) { n           vis[v] = true; n           q.push(v); n         } n       } n     } n   } n   int mxflow = inf; n   if (path[ed] != -1) { n     for (int u = ed; u != st; u = g[path[u] ^ 1].v) { n       mxflow = min(mxflow, g[path[u]].w); n     } n     flow += mxflow; n     for (int u = ed; u != st; u = g[path[u] ^ 1].v) { n       g[path[u]].w -= mxflow; n       g[path[u] ^ 1].w += mxflow; n       cost += mxflow * g[path[u]].cost; n     } n   } n   return path[ed] != -1; n } n int minCost(int n) { n   while (spfaFlow(n)) n     ; n   return cost; n } n int main(int argc, char *argv[]) { n   int n; n   cin >> n; n   string s; n   cin >> s; n   vector<int> b(n + 1); n   for (int i = 1; i <= n; i++) { n     cin >> b[i]; n   } n   int N = 26 + n * 26 + (n / 2) * 26 + (n / 2); n   st = N + 1, ed = N + 2; n   init(); n   vector<int> cnt(255); n   for (auto c : s) { n     cnt[c]++; n   } n   int Z = n * 26 + (n / 2) * 27; n   for (int i =  a ; i <=  z ; i++) { n     addEdge(st, Z + i -  a  + 1, cnt[i], 0); n     for (int j = 1; j <= n; j++) { n       addEdge(Z + i -  a  + 1, (i -  a ) * n + j, 1, i == s[j - 1] ? -b[j] : 0); n     } n   } n   for (int i = 0; i < 26; i++) { n     for (int j = 1; j <= n / 2; j++) { n       int ux = i * n + j, uy = i * n + n + 1 - j; n       addEdge(ux, n * 26 + (n / 2) * i + j, 1, 0); n       addEdge(uy, n * 26 + (n / 2) * i + j, 1, 0); n       addEdge(n * 26 + (n / 2) * i + j, n * 26 + (n / 2) * 26 + j, 1, 0); n     } n   } n   for (int j = 1; j <= n / 2; j++) { n     addEdge(n * 26 + (n / 2) * 26 + j, ed, 2, 0); n   } n   cout << -minCost(N + 2) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long double pi = 3.141592653589793238; n set<long long>::iterator it; n multiset<long long>::iterator itr; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long t; n   t = 1; n   while (t--) { n     long long n; n     cin >> n; n     long long m, a, b; n     cin >> m >> a >> b; n     long long r = n % m; n     cout << min(b * (r), a * (m - r)) << endl; n   } n } n


#include <bits/stdc++.h> n using ll = long long; n using namespace std; n const int MOD = 1e9 + 7; n map<int, vector<int>> nodecolor; n map<int, vector<int>> tree; n void dfs(vector<int>& vis, int node, vector<int>& color) { n   int blue = 0, c = 0, red = 0; n   if (color[node - 1] == 0) { n     c += 1; n   } n   if (color[node - 1] == 1) { n     red += 1; n   } n   if (color[node - 1] == 2) { n     blue += 1; n   } n   for (auto x : tree[node]) { n     if (vis[x] == 0) { n       vis[x] = 1; n       dfs(vis, x, color); n       blue += nodecolor[x][0]; n       red += nodecolor[x][1]; n       c += nodecolor[x][2]; n     } n   } n   nodecolor[node] = {blue, red, c}; n } n int dfs2(vector<int>& vis, int node, int b, int r, int c, int ans) { n   int b1 = 0, r1 = 0, b2 = 0, r2 = 0; n   for (auto x : tree[node]) { n     if (vis[x] == 0) { n       vis[x] = 1; n       b1 = b - (nodecolor[x][0]); n       b2 = nodecolor[x][0]; n       r1 = r - (nodecolor[x][1]); n       r2 = nodecolor[x][1]; n       if ((b1 == 0 || r1 == 0) && (b2 == 0 || r2 == 0)) { n         ans += 1; n       } n       ans += dfs2(vis, x, b, r, c, 0); n     } n   } n   return ans; n } n int main(int argc, const char* argv[]) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, u, v; n   cin >> n; n   vector<int> color(n); n   int b = 0, r = 0, c = 0; n   for (int i = 0; i < n; i++) { n     cin >> color[i]; n     b += (color[i] == 2); n     c += (color[i] == 0); n     r += (color[i] == 1); n   } n   for (int i = 0; i < n - 1; i++) { n     cin >> u >> v; n     tree[u].push_back(v); n     tree[v].push_back(u); n   } n   int ans = 0; n   vector<int> vis(n + 1, 0); n   vis[1] = 1; n   dfs(vis, 1, color); n   for (int i = 0; i < n + 1; i++) { n     vis[i] = 0; n   } n   vis[1] = 1; n   ans = dfs2(vis, 1, b, r, c, 0); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long in() { n   int32_t x; n   scanf( %d , &x); n   return x; n } n const long long maxn = 1e6 + 10; n const long long mod = 1e9 + 7; n const long long inf = 1e9 + 10; n long long dp[maxn]; n long long ans[maxn]; n int32_t main() { n   dp[1] = 1; n   for (long long i = 2; i < maxn; i++) dp[i] = dp[i / 2] * 2; n   long long n = in(); n   if (n & 1) { n     cout <<  NO n ; n   } else { n     long long nn = n; n     while (n) { n       long long maxPower = dp[n]; n       for (long long x = 1; x <= n - maxPower + 1; x++) n         ans[maxPower - x] = maxPower + x - 1, n                        ans[maxPower + x - 1] = maxPower - x; n       n = 2 * maxPower - n - 2; n     } n     cout <<  YES n ; n     for (long long i = 1; i <= nn; i++) cout << ans[i] <<    ; n     cout <<   n ; n     n = nn; n   } n   if (n <= 7) { n     bool ok = false; n     long long arr[7]; n     for (long long i = 0; i < n; i++) arr[i] = i + 1; n     do { n       bool fl = true; n       for (long long i = 0; i < n; i++) { n         if ((arr[i] & (i + 1)) && arr[i] != i + 1) continue; n         fl = false; n       } n       if (fl) { n         ok = true; n         break; n       } n     } while (next_permutation(arr, arr + n)); n     if (ok) { n       cout <<  YES n ; n       for (long long i = 0; i < n; i++) cout << arr[i] <<    ; n     } else n       cout <<  NO n ; n   } else { n     if (__builtin_popcountll(n) == 1) n       cout <<  NO n ; n     else { n       cout <<  YES n ; n       long long arr[7]; n       for (long long i = 0; i < 7; i++) arr[i] = i + 1; n       do { n         bool fl = true; n         for (long long i = 0; i < 7; i++) { n           if ((arr[i] & (i + 1)) && arr[i] != i + 1) continue; n           fl = false; n         } n         if (fl) break; n       } while (next_permutation(arr, arr + 7)); n       for (long long i = 0; i < 7; i++) cout << arr[i] <<    ; n       for (long long i = 8; i <= n; i *= 2) { n         long long lim = min(2 * i - 1, n); n         for (long long x = i; x <= lim; x++) { n           if (x == i) n             cout << lim <<    ; n           else n             cout << x - 1 <<    ; n         } n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, t, ans = 0, i, a, b, m, x, y; n   cin >> t; n   while (t > 0) { n     cin >> n; n     vector<int> res; n     long long es = 0, os = 0; n     if ((n / 2) % 2 == 0) { n       cout <<  YES  << endl; n       for (i = 0; i < n / 2; i++) { n         res.push_back(2 * (i + 1)); n         es += 2 * (i + 1); n       } n       for (i = 0; i < n / 2; i++) { n         res.push_back(2 * (i + 1) - 1); n         os += 2 * (i + 1) - 1; n       } n       res[n - 1] += es - os; n       for (i = 0; i < n; i++) cout << res[i] <<    ; n       cout << endl; n     } else n       cout <<  NO  << endl; n     t--; n   } n } n


#include <bits/stdc++.h> n const int INF = 0x3f3f3f3f; n const int kN = 100 + 5; n const int MOD = (int)1e9 + 7; n int n, limit; n int graph[kN][kN]; n std::vector<int> edges[kN]; n int dp[kN][kN]; n void dfs(int u, int fa) { n   for (int w = 0; w < n; ++w) n     if (graph[u][w] <= limit) dp[u][w] = 1; n   for (int v : edges[u]) { n     if (fa == v) continue; n     dfs(v, u); n   } n   for (int w = 0; w < n; ++w) { n     if (dp[u][w] == 0) continue; n     for (int v : edges[u]) { n       int slot = 0; n       if (v == fa) continue; n       for (int r = 0; r < n; ++r) { n         if (dp[v][r] == 0) continue; n         if (graph[u][r] < graph[u][w] || graph[u][r] == graph[u][w] && r < w) n           continue; n         if (graph[v][w] < graph[v][r] || graph[v][w] == graph[v][r] && w < r) n           continue; n         slot += dp[v][r]; n         if (slot >= MOD) slot -= MOD; n       } n       dp[u][w] = dp[u][w] * 1ll * slot % MOD; n     } n   } n } n int main() { n   scanf( %d%d , &n, &limit); n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < n; ++j) graph[i][j] = i == j ? 0 : INF; n   for (int i = 0; i < n - 1; ++i) { n     int a, b; n     scanf( %d%d , &a, &b); n     a--; n     b--; n     edges[a].emplace_back(b); n     edges[b].emplace_back(a); n     graph[a][b] = graph[b][a] = 1; n   } n   for (int k = 0; k < n; ++k) n     for (int i = 0; i < n; ++i) n       for (int j = 0; j < n; ++j) n         graph[i][j] = std::min(graph[i][j], graph[i][k] + graph[k][j]); n   dfs(0, -1); n   int result = 0; n   for (int v = 0; v < n; ++v) { n     result = (result + dp[0][v]) % MOD; n   } n   printf( %d n , result); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1001; n int a[N][N], LR[N][N], RR[N][N], LC[N][N], RC[N][N]; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) { n     vector<int> v; n     for (int j = 1; j <= m; ++j) { n       cin >> a[i][j]; n       v.push_back(a[i][j]); n     } n     sort(v.begin(), v.end()); n     auto last = unique(v.begin(), v.end()); n     v.erase(last, v.end()); n     for (int j = 1; j <= m; ++j) { n       int l = 0, r = (int)v.size() - 1; n       while (l <= r) { n         int m = (l + r) / 2; n         if (v[m] >= a[i][j]) { n           if (v[m] == a[i][j]) { n             LR[i][j] = m; n             RR[i][j] = (int)v.size() - 1 - m; n           } n           r = m - 1; n         } else { n           l = m + 1; n         } n       } n     } n   } n   for (int i = 1; i <= m; ++i) { n     set<int> s; n     vector<int> v; n     for (int j = 1; j <= n; ++j) { n       v.push_back(a[j][i]); n     } n     sort(v.begin(), v.end()); n     auto last = unique(v.begin(), v.end()); n     v.erase(last, v.end()); n     for (int j = 1; j <= n; ++j) { n       int l = 0, r = (int)v.size() - 1; n       while (l <= r) { n         int m = (l + r) / 2; n         if (v[m] >= a[j][i]) { n           if (v[m] == a[j][i]) { n             LC[j][i] = m; n             RC[j][i] = (int)v.size() - 1 - m; n           } n           r = m - 1; n         } else { n           l = m + 1; n         } n       } n     } n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       cout << max(LC[i][j], LR[i][j]) + 1 + max(RC[i][j], RR[i][j]) <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n namespace fast_IO { n inline int read_int() { n   register int ret = 0, f = 1; n   register char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     ret = (ret << 1) + (ret << 3) + int(c - 48); n     c = getchar(); n   } n   return ret * f; n } n }  // namespace fast_IO n using namespace fast_IO; n int N, Q; n int a[200005]; n char s[200005]; n struct BIT { n   int c[200005]; n   inline int lowbit(int x) { return x & (-x); } n   inline void add(int pos, int w) { n     while (pos <= N) c[pos] += w, pos += lowbit(pos); n   } n   inline int query(int pos) { n     int res = 0; n     while (pos) res += c[pos], pos -= lowbit(pos); n     return res; n   } n } BIT[3]; n set<int> S[3]; n inline int decode(char c) { n   if (c ==  R ) return 0; n   if (c ==  P ) return 1; n   if (c ==  S ) return 2; n } n inline void init() { n   N = read_int(), Q = read_int(); n   scanf( %s , s + 1); n   for (register int i = 0; i < 3; i++) n     S[i].insert(-0x3f3f3f3f), S[i].insert(0x3f3f3f3f); n   for (register int i = 1; i <= N; i++) { n     a[i] = decode(s[i]); n     BIT[a[i]].add(i, 1); n     S[a[i]].insert(i); n   } n } n inline int calc() { n   if (S[0].size() == N + 2 || S[1].size() == N + 2 || S[2].size() == N + 2) n     return N; n   if (S[0].size() == 2) return S[2].size() - 2; n   if (S[1].size() == 2) return S[0].size() - 2; n   if (S[2].size() == 2) return S[1].size() - 2; n   set<int>::iterator it_min[3], it_max[3]; n   for (register int i = 0; i < 3; i++) it_min[i] = S[i].begin(), it_min[i]++; n   for (register int i = 0; i < 3; i++) n     it_max[i] = S[i].end(), it_max[i]--, it_max[i]--; n   int ans = N; n   for (register int i = 0; i < 3; i++) { n     int l1 = (i + 1) % 3, r1 = (i + 2) % 3; n     int L = *it_min[l1], R = *it_min[r1] - 1; n     ans -= max(0, BIT[i].query(R) - BIT[i].query(L - 1)); n     L = *it_max[r1], R = *it_max[l1]; n     ans -= max(0, BIT[i].query(R) - BIT[i].query(L - 1)); n   } n   return ans; n } n inline void ans() { n   printf( %d n , calc()); n   register int pos, nw; n   static char opt[20]; n   while (Q--) { n     pos = read_int(); n     scanf( %s , opt); n     nw = decode(opt[0]); n     S[a[pos]].erase(pos); n     BIT[a[pos]].add(pos, -1); n     a[pos] = nw, S[a[pos]].insert(pos), BIT[a[pos]].add(pos, 1); n     printf( %d n , calc()); n   } n } n int main() { n   init(); n   ans(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxx = 500 + 7; n const int maxn = 100 + 7; n const long long mod = 1E9 + 7; n const int maxs = 2 * (1E7) + 5; n const int maxm = 100 + 3; n const long long maxk = 1e13; n long double dp[maxn][maxn]; n int last[1000]; n char seq[maxn]; n long long n, k; n void init() { n   cin >> n >> k; n   cin >> seq; n   memset(last, -1, sizeof(last)); n } n void solve() { n   dp[0][0] = 1; n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j <= i; j++) { n       if (!j) { n         dp[i][j] = 1; n         continue; n       } n       dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1]; n       if (last[seq[i - 1]] != -1) dp[i][j] -= dp[last[seq[i - 1]]][j - 1]; n     } n     last[seq[i - 1]] = i - 1; n   } n   long long cost = 0; n   long double kk = k; n   for (long long i = n; i >= 0; i--) { n     long double tt = min(kk, dp[n][i]); n     cost += tt * (n - i); n     kk -= min(tt, kk); n   } n   if (kk > 0) n     cout << -1 << endl; n   else n     cout << (long long)cost << endl; n } n int main(void) { n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T& x) { n   static char c; n   static int f; n   for (c = getchar(), f = 1; c <  0  || c >  9 ; c = getchar()) n     if (c ==  - ) f = -f; n   for (x = 0; c >=  0  && c <=  9 ; c = getchar()) x = x * 10 + (c & 15); n   x *= f; n } n template <typename T> n void write(T x) { n   static char q[65]; n   int cnt = 0; n   if (x < 0) putchar( - ), x = -x; n   q[++cnt] = x % 10, x /= 10; n   while (x) q[++cnt] = x % 10, x /= 10; n   while (cnt) putchar(q[cnt--] +  0 ); n } n const int mod = 1000000007, maxn = 1000005; n int mo(const int x) { return x >= mod ? x - mod : x; } n int iac[maxn], fac[maxn], _n[maxn], _m[maxn]; n int main() { n   int n, m, a, b; n   read(n), read(m), read(a), read(b); n   int mx = max(n, m); n   iac[0] = iac[1] = fac[0] = fac[1] = _n[1] = _m[0] = 1; n   _m[1] = m; n   for (int i = 2; i <= mx; ++i) n     iac[i] = 1ll * (mod - mod / i) * iac[mod % i] % mod; n   _n[0] = iac[n]; n   for (int i = 2; i <= mx; ++i) n     iac[i] = 1ll * iac[i - 1] * iac[i] % mod, n     fac[i] = 1ll * fac[i - 1] * i % mod, _m[i] = 1ll * _m[i - 1] * m % mod, n     _n[i] = 1ll * _n[i - 1] * n % mod; n   int ans = 0; n   for (int i = 1; i <= n - 1 && i <= m; ++i) n     ans = mo(ans + 1ll * (i + 1) * _n[n - 1 - i] % mod * _m[n - 1 - i] % mod * n                        iac[n - 1 - i] % mod * iac[m - i] % mod * iac[i - 1] % n                        mod); n   ans = 1ll * ans * fac[n - 2] % mod * fac[m - 1] % mod; n   write(ans), putchar(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int Set(int N, int pos) { return N = N | (1 << pos); } n int reset(int N, int pos) { return N = N & ~(1 << pos); } n bool check(int N, int pos) { return (bool)(N & (1 << pos)); } n string st, st2, st3, st4, ans; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL); n   int i, j, a, b, n, m, x, y, ck, t, r, rem, cnt, lo, hi, sum, cs = 1; n   cin >> n >> m; n   cin >> st; n   a = b = m / 2; n   for (i = 0; i < n; i++) { n     if (st[i] ==  (  && a > 0) ans +=  ( , a--; n     if (st[i] ==  )  && b > 0) ans +=  ) , b--; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     int ans = -1; n     for (int i = 0; i < s.size(); i++) { n       if (s[i] ==  R ) { n         ans = i + 1; n         break; n       } n     } n     if (ans == -1) { n       cout << s.size() + 1 << endl; n       continue; n     } else { n       int last = ans - 1; n       for (int i = ans; i < s.size(); i++) { n         if (s[i] ==  R ) { n           ans = max(i - last, ans); n           last = i; n         } n       } n       ans = max(ans, int(s.size()) - last); n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxk = 15; n const int maxn = 1e5 + 10; n long long D[maxk][maxn]; n long long queryBIT(int k, int x) { n   long long ret = 0; n   while (x > 0) { n     ret += D[k][x]; n     x -= x & -x; n   } n   return ret; n } n void addBIT(int k, int x, long long d) { n   while (x < maxn) { n     D[k][x] += d; n     x += x & -x; n   } n } n int main() { n   int n, k; n   cin >> n >> k; n   while (n--) { n     int a; n     cin >> a; n     for (int j = 11; j > 0; j--) { n       addBIT(j, a, queryBIT(j - 1, a)); n     } n     addBIT(0, a, 1); n   } n   cout << queryBIT(k, maxn - 1) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010; n vector<int> g[maxn]; n int src[maxn], dest[maxn]; n bool vis[maxn]; n int main() { n   int n, m; n   int a, b; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < m; i++) { n     scanf( %d %d , &a, &b); n     a--, b--; n     g[a].push_back(b); n     g[b].push_back(a); n   } n   queue<int> Q; n   for (int i = 0; i < n; i++) { n     scanf( %d , &dest[i]); n     if (dest[i] == 0) { n       Q.push(i); n     } n   } n   int ans = 0; n   while (!Q.empty()) { n     int i = Q.front(); n     Q.pop(); n     if (dest[i] == src[i]) { n       vis[i] = true; n       ans++; n       src[i]++; n       int len = g[i].size(); n       for (int j = 0; j < len; j++) { n         src[g[i][j]]++; n         if (src[g[i][j]] == dest[g[i][j]]) { n           Q.push(g[i][j]); n         } n       } n     } n   } n   printf( %d n , ans); n   for (int i = 0; i < n; i++) { n     if (vis[i]) { n       ans--; n       printf( %d%c , i + 1, ans ?     :   n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ls, cc; n char s[1111111], a[1111111]; n int main() { n   gets(s), ls = strlen(s), cc = 0, a[0] =  # ; n   for (int i = 0; i < ls; ++i) { n     a[++cc] = s[i]; n     while (cc > 1 && a[cc - 1] != a[cc]) cc -= 2; n   } n   for (int i = 1; i <= cc; ++i) putchar(a[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int sz = 100005; n int a[sz], b[sz], sum[sz]; n int main() { n   int n, m, c; n   cin >> n >> m >> c; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= m; i++) { n     cin >> b[i]; n     sum[i] = (sum[i - 1] + b[i]) % c; n   } n   for (int i = 1; i <= n; i++) { n     int l, r; n     r = min(m, i); n     l = max(1, m - (n - i + 1) + 1); n     if (l <= r) a[i] += (sum[r] - sum[l - 1] + 3 * c); n     a[i] %= c; n     cout << a[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 105; n int n, res = 1e9, a[5][N][N]; n void calc(int a[N][N], int b[N][N], int c[N][N], int d[N][N]) { n   int tmp = 0, tmp2 = 0; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= n; ++j) { n       if (a[i][j] != (i + j) % 2) n         ++tmp; n       else n         ++tmp2; n       if (c[i][j] != (i + j) % 2) n         ++tmp; n       else n         ++tmp2; n       if (b[i][j] != (i + j) % 2) n         ++tmp2; n       else n         ++tmp; n       if (d[i][j] != (i + j) % 2) n         ++tmp2; n       else n         ++tmp; n     } n   res = min(res, min(tmp, tmp2)); n } n int main() { n   cin >> n; n   for (int i = 1; i <= 4; ++i) n     for (int j = 1; j <= n; ++j) n       for (int k = 1; k <= n; ++k) { n         char z; n         cin >> z; n         a[i][j][k] = (int)(z -  0 ); n       } n   vector<int> v; n   v.push_back(1); n   v.push_back(2); n   v.push_back(3); n   v.push_back(4); n   do { n     calc(a[v[0]], a[v[1]], a[v[2]], a[v[3]]); n   } while (next_permutation(v.begin(), v.end())); n   cout << res <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n string name[101]; n vector<string> seen; n bool see = false; n int m, n, i, j; n int main() { n   cin >> n; n   m = n; n   i = 1; n   int k = 0; n   while (n > 0) { n     cin >> name[i]; n     i++; n     n--; n   } n   for (i = 1; i <= m; i++) { n     see = false; n     for (vector<string>::iterator it = seen.begin(); it != seen.end(); ++it) { n       if (name[i] == *it) { n         cout <<  YES  << endl; n         see = true; n         break; n       } n     } n     if (see == false) { n       cout <<  NO  << endl; n       see = false; n       seen.push_back(name[i]); n     } n   } n   return 0; n } n


//Jai Shree Ram n #include<bits/stdc++.h> n using namespace std; n #define   f(a,b,n)        for(ll a=b;a<n;a++) n #define   readarr(arr,n)  f(i,0,n) cin>>arr[i]; n #define   ll              long long n #define   pb              push_back n #define   fastio            ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL) n #define   all(x)          x.begin(), x.end() n #define   mod             1000000007  n #define   w(t)            int t; cin>>t; while(t--)  n #define   fi              first n #define   sd              second                                                                                n #define   sp(x,y)         fixed<<setprecision(y)<<x               n #define   trace(x)     cout<<#x<< :  <<x<<endl;  n #define   trav(a, x)      for(auto& a : x)  n #define   setbits(x)      __builtin_popcountll(x)   n #define   zrobits(x)      __builtin_ctzll(x)   n #define   pqmax           priority_queue<ll> n #define   pqmin           priority_queue<ll,vi,greater<ll>>                          n #define   vi              vector<ll> n #define   mpi              map<ll,ll>  n #define   pi              pair<ll,ll>   n #define   N               1e5    n #define   inf             1e18  n #define   nline             n  n void c_a_c() n { n  #ifndef ONLINE_JUDGE n  freopen( input.txt , r ,stdin); n  freopen( output.txt , w ,stdout); n  #endif n } n int main() n { n  c_a_c(); n  ll t; n  cin>>t; n  while(t--) n  { n   ll n; n   cin>>n; n      vector<long long> v[n+1]; n      long long array[n]; n      for(long long i = 0; i < n; i++) { n       cin >> array[i]; n      } n      long long x; n      for(long long i = 0; i < n; i++) { n       cin >> x; n       v[array[i]].push_back(x); n      } n      for(long long i = 1; i <= n; i++) { n       sort(all(v[i])); n       reverse(all(v[i])); n      } n      long long ans[n+1]; n      for(long long i = 1; i <= n; i++) { n       ans[i] = 0; n      } n      for(long long i  = 1; i <= n; i++) { n       for(long long j  = 1; j < v[i].size(); j++) { n        v[i][j] += v[i][j-1]; n       } n       for(long long j = 1; j <= v[i].size(); j++) { n        long long temp = v[i].size() % j; n        ans[j] += v[i][v[i].size() - temp-1]; n       } n      } n      for(long long i = 1; i <= n; i++) { n       cout << ans[i] <<    ; n      } n      cout <<   n ; n     } n } n  n  n


#include <bits/stdc++.h> n using namespace std; n bool flag = 0, flag1 = 0, flag2 = 0; n string t =  abacaba ; n int check(string s) { n   long long int x = 0; n   for (long long int i = 0; i < s.size(); i++) { n     if (s.substr(i, 7) == t) { n       x++; n     } n   } n   return x; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int tt = 1; n   cin >> tt; n   while (tt--) { n     long long int n; n     cin >> n; n     string s; n     cin >> s; n     long long int ans = 0; n     ans = check(s); n     if (ans == 0) { n       for (long long int i = 0; i < n - 6; i++) { n         if (s[i] == t[0] || s[i] ==  ? ) { n           flag = 0; n           for (long long int j = 0; j < 7; j++) { n             if (s[j + i] != t[j] && s[i + j] !=  ? ) { n               flag = 1; n               break; n             } n           } n           if (flag == 0) { n             string temp = s; n             for (long long int j = 0; j < 7; j++) temp[i + j] = t[j]; n             if (check(temp) == 1) { n               ans++; n               s = temp; n               break; n             } n           } n         } n       } n     } n     if (ans == 1) { n       cout <<  Yes n ; n       for (long long int i = 0; i < s.size(); i++) { n         if (s[i] ==  ? ) n           cout <<  d ; n         else n           cout << s[i]; n       } n       cout <<   n ; n     } else n       cout <<  No n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct rua { n   long double x, y; n } m; n set<pair<long double, long double> > p; n vector<pair<long double, long double> > del; n vector<long double> d; n set<long double> mm; n int n, ans; n long double x, y; n bool tt; n bool check(rua T) { n   d.clear(); n   rua M = {(T.x + x) / 2, (T.y + y) / 2}; n   rua mM = {M.x - m.x, M.y - m.y}; n   rua r0 = {mM.y, -mM.x}; n   if (!r0.x) n     if (tt) n       return false; n     else n       tt = true; n   long double k = r0.y / r0.x; n   if (mm.count(k)) n     return false; n   else n     mm.insert(k); n   long double R = pow(r0.x * r0.x + r0.y * r0.y, 0.5); n   for (auto i = p.begin(); i != p.end(); i++) { n     rua mP = {(*i).first - m.x, (*i).second - m.y}; n     long double t = r0.x * mP.x + r0.y * mP.y; n     t /= r0.x * r0.x + r0.y * r0.y; n     if (t) d.push_back(t); n   } n   long double _ = (r0.x * mM.x + r0.y * mM.y) / R; n   int l = 0, r = d.size() - 1; n   sort(d.begin(), d.end()); n   while (l <= r && d[l] + d[r] == 0) l++, r--; n   return l > r; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     cin >> x >> y; n     p.insert(make_pair(x, y)); n     m.x += x, m.y += y; n   } n   m.x = m.x * 1.0 / n, m.y = m.y * 1.0 / n; n   for (auto i = p.begin(); i != p.end(); i++) { n     x = (*i).first, y = (*i).second; n     if (p.count(make_pair(2 * m.x - x, 2 * m.y - y))) n       del.push_back(make_pair(2 * m.x - x, 2 * m.y - y)), del.push_back(*i); n   } n   for (int i = 0; i < del.size(); i++) n     if (p.count(del[i])) p.erase(del[i]); n   if (!p.size()) return printf( -1 n ), 0; n   x = (*p.begin()).first, y = (*p.begin()).second; n   for (auto i = p.begin(); i != p.end(); i++) n     if (check({(*i).first, (*i).second})) ans++; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.tie(0); n   ios::sync_with_stdio(0); n   long long n; n   cin >> n; n   long long num = 1e5 + 3; n   long long pos[num]; n   for (int i = 0; i < n; ++i) { n     long long a; n     cin >> a; n     pos[a] = i + 1; n   } n   long long m; n   cin >> m; n   long long counta = 0, countb = 0; n   for (int j = 0; j < m; ++j) { n     long long b; n     cin >> b; n     counta += pos[b]; n     countb += n - pos[b] + 1; n   } n   cout << counta <<     << countb; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n const long long inf = 1e9 + 7; n long long mod = 998244353; n vector<int> a; n int solve(vector<int> &b, int bit) { n   vector<int> l, r; n   if (b.size() == 0 || bit < 0) return 0; n   for (int i = 0; i < b.size(); i++) { n     if ((b[i] >> bit) & 1 == 1) n       l.push_back(b[i]); n     else n       r.push_back(b[i]); n   } n   if (l.size() == 0) return solve(r, bit - 1); n   if (r.size() == 0) return solve(l, bit - 1); n   return min(solve(l, bit - 1), solve(r, bit - 1)) + (1 << bit); n } n int main(void) { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     int x; n     scanf( %d , &x); n     a.push_back(x); n   } n   int ans = solve(a, 30); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n int n, m, u, v, w, answer, capital, intree, l, dis[N]; n vector<pair<int, int> > nei[N]; n vector<pair<int, pair<int, int> > > edge; n bool done[N]; n priority_queue<pair<int, int> > pq; n int main() { n   cin >> n >> m >> capital; n   capital--; n   for (int i = 0; i < m; i++) { n     cin >> u >> v >> w; n     u--, v--; n     nei[v].push_back(make_pair(u, w)); n     nei[u].push_back(make_pair(v, w)); n     edge.push_back(make_pair(w, make_pair(u, v))); n   } n   cin >> l; n   done[capital] = true; n   for (int i = 0; i < nei[capital].size(); i++) { n     pq.push(make_pair(-nei[capital][i].second, nei[capital][i].first)); n   } n   while (intree < n - 1) { n     while (done[(pq.top()).second]) pq.pop(); n     pair<int, int> p = pq.top(); n     v = p.second; n     dis[v] = -p.first; n     done[v] = true; n     for (int i = 0; i < nei[v].size(); i++) { n       if (!done[nei[v][i].first]) n         pq.push(make_pair(-dis[v] - nei[v][i].second, nei[v][i].first)); n     } n     intree++; n   } n   for (int i = 0; i < n; i++) { n     answer += (dis[i] == l); n   } n   for (int i = 0; i < m; i++) { n     pair<int, pair<int, int> > pa = edge[i]; n     for (int j = 1; j < pa.first; j++) { n       if (min(dis[pa.second.first] + j, dis[pa.second.second] + pa.first - j) == n           l) { n         answer++; n       } n     } n   } n   cout << answer; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n; n   cin >> n; n   vector<long long> v(n + 1); n   long long xr = 0; n   for (long long i = 1; i <= n; i++) { n     cin >> v[i]; n     xr ^= v[i]; n   } n   if (xr == 0) { n     cout <<  DRAW  n          <<   n ; n     return; n   } n   long long msb; n   for (long long i = 32; ~i; i--) { n     if (xr & (1ll << i)) { n       msb = i; n       break; n     } n   } n   long long count = 0; n   for (long long i = 1; i <= n; i++) { n     count += ((v[i] & (1ll << msb)) > 0); n   } n   bool win = (count % 4 == 1 or ((n - count) & 1)); n   cout << (win ?  WIN  :  LOSE ) <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ul = unsigned long long; n using ld = long double; n const int N = 101; n int a[N][N]; n vector<vector<int>> ans; n void q(int ax, int ay, int bx, int by, int cx, int cy) { n   a[ax][ay] ^= 1; n   a[bx][by] ^= 1; n   a[cx][cy] ^= 1; n   ans.push_back({ax, ay, bx, by, cx, cy}); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t, n, m; n   cin >> t; n   for (int z = 0; z < (t); z++) { n     cin >> n >> m; n     string s; n     for (int i = 0; i < (n); i++) { n       cin >> s; n       for (int j = 0; j < (m); j++) a[i][j] = s[j] -  0 ; n     } n     ans.clear(); n     for (int i = 0; i < (n - 2); i++) n       for (int j = 0; j < (m); j++) { n         if (a[i][j]) q(i, j, i + 1, j, i + 1, j + (j == 0 ? 1 : -1)); n       } n     for (int i = 0; i < (m - 2); i++) { n       if (a[n - 2][i] && a[n - 1][i]) n         q(n - 2, i, n - 1, i, n - 2, i + 1); n       else if (a[n - 2][i]) n         q(n - 2, i, n - 2, i + 1, n - 1, i + 1); n       else if (a[n - 1][i]) n         q(n - 1, i, n - 1, i + 1, n - 2, i + 1); n     } n     int ss = 0; n     for (int i = 0; i < (2); i++) n       for (int j = 0; j < (2); j++) ss ^= a[n - 2 + i][m - 2 + j]; n     for (int i = 0; i < (2); i++) { n       for (int j = 0; j < (2); j++) { n         if (a[n - 2 + i][m - 2 + j] == ss) continue; n         vector<int> cv; n         for (int i1 = 0; i1 < (2); i1++) n           for (int j1 = 0; j1 < (2); j1++) n             if (i1 != i || j1 != j) { n               cv.push_back(n - 2 + i1); n               cv.push_back(m - 2 + j1); n             } n         ans.push_back(cv); n       } n     } n     cout << ans.size() <<   n ; n     for (auto &pp : ans) { n       for (int i : pp) cout << i + 1 <<    ; n       cout <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A, typename B> n string to_string(pair<A, B> p); n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p); n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p); n string to_string(const string& s) { return     + s +    ; } n string to_string(char ch) { n   string s; n   s += ch; n   return s; n } n string to_string(const char* s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n string to_string(vector<bool> v) { n   bool first = true; n   string res =  { ; n   for (int i = 0; i < static_cast<int>(v.size()); i++) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(v[i]); n   } n   res +=  } ; n   return res; n } n template <size_t N> n string to_string(bitset<N> v) { n   string res =   ; n   for (size_t i = 0; i < N; i++) { n     res += static_cast<char>( 0  + v[i]); n   } n   return res; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto& x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ) ; n } n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ,   + to_string(get<3>(p)) +  ) ; n } n void debug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   debug_out(T...); n } n long long expmod(long long x, long long n, long long mod) { n   if (n == 0) n     return 1; n   else if (n % 2 == 0) { n     long long temp = expmod(x, n / 2, mod); n     return (temp * temp) % mod; n   } else { n     return (expmod(x, n - 1, mod) * x) % mod; n   } n } n long long modinv(long long x, long long mod) { return expmod(x, mod - 2, mod); } n void solve() { n   int n; n   cin >> n; n   long long mod = 1e9 + 7; n   long long p = 0, q = 0; n   int even = 0; n   long long power = 2; n   for (int i = 0; i < n; i++) { n     long long x; n     cin >> x; n     even |= (x % 2 == 0); n     power = expmod(power, x, mod); n   } n   42; n   power = power * modinv(2, mod) % mod; n   p = power + (even ? 1 : -1); n   p = (p + mod) % mod; n   p = p * modinv(3, mod) % mod; n   q = power; n   42; n   cout << p <<  /  << q <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   for (int tc = 1; tc <= t; tc++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int Read() { n   char c; n   while (c = getchar(), (c !=  - ) && (c <  0  || c >  9 )) n     ; n   bool neg = (c ==  - ); n   int ret = (neg ? 0 : c - 48); n   while (c = getchar(), c >=  0  && c <=  9 ) ret = ret * 10 + c - 48; n   return neg ? -ret : ret; n } n const int MAXN = 1005; n int N, n, a[MAXN]; n pair<int, int> ans[MAXN * MAXN]; n void init() { n   scanf( %d , &N); n   for (int i = 1; i <= N; i++) a[i] = Read(), n += (a[i] > 0); n } n void work() { n   if (n < 2) { n     puts( -1 ); n     return; n   } else n     n = 0; n   int B = 1, C = 2; n   for (int i = 3; i <= N; i++) n     for (int A = i;;) { n       if (a[B] < a[A]) swap(A, B); n       if (a[C] < a[B]) swap(C, B); n       if (a[B] < a[A]) swap(A, B); n       if (!a[A]) break; n       for (int j = a[B] / a[A]; j; j >>= 1) n         if (j & 1) n           ans[++n] = make_pair(A, B), a[B] -= a[A], a[A] <<= 1; n         else n           ans[++n] = make_pair(A, C), a[C] -= a[A], a[A] <<= 1; n     } n   printf( %d n , n); n   for (int i = 1; i <= n; i++) printf( %d %d n , ans[i].first, ans[i].second); n } n int main() { n   init(); n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long int cvp = 3; n int main() { n   scanf( %d , &n); n   for (int i = 3; i <= n; i++) { n     cvp = cvp * 3; n     if (i % 2 == 1) n       cvp -= 3; n     else n       cvp += 3; n     cvp = cvp % (long long int)1000000007; n   } n   if (n == 1) { n     printf( 0 n ); n     return 0; n   } n   printf( %lld n , cvp); n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long ret = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) ret = ret * 10 + ch -  0 , ch = getchar(); n   return ret * f; n } n const long long maxn = 100005; n long long n, A, cj, cm, m; n pair<long long, long long> a[maxn]; n long long sum[maxn]; n long long ans = 0, ansx = -1, ansy = -1; n inline bool cmp_value(pair<long long, long long> aa, n                       pair<long long, long long> bb) { n   return aa.first < bb.first; n } n inline bool cmp_id(pair<long long, long long> aa, n                    pair<long long, long long> bb) { n   return aa.second < bb.second; n } n inline long long get_all_sum(long long x, long long up) { n   long long L = 1, R = up; n   long long ret = 0; n   while (L <= R) { n     long long mid = ((R - L) >> 1) + L; n     if (a[mid].first > x) n       R = mid - 1; n     else n       ret = x * mid - sum[mid], L = mid + 1; n   } n   return ret; n } n signed main() { n   n = read(); n   A = read(); n   cj = read(); n   cm = read(); n   m = read(); n   for (long long i = 1; i <= n; i++) a[i].first = read(), a[i].second = i; n   sort(a + 1, a + 1 + n, cmp_value); n   for (long long i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i].first; n   long long need = 0; n   for (long long i = n + 1; i >= 1; i--) { n     if (i <= n) need += A - a[i].first; n     if (need > m) break; n     long long L = 0, R = A; n     while (L <= R) { n       long long mid = ((R - L) >> 1) + L; n       long long all_sum = get_all_sum(mid, i - 1); n       if (all_sum > m - need) n         R = mid - 1; n       else { n         if ((n - i + 1) * cj + mid * cm > ans) { n           ans = (n - i + 1) * cj + mid * cm; n           ansx = mid; n           ansy = i; n         } n         L = mid + 1; n       } n     } n   } n   printf( %lld n , ans); n   if (ansx != -1 && ansy != -1) { n     for (long long i = 1; i < ansy; i++) a[i].first = max(ansx, a[i].first); n     for (long long i = ansy; i <= n; i++) a[i].first = A; n   } n   sort(a + 1, a + 1 + n, cmp_id); n   for (long long i = 1; i <= n; i++) printf( %lld  , a[i].first); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void read(int &x) { n   char c; n   int flag = 1; n   while ((c = getchar()) <  0  || c >  9 ) n     if (c ==  - ) flag *= -1; n   x = c -  0 ; n   while ((c = getchar()) >=  0  && c <=  9 ) x = x * 10 + c -  0 ; n   x *= flag; n   return; n } n inline void read(long long &x) { n   char c; n   int flag = 1; n   while ((c = getchar()) <  0  || c >  9 ) n     if (c ==  - ) flag *= -1; n   x = c -  0 ; n   while ((c = getchar()) >=  0  && c <=  9 ) x = x * 10 + c -  0 ; n   x *= flag; n   return; n } n int dp[5005][5005]; n int arr[5005]; n int main() { n   int n; n   read(n); n   for (int i = 1; i <= n; ++i) { n     read(arr[i]); n     dp[i][1] = arr[i]; n   } n   for (int i = 2; i <= n; ++i) { n     for (int j = 1; j <= n; ++j) { n       dp[j][i] = dp[j + 1][i - 1] ^ dp[j][i - 1]; n     } n   } n   for (int i = 2; i <= n; ++i) { n     for (int j = 1; j <= n; ++j) { n       dp[j][i] = max(dp[j][i], max(dp[j + 1][i - 1], dp[j][i - 1])); n     } n   } n   int q; n   cin >> q; n   while (q--) { n     int l, r; n     read(l); n     read(r); n     printf( %d n , dp[l][r - l + 1]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n int add(int a, int b) { return (a + b) % MOD; } n int sub(int a, int b) { return add(a, MOD - b); } n int mul(int a, int b) { return ((long long)a * b) % MOD; } n int inv(int a) { n   int b = 1; n   for (int i = 0; (1 << i) <= MOD - 2; i++) { n     if ((1 << i) & (MOD - 2)) b = mul(b, a); n     a = mul(a, a); n   } n   return b; n } n const int MAXN = 1e5 + 10; n vector<int> e[MAXN]; n int val[MAXN], sz[MAXN], parent[MAXN]; n map<int, int> dist[MAXN]; n int A[MAXN], B[MAXN], total; n bool active[MAXN]; n vector<int> in[MAXN], out[MAXN]; n int calc_sz(int i, int p) { n   sz[i] = 1; n   for (int j : e[i]) { n     if (!active[j] || j == p) continue; n     sz[i] += calc_sz(j, i); n   } n   return sz[i]; n } n int find_centroid(int i, int p, int n) { n   for (int j : e[i]) { n     if (!active[j] || j == p) continue; n     if (sz[j] > n / 2) return find_centroid(j, i, n); n   } n   return i; n } n void calc_dist(int i, int p, int r, int d) { n   dist[i][r] = d; n   for (int j : e[i]) { n     if (!active[j] || j == p) continue; n     calc_dist(j, i, r, d + 1); n   } n } n int build_centroid(int i) { n   i = find_centroid(i, -1, calc_sz(i, -1)); n   calc_dist(i, -1, i, 0); n   active[i] = false; n   for (int j : e[i]) { n     if (!active[j]) continue; n     parent[build_centroid(j)] = i; n   } n   return i; n } n void update(int i, int k) { n   int v = mul(val[i], MOD + k); n   for (int p = i; p != -1; p = parent[p]) { n     total = sub(total, mul(A[p], B[p])); n     A[p] = add(A[p], mul(v, dist[i][p])); n     if (parent[p] != -1) A[p] = sub(A[p], mul(v, dist[i][parent[p]])); n     B[p] = add(B[p], v); n     total = add(total, mul(A[p], B[p])); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   int t = 1; n   for (int i = 0; i < n; i++) { n     int l, r; n     cin >> l >> r; n     in[l].push_back(i); n     out[r].push_back(i); n     val[i] = inv(r - l + 1); n     t = mul(t, r - l + 1); n     active[i] = true; n   } n   for (int i = 0; i < n - 1; i++) { n     int x, y; n     cin >> x >> y; n     x--; n     y--; n     e[x].push_back(y); n     e[y].push_back(x); n   } n   parent[build_centroid(0)] = -1; n   int sol = 0; n   for (int color = 0; color < MAXN; color++) { n     for (int i : in[color]) update(i, 1); n     sol = add(sol, total); n     for (int i : out[color]) update(i, -1); n   } n   cout << mul(sol, t); n   return 0; n } n


#include <bits/stdc++.h> n #pragma hdrstop n using namespace std; n void solve(); n int main() { n   int t = 1; n   while (t--) solve(); n   return 0; n } n long long n, k; n long long dp[5000][5000]; n vector<pair<long long, long long> > a; n long long b[5000]; n void solve() { n   cin >> n >> k; n   a.push_back(make_pair(1, 0)); n   b[0] = 1; n   for (long long i = 0; i < n; i++) { n     long long q, w; n     cin >> q >> w; n     a.push_back(make_pair(q, w)); n     b[i + 1] = q; n   } n   a.push_back(make_pair(86401, 0)); n   b[n + 1] = 86401; n   long long border = 0; n   for (long long i = 0; i <= n + 5; i++) n     for (long long j = 0; j <= i; j++) dp[i][j] = (1LL << 50); n   dp[0][0] = 1; n   for (long long i = 0; i <= n; i++) n     for (long long j = 0; j <= i; j++) { n       pair<long long, long long> cur = a[i + 1]; n       dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j]); n       long long now = max(b[i + 1], dp[i][j]) + cur.second; n       dp[i + 1][j] = min(dp[i + 1][j], now); n     } n   long long ans = 0; n   for (long long i = 1; i <= n + 1; i++) { n     for (long long j = 0; j <= k; j++) { n       long long cur = dp[i - 1][j]; n       long long now = b[i]; n       ans = max(ans, now - cur); n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n long long arr[500005]; n map<long long, int> mm; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %lld , &arr[i]); n   for (int i = 1; i <= n - 1; i++) arr[i] += arr[i - 1]; n   for (int i = 0; i < n; i++) mm[arr[i]]++; n   int mx = -1; n   map<long long, int>::iterator it; n   for (it = mm.begin(); it != mm.end(); it++) { n     mx = max(mx, it->second); n   } n   printf( %d n , n - mx); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100007; n long long i, j, k, n, m, t, x, y, ans; n long long pw2[32]; n long long a[MAXN]; n void calPowerTwo() { n   long long i, j, k; n   pw2[0] = 1; n   for (i = 1; i < 32; i++) { n     pw2[i] = pw2[i - 1] * 2; n   } n } n int main() { n   calPowerTwo(); n   while (cin >> n) { n     for (i = 0; i < n; i++) { n       cin >> a[i]; n     } n     ans = 0; n     for (i = 0; i < n - 1; i++) { n       ans += a[i]; n       k = 0; n       while (i + pw2[k] < n) { n         k++; n       } n       k--; n       a[i + pw2[k]] += a[i]; n       a[i] = 0; n       cout << ans << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dadsadasda; n const int INF = 0x3f3f3f3f; n const long long LLINF = 1e18; n const int MAXN = 3e5; n int n; n int a[MAXN]; n int ans[MAXN]; n priority_queue<pair<int, int> > pq; n int father[MAXN], siz[MAXN], cnt[MAXN]; n void makeSet(int nn, int m) { n   for (int i = nn; i <= m; ++i) { n     father[i] = -1; n     siz[i] = 0; n     cnt[i] = 1; n   } n } n int find(int x) { n   if (father[x] == -1) return x; n   return father[x] = find(father[x]); n } n void unio(int x, int y) { n   if (siz[x] > siz[y]) { n     father[y] = x; n     cnt[x] += cnt[y]; n   } else { n     father[x] = y; n     cnt[y] += cnt[x]; n     if (siz[x] == siz[y]) siz[y]++; n   } n } n bool active[MAXN]; n int main() { n   dadsadasda = scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     dadsadasda = scanf( %d , &a[i]); n     pq.push({a[i], i}); n   } n   makeSet(0, n - 1); n   while (!pq.empty()) { n     auto p = pq.top(); n     pq.pop(); n     active[p.second] = true; n     if (p.second > 0 && find(p.second) != find(p.second - 1) && n         active[p.second - 1]) n       unio(find(p.second), find(p.second - 1)); n     if (p.second < n - 1 && find(p.second) != find(p.second + 1) && n         active[p.second + 1]) n       unio(find(p.second), find(p.second + 1)); n     int sz = cnt[find(p.second)]; n     ans[sz] = max(ans[sz], p.first); n   } n   for (int i = n - 1; i >= 1; i--) { n     ans[i] = max(ans[i], ans[i + 1]); n   } n   for (int i = 1; i < n + 1; i++) { n     printf( %d  , ans[i]); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n double const MOD = 1000000007; n double const N = 1000005; n double const LN = 20; n double const inf = 1e18 + 5; n using namespace std; n double a, b; n void mirror(double &x, double &z) { n   if (x < 0) { n     x = -x; n     return; n   } n   if (z < 0) { n     z = -z; n     return; n   } n   if (x > a) { n     x = 2 * a - x; n     return; n   } n   if (z > b) { n     z = 2 * b - z; n     return; n   } n } n int main() { n   double m; n   double vx, vy, vz; n   cin >> a >> b >> m; n   cin >> vx >> vy >> vz; n   double ok = abs(m / vy); n   double x = a / 2 + ok * vx; n   double z = 0 + vz * ok; n   while (1) { n     if (x >= 0 && x <= a && z >= 0 && z <= b) break; n     mirror(x, z); n   } n   printf( %.10f %.10f n , x, z); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ara[100010]; n int segtree[20][100010 * 4]; n int lazy[20][100010 * 4]; n void build(int node, int l, int r, int po) { n   if (l == r) { n     segtree[po][node] = 0; n     if ((ara[l] & (1 << po))) { n       segtree[po][node] = 1; n     } n     return; n   } n   int mid = (l + r) >> 1; n   int left = node << 1; n   int right = left | 1; n   build(left, l, mid, po); n   build(right, mid + 1, r, po); n   segtree[po][node] = segtree[po][left] + segtree[po][right]; n   return; n } n int query(int node, int l, int r, int ql, int qr, int po) { n   if (lazy[po][node]) { n     segtree[po][node] = (r - l + 1) - segtree[po][node]; n     if (l != r) { n       lazy[po][(node << 1)] ^= 1; n       lazy[po][1 + (node << 1)] ^= 1; n     } n     lazy[po][node] = 0; n   } n   if (l >= ql && qr >= r) { n     return segtree[po][node]; n   } n   if (l > qr || ql > r) return 0; n   int mid = (l + r) >> 1; n   int left = node << 1; n   int right = left | 1; n   int p = query(left, l, mid, ql, qr, po); n   int q = query(right, mid + 1, r, ql, qr, po); n   return p + q; n } n void update(int node, int l, int r, int ql, int qr, int po) { n   if (lazy[po][node]) { n     segtree[po][node] = (r - l + 1) - segtree[po][node]; n     if (l != r) { n       lazy[po][node << 1] ^= 1; n       lazy[po][1 + (node << 1)] ^= 1; n     } n     lazy[po][node] = 0; n   } n   if (l >= ql && qr >= r) { n     segtree[po][node] = (r - l + 1) - segtree[po][node]; n     if (l != r) { n       lazy[po][node << 1] ^= 1; n       lazy[po][1 + (node << 1)] ^= 1; n     } n     lazy[po][node] = 0; n     return; n   } n   if (l > qr || r < ql) return; n   int mid = (l + r) >> 1; n   int left = node << 1; n   int right = left | 1; n   update(left, l, mid, ql, qr, po); n   update(right, mid + 1, r, ql, qr, po); n   segtree[po][node] = segtree[po][left] + segtree[po][right]; n   return; n } n int main() { n   long long n; n   scanf( %lld , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &ara[i]); n   } n   for (int i = 0; i < 20; i++) { n     build(1, 1, n, i); n   } n   long long m; n   scanf( %lld , &m); n   while (m--) { n     long long x; n     scanf( %lld , &x); n     if (x == 1) { n       long long a, b; n       scanf( %lld%lld , &a, &b); n       long long sum = 0; n       for (int i = 0; i < 20; i++) { n         int y = query(1, 1, n, a, b, i); n         sum += y * (1LL << i); n       } n       printf( %lld n , sum); n     } else { n       long long a, b, c; n       scanf( %lld%lld , &a, &b); n       scanf( %lld , &c); n       for (int i = 0; i < 20; i++) { n         if (c & (1 << i)) { n           update(1, 1, n, a, b, i); n         } n       } n     } n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n struct road { n   int x, next; n   double dis; n } r[305 * 305 * 2]; n int N, M; n int st[305], w; n int vis[305]; n double P[305]; n double l1 = -1, r1 = 0, mid; n double dis[305]; n void add(int x, int y, double dis) { n   r[++w].x = y, r[w].next = st[x]; n   r[w].dis = dis; n   st[x] = w; n } n bool work(int x) { n   int i, j, tmp; n   if (vis[x]) return true; n   vis[x] = 1; n   for (i = st[x]; i; i = r[i].next) { n     tmp = r[i].x; n     if (dis[tmp] > dis[x] + r[i].dis - mid) { n       dis[tmp] = dis[x] + r[i].dis - mid; n       if (work(tmp)) return true; n     } n   } n   vis[x] = 0; n   return false; n } n int main() { n   int i, j; n   int fr, to; n   scanf( %d , &N); n   for (i = 0; i <= N; i++) scanf( %lf , &P[i]); n   M = N * 3; n   for (i = 0; i <= M; i++) n     for (j = N, fr = i; j >= -N; j -= 2) { n       to = fr + j; n       if (to > M || to < 0) continue; n       add(fr, to, -P[(N - j) / 2]); n     } n   for (i = 1; i <= 21; i++) { n     mid = (l1 + r1) / 2; n     for (j = 1; j <= 20; j++) { n       memset(dis, 63, sizeof(dis)); n       memset(vis, 0, sizeof(vis)); n       fr = rand() % M + 1, dis[fr] = 0; n       if (work(fr)) break; n     } n     if (j <= 9) n       r1 = mid; n     else n       l1 = mid; n   } n   printf( %.8lf n , -mid); n   return 0; n } n


#include <bits/stdc++.h> n char map[1005][1005]; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   for (int r = 0; r < n; r++) scanf( %s , map[r]); n   int row[1005] = {0}; n   int col[1005] = {0}; n   for (int r = 0; r < n; r++) { n     int count = 0; n     for (int c = 0; c < m; c++) n       if (map[r][c] ==  * ) count++; n     row[r] = count; n   } n   for (int c = 0; c < m; c++) { n     int count = 0; n     for (int r = 0; r < n; r++) n       if (map[r][c] ==  * ) count++; n     col[c] = count; n   } n   long long answer = 0; n   for (int r = 0; r < n; r++) n     for (int c = 0; c < m; c++) n       if (map[r][c] ==  * ) answer += (row[r] - 1) * (col[c] - 1); n   printf( %I64d n , answer); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 20; n const int mod = 1e9 + 7; n long double a[N]; n int n, k; n long double dp[1 << N], out[N]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> k; n   int zeros = 0; n   for (int i = 0; i < n; i++) cin >> a[i], zeros += fabs(a[i] < (1e-7)); n   k = min(k, n - zeros); n   dp[0] = 1.0; n   for (int i = 0; i < 1 << n; i++) { n     double tmp = 1.0; n     for (int j = 0; j < n; j++) n       if (i >> j & 1) tmp -= a[j]; n     if (__builtin_popcount(i) == k) n       for (int j = 0; j < n; j++) n         if (i >> j & 1) out[j] += dp[i]; n     if (tmp == 0.0) continue; n     cout << fixed << setprecision(6); n     for (int j = 0; j < n; j++) n       if (!(i >> j & 1)) dp[i | 1 << j] += a[j] * dp[i] / tmp; n   } n   for (int i = 0; i < n; i++) cout << out[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n const long long N = 1000000; n using namespace std; n long long n, k, l, i = 0, j, mn, mx = N, ans = 0, als, r1, r2, r3, t, q, v, m, n                    ii, r4; n string d1, d3, d2, s; n char kl, km, mk, lk; n double fg; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   cin >> n; n   bool a[N]; n   for (i = 0; i < n * 2; i++) { n     cin >> t; n     a[t - 1] = !a[t - 1]; n     m += a[t - 1] * 2 - 1; n     ans = max(ans, m); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   long long r1, r2, r3, d; n   cin >> n >> r1 >> r2 >> r3 >> d; n   vector<int> a(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   vector<vector<long long>> dp(n, vector<long long>(2, LLONG_MAX)); n   dp[0][0] = r1 * a[0] + r3; n   dp[0][1] = min(r1 * (a[0] + 2), r2 + r1); n   for (int i = 1; i < n; i++) { n     long long one = r1 * a[i] + r3; n     long long two = min(r1 * (a[i] + 2), r2 + r1); n     dp[i][0] = min(dp[i][0], dp[i - 1][0] + one); n     dp[i][0] = min(dp[i][0], dp[i - 1][1] + min(one, two) + 2 * d); n     if (i == n - 1) dp[i][0] = min(dp[i][0], dp[i - 1][1] + one + d); n     dp[i][1] = min(dp[i][1], dp[i - 1][0] + two); n   } n   printf( %lld n , min(dp[n - 1][0], dp[n - 1][1] + 2 * d) + (n - 1) * d); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n const long long MOD = 1000000007; n vector<vector<long long> > matrix_mul(vector<vector<long long> > a, n                                       vector<vector<long long> > b) { n   vector<vector<long long> > c(a.size(), vector<long long>(b[0].size())); n   for (int i = 0; i < a.size(); i++) n     for (int j = 0; j < b[0].size(); j++) n       for (int k = 0; k < b.size(); k++) { n         c[i][j] += (a[i][k] * b[k][j]) % MOD; n         c[i][j] %= MOD; n       } n   return c; n } n vector<vector<long long> > matrix_pow(vector<vector<long long> > a, n                                       long long p) { n   if (p == 1) return a; n   if (p % 2 == 1) return matrix_mul(a, matrix_pow(a, p - 1)); n   vector<vector<long long> > c = matrix_pow(a, p / 2); n   return matrix_mul(c, c); n } n int N, B, K, X; n long long occ[10]; n int main() { n   cin >> N >> B >> K >> X; n   for (int i = 0; i < N; i++) { n     int d; n     cin >> d; n     occ[d]++; n   } n   vector<long long> f(X); n   f[0] = 1; n   vector<vector<long long> > T(X); n   for (int i = 0; i < X; i++) { n     T[i] = vector<long long>(X); n   } n   for (int i = 0; i < X; i++) { n     for (int d = 1; d <= 9; d++) n       if (occ[d]) { n         int a = (i * 10 + d) % X; n         T[a][i] += occ[d]; n       } n   } n   T = matrix_pow(T, B); n   long long res = 0; n   res = (T[K][0] * f[0] % MOD) % MOD; n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e3 + 5; n const int inf = 0x3f3f3f3f; n const long long mod = 1e9 + 7; n const double eps = 1e-6; n vector<int> g[maxn]; n vector<int> tree[maxn]; n int deep[maxn], son[maxn]; n int cnt, mx; n void dfs(int x, int y) { n   int sz = g[x].size(); n   deep[x] = 0; n   son[x] = -1; n   for (int i = 0; i < sz; ++i) { n     int to = g[x][i]; n     if (to == y) { n       continue; n     } n     tree[x].push_back(to); n     dfs(to, x); n     if (son[x] == -1 || deep[son[x]] < deep[to]) { n       son[x] = son[to]; n     } n   } n   if (son[x] == -1) { n     son[x] = x; n   } else { n     deep[x] = deep[son[x]] + 1; n   } n } n struct node { n   int id, w; n   bool operator<(const node& rhs) const { return w > rhs.w; } n } nd[maxn]; n int solve(int x) { n   int sz = tree[x].size(); n   for (int i = 0; i < sz; ++i) { n     nd[i] = {tree[x][i], deep[tree[x][i]]}; n   } n   sort(nd, nd + sz); n   int hf = sz / 2; n   for (int i = 0; i < hf; ++i) { n     int a = nd[i * 2].id, b = nd[i * 2 + 1].id; n     ++cnt; n     printf( ? %d %d n , son[a], son[b]); n     fflush(stdout); n     int c; n     scanf( %d , &c); n     if (c != x) { n       return solve(c); n     } n     if (cnt >= mx) break; n   } n   if ((sz & 1) == 1 && cnt < mx) { n     ++cnt; n     printf( ? %d %d n , x, son[nd[sz - 1].id]); n     fflush(stdout); n     int c; n     scanf( %d , &c); n     if (c != x) { n       return solve(c); n     } n   } n   return x; n } n int main() { n   int n; n   scanf( %d , &n); n   cnt = 0, mx = n / 2; n   for (int i = 1; i < n; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     g[u].push_back(v); n     g[v].push_back(u); n   } n   dfs(1, -1); n   printf( ! %d n , solve(1)); n   fflush(stdout); n   return 0; n } n