#include <bits/stdc++.h> n using namespace std; n int indeg[510], vis[510], indeg1[510], vis1[510]; n int n, m; n vector<vector<int> > adj(510); n int check(int x) { n   int i, c, p; n   queue<int> q; n   for (i = 0; i < n; i++) { n     vis1[i] = vis[i]; n     indeg1[i] = indeg[i]; n   } n   indeg1[x]--; n   q.push(x); n   c = 1; n   while (!q.empty()) { n     p = q.front(); n     q.pop(); n     for (i = 0; i < adj[p].size(); i++) { n       indeg1[adj[p][i]]--; n       if (indeg1[adj[p][i]] == 0 && vis1[adj[p][i]] == 0) { n         vis1[adj[p][i]] = 1; n         c++; n         q.push(adj[p][i]); n       } n     } n   } n   return c; n } n int toposort() { n   int i, c = 0, j, p; n   queue<int> q; n   vector<int> v; n   for (i = 0; i < n; i++) { n     indeg[i] = 0; n     vis[i] = 0; n   } n   for (i = 0; i < n; i++) { n     for (j = 0; j < adj[i].size(); j++) { n       indeg[adj[i][j]]++; n     } n   } n   for (i = 0; i < n; i++) { n     if (indeg[i] == 0) { n       q.push(i); n       vis[i] = 1; n       c++; n     } n   } n   while (!q.empty()) { n     p = q.front(); n     q.pop(); n     for (i = 0; i < adj[p].size(); i++) { n       indeg[adj[p][i]]--; n       if (indeg[adj[p][i]] == 0 && vis[adj[p][i]] == 0) { n         vis[adj[p][i]] = 1; n         c++; n         q.push(adj[p][i]); n       } n     } n   } n   if (c == n) { n     cout <<  YES  << endl; n     exit(0); n   } else { n     for (i = 0; i < n; i++) { n       if (indeg[i] == 1) { n         v.push_back(i); n       } n     } n     if (v.size() == 0) { n       cout <<  NO  << endl; n       exit(0); n     } else { n       for (i = 0; i < v.size(); i++) n         if (check(v[i]) + c == n) { n           cout <<  YES  << endl; n           exit(0); n         } n       cout <<  NO  << endl; n       exit(0); n     } n   } n } n int main() { n   std::ios::sync_with_stdio(false); n   int u, v, i; n   cin >> n >> m; n   for (i = 0; i < m; i++) { n     cin >> u >> v; n     u--; n     v--; n     adj[u].push_back(v); n   } n   toposort(); n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-9; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n template <typename t1> n t1 gcd(t1 a, t1 b) { n   return b == 0 ? a : gcd(b, a % b); n } n template <typename t1> n t1 lcm(t1 a, t1 b) { n   return a * (b / gcd(a, b)); n } n int main() { n   string s; n   while (cin >> s) { n     stack<char> st; n     int len = s.length(); n     int count = 0; n     int flag = 0; n     for (int i = 0; i < len; i++) { n       char x = s[i]; n       if (x ==  <  || x ==  (  || x ==  {  || x ==  [ ) { n         st.push(x); n       } else { n         if (st.empty()) { n           flag = 1; n           break; n         } n         char y = st.top(); n         if (y ==  <  && x ==  > ) { n           st.pop(); n         } else if (y ==  (  && x ==  ) ) { n           st.pop(); n         } else if (y ==  [  && x ==  ] ) { n           st.pop(); n         } else if (y ==  {  && x ==  } ) { n           st.pop(); n         } else { n           count++; n           st.pop(); n         } n       } n     } n     if (!st.empty()) flag = 1; n     if (flag) n       cout <<  Impossible  << endl; n     else n       cout << count << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 dadi(chrono::high_resolution_clock::now().time_since_epoch().count()); n const int N = 100 + 5, K = 1e3 + 5; n int n, m, k, w[N], p[K], cnt[N][K]; n struct game { n   int u, v, t; n } a[K]; n inline int add(int u, int v) { n   shuffle(p, p + k, dadi); n   int mx1 = *max_element(cnt[u], cnt[u] + k); n   int mx2 = *max_element(cnt[v], cnt[v] + k); n   for (int i = 0; i < k; i++) n     if (cnt[u][p[i]] < mx1 && cnt[v][p[i]] < mx2) return p[i]; n   for (int i = 0; i < k; i++) n     if (cnt[u][p[i]] < mx1 || cnt[v][p[i]] < mx2) return p[i]; n   return p[0]; n } n inline void read_input() { n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) cin >> w[i]; n   for (int i = 0; i < m; i++) { n     cin >> a[i].u >> a[i].v; n     a[i].u--, a[i].v--; n   } n } n inline void solve() { n   for (iota(p, p + k, 0); true; memset(cnt, 0, sizeof cnt)) { n     bool flag = true; n     for (int i = 0; i < m; i++) { n       cnt[a[i].u][a[i].t = add(a[i].u, a[i].v)]++; n       cnt[a[i].v][a[i].t++]++; n     } n     for (int i = 0; i < n; i++) n       flag &= n           *max_element(cnt[i], cnt[i] + k) - *min_element(cnt[i], cnt[i] + k) < n           3; n     if (flag) break; n   } n } n inline void write_output() { n   for (int i = 0; i < m; i++) cout << a[i].t << endl; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   read_input(), solve(), write_output(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int k, m = 1e9, a, b, n, i; n int main() { n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %d%d , &a, &b); n     m = min(m, b); n     k += m * a; n   } n   printf( %d n , k); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #define rep(i, a, b) for(int i=a; i<=b; i++) n #define repd(i, a, b) for(int i=a; i>=b; i--) n #define vi vector<int> n #define vll vector<long long> n #define pii pair<int,int> n #define pll pair<long long,long long> n #define pb push_back n #define fr first n #define sc second n #define vb vector<bool> n  n typedef long long ll; n typedef unsigned long long ull; n  n /*struct cmp { n     bool operator()(const pair<int, pii> &p1, const pair<int, pii> &p2) { n         return p1.fr > p2.fr; n     } n };*/ n  n void google(){ n  static int tc=1; n  cout<< Case # <<tc<< :  ; n  tc++; n } n  n int n; n  n void solve(int tc = 1) { n   n  vi v(n); n  for(int i=0;i<n;i++) cin>>v[i]; n   n  int cnt=0; n  ll h=0; n  priority_queue<ll> q; n  for(int i=0;i<n;i++){ n   h+=v[i]; n   if(v[i]<0){ n    q.push(-v[i]); n   } n   cnt++; n   if(h<0){ n    int val=q.top(); n    q.pop(); n    cnt--; n    h+=val; n   } n   // cout<<cnt<<   <<h<<endl; n  } n   n  cout<<cnt<<endl; n } n  n int main() { n /*#ifndef ONLINE_JUDGE n     freopen( input.txt ,  r , stdin); n     freopen( output.txt ,  w , stdout); n #endif*/ n     ios::sync_with_stdio(false); n     cin.tie(0); n     //cout << setprecision(16) << fixed; n     int t = 1; n     // cin >> t; n     int tc=0; n     while (t--) { n      cin>>n; n         tc++; n         solve(tc); n     } n } n  n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1e9 + 7; n long long a[1000005], b[1000005]; n long long nine[100], t[100]; n int main() { n   int n, k; n   t[0] = 1LL; n   for (int i = (1); i <= (10); i++) t[i] = (t[i - 1] * 10LL) % mod; n   nine[1] = 9LL; n   for (int i = (2); i <= (10); i++) nine[i] = (nine[i - 1] * 10 + 9) % mod; n   long long ans = 1; n   scanf( %d , &(n)); n   scanf( %d , &(k)); n   for (int i = (1); i <= (n / k); i++) scanf( %lld , &(a[i])); n   for (int i = (1); i <= (n / k); i++) scanf( %lld , &(b[i])); n   t[0] = 0; n   nine[0] = 9; n   for (int i = (1); i <= (n / k); i++) { n     long long f = ((t[k] - 1) / a[i]) + 1; n     f %= mod; n     long long ss = b[i]; n     long long temp = 0; n     long long st = ss * t[k - 1]; n     long long e = st + nine[k - 1]; n     if (k == 1) st = b[i]; n     if (k == 1) e = st; n     temp = ((e / a[i]) - ((st - 1) / a[i])); n     if (st == 0) temp = e / a[i] + 1; n     f -= temp; n     if (f < mod) f += mod; n     ans = (ans * f) % mod; n   } n   cout << ans % mod << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, x1, y1, x2, y2, j, k, sum = 0; n   int a[101][101]; n   memset(a, 0, sizeof(a)); n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %d%d%d%d , &x1, &y1, &x2, &y2); n     for (k = x1; k <= x2; k++) n       for (j = y1; j <= y2; j++) a[k][j]++; n   } n   for (i = 1; i <= 100; i++) n     for (j = 1; j <= 100; j++) sum += a[i][j]; n   printf( %d n , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int spend(int x) { n   int b, total, cash_back, left; n   cash_back = x / 10; n   b = cash_back * 10; n   left = x - b + cash_back; n   if (cash_back < 1) { n     return left; n   } n   return b + spend(left); n } n int main() { n   int testcase; n   cin >> testcase; n   int burles[testcase]; n   for (int i = 0; i < testcase; ++i) { n     cin >> burles[i]; n     cout << spend(burles[i]); n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, q; n string s; n int main() { n   cin >> n >> q; n   while (q--) { n     vector<pair<long long, long long> > v; n     long long x, l = 1, r = n, cur = n / 2 + 1; n     cin >> x >> s; n     while (cur != x) { n       v.push_back({l, r}); n       if (x < cur) { n         r = cur - 1; n         cur = (l + r) / 2; n       } else { n         l = cur + 1; n         cur = (l + r) / 2; n       } n     } n     for (int i = 0; i < s.size(); i++) { n       if (s[i] ==  U ) { n         if (!v.empty()) { n           l = v.back().first, r = v.back().second; n           cur = (l + r) / 2; n           v.pop_back(); n         } n       } n       if (s[i] ==  L ) { n         if (l != r) { n           v.push_back({l, r}); n           r = cur - 1; n           cur = (l + r) / 2; n         } n       } n       if (s[i] ==  R ) { n         if (l != r) { n           v.push_back({l, r}); n           l = cur + 1; n           cur = (l + r) / 2; n         } n       } n     } n     cout << cur << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve(void); n int main() { n   { n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n     { solve(); } n   } n   return 0; n } n void solve() { n   long long int n, i; n   cin >> n; n   vector<long long int> v; n   for (i = 1; i * i <= n; i++) { n     if (n % i == 0) { n       v.push_back(i); n       if (i != n / i) v.push_back(n / i); n     } n   } n   sort(v.rbegin(), v.rend()); n   for (auto x : v) { n     long long int len = n / x; n     long long int a1 = 1; n     long long int aend = 1 + (len - 1) * x; n     cout << (len * (a1 + aend)) / 2 <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2, typename T3> n T2 x_pow_y(T1 x, T3 y, T2 mod) { n   T2 ans = 1; n   long long a = x % mod; n   while (y) { n     if (y & 1) ans = (ans * a) % mod; n     a = (a * a) % mod; n     y >>= 1; n   } n   return ans; n } n template <typename T1, typename T2> n T2 inv_mod(T1 x, T2 mod) { n   return x_pow_y(x, mod - 2, mod); n } n template <typename T> n vector<T> find_factors(T &n) { n   vector<T> factors; n   int i; n   for (i = 1; i * i < n; i++) { n     if (n % i == 0) { n       factors.push_back(i); n       factors.push_back(n / i); n     } n   } n   if (i * i == n) factors.push_back(i); n   return factors; n } n void sieve(vector<int> &prime, size_t max) { n   vector<int> ar(max + 1, 1); n   ar[0] = ar[1] = 0; n   for (int i = 2; i * i <= max; i++) { n     if (!ar[i]) continue; n     for (int j = i * i; j <= max; j++) ar[j] = 0; n   } n   for (int i = 2; i <= max; i++) { n     if (ar[i]) prime.push_back(i); n   } n } n template <typename T1, typename T2> n void make_pascal_triangle_mod_p(vector<vector<T1>> &binomial_coefficient, n                                 size_t size, T2 &mod) { n   for (int i = 0; i <= size; i++) { n     vector<T1> temp(i + 1, 1); n     for (int j = 1; j <= i / 2; j++) { n       temp[i - j] = temp[j] = (binomial_coefficient[i - 1][j] + n                                binomial_coefficient[i - 1][j - 1]) % n                               mod; n     } n     binomial_coefficient.push_back(temp); n   } n } n template <typename T1, typename T2> n void find_factorial_mod_p(vector<T1> &fact, size_t size, T2 mod) { n   fact.resize(size + 1, 1); n   for (size_t i = 2; i <= size; i++) { n     fact[i] = (i * fact[i - 1]) % mod; n   } n } n template <typename T1, typename T2> n T2 nCr_mod_p(vector<T1> &fact, size_t n, size_t r, T2 &mod) { n   if (fact.empty()) find_factorial(fact, n, mod); n   size_t f_n = fact[n]; n   size_t f_r = inv_mod(fact[r], mod); n   size_t f_nr = inv_mod(fact[n - r], mod); n   return (f_nr * ((f_n * f_r) % mod)) % mod; n } n size_t nCr(size_t n, size_t r) { n   if (n - r < r) r = n - r; n   size_t ans = 1; n   for (int i = 0; i < n; i++) { n     ans *= (n - i); n     ans /= (i + 1); n   } n   return ans; n } n vector<vector<int>> out_; n bool fill_requirements(int s, int n) { n   int r_sum[n], c_sum[n]; n   memset(r_sum, 0, n * sizeof(int)); n   memset(c_sum, 0, n * sizeof(int)); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       r_sum[i] += out_[i][j]; n       c_sum[j] += out_[i][j]; n     } n   } n   bool flag = true; n   for (int i = 0; i < n; i++) { n     if (r_sum[i] != s) { n       flag = false; n       break; n     } n     if (c_sum[i] != s) { n       flag = false; n       break; n     } n   } n   int diag_sum = 0; n   for (int i = 0; i < n; i++) { n     diag_sum += out_[i][i]; n   } n   if (diag_sum != s) flag = false; n   int i = n - 1, j = 0; n   diag_sum = 0; n   while (j != n) { n     diag_sum += out_[i][j]; n     i--; n     j++; n   } n   if (diag_sum != s) flag = false; n   return flag; n } n bool flag = false; n void fill_array(int s, vector<int> &ar, int n, int i, int j, vector<int> &vis) { n   if (i == n - 1 && j == n) { n     flag = fill_requirements(s, n); n     return; n   } n   if (j == n) { n     j = 0; n     i++; n   } n   for (int indx = 0; indx < n * n; indx++) { n     if (vis[indx]) continue; n     vis[indx] = 1; n     out_[i][j] = ar[indx]; n     fill_array(s, ar, n, i, j + 1, vis); n     if (flag) { n       return; n     } else { n       vis[indx] = 0; n     } n   } n } n void solve() { n   int n; n   cin >> n; n   vector<int> ar(n * n); n   for (int i = 0; i < n * n; i++) cin >> ar[i]; n   int sum = 0; n   for (int i = 0; i < n * n; i++) sum += ar[i]; n   out_ = vector<vector<int>>(n, vector<int>(n)); n   int s = sum / n; n   vector<int> vis(n * n); n   fill_array(s, ar, n, 0, 0, vis); n   cout << s <<   n ; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) cout << out_[i][j] <<    ; n     cout <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n enum { UP, DOWN, LEFT, RIGHT }; n const int MAX = 15500; n struct Node { n   int dir; n   Node* pt[4]; n } node[MAX], *cur, *row[MAX], *col[MAX], *st[MAX]; n char s[MAX]; n int r, c, top; n int getDir(char c) { n   switch (c) { n     case  U : n       return UP; n     case  D : n       return DOWN; n     case  L : n       return LEFT; n     case  R : n       return RIGHT; n   } n   return -1; n } n void init() { n   cur = node; n   top = 0; n   for (int i = 0; i < r; i++) { n     row[i] = cur++; n     row[i]->pt[LEFT] = row[i]->pt[RIGHT] = row[i]; n     row[i]->pt[UP] = row[i]->pt[DOWN] = NULL; n     row[i]->dir = -1; n   } n   for (int i = 0; i < c; i++) { n     col[i] = cur++; n     col[i]->pt[UP] = col[i]->pt[DOWN] = col[i]; n     col[i]->pt[LEFT] = col[i]->pt[RIGHT] = NULL; n     col[i]->dir = -1; n   } n } n Node* add(int x, int y, int dir) { n   cur->pt[RIGHT] = row[x]->pt[RIGHT]; n   cur->pt[LEFT] = row[x]; n   cur->pt[DOWN] = col[y]->pt[DOWN]; n   cur->pt[UP] = col[y]; n   cur->pt[LEFT]->pt[RIGHT] = cur; n   cur->pt[RIGHT]->pt[LEFT] = cur; n   cur->pt[UP]->pt[DOWN] = cur; n   cur->pt[DOWN]->pt[UP] = cur; n   cur->dir = dir; n   return cur++; n } n int dfs(Node* t) { n   if (t->dir == -1) return 0; n   t->pt[LEFT]->pt[RIGHT] = t->pt[RIGHT]; n   t->pt[RIGHT]->pt[LEFT] = t->pt[LEFT]; n   t->pt[UP]->pt[DOWN] = t->pt[DOWN]; n   t->pt[DOWN]->pt[UP] = t->pt[UP]; n   int ret = dfs(t->pt[t->dir]) + 1; n   t->pt[LEFT]->pt[RIGHT] = t; n   t->pt[RIGHT]->pt[LEFT] = t; n   t->pt[UP]->pt[DOWN] = t; n   t->pt[DOWN]->pt[UP] = t; n   return ret; n } n int main() { n   int ret, cnt; n   while (scanf( %d%d , &r, &c) == 2) { n     ret = -1; n     cnt = 0; n     init(); n     for (int i = 0; i < r; i++) { n       scanf( %s , &s[i * c]); n     } n     for (int i = r - 1; i >= 0; i--) { n       for (int j = c - 1; j >= 0; j--) { n         if (s[i * c + j] !=  . ) st[top++] = add(i, j, getDir(s[i * c + j])); n       } n     } n     for (int i = 0; i < top; i++) { n       int sub = dfs(st[i]); n       if (sub > ret) { n         ret = sub; n         cnt = 1; n       } else if (sub == ret) { n         cnt++; n       } n     } n     printf( %d %d n , ret, cnt); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int i, j, ans = 1, val[256], v; n   string s, p; n   cin >> s; n   char a; n   for (a =  0 ; a <=  9 ; a++) { n     p.push_back(a); n   } n   for (a =  A ; a <=  Z ; a++) { n     p.push_back(a); n   } n   for (a =  a ; a <=  z ; a++) { n     p.push_back(a); n   } n   p.push_back( - ); n   p.push_back( _ ); n   for (i = 0; i <= 63; ++i) { n     val[p[i]] = i; n   } n   for (i = 0; i < s.length(); ++i) { n     v = val[s[i]]; n     for (j = 0; j < 6; ++j) { n       if ((v & (1 << j)) == 0) { n         ans = (ans * 3) % 1000000007; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void readi(int &x) { n   int v = 0, f = 1; n   char c = getchar(); n   while (!isdigit(c) && c !=  - ) c = getchar(); n   if (c ==  - ) n     f = -1; n   else n     v = v * 10 + c -  0 ; n   while (isdigit(c = getchar())) v = v * 10 + c -  0 ; n   x = v * f; n } n void readll(long long &x) { n   long long v = 0ll, f = 1ll; n   char c = getchar(); n   while (!isdigit(c) && c !=  - ) c = getchar(); n   if (c ==  - ) n     f = -1; n   else n     v = v * 10 + c -  0 ; n   while (isdigit(c = getchar())) v = v * 10 + c -  0 ; n   x = v * f; n } n void readc(char &x) { n   char c; n   while ((c = getchar()) ==    ) n     ; n   x = c; n } n void writes(string s) { puts(s.c_str()); } n void writeln() { writes(  ); } n void writei(int x) { n   if (!x) putchar( 0 ); n   char a[25]; n   int top = 0; n   while (x) { n     a[++top] = (x % 10) +  0 ; n     x /= 10; n   } n   while (top) { n     putchar(a[top]); n     top--; n   } n } n void writell(long long x) { n   if (!x) putchar( 0 ); n   char a[25]; n   int top = 0; n   while (x) { n     a[++top] = (x % 10) +  0 ; n     x /= 10; n   } n   while (top) { n     putchar(a[top]); n     top--; n   } n } n inline long long inc(int &x) { return ++x; } n inline long long inc(long long &x) { return ++x; } n inline long long inc(int &x, long long y) { return x += y; } n inline long long inc(long long &x, long long y) { return x += y; } n inline double inc(double &x, double y) { return x += y; } n inline long long dec(int &x) { return --x; } n inline long long dec(long long &x) { return --x; } n inline long long dec(int &x, long long y) { return x -= y; } n inline long long dec(long long &x, long long y) { return x -= y; } n inline double dec(double &x, double y) { return x -= y; } n inline long long mul(int &x) { return x = ((long long)x) * x; } n inline long long mul(long long &x) { return x = x * x; } n inline long long mul(int &x, long long y) { return x *= y; } n inline long long mul(long long &x, long long y) { return x *= y; } n inline double mul(double &x, double y) { return x *= y; } n inline long long divi(int &x) { return x = sqrt(x); } n inline long long divi(long long &x) { return x = sqrt(x); } n inline long long divi(int &x, long long y) { return x /= y; } n inline long long divi(long long &x, long long y) { return x /= y; } n inline double divi(double &x, double y) { return x /= y; } n inline long long mod(int &x, long long y) { return x %= y; } n inline long long mod(long long &x, long long y) { return x %= y; } n long long n, m, i, j, ans = 0; n string s; n int main() { n   cin >> n >> s; n   if (n == 1) { n     if (s ==  ? ) { n       cout <<  Yes ; n       return 0; n     } else { n       cout <<  No ; n       return 0; n     }; n   } n   s =     + s +    ; n   for (i = 1; i + 1 < s.size(); i++) { n     if (s[i] !=  ?  && s[i] == s[i - 1]) { n       cout <<  No ; n       return 0; n     }; n   } n   s[0] = s[2]; n   s[s.size() - 1] = s[s.size() - 3]; n   for (i = 1; i + 1 < s.size(); i++) { n     if (s[i] ==  ? ) { n       if (s[i - 1] != s[i + 1] && s[i - 1] !=  ?  && s[i + 1] !=  ? ) n         ans++; n       else { n         cout <<  Yes ; n         return 0; n       }; n     } n   } n   { n     cout <<  No ; n     return 0; n   }; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ii = pair<int, int>; n int z[][2] = {{-1, 0}, {1, 0}}; n vector<ii> solve(int x, int y) { n   if (x < y) swap(x, y); n   vector<ii> a; n   x -= y - 1; n   for (int k = 0; k < 2 * y - 1; ++k) { n     a.push_back({2, 3 + k}); n     if (~k & 1) n       for (int j = 0; j < 2 && x > 0; ++j, --x) n         a.push_back({2 + z[j][0], 3 + k + z[j][1]}); n   } n   if (x-- > 0) a.push_back({2, 2}); n   if (x > 0) a.push_back({2, 4 + 2 * (y - 1)}); n   return a; n } n signed main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int b, w; n     cin >> b >> w; n     if (max(w, b) > 3 * min(w, b) + 1) { n       cout <<  NO n ; n       continue; n     } n     cout <<  YES n ; n     auto a = solve(w, b); n     if (b > w) n       for (auto& [x, y] : a) --y; n     for (auto [x, y] : a) cout << x <<     << y <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool chmax(T &a, const T &b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n bool chmin(T &a, const T &b) { n   if (b < a) { n     a = b; n     return 1; n   } n   return 0; n } n signed main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   long long N, K; n   cin >> N >> K; n   vector<long long> A(N), sum(N + 1, 0); n   long long ans = 0; n   for (long long i = 0; i < N; i++) { n     cin >> A[i]; n     ans += A[i]; n     sum[i + 1] += sum[i] + A[i]; n   } n   vector<long long> tmp; n   for (long long i = (1); i <= (N - 1); ++i) { n     tmp.push_back(sum[N] - sum[i]); n   } n   sort((tmp).rbegin(), (tmp).rend()); n   for (long long i = 0; i < K - 1; i++) ans += tmp[i]; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int ans = 1, n, mod = 1000003; n   scanf( %d , &n); n   for (int i = 0; i < n - 1; i++) { n     ans *= 3; n     ans %= mod; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, sum = 0; n   string s; n   cin >> n >> s; n   for (int i = 0; i < n; i++) sum += s[i] -  0 ; n   for (int i = 2; i <= n; i++) { n     if (sum % i == 0) { n       int cur = 0; n       bool flag = 1; n       for (int j = 0; j < n; j++) { n         cur += s[j] -  0 ; n         if (sum / i < cur) { n           flag = 0; n           break; n         } n         if (sum / i == cur) cur = 0; n       } n       if (flag) { n         cout <<  YES ; n         return 0; n       } n     } n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, x; n long long f[200010]; n vector<pair<int, long long> > ke[200010]; n struct te { n   int l, r; n   long long s; n } a[200010]; n bool cmp(te i, te j) { return ((i.l < j.l) || (i.l == j.l && i.r < j.r)); } n int main() { n   if (fopen( a.txt ,  r )) { n     freopen( a.txt ,  r , stdin); n   } else n     ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> x; n   for (int i = 1; i <= n; i++) cin >> a[i].l >> a[i].r >> a[i].s; n   sort(a + 1, a + n + 1, cmp); n   int cur = 0; n   for (int i = 0; i <= x; i++) f[i] = (1LL << 61); n   long long res = (1LL << 61); n   for (int i = 1; i <= n; i++) { n     while (cur < a[i].l - 1) { n       cur++; n       for (auto tmp : ke[cur]) { n         f[tmp.first] = min(f[tmp.first], tmp.second); n       } n     } n     if (x > a[i].r - a[i].l + 1) n       res = min(res, a[i].s + f[x - a[i].r + a[i].l - 1]); n     ke[a[i].r].push_back(make_pair(a[i].r - a[i].l + 1, a[i].s)); n   } n   if (res >= (1LL << 61)) res = -1; n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n constexpr long double m_pi = 3.1415926535897932L; n constexpr long long MOD = 1000000007; n constexpr long long INF = 1LL << 61; n constexpr long double EPS = 1e-10; n template <typename T> n using vector2 = vector<vector<T>>; n template <typename T> n using vector3 = vector<vector2<T>>; n string operator*(const string& s, int k) { n   if (k == 0) return   ; n   string p = (s + s) * (k / 2); n   if (k % 2 == 1) p += s; n   return p; n } n template <class T> n inline bool chmin(T& a, T b) { n   if (a > b) { n     a = b; n     return true; n   } n   return false; n } n template <class T> n inline bool chmax(T& a, T b) { n   if (a < b) { n     a = b; n     return true; n   } n   return false; n } n struct Edge { n   int to, rev; n   long long cap; n   Edge(int _to, long long _cap, int _rev) { n     to = _to; n     cap = _cap; n     rev = _rev; n   } n }; n void add_edge(vector<vector<Edge>>& G, int from, int to, long long cap, n               bool revFlag, long long revCap) { n   G[from].push_back(Edge(to, cap, (long long)G[to].size() + (from == to))); n   if (revFlag) n     G[to].push_back(Edge(from, revCap, (long long)G[from].size() - 1)); n } n long long max_flow_dfs(vector<vector<Edge>>& G, long long v, long long t, n                        long long f, vector<bool>& used) { n   if (v == t) return f; n   used[v] = true; n   for (int i = 0; i < G[v].size(); ++i) { n     Edge& e = G[v][i]; n     if (!used[e.to] && e.cap > 0) { n       long long d = max_flow_dfs(G, e.to, t, min(f, e.cap), used); n       if (d > 0) { n         e.cap -= d; n         G[e.to][e.rev].cap += d; n         return d; n       } n     } n   } n   return 0; n } n long long max_flow(vector<vector<Edge>>& G, long long s, long long t) { n   long long flow = 0; n   for (;;) { n     vector<bool> used(G.size()); n     for (long long(i) = (long long)(0); i < (long long)(used.size()); i++) n       used[i] = false; n     long long f = max_flow_dfs(G, s, t, INF, used); n     if (f == 0) { n       return flow; n     } n     flow += f; n   } n } n void BellmanFord(vector<vector<Edge>>& G, long long s, vector<long long>& d, n                  vector<long long>& negative) { n   d.resize(G.size()); n   negative.resize(G.size()); n   for (long long(i) = (long long)(0); i < (long long)(d.size()); i++) n     d[i] = INF; n   for (long long(i) = (long long)(0); i < (long long)(d.size()); i++) n     negative[i] = false; n   d[s] = 0; n   for (long long(k) = (long long)(0); k < (long long)(G.size() - 1); k++) { n     for (long long(i) = (long long)(0); i < (long long)(G.size()); i++) { n       for (long long(j) = (long long)(0); j < (long long)(G[i].size()); j++) { n         if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { n           d[G[i][j].to] = d[i] + G[i][j].cap; n         } n       } n     } n   } n   for (long long(k) = (long long)(0); k < (long long)(G.size() - 1); k++) { n     for (long long(i) = (long long)(0); i < (long long)(G.size()); i++) { n       for (long long(j) = (long long)(0); j < (long long)(G[i].size()); j++) { n         if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { n           d[G[i][j].to] = d[i] + G[i][j].cap; n           negative[G[i][j].to] = true; n         } n         if (negative[i] == true) negative[G[i][j].to] = true; n       } n     } n   } n } n void Dijkstra(vector<vector<Edge>>& G, long long s, vector<long long>& d) { n   d.resize(G.size()); n   for (long long(i) = (long long)(0); i < (long long)(d.size()); i++) n     d[i] = INF; n   d[s] = 0; n   priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, n                  greater<pair<long long, long long>>> n       q; n   q.push(make_pair(0, s)); n   while (!q.empty()) { n     pair<long long, long long> a = q.top(); n     q.pop(); n     if (d[a.second] < a.first) continue; n     for (long long(i) = (long long)(0); i < (long long)(G[a.second].size()); n          i++) { n       Edge e = G[a.second][i]; n       if (d[e.to] > d[a.second] + e.cap) { n         d[e.to] = d[a.second] + e.cap; n         q.push(make_pair(d[e.to], e.to)); n       } n     } n   } n } n void WarshallFloyd(vector<vector<Edge>>& G, vector<vector<long long>>& d) { n   d.resize(G.size()); n   for (long long(i) = (long long)(0); i < (long long)(d.size()); i++) n     d[i].resize(G.size()); n   for (long long(i) = (long long)(0); i < (long long)(d.size()); i++) { n     for (long long(j) = (long long)(0); j < (long long)(d[i].size()); j++) { n       d[i][j] = ((i != j) ? INF : 0); n     } n   } n   for (long long(i) = (long long)(0); i < (long long)(G.size()); i++) { n     for (long long(j) = (long long)(0); j < (long long)(G[i].size()); j++) { n       chmin(d[i][G[i][j].to], G[i][j].cap); n     } n   } n   for (long long(i) = (long long)(0); i < (long long)(G.size()); i++) { n     for (long long(j) = (long long)(0); j < (long long)(G.size()); j++) { n       for (long long(k) = (long long)(0); k < (long long)(G.size()); k++) { n         if (d[j][i] != INF && d[i][k] != INF) chmin(d[j][k], d[j][i] + d[i][k]); n       } n     } n   } n } n bool tsort(vector<vector<Edge>>& graph, vector<int>& order) { n   int n = graph.size(), k = 0; n   vector<int> in(n); n   for (auto& es : graph) n     for (auto& e : es) in[e.to]++; n   priority_queue<int, vector<int>, greater<int>> que; n   for (long long(i) = (long long)(0); i < (long long)(n); i++) n     if (in[i] == 0) que.push(i); n   while (que.size()) { n     int v = que.top(); n     que.pop(); n     order.push_back(v); n     for (auto& e : graph[v]) n       if (--in[e.to] == 0) que.push(e.to); n   } n   if (order.size() != n) n     return false; n   else n     return true; n } n class Lca { n  public: n   const int n = 0; n   const int log2_n = 0; n   std::vector<std::vector<int>> parent; n   std::vector<int> depth; n   Lca() {} n   Lca(const vector<vector<Edge>>& g, int root) n       : n(g.size()), n         log2_n(log2(n) + 1), n         parent(log2_n, std::vector<int>(n)), n         depth(n) { n     dfs(g, root, -1, 0); n     for (int k = 0; k + 1 < log2_n; k++) { n       for (int v = 0; v < (int)g.size(); v++) { n         if (parent[k][v] < 0) n           parent[k + 1][v] = -1; n         else n           parent[k + 1][v] = parent[k][parent[k][v]]; n       } n     } n   } n   void dfs(const vector<vector<Edge>>& g, int v, int p, int d) { n     parent[0][v] = p; n     depth[v] = d; n     for (auto& e : g[v]) { n       if (e.to != p) dfs(g, e.to, v, d + 1); n     } n   } n   int get(int u, int v) { n     if (depth[u] > depth[v]) std::swap(u, v); n     for (int k = 0; k < log2_n; k++) { n       if ((depth[v] - depth[u]) >> k & 1) { n         v = parent[k][v]; n       } n     } n     if (u == v) return u; n     for (int k = log2_n - 1; k >= 0; k--) { n       if (parent[k][u] != parent[k][v]) { n         u = parent[k][u]; n         v = parent[k][v]; n       } n     } n     return parent[0][u]; n   } n }; n class UnionFind { n   vector<int> data; n   int n; n  n  public: n   UnionFind(int size) : data(size, -1), n(size) {} n   bool merge(int x, int y) { n     x = root(x); n     y = root(y); n     if (x != y) { n       if (data[y] < data[x]) swap(x, y); n       data[x] += data[y]; n       data[y] = x; n     } n     n -= (x != y); n     return x != y; n   } n   bool same(int x, int y) { return root(x) == root(y); } n   int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); } n   int size(int x) { return -data[root(x)]; } n   int num() { return n; } n }; n template <typename T, typename F> n class SegmentTree { n  private: n   T identity; n   F merge; n   long long n; n   vector<T> dat; n  n  public: n   SegmentTree(F f, T id, vector<T> v) : merge(f), identity(id) { n     int _n = v.size(); n     n = 1; n     while (n < _n) n *= 2; n     dat.resize(2 * n - 1, identity); n     for (long long(i) = (long long)(0); i < (long long)(_n); i++) n       dat[n + i - 1] = v[i]; n     for (int i = n - 2; i >= 0; i--) n       dat[i] = merge(dat[i * 2 + 1], dat[i * 2 + 2]); n   } n   SegmentTree(F f, T id, int _n) : merge(f), identity(id) { n     n = 1; n     while (n < _n) n *= 2; n     dat.resize(2 * n - 1, identity); n   } n   void set_val(int i, T x) { n     i += n - 1; n     dat[i] = x; n     while (i > 0) { n       i = (i - 1) / 2; n       dat[i] = merge(dat[i * 2 + 1], dat[i * 2 + 2]); n     } n   } n   T query(int l, int r) { n     T left = identity, right = identity; n     l += n - 1; n     r += n - 1; n     while (l < r) { n       if ((l & 1) == 0) left = merge(left, dat[l]); n       if ((r & 1) == 0) right = merge(dat[r - 1], right); n       l = l / 2; n       r = (r - 1) / 2; n     } n     return merge(left, right); n   } n }; n template <typename T> n class FenwickTree { n   vector<T> data; n   int n; n   int p; n  n  public: n   FenwickTree(int n) : n(n) { n     data.resize(n + 1LL, 0); n     p = 1; n     while (p < data.size()) p *= 2; n   } n   T sum(int k) { n     T ret = 0; n     for (; k > 0; k -= k & -k) ret += data[k]; n     return (ret); n   } n   T sum(int a, int b) { return sum(b) - sum(a); } n   void add(int k, T x) { n     for (++k; k <= n; k += k & -k) data[k] += x; n   } n   int lower_bound(long long w) { n     if (w <= 0) return -1; n     int x = 0; n     for (int k = p / 2; k > 0; k /= 2) { n       if (x + k <= n && data[x + k] < w) w -= data[x + k], x += k; n     } n     return x; n   } n }; n void divisor(long long n, vector<long long>& ret) { n   for (long long i = 1; i * i <= n; i++) { n     if (n % i == 0) { n       ret.push_back(i); n       if (i * i != n) ret.push_back(n / i); n     } n   } n   sort(ret.begin(), ret.end()); n } n void prime_factorization(long long n, vector<pair<long long, long long>>& ret) { n   for (long long i = 2; i * i <= n; i++) { n     if (n % i == 0) { n       ret.push_back({i, 0}); n       while (n % i == 0) { n         n /= i; n         ret[ret.size() - 1].second++; n       } n     } n   } n   if (n != 1) ret.push_back({n, 1}); n } n inline long long mod_pow(long long x, long long n, long long mod) { n   long long res = 1; n   while (n > 0) { n     if (n & 1) res = res * x % mod; n     x = x * x % mod; n     n >>= 1; n   } n   return res; n } n inline long long mod_inv(long long x, long long mod) { n   return mod_pow(x, mod - 2, mod); n } n class Combination { n  public: n   vector<long long> fact; n   vector<long long> fact_inv; n   long long mod; n   long long nCr(long long n, long long r) { n     if (n < r) return 0; n     if (n < mod) return ((fact[n] * fact_inv[r] % mod) * fact_inv[n - r]) % mod; n     long long ret = 1; n     while (n || r) { n       long long _n = n % mod, _r = r % mod; n       n /= mod; n       r /= mod; n       (ret *= nCr(_n, _r)) %= mod; n     } n     return ret; n   } n   long long nPr(long long n, long long r) { n     return (fact[n] * fact_inv[n - r]) % mod; n   } n   long long nHr(long long n, long long r) { return nCr(r + n - 1, r); } n   Combination(long long _n, long long _mod) { n     mod = _mod; n     long long n = min(_n + 1, mod); n     fact.resize(n); n     fact[0] = 1; n     for (long long(i) = (long long)(0); i < (long long)(n - 1); i++) { n       fact[i + 1] = (fact[i] * (i + 1LL)) % mod; n     } n     fact_inv.resize(n); n     fact_inv[n - 1] = mod_inv(fact[n - 1], mod); n     for (int i = n - 1; i > 0; i--) { n       fact_inv[i - 1] = fact_inv[i] * i % mod; n     } n   } n }; n long long popcount(long long x) { n   x = (x & 0x5555555555555555) + (x >> 1 & 0x5555555555555555); n   x = (x & 0x3333333333333333) + (x >> 2 & 0x3333333333333333); n   x = (x & 0x0F0F0F0F0F0F0F0F) + (x >> 4 & 0x0F0F0F0F0F0F0F0F); n   x = (x & 0x00FF00FF00FF00FF) + (x >> 8 & 0x00FF00FF00FF00FF); n   x = (x & 0x0000FFFF0000FFFF) + (x >> 16 & 0x0000FFFF0000FFFF); n   x = (x & 0x00000000FFFFFFFF) + (x >> 32 & 0x00000000FFFFFFFF); n   return x; n } n int main() { n   ios::sync_with_stdio(false); n   std::cin.tie(0); n   std::cout.tie(0); n   long long n; n   cin >> n; n   vector<long long> a(n); n   for (long long(i) = (long long)(0); i < (long long)(n); i++) cin >> a[i]; n   vector<array<int, 3>> ans; n   for (int i = n; i >= 3; i -= 2) { n     ans.push_back({i - 2, i - 1, i}); n     long long t = a[i - 3] ^ a[i - 2] ^ a[i - 1]; n     a[i - 3] = a[i - 2] = a[i - 1] = t; n   } n   for (int i = 3 + (n % 2 == 0 ? 1 : 0); i <= n - 2; i += 2) { n     ans.push_back({i, i + 1, i + 2}); n     long long t = a[i - 1] ^ a[i] ^ a[i + 1]; n     a[i - 1] = a[i] = a[i + 1] = t; n   } n   if (*min_element((a).begin(), (a).end()) == n       *max_element((a).begin(), (a).end())) { n     cout <<  YES  n          <<   n ; n     cout << ans.size() <<   n ; n     for (auto e : ans) cout << e[0] <<     << e[1] <<     << e[2] << endl; n   } else n     cout <<  NO  n          <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fastio() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout << fixed; n } n int main() { n   fastio(); n   int n; n   cin >> n; n   long long int a[n]; n   long long int totsum = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     totsum += a[i]; n   } n   int m = n - 1; n   m >>= 1; n   long long int cursum = 0; n   for (int i = 0; i < m; i++) cursum += a[2 * i]; n   long long int maxsum = totsum - cursum; n   int last = 0; n   int first = 2 * m - 2; n   for (int i = 0; i < n; i++) { n     cursum -= a[last]; n     last += 2; n     last %= n; n     first += 2; n     first %= n; n     cursum += a[first]; n     maxsum = max(maxsum, totsum - cursum); n   } n   cout << maxsum <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ii = pair<int, int>; n using i3 = pair<int, ii>; n using li = pair<ll, int>; n using lii = pair<ll, ii>; n using pll = pair<ll, ll>; n using vi = vector<int>; n using vl = vector<ll>; n using vii = vector<ii>; n using vli = vector<li>; n using vpll = vector<pll>; n using vi3 = vector<i3>; n using vlii = vector<lii>; n const int N = 2e5 + 5, L = 20; n const ll INF = 1e17 + 7, mod = 1e9 + 7; n const double eps = 1e-9, PI = acos(-1); n int n, m; n vector<ii> adj[N]; n vector<int> tree[N]; n int down[N], level[N]; n int cycleId[N]; n int to[N][L]; n int dep[N]; n int cact[N]; n void dfs(int u, int p) { n   cycleId[u] = u; n   level[u] = 1 + level[p]; n   for (ii A : adj[u]) { n     int v = A.second; n     int e = A.first; n     if (cycleId[v] == 0) { n       dfs(v, u); n     } else if (v != p) { n       if (level[v] < level[u]) n         cycleId[u] = e; n       else n         down[u]++; n     } n   } n   for (ii A : adj[u]) { n     int v = A.second; n     if (v == p) continue; n     if ((cycleId[v] != v) && (down[v] == 0)) { n       cycleId[u] = cycleId[v]; n     } n   } n } n void dfsTree(int u, int p) { n   to[u][0] = p; n   dep[u] = 1 + dep[p]; n   for (int i = 1; i < L; i++) { n     to[u][i] = to[to[u][i - 1]][i - 1]; n   } n   for (int v : tree[u]) { n     if (v != p) { n       cact[v] += cact[u]; n       dfsTree(v, u); n     } n   } n } n int jump(int u, int k) { n   for (int i = 0; i < L; i++) n     if (k & (1 << i)) u = to[u][i]; n   return u; n } n int LCA(int u, int v) { n   if (u == v) return u; n   if (dep[u] < dep[v]) swap(u, v); n   u = jump(u, dep[u] - dep[v]); n   if (u == v) return u; n   for (int i = L - 1; i >= 0; i--) { n     if (to[u][i] != to[v][i]) { n       u = to[u][i]; n       v = to[v][i]; n     } n   } n   return to[u][0]; n } n ll pow_mod(ll a, ll b) { n   ll ret = 1ll; n   a %= mod; n   for (; b; ret = ret * (b & 1 ? a : 1ll) % mod, a = a * a % mod, b >>= 1ll) n     ; n   return ret; n } n ll calc(int u, int v) { n   ll A = cact[u]; n   ll B = cact[v]; n   int lca = LCA(u, v); n   ll C = cact[lca]; n   return pow_mod(2, A + B - 2 * C + (lca > n)); n } n void solve(int testCase) { n   scanf( %d %d , &n, &m); n   vii edges; n   for (int i = 0; i < m; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     adj[u].push_back({i + n + 1, v}); n     adj[v].push_back({i + n + 1, u}); n     edges.push_back({u, v}); n   } n   level[1] = 0; n   dfs(1, 0); n   for (pair<int, int> E : edges) { n     int u = cycleId[E.first]; n     int v = cycleId[E.second]; n     if (u != v) { n       tree[u].push_back(v); n       tree[v].push_back(u); n     } n   } n   for (int i = 1; i <= n; i++) { n     cact[cycleId[i]] = (cycleId[i] != i); n   } n   dfsTree(cycleId[1], 0); n   int q; n   scanf( %d , &q); n   while (q--) { n     int u, v; n     scanf( %d %d , &u, &v); n     printf( %lld n , calc(cycleId[u], cycleId[v])); n   } n } n int main() { n   int t = 1; n   for (int testCase = 1; testCase <= t; testCase++) { n     solve(testCase); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 2e5 + 50; n using namespace std; n int n; n long long a[maxn], dp[maxn], he[maxn]; n vector<int> maps[maxn]; n void dfs(int u, int fa) { n   he[u] = 0; n   for (int i = 0, len = maps[u].size(); i < len; i++) { n     int v = maps[u][i]; n     if (v != fa) { n       dfs(v, u); n       dp[u] += dp[v] + he[v]; n       he[u] += he[v]; n     } n   } n   he[u] += a[u]; n } n long long ans; n void dfs1(int u, int fa) { n   for (int i = 0, len = maps[u].size(); i < len; i++) { n     int v = maps[u][i]; n     if (v != fa) { n       dp[v] = dp[u] - he[v] + he[1] - he[v]; n       ans = max(ans, dp[v]); n       dfs1(v, u); n     } n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1, a, b; i < n; i++) { n     scanf( %d%d , &a, &b); n     maps[a].push_back(b); n     maps[b].push_back(a); n   } n   dfs(1, 1); n   ans = dp[1]; n   dfs1(1, 1); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, k; n   cin >> n >> k; n   vector<int> a(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   long long maxa = 0; n   for (int i = 0; i < k; i++) { n     maxa += n - i; n   } n   vector<int> pos; n   for (int i = 0; i < n; i++) { n     if (a[i] > n - k) { n       pos.emplace_back(i); n     } n   } n   sort(pos.begin(), pos.end()); n   long long ans = 1; n   for (int i = 1; i < pos.size(); i++) { n     ans *= pos[i] - pos[i - 1]; n     ans %= mod; n   } n   cout << maxa <<     << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = 3.141592653589793238462643383; n int main() { n   { n     int n, m, k, t; n     cin >> n >> m >> k >> t; n     set<int> s; n     ; n     for (long long i = 0; i < k; i++) { n       int a, b; n       cin >> a >> b; n       int id = (a - 1) * m + b; n       s.insert(id); n     }; n     for (long long i = 0; i < t; i++) { n       int a, b; n       cin >> a >> b; n       int id = (a - 1) * m + b; n       if (s.find(id) != s.end()) n         cout <<  Waste  << endl; n       else { n         auto it = lower_bound(s.begin(), s.end(), id); n         int d = distance(s.begin(), it); n         int val = (id - d) % 3; n         if (val == 0) n           cout <<  Grapes  << endl; n         else if (val == 1) n           cout <<  Carrots  << endl; n         else n           cout <<  Kiwis  << endl; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n array<string, 1510> arr; n unordered_set<string> f; n int n, k, t = int( S  +  E  +  T ); n char get(char c1, char c2) { n   if (c1 == c2) return c1; n   return t - c1 - c2; n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) { n     cin >> arr[i]; n     f.insert(arr[i]); n   } n   int res = 0; n   for (int i = 1; i <= n; i++) n     for (int j = i + 1; j <= n; j++) { n       string s =   ; n       for (int p = 0; p < k; p++) s.push_back(get(arr[i][p], arr[j][p])); n       if (f.count(s)) res++; n     } n   printf( %d n , res / 3); n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   int n; n   cin >> n; n   vector<int> a(n); n   for (int& i : a) cin >> i; n   int M = *min_element((a).begin(), (a).end()); n   int c = 0; n   for (int& i : a) c += M == i; n   if (c <= n / 2) n     cout <<  Alice ; n   else n     cout <<  Bob ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   int n; n   cin >> t; n   while (t--) { n     cin >> n; n     for (int i = 0; i < n; i++) cout << 400 <<    ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n int _min(int a, int b) { n   if (a < b) return a; n   return b; n } n int _max(int a, int b) { n   if (a > b) return a; n   return b; n } n int main() { n   int i, j, k; n   int n; n   int x1, y1, x2, y2, x3, y3, res; n   scanf( %d %d , &x1, &y1); n   scanf( %d %d , &x2, &y2); n   scanf( %d %d , &x3, &y3); n   if ((x1 == x2 && x1 == x3) || (y1 == y2 && y1 == y3)) { n     printf( 1 ); n     return 0; n   } n   if (x1 == x2) { n     int min = _min(y1, y2); n     int max = _max(y1, y2); n     if (y3 <= min || y3 >= max) { n       printf( 2 ); n       return 0; n     } n   } n   if (y1 == y2) { n     int min = _min(x1, x2); n     int max = _max(x1, x2); n     if (x3 <= min || x3 >= max) { n       printf( 2 ); n       return 0; n     } n   } n   if (x1 == x3) { n     int min = _min(y1, y3); n     int max = _max(y1, y3); n     if (y2 <= min || y2 >= max) { n       printf( 2 ); n       return 0; n     } n   } n   if (y1 == y3) { n     int min = _min(x1, x3); n     int max = _max(x1, x3); n     if (x2 <= min || x2 >= max) { n       printf( 2 ); n       return 0; n     } n   } n   if (x3 == x2) { n     int min = _min(y3, y2); n     int max = _max(y3, y2); n     if (y1 <= min || y1 >= max) { n       printf( 2 ); n       return 0; n     } n   } n   if (y3 == y2) { n     int min = _min(x3, x2); n     int max = _max(x3, x2); n     if (x1 <= min || x1 >= max) { n       printf( 2 ); n       return 0; n     } n   } n   printf( 3 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, m, ax, ay, cx, cy; n int d[100][100], s[100][100]; n void take(int ax, int ay, int m) { n   for (int y = ay; y < ay + m; ++y) { n     for (int i = y; i <= k; ++i) ++s[ax][i]; n   } n } n int main() { n   while (scanf( %d%d , &n, &k) != EOF) { n     cx = cy = (k + 1) / 2; n     memset(s, 0, sizeof(s)); n     for (int x = 1; x <= k; ++x) n       for (int y = 1; y <= k; ++y) n         d[x][y] = d[x][y - 1] + abs(x - cx) + abs(y - cy); n     for (int i = 0; i < n; ++i) { n       scanf( %d , &m); n       int ax = -1, ay = -1, mini = 1000000000; n       for (int x = 1; x <= k; ++x) { n         for (int y = 1; y + m - 1 <= k; ++y) { n           if (s[x][y + m - 1] - s[x][y - 1] == 0) { n             if (d[x][y + m - 1] - d[x][y - 1] < mini) { n               ax = x, ay = y; n               mini = d[x][y + m - 1] - d[x][y - 1]; n             } n           } n         } n       } n       if (ax == -1) { n         puts( -1 ); n       } else { n         take(ax, ay, m); n         printf( %d %d %d n , ax, ay, ay + m - 1); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n]; n   int o = 0, s = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     s = s + a[i]; n     if ((a[i] % 2) != 0) { n       o++; n     } n   } n   if ((s % 2) == 0) { n     cout << n - o; n   } else { n     cout << o; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2 * 1e5 + 100, mod = 1e9 + 7; n int n, k, a, b; n int dp[5010][5010], pr[5010][5010]; n int add(int a, int b) { n   a += b; n   if (a < 0) a += mod; n   if (a >= mod) a -= mod; n   return a; n } n int main() { n   scanf( %d%d%d%d , &n, &a, &b, &k); n   dp[0][a] = 1; n   for (int i = 0; i <= k; i++) { n     if (i != 0) { n       for (int j = 1; j <= n; j++) { n         if (j > b) { n           int t = ((j + b) >> 1) + 1; n           dp[i][j] += add(add(pr[i - 1][n], -pr[i - 1][t - 1]), -dp[i - 1][j]); n         } else if (j < b) { n           int t = ((j + b - 1) >> 1); n           dp[i][j] += add(pr[i - 1][t], -dp[i - 1][j]); n         } n       } n     } n     for (int j = 1; j <= n; j++) { n       pr[i][j] = add(pr[i][j - 1], dp[i][j]); n     } n   } n   cout << pr[k][n] % mod; n } n


