#include <bits/stdc++.h> n using namespace std; n template <class T> n void chkmin(T &x, T y) { n   if (y < x) x = y; n } n template <class T> n void chkmax(T &x, T y) { n   if (y > x) x = y; n } n const int N = 105; n int a[N], n, x, ans1, ans2[N][N], len; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= (n); i++) scanf( %d , a + i); n   for (int i = 1; i <= (n - (n & 1)); i++) { n     for (int j = 1; j <= (n); j++) n       if (a[j] == i) { n         x = j; n         break; n       } n     ans1++; n     len = 0; n     for (int j = 1; j <= (i - 1); j++) ans2[ans1][++len] = 1; n     ans2[ans1][++len] = x - i + 1; n     reverse(a + i, a + x + 1); n     if (x < n) { n       ans2[ans1][++len] = n - x; n       reverse(a + x + 1, a + n + 1); n     } n     if (i % 2 == 0) reverse(ans2[ans1] + 1, ans2[ans1] + len + 1); n     if (len == 1) ans1--; n     ans2[ans1][0] = len; n   } n   printf( %d n , ans1); n   for (int i = 1; i <= (ans1); i++) { n     printf( %d  , ans2[i][0]); n     for (int j = 1; j <= (ans2[i][0]); j++) printf( %d  , ans2[i][j]); n     putchar(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3fffffff; n const int maxn = 100000 + 5, maxh = 800; n int n, k; n int f[maxh], g[maxh]; n vector<int> P[maxn]; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1, iend = k; i <= iend; ++i) { n     int x, y; n     scanf( %d%d , &x, &y); n     P[y].push_back(n - x + 1); n   } n   for (int i = 1, iend = n; i <= iend; ++i) sort(P[i].begin(), P[i].end()); n   int hgt = sqrt(6 * k) + 5; n   memset(f, 0x3f, sizeof f); n   f[0] = 0; n   for (int i = 1, iend = n; i <= iend; ++i) { n     memcpy(g, f, sizeof f); n     memset(f, 0x3f, sizeof f); n     int minf = 0, pt = 0; n     for (int j = 0, jend = hgt; j <= jend; ++j) { n       if (pt < P[i].size() && P[i][pt] == j) pt++; n       if (g[j] < g[minf]) minf = j; n       f[j] = min(g[j + 1], g[minf] + (j ? j * (j + 1) / 2 + 2 : 0)) + n              (P[i].size() - pt) * 3; n     } n   } n   int ans = inf; n   for (int i = 0, iend = hgt; i <= iend; ++i) ans = min(ans, f[i]); n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[27]; n bool h[27]; n bool valid[1005][1005]; n long long dp[1005]; n int main() { n   for (int i = 0; i < 1005; i++) { n     for (int j = 0; j < 1005; j++) valid[i][j] = false; n   } n   for (int i = 0; i < 26; i++) h[i] = false; n   int n, lsub = 1; n   cin >> n; n   string s; n   cin >> s; n   for (int i = 0; i < 26; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) { n     int f = 0; n     for (int j = i; j < n; j++) { n       h[s[j] -  a ] = true; n       for (int k = 0; k < 26; k++) n         if (h[k] && a[k] < j - i + 1) { n           f = 1; n           break; n         } n       if (f == 1) break; n       valid[i][j] = true; n       lsub = max(lsub, j - i + 1); n     } n     for (int k = 0; k < 26; k++) h[k] = false; n   } n   dp[0] = 1; n   dp[-1] = 1; n   for (int i = 1; i < n; i++) { n     dp[i] = dp[i - 1]; n     for (int j = 0; j < i; j++) { n       if (valid[j][i]) dp[i] = (dp[i] + dp[j - 1]) % 1000000007; n     } n   } n   int i = 0, j = 0, k = 0; n   while (i < n) { n     if (!valid[j][i]) { n       j = i; n       k++; n     } n     i++; n   } n   cout << dp[n - 1] <<   n ; n   cout << lsub <<   n ; n   cout << k + 1 <<   n ; n } n


#include <bits/stdc++.h> n int dp[2][1005]; n int main() { n   char start[2 * 1005], end[1005]; n   int i, j, k, K; n   while (scanf( %s %s %d , start, end, &K) != EOF) { n     memset(dp, 0, sizeof(dp)); n     int n = strlen(start); n     for (i = n; i < 2 * n; i++) start[i] = start[i - n]; n     dp[0][0] = 1; n     int cur = 1; n     long long sum[2]; n     sum[0] = 1, sum[1] = 0; n     for (k = 0; k < K; k++) { n       sum[cur] = 0; n       for (i = 0; i < n; i++) { n         dp[cur][i] = (sum[cur ^ 1] - dp[cur ^ 1][i] + 1000000007); n         if (dp[cur][i] >= 1000000007) dp[cur][i] -= 1000000007; n         sum[cur] = (sum[cur] + dp[cur][i]); n         if (sum[cur] >= 1000000007) sum[cur] -= 1000000007; n       } n       cur ^= 1; n     } n     int ans = 0; n     for (i = 0; i < n; i++) n       if (strncmp(start + i, end, n) == 0) n         ans += dp[cur ^ 1][i], ans %= 1000000007; n     printf( %d n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n char ans[1010][1010]; n int len[1010]; n void upd(int n, int c) { ans[n][len[n]++] = c; } n char all[1 << 21]; n int main() { n   int n, k, l; n   scanf( %d%d%d , &n, &l, &k); n   scanf( %s , all); n   sort(all, all + strlen(all)); n   int cur = 0; n   int beg = 1; n   while (beg < k) { n     int beg_ = beg; n     while (beg <= k) { n       upd(beg, all[cur]); n       beg++; n       cur++; n     } n     beg = beg_; n     for (int i = k; i >= beg_; i--) n       if (ans[i][len[i] - 1] != ans[k][len[k] - 1]) { n         beg = i + 1; n         break; n       } n     if (len[k] == l) break; n   } n   while (len[k] < l) upd(k, all[cur++]); n   for (int i = 1; i <= n; i++) { n     while (len[i] < l) upd(i, all[cur++]); n   } n   for (int i = 1; i <= n; i++) puts(ans[i]); n } n


