#include <bits/stdc++.h> n using namespace std; n const long long N = 1e6 + 5; n const long long INF = 1e9 + 7; n long long n, ans = INF; n string s; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> s; n   long long n = s.size(); n   for (long long mask = 1; mask < 1 << n; mask++) { n     long long t = 0; n     bool check = true; n     for (long long i = 0; i < n; i++) n       if (mask >> i & 1) { n         t = t * 10 + s[i] -  0 ; n         if (t == 0 && s[i] ==  0 ) check = false; n       } n     long long tt = sqrt(t); n     if (tt * tt == t && tt != 0 && check) n       ans = min(ans, n - __builtin_popcount(mask)); n   } n   cout << (ans == INF ? -1 : ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 2000000000000000001, mod = 1000000007; n long long toi(string s) { n   long long a; n   istringstream in(s); n   in >> a; n   return a; n } n int main() { n   string s; n   cin >> s; n   for (int i = 1; i < s.length(); i++) n     if (s[i] == s[i - 1]) { n       cout <<  Impossible ; n       return 0; n     } n   int a, b; n   for (int i = 0; i < s.length(); i++) n     for (int j = i + 1; j < s.length(); j++) n       if (s[i] == s[j]) { n         a = i; n         b = j; n         break; n       } n   char arr[2][13]; n   int A = b - a - 1, B = 26 - A - 1; n   arr[0][A / 2] = s[a]; n   int x = 0, y = A / 2; n   int cnt = A; n   int p = a + 1; n   while (cnt) { n     if (x == 0 && y > 0) n       y--; n     else if (x == 0 && y == 0) n       x++; n     else n       y++; n     arr[x][y] = s[p]; n     p++; n     cnt--; n   } n   cnt = B; n   x = 0; n   y = A / 2; n   p = (b + 1) % 27; n   while (cnt) { n     if (x == 0 && y < 12) n       y++; n     else if (x == 0 && y == 12) n       x++; n     else n       y--; n     arr[x][y] = s[p]; n     p++; n     p %= 27; n     cnt--; n   } n   for (int i = 0; i < 13; i++) cout << arr[0][i]; n   cout <<   n ; n   for (int i = 0; i < 13; i++) cout << arr[1][i]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t = 1; n   while (t--) { n     long long int n, i, arr[200005]; n     string s; n     cin >> n; n     cin >> s; n     long long int ans = 0; n     for (i = 0; i < n; i++) { n       int p = s[i] - 48; n       if (p % 2 == 0) { n         ans += i + 1; n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long k; n vector<vector<long long>> operator*(vector<vector<long long>> a, n                                     vector<vector<long long>> b) { n   vector<vector<long long>> ans(n, vector<long long>(n)); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       ans[i][j] = 0; n       for (int k = 0; k < n; k++) n         ans[i][j] = (ans[i][j] + a[i][k] * b[k][j]) % 1000000007; n     } n   } n   return ans; n } n vector<vector<long long>> fastxp(vector<vector<long long>> a, long long e) { n   if (e == 1) return a; n   vector<vector<long long>> ans = fastxp(a, e / 2); n   ans = (ans * ans); n   if (e % 2 == 1) ans = ans * a; n   return ans; n } n int sumbits(long long a) { n   int sum = 0; n   while (a > 0) { n     sum += a % 2; n     a /= 2; n   } n   return sum; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> k; n   vector<vector<long long>> aux(n, vector<long long>(n)); n   vector<vector<long long>> v(n, vector<long long>(n)); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       aux[i][j] = 0; n       v[i][j] = 0; n     } n   } n   for (int i = 0; i < n; i++) v[i][0] = 1; n   long long a[110]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (sumbits((a[i] ^ a[j])) % 3 == 0) aux[i][j] = 1; n     } n   } n   if (k == 1) { n     cout << n << endl; n     return 0; n   } else { n     vector<vector<long long>> ans = fastxp(aux, k - 1); n     v = ans * v; n     long long sum = 0; n     for (int i = 0; i < n; i++) { n       sum = (sum + v[i][0]) % 1000000007; n     } n     cout << sum << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, n, i, x, y, v; n double min1, ans, k; n int main() { n   cin >> a >> b; n   cin >> n; n   min1 = 999999; n   for (i = 1; i <= n; i++) { n     cin >> x >> y >> v; n     k = (x - a) * (x - a) + (y - b) * (y - b); n     ans = sqrt(k) / v; n     min1 = min(min1, ans); n   } n   printf( %.12f n , min1); n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= mod; n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n   } n   return res; n } n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n int main() { n   ios_base::sync_with_stdio(false); n   int _; n   int m, d, w; n   for (cin >> _; _ && _--;) { n     cin >> m >> d >> w; n     int d_dcg = gcd(d - 1, w); n     int d1 = (d - 1) / d_dcg; n     int w1 = w / d_dcg; n     int m_w1 = (min(m, d) - 1) / w1; n     cout << 1LL * m_w1 * min(m, d) - 1LL * w1 * (1 + m_w1) * m_w1 / 2 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, n, k, j; n   cin >> n >> k; n   for (i = 0; i < n; i++) { n     for (j = 0; j < n; j++) n       if (i == j) n         cout << k <<    ; n       else n         cout << 0 <<    ; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); n   int t; n   cin >> t; n   while (t--) { n     int c, d; n     cin >> c >> d; n     int temp = abs(c - d); n     if (temp % 2) { n       cout <<  -1  << endl; n       continue; n     } n     if (c == 0 and d == 0) { n       cout <<  0  << endl; n       continue; n     } n     if (temp == 0) { n       cout <<  1  << endl; n       continue; n     } n     cout <<  2  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 305; n long long n, k; n long long a[N][N], rows[N], cols[N]; n long long sqr(long long x) { return x * x; } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long t; n   cin >> t; n   while (t--) { n     cin >> n >> k; n     for (long long i = 0; i < n; i++) { n       rows[i] = cols[i] = 0; n       for (long long j = 0; j < n; j++) a[i][j] = 0; n     } n     long long sti = 0, i = 0, j = 0; n     while (k > 0) { n       if (a[i][j]) { n         sti++; n         i = sti, j = 0; n       } else { n         a[i][j] = 1; n         rows[i]++; n         cols[j]++; n         i++, j++; n         i %= n; n         j %= n; n         k--; n       } n     } n     long long ans = 0; n     sort(rows, rows + n); n     sort(cols, cols + n); n     ans += sqr(rows[n - 1] - rows[0]) + sqr(cols[n - 1] - cols[0]); n     cout << ans <<   n ; n     for (long long i = 0; i < n; i++) { n       for (long long j = 0; j < n; j++) cout << a[i][j]; n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 100000 + 10; n struct Node { n   int l, r, q; n }; n struct Tree { n   int l, r; n   int ans; n   int lazy; n }; n Tree tree[M * 3]; n Node d[M]; n int mex[M]; n int n, m; n void get_same(int s) { n   if (tree[s].l == tree[s].r) return; n   if (tree[s].lazy) { n     tree[s << 1].ans = tree[s << 1].ans | tree[s].ans; n     tree[s << 1 | 1].ans = tree[s << 1 | 1].ans | tree[s].ans; n     tree[s << 1].lazy = tree[s << 1 | 1].lazy = 1; n     tree[s].lazy = tree[s].ans = 0; n   } n } n void bulit(int s, int l, int r) { n   tree[s].l = l; n   tree[s].r = r; n   tree[s].ans = 0; n   tree[s].lazy = 0; n   if (l != r) { n     int mid = (l + r) >> 1; n     bulit(s << 1, l, mid); n     bulit(s << 1 | 1, mid + 1, r); n   } n } n void updata(int s, int l, int r, int z) { n   if (tree[s].l == l && tree[s].r == r) { n     tree[s].ans = tree[s].ans | z; n     tree[s].lazy = 1; n     return; n   } n   get_same(s); n   int mid = (tree[s].l + tree[s].r) >> 1; n   if (r <= mid) n     updata(s << 1, l, r, z); n   else if (l > mid) n     updata(s << 1 | 1, l, r, z); n   else { n     updata(s << 1, l, mid, z); n     updata(s << 1 | 1, mid + 1, r, z); n   } n } n void inse(int s) { n   get_same(s); n   if (tree[s].l == tree[s].r) { n     mex[tree[s].l] = tree[s].ans; n     return; n   } n   inse(s << 1); n   inse(s << 1 | 1); n   tree[s].ans = tree[s << 1].ans & tree[s << 1 | 1].ans; n } n int query(int s, int l, int r) { n   if (tree[s].l == l && tree[s].r == r) { n     return tree[s].ans; n   } n   int mid = (tree[s].l + tree[s].r) >> 1; n   if (r <= mid) n     return query(s << 1, l, r); n   else if (l > mid) n     return query(s << 1 | 1, l, r); n   else n     return query(s << 1, l, mid) & query(s << 1 | 1, mid + 1, r); n } n bool solve() { n   for (int i = 0; i < m; i++) { n     if (query(1, d[i].l, d[i].r) != d[i].q) return false; n   } n   return true; n } n int main() { n   while (scanf( %d%d , &n, &m) == 2) { n     bulit(1, 1, n); n     for (int i = 0; i < m; i++) { n       scanf( %d%d%d , &d[i].l, &d[i].r, &d[i].q); n       updata(1, d[i].l, d[i].r, d[i].q); n     } n     inse(1); n     if (solve()) { n       puts( YES ); n       for (int i = 1; i <= n; i++) { n         if (i != 1) printf(   ); n         printf( %d , mex[i]); n       } n       printf(  n ); n     } else { n       puts( NO ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   char arr[105]; n   cin >> t; n   gets(arr); n   for (int i = 0; i < t; i++) { n     int flg1 = 0; n     int flg2 = 0; n     gets(arr); n     int len = strlen(arr); n     if (arr[0] ==  m  && arr[1] ==  i  && arr[2] ==  a  && arr[3] ==  o  && n         arr[4] ==  . ) { n       flg1 = 1; n     } n     if (arr[len - 1] ==  .  && arr[len - 2] ==  a  && arr[len - 3] ==  l  && n         arr[len - 4] ==  a  && arr[len - 5] ==  l ) { n       flg2 = 1; n     } n     if ((flg1 == 1 && flg2 == 1) || (flg1 == 0 && flg2 == 0)) { n       printf( OMG>.< I don t know! n ); n     } else if (flg1 == 1) { n       printf( Rainbow s n ); n     } else { n       printf( Freda s n ); n     } n   } n   return 0; n } n


