#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:102400000,102400000 ) n using namespace std; n void makedata() { n   freopen( input.txt ,  w , stdout); n   fclose(stdout); n } n long long a[250000]; n int n, q; n template <typename T> n class SegmentTree { n  private: n   T *data, *lazy; n   int type; n   void pushup(int rt) { n     if (type == 0) n       data[rt] = data[rt << 1] + data[rt << 1 | 1]; n     else if (type == 1) n       data[rt] = max(data[rt << 1], data[rt << 1 | 1]); n   } n   void pushdown(int rt, T m) { n     if (lazy[rt] == 0) return; n     lazy[rt << 1] += lazy[rt]; n     lazy[rt << 1 | 1] += lazy[rt]; n     if (type == 0) { n       data[rt << 1] += (m - (m >> 1)) * lazy[rt]; n       data[rt << 1 | 1] += (m >> 1) * lazy[rt]; n     } else if (type == 1) { n       data[rt << 1] += lazy[rt]; n       data[rt << 1 | 1] += lazy[rt]; n     } n     lazy[rt] = 0; n   } n  n  public: n   SegmentTree(int n, int t) n       : data((T *)malloc((n << 3) * sizeof(T))), n         lazy((T *)malloc((n << 3) * sizeof(T))), n         type(t) {} n   void Build(T *base, int l, int r, int rt) { n     lazy[rt] = 0; n     if (l == r) n       data[rt] = base[l]; n     else { n       int mid = (l + r) >> 1; n       Build(base, l, mid, rt << 1); n       Build(base, mid + 1, r, rt << 1 | 1); n       pushup(rt); n     } n   } n   void Modify(int l, int r, int rt, int L, int R, T v) { n     if (L <= l && R >= r) { n       lazy[rt] += v; n       if (type == 0) n         data[rt] += v * (r - l + 1); n       else if (type == 1) n         data[rt] += v; n       return; n     } n     pushdown(rt, r - l + 1); n     int mid = (l + r) >> 1; n     if (L <= mid) Modify(l, mid, rt << 1, L, R, v); n     if (R > mid) Modify(mid + 1, r, rt << 1 | 1, L, R, v); n     pushup(rt); n   } n   T QueryPoint(int l, int r, int rt, int val) { n     if (l == r) return data[rt]; n     pushdown(rt, r - l + 1); n     int mid = (l + r) >> 1; n     T ret = 0; n     if (val <= mid) n       ret = QueryPoint(l, mid, rt << 1, val); n     else n       ret = QueryPoint(mid + 1, r, rt << 1 | 1, val); n     pushup(rt); n     return ret; n   } n   T QuerySegment(int l, int r, int rt, int L, int R) { n     pushdown(rt, r - l + 1); n     if (L == l && R == r) return data[rt]; n     int mid = (l + r) >> 1; n     if (R <= mid) return QuerySegment(l, mid, rt << 1, L, R); n     if (mid < L) return QuerySegment(mid + 1, r, rt << 1 | 1, L, R); n     if (type == 0) n       return QuerySegment(l, mid, rt << 1, L, mid) + n              QuerySegment(mid + 1, r, rt << 1 | 1, mid + 1, R); n     else if (type == 1) n       return max(QuerySegment(l, mid, rt << 1, L, mid), n                  QuerySegment(mid + 1, r, rt << 1 | 1, mid + 1, R)); n   } n   int find(int l, int r, int rt, int p, T x) { n     if (r <= p) return -1; n     if (data[rt] < x) return -1; n     if (l == r) return l; n     int mid = (l + r) >> 1; n     if (l > p) { n       if (data[rt << 1] >= x) n         return find(l, mid, rt << 1, p, x); n       else n         return find(mid + 1, r, rt << 1 | 1, p, x); n     } n     int rtn = find(l, mid, rt << 1, p, x); n     if (rtn != -1) return rtn; n     return find(mid + 1, r, rt << 1 | 1, p, x); n   } n }; n SegmentTree<long long> Tsum(250000, 0), Tmax(250000, 1); n int main() { n   std::ios::sync_with_stdio(0), cin.tie(0); n   cin >> n >> q; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   Tsum.Build(a, 1, n, 1); n   Tmax.Build(a, 1, n, 1); n   while (q--) { n     int p; n     long long x; n     cin >> p >> x; n     Tsum.Modify(1, n, 1, p, p, x - a[p]); n     Tmax.Modify(1, n, 1, p, p, x - a[p]); n     a[p] = x; n     if (a[1] == 0) { n       cout << 1 << endl; n       continue; n     } n     int m = 1; n     while (1) { n       long long sum = Tsum.QuerySegment(1, n, 1, 1, m); n       int nex = Tmax.find(1, n, 1, m, sum); n       if (nex == -1) { n         cout << -1 << endl; n         break; n       } n       if (a[nex] == Tsum.QuerySegment(1, n, 1, 1, nex - 1)) { n         cout << nex << endl; n         break; n       } n       m = nex; n     } n   } n   return 0; n } n