#include<bits/stdc++.h> n using namespace std; n #define  FIO        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); n #define  all(v)     v.begin(),v.end() n #define  rall(v)    v.rbegin(),v.rend() n #define MEM(a,x)    memset(a,x,sizeof(a)) n #define pi          acos(-1.0) n #define  ll     long long n #define  pb     push_back n #define  mp     make_pair n #define  pii    pair< int,int > n #define  pll    pair< ll,ll > n #define  vii    vector< int > n #define  vll    vector< ll > n #define  vpi    vector< pii > n #define  MX     200005 n #define  EPS    1e-12 n #define  ss     second n #define  ff     first n int cases=1; n #ifdef ppqq n      #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__) n     template < typename Arg1 > n     void __f(const char* name, Arg1&& arg1){ n         cerr << name <<   is   << arg1 << std::endl; n     } n     template < typename Arg1, typename... Args> n     void __f(const char* names, Arg1&& arg1, Args&&... args){ n         const char* comma = strchr(names+1,  , ); n         cerr.write(names, comma - names) <<   is   << arg1 <<    ; n         __f(comma+1, args...); n     } n #else n     #define debug(...) n #endif n ///******************************************START****************************************** n struct info n { n     ll u,v; n     ll w; n  n }; n vector<info> graph; n ll par[MX],cnt,rnk[MX]; n bool cmp(const info&a,const info&b) n { n     if(a.w<b.w) return true; n     return false; n } n ll find_set(ll v) n { n     if(v==par[v]) return v; n     return par[v]=find_set(par[v]); n } n void mrg(ll a,ll b) n { n     if(rnk[a]<=rnk[b]) n     { n         par[a]=b; n         rnk[b]++; n     } n     else n     { n         par[b]=a; n         rnk[a]++; n     } n } n ll MST(ll n) n { n     ll sum=0; n     for(ll i=0;i<graph.size();i++) n     { n         ll a=find_set(graph[i].u); n         ll b=find_set(graph[i].v); n         if(a!=b) n         { n             mrg(a,b); n             cnt++; n             sum+=graph[i].w; n         } n         if(cnt==n-1) break; n     } n     return sum; n } n bool tree(ll n) n { n     for(ll i=1;i<n;i++) n     { n         if(find_set(i)!=find_set(i+1)) return false; n     } n     return true; n } n void CLR(ll n) n { n     graph.clear(); n     cnt=0; n     for(ll i=1;i<=n;i++) par[i]=0,rnk[i]=0; n } n int main() n { n     FIO; n     int t; n     cin>>t; n     while(t--) { n         ll n,m; n         ll k,mini=LLONG_MAX; n         cin>>n>>m>>k; n         vector<pair<ll,pll>> kom,shb; n         for(ll i=0;i<m;i++) n         { n             ll u,v; n             ll w; n             cin>>u>>v>>w; n             info temp; n             temp.u=u; n             temp.v=v; n             temp.w=w; n             if(w<=k) graph.pb(temp); n             shb.pb({w,{u,v}}); n         } n         sort(all(shb)); n         sort(all(graph),cmp); n         for (ll i = 1; i <= n; i++) par[i] = i; n         MST(n); n  n         if(tree(n)) n         { n             ll ans=LLONG_MAX; n             for(auto it: shb) ans=min(ans,abs(it.ff-k)); n             cout<<ans<<  n ; n         } n         else n         { n             for (ll i = 1; i <= n; i++) par[i] = i; n             for(ll i=0;i<graph.size();i++) n                 graph[i].w=0; n             cnt=0; n             for(auto it: shb) n             { n                 if(it.ff>k){ n                     info temp; n                     temp.u=it.ss.ff;temp.v=it.ss.ss; n                     temp.w=it.ff-k; n                     graph.pb(temp); n                 } n             } n             sort(all(graph),cmp); n             cout<<MST(n)<<  n ; n         } n  n         CLR(n+2); n     } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int n, m, chk[100100], a[100100], b[100100]; n vector<int> v[100100]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     int a, b; n     cin >> a >> b; n     v[a].push_back(b), v[b].push_back(a); n   } n   for (int i = 1; i <= n; i++) n     if (v[i].size() != n - 1) { n       chk[i] = 1; n       for (int x : v[i]) chk[x] = 1; n       int j = 1, c = 0; n       while (chk[j]) j++; n       for (int k = 1; k <= n; k++) n         if (k != i && k != j) a[k] = b[k] = ++c; n       a[i] = b[i] = b[j] = ++c; n       a[j] = ++c; n       cout <<  YES n ; n       for (int i = 1; i <= n; i++) cout << a[i] <<    ; n       cout <<   n ; n       for (int i = 1; i <= n; i++) cout << b[i] <<    ; n       cout <<   n ; n       return 0; n     } n   cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string org; n   cin >> org; n   int n; n   cin >> n; n   map<string, int> mp; n   for (int i = 0; i < n; i++) { n     string s, t; n     cin >> s >> t; n     string name; n     string trash; n     if (t[0] ==  p  || t[0] ==  c ) { n       string trash; n       cin >> trash; n       cin >> name >> trash; n       name.pop_back(); n       name.pop_back(); n       if (t[0] ==  p ) { n         if (s == org) n           mp[name] += 15; n         else if (name == org) n           mp[s] += 15; n       } else { n         if (s == org) n           mp[name] += 10; n         else if (name == org) n           mp[s] += 10; n       } n     } else { n       cin >> name >> trash; n       name.pop_back(); n       name.pop_back(); n       if (s == org) n         mp[name] += 5; n       else if (name == org) n         mp[s] += 5; n     } n     mp[s]; n     mp[name]; n   } n   vector<pair<string, int>> v; n   for (auto x : mp) { n     if (x.first == org) continue; n     v.emplace_back(x.first, x.second); n   } n   sort(v.begin(), v.end(), [](pair<string, int> p1, pair<string, int> p2) { n     if (p1.second != p2.second) return p1.second > p2.second; n     return p1.first < p2.first; n   }); n   for (auto x : v) { n     cout << x.first <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, mid; n   bool flag; n   string str; n   char ans[3000]; n   cin >> n; n   cin >> str; n   if (n % 2 == 0) { n     mid = (n / 2) - 1; n     flag = true; n   } else { n     mid = (n - 1) / 2; n     flag = false; n   } n   long long k = 1, cnt = 0; n   ans[mid] = str[0]; n   for (long long i = 1; i < str.size(); i++) { n     cnt++; n     if (flag) { n       ans[mid + k] = str[i]; n     } else { n       ans[mid - k] = str[i]; n     } n     if (cnt == 2) { n       k++; n       cnt = 0; n     } n     flag = !flag; n   } n   for (long long i = 0; i < n; i++) { n     cout << ans[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n int N, dp[5000][5001], cumlcp[5000][5000]; n vector<int> sa, ra, rsa, lcp; n string s; n void saInit() { n   int l = (int)s.size(); n   sa.resize(l); n   iota(sa.begin(), sa.end(), 0); n   ra.assign(s.begin(), s.end()); n   for (int k = 1; k < l; k *= 2) { n     sort(sa.begin(), sa.end(), [&](int a, int b) { n       if (ra[a] != ra[b]) return ra[a] < ra[b]; n       int ak = a + k < l ? ra[a + k] : -1; n       int bk = b + k < l ? ra[b + k] : -1; n       return ak < bk; n     }); n     vector<int> ra2(l); n     int x = 0; n     for (int i = 1; i < l; i++) { n       if (ra[sa[i]] != ra[sa[i - 1]] || sa[i - 1] + k >= l || n           ra[sa[i] + k] != ra[sa[i - 1] + k]) n         x++; n       ra2[sa[i]] = x; n     } n     ra = ra2; n   } n   rsa.resize(l); n   for (int i = 0; i < int(l); i++) rsa[sa[i]] = i; n } n void saLCP() { n   int l = (int)s.size(); n   lcp.resize(l); n   vector<int> p(l); n   for (int i = 0; i < l; i++) { n     p[sa[i]] = (i) ? sa[i - 1] : -1; n   } n   int x = 0; n   for (int i = 0; i < l; i++) { n     while (p[i] != -1 && p[i] + x < l && s[i + x] == s[p[i] + x] && n            s[i + x] !=  $ ) n       x++; n     lcp[rsa[i]] = x; n     if (x) x--; n   } n   for (int i = 0; i < int(l); i++) { n     cumlcp[i][i] = INT_MAX; n     for (int j = i + 1; j < int(l); j++) n       cumlcp[i][j] = min(cumlcp[i][j - 1], lcp[j]); n   } n } n bool ism(int a, int b, int n) { n   int as = a - n + 1, bs = b - n + 1; n   if (as < 0) return false; n   if (rsa[bs] <= rsa[as]) return false; n   return cumlcp[rsa[as]][rsa[bs]] < n; n } n int main() { n   cin >> N >> s; n   saInit(); n   saLCP(); n   for (int i = 0; i < int(N); i++) n     for (int j = 1; j < int(5001); j++) { n       int& ans = dp[i][j]; n       ans = dp[i][j - 1]; n       if (j > i + 1) { n         continue; n       } else if (s[i - j + 1] ==  0 ) { n       } else if (j == i + 1) { n         ans = (ans + 1) % MOD; n       } else if (ism(i - j, i, j)) { n         ans = (ans + dp[i - j][j]) % MOD; n       } else { n         ans = (ans + dp[i - j][j - 1]) % MOD; n       } n     } n   cout << dp[N - 1][N] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long int n, m; n int main() { n   cin >> n; n   m = (((n - 3) * 3) + ((n - 3) * (n - 4))) + 1; n   cout << m << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 8e5 + 5; n struct trie { n   int a[26]; n   vector<int> val[2]; n } a[N]; n int vis[N][2]; n char ch[800005]; n int cnt, n; n void insert(char ch[], int op, int k) { n   int u = 0; n   int len = strlen(ch + 1); n   a[u].val[op].push_back(k); n   for (int i = 1; i <= len; i++) { n     if (!a[u].a[ch[i] -  a ]) a[u].a[ch[i] -  a ] = ++cnt; n     u = a[u].a[ch[i] -  a ]; n     a[u].val[op].push_back(k); n   } n } n int dfs(int k, int dep) { n   int ans = 0; n   for (int i = 0; i < 26; i++) n     if (a[k].a[i]) ans += dfs(a[k].a[i], dep + 1); n   vector<int> t[2]; n   for (int i = 0; i <= 1; i++) n     for (int j = 0; j < a[k].val[i].size(); j++) n       if (!vis[a[k].val[i][j]][i]) t[i].push_back(a[k].val[i][j]); n   int tmp = min(t[0].size(), t[1].size()); n   ans += tmp * dep; n   for (int i = 0; i < tmp; i++) { n     vis[t[0][i]][0] = t[1][i]; n     vis[t[1][i]][1] = t[0][i]; n   } n   return ans; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %s , ch + 1); n     insert(ch, 0, i); n   } n   for (int i = 1; i <= n; i++) { n     scanf( %s , ch + 1); n     insert(ch, 1, i); n   } n   printf( %d n , dfs(0, 0)); n   for (int i = 1; i <= n; i++) printf( %d %d n , i, vis[i][0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1e6 + 50; n const long long inf = 0x3f3f3f3f3f3f3f3f; n const long long mod = 1e9 + 7; n const long double eps = 1e-20; n const long long HASH = 131; n using namespace std; n long long a[maxn]; n signed main() { n   std::ios::sync_with_stdio(false); n   long long t; n   cin >> t; n   while (t--) { n     long long a, b; n     cin >> a >> b; n     if ((a & 1)) { n       long long tmp = b % 4; n       if (tmp == 1) { n         cout << a + b << endl; n       } else if (tmp == 2) { n         cout << a - 1 << endl; n       } else if (tmp == 3) { n         cout << a - 1 - b << endl; n       } else n         cout << a << endl; n     } else { n       long long tmp = b % 4; n       if (tmp == 1) { n         cout << a - b << endl; n       } else if (tmp == 2) { n         cout << a + 1 << endl; n       } else if (tmp == 3) { n         cout << a + 1 + b << endl; n       } else n         cout << a << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n using ii = pair<long long, long long>; n using ll = long long; n using ull = unsigned long long; n using ld = long double; n using vec = vector<long long>; n using vecc = vector<vector<long long> >; n namespace royyaha { n template <typename T> n void pr(const T& x) { n   cout << x; n } n template <typename T1, typename T2> n void pr(const pair<T1, T2>& x) { n   cout << x.first <<     << x.second; n } n template <typename T, typename... Args> n void pr(const T& x, const Args&... rest) { n   pr(x); n   pr(rest...); n } n template <typename T> n void ou(const T& x) { n   pr(x); n   cout << endl; n } n template <typename T, typename... Args> n void ou(const T& x, const Args&... rest) { n   pr(x,    ); n   ou(rest...); n } n template <typename T> n void ou(const vector<T>& v, long long n = 0) { n   n = (n ? n : v.size()); n   for (auto it = v.begin(); it != v.begin() + n; ++it) { n     if (it + 1 == v.end()) n       ou(*it); n     else n       pr(*it,    ); n   } n } n }  // namespace royyaha n using namespace royyaha; n bool intersection(ii a, ii b) { n   return !(a.first > b.second || a.second < b.first); n } n const long long maxn = 2e5 + 50; n vec depth(maxn); n vec son(maxn); n vec e[maxn]; n void dfs(long long u, long long pre) { n   depth[u] = depth[pre] + 1; n   son[u] = 1; n   for (auto v : e[u]) n     if (v != pre) { n       dfs(v, u); n       son[u] += son[v]; n     } n } n signed main() { n   long long n, k; n   cin >> n >> k; n   for (long long i = 0; i < n - 1; ++i) { n     long long u, v; n     cin >> u >> v; n     u--, v--; n     e[u].push_back(v); n     e[v].push_back(u); n   } n   dfs(0, 0); n   vec f(n); n   for (long long i = 0; i < n; ++i) f[i] = depth[i] - son[i]; n   sort(f.begin(), f.end()); n   reverse(f.begin(), f.end()); n   cout << accumulate(f.begin(), f.begin() + k, 0ll) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void AWAD() { n   std::ios::sync_with_stdio(false); n   cin.tie(0); n } n bool cmp(long long int a, long long int b) { n   if (a > b) return true; n   return false; n } n int a[1000001], b[1000001]; n int main() { n   AWAD(); n   string input; n   long long int c4 = 0, c7 = 0; n   cin >> input; n   for (int i = 0; i < input.size(); i++) n     if (input[i] ==  4 ) n       ++c4; n     else if (input[i] ==  7 ) n       ++c7; n   if (!c4 && !c7) n     cout <<  -1 n ; n   else if (c4 >= c7) n     cout << 4 << endl; n   else n     cout << 7 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char c; n int mo = 1000003; n int main() { n   int pre = 0; n   while (scanf( %c , &c) != EOF) { n     if (c < 32 || c > 126) break; n     int w = c; n     int tmp = 0; n     for (int j = 0; j < 8; j++) { n       tmp = tmp * 2 + w % 2; n       w /= 2; n     } n     w = tmp; n     tmp = pre - tmp; n     pre = w; n     if (tmp < 0) tmp += 256; n     printf( %d n , tmp); n   } n   return 0; n } n