#include<cstdio> n #include<cstring> n #include<iostream> n #include<algorithm> n #include<cmath> n #include<vector> n #include<queue> n #include<stack> n #include<ctime> n #include<map> n #define ll long long n #define MAXN 20105 n #define uns unsigned n #define INF 0x3f3f3f3f n #define MOD 1000000007ll n #define lowbit(x) ((x)&(-(x))) n using namespace std; n inline ll read(){ n  ll x=0;bool f=1;char s=getchar(); n  while((s< 0 ||s> 9 )&&s>0){if(s== - )f^=1;s=getchar();} n  while(s>= 0 &&s<= 9 )x=(x<<1)+(x<<3)+s- 0 ,s=getchar(); n  return f?x:-x; n } n int n,q,a[MAXN],st[MAXN][20],dp[MAXN][20][35],lg[MAXN]; n int A[35],B[35],tmp[35]; n inline int maxx(int x,int y){return x+a[x]>y+a[y]?x:y;} n inline int rmq(int l,int r){ n  if(l>r)return 0;r=min(r,n);int k=lg[r-l+1]; n  return maxx(st[l][k],st[r-(1<<k)+1][k]); n } n signed main() n { n  n=read(),q=read(); n  for(int i=1;i<=n;i++)a[i]=read(),st[i][0]=i; n  lg[1]=0; n  for(int i=2;i<=n;i++)lg[i]=lg[i>>1]+1; n  for(int i=n;i>0;i--) n   for(int j=1;i+(1<<j)-1<=n;j++) n    st[i][j]=maxx(st[i][j-1],st[i+(1<<(j-1))][j-1]); n  for(int i=1;i<=n;i++) n   for(int k=0;k<=30;k++)dp[i][0][k]=i+a[i]+k; n  for(int j=0;j<lg[n];j++) n   for(int i=1;i<=n;i++) n    for(int k=0;k<=30;k++){ n     for(int l=k;l<=30;l++) n      dp[i][j+1][l]=max(dp[i][j+1][l],dp[ rmq(i,dp[i][j][k]) ][j][l-k]); n     for(int l=k;l<=30;l++) n      dp[i][j+1][l]=max(dp[i][j+1][l],dp[i][j+1][k]+l-k); n    } n  while(q--){ n   int l=read(),r=read(),k=read(); n   if(l==r){printf( 0 n );continue;} n   if(r<=l+a[l]+k){printf( 1 n );continue;} n   int ans=2; n   for(int i=0;i<=k;i++)A[i]=l+a[l]+i; n   for(int e=lg[r-l+1];e>=0;e--){ n    for(int i=0;i<=k;i++)tmp[i]=A[i],B[i]=rmq(l,A[i]); n    bool off=0; n    for(int i=0;i<=k&&!off;i++) n     for(int j=i;j<=k&&!off;j++) n      if(dp[B[i]][e][j-i]>tmp[j]){ n       tmp[j]=dp[B[i]][e][j-i]; n       if(tmp[j]>=r)off=1; n      } n    if(off)continue;ans+=(1<<e); n    for(int i=0;i<=k;i++)A[i]=tmp[i]; n   } n   printf( %d n ,ans); n  } n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b, arr[25], ans = 0, c[3]; n   bool r1 = 1; n   cin >> n >> a >> b; n   c[0] = a, c[1] = b; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   int mid = n / 2; n   if (2 * mid < n) mid++; n   for (int i = 0; i < mid; i++) { n     if (arr[i] == arr[n - i - 1] && arr[i] != 2) { n       continue; n     } else if (arr[i] == arr[n - i - 1] && arr[i] == 2) { n       if (i == n - i - 1) n         ans += min(a, b); n       else n         ans += 2 * min(a, b); n     } else if (arr[i] == 2 || arr[n - i - 1] == 2) { n       if (arr[i] == 2) n         ans += c[arr[n - i - 1]]; n       else n         ans += c[arr[i]]; n     } else n       r1 = 0; n   } n   if (r1) n     cout << ans << endl; n   else n     cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 3e5 + 10; n const long long M = 1e9 + 7; n int n, x, y, z; n int last[N], to[2 * N], w[2 * N], num, nxt[2 * N]; n long long a[N]; n long long ans; n void Add(int u, int v, int c) { n   nxt[++num] = last[u]; n   to[num] = v; n   w[num] = c; n   last[u] = num; n } n long long dp[N]; n long long dfs(int u, int f) { n   long long max1, max2; n   max1 = max2 = 0; n   for (int i = last[u]; i; i = nxt[i]) { n     int v = to[i]; n     if (v == f) continue; n     dfs(v, u); n     long long now = dp[v] - w[i]; n     if (now >= max1) { n       max2 = max1; n       max1 = now; n     } else if (now > max2) n       max2 = now; n   } n   long long tot = max1 + max2 + a[u]; n   ans = max(ans, tot); n   return dp[u] = max(max1, max2) + a[u]; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %lld , a + i); n   for (int i = 1; i <= n - 1; ++i) { n     scanf( %d%d%d , &x, &y, &z); n     Add(x, y, z); n     Add(y, x, z); n   } n   dfs(1, 0); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int a, b, c, t, d, e, f; n long long int max(long long int x, long long int y) { n   if (x > y) n     return x; n   else n     return y; n } n int main() { n   scanf( %I64d%I64d%I64d , &a, &b, &c); n   t = max(a, max(b, c)); n   d = max(t - 1, b); n   e = max(t - 1, c); n   f = max(t - 1, a); n   printf( %I64d n , d - b + e - c + f - a); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2> n istream& operator>>(istream& in, pair<T1, T2>& a) { n   in >> a.first >> a.second; n   return in; n } n template <typename T1, typename T2> n ostream& operator<<(ostream& out, pair<T1, T2> a) { n   out << a.first <<     << a.second; n   return out; n } n template <typename T, typename T1> n T amax(T& a, T1 b) { n   if (b > a) a = b; n   return a; n } n template <typename T, typename T1> n T amin(T& a, T1 b) { n   if (b < a) a = b; n   return a; n } n const long long INF = 1e18; n const int32_t M = 1e9 + 7; n const int32_t MM = 998244353; n const double inf = 2 * INF; n const long long N = 0; n vector<long long> tmp, v; n long long pw(long long a, long long p) { n   long long result = 1; n   while (p > 0) { n     if (p & 1) result = a * result; n     a = a * a; n     p >>= 1; n   } n   return result; n } n long long sq(long long x) { n   long long ans = sqrt(x); n   while (ans * ans < x) { n     ans++; n   } n   while (ans * ans > x) { n     ans--; n   } n   return ans; n } n void precompute() { n   for (long long i = 3; i < 61; i++) { n     for (long long j = 2; j < INF; j++) { n       if (pow(j, i) >= inf) { n         break; n       } n       tmp.push_back(pw(j, i)); n     } n   } n   sort((tmp).begin(), (tmp).end()); n   (tmp).erase(unique((tmp).begin(), (tmp).end()), (tmp).end()); n   for (long long x : tmp) { n     long long t = sq(x); n     if (t * t == x) { n       continue; n     } n     v.push_back(x); n   } n } n void solve() { n   long long n; n   cin >> n; n   long long ans = upper_bound((v).begin(), (v).end(), n) - v.begin(); n   long long x = sq(n) - 1; n   ans += x; n   ans = n - 1 - ans; n   cout << ans <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   precompute(); n   long long t = 1; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long srw[1000], scol[1000]; n long long rows[1000001], cols[1000001]; n int main() { n   int x, n, m, k, p, y, i, j; n   scanf( %d%d%d%d , &n, &m, &k, &p); n   for (i = 0; i < n; i++) n     for (j = 0; j < m; j++) { n       scanf( %d , &x); n       srw[i] += x; n       scol[j] += x; n     } n   multiset<long long, greater<long long> > rsm, csm; n   multiset<long long, greater<long long> >::iterator it; n   for (i = 0; i < n; i++) rsm.insert(srw[i]); n   for (j = 0; j < m; j++) csm.insert(scol[j]); n   for (i = 1; i <= k; i++) { n     it = rsm.begin(); n     long long val = *it; n     rsm.erase(it); n     rsm.insert(val - p * m); n     rows[i] = rows[i - 1] + val; n     it = csm.begin(); n     val = *it; n     csm.erase(it); n     csm.insert(val - p * n); n     cols[i] = cols[i - 1] + val; n   } n   long long ans = -99999999999999999; n   for (i = 0; i <= k; i++) n     ans = max(ans, cols[i] + rows[k - i] - 1LL * (k - i) * i * p); n   printf( %I64d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, i, j, T, ti; n long long MX = 4294967295; n long long temp; n long long loop(int n) { n   long long ret = 0; n   string str; n   int rn; n   cin >> str; n   m--; n   while (str !=  end ) { n     if (str ==  add ) { n       ret++; n     } else if (str ==  for ) { n       cin >> rn; n       temp = loop(rn); n       if (temp == -1) break; n       ret += temp; n     } else { n       cerr <<  WOW   << str << endl; n     } n     if (m == 0) break; n     cin >> str; n     m--; n   } n   if (temp == -1) return -1; n   if (ret * n > MX) { n     return -1; n   } n   return ret * n; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> m; n   temp = 0; n   temp = loop(1); n   if (temp == -1) { n     cout <<  OVERFLOW!!!  << endl; n   } else { n     cout << temp << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &t) { n   t = 0; n   char ch = getchar(); n   int f = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   do { n     (t *= 10) += ch -  0 ; n     ch = getchar(); n   } while ( 0  <= ch && ch <=  9 ); n   t *= f; n } n const long long mod = 998244353; n long long ksm(long long x, long long y) { n   if (x < 0) x += mod; n   if (x >= mod) x -= mod; n   long long res = 1; n   while (y) { n     if (y & 1) res = res * x % mod; n     x = x * x % mod; n     y >>= 1; n   } n   return res; n } n long long n, m, N, L, R, ans, x, y; n int main() { n   read(n); n   read(m); n   read(L); n   read(R); n   N = n * m; n   x = R / 2 - (L - 1) / 2; n   y = R - L + 1 - x; n   if (N % 2 == 1) n     ans = ksm(x + y, N); n   else { n     ans = ksm(x + y, N) + ksm(x - y, N); n     ans %= mod; n     if (N % 2 == 0) ans = ans * ksm(2, mod - 2) % mod; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void read(T &k) { n   k = 0; n   int flag = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) flag = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     k = k * 10 + c -  0 ; n     c = getchar(); n   } n   k *= flag; n } n const int N = 1005; n int n, m, k, s, t; n long long pa, pb, f[N][N], A, B; n void exgcd(long long a, long long b, long long &x, long long &y) { n   if (a == 0) { n     x = 0; n     y = 1; n     return; n   } n   exgcd(b % a, a, y, x); n   x -= b / a * y; n } n long long inv(long long a) { n   long long x, y; n   exgcd(a, 1000000007, x, y); n   return x; n } n int main() { n   cin >> k >> pa >> pb; n   A = pa * inv(pa + pb) % 1000000007; n   B = pb * inv(pa + pb) % 1000000007; n   for (int i = k; i >= 1; --i) n     for (int j = k - 1; j >= 0; --j) { n       if (i + j >= k) { n         f[i][j] = (i + j + pa * inv(pb)) % 1000000007; n       } else { n         f[i][j] = (A * f[i + 1][j] + B * f[i][i + j]) % 1000000007; n       } n     } n   cout << (f[1][0] + 1000000007) % 1000000007 << endl; n } n


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


#include <bits/stdc++.h> n using namespace std; n const long double PI = 3.1415926535897923846; n const long long int MOD = 1000000007; n const long long int N = 998244353; n long long int power(long long int x, long long int n) { n   long long int 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 long long int modinverse(long long int a) { return power(a, MOD - 2); } n void solve() { n   long long int n; n   cin >> n; n   vector<long long int> a(n); n   for (long long int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   if (n % 2 == 0) { n     long long int xo = 0; n     for (long long int i = 0; i < n - 1; i++) xo = (xo ^ a[i]); n     if (a.back() - xo) { n       cout <<  NO n ; n       return; n     } n   } n   n -= (1 - n % 2); n   cout <<  YES n ; n   cout << n - 1 << endl; n   for (long long int i = 0; i + 2 < n; i += 2) { n     cout << i + 1 <<     << i + 2 <<     << i + 3 << endl; n   } n   for (long long int i = n - 3; i >= 0; i -= 2) { n     cout << i + 1 <<     << i + 2 <<     << i + 3 << endl; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int test = 1; n   while (test--) { n     solve(); n   } n   cerr <<  Time :   << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC n        <<  ms n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int p[3500]; n   int n; n   cin >> n; n   bool b = 1; n   int j = 1; n   for (int i = 1; i <= n; i++) { n     cin >> p[i]; n   } n   sort(p + 1, p + n + 1); n   for (int i = 1; i <= n; i++) { n     if (p[i] != j) { n       cout << j; n       return 0; n     } else { n       j++; n     } n   } n   cout << p[n] + 1; n   return 0; n } n


