#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long sign = 1; n   long long x = 0; n   char ch; n   while (!isdigit(ch = getchar())) n     if (ch ==  - ) sign = -1; n   while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar(); n   return x * sign; n } n inline void wonl() { putchar(  n ); } n inline void wws() { putchar(   ); } n inline void dbg() { cout << endl; } n inline void ww(long long k) { n   if (k < 0) putchar( - ), k *= -1; n   char ch[20]; n   int num = 0; n   while (k) ch[++num] = k % 10, k /= 10; n   if (num == 0) ch[++num] = 0; n   while (num) putchar(ch[num--] +  0 ); n } n const int N = 1e5 + 4; n template <typename T, typename... V> n inline void wonl(T t, V... v) { n   ww(t); n   if (sizeof...(v)) putchar(   ); n   wonl(v...); n } n template <typename T, typename... V> n inline void wws(T t, V... v) { n   ww(t); n   if (sizeof...(v)) putchar(   ); n   wws(v...); n } n template <typename T, typename... V> n inline void dbg(T t, V... v) { n   cout <<     << t; n   dbg(v...); n } n void solve() { n   int n = read(), a; n   map<int, int> ma; n   for (int i = 0; i < n; i++) a = read(), ma[a]++; n   vector<int> v; n   for (auto it : ma) v.push_back(it.second); n   sort(v.begin(), v.end()); n   reverse(v.begin(), v.end()); n   long long num = 1; n   while (v[0] >= num) num *= 2; n   long long fans = 0; n   while (num) { n     long long ter = num; n     long long a = INT_MAX; n     int k = 0; n     while (ter != 0 && k < v.size()) { n       a = min(a, v[k] / ter), ter = ter / 2, k++; n     } n     if (ter == 0) fans = max(fans, a * (num * 2 - 1)); n     num = num / 2; n   } n   wonl(fans); n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 300000; n const long long mod = 1e9 + 7; n const long long inf = 1e18; n long long a[MX + 5]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long tc, cs = 1, i, j, k, x, y, z, n, m; n   cin >> n; n   for (i = 1; i <= n; i++) cin >> a[i]; n   sort(a + 1, a + n + 1); n   for (i = 1; i <= n; i++) a[i] += a[i - 1]; n   cin >> m; n   for (i = 1; i <= m; i++) { n     cin >> x; n     y = a[n] - (a[n - x + 1] - a[n - x]); n     cout << y << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   int a[n], i, max = 0, f; n   for (i = 0; i < n; i++) scanf( %d , &a[i]); n   stack<int> s; n   for (i = 0; i < n; i++) { n     while (!s.empty()) { n       if (s.top() < a[i]) { n         f = s.top() ^ a[i]; n         s.pop(); n         if (f > max) max = f; n       } else { n         f = s.top() ^ a[i]; n         if (f > max) max = f; n         break; n       } n     } n     s.push(a[i]); n   } n   printf( %d , max); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n; n   cin >> n; n   while (n--) { n     long long int m; n     cin >> m; n     if (m % 3 == 0 || m % 7 == 0) n       cout <<  YES  << endl; n     else { n       long long int k = m / 3; n       if (k >= 4) n         cout <<  YES  << endl; n       else { n         if (m <= 8 || m == 11) cout <<  NO  << endl; n       } n       if (m == 10) cout <<  YES  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e3 + 5; n const int mod = 998244353; n map<pair<int, int>, int> mp; n pair<int, int> a[maxn], b[maxn]; n int n; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d%d , &a[i].first, &a[i].second); n   for (int i = 1; i <= n; ++i) scanf( %d%d , &b[i].first, &b[i].second); n   if (n == 1) { n     printf( %d %d n , a[1].first + b[1].first, a[1].second + b[1].second); n     return 0; n   } n   for (int i = 1; i <= n; ++i) { n     ++mp[{a[1].first + b[i].first, a[1].second + b[i].second}]; n   } n   for (int i = 2; i <= n; ++i) { n     for (int j = 1; j <= n; ++j) { n       int x = a[i].first + b[j].first, y = a[i].second + b[j].second; n       if (mp.find({x, y}) != mp.end()) ++mp[{x, y}]; n     } n   } n   for (auto it : mp) { n     if (it.second == n) { n       printf( %d %d n , it.first.first, it.first.second); n       break; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int GCD(int u, int v) { n   while (v != 0) { n     int r = u % v; n     u = v; n     v = r; n   } n   return u; n } n int main() { n   int amtV; n   int amtE; n   std::vector<std::pair<int, int>> graph; n   std::cin >> amtV >> amtE; n   int connected = 0; n   int remaining = amtE; n   if (amtE >= amtV - 1) { n     for (int i = 2; i <= amtV; i++) { n       graph.push_back(std::pair<int, int>(i, 1)); n     } n     remaining = amtE - (amtV - 1); n     for (int i = 3; i <= amtV; i += 2) { n       if (remaining == 0) break; n       graph.push_back(std::pair<int, int>(i, 2)); n       remaining--; n     } n     int currInd = 3; n     int currCompare; n     while (remaining > 0 && currInd < amtV) { n       currCompare = currInd + 1; n       while (remaining > 0 && currCompare <= amtV) { n         if (GCD(currInd, currCompare) == 1) { n           graph.push_back(std::pair<int, int>(currInd, currCompare)); n           remaining--; n         } n         currCompare++; n       } n       currInd++; n     } n   } n   if (remaining == 0) { n     std::cout <<  Possible  << std::endl; n     for (auto it = graph.begin(); it != graph.end(); it++) { n       std::cout << it->first <<     << it->second << std::endl; n     } n   } else { n     std::cout <<  Impossible  << std::endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dist(int x, int y) { n   if (y > x) n     return (y - x); n   else n     return (12 - x + y); n } n int main() { n   int i, j, q, r, t; n   string a, b, c; n   cin >> a >> b >> c; n   map<string, int> m; n   m[ C ] = 1; n   m[ C# ] = 2; n   m[ D ] = 3; n   m[ D# ] = 4; n   m[ E ] = 5; n   m[ F ] = 6; n   m[ F# ] = 7; n   m[ G ] = 8; n   m[ G# ] = 9; n   m[ A ] = 10; n   m[ B ] = 11; n   m[ H ] = 12; n   int x, y, z; n   x = m[a]; n   y = m[b]; n   z = m[c]; n   if (dist(x, y) == 3 && dist(y, z) == 4) n     cout <<  minor ; n   else if (dist(x, y) == 4 && dist(y, z) == 3) n     cout <<  major ; n   else if (dist(x, z) == 3 && dist(z, y) == 4) n     cout <<  minor ; n   else if (dist(x, z) == 4 && dist(z, y) == 3) n     cout <<  major ; n   else if (dist(y, x) == 3 && dist(x, z) == 4) n     cout <<  minor ; n   else if (dist(y, x) == 4 && dist(x, z) == 3) n     cout <<  major ; n   else if (dist(y, z) == 3 && dist(z, x) == 4) n     cout <<  minor ; n   else if (dist(y, z) == 4 && dist(z, x) == 3) n     cout <<  major ; n   else if (dist(z, x) == 3 && dist(x, y) == 4) n     cout <<  minor ; n   else if (dist(z, x) == 4 && dist(x, y) == 3) n     cout <<  major ; n   else if (dist(z, y) == 3 && dist(y, x) == 4) n     cout <<  minor ; n   else if (dist(z, y) == 4 && dist(y, x) == 3) n     cout <<  major ; n   else n     cout <<  strange ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long x, y, n, m, t, q, a, b, count = 0; n   cin >> n; n   long long arr[n]; n   for (x = 0; x < n; x++) cin >> arr[x]; n   long long cop[n]; n   for (x = 0; x < n; x++) cop[x] = arr[x]; n   sort(cop, cop + n); n   vector<long long> v; n   for (x = 0; x < n; x++) n     if (arr[x] != cop[x]) count++, v.push_back(x); n   if (count <= 2) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O2 ) n using namespace std; n using pii = pair<int, int>; n using pll = pair<long long, long long>; n using vb = vector<bool>; n using vi = vector<int>; n using vl = vector<long long>; n using vvb = vector<vector<bool>>; n using vvi = vector<vector<int>>; n using vvl = vector<vector<long long>>; n using vpii = vector<pair<int, int>>; n using mii = map<int, int>; n template <typename... ArgTypes> n void print(ArgTypes... args); n template <typename... ArgTypes> n void input(ArgTypes &...args); n template <> n void print() {} n template <> n void input() {} n template <typename T, typename... ArgTypes> n void print(T t, ArgTypes... args) { n   cout << t; n   print(args...); n } n template <typename T, typename... ArgTypes> n void input(T &t, ArgTypes &...args) { n   cin >> t; n   input(args...); n } n void traverse(int n, int mul) { n   if (n == 0) return; n   int a = n - n / 2, b = n - n / 3; n   int x = 2, y = 3; n   if (a >= b) { n     swap(a, b); n     swap(x, y); n   } n   for (int i = 1; i <= a; i++) cout << mul <<    ; n   int remaining = n / x; n   traverse(remaining, mul * x); n } n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   traverse(n, 1); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   cout << fixed << setprecision(0) << powl(2, n + 1) - 2; n } n


#include <bits/stdc++.h> n inline int two(int n) { return 1 << n; } n inline int test(int n, int b) { return (n >> b) & 1; } n inline void set_bit(int& n, int b) { n |= two(b); } n inline void unset_bit(int& n, int b) { n &= ~two(b); } n inline int last_bit(int n) { return n & (-n); } n inline int ones(int n) { n   int res = 0; n   while (n && ++res) n -= n & (-n); n   return res; n } n long long int gcd(long long int a, long long int b) { n   return (a ? gcd(b % a, a) : b); n } n long long int modPow(long long int a, long long int b, long long int MOD) { n   long long int x = 1, y = a; n   while (b > 0) { n     if (b % 2 == 1) { n       x = (x * y) % MOD; n     } n     b /= 2; n     y = (y * y) % MOD; n   } n   return x; n } n long long int modInverse(long long int a, long long int p) { n   return modPow(a, p - 2, p); n } n using namespace std; n const int N = 1e3 + 3; n int arr[N]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, i, j, l, r, taiyar, submit, ans, m; n   ans = 0; n   taiyar = 0; n   submit = 0; n   cin >> n; n   for (int i = 0; i < (n); i++) { n     cin >> arr[i]; n     taiyar += arr[i]; n   } n   cin >> m; n   while (m--) { n     cin >> l >> r; n     if (taiyar <= r) { n       cout << max(taiyar, l); n       return 0; n     } n   } n   cout <<  -1 ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double eps = 1e-7; n int main() { n   int t; n   double a, b, ans; n   scanf( %d , &t); n   while (t--) { n     scanf( %lf%lf , &a, &b); n     ans = 0.50000000; n     if (b < eps) { n       printf( %lf n , 1.0); n       continue; n     } n     if (a < eps) { n       printf( %lf n , 0.5); n       continue; n     } n     if (a / b - 4.0 < -eps) { n       ans += 0.5 * (a * a / 8.0) / (a * b); n     } else { n       ans += 0.5 * (a * b - b * 2.0 * b) / (a * b); n     } n     printf( %lf n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n #define ll long long n #define II pair<int,int> n #define fi first n #define se second n #define task  test  n using namespace std; n   n void Init(){ n     ios::sync_with_stdio(0); n     cin.tie(0);cout.tie(0); n } n  n int n; n int a[200001]; n int res; n  n int d(II &a,II &b){ n     return abs(a.fi-b.fi)+b.se-a.se; n } n  n bool Check(int i,int j,int k){ n     II p={a[i],i}; n     II q={a[j],j}; n     II r={a[k],k}; n     //int s=d( p,q )+d( q,r ),S=d( p,r ); n     //cout<<i<<   <<j<<   <<k<<   <<s<<   <<S<<  n ; n     return( d( p,q )+d( q,r )==d( p,r ) ); n } n  n bool Cal(int l,int r){ n     for(int i=l;i<r;++i) n     for(int j=i+1;j<r;++j) n     if(Check(i,j,r))return 1; n     res++; n     return 0; n } n  n void Solve(){ n     cin>>n; n     while(cin>>n){ n         for(int i=1;i<=n;++i)cin>>a[i]; n  n         res=0; n         for(int i=1;i<=n;++i) n         for(int j=i+2;j<=n;++j){ n             if(Cal(i,j))break; n             //cout<<i<<   <<j<<  n ; n         } n  n         cout<<res+n+n-1<<  n ; n     } n } n  n int main(){ n     Init(); n     Solve(); n } n