#include <bits/stdc++.h> n int A, B, M = 6, N; n int main() { n   scanf( %d%d%d , &N, &A, &B); n   int nda = N / A; n   for (int i = 1; (i <= 2) && (B * i <= N); ++i) n     M = std::min((i ^ 3) + (4 - ((N - B * i) / A) * (i ^ 3) + nda - 1) / nda, n                  M); n   printf( %d n , M); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000009; n const int SGZ = 26; n const int M = 10009; n int n, m, nxt[N][SGZ], crt[N][SGZ], q[M], ans[M]; n vector<int> val; n char s[N], qy[SGZ]; n inline int read() { n   char c = getchar(); n   int ret = 0, f = 1; n   while (c <  0  || c >  9 ) { n     f = c ==  -  ? -1 : 1; n     c = getchar(); n   } n   while ( 0  <= c && c <=  9 ) { n     ret = ret * 10 + c -  0 ; n     c = getchar(); n   } n   return ret * f; n } n inline int index(int x) { n   for (int l = 0, r = val.size() - 1, mid; l <= r;) { n     mid = l + r >> 1; n     if (val[mid] == x) { n       return mid; n     } else if (val[mid] < x) { n       l = mid + 1; n     } else { n       r = mid - 1; n     } n   } n   return -1; n } n int main() { n   scanf( %s , s); n   n = strlen(s); n   m = read(); n   for (int i = 1; i <= m; i++) { n     scanf( %s , qy); n     int len = strlen(qy); n     for (int j = 0; j < len; j++) { n       q[i] |= 1 << qy[j] -  a ; n     } n     val.push_back(q[i]); n   } n   sort(val.begin(), val.end()); n   val.resize(unique(val.begin(), val.end()) - val.begin()); n   int last_position[SGZ]; n   fill(last_position, last_position + SGZ, n); n   for (int i = n - 1; ~i; i--) { n     last_position[s[i] -  a ] = i; n     for (int j = 0; j < SGZ; j++) { n       nxt[i][j] = last_position[j]; n     } n   } n   memset(crt, -1, sizeof(crt)); n   for (int i = 0; i < n; i++) { n     for (int j = 0, p = i, sta = 0; j < 26 && p < n; j++) { n       int np = n, c = -1; n       for (int k = 0; k < 26; k++) { n         if ((sta >> k & 1) == 0) { n           if (np > nxt[p][k]) { n             c = k; n             np = nxt[p][k]; n           } n         } n       } n       if (~c) { n         sta |= 1 << c; n         p = np; n         crt[i][j] = sta; n         if (!i || crt[i - 1][j] != sta) { n           int idx = index(sta); n           if (~idx) { n             ans[idx]++; n           } n         } n       } n     } n   } n   for (int i = 1; i <= m; i++) { n     printf( %d n , ans[index(q[i])]); n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long n; n   long m; n   std::cin >> n >> m; n   long a = 0, b = 0, c = 0, d = 0; n   while (!(n % 2)) { n     n = n / 2; n     a++; n   } n   while (!(n % 3)) { n     n = n / 3; n     b++; n   } n   while (!(m % 2)) { n     m = m / 2; n     c++; n   } n   while (!(m % 3)) { n     m = m / 3; n     d++; n   } n   if ((n != m) || (a > c) || (b > d)) { n     std::cout << -1; n   } else { n     std::cout << (c + d) - (a + b); n   } 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   long long n, k, x, s = 0; n   cin >> n >> k; n   map<long long, long long> m; n   queue<long long> q; n   for (long long i = 0; 0 < n ? i < n : i > n; 0 < n ? i += 1 : i -= 1) { n     cin >> x; n     q.push(x); n     m[x] = 0; n   } n   while (1) { n     x = q.front(); n     q.pop(); n     if (!m.count(x + 1)) { n       q.push(x + 1); n       m[x + 1] = m[x] + 1; n     } n     if (m.size() >= n + k) break; n     if (!m.count(x - 1)) { n       q.push(x - 1); n       m[x - 1] = m[x] + 1; n     } n     if (m.size() >= n + k) break; n   } n   for (auto i : m) s += i.second; n   cout << s <<   n ; n   for (auto i : m) n     if (i.second != 0) cout << i.first <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n char s[2000006]; n char t[1000006]; n int len[100005], pos[100005]; n vector<pair<int, int>> e; n int main() { n   int n; n   scanf( %d , &n); n   int tp = 0; n   for (int i = 0; i < n; i++) { n     scanf( %s , t + tp); n     len[i] = strlen(t + tp); n     pos[i] = tp; n     tp += len[i]; n     int ki; n     scanf( %d , &ki); n     while (ki--) { n       int xij; n       scanf( %d , &xij); n       e.push_back({xij - 1, i}); n     } n   } n   int p = 0; n   sort(e.begin(), e.end()); n   int esz = (int)e.size(); n   tp = -1; n   int te = -1, sp = 0; n   while (p < esz) { n     int f = e[p].first; n     while (sp < f) { n       if (tp != -1) { n         s[sp++] = t[tp++]; n         if (tp >= te) tp = te = -1; n       } else n         s[sp++] =  a ; n     } n     if (tp == -1) { n       tp = pos[e[p].second]; n       te = pos[e[p].second] + len[e[p].second]; n     } else if (len[e[p].second] > te - tp) { n       tp = pos[e[p].second]; n       te = pos[e[p].second] + len[e[p].second]; n     } n     while (p + 1 < esz && e[p + 1].first == e[p].first) { n       p++; n       if (len[e[p].second] > te - tp) { n         tp = pos[e[p].second]; n         te = pos[e[p].second] + len[e[p].second]; n       } n     } n     p++; n   } n   while (tp < te) s[sp++] = t[tp++]; n   s[sp] =   0 ; n   printf( %s , s); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e3 + 5; n const int mod = 998244353; n inline long long fpm(long long a, long long p) { n   long long r = 1; n   while (p) { n     if (p & 1) r = r * a % mod; n     a = a * a % mod; n     p /= 2; n   } n   return r; n } n int k, n, m; n int s[maxn][maxn]; n inline void init() { n   s[0][0] = 1; n   for (int i = 1; i <= k; i++) { n     for (int j = 1; j <= i; j++) { n       s[i][j] = (s[i - 1][j - 1] + 1ll * j * s[i - 1][j] % mod) % mod; n     } n   } n } n int main() { n   cin >> n >> m >> k; n   init(); n   long long ans = 0, cur = 1; n   long long invm = fpm(m, mod - 2); n   for (int i = 0; i <= k; i++) { n     ans = (ans + 1ll * cur * s[k][i] % mod * fpm(invm, i) % mod) % mod; n     cur = 1ll * cur * (n - i) % mod; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n using vint = vector<long long>; n using pint = pair<long long, long long>; n using vpint = vector<pint>; n template <typename A, typename B> n inline void chmin(A& a, B b) { n   if (a > b) a = b; n } n template <typename A, typename B> n inline void chmax(A& a, B b) { n   if (a < b) a = b; n } n template <class A, class B> n ostream& operator<<(ostream& ost, const pair<A, B>& p) { n   ost <<  {  << p.first <<  ,  << p.second <<  } ; n   return ost; n } n template <class T> n ostream& operator<<(ostream& ost, const vector<T>& v) { n   ost <<  { ; n   for (long long i = 0; i < v.size(); i++) { n     if (i) ost <<  , ; n     ost << v[i]; n   } n   ost <<  } ; n   return ost; n } n inline long long topbit(unsigned long long x) { n   return x ? 63 - __builtin_clzll(x) : -1; n } n inline long long popcount(unsigned long long x) { n   return __builtin_popcountll(x); n } n inline long long parity(unsigned long long x) { return __builtin_parity(x); } n signed main() { n   long long N, K; n   cin >> N >> K; n   if (K > 1000000 || K * (K + 1) / 2 > N) { n     cout << -1 << endl; n     return 0; n   } n   long long s = K * (K + 1) / 2; n   long long G = 0; n   for (long long g = 1; g * g <= N; g++) { n     if (N % g) continue; n     if (N / s >= g) chmax(G, g); n     if (N / s >= N / g) chmax(G, N / g); n   } n   vint ans(K); n   iota((ans).begin(), (ans).end(), 1); n   ans[K - 1] += N / G - s; n   for (long long i = 0; i < (K); i++) ans[i] *= G; n   for (long long i = 0; i < (K); i++) { n     if (i) cout <<    ; n     cout << ans[i]; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[100002]; n int mini[100002][21]; n vector<pair<int, int> > vp; n int f(int l, int r, int m) { n   if (l == r) { n     for (int i = 0; i < a[l] - m; i++) { n       vp.push_back(make_pair(l, r)); n     } n     return a[l] - m; n   } n   int i = 0; n   for (i = 0;; i++) n     if (l + (1 << i) > r) break; n   int minIndex = -1; n   if (a[mini[l][i - 1]] < a[mini[r - (1 << (i - 1)) + 1][i - 1]]) { n     minIndex = mini[l][i - 1]; n   } else { n     minIndex = mini[r - (1 << (i - 1)) + 1][i - 1]; n   } n   for (int i = 0; i < a[minIndex] - m; i++) { n     vp.push_back(make_pair(l, r)); n   } n   if (minIndex == l) { n     return a[minIndex] - m + f(l + 1, r, a[minIndex]); n   } n   if (minIndex == r) { n     return a[minIndex] - m + f(l, r - 1, a[minIndex]); n   } else { n     return a[minIndex] - m + f(l, minIndex - 1, a[minIndex]) + n            f(minIndex + 1, r, a[minIndex]); n   } n } n int main() { n   cout << setprecision(9); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) mini[i][0] = i; n   for (int i = 1; i < 21; i++) { n     for (int j = 0; j + (1 << i) - 1 < n; j++) { n       if (a[mini[j][i - 1]] < a[mini[j + (1 << (i - 1))][i - 1]]) { n         mini[j][i] = mini[j][i - 1]; n       } else { n         mini[j][i] = mini[j + (1 << (i - 1))][i - 1]; n       } n     } n   } n   cout << f(0, n - 1, 0) << endl; n   for (int i = 0; i < vp.size(); i++) n     cout << 1 + vp[i].first <<     << 1 + vp[i].second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct custom_hash { n   static uint64_t splitmix64(uint64_t x) { n     x += 0x9e3779b97f4a7c15; n     x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; n     x = (x ^ (x >> 27)) * 0x94d049bb133111eb; n     return x ^ (x >> 31); n   } n   size_t operator()(uint64_t x) const { n     static const uint64_t FIXED_RANDOM = n         chrono::steady_clock::now().time_since_epoch().count(); n     return splitmix64(x + FIXED_RANDOM); n   } n }; n const int maxn = 2e5 + 5; n const long long mod = 1e9 + 7; n long long gcd(long long a, long long b) { n   if (!b) return a; n   return gcd(b, a % b); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   long long maxl = 1e18 + 2; n   for (int i = 0; i < n; i++) { n     long long p, q, b; n     cin >> p >> q >> b; n     if (p % q == 0) { n       cout <<  Finite  << endl; n       continue; n     } n     long long g = gcd(p, q); n     p /= g; n     q /= g; n     while (true) { n       if (q == 1) { n         cout <<  Finite  << endl; n         break; n       } n       g = gcd(b, q); n       if (g == 1) { n         cout <<  Infinite  << endl; n         break; n       } n       q /= g; n       b = g; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n; n   for (int k = 0; k < n; k++) { n     cin >> m; n     for (int i = 0; i < m / 2; i++) { n       int fir, sec; n       cin >> fir >> sec; n       if (fir > 0 && sec > 0) { n         cout << sec <<     << -fir <<    ; n       } else if (fir < 0 && sec < 0) { n         cout << -sec <<     << fir <<    ; n       } else if (fir > 0 && sec < 0) { n         cout << -sec <<     << fir <<    ; n       } else if (fir < 0 && sec > 0) { n         cout << sec <<     << -fir <<    ; n       } n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n int n; n char s[N], t[N]; n bitset<N> c[30], ans; n int main() { n   scanf( %s%d , s + 1, &n); n   for (int i = 1, _ = strlen(s + 1); i <= _; i++) c[s[i] -  a ][i] = 1; n   int op, x, y; n   while (n--) { n     scanf( %d , &op); n     if (op == 1) { n       scanf( %d%s , &x, t + 1); n       c[s[x] -  a ].reset(x); n       s[x] = t[1]; n       c[s[x] -  a ].set(x); n     } else { n       scanf( %d%d%s , &x, &y, t + 1); n       int len = strlen(t + 1); n       ans.set(); n       y = y - len + 1; n       for (int i = 1; i <= len; i++) ans &= c[t[i] -  a ] >> (i - 1); n       printf( %d n , n              max(0, (int)((ans >> x).count() - (ans >> (y + 1)).count()))); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 1000005; n long long pw(long long b, long long r, long long md) { n   long long ans = 1; n   while (r) { n     if (r & 1) ans = (ans * b) % md; n     b = (b * b) % md; n     r >>= 1; n   } n   return ans; n } n bool cmp(pair<long long, long long> a, pair<long long, long long> b) { n   if (a.first == b.first) return a.second < b.second; n   return a.first > b.first; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, m, k; n   cin >> n >> m >> k; n   long long dp[n][k + 1]; n   for (long long i = 0; i <= n - 1; ++i) n     for (long long j = 0; j <= k; ++j) dp[i][j] = -1e18; n   long long arr[n]; n   for (long long i = 0; i <= n - 1; ++i) cin >> arr[i]; n   long long pref[n]; n   pref[0] = arr[0]; n   for (long long i = 1; i <= n - 1; ++i) pref[i] = pref[i - 1] + arr[i]; n   for (long long i = 0; i <= n - 1; ++i) dp[i][0] = 0; n   dp[m - 1][1] = pref[m - 1]; n   long long ans = 0; n   for (long long i = m; i <= n - 1; ++i) { n     for (long long j = 0; j <= k; ++j) { n       dp[i][j] = dp[i - 1][j]; n       if (j == 0) continue; n       dp[i][j] = max(dp[i][j], dp[i - m][j - 1] + pref[i] - pref[i - m]); n     } n   } n   for (long long i = 0; i <= n - 1; ++i) ans = max(ans, dp[i][k]); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int x[200005], y[200005], z[200005]; n long long a[200005], b[200005]; n int main() { n   long long n, t; n   cin >> n >> t; n   x[0] = 0; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     y[i] = 0; n   } n   for (int i = 1; i <= n; i++) { n     cin >> x[i]; n     y[x[i]]++; n     z[x[i]] = i; n   } n   for (int i = 1; i <= n; i++) { n     if ((x[i] < i) || (x[i] < x[i - 1])) { n       cout <<  No  << endl; n       return 0; n     } n     if (y[i] > 0 && z[i] != i) { n       cout <<  No  << endl; n       return 0; n     } n   } n   if (x[n] != n) { n     cout <<  No  << endl; n     return 0; n   } n   for (int i = 1; i < n; i++) n     if ((a[i + 1] == (a[i] + 1)) && x[i + 1] != x[i] && y[x[i]] > 1) { n       cout <<  No  << endl; n       return 0; n     } n   for (int i = 1; i <= n; i++) { n     int m = z[i]; n     if (y[i] == 1) n       b[m] = a[m] + t; n     else { n       for (int j = 0; j < y[i]; j++) b[m - j] = a[m] + t + 1LL - j; n     } n   } n   cout <<  Yes  << endl; n   for (int i = 1; i <= n; i++) cout << b[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ull = unsigned long long; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n template <class T> n inline bool Min(T &a, T b) { n   return a > b ? (a = b, true) : false; n } n template <class T> n inline bool Max(T &a, T b) { n   return a < b ? (a = b, true) : false; n } n inline int ni() { n   int a; n   scanf( %d , &a); n   return a; n } n inline ll nl() { n   ll a; n   scanf( %lld , &a); n   return a; n } n inline double nd() { n   double a; n   scanf( %lf , &a); n   return a; n } n inline void pi(int a) { printf( %d  , a); } n inline void pl(ll a) { printf( %lld  , a); } n inline void pd(double a) { printf( %.12lf  , a); } n const int N = 1e5 + 10; n using ld = long double; n void solve() { n   ld x, y, z; n   cin >> x >> y >> z; n   ld ans[10]; n   ans[1] = log(x) * pow(y, z); n   ans[2] = log(x) * pow(z, y); n   ans[3] = log(x) * y * z; n   ans[4] = log(y) * pow(x, z); n   ans[5] = log(y) * pow(z, x); n   ans[6] = log(y) * x * z; n   ans[7] = log(z) * pow(x, y); n   ans[8] = log(z) * pow(y, x); n   ans[9] = log(z) * x * y; n   ans[0] = ans[1]; n   int t = 1; n   for (int i = 1; i <= 9; i++) n     if (Max(ans[0], ans[i])) t = i; n   if (t == 1) puts( x^y^z ); n   if (t == 2) puts( x^z^y ); n   if (t == 3) puts( (x^y)^z ); n   if (t == 4) puts( y^x^z ); n   if (t == 5) puts( y^z^x ); n   if (t == 6) puts( (y^x)^z ); n   if (t == 7) puts( z^x^y ); n   if (t == 8) puts( z^y^x ); n   if (t == 9) puts( (z^x)^y ); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int powm[100005]; n int fact[100005]; n int sum[100005]; n string s; n void powers_of_10(int m) { n   powm[0] = 1; n   for (int i = 1; i <= m; i++) powm[i] = 1LL * powm[i - 1] * 10 % 1000000007; n } n void factorials(int m) { n   fact[0] = 1; n   for (int i = 1; i <= m; i++) fact[i] = 1LL * i * fact[i - 1] % 1000000007; n } n void sums(int m) { n   sum[0] = s[0] -  0 ; n   for (int i = 1; i < m; i++) { n     sum[i] = sum[i - 1] + (s[i] -  0 ); n     if (sum[i] >= 1000000007) sum[i] -= 1000000007; n   } n } n void euclid(int a, int b, long long& x, long long& y) { n   if (b == 0) { n     x = 1; n     y = 0; n   } else { n     long long x0, y0; n     euclid(b, a % b, x0, y0); n     x = y0; n     y = x0 - (a / b) * y0; n   } n } n int invers_modular(int n) { n   long long x, y; n   euclid(n, 1000000007, x, y); n   while (x < 0) x += 1000000007; n   return (int)x; n } n int comb(int n, int k) { n   if (k > n) return 0; n   int Ret = fact[n]; n   Ret = 1LL * Ret * invers_modular(fact[n - k]) % 1000000007; n   Ret = 1LL * Ret * invers_modular(fact[k]) % 1000000007; n   return Ret; n } n int main() { n   ios_base::sync_with_stdio(false); n   int n, k; n   cin >> n >> k; n   cin >> s; n   powers_of_10(n); n   factorials(n); n   sums(n); n   int FirstSum = 0; n   int SecondSum = 0; n   for (int i = 0; i < n; i++) { n     FirstSum += 1LL * (s[i] -  0 ) * powm[n - 1 - i] * comb(i, k) % 1000000007; n     if (FirstSum >= 1000000007) FirstSum -= 1000000007; n   } n   for (int i = 0; i <= n - 2; i++) { n     SecondSum += 1LL * (1LL * sum[n - i - 2] * powm[i] % 1000000007) * n                  comb(n - i - 2, k - 1) % 1000000007; n     if (SecondSum >= 1000000007) SecondSum -= 1000000007; n   } n   cout << (SecondSum + FirstSum) % 1000000007; n } n