#ifndef ONPC n   #define _FORTIFY_SOURCE 0 n   #pragma GCC optimize( Ofast ) n   #pragma GCC optimize( no-stack-protector ) n   #pragma GCC optimize( unroll-loops ) n   #pragma GCC target( sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native ) n   #pragma GCC optimize( fast-math ) n #endif n  n #include <bits/stdc++.h> n  n #define mp make_pair n #define eb emplace_back n #define x first n #define y second n  n template <typename T> n bool chmax(T &a, const T &b) { n     if (a < b) { n       a = b; n         return true; n     } n     return false; n } n  n template <typename T> n bool chmin(T &a, const T &b) { n     if (a > b) { n       a = b; n       return true; n     } n     return false; n } n  n using namespace std; n  n typedef long long ll; n typedef long double ld; n typedef pair<int, int> pt; n  n #ifdef ONPC n mt19937 rnd(228); n #else n mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); n #endif n  n const ll MOD = 1000000007LL; n const ll MOD2 = 998244353LL; n const ld PI = acos(-1); n  n #ifdef ONPC n const int MAXN = 100; n #else n const int MAXN = 300100; n #endif n //#define int long long n const ll INF = 1e18; n  n void solve(){ n   int n; n   cin >> n; n   vector<int> p(n); n   for (int i = 1; i < n; i++){ n     cin >> p[i]; n     p[i]--; n   } n   vector<ll> a(n); n   for (int i = 1; i < n; i++){ n     cin >> a[i]; n   } n   vector<int> d(n, 0); n   for (int i = 1; i < n; i++){ n     if (d[i]) continue; n     vector<int> q; n     int j = i; n     while (j != 0) { n       q.eb(j); n       j = p[j]; n     } n     reverse(begin(q), end(q)); n     for (int k = 0; k < q.size(); k++){ n       d[q[k]] = k + 1; n     } n   } n  n   vector<vector<int>> v(n, vector<int>()); n   for (int i = 0; i < n; i++){ n     v[d[i]].eb(i); n   } n  n   vector<ll> dp(n, 0); n   for (int i = 1; i < n; i++){ n     if (v[i].empty()) break; n  n     ll mnc = INF, mxc = -INF; n     for (int j : v[i]){ n       chmin(mnc, a[j]); n       chmax(mxc, a[j]); n     } n     ll mx1 = -INF, mx2 = -INF; n     for (int j : v[i]){ n       chmax(mx1, dp[p[j]] + a[j]); n       chmax(mx2, dp[p[j]] - a[j]); n     } n     for (int j : v[i]){ n       dp[j] = dp[p[j]] + max(a[j] - mnc, mxc - a[j]); n       chmax(dp[j], mx1 - a[j]); n       chmax(dp[j], mx2 + a[j]); n     } n   } n  n   ll ans = 0; n   for (int i = 0; i < n; i++) chmax(ans, dp[i]); n  n   //for (int i = 0; i < n; i++) cerr << i <<   :   << dp[i] <<   n ; n  n   cout << ans <<   n ; n } n  n /* n #ifndef ONPC n #define FILEIO n #endif n */ n signed main() n { n     #ifdef FILEIO n       freopen( input.txt ,  r , stdin); n       freopen( output.txt ,  w , stdout); n     #endif n  n     ios_base::sync_with_stdio(0); n     cin.tie(0); n  n     int Q; n     cin >> Q; n     while (Q--) n       solve(); n  n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string str; n   cin >> str; n   if (str[0] > 96) { n     str[0] -= 32; n   } n   cout << str << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int dx[] = {0, 1, 0, -1, -1, 1, -1, 1}; n long long int dy[] = {-1, 0, 1, 0, -1, 1, 1, -1}; n const long long int MAXN = 2e5 + 5; n const long long int N = 2e5 + 5; n long long int n, m; n void LCSubsequence(string s1, string s2) { n   long long int n1 = s1.size(), n2 = s2.size(); n   vector<vector<long long int>> dp(n1 + 1, vector<long long int>(n2 + 1, 0)); n   long long int res = 0; n   for (long long int i = 0; i <= n1; i++) { n     for (long long int j = 0; j <= n2; j++) { n       if (i == 0 || j == 0) { n         dp[i][j] = 0; n       } else if (s1[i - 1] == s2[j - 1]) { n         dp[i][j] = dp[i - 1][j - 1] + (4 - 2); n       } else { n         dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) - 1; n       } n       if (dp[i][j] < 0) dp[i][j] = 0; n       res = max(res, dp[i][j]); n     } n   } n   cout << res <<   n ; n } n long long int goAns = 0; n long long int go(long long int i, long long int j, string s1, string s2) { n   if (i >= n || j >= m) return 0; n   long long int x = 0, y = 0, res = 0; n   if (s1[i] == s2[j]) { n     res = (4 - 2) + max(0LL, go(i + 1, j + 1, s1, s2)); n   } else { n     x = -1 + go(i + 1, j, s1, s2); n     y = -1 + go(i, j + 1, s1, s2); n     res = max({0LL, x, y}); n   } n   goAns = max(goAns, res); n   return res; n } n void solve() { n   long long int x, y, z; n   cin >> n >> m; n   string s1, s2; n   cin >> s1 >> s2; n   LCSubsequence(s1, s2); n } n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int T = 1; n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma optimization_level 3 n #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops,fast-math,O3 ) n #pragma GCC target( avx,avx2,fma ) n using namespace std; n mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); n int a, k, nc = 0, isz = 0; n vector<unordered_set<int>> n, l; n vector<int> cmp, vrt; n void dfs(int v) { n   cmp[v] = nc; n   vrt.push_back(v); n   for (int i : l[v]) { n     if (cmp[i] == -1) n       dfs(i); n     else if (cmp[i] != nc) { n       assert(cmp[i] == 0); n       isz = 1; n     } n   } n } n int main() { n   cin.tie(0); n   cout.tie(0); n   cin.sync_with_stdio(0); n   cout.sync_with_stdio(0); n   ; n   cin >> a >> k; n   cmp = vector<int>(a, -1); n   l = vector<unordered_set<int>>(a, unordered_set<int>()); n   n = vector<unordered_set<int>>(a, unordered_set<int>()); n   for (int q = 0; q < k; q++) { n     int x, y; n     cin >> x >> y; n     x--, y--; n     n[x].insert(y); n     n[y].insert(x); n   } n   for (int q = 0; q < a; q++) { n     int sos = (a - 1) - n[q].size(); n     if (sos > a / 2) cmp[q] = 0; n   } n   for (int q = 0; q < a; q++) { n     if (cmp[q] != -1) continue; n     for (int w = 0; w < a; w++) { n       if (w == q || n[q].count(w)) continue; n       l[q].insert(w); n     } n   } n   for (int q = 0; q < a; q++) { n     if (cmp[q] != -1) continue; n     nc = q + 1; n     vrt.clear(); n     isz = 0; n     dfs(q); n     if (isz) { n       for (int i : vrt) cmp[i] = 0; n     } n   } n   map<int, int> mp; n   for (int i : cmp) mp[i]++; n   vector<int> u; n   for (pair<int, int> p : mp) u.push_back(p.second); n   sort(u.begin(), u.end()); n   cout << u.size() <<   n ; n   for (int i : u) cout << i <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int main() { n   int n, i, j, k, l, m; n   cin >> n; n   cin >> s; n   int cnt, ans; n   char c, c1, c2, c3; n   ans = 0; n   for (i = 0; i < n - 1; i++) { n     cnt = 0; n     if (s[i] == s[i + 1] && i < n - 1) { n       c = s[i]; n       while (s[i] == c) { n         i++; n         cnt++; n       } n       ans += (cnt / 2); n       if (i < n) c2 = s[i]; n       if (c2 ==  R  && c ==  B ) n         c1 =  G ; n       else if (c2 ==  R  && c ==  G ) n         c1 =  B ; n       else if (c2 ==  B  && c ==  G ) n         c1 =  R ; n       else if (c2 ==  B  && c ==  R ) n         c1 =  G ; n       else if (c2 ==  G  && c ==  B ) n         c1 =  R ; n       else if (c2 ==  G  && c ==  R ) n         c1 =  B ; n       if (i >= n) { n         if (c ==  R ) n           c1 =  B ; n         else if (c ==  B ) n           c1 =  G ; n         else n           c1 =  R ; n       } n       for (j = i - cnt + 1; j < i; j += 2) s[j] = c1; n       i--; n     } n   } n   printf( %d n , ans); n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int x[100009], y[100009], z[100009], m, l, r; n int main() { n   cin >> s; n   cin >> m; n   for (int i = 1; i <= s.size(); i++) { n     x[i] = x[i - 1]; n     y[i] = y[i - 1]; n     z[i] = z[i - 1]; n     if (s[i - 1] ==  x ) x[i]++; n     if (s[i - 1] ==  y ) y[i]++; n     if (s[i - 1] ==  z ) z[i]++; n   } n   for (int i = 1; i <= m; i++) { n     cin >> l >> r; n     if (min(x[r] - x[l - 1], min(y[r] - y[l - 1], z[r] - z[l - 1])) + 1 >= n         max(x[r] - x[l - 1], max(y[r] - y[l - 1], z[r] - z[l - 1]))) { n       cout <<  YES n ; n       continue; n     } n     if (r - l + 1 <= 2) { n       cout <<  YES n ; n       continue; n     } n     cout <<  NO n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   int a[110]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   int i, one, two, j, ans = 0; n   for (i = 0; i < k; i++) { n     for (one = 0, two = 0, j = i; j < n; j += k) { n       if (a[j] == 1) n         one++; n       else n         two++; n     } n     ans += min(one, two); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a, b; n int main() { n   cin >> n >> a >> b; n   int t = 1; n   int r = 0; n   if (a > b) { n     a ^= b ^= a ^= b; n   } n   while (t < n) { n     t <<= 1; n     ++r; n   } n   t >>= 1; n   if (a <= t && b > t) { n     cout <<  Final!  << endl; n     return 0; n   } n   while (a > t || b <= t) { n     a -= a > t ? t : 0; n     b -= b > t ? t : 0; n     t >>= 1; n     --r; n   } n   cout << r << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mo = 1e9 + 7; n const int maxi = 1e6 + 6; n long long a[maxi], x[maxi], b[maxi]; n int n; n long long t; n int sum[maxi]; n int los[maxi]; n int ll[maxi]; n int main() { n   cin >> n >> t; n   for (int i = 1; i <= n; i++) scanf( %lld , &a[i]); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &x[i]); n     sum[x[i] - 1]++; n     sum[i - 1]--; n   } n   for (int i = n; i > 0; i--) sum[i] += sum[i + 1]; n   for (int i = 1; i <= n; i++) n     if (x[i] < i || x[i] < x[i - 1]) return !printf( No n ); n   for (int i = 1; i <= n; i++) n     if (sum[i] > 0) n       b[i] = a[i + 1] + t; n     else n       b[i] = a[i] + t; n   for (int i = 2; i <= n; i++) n     if (b[i] <= b[i - 1]) b[i] = b[i - 1] + 1; n   for (int i = 2; i <= n; i++) n     if (a[i] + t > b[i - 1]) los[i] = 1; n   for (int i = 1; i <= n; i++) ll[i] = ll[i - 1] + los[i]; n   for (int i = 1; i <= n; i++) n     if (x[i] < n && x[i] - i > 0 && ll[x[i] + 1] - ll[i] == 0) n       return !printf( No n ); n   printf( Yes n ); n   for (int i = 1; i <= n; i++) printf( %lld  , b[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef vector<vector<int>> graph; n ifstream in( buffcraft.in , ios::in); n ofstream out( buffcraft.out , ios::out); n int pl, pr, vl, vr, k; n vector<int> p; n void dfs(long long x) { n   if (x < 1000000000) { n     p.push_back(x); n     dfs(x * 10 + 4); n     dfs(x * 10 + 7); n   } n } n int main() { n   dfs(0); n   p.push_back(INT_MAX); n   sort(p.begin(), p.end()); n   cin >> pl >> pr >> vl >> vr >> k; n   double cnt = 0; n   double all = ((vr - vl) + 1.0) * ((pr - pl) + 1.0); n   for (int i = 1; i < p.size() - k; i++) { n     int lm = p[i - 1] + 1, l = p[i], r = p[i + k - 1], rp = p[i + k] - 1; n     if (pl <= l && pr >= lm && vl <= rp && vr >= r) n       cnt += n           (min(pr, l) - max(pl, lm) + 1.0) * (min(vr, rp) - max(vl, r) + 1.0); n     if (vl <= l && vr >= lm && pl <= rp && pr >= r) { n       cnt += n           (min(vr, l) - max(vl, lm) + 1.0) * (min(pr, rp) - max(pl, r) + 1.0); n       if (k == 1) cnt -= 1.0; n     } n   } n   cout << setprecision(10) << cnt / all; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 256; n int n, a[MAXN], k, ans; n list<int> L; n set<int> s, S; n multiset<int> M; n int main() { n   cin >> n >> k; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n     if (i >= k) s.insert(a[i]); n   } n   if (s.size() > 1) { n     cout << -1; n     return 0; n   } n   ans = k - 1; n   while (a[ans] == a[k]) --ans; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n template <class t, class u> n void chmax(t& first, u second) { n   if (first < second) first = second; n } n template <class t, class u> n void chmin(t& first, u second) { n   if (second < first) first = second; n } n template <class t> n using vc = vector<t>; n template <class t> n using vvc = vc<vc<t>>; n using pi = pair<ll, ll>; n using vi = vc<ll>; n template <class t, class u> n ostream& operator<<(ostream& os, const pair<t, u>& p) { n   return os <<  {  << p.first <<  ,  << p.second <<  } ; n } n template <class t> n ostream& operator<<(ostream& os, const vc<t>& v) { n   os <<  { ; n   for (auto e : v) os << e <<  , ; n   return os <<  } ; n } n using uint = unsigned; n using ull = unsigned long long; n template <class t, size_t n> n ostream& operator<<(ostream& os, const array<t, n>& first) { n   return os << vc<t>(first.begin(), first.end()); n } n template <ll i, class T> n void print_tuple(ostream&, const T&) {} n template <ll i, class T, class H, class... Args> n void print_tuple(ostream& os, const T& t) { n   if (i) os <<  , ; n   os << get<i>(t); n   print_tuple<i + 1, T, Args...>(os, t); n } n template <class... Args> n ostream& operator<<(ostream& os, const tuple<Args...>& t) { n   os <<  { ; n   print_tuple<0, tuple<Args...>, Args...>(os, t); n   return os <<  } ; n } n template <class t> n void print(t x, ll suc = 1) { n   cout << x; n   if (suc == 1) cout <<   n ; n   if (suc == 2) cout <<    ; n } n ll read() { n   ll i; n   cin >> i; n   return i; n } n vi readvi(ll n, ll off = 0) { n   vi v(n); n   for (ll i = ll(0); i < ll(n); i++) v[i] = read() + off; n   return v; n } n template <class T> n void print(const vector<T>& v, ll suc = 1) { n   for (ll i = ll(0); i < ll(v.size()); i++) n     print(v[i], i == ll(v.size()) - 1 ? suc : 2); n } n string readString() { n   string s; n   cin >> s; n   return s; n } n template <class T> n T sq(const T& t) { n   return t * t; n } n void yes(bool ex = true) { n   cout <<  Yes  n        <<   n ; n   if (ex) exit(0); n } n void no(bool ex = true) { n   cout <<  No  n        <<   n ; n   if (ex) exit(0); n } n void possible(bool ex = true) { n   cout <<  Possible  n        <<   n ; n   if (ex) exit(0); n } n void impossible(bool ex = true) { n   cout <<  Impossible  n        <<   n ; n   if (ex) exit(0); n } n constexpr ll ten(ll n) { return n == 0 ? 1 : ten(n - 1) * 10; } n const ll infLL = LLONG_MAX / 3; n const ll inf = infLL; n ll topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); } n ll topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); } n ll botbit(signed first) { return first == 0 ? 32 : __builtin_ctz(first); } n ll botbit(ll first) { return first == 0 ? 64 : __builtin_ctzll(first); } n ll popcount(signed t) { return __builtin_popcount(t); } n ll popcount(ll t) { return __builtin_popcountll(t); } n bool ispow2(ll i) { return i && (i & -i) == i; } n ll mask(ll i) { return (ll(1) << i) - 1; } n bool inc(ll first, ll second, ll c) { return first <= second && second <= c; } n template <class t> n void mkuni(vc<t>& v) { n   sort(v.begin(), v.end()); n   v.erase(unique(v.begin(), v.end()), v.end()); n } n ll rand_int(ll l, ll r) { n   static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count()); n   return uniform_int_distribution<ll>(l, r)(gen); n } n template <class t> n void myshuffle(vc<t>& first) { n   for (ll i = ll(0); i < ll(ll(first.size())); i++) n     swap(first[i], first[rand_int(0, i)]); n } n template <class t> n ll lwb(const vc<t>& v, const t& first) { n   return lower_bound(v.begin(), v.end(), first) - v.begin(); n } n vi waf(vi x) { n   sort(x.begin(), x.end()); n   vi y{0}; n   for (auto v : x) y.push_back(y.back() + v); n   return y; n } n signed main() { n   cin.tie(0); n   ios::sync_with_stdio(0); n   cout << fixed << setprecision(20); n   ll n; n   cin >> n; n   vi pri(n + 1, 1), tot(n + 1); n   vi al, od; n   iota(tot.begin(), tot.end(), 0); n   for (ll i = ll(2); i < ll(n + 1); i++) n     if (pri[i]) { n       for (ll j = i; j <= n; j += i) tot[j] = tot[j] / i * (i - 1); n       for (ll j = i * 2; j <= n; j += i) pri[j] = 0; n     } n   for (ll i = ll(2); i < ll(n + 1); i++) { n     al.push_back(tot[i]); n     if (i % 2) od.push_back(tot[i]); n   } n   void(0); n   void(0); n   al = waf(al); n   od = waf(od); n   ll k; n   cin >> k; n   ll ans = inf; n   if (k + 1 < ll(al.size())) chmin(ans, al[k + 1]); n   if (k < ll(od.size())) chmin(ans, od[k]); n   ans++; n   print(ans); n } n


