#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops,fast-math ) n using namespace std; n const double inf = 1e121; n const double eps = 1e-10; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n long long randint(long long l, long long r) { n   long long out = rng() % (r - l + 1) + l; n   return out >= l ? out : out + r - l + 1; n } n template <typename A, typename B> n string to_string(pair<A, B> p); n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p); n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p); n string to_string(const string& s) { return     + s +    ; } n string to_string(const char* s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n string to_string(vector<bool> v) { n   bool first = true; n   string res =  { ; n   for (long long i = 0; i < static_cast<long long>(v.size()); i++) { n     if (!first) { n       res +=  , ; n     } n     first = false; n     res += to_string(v[i]); n   } n   res +=  } ; n   return res; n } n template <size_t N> n string to_string(bitset<N> v) { n   string res =   ; n   for (size_t i = 0; i < N; i++) { n     res += static_cast<char>( 0  + v[i]); n   } n   return res; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto& x : v) { n     if (!first) { n       res +=  , ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,  + to_string(p.second) +  ) ; n } n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p) { n   return  (  + to_string(get<0>(p)) +  ,  + to_string(get<1>(p)) +  ,  + n          to_string(get<2>(p)) +  ) ; n } n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p) { n   return  (  + to_string(get<0>(p)) +  ,  + to_string(get<1>(p)) +  ,  + n          to_string(get<2>(p)) +  ,  + to_string(get<3>(p)) +  ) ; n } n template <typename A, typename B, typename C, typename D, typename E> n string to_string(tuple<A, B, C, D, E> p) { n   return  (  + to_string(get<0>(p)) +  ,  + to_string(get<1>(p)) +  ,  + n          to_string(get<2>(p)) +  ,  + to_string(get<3>(p)) +  ,  + n          to_string(get<4>(p)) +  ) ; n } n void debug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   debug_out(T...); n } n template <typename T> n struct is_pair { n   static const bool value = false; n }; n template <typename T, typename U> n struct is_pair<std::pair<T, U>> { n   static const bool value = true; n }; n const long long INF = 0x3f3f3f3f3f3f3f3fll; n template <typename T> n typename enable_if<!is_integral<T>::value && !is_pair<T>::value, void>::type n read(T& x) { n   cin >> x; n } n long long read() { n   char c; n   long long out = 0, f = 1; n   for (c = getchar(); !isdigit(c) && c !=  - ; c = getchar()) { n   } n   if (c ==  - ) { n     f = -1; n     c = getchar(); n   } n   for (; isdigit(c); c = getchar()) out = (out << 3) + (out << 1) + c -  0 ; n   return out * f; n } n template <typename T> n typename enable_if<is_integral<T>::value, T>::type read(T& x) { n   char c; n   T f = 1; n   x = 0; n   for (c = getchar(); !isdigit(c) && c !=  - ; c = getchar()) { n   } n   if (c ==  - ) { n     f = -1; n     c = getchar(); n   } n   for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c -  0 ; n   return x *= f; n } n char read(char& x) { n   for (x = getchar(); isspace(x); x = getchar()) { n   } n   return x; n } n double read(double& x) { n   scanf( %lf , &x); n   return x; n } n template <typename T> n typename enable_if<!is_integral<T>::value && !is_pair<T>::value, void>::type n write(const T& x) { n   cout << x; n } n template <typename T> n typename enable_if<is_integral<T>::value, void>::type write(const T& x) { n   if (x < 0) { n     putchar( - ); n     write(-x); n     return; n   } n   if (x > 9) write(x / 10); n   putchar(x % 10 +  0 ); n } n void write(const char& x) { putchar(x); } n void write(const double& x) { printf( %.10lf , x); } n template <typename T> n typename enable_if<is_pair<T>::value, void>::type read(T& x) { n   read(x.first); n   read(x.second); n } n template <typename T> n typename enable_if<is_pair<T>::value, void>::type write(const T& x) { n   write(x.first); n   putchar(   ); n   write(x.second); n } n template <typename T, typename... Args> n void read(T& x, Args&... args) { n   read(x); n   read(args...); n } n template < n     typename OutputIt, n     typename = typename enable_if< n         is_same<output_iterator_tag, n                 typename iterator_traits<OutputIt>::iterator_category>::value || n         (is_base_of<forward_iterator_tag, typename iterator_traits<OutputIt>:: n                                               iterator_category>::value && n          !is_const<OutputIt>::value)>::type> n void read(OutputIt __first, OutputIt __last) { n   for (; __first != __last; ++__first) read(*__first); n } n template < n     typename InputIt, n     typename = typename enable_if<is_base_of< n         input_iterator_tag, n         typename iterator_traits<InputIt>::iterator_category>::value>::type> n void wts(InputIt __first, InputIt __last) { n   bool isFirst = true; n   for (; __first != __last; ++__first) { n     if (isFirst) n       isFirst = false; n     else n       putchar(   ); n     write(*__first); n   } n   putchar(  n ); n } n template < n     typename InputIt, n     typename = typename enable_if<is_base_of< n         input_iterator_tag, n         typename iterator_traits<InputIt>::iterator_category>::value>::type> n void wtb(InputIt __first, InputIt __last) { n   for (; __first != __last; ++__first) { n     write(*__first); n     putchar(  n ); n   } n } n template <typename T> n void wts(const T& x) { n   write(x); n   putchar(   ); n } n template <typename T> n void wtb(const T& x) { n   write(x); n   putchar(  n ); n } n template <typename T> n void wte(const T& x) { n   write(x); n   exit(0); n } n template <typename T, typename... Args> n void wts(const T& x, Args... args) { n   wts(x); n   wts(args...); n } n template <typename T, typename... Args> n void wtb(const T& x, Args... args) { n   wts(x); n   wtb(args...); n } n template <typename T, typename... Args> n void wte(const T& x, Args... args) { n   wts(x); n   wte(args...); n } n template <typename T1, typename T2> n inline bool up(T1& x, const T2& y) { n   return x < y ? x = y, 1 : 0; n } n template <typename T1, typename T2> n inline bool dn(T1& x, const T2& y) { n   return y < x ? x = y, 1 : 0; n } n template <typename T1, typename T2, typename T3> n inline bool inRange(const T1& x, const T2& l, const T3& r) { n   return !(x < l) && !(r < x); n } n template <typename T1, typename T2> n inline auto minOfDifferentTypes(const T1& x, const T2& y) n     -> decltype(x < y ? x : y) { n   return x < y ? x : y; n } n template <typename T1, typename T2> n inline auto maxOfDifferentTypes(const T1& x, const T2& y) n     -> decltype(x < y ? y : x) { n   return x < y ? y : x; n } n template <typename T1, typename T2, typename T3> n inline T1& madd(T1& x, const T2& y, const T3& modulo) { n   return x = (long long)(x + y + modulo) % modulo; n } n template <typename T1, typename T2, typename T3> n inline T1& mmul(T1& x, const T2& y, const T3& modulo) { n   return x = (long long)x * y % modulo; n } n template <typename T1, typename T2, typename T3> n inline long long modadd(long long x, long long y, long long modulo) { n   return (x += y) >= modulo ? x - modulo : x; n } n inline long long isinf(long long x) { return x < INF ? x : -1; } n inline void yesno(bool x) { wtb(x ?  Yes  :  No ); } n signed main() { n   long long t = read(); n   while (t--) { n     long long n = read(); n     long long k = read(); n     if (n >= k && ((n - (k - 1)) & 1)) { n       wtb( YES ); n       for (long long i = (1), iend = (k - 1); i <= iend; ++i) wts(1); n       wtb(n - k + 1); n     } else if (n >= 2 * k && !(n & 1)) { n       wtb( YES ); n       for (long long i = (1), iend = (k - 1); i <= iend; ++i) wts(2); n       wtb(n - 2 * k + 2); n     } else n       wtb( NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, tot, d[105]; n int read() { n   int _ = 0, ___ = 1; n   char __ = getchar(); n   while (__ >  9  || __ <  0 ) { n     if (__ ==  - ) ___ = -1; n     __ = getchar(); n   } n   while (__ >=  0  && __ <=  9 ) { n     _ = _ * 10 + __ -  0 ; n     __ = getchar(); n   } n   return _ * ___; n } n int get(int x) { n   int sum = 0; n   while (x) { n     sum += x % 10; n     x /= 10; n   } n   return sum; n } n int main() { n   n = read(); n   for (int i = max(n - 81, 0); i <= n; i++) { n     int x = get(i); n     if (x + i == n) d[++tot] = i; n   } n   printf( %d n , tot); n   for (int i = 1; i <= tot; i++) printf( %d  , d[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int prime(int n) { n   if (n == 1) { n     return 0; n   } n   for (int i = 2; i * i <= n; i++) { n     if (n % i == 0) { n       return 0; n     } n   } n   return 1; n } n int main() { n   int n; n   cin >> n; n   int cnt = n; n   while (!prime(cnt)) { n     cnt++; n   } n   cout << cnt << endl; n   for (int i = 1; i <= n; i++) { n     cout << i <<     << i % n + 1 << endl; n   } n   int now = 1; n   while (cnt != n) { n     cout << now <<     << n - now << endl; n     now++; n     cnt--; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n long double PI = acosl(-1); n const long long inf = 3e18; n const int nmax = 1e5 + 5; n const int MAXLG = log2(nmax) + 1; n vector<int> g[nmax]; n long long val[nmax], chein[nmax], path[nmax]; n long long dfs(int u, int p = 0) { n   chein[u] = path[u] = val[u]; n   vector<long long> mxcheins; n   for (int v : g[u]) n     if (v != p) { n       mxcheins.push_back(dfs(v, u)); n       chein[u] = max(chein[u], chein[v] + val[u]); n       path[u] = max(path[u], path[v]); n     } n   path[u] = max(path[u], chein[u]); n   sort(mxcheins.rbegin(), mxcheins.rend()); n   if (mxcheins.size() > 1) n     path[u] = max(path[u], val[u] + mxcheins[0] + mxcheins[1]); n   else if (mxcheins.size() > 0) n     path[u] = max(path[u], val[u] + mxcheins[0]); n   return chein[u]; n } n long long dfs2(int u, long long mxpath, long long mxchein, int p = 0) { n   multiset<long long> alpath; n   multiset<long long> alchein; n   for (int v : g[u]) n     if (v != p) alpath.insert(path[v]), alchein.insert(chein[v]); n   alchein.insert(mxchein), alpath.insert(mxpath); n   long long ret = mxpath + path[u]; n   for (int v : g[u]) n     if (v != p) { n       alpath.erase(alpath.find(path[v])); n       alchein.erase(alchein.find(chein[v])); n       long long mxp2 = *alpath.rbegin(), mxc2 = *alchein.rbegin() + val[u]; n       if (alchein.size() > 1) { n         long long tmp = *alchein.rbegin(); n         alchein.erase(alchein.find(tmp)); n         long long tmp2 = *alchein.rbegin(); n         mxp2 = max(mxp2, val[u] + tmp + tmp2); n         alchein.insert(tmp); n       } else if (alchein.size() > 0) { n         mxp2 = max(mxp2, val[u] + *alchein.rbegin()); n       } n       mxp2 = max(mxp2, mxc2); n       ret = max(ret, dfs2(v, mxp2, mxc2, u)); n       alpath.insert(path[v]), alchein.insert(chein[v]); n     } n   return ret; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> val[i]; n   for (int i = 1; i < n; i++) { n     int u, v; n     cin >> u >> v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   dfs(1); n   cout << dfs2(1, 0, 0); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, x, k; n   cin >> n >> k; n   vector<long long> v; n   set<long long> s; n   map<long long, long long> m; n   for (long long i = 0; i < n; i++) { n     cin >> x; n     if (m[x] == 0) { n       m[x]++; n       v.insert(v.begin(), x); n     } n     if (v.size() > k) { n       m[v.back()] = 0; n       v.pop_back(); n     } n   } n   long long a = v.size(); n   cout << min((a), k) << endl; n   for (long long i = 0; i < v.size(); i++) { n     cout << v[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int md = 1e9 + 7; n int __gcd(int a, int b) { n   if (b == 0) return a; n   return __gcd(b, a % b); n } n int poww(int a, int b) { n   int res = 1; n   while (b) { n     if (b & 1) { n       res *= a; n       res %= md; n     } n     a *= a; n     a %= md; n     b >>= 1; n   } n   return res; n } n void ainp(int arr[], int n) { n   for (int i = 1; i <= n; i++) cin >> arr[i]; n } n const int N = 1e5 + 5; n void fun() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n } n int n; n int a[N], cache[N][10]; n int dp(int idx, int cur) { n   if (idx == n) return 1; n   int &ans = cache[idx][cur]; n   if (ans != -1) return ans; n   ans = 0; n   if (a[idx + 1] > a[idx]) { n     for (int i = cur + 1; i <= 5; i++) ans |= dp(idx + 1, i); n   } else if (a[idx + 1] == a[idx]) { n     for (int i = 1; i <= 5; i++) { n       if (i == cur) continue; n       ans |= dp(idx + 1, i); n     } n   } else { n     for (int i = 1; i < cur; i++) ans |= dp(idx + 1, i); n   } n   return ans; n } n void path(int idx, int cur) { n   cout << cur <<    ; n   if (idx == n) return; n   if (a[idx + 1] > a[idx]) { n     for (int i = cur + 1; i <= 5; i++) n       if (dp(idx + 1, i)) return path(idx + 1, i); n   } else if (a[idx + 1] == a[idx]) { n     for (int i = 1; i <= 5; i++) { n       if (i == cur) continue; n       if (dp(idx + 1, i)) return path(idx + 1, i); n     } n   } else { n     for (int i = 1; i < cur; i++) n       if (dp(idx + 1, i)) return path(idx + 1, i); n   } n } n signed main() { n   fun(); n   memset(cache, -1, sizeof(cache)); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= 5; i++) { n     if (dp(1, i)) { n       path(1, i); n       return 0; n     } n   } n   cout <<  -1 ; n   return 0; n } n