#include <bits/stdc++.h> n using namespace std; n inline char gc() { n   static char buf[100000], *p1 = buf, *p2 = buf; n   return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) n              ? EOF n              : *p1++; n } n inline long long read() { n   long long x = 0; n   char ch = getchar(); n   bool positive = 1; n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) positive = 0; n   for (; isdigit(ch); ch = getchar()) x = x * 10 + ch -  0 ; n   return positive ? x : -x; n } n inline void write(long long a) { n   if (a < 0) { n     a = -a; n     putchar( - ); n   } n   if (a >= 10) write(a / 10); n   putchar( 0  + a % 10); n } n inline void writeln(long long a) { n   write(a); n   puts(  ); n } n inline void wri(long long a) { n   write(a); n   putchar(   ); n } n long long t, l, r; n long long dp[35][2][2]; n long long calc(long long x, long long y) { n   if (x < 0 || y < 0) return 0; n   memset(dp, 0, sizeof(dp)); n   dp[32][1][1] = 1; n   for (long long i = 31; i >= 0; i--) { n     for (int j = 0; j <= 1; j++) { n       for (int k = 0; k <= 1; k++) { n         long long nextj = (j && !((x >> i) & 1)); n         long long nextk = (k && !((y >> i) & 1)); n         dp[i][nextj][nextk] += dp[i + 1][j][k]; n         if (!j || ((x >> i) & 1)) dp[i][j][nextk] += dp[i + 1][j][k]; n         if (!k || ((y >> i) & 1)) dp[i][nextj][k] += dp[i + 1][j][k]; n       } n     } n   } n   return dp[0][0][0] + dp[0][0][1] + dp[0][1][0] + dp[0][1][1]; n } n int main() { n   t = read(); n   while (t--) { n     l = read(), r = read(); n     writeln(calc(r, r) - 2 * calc(l - 1, r) + calc(l - 1, l - 1)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n static inline void WRITE(int a) { printf( %d n , a); } n static inline void WRITE2(int a, int b) { printf( %d %d n , a, b); } n const int S9 = 1000000007, S6 = 1000007, S5 = 100007, S4 = 10007, S3 = 1007, n           S2 = 107; n const double E2 = 0.01, E4 = 0.0001, E6 = 0.000001, E8 = 0.00000001; n int tab[4][2]; n int check() { n   bool czy = false; n   int ob = 0, at = 1; n   if (tab[ob][0] > tab[2][1] && tab[at][1] > tab[3][0]) n     if (tab[ob][0] > tab[3][1] && tab[at][1] > tab[2][0]) return 1; n   if ((tab[ob][0] < tab[2][1] && tab[at][1] < tab[3][0]) || n       (tab[ob][0] < tab[3][1] && tab[at][1] < tab[2][0])) n     czy = true; n   ob = 1; n   at = 0; n   if (tab[ob][0] > tab[2][1] && tab[at][1] > tab[3][0]) n     if (tab[ob][0] > tab[3][1] && tab[at][1] > tab[2][0]) return 1; n   if (((tab[ob][0] < tab[2][1] && tab[at][1] < tab[3][0]) || n        (tab[ob][0] < tab[3][1] && tab[at][1] < tab[2][0])) && n       czy) n     return 2; n   return 0; n } n int main() { n   for (int i = 0; i < 4; i++) scanf( %d%d , &tab[i][0], &tab[i][1]); n   int x = check(); n   if (x == 0) n     puts( Draw ); n   else n     printf( Team %d n , x); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = (int)1010; n set<int> ans; n int n, m; n char a[MAXN][MAXN]; n int posk[MAXN]; n int posG[MAXN]; n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> a[i][j]; n       if (a[i][j] ==  S ) { n         posk[i] = j; n       } n       if (a[i][j] ==  G ) { n         posG[i] = j; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     if (posG[i] > posk[i]) { n       cout << -1; n       return 0; n     } else { n       ans.insert((int)posG[i] - posk[i] + 1); n     } n   } n   cout << ans.size(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   bool flag = 0; n   long long sum1 = 0, sum2 = 0; n   void operator+=(const node& rhs) { n     flag |= rhs.flag; n     sum1 += rhs.sum1; n     sum2 = max(sum2, rhs.sum2); n   } n }; n const int N = 200200; n int n, m, s, T; n int par[N], siz[N]; n int tin[N], low[N]; n long long a[N]; n vector<int> edges[N], tree[N]; n vector<pair<int, int>> bridges; n int findSetId(int u) { return (par[u] == u ? u : par[u] = findSetId(par[u])); } n void findBridges(int u = 1, int p = -1) { n   tin[u] = low[u] = ++T; n   for (int v : edges[u]) { n     if (v == p) { n       continue; n     } n     if (tin[v] == 0) { n       findBridges(v, u); n       if (low[v] > tin[u]) { n         bridges.push_back({u, v}); n       } else { n         int x = findSetId(u); n         int y = findSetId(v); n         if (x != y) { n           par[y] = x; n           siz[x] += siz[y]; n           a[x] += a[y]; n         } n       } n     } n     low[u] = min(low[u], low[v]); n   } n } n void buildBridgeTree() { n   for (int i = 1; i <= n; ++i) { n     par[i] = i, siz[i] = 1; n   } n   findBridges(); n   for (auto& b : bridges) { n     int u = findSetId(b.first); n     int v = findSetId(b.second); n     tree[u].push_back(v); n     tree[v].push_back(u); n   } n } n node solve(int u = s, int p = -1) { n   node ret; n   for (int v : tree[u]) { n     if (v == p) { n       continue; n     } n     ret += solve(v, u); n   } n   if (siz[u] > 1 || ret.flag) { n     ret.sum1 += a[u], ret.flag = 1; n   } else { n     ret.sum2 += a[u]; n   } n   return ret; n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) { n     scanf( %lld , a + i); n   } n   while (m--) { n     int u, v; n     scanf( %d %d , &u, &v); n     edges[u].push_back(v); n     edges[v].push_back(u); n   } n   cin >> s; n   buildBridgeTree(); n   auto out = solve(findSetId(s)); n   cout << out.sum1 + out.sum2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long double n; n   cin >> n; n   if (n <= 127) { n     cout <<  byte ; n   } else if (n <= 32767) { n     cout <<  short ; n   } else if (n <= 2147483647) { n     cout <<  int ; n   } else if (n <= 9223372036854775807LL) { n     cout <<  long ; n   } else { n     cout <<  BigInteger ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   std::ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     if (n <= 6) n       cout << 15 << endl; n     else { n       long long int r = n % 6; n       long long int y = ((n - r - 6) / 6) * 15; n       if (r != 0) long long int y = ((n - r - 6) / 6) * 15; n       if (r + 6 <= 6) n         y = y + 15; n       else if (r + 6 <= 8) n         y = y + 20; n       else if (r + 6 <= 10) n         y = y + 25; n       else n         y = y + 30; n       cout << y << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n char a[1005]; n char b[1005]; n int t[1005]; n int sum[1005]; n int main() { n   memset(t, 0, sizeof(t)); n   int p, n, m; n   cin >> n >> m >> p; n   cin >> a >> b; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (a[i + j] != b[j]) n         break; n       else if (j == m - 1) n         t[i] = 1; n     } n   } n   sum[0] = t[0]; n   for (int i = 1; i < n; i++) sum[i] = sum[i - 1] + t[i]; n   while (p--) { n     int l, r; n     scanf( %d%d , &l, &r); n     l--, r--; n     if (n < m || r - l + 1 < m) n       cout << 0 << endl; n     else n       cout << sum[r - m + 1] - sum[l - 1] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200006; n int a[MAXN], b[MAXN]; n int N; n int main() { n   while (scanf( %d , &N) != EOF) { n     int i, j; n     int x; n     int n = 0, m = 0; n     long long suma = 0, sumb = 0; n     int win = 0; n     int last; n     for (i = 1; i <= N; i++) { n       scanf( %d , &x); n       if (x > 0) { n         a[++n] = x; n         suma += x; n         last = 1; n       } else { n         b[++m] = -x; n         sumb += (-x); n         last = 2; n       } n     } n     if (suma > sumb) n       win = 1; n     else if (suma < sumb) n       win = 2; n     else { n       for (i = 1, j = 1; i <= n && j <= m; i++, j++) { n         if (a[i] > b[j]) { n           win = 1; n           break; n         } else if (a[i] < b[j]) { n           win = 2; n           break; n         } n       } n       if (i > n && j > m) n         win = last; n       else if (i > n && j <= m) n         win = 2; n       else if (i <= n && j > m) n         win = 1; n     } n     if (win == 1) n       printf( first n ); n     else if (win == 2) n       printf( second n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const double eps = 1e-6; n const int MAXN = 1e5 + 10; n const int MAXM = 1e6 + 10; n const long long mod = 1e9 + 7; n int a[1010]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   int ans = 0; n   for (int i = 1; i < n - 1; i++) { n     if (a[i] > a[i - 1] && a[i] > a[i + 1]) ans++; n     if (a[i] < a[i - 1] && a[i] < a[i + 1]) ans++; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const double eps = 1e-20; n int main() { n   int n, a, d; n   cin >> n >> a >> d; n   int *t = new int[n]; n   int *v = new int[n]; n   double ans = -1.0; n   for (int i = 0; i < n; i++) cin >> t[i] >> v[i]; n   for (int i = 0; i < n; i++) { n     double t1 = (v[i] + 0.0) / (a + 0.0); n     double t2 = (d - a * t1 * t1 * 0.5) / (v[i] + 0.0); n     if (a * t1 * t1 * 0.5 >= d) { n       t1 = sqrt(2.0 * d / (a + 0.0)); n       t2 = 0; n     } n     double tt = t1 + t2 + t[i]; n     if (tt > ans) { n       printf( %.6f n , tt); n       ans = tt; n     } else n       printf( %.6f n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 110; n const int M = 1010; n double f[N][M * N], sum[N * M]; n int a[N], n, m, s; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , a + i); n     s += a[i]; n   } n   f[0][0] = 1; n   sum[0] = 1.0; n   for (int j = 1; j < s; j++) sum[j] = sum[j - 1] + f[0][j]; n   for (int i = 1; i <= n; i++) { n     for (int j = i; j < s; j++) { n       int l = min(m, j - i + 1); n       int r = max(-1, j - l - 1); n       double tmp = sum[j - 1] - ((r == -1) ? 0 : sum[r]); n       if (l < a[i]) n         f[i][j] = tmp / (m - 1); n       else n         f[i][j] = (tmp - f[i - 1][j - a[i]]) / (m - 1); n     } n     sum[0] = 0; n     for (int j = 1; j < s; j++) { n       sum[j] = sum[j - 1] + f[i][j]; n     } n   } n   double ans = 0; n   for (int i = n; i < s; i++) { n     ans += f[n][i]; n   } n   printf( %.10f n , 1 + ans * (m - 1)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[111], x[111], saven; n long double f[2][100111], sum[2][100111]; n int main() { n   ios ::sync_with_stdio(0); n   cin.tie(0); n   cout << (fixed) << setprecision(12); n   while (cin >> n >> m) { n     int sumA = 0; n     for (int i = (1), _b = (n); i <= _b; ++i) { n       int x; n       cin >> x; n       --x; n       a[i] = x; n       sumA += x; n     } n     if (m == 1) { n       cout << 1.0 << endl; n       continue; n     } n     for (int i = (1), _b = (n); i <= _b; ++i) { n       int cur = i % 2; n       memset(f[cur], 0, sizeof f[cur]); n       if (i == 1) { n         for (int val = 0, _a = (m * i); val < _a; ++val) n           if (val < m && val != a[i]) f[cur][val] = 1; n       } else { n         for (int val = 0, _a = (m * i); val < _a; ++val) { n           f[cur][val] = sum[1 - cur][val]; n           if (val >= m) f[cur][val] -= sum[1 - cur][val - m]; n           f[cur][val] -= f[1 - cur][val - a[i]]; n         } n       } n       for (int val = 0, _a = (m * (i + 1)); val < _a; ++val) { n         sum[cur][val] = f[cur][val]; n         if (val > 0) sum[cur][val] += sum[cur][val - 1]; n       } n     } n     long double t = sum[n % 2][sumA - 1]; n     for (int turn = 0, _a = (n); turn < _a; ++turn) t /= (m - 1); n     cout << t * (m - 1.0) + 1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n  n int n, k; n  n void solve(){ n     cin >> n >> k; n  n     int cnt = 1; n  n     int query = 0; n  n     for(int i = 0; i < n; i++){ n         if(i) query = i ^ (i - 1); n         cout << query << endl; n         int r; n         cin >> r; n         if(r == 1) return; n     } n } n  n int main(){ n     // freopen( inp.txt ,  r , stdin); n     // freopen( out.txt ,  w , stdout); n     ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); n  n     int t; n     cin >> t; n     while(t--) solve(); n }