#include <bits/stdc++.h> n using namespace std; n long long ret = 0; n long long La, Ra, Ta; n long long Lb, Rb, Tb; n long long GCD = 0; n pair<long long, long long> gcd(long long a, long long b) { n   if (b == 0) { n     GCD = a; n     return {1, 0}; n   } n   pair<long long, long long> nxt = gcd(b, a % b); n   long long x = nxt.first; n   long long y = nxt.second; n   long long k = a / b; n   return {y, x - k * y}; n } n int main() { n   cout << setprecision(10); n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> La >> Ra >> Ta; n   cin >> Lb >> Rb >> Tb; n   pair<long long, long long> eq = gcd(Ta, Tb); n   long long L0 = La % GCD, R0 = L0 + (Ra - La); n   long long L1 = Lb % GCD, R1 = L1 + (Rb - Lb); n   La = L0; n   Ra = R0; n   Lb = L1; n   Rb = R1; n   for (long long offA = -2; offA <= 2; offA++) n     for (long long offB = -2; offB <= 2; offB++) { n       long long N_Ra = Ra + offA * Ta; n       long long N_La = La + offA * Ta; n       long long N_Rb = Rb + offB * Tb; n       long long N_Lb = Lb + offB * Tb; n       ret = max(ret, min(N_Ra, N_Rb) - max(N_La, N_Lb) + 1); n     } n   cout << ret <<   n ; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 10; n int n, m, ans, l; n int mark[MAXN], comp[MAXN], scc[MAXN], cyc[MAXN]; n vector<int> edge[MAXN], adj[MAXN], radj[MAXN]; n deque<int> dq; n void dfs(int source) { n   mark[source]++; n   ans++, comp[source] = l; n   for (auto &u : edge[source]) n     if (!mark[u]) dfs(u); n } n void dfs1(int source) { n   mark[source]++; n   for (auto &u : adj[source]) n     if (!mark[u]) dfs1(u); n   dq.push_back(source); n } n void rev(int source) { n   mark[source]++; n   scc[l]++; n   for (auto &u : radj[source]) n     if (mark[u] == 1) rev(u); n } n int main() { n   cin >> n >> m; n   while (m--) { n     int x, y; n     cin >> x >> y; n     edge[x].push_back(y), edge[y].push_back(x); n     adj[x].push_back(y), radj[y].push_back(x); n   } n   for (int i = 1; i <= n; i++) n     if (!mark[i]) ++l, dfs(i), ans--; n   fill(mark, mark + MAXN, 0); n   l = 0; n   for (int i = 1; i <= n; i++) n     if (!mark[i]) dfs1(i); n   while (dq.size()) { n     int x = dq.back(); n     dq.pop_back(); n     if (mark[x] < 2 && !cyc[comp[x]]) { n       ++l, rev(x); n       if (scc[l] > 1) cyc[comp[x]] = true, ans++; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int MAX3(long long int x, long long int y, long long int z) { n   long long int t = max(x, y); n   return max(t, z); n } n long long int power(long long int x, long long int n) { n   long long int i, ans = 1; n   for (i = 1; i <= (n); i += 1) { n     ans = ans * x; n   } n   return ans; n } n void solve() { n   long long int n, i, sum = 0; n   vector<long long int> a; n   cin >> n; n   for (i = 0; i < (n); i += 1) { n     long long int x; n     cin >> x; n     a.push_back(x); n     if (i % 2 == 0) sum += x; n   } n   vector<long long int> a1, a2; n   long long int m1 = 0, m2 = 0; n   for (i = 0; i < (n - 1); i += 1) { n     long long int t = -a[i] + a[i + 1]; n     i++; n     a1.push_back(t); n   } n   for (i = 1; i < (n - 1); i += 1) { n     long long int t = -a[i + 1] + a[i]; n     i++; n     a2.push_back(t); n   } n   long long int s = a1.size(); n   long long int max_so_far = 0; n   long long int max_ending_here = 0; n   for (i = 0; i < (s); i += 1) { n     max_ending_here += a1[i]; n     max_so_far = max(max_so_far, max_ending_here); n     if (max_ending_here < 0) max_ending_here = 0; n   } n   m1 = max_so_far; n   max_so_far = 0; n   max_ending_here = 0; n   s = a2.size(); n   for (i = 0; i < (s); i += 1) { n     max_ending_here += a2[i]; n     max_so_far = max(max_so_far, max_ending_here); n     if (max_ending_here < 0) max_ending_here = 0; n   } n   m2 = max_so_far; n   long long int ans = max(m1, m2); n   cout << ans + sum << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<vector<int> > a(n, vector<int>(n)); n   int sum = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       scanf( %d , &a[i][j]); n       if (i == j) sum += a[i][j]; n     } n   } n   sum &= 1; n   int q; n   scanf( %d , &q); n   const char *cc =  01 ; n   while (q--) { n     int t; n     scanf( %d , &t); n     if (t == 3) { n       printf( %c , cc[sum]); n     } else { n       scanf( %d , &t); n       sum ^= 1; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n vector<vector<int>> g; n vector<int> deg; n ll ans; n int n, m; n void add_edge(int x, int y) { n   if (x > y) { n     int t = x; n     x = y; n     y = t; n   } n   g[x].push_back(y); n   deg[x]++; n   deg[y]++; n } n void init() { n   scanf( %d %d , &n, &m); n   deg.resize(n, 0); n   g.resize(n, vector<int>()); n   ans = 0; n   for (int i = 0; i < m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     x--; n     y--; n     add_edge(x, y); n   } n   ans = 0; n   for (int i = 0; i < n; i++) n     ans += (ll)g[i].size() * (ll)(deg[i] - g[i].size()); n } n void work() { n   int Q; n   cout << ans << endl; n   scanf( %d , &Q); n   for (int qi = 0; qi < Q; qi++) { n     int x; n     scanf( %d , &x); n     x--; n     ans -= (ll)g[x].size() * (ll)(deg[x] - g[x].size()); n     ans += 0; n     for (int y : g[x]) { n       ll y_size = g[y].size(); n       ans -= (ll)y_size * (ll)(deg[y] - y_size); n       g[y].push_back(x); n       y_size++; n       ans += (ll)y_size * (ll)(deg[y] - y_size); n     } n     g[x].clear(); n     printf( %lld n , ans); n   } n } n int main() { n   init(); n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class c> n struct rge { n   c b, e; n }; n template <class c> n rge<c> range(c i, c j) { n   return rge<c>{i, j}; n } n template <class c> n auto dud(c *x) -> decltype(cerr << *x, 0); n template <class c> n char dud(...); n struct deb { n   template <class c> n   deb &operator<<(const c &) { n     return *this; n   } n }; n template <class T> n T &chmin(T &a, const T &b) { n   return a = min(a, b); n } n template <class T> n T &chmax(T &a, const T &b) { n   return a = max(a, b); n } n const long long dx[8] = {0, 1, -1, 0, 1, 1, -1, -1}; n const long long dy[8] = {1, 0, 0, -1, 1, -1, -1, 1}; n const long long mod = 1e9 + 7; n inline long long add(long long a, long long b) { n   return ((a % mod) + (b % mod)) % mod; n } n inline long long sub(long long a, long long b) { n   return ((a % mod) - (b % mod) + mod) % mod; n } n inline long long mul(long long a, long long b) { n   return ((a % mod) * (b % mod)) % mod; n } n template <typename T> n istream &operator>>(istream &is, vector<T> &vec) { n   for (auto &v : vec) is >> v; n   return is; n } n const double PI = acos(-1.0); n const long long inf = 1e18; n const long long maxn = 1e3 + 5; n void solve() { n   long long n; n   cin >> n; n   vector<long long> arr(2 * n); n   cin >> arr; n   cin >> arr; n   sort(arr.begin(), arr.end()); n   long long s1 = 0; n   long long s2 = 0; n   for (long long i = 0; i < n; i++) s1 += arr[i]; n   for (long long i = n; i < 2 * n; i++) s2 += arr[i]; n   if (s1 != s2) { n     for (long long i = 0; i < 2 * n; i++) { n       cout << arr[i] <<    ; n     } n     cout <<   n ; n   } else { n     cout << -1 <<   n ; n   } n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   for (long long tt = 1; tt <= t; tt++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1); n const double eps = 1e-9; n const int inf = 2000000000; n const long long infLL = 9000000000000000000; n inline bool checkBit(long long n, int i) { return n & (1LL << i); } n inline long long setBit(long long n, int i) { n   return n | (1LL << i); n   ; n } n inline long long resetBit(long long n, int i) { return n & (~(1LL << i)); } n int dx[] = {0, 0, +1, -1}; n int dy[] = {+1, -1, 0, 0}; n inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } n inline bool isLeapYear(long long year) { n   return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); n } n inline void normal(long long &a) { n   a %= 1000000007; n   (a < 0) && (a += 1000000007); n } n inline long long modMul(long long a, long long b) { n   a %= 1000000007, b %= 1000000007; n   normal(a), normal(b); n   return (a * b) % 1000000007; n } n inline long long modAdd(long long a, long long b) { n   a %= 1000000007, b %= 1000000007; n   normal(a), normal(b); n   return (a + b) % 1000000007; n } n inline long long modSub(long long a, long long b) { n   a %= 1000000007, b %= 1000000007; n   normal(a), normal(b); n   a -= b; n   normal(a); n   return a; n } n inline long long modPow(long long b, long long p) { n   long long r = 1; n   while (p) { n     if (p & 1) r = modMul(r, b); n     b = modMul(b, b); n     p >>= 1; n   } n   return r; n } n inline long long modInverse(long long a) { return modPow(a, 1000000007 - 2); } n inline long long modDiv(long long a, long long b) { n   return modMul(a, modInverse(b)); n } n inline long long power(long long bs, long long k) { n   long long x = 1LL, y = bs; n   if (k == 0) return 1LL; n   while (k > 0) { n     if (k % 2) x *= y; n     y *= y; n     k /= 2; n   } n   return x; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cout.unsetf(ios::floatfield); n   cout.precision(10); n   cout.setf(ios::fixed, ios::floatfield); n   ; n   long long n; n   cin >> n; n   if (n % 2) n     cout <<  black ; n   else { n     cout <<  white  <<   n ; n     cout <<  1 2 ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX = 1e3 + 20; n long long inf = (long long)1e15; n long long mod = (long long)1e9 + 7; n long long n, cntr[MAX], cntc[MAX], l[MAX][MAX], used[MAX]; n void swpr(long long ind1, long long ind2) { n   swap(cntr[ind1], cntr[ind2]); n   for (int i = 1; i <= n; i++) swap(l[ind1][i], l[ind2][i]); n } n void swpc(long long ind1, long long ind2) { n   swap(cntc[ind1], cntc[ind2]); n   for (int i = 1; i <= n; i++) swap(l[i][ind1], l[i][ind2]); n } n void solve() { n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     long long x, y; n     cin >> x >> y; n     l[x][y] = 1; n     cntr[x]++; n     cntc[y]++; n   } n   vector<pair<long long, pair<long long, long long>>> ans; n   long long need = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (cntr[j] == need && !used[j]) { n         used[i] = 1; n         need = 0; n         if (i != j) { n           swpr(i, j); n           ans.push_back({1, {i, j}}); n         } n         break; n       } n     } n     need++; n   } n   for (int i = 2; i <= n; i++) { n     long long start = (i - 1) - cntr[i] + 1; n     long long bord = start; n     while (bord <= n) { n       if (l[i][bord] == 1) { n         if (bord != start) { n           swpc(start, bord); n           ans.push_back({2, {start, bord}}); n         } n         start++; n       } n       bord++; n     } n   } n   cout << ans.size() <<   n ; n   for (int i = 0; i < ans.size(); i++) { n     cout << ans[i].first <<     << ans[i].second.first <<     n          << ans[i].second.second <<   n ; n   } n } n int main() { n   mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n   ios::sync_with_stdio(0); n   long long q; n   q = 1; n   while (q--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   string s; n   cin >> s; n   size_t pos1 = s.find( AB ), pos2 = s.find( BA ); n   string tmp = s; n   s.erase(0, pos1 + 2); n   tmp.erase(0, pos2 + 2); n   bool check = false; n   if (pos1 == string::npos || pos2 == string::npos) { n     cout <<  NO ; n   } else { n     if (s.find( BA ) != string::npos || tmp.find( AB ) != string::npos) n       check = true; n     if (check) n       cout <<  YES ; n     else n       cout <<  NO ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string mask(int a) { n   string ans; n   int x = a; n   while (x) { n     if (x % 10 == 4 || x % 10 == 7) ans.insert(ans.begin(), x % 10 +  0 ); n     x /= 10; n   } n   return ans; n } n int main() { n   string b; n   int a; n   cin >> a >> b; n   a++; n   while (mask(a) != b) a++; n   cout << a; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, d, e, f, mn1, mn2, res, ans1, ans2; n   cin >> a >> b >> c >> d >> e >> f; n   res = d; n   mn2 = min(b, min(c, d)); n   d = abs(mn2 - d); n   mn1 = min(a, d); n   ans1 = (mn2 * f) + (mn1 * e); n   mn1 = min(a, res); n   res = abs(mn1 - res); n   mn2 = min(b, min(c, res)); n   ans2 = (mn2 * f) + (mn1 * e); n   cout << max(ans1, ans2) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n int node[4][11][11][maxn]; n int h[110]; n char s[maxn], e[11]; n int lowbit(int x) { return x & (-x); } n void add(int ty, int pos, int x, int d) { n   for (; x < maxn; x += lowbit(x)) { n     for (int k = 1; k <= 10; k++) { n       node[ty][k][pos % k][x] += d; n     } n   } n } n int sum(int ty, int len, int pos, int x) { n   int ans = 0; n   for (; x > 0; x -= lowbit(x)) { n     ans += node[ty][len][pos][x]; n   } n   return ans; n } n int query(int l, int r, int ty, int len, int pos) { n   return sum(ty, len, pos, r) - sum(ty, len, pos, l - 1); n } n int main() { n   int n; n   h[ A ] = 0; n   h[ T ] = 1; n   h[ C ] = 2; n   h[ G ] = 3; n   scanf( %s%d , s, &n); n   int len = strlen(s); n   for (int i = 0; i < len; i++) { n     add(h[s[i]], i, i + 1, 1); n   } n   while (n--) { n     int a, b, c; n     scanf( %d , &a); n     if (a == 1) { n       scanf( %d%s , &b, e); n       add(h[s[b - 1]], b - 1, b, -1); n       add(h[e[0]], b - 1, b, 1); n       s[b - 1] = e[0]; n     } else { n       scanf( %d%d%s , &b, &c, e); n       len = strlen(e); n       int ans = 0; n       for (int i = 0; i < len; i++) { n         ans += query(b, c, h[e[i]], len, (i + b - 1) % len); n       } n       printf( %d n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long p[1000100], h[1000100], g[1000100], ans; n int main() { n   int m, n, x, y; n   cin >> n >> m; n   for (int i = 0; i <= n; i++) p[i] = (i ? p[i - 1] * 9 : 1); n   while (m--) { n     scanf( %d%d , &x, &y); n     h[x] += p[y]; n     h[y] += p[x]; n   } n   for (int i = 1; i <= n; i++) g[i] = h[i] + p[i]; n   sort(h + 1, h + n + 1); n   for (int i = 1, cnt = 0; i <= n; i++) n     if (h[i] == h[i - 1]) n       ans += cnt++; n     else n       cnt = 1; n   sort(g + 1, g + n + 1); n   for (int i = 1, cnt = 0; i <= n; i++) n     if (g[i] == g[i - 1]) n       ans += cnt++; n     else n       cnt = 1; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void die(string S) { n   puts(S.c_str()); n   exit(0); n } n int number[333][333]; n int cnt[100100]; n vector<int> V[100100]; n int grid[333][333]; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     vector<int> v; n     for (int i = 0; i < n * m; i++) { n       int x; n       cin >> x; n       v.push_back(x); n       number[i / m][i % m] = x; n       cnt[i] = 0; n       V[i].clear(); n     } n     sort((v).begin(), (v).end()); n     for (int i = 0; i < n; i++) n       for (int j = 0; j < m; j++) { n         number[i][j] = n             lower_bound((v).begin(), (v).end(), number[i][j]) - v.begin(); n         cnt[number[i][j]]++; n         V[number[i][j]].push_back(i * m + j); n       } n     int pos = 0; n     for (int i = 0; i < n * m; i++) n       if (cnt[i]) { n         int endp = (pos + cnt[i] - 1); n         if (endp / m == pos / m) { n           sort((V[i]).rbegin(), (V[i]).rend()); n           for (int j = 0; j < cnt[i]; j++) n             grid[(pos + j) / m][(pos + j) % m] = V[i][j]; n         } else { n           sort((V[i]).begin(), (V[i]).end()); n           int x = m - pos % m; n           for (int j = x - 1; j >= 0; j--) n             grid[(pos + x - j - 1) / m][(pos + x - j - 1) % m] = V[i][j]; n           x = endp % m + 1; n           for (int j = cnt[i] - 1; j >= cnt[i] - x; j--) n             grid[(endp / m * m + cnt[i] - j - 1) / m] n                 [(endp / m * m + cnt[i] - j - 1) % m] = V[i][j]; n           for (int j = m - pos % m; j < cnt[i] - x; j++) n             grid[(pos + j) / m][(pos + j) % m] = V[i][j]; n           for (int j = pos / m + 1; j < endp / m; j++) n             reverse(grid[j], grid[j] + m); n         } n         pos += cnt[i]; n       } n     int ans = 0; n     for (int i = 0; i < n; i++) n       for (int j = 0; j < m; j++) n         for (int k = j + 1; k < m; k++) n           if (grid[i][j] < grid[i][k]) ans++; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 10 + 5; n char a[maxN][maxN][maxN]; n int mark[maxN][maxN][maxN]; n int k, n, m; n int dx[] = {-1, 1, 0, 0, 0, 0}; n int dy[] = {0, 0, -1, 1, 0, 0}; n int dz[] = {0, 0, 0, 0, -1, 1}; n int cnt; n void dfs(int x, int y, int z) { n   mark[x][y][z] = 1; n   cnt++; n   for (int i = 0; i < 6; i++) { n     int r = x + dx[i], c = y + dy[i], t = z + dz[i]; n     if (r < 0 || c < 0 || t < 0) continue; n     if (r >= k || c >= n || t >= m) continue; n     if (!mark[r][c][t] && a[r][c][t] ==  . ) dfs(r, c, t); n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> k >> n >> m; n   for (int i = 0; i < k; i++) n     for (int j = 0; j < n; j++) n       for (int t = 0; t < m; t++) cin >> a[i][j][t]; n   int x, y; n   cin >> x >> y; n   x--; n   y--; n   dfs(0, x, y); n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   string s; n   cin >> s; n   int n = s.size(); n   int cz = 0; n   int co = 0; n   int ct = 0; n   for (int i = 0; i < n; i++) { n     co += (s[i] ==  1 ); n     ct += (s[i] ==  2 ); n     cz += (s[i] ==  0 ); n   } n   if (cz == 0 || ct == 0) { n     sort(s.begin(), s.end()); n     cout << s <<   n ; n     return 0; n   } n   if (co == 0) { n     cout << s <<   n ; n     return 0; n   } n   string aux =   ; n   int idx = -1; n   for (int i = 0; i < n; i++) { n     if (s[i] !=  1 ) aux += s[i]; n   } n   string res =   ; n   int occ = -1; n   for (int i = 0; i < aux.size(); i++) { n     if (aux[i] ==  2  && occ == -1) { n       while (co--) { n         res +=  1 ; n       } n       res += aux[i]; n       occ = 1; n     } else { n       res += aux[i]; n     } n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mx = (int)2e5 + 5; n int n, c[mx], ans[mx]; n char a[mx], b[mx]; n void solve() { n   int cnt = 0, temp = 0; n   for (int i = n - 1; i >= 0; i--) { n     c[cnt++] = (a[i] + b[i] - 2 *  a  + temp) % 26; n     temp = (a[i] + b[i] - 2 *  a  + temp) / 26; n   } n   c[cnt++] = temp; n   temp = 0; n   for (int i = cnt - 1; i >= 0; i--) { n     temp *= 26; n     ans[i] = (c[i] + temp) / 2; n     temp = (c[i] + temp) % 2; n   } n   for (int i = n - 1; i >= 0; i--) { n     printf( %c , ans[i] +  a ); n   } n   cout << endl; n } n int main() { n   cin >> n; n   scanf( %s%s , a, b); n   solve(); n } n


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


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353, MAX = 100003, INF = 1 << 30; n long long rui(long long a, long long b) { n   if (b == 1) n     return a % mod; n   else if (b == 0) n     return 1; n   else if (b % 2 == 0) n     return (rui(a, b / 2) * rui(a, b / 2)) % mod; n   else n     return (rui(a, b - 1) * a) % mod; n } n int main() { n   int Q; n   cin >> Q; n   for (int q = 0; q < Q; q++) { n     int N, M; n     cin >> N >> M; n     vector<int> S[N], color(N, 0); n     for (int i = 0; i < M; i++) { n       int a, b; n       cin >> a >> b; n       a--; n       b--; n       S[a].push_back(b); n       S[b].push_back(a); n     } n     long long ans = 1; n     for (int i = 0; i < N; i++) { n       if (color[i]) continue; n       int odd = 1, even = 0; n       color[i] = 1; n       queue<int> Q; n       Q.push(i); n       while (!Q.empty()) { n         int a = Q.front(); n         Q.pop(); n         for (int j = 0; j < S[a].size(); j++) { n           int b = S[a][j]; n           if (color[b] == 0) { n             color[b] = 3 - color[a]; n             if (color[b] % 2) n               odd++; n             else n               even++; n             Q.push(b); n           } else if (color[a] == color[b]) { n             ans = 0; n           } n         } n       } n       ans *= (rui(2, odd) + rui(2, even)) % mod; n       ans %= mod; n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int lenA, lenB; n int A[1000004]; n int B[1000004]; n int idxB[1000004]; n int lenP = 0, idxP = 0; n long long P[1000004 * 2]; n int main() { n   scanf( %d %d , &lenA, &lenB); n   for (int i = 0, _n = (lenA); i < _n; ++i) scanf( %d , A + i); n   memset(idxB, -1, sizeof(idxB)); n   for (int j = 0, _n = (lenB); j < _n; ++j) { n     scanf( %d , B + j); n     idxB[B[j]] = j; n   } n   long long offset = 0; n   int res = 0; n   for (int k = 0, _n = (lenA * 2); k < _n; ++k) { n     long long p = idxB[A[k % lenA]]; n     if (p < 0) { n       lenP = 0; n       idxP = 0; n       continue; n     } n     p += offset; n     if (lenP > 0 && P[lenP - 1] >= p) { n       p += lenB; n       offset += lenB; n     } n     P[lenP++] = p; n     idxP = lower_bound(P + idxP, P + lenP, p - lenB + 1) - P; n     int len = lenP - idxP; n     res = max(res, len); n   } n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     long long n, k; n     cin >> n >> k; n     long long d = (k / (n - 1)), r = (k % (n - 1)); n     if (r != 0) { n       cout << (d * n) + r << endl; n     } else { n       cout << (d * n) - 1 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int n, m; n int main() { n   long long ans; n   int b, c, A, B, C, l; n   scanf( %d%d , &n, &m); n   n++; n   m++; n   ans = 0; n   for (c = m; c <= n - m; c++) { n     C = 2 * (n * n - n * c + c * c); n     for (b = n - m; b >= m; b--) { n       A = 2 * n - b - c; n       B = b * (n + c); n       l = (C - B) / A; n       if (m > l) l = m - 1; n       if (l >= n - m) break; n       ans += n - m - l; n     } n   } n   printf( %I64d n , ans * 3); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005; n struct node { n   int c, f, l, i; n } a[maxn], b[maxn]; n bool cmp1(node a, node b) { return a.f < b.f; } n bool cmp2(node a, node b) { return a.c > b.c; } n int main() { n   int n, d, s; n   scanf( %d%d%d , &n, &d, &s); n   int nn = n; n   int m = 0; n   int ss = s; n   for (int i = 1; i <= n; i++) { n     scanf( %d%d%d , &a[i].c, &a[i].f, &a[i].l); n     a[i].i = i; n     b[i] = a[i]; n     if (a[i].c != 0 && a[i].l >= d) { n       if (m == 0 || a[m].f > a[i].f) m = i; n     } n   } n   int ansf = 0; n   int ansc = 0; n   if (m != 0 && a[m].f <= s) { n     s = s - a[m].f; n     sort(a + 1, a + 1 + n, cmp2); n     int sumc = a[1].c; n     int sum = 1; n     for (int i = 2; i <= n; i++) { n       sum++; n       sumc = sumc - 1 + a[i].c; n       if (sumc == 0) break; n     } n     int j = 0; n     for (int i = 1; i <= n; i++) n       if (a[i].l >= d && a[i].i != m) { n         a[++j] = a[i]; n       } n     n = j; n     sort(a + 1, a + 1 + n, cmp1); n     for (int i = 1; i <= n; i++) { n       if (sum == nn) break; n       if (s >= a[i].f) { n         sum++; n         s -= a[i].f; n       } else n         break; n     } n     ansf = ss - s; n     ansc = sum; n   } n   n = nn; n   for (int i = 1; i <= n; i++) a[i] = b[i]; n   s = ss; n   int sumc = 0; n   int j = 0; n   for (int i = 1; i <= n; i++) n     if (a[i].c == 0 && a[i].l >= d) { n       a[++j] = a[i]; n     } n   n = j; n   sort(a + 1, a + 1 + n, cmp1); n   for (int i = 1; i <= n; i++) { n     if (s >= a[i].f) { n       sumc++; n       s -= a[i].f; n     } else n       break; n   } n   if (ansc < sumc) { n     ansf = ss - s; n     ansc = sumc; n   } else if (ansc == sumc && ansf > ss - s) { n     ansf = ss - s; n     ansc = sumc; n   } n   printf( %d %d n , ansc, ansf); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool flag; n int vis[(1 << 17)]; n int n, m, k; n vector<pair<int, int> > vt[(1 << 17)]; n void dfs(int u) { n   for (int i = 0; i < vt[u].size(); i++) { n     int v = vt[u][i].first; n     int c = vt[u][i].second; n     if (vis[v] == -1) { n       if (c == 1) n         vis[v] = vis[u]; n       else n         vis[v] = !vis[u]; n       dfs(v); n     } n     if (c == 1 && (vis[v] != vis[u])) flag = true; n     if (c == 0 && (vis[v] == vis[u])) flag = true; n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     int u, v, c; n     scanf( %d%d%d , &u, &v, &c); n     vt[u].push_back(make_pair(v, c)); n     vt[v].push_back(make_pair(u, c)); n   } n   memset(vis, -1, sizeof(vis)); n   for (int i = 1; i <= n; i++) { n     if (vis[i] == -1) { n       vis[i] = 0; n       dfs(i); n       k++; n     } n   } n   long long ans = 1; n   if (flag) { n     puts( 0 ); n     return 0; n   } n   for (int i = 2; i <= k; i++) ans = ans * 2 % 1000000007; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int main() { n   pair<long long, long long> s, e; n   long long t; n   cin >> t; n   cin >> s.first >> s.second >> e.first >> e.second; n   map<char, int> mp; n   string str; n   cin >> str; n   for (int i = 0; i < t; ++i) { n     mp[str[i]]++; n   } n   char dir[] = { NSWE }; n   map<char, long long> nd; n   for (int j = 0; j < 4; ++j) { n     nd[dir[j]] = 0; n   } n   long long xdiff = abs(s.first - e.first); n   if (s.first < e.first) { n     nd[ E ] = xdiff; n   } else if (s.first > e.first) { n     nd[ W ] = xdiff; n   } n   long long ydiff = abs(s.second - e.second); n   if (s.second < e.second) { n     nd[ N ] = ydiff; n   } else if (s.second > e.second) { n     nd[ S ] = ydiff; n   } n   for (int i = 0; i < (int)str.size(); ++i) { n     if (nd[str[i]] > 0) { n       nd[str[i]]--; n     } n     int chck = 0; n     for (int j = 0; j < 4; ++j) chck += nd[dir[j]] == 0; n     if (chck == 4) { n       cout << i + 1 << endl; n       return 0; n     } n   } n   cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class graph { n   int v; n   list<int>* adj; n  n  public: n   graph(int x) { n     this->v = x; n     adj = new list<int>[v]; n   } n   void add_edge(int a, int b) { n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   void count_colors(int a, int& n, int color[], int color_ar[]); n }; n void graph::count_colors(int a, int& n, int color[], int color_ar[]) { n   list<int>::iterator itr; n   int x = 0; n } n int main() { n   int n, m, c, x, y; n   cin >> n >> m; n   int color[100001] = {0}, Ar[100001] = {0}, g[100001] = {0}; n   for (int i = 0; i < n; i++) { n     cin >> color[i]; n     g[color[i]] = 1; n   } n   map<pair<int, int>, int> mp; n   for (int i = 0; i < m; i++) { n     cin >> x >> y; n     if (mp[make_pair(color[x - 1], color[y - 1])] == 0 && n         color[x - 1] != color[y - 1]) { n       Ar[color[x - 1]]++; n     } n     if (mp[make_pair(color[y - 1], color[x - 1])] == 0 && n         color[x - 1] != color[y - 1]) { n       Ar[color[y - 1]]++; n     } n     mp[make_pair(color[x - 1], color[y - 1])] = 1; n     mp[make_pair(color[y - 1], color[x - 1])] = 1; n   } n   int max1 = INT_MIN, color1; n   for (int i = 0; i < 100001; i++) { n     if (g[i] == 1) { n       if (Ar[i] > max1) { n         max1 = Ar[i]; n         color1 = i; n       } n     } n   } n   cout << color1; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const int M = 1e9 + 7; n int n; n double v, v2, a[N], b[N]; n double x, y; n int id = 1; n int main() { n   ios_base ::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n >> v >> v2; n   for (int i = 0; i < n; ++i) { n     cin >> b[i]; n     a[i] = 1.0 * b[i] / v; n   } n   cin >> x >> y; n   for (int i = 1; i < n; ++i) { n     a[i] = a[i] + sqrt((b[i] - x) * (b[i] - x) + y * y) / v2; n     if (a[i] <= a[id]) { n       id = i; n     } n   } n   cout << id + 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int maxn = 1e5 + 6; n const long long int MOD = 1e9 + 7; n vector<int> prim(1000005, 1); n int fact[maxn]; n long long int binomialCoeff(long long int n, long long int k) { n   long long int res = 1; n   if (k > n - k) k = n - k; n   for (long long int i = 0; i < k; ++i) { n     res *= (n - i); n     res /= (i + 1); n   } n   return res; n } n bool isVowel(char ch) { n   if (ch ==  a  || ch ==  i  || ch ==  e  || ch ==  u  || ch ==  o ) { n     return true; n   } n   return false; n } n long long int power(long long int x, long long int i, long long int mod) { n   long long int ans = 1; n   while (i > 0) { n     if (i & 1) ans = (ans * x) % mod; n     i >>= 1; n     x = (x * x) % mod; n   } n   return ans; n } n long long int modInverse(long long int x, long long int mod) { n   return power(x, mod - 2, mod); n } n int nCr(int n, int r) { n   if (n < r) { n     return 0; n   } n   return (((fact[n] * modInverse(fact[n - r], MOD)) % MOD) * n           modInverse(fact[r], MOD)) % n          MOD; n } n long long int power(int x, unsigned int y) { n   long long int temp; n   if (y == 0) return 1; n   temp = power(x, y / 2); n   if (y % 2 == 0) n     return temp * temp; n   else n     return x * temp * temp; n } n void erath(int n) { n   prim[1] = 0; n   prim[0] = 0; n   prim[2] = 1; n   for (int i = 2; i * i <= n; i++) { n     if (prim[i]) { n       for (int j = i * i; j <= n; j += i) { n         prim[j] = 0; n       } n     } n   } n } n long long int gcd(long long int a, long long int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long int lcm(long long int a, long long int b) { n   return (a / gcd(a, b)) * b; n } n bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.second < b.second); n } n long long int n, m, k; n int getmid(int l, int r) { return (l + (r - l) / 2); } n struct segtree { n   int siz; n   vector<pair<long long int, long long int> > sums; n   void init(int n) { n     siz = 1; n     while (siz < n) siz *= 2; n     sums.assign(2 * siz, {LONG_MAX, 0}); n   } n   void set(int i, int v, int cur, int l, int r) { n     if (r - l == 1) { n       sums[cur].first = v; n       sums[cur].second = 1; n       return; n     } n     int m = getmid(l, r); n     if (i < m) { n       set(i, v, 2 * cur + 1, l, m); n     } else n       set(i, v, 2 * cur + 2, m, r); n     sums[cur].first = min(sums[2 * cur + 1].first, sums[2 * cur + 2].first); n     if (sums[2 * cur + 1].first == sums[cur].first && n         sums[2 * cur + 2].first == sums[cur].first) { n       sums[cur].second = sums[cur * 2 + 1].second + sums[cur * 2 + 2].second; n     } else if (sums[2 * cur + 1].first == sums[cur].first) { n       sums[cur].second = sums[cur * 2 + 1].second; n     } else { n       sums[cur].second = sums[cur * 2 + 2].second; n     } n   } n   void set(int i, int v) { set(i, v, 0, 0, siz); } n   pair<long long int, long long int> sum(int l, int r, int cur, int lx, n                                          int rx) { n     if (lx >= r || l >= rx) return {LONG_MAX, 0}; n     if (lx >= l && rx <= r) return {sums[cur].first, sums[cur].second}; n     int mid = getmid(lx, rx); n     pair<long long int, long long int> s1 = sum(l, r, 2 * cur + 1, lx, mid), n                                        s2 = sum(l, r, 2 * cur + 2, mid, rx); n     if (s1.first < s2.first) { n       return s1; n     } else if (s1.first > s2.first) n       return s2; n     else n       return {s1.first, s1.second + s2.second}; n   } n   pair<long long int, long long int> sum(int l, int r) { n     return sum(l, r, 0, 0, siz); n   } n }; n int nCrModp(int n, int r, int p) { n   if (r > n - r) r = n - r; n   int C[r + 1]; n   memset(C, 0, sizeof(C)); n   C[0] = 1; n   for (int i = 1; i <= n; i++) { n     for (int j = min(i, r); j > 0; j--) C[j] = (C[j] + C[j - 1]) % p; n   } n   return C[r]; n } n void ans(int top) { n   cin >> n >> m >> k; n   long long int mx = max(n, m), mn = min(m, n); n   if ((mn + (mx - mn)) > k) { n     cout << -1 << endl; n     return; n   } n   if (m == n) { n     long long int add = k + 1 - n; n     if (add % 2 == 0) add -= 2; n     cout << (mn - 1 + add) << endl; n     return; n   } n   long long int rem = mx - mn; n   k -= mn; n   if (rem & 1) { n     cout << (mn + k - 1) << endl; n   } else { n     if (k & 1) k -= 2; n     cout << (mn + k) << endl; n   } n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int t = 1, co = 1; n   cin >> t; n   while (t--) { n     ans(co); n     co++; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   int v, w; n }; n long long MOD = 1000000007; n long long _MOD = 1000000009; n double EPS = 1e-10; n long long extgcd(long long a, long long b, long long& x, long long& y) { n   if (b == 0) { n     x = (a >= 0) ? 1 : -1; n     y = 0; n     return abs(a); n   } else { n     long long res = extgcd(b, a % b, y, x); n     y -= (a / b) * x; n     return res; n   } n } n long long mod_inverse(long long a, long long m) { n   long long x, y; n   extgcd(a, m, x, y); n   return (m + x % m) % m; n } n long long f(int l, int r, vector<long long>& h, vector<long long>& zi) { n   long long x = (h[r] - h[l]) * zi[l] % MOD; n   return (x + MOD) % MOD; n } n int main() { n   int N, M; n   cin >> N >> M; n   vector<pair<long long, char> > a, b; n   while (N--) { n     int l; n     char c; n     scanf( %d-%c , &l, &c); n     if (a.size() && a.back().second == c) n       a.back().first += l; n     else n       a.push_back(pair<long long, char>(l, c)); n   } n   while (M--) { n     int l; n     char c; n     scanf( %d-%c , &l, &c); n     if (b.size() && b.back().second == c) n       b.back().first += l; n     else n       b.push_back(pair<long long, char>(l, c)); n   } n   N = a.size(); n   M = b.size(); n   if (M == 1) { n     long long ans = 0; n     for (int i = 0; i < N; i++) n       if (a[i].second == b[0].second) n         ans += max(0LL, a[i].first - b[0].first + 1); n     cout << ans << endl; n     return 0; n   } n   vector<pair<long long, char> > v; n   for (pair<long long, char> x : a) v.push_back(x); n   for (pair<long long, char> x : b) v.push_back(x); n   sort(v.begin(), v.end()); n   v.erase(unique(v.begin(), v.end()), v.end()); n   vector<int> A(N), B(M); n   for (int i = 0; i < N; i++) n     A[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin() + 1; n   for (int j = 0; j < M; j++) n     B[j] = lower_bound(v.begin(), v.end(), b[j]) - v.begin() + 1; n   vector<long long> z(200001), zi(200001); n   z[0] = zi[0] = 1; n   for (int i = 1; i <= 200000; i++) { n     z[i] = z[i - 1] * 523541 % MOD; n     zi[i] = mod_inverse(z[i], MOD); n   } n   vector<long long> ha(N + 1), hb(M + 1); n   for (int i = 0; i < N; i++) ha[i + 1] = (ha[i] + A[i] * z[i]) % MOD; n   for (int j = 0; j < M; j++) hb[j + 1] = (hb[j] + B[j] * z[j]) % MOD; n   int cnt = 0; n   for (int i = 0; i + M <= N; i++) { n     if (!(a[i].second == b[0].second && a[i].first >= b[0].first)) continue; n     if (!(a[i + M - 1].second == b[M - 1].second && n           a[i + M - 1].first >= b[M - 1].first)) n       continue; n     if (f(i + 1, i + M - 1, ha, zi) != f(1, M - 1, hb, zi)) continue; n     cnt++; n   } n   cout << cnt << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = ((long long)1E9 + 7); n template <class C> n void mini(C& a, C b) { n   a = min(a, b); n } n template <class C> n void maxi(C& a, C b) { n   a = max(a, b); n } n const int LG = 20; n struct node { n   int par; n   vector<int> inc; n   bool monk; n   int aim; n   vector<pair<int, int> > mx; n   int up[LG]; n   int depth; n   int dlow, dup; n   node() : par(-1), inc(), monk(false), aim(-1), depth(0), dlow(0), dup(0) {} n }; n struct dart { n   bool down; n   int rev; n   int dist; n   int head; n   int far; n   list<int> pil; n   dart(int _head = 0, int _dist = 0) n       : down(false), rev(0), dist(_dist), head(_head), far(-INF), pil() {} n }; n const int MN = 100100; n node T[MN]; n dart D[2 * MN]; n void assign(int i, int up, int d = 0) { n   T[i].par = up; n   T[i].depth = d; n   for (__typeof((T[i].inc).begin()) it = ((T[i].inc).begin()); n        it != (T[i].inc).end(); ++it) { n     if (*it == up) continue; n     D[*it].down = true; n     assign(D[*it].head, D[*it].rev, d + 1); n   } n } n void distUp(int i) { n   int mx = -INF; n   int up = T[i].par; n   for (__typeof((T[i].inc).begin()) it = ((T[i].inc).begin()); n        it != (T[i].inc).end(); ++it) { n     if (*it == up) continue; n     distUp(D[*it].head); n     maxi(mx, D[*it].far); n   } n   if (T[i].monk) maxi(mx, 0); n   if (up != -1) { n     D[D[up].rev].far = mx + D[up].dist; n   } n } n void distDown(int i) { n   int up = T[i].par; n   T[i].mx.push_back((up != -1) ? pair<int, int>(D[up].far, up) n                                : pair<int, int>(-INF, -1)); n   T[i].mx.push_back((T[i].monk) ? pair<int, int>(0, -1) n                                 : pair<int, int>(-INF, -1)); n   T[i].mx.push_back(pair<int, int>(-INF, -1)); n   sort((T[i].mx).begin(), (T[i].mx).end(), greater<pair<int, int> >()); n   for (__typeof((T[i].inc).begin()) it = ((T[i].inc).begin()); n        it != (T[i].inc).end(); ++it) { n     if (*it == up) continue; n     if (D[*it].far > T[i].mx[2].first) { n       T[i].mx[2] = make_pair(D[*it].far, *it); n       sort((T[i].mx).begin(), (T[i].mx).end(), greater<pair<int, int> >()); n     } n   } n   for (__typeof((T[i].inc).begin()) it = ((T[i].inc).begin()); n        it != (T[i].inc).end(); ++it) { n     if (*it == up) continue; n     int j = (*it == T[i].mx[0].second); n     D[D[*it].rev].far = T[i].mx[j].first + D[*it].dist; n     distDown(D[*it].head); n   } n } n void goUp(int i) { n   int up = T[i].par; n   if (T[i].monk) { n     if (T[i].mx[0].first == T[i].mx[1].first) n       T[i].aim = i; n     else { n       int rv = D[T[i].mx[0].second].rev; n       D[rv].pil.insert(D[rv].pil.end(), i); n     } n   } n   for (__typeof((T[i].inc).begin()) it = ((T[i].inc).begin()); n        it != (T[i].inc).end(); ++it) { n     if (*it == up) continue; n     goUp(D[*it].head); n     int j = 0, k = 1; n     if (T[i].mx[0].second == *it) n       j = 1, k = 2; n     else if (T[i].mx[1].second == *it) n       k = 2; n     int a = T[i].mx[j].second; n     int b = T[i].mx[k].second; n     if (a == -1 || (b != -1 && D[a].far == D[b].far)) { n       for (__typeof((D[*it].pil).begin()) jt = ((D[*it].pil).begin()); n            jt != (D[*it].pil).end(); ++jt) { n         T[*jt].aim = i; n       } n     } else { n       D[D[a].rev].pil.splice(D[D[a].rev].pil.end(), D[*it].pil); n     } n   } n } n void goDown(int i) { n   int up = T[i].par; n   if (up != -1) { n     int j = 0, k = 1; n     if (T[i].mx[0].second == up) n       j = 1, k = 2; n     else if (T[i].mx[1].second == up) n       k = 2; n     int a = T[i].mx[j].second; n     int b = T[i].mx[k].second; n     if (a == -1 || (b != -1 && D[a].far == D[b].far)) { n       for (__typeof((D[up].pil).begin()) jt = ((D[up].pil).begin()); n            jt != (D[up].pil).end(); ++jt) { n         T[*jt].aim = i; n       } n     } else { n       D[D[a].rev].pil.splice(D[D[a].rev].pil.end(), D[up].pil); n     } n   } n   for (__typeof((T[i].inc).begin()) it = ((T[i].inc).begin()); n        it != (T[i].inc).end(); ++it) { n     if (*it != up) goDown(D[*it].head); n   } n } n void add(int i, int j) { n   if (T[i].depth < T[j].depth) swap(i, j); n   T[i].dlow++; n   T[j].dlow++; n   int dd = T[i].depth - T[j].depth; n   for (int k = 0; k < (LG); ++k) { n     if (dd & (1 << k)) { n       i = T[i].up[k]; n     } n   } n   for (int k = (LG - 1); k >= (0); --k) { n     if (T[i].up[k] != T[j].up[k]) { n       i = T[i].up[k]; n       j = T[j].up[k]; n     } n   } n   if (i != j) { n     i = T[i].up[0]; n     j = T[j].up[0]; n   } n   T[i].dup++; n } n int bst = 0; n int cnt = 0; n int final(int i) { n   int v = 0; n   for (__typeof((T[i].inc).begin()) it = ((T[i].inc).begin()); n        it != (T[i].inc).end(); ++it) { n     if (*it != T[i].par) v += final(D[*it].head); n   } n   v += T[i].dlow; n   v -= T[i].dup; n   if (!T[i].monk) { n     if (v > bst) { n       bst = v; n       cnt = 0; n     } n     if (v == bst) ++cnt; n   } n   return v - T[i].dup; n } n int main() { n   ios_base::sync_with_stdio(false); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < (m); ++i) { n     int x; n     cin >> x; n     --x; n     T[x].monk = true; n   } n   for (int i = 0; i < (n - 1); ++i) { n     int a, b, c; n     cin >> a >> b >> c; n     --a; n     --b; n     D[2 * i] = dart(b, c); n     T[a].inc.push_back(2 * i); n     D[2 * i + 1] = dart(a, c); n     T[b].inc.push_back(2 * i + 1); n   } n   for (int i = 0; i < (2 * (n - 1)); ++i) D[i].rev = i ^ 1; n   assign(0, -1); n   distUp(0); n   distDown(0); n   goUp(0); n   goDown(0); n   for (int i = 0; i < (n); ++i) { n     for (int j = 0; j < (LG); ++j) T[i].up[j] = -1; n     int up = T[i].par; n     if (up != -1) T[i].up[0] = D[up].head; n   } n   for (int j = 0; j < (LG - 1); ++j) { n     for (int i = 0; i < (n); ++i) { n       if (T[i].up[j] != -1) T[i].up[j + 1] = T[T[i].up[j]].up[j]; n     } n   } n   for (int i = 0; i < (n); ++i) { n     if (T[i].monk) add(i, T[i].aim); n   } n   final(0); n   cout << bst <<     << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int Set(int N, int pos) { return N = N | (1 << pos); } n int reset(int N, int pos) { return N = N & ~(1 << pos); } n bool check(long long N, int pos) { return (bool)(N & (1LL << pos)); } n int k, n; n int ase[30]; n string arr[2505]; n vector<int> pos; n vector<int> adj[5005]; n vector<string> vec; n int main() { n   cin >> k >> n; n   for (int i = 1; i <= k; i++) { n     cin >> arr[i]; n   } n   if (n == 1) { n     cout << -1 << endl; n     return 0; n   } n   if (n == 2) { n     swap(arr[1][0], arr[1][1]); n     string s1 = arr[1]; n     swap(arr[1][0], arr[1][1]); n     for (int i = 1; i <= k; i++) { n       if (arr[i] != arr[1]) { n         cout << -1 << endl; n         return 0; n       } n     } n     cout << s1 << endl; n     return 0; n   } n   if (k == 1) { n     swap(arr[1][0], arr[1][1]); n     cout << arr[1] << endl; n     return 0; n   } n   bool hobe = true, first = true; n   int nul = 0; n   for (int i = 2; i <= k; i++) { n     int cnt = 0; n     for (int j = 0; j < n; j++) { n       if (arr[1][j] != arr[i][j]) { n         if (first) pos.push_back(j); n         adj[i].push_back(j); n         cnt++; n       } n     } n     if (adj[i].size() == 0) nul++; n     if (cnt == 1 || cnt > 4) hobe = false; n     if (cnt > 0 && first) first = false; n   } n   if (nul == k - 1) { n     swap(arr[1][0], arr[1][1]); n     cout << arr[1] << endl; n     return 0; n   } n   if (!hobe) n     cout << -1 << endl; n   else { n     for (int i = 0; i < pos.size(); i++) { n       for (int j = 0; j < n; j++) { n         if (pos[i] == j) continue; n         swap(arr[1][pos[i]], arr[1][j]); n         vec.push_back(arr[1]); n         swap(arr[1][pos[i]], arr[1][j]); n       } n     } n     int mx = 0; n     for (int i = 0; i < n; i++) { n       ase[arr[1][i] -  a ]++; n       mx = max(mx, ase[arr[1][i] -  a ]); n     } n     if (mx > 1) vec.push_back(arr[1]); n     string ans =  -1 ; n     for (int p = 0; p < vec.size(); p++) { n       int cnt = 0; n       for (int i = 2; i <= k; i++) { n         int mis = 0; n         char m1[5001], m2[5001]; n         for (int j = 0; j < n; j++) { n           if (vec[p][j] != arr[i][j]) { n             mis++; n             m1[mis] = vec[p][j]; n             m2[mis] = arr[i][j]; n           } n         } n         if (mis == 2 && m1[1] == m2[2] && m1[2] == m2[1]) n           cnt++; n         else if (mis == 0 && mx >= 2) n           cnt++; n         else n           break; n       } n       if (cnt == k - 1) { n         ans = vec[p]; n         break; n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, dx[] = {0, 0, -1, 1}, dy[] = {1, -1, 0, 0}, mp[100005], b; n int cheak(int x, int y) { n   if (abs(x - y) == m || abs(x - y) == 1) n     return 1; n   else n     return 0; n } n void dfs(int x) { n   if (x == n * m) { n     b = 1; n     cout <<  YES  << endl; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) printf( %d  , mp[i * m + j] + 1); n       printf(  n ); n     } n     return; n   } n   for (int i = x; i < n * m; i++) { n     swap(mp[i], mp[x]); n     if ((x / m == 0 || cheak(mp[x], mp[(x / m - 1) * m + x % m]) == 0) && n         (x % m == 0 || cheak(mp[x], mp[x / m * m + x % m - 1]) == 0)) { n       dfs(x + 1); n       if (b == 1) return; n       swap(mp[i], mp[x]); n     } n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n * m; i++) mp[i] = i; n   dfs(0); n   if (b == 0) printf( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct coordinate { n   float x; n   float y; n }; n float check1(coordinate c1, coordinate c2, coordinate c3, coordinate c4, n              coordinate x1) { n   float ans1 = (x1.y - c1.y) + (x1.x - c1.x); n   float ans2 = (x1.y - c3.y) + (x1.x - c3.x); n   if (ans1 == 0 || ans2 == 0) n     return 0; n   else { n     float ans = ans1 / ans2; n     return ans; n   } n } n float check2(coordinate c1, coordinate c2, coordinate c3, coordinate c4, n              coordinate x1) { n   float m = (c3.y - c1.y) / (c3.x - c1.x); n   float ans1 = (x1.y - c1.y) - m * (x1.x - c1.x); n   float ans2 = (x1.y - c2.y) - m * (x1.x - c2.x); n   if (ans1 == 0 || ans2 == 0) n     return 0; n   else { n     float ans = ans1 / ans2; n     return ans; n   } n } n int main() { n   float n, d; n   cin >> n >> d; n   coordinate c1, c2, c3, c4; n   c1.x = d; n   c1.y = 0; n   c2.x = 0; n   c2.y = d; n   c3.x = n; n   c3.y = n - d; n   c4.x = n - d; n   c4.y = n; n   int t; n   cin >> t; n   while (t--) { n     coordinate x1; n     cin >> x1.x >> x1.y; n     float ans1 = check1(c1, c2, c3, c4, x1); n     float ans2 = check2(c1, c2, c3, c4, x1); n     if (ans1 < 0 && ans2 < 0) n       cout <<  YES  << endl; n     else if (ans1 == 0 || ans2 == 0) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mul_mod(long long a, long long b, long long MOD) { return a * b; } n struct myhash { n   long long chash[401000][2]; n   long long mult[401000][2]; n   long long b1, M1, b2, M2; n   myhash(long long b1 = 1000000021, long long M1 = 1200000000000000013LL, n          long long b2 = 1000000009, long long M2 = 1200000000000000011LL) n       : b1(b1), M1(M1), b2(b2), M2(M2) { n     calc(); n   } n   void calc() { n     mult[0][0] = mult[0][1] = 1; n     for (long long i = 1; i < 401000; i++) { n       mult[i][0] = mul_mod(mult[i - 1][0], b1, M1); n       mult[i][1] = mul_mod(mult[i - 1][1], b2, M2); n     } n   } n   void compute(vector<long long> &st) { n     chash[0][0] = chash[0][1] = st[0] + 1; n     for (int i = 1; i < (int)st.size(); i++) { n       chash[i][0] = (mul_mod(chash[i - 1][0], b1, M1) + st[i] + 1); n       chash[i][1] = (mul_mod(chash[i - 1][1], b2, M2) + st[i] + 1); n     } n   } n   pair<long long, long long> substr(long long l, long long r) { n     if (!l) return make_pair(chash[r][0], chash[r][1]); n     long long p1 = n         (chash[r][0] - mul_mod(chash[l - 1][0], mult[r - l + 1][0], M1)); n     long long p2 = n         (chash[r][1] - mul_mod(chash[l - 1][1], mult[r - l + 1][1], M2)); n     return make_pair(p1, p2); n   } n } h1, h2; n long long n, m, a[401000]; n vector<long long> diff; n int main() { n   scanf( %lld %lld , &n, &m); n   for (int i = 0; i < n; i++) { n     scanf( %lld , &a[i]); n     if (i) diff.push_back((a[i] - a[i - 1] + m) % m); n   } n   diff.push_back((a[0] - a[n - 1] + m) % m); n   for (int i = 0, sz = diff.size(); i < sz; i++) diff.push_back(diff[i]); n   h1.compute(diff); n   reverse(diff.begin(), diff.end()); n   h2.compute(diff); n   reverse(diff.begin(), diff.end()); n   long long acum = 0; n   for (int i = 0; i < n; i++) acum += diff[i]; n   assert(acum % m == 0); n   vector<long long> ans; n   for (int i = n; i < diff.size(); i++) { n     acum += diff[i]; n     if (h1.substr(0, i) == h2.substr(diff.size() - i - 1, diff.size() - 1)) { n       ans.push_back((2LL * a[0] + acum) % m); n     } n   } n   sort(ans.begin(), ans.end()); n   ans.resize(unique(ans.begin(), ans.end()) - ans.begin()); n   printf( %d n , ans.size()); n   for (int i = 0; i < ans.size(); i++) { n     printf( %lld%c , ans[i], (i + 1 == ans.size()) ?   n  :    ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n long long mpow(long long x, long long n) { n   long long res = 1; n   while (n) { n     if (n & 1) res = res * x % mod; n     x = x * x % mod; n     n /= 2; n   } n   return res; n } n long long s2(long long n) { n   n %= mod; n   return n * (n - 1) % mod; n } n long long s3(long long n) { n   n %= mod; n   return n * (n - 1) % mod * (n - 2) % mod; n } n long long eval(vector<long long> p, long long t) { n   long long res = 0; n   for (int i = p.size() - 1; i >= 0; --i) { n     res = (res * t + p[i]) % mod; n   } n   return res; n } n int main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(0); n   long long n; n   cin >> n; n   vector<long long> p(4); n   long long inv2 = mpow(2, mod - 2); n   long long inv3 = mpow(3, mod - 2); n   p[3] = (inv3) % mod; n   p[2] = (-(n + 2) % mod * inv2) % mod; n   p[1] = (-inv3 + (n + 2) % mod * inv2) % mod; n   p[0] = (-s3(n + 2) * inv3 + (n + 2) % mod * inv2 % mod * s2(n + 1)) % mod; n   long long s = eval(p, 1); n   for (long long x = 1; x * x <= n; ++x) { n     s = (s + 4 * eval(p, x * x % mod)) % mod; n   } n   vector<vector<long long> > pws(4, vector<long long>(1000003)); n   for (int i = 0; i < pws.size(); ++i) { n     for (long long j = 1; j < pws[i].size(); ++j) { n       long long j2 = j * j % mod; n       long long add = 1; n       for (int t = 0; t < i; ++t) add = add * j2 % mod; n       pws[i][j] = (pws[i][j - 1] + add) % mod; n     } n   } n   long long y = sqrt(n) + 2; n   for (long long x = 1; x * x < n; ++x) { n     while (x * x + y * y > n) --y; n     long long x2 = x * x % mod; n     long long s1 = 0; n     s1 = (s1 + y * p[0]) % mod; n     s1 = (s1 + (x2 * y % mod + pws[1][y]) * p[1]) % mod; n     s1 = n         (s1 + (x2 * x2 % mod * y % mod + 2 * x2 * pws[1][y] % mod + pws[2][y]) * n                   p[2]) % n         mod; n     s1 = (s1 + (x2 * x2 % mod * x2 % mod * y % mod + n                 3 * x2 * x2 % mod * pws[1][y] % mod + 3 * x2 * pws[2][y] % mod + n                 pws[3][y]) * n                    p[3]) % n          mod; n     s = (s + 4 * s1) % mod; n   } n   cout << (s % mod + mod) % mod << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool cmp(int a, int b) { n   if (a > b) n     return true; n   else n     return false; n } n struct pack { n   long long t, T, x, cost; n }; n int main() { n   long long ans = 0, n, m; n   cin >> n >> m; n   vector<pack> data; n   for (int i = 0; i < (n); ++i) { n     pack temp; n     cin >> temp.t >> temp.T >> temp.x >> temp.cost; n     data.push_back(temp); n   } n   for (int i = 0; i < (n); ++i) { n     data[i].T = data[i].T - data[i].t; n     if (data[i].T > 0) { n       long long ans1; n       if (m > data[i].T) n         ans1 = data[i].cost + m * data[i].x; n       else n         ans1 = data[i].cost; n       if (m > data[i].T) { n         long long k = data[i].cost * ((m + data[i].T - 1) / data[i].T); n         ans1 = min(ans1, k); n       } n       if (m > 2 * data[i].T) { n         long long ans2 = m % data[i].T + data[i].T; n         ans1 = min(ans1, data[i].cost * (1 + (m - ans2) / data[i].T) + n                              ans2 * data[i].x); n       } n       ans += ans1; n     } else { n       ans += data[i].cost + m * data[i].x; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const long long infl = 0x3f3f3f3f3f3f3f3fLL; n template <class T> n void rd(T &a) { n   char ch = getchar(); n   bool ng = 0; n   a = 0; n   for (; ch <  0  || ch >  9 ; ch = getchar()) n     if (ch ==  - ) ng = 1; n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) n     a = (a << 1) + (a << 3) + (ch ^  0 ); n   if (ng) a = ~a + 1; n } n template <class T> n void wrt(T a) { n   if (a < 0) { n     putchar( - ); n     wrt(~a + 1); n   } else { n     if (a > 9) wrt(a / 10); n     putchar(a % 10 ^  0 ); n   } n } n const int N = 5e5 + 5; n int n, q, nxt[N], a[N], ans[N]; n map<int, int> pos; n vector<pair<int, int> > Q[N]; n int fen[N]; n void upd(int p, int x) { n   for (; p <= n; p += p & (-p)) fen[p] = min(fen[p], x); n } n int get(int p) { n   int res = inf; n   for (; p; p -= p & (-p)) res = min(res, fen[p]); n   return res; n } n int main() { n   rd(n), rd(q); n   for (int i = 1; i <= n; ++i) rd(a[i]); n   for (int i = n; i; --i) { n     if (pos.count(a[i])) n       nxt[i] = pos[a[i]]; n     else n       nxt[i] = -1; n     pos[a[i]] = i; n   } n   for (int i = 1; i <= n; ++i) fen[i] = inf; n   for (int i = 1; i <= q; ++i) { n     int l, r; n     rd(l), rd(r); n     Q[l].push_back(pair<int, int>(r, i)); n   } n   for (int i = n; i; --i) { n     if (~nxt[i]) upd(nxt[i], nxt[i] - i); n     for (int j = 0; j < (int)((Q[i]).size()); ++j) { n       int r = Q[i][j].first, id = Q[i][j].second; n       ans[id] = get(r); n     } n   } n   for (int i = 1; i <= q; ++i) { n     if (ans[i] >= inf) n       puts( -1 ); n     else n       wrt(ans[i]), puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n #define PI atan(1.0)*4 n #define rp(i,s,t) for (register int i = (s); i <= (t); i++) n #define RP(i,t,s) for (register int i = (t); i >= (s); i--) n #define sc(x) scanf( %d ,&x) n #define scl(x) scanf( %lld ,&x) n #define ll long long n #define ull unsigned long long n #define mst(a,b) memset(a,b,sizeof(a)) n #define lson l,m,rt<<1 n #define rson m+1,r,rt<<1|1 n #define pii pair<int,int> n #define pll pair<ll,ll> n #define pil pair<int,ll> n #define m_p make_pair n #define p_b push_back n #define ins insert n #define era erase n #define INF 0x3f3f3f3f n #define LINF 0x3f3f3f3f3f3f3f3f n #define dg if(debug) n #define outval(a) cout <<  Debuging...|  << #a <<  :   << a <<   n ; n using namespace std; n int debug = 0; n ll gcd(ll a,ll b){ n  return b?gcd(b,a%b):a; n } n ll lcm(ll a,ll b){ n  return a/gcd(a,b)*b; n } n inline int read(){ n  int s=0,f=1; n  char ch=getchar(); n  while(ch< 0 ||ch> 9 ){ n   if(ch== - ) f=-1; n   ch=getchar(); n  } n  while(ch>= 0 &&ch<= 9 ){ n   s=s*10+ch- 0 ; n   ch=getchar(); n  } n  return s*f; n } n const int N = 1e6+7; n const double eps = 1e-7; n int a[N]; n int f[N]; n void solve(){ n  int n=read(),m=read(); n  rp(i,1,n) a[i]=read(); n  rp(i,1,n) f[i]=0; n  RP(i,n,1){ n   if(a[i]!=i) break; n   else f[i]=1; n  } n  int flag=1; n  rp(i,1,n){ n   if(f[i]==0){ n    flag=0; n   } n  } n  double ans=0.0; n  double cur=1; n  int ff=0; n  rp(i,1,m){ n   int r;double p; n   scanf( %d%lf ,&r,&p); n   // printf( %d %lf n ,r,p); n   if(r==n||f[r+1]){ n    ans+=cur*p; n    cur*=(1.0-p); n    if(fabs(p-1.0)<=eps) ff=1; n   } n  } n  if(ff||flag){ n   printf( 1.000000 n ); n   return ; n  } n  printf( %.6f n ,ans); n } n int main(){ n  //ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); n #ifdef ONLINE_JUDGE n #else n  freopen( in.txt ,  r , stdin); n  //debug = 1; n #endif n  time_t beg, end; n  //if(debug) beg = clock(); n  n  int T=read(); n  while(T--) solve(); n  n  /* n  if(debug) { n   end = clock(); n   printf( time:%.2fs n , 1.0 * (end - beg) / CLOCKS_PER_SEC); n  } n  */ n  return 0; n }


#include <bits/stdc++.h> n const int N = 2e5 + 3; n int n, m, l[N + N + N], r[N + N + N], k; n char a[N + N + N], b[N]; n int main() { n   int p, q; n   scanf( %d%d%s , &n, &m, a + 1); n   for (int i = 1; i <= n; i++) a[i + n] = a[i + n + n] = a[i]; n   for (int i = 1; i <= n; i++) b[i] = a[i]; n   for (int i = 1; i <= n; i++) n     if (a[i + n] == a[i + n - 1]) goto Brk; n   for (int i = 1 + (m & 1); i <= n + (m & 1); i++) putchar(a[i]); n   return 0; n Brk:; n   k = 0; n   for (int i = 1; i <= n * 3; i++) n     if (a[i] == a[i - 1] && a[i] != a[i + 1]) r[++k] = i; n   k = 0; n   for (int i = 1; i <= n * 3; i++) n     if (a[i] != a[i - 1] && a[i] == a[i + 1]) l[++k] = i; n   for (int i = 1 + n; i <= n * 2; i++) n     if (a[i] != a[i - 1] && a[i] != a[i + 1]) { n       q = l[std::lower_bound(l + 1, l + 1 + k, i) - l]; n       p = r[std::lower_bound(r + 1, r + 1 + k, i) - r - 1]; n       if (q - i > m && i - p > m) n         b[i - n] = a[i + (m & 1)]; n       else if (q - i < i - p) n         b[i - n] = a[q]; n       else n         b[i - n] = a[p]; n     } n   for (int i = 1; i <= n; i++) putchar(b[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, t, k, x, d[200001] = {}, last_d = 1, last = 0, a; n   long long sum = 0; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> t; n     if (1 == t) { n       cin >> a >> x; n       sum += a * x; n       if (last_d == 0) { n         last += x; n       } else if (last_d == a) { n         last += x; n       } else { n         d[a] -= x; n       } n     } else if (2 == t) { n       cin >> k; n       sum += k; n       d[last_d++] = k - last; n       last = k; n     } else { n       sum -= last; n       last -= d[--last_d]; n     } n     cout << setiosflags(ios::fixed) << setprecision(6) n          << ((double)sum) / ((double)last_d) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, d; n   cin >> n >> d; n   int t[n], sum = 0, rest = (n - 1) * 10; n   for (int i = 0; i < n; i++) { n     cin >> t[i]; n     sum = sum + t[i]; n   } n   if ((rest + sum) > d) { n     cout <<  -1 ; n     return 0; n   } else { n     int bal = d - sum, max; n     max = bal / 5; n     cout << max; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:100000000 ) n using namespace std; n const int N = 200; n char s[N][N]; n int main() { n   int C; n   cin >> C; n   int n = 3; n   while (n * (n - 1) * (n - 2) / 6 <= C) ++n; n   int m = --n; n   C -= n * (n - 1) * (n - 2) / 6; n   vector<int> add; n   while (C > 0) { n     ++n; n     for (int i = 2; i <= m; i++) { n       if (i * (i - 1) / 2 > C) { n         --i; n         add.push_back(i); n         C -= i * (i - 1) / 2; n         break; n       } n     } n   } n   assert(n <= 100); n   memset(s,  0 , sizeof(s)); n   for (int(i) = 0; (i) < (m); (i)++) n     for (int(j) = 0; (j) < (i); (j)++) s[i][j] = s[j][i] =  1 ; n   for (int i = m; i < n; i++) { n     for (int(j) = 0; (j) < (add[i - m]); (j)++) { n       s[i][j] = s[j][i] =  1 ; n     } n   } n   printf( %d n , n); n   for (int(i) = 0; (i) < (n); (i)++) { n     s[i][n] = 0; n     puts(s[i]); n   } n   return 0; n } n