#define _CRT_SECURE_NO_WARNINGS n  n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target_tbl( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n  n  n //#include  stdafx.h  n #include  iostream  n #include  fstream  n #include  algorithm  n #include  iomanip  n #include  stack  n #include  queue  n #include  string  n #include  vector  n #include  map  n #include  set  n #include  list  n #include  deque  n #include  complex  n #include  bitset  n #include  cmath  n #include  unordered_set  n #include  unordered_map  n #include  iterator  n #include <ctime> n #include <cassert> n #include  numeric  n #include <cstdio> n #include  random  n #include  chrono  n #include  cstring  n  n  n using namespace std; n  n #define maxi(a,b) a = max(a, b); n #define mini(a,b) a = min(a, b); n  n ///////////////// n #define endl   n  n ///////////////// n  n #define all(a) a.begin(), a.end() n #define rall(a) a.rbegin(), a.rend() n #define sqr(x) ((x) * (x)) n #define SZ(a) ((int)(a.size())) n #define watch(x) cout << (#x) <<   =   << x << endl; n //typedef long long ll; n typedef long double ld; n  n //#define int unsigned int n #define int long long n #define double ld n typedef pair<int, int> pii; n typedef pair<double, double> pdd; n typedef vector<int> vi; n typedef vector<double> vd; n typedef vector<pii> vpii; n typedef vector<vi> vvi; n  n template<class T> n void show(const vector<T> &a) n { n  for (T x : a) n   cout << x <<    ; n  cout << endl; n } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n mt19937_64 rng_64(chrono::steady_clock::now().time_since_epoch().count()); n  n int xx[8] = { 1, -1, 0, 0, 1, -1, 1, -1 }; n int yy[8] = { 0, 0, 1, -1, -1, -1, 1, 1 }; n string dir =  RDLU ; n string travel =  ENWS ; n  n const int N = 2e5 + 50, oo = 1e9 + 500; n //const int mod = 998244353; n const int mod = 1e9 + 7; n //const int mod = 1e9 + 9; n const int M2 = 1000000093, x2 = 27162; n const int M1 = 1000000087, x1 = 241; n const double eps = 1e-18, PI = 2 * acos(0.0); n  n vi check(vi &a, vi &b) n { n  int n = SZ(a); n  int m = SZ(b); n  n  vi c; n  int i = 0, j = 0; n  while(i < n) n  { n   if (j >= m) n    break; n  n   if (a[i] < b[j]) n   { n    i++; n    continue; n   } n  n   if (a[i] == b[j]) n   { n    c.push_back(a[i]); n    i++; n    j++; n    continue; n   } n  n   if (a[i] > b[j]) n   { n    j++; n    continue; n   } n  } n  return c; n } n  n signed main() n { n  ios::sync_with_stdio(0); n  cout.tie(0); cin.tie(0); n  //freopen( input.txt ,  r , stdin); n  //freopen( output.txt ,  w , stdout); n  //freopen( paintbarn.in ,  r , stdin); n  //freopen( paintbarn.out ,  w , stdout); n  n  int T; n  cin >> T; n  n  while (T--) n  { n   int n; n   cin >> n; n  n   vvi g(1 << n); n   for (int i = 0; i < n*(1 << (n - 1)); i++) n   { n    int u, v; n    cin >> u >> v; n    g[u].push_back(v); n    g[v].push_back(u); n   } n  n   for (int i = 0; i < (1 << n); i++) n   { n    sort(all(g[i])); n   }   n   vi p(1 << n, -1); n   vi used(1 << n, 0); n   used[0] = 1; n   p[0] = 0; n  n   for (int i = 1; i < (1 << n); i++) n   { n    vi cur; n    int par = -1; n    for (int j = 0; j < n; j++) n    { n     int ii = i ^ (1 << j); n     if (p[ii] != -1) n     { n      par = p[ii]; n      cur.push_back(p[ii]); n     } n    } n  n    sort(all(cur)); n  n    for (int j: g[par]) //candidates n    { n     if (check(cur, g[j]) == cur && !used[j]) n     { n      used[j] = 1; n      p[i] = j; n      break; n     } n    } n   } n  n   //cout <<  !!!  ; n   show(p); n  n   if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16) n   { n    cout << -1 << endl; n    continue; n   } n  n   vi c(1 << n); n   for (int i = 0; i < (1 << n); i++) n   { n    int col = 0; n    for (int j = 0; j < n; j++) n    { n     if (i & (1 << j)) n      col ^= j; n    } n  n    c[i] = col; n   } n  n   vi cc(1 << n); n   for (int i = 0; i < (1 << n); i++) n   { n    cc[p[i]] = c[i]; n   } n   c = cc; n  n   show(c); n  } n } n  n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n long long t[N], dp[N]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &t[i]); n   } n   dp[0] = 0; n   long long sum = 0; n   for (int i = 1; i <= n; i++) { n     dp[i] = dp[i - 1] + 20; n     for (int j = i - 1; j >= 1; j--) { n       if (t[i] - t[j] < 90) n         dp[i] = min(dp[i], dp[j - 1] + 50); n       else if (t[i] - t[j] < 1440) n         dp[i] = min(dp[i], dp[j - 1] + 120); n       else n         break; n     } n     printf( %lld n , dp[i] - sum); n     sum = dp[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long GCD(long long a, long long b) { n   return (a % b) == 0 ? b : GCD(b, a % b); n } n long long mod(long long x) { return ((x % 998244353 + 998244353) % 998244353); } n bool cmp(const pair<int, int> &left, const pair<int, int> &right) { n   return left.first > right.first || n          (left.first == right.first && left.second < right.second); n } n int main() { n   int n, m, p1, p2, p3, t1, t2, i, j, k, mx = 0; n   vector<pair<int, int> > v; n   cin >> n >> p1 >> p2 >> p3 >> t1 >> t2; n   for (i = 0; i < n; i++) { n     cin >> j >> k; n     v.push_back(make_pair(j, k)); n   } n   sort(v.begin(), v.end()); n   for (i = 0; i < n - 1; i++) { n     mx += (v[i].second - v[i].first) * p1; n     if (v[i + 1].first > v[i].second + t1) { n       mx += t1 * p1; n       if (v[i + 1].first > v[i].second + t1 + t2) { n         mx += t2 * p2; n         mx += (v[i + 1].first - (v[i].second + t1 + t2)) * p3; n       } else n         mx += (v[i + 1].first - (v[i].second + t1)) * p2; n     } else n       mx += (v[i + 1].first - v[i].second) * p1; n   } n   mx += (v[n - 1].second - v[n - 1].first) * p1; n   cout << mx << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n const long long M = (long long)1e9 + 7; n int Int() { n   int x; n   scanf( %d , &x); n   return x; n } n long long Long() { n   long long x; n   scanf( %lld , &x); n   return x; n } n void Char(char *ch) { scanf( %s , ch); } n template <typename t> n inline t abs(t a) { n   if (a >= 0) return a; n   return -a; n } n template <typename T> n inline T modpow(T b, T p, T Mod) { n   T res = 1; n   while (p) { n     if (p & 1) { n       res *= b; n       res %= Mod; n     } n     b *= b; n     b %= Mod; n     p >>= 1; n   } n   return res; n } n template <typename T> n inline T ModInv(T b, T Mod) { n   return modpow(b, Mod - 2, Mod); n } n template <typename T> n inline T pwr(T b, T p) { n   T res = 1; n   while (p) { n     if (p & 1) { n       res *= b; n     } n     b *= b; n     p >>= 1; n   } n   return res; n } n template <typename T> n inline T gcd(T a, T b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n template <typename T> n inline void Int(T &n) { n   n = 0; n   int f = 1; n   register int ch = getchar(); n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) f = -1; n   for (; isdigit(ch); ch = getchar()) n = (n << 3) + (n << 1) + ch -  0 ; n   n = n * f; n } n template <typename T, typename U> n inline void Int(T &a, U &b) { n   Int(a); n   Int(b); n } n template <typename T, typename U, typename W> n inline void Int(T &a, U &b, W &c) { n   Int(a, b); n   Int(c); n } n template <typename T, typename U, typename W, typename X> n inline void Int(T &a, U &b, W &c, X &d) { n   Int(a, b, c); n   Int(d); n } n vector<int> Unique(vector<int> v) { n   sort(v.begin(), v.end()); n   v.erase(unique(v.begin(), v.end()), v.end()); n   return v; n } n vector<string> split(const string &s, char c) { n   vector<string> v; n   stringstream ss(s); n   string x; n   while (getline(ss, x, c)) v.emplace_back(x); n   return move(v); n } n void err(vector<string>::iterator it) {} n template <typename T, typename... Args> n void err(vector<string>::iterator it, T a, Args... args) { n   cout << it->substr((*it)[0] ==    , it->length()) <<   =   << a <<    ; n   err(++it, args...); n } n struct node { n   int cnt; n   node *tree[2]; n   node() { n     cnt = 0; n     tree[0] = tree[1] = NULL; n   } n } * root; n void add(int x) { n   node *cur = root; n   for (int i = 30; i >= 0; i--) { n     int f = (bool)(x & (1 << i)); n     if (cur->tree[f] == NULL) { n       cur->tree[f] = new node(); n     } n     cur = cur->tree[f]; n     cur->cnt++; n   } n } n int sum(int x, int k) { n   int res = 0; n   node *cur = root; n   for (int i = 30; i >= 0; i--) { n     int p = (bool)(x & (1 << i)); n     int q = (bool)(k & (1 << i)); n     if (q == 0) { n       if (p == 1) { n         if (cur->tree[1] != NULL) { n           cur = cur->tree[1]; n         } else { n           return res; n         } n       } else { n         if (cur->tree[0] != NULL) { n           cur = cur->tree[0]; n         } else { n           return res; n         } n       } n     } else { n       if (p == 1) { n         if (cur->tree[1] != NULL) res += cur->tree[1]->cnt; n         if (cur->tree[0] != NULL) { n           cur = cur->tree[0]; n         } else { n           return res; n         } n       } else { n         if (cur->tree[0] != NULL) res += cur->tree[0]->cnt; n         if (cur->tree[1] != NULL) { n           cur = cur->tree[1]; n         } else { n           return res; n         } n       } n     } n   } n   return res; n } n int a[N]; n int main() { n   int t = 1, tc = 0; n   while (t--) { n     root = new node(); n     int n = Int(), k = Int(); n     long long res = 0; n     add(0); n     for (int i = 1; i <= n; i++) { n       a[i] = Int(); n       a[i] ^= a[i - 1]; n       res += sum(a[i], k); n       add(a[i]); n     } n     long long s = (1LL * n * (n + 1LL)) / 2LL; n     printf( %lld n , s - res); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void read(T &x) { n   x = 0; n   char c = getchar(); n   int flag = 0; n   while (c <  0  || c >  9 ) flag |= (c ==  - ), c = getchar(); n   while (c >=  0  && c <=  9 ) n     x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); n   if (flag) x = -x; n } n template <class T> n T _max(T a, T b) { n   return a > b ? a : b; n } n template <class T> n T _min(T a, T b) { n   return a < b ? a : b; n } n template <class T> n bool checkmax(T &a, T b) { n   return b > a ? a = b, 1 : 0; n } n template <class T> n bool checkmin(T &a, T b) { n   return b < a ? a = b, 1 : 0; n } n const int N = 100005; n int n; n long long a[N]; n int sg(long long x) { n   if (x <= 3) n     return 0; n   else if (x <= 15) n     return 1; n   else if (x <= 81) n     return 2; n   else if (x <= 6723) n     return 0; n   else if (x <= 50625) n     return 3; n   else if (x <= (long long)2562890625) n     return 1; n   else n     return 2; n } n void init() { n   read(n); n   for (int i = 1; i <= n; ++i) { n     read(a[i]); n   } n } n void solve() { n   int ans = 0; n   for (int i = 1; i <= n; ++i) { n     ans ^= sg(a[i]); n   } n   if (ans) n     printf( Furlo n ); n   else n     printf( Rublo n ); n } n int main() { n   int T = 1; n   while (T--) { n     init(); n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<int> vans; n set<int> s; n int main() { n   scanf( %d , &n); n   int start = 1; n   for (int i = 1; i <= n; ++i) { n     int x; n     scanf( %d , &x); n     if (s.count(x)) { n       vans.push_back(start); n       vans.push_back(i); n       start = i + 1; n       s.clear(); n     } else { n       s.insert(x); n     } n   } n   if (vans.empty()) { n     printf( -1 n ); n     return 0; n   } else { n     printf( %d n , vans.size() / 2); n     for (int i = 0; i < vans.size() - 2; i += 2) n       printf( %d %d n , vans[i], vans[i + 1]); n     if (!s.empty()) n       printf( %d %d n , vans[vans.size() - 2], n); n     else n       printf( %d %d n , vans[vans.size() - 2], vans[vans.size() - 1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long N = 1e7 + 5; n std::vector<long long> v(N, 0); n std::vector<long long> p; n void sieve() { n   for (long long i = 2; i < N; i++) { n     if (v[i] != 0) continue; n     p.push_back(i); n     for (long long j = 2 * i; j < N; j += i) v[j] = 1; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   long long cnt = 0; n   for (long long i = 2; i <= (long long)sqrt(n); i++) n     if (n % i == 0) cnt++; n   v[0] = 1; n   v[1] = 1; n   if (cnt == 0) { n     cout <<  1  <<   n ; n     cout << n; n     return 0; n   } n   sieve(); n   long long bal = 0; n   if (n % 2 == 0) n     bal = 2; n   else n     bal = 3; n   long long temp = n - bal; n   if (n == 4) { n     cout << 2 <<   n  << 2 <<     << 2; n     return 0; n   } n   for (long long i = 0; i < p.size(); i++) { n     cnt = 0; n     for (long long j = 2; temp - p[i] > 0 and j <= (long long)sqrt(temp - p[i]); n          j++) { n       if ((temp - p[i]) % j == 0) { n         cnt = 1; n         break; n       } n     } n     if (!cnt) { n       cout << 3 <<   n  << bal <<     << p[i] <<     << temp - p[i]; n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, int>; n using PII = pair<ll, ll>; n using piii = pair<pii, pii>; n using Pi = pair<int, pii>; n using Graph = vector<vector<int>>; n const int dx[4] = {0, -1, 1, 0}; n const int dy[4] = {-1, 0, 0, 1}; n bool check(int x, int y) { n   if (0 <= x && x < 55 && 0 <= y && y < 55) n     return true; n   else n     return false; n } n const ll INF = 1e+7; n int gcd(int x, int y) { n   if (x < y) swap(x, y); n   if (y == 0) return x; n   return gcd(y, x % y); n } n void mul(ll a, ll b) { a = a * b % INF; } n using Graph = vector<vector<int>>; n long long modinv(long long a, long long m) { n   long long b = m, u = 1, v = 0; n   while (b) { n     long long t = a / b; n     a -= t * b; n     swap(a, b); n     u -= t * v; n     swap(u, v); n   } n   u %= m; n   if (u < 0) u += m; n   return u; n } n const double PI = 3.14159265358979323846; n const int MAX = 510000; n ll fac[MAX], finv[MAX], inv[MAX]; 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 < MAX; ++i) { n     fac[i] = fac[i - 1] * i % INF; n     inv[i] = INF - inv[INF % i] * (INF / i) % INF; n     finv[i] = finv[i - 1] * inv[i] % INF; n   } n } n ll COM(int n, int k) { n   if (n < k) return 0; n   if (n < 0 || k < 0) return 0; n   return fac[n] * (finv[k] * finv[n - k] % INF) % INF; n } n double Euclidean_distance(double x1, double y1, double x2, double y2) { n   return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); n } n int prime[1001000]; n bool is_prime[1001010]; n int sieve(int n) { n   int p = 0; n   for (int i = 0; i <= n; ++i) { n     is_prime[i] = true; n   } n   is_prime[0] = false; n   is_prime[1] = false; n   for (int i = 2; i <= n; ++i) { n     if (is_prime[i]) { n       prime[p] = i; n       p++; n       for (int j = 2 * i; j <= n; j += i) { n         is_prime[j] = false; n       } n     } n   } n   return p; n } n map<int, int> prime_factor(int n) { n   map<int, int> res; n   for (int i = 2; i * i <= n; ++i) { n     while (n % i == 0) { n       ++res[i]; n       n /= i; n     } n   } n   if (n != 1) res[n] = 1; n   return res; n } n ll powmod(ll a, ll k, ll mod) { n   ll ap = a, ans = 1; n   while (k) { n     if (k & 1) { n       ans *= ap; n       ans %= mod; n     } n     ap = ap * ap; n     ap %= mod; n     k >>= 1; n   } n   return ans; n } n ll invi(ll a, ll mod) { return powmod(a, mod - 2, mod); } n int main() { n   int t; n   cin >> t; n   while (t--) { n     ll n, m; n     cin >> n >> m; n     if (n == 1 || m == 1) { n       cout <<  YES  << endl; n     } else if (n == 2 && m == 2) { n       cout <<  YES  << endl; n     } else { n       cout <<  NO  << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 4; n set<int> ts[N]; n map<int, int> tmp; n int n, k, a[N]; n void add(int v) { n   auto it1 = tmp.find(v); n   if (it1 == tmp.end()) { n     tmp.emplace(v, 1); n     ts[1].insert(v); n   } else { n     ts[it1->second].erase(v); n     it1->second++; n     ts[it1->second].insert(v); n   } n } n void del(int v) { n   auto it1 = tmp.find(v); n   if (it1->second == 1) { n     ts[1].erase(v); n     tmp.erase(it1); n   } else { n     ts[it1->second].erase(v); n     it1->second--; n     ts[it1->second].insert(v); n   } n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %d , a + i); n   for (int i = 1; i <= n; i++) { n     add(a[i]); n     if (i > k) del(a[i - k]); n     if (i >= k) { n       if (ts[1].empty()) n         puts( Nothing ); n       else n         printf( %d n , *(ts[1].rbegin())); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n bool issafe(int i, int j, int n, int m) { n   return i >= 0 && j >= 0 && i < n && j < m; n } n char s[] = { D ,  L ,  R ,  U }; n int dx[] = {+1, 0, 0, -1}; n int dy[] = {0, -1, +1, 0}; n void solve() { n   int n, m, k; n   cin >> n >> m >> k; n   char a[n][m]; n   int dist[n][m]; n   pair<int, int> start; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       dist[i][j] = -1; n       cin >> a[i][j]; n       if (a[i][j] ==  X ) { n         start = {i, j}; n       } n     } n   } n   if (k & 1) { n     cout <<  IMPOSSIBLE  n          <<   n ; n     return; n   } n   queue<pair<int, int> > q; n   q.push(start); n   dist[start.first][start.second] = 0; n   while (!q.empty()) { n     pair<int, int> curr = q.front(); n     q.pop(); n     for (int i = 0; i < 4; i++) { n       int nx = curr.first + dx[i], ny = curr.second + dy[i]; n       if (issafe(nx, ny, n, m) && a[nx][ny] !=  *  && dist[nx][ny] == -1) { n         q.push({nx, ny}); n         dist[nx][ny] = dist[curr.first][curr.second] + 1; n       } n     } n   } n   string ans =   ; n   pair<int, int> curr = start; n   bool flag = 0; n   while (k) { n     flag = 0; n     for (int i = 0; i < 4; i++) { n       int nx = curr.first + dx[i], ny = curr.second + dy[i]; n       if (issafe(nx, ny, n, m) && a[nx][ny] !=  *  && dist[nx][ny] <= k) { n         flag = 1; n         ans += s[i]; n         curr = {nx, ny}; n         k--; n         break; n       } n     } n     if (!flag) break; n   } n   if (!flag) { n     cout <<  IMPOSSIBLE  n          <<   n ; n     return; n   } n   cout << ans <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4005; n int n; n struct child { n   long long v, d, p; n   int idx; n   child(long long v, long long d, long long p, int idx) n       : v(v), d(d), p(p), idx(idx) {} n }; n long long v, d, p; n vector<child *> ve; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   for (int i = (0); i < (n); i++) { n     cin >> v >> d >> p; n     ve.push_back(new child(v, d, p, i)); n   } n   vector<int> ans; n   while (!ve.empty()) { n     ans.push_back(ve[0]->idx); n     ve[0]->p = -1; n     for (int i = (1); i < (((int)(ve).size())); i++) { n       ve[i]->p -= max(0LL, ve[0]->v - i + 1); n       if (ve[i]->p < 0) { n         for (int j = (i + 1); j < (((int)(ve).size())); j++) { n           ve[j]->p -= ve[i]->d; n         } n       } n     } n     ve.erase(stable_partition((ve).begin(), (ve).end(), n                               [&](child *c) { return c->p >= 0; }), n              ve.end()); n   } n   cout << ((int)(ans).size()) <<   n ; n   for (int x : ans) { n     cout << x + 1 <<    ; n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N, M; n   cin >> N >> M; n   vector<string> st(N); n   vector<char> max(M,  0 ); n   for (int i = 0; i < N; i++) { n     cin >> st[i]; n     for (int j = 0; j < M; j++) n       if (st[i][j] > max[j]) max[j] = st[i][j]; n   } n   int sum = 0; n   for (int i = 0; i < st.size(); i++) n     for (int j = 0; j < st[i].length(); j++) n       if (st[i][j] == max[j]) { n         sum++; n         break; n       } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n char s[1005]; n int a[25], mark[6071], dp[1005][6071], ch[6071][12], link[6071], tl, ans = 1e9, n                                                                      x, n; n void insert(int len) { n   int rt = 0; n   for (int i = 1; i <= len; ++i) { n     if (!ch[rt][a[i]]) ch[rt][a[i]] = ++tl; n     rt = ch[rt][a[i]]; n   } n   mark[rt] = 1; n } n void build() { n   std::queue<int> q; n   std::memset(link, -1, sizeof(link)); n   link[0] = 0; n   for (int i = 0; i < 10; ++i) n     if (ch[0][i]) { n       q.push(ch[0][i]); n       link[ch[0][i]] = 0; n     } n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     mark[u] |= mark[link[u]]; n     for (int i = 0; i < 10; ++i) { n       if (ch[u][i]) { n         mark[ch[u][i]] |= mark[u]; n         link[ch[u][i]] = ch[link[u]][i]; n         q.push(ch[u][i]); n       } else n         ch[u][i] = ch[link[u]][i]; n     } n   } n } n void dfs(int u, int sum) { n   if (sum == x) { n     for (int l = 1; l < u; ++l) { n       int s = 0; n       for (int r = l; r < u; ++r) { n         s += a[r]; n         if (s != x && x % s == 0) return; n       } n     } n     insert(u - 1); n     return; n   } n   for (int j = 1; sum + j <= x && j < 10; ++j) { n     a[u] = j; n     dfs(u + 1, sum + j); n   } n } n void generate() { n   dfs(1, 0); n   build(); n } n int main() { n   scanf( %s%d , s + 1, &x); n   n = std::strlen(s + 1); n   generate(); n   std::memset(dp, 63, sizeof(dp)); n   dp[1][0] = 0; n   for (int i = 1; i <= n; ++i) { n     for (int j = 0; j <= tl; ++j) { n       if (!mark[ch[j][s[i] -  0 ]]) n         dp[i + 1][ch[j][s[i] -  0 ]] = n             std::min(dp[i + 1][ch[j][s[i] -  0 ]], dp[i][j]); n       dp[i + 1][j] = std::min(dp[i + 1][j], dp[i][j] + 1); n     } n   } n   for (int j = 0; j <= tl; ++j) ans = std::min(ans, dp[n + 1][j]); n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Tree { n   int ch[10]; n   int len; n   int fail; n } T[100010]; n int tot; n void insert(char* str, int len) { n   int now = 0; n   for (int i = 0; i < len; i++) { n     if (!T[now].ch[str[i] -  0 ]) { n       T[now].ch[str[i] -  0 ] = ++tot; n       T[tot].len = i + 1; n     } n     now = T[now].ch[str[i] -  0 ]; n   } n   return; n } n queue<int> q; n void bfs() { n   for (int i = 0; i < 10; i++) n     if (T[0].ch[i]) { n       q.push(T[0].ch[i]); n       T[T[0].ch[i]].fail = 0; n     } n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (int i = 0; i < 10; i++) n       if (T[u].ch[i]) { n         T[T[u].ch[i]].fail = T[T[u].fail].ch[i]; n         q.push(T[u].ch[i]); n       } else n         T[u].ch[i] = T[T[u].fail].ch[i]; n   } n   return; n } n int d, D; n int dp[61][60010][2][2]; n void add(int& x, int y) { n   x += y; n   if (x >= 1000000007) x -= 1000000007; n   return; n } n int solve(int* a) { n   memset(dp, 0, sizeof(dp)); n   dp[d][0][0][0] = 1; n   for (int i = d; i >= 1; i--) n     for (int j = 0; j <= tot; j++) n       for (int k = 0; k < 2; k++) { n         for (int l = 0; l <= a[i]; l++) { n           int v = T[j].ch[l]; n           int tk = k || (T[v].len >= D); n           int o = (l < a[i]); n           add(dp[i - 1][v][tk][o], dp[i][j][k][0]); n         } n         for (int l = 0; l <= 9; l++) { n           int v = T[j].ch[l]; n           int tk = k || (T[v].len >= D); n           add(dp[i - 1][v][tk][1], dp[i][j][k][1]); n         } n       } n   int ans = 0; n   for (int i = 0; i <= tot; i++) { n     add(ans, dp[0][i][1][0]); n     add(ans, dp[0][i][1][1]); n   } n   return ans; n } n char x[61], y[61], str[1010]; n int tmp[61]; n int main() { n   scanf( %s , str); n   scanf( %s , x + 1); n   scanf( %s , y + 1); n   int len = strlen(str); n   d = strlen(x + 1); n   D = d / 2; n   for (int i = 0; i + D <= len; i++) insert(str + i, D); n   bfs(); n   for (int i = 1; i <= d; i++) tmp[i] = x[d - i + 1] -  0 ; n   tmp[1]--; n   for (int i = 1; i <= d; i++) n     if (tmp[i] < 0) { n       tmp[i + 1]--; n       tmp[i] += 10; n     } n   int ans1 = solve(tmp); n   for (int i = 1; i <= d; i++) tmp[i] = y[d - i + 1] -  0 ; n   int ans2 = solve(tmp); n   printf( %d n , (ans2 - ans1 + 1000000007) % 1000000007); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void prepare() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   freopen( input.txt ,  r , stdin); n   freopen( output.txt ,  w , stdout); n } n int main() { n   long double d, l, v1, v2; n   cin >> d >> l >> v1 >> v2; n   long double ans = (l - d) / (v1 + v2); n   cout << fixed << setprecision(10) << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T read() { n   T x = 0, f = 1; n   char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) f = -f; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = (x << 3) + (x << 1) + (c - 48); n     c = getchar(); n   } n   return x * f; n } n const int inf = 1e9 + 1e7, MAXN = 2e5 + 1e1; n int a[MAXN], b[MAXN], q0[MAXN], q1[MAXN], n, ans, cnt, a0, a1, l0, r0, l1, r1; n void Solve() { n   n = read<int>(), ans = cnt = a0 = a1 = l0 = r0 = l1 = r1 = 0; n   for (int i = 1; i <= n; i++) scanf( %1d , &a[i]); n   for (int i = 1; i <= n; i++) { n     if (a[i]) { n       if (a1) n         --a1, ++a0, b[i] = q1[l1], q0[r0++] = q1[l1++]; n       else n         ++ans, ++a0, b[i] = (++cnt), q0[r0++] = cnt; n     } else { n       if (a0) n         --a0, ++a1, b[i] = q0[l0], q1[r1++] = q0[l0++]; n       else n         ++ans, ++a1, b[i] = (++cnt), q1[r1++] = cnt; n     } n   } n   printf( %d n , ans); n   for (int i = 1; i <= n; i++) printf( %d  , b[i]); n   printf(  n ); n   return; n } n int main(void) { n   int t = read<int>(); n   while (t--) Solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, a, b, c; n int main() { n   cin >> t; n   while (t--) { n     cin >> a >> b >> c; n     int res = 0; n     if (c & 1) res = 3; n     if (res) { n       if (b & 1) res = 1; n       if (a & 1) n         res = 0; n       else n         res = 1; n     } else { n       if (b & 1) res = 2; n       if (a & 1) n         res = 1; n       else n         res = 0; n     } n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T inline sqr(T x) { n   return x * x; n } n template <class T> n inline void relaxMin(T &a, T b) { n   a = min(a, b); n } n template <class T> n inline void relaxMax(T &a, T b) { n   a = max(a, b); n } n template <class T> n inline T sign(T x) { n   return x > 0 ? 1 : (x < 0 ? -1 : 0); n } n template <class T> n inline T myAbs(T a) { n   return a > 0 ? a : -a; n } n template <class T> n T iteratorK(T a, int k) { n   while (k--) a++; n   return a; n } n void err(const char *fmt, ...) {} n void Assert(bool f, const char *fmt =   , ...) { n   if (!f) { n     va_list list; n     va_start(list, fmt); n     err(fmt, list); n     exit(1); n   } n } n unsigned R() { return (rand() << 15) + rand(); } n double start = clock(); n void TimeStamp() { n   fprintf(stderr,  time = %.2f n , (clock() - start) / CLOCKS_PER_SEC); n   start = clock(); n } n const int maxN = (int)1e5 + 10; n int n, a, d; n int main() { n   scanf( %d%d%d , &n, &a, &d); n   long double minT = 0; n   for (int i = 0; i < (int)(n); i++) { n     int t, v; n     scanf( %d%d , &t, &v); n     long double D, T, T0 = (long double)v / a; n     if ((D = 0.5 * T0 * T0 * a) >= d) n       T = sqrt((long double)2.0 * d / a); n     else n       T = T0 + (long double)(d - D) / v; n     T += t; n     minT = T = max(minT, T); n     printf( %.20f n , (double)T); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n const int INF = (int)1e9 + 7; n int parent[(int)1e5 + 2]; n int deep[(int)1e5 + 2]; n long long setSum[(int)1e5 + 2]; n int a[(int)1e5 + 2]; n inline void input() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n void make_set(int v) { n   parent[v] = v; n   setSum[v] = a[v]; n   deep[v] = 0; n } n int find_set(int v) { n   if (v == parent[v]) return v; n   parent[v] = find_set(parent[v]); n   return parent[v]; n } n void union_sets(int a, int b) { n   a = find_set(a); n   b = find_set(b); n   if (a != b) { n     if (deep[a] < deep[b]) swap(a, b); n     if (deep[a] = deep[b]) ++deep[a]; n     setSum[parent[a]] += setSum[parent[b]]; n     parent[b] = a; n   } n } n int main() { n   input(); n   int n; n   cin >> n; n   vector<int> p(n + 1); n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n   } n   for (int i = 1; i <= n; ++i) { n     cin >> p[i]; n   } n   long long mxSum = 0; n   vector<long long> ans = {0}; n   for (int i = n; i > 0; --i) { n     make_set(p[i]); n     if (find_set(p[i] - 1) != 0) { n       union_sets(p[i], p[i] - 1); n     } n     if (find_set(p[i] + 1) != 0) { n       union_sets(p[i], p[i] + 1); n     } n     mxSum = max(setSum[p[i]], mxSum); n     ans.push_back(mxSum); n   } n   for (int i = n - 1; i >= 0; --i) { n     cout << ans[i] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int a[100005]; n long long int n, len, k; n class data { n  public: n   set<pair<long long int, long long int> > mx, mn; n   long long int sum1, ksum; n   data() { ksum = 0, sum1 = 0; } n   void ins(long long int v, long long int index) { n     sum1 += v; n     ksum += v; n     mx.insert(make_pair(v, index)); n     if (int((mx).size()) > k) { n       pair<long long int, long long int> p = *(mx.begin()); n       mx.erase(p); n       ksum -= p.first; n       p.first = -p.first; n       mn.insert(p); n     } n   } n   void rem(long long int v, long long int index) { n     sum1 -= v; n     if (mx.count(make_pair(v, index))) { n       ksum -= v; n       mx.erase(make_pair(v, index)); n       if (int((mn).size()) > 0) { n         pair<long long int, long long int> p = *(mn.begin()); n         mn.erase(p); n         p.first = -p.first; n         mx.insert(p); n         ksum += p.first; n       } n     } else if (mn.count(make_pair(-v, index))) { n       mn.erase(make_pair(-v, index)); n     } n   } n }; n data ap, an; n void ADD(long long int v, long long int index) { n   if (v < 0) n     an.ins(-v, index); n   else n     ap.ins(v, index); n } n void REMOVE(long long int v, long long int index) { n   if (v < 0) n     an.rem(-v, index); n   else n     ap.rem(v, index); n } n int main() { n   long long int ans = 0; n   cin >> n >> len; n   for (long long int i = 0; i < n; i++) cin >> a[i]; n   cin >> k; n   for (long long int i = 0; i < n; i++) { n     ADD(a[i], i); n     if (i >= len) REMOVE(a[i - len], i - len); n     if (i >= len - 1) n       ans = max(ans, max(ap.sum1 - an.sum1 + 2 * an.ksum, n                          an.sum1 + 2 * ap.ksum - ap.sum1)); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long f[20]; n vector<long long> lu; n void go(int x, int y, long long p) { n   if (x == y) return; n   lu.push_back(p * 10 + 4); n   lu.push_back(p * 10 + 7); n   go(x, y + 1, p * 10 + 4); n   go(x, y + 1, p * 10 + 7); n } n bool ok(long long x) { n   if (!x) return 0; n   while (x) { n     if (!(x % 10 == 4 || x % 10 == 7)) return 0; n     x /= 10; n   } n   return 1; n } n void radix(long long k, vector<long long> &num) { n   k--; n   int len = num.size(); n   int aw, get; n   for (get = 1, aw = len - 1; get < len; get++, aw--) n     if (f[get] > k) break; n   get--; n   while (k) { n     long long a = k / f[get]; n     k = k % f[get]; n     swap(num[aw], num[aw + a]); n     sort(num.begin() + aw + 1, num.end()); n     aw++; n     get--; n   } n } n int main() { n   go(13, 0, 0); n   sort(lu.begin(), lu.end()); n   int p = lu.size(); n   f[0] = 1; n   for (long long i = 1; i < 15; i++) f[i] = f[i - 1] * i; n   long long n, k; n   vector<long long> num; n   while (cin >> n >> k) { n     long long ans = 0; n     if (n > 13) { n       for (long long i = 12; i >= 0; i--) num.push_back(n - i); n       for (int i = 0; i < p; i++) { n         if (lu[i] <= n - 13) ans++; n       } n       radix(k, num); n       for (long long i = 12, j = 0; i >= 0; i--, j++) { n         if (ok(num[j]) && ok(n - i)) ans++; n       } n     } else if (f[n] >= k) { n       for (int i = 0; i < n; i++) num.push_back(i + 1); n       radix(k, num); n       for (int i = 0; i < n; i++) { n         if (ok(num[i]) && (ok(i + 1))) ans++; n       } n     } n     if (n <= 13) n       if (f[n] < k) ans = -1; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline bool cmin(T &a, const T &b) { n   return a > b ? a = b, 1 : 0; n } n template <typename T> n inline bool cmax(T &a, const T &b) { n   return a < b ? a = b, 1 : 0; n } n template <typename L, typename R> n ostream &operator<<(ostream &out, pair<L, R> t) { n   return out <<  (  << t.first <<  ,  << t.second <<  ) ; n } n template <typename T> n ostream &operator<<(ostream &out, vector<T> t) { n   for (unsigned i = 0; i < t.size(); i++) n     out << (  ( [!i]) << t[i] << ( ,) [i == t.size() - 1]); n   return out; n } n const int NX = 101234; n int n, m, k, q; n int fa[NX], edge[NX][6], intree[NX][6], f[NX], g[NX]; n int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } n int main() { n   scanf( %d%d%d , &n, &k, &m); n   for (int i = 1; i <= m; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     if (u > v) swap(u, v); n     int width = v - u; n     edge[u][width] = true; n   } n   for (int i = 1; i <= n; i++) fa[i] = i; n   for (int i = 1; i <= k; i++) { n     for (int j = 1; j <= n; j++) { n       if (!edge[j][i]) continue; n       int u = find(j), v = find(j + i); n       if (u != v) { n         fa[u] = v; n         intree[j][i] = true; n         ++f[i + j]; n         ++g[j]; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     f[i] += f[i - 1]; n     g[i] += g[i - 1]; n   } n   int q; n   scanf( %d , &q); n   while (q--) { n     int l, r; n     scanf( %d%d , &l, &r); n     int ans = r - l + 1 - (f[r] - g[l - 1]); n     for (int i = l - 1; i + k > r; i--) n       for (int j = k; i + j > r; j--) { n         if (intree[i][j]) --ans; n       } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   long long n = s.size(), i, j; n   long long displaced_left[n], displaced_right[n], minimum = INT_MAX; n   for (i = 0, j = s.size() - 1; i < n; i++, j--) { n     if (i == 0) { n       displaced_left[i] = 0, displaced_right[j] = 0; n       continue; n     } n     if (s[i - 1] >=  a  && s[i - 1] <=  z ) n       displaced_left[i] = displaced_left[i - 1] + 1; n     else n       displaced_left[i] = displaced_left[i - 1]; n     if (s[j + 1] >=  A  && s[j + 1] <=  Z ) n       displaced_right[j] = 1 + displaced_right[j + 1]; n     else n       displaced_right[j] = displaced_right[j + 1]; n   } n   for (i = 0; i < n; i++) n     minimum = min(minimum, displaced_left[i] + displaced_right[i]); n   cout << minimum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, ch; n   cin >> n; n   if (n == 0) n     cout <<  O-|-OOOO n ; n   else n     while (n != 0) { n       ch = n % 10; n       string s; n       switch (ch) { n         case 0: n           s =  O-|-OOOO ; n           break; n         case 1: n           s =  O-|O-OOO ; n           break; n         case 2: n           s =  O-|OO-OO ; n           break; n         case 3: n           s =  O-|OOO-O ; n           break; n         case 4: n           s =  O-|OOOO- ; n           break; n         case 5: n           s =  -O|-OOOO ; n           break; n         case 6: n           s =  -O|O-OOO ; n           break; n         case 7: n           s =  -O|OO-OO ; n           break; n         case 8: n           s =  -O|OOO-O ; n           break; n         case 9: n           s =  -O|OOOO- ; n           break; n       } n       cout << s <<   n ; n       n /= 10; n     } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, q, m; n   cin >> n >> q >> m; n   vector<int> b(n + 1), a(m + 1); n   for (int i = 1; i <= n; i++) cin >> b[i]; n   int t[q], l[q], r[q]; n   for (int i = 0; i < q; i++) cin >> t[i] >> l[i] >> r[i]; n   for (int i = 1; i <= m; i++) { n     cin >> a[i]; n   } n   for (int i = 1; i <= m; i++) { n     for (int j = q - 1; j >= 0; j--) { n       if (l[j] > a[i] || r[j] < a[i]) continue; n       if (t[j] & 1) { n         if (a[i] == l[j]) { n           a[i] = r[j]; n         } else { n           --a[i]; n         } n       } else { n         a[i] = r[j] + l[j] - a[i]; n       } n     } n   } n   for (int i = 1; i <= m; i++) { n     cout << b[a[i]] <<    ; n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <complex> n #include <queue> n #include <set> n #include <unordered_set> n #include <list> n #include <chrono> n #include <random> n #include <iostream> n #include <algorithm> n #include <cmath> n #include <string> n #include <vector> n #include <map> n #include <unordered_map> n #include <stack> n #include <iomanip> n #include <fstream> n  n using namespace std; n  n const int N = 200001; n int cntA[N], cntB[N], A[N], B[N]; n  n int gcd(int a, int b) n { n     if (b == 0) return a; n     else return gcd(b, a % b); n } n  n int lcm(int a, int b) n { n     return a * b / gcd(a, b); n } n  n bool isPrime(int n) n { n     int d = 2; n     while (d * d <= n && n % d) d++; n     return n - 1 && d * d > n; n } n  n void localTest() n { n     int a, b, k; cin >> a >> b >> k; n     memset(A, 0, N); n     memset(B, 0, N); n     memset(cntA, 0, N); n     memset(cntB, 0, N); n     for (int i = 0; i < k; i++) n     { n         cin >> A[i]; n         cntA[A[i]]++; n     } n     for (int i = 0; i < k; i++) n     { n         cin >> B[i]; n         cntB[B[i]]++; n     } n  n     long long ans = 0; n     for (int i = 0; i < k; i++) ans += k - cntA[A[i]] - cntB[B[i]] + 1; n  n     cout << ans / 2 <<   n ; n     //не недооценивай первую задачу, если она для первого и второго дивизионов n     //внимательно читай условия n     //всё будет хорошо n } n  n int main() n { n     cin.tie(nullptr)->sync_with_stdio(false); n     int globalTests = 1; n  n     cin >> globalTests; n  n     while (globalTests--) localTest(); n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   vector<pair<int, int>> v; n   int row[1010] = {0}, col[1010] = {0}; n   char g[1010][1010]; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       cin >> g[i][j]; n       if (g[i][j] ==  * ) row[i]++, col[j]++, v.push_back({i, j}); n     }; n   int mx_col = 0, mx_row = 0, max_covered = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (row[i] + col[j] - (g[i][j] ==  * ) > max_covered) n         mx_row = i, mx_col = j, n         max_covered = row[i] + col[j] - (g[i][j] ==  * ); n   for (auto i : v) n     if (i.first != mx_row && i.second != mx_col) { n       cout <<  NO ; n       return 0; n     } n   cout <<  YES  << endl << mx_row + 1 <<     << mx_col + 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char s[110]; n   while (scanf( %s , s) != EOF) { n     int l = strlen(s); n     int ans = 0; n     for (int i = 0; i < l; i++) { n       if (s[i] ==  Q ) { n         for (int j = i + 1; j < l; j++) { n           if (s[j] ==  A ) { n             for (int k = j + 1; k < l; k++) { n               if (s[k] ==  Q ) { n                 ans++; n               } n             } n           } n         } n       } n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   unsigned int m = 0, n = 0, s; n   scanf( %d%d , &m, &n); n   s = (m * n) / 2; n   printf( %d , s); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:2560000000000 ) n using namespace std; n map<int, int> sell; n map<int, int> buy; n int main() { n   int n, s; n   cin >> n >> s; n   for (int i = 1; i <= n; i++) { n     int p, d; n     char c; n     cin >> c >> p >> d; n     if (c ==  S ) { n       sell[p] += d; n     } else { n       buy[-p] += d; n     } n   } n   map<int, int>::iterator it = sell.begin(); n   vector<pair<int, int>> ans; n   for (int i = 1; i <= s && it != sell.end(); i++) { n     ans.push_back(make_pair(it->first, it->second)); n     it++; n   } n   for (int i = ans.size() - 1; i >= 0; i--) { n     cout <<  S   << ans[i].first <<     << ans[i].second << endl; n   } n   map<int, int>::iterator it1 = buy.begin(); n   for (int i = 1; i <= s && it1 != buy.end(); i++) { n     cout <<  B   << -it1->first <<     << it1->second << endl; n     it1++; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1003; n const int mod = 1e9 + 7; n int a[N][N]; n int c[N]; n int n; n int sum; n int main() { n   int i, j; n   scanf( %d , &n); n   for (i = 0; i < n; i++) n     for (j = 0; j < n; j++) scanf( %d , &a[i][j]); n   for (i = 0; i < n; i++) sum += a[i][i] * a[i][i]; n   sum %= 2; n   int m; n   scanf( %d , &m); n   int op, x; n   while (m--) { n     scanf( %d , &op); n     if (op == 3) { n       printf( %d , sum); n     } else { n       scanf( %d , &x); n       sum = sum - a[x][x] + (a[x][x] ^ 1); n       a[x][x] ^= 1; n       sum &= 1; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A, typename B> n ostream& operator<<(ostream& os, const pair<A, B>& p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename C> n ostream& operator<<(ostream& os, const vector<C>& v) { n   for (__typeof((v).begin()) __it = (v).begin(); __it != (v).end(); __it++) n     os << *(__it) <<    ; n   return os; n } n const int inf = 0x3f3f3f3f; n const double eps = 1e-5; n const int maxm = 123; n const int maxn = 1024; n int ms[maxn]; n int res[maxn]; n int n; n bool can(int pos, int col) { n   int prev = (pos + n - 1) % n; n   if (res[prev] == col) return false; n   int next = (pos + n + 1) % n; n   if (res[next] == col) return false; n   return true; n } n int main() { n   int m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int t; n     cin >> t; n     ms[i] = t; n   } n   int maxpr = -1; n   bool fail = false; n   memset(res, 0x3f, sizeof(res)); n   for (int i = 0; i < n; i++) { n     vector<pair<int, int> > cs; n     for (int j = 0; j < m; j++) n       if (ms[j] && can(i, j)) { n         pair<int, int> t(ms[j], j); n         if (j == maxpr) t.first += inf; n         cs.push_back(t); n       } n     if ((int((cs).size())) == 0) { n       fail = true; n       break; n     } n     sort(cs.begin(), cs.end()); n     int ncolor = cs.rbegin()->second; n     ms[ncolor]--; n     res[i] = ncolor; n     if (!i) maxpr = ncolor; n   } n   if (fail) { n     puts( -1 ); n     return 0; n   } n   for (int i = 0; i < n; i++) cout << res[i] + 1 <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n struct que { n   int l, r, id; n   long long ans; n } q[N]; n int block, s[N], tong[N * 20], k; n bool cmp1(que i, que j) { n   if (i.l / block != j.l / block) return i.l / block < j.l / block; n   return i.r < j.r; n } n bool cmp2(que i, que j) { return i.id < j.id; } n int read() { n   int x = 0; n   char ch = getchar(); n   while (!isdigit(ch)) ch = getchar(); n   while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar(); n   return x; n } n inline void print(long long x) { n   if (x > 9) print(x / 10); n   putchar(x % 10 +  0 ); n } n long long ans = 0; n void add(int x) { n   ans += (long long)tong[s[x] ^ k]; n   tong[s[x]]++; n } n void del(int x) { n   tong[s[x]]--; n   ans -= (long long)tong[s[x] ^ k]; n } n int main() { n   int n, m, x; n   scanf( %d%d%d , &n, &m, &k); n   block = sqrt(n); n   for (int i = 1; i <= n; ++i) scanf( %d , &x), s[i] = s[i - 1] ^ x; n   for (int i = 1; i <= m; ++i) n     scanf( %d%d , &q[i].l, &q[i].r), q[i].l--, q[i].id = i; n   sort(q + 1, q + m + 1, cmp1); n   int l = 0, r = 0; n   tong[0] = 1; n   for (int i = 1; i <= m; i++) { n     for (; l < q[i].l; l++) del(l); n     for (; l > q[i].l; l--) add(l - 1); n     for (; r < q[i].r; r++) add(r + 1); n     for (; r > q[i].r; r--) del(r); n     q[i].ans = ans; n   } n   sort(q + 1, q + m + 1, cmp2); n   for (int i = 1; i <= m; ++i) cout << q[i].ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-10; n const int oo = ~0u >> 2, mo = (int)1e9 + 7; n const int mn = 110000, mm = 110000 * 2, ml = 18; n struct Edge { n   int y, l; n } E[mm]; n int g[mn], d[mn], s[mn], blg[mn]; n int fa[mn][ml], f[mn]; n bool o[mn]; n int n, m, tt(1); n void add(int x, int y) { n   E[++tt].y = y, E[tt].l = g[x], g[x] = tt; n   E[++tt].y = x, E[tt].l = g[y], g[y] = tt; n } n int LOG2(int x) { n   int t = (int)log2(x); n   while ((1 << t) > x) --t; n   while ((1 << (t + 1)) <= x) ++t; n   return t; n } n int find(int x) { n   int i = x, t; n   while (f[i] != i) i = f[i]; n   while (x != i) t = f[x], f[x] = i, x = t; n   return x; n } n struct Stack { n   int x, i; n   Stack() {} n   Stack(int _x) { x = _x, i = -1; } n   void init(int _x) { x = _x, i = -1; } n } Q[mn]; n void init() { n   for (int i = 1; i <= n; ++i) n     if (!d[i]) { n       int r = 0; n       d[i] = 1, fa[i][0] = i, blg[i] = i, f[i] = i; n       Q[++r].init(i); n       while (r) { n         int x = Q[r].x, &i = Q[r].i, y; n         (d[0] = max(d[0], d[x])); n         if (i == -1) n           f[x] = x, i = g[x]; n         else { n           y = E[i].y; n           if (find(x) == find(y)) o[x] |= o[y]; n           i = E[i].l; n         } n         y = E[i].y; n         if (!i) { n           --r; n           continue; n         } n         if (!d[y]) { n           d[y] = d[x] + 1, fa[y][0] = x, blg[y] = blg[x], f[y] = y; n           Q[++r].init(y); n         } else if (d[x] > d[y] + 1) { n           int z = find(x); n           while (d[z] > d[y] + 1) f[z] = fa[z][0], z = find(z); n           o[x] |= !((d[x] - d[y]) % 2); n         } n       } n       Q[r = 1].init(i); n       while (r) { n         int x = Q[r].x, &i = Q[r].i, y; n         if (i == -1) n           s[x] += o[find(x)], i = g[x]; n         else n           i = E[i].l; n         y = E[i].y; n         if (!i) { n           --r; n           continue; n         } n         if (d[y] == d[x] + 1) { n           if (find(x) == find(y)) o[y] |= o[x]; n           s[y] = s[x], Q[++r].init(y); n         } n       } n     } n   int t = LOG2(d[0]); n   for (int j = 1; j <= t; ++j) n     for (int i = 1; i <= n; ++i) fa[i][j] = fa[fa[i][j - 1]][j - 1]; n } n int lca(int x, int y) { n   if (d[x] < d[y]) swap(x, y); n   while (d[x] > d[y]) x = fa[x][LOG2(d[x] - d[y])]; n   if (x == y) return x; n   for (int i = LOG2(d[x]); i >= 0; --i) n     if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i]; n   return fa[x][0]; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; ++i) { n     int x, y; n     scanf( %d%d , &x, &y); n     add(x, y); n   } n   init(); n   scanf( %d , &tt); n   while (tt--) { n     int x, y, z; n     scanf( %d%d , &x, &y); n     if (blg[x] != blg[y]) { n       printf( No n ); n       continue; n     } n     z = lca(x, y); n     if (((d[x] + d[y]) & 1) || s[x] + s[y] - 2 * s[z]) n       printf( Yes n ); n     else n       printf( No n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int n, a; n   cin >> n >> a; n   double angd = 180 * 1.0 / n; n   cout <<  2 1  ; n   int x = floor(a / angd); n   if (x == 0) n     cout << 3 << endl; n   else if (a - x * angd < (1 + x) * angd - a) n     cout << min(x + 2, n) << endl; n   else n     cout << min(x + 3, n) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n using V = vector<T>; n int gcdex(int a, int b, int &x, int &y) { n   if (a == 0) { n     x = 0; n     y = 1; n     return b; n   } n   int x1, y1; n   int g = gcdex(b % a, a, x1, y1); n   x = y1 - (b / a) * x1; n   y = x1; n   return g; n } n inline int ADD_MOD(int a, int b) { return (a + b) % 1000000007; } n inline int MUL_MOD(int a, int b) { return (int64_t(a) * b) % 1000000007; } n inline int SUB_MOD(int a, int b) { return a >= b ? a - b : a + 1000000007 - b; } n int DIV_MOD(int a, int b) { n   int x, y; n   gcdex(b, 1000000007, x, y); n   int b1 = (x % 1000000007 + 1000000007) % 1000000007; n   return MUL_MOD(a, b1); n } n const long double EPS = 1. / 1e9; n inline bool EPS_EQUAL(long double a, long double b) { n   return abs(a - b) <= EPS; n } n inline bool EPS_LESS(long double a, long double b) { return b - a > EPS; } n inline bool EPS_GREATER(long double a, long double b) { return a - b > EPS; } n const int INF = 1e9; n const int64_t INF64 = 2e18; n void get_cycle(V<int> &a, V<uint8_t> &visited, int start, V<int> &c) { n   int i = start; n   while (a[i] != start) { n     c.push_back(a[i]); n     visited[a[i]] = 1; n     i = a[i]; n   } n   c.push_back(start); n   visited[start] = 1; n } n void add_out_cycle(V<int> &b, V<int> &c) { n   for (int i = 0; i < c.size(); ++i) { n     int prev = i > 0 ? c[i - 1] : c.back(); n     b[prev] = c[i]; n   } n } n void add_odd_cycle(V<int> &b, V<int> &c) { n   int len = c.size(); n   V<int> outC(len); n   for (int i = 0; i < len; ++i) { n     if (i % 2 == 0) { n       outC[i] = c[(len + i) / 2]; n     } else { n       outC[i] = c[i / 2]; n     } n   } n   add_out_cycle(b, outC); n } n void add_even_cycle(V<int> &b, V<int> &c1, V<int> &c2) { n   int len = c1.size(); n   V<int> outC(len * 2); n   for (int i = 0; i < len; ++i) { n     outC[i * 2] = c1[i]; n     outC[i * 2 + 1] = c2[i]; n   } n   add_out_cycle(b, outC); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.precision(15); n   int n; n   cin >> n; n   V<int> a(n); n   for (auto &i : a) { n     cin >> i; n     --i; n   } n   V<int> b(n); n   V<uint8_t> visited(n); n   V<V<int> > evenCycles(n + 1); n   int evenCyclesCount = 0; n   for (int i = 0; i < n; ++i) { n     if (!visited[i]) { n       V<int> c; n       get_cycle(a, visited, i, c); n       int len = c.size(); n       if (len % 2 == 1) { n         add_odd_cycle(b, c); n       } else { n         if (!evenCycles[len].empty()) { n           add_even_cycle(b, evenCycles[len], c); n           evenCycles[len].clear(); n           evenCyclesCount--; n         } else { n           swap(evenCycles[len], c); n           evenCyclesCount++; n         } n       } n     } n   } n   if (evenCyclesCount > 0) { n     cout << -1 << endl; n     return 0; n   } n   for (auto &i : b) { n     cout << i + 1 <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1005; n int n, a[N][N]; n deque<int> p[N]; n int ptr[N], mat[N]; n bool vis[N][N]; n void upd(int x) { n   while (!p[x].empty() && vis[x][p[x].front()]) p[x].pop_front(); n   while (!p[x].empty() && vis[x][p[x].back()]) p[x].pop_back(); n   if (p[x].empty()) { n     cout << -1 << endl; n     exit(0); n   } n } n inline void del(int x, int y) { vis[x][y] = vis[y][x] = 1; } n void add(int x, int y) { n   mat[y] = x; n   while (1) { n     upd(y); n     if (p[y].back() != x) n       del(p[y].back(), y); n     else n       break; n   } n } n int nxt(int x) { n   upd(x); n   int y = p[x].front(); n   p[x].pop_front(); n   upd(x); n   p[x].push_front(y); n   return mat[p[x][1]]; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   if (n & 1) { n     cout << -1 << endl; n     return 0; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (i != j) cin >> a[i][j], p[i].push_back(j); n     } n     sort(p[i].begin(), p[i].end(), n          [&](int x, int y) { return a[i][x] < a[i][y]; }); n   } n   queue<int> q; n   for (int i = 1; i <= n; i++) q.push(i); n   while (!q.empty()) { n     int x = q.front(); n     q.pop(); n     while (1) { n       upd(x); n       int y = p[x].front(), z = mat[y]; n       if (z && a[y][z] < a[y][x]) assert(0); n       if (z) { n         mat[y] = 0; n         q.push(z); n       } n       add(x, y); n       break; n     } n   } n   int cur = 1; n   while (1) { n     while (cur <= n) { n       upd(cur); n       if (p[cur].size() > 1) n         break; n       else n         ++cur; n     } n     if (cur > n) { n       for (int i = 1; i <= n; i++) cout << p[i].front() << endl; n       return 0; n     } n     vector<int> c1, c2; n     int x = cur, y = cur; n     do x = nxt(x), y = nxt(nxt(y)); n     while (x != y); n     do c1.push_back(y), y = nxt(y); n     while (x != y); n     for (auto &i : c1) { n       int y = p[i][0]; n       c2.push_back(y); n       mat[y] = 0; n       del(i, y); n     } n     for (int i = 0; i < int(c1.size()); i++) n       add(c1[i], c2[(i + 1) % c1.size()]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using Int = long long; n template <class T1, class T2> n ostream &operator<<(ostream &os, const pair<T1, T2> &a) { n   return os <<  (  << a.first <<  ,   << a.second <<  ) ; n }; n template <class T> n void pv(T a, T b) { n   for (T i = a; i != b; ++i) cerr << *i <<    ; n   cerr << endl; n } n template <class T> n bool chmin(T &t, const T &f) { n   if (t > f) { n     t = f; n     return true; n   } n   return false; n } n template <class T> n bool chmax(T &t, const T &f) { n   if (t < f) { n     t = f; n     return true; n   } n   return false; n } n constexpr int MAX_N = 1010; n constexpr int MAX_K = 1010; n constexpr int MUL[4][4] = { n     {0, 0, 0, 0}, n     {0, 1, 2, 3}, n     {0, 2, 3, 1}, n     {0, 3, 1, 2}, n }; n int N, K; n char typ[MAX_K][10], mix[MAX_K][10]; n int M[MAX_K], J[MAX_K][MAX_N]; n int fs[MAX_N], gs[MAX_N]; n int aLen; n vector<bitset<2 * MAX_N>> a; n int main() { n   for (; ~scanf( %d%d , &N, &K);) { n     for (int k = 0; k < K; ++k) { n       scanf( %s%d , typ[k], &M[k]); n       for (int m = 0; m < M[k]; ++m) { n         scanf( %d , &J[k][m]); n         --J[k][m]; n       } n       if (!strcmp(typ[k],  mix )) { n         scanf( %s , mix[k]); n       } n     } n     fill(fs, fs + N, 1); n     fill(gs, gs + N, 0); n     aLen = 0; n     a.assign(2 * K, bitset<2 * MAX_N>()); n     for (int k = 0; k < K; ++k) { n       if (!strcmp(typ[k],  mix )) { n         const int num = string( WRYB ).find(mix[k]); n         for (int m = 0; m < M[k]; ++m) { n           const int i = J[k][m]; n           switch (gs[i]) { n             case 0: { n               const int p = (fs[i] >> 0) & 1; n               const int q = (fs[i] >> 1) & 1; n               a[aLen + 0][i << 1 | 0] = p; n               a[aLen + 0][i << 1 | 1] = q; n               a[aLen + 1][i << 1 | 0] = q; n               a[aLen + 1][i << 1 | 1] = p ^ q; n             } break; n             case 1: { n               const int p = (fs[i] >> 0) & 1; n               const int q = (fs[i] >> 1) & 1; n               a[aLen + 0][i << 1 | 0] = p; n               a[aLen + 0][i << 1 | 1] = p ^ q; n               a[aLen + 1][i << 1 | 0] = q; n               a[aLen + 1][i << 1 | 1] = p; n             } break; n             default: n               assert(false); n           } n         } n         a[aLen + 0][N << 1] = (num >> 0) & 1; n         a[aLen + 1][N << 1] = (num >> 1) & 1; n         aLen += 2; n       } else if (!strcmp(typ[k],  RY )) { n         for (int m = 0; m < M[k]; ++m) { n           const int i = J[k][m]; n           fs[i] = MUL[2][MUL[fs[i]][fs[i]]]; n           gs[i] ^= 1; n         } n       } else if (!strcmp(typ[k],  RB )) { n         for (int m = 0; m < M[k]; ++m) { n           const int i = J[k][m]; n           fs[i] = MUL[3][MUL[fs[i]][fs[i]]]; n           gs[i] ^= 1; n         } n       } else if (!strcmp(typ[k],  YB )) { n         for (int m = 0; m < M[k]; ++m) { n           const int i = J[k][m]; n           fs[i] = MUL[1][MUL[fs[i]][fs[i]]]; n           gs[i] ^= 1; n         } n       } else { n         assert(false); n       } n     } n     a.resize(aLen); n     int r = 0; n     vector<int> hs; n     for (int h = 0; h < N << 1; ++h) { n       for (int i = r; i < aLen; ++i) { n         if (a[i][h]) { n           swap(a[r], a[i]); n           break; n         } n       } n       if (a[r][h]) { n         for (int i = r + 1; i < aLen; ++i) { n           if (a[i][h]) { n             a[i] ^= a[r]; n           } n         } n         ++r; n         hs.push_back(h); n       } n     } n     bool ok = true; n     for (int i = r; i < aLen; ++i) { n       ok = ok && !a[i][N << 1]; n     } n     if (ok) { n       bitset<2 * MAX_N> sol; n       for (int i = r; i--;) { n         sol[hs[i]] = (int)a[i][N << 1] ^ ((a[i] & sol).count() & 1); n       } n       puts( YES ); n       for (int i = 0; i < N; ++i) { n         putchar( .RYB [(int)sol[i << 1 | 0] << 0 | (int)sol[i << 1 | 1] << 1]); n       } n       puts(  ); n     } else { n       puts( NO ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long int n, flag = 0; n     cin >> n; n     long long int a[n]; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n     } n     for (int i = 0; i < (n - 1); i++) { n       if (a[i] > a[i + 1]) { n         flag = 0; n       } else { n         flag = 1; n         break; n       } n     } n     if (flag == 0) { n       cout <<  NO  << endl; n     } else { n       cout <<  YES  << endl; n     } n   } 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   string s; n   cin >> s; n   for (int i = 0; i < n; i++) { n     long long int ml = 0, mr = 0, j; n     j = i; n     while (j < n - 1 && s[j] !=  R ) { n       s[j] ==  L  ? mr++ : mr += 0; n       j++; n     } n     j = i - 1; n     while (j > -1 && s[j] !=  L ) { n       s[j] ==  R  ? ml++ : ml += 0; n       j--; n     } n     cout << 1 + max(ml, mr) <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   pair<long long, long long> a[n]; n   for (long long i = 0; i < n; i++) { n     cin >> a[i].first; n     a[i].second = i + 1; n   } n   sort(a, a + n); n   long long mx = 0, mx_start = 0, mx_end = 0; n   long long j; n   long long mn = n; n   for (long long i = 0; i < n; i++) { n     long long c = a[i].second; n     long long d, ck = 0; n     for (j = i; j < n; j++) { n       if (a[i].first != a[j].first) { n         d = a[j - 1].second; n         break; n       } else n         ck++; n     } n     if (j == n) { n       d = a[j - 1].second; n       i = n - 1; n     } else n       i = j - 1; n     if (ck > mx) { n       mx = ck; n       mx_start = c; n       mx_end = d; n       mn = d - c + 1; n     } else if (ck == mx && (d - c + 1) < mn) { n       mx = ck; n       mx_start = c; n       mx_end = d; n       mn = d - c + 1; n     } n   } n   cout << mx_start <<     << mx_end << endl; n   return 0; n } n