#include <bits/stdc++.h> n  n using namespace std; n  n #define ar array n #define ll long long n  n const int MAX_N = 1e5 + 1; n const int m = 1e9 + 7; n const int INF = 1e9; n const ll LINF = 1e18; n  n int main() n { n  int t=1; n  cin>>t; n  int n; n  while(t--) n  { n   cin>>n; n   int A[n]; n   for(int i=0;i<n;i++)cin>>A[i]; n   int x=0,y=n-1; n   while(x<=y){ n    cout<<A[x]<<   ; n    if(x!=y)cout<<A[y--]<<   ; n    x++; n   } n   cout<<endl; n  } n  n return 0; n }


#include <bits/stdc++.h> n using namespace std; n long long dp[2000010], v[2000010]; n int main() { n   for (int i = 3; i <= 2000000; i++) { n     dp[i] = n         (dp[i - 1] + 2 * dp[i - 2] + 4 * (!v[i - 1] && !v[i - 2])) % 1000000007; n     v[i] = !v[i - 1] && !v[i - 2]; n   } n   int t; n   scanf( %d , &t); n   while (t--) { n     int n; n     scanf(  %d , &n); n     printf( %I64d n , dp[n]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e3; n int r, c, n, k, tot, bound[maxn + 2], vis[maxn + 2], suc[maxn + 2], n     pre[maxn + 2], rest[maxn + 2]; n bool exist[maxn + 2][maxn + 2]; n long long ans; n struct vertex { n   int x, y; n } a[maxn + 1]; n vector<int> t[maxn + 1]; n int main() { n   scanf( %d%d%d%d , &r, &c, &n, &k); n   for (int i = 1; i <= n; ++i) { n     scanf( %d%d , &a[i].x, &a[i].y); n     exist[a[i].x][a[i].y] = true; n   } n   for (int i = 1; i <= r; ++i) { n     memset(vis, 0, sizeof vis); n     memset(suc, 0, sizeof suc); n     memset(pre, 0, sizeof pre); n     memset(rest, 0, sizeof rest); n     tot = 0; n     suc[c + 1] = c + 1; n     for (int j = 1; j <= c + 1; ++j) bound[j] = c + 1; n     for (int j = 1; j <= n; ++j) n       if (a[j].x >= i) { n         ++vis[a[j].y]; n         t[a[j].x].push_back(a[j].y); n       } n     for (int j = 1, l = 0, sum = 0; j <= c; ++j) { n       while (l <= c && sum < k) sum += vis[++l]; n       if (l > c) break; n       bound[j] = l; n       rest[j] = sum; n       sum -= vis[j]; n     } n     for (int j = 1, last = 0; j <= c; ++j) { n       if (vis[j]) { n         suc[last] = j; n         pre[j] = last; n         last = j; n       } n       if (j == c) { n         suc[last] = c + 1; n         pre[c + 1] = last; n       } n     } n     long long sum = 0; n     for (int j = c + 1; j; j = pre[j]) n       sum += (long long)(c - bound[j] + 1) * (j - pre[j]); n     for (int j = r; j >= i; --j) { n       ans += sum; n       for (int l = 0; l < t[j].size(); ++l) { n         int lt = t[j][l]; n         --vis[lt]; n         for (int cnt = vis[lt], now = lt; now && bound[now] >= lt; n              now = pre[now]) { n           --rest[now]; n           if (rest[now] < k) { n             sum -= (long long)(c - bound[now] + 1) * (now - pre[now]); n             bound[now] = suc[bound[now]]; n             rest[now] += vis[bound[now]]; n             sum += (long long)(c - bound[now] + 1) * (now - pre[now]); n           } n         } n         if (!vis[lt]) { n           sum += (long long)(c - bound[suc[lt]] + 1) * (suc[lt] - pre[lt]) - n                  (long long)(c - bound[lt] + 1) * (lt - pre[lt]) - n                  (long long)(c - bound[suc[lt]] + 1) * (suc[lt] - lt); n           pre[suc[lt]] = pre[lt]; n           suc[pre[lt]] = suc[lt]; n         } n       } n       t[j].clear(); n     } n   } n   printf( %lld , ans); 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, a, b; n     string s =   ; n     cin >> n >> a >> b; n     s +=  a ; n     for (int i = 1; i < b; i++) { n       s += s[i - 1] + 1; n     } n     string s2 = s; n     for (int i = 0; i < n / b - 1; i++) { n       s += s2; n     } n     for (int i = 0; i < n % b; i++) { n       s += s[i]; n     } n     cout << s << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n set<int> cn[10000]; n int nx[10000]; n string xm[10000]; n int main() { n   int m; n   cin >> m; n   ; n   map<string, int> mx; n   int n = 0; n   for (int i = (0); i < m; i++) { n     string a, b; n     cin >> a >> b; n     if (!mx.count(a)) mx[a] = n, xm[n++] = a; n     if (!mx.count(b)) mx[b] = n, xm[n++] = b; n     cn[mx[a]].insert(mx[b]); n     cn[mx[b]].insert(mx[a]); n   } n   cout << n << endl; n   for (int i = (0); i < n; i++) { n     memset(nx, 0, sizeof(nx)); n     int my = -1, cmy = 0; n     for (auto f : cn[i]) { n       for (auto fx : cn[f]) { n         if (fx == i || cn[fx].count(i)) continue; n         nx[fx]++; n         if (nx[fx] > my) n           my = nx[fx], cmy = 1; n         else if (nx[fx] == my) n           cmy++; n       } n     } n     if (cmy == 0) { n       cmy = n - 1 - cn[i].size(); n     } n     cout << xm[i] <<     << cmy << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n enum { WHITE, GREY, BLACK }; n using namespace std; n long long int inf = 1e18; n template <class T> n T gcd(T a, T b) { n   return b == 0 ? a : gcd(b, a % b); n } n template <typename T> n T lcm(T a, T b) { n   return a / gcd(a, b) * b; n } n template <typename T> n T last_bit(T n) { n   return n & 1; n } n template <class T> n T big_mod(T n, T p, T m) { n   if (p == 0) return (T)1; n   T x = big_mod(n, p / 2, m); n   x = (x * x) % m; n   if (p & 1) x = (x * n) % m; n   return x; n } n template <class T> n T modInv(T a, T m) { n   T x, y; n   extgcd(a, m, x, y); n   x %= m; n   while (x < 0) { n     x += m; n   } n   return x; n } n template <class T> n T extgcd(T a, T b, T& x, T& y) { n   if (b == 0) { n     x = 1; n     y = 0; n     return a; n   } else { n     int g = extgcd(b, a % b, y, x); n     y -= a / b * x; n     return g; n   } n } n template <class T> n T multiplication(T n, T p, T m) { n   if (p == 0) return (T)0; n   T x = multiplication(n, p / 2, m); n   x = (x + x) % m; n   if (p & 1) x = (x + n) % m; n   return x; n } n template <class T> n T my_pow(T n, T p) { n   if (p == 0) return 1; n   T x = my_pow(n, p / 2); n   x = (x * x); n   if (p & 1) x = (x * n); n   return x; n } n template <class T> n double getdist(T a, T b) { n   return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); n } n template <class T> n T extract(string s, T ret) { n   stringstream ss(s); n   ss >> ret; n   return ret; n } n template <class T> n string tostring(T n) { n   stringstream ss; n   ss << n; n   return ss.str(); n } n template <class T> n T Mod(T n, T m) { n   return (n % m + m) % m; n } n template <class T> n T MIN3(T a, T b, T c) { n   return min(a, min(b, c)); n } n template <class T> n T MAX3(T a, T b, T c) { n   return max(a, max(b, c)); n } n template <class T> n void print_vector(T& v) { n   int sz = v.size(); n   if (sz) cout << v[0]; n   for (int i = 1; i < sz; i++) cout <<     << v[i]; n   cout <<   n ; n } n bool isVowel(char ch) { n   ch = toupper(ch); n   if (ch ==  A  || ch ==  U  || ch ==  I  || ch ==  O  || ch ==  E ) n     return true; n   return false; n } n bool isConsonant(char ch) { n   if (isalpha(ch) && !isVowel(ch)) return true; n   return false; n } n template <class R> n R Josephus(R n, R k) { n   R ans = 1; n   for (R i = 2; i <= n; i++) ans = (ans + k - 1) % i + 1; n   return ans; n } n template <class R> n R toitent_Phi2(R a) { n   R result = a; n   for (R i = 2; i * i <= a; i++) { n     if (a % i == 0) result = result - result / i; n     while (a % i == 0) a = a / i; n   } n   if (a > 1) result = result - result / a; n   return result; n } n template <typename T> n T Angle(T x1, T y1, T x2, T y2) { n   return atan(double(y1 - y2) / double(x1 - x2)); n } n long long int a[102]; n int main() { n   cin.sync_with_stdio(false); n   cin.tie(); n   ; n   long long int T, t; n   long long int i, j, k; n   long long int n, m, q; n   scanf( %lld , &n); n   for (i = 1; i <= 2 * n; i++) { n     scanf( %lld , &a[i]); n   } n   sort(a + 1, a + 1 + 2 * n); n   long long int f = 1; n   j = n; n   for (i = 2 * n; i > n; i--) { n     for (j = n; j >= 1; j--) { n       if (a[i] <= a[j]) f = 0; n     } n   } n   if (f) n     printf( YES n ); n   else n     printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, ans, s, e, in[100020], pre[100020], vis[100020], ff[100020]; n long long len[100020]; n struct node { n   int v, f; n   long long w; n }; n vector<node> adj[100020]; n bool operator<(const node &x, const node &y) { n   if (x.w > y.w) return true; n   return false; n } n int dijk(int s) { n   priority_queue<node> q; n   node now, newnode; n   now.w = 0; n   now.v = s; n   q.push(now); n   int i, u, v; n   memset(ff, 0, sizeof(ff)); n   memset(vis, 0, sizeof(vis)); n   memset(len, 123, sizeof(len)); n   len[s] = 0; n   int cnt = 0; n   while (!q.empty()) { n     if (cnt == n) break; n     u = q.top().v; n     q.pop(); n     if (vis[u]) continue; n     vis[u] = 1; n     for (i = 0; i < adj[u].size(); i++) { n       now = adj[u][i]; n       v = now.v; n       if (now.w + len[u] < len[v] || (now.w + len[u] == len[v] && !now.f)) { n         len[v] = now.w + len[u]; n         newnode.w = len[v]; n         newnode.v = v; n         if (now.f) n           ff[v] = 1; n         else n           ff[v] = 0; n         pre[v] = u; n         q.push(newnode); n       } n     } n     cnt++; n   } n   return 0; n } n void addnode(int u, int v, int w, int f) { n   node nn; n   nn.v = v; n   nn.w = w; n   nn.f = f; n   adj[u].push_back(nn); n } n int main() { n   ans = 0; n   int t, i, u, v, w, cnt = 0; n   node now, nn; n   scanf( %d%d%d , &n, &m, &k); n   for (i = 0; i < m; i++) { n     scanf( %d%d%d , &u, &v, &w); n     addnode(u, v, w, 0); n     addnode(v, u, w, 0); n   } n   for (i = 0; i < k; i++) { n     scanf( %d%d , &v, &w); n     addnode(1, v, w, 1); n     addnode(v, 1, w, 1); n   } n   dijk(1); n   ans = 0; n   for (int i = 2; i <= n; i++) { n     if (ff[i]) ans++; n   } n   printf( %d n , k - ans); 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 int n; n   cin >> n; n   long long int mc[6]; n   mc[0] = mc[5] = 0; n   for (long long int i = 0, k = 0; i < n; i++) { n     cin >> k; n     mc[k] += 1; n   } n   if (mc[0] < 1) n     cout << -1; n   else if (mc[5] < 9) n     cout << 0; n   else { n     for (long long int i = 0; i < mc[5] / 9 * 9; i++) cout << 5; n     for (long long int i = 0; i < mc[0]; i++) cout << 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 32768, N = 109; n char ch; n string s[N], t; n int ans, v, f[MOD + 9], num, p, pos; n int ex(); n int gt() { n   int ret = 0; n   if (t[pos] ==  n ) return pos++, v; n   if (t[pos] ==  f ) return pos += 2, ret = f[ex()], pos++, ret; n   while (t[pos] >=  0  && t[pos] <=  9 ) ret = ret * 10 + (t[pos] -  0 ), pos++; n   return ret; n } n int nex() { n   int ret = gt(); n   while (t[pos] ==  *  || t[pos] ==  / ) { n     char tp = t[pos]; n     pos++; n     int t1 = gt(); n     if (tp ==  * ) n       ret = (int)((1ll * ret * t1) % MOD); n     else n       ret = (ret / t1) % MOD; n   } n   return ret; n } n int ex() { n   int ret = nex(); n   while (t[pos] ==  +  || t[pos] ==  - ) { n     char tp = t[pos]; n     pos++; n     int t1 = nex(); n     if (tp ==  + ) n       ret = (ret + t1) % MOD; n     else n       ret = (ret + MOD - t1) % MOD; n   } n   return ret; n } n bool iex() { n   int p1, p2, tp; n   p1 = ex(); n   if (t[pos] ==  = ) n     tp = 0, pos += 2; n   else if (t[pos] ==  < ) n     tp = -1, pos++; n   else n     tp = 1, pos++; n   p2 = ex(); n   if (tp == 0) return p1 == p2; n   if (tp == -1) return p1 < p2; n   if (tp == 1) return p1 > p2; n   return 0; n } n int getans() { n   for (int i = 0; i < num; i++) { n     t = s[i]; n     if (t[0] ==  i ) { n       pos = 3; n       if (iex()) return pos += 7, ex(); n     } else n       return pos = 6, ex(); n   } n   return -1; n } n int main() { n   cin >> ans >> ch; n   while (ch !=  { ) cin >> ch; n   num = 0; n   while (true) { n     cin >> ch; n     if (ch ==  } ) break; n     if (ch ==  ; ) n       num++; n     else n       s[num] += ch; n   } n   int anst = -1; n   for (int i = 0; i < MOD; i++) { n     v = i, f[i] = getans(); n     if (f[i] == ans) anst = i; n   } n   cout << anst; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, l; n vector<vector<int>> adj; n int timer; n vector<int> tin, tout; n vector<vector<int>> up; n vector<int> dist; n vector<int> down; n void dfs(int v, int p) { n   dist[v] = dist[p] + 1; n   down[v] = 1; n   tin[v] = ++timer; n   up[v][0] = p; n   for (int i = 1; i <= l; ++i) up[v][i] = up[up[v][i - 1]][i - 1]; n   for (int u : adj[v]) { n     if (u != p) { n       dfs(u, v); n       down[v] += down[u]; n     } n   } n   tout[v] = ++timer; n } n bool is_ancestor(int u, int v) { n   return tin[u] <= tin[v] && tout[u] >= tout[v]; n } n int lca(int u, int v) { n   if (is_ancestor(u, v)) return u; n   if (is_ancestor(v, u)) return v; n   for (int i = l; i >= 0; --i) { n     if (!is_ancestor(up[u][i], v)) u = up[u][i]; n   } n   return up[u][0]; n } n int pre(int u, int v) { n   for (int i = l; i >= 0; --i) { n     if (!is_ancestor(up[u][i], v)) u = up[u][i]; n   } n   return u; n } n void preprocess(int root) { n   tin.resize(n + 1); n   tout.resize(n + 1); n   timer = 0; n   l = ceil(log2(n + 1)); n   up.assign(n + 1, vector<int>(l + 2)); n   dfs(root, root); n } n void trav(int x, int d) { n   int to = x; n   for (int i = l; i >= 0; i--) { n     if (dist[x] - dist[up[to][i]] < d) { n       to = up[to][i]; n     } n   } n   cout << down[up[to][0]] - down[to] <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   adj.resize(n + 1); n   dist.resize(n + 1); n   down.resize(n + 1); n   for (int i = 0; i < n - 1; i++) { n     int x, y; n     cin >> x >> y; n     adj[x].push_back(y); n     adj[y].push_back(x); n   } n   preprocess(1); n   cin >> m; n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     int L = lca(x, y); n     if (x == y) { n       cout << n <<   n ; n       continue; n     } n     int d = (dist[x] - dist[L] + dist[y] - dist[L] + 1); n     if (d % 2 == 0) { n       cout << 0 <<   n ; n     } else { n       if (dist[x] - dist[L] == dist[y] - dist[L]) { n         cout << n - down[pre(x, L)] - down[pre(y, L)] <<   n ; n       } else { n         d = (dist[x] - dist[L] + dist[y] - dist[L]); n         if (dist[x] < dist[y]) swap(x, y); n         int z = d / 2; n         trav(x, z); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int fp(int abc, int parent[]) { n   if (parent[abc] == abc) return abc; n   int def = fp(parent[abc], parent); n   parent[abc] = def; n   return def; n } n void cycledmerge(vector<int> hub[], int n, int parent[]) { n   stack<int> urutan; n   for (int i = 0; i < n; i++) parent[i] = i; n   stack<pair<int, int> > ngurut; n   vector<int> sudah(n, 0); n   vector<int> adadiurutan(n, 0); n   vector<int> dari(n, -1); n   for (int i = 0; i < n; i++) { n     if (sudah[i]) continue; n     urutan.push(i); n     ngurut.push(make_pair(i, 0)); n     adadiurutan[i] = 1; n     sudah[i] = 1; n     while (!ngurut.empty()) { n       int id = ngurut.top().first; n       int val = ngurut.top().second; n       ngurut.pop(); n       if (val == (int)hub[id].size()) { n         while (!urutan.empty() && urutan.top() == id) urutan.pop(); n         adadiurutan[id] = 0; n         continue; n       } n       ngurut.push(make_pair(id, val + 1)); n       int next = hub[id][val]; n       if (dari[id] == next) continue; n       if (sudah[next] && !adadiurutan[fp(next, parent)]) continue; n       if (!sudah[next]) { n         sudah[next] = 1; n         urutan.push(next); n         ngurut.push(make_pair(next, 0)); n         adadiurutan[next] = 1; n         dari[next] = id; n         continue; n       } n       if (parent[fp(id, parent)] == fp(next, parent)) continue; n       parent[fp(id, parent)] = fp(next, parent); n       while (!urutan.empty() && urutan.top() != fp(next, parent)) { n         parent[fp(urutan.top(), parent)] = fp(next, parent); n         urutan.pop(); n       } n     } n   } n   return; n } n int main() { n   int p[200]; n   vector<int> hub[200]; n   int n, m; n   cin >> n >> m; n   for (int(i) = 0; (i) < (m); ++(i)) { n     int a, b; n     cin >> a >> b; n     --a; n     --b; n     hub[a].push_back(b); n     hub[b].push_back(a); n   } n   cycledmerge(hub, n, p); n   vector<int> ukuran(n, 0); n   for (int(i) = 0; (i) < (n); ++(i)) ukuran[p[i]]++; n   int numcyc = 0; n   for (int(i) = 0; (i) < (n); ++(i)) n     if (ukuran[i] >= 3) ++numcyc; n   if (numcyc != 1) { n     cout <<  NO  << endl; n     return 0; n   } n   int besar = 0; n   for (int(i) = 0; (i) < (n); ++(i)) n     if (ukuran[i] >= 3) besar = ukuran[i]; n   int yesadj = 0; n   int noadj = 0; n   for (int(i) = 0; (i) < (n); ++(i)) n     for (int(j) = 0; (j) < (((int)hub[i].size())); ++(j)) { n       int k = hub[i][j]; n       if (k < i) continue; n       if (p[i] != p[k]) n         ++noadj; n       else n         ++yesadj; n     } n   if (yesadj != besar || noadj != (n - besar + 1) - 1) { n     cout <<  NO  << endl; n   } else { n     cout <<  FHTAGN!  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, resmax = -round(1e9), resmin = round(1e9); n int main() { n   cin >> n; n   for (int i = 1; i <= 7; i++) { n     int j = i, dem1 = 0; n     for (int dem = 1; dem <= n; dem++) { n       if ((j % 7 == 0) || (j % 7 == 6)) dem1++; n       j++; n       if (j > 7) j = 1; n     } n     resmax = max(resmax, dem1); n     resmin = min(resmin, dem1); n   } n   cout << resmin <<     << resmax; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string st; n   cin >> st; n   for (int i = 0; i < st.length(); i++) { n     if (st[i] ==  H  || st[i] ==  Q  || st[i] ==  9 ) { n       cout <<  YES  << endl; n       return 0; n     } n   } n   cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int solve() { n   int n; n   cin >> n; n   if (n == 1) return cout << -1 << endl, 0; n   string s =   ; n   for (int i = 0; i < n - 1; i++) s +=  3 ; n   cout <<  2  + s << endl; n   return 0; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gi() { n   int res = 0, w = 1; n   char ch = getchar(); n   while (ch !=  -  && !isdigit(ch)) ch = getchar(); n   if (ch ==  - ) w = -1, ch = getchar(); n   while (isdigit(ch)) res = res * 10 + ch -  0 , ch = getchar(); n   return res * w; n } n const int Mod = 1e9 + 7, K = 1e9; n int upd(int x) { return x + (x >> 31 & Mod); } n const int MAX_N = 3e5 + 5; n int N, M, a[MAX_N], b[MAX_N], c[MAX_N]; n char s[MAX_N]; n int d[MAX_N], m; n void Div(int x, int &v) { n   long long now = 0; n   for (int i = m; i; i--) { n     now = now * K + d[i]; n     d[i] = now / x, now %= x; n   } n   v = now; n   for (; m > 1 && !d[m]; m--) n     ; n } n int f[2][MAX_N * 20], mx[2]; n void add(int o, int x, int v) { n   f[o][x] = upd(f[o][x] + v - Mod); n   mx[o] = max(mx[o], x); n } n int main() { n   N = gi(); n   a[1] = 1; n   for (int i = 2; i <= N; i++) a[i] = gi(); n   for (int i = 1; i <= N; i++) b[i] = gi(); n   scanf( %s , s + 1), M = strlen(s + 1); n   reverse(&s[1], &s[M + 1]); n   for (int i = 1; i <= M; i += 9) { n     ++m; n     for (int j = min(M, i + 8); j >= i; j--) d[m] = d[m] * 10 + s[j] -  0 ; n   } n   a[N + 1] = K; n   for (int i = 1; i <= N; i++) n     if (a[i + 1] > 1) Div(a[i + 1], c[i]); n   if (m != 1 || d[1] != 0) return puts( 0 ) & 0; n   f[0][0] = 1, f[0][1] = Mod - 1, mx[0] = 1; n   for (int i = 1, o = 0; i <= N; i++, o ^= 1) { n     for (int j = 1; j <= mx[o]; j++) f[o][j] = upd(f[o][j] + f[o][j - 1] - Mod); n     for (int j = 0; j <= mx[o]; j++) n       if (f[o][j]) { n         int k = (c[i] - j % a[i + 1] + a[i + 1]) % a[i + 1]; n         int l = (j + k - c[i]) / a[i + 1], n             r = b[i] / a[i + 1] + 1 - (b[i] % a[i + 1] < k); n         add(o ^ 1, l, f[o][j]), add(o ^ 1, l + r, Mod - f[o][j]); n         f[o][j] = 0; n       } n     mx[o] = 0; n   } n   printf( %d n , f[N & 1][0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> cnt, b; n vector<int> ans; n int a[200010], x; n int n, m, p; n int main() { n   cin >> n >> m >> p; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < m; i++) { n     cin >> x; n     b[x]++; n   } n   for (int i = 0; i < p; i++) { n     if (i + (long long)(m - 1) * p >= n) break; n     long long j; n     cnt = b; n     for (j = i; j <= i + (long long)(m - 1) * p; j += p) n       if (--cnt[a[j]] == 0) cnt.erase(a[j]); n     if (cnt.size() == 0) ans.push_back(i + 1); n     for (long long k = i; j < n; j += p, k += p) { n       if (--cnt[a[j]] == 0) cnt.erase(a[j]); n       if (++cnt[a[k]] == 0) cnt.erase(a[k]); n       if (cnt.size() == 0) ans.push_back(k + p + 1); n     } n   } n   sort(ans.begin(), ans.end()); n   cout << ans.size() << endl; n   for (int i = 0; i < ans.size(); i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int k[5] = {100, 20, 10, 5, 1}; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   while (cin >> n) { n     long long ans = 0; n     for (int i = 0; i < 5; i++) { n       ans += n / k[i]; n       n %= k[i]; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma optimize( Ofast ) n using namespace std; n using ll = long long int; n using ull = unsigned long long int; n using dd = double; n using ldd = long double; n using si = short int; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n int _size = 1 << 20; n vector<ll> tree(_size << 1), cnt(_size << 1); n vector<ll> pos; n vector<ll> x; n vector<ll> price; n vector<ll> if_now; n vector<ll> to_pay; n vector<vector<pll>> gr; n vector<ll> ans; n int n; n void upd(int v, ll cn) { n   ll c = cn; n   cn *= price[v]; n   v = pos[v]; n   v += _size; n   cnt[v] += c; n   tree[v] += cn; n   v >>= 1; n   while (v) { n     tree[v] += cn; n     cnt[v] += c; n     v >>= 1; n   } n } n int find_f(ll t, int v = 1) { n   if (v >= _size) return v - _size; n   if (tree[v << 1] > t) n     return find_f(t, v << 1); n   else n     return find_f(t - tree[v << 1], v << 1 | 1); n } n ll get_cnt(ll rr, int v = 1, int l = 0, int r = _size) { n   if (r <= rr) return cnt[v]; n   if (l >= rr) return 0; n   return get_cnt(rr, v << 1, l, (r + l) >> 1) + n          get_cnt(rr, v << 1 | 1, (r + l) >> 1, r); n } n ll get_pay(ll rr, int v = 1, int l = 0, int r = _size) { n   if (r <= rr) return tree[v]; n   if (l >= rr || r <= l) return 0; n   return get_pay(rr, v << 1, l, (r + l) >> 1) + n          get_pay(rr, v << 1 | 1, (r + l) >> 1, r); n } n ll get_now(ll t) { n   0; n   if (t >= tree[1]) { n     return get_cnt(_size); n   } n   0; n   int ind = find_f(t); n   0; n   ll ans = get_cnt(ind); n   0; n   t -= get_pay(ind); n   0; n   if (ind >= to_pay.size() || to_pay[ind] == 0) { n     return ans; n   } n   return ans + t / to_pay[ind]; n } n void dfs(int v, ll t) { n   0; n   upd(v, x[v]); n   0; n   if_now[v] = get_now(t); n   0; n   for (pll i : gr[v]) { n     t -= i.second * 2; n     if (t >= 0) dfs(i.first, t); n     t += i.second * 2; n   } n   upd(v, -x[v]); n } n void afs(int v) { n   ans[v] = if_now[v]; n   vector<ll> ch; n   for (pii i : gr[v]) { n     afs(i.first); n     ch.push_back(ans[i.first]); n   } n   sort((ch).begin(), (ch).end()); n   reverse((ch).begin(), (ch).end()); n   ans[v] = max(ans[v], (ch.size() >= 2 ? ch[1] : 0)); n } n int main() { n   ios_base::sync_with_stdio(0); n   cout.tie(0); n   cin.tie(0); n   ll t; n   cin >> n >> t; n   x.resize(n); n   pos.resize(n); n   price.resize(n); n   if_now.resize(n); n   gr.resize(n); n   to_pay.resize(n); n   ans.resize(n); n   for (int i = 0; i < n; ++i) cin >> x[i]; n   map<ll, ll> ttt; n   int s = 0; n   for (int i = 0; i < n; ++i) { n     cin >> price[i]; n     ttt[price[i]] = 0; n   } n   for (auto& i : ttt) { n     i.second = s; n     to_pay[s] = i.first; n     ++s; n   } n   0; n   for (int i = 0; i < n; ++i) pos[i] = ttt[price[i]]; n   0; n   vector<pair<ll, ll>> tt; n   for (int i = 0; i < n - 1; ++i) { n     ll a, b; n     cin >> a >> b; n     tt.push_back({a, b}); n     gr[a - 1].push_back({i + 1, b}); n   } n   0; n   dfs(0, t); n   0; n   0; n   afs(0); n   0; n   for (pll i : gr[0]) { n     ans[0] = max(ans[0], ans[i.first]); n   } n   cout << ans[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   int TESTS = 1; n   cin >> TESTS; n   while (TESTS--) { n     long long n, x, y, d; n     cin >> n >> x >> y >> d; n     if (abs(y - x) % d == 0) { n       cout << abs(y - x) / d <<   n ; n       continue; n     } n     long long l = 1000000007, r = 1000000007; n     if ((n - y) % d == 0) { n       l = (n - y) / d + (n - x + d - 1) / d; n     } n     if ((y - 1) % d == 0) { n       r = (y - 1) / d + (x - 1 + d - 1) / d; n     } n     long long ans = min(l, r); n     if (ans != 1000000007) n       cout << ans <<   n ; n     else n       cout <<  -1 n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long nch(long long n) { n   long long ans = 0, tmp = 1, k = 1; n   while (n) { n     ans += min(n, tmp * 9) * k; n     n -= min(n, tmp * 9); n     tmp *= 10; n     k++; n   } n   return ans; n } n char get(long long n, long long d) { n   string s; n   stringstream ss; n   ss << n; n   ss >> s; n   return s[d - 1]; n } n char kth(long long n) { n   long long s = 0, nine = 9, d = 0, len; n   for (len = 1;; len++) { n     s += nine * len; n     d += nine; n     if (s >= n) { n       s -= nine * len; n       d -= nine; n       n -= s; n       break; n     } n     nine *= 10; n   } n   long long diff = ceil((double)n / len); n   long long dd = n % len; n   if (dd == 0) dd = len; n   return get(d + diff, dd); n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long q; n   cin >> q; n   while (q--) { n     long long k, n = 1; n     cin >> k; n     for (n = 1; nch(n) < k; n++) k -= nch(n); n     cout << kth(k) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 7; n vector<int> g[N]; n int used[N]; n int pos[N]; n vector<vector<int>> cycles; n vector<int> st; n void dfs(int u, int p = -1) { n   st.push_back(u); n   pos[st.back()] = (int)st.size() - 1; n   used[u] = 1; n   for (auto v : g[u]) { n     if (!used[v]) { n       dfs(v, u); n     } else if (v != p && used[v] == 1) { n       int x = pos[v]; n       cycles.push_back({}); n       for (int i = x; i < (int)st.size(); ++i) { n         cycles.back().push_back(st[i]); n       } n     } n   } n   st.pop_back(); n   used[u] = 2; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<pair<int, int>> edges; n   map<pair<int, int>, int> id; n   for (int i = 0; i < m; ++i) { n     int u, v; n     cin >> u >> v; n     --u, --v; n     g[u].push_back(v); n     g[v].push_back(u); n     edges.push_back({u, v}); n     id[{u, v}] = id[{v, u}] = i; n   } n   dfs(0); n   vector<int> res(m); n   vector<int> who(m, -1); n   vector<int> cmp(n, -1); n   for (int i = 0; i < (int)cycles.size(); ++i) { n     vector<int> ce; n     for (int j = 0; j < (int)cycles[i].size(); ++j) { n       cmp[cycles[i][j]] = i; n       ce.push_back( n           id[{cycles[i][j], cycles[i][(j + 1) % (int)cycles[i].size()]}]); n     } n     rotate(ce.begin(), min_element(ce.begin(), ce.end()), ce.end()); n     int mx = max_element(ce.begin(), ce.end()) - ce.begin(); n     bool ok = true; n     for (int i = 1; i <= mx; ++i) ok &= ce[i] > ce[i - 1]; n     for (int i = mx + 1; i < ce.size(); ++i) ok &= ce[i] < ce[i - 1]; n     if (ok) who[ce[0]] = ce[mx]; n   } n   vector<int> ans(n, 1); n   for (int i = m - 1; i >= 0; --i) { n     int a = edges[i].first; n     int b = edges[i].second; n     int sum = ans[a] + ans[b]; n     if (who[i] != -1) sum -= res[who[i]]; n     res[i] = sum; n     ans[a] = ans[b] = sum; n   } n   for (int i = 0; i < n; ++i) cout << ans[i] - 1 <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200015; n const int MOD = 1000000007; n int a[MAXN]; n bool F(int len, int num) { n   bool flag; n   int pres; n   for (int i = 0; i <= num - 1; i++) { n     flag = true; n     pres = i; n     for (int j = 0; j <= len - 1; j++) { n       if (a[pres] == 0) flag = false; n       pres += num; n     } n     if (flag == true) return flag; n   } n   return false; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i <= n - 1; i++) scanf( %d , &a[i]); n   bool ok = false; n   for (int i = 1; i <= sqrt(n); i++) { n     if (n % i == 0) { n       if (i >= 3) ok = ok | F(i, n / i); n       if (i != sqrt(n) && n / i >= 3) ok = ok | F(n / i, i); n     } n   } n   if (ok) n     cout <<  YES n ; n   else n     cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   int s; n   cin >> n; n   s = n; n   int k = 2; n   while (k <= n) { n     if (n % k == 0) { n       s += n / k; n       n = n / k; n     } else n       k++; n   } n   cout << s; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int signum(long long a) { return (a == 0 ? 0 : (a > 0 ? 1 : -1)); } n int dir(pair<long long, long long>& a, pair<long long, long long>& b, n         pair<long long, long long>& c) { n   return signum((b.first - a.first) * (c.second - a.second) - n                 (b.second - a.second) * (c.first - a.first)); n } n bool inside(vector<pair<long long, long long> >& hull, n             pair<long long, long long>& p) { n   if (dir(hull[0], hull[1], p) != 1) return false; n   if (dir(hull[0], hull[hull.size() - 1], p) != -1) return false; n   int l = 1, r = hull.size() - 2; n   while (l < r) { n     int m = (l + r) / 2 + 1; n     if (dir(hull[0], hull[m], p) == -1) n       r = m - 1; n     else n       l = m; n   } n   return dir(hull[l], hull[l + 1], p) == 1; n } n int main() { n   cin.sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n; n   vector<pair<long long, long long> > A(n); n   for (pair<long long, long long>& a : A) cin >> a.first >> a.second; n   reverse(A.begin(), A.end()); n   cin >> m; n   vector<pair<long long, long long> > B(m); n   bool sol = true; n   for (pair<long long, long long>& a : B) { n     cin >> a.first >> a.second; n     sol &= inside(A, a); n   } n   cout << (sol ?  YES  :  NO ) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, y = 0; n   cin >> a >> b; n   while (a <= b) { n     a *= 3; n     b *= 2; n     y++; n   } n   cout << y; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 2e9; n const long long Inf = 1e10; n const int P = 1e9 + 7; n const int N = 100005; n const int M = 1000005; n inline long long IN() { n   long long x = 0; n   int ch = 0, f = 0; n   for (ch = getchar(); ch != -1 && (ch < 48 || ch > 57); ch = getchar()) n     f = (ch ==  - ); n   for (; ch >= 48 && ch <= 57; ch = getchar()) n     x = (x << 1) + (x << 3) + ch -  0 ; n   return f ? (-x) : x; n } n template <typename T> n inline int chkmin(T &a, const T &b) { n   if (b < a) return a = b, 1; n   return 0; n } n template <typename T> n inline int chkmax(T &a, const T &b) { n   if (b > a) return a = b, 1; n   return 0; n } n void renew(int &x, const int &y) { n   x += y; n   if (x >= P) x -= P; n   if (x < 0) x += P; n } n int Pow(int x, int y, int p) { n   int a = 1; n   for (; y; y >>= 1, x = (long long)x * x % p) n     if (y & 1) a = (long long)a * x % p; n   return a; n } n vector<pair<int, int> > in[M], ou[M]; n int n, m, K; n int cov[M << 2], sum[M << 2], len[M << 2]; n int ql, qr, qv, qans; n void Cov(int x, int y) { n   cov[x] = y; n   sum[x] = 1LL * len[x] * y % P; n } n void sep(int x) { n   if (cov[x] == -1) return; n   Cov(x << 1, cov[x]); n   Cov(x + x + 1, cov[x]); n   cov[x] = -1; n } n void A(int x, int L, int R) { n   if (ql <= L && R <= qr) { n     Cov(x, qv); n     return; n   } n   sep(x); n   int md = (L + R) >> 1; n   if (ql <= md) A(x << 1, L, md); n   if (md < qr) A(x + x + 1, md + 1, R); n   sum[x] = sum[x + x + 1]; n   renew(sum[x], sum[x << 1]); n } n void Add(int l, int r, int v) { n   if (l > r) return; n   ql = l; n   qr = r; n   qv = v; n   A(1, 1, m); n } n void Q(int x, int L, int R) { n   if (ql <= L && R <= qr) { n     renew(qans, sum[x]); n     return; n   } n   sep(x); n   int md = (L + R) >> 1; n   if (ql <= md) Q(x << 1, L, md); n   if (md < qr) Q(x + x + 1, md + 1, R); n } n int Ask(int l, int r) { n   if (l > r) return 0; n   ql = l; n   qr = r; n   qans = 0; n   Q(1, 1, m); n   return qans; n } n void B(int x, int L, int R) { n   len[x] = R - L + 1; n   if (L == R) return; n   int md = (L + R) / 2; n   B(x << 1, L, md); n   B(x + x + 1, md + 1, R); n } n set<pair<int, int> > st; n int main() { n   scanf( %d%d%d , &n, &m, &K); n   for (int i = (int)(1); i <= (int)(K); i++) { n     int x1, y1, x2, y2; n     scanf( %d%d%d%d , &x1, &y1, &x2, &y2); n     in[x1].push_back(make_pair(y1, y2)); n     ou[x2 + 1].push_back(make_pair(y1, y2)); n   } n   B(1, 1, m); n   Add(1, 1, 1); n   for (auto v : in[1]) { n     st.insert(v); n     Add(v.first, v.second, 0); n   } n   for (int i = (int)(2); i <= (int)(n); i++) { n     static int tot, vx[N], vc[N]; n     tot = 0; n     for (auto v : in[i]) { n       if (v.second + 1 > m) continue; n       auto w = st.lower_bound(make_pair(v.second + 2, 0)); n       int Low = 0; n       if (w != st.begin()) { n         --w; n         Low = w->second; n       } n       ++tot; n       vc[tot] = Ask(Low + 1, v.second + 1); n       vx[tot] = v.second + 1; n     } n     for (int j = (int)(1); j <= (int)(tot); j++) Add(vx[j], vx[j], vc[j]); n     for (auto v : ou[i]) st.erase(v); n     for (auto v : in[i]) { n       st.insert(v); n       Add(v.first, v.second, 0); n     } n   } n   int Low = (st.empty()) ? (0) : ((--st.end())->second); n   cout << Ask(Low + 1, m) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 500010; n const int Maxk = 110; n const int Maxp = 110; n const int inf = 2147483647; n int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) n     x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar(); n   return x * f; n } n int n, k, p, s[Maxn], f[Maxn][Maxk]; n int mn[2][Maxp], v[Maxp]; n void modify(int o, int x, int y) { n   for (; x <= p; x += (x & -x)) mn[o][x] = min(mn[o][x], y); n } n int query(int o, int x) { n   int re = inf; n   for (; x; x -= (x & -x)) re = min(re, mn[o][x]); n   return re; n } n int main() { n   memset(f, 63, sizeof(f)); n   n = read(), k = read(), p = read(); n   s[0] = 0; n   for (int i = 1; i <= n; i++) n     s[i] = (read() % p + s[i - 1]) % p, f[i][1] = s[i]; n   for (int j = 2; j <= k; j++) { n     memset(v, 63, sizeof(v)); n     memset(mn, 63, sizeof(mn)); n     v[s[j - 1]] = f[j - 1][j - 1] - s[j - 1]; n     modify(0, s[j - 1] + 1, v[s[j - 1]]); n     modify(1, p - s[j - 1], v[s[j - 1]]); n     for (int i = j; i <= n; i++) { n       f[i][j] = min(s[i] + query(0, s[i] + 1), s[i] + p + query(1, p - s[i])); n       if (f[i][j - 1] - s[i] < v[s[i]]) { n         v[s[i]] = f[i][j - 1] - s[i]; n         modify(0, s[i] + 1, v[s[i]]); n         modify(1, p - s[i], v[s[i]]); n       } n     } n   } n   printf( %d , f[n][k]); n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   while (b) { n     a %= b; n     swap(a, b); n   } n   return a; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long x, y; n     cin >> x >> y; n     if (gcd(x, y) == 1) { n       cout <<  FINITE  << endl; n     } else { n       cout <<  INFINITE  << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n int cycl[N], set_val[N], cycl_size[N]; n int main() { n   int h, m, n; n   cin >> h >> m >> n; n   vector<int> strt; n   bool vis[h]; n   memset(vis, false, sizeof vis); n   int kk = 0; n   set<int> dmm[h + 1]; n   for (long long i = 0; i < m; i++) { n     if (vis[i]) { n       continue; n     } n     int ii = i; n     int jp = 0; n     while (1) { n       if (vis[ii]) { n         break; n       } n       set_val[ii] = jp; n       cycl[ii] = kk; n       dmm[kk].insert(jp); n       vis[ii] = true; n       ii += m; n       ii %= h; n       jp++; n     } n     cycl_size[kk] = jp; n     kk++; n   } n   map<int, int> hash; n   map<int, int> final; n   long long cnt = 0; n   while (n--) { n     char ch; n     scanf(  %c , &ch); n     if (ch ==  + ) { n       int id, t; n       scanf( %d%d , &id, &t); n       hash[id] = t; n       if (dmm[cycl[t]].find(set_val[t]) != dmm[cycl[t]].end()) { n         final[id] = set_val[t]; n         dmm[cycl[t]].erase(set_val[t]); n         continue; n       } n       set<int>::iterator it, it1; n       it = dmm[cycl[t]].lower_bound(set_val[t]); n       int xx1; n       if (it == dmm[cycl[t]].end()) { n         it = dmm[cycl[t]].begin(); n         xx1 = cycl_size[cycl[t]] + *it - set_val[t]; n         int jj = *it; n         final[id] = jj; n         dmm[cycl[t]].erase(jj); n       } else { n         xx1 = *it - set_val[t]; n         int jj = *it; n         final[id] = jj; n         dmm[cycl[t]].erase(jj); n       } n       cnt += (long long)xx1; n     } else { n       int id; n       scanf( %d , &id); n       dmm[cycl[hash[id]]].insert(final[id]); n     } n   } n   cout << cnt << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   long long int a[n], p[n], s[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   int blocks = 0; n   p[0] = a[0]; n   for (int i = 1; i < n; i++) { n     p[i] = max(p[i - 1], a[i]); n   } n   s[n - 1] = a[n - 1]; n   for (int i = n - 2; i >= 0; i--) { n     s[i] = min(s[i + 1], a[i]); n   } n   for (int i = 0; i < n - 1; i++) { n     if (p[i] <= s[i + 1]) { n       blocks++; n     } n   } n   cout << blocks + 1 << endl; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i = 0; n   cin >> n; n   long long a; n   vector<long long> vc; n   while (n--) { n     cin >> a; n     vc.push_back(a); n   } n   sort(vc.begin(), vc.end()); n   i = vc.size(); n   long long ans = vc[i - 1]; n   for (int j = i - 2; j >= 0; j--) { n     if (vc[j] >= vc[j + 1]) { n       vc[j] = vc[j + 1] - 1; n     } n     if (vc[j] > 0) ans += vc[j]; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int q, n; n long long s, cur; n char str[100100]; n int main() { n   scanf( %lld%d , &s, &q); n   while (q--) { n     scanf( %lld , &cur); n     scanf( %s , str); n     n = strlen(str); n     for (int i = 0; i < n; i++) { n       long long t = 1; n       while (cur % (t << 1) == 0) t <<= 1; n       if (str[i] ==  U ) { n         if ((t << 1) > s) continue; n         cur ^= t; n         cur |= (t << 1); n       } else if (str[i] ==  L ) { n         if (t == 1) continue; n         cur ^= t; n         cur |= (t >> 1); n       } else { n         if (t == 1) continue; n         cur |= (t >> 1); n       } n     } n     printf( %lld n , cur); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool iss(int d) { n   for (int i = 2; i * i <= d; ++i) n     if (d % i == 0) return false; n   return true; n } n int main() { n   int a, b, c; n   scanf( %d%d%d , &a, &b, &c); n   vector<int> simple; n   for (int i = 2; i < 100; ++i) n     if (iss(i)) simple.push_back(i); n   vector<vector<int> > d(101); n   for (int i = 1; i <= 100; ++i) { n     d[i].resize(simple.size()); n     for (int j = 0, tmp = i; j < simple.size(); ++j) n       while (tmp % simple[j] == 0) { n         tmp /= simple[j]; n         d[i][j]++; n       } n   } n   int ans = 0; n   int mod = 1 << 30; n   for (int i = 1; i <= a; ++i) n     for (int j = 1; j <= b; ++j) n       for (int k = 1; k <= c; ++k) { n         int prod = 1; n         for (int l = 0; l < simple.size(); ++l) n           prod *= d[i][l] + d[j][l] + d[k][l] + 1; n         ans = (ans + prod) % mod; n       } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[60][30]; n long long id[1 << 20]; n int num[1 << 20]; n double f[1 << 20]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     scanf(  %s , &s[i]); n   } n   int m = strlen(s[1]); n   for (int i = 1; i <= n; i++) { n     for (int j = i + 1; j <= n; j++) { n       int mask = 0; n       for (int k = 0; k < m; k++) { n         mask |= s[i][k] == s[j][k] ? (1ll << k) : 0; n       } n       id[mask] |= (1ll << i) | (1ll << j); n     } n   } n   for (int mask = (1 << m) - 1; mask >= 0; mask--) { n     for (int i = 0; i < m; i++) { n       if ((mask >> i) & 1) { n         id[mask ^ (1 << i)] |= id[mask]; n       } n     } n   } n   for (int i = 0; i <= (1 << m) - 1; i++) { n     long long tmp = id[i]; n     int ans = 0; n     while (tmp) { n       ans += tmp & 1; n       tmp >>= 1; n     } n     num[i] = ans; n     if (num[i] == 1) num[i] = 0; n   } n   f[(1 << m) - 1] = 0; n   for (int s = (1 << m) - 2; s >= 0; s--) { n     if (!num[s]) { n       f[s] = 0; n       continue; n     } n     int tot = 0; n     for (int i = 0; i < m; i++) n       if (!(s & (1 << i))) tot++; n     for (int i = 0; i < m; i++) { n       if (((1 << i) & s)) continue; n       f[s] += (1.0 * f[s | (1 << i)] / (double)tot * (double)num[s | (1 << i)] / n                (double)num[s]); n     } n     f[s] += 1.0; n   } n   printf( %.10lf , f[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   cout.tie(nullptr); n   int l, r, res = 0; n   cin >> l >> r; n   vector<unsigned long long> two, three, temp; n   for (int i = 0; i < 31; ++i) { n     two.push_back(1LL << i); n     if (i > 19) continue; n     three.push_back(pow(3LL, i)); n   } n   for (int i = 0; i < two.size(); ++i) { n     for (int j = 0; j < three.size(); ++j) { n       if (two[i] * three[j] <= 2000000000LL) temp.push_back(two[i] * three[j]); n     } n   } n   sort(temp.begin(), temp.end()); n   res = upper_bound(temp.begin(), temp.end(), r) - n         lower_bound(temp.begin(), temp.end(), l); n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n std::istream& operator>>(std::istream& i, pair<T, U>& p) { n   i >> p.first >> p.second; n   return i; n } n template <typename T> n std::istream& operator>>(std::istream& i, vector<T>& t) { n   for (auto& v : t) { n     i >> v; n   } n   return i; n } n template <typename T, typename U> n std::ostream& operator<<(std::ostream& o, const pair<T, U>& p) { n   o << p.first <<     << p.second; n   return o; n } n template <typename T> n std::ostream& operator<<(std::ostream& o, const vector<T>& t) { n   if (t.empty()) o <<   n ; n   for (size_t i = 0; i < t.size(); ++i) { n     o << t[i] <<    n [i == t.size() - 1]; n   } n   return o; n } n template <typename T> n using minheap = priority_queue<T, vector<T>, greater<T>>; n template <typename T> n using maxheap = priority_queue<T, vector<T>, less<T>>; n template <typename T> n bool in(T a, T b, T c) { n   return a <= b && b < c; n } n unsigned int logceil(long long first) { n   return first ? 8 * sizeof(long long) - __builtin_clzll(first) : 0; n } n namespace std { n template <typename T, typename U> n struct hash<pair<T, U>> { n   hash<T> t; n   hash<U> u; n   size_t operator()(const pair<T, U>& p) const { n     return t(p.first) ^ (u(p.second) << 7); n   } n }; n }  // namespace std n template <typename T, typename F> n T bsh(T l, T h, const F& f) { n   T r = -1, m; n   while (l <= h) { n     m = (l + h) / 2; n     if (f(m)) { n       l = m + 1; n       r = m; n     } else { n       h = m - 1; n     } n   } n   return r; n } n template <typename F> n double bshd(double l, double h, const F& f, double p = 1e-9) { n   unsigned int r = 3 + (unsigned int)log2((h - l) / p); n   while (r--) { n     double m = (l + h) / 2; n     if (f(m)) { n       l = m; n     } else { n       h = m; n     } n   } n   return (l + h) / 2; n } n template <typename T, typename F> n T bsl(T l, T h, const F& f) { n   T r = -1, m; n   while (l <= h) { n     m = (l + h) / 2; n     if (f(m)) { n       h = m - 1; n       r = m; n     } else { n       l = m + 1; n     } n   } n   return r; n } n template <typename F> n double bsld(double l, double h, const F& f, double p = 1e-9) { n   unsigned int r = 3 + (unsigned int)log2((h - l) / p); n   while (r--) { n     double m = (l + h) / 2; n     if (f(m)) { n       h = m; n     } else { n       l = m; n     } n   } n   return (l + h) / 2; n } n template <typename T> n T gcd(T a, T b) { n   if (a < b) swap(a, b); n   return b ? gcd(b, a % b) : a; n } n template <typename T> n class vector2 : public vector<vector<T>> { n  public: n   vector2() {} n   vector2(size_t a, size_t b, T t = T()) n       : vector<vector<T>>(a, vector<T>(b, t)) {} n }; n template <typename T> n class vector3 : public vector<vector2<T>> { n  public: n   vector3() {} n   vector3(size_t a, size_t b, size_t c, T t = T()) n       : vector<vector2<T>>(a, vector2<T>(b, c, t)) {} n }; n template <typename T> n class vector4 : public vector<vector3<T>> { n  public: n   vector4() {} n   vector4(size_t a, size_t b, size_t c, size_t d, T t = T()) n       : vector<vector3<T>>(a, vector3<T>(b, c, d, t)) {} n }; n template <typename T> n class vector5 : public vector<vector4<T>> { n  public: n   vector5() {} n   vector5(size_t a, size_t b, size_t c, size_t d, size_t e, T t = T()) n       : vector<vector4<T>>(a, vector4<T>(b, c, d, e, t)) {} n }; n class ENeatness { n  public: n   int N, A, B, X, Y; n   string Sol; n   vector<vector<bool>> L, V; n   vector<int> LOX, HIX, LOY, HIY; n   constexpr char op(char c) { n     switch (c) { n       case  L : n         return  R ; n       case  R : n         return  L ; n       case  D : n         return  U ; n       case  U : n         return  D ; n       case  1 : n         return  2 ; n       case  2 : n         return  1 ; n       default: n         return  x ; n     } n   } n   void goUp() { n     if (A == 0) return; n     while (B > 0 && L[A][B - 1]) { n       add( L ); n       --B; n     } n     while (B < N - 1 && !L[A - 1][B] && L[A][B + 1]) { n       add( R ); n       ++B; n     } n     if (L[A - 1][B]) { n       --A; n       add( U ); n       goUp(); n     } n   } n   void goDown() { n     if (A == N - 1) return; n     while (B > 0 && L[A][B - 1]) { n       add( L ); n       --B; n     } n     while (B < N - 1 && !L[A + 1][B] && L[A][B + 1]) { n       add( R ); n       ++B; n     } n     if (L[A + 1][B]) { n       ++A; n       add( D ); n       goDown(); n     } n   } n   void turnOffAndDown() { n     if (A == X) return; n     int b = -1; n     for (int i = 0; i < N; ++i) { n       if (L[A][i] && L[A + 1][i]) { n         b = i; n       } n     } n     while (B > 0 && L[A][B - 1]) { n       add( L ); n       --B; n     } n     while (B < N - 1 && L[A][B + 1]) { n       if (B != b) { n         add( 2 ); n         L[A][B] = false; n       } n       add( R ); n       ++B; n     } n     if (B != b) { n       add( 2 ); n       L[A][B] = false; n     } n     while (B != b) { n       add( L ); n       --B; n     } n     add( 2 ); n     L[A][B] = false; n     add( D ); n     ++A; n     turnOffAndDown(); n   } n   void turnOffAndUp() { n     int b = Y; n     if (A != X) { n       for (int i = 0; i < N; ++i) { n         if (L[A][i] && L[A - 1][i]) { n           b = i; n         } n       } n     } n     while (B > 0 && L[A][B - 1]) { n       add( L ); n       --B; n     } n     while (B < N - 1 && L[A][B + 1]) { n       if (B != b) { n         add( 2 ); n         L[A][B] = false; n       } n       add( R ); n       ++B; n     } n     if (B != b) { n       add( 2 ); n       L[A][B] = false; n     } n     while (B != b) { n       add( L ); n       --B; n     } n     add( 2 ); n     L[A][B] = false; n     if (A == X) return; n     add( U ); n     --A; n     turnOffAndUp(); n   } n   void add(char c) { n     if (!Sol.empty() && Sol.back() == op(c)) n       Sol.pop_back(); n     else n       Sol.push_back(c); n   } n   void bfs(int i, int j) { n     if (V[i][j]) return; n     V[i][j] = true; n     if (!L[i][j]) { n       add( 1 ); n       L[i][j] = true; n       LOX[i] = min(LOX[i], j); n       HIX[i] = max(HIX[i], j); n       LOY[j] = min(LOY[j], i); n       HIY[j] = max(HIY[j], i); n     } n     if (LOX[i] < j) { n       add( L ); n       bfs(i, j - 1); n       add( R ); n     } n     if (HIX[i] > j) { n       add( R ); n       bfs(i, j + 1); n       add( L ); n     } n     if (LOY[j] < i) { n       add( U ); n       bfs(i - 1, j); n       add( D ); n     } n     if (HIY[j] > i) { n       add( D ); n       bfs(i + 1, j); n       add( U ); n     } n   } n   void solve(istream& cin, ostream& cout) { n     cin >> N >> A >> B; n     L = vector2<bool>(N, N); n     V = vector2<bool>(N, N, false); n     for (auto& l : L) { n       for (int i = 0; i < N; ++i) { n         bool c; n         cin >> c; n         l[i] = c; n       } n     } n     LOX.assign(N, N); n     LOY.assign(N, N); n     HIX.assign(N, -1); n     HIY.assign(N, -1); n     for (int i = 0; i < N; ++i) { n       for (int j = 0; j < N; ++j) { n         if (L[i][j]) { n           LOX[i] = min(LOX[i], j); n           HIX[i] = max(HIX[i], j); n           LOY[j] = min(LOY[j], i); n           HIY[j] = max(HIY[j], i); n         } n       } n     } n     --A; n     --B; n     X = A; n     Y = B; n     bfs(A, B); n     goUp(); n     turnOffAndDown(); n     goDown(); n     turnOffAndUp(); n     for (int i = 0; i < N; ++i) { n       for (int j = 0; j < N; ++j) { n         if (L[i][j]) { n           cout <<  NO n ; n           return; n         } n       } n     } n     cout <<  YES n  << Sol <<   n ; n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   ENeatness solver; n   std::istream& in(std::cin); n   std::ostream& out(std::cout); n   solver.solve(in, out); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int T, arr[100005], a, b, ans; n int cal(int v) { n   int vv = v * (v - 1); n   return (vv >> 1); n } n int main() { n   cin >> T; n   for (int i = 1; i < T; i++) { n     cin >> a >> b; n     arr[a]++; n     arr[b]++; n   } n   for (int i = 1; i <= T; i++) { n     if (arr[i] >= 2) ans += cal(arr[i]); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200010; n struct node { n   int x, y; n } a[maxn]; n int ans[maxn]; n int f[maxn]; n bool cmp(int c, int d) { return c < d; } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &f[i]); n   } n   stack<int> s; n   node ff; n   ff.x = 0; n   ff.y = 0; n   s.push(0); n   a[0].y = 0; n   a[0].x = 0; n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &a[i].x, &a[i].y); n   } n   for (int i = m; i >= 1; i--) { n     if (a[i].y > a[s.top()].y) { n       s.push(i); n     } n   } n   int flag = n; n   int kk = 0; n   int gg = a[s.top()].y; n   for (int i = gg + 1; i <= n; i++) ans[i] = f[i]; n   sort(f + 1, f + 1 + gg); n   int l = 1; n   int r = gg; n   int per = s.top(); n   s.pop(); n   while (!s.empty()) { n     int now = s.top(); n     s.pop(); n     if (a[per].x == 1) { n       for (int i = a[per].y; i > a[now].y; i--) ans[i] = f[r--]; n     } else { n       for (int i = a[per].y; i > a[now].y; i--) { n         ans[i] = f[l++]; n       } n     } n     per = now; n   } n   for (int i = 1; i < n; i++) { n     printf( %d  , ans[i]); n   } n   printf( %d n , ans[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000000; n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= mod; n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n   } n   return res; n } n const int N = 201000; n struct node { n   long long fib[4]; n   int fg; n } nd[N * 4]; n int fib[N], a[N]; n int n, ty, l, r, v, m; n long long ret[2], ans; n void build(int p, int l, int r) { n   if (l == r) { n     nd[p].fib[0] = fib[l]; n     nd[p].fib[1] = fib[l + 1]; n     nd[p].fib[2] = nd[p].fib[0] * a[l] % mod; n     nd[p].fib[3] = nd[p].fib[1] * a[l] % mod; n   } else { n     int md = (l + r) >> 1; n     build(p + p, l, md); n     build(p + p + 1, md + 1, r); n     for (int i = 0; i < 4; i++) n       nd[p].fib[i] = (nd[p + p].fib[i] + nd[p + p + 1].fib[i]) % mod; n   } n } n void setp(int p, int w) { n   nd[p].fg += w; n   nd[p].fib[2] = (nd[p].fib[2] + nd[p].fib[0] * w) % mod; n   nd[p].fib[3] = (nd[p].fib[3] + nd[p].fib[1] * w) % mod; n } n void push(int p) { n   if (nd[p].fg) { n     setp(p + p, nd[p].fg); n     setp(p + p + 1, nd[p].fg); n     nd[p].fg = 0; n   } n } n void modify(int p, int l, int r, int x, long long w) { n   if (l == r) { n     nd[p].fib[2] = fib[l] * w % mod; n     nd[p].fib[3] = fib[l + 1] * w % mod; n   } else { n     push(p); n     int md = (l + r) >> 1; n     if (x <= md) n       modify(p + p, l, md, x, w); n     else n       modify(p + p + 1, md + 1, r, x, w); n     for (int i = 2; i < 4; i++) n       nd[p].fib[i] = (nd[p + p].fib[i] + nd[p + p + 1].fib[i]) % mod; n   } n } n void add(int p, int l, int r, int tl, int tr, int w) { n   if (l == tl && r == tr) n     setp(p, w); n   else { n     push(p); n     int md = (l + r) >> 1; n     if (tr <= md) n       add(p + p, l, md, tl, tr, w); n     else if (tl > md) n       add(p + p + 1, md + 1, r, tl, tr, w); n     else n       add(p + p, l, md, tl, md, w), add(p + p + 1, md + 1, r, md + 1, tr, w); n     for (int i = 2; i < 4; i++) n       nd[p].fib[i] = (nd[p + p].fib[i] + nd[p + p + 1].fib[i]) % mod; n   } n } n void query(int p, int l, int r, int tl, int tr) { n   if (l == tl && r == tr) { n     for (int i = 2; i < 4; i++) ret[i - 2] = (ret[i - 2] + nd[p].fib[i]) % mod; n   } else { n     push(p); n     int md = (l + r) >> 1; n     if (tr <= md) n       query(p + p, l, md, tl, tr); n     else if (tl > md) n       query(p + p + 1, md + 1, r, tl, tr); n     else n       query(p + p, l, md, tl, md), query(p + p + 1, md + 1, r, md + 1, tr); n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i < n + 1; i++) scanf( %d , a + i); n   fib[0] = fib[1] = 1; n   for (int i = 2; i < n + 10; i++) fib[i] = (fib[i - 1] + fib[i - 2]) % mod; n   build(1, 1, n); n   for (int i = 0; i < m; i++) { n     scanf( %d , &ty); n     if (ty == 1) { n       scanf( %d%d , &l, &v); n       modify(1, 1, n, l, v); n     } else if (ty == 2) { n       scanf( %d%d , &l, &r); n       ret[0] = ret[1] = 0; n       query(1, 1, n, l, r); n       ans = (fib[l] * ret[0] - fib[l - 1] * ret[1]) % mod; n       if (l % 2 == 1) ans *= -1; n       if (ans < 0) ans += mod; n       printf( %I64d n , ans); n     } else { n       scanf( %d%d%d , &l, &r, &v); n       add(1, 1, n, l, r, v); n     } n   } n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4786) n using namespace std; n int a[200000]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   a[n + 1] = a[1]; n   int y = 0; n   for (int i = 2; i <= n + 1; i++) { n     if (a[i] < a[i - 1]) { n       if (y != 0) { n         printf( -1 n ); n         return 0; n       } else { n         y = i; n       } n     } n   } n   if (y == 0) y = n + 1; n   printf( %d , n - y + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 11111; n pair<int, int> a[maxN]; n short dp[maxN][maxN]; n int s[maxN]; n bool mycmp(pair<int, int> a, pair<int, int> b) { n   if (a.second != b.second) return (a.second < b.second); n   return (a.first > b.first); n } n int main() { n   ios_base::sync_with_stdio(false); n   int n, l, r; n   cin >> n >> l >> r; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i].first; n   } n   for (int i = 1; i <= n; ++i) { n     cin >> a[i].second; n   } n   sort(a + 1, a + n + 1, mycmp); n   int sum = 0; n   for (int i = 1; i <= n; ++i) { n     sum += a[i].first; n   } n   s[0] = 0; n   for (int i = 1; i <= n; ++i) { n     s[i] = s[i - 1] + a[i].first; n   } n   for (int i = 0; i <= n; ++i) { n     for (int j = 0; j <= sum; ++j) { n       dp[i][j] = -1; n     } n   } n   dp[0][0] = 0; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j <= s[i]; ++j) { n       if (dp[i][j] < 0) continue; n       int type = a[i + 1].second, w = a[i + 1].first; n       if (type == 0) { n         dp[i + 1][j + w] = max(dp[i + 1][j + w], dp[i][j]); n         dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); n       } else { n         int x = j, y = sum - (s[i] - j); n         short f1 = (x >= l && x <= r); n         if (dp[i][j] + f1 > dp[i + 1][j + w]) { n           dp[i + 1][j + w] = dp[i][j] + f1; n         } n         short f2 = (y - a[i + 1].first >= l && y - a[i + 1].first <= r); n         if (dp[i][j] + f2 > dp[i + 1][j]) { n           dp[i + 1][j] = dp[i][j] + f2; n         } n       } n     } n   } n   short ans = -1; n   for (int i = 0; i <= sum; ++i) { n     ans = max(ans, dp[n][i]); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, pair<int, int> > a[200020]; n int f[200020]; n int v[200020]; n int n, m; n int F(int x) { n   if (f[x] == x) { n     return x; n   } n   f[x] = F(f[x]); n   v[f[x]] |= v[x]; n   return f[x]; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     f[i] = i; n     v[i] = 0; n   } n   for (int i = 0; i < m; i++) { n     scanf( %d%d%d , &a[i].second.first, &a[i].second.second, &a[i].first); n   } n   sort(a, a + m); n   reverse(a, a + m); n   long long ans = 0; n   for (int i = 0; i < m; i++) { n     int x = a[i].second.first; n     int y = a[i].second.second; n     x = F(x); n     y = F(y); n     if (x == y && v[x] == 0) { n       v[x] = 1; n       ans += a[i].first; n     } else if (x != y && (v[x] == 0 || v[y] == 0)) { n       v[y] |= v[x]; n       f[x] = y; n       ans += a[i].first; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> g1[30000 + 100], g2[30000 + 100]; n set<int> st; n int Next[30000 + 100], Prev[30000 + 100]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m, start; n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     int u, v; n     cin >> u >> v; n     g1[u].push_back(v); n     g2[v].push_back(u); n   } n   for (int i = 1; i <= n; i++) { n     sort(g1[i].begin(), g1[i].end()); n     sort(g2[i].begin(), g2[i].end()); n   } n   start = 0; n   st.insert(0); n   st.insert(1); n   Next[start] = 1; n   Prev[1] = 0; n   for (int i = 2; i <= n; i++) { n     vector<int> d; n     bool flag = false; n     for (auto it : g1[i]) { n       if (it > i) continue; n       int t1 = Next[it]; n       if (!binary_search(g1[i].begin(), g1[i].end(), t1)) { n         flag = true; n         Next[it] = i; n         Prev[t1] = i; n         Next[i] = t1; n         Prev[i] = it; n         break; n       } else { n         st.erase(it); n         d.push_back(it); n       } n     } n     if (!flag) { n       for (auto it : g2[i]) { n         if (it > i) continue; n         int t1 = Prev[it]; n         if (!binary_search(g1[t1].begin(), g1[t1].end(), i)) { n           flag = true; n           Next[t1] = i; n           Prev[it] = i; n           Next[i] = it; n           Prev[i] = t1; n           break; n         } else { n           st.erase(t1); n           d.push_back(t1); n         } n       } n     } n     if (!flag) { n       auto it = st.begin(); n       int t1 = Next[*it]; n       Next[*it] = i; n       Prev[t1] = i; n       Next[i] = t1; n       Prev[i] = *it; n     } n     for (auto it : d) { n       st.insert(it); n     } n     st.insert(i); n   } n   int now = Next[0]; n   while (now) { n     cout << now <<    ; n     now = Next[now]; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)1e6 + 7; n int n; n string s; n vector<int> p; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> s; n   n = (int)s.size(); n   for (int i = 0; i < n; i++) { n     if (s[i] != s[(i + 1) % n]) { n       p.push_back(i); n     } n   } n   int ant = -1; n   int yes = 0; n   int no = 0; n   for (int i = 1; i < (int)p.size(); i++) { n     if (ant == -1 || p[i] != ant + 1) { n       ant = p[i]; n       yes++; n     } n   } n   if (p.back() == n - 1 && p[0] == 0) { n     p.pop_back(); n   } n   ant = -1; n   for (int i = 0; i < (int)p.size(); i++) { n     if (ant == -1 || p[i] != ant + 1) { n       ant = p[i]; n       no++; n     } n   } n   cout << max(yes, no) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n bool isp(int a) { n   for (int i = 2; i * i <= a; i++) n     if (a % i == 0) return false; n   return true; n } n int power[100001]; n long long pow(long long a, long long b, long long MOD = n) { n   long long ret = 1, c = a; n   while (b) { n     if (b & 1) ret = (ret * c) % MOD; n     b >>= 1; n     c = (c * c) % MOD; n   } n   return ret; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   if (n < 4) { n     cout <<  YES n ; n     for (int i = 1; i <= n; i++) cout << i <<    ; n   } else if (n == 4) n     cout <<  YES n1 3 2 4  <<   n ; n   else if (isp(n) == false) n     cout <<  NO  <<   n ; n   else { n     cout <<  YES  <<   n ; n     int g; n     for (g = 2; g < n; g++) { n       int i, j = 1; n       for (i = 1;; i++) { n         j = (g * j) % n; n         if (j == 1) break; n       } n       if (i == n - 1) break; n     } n     power[0] = 1; n     for (int i = 1; i < n; i++) power[i] = (power[i - 1] * 1ll * g) % n; n     for (int i = 0; i < n - 1; i++) n       cout << power[i % 2 ? i : n - i - 1] <<   n ; n     cout << n <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)1e5 + 17; n const int MX = (int)1e6 + 17; n const int MOD = (int)1e9 + 7; n const long long oo = LLONG_MAX; n const int INF = INT_MAX; n const long double Pi = 3.14159265358979323846264338327950288419716939937510; n const int di[4] = {-1, 0, 1, 0}; n const int dj[4] = {0, 1, 0, -1}; n inline long long IN() { n   long long x = 0, ch = getchar(), f = 1; n   while (!isdigit(ch) && (ch !=  - ) && (ch != EOF)) ch = getchar(); n   if (ch ==  - ) { n     f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = (x << 1) + (x << 3) + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n inline void OUT(long long x) { n   if (x < 0) putchar( - ), x = -x; n   if (x >= 10) n     OUT(x / 10), putchar(x % 10 +  0 ); n   else n     putchar(x +  0 ); n } n void IOI2017() { n   freopen( n        partition  n        .in , n        r , stdin); n   freopen( n        partition  n        .out , n        w , stdout); n } n int n, cur = 4; n struct Week { n   int w[10]; n }; n Week a[2 * N]; n int Year(int x) { n   if (x % 400 == 0 || x % 4 == 0 && x % 100) return 366; n   return 365; n } n bool check(Week a, Week b) { n   for (int i = 1; i <= 7; i++) { n     if (a.w[i] != b.w[i]) return 0; n   } n   return 1; n } n int main() { n   for (int i = 1000; i <= N * 2; i++) { n     for (int j = 1; j <= Year(i); j++) { n       a[i].w[cur]++; n       cur++; n       if (cur == 8) cur = 1; n     } n   } n   ios_base::sync_with_stdio(0); n   cin >> n; n   for (int j = n + 1;; j++) { n     if (check(a[n], a[j])) { n       cout << j; n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INFL = 1LL << 60; n const int INF = 1 << 30; n const long long MOD = 998244353; n double EPS = 1e-10; n vector<long long> dy = {0, 1, 0, -1, 1, 1, -1, -1, 0}; n vector<long long> dx = {1, 0, -1, 0, 1, -1, 1, -1, 0}; n void pres(double A) { printf( %.12lf n , A); } n void BinarySay(long long x, long long y = 60) { n   for (int i = 0; i < (y); i++) cout << (x >> (y - 1 - i) & 1); n   cout << endl; n } n long long cnt_bit(long long x) { return __builtin_popcountll(x); } n long long pow_long(long long x, long long k) { n   long long res = 1; n   while (k > 0) { n     if (k % 2) res *= x; n     x *= x; n     k /= 2; n   } n   return res; n } n long long pow_mod(long long x, long long k) { n   x %= MOD; n   long long res = 1; n   while (k > 0) { n     if (k % 2) { n       res *= x; n       res %= MOD; n     } n     x *= x; n     x %= MOD; n     k /= 2; n   } n   return res; n } n long long inverse(long long x) { return pow_mod(x, MOD - 2); }; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long lcm(long long x, long long y) { n   long long res = x / gcd(x, y); n   res *= y; n   return res; n }; n const int MAXcomb = 200010; n long long fac[MAXcomb], finv[MAXcomb], inv[MAXcomb]; n void COMinit() { n   fac[0] = fac[1] = 1; n   finv[0] = finv[1] = 1; n   inv[1] = 1; n   for (int i = 2; i < MAXcomb; i++) { n     fac[i] = fac[i - 1] * i % MOD; n     inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; n     finv[i] = finv[i - 1] * inv[i] % MOD; n   } n } n long long comb(int n, int k) { n   if (n < k) return 0; n   if (n < 0 || k < 0) return 0; n   return fac[n] * finv[k] % MOD * finv[n - k] % MOD; n } n const int MAXkai = 200010; n long long kai_memo[MAXkai]; n long long kai(long long N) { n   if (kai_memo[N] != 0) return kai_memo[N]; n   if (N <= 1) return 1; n   return kai_memo[N] = N * kai(N - 1) % MOD; n } n long long disit(long long second, long long base = 10) { n   long long res = 0; n   while (second) { n     res++; n     second /= base; n   } n   return res; n } n vector<long long> divisor(long long n) { n   vector<long long> res(0); n   for (long long i = 1; i * i <= n; i++) { n     if (n % i == 0) { n       res.push_back(i); n       if (i != n / i) res.push_back(n / i); n     } n   } n   sort(res.begin(), res.end()); n   return res; n } n void solve() { n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < (n); i++) cin >> a[i]; n   sort(a.begin(), a.end()); n   long long sum = 0; n   for (int i = 0; i < (n); i++) sum += a[i]; n   int m; n   cin >> m; n   for (int i = 0; i < (m); i++) { n     int q; n     cin >> q; n     cout << sum - a[n - q] << endl; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 305; n struct sss { n   int a, b, c; n   bool operator<(const sss& a) const { return c < a.c; } n }; n vector<sss> v[MAXN * MAXN]; n int cost(int a, int b, int c, int d) { return abs(a - c) + abs(b - d); } n int main() { n   int m, n, i, j, k, p; n   cin >> m >> n >> p; n   for (i = 1; i <= m; i++) { n     for (j = 1; j <= n; j++) { n       int num; n       cin >> num; n       v[num].push_back({i, j, 999999999}); n     } n   } n   i = 1; n   for (j = 0; j < v[i].size(); j++) { n     v[i][j].c = cost(v[i][j].a, v[i][j].b, 1, 1); n   } n   for (i = 2; i <= p; i++) { n     sort((v[i - 1]).begin(), (v[i - 1]).end()); n     for (j = 0; j < v[i].size(); j++) { n       for (k = 0; k < min((int)v[i - 1].size(), m + n + 2); k++) { n         v[i][j].c = n             min(v[i][j].c, v[i - 1][k].c + cost(v[i - 1][k].a, v[i - 1][k].b, n                                                 v[i][j].a, v[i][j].b)); n       } n     } n   } n   int ans = 999999999; n   i = p; n   for (j = 0; j < v[i].size(); j++) ans = min(ans, v[i][j].c); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 11; n int s[N]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &s[i]); n   sort(s + 1, s + 1 + n); n   for (int i = 1, j = 1; i <= n; i++) { n     if (s[i] % s[j] != 0) { n       printf( %d , -1); n       return 0; n     } n   } n   printf( %d n , s[1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2, typename T3> n struct pair3 { n   T1 first; n   T2 second; n   T3 third; n }; n template <typename T1, typename T2, typename T3, typename T4> n struct pair4 { n   T1 first; n   T2 second; n   T3 third; n   T4 fourth; n }; n const long long MOD = 1000000007; n const long double PI = acos(-1.0); n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, m; n   cin >> n; n   vector<long long> ar(n); n   for (long long i = 0; i < n; ++i) { n     cin >> ar[i]; n   } n   vector<vector<long long>> ans(n + 1); n   for (long long len = 1; len <= n; ++len) { n     vector<long long> temp; n     long long mx = -1, mn = INT_MAX; n     multiset<long long> st; n     auto del = [&](long long val) { n       auto it = temp.end(); n       --it; n       while (1) { n         if (*it == val) { n           temp.erase(it); n           break; n         } n         it -= 1; n       } n     }; n     for (long long start = 0; start < n; ++start) { n       if (temp.size() < len) { n         temp.push_back(ar[start]); n         st.insert(ar[start]); n       } else { n         if (ar[start] > *st.begin()) { n           del(*st.begin()); n           st.erase(st.begin()); n           st.insert(ar[start]); n           temp.push_back(ar[start]); n         } n       } n       ans[len] = temp; n     } n   } n   cin >> m; n   long long l, idx; n   while (m--) { n     cin >> l >> idx; n     cout << ans[l][idx - 1] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n char s[1008][1008], last[1008][1008]; n int a[58][58]; n int flag[58], x[58], y[58]; n int ans, tot; n bool in[8][8]; n struct Tprogram { n   void open() { n     freopen( 1.in ,  r , stdin); n     freopen( 1.out ,  w , stdout); n   } n   void close() {} n   void init() { n     scanf( %d%d , &n, &m); n     for (int i = 1; i <= n; i++) scanf( %s , &s[i][1]); n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= m; j++) { n         if (s[i][j] !=  .  && !a[i][j]) n           a[i][j] = a[i + 1][j] = a[i][j + 1] = a[i + 1][j + 1] = ++tot; n         if (s[i][j] !=  . ) { n           int t = s[i][j]; n           t = t <=  B  ? t - 64 : t - 94; n           if (!x[t]) n             x[t] = a[i][j]; n           else n             y[t] = a[i][j]; n         } n       } n   } n   void dfs(int c, int d) { n     if (d > 14) { n       memset(in, 0, sizeof(in)); n       for (int i = 1; i <= 28; i++) n         if (!in[flag[x[i]]][flag[y[i]]]) n           in[flag[x[i]]][flag[y[i]]] = in[flag[y[i]]][flag[x[i]]] = 1; n         else n           return; n       ++ans; n       if (ans == 1) { n         for (int i = 1; i <= n; i++) n           for (int j = 1; j <= m; j++) { n             if (a[i][j]) n               last[i][j] = flag[a[i][j]] - 1 +  0 ; n             else n               last[i][j] =  . ; n           } n       } n       return; n     } n     if (flag[d]) n       dfs(c, d + 1); n     else { n       flag[d] = c; n       for (int i = d + 1; i <= 14; i++) n         if (!flag[i]) { n           flag[i] = c; n           dfs(c + 1, d + 1); n           flag[i] = 0; n         } n       flag[d] = 0; n     } n   } n   void work() { n     dfs(1, 1); n     printf( %d n , ans * 5040); n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= m; j++) printf( %c , last[i][j]); n       printf(  n ); n     } n   } n } Program; n int main() { n   Program.init(); n   Program.work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a; n   cin >> a; n   double x, y, s = 0; n   for (int i = 0; i < a; i++) { n     cin >> x >> y; n     s += y; n   } n   s /= a; n   printf( %.3lf n , s + 5); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) { n     int sum, limit; n     cin >> sum >> limit; n     vector<pair<int, int>> v; n     for (int i = 1; i <= limit; i++) { n       v.push_back({1 << __builtin_ctz(i), i}); n     } n     sort(v.rbegin(), v.rend()); n     vector<int> a; n     int i = 0; n     while (sum) { n       if (i == v.size()) break; n       if (sum >= v[i].first) { n         a.push_back(v[i].second); n         sum -= v[i].first; n       } n       i++; n     } n     if (sum != 0) n       cout <<  -1 n ; n     else { n       cout << a.size() <<   n ; n       for (auto i : a) { n         cout << i <<    ; n       } n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int GEN_SIZE = 6; n vector<int> v, adj[50005]; n int match[50005], match2[50005], vis[50005], ans[50005]; n int V, Vleft; n int conv(vector<int> v, int s) { n   int ans = 0; n   for (int i = 0; i < v.size(); i++) { n     ans *= s; n     ans += v[i]; n   } n   return ans; n } n vector<int> unconv(int p, int s) { n   vector<int> v1; n   for (int i = 0; i < s; i++) { n     v1.push_back(p % s); n     p /= s; n   } n   reverse(v1.begin(), v1.end()); n   return v1; n } n void recur(int x) { n   if (x == GEN_SIZE) { n     int rval = conv(v, GEN_SIZE); n     for (int rem = 0; rem < GEN_SIZE; rem++) { n       vector<int> tmp_v; n       tmp_v.push_back(rem); n       for (int i = 0; i < GEN_SIZE; i++) n         if (rem != i) tmp_v.push_back(v[i]); n       int lval = conv(tmp_v, GEN_SIZE); n       adj[rval].push_back(lval); n     } n     return; n   } n   for (int i = 0; i < GEN_SIZE; i++) { n     v.push_back(i); n     recur(x + 1); n     v.pop_back(); n   } n } n int aug(int v) { n   if (vis[v]) return 0; n   vis[v] = 1; n   for (int j = 0; j < (int)adj[v].size(); j++) { n     int r = adj[v][j]; n     if (match[r] == -1) { n       match[r] = v; n       return 1; n     } n   } n   for (int j = 0; j < (int)adj[v].size(); j++) { n     int r = adj[v][j]; n     if (aug(match[r])) { n       match[r] = v; n       return 1; n     } n   } n   return 0; n } n int T; n vector<pair<pair<int, int>, int> > q; n int main() { n   scanf( %d , &T); n   for (int i = 0; i < T; i++) { n     int N, R; n     scanf( %d%d , &N, &R); n     vector<int> v1; n     v1.push_back(R - 1); n     for (int j = 0; j < N - 1; j++) { n       int x; n       scanf( %d , &x); n       v1.push_back(x - 1); n     } n     int val = conv(v1, N); n     q.push_back(make_pair(make_pair(N, val), i)); n   } n   sort(q.begin(), q.end()); n   int curN = 0; n   for (int i = 0; i < q.size(); i++) { n     if (curN != q[i].first.first) { n       curN = q[i].first.first; n       GEN_SIZE = curN; n       memset(match, -1, sizeof(match)); n       int v = 1; n       for (int i = 0; i < GEN_SIZE; i++) { n         v *= GEN_SIZE; n       } n       for (int i = 0; i < v; i++) adj[i].clear(); n       recur(0); n       for (int i = 0; i < v; i++) { n         memset(vis, 0, sizeof(vis)); n         aug(i); n       } n       for (int i = 0; i < v; i++) match2[match[i]] = i; n     } n     vector<int> qv = unconv(q[i].first.second, curN); n     vector<int> v1 = unconv(match[q[i].first.second], curN); n     ans[q[i].second] = v1[qv[0]] + 1; n   } n   for (int i = 0; i < T; i++) printf( %d n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000][1000]; n int w, h; n int fastMax(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; } n int fastMin(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x; } n int main() { n   scanf( %d%d , &h, &w); n   for (int y = 0; y < h; y++) n     for (int x = 0; x < w; x++) scanf( %d , &a[y][x]); n   int ans = -1; n   for (int y1 = 0; y1 < h; y1++) n     for (int y2 = y1 + 1; y2 < h; y2++) { n       int m1 = -1, m2 = -1; n       for (int x = 0; x < w; x++) { n         const int v = fastMin(a[y1][x], a[y2][x]); n         if (m1 < v) { n           m2 = m1; n           m1 = v; n         } else if (m2 < v) { n           m2 = v; n         } n       } n       ans = fastMax(ans, m2); n     } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, sum = 0, inp; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> inp; n     sum += inp; n   } n   cout << (float)sum / (float)n; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, d, ff = 1, bb = 1; n long long mx; n pair<long long, long long> f[100005]; n int main() { n   scanf( %d%d , &n, &d); n   for (int i = 1; i <= n; i++) scanf( %d%d , &f[i].first, &f[i].second); n   sort(f + 1, f + n + 1); n   for (int i = 2; i <= n; i++) f[i].second += f[i - 1].second; n   while (true) { n     while (f[bb].first - f[ff].first < d && bb != n + 1) bb++; n     mx = max(mx, f[bb - 1].second - f[ff - 1].second); n     ff++; n     if (ff == n + 1) break; n   } n   printf( %I64d , mx); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 7; n const int SS = 4 * N; n long long n, q, k, a, b; n long long seg[SS][2]; n void modif(int ind, int val, int s = 0, int e = n, int v = 1) { n   if (ind < s || ind >= e) return; n   if (e - s == 1) { n     seg[v][0] += val, seg[v][0] = min(seg[v][0], a); n     seg[v][1] += val, seg[v][1] = min(seg[v][1], b); n     return; n   } n   int mid = (s + e) / 2; n   modif(ind, val, s, mid, 2 * v); n   modif(ind, val, mid, e, 2 * v + 1); n   for (int i = 0; i < 2; i++) seg[v][i] = seg[2 * v][i] + seg[2 * v + 1][i]; n } n long long get(int l, int r, int ind, int s = 0, int e = n, int v = 1) { n   if (l >= e || r <= s) return 0; n   if (l <= s && e <= r) return seg[v][ind]; n   int mid = (s + e) / 2; n   long long res = 0; n   res += get(l, r, ind, s, mid, 2 * v); n   res += get(l, r, ind, mid, e, 2 * v + 1); n   return res; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> k >> a >> b >> q; n   while (q--) { n     int t; n     cin >> t; n     if (t == 1) { n       int d, x; n       cin >> d >> x; n       d--; n       modif(d, x); n     } else { n       int d; n       cin >> d; n       d--; n       cout << get(0, d, 1) + get(d + k, n, 0) <<   n ; n     } n   } n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n #define ll long long n #define error(args...) { string _s = #args; replace(_s.begin(), _s.end(),  , ,    ); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } n  n void err(istream_iterator<string> it) {} n template<typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) n { n     cerr << *it <<   =   << a << endl; n     err(++it, args...); n } n  n int main() n { n     ll t; n     cin>>t; n     while(t--) n     { n         ll i,j,n,m,x; n         cin>>n>>m>>x; n         ll c=((x-1)/n)+1; n         ll r=x-(c-1)*n; n         ll ans=(r-1)*m+c; n         cout<<ans<<endl; n     } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (int)1e9; n const int maxN = 1.1e4; n vector<int> g[maxN]; n map<string, int> m[maxN]; n int len = 1; n void add(int v, string s) { n   if (m[v][s] == 0) { n     m[v][s] = len; n     g[v].push_back(len); n     len++; n   } n } n void parse(string s) { n   s = s +    ; n   string cur; n   int v = 0; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==      && cur.size()) { n       add(v, cur); n       v = m[v][cur]; n       cur =   ; n     } else { n       cur.push_back(s[i]); n     } n   } n   cout << endl; n } n int mx[2]; n pair<int, int> dfs(int v, int d) { n   pair<int, int> ans = {0, g[v].size() == 0}; n   for (auto u : g[v]) { n     pair<int, int> p = dfs(u, d + 1); n     ans.first += p.first; n     ans.second += p.second; n   } n   if (d >= 2) { n     mx[0] = max(mx[0], ans.first); n     mx[1] = max(mx[1], ans.second); n   } n   if (g[v].size()) ans.first++; n   return ans; n } n int main() { n   string s; n   while (cin >> s) { n     parse(s); n   } n   dfs(0, 0); n   cout << mx[0] <<     << mx[1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n  n #define pb push_back n #define f first n #define s second n #define ll long long n  n  n ll MOD = 1000000007; n  n int main(){ n  ll t; n  cin>>t; n  while(t--){ n   ll n; n   cin>>n; n   int c =1; n   if(n==1)cout<<1<<endl; n   if(n==2)cout<<-1<<endl; n   if(n>=3){ n    for(int i=0;i<n;i++){ n     for(int j=0;j<n;j++){ n      cout<<c<<   ; n      c+=2; n      if(c>n*n)c=2; n     } n     cout<<endl; n    } n   } n  } n }