#include <bits/stdc++.h> n using namespace std; n constexpr int MAXN = 128; n int type[MAXN][MAXN]; n constexpr char* pat[6] = { #//. ,    #.  ,    .#  ,  .//# ,  .... ,  #### }; n int main() { n   int n, m, a, b, c, k, x, y; n   scanf( %d%d , &n, &m); n   scanf( %d%d%d , &a, &b, &c); n   k = 0; n   for (int i = 0; i < a; ++i, ++k) type[k / m][k % m] = 5; n   y = k % m; n   for (int i = 0; i < m; ++i, ++k) n     type[k / m][k % m] = (((k % m - y) % 2 == 0) ^ (k % m < y)) ? 0 : 1; n   for (int i = 0; i < b; ++i, ++k) type[k / m][k % m] = 4; n   for (int i = m; i < c; ++i, ++k) n     for (int j = 0; j < 4; ++j) { n       x = k / m; n       y = k % m; n       if ((x == 0 || ((type[x - 1][y] ^ j) & 2) != 0) && n           (y == 0 || ((type[x][y - 1] ^ j) & 1) != 0)) { n         type[k / m][k % m] = j; n         break; n       } n     } n   for (int i = 0; i < n + n; ++i) { n     for (int j = 0; j < m + m; ++j) n       putchar(pat[type[i / 2][j / 2]][i % 2 * 2 + j % 2]); n     putchar(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int tata[2010], g[2010]; n vector<pair<int, int>> edges; n bitset<2001> secret[2001]; n int find(int x) { n   return (tata[tata[x]] == tata[x] ? tata[x] : tata[x] = find(tata[x])); n } n void unite(int a, int b) { n   a = find(a); n   b = find(b); n   assert(a != b); n   if (g[a] >= g[b]) n     tata[b] = a, g[a] += g[b]; n   else n     tata[a] = b, g[b] += g[a]; n } n void test() { n   edges.clear(); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     tata[i] = i, g[i] = 1; n     secret[i] = bitset<2001>(); n   } n   for (int i = 0; i < m; i++) { n     string s; n     cin >> s; n     for (int j = 0; j < n; j++) secret[j][i] = s[j] -  0 ; n   } n   vector<pair<int, pair<int, int>>> ord; n   for (int i = 0; i < n; i++) n     for (int j = i + 1; j < n; j++) n       ord.push_back({(secret[i] & secret[j]).count(), {i, j}}); n   sort(ord.rbegin(), ord.rend()); n   for (auto i : ord) { n     if (find(i.second.first) == find(i.second.second)) continue; n     unite(i.second.first, i.second.second); n     edges.push_back({i.second.first, i.second.second}); n   } n   for (int i = 0; i < m; i++) { n     int nr = 0; n     for (int j = 0; j < n; j++) nr += secret[j][i]; n     for (auto j : edges) nr -= secret[j.first][i] & secret[j.second][i]; n     if (nr != 1) { n       cout <<  NO n ; n       return; n     } n   } n   cout <<  YES n ; n   for (auto i : edges) cout << i.first + 1 <<     << i.second + 1 <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) test(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f, N = 10; n int ans = inf, n, k, idx[N]; n char arr[N][N]; n int convert(int num) { n   int ret = 0; n   for (int i = 0; i < k; ++i) { n     ret = (ret * 10) + (arr[num][idx[i]] -  0 ); n   } n   return ret; n } n int main() { n   cin >> n >> k; n   for (int i = 0; i < k; ++i) idx[i] = i; n   for (int i = 0; i < n; ++i) scanf( %s , arr[i]); n   do { n     int mn = inf, mx = 0; n     for (int i = 0; i < n; ++i) { n       mn = min(mn, convert(i)); n       mx = max(mx, convert(i)); n     } n     ans = min(ans, mx - mn); n   } while (next_permutation(idx, idx + k)); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class A, class B> n inline bool mina(A &x, B y) { n   return (x > y) ? (x = y, 1) : 0; n } n template <class A, class B> n inline bool maxa(A &x, B y) { n   return (x < y) ? (x = y, 1) : 0; n } n template <class Value_t> n class SegmentTree { n  public: n   typedef struct segment_st { n     Value_t value; n     int left, right; n   } TreeNode; n   vector<TreeNode> tree; n   int N; n   int tree_size; n   vector<int> leaves; n   SegmentTree(int n) { n     vector<Value_t> v(n); n     build_tree(n, v); n   } n   SegmentTree(int n, vector<Value_t> &v) { build_tree(n, v); } n   Value_t query(int ll, int rr) { return query(1, ll, rr); } n   void update(int at, Value_t val) { internal_update(at, val); } n  n  private: n   void build_tree(int n, vector<Value_t> &v) { n     N = n; n     int base = 1; n     while (base < N) base *= 2; n     tree_size = base * 2; n     leaves.resize(N + 1); n     tree = vector<TreeNode>(tree_size); n     init(1, 1, n, v); n   } n   void init(int at, int ll, int rr, vector<Value_t> &v) { n     tree[at].left = ll; n     tree[at].right = rr; n     if (ll == rr) { n       tree[at].value = v[ll - 1]; n       leaves[ll] = at; n     } else { n       int mid = (tree[at].left + tree[at].right) / 2; n       init(at * 2, tree[at].left, mid, v); n       init(at * 2 + 1, mid + 1, tree[at].right, v); n       update_up(at); n     } n   } n   Value_t initial_value() { return tuple<int, int>(-1, 0); } n   Value_t node_to_value(int at) { return tree[at].value; } n   void update_up(int at) { n     tuple<int, int> left = node_to_value(at * 2); n     tuple<int, int> right = node_to_value(at * 2 + 1); n     if (get<0>(left) > get<0>(right)) { n       tree[at].value = left; n     } else { n       tree[at].value = right; n     } n   } n   void update_result(Value_t &res, Value_t val) { n     if (get<0>(val) > get<0>(res)) { n       res = val; n     } n   } n   void update_leaf_value(int at, Value_t val) { tree[at].value = val; } n   Value_t query(int at, int ll, int rr) { n     if (tree[at].left == ll && tree[at].right == rr) { n       return node_to_value(at); n     } else { n       int mid = (tree[at].left + tree[at].right) / 2; n       Value_t res = initial_value(); n       if (ll <= mid) update_result(res, query(at * 2, ll, min(rr, mid))); n       if (rr > mid) update_result(res, query(at * 2 + 1, max(mid + 1, ll), rr)); n       return res; n     } n   } n   void internal_update(int at, Value_t val) { n     at = leaves[at]; n     update_leaf_value(at, val); n     at /= 2; n     while (at) { n       update_up(at); n       at /= 2; n     } n   } n }; n map<int, long long> ans[(100005)]; n int in[(100005)]; n int N, Q; n SegmentTree<tuple<int, int> > tree(1); n int solve2(int l, int r, int &x, int &y) { n   tuple<int, int> a = tree.query(l, r - 1); n   x = get<1>(a); n   y = x + 1; n   return get<0>(a); n } n long long solve(int l, int r) { n   if (l >= r) return 0; n   if (ans[l].find(r) != ans[l].end()) { n     return ans[l][r]; n   } n   int x = 0, y = 0; n   long long L = solve2(l, r, x, y); n   if (x > y) swap(x, y); n   ans[l][r] = L * (x - l + 1) * (r - y + 1) + solve(l, x) + solve(y, r); n   return ans[l][r]; n } n int main() { n   scanf( %d %d , &N, &Q); n   for (int(i) = 0; (i) < (N); ++(i)) { n     scanf( %d , &in[i]); n   } n   tree = SegmentTree<tuple<int, int> >(N - 1); n   for (int(i) = 0; (i) < (N - 1); ++(i)) { n     tree.update(i + 1, tuple<int, int>(abs(in[i + 1] - in[i]), i + 1)); n   } n   int l, r; n   for (int(q) = 0; (q) < (Q); ++(q)) { n     scanf( %d %d , &l, &r); n     printf( %I64d n , solve(l, r)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char* argv[]) { n   int v1, v2, t, d, sum, a, t1, t2; n   while (cin >> v1 >> v2 >> t >> d) { n     sum = v1 + v2; n     for (int i = 2; i < t; i++) { n       if (v1 <= v2) { n         v1 += d; n         sum += v1; n       } else { n         v2 += d; n         sum += v2; n       } n     } n     cout << sum << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int INF = 1e9 + 100; n const long long lINF = 4e18; n const double EPS = 1e-12; n using namespace std; n const int N = 5e5 + 100; n int n, k, h[N], s[N], mn = INF, mxh[N], should = INF; n vector<int> graph[N]; n vector<int> can, need; n void dfs1(int v, int par = -1, int mx = INF) { n   mxh[v] = mx; n   mx = min(mx, h[v]); n   can.push_back(mx); n   for (auto j : graph[v]) { n     if (j != par) dfs1(j, v, mx); n   } n } n void dfs2(int v, int par, map<int, int>& ans) { n   map<int, int> ans2; n   for (auto j : graph[v]) { n     if (j != par) { n       dfs2(j, v, ans2); n       if (ans.size() < ans2.size()) { n         swap(ans, ans2); n       } n       for (auto k : ans2) { n         ans[k.first] += k.second; n       } n       ans2.clear(); n     } n   } n   if (mxh[v] >= need[0] && h[v] <= should) { n     bool flag = true; n     ans[need[0]]++; n     map<int, int>::reverse_iterator j = ans.rbegin(); n     for (int i = 0; i < (int)need.size(); i++) { n       while (j != ans.rend() && j->second == 0) { n         j++; n       } n       if (j == ans.rend() || j->first < need[i]) { n         flag = false; n       } else { n         j->second--; n       } n     } n     if (flag) { n       mn = min(mn, need[0] - h[v]); n     } n     ans.clear(); n   } else { n     while (ans.size() > 0 && ans.rbegin()->first > h[v]) { n       int p = ans.rbegin()->second; n       ans.erase(ans.rbegin()->first); n       ans[h[v]] += p; n     } n     ans[h[v]]++; n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &h[i]); n   } n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     graph[a].push_back(b); n     graph[b].push_back(a); n   } n   scanf( %d , &k); n   for (int i = 0; i < k; i++) { n     scanf( %d , &s[i]); n   } n   dfs1(1); n   sort(s, s + k); n   reverse(s, s + k); n   sort((can).begin(), (can).end()); n   reverse((can).begin(), (can).end()); n   for (int i = 0, j = 0, delta = 0; i < k; i++) { n     delta--; n     while (j < (int)can.size() && can[j] >= s[i]) { n       j++; n       delta++; n     } n     if (j < i + 1) { n       should = min(should, s[i] - 1); n     } n     if (delta < 0) { n       need.push_back(s[i]); n       delta++; n     } n   } n   if (need.size() == 0) { n     cout << 0 << endl; n   } else { n     map<int, int> res; n     dfs2(1, -1, res); n     if (mn == INF) { n       cout <<  -1  << endl; n     } else { n       cout << mn << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100000; n bool isprime[maxn]; n int p[5], s[5], n; n const int tenpow[6] = {1, 10, 100, 1000, 10000, 100000}; n int tenpowmul[6][10]; n int getd[maxn][5]; n int ans; n bool good[2 * maxn][6]; n int tans[maxn]; n void solve(int i) { n   if (i == 0) { n     for (int i = 0; i < n; ++i) s[i] = tenpowmul[n - 1][getd[p[0]][n - 1 - i]]; n     solve(i + 1); n   } else if (i + 1 == n) n     ans += tans[s[i]]; n   else n     for (int j = 0; j < tenpow[n - i]; ++j) n       if (isprime[s[i] + j]) { n         p[i] = s[i] + j; n         int y[5], k; n         for (k = i; k < n; ++k) { n           s[k] += (y[k] = tenpowmul[n - i - 1][getd[p[i]][n - k - 1]]); n           if (!good[s[k]][n - i]) break; n         } n         if (k == n) solve(i + 1), k--; n         for (; k >= i; --k) s[k] -= y[k]; n       } n } n int main() { n   for (int i = 0; i < (int)(6); ++i) n     for (int j = 0; j < (int)(10); ++j) tenpowmul[i][j] = tenpow[i] * j; n   for (int i = 0; i < (int)(maxn); ++i) n     for (int j = 0; j < (int)(5); ++j) getd[i][j] = (i / tenpow[j]) % 10; n   for (int i = (int)(2); i <= (int)(maxn); ++i) isprime[i] = 1; n   for (int i = 2; i * i < maxn; ++i) n     if (isprime[i]) n       for (int j = i * i; j < maxn; j += i) isprime[j] = 0; n   for (int i = 0; i < (int)(maxn); ++i) good[i][0] = isprime[i]; n   for (int j = (int)(1); j <= (int)(5); ++j) n     for (int i = 0; i < (int)(maxn); ++i) n       for (int k = 0; k < (int)(10); ++k) n         good[i][j] |= good[i + tenpowmul[j - 1][k]][j - 1]; n   for (int i = 0; i < (int)(maxn); ++i) n     if (i % 10 == 0) n       for (int k = 0; k < (int)(10); ++k) tans[i] += isprime[i + k]; n   int T; n   cin >> T; n   for (int _t = 0; _t < (int)(T); ++_t) { n     cin >> p[0]; n     int k = 1; n     n = 0; n     while (k <= p[0]) k *= 10, n += 1; n     for (int i = (int)(1); i <= (int)(n - 1); ++i) p[i] = 0; n     ans = 0; n     solve(0); n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch - 48; n     ch = getchar(); n   } n   return x * f; n } n namespace star { n const int maxn = 2e5 + 5; n int head[maxn], len[maxn], dis[maxn], pre[maxn]; n int n, cnt = 1, m, ans; n char ch[maxn]; n bool vis[maxn]; n priority_queue<pair<int, int> > q; n queue<int> qt; n struct edge { n   int to, nxt, val; n } e[maxn]; n void add(int u, int v, int w) { n   e[++cnt].to = v; n   e[cnt].val = w; n   e[cnt].nxt = head[u]; n   head[u] = cnt; n   e[++cnt].to = u; n   e[cnt].val = w; n   e[cnt].nxt = head[v]; n   head[v] = cnt; n } n inline void dijkstra() { n   memset(pre, 0, sizeof(pre)); n   memset(dis, 0x3f, sizeof(dis)); n   memset(len, 0x3f, sizeof(len)); n   qt.push(n); n   dis[n] = 0; n   len[n] = 1; n   while (!qt.empty()) { n     int u = qt.front(); n     qt.pop(); n     q.push(make_pair(0, u)); n     for (int i = head[u]; i; i = e[i].nxt) { n       int v = e[i].to; n       int w = e[i].val; n       if (dis[v] && w == 0) { n         dis[v] = 0; n         len[v] = len[u] + 1; n         pre[v] = (i ^ 1); n         qt.push(v); n       } n     } n   } n   int pos = 0, lst = -1; n   while (!q.empty()) { n     int u = q.top().second; n     q.pop(); n     if (vis[u]) continue; n     vis[u] = true; n     if (lst != dis[u]) pos++, lst = dis[u]; n     for (int i = head[u]; i; i = e[i].nxt) { n       int v = e[i].to; n       int w = e[i].val; n       if (dis[v] > pos * 10 + w) { n         dis[v] = pos * 10 + w; n         len[v] = len[u] + 1; n         pre[v] = i ^ 1; n         q.push(make_pair(-dis[v], v)); n       } else if (dis[v] == pos * 10 + w && len[v] > len[u] + 1) { n         len[v] = len[u] + 1; n         pre[v] = i ^ 1; n         q.push(make_pair(-dis[v], v)); n       } n     } n   } n } n void work() { n   n = read(); n   m = read(); n   for (int a, b, i = 1; i <= m; i++) n     a = read() + 1, b = read() + 1, add(a, b, read()); n   dijkstra(); n   int tmp = 1; n   stack<int> s; n   while (tmp != n) { n     s.push(e[pre[tmp]].val); n     tmp = e[pre[tmp]].to; n   } n   while (!s.empty() && s.top() == 0) s.pop(); n   if (s.empty()) n     printf( 0 ); n   else n     while (!s.empty()) printf( %d , s.top()), s.pop(); n   putchar(  n ); n   printf( %d n0  , len[1]); n   tmp = 1; n   while (tmp != n) { n     tmp = e[pre[tmp]].to; n     printf( %d  , tmp - 1); n   } n } n }  // namespace star n signed main() { n   star::work(); n   return 0; n } n