#include <bits/stdc++.h> n char abc[1000001]; n int main() { n   char ab[1000001]; n   scanf( %s , ab); n   int abcc = strlen(ab); n   int i, x = 0, y = 0; n   for (i = 0; i < abcc; i++) { n     x += ab[i] ==  x  ? 1 : 0; n     y += ab[i] ==  y  ? 1 : 0; n   } n   if (x > y) n     for (i = 0; i < x - y; i++) abc[i] =  x ; n   else n     for (i = 0; i < y - x; i++) abc[i] =  y ; n   printf( %s , abc); n } n


#include <bits/stdc++.h> n using namespace std; n void solve(int test) { n   int x; n   cin >> x; n   int no = x % 10; n   int cnt = 0; n   while (x) { n     cnt++; n     x /= 10; n   } n   int ans = (no - 1) * 10; n   while (cnt) { n     ans += cnt; n     cnt--; n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout << setprecision(20); n   long long int t = 1, k; n   cin >> t; n   k = t; n   while (t--) { n     solve(k - t); n   } n   cerr <<  Timpe elapsed :  << clock() * 1000.0 / CLOCKS_PER_SEC <<   mps n ; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, p, q, i = 1, j, k, temp = 0, flag = 0; n   string s; n   cin >> n >> p >> q; n   cin >> s; n   if (n % p == 0 && n != p + q || n % q == 0 && n != p + q) { n     if (n % p == 0) { n       cout << n / p << endl; n       for (i = 0; i < n; i++) { n         cout << s[i]; n         if ((i + 1) % p == 0) cout << endl; n       } n     } else { n       cout << n / q << endl; n       for (i = 0; i < n; i++) { n         cout << s[i]; n         if ((i + 1) % q == 0) cout << endl; n       } n     } n   } else { n     while (1) { n       j = n - (p * i); n       k = n - (q * i); n       if (j < 0 || k < 0) break; n       if (j % q == 0) { n         temp = 1; n         break; n       } n       if (k % p == 0) { n         flag = 1; n         break; n       } n       if (j < 0 && k < 0) break; n       i++; n     } n     if (temp == 1) { n       cout << i + (j / q) << endl; n       for (int x = 0; x < i * p; x++) { n         cout << s[x]; n         if ((x + 1) % p == 0) cout << endl; n       } n       j = 1; n       for (int x = i * p; x < n; x++) { n         cout << s[x]; n         if ((j) % q == 0) cout << endl; n         j++; n       } n     } else if (flag == 1) { n       cout << i + (k / p) << endl; n       for (int x = 0; x < i * q; x++) { n         cout << s[x]; n         if ((x + 1) % q == 0) cout << endl; n       } n       j = 1; n       for (int x = i * q; x < n; x++) { n         cout << s[x]; n         if ((j) % p == 0) cout << endl; n         j++; n       } n     } else { n       cout <<  -1  << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c; n   cin >> a >> b >> c; n   for (int i = 0; i <= (c / b); i++) { n     int tmp = c - (i * b); n     if (tmp % a == 0) { 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 main() { n   int n, k, a[105], nr = 0, poz = 0; n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) { n     if (a[i] <= k) n       nr++; n     else { n       poz = i; n       break; n     } n   } n   if (poz || nr == 0) n     for (int i = n - 1; i > poz; i--) n       if (a[i] <= k) n         nr++; n       else n         break; n   cout << nr; n } n


