#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n const int G = N; n set<pair<int, int> > S; n set<int> a[N]; n int ans[N]; n bool was[N]; n vector<int> C[G + G]; n int n, m; n bool solve() { n   while (!S.empty()) { n     int idx = S.begin()->second; n     if (a[idx].empty()) { n       cerr <<  fail   << idx << endl; n       return 0; n     } n     int u = *a[idx].begin(); n     ans[abs(u)] = u / abs(u); n     was[idx] = 1; n     S.erase(S.begin()); n     for (int v : C[u + G]) n       if (!was[v]) { n         was[v] = 1; n         auto it = S.find(pair<int, int>(a[v].size(), v)); n         assert(it != S.end()); n         S.erase(it); n       } n     for (int v : C[-u + G]) n       if (!was[v]) { n         auto it = S.find(pair<int, int>(a[v].size(), v)); n         assert(it != S.end()); n         a[v].erase(-u); n         S.erase(it); n         S.insert(pair<int, int>(a[v].size(), v)); n       } n   } n   return 1; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   int s, u; n   for (int i = 1; i <= n; ++i) { n     cin >> s; n     for (int j = 1; j <= s; ++j) { n       cin >> u; n       a[i].insert(u); n       C[u + G].push_back(i); n     } n     S.insert(pair<int, int>(a[i].size(), i)); n   } n   bool res = solve(); n   if (res) { n     cout <<  YES n ; n     for (int i = 1; i <= m; ++i) cout << (ans[i] == 1); n   } else { n     cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n char an[505]; n vector<int> b, other[5]; n int A[505][505], visited[505], ans[5]; n int dfs(int node, int flag) { n   int sz = 1; n   visited[node] = flag; n   other[flag].push_back(node); n   for (int i = 0; i < n; ++i) n     if (!visited[i] and A[node][i]) sz += dfs(i, flag); n   return sz; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; ++i) { n     int a, b; n     scanf( %d%d , &a, &b); n     --a; n     --b; n     A[a][b] = A[b][a] = 1; n   } n   for (int i = 0; i < n; ++i) { n     an[i] =  b ; n     bool connected = true; n     for (int j = 0; j < n and connected; ++j) n       if (i != j and !A[i][j]) connected = false; n     if (connected) { n       visited[i] = 5; n       b.push_back(i); n     } n   } n   for (int i = 0; i < (int)b.size(); ++i) n     for (int j = 0; j < n; ++j) A[j][b[i]] = A[b[i]][j] = 0; n   int cnt = 0; n   for (int i = 0; i < n; ++i) { n     if (!visited[i]) { n       if (cnt == 2) { n         printf( No n ); n         return 0; n       } n       ans[cnt] = dfs(i, cnt + 1); n       ++cnt; n     } n   } n   for (int i = 1; i < 3; ++i) n     for (int j = 0; j < (int)other[i].size(); ++j) n       for (int k = j + 1; k < (int)other[i].size(); ++k) { n         assert(A[other[i][j]][other[i][k]] == A[other[i][k]][other[i][j]]); n         if (!A[other[i][j]][other[i][k]]) { n           printf( No n ); n           return 0; n         } n       } n   printf( Yes n ); n   for (int i = 0; i < ans[0]; ++i) an[other[1][i]] =  a ; n   for (int i = 0; i < ans[1]; ++i) an[other[2][i]] =  c ; n   an[n] =   0 ; n   printf( %s n , an); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1e9 + 7; n int dx[] = {-1, 1, 0, 0, -1, 1, -1, 1}; n int dy[] = {0, 0, 1, -1, 1, -1, -1, 1}; n void fast() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n } n long long gcd(long long x, long long y) { n   if (!y) return x; n   return gcd(y, x % y); n } n long long lcm(long long x, long long y) { return (x / gcd(x, y)) * y; } n long long fastpow(long long a, long long b) { n   long long res = 1; n   int last; n   while (b) { n     if (b % 2) res *= a; n     b /= 2, a *= a; n   } n   return res; n } n bool isprime(long long n) { n   if (n == 1 || n == 0) return 0; n   for (int i = 2; i <= sqrt(n); i++) n     if (n % i == 0) return 0; n   return 1; n } n int main() { n   fast(); n   int ma = INT_MIN; n   int n, a; n   cin >> n; n   bool found = 1; n   stack<int> stk; n   for (int i = 0; i < n; i++) { n     cin >> a; n     ma = max(ma, a); n     if (!stk.empty() && a > stk.top()) found = 0; n     if (stk.empty() || a != stk.top()) n       stk.push(a); n     else n       stk.pop(); n   } n   if (!found || (stk.size() == 1 && stk.top() < ma)) n     cout <<  NO n ; n   else n     cout << (stk.size() > 1 ?  NO n  :  YES n ); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Tp> n inline void outarr(Tp _begin, Tp _end, const char* _delim =    ) { n   for (Tp current = _begin; current != _end; ++current) { n     std::cout << *current << _delim; n   } n   std::cout <<   n ; n } n using ll = int64_t; n using pii = std::pair<int, int>; n constexpr int INF = 0x3f3f3f3f; n constexpr int MOD = static_cast<const int>(1e9 + 7); n int F(int x) { n   if (x == 1) { n     return 1; n   } n   return 12 * (x - 1); n } n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(nullptr); n   int a; n   cin >> a; n   ll ans = 0; n   for (int i = 1; i <= a; ++i) { n     ans += F(i); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int L = 1e5 + 1; n long long sofar[L], out[L]; n struct node { n   long long l, r, q; n }; n long long ans = 0, counter[3000002], k; n bool comp(node x, node y) { return x.r < y.r; } n void add(long long val) { n   ans += counter[val ^ k]; n   counter[val]++; n   return; n } n void remove(long long val) { n   counter[val]--; n   ans -= counter[val ^ k]; n   return; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long a, n, m, cur = 0, l, r; n   cin >> n >> m >> k; n   for (int i = 1; i < n + 1; ++i) { n     cin >> a; n     sofar[i] ^= sofar[i - 1]; n     sofar[i] ^= a; n   } n   std::vector<node> v[n / 316 + 5]; n   node temp; n   for (int i = 0; i < m; ++i) { n     cin >> l >> r; n     l--, r++; n     temp.l = l, temp.r = r, temp.q = i; n     v[l / 316].push_back(temp); n   } n   for (int i = 0; i < n / 316 + 1; ++i) sort(v[i].begin(), v[i].end(), comp); n   for (int i = 0; i < n / 316 + 3; ++i) { n     l = r = i * 316; n     ans = 0; n     for (auto &x : v[i]) { n       while (r < x.r) { n         add(sofar[r]); n         ++r; n       } n       while (l < x.l) { n         remove(sofar[l]); n         l++; n       } n       while (l > x.l) { n         --l; n         add(sofar[l]); n       } n       out[x.q] = ans; n     } n     for (int j = l; j < r; ++j) remove(sofar[j]); n   } n   for (int i = 0; i < m; ++i) cout << out[i] <<   n ; n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n #define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); n #define endl   n  n typedef long long LL; n const int maxn = 2e5 + 5; n  n int cnt, id, a[maxn], hea[maxn], siz[maxn], dfsid[maxn], sum[maxn]; n  n map<int, int> exist, tol; n  n struct node{ n     int v, next; n }edge[maxn << 1]; n  n inline void add(int u, int v){ n     edge[++cnt].v = v; n     edge[cnt].next = hea[u]; n     hea[u] = cnt; n } n  n inline void dfs(int u, int fa){ n     siz[u] = 1; n     dfsid[u] = ++id; n     int pre = exist[a[u]]++; n     for(int k = hea[u]; k; k = edge[k].next){ n         int v = edge[k].v; n         if(v == fa) continue; n         int x = exist[a[u]]; n         dfs(v, u); n         siz[u] += siz[v]; n         if(x != exist[a[u]]){   // 子树内出现了父节点的值，删除子树外所有点 n             sum[1]++; n             sum[dfsid[v]]--; n             sum[dfsid[v] + siz[v]]++; n         } n     } n     if(exist[a[u]] - pre != tol[a[u]]){ // 子树外存在父节点的值，删除子树内所有点 n         sum[dfsid[u]]++; n         sum[dfsid[u] + siz[u]]--; n     } n } n  n int main(){ n     IOS; n     int n, u, v; n     cin >> n; n     for(int i = 1; i <= n; i++){ n         cin >> a[i]; n         tol[a[i]]++; n     } n     for(int i = 1; i < n; i++){ n         cin >> u >> v; n         add(u, v); n         add(v, u); n     } n     dfs(1, 1); n     int ans = 0; n     for(int i = 1; i <= n; i++){ n         sum[i] += sum[i - 1]; n         ans += sum[i] == 0; n     } n     cout << ans << endl; n } n  n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n template <class T1, class T2> n struct cmpf { n   bool rev; n   inline bool operator()(const pair<T1, T2>& a, const pair<T1, T2>& b) const { n     return (a.first < b.first) ^ rev; n   } n   cmpf(bool b = false) { rev = b; } n }; n template <class T1, class T2> n struct cmps { n   bool rev; n   inline bool operator()(const pair<T1, T2>& a, const pair<T1, T2>& b) const { n     return (a.second < b.second) ^ rev; n   } n   cmps(bool b = false) { rev = b; } n }; n string to_string(const char& c) { return string(1, c); } n string to_string(const string& s) { return     + s +    ; } n string to_string(const char* s) { return to_string((string)s); } n string to_string(const bool& b) { return (b ?  true  :  false ); } n string to_string(const vector<bool>& v, const string& sep =    ) { n   string s =  [ ; n   for (int i = 0; i < v.size(); i++) { n     if (i) { n       s += sep; n     } n     s += to_string(v[i]); n   } n   return s +  ] ; n } n template <size_t N> n string to_string(const bitset<N>& v) { n   string s =  [ ; n   for (size_t i = 0; i < N; i++) s += v[i] ?  1  :  0 ; n   return s +  ] ; n } n template <class T1, class T2> n string to_string(const pair<T1, T2>& p, const string& sep =  , ); n template <class T> n string to_string(const T& v, const string& sep =    ) { n   bool first = true; n   string s =  { ; n   for (const auto& x : v) { n     if (!first) n       s += sep; n     else n       first = false; n     s += to_string(x); n   } n   return s +  } ; n } n template <class T> n string to_string(const T& v, const int& sz, const string& sep =    ) { n   string s =  [ ; n   for (int i = 0; i < sz; i++) { n     if (i) { n       s += sep; n     } n     s += to_string(v[i]); n   } n   return s +  ] ; n } n template <class T1, class T2> n string to_string(const pair<T1, T2>& p, const string& sep) { n   return  (  + to_string(p.first) + sep + to_string(p.second) +  ) ; n } n long long binpow(const long long& x, const long long& p, const long long& mod) { n   assert(mod > 0); n   if (p == 0) return 1; n   if (p == 1) return x % mod; n   if (p & 1) return (binpow((x * x) % mod, p / 2, mod) * x) % mod; n   return binpow((x * x) % mod, p / 2, mod) % mod; n } n template <class T> n void chmn(T& a, const T& b) { n   if (a > b) a = b; n } n template <class T> n void chmx(T& a, const T& b) { n   if (a < b) a = b; n } n void setIO() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n } n int chr[8] = {-1, 0, 1, 0, -1, -1, 1, 1}; n int chc[8] = {0, 1, 0, -1, -1, 1, -1, 1}; n const long long MOD = 1e9 + 7; n const int INIT = 1001001001; n const int MN = 255; n bool mat[MN][MN]; n int N, M; n inline const bool out(int r, int c) { n   return r <= 0 || r > N || c <= 0 || c > M; n } n inline const bool ok(int r, int c, int skip1, int skip2, int n) { n   if (out(r, c) || !mat[r][c]) return false; n   for (int i = 0; i < n; i++) { n     if (i != skip1 && i != skip2) { n       if (mat[r + chr[i]][c + chc[i]]) return false; n     } n   } n   return true; n } n bool side_square(int r, int c, int len) { n   if (!ok(r, c, 1, 2, 4)) return false; n   if (mat[r - 1][c - 1]) return false; n   r++; n   for (int i = 0; i + 2 < len; i++) { n     if (!ok(r, c, 0, 2, 4)) return false; n     r++; n   } n   if (!ok(r, c, 0, 1, 4)) return false; n   if (mat[r + 1][c - 1]) return false; n   c++; n   for (int i = 0; i + 2 < len; i++) { n     if (!ok(r, c, 1, 3, 4)) return false; n     c++; n   } n   if (!ok(r, c, 0, 3, 4)) return false; n   if (mat[r + 1][c + 1]) return false; n   r--; n   for (int i = 0; i + 2 < len; i++) { n     if (!ok(r, c, 0, 2, 4)) return false; n     r--; n   } n   if (!ok(r, c, 2, 3, 4)) return false; n   if (mat[r - 1][c + 1]) return false; n   c--; n   for (int i = 0; i + 2 < len; i++) { n     if (!ok(r, c, 1, 3, 4)) return false; n     c--; n   } n   return true; n } n bool diag_square(int r, int c, int len) { n   if (!ok(r, c, 6, 7, 8)) return false; n   r++; n   c--; n   for (int i = 0; i + 2 < len; i++) { n     if (!ok(r, c, 5, 6, 8)) return false; n     r++; n     c--; n   } n   if (!ok(r, c, 5, 7, 8)) return false; n   r++; n   c++; n   for (int i = 0; i + 2 < len; i++) { n     if (!ok(r, c, 4, 7, 8)) return false; n     r++; n     c++; n   } n   if (!ok(r, c, 4, 5, 8)) return false; n   r--; n   c++; n   for (int i = 0; i + 2 < len; i++) { n     if (!ok(r, c, 5, 6, 8)) return false; n     r--; n     c++; n   } n   if (!ok(r, c, 4, 6, 8)) return false; n   r--; n   c--; n   for (int i = 0; i + 2 < len; i++) { n     if (!ok(r, c, 4, 7, 8)) return false; n     r--; n     c--; n   } n   return true; n } n void solve() { n   cin >> N >> M; n   for (int i = 0; i < N + 5; i++) { n     for (int j = 0; j < M + 5; j++) { n       mat[i][j] = false; n     } n   } n   char c; n   for (int i = 1; i <= N; i++) { n     for (int j = 1; j <= M; j++) { n       cin >> c; n       mat[i][j] = c ==  1 ; n     } n   } n   long long ans = 0; n   for (int i = 1; i <= N; i++) { n     for (int j = 1; j <= M; j++) { n       if (mat[i][j]) { n         bool stop = false; n         for (int len = 2; i + len - 1 <= N && j + len - 1 <= M; len++) { n           if (side_square(i, j, len)) { n             ++ans; n             stop = true; n           } n           if (diag_square(i, j, len)) { n             ++ans; n             stop = true; n           } n           if (stop) break; n         } n       } n     } n   } n   cout << ans <<   n ; n } n int main() { n   setIO(); n   long long T = 1; n   cin >> T; n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int sq = sqrt(n); n   if (n % sq == 0) { n     cout << sq <<     << n / sq; n   } else { n     int i = 0; n     while (++i) { n       if (n % (sq - i) == 0) { n         cout << sq - i <<     << n / (sq - i); n         break; n       } else if (n % (sq + i) == 0) { n         cout << sq + i <<     << n / (sq + i); n         break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int p = 1e9 + 7; n long long binpow(long long a, long long b, long long mod) { n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n     b >>= 1; n   } n   return res % mod; n } n const int dx[] = {1, -1, 0, 0}; n const int dy[] = {0, 0, -1, 1}; n const int inf = 1e9; n const int N = 1e5 + 9; n void solve() { n   int n; n   cin >> n; n   vector<int> a(n); n   for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n)); n        i += 1 - 2 * ((0) > (n))) n     cin >> a[i]; n   if (n == 1) { n     cout << a[0] <<   n ; n     return; n   } n   int dp[n][2]; n   dp[0][0] = a[0]; n   dp[0][1] = 1e9; n   dp[1][0] = a[0] + a[1]; n   dp[1][1] = a[0]; n   for (__typeof(n) i = (2) - ((2) > (n)); i != (n) - ((2) > (n)); n        i += 1 - 2 * ((2) > (n))) { n     dp[i][0] = min(dp[i - 1][1] + a[i], dp[i - 2][1] + a[i] + a[i - 1]); n     dp[i][1] = min(dp[i - 1][0], dp[i - 2][0]); n   } n   cout << min(dp[n - 1][1], dp[n - 1][0]) <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t = 1; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 0x3f3f3f3f3f3f3f3f; n struct port { n   int id; n   int x, y; n }; n void solve() { n   int n, m, sx, sy, dx, dy; n   cin >> n >> m >> sx >> sy >> dx >> dy; n   vector<port> p; n   for (int i = 0, x, y; i < m; ++i) { n     cin >> x >> y; n     p.push_back({i + 1, x, y}); n   } n   vector<array<int, 2>> adj[m + 3]; n   for (auto &i : p) { n     int wt = min(abs(i.x - sx), abs(i.y - sy)); n     adj[m + 1].push_back({i.id, wt}); n     adj[i.id].push_back({m + 1, wt}); n   } n   for (auto &i : p) { n     int wt = abs(i.x - dx) + abs(i.y - dy); n     adj[m + 2].push_back({i.id, wt}); n     adj[i.id].push_back({m + 2, wt}); n   } n   sort(begin(p), end(p), [&](port a, port b) { return a.x < b.x; }); n   for (int i = 1; i < m; ++i) { n     int j = i - 1, wt = abs(p[i].x - p[j].x); n     adj[p[i].id].push_back({p[j].id, wt}); n     adj[p[j].id].push_back({p[i].id, wt}); n   } n   sort(begin(p), end(p), [&](port a, port b) { return a.y < b.y; }); n   for (int i = 1; i < m; ++i) { n     int j = i - 1, wt = abs(p[i].y - p[j].y); n     adj[p[i].id].push_back({p[j].id, wt}); n     adj[p[j].id].push_back({p[i].id, wt}); n   } n   int wt = abs(dx - sx) + abs(dy - sy); n   adj[m + 1].push_back({m + 2, wt}); n   adj[m + 2].push_back({m + 1, wt}); n   auto dijkstra = [&](int src, int des) { n     vector<long long> dist(m + 3, inf); n     priority_queue<array<long long, 2>, vector<array<long long, 2>>, n                    greater<array<long long, 2>>> n         pq; n     dist[src] = 0; n     pq.push({0, src}); n     while (!pq.empty()) { n       long long curdist = pq.top()[0]; n       long long curnode = pq.top()[1]; n       pq.pop(); n       if (curdist > dist[curnode]) continue; n       for (auto &[v, w] : adj[curnode]) { n         if (dist[curnode] + w < dist[v]) { n           dist[v] = dist[curnode] + w; n           pq.push({dist[v], v}); n         } n       } n     } n     return dist[des]; n   }; n   cout << dijkstra(m + 1, m + 2); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   int tc; n   tc = 1; n   while (tc--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n bool debug = 0; n int n, m, k; n int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; n string direc =  RDLU ; n long long ln, lk, lm; n void etp(bool f = 0) { n   puts(f ?  Yes  :  No ); n   exit(0); n } n void addmod(int &x, int y, int mod = 1000000007) { n   assert(y >= 0); n   x += y; n   if (x >= mod) x -= mod; n   assert(x >= 0 && x < mod); n } n void et() { n   puts( -1 ); n   exit(0); n } n long long fastPow(long long x, long long y, int mod = 1000000007) { n   long long ans = 1; n   while (y > 0) { n     if (y & 1) ans = (x * ans) % mod; n     x = x * x % mod; n     y >>= 1; n   } n   return ans; n } n long long gcd1(long long x, long long y) { n   long long z = y; n   while (x % y != 0) { n     z = x % y; n     x = y; n     y = z; n   } n   return z; n } n int fac[105], inv[105]; n void initfac(int n) { n   fac[0] = inv[0] = 1; n   for (int(i) = 1; (i) <= (int)(n); (i)++) { n     fac[i] = (long long)fac[i - 1] * i % 1000000007; n     inv[i] = fastPow(fac[i], 1000000007 - 2); n   } n } n int C(int n, int m) { n   if (n < m) return 0; n   return (long long)fac[n] * inv[m] % 1000000007 * inv[n - m] % 1000000007; n } n int d[105]; n long long dp[55][55][55], ways[55][55][55]; n long long wk(int a, int b, int c) { n   if (a < 0 || b < 0) return 0; n   if (dp[a][b][c] != -1) return dp[a][b][c]; n   int ans = 0; n   if (c) { n     ans = (wk(a - 1, b, c - 1) * a + wk(a + 1, b - 1, c - 1) * b) % 1000000007; n   } else if (a) { n     ans = (wk(a - 2, b, 0) * (a - 1) + wk(a, b - 1, 0) * b) % 1000000007; n   } else if (b > 2) { n     for (long long z = 3, x = (b - 1) * (b - 2) / 2; z <= b; n          x = x * (b - z++) % 1000000007) { n       ans += x * wk(0, b - z, 0) % 1000000007; n       ans %= 1000000007; n     } n   } else n     ans = (b == 0); n   return dp[a][b][c] = ans; n   ; n } n int mdp[55][55]; n int cal(int l, int r) { n   if (mdp[l][r] != -1) return mdp[l][r]; n   if (r > n) return 0; n   int ans = 0, c2 = 0, c3 = 0; n   for (int i = l; i <= r; i++) n     if (d[i] == 2) n       c2++; n     else n       c3++; n   if (r == n) n     ans = wk(c2, c3, 0) % 1000000007; n   else { n     for (int x = 1; x <= n - r; x++) { n       addmod(ans, wk(c2, c3, x) * cal(r + 1, r + x) % 1000000007); n     } n   } n   return mdp[l][r] = ans; n } n void fmain(int tid) { n   scanf( %d , &n); n   for (int(i) = 1; (i) <= (int)(n); (i)++) scanf( %d , d + i); n   initfac(55); n   ways[0][0][0] = 1; n   memset(dp, -1, sizeof dp); n   memset(mdp, -1, sizeof mdp); n   int ans = d[1] == 2 ? cal(2, 3) : cal(2, 4); n   printf( %d n , ans); n } n int main() { n   int t = 1; n   for (int(i) = 1; (i) <= (int)(t); (i)++) { n     fmain(i); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool s[100005]; n int main() { n   int n; n   cin >> n; n   n *= 2; n   int ans = 0, rst = 0; n   ; n   while (n--) { n     int tmp; n     cin >> tmp; n     if (s[tmp]) n       ans -= 1; n     else n       s[tmp] = 1, ans++; n     rst = max(rst, ans); n   } n   cout << rst << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   vector<bool> vt(100005); n   vector<int> side(100005, 0b111); n   set<int> G[100005]; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     G[a].insert(b); n     G[b].insert(a); n   } n   bool first = true; n   int g2 = 0; n   for (int i = 1; i <= n; i++) { n     if (G[1].find(i) != G[1].end()) { n       if (first) { n         g2 = i; n         first = false; n       } n       side[i] &= 0b110; n     } else { n       side[i] &= 0b001; n     } n   } n   for (int i = 1; i <= n; i++) { n     if (G[g2].find(i) != G[g2].end()) { n       side[i] &= 0b101; n     } else { n       side[i] &= 0b010; n     } n   } n   for (int i = 1; i <= n; i++) { n     if (side[i] != 1 && side[i] != 2 && side[i] != 4) { n       puts( -1 ); n       return 0; n     } n   } n   int ct1 = 0, ct2 = 0, ct3 = 0; n   for (int i = 1; i <= n; i++) { n     switch (side[i]) { n       case 1: n         ct1++; n         break; n       case 2: n         ct2++; n         break; n       case 4: n         ct3++; n         break; n     } n     for (auto e : G[i]) n       if (side[i] == side[e]) { n         puts( -1 ); n         return 0; n       } n   } n   if (m != ct1 * ct2 + ct2 * ct3 + ct3 * ct1 || !ct1 || !ct2 || !ct3) { n     puts( -1 ); n     return 0; n   } n   for (int i = 1; i <= n; i++) { n     switch (side[i]) { n       case 1: n         printf( %d  , 1); n         break; n       case 2: n         printf( %d  , 2); n         break; n       case 4: n         printf( %d  , 3); n         break; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline int size(const T& c) { n   return c.size(); n } n using namespace std; n const int maxn = 100000 + 10; n struct student { n   int a; n   int b; n   int cs; n } a[maxn]; n int n, p, k, k2; n long long f[maxn]; n long long sumgrey, sumplease, resgrey, resplease; n struct cmp { n   bool operator()(int u, int v) { n     return make_pair(make_pair(a[u].a, a[u].b), u) < n            make_pair(make_pair(a[v].a, a[v].b), v); n   } n }; n set<int, cmp> s; n int vt; n bool mycmp1(student a, student b) { n   if (a.b == b.b) { n     return a.a < b.a; n   } n   return a.b < b.b; n } n bool mycmp2(student a, student b) { n   if (a.b == b.b) { n     return a.a > b.a; n   } n   return a.b < b.b; n } n void update(int sep) { n   if (resgrey < sumgrey || (resgrey == sumgrey && resplease < sumplease)) { n     resgrey = sumgrey; n     resplease = sumplease; n     vt = sep; n   } n } n void process() { n   for (int i = (1), _b = (n); i <= _b; i++) f[i] = f[i - 1] + a[i].b; n   s.clear(); n   for (int i = (n), _a = (n - k + 1); i >= _a; i--) { n     s.insert(i); n     sumgrey += a[i].a; n     sumplease = f[i - 1] - f[i - k2 - 1]; n   } n   update(n - k + 1); n   for (int i = (n - k), _a = (k2 + 1); i >= _a; i--) { n     int u = *s.begin(); n     sumplease = f[i - 1] - f[i - k2 - 1]; n     if (a[i].a > a[u].a || (a[i].a == a[u].a && a[i].b > a[u].b)) { n       s.erase(u); n       sumgrey -= a[u].a; n       s.insert(i); n       sumgrey += a[i].a; n     } n     update(i); n   } n } n void trace() { n   s.clear(); n   for (int i = (n), _a = (n - k + 1); i >= _a; i--) s.insert(i); n   for (int i = (n - k), _a = (vt); i >= _a; i--) { n     int u = *s.begin(); n     if (a[i].a > a[u].a || (a[i].a == a[u].a && a[i].b > a[u].b)) { n       s.erase(u); n       s.insert(i); n     } n   } n   set<int, cmp>::iterator it; n   for (__typeof((s).begin()) it = (s).begin(); it != (s).end(); it++) n     cout << a[*it].cs <<    ; n   for (int i = (vt - k2), _b = (vt - 1); i <= _b; i++) cout << a[i].cs <<    ; n } n int main() { n   cin >> n >> p >> k; n   for (int i = (1), _b = (n); i <= _b; i++) n     cin >> a[i].a >> a[i].b, a[i].cs = i; n   k2 = p - k; n   sort(a + 1, a + 1 + n, mycmp2); n   process(); n   trace(); n   fclose(stdin); n   fclose(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1e9 + 7; n long long gcd(long long a, long long b) { n   while (b > 0) { n     long long p = a % b; n     a = b; n     b = p; n   } n   return a; n } n long long bexp(long long a, int b) { n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = res * a; n     a = a * a; n     b >>= 1; n   } n   return res; n } n long long bexpM(long long a, int b) { n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = (res * a) % M; n     a = (a * a) % M; n     b >>= 1; n   } n   return res; n } n long long nCk(int n, int k) { n   long long res = 1; n   for (int i = k + 1; i < n + 1; i++) res = res * i; n   long long p = 1; n   for (int i = 2; i < n - k + 1; i++) p = p * i; n   res = res / p; n   return res; n } n long long nCkM(int n, int k) { n   long long res = 1; n   for (int i = k + 1; i < n + 1; i++) res = (res * i) % M; n   long long p = 1; n   for (int i = 2; i < n - k + 1; i++) p = (p * i) % M; n   res = (res * bexpM(p, M - 2)) % M; n   return res; n } n const int N = 2e5; n long long st[N][18]; n long long q(int l, int s, int n) { n   int i = 0; n   while ((1 << (i + 1)) < s) i++; n   return gcd(st[l][i], st[l - (1 << i) + s][i]); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int u; n   cin >> u; n   for (int g = 0; g < u; g++) { n     int n; n     cin >> n; n     long long a[n]; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n     } n     n--; n     for (int i = 0; i < n; i++) { n       st[i][0] = abs(a[i + 1] - a[i]); n     } n     for (int i = 1; i < log2(n); i++) { n       for (int j = 0; j < n - (1 << (i - 1)); j++) { n         st[j][i] = gcd(st[j][i - 1], st[j + (1 << (i - 1))][i - 1]); n       } n     } n     int l = 1; n     int r = n; n     while (l < r) { n       int m = (l + r + 1) / 2; n       long long b[n - m + 1]; n       for (int i = 0; i < n - m + 1; i++) { n         b[i] = q(i, m, n); n       } n       bool bo = 1; n       for (int i = 0; i < n - m + 1; i++) { n         if (b[i] != 1) bo = 0; n       } n       if (bo) n         r = m - 1; n       else n         l = m; n     } n     bool bo = 1; n     for (int i = 0; i < n; i++) { n       if (st[i][0] > 1) bo = 0; n     } n     if (bo) n       cout <<  1 n ; n     else n       cout << l + 1 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1.1e5; n queue<int> Q[2]; n vector<int> E[maxn]; n int n, m, cnt, in[maxn], f[maxn]; n signed main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; ++i) scanf( %d , f + i); n   for (int i = 1, x, y; i <= m; ++i) n     scanf( %d%d , &x, &y), ++in[x], E[y].push_back(x); n   for (int i = 0; i < n; ++i) n     if (!in[i]) Q[f[i]].push(i); n   while (!(Q[1].empty() && Q[0].empty())) { n     while (!Q[0].empty()) { n       int u = Q[0].front(); n       Q[0].pop(); n       for (auto v : E[u]) n         if (!--in[v]) Q[f[v]].push(v); n     } n     if (!Q[1].empty()) ++cnt; n     while (!Q[1].empty()) { n       int u = Q[1].front(); n       Q[1].pop(); n       for (auto v : E[u]) n         if (!--in[v]) Q[f[v]].push(v); n     } n   } n   printf( %d , cnt); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Seller { n   long long first, second, diff; n } a[200005]; n bool cmp(Seller p, Seller q) { return (p.diff > q.diff); } n int main() { n   long long n, m, i, j, ans = 0; n   cin >> n >> m; n   for (i = 0; i < n; i++) cin >> a[i].first; n   for (i = 0; i < n; i++) cin >> a[i].second; n   for (i = 0; i < n; i++) { n     a[i].diff = (a[i].second - a[i].first); n   } n   sort(a, a + n, cmp); n   for (i = 0; i < m; i++) ans += a[i].first; n   for (i = m; i < n; i++) ans += min(a[i].first, a[i].second); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b, c, d; n   scanf( %d %d %d %d %d , &n, &a, &b, &c, &d); n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     int sum = a + b + i; n     if (sum - a - c <= 0 || sum - a - c > n) continue; n     if (sum - b - d <= 0 || sum - b - d > n) continue; n     if (sum - c - d <= 0 || sum - c - d > n) continue; n     ans++; n   } n   ans *= n; n   printf( %I64d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, uz[1005], kay; n char ar[1005][1005], k[1005]; n int kontrol(int a, int say) { n   for (int i = 1; i <= uz[a] - say + 1; i++) { n     if (ar[a][i] == k[1]) { n       int m = 1, j, k1; n       for (j = i + 1, k1 = 2; j <= uz[a] && k1 <= say && m; j++, k1++) n         if (ar[a][j] != k[k1]) m = 0; n       if (m) { n         return 0; n       } n     } n   } n   return 1; n } n int maxs = 0; n void oku() { n   cin >> N; n   for (int i = 1; i <= N; i++) { n     scanf(  %s , ar[i] + 1); n     uz[i] = strlen(ar[i] + 1); n     maxs = max(maxs, uz[i]); n   } n   int o = 1, l; n   k[o] =  a ; n   k[o]--; n   while (o <= maxs) { n     l = 1; n     if (k[o] !=  z ) n       k[o]++; n     else { n       int p1 = o; n       while (p1) { n         if (k[p1] !=  z ) { n           k[p1]++; n           break; n         } else n           p1--; n       } n       if (!p1) { n         memset(k, 0, sizeof(k)); n         o++; n         for (int i = 1; i <= o; i++) k[i] =  a ; n       } else { n         p1++; n         while (p1 <= o) { n           k[p1] =  a ; n           p1++; n         } n       } n     } n     l = 1; n     for (int i = 1; i <= N && l; i++) l = kontrol(i, o); n     if (l) { n       printf( %s n , k + 1); n       return; n     } n   } n } n int main() { n   oku(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char ch[100001]; n int N, n, m; n long long a[1 << 20], b[1 << 20]; n int g[21][100001]; n void FWT(long long p[], int flag) { n   int temp = N; n   for (int i = 2; i <= temp; i *= 2) { n     int t = i / 2; n     for (int j = 0; j < temp; j += i) { n       for (int k = j; k < j + t; k++) { n         long long x = p[k]; n         long long y = p[k + t]; n         p[k] = x + y; n         p[k + t] = x - y; n         if (flag == -1) { n           p[k] /= 2; n           p[k + t] /= 2; n         } n       } n     } n   } n } n int main() { n   cin >> n >> m; n   N = 1 << n; n   for (int i = 1; i <= n; i++) { n     scanf( %s , ch + 1); n     for (int j = 1; j <= m; j++) { n       g[i][j] = ch[j] -  0 ; n     } n   } n   for (int i = 1; i <= m; i++) { n     int x = 0; n     for (int j = 1; j <= n; j++) { n       x = (x * 2) | g[j][i]; n     } n     a[x]++; n   } n   for (int i = 0; i < N; ++i) b[i] = b[i >> 1] + (i & 1); n   for (int i = 0; i < N; ++i) b[i] = min(b[i], n - b[i]); n   FWT(a, 1); n   FWT(b, 1); n   for (int i = 0; i < N; ++i) a[i] *= b[i]; n   FWT(a, -1); n   long long ans = n * m; n   for (int i = 0; i < N; ++i) ans = min(ans, a[i]); n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<pair<long long int, long long int> > v; n   long long int n, x, c = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     long long int a, b; n     cin >> a >> b; n     v.push_back(make_pair(a, b)); n   } n   sort(v.begin(), v.end()); n   long long int i = 0; n   x = v[0].second; n   while (i < n) { n     if (v[i].first <= x) i++; n     if (v[i].second < x) x = v[i].second; n     if (v[i].first > x || i >= n) { n       c++; n       x = v[i].second; n     } n   } n   cout << c; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 157; n int f[N][N]; n int main() { n   int n; n   scanf( %d , &n); n   f[0][0] = 1; n   f[1][0] = 0; n   f[1][1] = 1; n   for (int i = 2; i <= n; i++) { n     for (int j = 0; j <= i - 2; j++) f[i][j] = f[i - 2][j]; n     for (int j = 0; j <= i - 1; j++) { n       f[i][j + 1] = (f[i][j + 1] + f[i - 1][j]) & 1; n     } n   } n   printf( %d n , n); n   for (int i = 0; i <= n; i++) printf( %d%c , f[n][i], i == n ?   n  :    ); n   n--; n   printf( %d n , n); n   for (int i = 0; i <= n; i++) printf( %d%c , f[n][i], i == n ?   n  :    ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:64000000 ) n int n; n pair<int, int> a[1 << 10]; n int b[1 << 10]; n int ans[1 << 10][1 << 10]; n int len[1 << 10]; n void answer(string s) { n   puts(s.c_str()); n   if (s ==  NO ) exit(0); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < len[i]; ++j) printf( %d , ans[i][j]); n     puts(  ); n   } n   exit(0); n } n void init() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i].first); n     a[i].second = i; n   } n   sort(a, a + n); n } n void add(int x) { n   int dif = 1; n   while (x >= 0 && dif) { n     ++b[x]; n     dif = 0; n     if (b[x] == 2) { n       dif = 1; n       b[x] = 0; n     } n     --x; n   } n   if (dif) answer( NO ); n } n int main() { n   init(); n   memset(b, 0, sizeof b); n   for (int i = 0; i < n; ++i) { n     int x = a[i].second; n     len[x] = a[i].first; n     for (int j = 0; j < a[i].first; ++j) { n       ans[x][j] = b[j]; n     } n     if (i == n - 1) break; n     add(a[i].first - 1); n   } n   answer( YES ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const int MAX = 2e6 + 5; n int ftree[MAX] = {0}; n int sum[MAX] = {0}; n int query(int node) { n   int sum = 0; n   while (node > 0) { n     sum += ftree[node]; n     node -= (node & -node); n   } n   return sum; n } n void update(int node, int diff, int &mx) { n   while (node <= mx) { n     ftree[node] += diff; n     node += (node & -node); n   } n } n int bsearch(int &n, int s) { n   int l = 1; n   int r = n; n   while (l <= r) { n     int m = (l + r) >> 1; n     int p = query(m); n     if (p < s) n       l = m + 1; n     else n       r = m - 1; n   } n   return l; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   memset(ftree, 0, sizeof(ftree)); n   memset(sum, 0, sizeof(sum)); n   int n; n   cin >> n; n   for (int i = (1); i <= (n); ++i) { n     update(i, 1, n); n   } n   int p; n   for (int i = 0; i < (int)(n); ++i) { n     cin >> p; n     int less = query(p + 1) - 1; n     sum[i] += less; n     update(p + 1, -1, n); n   } n   for (int i = (1); i <= (n); ++i) { n     update(i, 1, n); n   } n   for (int i = 0; i < (int)(n); ++i) { n     cin >> p; n     int less = query(p + 1) - 1; n     sum[i] += less; n     update(p + 1, -1, n); n   } n   for (int i = 0; i < n; ++i) { n     int carry = sum[n - i - 1] / (i + 1); n     sum[n - i - 2] += carry; n     sum[n - i - 1] %= (i + 1); n     update(i + 1, 1, n); n   } n   for (int i = 0; i < (int)(n); ++i) { n     int f = bsearch(n, sum[i] + 1); n     cout << (f - 1) <<    ; n     update(f, -1, n); n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 1000000; n const int Inf = 0x3f3f3f3f; n int n, m, k; n pair<int, int> edge[Maxn + 5]; n int a[Maxn + 5]; n map<pair<int, int>, int> mp; n int deg[Maxn + 5], sz[Maxn + 5], st[Maxn + 5], col[Maxn + 5]; n vector<pair<int, int> > g[Maxn + 5]; n int ans[Maxn + 5]; n void match(int u, int fa, int from, int to) { n   for (int i = 0; i < (int)g[u].size(); i++) { n     if (g[u][i].first == from) { n       g[u][i].first = to; n       match(g[u][i].second, u, to, from); n     } n   } n   for (int i = 0; i < (int)g[u].size(); i++) { n     if (g[u][i].second == fa) { n       g[u][i].first = from; n     } n   } n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %*d ); n   } n   for (int i = 1; i <= m; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     if (u > v) { n       swap(u, v); n     } n     edge[i] = make_pair(u, v + n); n     deg[u]++; n     deg[v + n]++; n   } n   n = (n << 1); n   st[0] = 1; n   for (int i = 1; i <= n; i++) { n     sz[i] = (deg[i] - 1) / k + 1; n     st[i] = st[i - 1] + sz[i - 1]; n   } n   for (int i = 1; i <= m; i++) { n     int u = edge[i].first, v = edge[i].second; n     g[st[u]].push_back(make_pair(Inf, st[v])); n     g[st[v]].push_back(make_pair(Inf, st[u])); n     edge[i] = make_pair(st[u], st[v]); n     mp[edge[i]] = i; n     col[u]++; n     col[v]++; n     if (col[u] == k) { n       st[u]++; n       col[u] = 0; n     } n     if (col[v] == k) { n       st[v]++; n       col[v] = 0; n     } n   } n   for (int i = 1; i <= m; i++) { n     int u = edge[i].first, v = edge[i].second; n     int pos_u = 0, pos_v = 0; n     sort(g[u].begin(), g[u].end()); n     sort(g[v].begin(), g[v].end()); n     for (int j = 0; j < (int)g[u].size(); j++) { n       if (j + 1 != g[u][j].first) { n         pos_u = j + 1; n         break; n       } n     } n     for (int j = 0; j < (int)g[v].size(); j++) { n       if (j + 1 != g[v][j].first) { n         pos_v = j + 1; n         break; n       } n     } n     for (int j = 0; j < (int)g[u].size(); j++) { n       if (g[u][j].second == v) { n         g[u][j].first = pos_u; n       } n     } n     match(v, u, pos_u, pos_v); n   } n   for (int i = 1; i <= st[n]; i++) { n     for (int j = 0; j < (int)g[i].size(); j++) { n       if (i < g[i][j].second) { n         ans[mp[make_pair(i, g[i][j].second)]] = g[i][j].first; n       } n     } n   } n   for (int i = 1; i <= m; i++) { n     printf( %d n , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > vertices; n vector<pair<int, int> > edges; n int main() { n   int v; n   cin >> v; n   int a, b; n   queue<int> q; n   for (int i = 0; i < v; i++) { n     cin >> a >> b; n     vertices.push_back(make_pair(a, b)); n     if (a == 1) { n       q.push(i); n     } n   } n   int size = v; n   while (q.empty() == false) { n     int i = q.front(); n     q.pop(); n     if (vertices[i].first == 1) { n       int ver = vertices[i].second; n       edges.push_back(make_pair(i, ver)); n       vertices[ver].first--; n       vertices[ver].second ^= i; n       if (vertices[ver].first == 1) { n         q.push(ver); n       } n     } n   } n   cout << edges.size() << endl; n   for (int i = 0; i < edges.size(); i++) { n     cout << edges[i].first <<     << edges[i].second << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using std::max; n const int N = 3e5 + 5; n template <typename T> n void read(T &x) { n   bool neg = false; n   unsigned char c = getchar(); n   for (; (c ^ 48) > 9; c = getchar()) n     if (c ==  - ) neg = true; n   for (x = 0; (c ^ 48) < 10; c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48); n   if (neg) x = -x; n } n struct line { n   int next, to, w; n } e[2 * N]; n int n, q, hd[N], cnt, a[N], b[N], fa[N], dep[N], siz[N], hc[N], top[N]; n long long s[N], f[N], g[N], h[N], c[N], ff[N]; n void adline(int u, int v, int w) { n   e[cnt].to = v; n   e[cnt].w = w; n   e[cnt].next = hd[u]; n   hd[u] = cnt++; n } n void dfs1(int u) { n   hc[u] = -1; n   s[u] = s[fa[u]] + a[u] - b[u]; n   for (int i = hd[u]; ~i; i = e[i].next) { n     int v = e[i].to; n     if (v == fa[u]) continue; n     fa[v] = u; n     b[v] = e[i].w; n     dep[v] = dep[u] + 1; n     dfs1(v); n     siz[u] += siz[v]; n     if (!~hc[u] || siz[v] > siz[hc[u]]) hc[u] = v; n     f[u] += max(0ll, f[v] + a[v] - 2 * b[v]); n   } n   ++siz[u]; n } n void dfs2(int u) { n   long long sum = (ff[u] = f[u]) + g[u] + a[u]; n   h[u] = max(h[u], 0ll); n   if (~hc[u]) { n     int v = hc[u]; n     top[v] = top[u]; n     g[v] = max(0ll, sum - 2 * b[v] - max(0ll, h[v] = f[v] + a[v] - 2 * b[v])); n     dfs2(v); n   } n   for (int i = hd[u]; ~i; i = e[i].next) { n     int v = e[i].to; n     if (v == fa[u] || v == hc[u]) continue; n     g[v] = max(0ll, sum - 2 * b[v] - max(0ll, h[v] = f[v] + a[v] - 2 * b[v])); n     dfs2(top[v] = v); n   } n } n void dfs3(int u) { n   for (int i = hd[u]; ~i; i = e[i].next) { n     int v = e[i].to; n     if (v == fa[u]) continue; n     f[v] += f[u]; n     h[v] += h[u]; n     dfs3(v); n   } n } n int lca(int u, int v) { n   while (top[u] != top[v]) { n     if (dep[top[u]] > dep[top[v]]) n       u = fa[top[u]]; n     else n       v = fa[top[v]]; n   } n   return dep[u] < dep[v] ? u : v; n } n int main() { n   read(n); n   read(q); n   for (int i = 0, i_end = n; i < i_end; ++i) read(a[i]); n   for (int i = 0, i_end = n; i < i_end; ++i) hd[i] = -1; n   for (int i = 0, i_end = n - 1; i < i_end; ++i) { n     int u, v, w; n     read(u); n     read(v); n     read(w); n     adline(--u, --v, w); n     adline(v, u, w); n   } n   dfs1(0); n   dfs2(0); n   dfs3(0); n   while (q--) { n     int u, v, m; n     long long ans; n     read(u); n     read(v); n     --u, --v; n     m = lca(u, v); n     ans = s[u] + s[v] - 2 * s[m] + a[m]; n     ans += f[u] + f[v] - 2 * f[m] + g[m]; n     ans -= h[u] + h[v] - 2 * h[m] - ff[m]; n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s[6] = { 170 ,  020 ,  200 ,  110 ,  050 ,  080 }; n int main() { n   int n; n   cin >> n; n   if (n < 3) n     cout << -1; n   else if (n == 3) n     cout << 210; n   else { n     cout << 1; n     for (int i = 0; i < n - 4; i++) cout << 0; n     cout << s[n % 6]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s1[10], s2[10], score[10]; n map<string, int> mp; n string name[10]; n int a[10][10]; n bool better(int x, int y) { n   int n; n   int sx = 0, px = 0, mx = 0, sy = 0, py = 0, my = 0; n   for (n = 0; n < 4; n++) n     if (n != x) { n       if (a[x][n] > a[n][x]) n         sx += 3; n       else if (a[x][n] == a[n][x]) n         sx++; n       px += a[x][n]; n       mx += a[n][x]; n     } n   for (n = 0; n < 4; n++) n     if (n != y) { n       if (a[y][n] > a[n][y]) n         sy += 3; n       else if (a[y][n] == a[n][y]) n         sy++; n       py += a[y][n]; n       my += a[n][y]; n     } n   if (sx != sy) return (sx > sy); n   if (px - mx != py - my) return (px - mx > py - my); n   if (px != py) return (px > py); n   return (x < y); n } n bool check(int x, int y) { n   int n; n   int b = mp[ BERLAND ]; n   int c; n   int rank = 1; n   for (n = 0; n < 4; n++) n     if (n != b && a[b][n] == -1) c = n; n   a[b][c] = x; n   a[c][b] = y; n   for (n = 0; n < 4; n++) n     if (n != b && better(n, b)) rank++; n   a[b][c] = -1, a[c][b] = -1; n   return (rank <= 2); n } n int main() { n   int i, j; n   for (i = 0; i < 5; i++) { n     cin >> s1[i] >> s2[i] >> score[i]; n   } n   for (i = 0; i < 5; i++) mp[s1[i]] = mp[s2[i]] = 0; n   i = 0; n   for (__typeof((mp).begin()) itr = (mp).begin(); itr != (mp).end(); itr++) { n     (itr->second) = i; n     name[i] = (itr->first); n     i++; n   } n   memset(a, -1, sizeof(a)); n   for (i = 0; i < 5; i++) { n     int x = mp[s1[i]], y = mp[s2[i]]; n     for (j = 0; j < score[i].size(); j++) n       if (score[i][j] ==  : ) score[i][j] =    ; n     istringstream ss(score[i]); n     ss >> a[x][y] >> a[y][x]; n   } n   for (int diff = 1; diff < 100; diff++) { n     for (int a = 0; a < 100; a++) { n       int b = a + diff; n       if (check(b, a)) { n         cout << b <<  :  << a << endl; n         return 0; n       } n     } n   } n   cout <<  IMPOSSIBLE  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace ::std; n const long double PI = acos(-1); n const long long MOD = 1000000000 + 7; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n long long add(long long a, long long b, long long m = MOD) { n   if (a >= m) a %= m; n   if (b >= m) b %= m; n   if (a < 0) a += m; n   if (b < 0) b += m; n   long long res = a + b; n   if (res >= m or res <= -m) res %= m; n   if (res < 0) res += m; n   return res; n } n long long mul(long long a, long long b, long long m = MOD) { n   if (a >= m) a %= m; n   if (b >= m) b %= m; n   if (a < 0) a += m; n   if (b < 0) b += m; n   long long res = a * b; n   if (res >= m or res <= -m) res %= m; n   if (res < 0) res += m; n   return res; n } n long long pow_mod(long long a, long long b, long long m = MOD) { n   long long res = 1LL; n   a = a % m; n   while (b) { n     if (b & 1) res = mul(res, a, m); n     b >>= 1; n     a = mul(a, a, m); n   } n   return res; n } n long long fastexp(long long a, long long b) { n   long long res = 1LL; n   while (b) { n     if (b & 1) res = res * a; n     b >>= 1; n     a *= a; n   } n   return res; n } n int gcdExtendido(int a, int b, int *x, int *y) { n   if (a == 0) { n     *x = 0; n     *y = 1; n     return b; n   } n   int x1, y1; n   int gcd = gcdExtendido(b % a, a, &x1, &y1); n   *x = y1 - (b / a) * x1; n   *y = x1; n   return gcd; n } n int modInverso(int a, int m) { n   int x, y; n   int g = gcdExtendido(a, m, &x, &y); n   if (g != 1) n     return -1; n   else n     return (x % m + m) % m; n } n const int M = 300000 + 5; n int N, n, m; n vector<int> G[M]; n map<pair<int, int>, int> E; n void clearAll() { n   for (int i = 1; i <= n; i++) { n     G[i].clear(); n   } n } n int main() { n   int t; n   scanf( %d , &(t)); n   int a, b; n   while (t--) { n     scanf( %d %d , &(N), &(m)); n     n = N + N + N; n     bool forest = true; n     E.clear(); n     vector<bool> used(n + 1, false); n     vector<int> matching; n     for (int i = 1; i <= m; i++) { n       scanf( %d %d , &(a), &(b)); n       if (a > b) swap(a, b); n       G[a].emplace_back(b); n       G[b].emplace_back(a); n       E[make_pair(a, b)] = i; n     } n     for (int u = 1; u <= n; u++) { n       if (used[u]) continue; n       for (int i = 0; i < G[u].size(); i++) { n         int v = G[u][i]; n         if (!used[v]) { n           matching.emplace_back(E[make_pair(min(u, v), max(u, v))]); n           used[u] = used[v] = true; n           break; n         } n       } n     } n     if (matching.size() >= N) { n       puts( Matching ); n       for (int i = 0; i < N; i++) { n         printf( %d%c , matching[i], (i + 1 == N ?   n  :    )); n       } n     } else { n       puts( IndSet ); n       int cnt = N; n       for (int i = 1; i <= n; i++) { n         if (cnt == 0) break; n         if (!used[i]) { n           cnt -= 1; n           printf( %d%c , i, (cnt ?     :   n )); n         } n       } n     } n     if (t) clearAll(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, f = 0; n   cin >> n; n   for (int i = 1; i < 50; i++) { n     if ((i * (i + 1)) / 2 == n) { n       f = 1; n       break; n     } n     if (i > n) break; n   } n   if (f) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T sqr(const T &x) { n   return x * x; n } n inline long long sqr(int x) { return sqr<long long>(x); } n template <class T> n T binpow(const T &a, long long n) { n   return n == 0 ? 1 : sqr(binpow(a, n / 2)) * (n % 2 ? a : 1); n } n long long binpow(long long a, long long n, long long modulo) { n   return n == 0 ? 1 n                 : sqr(binpow(a, n / 2, modulo)) % modulo * (n % 2 ? a : 1) % n                       modulo; n } n long long gcd(long long a, long long b, long long &x, long long &y) { n   if (a == 0) { n     x = 0; n     y = 1; n     return b; n   } n   long long x1, y1; n   long long d = gcd(b % a, a, x1, y1); n   x = y1 - (b / a) * x1; n   y = x1; n   return d; n } n inline long long phi(long long n) { n   long long result = n; n   for (long long i = 2; i * i <= n; ++i) n     if (n % i == 0) { n       while (n % i == 0) n /= i; n       result -= result / i; n     } n   if (n > 1) result -= result / n; n   return result; n } n inline vector<long long> inverseAll(long long m) { n   vector<long long> r(m); n   r[1] = 1; n   for (int i = 2; i < m; ++i) r[i] = (m - (m / i) * r[m % i] % m) % m; n   return r; n } n inline long long gcd(long long a, long long b) { return gcd(a, b, a, b); } n inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n inline long long llrand() { n   const long long lsbToFill = (sizeof(long long) << 3) - 1; n   const long long bitsFilledInIteration = __builtin_popcountll(RAND_MAX); n   long long number = 0; n   for (long long lsbFilled = 0; lsbFilled <= lsbToFill; n        lsbFilled += bitsFilledInIteration) { n     number ^= (((long long)rand()) << lsbFilled); n   } n   return number & numeric_limits<long long>::max(); n } n inline long long llrand(long long begin, long long end) { n   return begin + llrand() % (end - begin); n } n struct Dinic { n   struct Edge { n     int u, v; n     long long cap, flow; n     Edge() {} n     Edge(int u, int v, long long cap) : u(u), v(v), cap(cap), flow(0) {} n   }; n   int N; n   vector<Edge> E; n   vector<vector<int>> g; n   vector<int> d, pt; n   Dinic(int N) : N(N), E(0), g(N), d(N), pt(N) {} n   void AddEdge(int u, int v, long long cap) { n     if (u != v) { n       E.emplace_back(Edge(u, v, cap)); n       g[u].emplace_back(E.size() - 1); n       E.emplace_back(Edge(v, u, 0)); n       g[v].emplace_back(E.size() - 1); n     } n   } n   bool BFS(int S, int T) { n     queue<int> q({S}); n     fill(d.begin(), d.end(), N + 1); n     d[S] = 0; n     while (!q.empty()) { n       int u = q.front(); n       q.pop(); n       if (u == T) break; n       for (int k : g[u]) { n         Edge &e = E[k]; n         if (e.flow < e.cap && d[e.v] > d[e.u] + 1) { n           d[e.v] = d[e.u] + 1; n           q.emplace(e.v); n         } n       } n     } n     return d[T] != N + 1; n   } n   long long DFS(int u, int T, long long flow = -1) { n     if (u == T || flow == 0) return flow; n     for (int &i = pt[u]; i < g[u].size(); ++i) { n       Edge &e = E[g[u][i]]; n       Edge &oe = E[g[u][i] ^ 1]; n       if (d[e.v] == d[e.u] + 1) { n         long long amt = e.cap - e.flow; n         if (flow != -1 && amt > flow) amt = flow; n         if (long long pushed = DFS(e.v, T, amt)) { n           e.flow += pushed; n           oe.flow -= pushed; n           return pushed; n         } n       } n     } n     return 0; n   } n   long long MaxFlow(int S, int T) { n     long long total = 0; n     while (BFS(S, T)) { n       fill(pt.begin(), pt.end(), 0); n       while (long long flow = DFS(S, T)) total += flow; n     } n     return total; n   } n }; n vector<long long> Dijkstra(const vector<list<pair<int, long long>>> &g, int s) { n   vector<long long> d(int((g).size()), numeric_limits<long long>::max() / 2LL); n   priority_queue<pair<long long, int>> q; n   d[s] = 0; n   q.emplace(-0, s); n   while (!q.empty()) { n     while (q.top().first > d[q.top().second]) { n       q.pop(); n     } n     int v = q.top().second; n     q.pop(); n     for (const auto &cw : g[v]) { n       if (d[v] + cw.second < d[cw.first]) { n         d[cw.first] = d[v] + cw.second; n         q.emplace(-d[cw.first], cw.first); n       } n     } n   } n   return d; n } n struct BinarySearchIterator n     : public std::iterator<std::forward_iterator_tag, bool> { n   long long value; n   typename iterator_traits<BinarySearchIterator>::difference_type operator-( n       const BinarySearchIterator &it) const { n     return value - it.value; n   } n   BinarySearchIterator &operator++() { n     ++value; n     return *this; n   } n   bool operator!=(const BinarySearchIterator &it) const { n     return value != it.value; n   } n   bool operator*() const { return true; } n }; n template <int ALPHA> n class AhoCorasick { n  public: n   static const int ILLEGAL_INDEX; n   static const int ROOT; n   struct Node { n     bool leaf; n     int parent; n     int parentCharacter; n     int link; n     int next[ALPHA]; n     int go[ALPHA]; n     int outputFunction; n     Node(int parent = ILLEGAL_INDEX, int parentCharacter = ALPHA) n         : leaf(false), n           parent(parent), n           parentCharacter(parentCharacter), n           link(ILLEGAL_INDEX), n           outputFunction(ILLEGAL_INDEX) { n       fill_n(next, ALPHA, ILLEGAL_INDEX); n       fill_n(go, ALPHA, ILLEGAL_INDEX); n     } n   }; n   vector<Node> tree = vector<Node>(1); n   AhoCorasick() {} n   AhoCorasick(int maxStatesNumber) { tree.reserve(maxStatesNumber); } n   template <class Iterator> n   void add(int length, const Iterator begin) { n     int vertex = ROOT; n     for (int i = 0; i < length; ++i) { n       if (ILLEGAL_INDEX == tree[vertex].next[begin[i]]) { n         tree[vertex].next[begin[i]] = int((tree).size()); n         tree.push_back(Node(vertex, begin[i])); n       } n       vertex = tree[vertex].next[begin[i]]; n     } n     tree[vertex].leaf = true; n   } n   int getLink(int vertex) { n     assert(0 <= vertex && vertex < tree.size()); n     if (ILLEGAL_INDEX == tree[vertex].link) { n       if (ROOT == vertex || ROOT == tree[vertex].parent) { n         tree[vertex].link = ROOT; n       } else { n         tree[vertex].link = n             go(getLink(tree[vertex].parent), tree[vertex].parentCharacter); n       } n     } n     return tree[vertex].link; n   } n   int go(int vertex, int character) { n     assert(0 <= character && character < ALPHA); n     assert(0 <= vertex && vertex < tree.size()); n     if (ILLEGAL_INDEX == tree[vertex].go[character]) { n       if (ILLEGAL_INDEX == tree[vertex].next[character]) { n         tree[vertex].go[character] = n             ROOT == vertex ? ROOT : go(getLink(vertex), character); n       } else { n         tree[vertex].go[character] = tree[vertex].next[character]; n       } n     } n     return tree[vertex].go[character]; n   } n   int getOutputFunction(int vertex) { n     assert(0 <= vertex && vertex < tree.size()); n     if (ILLEGAL_INDEX == tree[vertex].outputFunction) { n       if (tree[vertex].leaf || ROOT == vertex) { n         tree[vertex].outputFunction = vertex; n       } else { n         tree[vertex].outputFunction = getOutputFunction(getLink(vertex)); n       } n     } n     return tree[vertex].outputFunction; n   } n }; n template <int ALPHA> n const int AhoCorasick<ALPHA>::ILLEGAL_INDEX = -1; n template <int ALPHA> n const int AhoCorasick<ALPHA>::ROOT = 0; n int main(int argc, const char *argv[]) { n   ios::sync_with_stdio(false); n   cin.tie(0); n   srand((unsigned int)time(NULL)); n   int n; n   while (cin >> n) { n     vector<long long> s(n); n     for (int i = (0); i < (n); ++i) { n       cin >> s[i]; n     } n     vector<long long> ps(n + 1, 0); n     partial_sum((s).begin(), (s).end(), ps.begin() + 1); n     int q; n     cin >> q; n     for (int i = (0); i < (q); ++i) { n       int l, r; n       cin >> l >> r; n       --l; n       --r; n       cout << (ps[r + 1] - ps[l]) / 10 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long MAXN = 1e5 + 3; n const long long inf = 2e9; n long long max(long long a, long long b) { n   if (a > b) n     return a; n   else n     return b; n } n long long min(long long a, long long b) { n   if (a < b) n     return a; n   else n     return b; n } n long long dx[] = {1, -1, 0, 0}; n long long dy[] = {0, 0, -1, 1}; n bool cmp(pair<long long, long long> p1, pair<long long, long long> p2) { n   if (p1.first < p2.first) n     return true; n   else if (p1.first == p2.first) { n     if (p1.second < p2.second) return true; n     return false; n   } n   return false; n } n void solve() { n   long long n; n   cin >> n; n   long long li, ri, di; n   for (long long i = (long long)0; i < (long long)n; ++i) { n     cin >> li >> ri >> di; n     long long t1 = li / di; n     long long t2 = ri / di; n     if (li % di == 0) t1--; n     t2++; n     if (t1 >= 1) n       cout << di <<   n ; n     else n       cout << t2 * di <<   n ; n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long nT = 1; n   for (long long i = (long long)1; i <= (long long)nT; ++i) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n struct node { n   int c, h, i; n   friend bool operator<(const node &a, const node &b) { n     if (a.c != b.c) return a.c < b.c; n     return a.h > b.h; n   } n } t[N]; n struct node2 { n   int s, t, i, size; n   friend bool operator<(const node2 &a, const node2 &b) { n     return a.size > b.size; n   } n } prepare[N]; n int n, ss[N], rest[N], cnt, ans1, ans2; n long long sum[N], Max; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d %d , &t[i].c, &t[i].h); n     t[i].i = i; n   } n   sort(t + 1, t + n + 1); n   for (int i = 1; i <= n; i++) { n     int s = 0, j; n     for (j = i + 1; t[j].c == t[i].c && j <= n; j++) n       ; n     ss[++cnt] = j - i; n     prepare[cnt].s = i, prepare[cnt].t = j; n     prepare[cnt].i = cnt, prepare[cnt].size = j - i; n     i = j - 1; n   } n   sort(prepare + 1, prepare + cnt + 1); n   for (int i = 1; i <= cnt; i++) rest[i] = prepare[i].size; n   for (int p = cnt, s = 0; p > 1;) { n     int delta = 10000000; n     for (int i = p; i > 0; i--) delta = min(delta, rest[i]); n     for (int i = p; i > 0; i--) { n       for (int j = 1; j <= delta; j++) sum[i] += t[prepare[i].s + s + j - 1].h; n       rest[i] -= delta; n     } n     s += delta; n     for (int i = p; i > 0; i--) n       if (rest[i] > 0) sum[i] += t[prepare[i].s + s].h; n     int p1 = 0, p2 = 0; n     for (int i = p; i > 0; i--) n       if (sum[i] > sum[p1]) n         p2 = p1, p1 = i; n       else if (sum[i] == sum[p1]) n         p2 = i; n       else if (sum[i] > sum[p2]) n         p2 = i; n     if (sum[p1] + sum[p2] > Max) Max = sum[p1] + sum[p2], ans1 = p1, ans2 = p2; n     for (int i = p; i > 0; i--) n       if (rest[i] > 0) sum[i] -= t[prepare[i].s + s].h; n     while (rest[p] == 0 && p > 0) p--; n   } n   cout << Max << endl; n   if (ss[prepare[ans1].i] == ss[prepare[ans2].i]) { n     printf( %d n , ss[prepare[ans1].i] * 2); n     for (int i = 1; i < ss[prepare[ans1].i]; i++) n       printf( %d %d  , t[prepare[ans1].s + i - 1].i, n              t[prepare[ans2].s + i - 1].i); n     printf( %d %d n , t[prepare[ans1].t - 1].i, t[prepare[ans2].t - 1].i); n   } else { n     if (ss[prepare[ans1].i] > ss[prepare[ans2].i]) swap(ans1, ans2); n     printf( %d n%d , ss[prepare[ans1].i] * 2 + 1, t[prepare[ans2].s].i); n     for (int i = 1; i <= ss[prepare[ans1].i]; i++) n       printf(  %d %d , t[prepare[ans1].s + i - 1].i, t[prepare[ans2].s + i].i); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct congressman { n   int votes; n   int last_vote; n   int nr_of_order; n   bool operator<(const congressman &other) const { n     if (votes == other.votes) return last_vote > other.last_vote; n     return votes < other.votes; n   }; n } v[105]; n int n, k, m, a, pos[105]; n bool elected(int i) { n   int better = n - i; n   int t = m - a; n   for (int j = i - 1; j >= 1; j--) n     if (t >= v[i].votes - v[j].votes + 1) n       t -= v[i].votes - v[j].votes + 1, better++; n     else n       break; n   return (better < k && v[i].votes != 0); n } n bool notelectible(int i) { n   int last = i; n   for (int j = i + 1; j <= n; j++) n     if (v[i].votes + m - a > v[j].votes) n       last = j; n     else n       break; n   if (v[i].votes + m - a == 0) return 1; n   return (last < n - k + 1); n } n int main() { n   int i, j; n   cin >> n >> k >> m >> a; n   for (i = 1; i <= a; i++) { n     cin >> j; n     v[j].votes++; n     v[j].last_vote = max(v[j].last_vote, i); n   } n   for (i = 1; i <= n; i++) v[i].nr_of_order = i; n   sort(v + 1, v + n + 1); n   for (i = 1; i <= n; i++) pos[v[i].nr_of_order] = i; n   for (i = 1; i <= n; i++) { n     int r = 0; n     if (elected(pos[i])) n       r = 1; n     else if (notelectible(pos[i])) n       r = 3; n     else n       r = 2; n     cout << r <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a[100100]; n   vector<vector<int> > b1; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   if (n < 3) { n     cout << 0; n     return 0; n   } n   for (int i = a[1] - 1; i <= a[1] + 1; i++) { n     for (int j = a[2] - 1; j <= a[2] + 1; j++) { n       vector<int> a1(n + 1, 0); n       a1[1] = i; n       a1[2] = j; n       int k = j - i, b = 0; n       for (int j = 3; j <= n; j++) { n         a1[j] = a1[j - 1] + k; n         if (abs(a1[j] - a[j]) > 1) { n           b = 1; n           break; n         } n       } n       if (b == 1) continue; n       b1.push_back(a1); n     } n   } n   int k = 0, min1 = 1000000; n   for (int i = 0; i < b1.size(); i++) { n     k = 0; n     for (int j = 1; j <= n; j++) { n       if (a[j] != b1[i][j]) k++; n     } n     min1 = min(k, min1); n   } n   if (b1.size() == 0) n     cout << -1; n   else n     cout << min1; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n const int N = (int)(2e6 + 20); n const int L = 1 << 6; n const long long int M = (long long int)(1e9 + 7); n const double E = 1e-7; n int a[N]; n int main() { n   ios_base::sync_with_stdio(0); n   long long int n, m; n   cin >> n >> m; n   int tm = m; n   if (n > 3) { n     int f = 0, l = n - 1; n     m--; n     vector<int> v(n - 1); n     for (int i = 0; i < n - 1; ++i) { n       v[i] = m & 1; n       m /= 2; n     } n     for (int i = 0; i < n - 1; ++i) { n       if (v[n - i - 2]) n         a[l--] = i + 1; n       else n         a[f++] = i + 1; n     } n     a[f] = n; n     for (int i = 0; i < n; ++i) cout << a[i] <<    ; n     cout << endl; n     return 0; n   } n   m = tm; n   long long int maxV = 0; n   vector<int> v(n); n   for (int i = 0; i < n; ++i) v[i] = i + 1; n   do { n     long long int curV = 0; n     for (int i = 0; i < n; ++i) { n       int minV = v[i]; n       for (int j = i; j < n; ++j) { n         minV = min(minV, v[j]); n         curV += minV; n       } n     } n     maxV = max(curV, maxV); n   } while (next_permutation(v.begin(), v.end())); n   for (int i = 0; i < n; ++i) v[i] = i + 1; n   int cnt = 0; n   do { n     long long int curV = 0; n     for (int i = 0; i < n; ++i) { n       int minV = v[i]; n       for (int j = i; j < n; ++j) { n         minV = min(minV, v[j]); n         curV += minV; n       } n     } n     if (curV == maxV) { n       ++cnt; n     } n     if (cnt == m && true) { n       for (int i = 0; i < n; ++i) cout << v[i] <<    ; n       return 0; n     } n   } while (next_permutation(v.begin(), v.end())); n   cout << cnt; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2020; n vector<int> g[MAXN]; n int maxDep = -1; n void DFS(int v, int depth) { n   if (depth > maxDep) maxDep = depth; n   for (int i = 0; i < g[v].size(); i++) DFS(g[v][i], depth + 1); n } n int main() { n   int n, a; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a; n     if (a != -1) g[i].push_back(a); n   } n   for (int i = 1; i <= n; i++) DFS(i, 1); n   cout << maxDep << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool f[110][110][2][60]; n int main() { n   string str; n   cin >> str; n   int n = str.size(); n   int m; n   cin >> m; n   memset(f, 0, sizeof(f)); n   f[0][0][1][0] = 1; n   for (int i = 0; i < n; i++) { n     char ch = str[i]; n     for (int dist = 0; dist <= n; dist++) n       for (int dir = 0; dir <= 1; dir++) n         for (int opr = 0; opr <= m; opr++) n           if (f[i][dist][dir][opr]) n             if (ch ==  F ) { n               int delta = dir ? 1 : -1; n               if (dist == 0) n                 f[i + 1][1][1][opr] = 1; n               else n                 f[i + 1][dist + delta][dir][opr] = 1; n               f[i + 1][dist][1 - dir][opr + 1] = 1; n             } else { n               f[i + 1][dist][1 - dir][opr] = 1; n               int delta = dir ? 1 : -1; n               if (dist == 0) n                 f[i + 1][1][1][opr + 1] = 1; n               else n                 f[i + 1][dist + delta][dir][opr + 1] = 1; n             } n   } n   int ans = 0; n   for (int dist = 0; dist <= n; dist++) n     for (int dir = 0; dir <= 1; dir++) n       for (int opr = m; opr >= 0; opr -= 2) n         if (f[n][dist][dir][opr]) ans = ((ans) > (dist) ? (ans) : (dist)); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int p = n - a[0]; n   for (int i = 1; i < n; i++) { n     if (i & 1) { n       a[i] -= p; n       a[i] += (n * 1000000); n       a[i] = a[i] % n; n       if (a[i] != i) { n         cout <<  No ; n         return 0; n       } n     } else { n       a[i] += p; n       a[i] %= n; n       if (a[i] != i) { n         cout <<  No ; n         return 0; n       } n     } n   } n   cout <<  Yes ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<int> v[1001]; n   int n, k, r; n   while (scanf( %d , &n) != EOF) { n     for (int i = 1; i <= n; i++) { n       v[i].clear(); n       scanf( %d , &k); n       while (k--) { n         scanf( %d , &r); n         v[i].push_back(r); n       } n       sort(v[i].begin(), v[i].end()); n     } n     int ans = 0; n     for (int i = 1; i <= n; i++) { n       int pre = i - 1; n       pre = pre == 0 ? n : pre; n       int next = i + 1; n       next = next == n + 1 ? 1 : next; n       for (int j = 1; j < v[i].size(); j++) { n         int x = upper_bound(v[pre].begin(), v[pre].end(), v[i][j]) - n                 upper_bound(v[pre].begin(), v[pre].end(), v[i][j - 1]); n         int y = upper_bound(v[next].begin(), v[next].end(), v[i][j]) - n                 upper_bound(v[next].begin(), v[next].end(), v[i][j - 1]); n         if (x != y) ans++; n       } n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0; n const double eps = 0; n const int ms = 0; n const int md = 0; n const int MAX_N = 1e5 + 5; n const int MAX_L = 20; n const long long MOD = 1e9 + 7; n const long long INF = 1e9 + 7; n long long mpow(long long k) { n   long long ans = 1; n   while (k > 0) { n     ans *= 10; n     k--; n   } n   return ans; n } n long long countNines(long long n) { n   long long ans = 0; n   while (n > 0) { n     if (n % 10 == 9) { n       ans++; n     } else n       break; n     n /= 10; n   } n   return ans; n } n int main() { n   long long p, d; n   cin >> p >> d; n   int k = 1; n   long long ans = -1; n   while ((p - p % mpow(k) - 1) > 0) { n     if ((p - p % mpow(k) - 1) >= p - d) { n       ans = p - p % mpow(k) - 1; n     } n     k++; n   } n   if (countNines(ans) > countNines(p)) n     cout << ans << endl; n   else n     cout << p << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e3 + 5; n int main() { n   int num, r; n   cin >> num >> r; n   vector<pair<int, int>> arr(num + 1); n   vector<bool> visited(r + 2, 0); n   for (int i = 1; i <= num; ++i) { n     cin >> arr[i].first >> arr[i].second; n     for (int j = arr[i].first; j <= arr[i].second; ++j) { n       visited[j] = 1; n     } n   } n   long long ans = 0; n   for (int k = 1; k <= r; ++k) { n     if (visited[k] == 0) ans++; n   } n   cout << ans <<   n ; n   for (int k = 1; k <= r; ++k) { n     if (visited[k] == 0) cout << k <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const long long mod1 = 998244353; n const double pie = 3.1415926535; n long long power(long long x, long long y) { n   if (y == 0) n     return 1; n   else { n     if (y % 2 == 0) n       return power(x * x, y / 2); n     else n       return x * power(x * x, (y - 1) / 2); n   } n } n long long mod_power(long long x, long long y, long long m) { n   long long r = 1; n   x = x % m; n   if (x == 0) return 0; n   while (y > 0) { n     if (y & 1) r = (r * x) % m; n     y = y / 2; n     x = (x * x) % m; n   } n   return r; n } n long long gcd(long long x, long long y) { n   if (y == 0) return x; n   return gcd(y, x % y); n } n vector<long long> extended_Euclid(long long a, long long b) { n   vector<long long> v(3); n   if (b == 0) { n     v[0] = a; n     v[1] = 1; n     v[2] = 0; n     return v; n   } else { n     vector<long long> v1 = extended_Euclid(b, a % b); n     v[0] = v1[0]; n     v[1] = v1[2]; n     v[2] = v1[1] - (a / b) * v1[2]; n     return v; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int TESTS = 1; n   while (TESTS--) { n     int n; n     cin >> n; n     map<int, vector<int> > M; n     for (int i = 1; i <= n; i++) { n       int x, y; n       cin >> x >> y; n       M[i].push_back(x); n       M[i].push_back(y); n     } n     vector<int> ans; n     ans.push_back(1); n     int x = M[1][0]; n     int y = M[1][1]; n     int j; n     if (find(M[x].begin(), M[x].end(), y) != M[x].end()) { n       ans.push_back(x); n       ans.push_back(y); n       j = x; n     } else { n       ans.push_back(y); n       ans.push_back(x); n       j = y; n     } n     for (int i = 1; i <= n; i++) { n       if (ans.size() == n) n         break; n       else { n         if (M[j][0] == ans[ans.size() - 1]) n           ans.push_back(M[j][1]); n         else n           ans.push_back(M[j][0]); n         j = ans[ans.size() - 2]; n       } n     } n     for (int i = 0; i < ans.size(); i++) cout << ans[i] <<    ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100012, H = 20; n vector<int> e[N], g[N]; n stack<int> Z; n int n, m, tot, tim, low[N], rea[N], f[N], d[N]; n int fa[N][H], k; n bool v[N]; n void dfs(int fa, int now) { n   low[now] = rea[now] = tim++; n   v[now] = true; n   Z.push(now); n   int top = int(e[now].size()); n   for (int i = 0; i < top; ++i) { n     int next = e[now][i]; n     if (next != fa) { n       if (!v[next]) dfs(now, next); n       low[now] = min(low[now], low[next]); n     } n   } n   if (low[now] == rea[now]) { n     int last = -1; n     ++tot; n     while (last != now) { n       last = Z.top(); n       f[last] = tot; n       Z.pop(); n     } n   } n } n void work(int now, int p) { n   fa[now][0] = p; n   int top = int(g[now].size()); n   for (int j = 0; j < top; ++j) n     if (fa[now][0] != g[now][j]) n       d[g[now][j]] = d[now] + 1, work(g[now][j], now); n } n void gao() { n   for (int j = 1; j < H; ++j) n     for (int i = 1; i <= tot; ++i) fa[i][j] = fa[fa[i][j - 1]][j - 1]; n } n inline int find(int x, int y) { n   if (d[x] < d[y]) swap(x, y); n   for (int i = H - 1; i >= 0; --i) n     if (d[fa[x][i]] >= d[y]) x = fa[x][i]; n   if (x == y) return x; n   for (int i = H - 1; i >= 0; --i) n     if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i]; n   return fa[x][0]; n } n int main() { n   scanf( %d%d , &n, &m); n   int a, b; n   for (int i = 1; i <= m; ++i) { n     scanf( %d%d , &a, &b); n     e[a].push_back(b); n     e[b].push_back(a); n   } n   for (int i = 1; i <= n; ++i) n     if (!v[i]) dfs(-1, i); n   for (int a = 1; a <= n; ++a) n     for (int j = 0, top = int(e[a].size()); j < top; ++j) { n       b = e[a][j]; n       if (f[a] != f[b]) { n         g[f[a]].push_back(f[b]); n         g[f[b]].push_back(f[a]); n       } n     } n   d[0] = 0; n   d[f[1]] = 1; n   work(f[1], 0); n   gao(); n   scanf( %d n , &k); n   for (int i = 1; i <= k; ++i) { n     scanf( %d%d , &a, &b); n     a = f[a]; n     b = f[b]; n     int c = find(a, b); n     printf( %d n , d[a] + d[b] - 2 * d[c]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6; n vector<vector<int>> g(N); n bool isDeleted[N]; n vector<int> res; n void del(int s, int p) { n   res.push_back(s); n   isDeleted[s] = true; n   for (auto I : g[s]) { n     if (!isDeleted[I] && I != p) { n       del(I, s); n     } n   } n } n bool dfs(int s, int p) { n   int siz = g[s].size(); n   for (auto I : g[s]) { n     if (I != p) { n       bool isDeleted = dfs(I, s); n       if (isDeleted) { n         --siz; n       } n     } n   } n   if (siz % 2 == 0) { n     del(s, p); n     return true; n   } else n     return false; n } n int main() { n   int n, x; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &x); n     if (x) { n       g[i].push_back(x - 1); n       g[x - 1].push_back(i); n     } n   } n   bool ans = dfs(0, -1); n   if (ans) { n     printf( YES n ); n     for (auto I : res) { n       printf( %d n , I + 1); n     } n   } else n     printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long sum = 0; n   string s; n   cin >> s; n   s +=  a ; n   int now_number = 0, buf_number = 0, size_buf = 0, point = 0; n   for (int i = 0; i < s.size(); ++i) { n     if (s[i] >=  a  && s[i] <=  z ) { n       now_number = now_number * pow(10, size_buf) + buf_number; n       buf_number = 0; n       if (size_buf != 2 || !point) { n         now_number *= 100; n       } n       size_buf = 0; n       sum += now_number; n       now_number = 0; n       point = 0; n     } else { n       if (s[i] ==  . ) { n         now_number = now_number * pow(10, size_buf) + buf_number; n         buf_number = 0; n         size_buf = 0; n         point = 1; n       } else { n         buf_number = buf_number * 10 + (s[i] -  0 ); n         size_buf++; n       } n     } n   } n   int kop = sum % 100; n   sum /= 100; n   if (sum == 0) { n     cout <<  0.  << kop / 10 << kop % 10; n     return 0; n   } n   vector<string> ss; n   while (sum != 0) { n     int tri = sum % 1000; n     sum /= 1000; n     string ans =   ; n     if (sum != 0) { n       ans = (char)(tri % 10 + 48) + ans; n       tri /= 10; n       ans = (char)(tri % 10 + 48) + ans; n       tri /= 10; n       ans = (char)(tri % 10 + 48) + ans; n       tri /= 10; n     } else { n       ans = to_string(tri); n     } n     ss.push_back(ans); n   } n   reverse(ss.begin(), ss.end()); n   for (int i = 0; i < ss.size(); ++i) { n     if (i == ss.size() - 1) { n       cout << ss[i]; n     } else { n       cout << ss[i] <<  . ; n     } n   } n   if (kop != 0) { n     cout <<  .  << kop / 10 << kop % 10; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1024; n int n; n int a[maxn], b[maxn], c[maxn]; n vector<int> g[maxn]; n vector<pair<pair<int, int>, int> > ans; n void add(int v, int u, int x) { ans.push_back({{v, u}, x}); } n int gfs(int v, int par, int d) { n   if (g[v].size() == 1) return v; n   for (int i : g[v]) { n     int u = a[i] + b[i] - v; n     if (u != par and u != d) { n       int res = gfs(u, v, d); n       if (res != -1) return res; n     } n   } n   return -1; n } n pair<int, int> gpr(int v, int d) { n   pair<int, int> ret = {-1, -1}; n   for (int i : g[v]) { n     int u = a[i] + b[i] - v; n     if (u != d) { n       int l = gfs(u, v, d); n       if (l != -1) { n         if (ret.first == -1) n           ret.first = l; n         else n           ret.second = l; n       } n     } n   } n   return ret; n } n void see(int v, int u, int x) { n   pair<int, int> vp = gpr(v, u); n   pair<int, int> up = gpr(u, v); n   add(vp.first, up.first, x / 2); n   add(vp.second, up.second, x / 2); n   add(vp.first, vp.second, -x / 2); n   add(up.first, up.second, -x / 2); n } n void zee(int v, int d, int x) { n   pair<int, int> vp = gpr(d, v); n   add(vp.first, v, x / 2); n   add(vp.second, v, x / 2); n   add(vp.first, vp.second, -x / 2); n } n void dfs(int v, int par = -1, int lst = -1, int x = 0) { n   if (g[v].size() > 2) { n     if (lst != -1) see(v, lst, x); n     lst = v; n   } else if (g[v].size() == 1) { n     zee(v, lst, x); n   } n   for (int i : g[v]) { n     int u = a[i] + b[i] - v; n     if (u != par) { n       dfs(u, v, lst, c[i]); n     } n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     cin >> a[i] >> b[i] >> c[i]; n     a[i]--, b[i]--; n     g[a[i]].push_back(i); n     g[b[i]].push_back(i); n   } n   for (int i = 0; i < n; i++) n     if (g[i].size() == 2) { n       if (c[g[i][0]] != c[g[i][1]]) { n         cout <<  NO  << endl; n         return 0; n       } n     } n   cout <<  YES  << endl; n   vector<int> lv; n   int r = -1; n   for (int i = 0; i < n; i++) n     if (g[i].size() > 2) n       r = i; n     else if (g[i].size() == 1) n       lv.push_back(i); n   if (r == -1) { n     cout << 1 << endl; n     cout << lv[0] + 1 <<     << lv[1] + 1 <<     << c[0] << endl; n     return 0; n   } n   dfs(r); n   cout << ans.size() << endl; n   for (auto p : ans) n     cout << p.first.first + 1 <<     << p.first.second + 1 <<     << p.second n          <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   puts( INTERCAL ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   int t = 0, ans = 100000, a; n   for (int i = (0); i < (n); ++i) n     if (s[i] ==  T ) t++; n   for (int i = (0); i < (n); ++i) { n     a = 0; n     for (int j = (i); j < (i + t); ++j) { n       if (s[j % n] ==  H ) a++; n     } n     ans = min(ans, a); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n]; n   cin >> a[0]; n   int count = a[0]; n   int max = a[0]; n   for (int i = 1; i < n; i++) { n     cin >> a[i]; n   } n   for (int i = 1; i < n; i++) { n     count = count + a[i] - a[i - 1]; n     if (max < count) max = count; n   } n   cout << max; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5, M = 5; n int cc[N][M], sz[N], k; n long long ret[N], sum = 0; n vector<int> adj[N]; n long long dfs(int x, int p) { n   int t, v, j, u, i; n   sz[x] = 1; n   long long R = 0, temp; n   for (i = 0; i < adj[x].size(); ++i) { n     u = adj[x][i]; n     if (u != p) { n       temp = dfs(u, x); n       sum += sz[u] * R; n       R += temp; n       sum += temp * sz[x]; n       for (j = 0; j < k; ++j) n         for (t = 0; t < k; ++t) { n           if (j + t + 2 <= k) sum -= (long long)cc[x][j] * cc[u][t]; n         } n       for (j = 0; j < k; ++j) cc[x][j] += cc[u][j]; n       sz[x] += sz[u]; n     } n   } n   u = ++cc[x][k - 1]; n   for (i = k - 2; i >= 0; --i) cc[x][i + 1] = cc[x][i]; n   cc[x][0] = u; n   ret[x] = R + u; n   return ret[x]; n } n int main() { n   int n, i, j, u, v, a, b; n   cin >> n >> k; n   for (i = 0; i < n - 1; ++i) { n     scanf( %d%d , &a, &b); n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   dfs(1, 0); n   cout << sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int sum = 0; n   for (int i = 0; i < n; i++) { n     int c; n     cin >> c; n     sum = sum + c; n   } n   n = n + 1; n   int count = 0; n   for (int j = 1; j <= 5; j++) { n     int flag = 0; n     int s = sum + j; n     if (s % n == 1) flag = 1; n     if (flag == 0) count++; n   } n   cout << count; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> v; n long long n, m, w, ai; n long long cnt[300005]; n bool go(long long mx) { n   memset(cnt, 0, sizeof cnt); n   long long use = m; n   long long add = 0; n   for (int i = 0; i < n; i++) { n     add += cnt[i]; n     long long curr = v[i] + add; n     if (curr >= mx) continue; n     if (mx - curr > use) return 0; n     add += (mx - curr); n     cnt[i + w] -= (mx - curr); n     use -= (mx - curr); n   } n   return 1; n } n int main() { n   cin >> n >> m >> w; n   for (int i = 0; i < n; i++) { n     cin >> ai; n     v.push_back(ai); n   } n   long long l = 1, r = 10000000000ll, bst = -1; n   while (l <= r) { n     long long mid = (l + r) / 2ll; n     if (go(mid)) { n       bst = mid; n       l = mid + 1; n     } else n       r = mid - 1; n   } n   cout << bst; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 200; n int num[MAX], vis[MAX]; n int main() { n   int n, m, a; n   while (scanf( %d %d , &n, &m) == 2) { n     memset(vis, 0, sizeof(vis)); n     for (int i = 0; i < m; i++) { n       scanf( %d , &a); n       for (int i = a; i <= n; i++) n         if (!vis[i]) { n           num[i] = a; n           vis[i] = 1; n         } n     } n     for (int i = 1; i <= n; i++) { n       if (i == 1) n         printf( %d , num[i]); n       else n         printf(  %d , num[i]); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5, inf = 9e8; n int n, a[N]; n array<int, 3> vals[N]; n void compress(int arr[]) { n   vector<int> all; n   for (int i = 1; i <= n; i++) all.push_back(arr[i]); n   sort(all.begin(), all.end()); n   for (int i = 1; i <= n; i++) n     arr[i] = lower_bound(all.begin(), all.end(), arr[i]) - all.begin(), n     ++arr[i]; n } n class segTree { n  private: n   int n; n   vector<int> st; n  n  public: n   void init(int _n) { n     n = _n; n     st = vector<int>(4 * n + 5, 0); n   } n   void pointUpdate(int node, int tl, int tr, int pos, int val) { n     if (tl == tr) { n       st[node] = max(st[node], val); n     } else { n       int mid = (tl + tr) / 2; n       if (pos <= mid) n         pointUpdate(2 * node, tl, mid, pos, val); n       else n         pointUpdate(2 * node + 1, mid + 1, tr, pos, val); n       st[node] = max(st[2 * node], st[2 * node + 1]); n     } n   } n   int rangeQuery(int node, int tl, int tr, int l, int r) { n     if (l > r) return -inf; n     if ((l == tl) && (r == tr)) return st[node]; n     int mid = (tl + tr) / 2; n     int left_ans = rangeQuery(2 * node, tl, mid, l, min(r, mid)); n     int right_ans = rangeQuery(2 * node + 1, mid + 1, tr, max(l, mid + 1), r); n     return max(left_ans, right_ans); n   } n   void update(int pos, int val) { pointUpdate(1, 1, n, pos, val); } n   int query(int l, int r) { n     r = min(r, n); n     return rangeQuery(1, 1, n, l, r); n   } n } rangeMax; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   compress(a); n   for (int i = 1; i <= n; i++) vals[i][0] = a[i]; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   compress(a); n   for (int i = 1; i <= n; i++) vals[i][1] = a[i]; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   compress(a); n   for (int i = 1; i <= n; i++) vals[i][2] = a[i]; n   rangeMax.init(N); n   sort(vals + 1, vals + n + 1); n   reverse(vals + 1, vals + n + 1); n   int ans = 0; n   int j = 1; n   for (int i = 1; i <= n; i = j) { n     while (j <= n && vals[i][0] == vals[j][0]) ++j; n     for (int k = i; k < j; k++) { n       int max_c_for_all_valid_b = rangeMax.query(vals[k][1] + 1, inf); n       if (max_c_for_all_valid_b > vals[k][2]) ++ans; n     } n     for (int k = i; k < j; k++) { n       rangeMax.update(vals[k][1], vals[k][2]); n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int mod = 1000000007; n int gcd(int a, int b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n int prime(long long int n) { n   long long int i; n   for (i = 2; i <= sqrt(n); i++) { n     if (n % i == 0) return 0; n   } n   return 1; n } n long long int ans[1000000], c = 0; n void get_num(long long int n) { n   long long int i; n   for (i = 2; i <= sqrt(n); i++) { n     if (n % i == 0) { n       if (prime(i)) ans[c++] = i; n       if (prime(n / i)) { n         if (n / i != i) ans[c++] = i; n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int n; n   cin >> n; n   get_num(n); n   if (c == 0) n     cout << n; n   else if (c == 1) n     cout << ans[0]; n   else n     cout << 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool viz[105][105]; n int dx[4] = {0, 1, 0, -1}; n int dy[4] = {1, 0, -1, 0}; n int main() { n   cin.tie(0)->sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   int T = 1; n   while (T--) { n     int n, m, x, y; n     cin >> n >> m >> x >> y; n     vector<pair<int, int> > sol; n     viz[x][y] = 1; n     cout << x <<     << y <<   n ; n     int cnt = 1; n     for (int j = 1; j <= m; j++) { n       if (j != y) { n         cout << x <<     << j <<   n ; n       } n     } n     for (int i = x + 1; i <= n; i++) { n       if (cnt % 2 == 1) { n         for (int j = m; j >= 1; j--) { n           cout << i <<     << j <<   n ; n         } n       } else { n         for (int j = 1; j <= m; j++) { n           cout << i <<     << j <<   n ; n         } n       } n       cnt++; n     } n     for (int i = x - 1; i >= 1; i--) { n       if (cnt % 2 == 1) { n         for (int j = m; j >= 1; j--) { n           cout << i <<     << j <<   n ; n         } n       } else { n         for (int j = 1; j <= m; j++) { n           cout << i <<     << j <<   n ; n         } n       } n       cnt++; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ; n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int t; n   cin >> t; n   while (t--) { n     long long a, b, c; n     cin >> a >> b >> c; n     long long cnt = 0; n     while (1) { n       if (a > c || b > c) break; n       if (a > b) { n         b += a; n       } else n         a += b; n       cnt++; n     } n     cout << cnt << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n char s[200]; n vector<pair<int, int> > v; n int main() { n   scanf( %s , s); n   int len = strlen(s); n   pair<int, int> cur(0, 0); n   v.push_back(cur); n   for (int i = 0; i < len; i++) { n     if (s[i] ==  L ) { n       cur.first--; n     } else if (s[i] ==  R ) { n       cur.first++; n     } else if (s[i] ==  U ) { n       cur.second++; n     } else if (s[i] ==  D ) { n       cur.second--; n     } n     v.push_back(cur); n   } n   bool res = false; n   len = v.size(); n   for (int i = 0; i < len; i++) { n     for (int j = i + 2; j < len; j++) { n       if (abs(v[i].first - v[j].first) + abs(v[i].second - v[j].second) <= 1) { n         res = true; n         goto zzz; n       } n     } n   } n zzz: n   if (res) { n     printf( BUG ); n   } else { n     printf( OK ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, cnt, a[2010], b[2010], bel[2010], ans[2010 * 2010 / 2][2]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &b[i]); n     bel[b[i]] = i; n   } n   for (int i = 1; i <= n; i++) { n     a[i] = bel[a[i]]; n     cnt += abs(a[i] - i); n   } n   cnt /= 2; n   printf( %d n , cnt); n   cnt = 0; n   for (int i = 1; i <= n; i++) bel[a[i]] = i; n   for (int i = n, now = bel[n]; i; i--, now = bel[i]) n     for (int j = bel[i] + 1; j <= n; j++) n       if (a[j] <= now) { n         ans[++cnt][0] = j; n         ans[cnt][1] = now; n         swap(a[now], a[j]); n         now = j; n       } n   printf( %d n , cnt); n   for (int i = 1; i <= cnt; i++) printf( %d %d n , ans[i][0], ans[i][1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, page; n   cin >> n; n   vector<int> pages(7); n   for (int i = 0; i < 7; i++) { n     cin >> page; n     pages[i] = page; n   } n   int ans = 0; n   while (true) { n     n -= pages[ans]; n     if (n <= 0) { n       break; n     } n     ans = (ans + 1) % 7; n   } n   cout << ans + 1; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q; n int C[500001]; n vector<pair<int, int> > P[500001]; n int ans[500001]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> C[i]; n   cin >> q; n   for (int i = 0; i < q; ++i) { n     int l, r; n     cin >> l >> r; n     P[r].emplace_back(l, i); n   } n   pair<int, int> basis[20] = {}; n   for (int R = 1; R <= n; ++R) { n     pair<int, int> v(C[R], R); n     for (int i = 20; i--;) { n       if (v.first >> i) { n         if (v.second < basis[i].second) n           v.first ^= basis[i].first; n         else if (basis[i].first == 0) { n           basis[i] = v; n           break; n         } else { n           swap(v, basis[i]); n           v.first ^= basis[i].first; n         } n       } n     } n     for (pair<int, int> _ : P[R]) { n       int L, idx; n       tie(L, idx) = _; n       int v = 0; n       for (int i = 20; i--;) { n         if (basis[i].second < L) continue; n         if (((v >> i) & 1) == 0) v ^= basis[i].first; n       } n       ans[idx] = v; n     } n   } n   for (int i = 0; i < q; ++i) { n     printf( %d n , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int OO = 1e9; n const double EPS = 1e-9; n template <class T> n void _db(const char* dbStr, T e) { n   cout << dbStr <<   =   << e << endl; n } n template <class T, class... L> n void _db(const char* dbStr, T e, L... r) { n   while (*dbStr !=  , ) cout << *dbStr++; n   cout <<   =   << e <<  , ; n   _db(dbStr + 1, r...); n } n template <class S, class T> n ostream& operator<<(ostream& o, const map<S, T>& v) { n   o <<  [ ; n   int i = 0; n   for (const pair<S, T>& pr : v) n     o << (!i++ ?    :  ,  ) <<  {  << pr.first <<   :   << pr.second <<  } ; n   return o <<  ] ; n } n template <template <class, class...> class S, class T, class... L> n ostream& operator<<(ostream& o, const S<T, L...>& v) { n   o <<  [ ; n   int i = 0; n   for (const auto& e : v) o << (!i++ ?    :  ,  ) << e; n   return o <<  ] ; n } n template <class S, class T> n ostream& operator<<(ostream& o, const pair<S, T>& pr) { n   return o <<  (  << pr.first <<  ,   << pr.second <<  ) ; n } n ostream& operator<<(ostream& o, const string& s) { n   for (const char& c : s) o << c; n   return o; n } n template <class T> n using V = vector<T>; n template <class T> n using VV = V<V<T>>; n template <class T> n using VVV = VV<V<T>>; n using ll = long long; n using pii = pair<int, int>; n using vi = V<int>; n using vii = V<pii>; n using vvi = VV<int>; n using mii = map<int, int>; n using umii = unordered_map<int, int>; n using si = set<int>; n using usi = unordered_set<int>; n int n; n vi c; n vvi adj; n pii dfs(int u, int par) { n   int mxH[] = {0, 0, 0}, diam = 0; n   for (auto& v : adj[u]) n     if (v != par) { n       pii pr = dfs(v, u); n       diam = max(diam, pr.first); n       mxH[0] = pr.second + (c[u] != c[v]); n       sort(mxH, mxH + 3); n     } n   diam = max(diam, mxH[1] + mxH[2]); n   return make_pair(diam, mxH[2]); n } n int main() { n   ios::sync_with_stdio(false); n   cout.precision(10); n   cin.tie(0); n   cin >> n; n   c.assign(n + 1, 0); n   adj.assign(n + 1, vi()); n   for (int i = 1; i <= n; ++i) { n     cin >> c[i]; n   } n   for (int i = 1; i <= n - 1; ++i) { n     int u, v; n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   cout << (dfs(1, -1).first + 1) / 2 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long xorsum[(1L << 16)]; n long long deg[(1L << 16)]; n int main(void) { n   long long n; n   cin >> n; n   queue<long long> q; n   long long td = 0; n   for (long long i = 0; i < n; i++) { n     cin >> deg[i] >> xorsum[i]; n     if (deg[i] == 1) { n       q.push(i); n     } n     td += deg[i]; n   } n   vector<pair<long long, long long> > ans; n   while (!q.empty()) { n     long long u = q.front(); n     q.pop(); n     if (deg[u] == 0) continue; n     long long v = xorsum[u]; n     xorsum[v] ^= u; n     ans.push_back(make_pair(u, v)); n     deg[v]--; n     if (deg[v] == 1) q.push(v); n   } n   cout << (td / 2) << endl; n   for (long long i = 0; i < ans.size(); i++) { n     cout << ans[i].first <<     << ans[i].second << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = (int)1e6 + 5; n const int INF = (int)1e9 + 7; n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int a, b, c, d; n   cin >> a >> b >> c >> d; n   if (a + b == c + d or a + c == b + d or a + d == c + b or a + b + c == d or n       a + c + d == b or a + b + d == c or a == b + c + d) { n     cout <<  YES ; n     return 0; n   } n   cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int n = s.length(); n   bool ok = false; n   for (int i = 0; i < n - 1; i++) { n     if (s[i] ==  A  && s[i + 1] ==  B ) { n       for (int j = i + 2; j < n - 1; j++) { n         if (s[j] ==  B  && s[j + 1] ==  A ) { n           ok = true; n           break; n         } n       } n     } n     if (ok) break; n   } n   if (ok) { n     cout <<  YES n ; n     return 0; n   } n   for (int i = 0; i < n - 1; i++) { n     if (s[i] ==  B  && s[i + 1] ==  A ) { n       for (int j = i + 2; j < n - 1; j++) { n         if (s[j] ==  A  && s[j + 1] ==  B ) { n           ok = true; n           break; n         } n       } n     } n     if (ok) break; n   } n   if (ok) n     cout <<  YES n ; n   else n     cout <<  NO n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   long long r; n   while (b != 0) { n     r = a % b; n     a = b; n     b = r; n   } n   return a; n } n long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n long long fpow(long long n, long long k, int p = (int)1e9 + 7) { n   long long r = 1; n   for (; k; k >>= 1) { n     if (k & 1) r = r * n % p; n     n = n * n % p; n   } n   return r; n } n const int MOD = (int)1e9 + 7; n const int INF = (int)1e9; n const long long LINF = (long long)1e18; n const long double PI = 2 * acos((long double)0); n const int maxn = 500010; n int n, w, h; n char s[maxn]; n int xmin[maxn]; n int xmax[maxn]; n int ymin[maxn]; n int ymax[maxn]; n long long alox[maxn]; n long long aloy[maxn]; n long long tot[maxn]; n void solve() { n   scanf( %d%d%d%s , &n, &h, &w, s); n   int dx = 0, dy = 0; n   for (int i = (0); i < (n); i++) { n     if (s[i] ==  L ) n       dx--; n     else if (s[i] ==  R ) n       dx++; n     else if (s[i] ==  U ) n       dy--; n     else n       dy++; n     if (!i) { n       xmin[i] = xmax[i] = dx; n       ymin[i] = ymax[i] = dy; n     } else { n       xmin[i] = min(xmin[i - 1], dx); n       xmax[i] = max(xmax[i - 1], dx); n       ymin[i] = min(ymin[i - 1], dy); n       ymax[i] = max(ymax[i - 1], dy); n     } n   } n   for (int i = (0); i < (w); i++) { n     int c = 0; n     if (i + xmin[n - 1] >= 0 && i + xmax[n - 1] < w) { n       int l = 0, r = INF; n       while (l < r) { n         int m = (l + r) >> 1; n         if (dx >= 0) { n           if (i + xmax[n - 1] + 1LL * m * dx < w) n             l = m + 1; n           else n             r = m; n         } else { n           if (i + xmin[n - 1] + 1LL * m * dx >= 0) n             l = m + 1; n           else n             r = m; n         } n       } n       c = l; n     } n     if (c == INF) { n       alox[i] = -1; n       continue; n     } n     int l = 0, r = n - 1; n     while (l < r) { n       int m = (l + r) >> 1; n       if (i + xmin[m] + 1LL * c * dx >= 0 && i + xmax[m] + 1LL * c * dx < w) n         l = m + 1; n       else n         r = m; n     } n     alox[i] = 1LL * c * n + l + 1; n   } n   for (int i = (0); i < (h); i++) { n     int c = 0; n     if (i + ymin[n - 1] >= 0 && i + ymax[n - 1] < h) { n       int l = 0, r = INF; n       while (l < r) { n         int m = (l + r) >> 1; n         if (dy >= 0) { n           if (i + ymax[n - 1] + 1LL * m * dy < h) n             l = m + 1; n           else n             r = m; n         } else { n           if (i + ymin[n - 1] + 1LL * m * dy >= 0) n             l = m + 1; n           else n             r = m; n         } n       } n       c = l; n     } n     if (c == INF) { n       aloy[i] = -1; n       continue; n     } n     int l = 0, r = n - 1; n     while (l < r) { n       int m = (l + r) >> 1; n       if (i + ymin[m] + 1LL * c * dy >= 0 && i + ymax[m] + 1LL * c * dy < h) n         l = m + 1; n       else n         r = m; n     } n     aloy[i] = 1LL * c * n + l + 1; n   } n   int flagx = 0, flagy = 0; n   for (int i = (0); i < (w); i++) { n     if (!~alox[i]) { n       flagx = 1; n       alox[i] = LINF; n     } n   } n   for (int i = (0); i < (h); i++) { n     if (!~aloy[i]) { n       flagy = 1; n       aloy[i] = LINF; n     } n   } n   if (flagx && flagy) { n     printf( -1 ); n     return; n   } n   int ans = 0; n   sort(aloy, aloy + h); n   for (int i = (0); i < (h); i++) { n     if (!i) n       tot[i] = aloy[i] % MOD; n     else n       tot[i] = (tot[i - 1] + aloy[i]) % MOD; n   } n   for (int i = (0); i < (w); i++) { n     int k = upper_bound(aloy, aloy + h, alox[i]) - aloy - 1; n     if (~k) ans = (ans + tot[k]) % MOD; n     ans = (ans + 1LL * (h - k - 1) * alox[i]) % MOD; n   } n   printf( %d , ans); n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e12 + 1, maxn = 2 * (1e5 + 5), mod = 1e9 + 7; n const double eps = 1e-9; n long long m, i, j, k, n, answer, t, l, r; n string s; n bool f(long long l0, long long r0, long long i0) { n   if (r0 - i0 + 1 < i0 - l0) { n     return 1; n   } n   if (r0 - i0 + 1 > i0 - l0) { n     return 0; n   } n   long long jd = 0; n   for (long long i1 = l0; i1 < i0; ++i1) { n     if (s[i1] > s[i1 + i0]) { n       jd = 1; n       break; n     } n     if (s[i1] < s[i1 + i0]) { n       if (!jd) { n         return 0; n       } n       break; n     } n   } n   if (jd) { n     return 1; n   } n } n int main() { n   cin >> s; n   n = s.size(); n   l = 0; n   r = n - 1; n   for (i = n - 1; i >= 0; --i) { n     j = 0; n     while (i >= 0 && s[i] ==  0 ) { n       j = 1; n       i--; n     } n     if (f(l, r, i)) { n       r = i - 1; n       answer++; n     } else { n       break; n     } n   } n   cout << answer + 1; n } n