#include <bits/stdc++.h> n using namespace std; n const int n0 = 1503, inf = 1e9; n int n, s, m, k, a[n0], b[n0], p[n0], L[n0]; n int dp[2][n0], mx[n0], best[n0]; n int num(int pos) { n   for (int i = 1; i <= n; i++) { n     p[i] = p[i - 1] + (a[i] <= b[pos]); n   } n   memset(&dp, -0x3f, sizeof(dp)); n   int res = 0; n   for (int i = 1; i <= m; i++) { n     dp[(i - 1) & 1][0] = 0; n     mx[0] = dp[(i - 1) & 1][0]; n     for (int j = 1; j <= n; j++) { n       mx[j] = max(mx[j - 1], dp[(i - 1) & 1][j]); n       if (L[j] != n0) { n         if (best[j] != -1) n           dp[i & 1][j] = dp[(i - 1) & 1][best[j]] - p[best[j]] + p[j]; n         dp[i & 1][j] = max(dp[i & 1][j], mx[L[j] - 1] + p[j] - p[L[j] - 1]); n         if (i == m) res = max(res, dp[i & 1][j]); n       } n     } n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0); n   cin >> n >> s >> m >> k; n   for (int i = 1; i <= n; i++) cin >> a[i], b[i] = a[i], L[i] = n0; n   sort(b + 1, b + n + 1); n   for (int i = 0; i < s; i++) { n     int l, r; n     cin >> l >> r; n     L[r] = min(L[r], l); n   } n   for (int i = 1; i <= n; i++) { n     best[i] = -1; n     for (int j = L[i]; j < i; j++) { n       if (best[i] == -1 || L[j] < L[best[i]]) best[i] = j; n     } n   } n   int l = 0, r = n + 1; n   while (l < r - 1) { n     int mid = l + r >> 1; n     if (num(mid) >= k) n       r = mid; n     else n       l = mid; n   } n   if (r == n + 1) n     cout << -1; n   else n     cout << b[r]; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 4e4 + 44; n const long long INF = 1e11 + 9; n const double eps = 1e-12; n int main() { n   int t; n   scanf( %d , &t); n   long long x1, y1, x2, y2, n, m; n   long long x3, y3, x4, y4; n   long long answ = 0, ansb = 0; n   while (t--) { n     scanf( %lld , &n); n     scanf( %lld , &m); n     if (n % 2 == 0 || m % 2 == 0) { n       answ = ansb = n * m / 2; n     } else { n       answ = (n * m) / 2 + 1; n       ansb = answ - 1; n     } n     scanf( %lld , &x1); n     scanf( %lld , &y1); n     scanf( %lld , &x2); n     scanf( %lld , &y2); n     long long dx = x2 - x1 + 1, dy = y2 - y1 + 1; n     if (dx % 2 == 0 || dy % 2 == 0) { n       answ += (dx * dy) / 2; n       ansb -= (dx * dy) / 2; n     } else { n       if (x1 % 2 && y1 % 2 || x1 % 2 == 0 && y1 % 2 == 0) { n         answ += (dx * dy) / 2; n         ansb -= (dx * dy) / 2; n       } else { n         answ += (dx * dy) / 2 + 1; n         ansb -= (dx * dy) / 2 + 1; n       } n     } n     scanf( %lld , &x3); n     scanf( %lld , &y3); n     scanf( %lld , &x4); n     scanf( %lld , &y4); n     dx = x4 - x3 + 1, dy = y4 - y3 + 1; n     if (dx % 2 == 0 || dy % 2 == 0) { n       answ -= (dx * dy) / 2; n       ansb += (dx * dy) / 2; n     } else { n       if (x3 % 2 && y3 % 2 || x3 % 2 == 0 && y3 % 2 == 0) { n         answ -= (dx * dy) / 2 + 1; n         ansb += (dx * dy) / 2 + 1; n       } else { n         answ -= (dx * dy) / 2; n         ansb += (dx * dy) / 2; n       } n     } n     long long x5, y5, x6, y6; n     if (y3 > y2 || y4 < y1 || x3 > x2 || x4 < x1) { n       printf( %lld %lld n , answ, ansb); n       continue; n     } n     x5 = max(x1, x3); n     y5 = max(y1, y3); n     x6 = min(x2, x4); n     y6 = min(y2, y4); n     dx = x6 - x5 + 1, dy = y6 - y5 + 1; n     if (dx % 2 == 0 || dy % 2 == 0) { n       if (x5 % 2 == 0 && y5 % 2 == 0 || x5 % 2 && y5 % 2) { n         answ -= dx * dy / 2; n         ansb += dx * dy / 2; n       } else { n         answ -= dx * dy / 2; n         ansb += dx * dy / 2; n       } n     } else { n       if (x5 % 2 && y5 % 2 || x5 % 2 == 0 && y5 % 2 == 0) { n         answ -= dx * dy / 2; n         ansb += dx * dy / 2; n       } else { n         answ -= dx * dy / 2 + 1; n         ansb += dx * dy / 2 + 1; n       } n     } n     printf( %lld %lld n , answ, ansb); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> deg(n), s(n); n   for (int i = 0; i < n; i++) cin >> deg[i] >> s[i]; n   vector<pair<int, int>> edges; n   queue<int> q; n   for (int i = 0; i < n; i++) n     if (deg[i] == 1) q.push(i); n   while (!q.empty()) { n     int leaf = q.front(); n     q.pop(); n     if (deg[leaf] != 1) continue; n     int par = s[leaf]; n     edges.push_back({par, leaf}); n     deg[par] -= 1; n     s[par] ^= leaf; n     if (deg[par] == 1) q.push(par); n   } n   cout << edges.size() << endl; n   for (auto p : edges) cout << p.first <<     << p.second <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m; n   cin >> n >> m; n   long long k[n]; n   pair<long long, long long> dt[m]; n   for (int i = 0; i < n; i++) cin >> k[i]; n   for (int i = 0; i < m; i++) cin >> dt[i].first >> dt[i].second; n   long long total = 0; n   for (int i = 0; i < n; i++) total += k[i]; n   long long start = 1; n   long long end = 2 * total; n   long long ans = 2 * total; n   while (start <= end) { n     long long check = start + (end - start) / 2; n     pair<long long, long long> last[n]; n     for (int i = 0; i < n; i++) { n       last[i].first = LONG_MAX; n       last[i].second = i; n     } n     for (int i = 0; i < m; i++) { n       if (dt[i].first <= check) { n         if (last[dt[i].second - 1].first == LONG_MAX) n           last[dt[i].second - 1].first = dt[i].first; n         else n           last[dt[i].second - 1].first = n               max(dt[i].first, last[dt[i].second - 1].first); n       } n     } n     sort(last, last + n); n     int point = 0; n     long long money = 0; n     long long count = 0; n     for (int i = 1; i <= check; i++) { n       money++; n       while (last[point].first == i) { n         long long deduce = min(money, k[last[point].second]); n         count += deduce; n         money -= deduce; n         point++; n       } n     } n     money -= 2 * (total - count); n     if (money < 0) { n       start = check + 1; n     } else { n       ans = check; n       end = check - 1; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char c[1005], s[55], t[55]; n int ns[55], nt[55]; n int tos[55][30], tot[55][30]; n int lenc, lens, lent; n int dp[1005][55][55]; n void init() { n   lenc = strlen(c + 1), lens = strlen(s + 1), lent = strlen(t + 1); n   ns[1] = 0, nt[1] = 0; n   for (int i = 2, j = 0; i <= lens; i++) { n     while (j && s[j + 1] != s[i]) j = ns[j]; n     if (s[j + 1] == s[i]) j++; n     ns[i] = j; n   } n   for (int i = 2, j = 0; i <= lent; i++) { n     while (j && t[j + 1] != t[i]) j = nt[j]; n     if (t[j + 1] == t[i]) j++; n     nt[i] = j; n   } n   for (int i = 0; i <= lens; i++) { n     for (int k = 0; k < 26; k++) { n       int j = i; n       while (j && s[j + 1] != k +  a ) j = ns[j]; n       if (s[j + 1] == k +  a ) j++; n       tos[i][k] = j; n     } n   } n   for (int i = 0; i <= lent; i++) { n     for (int k = 0; k < 26; k++) { n       int j = i; n       while (j && t[j + 1] != k +  a ) j = nt[j]; n       if (t[j + 1] == k +  a ) j++; n       tot[i][k] = j; n     } n   } n } n int main() { n   scanf( %s%s%s , c + 1, s + 1, t + 1); n   init(); n   int n = strlen(c + 1); n   for (int i = 0; i <= n; i++) n     for (int j = 0; j <= 50; j++) n       for (int k = 0; k <= 50; k++) dp[i][j][k] = -1000000; n   dp[0][0][0] = 0; n   for (int i = 1; i <= n; i++) { n     if (c[i] ==  * ) { n       for (int j = 0; j <= lens; j++) { n         for (int k = 0; k <= lent; k++) { n           if (dp[i - 1][j][k] <= -1000000) continue; n           for (int q = 0; q < 26; q++) { n             int temp1 = tos[j][q], temp2 = tot[k][q]; n             dp[i][temp1][temp2] = n                 max(dp[i][temp1][temp2], n                     dp[i - 1][j][k] + (temp1 == lens) - (temp2 == lent)); n           } n         } n       } n     } else { n       for (int j = 0; j <= lens; j++) { n         for (int k = 0; k <= lent; k++) { n           if (dp[i - 1][j][k] <= -1000000) continue; n           int q = c[i] -  a ; n           int temp1 = tos[j][q], temp2 = tot[k][q]; n           dp[i][temp1][temp2] = n               max(dp[i][temp1][temp2], n                   dp[i - 1][j][k] + (temp1 == lens) - (temp2 == lent)); n         } n       } n     } n   } n   int res = -1000000; n   for (int i = 0; i <= lens; i++) n     for (int j = 0; j <= lent; j++) res = max(res, dp[n][i][j]); n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n const int MAXN = 1000; n const int MAXK = 1000; n int n, K; n char s[MAXN + 1]; n int act[MAXN + 1][2 * MAXK + 1]; n char ret[MAXN + 1]; n void run() { n   scanf( %d%d%s , &n, &K, s); n   memset(act, -1, sizeof(act)); n   act[0][MAXK] = 1; n   for (int i = (0); i < (n); ++i) { n     int mxb = min(i, K - 1); n     for (int k = (-mxb); k <= (+mxb); ++k) n       if (act[i][MAXK + k] != -1) { n         for (int a = (-1); a <= (+1); ++a) { n           if (s[i] ==  L  && a != -1) continue; n           if (s[i] ==  D  && a != 0) continue; n           if (s[i] ==  W  && a != +1) continue; n           act[i + 1][MAXK + k + a] = a + 1; n         } n       } n   } n   for (int k = -K; k <= +K; k += 2 * K) n     if (act[n][MAXK + k] != -1) { n       for (int i = n - 1; i >= 0; --i) { n         ret[i] =  LDW [act[i + 1][MAXK + k]]; n         k -= act[i + 1][MAXK + k] - 1; n       } n       ret[n] =   0 ; n       printf( %s , ret); n       return; n     } n   printf( NO n ); n } n int main() { n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string nameOne, nameTwo, tmpOne, tmpTwo; n   cin >> nameOne >> nameTwo; n   int n; n   cin >> n; n   cout << nameOne <<     << nameTwo << endl; n   for (int i = 0; i < n; i++) { n     cin >> tmpOne; n     cin >> tmpTwo; n     if (tmpOne == nameOne) n       nameOne = tmpTwo; n     else n       nameTwo = tmpTwo; n     cout << nameOne <<     << nameTwo << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve(long long int arr[], long long int n, long long int l) { n   for (int i = 1; i < n; i++) { n     arr[i] = min(arr[i], 2 * arr[i - 1]); n   } n   long long int exp[n]; n   for (int i = 0; i < n; i++) { n     exp[i] = 1LL << i; n   } n   long long int ans = LLONG_MAX; n   long long int val = 0; n   for (int i = n - 1; i >= 0; i--) { n     long long int currentAns = val + (l + exp[i] - 1) / exp[i] * arr[i]; n     ans = min(ans, currentAns); n     long long int x = l / exp[i]; n     l -= x * exp[i]; n     val += x * arr[i]; n   } n   cout << ans << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t = 1; n   while (t--) { n     long long int n, l; n     cin >> n >> l; n     long long int arr[n]; n     for (int i = 0; i < n; i++) { n       cin >> arr[i]; n     } n     solve(arr, n, l); n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.second < b.second); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m, i, j, c; n   string s, f =  YES ; n   vector<string> v; n   cin >> n >> m; n   for (i = 0; i < n; i++) { n     cin >> s; n     v.push_back(s); n   } n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       if (v[i][j] ==  * ) continue; n       c = 0; n       if (i - 1 >= 0 && v[i - 1][j] ==  * ) c++; n       if (i - 1 >= 0 && j - 1 >= 0 && v[i - 1][j - 1] ==  * ) c++; n       if (i - 1 >= 0 && j + 1 < m && v[i - 1][j + 1] ==  * ) c++; n       if (j - 1 >= 0 && v[i][j - 1] ==  * ) c++; n       if (j + 1 < m && v[i][j + 1] ==  * ) c++; n       if (i + 1 < n && v[i + 1][j] ==  * ) c++; n       if (i + 1 < n && j - 1 >= 0 && v[i + 1][j - 1] ==  * ) c++; n       if (i + 1 < n && j + 1 < m && v[i + 1][j + 1] ==  * ) c++; n       if (c > 0 && c != v[i][j] -  0 ) f =  NO ; n       if (!c && v[i][j] !=  . ) f =  NO ; n     } n   } n   cout << f; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long fastPrime(long long n) { n   if (n == 2) return true; n   if (n % 2 == 0) return false; n   for (long long i = 3; i * i <= n; i++) { n     if (n % i == 0) return false; n   } n   return true; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, val; n   cin >> n; n   for (long long i = 0; i < n; i++) { n     cin >> val; n     long long sr = sqrt(val); n     if (val != 1 && fastPrime(sr) && sr * sr == val) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 5e5 + 5; n template <typename T> n void chkmax(T& x, T y) { n   if (y > x) x = y; n } n template <typename T> n void chkmin(T& x, T y) { n   if (y < x) x = y; n } n int N, Q, a[1 << 18]; n long long sum; n int main() { n   scanf( %d%d , &N, &Q); n   for (int i = 0; i < (1 << N); ++i) scanf( %d , &a[i]), sum += a[i]; n   printf( %.7f n , sum / (double)(1 << N)); n   while (Q--) { n     int p, x; n     scanf( %d%d , &p, &x), sum -= a[p], a[p] = x, sum += a[p]; n     printf( %.7f n , sum / (double)(1 << N)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int maxm = 1e5 + 80; n const int maxn = 5e3 + 80; n const int up = 20; n template <class T> n void read(T &x) { n   x = 0; n   char c = getchar(); n   int f = 1; n   for (; c <  0  || c >  9 ; c = getchar()) n     if (c ==  - ) f = -1; n   for (;  0  <= c && c <=  9 ; c = getchar()) x = x * 10 + (c ^ 48); n   x *= f; n } n unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); n mt19937 rand_num(seed); n uniform_int_distribution<int> dist(0, 10000000); n mt19937 rnd(time(NULL)); n int e[maxm][2], cnt[maxm]; n int f[maxm], flag, nxt; n int get_rand(int from, int to) { n   int x = dist(rand_num) % (to - from + 1); n   return x + from; n } n bool solve(int n, int m) { n   shuffle(f + 1, f + 1 + n, rnd); n   for (int i = 2; i <= m; i++) { n     int u = f[i - 1], v = f[i], ti = 0; n     bool wa = (e[u][0] == v | e[u][1] == v); n     if (wa && i == n) return false; n     while (wa && ti < up) { n       int t = get_rand(i + 1, n); n       ti++; n       swap(f[i], f[t]); n       v = f[i]; n       wa = (e[u][0] == v | e[u][1] == v); n     } n     if (ti == up) return false; n   } n   nxt = 0; n   if (m == 2 || m == 1) nxt = 1; n   if (e[f[m]][0] == f[1] || e[f[m]][1] == f[1]) nxt = 1; n   if (nxt == 1 && n == m) return false; n   if (nxt == 1) { n     int u = f[m], v = f[m + 1], ti = 0; n     bool wa = (e[u][0] == v | e[u][1] == v); n     while (wa && ti < up) { n       int t = get_rand(m + 1, n); n       ti++; n       swap(f[m + 1], f[t]); n       v = f[m + 1]; n       wa = (e[u][0] == v | e[u][1] == v); n     } n     if (ti == up) return false; n   } n   flag = 1; n   return true; n } n void MC() { n   int n, m; n   read(n); n   read(m); n   for (int i = 1; i <= m; i++) { n     int u, v; n     read(u); n     read(v); n     e[u][cnt[u]++] = v; n     e[v][cnt[v]++] = u; n   } n   if (n == 2 && m == 2 || m > n) { n     printf( -1 n ); n     return; n   } n   flag = 0; n   for (int i = 1; i <= n; i++) f[i] = i; n   for (int i = 1; i <= up + up; i++) n     if (solve(n, m)) break; n   if (!flag) { n     printf( -1 n ); n     return; n   } n   for (int i = 2; i <= m; i++) printf( %d %d n , f[i - 1], f[i]); n   if (!nxt) n     printf( %d %d n , f[m], f[1]); n   else n     printf( %d %d n , f[m], f[m + 1]); n } n int main() { n   MC(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int nmax = 1e7 + 5; n int cnt[2][nmax]; n int valu[100005]; n vector<int> g[100005]; n int liflevel; n int koyta[2]; n void dfs(int u, int level = 0) { n   cnt[level % 2][valu[u]]++; n   koyta[level % 2]++; n   for (int v : g[u]) dfs(v, level + 1); n   if (g[u].empty()) liflevel = level % 2; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> valu[i]; n   for (int i = 2; i <= n; i++) { n     int p; n     cin >> p; n     g[p].push_back(i); n   } n   dfs(1); n   int jor = 0; n   long long ans = 0; n   for (int i = 0; i < nmax; i++) n     if (cnt[liflevel][i] % 2) jor ^= i; n   if (jor) { n     for (int i = 0; i < nmax; i++) { n       int tmp = jor ^ i; n       if (tmp < nmax) ans += 1LL * cnt[liflevel][i] * cnt[liflevel ^ 1][tmp]; n     } n   } else { n     ans += 1LL * koyta[0] * (koyta[0] - 1) / 2 + n            1LL * koyta[1] * (koyta[1] - 1) / 2; n     for (int i = 0; i < nmax; i++) ans += 1LL * cnt[0][i] * cnt[1][i]; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16777216 ) n using namespace std; n const int MX = 1 << 23; n int a, b, c, d, i, j, n, m, k; n int lenl[MX + 1], lenr[MX + 1], cnt[MX + 1], len[MX + 1], num[MX + 1]; n pair<int, int> sm[MX + 1]; n map<int, int> onwork; n inline int create(int l, int r) { n   sm[k] = make_pair(-1, -1); n   cnt[k] = 0; n   len[k] = lenl[k] = lenr[k] = r - l; n   num[k] = l; n   return k++; n } n inline int getcnt(int l, int r, int a, int b, int v) { n   if (l >= r || a > b) return 0; n   if (l == a && b == r - 1) return cnt[v]; n   int xx = (r + l) / 2; n   if (b < xx) { n     if (sm[v].first == -1) sm[v].first = create(l, xx); n     return getcnt(l, xx, a, b, sm[v].first); n   } n   if (a >= xx) { n     if (sm[v].second == -1) sm[v].second = create(xx, r); n     return getcnt(xx, r, a, b, sm[v].second); n   } n   if (sm[v].first == -1) sm[v].first = create(l, xx); n   if (sm[v].second == -1) sm[v].second = create(xx, r); n   return getcnt(l, xx, a, xx - 1, sm[v].first) + n          getcnt(xx, r, xx, b, sm[v].second); n } n void add(int l, int r, int first, int cc, int v) { n   if (l >= r || first >= r || first < l) return; n   if (l == r - 1) { n     cnt[v] = cc; n     num[v] = l; n     lenl[v] = lenr[v] = len[v] = cc ^ 1; n     return; n   } n   int xx = (r + l) / 2; n   if (sm[v].first == -1) sm[v].first = create(l, xx); n   if (sm[v].second == -1) sm[v].second = create(xx, r); n   if (first < xx) { n     add(l, xx, first, cc, sm[v].first); n   } else { n     add(xx, r, first, cc, sm[v].second); n   } n   lenl[v] = lenr[v] = len[v] = 0; n   cnt[v] = 0; n   { n     lenl[v] = lenl[sm[v].first]; n     len[v] = len[sm[v].first]; n     num[v] = num[sm[v].first]; n     cnt[v] = cnt[sm[v].first]; n   } n   { n     if (lenl[v] == xx - l) lenl[v] += lenl[sm[v].second]; n     lenr[v] = lenr[sm[v].second]; n     if (lenr[v] == r - xx) lenr[v] += lenr[sm[v].first]; n     if (lenr[sm[v].first] + lenl[sm[v].second] >= len[v]) { n       len[v] = lenr[sm[v].first] + lenl[sm[v].second]; n       num[v] = xx - lenr[sm[v].first]; n     } n     if (len[sm[v].second] >= len[v]) { n       len[v] = len[sm[v].second]; n       num[v] = num[sm[v].second]; n     } n     cnt[v] += cnt[sm[v].second]; n   } n } n int main() { n   scanf( %d%d , &n, &m); n   sm[0] = make_pair(-1, -1); n   lenl[0] = lenr[0] = len[0] = n; n   num[0] = 1; n   k = 1; n   for (int _n((m)-1), i(0); i <= _n; i++) { n     scanf( %d , &a); n     if (!a) { n       scanf( %d%d , &a, &b); n       printf( %d n , getcnt(1, n + 1, a, b, 0)); n     } else { n       if (onwork.count(a)) { n         add(1, n + 1, onwork[a], 0, 0); n         onwork.erase(onwork.find(a)); n       } else { n         int l = num[0], r = num[0] + len[0]; n         add(1, n + 1, (r + l) / 2, 1, 0); n         onwork[a] = (r + l) / 2; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1e9 + 7; n long long Bpow(long long a, long long s) { n   if (s == 0) { n     return 1; n   } n   long long u = Bpow(a, s / 2); n   if (s & 1) n     return u * u % mod * a % mod; n   else n     return u * u % mod; n } n int32_t main() { n   long long n; n   cin >> n; n   vector<long long> a(n); n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   long long ans = 0; n   for (long long i = 0; i < n - 2; i++) { n     long long now = a[i + 1]; n     for (long long j = i + 2; j < n; j++) { n       if (now > a[i] * 2) break; n       if ((a[i] ^ a[j]) == now && a[i] > a[j]) ++ans; n       now += a[j]; n     } n   } n   for (long long i = n - 1; i > 1; i--) { n     long long now = a[i - 1]; n     for (long long j = i - 2; j >= 0; j--) { n       if (now > a[i] * 2) break; n       if ((a[i] ^ a[j]) == now && a[i] > a[j]) ++ans; n       now += a[j]; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n long long t[2003]; n long long viz[2003]; n pair<long long, long long> a[2003]; n pair<pair<long long, long long>, long long> b[2003]; n vector<long long> v[2003]; n vector<pair<pair<long long, long long>, long long> > much; n vector<pair<pair<long long, long long>, long long> > muchsel; n bool cmp_first(pair<pair<long long, long long>, long long> a, n                pair<pair<long long, long long>, long long> b) { n   if (a.first.first == b.first.first) return a.first.second < b.first.second; n   return a.first.first < b.first.first; n } n bool cmp_second(pair<pair<long long, long long>, long long> a, n                 pair<pair<long long, long long>, long long> b) { n   if (a.first.second == b.first.second) return a.first.first < b.first.first; n   return a.first.second < b.first.second; n } n bool cmp(pair<pair<long long, long long>, long long> a, n          pair<pair<long long, long long>, long long> b) { n   return a.second < b.second; n } n long long dist(pair<long long, long long> a, pair<long long, long long> b) { n   return abs(a.first - b.first) + abs(a.second - b.second); n } n long long root(long long x) { n   if (t[x] == 0) return x; n   t[x] = root(t[x]); n   return t[x]; n } n void calc(long long mx) { n   muchsel.clear(); n   long long rx, ry; n   int id = 0; n   for (auto val : much) { n     rx = root(val.first.first); n     ry = root(val.first.second); n     if (val.second > mx) return; n     if (rx != ry) { n       t[rx] = ry; n       viz[id] = 1; n       muchsel.push_back(val); n     } n     id++; n   } n   return; n } n long long intercl(pair<long long, long long> a, pair<long long, long long> b, n                   pair<long long, long long> c, pair<long long, long long> d) { n   long long x = -1, y = -1; n   if (a.first == b.first) x = a.first; n   if (a.second == b.second) y = a.second; n   if (c.first == d.first) x = c.first; n   if (c.second == d.second) y = c.second; n   if (x != -1 && y != -1) { n     long long val1 = max(abs(x - a.first), abs(x - b.first)), n               val2 = max(abs(y - a.second), abs(y - b.second)), n               val3 = max(abs(x - c.first), abs(x - d.first)), n               val4 = max(abs(y - c.second), abs(y - d.second)); n     long long val = max(val1, max(val2, max(val3, val4))); n     return val; n   } n   return 2000000000000000018; n } n void cleart(long long n) { n   for (int i = 1; i <= n; ++i) t[i] = 0; n } n bool solve(long long n, long long t) { n   cleart(n); n   calc(t); n   long long sz = muchsel.size(); n   long long nrc = n - sz; n   long long rx, ry, rk, rq; n   if (nrc == 1) return true; n   if (nrc == 2) { n     for (int i = 1; i <= n; ++i) { n       rx = root(i); n       for (int j = i + 1; j <= n; ++j) { n         ry = root(j); n         if (rx == ry) continue; n         long long ds = n             max(abs(a[i].first - a[j].first), abs(a[i].second - a[j].second)); n         if (ds <= t) return true; n         if (a[i].first == a[j].first || a[i].second == a[j].second) n           if ((ds + 1) / 2 <= t) return true; n       } n     } n     return false; n   } n   if (nrc == 3) { n     for (int i = 0; i < much.size(); ++i) { n       rx = root(much[i].first.first); n       ry = root(much[i].first.second); n       if (rx == ry) continue; n       for (int j = 1; j <= n; ++j) { n         rk = root(j); n         if (rx == rk || ry == rk) continue; n         pair<long long, long long> e1 = a[much[i].first.first]; n         pair<long long, long long> e2 = a[much[i].first.second]; n         pair<long long, long long> e3 = a[j]; n         pair<long long, long long> pct = {-2000000000000000018, n                                           -2000000000000000018}; n         if (e1.first == e2.first) pct = {e1.first, e3.second}; n         if (e1.first == e3.first) pct = {e1.first, e2.second}; n         if (e2.first == e3.first) pct = {e2.first, e1.second}; n         if (e1.second == e2.second) pct = {e3.first, e1.second}; n         if (e1.second == e3.second) pct = {e2.first, e1.second}; n         if (e2.second == e3.second) pct = {e1.first, e2.second}; n         if (max(dist(e1, pct), max(dist(e2, pct), dist(e3, pct))) <= t) n           return true; n       } n     } n     return false; n   } n   if (nrc == 4) { n     for (int i = 0; i < much.size(); ++i) { n       rx = root(much[i].first.first); n       ry = root(much[i].first.second); n       if (rx == ry) continue; n       for (int j = i + 1; j < much.size(); ++j) { n         rk = root(much[j].first.first); n         rq = root(much[j].first.second); n         if (rk == rq || rx == rk || rx == rq || ry == rk || ry == rq) continue; n         if (intercl(a[much[i].first.first], a[much[i].first.second], n                     a[much[j].first.first], a[much[j].first.second]) <= t) n           return true; n       } n     } n     return false; n   } n   return false; n } n int main() { n   long long n; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i].first >> a[i].second; n     b[i] = {a[i], i}; n   } n   sort(b + 1, b + n + 1, cmp_first); n   for (int i = 1; i < n; ++i) { n     if (b[i].first.first == b[i + 1].first.first) { n       long long dif = b[i + 1].first.second - b[i].first.second, n                 id1 = b[i].second, id2 = b[i + 1].second; n       if (dif < 0) dif = -dif; n       v[id1].push_back(id2); n       v[id2].push_back(id1); n       much.push_back({{id1, id2}, dif}); n     } n   } n   sort(b + 1, b + n + 1, cmp_second); n   for (int i = 1; i < n; ++i) { n     if (b[i].first.second == b[i + 1].first.second) { n       long long dif = b[i + 1].first.first - b[i].first.first, n                 id1 = b[i].second, id2 = b[i + 1].second; n       if (dif < 0) dif = -dif; n       v[id1].push_back(id2); n       v[id2].push_back(id1); n       much.push_back({{id1, id2}, dif}); n     } n   } n   sort(much.begin(), much.end(), cmp); n   long long st = 1, dr = 2000000009; n   while (st <= dr) { n     long long mij = (st + dr) / 2; n     if (solve(n, mij)) n       dr = mij - 1; n     else n       st = mij + 1; n   } n   if (solve(n, dr)) n     cout << dr <<   n ; n   else { n     if (st == 2000000009 + 1) n       cout << -1 <<   n ; n     else n       cout << st <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16777216 ) n using namespace std; n const double pi = acos(-1.0); n const int inf = (int)1e9; n const double eps = 1e-4; n const int ss = (int)1e6 + 3; n const int base = inf; n bool pred(const pair<int, int>& i, const pair<int, int>& j) { n   if (i.first == j.first) { n     return i.second > j.second; n   } else { n     return i.first > j.first; n   } n } n int a[2000005]; n int main() { n   int k; n   scanf( %d , &k); n   long long int res = 0; n   for (int i = 0; i < k; ++i) { n     scanf( %d , &a[i]); n     res += a[i]; n   } n   if (k == 1) { n     cout << res; n     return 0; n   } n   sort(a, a + k); n   int z = k / 4; n   while (z != 0) { n     for (int i = k - 1; i >= k - z; --i) { n       res += a[i]; n     } n     z /= 4; n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n const long long INFLL = 1e18 + 1; n const int MAX = 200001; n const long long MOD = 1000000007; n long long inq(long long k, long long q) { n   if (q == 0) return 1; n   long long l = inq(k, q / 2); n   if (q % 2 == 0) n     return l * l % MOD; n   else n     return (l * l) % MOD * k % MOD; n } n long long gcd(long long a, long long b) { n   if (a < b) swap(a, b); n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long cubr(long long a) { n   long long l = -1, r = 1e6 + 2; n   while (l < r - 1) { n     long long mid = (l + r) / 2; n     if (mid * mid * mid > a) n       r = mid; n     else n       l = mid; n   } n   return l; n } n long long max(long long a, long long b) { n   if (a > b) return a; n   return b; n } n long long min(long long a, long long b) { return -1 * max(-a, -b); } n long long possible(long long q) { n   if (q == INF) return -1; n   return q; n } n bool correct(int x, int xx) { n   if (x < 0) return 0; n   if (x >= xx) return 0; n   return 1; n } n long long dsumm(long long x, long long k) { n   long long y = 1; n   long long z = 0; n   for (int i = 0; y < 1e18; i++) { n     z += x / y % k; n     y *= k; n   } n   return z; n } n long long dcount(long long x) { n   long long y = 1; n   long long z = 0; n   int c[100]; n   for (int i = 0; i < 10; i++) c[i] = 0; n   for (int i = 0; x > 0; i++) { n     if (c[x / y % 10] == 0) z++; n     c[x / y % 10] = 1; n     x /= 10; n   } n   return z; n } n long long lg10(long long x) { n   if (x == 0) return 0; n   return lg10(x / 10) + 1; n } n long long n, m; n vector<int> graph[4000], graph2[4000]; n long long ans; n long long d[5000]; n long long f[4000]; n long long ff[4000]; n int main() { n   cin >> n >> m; n   for (int(i) = 0; (i) != (m); i++) { n     int x, y; n     cin >> x >> y; n     x--; n     y--; n     graph[x].push_back(y); n     graph2[y].push_back(x); n   } n   long long it = 2; n   for (int b = 0; b < n; b++) { n     for (int c = b + 1; c < n; c++) { n       long long x = 0; n       long long y = 0; n       long long z = 0; n       for (int i = 0; i < graph[b].size(); i++) { n         d[graph[b][i]] = it; n       } n       for (int i = 0; i < graph[c].size(); i++) { n         if (d[graph[c][i]] == it) { n           x++; n         } n       } n       it++; n       for (int i = 0; i < graph2[b].size(); i++) { n         d[graph2[b][i]] = it; n       } n       for (int i = 0; i < graph2[c].size(); i++) { n         if (d[graph2[c][i]] == it) { n           y++; n         } n       } n       it++; n       for (int i = 0; i < graph[b].size(); i++) { n         ff[graph[b][i]] = it * 7; n       } n       for (int i = 0; i < graph[c].size(); i++) { n         if (ff[graph[c][i]] == it * 7) n           ff[graph[c][i]]++; n         else n           ff[graph[c][i]] = 0; n       } n       for (int i = 0; i < graph2[b].size(); i++) { n         if (ff[graph2[b][i]] == it * 7 + 1) n           ff[graph2[b][i]]++; n         else n           ff[graph2[b][i]] = 0; n       } n       for (int i = 0; i < graph2[c].size(); i++) { n         if (ff[graph2[c][i]] == it * 7 + 2) { n           z++; n           ff[graph2[c][i]] = 0; n         } n       } n       ans += (x - z) * (y - z) + z * (z - 1) + z * (x - z) + z * (y - z); n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 100; n const long long inf = 1e18; n int bcg[maxn]; n struct Edge { n   int u, v, nxt; n   long long w; n } edge[maxn * 2], edge1[maxn * 2]; n vector<Edge> vec[maxn]; n struct Node { n   long long p; n   int v; n   friend bool operator<(Node a, Node b) { return a.p > b.p; } n   Node() {} n   Node(long long x, int y) { p = x, v = y; } n }; n int head[maxn], head1[maxn]; n long long deep[maxn]; n int depth[maxn]; n int bian[maxn * 2]; n long long dis[50][maxn]; n int lca[maxn][25]; n int vis[maxn]; n int cnt, cnt1, tot; n int n, m; n int findd(int x) { return bcg[x] == x ? bcg[x] : bcg[x] = findd(bcg[x]); } n bool cmp(Edge a, Edge b) { return a.w < b.w; } n void add_e1(int x, int y, long long w) { n   ++cnt1; n   edge1[cnt1].u = x; n   edge1[cnt1].v = y; n   edge1[cnt1].w = w; n   edge1[cnt1].nxt = head1[x]; n   head1[x] = cnt1; n } n void add_e(int x, int y, long long w) { n   ++cnt; n   edge[cnt].u = x; n   edge[cnt].v = y; n   edge[cnt].w = w; n   edge[cnt].nxt = head[x]; n   head[x] = cnt; n } n int merges(int x, int y) { n   int xx = findd(x); n   int yy = findd(y); n   if (xx != yy) { n     bcg[xx] = yy; n     return 1; n   } n   return 0; n } n void init() { n   cnt = cnt1 = 0; n   tot = 0; n   memset(deep, 0, sizeof(deep)); n   memset(head, 0, sizeof(head)); n   memset(head1, 0, sizeof(head1)); n } n void dfs(int x, int ff) { n   lca[x][0] = ff; n   for (int i = 1; i <= 20; i++) lca[x][i] = lca[lca[x][i - 1]][i - 1]; n   for (int i = head[x]; i; i = edge[i].nxt) { n     int v = edge[i].v; n     if (v == ff) continue; n     deep[v] = deep[x] + edge[i].w; n     depth[v] = depth[x] + 1; n     dfs(v, x); n   } n } n int LCA(int x, int y) { n   if (depth[x] < depth[y]) swap(x, y); n   int ff = depth[x] - depth[y]; n   for (int i = 20; i >= 0; i--) { n     if (ff & (1 << i)) x = lca[x][i]; n   } n   if (x == y) return y; n   for (int i = 20; i >= 0; i--) { n     if (lca[x][i] != lca[y][i]) x = lca[x][i], y = lca[y][i]; n   } n   return lca[y][0]; n } n priority_queue<Node> que; n void dij(int id, int x) { n   for (int i = 0; i <= n + 4; i++) { n     dis[id][i] = inf; n     vis[i] = 0; n   } n   dis[id][x] = 0; n   while (!que.empty()) que.pop(); n   que.push(Node(0, x)); n   while (!que.empty()) { n     Node vv = que.top(); n     que.pop(); n     if (vis[vv.v] || vv.p > dis[id][vv.v]) continue; n     vis[vv.v] = 1; n     for (int i = 0; i < (int)vec[vv.v].size(); i++) { n       Edge k = vec[vv.v][i]; n       int sw = k.v; n       if (vis[sw]) continue; n       if (dis[id][sw] > dis[id][vv.v] + k.w) { n         dis[id][sw] = dis[id][vv.v] + k.w; n         que.push(Node(dis[id][sw], sw)); n       } n     } n   } n } n int main() { n   scanf( %d%d , &n, &m); n   int u, v; n   long long w; n   init(); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d%lld , &u, &v, &w); n     add_e1(u, v, w); n     vec[u].push_back((Edge){u, v, 0, w}); n     vec[v].push_back((Edge){v, u, 0, w}); n   } n   sort(edge1 + 1, edge1 + cnt1 + 1, cmp); n   for (int i = 1; i <= n + 4; i++) bcg[i] = i; n   for (int i = 1; i <= cnt1; i++) { n     int x = edge1[i].u, y = edge1[i].v; n     if (merges(x, y)) { n       add_e(x, y, edge1[i].w); n       add_e(y, x, edge1[i].w); n     } else { n       bian[++tot] = x; n       bian[++tot] = y; n     } n   } n   depth[1] = 0; n   deep[1] = 0; n   dfs(1, 0); n   sort(bian + 1, bian + 1 + tot); n   int sz = unique(bian + 1, bian + 1 + tot) - (bian + 1); n   for (int i = 1; i <= sz; i++) { n     dij(i, bian[i]); n   } n   int q; n   scanf( %d , &q); n   int x, y; n   while (q--) { n     scanf( %d%d , &x, &y); n     long long ans = inf; n     int pq = LCA(x, y); n     ans = deep[x] + deep[y] - 2 * deep[pq]; n     for (int i = 1; i <= sz; i++) { n       ans = min(ans, dis[i][x] + dis[i][y]); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const unsigned int INF = 2000000001; n const long long INF0 = 2000000001000000001; n const bool DEBUG_OUTPUT = false; n const int modulo = 1000000007; n const int go[8][2] = {{0, 1}, {0, -1},  {1, 0},  {-1, 0}, n                       {1, 1}, {-1, -1}, {1, -1}, {-1, 1}}; n int main() { n   long long n, k; n   int p; n   cin >> n >> k >> p; n   long long a = (n >> 1) + (n & 1); n   long long b = 0; n   if (k >= a) { n     b = k - a; n   } else n     a = k; n   if ((n - 2 * a + 1) >= 0 && ((n - 2 * a + 1) & 1) == 0 && k > 0) { n     b = a - 1; n     a = k - b; n   } n   for (int i = 0; i < p; i++) { n     long long q; n     cin >> q; n     q = n - q; n     if (q & 1) { n       cout << ((q >> 1) < b ?  X  :  . ); n     } else n       cout << ((q >> 1) < a ?  X  :  . ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 11; n int n; n int a[MAXN][MAXN]; n void read() { n   cin >> n; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) cin >> a[i][j]; n   } n } n const int INF = 1e9 + 7; n const int MAXANS = MAXN * MAXN * 2; n int dp[MAXANS][MAXN * MAXN][MAXN][MAXN][3]; n bool ch(int i, int j) { return 0 <= i && i < n && 0 <= j && j < n; } n inline bool check(int i, int j, int ni, int nj, int k) { n   if (k == 0) { n     return (abs(i - ni) == 1 && abs(j - nj) == 2) || n            (abs(i - ni) == 2 && abs(j - nj) == 1); n   } else if (k == 1) { n     return abs(i - ni) == abs(j - nj); n   } else { n     return (i == ni || j == nj); n   } n } n pair<int, int> ans = {INF, INF}; n pair<int, int> pos[MAXN * MAXN]; n vector<pair<int, int> > go[MAXN][MAXN][3]; n void solve() { n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       pos[a[i][j]] = {i, j}; n     } n   } n   for (int i = 0; i < MAXANS; ++i) { n     for (int j = 0; j < MAXN * MAXN; ++j) { n       for (int x = 0; x < MAXN; ++x) { n         for (int y = 0; y < MAXN; ++y) { n           for (int k = 0; k < 3; ++k) { n             dp[i][j][x][y][k] = INF; n           } n         } n       } n     } n   } n   for (int i = 0; i < 3; ++i) { n     dp[0][1][pos[1].first][pos[1].second][i] = 0; n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       for (int ni = 0; ni < n; ++ni) { n         for (int nj = 0; nj < n; ++nj) { n           for (int k = 0; k < 3; ++k) { n             if (check(i, j, ni, nj, k)) { n               go[i][j][k].push_back({ni, nj}); n             } n           } n         } n       } n     } n   } n   for (int op = 0; op < MAXANS - 1; ++op) { n     for (int num = 1; num <= n * n; ++num) { n       for (int i = 0; i < n; ++i) { n         for (int j = 0; j < n; ++j) { n           for (int k = 0; k < 3; ++k) { n             if (num == n * n && dp[op][num][i][j][k] != INF) { n               ans = min(ans, {op, dp[op][num][i][j][k]}); n             } n             for (auto t : go[i][j][k]) { n               int ni = t.first, nj = t.second; n               if (check(i, j, ni, nj, k)) { n                 bool ch = n                     (pos[num + 1].first == ni && pos[num + 1].second == nj); n                 dp[op + 1][num + ch][ni][nj][k] = n                     min(dp[op + 1][num + ch][ni][nj][k], dp[op][num][i][j][k]); n               } n             } n             for (int nk = 0; nk < 3; ++nk) { n               if (nk != k) { n                 dp[op + 1][num][i][j][nk] = n                     min(dp[op + 1][num][i][j][nk], dp[op][num][i][j][k] + 1); n               } n             } n           } n         } n       } n     } n   } n } n void print() { cout << ans.first <<     << ans.second <<   n ; } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   read(); n   solve(); n   print(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int Read() { n   int x = 0, f = 1, c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -1; n   for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ; n   return x * f; n } n const int MAXN = 1000005; n const int mod = 1e9 + 7; n int n, m, k, ans, minp, maxp, a[MAXN], bin[MAXN]; n int main() { n   n = Read(), m = Read(), k = Read(), minp = n + 1, bin[0] = 1; n   for (int i = 1, x, y; i <= m; i++) { n     x = Read(), y = Read(); n     if (y == x + k + 1) n       a[x]++, minp = min(minp, x), maxp = max(maxp, x); n     else if (y ^ x + 1) n       return puts( 0 ), 0; n   } n   if (maxp - minp > k) return puts( 0 ), 0; n   n -= k + 1; n   for (int i = 1; i <= n; i++) n     a[i] += a[i - 1], bin[i] = (bin[i - 1] << 1) % mod; n   if (!maxp) { n     ans = 1; n     for (int i = 1; i <= n; i++) ans = (ans + bin[min(i + k, n) - i]) % mod; n   } else n     for (int i = max(maxp - k, 1); i <= minp; i++) n       ans = (ans + bin[min(i + k, n) - i - (a[min(i + k, n)] - a[i])]) % mod; n   return printf( %d n , ans), 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   { n     long long i, j, n, a, b, c, d, mx, mn; n     cin >> n >> a >> b >> c >> d; n     mx = max(a + b, a + c); n     mx = max(mx, b + d); n     mx = max(mx, c + d); n     mn = min(a + b, a + c); n     mn = min(mn, b + d); n     mn = min(mn, c + d); n     if (mx + 1 <= mn + n) { n       i = min(a + b + n, mn + n); n       j = max(mx + 1, a + b + 1); n       cout << n * (i - j + 1); n     } else n       cout <<  0 ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, ans, x, y, sum, res, day1, day2, moun1, moun2, year1, year2; n bool l; n char a[100]; n string s, s1, s2; n vector<int> v; n int main() { n   cin >> n; n   if (!(n & 1)) { n     cout << -1; n     return 0; n   } n   for (int i = 0; i < n; i++) printf( %d  , i); n   cout << endl; n   for (int i = 0; i < n; i++) printf( %d  , i); n   cout << endl; n   for (int i = 0; i < n; i++) printf( %d  , (i + i) % n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, long long> m; n void solve() { n   long long n; n   cin >> n; n   map<long long, long long>::iterator itr; n   long long x, maxm = INT_MIN; n   for (long long i = 0; i < n; i++) { n     cin >> x; n     m[x - (i + 1)] += x; n   } n   for (auto &it : m) { n     maxm = max(maxm, it.second); n   } n   cout << maxm; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool isVowel(char c) { n   if (c ==  a  or c ==  e  or c ==  i  or c ==  o  or c ==  u  or c ==  y ) n     return true; n   return false; n } n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   long long n; n   cin >> n; n   string s; n   cin >> s; n   string ans =   ; n   for (long long i = 0; i < n; i++) { n     ans += s[i]; n     if (!isVowel(s[i])) continue; n     long long j = i; n     while (j < n && isVowel(s[j])) j++; n     i = j - 1; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[11]; n string str[11]; n int m, n; n int f(int k) { n   if (k == n) { n     for (int i = 0; i < m; i++) n       if (arr[i] != 0) return 0; n     return 1; n   } n   int ans = 0; n   bool c = 0; n   for (int i = 0; i < m; i++) n     if (str[i][k] ==  0 ) { n       arr[i]--; n       if (arr[i] < 0) c++; n     } n   if (c == 0) ans += f(k + 1); n   for (int i = 0; i < m; i++) n     if (str[i][k] ==  0 ) arr[i]++; n   c = 0; n   for (int i = 0; i < m; i++) n     if (str[i][k] ==  1 ) { n       arr[i]--; n       if (arr[i] < 0) c++; n     } n   if (!c) ans += f(k + 1); n   for (int i = 0; i < m; i++) n     if (str[i][k] ==  1 ) arr[i]++; n   return ans; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < m; i++) cin >> str[i] >> arr[i]; n   cout << f(0) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, m, k; n     cin >> n >> m >> k; n     vector<int> val(n); n     for (int i = 0; i < n; i++) { n       cin >> val[i]; n     } n     if (m == 1) { n       cout << max(val[0], val[n - 1]) << endl; n       continue; n     } n     if (k >= m - 1) { n       vector<int> vec; n       for (int i = 0; i < m; i++) { n         vec.push_back(val[i]); n         vec.push_back(val[n - i - 1]); n       } n       cout << *max_element(vec.begin(), vec.end()) << endl; n       continue; n     } n     int res_glob = 0; n     for (int i = 0; i <= k; i++) { n       int ind_l = i; n       int ind_r = n - 1 - (k - i); n       int need = m - k - 1; n       int cur_res = 2000000000; n       for (int j = 0; j <= need; j++) { n         int cur_elem = ind_l + j; n         int cur_elem_left = ind_r - (need - j); n         cur_res = min(cur_res, max(val[cur_elem], val[cur_elem_left])); n       } n       res_glob = max(res_glob, cur_res); n     } n     cout << res_glob << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[500005]; n int main() { n   int n, b, i, ma = 0, ju, cnt, le, st, j; n   scanf( %d , &n); n   int hahaha = 1; n   for (i = 0; i < n; i++) { n     scanf( %d , &a[i]); n   } n   le = 1; n   ju = a[0]; n   for (i = 1; i < n; i++) { n     if (a[i] != ju) n       le++; n     else { n       ma = max(ma, le); n       st = i - le + 1; n       cnt = 0; n       if (le >= 3) { n         if (le % 2 == 0) { n           le = (le - 2) / 2; n           for (j = st; j < i - 1; j++) { n             if (cnt < le) { n               a[j] = a[st - 1]; n             } else { n               a[j] = a[i]; n             } n             cnt++; n           } n         } else { n           for (j = st; j < i - 1; j++) { n             a[j] = a[st - 1]; n           } n         } n       } n       le = 1; n     } n     ju = a[i]; n   } n   ma = max(ma, le); n   st = i - le + 1; n   cnt = 0; n   if (le >= 3) { n     if (le % 2 == 0) { n       le = (le - 2) / 2; n       for (j = st; j < i - 1; j++) { n         if (cnt < le) { n           a[j] = a[st - 1]; n         } else { n           a[j] = a[i - 1]; n         } n         cnt++; n       } n     } else { n       for (j = st; j < i - 1; j++) { n         a[j] = a[st - 1]; n       } n     } n   } n   le = (ma - 1) / 2; n   printf( %d n , le); n   for (i = 0; i < n; i++) { n     printf( %d  , a[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int mod = 1e9 + 7; n const int maxn = 1e6 + 10; n int n, k, cnt[maxn], a[maxn], L[maxn], R[maxn]; n stack<pair<int, int> > stk; n int main() { n   scanf( %d , &n); n   ; n   scanf( %d , &k); n   ; n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   ; n   stk.push({INF, 0}); n   for (int i = 1; i <= n; i++) { n     while (stk.top().first < a[i]) stk.pop(); n     L[i] = stk.top().second + 1; n     stk.push({a[i], i}); n   } n   while (stk.size()) stk.pop(); n   stk.push({INF, n + 1}); n   for (int i = n; i >= 1; i--) { n     while (stk.top().first <= a[i]) stk.pop(); n     R[i] = stk.top().second - 1; n     stk.push({a[i], i}); n   } n   for (int i = 1; i <= n; i++) { n     cnt[i] = (cnt[i - 1] + (i - 1) / (k - 1)) % mod; n   } n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     int t = (cnt[R[i] - L[i] + 1] - cnt[i - L[i]] - cnt[R[i] - i]) % mod; n     ans += 1LL * t * a[i] % mod; n   } n   printf( %I64d n , (ans % mod + mod) % mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int p[50001], n, l, r; n queue<int> ver[50001]; n void go(int pre, int v) { n   int t; n   p[v] = pre; n   if (v != l) { n     while (!ver[v].empty()) { n       t = ver[v].front(); n       ver[v].pop(); n       if (t != pre) go(v, t); n     } n   } n } n int main() { n   scanf( %d%d%d , &n, &l, &r); n   l--; n   r--; n   for (int i = 0; i < n; i++) n     if (i == l) n       continue; n     else { n       scanf( %d , &p[i]); n       p[i]--; n       ver[p[i]].push(i); n       ver[i].push(p[i]); n     } n   go(-1, r); n   for (int i = 0; i < n; i++) n     if (i == r) n       continue; n     else n       printf( %d  , p[i] + 1); n   return 0; n } n