#include <bits/stdc++.h> n using ll = long long; n using namespace std; n int main() { n   cin.tie(0); n   ios_base::sync_with_stdio(false); n   int n, m; n   char c; n   cin >> n >> m >> c; n   vector<string> vec(n); n   for (int i = 0; i < n; ++i) { n     cin >> vec[i]; n   } n   unordered_set<char> s; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       if (vec[i][j] == c) { n         if (i - 1 >= 0 && vec[i - 1][j] !=  .  && vec[i - 1][j] != c) { n           s.insert(vec[i - 1][j]); n         } n         if (j - 1 >= 0 && vec[i][j - 1] !=  .  && vec[i][j - 1] != c) { n           s.insert(vec[i][j - 1]); n         } n         if (i + 1 < n && vec[i + 1][j] !=  .  && vec[i + 1][j] != c) { n           s.insert(vec[i + 1][j]); n         } n         if (j + 1 < m && vec[i][j + 1] !=  .  && vec[i][j + 1] != c) { n           s.insert(vec[i][j + 1]); n         } n       } n     } n   } n   cout << s.size() <<   n ; n   return 0; n } n


#include<bits/stdc++.h> n  n using namespace std; n #define show(x) cout<< #x <<  =   << x <<    n  ; n #define mod 998244353 n #define INF 1000000000000000000 n #define pie 3.14159265358979323846 n #define ll long long int n #define pb push_back n #define popcnt __builtin_popcount n #define mp make_pair n #define pii pair<ll,ll> n #define prod(a,b) ((a*b)%mod) n #define set0(x) memset(x,0,sizeof(x)) n #define setm1(x) memset(x,-1,sizeof(x)) n #define printlist(arr) for(auto lstelem :arr) cout<< lstelem <<    ;cout<<   n   n #define all(a) a.begin(),a.end() n #define sqre(a) ((a)*(a)) n  n //maths stuff n ll powr(ll a,ll b){ n     ll res=1; n     while(b){ n         if(b%2) n             res=(a*res)%mod; n         a=(a*a)%mod; n         b/=2; n     } n     return res; n } n  n void extentded_eucledian(ll a,ll b,ll &ex,ll &ey){ n     if(b==0){ n         ex=1; n         ey=0; n         return; n     } n     extentded_eucledian(b,a%b,ex,ey); n     swap(ex,ey); n     ey-= (a/b)*ex;//gcd= x*a+y*b n } n  n ll dp[1002][1002][2]; n  n int main(){  n     #ifdef ONLINE_JUDGE n         //onlinesubmission n     #else n     freopen( input.txt , r ,stdin); n     #endif n     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); n     string x,y; cin>> x >> y ; n     ll n=x.size(),m=y.size(); n     set0(dp); n     ll ans=0; n     for(ll i=0;i<=n;++i){ n         for(ll j=0;j<=m;++j){ n             if(i>0) dp[i][j][0]=(dp[i][j][0]+1)%mod; n             if(j>0) dp[i][j][1]=(dp[i][j][1]+1)%mod; n             if(i>1){ n                 if(x[i-1]!=x[i-2]) dp[i][j][0]=(dp[i][j][0]+dp[i-1][j][0])%mod; n             } n             if(i>0 && j>0){ n                 if(x[i-1]!=y[j-1]){ n                     dp[i][j][0]=(dp[i][j][0]+dp[i-1][j][1])%mod; n                     dp[i][j][1]=(dp[i][j][1]+dp[i][j-1][0])%mod; n                 } n             } n             if(j>1){ n                 if(y[j-1]!=y[j-2]) dp[i][j][1]=(dp[i][j][1]+dp[i][j-1][1])%mod; n             } n             ans=(ans+dp[i][j][0]+dp[i][j][1])%mod; n         } n     } n     for(ll i=0;i<n;++i){ n         ll j=i; n         while(j+1<n) if(x[j]!=x[j+1]) ++j; else break; n         ll l=(j-i+1),v=(( ((l+1)*l/2)%mod )*(m+1))%mod; n         ans=(ans-v+mod)%mod; n         i=j; n     } n     for(ll i=0;i<m;++i){ n         ll j=i; n         while(j+1<m) if(y[j]!=y[j+1]) ++j; else break; n         ll l=(j-i+1),v=((((l+1)*l/2)%mod)*(n+1))%mod; n         ans=(ans-v+mod)%mod; n         i=j; n     } n     cout<< ans ; n     return 0; n } n  n //might1331 pratapSingh: Try Kr raha hu mai bc, timer laga rakha h