#include <bits/stdc++.h> n using namespace std; n int n, m, i, x, y, k, a, q, h, l, r, j, p, s[400]; n vector<int> ve[51][51]; n bool ok1, ok2, Find, ok[51][51]; n void get(int x, int y, int z) { n   q = 105; n   h = 104; n   for (int i = 1; i < ve[x][y].size(); i++) { n     s[++h] = ve[x][y][i]; n     if (i == z) l = h, r = h + 1; n   } n   while (q != l && h - q + 1 <= 2 * n) { n     if (ve[s[l - 1]][s[l]].size() == 0) return; n     for (int i = ve[s[l - 1]][s[l]].size() - 1; i; i--) n       s[--q] = ve[s[l - 1]][s[l]][i]; n     l--; n   } n   if (h - q + 1 > 2 * n) return; n   while (h != r && h - q + 1 <= 2 * n) { n     if (ve[s[r]][s[r + 1]].size() == 0) return; n     for (int i = 1; i < ve[s[r]][s[r + 1]].size(); i++) n       s[++h] = ve[s[r]][s[r + 1]][i]; n     r++; n   } n   if (h - q + 1 > 2 * n) return; n   Find = true; n } n int main() { n   scanf( %d%d , &n, &m); n   for (i = 1; i <= m; i++) { n     scanf( %d%d%d , &x, &y, &k); n     ok1 = false; n     ok2 = false; n     ve[x][y].push_back(0); n     a = 0; n     while (k--) { n       if (a != x) n         ok1 = false; n       else n         ok1 = true; n       scanf( %d , &a); n       if (a == y && ok1) ok2 = true; n       ve[x][y].push_back(a); n     } n     if (ok2) ok[x][y] = true; n   } n   for (i = 1; i <= n; i++) n     for (j = 1; j <= n; j++) n       if (ok[i][j]) n         for (p = 1; p < ve[i][j].size() - 1; p++) n           if (ve[i][j][p] == i && ve[i][j][p + 1] == j) { n             get(i, j, p); n             if (Find) { n               printf( %d n , h - q + 1); n               for (i = q; i <= h; i++) printf( %d  , s[i]); n               printf(  n ); n               return 0; n             } n           } n   printf( 0 n ); n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI = 3.141592653589793238462643383; n int w; n int visit[37] = {0}; n int Ceil(int a, int b) { n   if (a % b == 0) n     return a / b; n   else n     return (a / b) + 1; n } n struct TrieNode { n   struct TrieNode *children[37]; n   bool isEndofWord; n   int cnt[37]; n   int snum[37]; n   int visit[37]; n }; n TrieNode *getNode() { n   TrieNode *x = new TrieNode; n   x->isEndofWord = false; n   for (int i = 0; i < (37); ++i) x->children[i] = NULL; n   for (int i = 0; i < (37); ++i) x->cnt[i] = 0; n   for (int i = 0; i < (37); ++i) x->snum[i] = -1; n   for (int i = 0; i < (37); ++i) x->visit[i] = -1; n   return x; n } n int find(char c) { n   if (c ==  . ) { n     return 26; n   } n   if (c >=  a  && c <=  z ) { n     return c -  a ; n   } n   return 27 + c -  0 ; n } n void insert(TrieNode *root, string s, int num) { n   TrieNode *p = root; n   for (int i = 0; i < (s.size()); ++i) { n     int index; n     index = find(s[i]); n     if (!p->children[index]) { n       p->children[index] = getNode(); n     } n     if (p->visit[index] != num) p->cnt[index]++; n     p->snum[index] = num; n     p->visit[index] = num; n     p = p->children[index]; n   } n   p->isEndofWord = true; n } n int search(TrieNode *root, string s) { n   TrieNode *p = root; n   int ans; n   for (int i = 0; i < (s.size()); ++i) { n     int index; n     index = find(s[i]); n     if (!p->children[index]) { n       return 0; n     } n     ans = p->cnt[index]; n     w = p->snum[index]; n     p = p->children[index]; n   } n   return ans; n } n int main() { n   cin.sync_with_stdio(0); n   cin.tie(0); n   cin.exceptions(cin.failbit); n   int n; n   cin >> n; n   string a[n]; n   TrieNode *root = getNode(); n   int e = 0; n   while (n--) { n     string s; n     cin >> s; n     a[e] = s; n     e++; n     int y = s.size() - 1; n     for (int i = 0; i < (s.size()); ++i) { n       string str; n       int x = s.size() - i; n       str.resize(x); n       int q = 0; n       for (int j = i; j <= (y); ++j) { n         str[q] = s[j]; n         q++; n       } n       insert(root, str, e); n     } n   } n   int q; n   cin >> q; n   while (q--) { n     string s; n     cin >> s; n     int y = search(root, s); n     if (y == 0) { n       cout <<  0 -  <<   n ; n     } else { n       cout << y <<     << a[w - 1] <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:64000000 ) n using namespace std; n int main() { n   long long n; n   cin >> n; n   vector<long long> v; n   long long mx = -1, mn = -1; n   for (long long i = 1; i * i <= n; i++) n     if (n % i == 0) { n       v.push_back(i); n       v.push_back(n / i); n     } n   sort((v).begin(), (v).end()); n   for (int i = 0; i < (int)(v).size(); i++) { n     long long rest = n / v[i]; n     for (int j = 0; j < (int)(v).size(); j++) n       if (rest % v[j] == 0) { n         long long a = v[i] + 1; n         long long b = v[j] + 2; n         long long c = rest / v[j] + 2; n         mx = max(mx, a * b * c - (a - 1) * (b - 2) * (c - 2)); n         if (mn == -1) mn = a * b * c - (a - 1) * (b - 2) * (c - 2); n         mn = min(mn, a * b * c - (a - 1) * (b - 2) * (c - 2)); n       } n   } n   cout << mn <<     << mx; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10, inf = 1e9 + 10, P = 53, MOD = 1e9 + 7; n int n, a, b, k, q, seg1[4 * maxn], seg2[4 * maxn]; n inline void update(int ind) { n   seg1[ind] = seg1[((ind) * (2))] + seg1[(((ind) * (2)) + 1)]; n   seg2[ind] = seg2[((ind) * (2))] + seg2[(((ind) * (2)) + 1)]; n } n void add(int s, int e, int ind, int l, int r, int x) { n   if (s >= r || e <= l) return; n   if (s >= l && e <= r) { n     seg1[ind] += x; n     seg2[ind] += x; n     seg1[ind] = min(seg1[ind], b); n     seg2[ind] = min(seg2[ind], a); n     return; n   } n   int mid = (s + e) / 2; n   add(s, mid, ((ind) * (2)), l, r, x); n   add(mid, e, (((ind) * (2)) + 1), l, r, x); n   update(ind); n } n int query(int type, int s, int e, int ind, int l, int r) { n   if (s >= r || e <= l) return 0; n   if (s >= l && e <= r) { n     if (type == 1) n       return seg1[ind]; n     else n       return seg2[ind]; n   } n   int mid = (s + e) / 2; n   return query(type, s, mid, ((ind) * (2)), l, r) + n          query(type, mid, e, (((ind) * (2)) + 1), l, r); n } n int main() { n   scanf( %d%d%d%d%d , &n, &k, &a, &b, &q); n   while (q--) { n     int type, l, x; n     scanf( %d , &type); n     if (type == 1) { n       scanf( %d%d , &l, &x); n       add(0, n, 1, l - 1, l, x); n     } else { n       scanf( %d , &x); n       x--; n       printf( %d n , query(1, 0, n, 1, 0, x) + query(2, 0, n, 1, x + k, n)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 100; n struct node { n   int x, y; n } point[maxn]; n bool check(node a, node b, node c, node d) { n   if (a.x - b.x == c.x - d.x && a.y - b.y == c.y - d.y) return true; n   return false; n } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> point[i].x >> point[i].y; n   if (n & 1) { n     cout <<  nO  << endl; n   } else { n     bool flag = true; n     for (int i = 0; i < n; ++i) { n       if (!check(point[(i + 1) % n], point[i], point[(i + n / 2) % n], n                  point[(i + n / 2 + 1) % n])) n         flag = false; n     } n     if (flag) 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 const long long N = 1e3 + 10; n const long long M = 1e9 + 7; n long long Max(long long a, long long b) { n   if (a > b) return a; n   return b; n } n long long Min(long long a, long long b) { n   if (a > b) return b; n   return a; n } n long long big_mul(long long a, long long b, long long m) { n   long long x = 0, y = a % m; n   while (b > 0) { n     if (b & 1) x = (x + y) % m; n     y = (2 * y) % m; n     b >>= 1; n   } n   return x % m; n } n long long big_mod(long long a, long long b, long long m) { n   long long x = 1, y = a % m; n   while (b > 0) { n     if (b & 1) x = (x * y) % m; n     y = (y * y) % m; n     b >>= 1; n   } n   return x % m; n } n long long Big_mod(long long a, long long b, long long m) { n   long long x = 1, y = a % m; n   while (b > 0) { n     if (b & 1) x = big_mul(x, y, m); n     y = big_mul(y, y, m); n     b >>= 1; n   } n   return x % m; n } n long long mod_inverse(long long a, long long m) { return big_mod(a, m - 2, m); } n unordered_map<string, bool> Map; n int a, b, c, d, e, f, g, h; n vector<pair<int, int> > vec; n vector<pair<pair<int, int>, int> > save; n void fnct(int x1, int y1, int x2, int y2) { n   if (x1 < x2 && y1 > y2) { n     a = x1; n     b = y2; n     c = x1; n     d = y1; n     e = x2; n     f = y1; n     g = x2; n     h = y2; n   } else if (x1 > x2 && y1 < y2) { n     a = x2; n     b = y1; n     c = x2; n     d = y2; n     e = x1; n     f = y2; n     g = x1; n     h = y1; n   } else if (x1 < x2 && y1 < y2) { n     a = x1; n     b = y1; n     c = x1; n     d = y2; n     e = x2; n     f = y2; n     g = x2; n     h = y1; n   } else { n     a = x2; n     b = y2; n     c = x2; n     d = y1; n     e = x1; n     f = y1; n     g = x1; n     h = y2; n   } n } n double dis(double w, double z, double p, double q) { n   double n = w - p; n   double m = z - q; n   return sqrt(n * n + m * m); n } n bool check(double p, double q) { n   for (int i = 0; i < save.size(); i++) { n     double w = (double)(save[i].first.first); n     double z = (double)(save[i].first.second); n     double r = (double)(save[i].second); n     if (dis(w, z, p, q) <= r) return true; n   } n   return false; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int x1, y1, x2, y2; n   cin >> x1 >> y1 >> x2 >> y2; n   fnct(x1, y1, x2, y2); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int w, z, r; n     cin >> w >> z >> r; n     if (Map[to_string(w) + to_string(z) + to_string(r)] == false) n       save.push_back(make_pair(make_pair(w, z), r)); n     Map[to_string(w) + to_string(z) + to_string(r)] = true; n   } n   for (int i = c; i <= e; i++) vec.push_back(make_pair(i, d)); n   for (int i = a; i <= g; i++) vec.push_back(make_pair(i, b)); n   for (int i = d - 1; i > b; i--) vec.push_back(make_pair(a, i)); n   for (int i = f - 1; i > h; i--) vec.push_back(make_pair(e, i)); n   int ans = 0; n   for (int i = 0; i < vec.size(); i++) { n     double p = (double)(vec[i].first); n     double q = (double)(vec[i].second); n     if (check(p, q) == false) ans++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100010]; n bool b[100010]; n long long gcd(long long a, long long b) { return a % b ? gcd(b, a % b) : b; } n int main() { n   long long n, t; n   scanf( %lld , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n     b[i] = a[i] % 2; n   } n   long long ans = gcd(abs(a[1]), abs(a[2])); n   for (int i = 3; i <= n; i++) ans = gcd(ans, abs(a[i])); n   if (ans > 1) n     printf( YES n0 n ); n   else { n     int num = 0; n     for (int i = 1; i < n; i++) n       while (b[i] == 1) { n         if (b[i + 1] == 1) { n           b[i] = 0; n           b[i + 1] = 0; n         } else { n           b[i + 1] = 1; n         } n         ++num; n       } n     if (b[n] == 1) num += 2; n     printf( YES n%d n , num); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n const long long INF = 1000000007; n const long long INF2 = 1000000007LL * 1000000007LL; n const long double EPS = 1e-9; n const int SIZE = 1001000; n int n, a, b; n string fn[SIZE]; n int val[SIZE], ans[SIZE]; n int z[SIZE]; n vector<int> g[SIZE]; n void setVal(int v) { n   if (fn[v][0] ==  I ) { n     return; n   } n   for (auto& i : g[v]) setVal(i); n   if (fn[v][0] ==  A ) { n     val[v] = val[g[v][0]] && val[g[v][1]]; n     return; n   } n   if (fn[v][0] ==  O ) { n     val[v] = val[g[v][0]] || val[g[v][1]]; n     return; n   } n   if (fn[v][0] ==  X ) { n     val[v] = (val[g[v][0]] + val[g[v][1]]) % 2; n     return; n   } n   val[v] = !val[g[v][0]]; n } n void dfs(int v, int zn) { n   if (fn[v][0] ==  I ) { n     z[v] = zn; n     return; n   } n   if (zn == 0) { n     for (auto& i : g[v]) dfs(i, 0); n     return; n   } n   if (fn[v][0] ==  A ) { n     if (val[v] == 1) { n       for (auto& i : g[v]) dfs(i, 1); n     } else { n       if (!val[g[v][0]] && val[g[v][1]]) { n         dfs(g[v][0], 1); n         dfs(g[v][1], 0); n       } else if (val[g[v][0]] && !val[g[v][1]]) { n         dfs(g[v][0], 0); n         dfs(g[v][1], 1); n       } else { n         for (auto& i : g[v]) dfs(i, 0); n       } n     } n     return; n   } n   if (fn[v][0] ==  O ) { n     if (val[v] == 0) { n       for (auto& i : g[v]) dfs(i, 1); n     } else { n       if (val[g[v][0]] && val[g[v][1]]) { n         dfs(g[v][0], 0); n         dfs(g[v][1], 0); n       } else if (!val[g[v][0]] && val[g[v][1]]) { n         dfs(g[v][0], 0); n         dfs(g[v][1], 1); n       } else if (val[g[v][0]] && !val[g[v][1]]) { n         dfs(g[v][0], 1); n         dfs(g[v][1], 0); n       } n     } n     return; n   } n   for (auto& i : g[v]) dfs(i, zn); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> fn[i]; n     if (fn[i] ==  IN ) { n       cin >> val[i]; n     } else if (fn[i] ==  NOT ) { n       cin >> a; n       --a; n       g[i].push_back(a); n     } else { n       cin >> a; n       --a; n       g[i].push_back(a); n       cin >> a; n       --a; n       g[i].push_back(a); n     } n   } n   setVal(0); n   dfs(0, 1); n   for (int i = 0; i < n; ++i) { n     if (fn[i][0] ==  I ) { n       if (z[i]) n         cout << !val[0]; n       else n         cout << val[0]; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 202; n const int MAXM = 50005; n struct edge { n   int a, b; n   long long int g, s; n   edge(int a_ = 0, int b_ = 0, long long int g_ = 0LL, long long int s_ = 0LL) { n     a = a_, b = b_, g = g_, s = s_; n   } n }; n int N, M, cnt; n int dad[MAXN]; n long long int G, S, res = 1LL << 62; n edge g[MAXM]; n edge way[MAXM]; n inline bool compare(const edge &a, const edge &b) { return a.g < b.g; } n inline int find(int a) { n   if (a == dad[a]) return a; n   return dad[a] = find(dad[a]); n } n int main() { n   scanf(  %d %d %I64d %I64d , &N, &M, &G, &S); n   for (int i = 1; i <= M; i++) { n     scanf(  %d %d %I64d %I64d , &way[i].a, &way[i].b, &way[i].g, &way[i].s); n     way[i].g *= G; n     way[i].s *= S; n   } n   sort(way + 1, way + M + 1, compare); n   for (int i = 1; i <= M; i++) { n     g[++cnt] = way[i]; n     for (int j = cnt; j > 1; j--) { n       if (g[j].s < g[j - 1].s) n         swap(g[j], g[j - 1]); n       else n         break; n     } n     if (i >= N - 1) { n       long long int ss; n       for (int j = 1; j <= N; j++) dad[j] = j; n       int c = 0; n       int ncnt = 0; n       for (int j = 1; j <= cnt; j++) n         if (find(g[j].a) != find(g[j].b)) { n           c++; n           g[++ncnt] = g[j]; n           ss = g[j].s; n           dad[find(g[j].a)] = g[j].b; n         } n       cnt = ncnt; n       if (c == N - 1) res = min(res, way[i].g + ss); n     } n   } n   if (res == 1LL << 62) n     printf( -1 n ); n   else n     printf( %I64d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, id, nxt[100005], num[100005], mark[100005]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &nxt[i]); n     if (nxt[i] == i) { n       puts( YES ); n       for (int j = 1; j <= n; j++) n         if (j != i) printf( %d %d n , i, j); n       return 0; n     } n   } n   for (int i = 1; i <= n; i++) { n     if (num[i]) continue; n     int j = nxt[i], t = 1; n     while (j != i) j = nxt[j], t++; n     while (!num[j]) num[j] = t, j = nxt[j]; n     if (t & 1) { n       puts( NO ); n       return 0; n     } n     if (t == 2) id = i; n   } n   if (!id) { n     puts( NO ); n     return 0; n   } n   puts( YES ); n   printf( %d %d n , id, nxt[id]); n   for (int i = 1; i <= n; i++) { n     if (i == id || i == nxt[id] || mark[i]) continue; n     int a = id, b = i; n     while (num[i]--) { n       printf( %d %d n , a, b); n       mark[b] = 1; n       a = nxt[a]; n       b = nxt[b]; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   scanf( %d , &n); n   int ara[n]; n   int temp, j; n   int i = 0; n   while (i < n) { n     scanf( %d , &ara[i]); n     i++; n   } n   i = 0; n   while (i < (n - 1)) { n     j = 0; n     while (j < (n - 1)) { n       if (ara[j] > ara[j + 1]) { n         temp = ara[j]; n         ara[j] = ara[j + 1]; n         ara[j + 1] = temp; n       } n       j++; n     } n     i++; n   } n   i = 0; n   while (i < n) { n     printf( %d  , ara[i]); n     i++; n   } n   return 0; n } n