#include <bits/stdc++.h> n long long gcd(long long x, long long y) { n   if (x > y) std::swap(x, y); n   return y % x == 0 ? x : gcd(y % x, x); n } n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   int a[20]; n   bool flag = true; n   for (int i = (1); i <= (n); i++) scanf( %d , a + i); n   for (int i = (1); i <= (n - 1); i++) n     if (abs(a[i] - a[i + 1]) >= 2) flag = false; n   if (flag) n     puts( YES ); n   else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 50; n int n, cnt, f[N][18], d[N], fa[N], id[N], s[N]; n vector<int> v1[N], v2[N]; n void dfs1(int x, int ff) { n   for (int i = 0; i < 17; i++) f[x][i + 1] = f[f[x][i]][i]; n   for (int i = 0, y; i < v1[x].size(); i++) n     if ((y = v1[x][i]) != ff) n       f[y = v1[x][i]][0] = x, d[y] = d[x] + 1, dfs1(y, x); n } n int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } n void merge(int x, int y) { n   x = find(x); n   y = find(y); n   if (s[x] > s[y]) swap(x, y); n   fa[x] = y; n   s[y] += s[x]; n   id[y] = d[id[y]] < d[id[x]] ? id[y] : id[x]; n } n void dfs2(int x, int ff) { n   for (int i = 0, y; i < v2[x].size(); i++) n     if ((y = v2[x][i]) != ff) dfs2(y, x); n   if (!ff) return; n   int fx = id[find(x)], p = ff; n   for (int i = 17; ~i; i--) n     if (d[id[find(f[p][i])]] > d[fx]) p = f[p][i]; n   if (id[find(f[p][0])] != fx) p = fx; n   merge(p, f[p][0]); n   printf( %d %d %d %d n , p, f[p][0], x, ff); n } n int main() { n   scanf( %d , &n); n   for (int i = 1, x, y; i < n; i++) n     scanf( %d%d , &x, &y), v1[x].push_back(y), v1[y].push_back(x); n   for (int i = 1, x, y; i < n; i++) n     scanf( %d%d , &x, &y), v2[x].push_back(y), v2[y].push_back(x); n   printf( %d n , n - 1); n   for (int i = 1; i <= n; i++) id[i] = fa[i] = i, s[i] = 1; n   dfs1(d[1] = 1, 0); n   dfs2(1, 0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a, b; n   int n, m; n   cin >> n >> m; n   cin >> a >> b; n   int pw = 1; n   int ans = 0, res = 0; n   for (int i = 0; i < m; i++) { n     if (i < n && a[n - i - 1] ==  1 ) res = (res + pw) % 998244353; n     if (b[m - i - 1] ==  1 ) ans = (ans + res) % 998244353; n     pw = (pw + pw) % 998244353; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b, ans, w1, w2; n int main() { n   cin >> a >> b; n   ans = 2 * a + 2 * b + 2; n   w1 = 2 * a + 2 * b + 2; n   w2 = 2 * a + 2 * b + 2; n   for (long long i = 1; i <= sqrt(a + b); i++) { n     if (a % i == 0 && a / i < w1) w1 = a / i; n     if (b % i == 0 && b / i < w2) w2 = b / i; n     if ((a + b) % i == 0) { n       if (w1 <= (a + b) / i) { n         if (2 * (i + (a + b) / i) < ans) ans = 2 * (i + (a + b) / i); n       } n       if (w2 <= (a + b) / i) { n         if (2 * (i + (a + b) / i) < ans) ans = 2 * (i + (a + b) / i); n       } n     } n   } n   cout << ans << endl; n   return 0; n } n