#include <bits/stdc++.h> n using namespace std; n bool prime(long long x) { n   if (x == 2) { n     return true; n   } n   if (x <= 1) { n     return false; n   } n   for (int i = 2; i < sqrt(x); i++) { n     if (x % i == 0) { n       return false; n     } n   } n   return true; n } n void printv(vector<long long> v) { n   for (int i = 0; i < v.size(); i++) { n     cout << v[i] <<    ; n   } n   cout << endl; n } n string Y =  YES n ; n string N =  NO n ; n int rec(string s, int pos, int team1, int team2) { n   if (team1 < team2) { n     if (5 - (ceil((pos * 1.0) / 2)) < (team2 - team1)) { n       return pos; n     } n   } else { n     if (5 - (pos / 2) < (team1 - team2)) { n       return pos; n     } n   } n   if (pos == 10) { n     return 10; n   } n   int h = 10; n   if (pos % 2 == 0) { n     if (s[pos] ==  1 ) { n       h = min(h, rec(s, pos + 1, team1 + 1, team2)); n     } else if (s[pos] ==  0 ) { n       h = min(h, rec(s, pos + 1, team1, team2)); n     } else { n       int x = n           min(rec(s, pos + 1, team1 + 1, team2), rec(s, pos + 1, team1, team2)); n       h = min(x, h); n     } n   } else { n     if (s[pos] ==  1 ) { n       h = min(h, rec(s, pos + 1, team1, team2 + 1)); n     } else if (s[pos] ==  0 ) { n       h = min(h, rec(s, pos + 1, team1, team2)); n     } else { n       int x = n           min(rec(s, pos + 1, team1, team2 + 1), rec(s, pos + 1, team1, team2)); n       h = min(x, h); n     } n   } n   return h; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   cin >> t; n   for (int pop = 0; pop < t; pop++) { n     string q; n     cin >> q; n     int c = rec(q, 0, 0, 0); n     cout << c << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int k[3]; n int t[3]; n long long a[100010]; n struct mydata { n   int idx; n   long long x; n   bool operator<(const mydata &r) const { n     if (x == r.x) n       return idx < r.idx; n     else n       return x < r.x; n   } n } b[100010]; n std::priority_queue<long long, vector<long long>, greater<long long> > Q; n int main() { n   int i; n   cin >> k[0] >> k[1] >> k[2]; n   cin >> t[0] >> t[1] >> t[2]; n   int n; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   for (i = 0; i < n; i++) { n     b[i].x = a[i]; n     b[i].idx = i; n   } n   for (i = 0; i < n; i++) { n     if (k[0] > 0) { n       b[i].x += t[0]; n       Q.push(b[i].x); n       k[0]--; n     } else { n       long long x = Q.top(); n       Q.pop(); n       b[i].x = max(x, b[i].x) + t[0]; n       Q.push(b[i].x); n     } n   } n   while (!Q.empty()) Q.pop(); n   sort(b, b + n); n   for (i = 0; i < n; i++) { n     if (k[1] > 0) { n       b[i].x += t[1]; n       Q.push(b[i].x); n       k[1]--; n     } else { n       long long x = Q.top(); n       Q.pop(); n       b[i].x = max(x, b[i].x) + t[1]; n       Q.push(b[i].x); n     } n   } n   while (!Q.empty()) Q.pop(); n   sort(b, b + n); n   for (i = 0; i < n; i++) { n     if (k[2] > 0) { n       b[i].x += t[2]; n       Q.push(b[i].x); n       k[2]--; n     } else { n       long long x = Q.top(); n       Q.pop(); n       b[i].x = max(x, b[i].x) + t[2]; n       Q.push(b[i].x); n     } n   } n   while (!Q.empty()) Q.pop(); n   sort(b, b + n); n   long long ret = 0; n   for (i = 0; i < n; i++) { n     if (ret < b[i].x - a[b[i].idx]) ret = b[i].x - a[b[i].idx]; n   } n   cout << ret << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100005, MAXM = 1005; n bitset<MAXM> All, P[MAXM]; n int n, m, Q; n int start[MAXN], cnt = 0; n int Ans[MAXN]; n struct Node { n   int op, x, y, id; n } T[MAXN]; n struct Edge { n   int next, to; n } edge[MAXN]; n void add(int from, int to) { n   cnt++; n   edge[cnt].to = to; n   edge[cnt].next = start[from]; n   start[from] = cnt; n   return; n } n void DFS(int x, int from, int Now) { n   bool q = 0; n   if (T[x].op == 1) { n     q = P[T[x].x][T[x].y]; n     if (P[T[x].x][T[x].y] == 0) Now++; n     P[T[x].x][T[x].y] = 1; n   } n   if (T[x].op == 2) { n     q = P[T[x].x][T[x].y]; n     if (P[T[x].x][T[x].y] == 1) Now--; n     P[T[x].x][T[x].y] = 0; n   } n   if (T[x].op == 3) { n     int A = P[T[x].x].count(); n     P[T[x].x] ^= All; n     Now += P[T[x].x].count() - A; n   } n   Ans[T[x].id] = Now; n   for (int i = start[x]; i; i = edge[i].next) { n     int to = edge[i].to; n     if (to != from) DFS(to, x, Now); n   } n   if (T[x].op <= 2) P[T[x].x][T[x].y] = q; n   if (T[x].op == 3) P[T[x].x] ^= All; n   return; n } n inline int read() { n   int x = 0, flag = 1; n   char ch = getchar(); n   for (; ch >  9  || ch <  0 ; ch = getchar()) n     if (ch ==  - ) flag = -1; n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) n     x = (x << 3) + (x << 1) + ch -  0 ; n   return x * flag; n } n int main() { n   n = read(), m = read(), Q = read(); n   for (int i = 1; i <= m; i++) All[i] = 1; n   for (int i = 1; i <= n; i++) P[i].reset(); n   T[0].x = T[0].y = 0; n   for (int i = 1; i <= Q; i++) { n     T[i].op = read(); n     T[i].id = i; n     if (T[i].op <= 2) n       T[i].x = read(), T[i].y = read(); n     else n       T[i].x = read(); n     if (T[i].op <= 3) n       add(i - 1, i); n     else n       add(T[i].x, i); n   } n   DFS(0, 0, 0); n   for (int i = 1; i <= Q; i++) cout << Ans[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T, class L> n bool smax(T& x, L y) { n   return x < y ? (x = y, 1) : 0; n } n template <class T, class L> n bool smin(T& x, L y) { n   return x > y ? (x = y, 1) : 0; n } n const int maxn = 2e5 + 17; n int n, m, gp[maxn], rate[maxn], cnt, sz[maxn]; n unordered_map<int, pair<int, int> > p; n set<int> s[maxn]; n vector<int> ag[maxn]; n void dfs(int v, int rt = 0) { n   if (gp[v]) return; n   gp[v] = cnt, rate[v] = rt; n   s[cnt].insert(rt); n   sz[cnt]++; n   dfs((v + m) % n, rt + 1); n } n int main() { n   long long ans = 0; n   p.max_load_factor(0.25), p.reserve(512); n   scanf( %d %d , &n, &m); n   for (int i = 0; i < n; i++) n     if (!gp[i]) cnt++, dfs(i); n   int q; n   cin >> q; n   for (int id, h; q--;) { n     char c; n     scanf(  %c %d , &c, &id); n     if (c ==  + ) { n       scanf( %d , &h); n       int g = gp[h], r = rate[h]; n       auto it = s[g].lower_bound(r); n       if (it == s[g].end()) it = s[g].begin(), ans += sz[g]; n       ans += *it - r; n       p[id] = {g, *it}; n       s[g].erase(it); n     } else { n       auto tmp = p[id]; n       s[tmp.first].insert(tmp.second); n     } n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> mp; n long long v[100000]; n vector<long long> V[200005]; n long long sum[200005] = {0}, l[5005], r[5005], ans[5005]; n int main() { n   long long int n, k, q, i, j, a, b, d, l1, t, sm = 0; n   cin >> t; n   for (i = 1; i <= t; i++) { n     cin >> l[i]; n   } n   for (i = 1; i <= t; i++) { n     memset(r, 0, sizeof(r)); n     long long mx = 0, w = -1; n     for (j = i; j <= t; j++) { n       r[l[j]]++; n       if (r[l[j]] > mx) { n         mx = r[l[j]]; n         w = l[j]; n       } else if (r[l[j]] == mx && w > l[j]) { n         w = l[j]; n       } n       ans[w]++; n     } n   } n   for (i = 1; i <= t; i++) { n     cout << ans[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O2 ) n using namespace std; n const int MAX = 1e5 + 5; n const long long MAX2 = 11; n const int MOD = 1000000000 + 7; n const long long INF = 20000; n const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1}; n const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1}; n const double pi = acos(-1); n long long n; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   cout << n / 2520; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char str[1000001]; n int main() { n   int n; n   scanf( %d , &n); n   scanf( %s , str); n   int sum = 0; n   int ln = -1; n   int i, j, k, ans = 0; n   for (i = 0; i < n; i++) { n     if (str[i] ==  ( ) { n       sum++; n       if (sum == 0) { n         ans += i - ln + 1; n         ln = -1; n       } n     } else { n       sum--; n       if (sum < 0 && ln == -1) ln = i; n     } n   } n   if (sum != 0) { n     printf( -1 n ); n   } else n     printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = (long long)1e9 + 7; n const long long N = (long long)2e6 + 222; n const long long INF = (long long)1e18; n const double eps = (double)1e-6; n long long n, m, l, r, used[N]; n vector<long long> g[N]; n set<long long> s; n long long cnt = 0; n void dfs(long long x) { n   used[x] = 1; n   s.erase(x); n   cnt++; n   long long pr = 0; n   for (auto to : g[x]) { n     while (s.upper_bound(pr) != s.end() && (*s.upper_bound(pr)) < to) { n       long long k = (*s.upper_bound(pr)); n       if (!used[k]) dfs(k); n     } n     pr = to; n   } n } n void solve() { n   cin >> n >> m; n   for (long long i = 1; i <= m; ++i) { n     cin >> l >> r; n     g[l].push_back(r); n     g[r].push_back(l); n   } n   for (long long i = 1; i <= n; ++i) s.insert(i); n   long long ans = 0; n   for (long long i = 1; i <= n; ++i) sort(g[i].begin(), g[i].end()); n   for (long long i = 1; i <= n; ++i) g[i].push_back(n + 1); n   for (long long i = 1; i <= n; ++i) { n     if (!used[i]) { n       ans++; n       dfs(i); n     } n   } n   cout << ans - 1; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     if (n <= 3 || n == 5 || n == 7 || n == 11) { n       cout <<  -1  n            <<   n ; n     } else { n       if (n % 2 == 0) { n         cout << n / 4 <<   n ; n       } else { n         cout << n / 4 - 1 <<   n ; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, a[300000][2], fa[300000], sz[300000], top[300000], d[300000], n     prf[300000], f1[300000], q1[300000], q2[300000]; n vector<int> g[300000], b[300000]; n map<int, map<int, int> > f2[300000]; n long long ans; n void dfs1(int u) { n   d[u] = d[fa[u]] + 1; n   sz[u] = 1; n   for (int v : g[u]) n     if (v != fa[u]) { n       fa[v] = u; n       dfs1(v); n       sz[u] += sz[v]; n     } n } n void dfs2(int u) { n   if (!top[u]) top[u] = u; n   b[top[u]].push_back(u); n   int t = 0; n   for (int v : g[u]) n     if (v != fa[u] && sz[v] > sz[t]) t = v; n   if (!t) return; n   prf[u] = t; n   top[t] = top[u]; n   dfs2(t); n   for (int v : g[u]) n     if (v != fa[u] && v != t) dfs2(v); n } n int lca(int x, int y) { n   for (; top[x] != top[y]; x = fa[top[x]]) n     if (d[top[x]] < d[top[y]]) swap(x, y); n   return d[x] < d[y] ? x : y; n } n int go(int x, int k) { n   int len = d[x] - d[top[x]]; n   if (len < k) return go(fa[top[x]], k - len - 1); n   return b[top[x]][len - k]; n } n int add1(int x, int y) { n   int len = d[x] - d[y]; n   if (len < k) return x; n   int z = go(x, len - k + 1); n   ++f1[x]; n   --f1[z]; n   return z; n } n void add2(int u1, int u2, int v1, int v2) { n   if (top[u1] > top[v1]) swap(u1, v1), swap(u2, v2); n   if (d[u1] > d[u2]) swap(u1, u2); n   ++f2[top[u1]][top[v1]][d[u1]]; n   --f2[top[u1]][top[v1]][d[u2] + 1]; n } n void add(int x, int y) { n   int z = lca(x, y); n   x = add1(x, z); n   y = add1(y, z); n   int u = x, v = y; n   int n1 = 0, n2 = 0; n   for (; top[u] != top[z]; u = fa[top[u]]) q1[++n1] = u; n   for (; top[v] != top[z]; v = fa[top[v]]) q2[++n2] = v; n   if (u != z) q1[++n1] = u; n   if (v != z) q2[++n2] = v; n   for (int i1 = 1, i2 = n2; i1 <= n1; ++i1) { n     int u1 = q1[i1], u2 = top[u1]; n     if (d[u2] <= d[z]) u2 = prf[z]; n     for (; i2; --i2) { n       int v1 = q2[i2], v2 = top[v1]; n       if (d[v2] <= d[z]) v2 = prf[z]; n       if (d[u1] + d[v1] - d[z] * 2 < k) continue; n       int len = d[u1] + d[v1] - d[z] * 2; n       int w1 = go(v1, len - k), w2 = 0; n       len = d[u2] + d[v1] - d[z] * 2; n       if (len >= k) { n         w2 = go(v1, len - k); n         add2(u1, u2, w1, w2); n         break; n       } n       len = d[u1] + d[v1] - d[z] * 2; n       w2 = go(u1, len - k); n       add2(u1, w2, w1, v1); n       u1 = fa[w2]; n     } n   } n } n long long C(long long x) { return x * (x - 1) / 2; } n void dfs3(int u, int t) { n   for (int v : g[u]) n     if (v != fa[u]) { n       dfs3(v, t); n       f1[u] += f1[v]; n     } n   ans += C(f1[u]) * t; n } n void calc(int t) { n   for (int i = 1; i <= n; ++i) { n     for (auto j : f2[i]) { n       long long sum = 0, lst = 0; n       for (auto k : j.second) { n         ans += (k.first - lst) * C(sum) * t; n         lst = k.first; n         sum += k.second; n       } n     } n     f2[i].clear(); n   } n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i < n; ++i) { n     int x, y; n     scanf( %d%d , &x, &y); n     g[x].push_back(y); n     g[y].push_back(x); n   } n   dfs1(1); n   dfs2(1); n   for (int i = 1; i <= m; ++i) { n     scanf( %d%d , a[i] + 0, a[i] + 1); n     add(a[i][0], a[i][1]); n   } n   dfs3(1, 1); n   calc(1); n   memset(f1, 0, sizeof f1); n   ++k; n   for (int i = 1; i <= m; ++i) add(a[i][0], a[i][1]); n   dfs3(1, -1); n   calc(-1); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[300005]; n map<int, int> mp; n vector<int> opt; n int main() { n   int n, lst = 0; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &arr[i]); n     if (mp[arr[i]] && mp[arr[i]] > lst) { n       mp[arr[i]] = 0; n       opt.push_back(++lst); n       opt.push_back(i); n       lst = i; n     } else { n       mp[arr[i]] = i; n     } n   } n   int l = opt.size(); n   if (l / 2 == 0) { n     printf( -1 n ); n     return 0; n   } n   printf( %d n , l / 2); n   opt[l - 1] = n; n   for (int i = 0; i < l; i++) { n     printf( %d  , opt[i++]); n     printf( %d n , opt[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const double EPS = 1e-9; n const long long INF = 1e18 + 2007; n const double PI = 3.1415926535897932384626433832795; n const long long N = 200005; n long long gcd(long long a, long long b) { return a ? gcd(b % a, a) : b; } n signed main() { n   ios_base::sync_with_stdio(0); n   long long n, k; n   cin >> n >> k; n   long long g = 0; n   for (long long i = 0; i < n; i++) { n     long long a; n     cin >> a; n     g = gcd(g, a); n   } n   set<long long> ans; n   for (long long i = 0; i < k; i++) { n     ans.insert((g * i) % k); n   } n   cout << ans.size() << endl; n   for (auto x : ans) { n     cout << x <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, i, j, z, x = 10, k = 0, year = 0, l = 0, f = 0; n   string s; n   cin >> n; n   while (n--) { n     cin >> s; n     x = 10; n     f = 0; n     year = 0; n     k = s.length() - 4; n     for (i = 1; i < k; i++) { n       f += x; n       x *= 10; n     } n     for (i = 4; i < s.length(); i++) { n       year = year * 10 + 10 * (s[i] - 48); n     } n     year /= 10; n     while (year < 1989 + f) { n       year += x; n     } n     cout << year << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long read() { n   long long f = 1, x = 0; n   char ss = getchar(); n   while (ss <  0  || ss >  9 ) { n     if (ss ==  - ) f = -1; n     ss = getchar(); n   } n   while (ss >=  0  && ss <=  9 ) { n     x = x * 10 + ss -  0 ; n     ss = getchar(); n   } n   return f * x; n } n long long seed, vmax; n long long rnd() { n   long long res = seed; n   seed = (seed * 7 + 13) % 1000000007; n   return res; n } n const int maxn = 100010; n int n, m; n long long a[maxn]; n struct node { n   int ll, rr; n   mutable long long val; n   bool operator<(const node& tt) const { return ll < tt.ll; } n }; n set<node> st; n long long qpow(long long a, long long k, long long p) { n   long long res = 1; n   a %= p; n   while (k > 0) { n     if (k & 1) res = (res * a) % p; n     a = (a * a) % p; n     k >>= 1; n   } n   return res; n } n set<node>::iterator split(int pos) { n   set<node>::iterator it = st.lower_bound(node{pos}); n   if (it != st.end() && it->ll == pos) return it; n   --it; n   int ll = it->ll, rr = it->rr; n   long long val = it->val; n   st.erase(it); n   st.insert(node{ll, pos - 1, val}); n   return st.insert(node{pos, rr, val}).first; n } n void assign(int ll, int rr, long long val) { n   set<node>::iterator itr = split(rr + 1), itl = split(ll); n   st.erase(itl, itr); n   st.insert(node{ll, rr, val}); n } n void add(int ll, int rr, long long val) { n   set<node>::iterator itr = split(rr + 1), itl = split(ll); n   for (; itl != itr; ++itl) itl->val += val; n } n long long kth(int ll, int rr, int k) { n   vector<pair<long long, int> > vec; n   set<node>::iterator itr = split(rr + 1), itl = split(ll); n   for (; itl != itr; ++itl) n     vec.push_back(pair<long long, int>(itl->val, itl->rr - itl->ll + 1)); n   sort(vec.begin(), vec.end()); n   for (vector<pair<long long, int> >::iterator it = vec.begin(); n        it != vec.end(); ++it) { n     k -= it->second; n     if (k <= 0) return it->first; n   } n   return -1; n } n long long qsum(int ll, int rr, long long x, long long y) { n   long long res = 0; n   set<node>::iterator itr = split(rr + 1), itl = split(ll); n   for (; itl != itr; ++itl) n     res += (qpow(itl->val, x, y) * ((itl->rr - itl->ll + 1) % y)) % y, res %= y; n   return res; n } n int main() { n   n = read(); n   m = read(); n   seed = read(); n   vmax = read(); n   for (int i = 1; i <= n; ++i) { n     a[i] = (rnd() % vmax) + 1; n     st.insert(node{i, i, a[i]}); n   } n   for (int i = 1; i <= m; ++i) { n     int op = (rnd() % 4) + 1; n     int ll = (rnd() % n) + 1, rr = (rnd() % n) + 1; n     long long x, y; n     if (ll > rr) swap(ll, rr); n     if (op == 3) n       x = (rnd() % (rr - ll + 1)) + 1; n     else n       x = (rnd() % vmax) + 1; n     if (op == 4) y = (rnd() % vmax) + 1; n     if (op == 1) n       add(ll, rr, x); n     else if (op == 2) n       assign(ll, rr, x); n     else if (op == 3) n       printf( %lld n , kth(ll, rr, x)); n     else if (op == 4) n       printf( %lld n , qsum(ll, rr, x, y)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int fw[100005], s[100005], t[100005]; n bool r[100005], v[100005]; n int res = 0; n void dfs(int i) { n   v[i] = true; n   if (v[fw[i]]) { n     t[res] = i; n   } else { n     dfs(fw[i]); n   } n } n int main() { n   int n = 0; n   ios::sync_with_stdio(false); n   memset(fw, 0, sizeof(fw)); n   memset(r, true, sizeof(r)); n   memset(v, false, sizeof(v)); n   cin >> n; n   int num = 0; n   for (int i = 1; i <= n; ++i) { n     cin >> fw[i]; n     r[fw[i]] = false; n   } n   for (int i = 1; i <= n; ++i) { n     if (r[i] && !v[i]) { n       ++res; n       s[res] = i; n       dfs(i); n     } n   } n   int cur = res; n   for (int i = 1; i <= n; ++i) { n     if (!v[i]) { n       ++res; n       s[res] = i; n       dfs(i); n     } n   } n   if (cur == 0 && res == 1) { n     cout << 0 << endl; n   } else { n     cout << res << endl; n     for (int i = 1; i <= res; ++i) { n       cout << t[i] <<     << s[i % res + 1] << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class F, class T> n T convert(F input, int width = 0, int prec = -1) { n   stringstream A; n   T res; n   if (prec > -1) A << fixed << setprecision(prec); n   A << setw(width) << setfill( 0 ) << input; n   A >> res; n   return res; n } n int main() { n   string nm; n   cin >> nm; n   int ln = nm.length() - 1; n   int dt = find((nm).begin(), (nm).end(),  . ) - nm.begin(); n   if (nm[dt - 1] !=  9  && nm[dt + 1] <  5 ) { n     cout << nm.substr(0, dt); n     return 0; n   } n   if (nm[dt - 1] !=  9  && nm[dt + 1] >=  5 ) { n     string res = nm.substr(0, dt); n     res[int((res).size()) - 1] = res[int((res).size()) - 1] + 1; n     cout << res; n     return 0; n   } n   cout <<  GOTO Vasilisa. ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string board[8]; n int main() { n   for (int i = 0; i < 8; ++i) cin >> board[i]; n   const char* s =  WB ; n   for (int i = 0; i < 8; ++i) { n     int c = board[i][0] ==  B ; n     for (int j = 1; j < 8; ++j) { n       if (board[i][j] != s[(c + j) % 2]) { n         cout <<  NO n ; n         return 0; n       } n     } n   } n   cout <<  YES n ; n   return 0; n } n