#include <bits/stdc++.h> n #define MP make_pair n using namespace std; n  n #define rep(i, n) for (int i = 0; i < (int)(n); ++ i) n #define rep1(i, n) for (int i = 1; i <= (int)(n); ++ i) n #define foreach(itr, c) for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); ++ itr) n  n typedef long long LL; n typedef pair<int, int> pii; n  n const int MAXN = 2e5 + 5; n  n int N; n string str[MAXN]; n map<string, vector<string> > grps; n LL ans; n  n int solve_pair(const string& s1, const string& s2) n { n     int prefix = 0, suffix = 0, n = s1.size(); n  n     while (s1[prefix] == s2[prefix]) prefix ++; n     while (s1[n - suffix - 1] == s2[n - suffix - 1]) suffix ++; n  n     return is_sorted(s1.begin() + prefix, s1.end() - suffix) || is_sorted(s2.begin() + prefix, s2.end() - suffix) ? 1ll : 2ll; n } n void solve(vector<string>& grp) n { n     int n = grp.size(), len = grp[0].size(); n     ans -= 1ll * (n - 1) * n / 2 * 1337; n  n     if (n <= 200 * len) { n         rep(i, n) for (int j = i + 1; j < n; ++ j) { n             ans += solve_pair(grp[i], grp[j]); n         } n     } else { n         sort(grp.begin(), grp.end()); n         ans += 1ll * (n - 1) * n / 2 * 2; n  n         for (auto& s : grp) { n             rep(i, len) for (int j = i + 2; j <= len; ++ j) { n                 string sorted = s; n                 sort(sorted.begin() + i, sorted.begin() + j); n                 ans -= (sorted[i] != s[i] && sorted[j - 1] != s[j - 1] && binary_search(grp.begin(), grp.end(), sorted)); n             } n         } n     } n }; n  n int main() n { n     scanf( %d , &N); n     rep(i, N) { n         cin >> str[i]; n         string sorted = str[i]; n         sort(sorted.begin(), sorted.end()); n         grps[sorted].push_back(str[i]); n     } n  n     ans = 1ll * (N - 1) * N / 2 * 1337; n  n     for (auto& grp : grps) solve(grp.second); n  n     printf( %lld n , ans); n      n     return 0; n }