#include <bits/stdc++.h> n using namespace std; n int p[2010]; n int main(void) { n   int N, ans = 0, i, j; n   cin >> N; n   for ((i) = 0; (i) < (int)(N); (i)++) cin >> p[i]; n   for ((i) = 0; (i) < (int)(N); (i)++) n     if (p[i] < 0) p[i] = -p[i]; n   for ((i) = 0; (i) < (int)(N); (i)++) { n     int a = 0, b = 0; n     for ((j) = 0; (j) < (int)(N); (j)++) n       if (p[j] < p[i]) { n         if (j < i) a++; n         if (j > i) b++; n       } n     ans += min(a, b); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Max_L = 100000 + 100, inf = ~0U >> 2; n pair<int, int> Ret[1000]; n int main() { n   int L, b, f; n   cin >> L >> f >> b; n   int n; n   cin >> n; n   set<pair<int, int> > S; n   S.insert(pair<int, int>(-inf, -inf)); n   S.insert(pair<int, int>(inf, inf)); n   for (int i = 0; i < n; i++) { n     int t, len; n     cin >> t >> len; n     if (t == 1) { n       bool ok = false; n       for (typeof(S.begin()) it = S.begin(); it != S.end(); it++) { n         if (it == --S.end()) continue; n         int l = max(it->second + f, 0); n         ++it; n         int r = min(it->first - b, L); n         --it; n         if (r - l >= len) { n           ok = true; n           cout << l << endl; n           Ret[i] = pair<int, int>(l, l + len); n           S.insert(Ret[i]); n           break; n         } n       } n       if (!ok) cout << -1 << endl; n     } else { n       len--; n       S.erase(Ret[len]); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   string ans; n   for (long long i = 0; i < k; i++) { n     ans.push_back(s[i]); n   } n   for (long long i = k; i < n; i++) { n     ans.push_back(ans[i - k]); n   } n   cout << n << endl; n   if (ans >= s) { n     cout << ans << endl; n   } else { n     long long i = k - 1; n     while (i >= 0 && ans[i] ==  9 ) { n       ans[i] =  0 ; n       for (long long j = i; j < n; j += k) { n         ans[j] =  0 ; n       } n       i--; n     } n     for (; i < n; i += k) { n       ans[i]++; n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<int> seq; n   int n; n   long e; n   cin >> n; n   int first = 0; n   long long s = 0; n   int f = 0; n   for (int i = 0; i < n; i++) { n     cin >> e; n     if (e != 1) { n       s += e; n       f++; n     } else { n       first++; n     } n     if (s > f && (s - f + 1) % 2 == 0) { n       cout << 1 << endl; n     } else { n       cout << 2 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<long long int, long long int> > points, hull; n bool comp(pair<long long int, long long int> a, n           pair<long long int, long long int> b) { n   if (a.first != b.first) return a.first < b.first; n   return a.second < b.second; n } n int cross(pair<long long int, long long int> a, n           pair<long long int, long long int> b, n           pair<long long int, long long int> c) { n   return a.first * (b.second - c.second) + b.first * (c.second - a.second) + n              c.first * (a.second - b.second) >= n          0; n } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     long long int x, y; n     cin >> x >> y; n     y = y - x * x; n     points.push_back({x, y}); n   } n   sort(points.begin(), points.end(), comp); n   int sz = 1; n   hull.push_back(points[0]); n   for (int i = 1; i < points.size(); i++) { n     while (sz >= 2 && cross(hull[sz - 2], hull[sz - 1], points[i])) n       hull.pop_back(), sz--; n     hull.push_back(points[i]); n     sz++; n   } n   int ans = 0; n   for (int i = 0; i < sz - 1; i++) { n     if (hull[i].first != hull[i + 1].first) ans++; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, x, sum = 0, sumtotal; n int main() { n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     cin >> x; n     sum += x; n   } n   sumtotal = n * (n + 1) / 2; n   cout << sumtotal - sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:167177216 ) n int main() { n   double y1, y2, yw, xa, ya, r; n   cin >> y1 >> y2 >> yw >> xa >> ya >> r; n   double ll = 0, rr = xa; n   yw -= r; n   double yt = y1 + r + 1e-9; n   for (int i = 0; i < 300; ++i) { n     double xm = (ll + rr) / 2; n     double dx1 = -xm, dy1 = yt - yw; n     double a1 = acos(-dy1 / sqrt(dx1 * dx1 + dy1 * dy1)); n     dx1 = xa - xm, dy1 = ya - yw; n     double a2 = acos(-dy1 / sqrt(dx1 * dx1 + dy1 * dy1)); n     if (a1 > a2) n       rr = xm; n     else n       ll = xm; n   } n   double xm = (ll + rr) / 2; n   double a = yw - yt, b = -xm, c = -b * yt; n   double d = fabs(b * y2 + c) / sqrt(a * a + b * b); n   if (d + 1e-9 < r) { n     cout << -1 << endl; n     return 0; n   } n   cout.precision(20); n   cout << xm << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, d[100000]; n   string ch; n   long long res = 0; n   bool b = 0; n   cin >> n >> ch; n   for (int i = 0; i < n; i++) cin >> d[i]; n   for (int i = 0; i < n; i++) { n     if (ch[res] ==  < ) n       res -= d[res]; n     else n       res += d[res]; n     if (res < 0 || res >= n) { n       b = 1; n       break; n     } n   } n   if (b) n     cout <<  FINITE  << endl; n   else n     cout <<  INFINITE  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000000; n int centroid[maxn]; n int parent[maxn]; n int subtree[maxn]; n int heaviest[maxn]; n vector<int> graph[maxn]; n void dfs(int u, int p = 0) { n   parent[u] = p; n   subtree[u] = 1; n   heaviest[u] = 0; n   int vert = 0; n   for (auto v : graph[u]) { n     if (v == p) continue; n     dfs(v, u); n     subtree[u] += subtree[v]; n     if (subtree[v] > heaviest[u]) { n       heaviest[u] = subtree[v]; n       vert = v; n     } n   } n   if ((int)graph[u].size() == 0 or (2 * heaviest[u] <= subtree[u])) n     centroid[u] = u; n   else { n     int curr = centroid[vert]; n     centroid[u] = curr; n     while (curr != u) { n       int P = parent[curr]; n       int rem = subtree[u] - subtree[P]; n       if (2 * heaviest[P] > subtree[u]) break; n       if (2 * rem <= subtree[u]) centroid[u] = P; n       curr = parent[curr]; n     } n   } n } n int main() { n   int n; n   scanf( %d , &n); n   int q; n   scanf( %d , &q); n   for (int i = 2; i <= n; i++) { n     int p; n     scanf( %d , &p); n     graph[p].push_back(i); n   } n   dfs(1); n   while (q--) { n     int v; n     scanf( %d , &v); n     printf( %d n , centroid[v]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, r1, r2; n string ans; n bool pytaj(int x, int y) { n   cout << 1 <<     << x <<     << y <<   n ; n   fflush(stdout); n   cin >> ans; n   return ans[0] ==  T ; n } n int znajdz(int l, int r) { n   if (r < l) return -1; n   while (l < r) { n     int d = (r - l + 1 - 2 + 2) / 4; n     int a = l + d; n     int b = r - d; n     if (pytaj(a, b)) n       r = (l + r) / 2; n     else n       l = (l + r + 1) / 2; n   } n   return l; n } n bool sprawdz(int x) { n   if (x - 1 >= 1 && !pytaj(x, x - 1)) return 0; n   if (x + 1 <= n && !pytaj(x, x + 1)) return 0; n   return 1; n } n int32_t main() { n   cin >> n >> k; n   r1 = znajdz(1, n); n   r2 = znajdz(1, r1 - 1); n   if (r2 == -1 || !sprawdz(r2)) r2 = znajdz(r1 + 1, n); n   cout << 2 <<     << r1 <<     << r2 <<   n ; n   fflush(stdout); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e6 + 5; n int c[N][26], fa[N], l[N], sz[N], np = 1, cnt = 1; n int a[N], b[N]; n int q, len, vi[N]; n char s[N]; n void expend(int x) { n   int p = np; n   np = ++cnt; n   l[np] = l[p] + 1; n   sz[np] = 1; n   for (; p && !c[p][x]; p = fa[p]) c[p][x] = np; n   if (!p) { n     fa[np] = 1; n     return; n   } n   int q = c[p][x]; n   if (l[q] == l[p] + 1) { n     fa[np] = q; n     return; n   } n   int nq = ++cnt; n   l[nq] = l[p] + 1; n   memcpy(c[nq], c[q], 104); n   fa[nq] = fa[q], fa[q] = fa[np] = nq; n   for (; c[p][x] == q; p = fa[p]) c[p][x] = nq; n } n void init() { n   for (int i = 1; i <= cnt; ++i) ++b[l[i]]; n   for (int i = 1; i <= len; ++i) b[i] += b[i - 1]; n   for (int i = cnt; i; --i) a[b[l[i]]--] = i; n   for (int i = cnt; i; --i) sz[fa[a[i]]] += sz[a[i]]; n } n int walk() { n   int re = 0, u = 1, cur = 0; n   for (int i = 0; i < (len << 1); ++i) { n     int x = s[i] - 97; n     if (c[u][x]) n       ++cur, u = c[u][x]; n     else { n       while (u && !c[u][x]) u = fa[u]; n       if (u) n         cur = l[u] + 1, u = c[u][x]; n       else n         cur = 0, u = 1; n     } n     if (i >= len && cur >= len) { n       int p = u; n       while (p && !(len >= l[fa[p]] + 1 && len <= l[p])) p = fa[p]; n       if (!p) p = 1; n       if (vi[p] != q + 1) vi[p] = q + 1, re += sz[p]; n     } n   } n   return re; n } n int main() { n   scanf( %s%d , s, &q), len = strlen(s); n   for (int i = 0; i < len; ++i) expend(s[i] - 97); n   init(); n   while (q--) { n     scanf( %s , s), len = strlen(s); n     memcpy(s + len, s, len); n     printf( %d n , walk()); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int k; n bool primes[200000][100]; n int okay[5000000]; n int okayN; n int nLeft[1000]; n bool isPrime[1000]; n bool used[5000000]; n vector<int> ans; n bool compare(int a, int b) { n   if (nLeft[a] > nLeft[b]) return 1; n   if (nLeft[a] < nLeft[b]) return 0; n   return (a < b); n } n int main() { n   scanf( %d , &k); n   okayN = 1; n   okay[0] = 1; n   isPrime[1] = 0; n   for (int p = 2; true; ++p) { n     bool prime = 1; n     for (int i = 2; i * i <= p; ++i) { n       if (p % i == 0) prime = 0; n     } n     if (!prime) { n       isPrime[p] = 0; n       continue; n     } n     isPrime[p] = 1; n     for (int i = okayN - 1; i >= 0; --i) { n       int j = okay[i] * p; n       while (j <= 2 * k * k) { n         for (int m = 2; m < p; ++m) primes[okayN][m] = primes[i][m]; n         primes[okayN][p] = 1; n         okay[okayN++] = j; n         j *= p; n       } n     } n     for (int i = 2; i <= p; ++i) nLeft[i] = (k + 1) / 2; n     int need[1000]; n     int needN = 0; n     for (int i = 2; i <= p; ++i) n       if (isPrime[i]) need[needN++] = i; n     for (int i = 0; i < okayN; ++i) used[i] = 0; n     bool ok = 1; n     bool anybad = 0; n     ans.clear(); n     while (ans.size() < k) { n       int best = 0; n       for (int i = 1; i < okayN; ++i) { n         if (used[i]) continue; n         for (int j = 0; j < needN; ++j) { n           if (primes[i][need[j]] && !primes[best][need[j]]) { n             best = i; n             break; n           } n           if (!primes[i][need[j]] && primes[best][need[j]]) { n             break; n           } n         } n       } n       ans.push_back(okay[best]); n       if (used[best]) { n         ok = 0; n         break; n       } n       used[best] = 1; n       anybad = 0; n       for (int j = 0; j < needN; ++j) { n         if (nLeft[need[j]]) anybad = 1; n         if (primes[best][need[j]] && nLeft[need[j]]) --nLeft[need[j]]; n       } n       sort(need, need + needN, compare); n     } n     if (ok && !anybad) { n       for (int i = 0; i < ans.size(); ++i) printf( %d  , ans[i]); n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n const int inf = 0x7fffffff / 2; n int n, m; n int fa[maxn]; n int vis[maxn]; n struct edge { n   int u; n   int v; n   int w; n } e[maxn]; n struct ex { n   int mi; n   int mx; n   int sum; n } ex[maxn]; n int father(int x) { n   if (x != fa[x]) fa[x] = father(fa[x]); n   return fa[x]; n } n void join(int x, int y) { n   ex[father(y)].mi = min(ex[father(y)].mi, ex[father(x)].mi); n   ex[father(y)].mx = max(ex[father(y)].mx, ex[father(x)].mx); n   ex[father(y)].sum += ex[father(x)].sum; n   fa[father(x)] = father(y); n } n int judge_node(int x) { n   x = father(x); n   int mi = ex[x].mi; n   int mx = ex[x].mx; n   int sum = ex[x].sum; n   if (mi == -inf) return 1; n   mi = ((mi - 1) * mi) / 2; n   mx = ((mx + 1) * mx) / 2; n   if ((mx - mi) != sum) n     return 1; n   else n     return 0; n } n void init(int u, int v) { n   ex[u].sum = u; n   ex[v].sum = v; n   ex[u].mx = u; n   ex[u].mi = u; n   ex[v].mx = v; n   ex[v].mi = v; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     fa[i] = i; n     ex[i].mi = inf; n     ex[i].mx = -inf; n     ex[i].sum = 0; n   } n   for (int i = 1; i <= m; i++) { n     cin >> e[i].u >> e[i].v; n     init(e[i].u, e[i].v); n   } n   for (int i = 1; i <= m; i++) { n     if (father(e[i].u) != father(e[i].v)) { n       join(e[i].u, e[i].v); n     } n   } n   int ans = 0; n   int mxx = -inf, last; n   for (int i = 1; i <= n; i++) { n     if (judge_node(i) && vis[father(i)] == 0) { n       int l = ex[father(i)].mi; n       int r = ex[father(i)].mx; n       for (int j = l; j < r; j++) { n         if (father(j) != father(i)) { n           join(i, j); n           ans++; n           last = j; n         } n         l = ex[father(i)].mi; n         r = ex[father(i)].mx; n       } n       vis[father(last)] = 1; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int P = 1e9 + 7, INF = 0x3f3f3f3f; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n long long qpow(long long a, long long n) { n   long long r = 1 % P; n   for (a %= P; n; a = a * a % P, n >>= 1) n     if (n & 1) r = r * a % P; n   return r; n } n long long inv(long long first) { n   return first <= 1 ? 1 : inv(P % first) * (P - P / first) % P; n } n inline int rd() { n   int first = 0; n   char p = getchar(); n   while (p <  0  || p >  9 ) p = getchar(); n   while (p >=  0  && p <=  9 ) first = first * 10 + p -  0 , p = getchar(); n   return first; n } n const int N = 5e3 + 10; n int n, a, b, k; n int dp[N][N], s[N]; n int main() { n   scanf( %d%d%d%d , &n, &a, &b, &k); n   dp[0][a] = 1; n   int ans = 0; n   for (int i = 0; i <= k; ++i) { n     for (int first = 1; first <= n; ++first) { n       int &r = dp[i][first]; n       if (i) (r += dp[i][first - 1]) %= P; n       if (!r) continue; n       if (i == k) (ans += r) %= P; n       int w = abs(first - b); n       if (w == 1) continue; n       (dp[i + 1][max(first - w + 1, 0)] += r) %= P; n       (dp[i + 1][first] -= r) %= P; n       (dp[i + 1][first + 1] += r) %= P; n       (dp[i + 1][min(first + w, n + 1)] -= r) %= P; n     } n   } n   if (ans < 0) ans += P; n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n namespace FastIO { n bool IOerror = 0; n inline char nc() { n   static char buf[100000], *p1 = buf + 100000, *pend = buf + 100000; n   if (p1 == pend) { n     p1 = buf; n     pend = buf + fread(buf, 1, 100000, stdin); n     if (pend == p1) { n       IOerror = 1; n       return -1; n     } n   } n   return *p1++; n } n inline bool blank(char ch) { n   return ch ==     || ch ==   n  || ch ==   r  || ch ==   t ; n } n template <class T> n inline bool read(T& x) { n   bool sign = 0; n   char ch = nc(); n   x = 0; n   for (; blank(ch); ch = nc()) n     ; n   if (IOerror) return false; n   if (ch ==  - ) sign = 1, ch = nc(); n   for (; ch >=  0  && ch <=  9 ; ch = nc()) x = x * 10 + ch -  0 ; n   if (sign) x = -x; n   return true; n } n template <class T, class... U> n bool read(T& h, U&... t) { n   return read(h) && read(t...); n } n };  // namespace FastIO n using namespace std; n using namespace FastIO; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const int INF = 0x3f3f3f3f; n const int N = 2e5 + 10; n const int mod = 1e9 + 7; n vector<long long> e[N]; n long long dis[N][2], siz[N][2], n, ans, w[N]; n void dfs(long long u, long long fa) { n   siz[u][1] = 1; n   dis[u][1] = (w[u] + mod) % mod; n   for (auto v : e[u]) { n     if (v == fa) continue; n     dfs(v, u); n     ans = n         (ans + dis[u][0] * siz[v][1] % mod + dis[v][1] * siz[u][0] % mod) % mod; n     ans = n         (ans + dis[u][1] * siz[v][0] % mod + dis[v][0] * siz[u][1] % mod) % mod; n     siz[u][0] += siz[v][1]; n     siz[u][1] += siz[v][0]; n     dis[u][0] = (dis[u][0] + dis[v][1] - w[u] * siz[v][1] % mod + mod) % mod; n     dis[u][1] = (dis[u][1] + dis[v][0] + w[u] * siz[v][0] % mod + mod) % mod; n   } n } n signed main() { n   read(n); n   for (long long i = 1; i <= n; i++) read(w[i]); n   for (long long i = 1; i < n; i++) { n     long long u, v; n     read(u, v); n     e[u].push_back(v); n     e[v].push_back(u); n   } n   dfs(1, 1); n   ans *= 2; n   ans %= mod; n   for (long long i = 1; i <= n; i++) ans = (ans + w[i] + mod) % mod; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef struct cell { n   long long mx, pref, suf, total; n } cell; n int tam[55], vet[300000]; n long long sum[55], preffix[55], suffix[55], best[55], v[300000]; n long long getMax(int n) { n   long long ret = -1000000000000000000LL, aux = 0LL; n   for (int i = 0; i < n; i++) { n     aux += v[i]; n     ret = max(ret, aux); n     if (aux < 0) aux = 0LL; n   } n   return ret; n } n cell solve(int b, int e) { n   cell r; n   if (b == e) { n     r.mx = best[vet[b]]; n     r.total = sum[vet[b]]; n     r.pref = preffix[vet[b]]; n     r.suf = suffix[vet[b]]; n     return r; n   } n   int m = (b + e) / 2; n   cell p1, p2; n   p1 = solve(b, m); n   p2 = solve(m + 1, e); n   r.mx = max(max(p1.mx, p2.mx), p1.suf + p2.pref); n   r.total = p1.total + p2.total; n   r.pref = max(p1.pref, p1.total + p2.pref); n   r.suf = max(p2.suf, p1.suf + p2.total); n   return r; n } n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> tam[i]; n     for (int j = 0; j < tam[i]; j++) { n       cin >> v[j]; n     } n     best[i] = getMax(tam[i]); n     sum[i] = 0LL; n     preffix[i] = suffix[i] = -1000000000000000000LL; n     for (int j = 0; j < tam[i]; j++) { n       sum[i] += v[j]; n       preffix[i] = max(preffix[i], sum[i]); n     } n     sum[i] = 0LL; n     for (int j = tam[i] - 1; j >= 0; j--) { n       sum[i] += v[j]; n       suffix[i] = max(suffix[i], sum[i]); n     } n   } n   for (int i = 0; i < m; i++) { n     cin >> vet[i]; n     vet[i]--; n   } n   cell ans = solve(0, m - 1); n   cout << ans.mx << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-8; n const double pi = acos(-1); n inline int sgn(double x) { n   if (x < -eps) n     return -1; n   else if (x > eps) n     return 1; n   else n     return 0; n } n struct Point { n   double x, y; n   Point(double _x = 0.0, double _y = 0.0) : x(_x), y(_y) {} n   Point operator+(const Point &b) const { return Point(x + b.x, y + b.y); } n   Point operator-(const Point &b) const { return Point(x - b.x, y - b.y); } n   double operator*(const Point &b) const { return (x * b.x + y * b.y); } n   double operator^(const Point &b) const { return (x * b.y - y * b.x); } n   Point operator*(double b) { return Point(x * b, y * b); } n   bool operator<(const Point &b) const { n     if (sgn(x - b.x) == 0) n       return sgn(y - b.y) <= 0; n     else n       return sgn(x - b.x) < 0; n   } n   bool operator==(const Point &b) const { n     return sgn(x - b.x) == 0 && sgn(y - b.y) == 0; n   } n   Point rot(double ang) { n     return Point(x * cos(ang) - y * sin(ang), x * sin(ang) + y * cos(ang)); n   } n   Point rot(double g1, double g2) { n     return Point(x * g2 - y * g1, x * g1 + y * g2); n   } n   double norm() { return sqrt(x * x + y * y); } n   Point unit() { n     double ll = norm(); n     return Point(x / ll, y / ll); n   } n }; n Point f[2005], p[2005]; n bool vis[2005]; n double d[2005]; n int main() { n   int n, m, ans = 0, i, j; n   Point o = Point(0, 0); n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %lf%lf , &f[i].x, &f[i].y); n     o = o + f[i]; n   } n   o = o * (1.0 / n); n   for (i = 0; i < n; i++) { n     if (vis[i]) continue; n     for (j = i; j < n; j++) { n       if (f[i] + f[j] == o * 2.0) { n         vis[i] = vis[j] = 1; n         break; n       } n     } n   } n   for (i = j = 0; i < n; i++) { n     if (!vis[i]) f[j++] = f[i] - o; n   } n   if (!j) return puts( -1 ), 0; n   n = j; n   for (i = m = 0; i < n; i++) { n     Point u = f[0] + f[i]; n     Point v = Point(-u.y, u.x); n     for (j = 0; j < n; j++) d[j] = f[j] * v; n     sort(d, d + n); n     for (j = 0; j < n; j++) n       if (sgn(d[j] + d[n - 1 - j])) break; n     if (j == n) { n       v = v.unit(); n       for (j = 0; j < m; j++) n         if (!sgn(p[j] ^ v)) break; n       if (j == m) { n         ans++; n         p[m++] = v; n       } n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long inf = 10000000; n inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } n inline bool isLeapYear(long long year) { n   return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); n } n inline void normal(long long &a) { n   a %= MOD; n   (a < 0) && (a += MOD); n } n inline long long modMul(long long a, long long b) { n   a %= MOD, b %= MOD; n   normal(a), normal(b); n   return (a * b) % MOD; n } n inline long long modAdd(long long a, long long b) { n   a %= MOD, b %= MOD; n   normal(a), normal(b); n   return (a + b) % MOD; n } n inline long long modSub(long long a, long long b) { n   a %= MOD, b %= MOD; n   normal(a), normal(b); n   a -= b; n   normal(a); n   return a; n } n inline long long modPow(long long b, long long p) { n   long long r = 1; n   while (p) { n     if (p & 1) r = modMul(r, b); n     b = modMul(b, b); n     p >>= 1; n   } n   return r; n } n inline long long modInverse(long long a) { return modPow(a, MOD - 2); } n inline long long modDiv(long long a, long long b) { n   return modMul(a, modInverse(b)); n } n inline bool checkBit(long long n, int i) { return n & (1LL << i); } n inline long long setBit(long long n, int i) { n   return n | (1LL << i); n   ; n } n inline long long resetBit(long long n, int i) { return n & (~(1LL << i)); } n int dx[] = {0, 0, 1, -1}; n int dy[] = {1, -1, 0, 0}; n void input(long long n, const long long a[], int co = 0) { n   long long i = 1; n   for (; i <= n; i++) { n   } n } n const int N = 45; n int dp[N][N][N][N]; n char Grid[N][N]; n int cal[N][N]; n int n, m, k; n void dfs() {} n bool cmp() {} n void gen() { n   for (int i = int(1); i < int(n + 1); ++i) n     for (int j = int(1); j < int(m + 1); ++j) { n       cal[i][j] = cal[i - 1][j] + cal[i][j - 1] - cal[i - 1][j - 1] + n                   (Grid[i][j] ==  1 ); n     } n } n void marmarkatkat() { n   for (int i = int(1); i < int(n + 1); ++i) { n     for (int j = int(1); j < int(m + 1); ++j) { n       for (int l = int(i); l < int(n + 1); ++l) n         for (int r = int(j); r < int(m + 1); ++r) n           dp[i][j][l][r] += (cal[l][r] + cal[i - 1][j - 1] - cal[i - 1][r] - n                              cal[l][j - 1]) == 0; n     } n   } n } n int main() { n   scanf( %d %d %d , &n, &m, &k); n   memset(cal, 0, sizeof cal); n   ; n   for (int i = int(1); i < int(n + 1); ++i) { n     scanf( %s , Grid[i] + 1); n   } n   gen(); n   marmarkatkat(); n   for (int l = int(1); l < int(n + 1); ++l) { n     for (int r = int(1); r < int(m + 1); ++r) { n       for (int i = int(n); i >= int(1); --i) n         for (int j = int(m); j >= int(1); --j) { n           dp[i][j][l][r] += (dp[i + 1][j][l][r] + dp[i][j + 1][l][r] - n                              dp[i + 1][j + 1][l][r]); n         } n     } n   } n   for (int l = int(1); l < int(n + 1); ++l) { n     for (int r = int(1); r < int(m + 1); ++r) { n       for (int i = int(n); i >= int(1); --i) n         for (int j = int(m); j >= int(1); --j) { n           dp[i][j][l][r] += n               dp[i][j][l - 1][r] + dp[i][j][l][r - 1] - dp[i][j][l - 1][r - 1]; n         } n     } n   } n   int a, b, x, y; n   for (int i = int(0); i < int(k); ++i) { n     scanf( %d %d , &a, &b), scanf( %d %d , &x, &y); n     ; n     printf( %d n , dp[a][b][x][y]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18; n const long long MOD = 1e9 + 7; n const long long mod = 998244353; n const long long mxN = 1e6; n void fileio() {} n vector<long long> v[mxN]; n vector<long long> vis(mxN); n long long nd; n void dfs(long long x, long long p) { n   vis[x] = 1; n   for (auto i : v[x]) { n     if (i == p) continue; n     if (!vis[i]) n       dfs(i, x); n     else n       nd = x; n   } n } n void solve() { n   long long n; n   cin >> n; n   vector<long long> a(n); n   for (long long i = 0; i < n; i++) cin >> a[i]; n   ; n   for (long long i = 0; i < n; i++) { n     v[i + 1].push_back(a[i]); n     v[a[i]].push_back(i + 1); n   } n   vector<long long> v; n   for (long long i = 1; i < n + 1; i++) { n     if (vis[i]) continue; n     nd = i; n     dfs(i, -1); n     v.push_back(nd); n   } n   long long ans = v.size() - 1; n   long long root = -1; n   for (long long i = 0; i < v.size(); i++) { n     if (a[v[i] - 1] == v[i]) { n       root = v[i]; n       break; n     } n   } n   if (root == -1) { n     ans++; n     a[v[0] - 1] = v[0]; n     root = v[0]; n   } n   for (long long i = 0; i < v.size(); i++) a[v[i] - 1] = root; n   cout << ans <<   n ; n   for (long long i = 0; i < a.size(); i++) cout << a[i] <<    ; n   cout <<   n ; n   ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   fileio(); n   long long tc = 1; n   while (tc--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long dx[4] = {-1, 1, 0, 0}; n const long long dy[4] = {0, 0, -1, 1}; n const long long MAX = 5e3 + 5; n long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); } n long long n, m, ans, k, Q; n string second; n bool pal[MAX][MAX]; n long long cnt[MAX][MAX]; n void solve() { n   for (int i = 0; i < n; ++i) pal[i][i] = cnt[i][i] = true; n   for (int i = 0; i < n - 1; ++i) { n     if (second[i] == second[i + 1]) pal[i][i + 1] = true, cnt[i][i + 1] = 1; n     cnt[i][i + 1] += 2; n   } n   for (int len = 3; len <= n; ++len) n     for (int i = 0; i < n - len + 1; ++i) { n       int j = i + len - 1; n       if (second[i] == second[j] && pal[i + 1][j - 1]) n         pal[i][j] = true; n       else n         pal[i][j] = false; n       cnt[i][j] = pal[i][j] + cnt[i][j - 1] + cnt[i + 1][j] - cnt[i + 1][j - 1]; n     } n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> second; n   n = second.length(); n   solve(); n   cin >> Q; n   while (Q--) { n     int lo, hi; n     cin >> lo >> hi; n     --lo; n     --hi; n     cout << cnt[lo][hi] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int count = 0; n     int n, m; n     cin >> n >> m; n     char a[n][m]; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         cin >> a[i][j]; n       } n     } n     for (int i = 0; i < n - 1; i++) { n       if (a[i][m - 1] !=  D ) { n         count++; n       } n     } n     for (int i = 0; i < m - 1; i++) { n       if (a[n - 1][i] !=  R ) { n         count++; n       } n     } n     cout << count << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5, mod = 998244353; n inline int mod1(int x) { return x < mod ? x : x - mod; } n inline int mod2(int x) { return x < 0 ? x + mod : x; } n inline void add(int &x, int y) { x = mod1(x + y); } n inline void sub(int &x, int y) { x = mod2(x - y); } n inline int pow_mod(int x, int i) { n   int y = 1; n   while (i) { n     if (i & 1) y = (long long)y * x % mod; n     x = (long long)x * x % mod; n     i >>= 1; n   } n   return y; n } n int p[26], val[26], m, n, s[MAXN + 5], t[MAXN + 5], vs[MAXN + 5], vt[MAXN + 5], n     res[MAXN + 5], f[MAXN * 4 + 5], g[MAXN * 4 + 5], rev[MAXN * 4 + 5]; n char str[MAXN + 5]; n void NTT(int *a, int n, int flag) { n   for (int i = 0; i < n; ++i) n     if (i < rev[i]) swap(a[i], a[rev[i]]); n   for (int i = 1; i < n; i <<= 1) { n     int T = pow_mod(3, (mod - 1) / (i << 1)); n     if (flag == -1) T = pow_mod(T, mod - 2); n     for (int j = 0; j < n; j += (i << 1)) { n       for (int k = 0, t = 1; k < i; ++k, t = (long long)t * T % mod) { n         int Nx = a[j + k], Ny = (long long)a[i + j + k] * t % mod; n         a[j + k] = mod1(Nx + Ny); n         a[i + j + k] = mod2(Nx - Ny); n       } n     } n   } n   if (flag == -1) { n     int invn = pow_mod(n, mod - 2); n     for (int i = 0; i < n; ++i) a[i] = (long long)a[i] * invn % mod; n   } n } n bool ck(int i) { n   for (int j = 0; j < i; ++j) n     if (val[i] == val[j]) return 0; n   return 1; n } n int main() { n   for (int i = 0; i < 26; ++i) { n     val[i] = rand() % mod; n     while (!ck(i)) add(val[i], 1); n   } n   for (int i = 0; i < 26; ++i) cin >> p[i], p[i] = val[p[i] - 1]; n   cin >> str; n   m = strlen(str); n   reverse(str, str + m); n   for (int i = 0; i < m; ++i) vs[i] = val[s[i] = str[i] -  a ]; n   cin >> str; n   n = strlen(str); n   for (int i = 0; i < n; ++i) vt[i] = val[t[i] = str[i] -  a ]; n   int tmp = 0, tmpp = 0; n   for (int i = 0; i < m; ++i) n     add(tmp, (long long)p[s[i]] * p[s[i]] % mod * vs[i] % mod * vs[i] % mod); n   for (int i = 0; i < m; ++i) add(tmpp, (long long)pow_mod(vt[i], 4)); n   res[m - 1] = (tmpp + tmp) % mod; n   for (int i = m; i < n; ++i) n     res[i] = n         ((res[i - 1] + pow_mod(vt[i], 4) - pow_mod(vt[i - m], 4)) % mod + mod) % n         mod; n   for (int i = 0; i < m; ++i) n     f[i] = (long long)p[s[i]] * vs[i] % mod * mod1(p[s[i]] + vs[i]) % mod; n   for (int i = 0; i < n; ++i) g[i] = vt[i]; n   int len = 1, ct = 0; n   while (len <= n + m) len <<= 1, ++ct; n   for (int i = 0; i < len; ++i) n     rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (ct - 1)); n   NTT(f, len, 1); n   NTT(g, len, 1); n   for (int i = 0; i < len; ++i) f[i] = (long long)f[i] * g[i] % mod; n   NTT(f, len, -1); n   for (int i = m - 1; i < n; ++i) sub(res[i], mod1(2 * f[i])); n   for (int i = 0; i < m; ++i) n     f[i] = ((long long)vs[i] * vs[i] % mod + n             (long long)p[s[i]] * p[s[i]] % mod + 4LL * vs[i] * p[s[i]] % mod) % n            mod; n   for (int i = m; i < len; ++i) f[i] = 0; n   for (int i = 0; i < n; ++i) g[i] = (long long)vt[i] * vt[i] % mod; n   for (int i = n; i < len; ++i) g[i] = 0; n   NTT(f, len, 1); n   NTT(g, len, 1); n   for (int i = 0; i < len; ++i) f[i] = (long long)f[i] * g[i] % mod; n   NTT(f, len, -1); n   for (int i = m - 1; i < n; ++i) add(res[i], f[i]); n   for (int i = 0; i < m; ++i) f[i] = mod1(vs[i] + p[s[i]]); n   for (int i = m; i < len; ++i) f[i] = 0; n   for (int i = 0; i < n; ++i) n     g[i] = (long long)vt[i] * vt[i] % mod * vt[i] % mod; n   for (int i = n; i < len; ++i) g[i] = 0; n   NTT(f, len, 1); n   NTT(g, len, 1); n   for (int i = 0; i < len; ++i) f[i] = (long long)f[i] * g[i] % mod; n   NTT(f, len, -1); n   for (int i = m - 1; i < n; ++i) sub(res[i], mod1(2 * f[i])); n   for (int i = m - 1; i < n; ++i) n     if (!res[i]) n       cout <<  1 ; n     else n       cout <<  0 ; n } n