#include <bits/stdc++.h> n std::mt19937 rng( n     (int)std::chrono::steady_clock::now().time_since_epoch().count()); n const int ms = 101; n long long memo[ms][ms], memo2[ms][ms][ms]; n long long a[ms]; n std::string str; n long long dp(int l, int r); n long long dp2(int l, int r, int x) { n   if (r - l <= 0) return 0; n   long long &ans = memo2[l][r][x]; n   if (ans != -1) return ans; n   ans = dp(l + 1, r) + a[x]; n   for (int i = l + 1; i < r; i++) { n     if (str[i] == str[l]) { n       ans = std::max(ans, dp(l + 1, i) + dp2(i, r, x + 1)); n     } n   } n   return ans; n } n long long dp(int l, int r) { n   if (r - l <= 0) n     return 0; n   else if (r - l == 1) n     return a[1]; n   long long &ans = memo[l][r]; n   if (ans != -1) return ans; n   ans = 0; n   for (int m = l; m < r; m++) { n     ans = std::max(ans, dp2(m, r, 1) + dp(l, m)); n   } n   return ans; n } n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(NULL); n   int n; n   std::cin >> n >> str; n   for (int i = 0; i < n; i++) { n     std::cin >> a[i + 1]; n   } n   memset(memo, -1, sizeof memo); n   memset(memo2, -1, sizeof memo2); n   std::cout << dp(0, n) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, i, x, b[110000]; n void modify(int x, int y) { n   for (; x <= n; x += x & -x) b[x] = ((b[x]) > (y) ? (b[x]) : (y)); n } n int get(int x) { n   int ans = 0; n   for (; x; x -= x & -x) ans = ((ans) > (b[x]) ? (ans) : (b[x])); n   return ans; n } n int main() { n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d , &x); n     modify(x, get(x) + 1); n   } n   printf( %d n , get(n)); n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, c, d, arr[200001], arr2[200001], e, f, g, mn = 1e9, ans, sum, ct, t, n                                                     ct2, mx, sum2; n bool kk; n int main() { n   cin >> a; n   for (int i = 0; i < a; i++) { n     cin >> arr[i]; n     sum += arr[i]; n   } n   for (int i = 0; i < a; i++) { n     sum2 += arr[i]; n     if (sum % 2) { n       if (sum2 >= sum / 2 + 1) { n         cout << i + 1; n         return 0; n       } n     } else { n       if (sum2 >= sum / 2) { n         cout << i + 1; n         return 0; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int x[5], y[5]; n int matr[5005][5005]; n inline void err() { n   for (int i = 0; i <= 5000; ++i) n     for (int j = 0; j <= 5000; ++j) matr[i][j] = -1; n } n inline void fil(int i, int j, int ii, int jj, int val) { n   for (int a = i; a <= ii; ++a) n     for (int b = j; b <= jj; ++b) matr[a][b] = val; n } n int main() { n   ios ::sync_with_stdio(false); n   int a, b, c, d, e, f; n   cin >> a >> b >> c >> d >> e >> f; n   int S = a * b + c * d + e * f; n   int rad = (int)sqrt((double)S); n   if (rad * rad != S) { n     cout << -1; n     exit(0); n   } n   x[0] = a; n   y[0] = b; n   x[1] = c; n   y[1] = d; n   x[2] = e; n   y[2] = f; n   int lim = 1 << 3; n   lim--; n   for (int i = 0; i <= lim; ++i) { n     for (int j = 0; j <= 3; ++j) n       if (i & (1 << j)) swap(x[j], y[j]); n     err(); n     for (int masca = 0; masca <= lim; ++masca) { n       if (__builtin_popcount(masca) < 2) continue; n       int xx1 = 0; n       int m = 0; n       m ^= 1; n       m ^= 2; n       m ^= 4; n       int ymax = 0; n       int ymin = 1 << 30; n       int tx = 0; n       int ty = 0; n       for (int j = 0; j <= 2; ++j) n         if (masca & (1 << j)) { n           xx1 += x[j]; n           m ^= (1 << j); n           ymax = max(ymax, y[j]); n           ymin = min(ymin, y[j]); n         } else { n           tx = x[j]; n           ty = y[j]; n         } n       if (ty != ymax - ymin and ty != rad - ymax) continue; n       if (xx1 == rad) { n         err(); n         int lastx = 1; n         int lasty = 1; n         for (int j = 0; j <= 3; ++j) n           if (masca & (1 << j)) { n             fil(lastx, lasty, lastx + x[j] - 1, lasty + y[j] - 1, j); n             lastx = lastx + x[j]; n             lasty = 1; n           } n         int muie = -1; n         while (m) { n           ++muie; n           m >>= 1; n         } n         cout << rad <<   n ; n         for (int i = 1; i <= rad; ++i) { n           for (int j = 1; j <= rad; ++j) { n             if (matr[i][j] == -1) matr[i][j] = muie; n             cout << (char)(matr[i][j] +  A ); n           } n           cout <<   n ; n         } n         exit(0); n       } n     } n     for (int j = 0; j <= 3; ++j) n       if (i & (1 << j)) swap(x[j], y[j]); n   } n   cout << -1; n   exit(0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n[101]; n vector<int> cost; n int main() { n   int N, B; n   cin >> N >> B; n   int o, e; n   o = e = 0; n   for (int i = 0; i < N; i++) { n     cin >> n[i]; n   } n   for (int i = 0; i < N; i++) { n     if (n[i] % 2) n       o++; n     else n       e++; n     if (o == e) { n       if (i < N - 1) { n         cost.push_back(abs(n[i] - n[i + 1])); n       } n     } n   } n   if (o != e) { n     cout << 0 << endl; n   } else { n     sort(cost.begin(), cost.end()); n     int i = 1, tot = 0; n     for (; i <= cost.size(); i++) { n       tot += cost[i - 1]; n       if (tot > B) break; n     } n     cout << i - 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int root, n; n vector<int> mp[200605]; n int Dfs(int u, int from) { n   set<int> s; n   for (int i = 0; i < mp[u].size(); i++) { n     int v = mp[u][i]; n     if (v == from) continue; n     int tmp = Dfs(v, u); n     if (tmp == -1) return -1; n     s.insert(tmp + 1); n   } n   if (s.size() == 0) return 0; n   if (s.size() == 1) return *s.begin(); n   if (s.size() == 2) { n     if (from == -1) { n       int sum = 0; n       for (set<int>::iterator it = s.begin(); it != s.end(); it++) { n         sum += *it; n       } n       return sum; n     } else { n       root = u; n       return -1; n     } n   } n   return -1; n } n int main() { n   while (~scanf( %d , &n)) { n     for (int i = 1; i <= n; i++) mp[i].clear(); n     for (int i = 1; i <= n - 1; i++) { n       int x, y; n       scanf( %d%d , &x, &y); n       mp[x].push_back(y); n       mp[y].push_back(x); n     } n     root = 1; n     int ans = Dfs(root, -1); n     if (ans == -1 && root != 1) ans = Dfs(root, -1); n     while (ans % 2 == 0) ans /= 2; n     printf( %d n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long dx[] = {1, 0, -1, 0}; n long long dy[] = {0, 1, 0, -1}; n long long gcd(long long x, long long y) { n   if (y == 0) n     return x; n   else n     return gcd(y, x % y); n } n long long expo(long long n, long long m, long long p) { n   long long r = 1; n   n = n % p; n   while (m > 0) { n     if (m % 2) r = (r * n) % p; n     n = (n * n) % p; n     m = m / 2; n   } n   return r % p; n } n bool isPrime(long long n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (long long i = 5; i * i <= n; i = i + 6) n     if (n % i == 0 || n % (i + 2) == 0) return false; n   return true; n } n vector<long long> z_function(string s) { n   long long n = (long long)s.length(); n   vector<long long> z(n); n   for (long long i = 1, l = 0, r = 0; i < n; ++i) { n     if (i <= r) z[i] = min(r - i + 1, z[i - l]); n     while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i]; n     if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; n   } n   return z; n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n, m; n   cin >> n >> m; n   long long a[n], b[m]; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   long long ans = 0; n   for (long long i = 0; i < m; i++) { n     cin >> b[i]; n   } n   long long j = 0; n   for (long long i = 0; i < n; i++) { n     if (a[i] <= b[j]) { n       ans++; n       j++; n     } n     if (j == m) break; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 14050; n template <class t> n inline void read(t &s) { n   s = 0; n   register int f = 1; n   register char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) s = (s << 3) + (s << 1) + (c ^ 48), c = getchar(); n   s *= f; n   return; n } n template <class t, class... A> n inline void read(t &x, A &...a) { n   read(x); n   read(a...); n } n template <class t> n inline void write(t x) { n   if (x < 0) putchar( - ), x = -x; n   int buf[21], top = 0; n   while (x) buf[++top] = x % 10, x /= 10; n   if (!top) buf[++top] = 0; n   while (top) putchar(buf[top--] ^  0 ); n   return; n } n inline void setIn(string s) { n   freopen(s.c_str(),  r , stdin); n   return; n } n inline void setOut(string s) { n   freopen(s.c_str(),  w , stdout); n   return; n } n inline void setIO(string s =   ) { n   setIn(s +  .in ); n   setOut(s +  .out ); n   return; n } n template <class t> n inline bool checkmin(t &x, t y) { n   if (x > y) { n     x = y; n     return 1; n   } n   return 0; n } n template <class t> n inline bool checkmax(t &x, t y) { n   if (x < y) { n     x = y; n     return 1; n   } n   return 0; n } n inline int lowbit(int x) { return x & (-x); } n int s[2][MaxN], m[2]; n int n; n bool vis[2][MaxN], win[2][MaxN]; n int cnt[2][MaxN]; n inline void dfs(int k, int u) { n   if (!win[k][u]) n     for (int i = 1; i <= m[k ^ 1]; ++i) { n       register int nxt = (u - s[k ^ 1][i] - 1 + n) % n + 1; n       if (!vis[k ^ 1][nxt]) n         win[k ^ 1][nxt] = vis[k ^ 1][nxt] = true, dfs(k ^ 1, nxt); n     } n   else n     for (int i = 1; i <= m[k ^ 1]; ++i) { n       register int nxt = (u - s[k ^ 1][i] - 1 + n) % n + 1; n       if (!vis[k ^ 1][nxt] && ++cnt[k ^ 1][nxt] >= m[k ^ 1]) n         vis[k ^ 1][nxt] = true, dfs(k ^ 1, nxt); n     } n   return; n } n signed main(void) { n   read(n); n   read(m[0]); n   for (int i = 1; i <= m[0]; ++i) read(s[0][i]); n   read(m[1]); n   for (int i = 1; i <= m[1]; ++i) read(s[1][i]); n   vis[0][1] = vis[1][1] = true; n   dfs(1, 1); n   dfs(0, 1); n   for (int i = 0; i < 2; ++i, puts(  )) n     for (int j = 2; j <= n; ++j) n       printf( %s  , (vis[i][j] ? (win[i][j] ?  Win  :  Lose ) :  Loop )); n   return 0; n } n