#include <iostream> n #include <cmath> n #include <map> n #include <string> n #include <vector> n #include <algorithm> n #define int long long n using namespace std; n string s; n int a, b, c; n int n, m, k; n vector<int>vec, vec2; n int arr[4000000], mark[4000000]; n signed main() { n  ios_base::sync_with_stdio(false); n  cin.tie(NULL); n  cout.tie(NULL); n  cin >> n >> m >> k; n  for (int i = 0; i < n; i++) { n   cin >> a; n   vec.push_back(a); n  } n  for (int i = 0; i < m; i++) { n   cin >> a; n   vec2.push_back(a); n  } n  if (n < m) { n   swap(n, m); n   swap(vec, vec2); n  } n  int iter = 0; n  for (auto i : vec) { n   mark[iter] = m; n   iter++; n   arr[i] = iter; n  } n  iter = 0; n  int pos; n  for (auto i : vec2) { n   iter++; n   if (arr[i] != 0) { n    pos = (arr[i] - iter + n) % n; n    mark[pos]--; n   } n  } n  pos = 0; n  int sum = 0; n  int len = 0; n  while (true) { n   sum += mark[pos]; n   pos += m; n   len += m; n   pos %= n; n   if (pos == 0) { n    break; n   } n  } n  k -= 2; n  int ans = (k / sum); n  ans *= len; n  k %= sum; n  k += 2; n  pos = 0; n  while (k > mark[pos]) { n   k -= mark[pos]; n   pos += m; n   ans += m; n   pos %= n; n  } n  int pos2 = 0; n  while (k > 0) { n   ans++; n   if (vec[(pos + pos2) % n] != vec2[pos2 % m]) { n    k--; n   } n   pos2++; n  } n  cout << ans; n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 15; n const int inf = 1e9; n const int md = 1e9 + 7; n const int shift = 1 << 17; n int n, m; n vector<vector<int> > g; n set<pair<int, int> > br; n int timer; n int tin[maxn], fup[maxn]; n int used[maxn]; n void dfs(int v, int p = -1) { n   used[v] = 1; n   tin[v] = fup[v] = timer++; n   for (int i = 0; i < g[v].size(); ++i) { n     int to = g[v][i]; n     if (to == p) continue; n     if (used[to]) n       fup[v] = min(fup[v], tin[to]); n     else { n       dfs(to, v); n       fup[v] = min(fup[v], fup[to]); n       if (fup[to] > tin[v]) { n         br.insert(make_pair(to, v)); n         br.insert(make_pair(v, to)); n       } n     } n   } n } n int color[maxn]; n int first[maxn]; n int d[maxn]; n vector<int> eu; n int z[shift * 2]; n int c; n void dfs2(int v, int tekc, int len, int p = -1) { n   used[v] = 1; n   color[v] = tekc; n   d[tekc] = len; n   for (int i = 0; i < g[v].size(); ++i) { n     int to = g[v][i]; n     if (to == p) continue; n     if (used[to]) continue; n     if (br.find(make_pair(v, to)) != br.end()) { n       eu.push_back(c + 1); n       first[c + 1] = eu.size() - 1; n       ++c; n       dfs2(to, c, len + 1, v); n       eu.push_back(tekc); n     } else n       dfs2(to, tekc, len, v); n   } n } n void build() { n   for (int i = 0; i < eu.size(); ++i) z[i + shift] = eu[i]; n   for (int i = eu.size(); i < shift; ++i) z[i + shift] = n + 1; n   for (int i = shift - 1; i > 0; --i) n     if (d[z[i * 2]] < d[z[i * 2 + 1]]) n       z[i] = z[i * 2]; n     else n       z[i] = z[i * 2 + 1]; n } n int get(int v, int tl, int tr, int l, int r) { n   if (l > r) return n + 1; n   if (l == tl && r == tr) return z[v]; n   int tm = (tl + tr) / 2; n   int ans1 = get(v * 2, tl, tm, l, min(r, tm)); n   int ans2 = get(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r); n   return d[ans1] < d[ans2] ? ans1 : ans2; n } n int main() { n   cin >> n >> m; n   g.resize(n); n   for (int i = 0; i < m; ++i) { n     int v, u; n     cin >> v >> u; n     --v, --u; n     g[v].push_back(u); n     g[u].push_back(v); n   } n   dfs(0); n   memset(used, 0, sizeof(used)); n   memset(first, -1, sizeof(first)); n   eu.push_back(0); n   first[0] = 0; n   dfs2(0, 0, 0); n   d[n + 1] = inf; n   build(); n   int k; n   cin >> k; n   for (int i = 0; i < k; ++i) { n     int s, l; n     cin >> s >> l; n     --s, --l; n     int L = first[color[s]]; n     int R = first[color[l]]; n     if (L > R) swap(L, R); n     int lca = get(1, 0, shift - 1, L, R); n     cout << d[color[s]] + d[color[l]] - 2 * d[lca] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, siz[500010]; n vector<int> v[500010]; n class DS { n  public: n   long long dat[2][2]; n   DS() { memset(dat, 0, sizeof(dat)); } n   DS(long long a00, long long a01, long long a10, long long a11) { n     dat[0][0] = a00; n     dat[0][1] = a01; n     dat[1][0] = a10; n     dat[1][1] = a11; n   } n   DS trans() { n     return DS(dat[1][0], dat[0][0] + dat[1][1], dat[0][0], dat[0][1]); n   } n   DS operator*(const DS &t) const { n     DS ret; n     for (int i = 0; i < 2; i++) { n       for (int j = 0; j < 2; j++) { n         for (int k = 0; k + i < 2; k++) { n           for (int l = 0; j + l < 2; l++) { n             ret.dat[i + k][j + l] += dat[i][j] * t.dat[k][l]; n           } n         } n       } n     } n     return ret; n   } n } f[500010], g[500010]; n void dfs(int np, int fath) { n   f[np] = DS(1, 0, 0, 0); n   siz[np] = 1; n   for (int &x : v[np]) { n     if (x == fath) continue; n     dfs(x, np); n     siz[np] += siz[x]; n     f[np] = f[np] * f[x].trans(); n   } n } n void dfs2(int np, int fath) { n   vector<DS> pre; n   pre.push_back(DS(1, 0, 0, 0)); n   for (int i = 0; i < v[np].size(); i++) { n     if (v[np][i] == fath) n       pre.push_back(pre.back()); n     else n       pre.push_back(pre.back() * f[v[np][i]].trans()); n   } n   DS cur = fath ? g[np].trans() : DS(1, 0, 0, 0); n   for (int i = (int)v[np].size() - 1; i >= 0; i--) { n     if (v[np][i] == fath) continue; n     g[v[np][i]] = cur * pre[i]; n     dfs2(v[np][i], np); n     cur = cur * f[v[np][i]].trans(); n   } n } n int main() { n   scanf( %d , &n); n   if (n & 1) { n     puts( 0 ); n     return 0; n   } n   for (int i = 1, a, b; i < n; i++) { n     scanf( %d%d , &a, &b); n     v[a].push_back(b); n     v[b].push_back(a); n   } n   dfs(1, 0); n   dfs2(1, 0); n   long long ans = 0; n   for (int i = 2; i <= n; i++) { n     if (f[i].dat[1][0] == 1 && g[i].dat[1][0] == 1) n       ans += (long long)siz[i] * (n - siz[i]); n     else n       ans += n           (f[i].dat[0][0] + f[i].dat[1][1]) * (g[i].dat[0][0] + g[i].dat[1][1]); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, u, a[300001], x, y; n long double ans = -1.0; n int main() { n   cin >> n >> u; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   y = 1; n   while (x < n - 2) { n     while (y < n - 1 && a[y + 1] - a[x] <= u) ++y; n     if (y - x >= 2 && a[y] - a[x] <= u) n       ans = n           max(ans, (long double)(a[y] - a[x + 1]) / (long double)(a[y] - a[x])); n     ++x; n   } n   printf( %.9Lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 5; n int n, m, cnt, belong[N]; n vector<int> g[N], ans[N]; n int get(int x) { return belong[x] ? belong[x] = get(belong[x]) : x; } n void dfs(int i) { n   ans[cnt].push_back(i); n   belong[i] = i + 1; n   for (int j = get(1), k = 0; j <= n; j = get(j + 1)) { n     for (; k < g[i].size() && g[i][k] < j; k++) n       ; n     if (k < g[i].size() && g[i][k] == j) continue; n     dfs(j); n   } n } n int main() { n   scanf( %d%d , &n, &m); n   while (m--) { n     int a, b; n     scanf( %d%d , &a, &b); n     g[a].push_back(b); n     g[b].push_back(a); n   } n   for (int i = 1; i <= n; i++) sort(g[i].begin(), g[i].end()); n   for (int i = 1; i <= n; i++) n     if (!belong[i]) { n       dfs(i); n       cnt++; n     } n   printf( %d n , cnt); n   for (int i = 0; i < cnt; i++) { n     printf( %u , ans[i].size()); n     for (int j = 0; j < ans[i].size(); j++) printf(  %d , ans[i][j]); n     puts(  ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int q, n; n int a[100010], b[100010]; n int main() { n   scanf( %d , &q); n   while (q--) { n     scanf( %d , &n); n     for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n     for (int i = 1; i <= n; i++) scanf( %d , &b[i]); n     int pos = 0, dis = 0, flag1 = 0, flag2 = 0; n     for (int i = 1; i <= n; i++) { n       if (a[i] != b[i] && flag1 == 0) { n         pos = i; n         dis = b[i] - a[i]; n         if (dis < 0) { n           flag2 = 1; n           break; n         } n         flag1 = 1; n       } else if (a[i] != b[i] && flag1 == 1) { n         if (b[i] - a[i] != dis || i != pos + 1) { n           flag2 = 1; n           break; n         } else n           pos = i; n       } n     } n     if (flag2) n       printf( NO n ); n     else n       printf( YES n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int OO = (int)2e9; n const double eps = 1e-9; n int x[3], y[3]; n int main() { n   int i; n   vector<pair<int, int> > V(8); n   set<int> X; n   set<int> Y; n   for (i = 0; i < 8; i++) { n     cin >> V[i].first >> V[i].second; n     X.insert(V[i].first); n     Y.insert(V[i].second); n   } n   i = 0; n   for (__typeof((X).begin()) it = (X).begin(); it != (X).end(); ++it) n     x[i++] = *it; n   i = 0; n   for (__typeof((Y).begin()) it = (Y).begin(); it != (Y).end(); ++it) n     y[i++] = *it; n   vector<pair<int, int> > C(8); n   C[0] = make_pair(x[0], y[0]); n   C[1] = make_pair(x[0], y[1]); n   C[2] = make_pair(x[0], y[2]); n   C[3] = make_pair(x[1], y[0]); n   C[4] = make_pair(x[1], y[2]); n   C[5] = make_pair(x[2], y[0]); n   C[6] = make_pair(x[2], y[1]); n   C[7] = make_pair(x[2], y[2]); n   sort((V).begin(), (V).end()); n   if (((int)(X).size()) != 3 || ((int)(Y).size()) != 3 || V != C) n     puts( ugly ); n   else n     puts( respectable ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[4000]; n int ans[4000][4000]; n int main() { n   cin >> n; n   map<int, int> r; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (r.find(a[i]) == r.end()) { n       int k = r.size(); n       r[a[i]] = k; n     } n     a[i] = r[a[i]]; n   } n   int m = r.size(); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (ans[j][a[i]] + 1 > ans[a[i]][j]) { n         ans[a[i]][j] = ans[j][a[i]] + 1; n       } n     } n   } n   int best = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (ans[i][j] > best) { n         best = ans[i][j]; n       } n     } n   } n   cout << best << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int a[100001], b[100001]; n vector<long long int> v; n map<long long int, long long int> assign; n pair<long long int, long long int> dp[1001][7001]; n inline long long int gcd(long long int a, long long int b) { n   long long int tmp; n   while (b) { n     tmp = a % b; n     a = b; n     b = tmp; n   } n   return a; n } n int main() { n   ios::sync_with_stdio(false); n   ; n   cin.tie(0); n   ; n   long long int i, j, n, k, x, y, temp, sz; n   cin >> n >> k; n   temp = k; n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n     b[i] = gcd(a[i], k); n     temp = temp / gcd(temp, a[i]); n   } n   if (temp != 1) { n     cout <<  -1 ; n     return 0; n   } n   if (k == 1) { n     cout <<  1  n          <<   n ; n     cout << min_element(a + 1, a + n + 1) - a; n     return 0; n   } n   for (i = 1; i * i <= k; i++) { n     if (k % i) continue; n     v.push_back(i); n     if (i != (k / i)) v.push_back(k / i); n   } n   sort(v.begin(), v.end()); n   sz = v.size(); n   for (i = 0; i < sz; i++) assign[v[i]] = i; n   for (i = 1; i < sz; i++) dp[0][i] = make_pair(1000000000000, 0); n   dp[0][0] = make_pair(0, 0); n   for (i = 1; i <= n; i++) { n     for (j = 0; j < sz; j++) { n       dp[i][j] = dp[i - 1][j]; n       x = assign[v[j] / gcd(v[j], b[i])]; n       if (dp[i][j] > n           make_pair(dp[i - 1][x].first + 1, dp[i - 1][x].second + a[i])) n         dp[i][j] = n             make_pair(dp[i - 1][x].first + 1, dp[i - 1][x].second + a[i]); n     } n   } n   cout << dp[n][sz - 1].first <<   n ; n   x = k; n   for (i = n; i >= 1; i--) { n     if (dp[i][assign[x]] == dp[i - 1][assign[x]]) continue; n     cout << i <<    ; n     x = x / gcd(x, b[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1 << 30; n const int N = 200111; n struct node { n   int i, j; n   long long mn, ad = 0; n   node *l, *r; n   node(long long *a, int i, int j) : i(i), j(j) { n     if (j - i == 1) { n       l = r = NULL; n       mn = a[i]; n     } else { n       int k = i + j >> 1; n       l = new node(a, i, k); n       r = new node(a, k, j); n       combine(); n     } n   } n   inline void combine() { mn = min(l->mn, r->mn); } n   void visit() { n     if (ad) { n       mn += ad; n       if (l) l->ad += ad, r->ad += ad; n       ad = 0; n     } n   } n   void inc(int I, int J, long long a) { n     if (I <= i && j <= J) { n       ad += a; n       visit(); n     } else { n       visit(); n       if (!(J <= i || j <= I)) { n         l->inc(I, J, a); n         r->inc(I, J, a); n         combine(); n       } n     } n   } n   int find() { n     visit(); n     if (mn > 0) return INF; n     if (!l) { n       return mn == 0 ? i : INF; n     } else { n       int ans = INF; n       if (ans >= INF) ans = l->find(); n       if (ans >= INF) ans = r->find(); n       return ans; n     } n   } n }; n long long a[N], s[N]; n int main() { n   int n, q; n   scanf( %d%d , &n, &q); n   for (int i = 0; i < n; i++) { n     scanf( %lld , &a[i]); n     s[i + 1] = s[i] + a[i]; n   } n   for (int i = 0; i < n; i++) { n     s[i] -= a[i]; n   } n   node *t = new node(s, 0, n); n   while (q--) { n     int i; n     long long v; n     scanf( %d%lld , &i, &v); n     long long d = v - a[--i]; n     t->inc(i, i + 1, -d); n     t->inc(i + 1, n, +d); n     t->visit(); n     a[i] += d; n     int ans = t->find(); n     printf( %d n , ans >= INF ? -1 : ans + 1); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2501; n mt19937 rng(std::chrono::system_clock::now().time_since_epoch().count()); n int n, p[2 * N], a[N][N], cur; n void no() { n   puts( NOT MAGIC ); n   exit(0); n } n struct edge { n   int u, v, w; n   edge(int a = 0, int b = 0, int c = 0) { n     u = a; n     v = b; n     w = c; n   } n   bool operator<(const edge &a) const { return w < a.w; } n }; n int get(int u) { return u == p[u] ? u : p[u] = get(p[u]); } n vector<edge> e; n vector<pair<int, int>> g[2 * N]; n void dfs(int u, int p, int mx) { n   if (u >= n && a[cur][u - n] > mx) no(); n   for (auto i : g[u]) n     if (i.first != p) dfs(i.first, u, max(mx, i.second)); n } n int main() { n   for (int i = 0; i < 2 * N; i++) p[i] = i; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) { n       scanf( %d , &a[i][j]); n       e.push_back(edge(i, n + j, a[i][j])); n     } n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) { n       if (a[i][j] != a[j][i] || i == j && a[i][j]) no(); n     } n   sort(e.begin(), e.end()); n   for (auto i : e) { n     int a = get(i.u); n     int b = get(i.v); n     if (a != b) { n       p[a] = b; n       g[i.u].push_back({i.v, i.w}); n       g[i.v].push_back({i.u, i.w}); n     } n   } n   for (int i = 0; i < n; i++) { n     cur = i; n     dfs(i, -1, 0); n   } n   puts( MAGIC ); n } n


#include <bits/stdc++.h> n using ll = long long; n const ll maxn = 1234; n const ll inf = 1e9 + 7; n int main(int argc, char *argv[]) { n   ll n, m, a[maxn]; n   std::cin >> n >> m; n   ll cur = 0, maxx = 0, minn = 0; n   for (int i = 0; i < n; i++) { n     std::cin >> a[i]; n     cur += a[i]; n     maxx = std::max(maxx, cur); n     minn = std::min(minn, cur); n   } n   ll ans = maxx - minn; n   if (maxx - minn > m) { n     std::cout << 0; n   } else { n     std::cout << m - (maxx - minn) + 1; n   } n } n