#include <bits/stdc++.h> n using namespace std; n int n; n vector<pair<int, int> > A; n int F(int i, int j) { n   return (A[i].first - A[j].first) * (A[i].first - A[j].first) + n          (A[i].second - A[j].second) * (A[i].second - A[j].second); n } n bool F1(vector<int> I) { n   for (int(i) = (0); (i) < (4); ++(i)) n     for (int(j) = (0); (j) < (i); ++(j)) { n       int d = F(I[i], I[j]); n       vector<int> t; n       for (int(k) = (0); (k) < (4); ++(k)) n         if (k != i && k != j) t.push_back(k); n       int dd = F(I[t[0]], I[t[1]]); n       if (d != dd) continue; n       vector<int> v; n       vector<int> q; n       v.push_back(F(I[i], I[t[0]])); n       q.push_back(F(I[t[0]], I[j])); n       v.push_back(F(I[j], I[t[1]])); n       q.push_back(F(I[t[1]], I[i])); n       if (q[0] == q[1] && v[0] == v[1]) return true; n     } n   return false; n } n bool F2(vector<int> I) { n   for (int(i) = (0); (i) < (4); ++(i)) n     for (int(j) = (0); (j) < (i); ++(j)) { n       int d = F(I[i], I[j]); n       vector<int> t; n       for (int(k) = (0); (k) < (4); ++(k)) n         if (k != i && k != j) t.push_back(k); n       int dd = F(I[t[0]], I[t[1]]); n       if (d != dd) continue; n       vector<int> v; n       v.push_back(F(I[i], I[t[0]])); n       v.push_back(F(I[t[0]], I[j])); n       v.push_back(F(I[j], I[t[1]])); n       v.push_back(F(I[t[1]], I[i])); n       bool ok = true; n       for (int(k) = (1); (k) < (4); ++(k)) n         if (v[k] != v[k - 1]) ok = false; n       if (ok) return true; n     } n   return false; n } n int main() { n   for (int(i) = (0); (i) < (8); ++(i)) { n     int x, y; n     cin >> x >> y; n     A.push_back(make_pair(x, y)); n   } n   int res = -1; n   for (int(i) = (0); (i) < ((1 << 8)); ++(i)) { n     vector<int> I; n     vector<int> J; n     for (int(j) = (0); (j) < (8); ++(j)) n       if ((i & (1 << j)) != 0) n         I.push_back(j); n       else n         J.push_back(j); n     if (I.size() != 4) continue; n     if (F1(I) && F2(J)) res = i; n   } n   if (res == -1) n     cout <<  NO  << endl; n   else { n     cout <<  YES  << endl; n     for (int(i) = (0); (i) < (8); ++(i)) n       if ((res & (1 << i)) == 0) cout << i + 1 <<    ; n     cout << endl; n     for (int(i) = (0); (i) < (8); ++(i)) n       if ((res & (1 << i)) != 0) cout << i + 1 <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n int n, a[N] = {}, tot = 0; n bool v[N] = {}; n void dfs(int s) { n   v[s] = true; n   if (!v[s * 2 % n]) dfs(s * 2 % n); n   if (!v[s * 2 % n + 1]) dfs(s * 2 % n + 1); n   a[++tot] = s; n } n int main() { n   cin >> n; n   if (n % 2) n     puts( -1 ); n   else { n     dfs(0); n     for (int i = tot; i >= 0; --i) printf( %d%c , a[i],    n [i == 0]); n   } n   return 0; n } n