/* n  Solution by:-  n  --------------Sannidhay Vashal n  ----------------NIT SRINAGAR n   n */ n #include<iostream> n #include<vector> n #include<algorithm> n #include<stack> n #include<queue> n #include<map> n #include<math.h> n #include<climits> n #include<set> n #include<cstring> n #include<unordered_map> n #include <cstdlib>  n #include<cmath> n   n #define fio ios_base::sync_with_stdio(false);cin.tie(NULL) n #define ll            long long int n #define ull           unsigned long long int n #define cinll(x)      ll x;cin>>x; n #define cini(x)       int x;cin>>x; n #define cins(x)       string x;cin>>x; n #define vect(x)       vector<ll> x; n #define vect1(x)      vector<ll> x;x.push_back(0); n #define pb(x)         push_back(x) n #define mp(x,y)       make_pair(x,y) n #define MAX           1e18 n #define MIN           -1000000007 n #define MOD           1000000007 n  n using namespace std; n  n bool compare(pair<ll,ll> a,pair<ll,ll> b){ n   return a.second<b.second; n } n  n int main(){ n   cinll(t); n   while(t--){ n     cinll(n); n     vector<ll> arr; n     unordered_map<ll,ll> mapit; n     vector<pair<ll,ll>> ans; n     for(ll i=0;i<n;i++) { n       cinll(x); n       arr.pb(x); n       mapit[x]++; n     } n     ll anss = -1; n     for(ll i=0;i<n;i++){ n       if(mapit[arr[i]]==1) ans.pb(mp(i+1,arr[i])); n     } n     sort(ans.begin(),ans.end(),compare); n     if(ans.size()>=1) anss = ans[0].first; n     cout<<anss<<  n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ans, n, m, a, r, c, sum, sums[1000002], sumsR[1000002], n     sumsC[1000002], sumsR2[1000002], sumsC2[1000002]; n const long long p = 998244353; n struct el { n   long long x, r, c; n }; n bool operator<(el x, el y) { return x.x < y.x; } n el arr[1000002]; n long long modu(long long am, long long bm) { n   long long ansm = am, p2 = p - 2, h = bm; n   while (p2 > 0) { n     if ((p2 & 1) != 0) ansm = (ansm * h) % p; n     h = (h * h) % p; n     p2 /= 2; n   } n   return ansm; n } n int main() { n   cin >> n >> m; n   for (long long i = 1; i <= n * m; i++) { n     cin >> a; n     arr[i].x = a; n     arr[i].r = (i - 1) / m + 1; n     arr[i].c = (i - 1) % m + 1; n   } n   cin >> r >> c; n   sort(arr + 1, arr + n * m + 1); n   sums[1] = 0; n   sumsR[1] = arr[1].r % p; n   sumsC[1] = arr[1].c % p; n   sumsR2[1] = (arr[1].r * arr[1].r) % p; n   sumsC2[1] = (arr[1].c * arr[1].c) % p; n   a = 0; n   for (int i = 2; i <= n * m; i++) { n     if (arr[i].x != arr[i - 1].x) a = i - 1; n     sum = 0; n     sum += sums[a] % p; n     sum %= p; n     sum += (a * arr[i].r * arr[i].r) % p; n     sum %= p; n     sum += (a * arr[i].c * arr[i].c) % p; n     sum %= p; n     sum += (sumsR2[a]) % p; n     sum %= p; n     sum += (sumsC2[a]) % p; n     sum %= p; n     sum -= (2 * arr[i].r * sumsR[a]) % p; n     sum %= p; n     if (sum < 0) sum += p; n     sum -= (2 * arr[i].c * sumsC[a]) % p; n     sum %= p; n     if (sum < 0) sum += p; n     sum = modu(sum, a); n     if (arr[i].r == r && arr[i].c == c) { n       ans = sum; n       break; n     } n     sumsR[i] = (sumsR[i - 1] + arr[i].r) % p; n     sumsC[i] = (sumsC[i - 1] + arr[i].c) % p; n     sumsR2[i] = (sumsR2[i - 1] + arr[i].r * arr[i].r) % p; n     sumsC2[i] = (sumsC2[i - 1] + arr[i].c * arr[i].c) % p; n     sums[i] = (sums[i - 1] + sum); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long cnt, n, pre, i, j, k, l, x, y, z, a[310][310]; n   cin >> n; n   pre = 0; n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= n; j++) { n       cin >> a[i][j]; n       pre += a[i][j]; n     } n   } n   cin >> k; n   while (k--) { n     cin >> x >> y >> z; n     if (a[x][y] <= z) n       cout << pre / 2 <<    ; n     else { n       cnt = 0; n       a[x][y] = z; n       a[y][x] = z; n       for (i = 1; i <= n; i++) { n         a[x][i] = min(a[x][i], a[x][y] + a[y][i]); n         a[i][x] = a[x][i]; n       } n       for (i = 1; i <= n; i++) { n         a[y][i] = min(a[y][i], a[y][x] + a[x][i]); n         a[i][y] = a[y][i]; n       } n       for (i = 1; i <= n; i++) n         for (j = 1; j <= n; j++) n           a[i][j] = min(min(a[i][j], a[i][x] + a[x][j]), a[i][y] + a[y][j]); n       for (i = 1; i <= n; i++) n         for (j = 1; j <= n; j++) cnt += a[i][j]; n       pre = cnt; n       cout << pre / 2 <<    ; n     } n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NM = 1000005; n const int Mod = 1000000007; n int N, m, n, p26 = 0, pi[NM], i, x, y, b[NM]; n char a[NM]; n bool kmp[NM]; n long long put(long long a, int b) { n   if (!b) return 1; n   if (b & 1) return a * put(a * a % Mod, b >> 1) % Mod; n   return put(a * a % Mod, b >> 1); n } n void KMP() { n   int i, k = 0; n   pi[1] = 0; n   for (i = 2; i <= n; ++i) { n     while (k && a[i] != a[k + 1]) k = pi[k]; n     if (a[i] == a[k + 1]) ++k; n     pi[i] = k; n   } n   while (k) { n     kmp[k] = 1; n     k = pi[k]; n   } n } n int main() { n   scanf( %d %d n , &N, &m); n   gets(a + 1); n   n = strlen(a + 1); n   KMP(); n   if (!m) { n     printf( %I64d n , put(26LL, N)); n     return 0; n   } n   for (i = 1; i <= m; ++i) scanf( %d , &b[i]); n   p26 += b[1] - 1; n   for (i = 1; i < m; ++i) { n     x = b[i] + n - 1; n     y = b[i + 1]; n     if (x < y) n       p26 += y - x - 1; n     else if (!kmp[x - y + 1]) { n       printf( 0 n ); n       return 0; n     } n   } n   p26 += N; n   p26 -= b[i] + n - 1; n   printf( %I64d n , put(26LL, p26)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 100; n char s[N]; n int cost(char a, char b) { return min(abs(b - a), 26 - abs(b - a)); } n int main() { n   int ans = 1e9; n   int n; n   cin >> n; n   scanf( %s , s + 1); n   for (int i = 1; i <= n; i++) { n     int j = i + 1, k = j + 1, t = k + 1; n     if (t > n) continue; n     ans = min(ans, cost(s[i],  A ) + cost(s[j],  C ) + cost(s[k],  T ) + n                        cost(s[t],  G )); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n int m, h, t, n, i, j, k; n int a[200000], q[200000]; n long long sum[200000], ans; n long long pas[200000]; n long long get(int i, int j) { return sum[j] - sum[i] + a[i]; } n int main() { n   cin >> n; n   for (i = 0; i < n; i++) cin >> a[i]; n   cin >> m; n   for (i = 0; i < m; i++) cin >> q[i]; n   sort(a, a + n); n   sum[0] = a[0]; n   for (i = 1; i < n; i++) { n     sum[i] = sum[i - 1]; n     sum[i] += a[i]; n   } n   for (i = 1; i <= 100000; i++) { n     ans = 0; n     long long num = 0; n     long long freespace = i; n     for (j = n - 2; j >= 0;) { n       if (freespace > n * 10) freespace = n * 10; n       num++; n       long long l = j - freespace + 1; n       if (l < 0) l = 0; n       ans += num * get(l, j); n       j = j - freespace; n       freespace = freespace * i; n     } n     pas[i] = ans; n   } n   for (i = 0; i < m; i++) cout << pas[q[i]] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target(   n      sse,sse2,sse3,sse4,fma4,ssse3,popcnt,abm,aes,pclmul,cld,inline-all-stringops ) n #pragma comment(linker,  /STACK:66777216 ) n using namespace std; n static unsigned rnd() { n   static int second = 124612741; n   second ^= second << 13; n   second ^= second >> 17; n   return second ^= second << 5; n } n inline int fpow(int a, int st, int mod) { n   int ans = 1; n   while (st) { n     if (st % 2) ans = (long long)ans * a % mod; n     a = (long long)a * a % mod; n     st /= 2; n   } n   return ans; n } n int invmod(int first, int second) { n   int a = 1, b = 0, k; n   for (; second; swap(first, second), swap(a, b)) { n     k = first / second; n     first -= second * k; n     a -= b * k; n   } n   return a + max(b, 0); n } n inline int calcBits(int a) { return __builtin_popcount(a); } n struct Hash { n   Hash() : x1(0), x2(0) {} n   Hash(const int& x1, const int& x2) : x1(x1), x2(x2) {} n   int x1, x2; n   static int mod1, mod2; n   static int p1, p2; n   static int invp1, invp2; n   static bool initCalled; n   static const Hash ONE, ZERO, INVALID; n   static Hash UNIT, INV_UNIT; n   static bool prime(int a) { n     if (a == 1) return 0; n     for (int i = 2; i * i <= a; ++i) { n       if (a % i == 0) return false; n     } n     return true; n   } n   static int pickModHelper(int start, int bad) { n     int mn = 1000000000, best = -1; n     for (int _n(start + 1000), i(start); i <= _n; i++) { n       if (i == bad || !prime(i)) continue; n       int cur = calcBits(i); n       if (cur < mn) { n         mn = cur; n         best = i; n       } n     } n     return best; n   } n   static void init() { n     if (initCalled) return; n     initCalled = true; n     mod1 = 1 << 30; n     while (!prime(mod1)) ++mod1; n     mod2 = pickModHelper((1 << 29) + rnd() % 1000000, mod1); n     invp1 = invmod(p1, mod1); n     invp2 = invmod(p2, mod2); n     INV_UNIT = Hash(invp1, invp2); n   } n   inline Hash& operator+=(const Hash& other) { n     x1 += other.x1; n     if (x1 >= Hash::mod1) x1 -= Hash::mod1; n     x2 += other.x2; n     if (x2 >= Hash::mod2) x2 -= Hash::mod2; n     return *this; n   } n   inline Hash& operator-=(const Hash& other) { n     x1 -= other.x1; n     if (x1 < 0) x1 += Hash::mod1; n     x2 -= other.x2; n     if (x2 < 0) x2 += Hash::mod2; n     return *this; n   } n   inline Hash& operator*=(const Hash& other) { n     x1 = (long long)x1 * other.x1 % Hash::mod1; n     x2 = (long long)x2 * other.x2 % Hash::mod2; n     return *this; n   } n   inline Hash& operator*=(const int& other) { n     x1 = (long long)x1 * other % Hash::mod1; n     x2 = (long long)x2 * other % Hash::mod2; n     return *this; n   } n   inline Hash& operator^=(const int& power) { n     int st = power; n     Hash ans(1, 1); n     while (st) { n       if (st % 2) ans *= *this; n       x1 = (long long)x1 * x1 % Hash::mod1; n       x2 = (long long)x2 * x2 % Hash::mod2; n       st /= 2; n     } n     return *this; n   } n   inline bool operator==(const Hash& other) const { n     return x1 == other.x1 && x2 == other.x2; n   } n }; n bool Hash::initCalled = false; n int Hash::mod1 = 0; n int Hash::mod2 = 0; n int Hash::p1 = 23561; n int Hash::p2 = 10000019; n int Hash::invp1 = -1; n int Hash::invp2 = -1; n const Hash Hash::ONE(1, 1); n const Hash Hash::ZERO(0, 0); n const Hash Hash::INVALID(-1, -1); n Hash Hash::UNIT(Hash::p1, Hash::p2); n Hash Hash::INV_UNIT(-1, -1); n inline Hash operator+(const Hash& a, const Hash& b) { n   Hash ans(a.x1 + b.x1, a.x2 + b.x2); n   if (ans.x1 >= Hash::mod1) ans.x1 -= Hash::mod1; n   if (ans.x2 >= Hash::mod2) ans.x2 -= Hash::mod2; n   return ans; n } n inline Hash operator-(const Hash& a, const Hash& b) { n   Hash ans(a.x1 - b.x1, a.x2 - b.x2); n   if (ans.x1 < 0) ans.x1 += Hash::mod1; n   if (ans.x2 < 0) ans.x2 += Hash::mod2; n   return ans; n } n inline Hash operator*(const Hash& a, const Hash& b) { n   return Hash((long long)a.x1 * b.x1 % Hash::mod1, n               (long long)a.x2 * b.x2 % Hash::mod2); n } n inline Hash operator*(const Hash& a, const int& b) { n   return Hash((long long)a.x1 * b % Hash::mod1, n               (long long)a.x2 * b % Hash::mod2); n } n inline Hash operator^(const Hash& a, const int& power) { n   int st = power; n   Hash ans(1, 1), cur(a); n   while (st) { n     if (st % 2) ans *= cur; n     cur *= cur; n     st /= 2; n   } n   return ans; n } n inline Hash fpow(const Hash& a, const int& power) { return a ^ power; } n inline Hash invmod(const Hash& a) { n   return Hash(invmod(a.x1, Hash::mod1), invmod(a.x2, Hash::mod2)); n } n template <class H> n struct HashString : public string { n   HashString() : string() { precalc(0); } n   HashString(const HashString& other) : string(other) { precalc(size()); } n   HashString(HashString&& other) n       : hh(std::move(other.hh)), string(std::move(other)) { n     precalc(size()); n   } n   HashString(const string& other) : string(other) { n     precalc(size()); n     build(); n   } n   HashString(string&& other) : string(std::move(other)) { n     precalc(size()); n     build(); n   } n   HashString(const char* other) : string(other) { n     precalc(size()); n     build(); n   } n   inline void clear() { n     hh.clear(); n     string::clear(); n   } n   inline static H getPow(int p) { n     if (p < haveN) { n       return pp[p]; n     } else { n       return fpow(H::UNIT, p); n     } n   } n   inline static H getInvPow(size_t p) { n     if (p < invpp.size()) { n       return invpp[p]; n     } else { n       return fpow(H::INV_UNIT, p); n     } n   } n   inline H getInvRegister(const H& p) const { n     if (p == register_) { n       return registerValue_; n     } else { n       register_ = p; n       return registerValue_ = invmod(p); n     } n   } n   static void precalc(int N = 0) { n     Hash::init(); n     if (haveN >= N) return; n     pp.resize(N); n     invpp.resize(N); n     if (!haveN) { n       pp[0] = H::ONE; n       invpp[0] = H::ONE; n       haveN = 1; n     } n     for (int i = haveN; i < N; ++i) { n       pp[i] = pp[i - 1] * H::UNIT; n       invpp[i] = invpp[i - 1] * H::INV_UNIT; n     } n     haveN = N; n   } n   inline void build() { n     int curSize = size(); n     if (haveN <= curSize) { n       precalc(curSize); n     } n     for (int i = hh.size(); i < curSize; ++i) { n       H pr = i ? hh.back() : H::ZERO; n       hh.push_back(pr + getPow(i) * (*this)[i]); n     } n   } n   inline H getHash(int pos, int len) const { n     if (len <= 0) return H::ZERO; n     const int S = size(); n     if (pos + len - 1 < S) { n       H ans = hh[pos + len - 1]; n       if (pos) { n         ans -= hh[pos - 1]; n         ans *= getInvPow(pos); n       } n       return ans; n     } else { n       H head = hh.back(); n       if (pos) { n         head -= hh[pos - 1]; n         head *= getInvPow(pos); n       } n       int full = -1, en = -1; n       H mid = H::ZERO; n       int midlen = 0; n       if (len <= S) { n         en = pos + len - 1 - S; n         H tail = hh[en]; n         full = 0; n         return head + tail * getPow(S - pos); n       } else { n         en = (pos + len - 1) % S; n         H tail = hh[en]; n         full = (len - (S - pos) - (en + 1)) / S; n         mid = hh.back() * (H::ONE - getInvPow(full * S)) * n               getInvRegister(H::ONE - getInvPow(S)); n         return head + mid * getPow(S - pos) + tail * getPow(len - (en + 1)); n       } n     } n   } n   inline HashString& operator+=(const char& ch) { n     string::operator+=(ch); n     build(); n     return *this; n   } n   inline HashString& operator+=(const HashString& other) { n     string::operator+=(other); n     build(); n     return *this; n   } n   inline bool operator==(const HashString& other) const { n     return hh.back() == other.hh.back(); n   } n   inline bool operator<(const HashString& other) const { n     int s1 = size(); n     int s2 = other.size(); n     const int S = min(s1, s2); n     int l = 0, r = S + 1; n     while (r - l > 1) { n       int xx = (r + l) / 2; n       if (getHash(0, xx) == other.getHash(0, xx)) n         l = xx; n       else n         r = xx; n     } n     char c1 = 0, c2 = 0; n     if (l < s1) c1 = (*this)[l]; n     if (l < s2) c2 = other[l]; n     return c1 < c2; n   } n   inline bool operator<=(const HashString& other) const { n     return (*this == other) || (*this < other); n   } n   inline bool operator>(const HashString& other) const { n     return !(*this == other) && !(*this < other); n   } n   inline bool operator>=(const HashString& other) const { n     return !(*this < other); n   } n   inline bool operator!=(const HashString& other) const { n     return !(*this == other); n   } n   inline void reserve(int N) { n     hh.reserve(N); n     string::reserve(N); n   } n   vector<H> hh; n   static vector<H> pp, invpp; n  n  private: n   mutable H register_, registerValue_; n   static int haveN; n }; n template <> n int HashString<Hash>::haveN = 0; n template <> n vector<Hash> HashString<Hash>::pp = vector<Hash>(); n template <> n vector<Hash> HashString<Hash>::invpp = vector<Hash>(); n template <class H, bool cyclic = true> n struct HashStringPiece { n   explicit HashStringPiece(const HashString<H>& s, int pos, int len) n       : s(s), pos(pos), len(len), hash(H::INVALID) {} n   inline H getHash() const { n     if (hash == H::INVALID) hash = s.getHash(pos, len); n     return hash; n   } n   inline H getPartialHash(int needLen) const { return s.getHash(pos, needLen); } n   inline char getChar(int i) const { n     i += pos; n     const int S = s.size(); n     if (i < S) return s[i]; n     if (!cyclic) return 0; n     i -= S; n     if (i < S) return s[i]; n     return s[i % S]; n   } n   const HashString<H>& s; n   const int pos, len; n   mutable H hash; n }; n template <class H, class T> n char getHSPRangeChar(T beg, T en, int pos) { n   for (T it = beg; it != en; ++it) { n     if (pos < it->len) { n       return it->getChar(pos); n     } n     pos -= it->len; n   } n   return 0; n } n template <class H, class T> n H getHSPRangeHash(T beg, T en, int len) { n   H ans = H::ZERO; n   int cur = 0; n   for (T it = beg; it != en; ++it) { n     if (len >= it->len) { n       ans += it->getHash() * it->s.getPow(cur); n       cur += it->len; n       len -= it->len; n     } else { n       ans += it->getPartialHash(len) * it->s.getPow(cur); n       break; n     } n   } n   return ans; n } n int TB, IT; n template <class H, class T> n inline int compareHSPRanges(T beg1, T en1, int len1, T beg2, T en2, int len2) { n   if (beg1 == en1 && beg2 == en2) return 0; n   if (beg1 == en1) return -1; n   if (beg2 == en2) return 1; n   int l = 0, r = min(len1, len2) + 1; n   while (r - l > 1) { n     int xx = (r + l) / 2; n     auto h1 = getHSPRangeHash<H, T>(beg1, en1, xx); n     auto h2 = getHSPRangeHash<H, T>(beg2, en2, xx); n     if (h1 == h2) n       l = xx; n     else n       r = xx; n   } n   char c1 = getHSPRangeChar<H, T>(beg1, en1, l); n   char c2 = getHSPRangeChar<H, T>(beg2, en2, l); n   if (c1 < c2) n     return -1; n   else if (c1 == c2) n     return 0; n   else n     return 1; n } n template <class H, class T> n inline int compareHSPRanges(T beg1, T en1, T beg2, T en2) { n   int len1 = 0; n   for (T it = beg1; it != en1; ++it) { n     len1 += it->len; n   } n   int len2 = 0; n   for (T it = beg2; it != en2; ++it) { n     len2 += it->len; n   } n   return compareHSPRanges<H, T>(beg1, en1, len1, beg2, en2, len2); n } n int a, b, c, d, n, m, k; n char str[1000002]; n int bal[2000002]; n HashString<Hash> s; n inline string build(pair<pair<int, int>, int> a) { n   string ans =   ; n   for (int _n((a.first.first) - 1), i(0); i <= _n; i++) ans +=  ( ; n   for (int _n((n)-1), i(0); i <= _n; i++) { n     int cur = a.second + i; n     if (cur >= n) cur -= n; n     ans += str[cur]; n   } n   for (int _n((a.first.second) - 1), i(0); i <= _n; i++) ans +=  ) ; n   return ans; n } n HashString<Hash> op, cl; n inline bool better(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b, n                    const HashString<Hash>& s) { n   HashStringPiece<Hash> A[3] = {HashStringPiece<Hash>(op, 0, a.first.first), n                                 HashStringPiece<Hash>(s, a.second, n), n                                 HashStringPiece<Hash>(cl, 0, a.first.second)}; n   HashStringPiece<Hash> B[3] = {HashStringPiece<Hash>(op, 0, b.first.first), n                                 HashStringPiece<Hash>(s, b.second, n), n                                 HashStringPiece<Hash>(cl, 0, b.first.second)}; n   int t = compareHSPRanges<Hash>(A, A + 3, B, B + 3); n   if (t == -1) n     return 1; n   else n     return 0; n } n int main() { n   gets(str); n   n = (int)strlen(str); n   HashString<Hash> s(str); n   HashString<Hash>::precalc(2 * n + 1); n   op.reserve(n + 1); n   cl.reserve(n + 1); n   for (int _n((n)-1), i(0); i <= _n; i++) { n     op +=  ( ; n     cl +=  ) ; n   } n   c = 0; n   for (int _n((n)-1), i(0); i <= _n; i++) { n     if (str[i] ==  ( ) n       ++c; n     else n       --c; n   } n   bal[0] = 0; n   for (int _n((2 * n) - 1), i(0); i <= _n; i++) { n     if (i) bal[i] = bal[i - 1]; n     int cur = i; n     if (i >= n) cur = i - n; n     if (str[cur] ==  ( ) n       ++bal[i]; n     else n       --bal[i]; n   } n   int blen = 1000000000; n   pair<pair<int, int>, int> best = make_pair(make_pair(0, 0), 0); n   stack<pair<int, int> > s1, s2; n   for (int _n((n)-1), i(0); i <= _n; i++) { n     int new_element = bal[i]; n     int minima = s1.empty() ? new_element : min(new_element, s1.top().second); n     s1.push(make_pair(new_element, minima)); n   } n   for (int _n((n)-1), cyc(0); cyc <= _n; cyc++) { n     int current_minimum = -1000000000; n     if (s1.empty() || s2.empty()) n       current_minimum = s1.empty() ? s2.top().second : s1.top().second; n     else n       current_minimum = min(s1.top().second, s2.top().second); n     int p = 0; n     if (cyc) p = bal[cyc - 1]; n     int mn = current_minimum - p; n     int beg = max(0, -mn); n     int en = c + beg; n     int len = beg + en + n; n     if (len < blen || n         (len == blen && better(make_pair(make_pair(beg, en), cyc), best, s))) { n       blen = len; n       best = make_pair(make_pair(beg, en), cyc); n     } n     if (s2.empty()) { n       while (!s1.empty()) { n         int element = s1.top().first; n         s1.pop(); n         int minima = s2.empty() ? element : min(element, s2.top().second); n         s2.push(make_pair(element, minima)); n       } n     } n     s2.pop(); n     int new_element = bal[cyc + n]; n     int minima = s1.empty() ? new_element : min(new_element, s1.top().second); n     s1.push(make_pair(new_element, minima)); n   } n   string ans = build(best); n   printf( %s n , ans.c_str()); n } n