/* n Author: QAQAutoMaton n Lang: C++ n Code: F.cpp n Mail: lk@qaq-am.com n Blog: https://www.qaq-am.com/ n */ n #include<bits/stdc++.h> n #define int long long n #define debug(qaq...) fprintf(stderr,qaq) n #define DEBUG printf( Passing [%s] in LINE %d n ,__FUNCTION__,__LINE__) n #define Debug debug( Passing [%s] in LINE %d n ,__FUNCTION__,__LINE__) n #define all(x) x.begin(),x.end() n #define x first n #define y second n #define unq(a) sort(all(a)),a.erase(unique(all(a)),a.end()) n using namespace std; n typedef unsigned uint; n typedef long long ll; n typedef unsigned long long ull; n typedef complex<double> cp; n typedef pair<int,int> pii; n int inf; n const double eps=1e-8; n const double pi=acos(-1.0); n template<class T,class T2>int chkmin(T &a,T2 b){return a>b?a=b,1:0;} n template<class T,class T2>int chkmax(T &a,T2 b){return a<b?a=b,1:0;} n template<class T>T sqr(T a){return a*a;} n template<class T,class T2>T mmin(T a,T2 b){return a<b?a:b;} n template<class T,class T2>T mmax(T a,T2 b){return a>b?a:b;} n template<class T>T aabs(T a){return a<0?-a:a;} n template<class T>int dcmp(T a,T b){return a>b;} n template<int *a>int cmp_a(int x,int y){return a[x]<a[y];} n template<class T>bool sort2(T &a,T &b){return a>b?swap(a,b),1:0;} n #define min mmin n #define max mmax n #define abs aabs n struct __INIT__{ n  __INIT__(){ n   fill((unsigned char*)&inf,(unsigned char*)&inf+sizeof(inf),0x3f); n  } n }__INIT___; n namespace io { n  const int SIZE = (1 << 21) + 1; n  char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr; n  // getchar n  #define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++) n  // print the remaining part n  inline void flush () { n   fwrite (obuf, 1, oS - obuf, stdout); n   oS = obuf; n  } n  // putchar n  inline void putc (char x) { n   *oS ++ = x; n   if (oS == oT) flush (); n  } n  template<typename A> n  inline bool read (A &x) { n   for (f = 1, c = gc(); c <  0  || c >  9 ; c = gc()) if (c ==  - ) f = -1;else if(c==EOF)return 0; n   for (x = 0; c <=  9  && c >=  0 ; c = gc()) x = x * 10 + (c & 15); x *= f; n   return 1; n  } n  inline bool read (char &x) { n   while((x=gc())==   ||x==  n  || x==  r ); n   return x!=EOF; n  } n  inline bool read(char *x){ n   while((*x=gc())==  n  || *x==   ||*x==  r ); n   if(*x==EOF)return 0; n   while(!(*x==  n ||*x==   ||*x==  r ||*x==EOF))*(++x)=gc(); n   *x=0; n   return 1; n  } n  template<typename A,typename ...B> n  inline bool read(A &x,B &...y){ n   return read(x)&&read(y...); n  } n  template<typename A> n  inline bool write (A x) { n   if (!x) putc ( 0 ); if (x < 0) putc ( - ), x = -x; n   while (x) qu[++ qr] = x % 10 +  0 ,  x /= 10; n   while (qr) putc (qu[qr --]); n   return 0; n  } n  inline bool write (char x) { n   putc(x); n   return 0; n  } n  inline bool write(const char *x){ n   while(*x){putc(*x);++x;} n   return 0; n  } n  inline bool write(char *x){ n   while(*x){putc(*x);++x;} n   return 0; n  } n  template<typename A,typename ...B> n  inline bool write(A x,B ...y){ n   return write(x)||write(y...); n  } n  //no need to call flush at the end manually! n  struct Flusher_ {~Flusher_(){flush();}}io_flusher_; n } n using io :: read; n using io :: putc; n using io :: write; n int p[200005]; n int a[200005],b[200005]; n int to[200005],mn[200005]; n int fa[19][200005],f[19][200005]; n int stk[200005],cnt[200005]; n int query(int l,int r){ n  if(l==r)return 0; n  int ans=0; n  for(int i=18;~i;--i)if(fa[i][l]<=r){ n   ans+=f[i][l];l=fa[i][l]; n  } n  return ans; n } n int ans[200005]; n vector<pii> xa[200005],xd[200005]; n void add(int l,int r,int id){ n  xa[r].emplace_back(l,id); n } n void del(int l,int r,int id){ n  xd[r].emplace_back(l,id); n } n set<int> st; n bitset<200005>cur; n int t; n int ask(int x){ n  int v=lower_bound(stk+1,stk+t+1,x)-stk; n  return cnt[t]-cnt[v]+query(x,stk[v]); n } n signed main(){ n #ifdef QAQAutoMaton  n  freopen( F.in , r ,stdin); n  freopen( F.out , w ,stdout); n #endif n  int n,q;  n  read(n,q); n  for(int i=1;i<=n;++i)read(p[i]); n  p[n+1]=n+1; n  for(int i=1;i<=n;++i)read(a[i]); n  for(int i=1;i<=n;++i)read(b[i]); n  stk[0]=n+1; n  for(int i=0;i<=18;++i){ n   fa[i][n+1]=n+1;f[i][n+1]=0; n  } n  for(int i=n;i;--i){ n   while(p[stk[t]]<p[i])--t; n   int j=stk[t]; n   stk[++t]=i; n   to[i]=j; n   mn[i]=min(b[i],a[i]+query(i+1,j)); n   f[0][i]=mn[i];fa[0][i]=to[i]; n   //write(i,   ,to[i],   ,mn[i],  n ); n   for(int j=1;j<=18;++j){fa[j][i]=fa[j-1][fa[j-1][i]];f[j][i]=f[j-1][i]+f[j-1][fa[j-1][i]];} n  } n  st.insert(1);st.insert(n+1); n  add(1,n+1,0); n  for(int i=1;i<=q;++i){ n   int x; n   read(x); n   if(x==1)continue; n   if(cur[x]){ n    auto it=st.lower_bound(x); n    auto l=it,r=it; n    --l;++r; n    add(*l,*r,i); n    del(*l,x,i); n    del(x,*r,i); n    st.erase(it); n    cur[x]=0; n   } n   else{ n    cur[x]=1; n    auto r=st.lower_bound(x); n    auto l=r; n    --l; n    del(*l,*r,i); n    add(*l,x,i); n    add(x,*r,i); n    st.insert(x); n   } n  } n  stk[t=0]=0; n  p[0]=n+2; n  for(int i=1;i<=n+1;++i){ n   while(p[stk[t]]<p[i]){ n    --t;  n   } n   stk[++t]=i; n   cnt[t]=cnt[t-1]+(a[stk[t-1]]+query(stk[t-1]+1,stk[t])); n   for(auto x:xa[i]){ans[x.y]+=ask(x.x);} n   for(auto x:xd[i]){ans[x.y]-=ask(x.x);} n  } n  for(int i=1;i<=q;++i){ n   ans[i]+=ans[i-1]; n   write(ans[i],  n ); n  } n  return 0; n } n  n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n struct BIT { n   vector<int> tree; n   BIT(int sz) { tree = vector<int>(sz + 1, 0); } n   void upd(int idx, int v) { n     for (idx++; idx < tree.size(); idx += idx & -idx) { n       tree[idx] += v; n     } n   } n   int query(int idx) { n     int res = 0; n     for (idx++; idx > 0; idx -= idx & -idx) { n       res += tree[idx]; n     } n     return res; n   } n }; n int n, l, ss[100000]; n long long w, res; n bool solved[100000]; n vector<pair<int, long long> > adj[100000]; n void dfs1(int node, int par = -1) { n   ss[node] = 1; n   for (const pair<int, long long> edge : adj[node]) { n     int next = edge.first; n     if (next != par && !solved[next]) { n       dfs1(next, node); n       ss[node] += ss[next]; n     } n   } n } n int find(int node) { n   dfs1(node); n   int par = -1; n   int total = ss[node]; n   while (1) { n     bool breakout = true; n     for (const pair<int, long long> edge : adj[node]) { n       int next = edge.first; n       if (!solved[next] && next != par && ss[next] * 2 > total) { n         par = node; n         node = next; n         breakout = false; n         break; n       } n     } n     if (breakout) { n       break; n     } n   } n   return node; n } n vector<pair<int, long long> > combine( n     vector<vector<pair<int, long long> > >& vec) { n   vector<pair<int, long long> > res; n   for (const vector<pair<int, long long> > v : vec) { n     res.insert(res.end(), (v).begin(), (v).end()); n   } n   return res; n } n long long count(const vector<pair<int, long long> >& _v) { n   vector<pair<int, long long> > v(_v); n   sort((v).begin(), (v).end(), n        [](pair<int, long long>& a, pair<int, long long>& b) -> bool { n          return make_pair(a.second, a.first) < make_pair(b.second, b.first); n        }); n   vector<int> fVals; n   for (const pair<int, long long> p : v) { n     fVals.push_back(p.first); n   } n   sort((fVals).begin(), (fVals).end()); n   long long ret = 0; n   int j = 0; n   BIT x(fVals.size()); n   for (int i = (v.size()) - 1; i >= 0; i--) { n     while (j < i && v[j].second + v[i].second <= w) { n       x.upd(lower_bound((fVals).begin(), (fVals).end(), v[j++].first) - n                 fVals.begin(), n             1); n     } n     if (j > i) { n       x.upd(lower_bound((fVals).begin(), (fVals).end(), v[--j].first) - n                 fVals.begin(), n             -1); n     } n     ret += x.query(upper_bound((fVals).begin(), (fVals).end(), l - v[i].first) - n                    fVals.begin() - 1); n   } n   return ret; n } n void dfs2(int node, int par, vector<pair<int, long long> >& v, int depth = 0, n           long long depth2 = 0) { n   v.push_back(make_pair(depth, depth2)); n   for (const pair<int, long long> edge : adj[node]) { n     int next = edge.first; n     if (next != par && !solved[next]) { n       dfs2(next, node, v, depth + 1, depth2 + edge.second); n     } n   } n } n void solve(int node) { n   node = find(node); n   vector<vector<pair<int, long long> > > vec; n   vec.push_back(vector<pair<int, long long> >(1, make_pair(0, 0))); n   for (const pair<int, long long> edge : adj[node]) { n     int next = edge.first; n     if (!solved[next]) { n       vec.push_back(vector<pair<int, long long> >()); n       dfs2(next, node, vec.back(), 1, edge.second); n     } n   } n   for (vector<pair<int, long long> >& v : vec) { n     res -= count(v); n   } n   res += count(combine(vec)); n   solved[node] = true; n   for (const pair<int, long long> edge : adj[node]) { n     int next = edge.first; n     if (!solved[next]) { n       solve(next); n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> l >> w; n   for (int i = 0; i < (n - 1); i++) { n     int p; n     long long weight; n     cin >> p >> weight; n     p--; n     adj[p].push_back(make_pair(i + 1, weight)); n     adj[i + 1].push_back(make_pair(p, weight)); n   } n   solve(0); n   cout << res <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n void A() { n   cout <<  tokitsukaze  << endl; n   exit(0); n } n void B() { n   cout <<  quailty  << endl; n   exit(0); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   set<int> p[2]; n   for (int i = 0; i < n; ++i) { n     p[s[i] -  0 ].insert(i); n   } n   int m[2], M[2]; n   if (p[0].empty() || p[1].empty()) { n     A(); n   } n   m[0] = *p[0].begin(); n   M[0] = *--p[0].end(); n   m[1] = *p[1].begin(); n   M[1] = *--p[1].end(); n   if (M[0] - m[0] + 1 <= k || M[1] - m[1] + 1 <= k) { n     A(); n   } n   bool b = true; n   for (int i = 0; i < n - k + 1; ++i) { n     bool ok = false; n     int l, r; n     l = min(m[0], i); n     r = max(M[0], i + k - 1); n     if (r - l + 1 <= k) { n       ok = true; n     } n     if (m[1] < i) { n       l = m[1]; n     } else { n       l = *p[1].lower_bound(i + k); n     } n     if (M[1] >= i + k) { n       r = M[1]; n     } else { n       r = *--p[1].lower_bound(i); n     } n     if (r - l + 1 <= k) { n       ok = true; n     } n     if (!ok) { n       b = false; n     } n     ok = false; n     l = min(m[1], i); n     r = max(M[1], i + k - 1); n     if (r - l + 1 <= k) { n       ok = true; n     } n     if (m[0] < i) { n       l = m[0]; n     } else { n       l = *p[0].lower_bound(i + k); n     } n     if (M[0] >= i + k) { n       r = M[0]; n     } else { n       r = *--p[0].lower_bound(i); n     } n     if (r - l + 1 <= k) { n       ok = true; n     } n     if (!ok) { n       ok = false; n     } n   } n   if (b) { n     B(); n   } n   cout <<  once again  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   char ch = getchar(); n   int f = 0, x = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) x = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     f = (f << 1) + (f << 3) + ch -  0 ; n     ch = getchar(); n   } n   return f * x; n } n int a[200005], b[600005], m, n; n bool check(int x) { n   int l = 1, r = 3 * n; n   for (int i = 1; i <= n; i++) { n     while (a[i] - b[l] > x) l++; n     while (b[r] - a[i] > x) r--; n     l++; n     r++; n   } n   return l <= r; n } n int main() { n   n = read(); n   m = read(); n   for (int i = 1; i <= n; i++) a[i] = read(); n   for (int i = 1; i <= n; i++) b[i] = read(); n   for (int i = 1; i <= n; i++) { n     b[i + n] = b[i] - m; n     b[i + 2 * n] = b[i] + m; n   } n   sort(b + 1, b + 3 * n + 1); n   sort(a + 1, a + n + 1); n   int l = 0, r = (m + 1) >> 1, ans = 0; n   while (l <= r) { n     int mid = l + r >> 1; n     if (check(mid)) { n       ans = mid; n       r = mid - 1; n     } else n       l = mid + 1; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n const int MAXN = 100100; n char S[MAXN]; n char T[MAXN]; n int aS[MAXN], aT[MAXN]; n int bS[MAXN], bT[MAXN]; n bool go() { n   int leftS, rightS, leftT, rightT; n   scanf( %d %d %d %d , &leftS, &rightS, &leftT, &rightT); n   int lengthS = rightS - leftS + 1; n   int lengthT = rightT - leftT + 1; n   int as = aS[rightS]; n   if (as > lengthS) as = lengthS; n   int at = aT[rightT]; n   if (at > lengthT) at = lengthT; n   int bs = bS[rightS] - bS[leftS - 1]; n   int bt = bT[rightT] - bT[leftT - 1]; n   if (bs % 2 != bt % 2) { n     return false; n   } n   if (bs > bt) { n     return false; n   } n   if (as < at) { n     return false; n   } n   if (bs == bt) { n     if (as % 3 != at % 3) { n       return false; n     } n   } else { n     if (bs == 0 && as == at) { n       return false; n     } n   } n   return true; n } n void goA(char *s, int *ar) { n   int run = 0; n   for (int i = 1; s[i]; ++i) { n     if (s[i] ==  A ) n       ++run; n     else n       run = 0; n     ar[i] = run; n   } n } n void goB(char *s, int *ar) { n   int run = 0; n   for (int i = 1; s[i]; ++i) { n     if (s[i] ==  B  || s[i] ==  C ) ++run; n     ar[i] = run; n   } n } n int main() { n   scanf( %s , S + 1); n   scanf( %s , T + 1); n   goA(S, aS); n   goA(T, aT); n   goB(S, bS); n   goB(T, bT); n   int q; n   scanf( %d , &q); n   for (int i = 0; i < q; ++i) { n     if (go()) { n       printf( 1 ); n     } else { n       printf( 0 ); n     } n   } n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<int> a(n); n     cout << 2 << endl; n     int prev = (n + n - 1) / 2; n     if ((n + n - 1) % 2 != 0) prev++; n     cout << n <<     << n - 1 << endl; n     for (int i = n - 2; i >= 1; i--) { n       int ad = 0; n       cout << i <<     << prev << endl; n       if ((i + prev) % 2 != 0) ad = 1; n       prev = (i + prev) / 2 + ad; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int l, r; n   cin >> l >> r; n   long long int p = 1; n   long long int sum = 0; n   for (long long int i = 0; i < 32; i++) { n     for (long long int j = 0; j < 32; j++) { n       long long int x = pow(2, i) * pow(3, j); n       if (x > r) break; n       if (x >= l && x <= r) sum++; n     } n   } n   cout << sum; n   return 0; n } n