#include <bits/stdc++.h> n using namespace std; n long long read() { n   long long cc = getc(stdin); n   for (; cc <  0  || cc >  9 ;) cc = getc(stdin); n   long long ret = 0; n   for (; cc >=  0  && cc <=  9 ;) { n     ret = ret * 10 + cc -  0 ; n     cc = getc(stdin); n   } n   return ret; n } n long long power(long long num, long long g) { n   if (g == 0) return 1; n   if (g % 2 == 1) n     return (num * power((num * num) % 998244353, g / 2)) % 998244353; n   return power((num * num) % 998244353, g / 2); n } n int parent[100005]; n int siz[100005]; n int find(int index) { n   if (parent[index] == index) return index; n   return parent[index] = find(parent[index]); n } n vector<pair<int, int> > vec[100005]; n int b[100005][18], c[100005][18]; n int timer, intime[100005], outtime[100005], level[100005]; n void dfs(int index, int par, int lev) { n   level[index] = lev; n   timer++; n   intime[index] = timer; n   parent[index] = par; n   for (long long i = 0; i < vec[index].size(); i++) { n     if (vec[index][i].first == par) continue; n     c[vec[index][i].first][0] = vec[index][i].second; n     dfs(vec[index][i].first, index, lev + 1); n   } n   timer++; n   outtime[index] = timer; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, m; n   cin >> n >> m; n   pair<pair<int, int>, pair<int, int> > pa[m]; n   vector<pair<int, int> > vec1; n   for (long long i = 0; i < m; i++) { n     int u, v, w; n     cin >> u >> v >> w; n     pa[i] = make_pair(make_pair(w, i), make_pair(u, v)); n   } n   sort(pa, pa + m); n   for (long long i = 0; i < n; i++) parent[i + 1] = i + 1, siz[i + 1] = 1; n   for (long long i = 0; i < m; i++) { n     int u = pa[i].second.first; n     int v = pa[i].second.second; n     int w = pa[i].first.first; n     int g = find(u); n     int h = find(v); n     if (g == h) continue; n     if (siz[g] > siz[h]) n       siz[g] += siz[h], parent[h] = g; n     else n       siz[h] += siz[g], parent[g] = h; n     vec[u].push_back(make_pair(v, w)); n     vec[v].push_back(make_pair(u, w)); n   } n   timer = 0; n   dfs(1, 1, 1); n   for (long long i = 0; i < n; i++) { n     b[i + 1][0] = parent[i + 1]; n   } n   for (int i = 1; i <= 17; i++) { n     for (int j = 1; j <= n; j++) { n       b[j][i] = b[b[j][i - 1]][i - 1]; n       c[j][i] = max(c[j][i - 1], c[b[j][i - 1]][i - 1]); n     } n   } n   for (long long i = 0; i < n; i++) parent[i + 1] = i + 1, siz[i + 1] = 1; n   for (long long i = 0; i < m; i++) { n     int u = pa[i].second.first; n     int v = pa[i].second.second; n     int w = pa[i].first.first; n     int xyz = pa[i].first.second; n     int g = find(u); n     int h = find(v); n     if (g == h) { n       int g = 1 << 17; n       int ans = 0; n       int h = u; n       for (int j = 17; j >= 0; j--) { n         int d = b[h][j]; n         if (intime[d] <= intime[v] && outtime[d] >= outtime[v]) { n           continue; n         } n         ans = max(ans, c[h][j]); n         h = d; n       } n       while (intime[h] > intime[v] || outtime[h] < outtime[v]) { n         ans = max(ans, c[h][0]); n         h = b[h][0]; n       } n       h = v; n       for (int j = 17; j >= 0; j--) { n         int d = b[h][j]; n         if (intime[d] <= intime[u] && outtime[d] >= outtime[u]) { n           continue; n         } n         ans = max(ans, c[h][j]); n         h = d; n       } n       while (intime[h] > intime[u] || outtime[h] < outtime[u]) { n         ans = max(ans, c[h][0]); n         h = b[h][0]; n       } n       vec1.push_back(make_pair(xyz, ans)); n       continue; n     } n     if (siz[g] > siz[h]) n       siz[g] += siz[h], parent[h] = g; n     else n       siz[h] += siz[g], parent[g] = h; n     vec[u].push_back(make_pair(v, w)); n     vec[v].push_back(make_pair(u, w)); n   } n   sort(vec1.begin(), vec1.end()); n   for (long long i = 0; i < vec1.size(); i++) cout << vec1[i].second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string st; n long n, k, ans, calc; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> k; n   cin >> st; n   for (int i = 0; i < st.size(); i++) { n     if (st[i] ==  # ) n       ++calc; n     else { n       ans = max(ans, calc); n       calc = 0; n     } n   } n   if (ans >= k) n     cout <<  NO  << endl; n   else n     cout <<  YES  << endl; n   cin.get(); n   cin.get(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 5005; n struct Point { n   double x, y, z; n }; n double dist(Point a, Point b) { n   return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) + n               (a.z - b.z) * (a.z - b.z)); n } n Point p[MAXN]; n double dis[MAXN]; n int main() { n   int n; n   while (scanf( %d , &n) != EOF) { n     for (int i = 0; i < n; i++) scanf( %lf%lf%lf , &p[i].x, &p[i].y, &p[i].z); n     for (int i = 1; i < n; i++) dis[i] = dist(p[0], p[i]); n     double ta = 100000.0; n     for (int i = 1; i < n - 1; i++) { n       for (int j = i + 1; j < n; j++) { n         double tmp = dist(p[j], p[i]); n         double td; n         if (dis[i] > dis[j]) n           td = (tmp - dis[i] + dis[j]) / 2 + dis[i]; n         else n           td = (tmp - dis[j] + dis[i]) / 2 + dis[j]; n         if (ta > td) ta = td; n       } n     } n     printf( %.6lf n , ta); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 510; n bool dp[N][N]; n int n, k; n int a[N], b[N]; n int main() { n   cin >> n >> k; n   long long sum1 = 0, sum2 = 0; n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &a[i], &b[i]); n     sum1 += a[i]; n     sum2 += b[i]; n   } n   ans = sum1 / k + sum2 / k; n   if (sum1 % k + sum2 % k < k) { n     printf( %lld n , ans); n     return 0; n   } else { n     bool flag = false; n     dp[0][0] = true; n     for (int i = 1; i <= n; i++) n       for (int j = 0; j < k; j++) n         if (dp[i - 1][j]) { n           dp[i][j] = true; n           for (int x = 1; x < k; x++) { n             if (a[i] >= x && b[i] >= k - x) dp[i][(j + x) % k] = true; n           } n         } n     for (int i = 0; i <= sum1 % k; i++) n       if (dp[n][i] && sum2 % k + i >= k) { n         flag = true; n         break; n       } n     if (flag) n       printf( %lld n , ans + 1); n     else n       printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, t; n   getline(cin, s); n   getline(cin, t); n   int x = -1, y = -1; n   for (int i = 0, j = 0; j < t.length(); j++) { n     i += s[i] == t[j]; n     if (i == s.length()) { n       x = j; n       break; n     } n   } n   for (int i = s.length() - 1, j = t.length() - 1; j >= 0; j--) { n     i -= s[i] == t[j]; n     if (i == -1) { n       y = j; n       break; n     } n   } n   if (x != -1 && y != -1 && x < y) { n     printf( %d , y - x); n   } else { n     printf( 0 ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const double eps = 1e-9; n char mapp[110][110]; n int main() { n   cin.sync_with_stdio(false); n   int n, m; n   int ans, cnt; n   int x1, x2, y1, y2; n   while (cin >> n >> m) { n     ans = 0; n     x1 = 0x3f3f3f3f, x2 = -1, y1 = 0x3f3f3f3f, y2 = -1; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         cin >> mapp[i][j]; n         if (mapp[i][j] ==  B ) { n           x1 = min(x1, i); n           y1 = min(y1, j); n           x2 = max(x2, i); n           y2 = max(y2, j); n           ans++; n         } n       } n     } n     if (x1 == 0x3f3f3f3f) { n       cout << 1 << endl; n     } else { n       cnt = max(x2 - x1, y2 - y1) + 1; n       if (cnt > n || cnt > m) { n         cout << -1 << endl; n         continue; n       } n       cout << cnt * cnt - ans << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int maxN = -1; n vector<string> str; n bool ok = false; n int main() { n   string s; n   while (getline(cin, s)) { n     maxN = max(maxN, (int)s.size()); n     str.push_back(s); n   } n   for (int i = 0; i < maxN + 2; i++) cout <<  * ; n   cout << endl; n   int n = str.size(); n   for (int i = 0; i < n; i++) { n     cout <<  * ; n     int ni = str[i].size(); n     int align = maxN - ni; n     int space = align / 2; n     if (align % 2 != 0) { n       if (ok == false) { n       } else n         space++; n     } n     for (int j = 0; j < space; j++) cout <<    ; n     cout << str[i]; n     if (align % 2 != 0) { n       if (ok == true) n         space--; n       else if (ok == false) n         space++; n     } n     for (int j = 0; j < space; j++) cout <<    ; n     cout <<  * ; n     cout << endl; n     if (align % 2) ok = !ok; n   } n   for (int i = 0; i < maxN + 2; i++) cout <<  * ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   char c = getchar(); n   long long x = 0; n   bool f = 0; n   for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45); n   for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); n   if (f) x = -x; n   return x; n } n long long n, a[300005], l[300005], r[300005]; n bool vis[300005]; n void work() { n   n = read(); n   for (register long long i = (1); i <= (n); ++i) a[i] = read(), vis[i] = 0; n   long long all = 0, now = 0, mx = 0; n   for (register long long i = (1); i <= (n); ++i) { n     if (!vis[a[i]]) vis[a[i]] = 1, l[a[i]] = i, ++all; n     r[a[i]] = i; n   } n   long long lst = -1; n   for (register long long i = (1); i <= (n); ++i) n     if (vis[i]) { n       if (!now) n         now = mx = 1, lst = i; n       else { n         if (l[i] > r[lst]) n           now++; n         else n           now = 1; n         lst = i; n         mx = max(mx, now); n       } n     } n   cout << all - mx << endl; n } n signed main() { n   long long T = read(); n   while (T--) work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T sqr(T x) { n   return x * x; n } n long double pi = 3.1415926535897932384626433832795; n long long mod = 1e9 + 7; n const int N = 1e6 + 10; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n vector<vector<long long> > e(int n) { n   vector<vector<long long> > res; n   for (int i = 0; i < n; i++) { n     vector<long long> v; n     for (int j = 0; j < n; j++) { n       if (i == j) n         v.push_back(1); n       else n         v.push_back(0); n     } n     res.push_back(v); n   } n   return res; n } n vector<vector<long long> > zero(int n, int m) { n   vector<vector<long long> > res; n   for (int i = 0; i < n; i++) { n     vector<long long> v; n     for (int j = 0; j < m; j++) { n       v.push_back(0); n     } n     res.push_back(v); n   } n   return res; n } n vector<vector<long long> > operator^(vector<vector<long long> > b, n                                      vector<vector<long long> > a) { n   int n = b.size(), m = a[0].size(), K = a.size(); n   vector<vector<long long> > c = zero(n, m); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       for (int k = 0; k < K; k++) { n         c[i][j] += b[i][k] * a[k][j]; n         c[i][j] %= mod; n       } n     } n   } n   return c; n } n vector<vector<long long> > bin_pow(vector<vector<long long> > a, long long pw) { n   int n = a.size(); n   vector<vector<long long> > res = e(n); n   while (pw) { n     if (pw & 1ll) res = res ^ a; n     a = a ^ a; n     pw >>= 1; n   } n   return res; n } n long long n, k; n int main() { n   cin >> n >> k; n   vector<vector<long long> > ans = zero(1, 16); n   ans[0][0] = 1; n   for (int i = 0; i < n; i++) { n     long long a, b, c; n     cin >> a >> b >> c; n     b = min(b, k); n     long long pw = b - a; n     vector<vector<long long> > m = zero(c + 1, c + 1); n     ans[0].resize(c + 1); n     for (int i = 0; i <= c; i++) { n       for (int j = -1; j <= +1; j++) { n         int ni = i + j; n         if (ni < 0 || ni > c) continue; n         m[i][ni] = 1; n       } n     } n     m = bin_pow(m, pw); n     ans = ans ^ m; n   } n   cout << ans[0][0]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int n; n   cin >> n; n   vector<int> scores; n   for (int i = 0; i < n; i++) { n     int temp; n     cin >> temp; n     scores.push_back(temp); n   } n   int max = scores[0]; n   int min = scores[0]; n   int count = 0; n   for (int s : scores) { n     if (s > max) { n       max = s; n       count++; n     } n     if (s < min) { n       min = s; n       count++; n     } n   } n   cout << count <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int inf = 1e9 + 7; n const long long int inf2 = inf * inf; n priority_queue<long long int, vector<long long int>, greater<long long int>> n     mnheap; n long long int gcd(long long int x, long long int y) { n   if (x == 0) return y; n   return gcd(y % x, x); n } n int is_prime(long long int x) { n   if (x < 2) return 0; n   for (long long int i = 2; i * i <= x; i++) { n     if (x % i == 0) return 0; n   } n   return 1; n } n inline long long int modpw(long long int x, long long int y, long long int z) { n   long long int res = 1; n   x = x % z; n   while (y) { n     if (y & 1) res = (res * x) % z; n     x = (x * x) % z; n     y /= 2; n   } n   return res; n } n inline long long int modinv(long long int x, long long int z) { n   return modpw(x, z - 2, z); n } n long long int lcm(long long int x, long long int y) { n   return (x * y) / gcd(x, y); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   t = 1; n   while (t--) { n     int n; n     cin >> n; n     long long int x; n     vector<long long int> vv; n     for (int i = 0; i < n; i++) { n       cin >> x; n       vv.push_back(x); n     } n     if (n == 1) { n       cout << 1 <<     << 1 << endl; n       cout << -vv[0] << endl; n       cout << 1 <<     << 1 << endl; n       cout << 0 << endl; n       cout << 1 <<     << 1 << endl; n       cout << 0 << endl; n       return 0; n     } n     cout << 1 <<     << n - 1 << endl; n     for (int i = 0; i < n - 1; i++) { n       cout << vv[i] * (n - 1) <<    ; n     } n     cout << endl; n     cout << n <<     << n << endl; n     cout << vv[n - 1] * (n - 1) << endl; n     cout << 1 <<     << n << endl; n     for (int i = 0; i < n; i++) { n       cout << -1 * vv[i] * n <<    ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 100000 + 10; n int pos[MAX], s[MAX]; n int main() { n   int n, a; n   while (cin >> n) { n     memset(pos, 0, sizeof pos); n     memset(s, -1, sizeof s); n     for (int i = 1; i <= n; ++i) { n       cin >> a; n       if (s[a] == -1) n         s[a] = 0; n       else if (s[a] == 0) n         s[a] = i - pos[a]; n       else if (s[a] > 0 && i - pos[a] != s[a]) n         s[a] = -2; n       pos[a] = i; n     } n     int sum = 0; n     for (int i = 1; i < MAX; ++i) n       if (s[i] >= 0) ++sum; n     cout << sum << endl; n     for (int i = 1; i < MAX; ++i) { n       if (s[i] >= 0) cout << i <<     << s[i] << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = -1; n int n; n int main() { n   int t; n   scanf( %d , &t); n   for (int cs = (0); cs < (int)(t); cs++) { n     scanf( %d , &n); n     printf( %d %d n , n - 1, 1); n   } n   return 0; n } n