#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using cd = complex<double>; n const int max_n = 200500; n vector<int> g[max_n]; n vector<int> og[max_n]; n vector<int> used(max_n); n bool find_cycles(int x) { n   used[x] = 1; n   for (int i : g[x]) { n     if (used[i] == 1) { n       return true; n     } n     if (!used[i] && find_cycles(i)) { n       return true; n     } n   } n   used[x] = 2; n   return false; n } n vector<int> ts; n void top_sort(int x) { n   used[x] = 1; n   for (int i : g[x]) { n     if (!used[i]) { n       top_sort(i); n     } n   } n   ts.push_back(x); n } n vector<int> dp1(max_n); n void calc_dp1(int x) { n   used[x] = 1; n   dp1[x] = x; n   for (int i : og[x]) { n     if (!used[i]) { n       calc_dp1(i); n     } n     dp1[x] = min(dp1[x], dp1[i]); n   } n } n vector<int> dp2(max_n); n void calc_dp2(int x) { n   used[x] = 1; n   dp2[x] = x; n   for (int i : g[x]) { n     if (!used[i]) { n       calc_dp2(i); n     } n     dp2[x] = min(dp2[x], dp2[i]); n   } n } n int main() { n   cout.precision(20); n   cout << fixed; n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     g[a].push_back(b); n     og[b].push_back(a); n   } n   for (int i = 1; i <= n; i++) { n     if (!used[i]) { n       if (find_cycles(i)) { n         cout << -1; n         return 0; n       } n     } n   } n   fill(used.begin(), used.end(), 0); n   for (int i = 1; i <= n; i++) { n     if (!used[i]) { n       top_sort(i); n     } n   } n   reverse(ts.begin(), ts.end()); n   fill(used.begin(), used.end(), 0); n   for (int i = 0; i < n; i++) { n     if (!used[ts[i]]) { n       calc_dp1(ts[i]); n     } n   } n   fill(used.begin(), used.end(), 0); n   for (int i = 0; i < n; i++) { n     if (!used[ts[i]]) { n       calc_dp2(ts[i]); n     } n   } n   int cnt = 0; n   for (int i = 1; i <= n; i++) { n     cnt += dp1[i] == i && dp2[i] == i; n   } n   cout << cnt <<   n ; n   for (int i = 1; i <= n; i++) { n     cout << (dp1[i] == i && dp2[i] == i ?  A  :  E ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int x[2][2]; n int main() { n   for (int i = 0; i < 2; i++) n     for (int j = 0; j < 2; j++) scanf( %d , x[i] + j); n   printf( %d n , n          (max(1, abs(x[0][0] - x[1][0])) + max(1, abs(x[0][1] - x[1][1])) + 2) * n              2); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int n, k; n int c[5005], h[11], f[505]; n int cnt[100005]; n int fcnt[100005]; n int cache[5005][505]; n int solve(int idx1, int idx2) { n   if (idx1 == 0 || idx2 == 0) return 0; n   int &ret = cache[idx1][idx2]; n   if (ret != -1) return ret; n   ret = 0; n   for (int i = 0; i <= k && i <= idx1; i++) n     ret = max(ret, solve(idx1 - i, idx2 - 1) + h[i]); n   return ret; n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n * k; i++) { n     scanf( %d , &c[i]); n     cnt[c[i]]++; n   } n   for (int i = 0; i < n; i++) { n     scanf( %d , &f[i]); n     fcnt[f[i]]++; n   } n   for (int i = 1; i <= k; i++) scanf( %d , &h[i]); n   int ans = 0; n   for (int i = 0; i < 100005; i++) { n     if (fcnt[i]) { n       memset(cache, -1, sizeof(cache)); n       ans += solve(cnt[i], fcnt[i]); n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const mxn = 1e3 + 10; n vector<pair<int, int> > ha[3], adj0[mxn][mxn], adj1[mxn][mxn]; n int mark[mxn][mxn][3], fl[mxn][mxn][3]; n int n, m; n int ans = 1e9; n string l[mxn]; n void connect(int i, int j, int i2, int j2) { n   if (i2 < 0 or i2 >= n or j2 < 0 or j2 >= m) n     return; n   else { n     if (l[i2][j2] ==  . ) n       adj1[i][j].push_back({i2, j2}); n     else if (l[i2][j2] !=  # ) n       adj0[i][j].push_back({i2, j2}); n   } n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> l[i]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (l[i][j] ==  1  or l[i][j] ==  2  or l[i][j] ==  3 ) { n         ha[l[i][j] -  1 ].push_back({i, j}); n       } n       connect(i, j, i + 1, j); n       connect(i, j, i - 1, j); n       connect(i, j, i, j + 1); n       connect(i, j, i, j - 1); n     } n   } n   for (int now = 0; now <= 2; now++) { n     queue<pair<int, int> > q; n     for (int i = 0; i < ha[now].size(); i++) { n       q.push(ha[now][i]); n       fl[ha[now][i].first][ha[now][i].second][now] = 0; n       mark[ha[now][i].first][ha[now][i].second][now] = 1; n     } n     while (q.size()) { n       int v1, v2; n       v1 = q.front().first, v2 = q.front().second; n       q.pop(); n       queue<pair<int, int> > q0; n       q0.push({v1, v2}); n       while (q0.size()) { n         int a1, a2; n         a1 = q0.front().first, a2 = q0.front().second; n         q0.pop(); n         for (int j = 0; j < adj0[a1][a2].size(); j++) { n           int u1 = adj0[a1][a2][j].first, u2 = adj0[a1][a2][j].second; n           if (!mark[u1][u2][now]) { n             q0.push({u1, u2}); n             q.push({u1, u2}); n             mark[u1][u2][now] = 1; n             fl[u1][u2][now] = fl[v1][v2][now]; n           } n         } n       } n       for (int j = 0; j < adj1[v1][v2].size(); j++) { n         int k1 = adj1[v1][v2][j].first, k2 = adj1[v1][v2][j].second; n         if (!mark[k1][k2][now]) { n           mark[k1][k2][now] = 1; n           q.push({k1, k2}); n           fl[k1][k2][now] = fl[v1][v2][now] + 1; n         } n       } n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (l[i][j] ==  .  and mark[i][j][0] and mark[i][j][1] and mark[i][j][2]) n         ans = min(ans, (fl[i][j][0] + fl[i][j][2] + fl[i][j][1]) - 2); n       else if (l[i][j] !=  #  and mark[i][j][0] and mark[i][j][1] and n                mark[i][j][2]) n         ans = min(ans, fl[i][j][0] + fl[i][j][2] + fl[i][j][1]); n     } n   } n   if (ans == 1e9) n     cout << -1; n   else n     cout << ans; 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 long long getPairsCount(int arr[], int n, long long sum) { n   unordered_map<int, int, custom_hash> m; n   for (int i = 0; i < n; i++) m[arr[i]]++; n   long long twice_count = 0; n   for (int i = 0; i < n; i++) { n     twice_count += m[sum - arr[i]]; n     if (sum - arr[i] == arr[i]) twice_count--; n   } n   return twice_count / 2; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int ara[n]; n     long long sum = 0; n     for (int i = 0; i < n; i++) { n       cin >> ara[i]; n       sum += ara[i]; n     } n     sum *= 2; n     if (sum % n) { n       cout <<  0 n ; n       continue; n     } n     cout << getPairsCount(ara, n, sum / n) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const pair<int, int> MOD = make_pair(1e9 + 7, 1e9 + 9); n const int base = 1e6 + 3; n const int N = 1e5 + 100, B = 600; n pair<int, int> operator+(pair<int, int> a, pair<int, int> b) { n   return make_pair((a.first + b.first) % MOD.first, n                    (a.second + b.second) % MOD.second); n } n pair<int, int> operator-(pair<int, int> a, pair<int, int> b) { n   return make_pair((a.first - b.first + MOD.first) % MOD.first, n                    (a.second - b.second + MOD.second) % MOD.second); n } n pair<int, int> operator*(pair<int, int> a, int x) { n   return make_pair(1ll * a.first * x % MOD.first, n                    1ll * a.second * x % MOD.second); n } n pair<int, int> operator*(pair<int, int> a, pair<int, int> b) { n   return make_pair(1ll * a.first * b.first % MOD.first, n                    1ll * a.second * b.second % MOD.second); n } n int n, K, q; n int a[N]; n vector<pair<int, int>> open_Hash[B], close_Hash[B]; n pair<int, int> base_pow[N], inv_pow[N]; n int good[N]; n int bm(int b, int p, int m) { n   if (p == 0) return 1; n   int t = bm(b, p / 2, m); n   t = 1ll * t * t % m; n   if (p & 1) return 1ll * b * t % m; n   return t; n } n void pre() { n   base_pow[0] = {1, 1}; n   inv_pow[0] = {1, 1}; n   for (int i = 1; i < N; i++) { n     base_pow[i] = base_pow[i - 1] * base; n     inv_pow[i].first = bm(base_pow[i].first, MOD.first - 2, MOD.first); n     inv_pow[i].second = bm(base_pow[i].second, MOD.second - 2, MOD.second); n   } n } n bool recal(int l, int r, vector<pair<int, int>> &close, n            vector<pair<int, int>> &open) { n   vector<int> stk; n   for (int i = l; i <= r; i++) { n     if (a[i] < 0 && stk.size() > 0 && stk.back() == abs(a[i])) n       stk.pop_back(); n     else n       stk.push_back(a[i]); n   } n   close.clear(); n   open.clear(); n   bool pos = false; n   for (int i = 0; i < stk.size(); i++) { n     if (stk[i] > 0) pos = true; n     if (stk[i] < 0 && pos) return false; n   } n   for (int i = 0; i < stk.size(); i++) { n     if (stk[i] > 0) break; n     int x = abs(stk[i]); n     int kth = close.size(); n     close.push_back(base_pow[kth] * x); n   } n   for (int i = 1; i < close.size(); i++) { n     close[i] = close[i - 1] + close[i]; n   } n   for (int i = (int)stk.size() - 1; i >= 0; i--) { n     if (stk[i] < 0) break; n     int x = abs(stk[i]); n     int kth = open.size(); n     open.push_back(base_pow[kth] * x); n   } n   for (int i = 1; i < open.size(); i++) { n     open[i] = open[i - 1] + open[i]; n   } n   return true; n } n bool find_hash(vector<pair<int, int>> &all_open, int sz, pair<int, int> hhh) { n   assert(sz > 0); n   int psz = 0; n   pair<int, int> h = {0, 0}; n   while (psz < sz && all_open.size() > 0) { n     auto t = all_open.back(); n     all_open.pop_back(); n     int which_block = t.first, idx = t.second; n     int cur_sz = open_Hash[which_block].size() - idx; n     assert(cur_sz > 0); n     if (cur_sz + psz < sz) { n       pair<int, int> hh = n           (open_Hash[which_block].back() - n            (idx > 0 ? open_Hash[which_block][idx - 1] : make_pair(0, 0))) * n           inv_pow[idx]; n       hh = hh * base_pow[psz]; n       h = h + hh; n       psz += cur_sz; n     } else { n       int lage = sz - psz; n       pair<int, int> hh = n           (open_Hash[which_block][idx + lage - 1] - n            (idx > 0 ? open_Hash[which_block][idx - 1] : make_pair(0, 0))) * n           inv_pow[idx]; n       hh = hh * base_pow[psz]; n       h = h + hh; n       if (idx + lage < open_Hash[which_block].size()) n         all_open.push_back({which_block, idx + lage}); n       psz += lage; n     } n   } n   if (psz < sz) return 0; n   if (hhh != h) return 0; n   return 1; n } n int query(int l, int r) { n   int bl = l / B, br = r / B; n   if (bl == br) { n     vector<pair<int, int>> p, s; n     int x = recal(l, r, p, s); n     if (x && (int)p.size() == 0 && (int)s.size() == 0) return 1; n     return 0; n   } n   if (recal(l, (bl + 1) * B - 1, close_Hash[B - 2], open_Hash[B - 2]) == 0) n     return 0; n   if (close_Hash[B - 2].size() > 0) return 0; n   vector<pair<int, int>> all_open; n   if (open_Hash[B - 2].size() > 0) all_open.push_back({B - 2, 0}); n   for (int b = bl + 1; b < br; b++) { n     if (!good[b]) return 0; n     if (close_Hash[b].size() == 0) { n       if (open_Hash[b].size() > 0) all_open.push_back({b, 0}); n       continue; n     } n     int sz = close_Hash[b].size(); n     if (!find_hash(all_open, sz, close_Hash[b].back())) return 0; n     if (open_Hash[b].size() > 0) all_open.push_back({b, 0}); n   } n   if (recal(br * B, r, close_Hash[B - 1], open_Hash[B - 1]) == 0) return 0; n   if (open_Hash[B - 1].size() > 0) return 0; n   if (close_Hash[B - 1].size() > 0 && n       !find_hash(all_open, close_Hash[B - 1].size(), close_Hash[B - 1].back())) n     return 0; n   if (all_open.size() > 0) return 0; n   return 1; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   pre(); n   cin >> n >> K; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0, j = 0; i < n; i += B, j++) { n     good[j] = recal(i, min(i + B - 1, n - 1), close_Hash[j], open_Hash[j]); n   } n   int q; n   cin >> q; n   while (q--) { n     int t; n     cin >> t; n     if (t == 1) { n       int idx, x; n       cin >> idx >> x; n       idx--; n       a[idx] = x; n       int b = idx / B; n       int l = b * B, r = min(l + B - 1, n - 1); n       good[b] = recal(l, r, close_Hash[b], open_Hash[b]); n     } else { n       int l, r; n       cin >> l >> r; n       l--, r--; n       if (query(l, r)) n         cout <<  Yes  << endl; n       else n         cout <<  No  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n template <typename X> n inline bool minimize(X& p, X q) { n   if (p <= q) return 0; n   p = q; n   return 1; n } n template <typename X> n inline bool maximize(X& p, X q) { n   if (p >= q) return 0; n   p = q; n   return 1; n } n const int MAXN = 100005; n int Ya[MAXN], Yb[MAXN], L[MAXN]; n inline double dis(int dx, int dy) { n   return sqrt(1.0 * dx * dx + 1.0 * dy * dy); n } n int main() { n   int N, M, a, b; n   scanf( %d%d%d%d , &N, &M, &a, &b); n   for (int i = 1; i <= N; ++i) scanf( %d , Ya + i); n   for (int i = 1; i <= M; ++i) scanf( %d , Yb + i); n   for (int i = 1; i <= M; ++i) scanf( %d , L + i); n   double ans = 1e100; n   int ap = 1, bp = 1; n   for (int i = 1, j = 1; i <= M; ++i) { n     int temp = ceil((double)a * Yb[i] / b); n     while (j < N && Ya[j] < temp) ++j; n     if (minimize(ans, dis(a, Ya[j]) + dis(b - a, Yb[i] - Ya[j]) + L[i])) n       ap = j, bp = i; n     if (j != 1 && n         minimize(ans, dis(a, Ya[j - 1]) + dis(b - a, Yb[i] - Ya[j - 1]) + L[i])) n       ap = j - 1, bp = i; n   } n   printf( %d %d n , ap, bp); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline bool chkmin(T &x, T y) { n   return y < x ? x = y, 1 : 0; n } n template <class T> n inline bool chkmax(T &x, T y) { n   return x < y ? x = y, 1 : 0; n } n inline long long Max(long long x, long long y) { return x > y ? x : y; } n inline long long Min(long long x, long long y) { return x < y ? x : y; } n int n, m; n long long dp[2002][2002]; n void Add(long long &x, long long y) { n   x += y; n   if (x >= 1000000007) x %= 1000000007; n   if (x < 0) (x += 1000000007) %= 1000000007; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = (1), i_end_ = (n); i <= i_end_; i++) dp[i][1] = 1; n   for (int k = (2), k_end_ = (m); k <= k_end_; k++) n     for (int i = (1), i_end_ = (n); i <= i_end_; i++) n       for (int j = i; j <= n; j += i) Add(dp[j][k], dp[i][k - 1]); n   long long ans = 0; n   for (int i = (1), i_end_ = (n); i <= i_end_; i++) Add(ans, dp[i][m]); n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long t, t0, t2, x, x2; n void cmp(long double &temp, long long &o, long long &o2, long long y, n          long long y2) { n   if (y2 > x2) return; n   long double newTemp = (1.0 * t * y + t2 * y2) / (y + y2); n   if (newTemp < t0) return; n   if (newTemp > temp + 1e-9) return; n   if (newTemp < temp || y + y2 > o + o2) o = y, o2 = y2, temp = newTemp; n } n int main() { n   cin >> t >> t2 >> x >> x2 >> t0; n   if (t0 == t2) { n     if (t != t0) n       cout << 0 <<     << x2 << endl; n     else n       cout << x <<     << x2 << endl; n   } else if (t0 == t) n     cout << x <<     << 0 << endl; n   else { n     long double temp = 1e9; n     long long o = 0, o2 = 0; n     cmp(temp, o, o2, 0, x2); n     cmp(temp, o, o2, x, 0); n     for (long long y = 0; y <= x; y++) { n       long long y2 = (y * (t0 - t) + t2 - t0 - 1) / (t2 - t0); n       cmp(temp, o, o2, y, y2); n     } n     cout << o <<     << o2 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n using vpii = vector<pii>; n using vpll = vector<pll>; n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   for (const auto &x : v) os << x <<    ; n   return os << endl; n } n ll ceil_div(ll a, ll b) { return (a + b - 1) / b; } n void solve(int n) { n   if (n == 1) { n     cout << -1 << endl; n     return; n   } n   for (int i = 0; i < n; i++) { n     cout << (i == 0 ? 2 : 3); n   } n   cout << endl; n } n int main(int argc, char const *argv[]) { n   int T; n   cin >> T; n   for (int t = 0; t < T; t++) { n     int n; n     cin >> n; n     solve(n); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #define cp ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); n #define ll long long int   n #define vint vector<int> n #define vllint vector<ll> n #define pair_int pair<int , int> n #define pair_ll_int pair<ll , ll> n #define vector_of_pair_int vector<pair_int> n #define pb push_back  n #define test_case int t; cin>>t; while(t--) n #define umap unordered_map <int,int>  n #define mapint map<int,int> n #define infinity 1000000000 n #define mp make_pair n  n int main()  n {  n  cp n      n     test_case n     { n        ll n; n        cin>>n; n  n        if(n<=256) n        { n          int count=0;         n          ll temp=n; n          while(temp!=1) n          { n           count++; n           if(temp%2==0) n            temp=temp/2; n           else n            temp=temp/2+1; n          } n  n          cout<<count+n-3<<endl; n          n          for (int i = 1; i <=n-3; ++i) n          { n           /* code */ n            cout<<3+i-1<<   <<3+i<<endl; n          } n  n          for (int i = 0; i < count; ++i) n          { n           /* code */ n           cout<<n<<   <<2<<endl; n          } n  n           n  n  n  n  n        } n        else n        { n          int count=0;         n          ll temp=n; n          while(temp!=1) n          { n           count++; n            n           temp=ceil(float(temp)/float(16)); n          } n  n          cout<<count+n<<endl; n           n          for (int i = 3; i <n; ++i) n          { n           /* code */ n              if(i!=16) n              { n               cout<<i<<   <<i+1<<endl; n              } n  n          } n            n            for (int i = 0; i < count; ++i) n          { n           /* code */ n           cout<<n<<   <<16<<endl; n          } n           n          for (int i = 0; i < 4; ++i) n          { n           /* code */ n           cout<<16<<   <<2<<endl; n          } n           n          n        } n     } n  n     return 0;  n }


#include <bits/stdc++.h> n using namespace std; n const int N = 205; n int mem[N][N]; n int asol[N]; n bool query(int i, int j) { n   i = asol[i]; n   j = asol[j]; n   if (mem[i][j] != -1) return mem[i][j]; n   if (i == j) return false; n   cout <<  ?   << i <<     << j << endl; n   char c; n   cin >> c; n   mem[i][j] = (c ==  < ); n   mem[j][i] = !mem[i][j]; n   return mem[i][j]; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     memset(mem, -1, sizeof mem); n     int n; n     cin >> n; n     for (int i = 1; i <= 2 * n; i++) asol[i] = i; n     shuffle(asol + 1, asol + 2 * n + 1, mt19937(23232)); n     vector<int> worse; n     for (int i = 1; i < 2 * n; i += 2) { n       if (query(i, i + 1)) n         worse.push_back(i); n       else n         worse.push_back(i + 1); n     } n     sort(worse.begin(), worse.end(), query); n     int xx = worse.back(); n     worse.pop_back(); n     int yy = worse.back(); n     vector<int> choto, boro; n     for (int i = 1; i <= 2 * n; i++) { n       if (i == yy) continue; n       if (query(yy, i)) n         boro.push_back(i); n       else n         choto.push_back(i); n     } n     if (boro.size() <= n) { n       sort(choto.begin(), choto.end(), query); n       cout <<  !  << endl; n     } else { n       int xxpr = (xx % 2 ? xx + 1 : xx - 1); n       vector<int> worse; n       if (query(xx, xxpr)) n         worse.push_back(xx); n       else n         worse.push_back(xxpr); n       int unpaired = -1; n       for (int x : boro) { n         if (x == xx || x == xxpr) continue; n         if (unpaired == -1) n           unpaired = x; n         else { n           if (query(unpaired, x)) n             worse.push_back(unpaired); n           else n             worse.push_back(x); n           unpaired = -1; n         } n       } n       if (unpaired != -1) worse.push_back(unpaired); n       sort(worse.begin(), worse.end(), query); n       cout <<  !  << endl; n     } n   } n } n


#include <bits/stdc++.h> n  n using namespace std; n  n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int tt; n   cin >> tt; n   while(tt--) { n     int n; n     cin >> n; n     vector<int> a(n); n     for(int i = 0; i < n; i++) { n       cin >> a[i]; n     } n     set<int> s; n     s.insert(a[0]); n     bool ok = true; n     for(int i = 1; i < n; i++) { n       int x = min(a[i], a[i - 1]); n       int y = max(a[i], a[i - 1]); n       if(s.upper_bound(x) != s.end()) { n         if(*s.upper_bound(x) < y) { n           ok = false; n           break; n         } n       } n       s.insert(a[i]); n     } n     cout << (ok ?  YES  :  NO ) <<   n ; n   } n   return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a[3]; n   int x[3]; n   int last = 0; n   cin >> a[0] >> a[1] >> a[2]; n   cin >> x[0] >> x[1] >> x[2]; n   for (int i = 0; i < 3; ++i) { n     int t = a[i] - x[i]; n     if (t > 0) t /= 2; n     last += t; n   } n   if (last < 0) n     cout <<  No n ; n   else n     cout <<  Yes n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = -1e8; n const double Pi = acos(-1); n bool inline equ(double a, double b) { return fabs(a - b) < EPS; } n int _R(int& x) { return scanf( %d , &x); } n int _R(long long& x) { return scanf( %  PRId64, &x); } n int _R(double& x) { return scanf( %lf , &x); } n int _R(char* s) { return scanf( %s , s); } n int R() { return 0; } n template <typename T1, typename... T2> n int R(T1& x, T2&... tail) { n   int tmp = _R(x); n   return tmp + R(tail...); n } n template <typename Iter, typename F> n inline void out(Iter s, Iter e, F of) { n   bool flag = 0; n   for (Iter it = s; it != e; it++) { n     if (flag) n       printf(   ); n     else n       flag = 1; n     of(*it); n   } n   puts(  ); n } n const int MAXN = 1e5 + 10; n struct Edge { n   int u, v; n   long long c; n   bool operator<(const Edge& e) const { return c < e.c; } n }; n int n, m; n Edge es[MAXN * 2]; n struct DSJ { n   int mom[MAXN], sz[MAXN]; n   void init() { n     for (int i = (1); i <= (n); i++) mom[i] = i; n   } n   int find(int x) { return mom[x] = mom[x] == x ? x : find(mom[x]); } n   void merge(int a, int b) { n     a = find(a), b = find(b); n     mom[b] = a; n   } n } dsj; n struct Tree { n   static const int LOG = 22; n   vector<pair<int, int> > g[MAXN]; n   int dep[MAXN]; n   int mom[LOG][MAXN], mx[LOG][MAXN]; n   int sz[MAXN], root[MAXN]; n   inline void adde(Edge& e) { n     g[e.u].push_back(pair<int, int>(e.v, e.c)); n     g[e.v].push_back(pair<int, int>(e.u, e.c)); n   } n   void dfs(int u, int p, int c, int d, int rt) { n     mom[0][u] = p; n     mx[0][u] = c; n     dep[u] = d; n     root[u] = rt; n     for (pair<int, int> e : g[u]) { n       int v, tc; n       tie(v, tc) = e; n       if (v == p) continue; n       dfs(v, u, tc, d + 1, rt); n     } n   } n   inline void predo() { n     for (int i = (1); i <= (n); i++) n       if (!dep[i]) { n         dfs(i, -1, 0, 1, i); n       } n     for (int j = (1); j <= (LOG - 1); j++) n       for (int i = (1); i <= (n); i++) { n         mom[j][i] = mom[j - 1][mom[j - 1][i]]; n         mx[j][i] = max(mx[j - 1][i], mx[j - 1][mom[j - 1][i]]); n       } n     for (int i = (1); i <= (n); i++) sz[root[i]]++; n   } n   inline int lca(int a, int b) { n     if (dep[a] < dep[b]) swap(a, b); n     int jmp = dep[a] - dep[b]; n     for (int k = 0; k < (LOG); k++) n       if ((jmp >> k) & 1) a = mom[k][a]; n     if (a == b) return a; n     for (int k = (LOG - 1); k >= (0); k--) n       if (mom[k][a] != mom[k][b]) { n         a = mom[k][a]; n         b = mom[k][b]; n       } n     return mom[0][a]; n   } n   inline int qry(int a, int b) { n     int anc = lca(a, b); n     int res = 0; n     { n       int jmp = dep[a] - dep[anc]; n       for (int k = 0; k < (LOG); k++) n         if ((jmp >> k) & 1) { n           res = max(res, mx[k][a]); n           a = mom[k][a]; n         } n     } n     { n       int jmp = dep[b] - dep[anc]; n       for (int k = 0; k < (LOG); k++) n         if ((jmp >> k) & 1) { n           res = max(res, mx[k][b]); n           b = mom[k][b]; n         } n     } n     return res; n   } n } tree; n int main() { n   R(n, m); n   for (int i = (1); i <= (m); i++) R(es[i].u, es[i].v, es[i].c); n   sort(es + 1, es + 1 + m); n   long long mst_cst = 0ll; n   dsj.init(); n   for (int i = (1); i <= (m); i++) { n     Edge& e = es[i]; n     if (dsj.find(e.u) != dsj.find(e.v)) { n       dsj.merge(e.u, e.v); n       mst_cst += e.c; n       tree.adde(e); n     } n   } n   tree.predo(); n   int q; n   R(q); n   while (q--) { n     int a, b; n     R(a, b); n     if (tree.root[a] == tree.root[b]) { n       if (tree.sz[tree.root[a]] < n) n         puts( -1 ); n       else n         printf( %I64d n , mst_cst - tree.qry(a, b)); n     } else { n       if (tree.sz[tree.root[a]] + tree.sz[tree.root[b]] < n) n         puts( -1 ); n       else n         printf( %I64d n , mst_cst); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n #define rep(i, a, b) for(int i = a; i < b; ++i) n  n int debug = 0; n  n const int N = 510; n  n int n, p[N], a[N][N]; n  n int main() { n     cin >> n; n     rep(i, 1, n + 1) { n         cin >> p[i]; n     } n     rep(i, 1, n + 1) { n         int x = i, y = i; n         rep(_, 0, p[i]) { n             a[x][y] = p[i]; n             if (y > 1 && a[x][y - 1] == 0) { n                 --y; n             } else { n                 ++x; n             } n         } n     } n     rep(i, 1, n + 1) { n         rep(j, 1, i + 1) { n             cout << a[i][j] <<    ; n         } n         cout <<   n ; n     } n }


#include <bits/stdc++.h> n using namespace std; n long long getmax(long long arr[]) { n   long long m = 0; n   for (long long i = 0; i < 26; i++) { n     m = max(m, arr[i]); n   } n   return m; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, m; n   cin >> n >> m; n   string arr[n]; n   long long alpha[26]; n   long long res = 0; n   for (long long i = 0; i < n; i++) cin >> arr[i]; n   long long marks[m]; n   for (long long i = 0; i < m; i++) cin >> marks[i]; n   for (long long i = 0; i < m; i++) { n     memset(alpha, 0, sizeof(alpha)); n     for (long long j = 0; j < n; j++) { n       int k = arr[j][i] -  A ; n       alpha[k]++; n     } n     long long c = getmax(alpha); n     res += c * marks[i]; n   } n   cout << res <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n struct ee { n   int d; n   int y; n }; n ee e[120]; n ee r[1200]; n bool ok(ee a, ee b) { return a.y > b.y; } n int main() { n   int n; n   int k; n   while (cin >> n >> k) { n     memset(e, 0, sizeof(e)); n     memset(r, 0, sizeof(r)); n     for (int i = 0; i < n; i++) { n       cin >> e[i].y; n       e[i].d = i + 1; n     } n     sort(e, e + n, ok); n     int m = 0, m2 = 0; n     int ss; n     for (int i = 0; i < k; i++) { n       ss = e[0].y - e[n - 1].y; n       if (ss <= 1) { n         break; n       } n       e[0].y--; n       e[n - 1].y++; n       r[i].d = e[0].d; n       r[i].y = e[n - 1].d; n       sort(e, e + n, ok); n       m++; n       if (ss > e[0].y - e[n - 1].y) { n         m2 = m; n         ss = e[0].y - e[n - 1].y; n       } n     } n     cout << ss <<     << m2 << endl; n     for (int i = 0; i < m2; i++) { n       cout << r[i].d <<     << r[i].y << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxk = 50, maxn = 100; n int k, x, n, m; n inline int calc(int x, int y) { n   if (!x && y == 2) return 1; n   return 0; n } n struct node { n   long long c1[2], c2[2][2]; n   bool h, t; n   node operator+(const node &Other) const { n     node N = {}; n     N.h = h; n     N.t = Other.t; n     for (int i = 0; i < 2; ++i) N.c1[i] = c1[i] + Other.c1[i]; n     for (int i = 0; i < 2; ++i) n       for (int j = 0; j < 2; ++j) N.c2[i][j] = c2[i][j] + Other.c2[i][j]; n     ++N.c2[t][Other.h]; n     return N; n   } n } f[maxk + 1] = {}; n int dp[3][3][maxn + 1][maxn + 1]; n int s1[3][3][maxn + 1][maxn + 1], s2[3][3][maxn + 1][maxn + 1]; n int main() { n   scanf( %d%d%d%d , &k, &x, &n, &m); n   f[1].c1[0] = 1; n   f[2].c1[1] = 1; n   f[2].h = f[2].t = 1; n   for (int i = 3; i <= k; ++i) f[i] = f[i - 2] + f[i - 1]; n   if (n > 1) { n     memset(dp, -1, sizeof(dp)); n     for (int i = 0; i < 3; ++i) dp[i][i][1][0] = 0; n     for (int h = 0; h < 3; ++h) { n       for (int i = 2; i <= n; ++i) n         for (int j1 = 0; j1 < 3; ++j1) n           for (int k = 0; k < i - 1; ++k) n             if (~dp[h][j1][i - 1][k]) { n               for (int j2 = 0; j2 < 3; ++j2) n                 dp[h][j2][i][k + calc(j1, j2)] = j1; n             } n       for (int t = 0; t < 3; ++t) n         for (int k = 0; k < n; ++k) n           if (~dp[h][t][n][k] && !s1[h][t][k][0]) { n             s1[h][t][k][0] = 1; n             for (int dep = n, c = t, j = k; dep; --dep) { n               s1[h][t][k][dep] = c; n               int d = dp[h][c][dep][j]; n               j -= calc(d, c); n               c = d; n             } n           } n     } n   } else { n     for (int i = 0; i < 3; ++i) { n       s1[i][i][0][0] = 1; n       s1[i][i][0][1] = i; n     } n   } n   if (m > 1) { n     memset(dp, -1, sizeof(dp)); n     for (int i = 0; i < 3; ++i) dp[i][i][1][0] = 0; n     for (int h = 0; h < 3; ++h) { n       for (int i = 2; i <= m; ++i) n         for (int j1 = 0; j1 < 3; ++j1) n           for (int k = 0; k < i - 1; ++k) n             for (int j2 = 0; j2 < 3; ++j2) n               if (~dp[h][j1][i - 1][k]) dp[h][j2][i][k + calc(j1, j2)] = j1; n       for (int t = 0; t < 3; ++t) n         for (int k = 0; k < m; ++k) n           if (~dp[h][t][m][k]) { n             s2[h][t][k][0] = 1; n             for (int dep = m, c = t, j = k; dep; --dep) { n               s2[h][t][k][dep] = c; n               int d = dp[h][c][dep][j]; n               j -= calc(d, c); n               c = d; n             } n           } n     } n   } else { n     for (int i = 0; i < 3; ++i) { n       s2[i][i][0][0] = 1; n       s2[i][i][0][1] = i; n     } n   } n   for (int h1 = 0; h1 < 3; ++h1) n     for (int t1 = 0; t1 < 3; ++t1) n       for (int i = 0; i < n; ++i) n         if (s1[h1][t1][i][0]) { n           for (int h2 = 0; h2 < 3; ++h2) n             for (int t2 = 0; t2 < 3; ++t2) n               for (int j = 0; j < m; ++j) n                 if (s2[h2][t2][j][0]) { n                   long long cnt = f[k].c1[0] * i + f[k].c1[1] * j; n                   cnt += f[k].c2[0][0] * calc(t1, h1) + n                          f[k].c2[0][1] * calc(t1, h2) + n                          f[k].c2[1][0] * calc(t2, h1) + n                          f[k].c2[1][1] * calc(t2, h2); n                   if (cnt == x) { n                     for (int k = 1; k <= n; ++k) putchar(s1[h1][t1][i][k] + 65); n                     putchar(  n ); n                     for (int k = 1; k <= m; ++k) putchar(s2[h2][t2][j][k] + 65); n                     putchar(  n ); n                     return 0; n                   } n                 } n         } n   puts( Happy new year! ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, i, j, k, l, tot, ans, v; n long long o, p; n long long f[110010]; n struct rec { n   long long x, y; n } a[100010]; n bool cmp(rec x, rec y) { return x.x < y.x || ((x.x == y.x) && (x.y < y.y)); } n int find(int k, int l, long long x) { n   while (k < l) { n     int mid = (k + l) / 2; n     if (f[mid] <= x) n       k = mid + 1; n     else n       l = mid; n   } n   return k; n } n int main() { n   cin >> n; n   for (i = 0; i < n; i++) { n     scanf( %I64d%I64d , &a[i].x, &a[i].y); n   } n   cin >> v; n   for (i = 0; i < n; i++) { n     o = a[i].x; n     p = a[i].y; n     a[i].x = -o + p * v; n     a[i].y = o + p * v; n   } n   sort(a, a + n, cmp); n   tot = 1; n   f[0] = -1e12, f[1] = 1e12; n   for (i = 0; i < n; i++) { n     j = find(0, tot, a[i].y); n     if (f[j] > a[i].y) f[j] = a[i].y; n     if (j >= tot) f[++tot] = 1e12; n   } n   ans = tot - 1; n   f[tot = 1] = 1e12; n   for (i = 0; i < n; i++) n     if ((a[i].x >= 0) && (a[i].y >= 0)) { n       j = find(0, tot, a[i].y); n       if (f[j] > a[i].y) f[j] = a[i].y; n       if (j >= tot) f[++tot] = 1e12; n     } n   printf( %d %d n , tot - 1, ans); n   cin >> n; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n int a[100000]; n int main() { n   int n, k; n   cin >> n >> k; n   int sum = 0; n   bool ok = false; n   vector<int> ans; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     sum += a[i]; n   } n   if (sum % k != 0) { n     printf( No n ); n   } else { n     sum /= k; n     int temp = 0; n     int cnt = 0; n     for (int i = 0; i < n; i++) { n       temp += a[i]; n       cnt++; n       if (temp > sum) { n         printf( No n ); n         ok = true; n         break; n       } else if (temp == sum) { n         temp = 0; n         ans.push_back(cnt); n         cnt = 0; n       } n     } n     if (!ok) { n       printf( Yes n ); n       for (int i = 0; i < ans.size(); i++) { n         if (i > 0) printf(   ); n         printf( %d , ans[i]); n       } n       printf(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int SIZE = 2100; n struct kstr { n   int C[30]; n   bool mask[110]; n } A[SIZE]; n int CC[30]; n int main() { n   int K, N, len; n   char S[110]; n   cin >> K >> S >> N; n   len = strlen(S); n   for (int i = 0; i != len; i++) CC[S[i] -  a ]++; n   for (int i = 0; i != K; i++) { n     for (int j = 0; j != len; j++) A[i].mask[j] = true; n     for (int j = 0; j != 30; j++) A[i].C[j] = CC[j]; n   } n   int a; n   char ch; n   while (N--) { n     cin >> a >> ch; n     int sumch = 0; n     for (int i = 0; i != K; i++) n       if (sumch + A[i].C[ch -  a ] >= a) { n         a -= sumch; n         A[i].C[ch -  a ]--; n         for (int j = 0; j != len; j++) { n           if (S[j] == ch && A[i].mask[j]) a--; n           if (!a) { n             A[i].mask[j] = false; n             break; n           } n         } n         break; n       } else n         sumch += A[i].C[ch -  a ]; n   } n   for (int i = 0; i != K; i++) n     for (int j = 0; j != len; j++) n       if (A[i].mask[j]) cout << S[j]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > v[100000]; n int f[100000]; n int d[100000]; n void dfs(int x) { n   int i; n   for (i = 0; i < v[x].size(); i++) { n     if (f[v[x][i].first] == 0) { n       d[v[x][i].first] = d[x] + v[x][i].second; n       f[v[x][i].first] = 1; n       dfs(v[x][i].first); n     } n   } n } n int main() { n   int n, i, j; n   long long sum = 0; n   scanf( %d , &n); n   for (i = 0; i < n - 1; i++) { n     int x, y, w; n     scanf( %d %d %d , &x, &y, &w); n     v[x - 1].push_back(make_pair(y - 1, w)); n     v[y - 1].push_back(make_pair(x - 1, w)); n     sum += w * 2; n   } n   f[0] = 1; n   dfs(0); n   sort(d, d + n); n   sum -= d[n - 1]; n   printf( %I64d n , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fast_io() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n long long r(long long num, long long den) { n   if (den == 1) n     return num; n   else if (num == 1) n     return den; n   if (num > den) n     return num / den + r(num % den, den); n   else n     return den / num + r(num, den % num); n } n void solve() { n   long long a, b; n   cin >> a >> b; n   cout << r(a, b) << endl; n } n int main() { n   fast_io(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void pisz(int n) { printf( %d n , n); } n const int fx[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; n const int fxx[8][2] = {{0, 1}, {0, -1}, {1, 0},  {-1, 0}, n                        {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; n template <typename T, typename TT> n ostream& operator<<(ostream& s, pair<T, TT> t) { n   return s <<  (  << t.first <<  ,  << t.second <<  ) ; n } n template <typename T> n ostream& operator<<(ostream& s, vector<T> t) { n   for (int i = 0; i < ((int)((t).size())); i++) s << t[i] <<    ; n   return s; n } n int main() { n   int n; n   string A; n   int cursor; n   cin >> n >> cursor >> A; n   cursor--; n   n--; n   cursor = min(cursor, n - cursor); n   int middle = n / 2; n   int leftmost = cursor, rightmost = cursor; n   for (int i = cursor; i <= middle; i++) n     if (A[i] != A[n - i]) rightmost = i; n   for (int i = cursor; i >= 0; i--) n     if (A[i] != A[n - i]) leftmost = i; n   int vertical_moves = 0; n   for (int i = leftmost; i <= rightmost; i++) n     vertical_moves += min(abs(A[i] - A[n - i]), 26 - abs(A[i] - A[n - i])); n   int horizontal_moves = min((cursor - leftmost) + (rightmost - leftmost), n                              (rightmost - cursor) + (rightmost - leftmost)); n   int total_moves = horizontal_moves + vertical_moves; n   cout << total_moves; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int MXN = 1e3 + 7; n const int mod = 998244353; n int n, m; n int ar[MXN][MXN]; n int br[MXN]; n struct lp { n   int m, v; n } A[MXN][MXN], B[MXN][MXN]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) scanf( %d , &ar[i][j]); n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       br[j] = ar[i][j]; n     } n     sort(br + 1, br + 1 + m); n     int k = unique(br + 1, br + 1 + m) - br - 1; n     for (int j = 1; j <= m; ++j) { n       A[i][j].m = k; n       A[i][j].v = lower_bound(br + 1, br + 1 + k, ar[i][j]) - br; n     } n   } n   for (int i = 1; i <= m; ++i) { n     for (int j = 1; j <= n; ++j) { n       br[j] = ar[j][i]; n     } n     sort(br + 1, br + 1 + n); n     int k = unique(br + 1, br + 1 + n) - br - 1; n     for (int j = 1; j <= n; ++j) { n       B[j][i].m = k; n       B[j][i].v = lower_bound(br + 1, br + 1 + k, ar[j][i]) - br; n     } n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       int t = abs(A[i][j].v - B[i][j].v); n       if (A[i][j].v > B[i][j].v) { n         printf( %d  , max(A[i][j].m, B[i][j].m + t)); n       } else if (A[i][j].v < B[i][j].v) { n         printf( %d  , max(A[i][j].m + t, B[i][j].m)); n       } else { n         printf( %d  , max(A[i][j].m, B[i][j].m)); n       } n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << 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) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n bool ok(int x) { n   if (x < 0) return false; n   for (int i = 0; i * i <= x; i++) n     if (i * i == x) return true; n   return false; n } n int main() { n   int n; n   scanf( %d , &n); n   int mx = -10000000; n   for (int i = (0); i < (n); ++i) { n     int x; n     scanf( %d , &x); n     if (ok(x)) continue; n     mx = max(mx, x); n   } n   printf( %d n , mx); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, a, b, c, d; n     cin >> n >> a >> b >> c >> d; n     if ((a + b) * n < c - d) n       cout <<  No n ; n     else if ((a - b) * n > c + d) n       cout <<  No n ; n     else n       cout <<  Yes n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[200100]; n int main() { n   long long k, Answer = 0; n   int n; n   cin >> n >> k; n   for (int i = 0; i < n; i++) scanf( %I64d , &a[i]); n   for (int i = 0; i < n; i++) { n     Answer += a[i] / k; n     a[i] = a[i] % k; n     if (a[i] + a[i + 1] >= k) n       a[i + 1] += a[i]; n     else if (a[i] > 0) { n       a[i + 1] = 0; n       Answer++; n     } n   } n   cout << Answer; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void substr(int start, int end, char in[], char out[]) { n   for (int i = start; i < end; ++i) out[i - start] = in[i]; n   out[end - start] =   0 ; n } n int main() { n   int n, p, q; n   char s[100]; n   cin >> n >> p >> q; n   cin >> s; n   int pdivmax = n / p + 1, qdivmax = n / q + 1; n   bool divided = false; n   if (p == q) { n     if (n % p == 0) { n       cout << n / p << endl; n       for (int i = 0; i < n; i += p) { n         char out[100]; n         substr(i, i + p, s, out); n         cout << out << endl; n       } n       divided = true; n     } n   } else { n     for (int i = 0; i < pdivmax; ++i) { n       for (int j = 0; j < qdivmax; ++j) { n         if (p * i + q * j == n) { n           cout << i + j << endl; n           for (int k = 0; k < p * i; k += p) { n             char out[100]; n             substr(k, k + p, s, out); n             cout << out << endl; n           } n           for (int k = p * i; k < n; k += q) { n             char out[100]; n             substr(k, k + q, s, out); n             cout << out << endl; n           } n           divided = true; n           break; n         } n       } n       if (divided) break; n     } n   } n   if (!divided) cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, j, k, l, m, t; n   vector<int> a[10001]; n   cin >> n; n   for (i = 1; i < n; i++) { n     cin >> m >> t; n     a[m].push_back(t); n     a[t].push_back(m); n   } n   l = 0; n   m = 0; n   int b[30001], c[30001]; n   for (t = 1; t <= n; t++) { n     j = 0; n     for (i = 0; i < a[t].size(); i++) { n       j++; n       b[j] = a[t][i]; n     } n     l = 0; n     for (i = 1; i <= a[t].size(); i++) { n       for (j = 0; j < a[b[i]].size(); j++) { n         l++; n         c[l] = a[b[i]][j]; n       } n     } n     for (i = 1; i <= l; i++) n       if (c[i] > t) m++; n   } n   cout << m << endl; n } n


#include <bits/stdc++.h> n using namespace std; n bool not_prime[100000]; n long long int primes[100000]; n int p, prime_factors[100000], pf_count[100000], q; n int a[10000]; n void find_primes() { n   long long int i, j; n   p = 0; n   for (i = 2; i < 100000; i++) { n     if (not_prime[i] == false) { n       primes[p++] = i; n       for (j = 2; i * j < 100000; j++) { n         not_prime[i * j] = false; n       } n     } n   } n } n void factor(long long int n) { n   q = 0; n   for (int i = 0; i < p; i++) { n     int count = 0; n     while (n % primes[i] == 0) { n       count++; n       n /= primes[i]; n     } n     if (count > 0) { n       prime_factors[q] = i; n       pf_count[q++] = count; n     } n   } n   if (n > 1) { n     primes[p++] = n; n     prime_factors[q] = p - 1; n     pf_count[q++] = 1; n   } n } n int rec(int d, long long int b) { n   int ret = 0; n   if (d == q) { n     long long int x = 1; n     for (int i = 0; i < q; i++) { n       for (int j = 0; j < a[i]; j++) x *= primes[prime_factors[i]]; n     } n     if (x <= b) n       return 0; n     else n       return 1; n   } n   for (int j = pf_count[d]; j >= 0; j--) { n     a[d] = j; n     int tmp = rec(d + 1, b); n     if (tmp == 0) break; n     ret += tmp; n   } n   return ret; n } n int main() { n   long long int a, b, n; n   int ans = 0; n   cin >> a >> b; n   n = a - b; n   if (a == b) { n     cout <<  infinity  << endl; n   } else if (a < b) { n     cout << 0 << endl; n   } else { n     find_primes(); n     factor(n); n     cout << rec(0, b) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool prime(long long int i) { n   if (i == 1) return false; n   if ((i == 2) || (i == 3)) return true; n   if ((i % 2 == 0) || (i % 3 == 0)) n     return false; n   else { n     for (long long int j = 5; j * j <= i; j += 6) n       if ((i % j == 0) || (i % (j + 2) == 0)) return false; n   } n   return true; n } n int main() { n   long long p, y, ans; n   cin >> p >> y; n   int f = 0; n   if (p == y) { n     cout <<  -1 ; n     return 0; n   } n   for (long long i = y; i > p; i--) { n     int f1 = 0; n     if (prime(i)) { n       f = 1; n       ans = i; n       break; n     } n     for (long long j = 2; j <= p; j++) { n       if (i % j == 0) { n         f1 = 1; n         break; n       } n     } n     if (f1 == 0) { n       ans = i; n       f = 1; n       break; n     } n   } n   if (f == 1) { n     cout << ans; n   } else { n     cout <<  -1 ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 600; n int mat[2000][2000]; n int quad[20][20]; n int cont[20][20]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   int foi = 0; n   int result = -1; n   for (int i = 0; i < m; i++) { n     int x, y; n     for (int h = 0; h < 5; h++) { n       for (int k = 0; k < 5; k++) { n         quad[h][k] = 0; n         cont[h][k] = 0; n       } n     } n     scanf( %d%d , &x, &y); n     x--; n     y--; n     mat[x][y]++; n     int maiorX = max(x - 2, 0); n     int menorX = min(x + 2, n - 1) + 1; n     int maiorY = max(y - 2, 0); n     int menorY = min(y + 2, n - 1) + 1; n     for (int h = maiorX; h < menorX; h++) { n       for (int k = maiorY; k < menorY; k++) { n         if (mat[h][k]) { n           cont[h - maiorX][k - maiorY]++; n           if (h - maiorX - 1 >= 0) { n             cont[h - maiorX][k - maiorY] += cont[h - maiorX - 1][k - maiorY]; n           } n         } n       } n     } n     int pode = 0; n     for (int h = 0; h < 5; h++) { n       int soma = 0; n       for (int k = 0; k < 5; k++) { n         if (cont[h][k] >= 3) { n           soma++; n           if (soma >= 3) { n             pode = 1; n           } n         } else { n           soma = 0; n         } n       } n     } n     if (foi == 0 && pode) { n       result = i + 1; n       foi = 1; n     } n   } n   printf( %d n , result); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void prnt(bool ok) { cout << (ok ?  YES  :  NO ); } n const int ppr = 257; n const long long INF = 2e18; n const int inf = 2e9; n const int mod = 1e9 + 7; n const int N = 3e6 + 123; n const long double pi = 3.141592653589793238462643; n const int dx[] = {1, 0, -1, 0}; n const int dy[] = {0, 1, 0, -1}; n long long n, m, a[N], b[N], q, cur; n vector<long long> g[N]; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n >> m; n   while (m--) { n     long long x, y; n     cin >> x >> y; n     if (x < y) swap(x, y); n     g[y].push_back(x); n     a[x]++; n     b[y]++; n   } n   for (long long i = 1; i <= n; i++) cur += a[i] * b[i]; n   cin >> q; n   cout << cur <<   n ; n   while (q--) { n     long long x; n     cin >> x; n     cur -= a[x] * b[x]; n     a[x] += g[x].size(); n     b[x] = 0; n     for (auto to : g[x]) { n       cur -= a[to] * b[to]; n       b[to]++; n       a[to]--; n       cur += a[to] * b[to]; n       g[to].push_back(x); n     } n     g[x].clear(); n     cout << cur <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = (int)1e5 + 5; n string check(char x, int n) { n   string it =   ; n   it.push_back(x); n   string z =   ; n   for (int i = 0; i < n; i++) { n     z = z +  0 ; n   } n   it = it + z; n   return it; n } n int main() { n   int tc; n   cin >> tc; n   while (tc--) { n     int x; n     int count = 0; n     vector<string> v; n     cin >> x; n     string it = to_string(x); n     int size = it.length(); n     for (int i = 0; i < size; i++) { n       if (it[size - 1 - i] !=  0 ) { n         string second = check(it[size - 1 - i], i); n         count++; n         v.push_back(second); n       } n     } n     reverse(v.begin(), v.end()); n     cout << count << endl; n     for (auto& i : v) { n       cout << i <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3F3F3F3F; n const int MAXN = 100001; n char str[MAXN]; n inline void solve(int test) { n   int t, sx, sy, ex, ey; n   scanf( %d %d %d %d %d , &t, &sx, &sy, &ex, &ey); n   scanf( %s , str); n   ; n   int X, Y; n   X = ex - sx, Y = ey - sy; n   pair<char, char> S; n   if (Y < 0) n     S.first =  S ; n   else n     S.first =  N ; n   if (X < 0) n     S.second =  W ; n   else n     S.second =  E ; n   X = abs(X); n   Y = abs(Y); n   for (int i = 0; i < t; ++i) { n     char G = str[i]; n     if (S.first == G) n       Y--; n     else if (S.second == G) n       X--; n     if (X <= 0 && Y <= 0) { n       printf( %d n , i + 1); n       return; n     } n   } n   printf( -1 n ); n } n int main() { n   solve(0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1 << 30; n char buf[1000]; n string nextLine(int length = 100) { n   cin.getline(buf, length); n   string s(buf); n   return s; n } n string next(int length = 100) { n   string tmp; n   cin >> tmp; n   return tmp; n } n int nextInt() { n   int tmp; n   scanf( %d , &tmp); n   return tmp; n } n long long mod = 1E9 + 7; n long long add(long long a, long long b) { return (a + b) % mod; } n const int MAXN = 5010; n string s1, s2; n int m, n; n long long dp[MAXN][MAXN]; n long long get(int starta, int startb) { n   if (starta == m) return 0; n   if (startb == n) return 0; n   long long &ret = dp[starta][startb]; n   if (ret != -1) return ret; n   ret = 0; n   if (s2[startb] == s1[starta]) { n     ret++; n     ret = add(ret, get(starta + 1, startb + 1)); n   } n   ret = add(ret, get(starta, startb + 1)); n   return ret; n } n int main() { n   s1 = next(); n   s2 = next(); n   m = ((int)(s1).size()); n   n = ((int)(s2).size()); n   long long sum = 0; n   for (int i = 0, _i = (MAXN); i < _i; ++i) n     for (int j = 0, _j = (MAXN); j < _j; ++j) dp[i][j] = -1; n   for (int i = 0, _i = (m); i < _i; ++i) { n     sum = add(sum, get(i, 0)); n   } n   cout << sum << endl; n } n


#include <iostream> n         #include <string> n         #include<bits/stdc++.h> n         #include<algorithm> n         using namespace std; n         int main() n         { n             int t; n             cin>>t; n             while(t--) n             { n                 long long n,k; n                 cin>>n>>k; n                 long long a=n,b=1; n                 if(n%2==0) n                 { n                     k--; n                     k=k%n; n                     cout<<b+k<<endl; n                 } n                 else n                 { n                     k--; n                     long long t=n/2; n                     long long y=k/t; n                     long long j=k+y+1; n                     long long ans=j%n; n                     if(!ans)ans=n; n                     cout<<ans<<endl; n                 } n             } n         }


#include <bits/stdc++.h> n using namespace std; n const int maxN = 1e5; n int n; n char ans[maxN + 1]; n string s; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   cin.ignore(); n   for (int i = 0; i < n; i++) { n     getline(cin, s); n     for (int j = 0; j < s.length(); j++) { n       if (ans[j] == 0) ans[j] =  ? ; n       if (s[j] ==  ?  || ans[j] ==  X ) continue; n       if (ans[j] ==  ?  && s[j] !=  ? ) n         ans[j] = s[j]; n       else if (ans[j] !=  ?  && s[j] !=  ?  && ans[j] != s[j]) n         ans[j] =  X ; n     } n   } n   for (int i = 0; i < s.length(); i++) { n     if (ans[i] ==  X ) n       ans[i] =  ? ; n     else if (ans[i] ==  ? ) n       ans[i] =  a ; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char a[100020]; n char b[100020]; n int zf[100020]; n int zb[100020]; n int main() { n   scanf( %s , a); n   scanf( %s , b); n   int la = strlen(a); n   int lb = strlen(b); n   zf[0] = 0; n   zb[la] = lb; n   for (int i = 1; i <= la; i++) { n     if (a[i - 1] == b[zf[i - 1]]) { n       zf[i] = zf[i - 1] + 1; n     } else { n       zf[i] = zf[i - 1]; n     } n   } n   for (int i = la - 1; i >= 0; i--) { n     if (a[i] == b[zb[i + 1] - 1]) { n       zb[i] = zb[i + 1] - 1; n     } else { n       zb[i] = zb[i + 1]; n     } n   } n   int ans = lb + 1; n   int p = -1; n   for (int i = 0; i <= la; i++) { n     int tmp = max(zb[i] - zf[i], 0); n     if (ans > tmp) { n       ans = tmp; n       p = i; n     } n   } n   for (int i = 0; i < zf[p]; i++) { n     printf( %c , b[i]); n   } n   for (int i = max(zf[p], zb[p]); i < lb; i++) { n     printf( %c , b[i]); n   } n   if (ans == lb) { n     printf( - ); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int maxn = 1e6 + 500; n const long long int mod = 998244353; n int dep[maxn], low[maxn], ans[maxn], dp[maxn]; n int head[maxn], cnt; n int n, k; n struct C { n   int next, to; n } E[maxn]; n void add(int x, int y) { n   E[cnt].to = y; n   E[cnt].next = head[x]; n   head[x] = cnt++; n } n void dfs1(int u, int fa) { n   low[u] = 0x7f7f7f7f; n   dep[u] = dep[fa] + 1; n   if (head[u] == -1) { n     low[u] = dep[u]; n     dp[u] = 1; n   } else { n     for (int i = head[u]; i + 1; i = E[i].next) { n       int v = E[i].to; n       dfs1(v, u); n       low[u] = min(low[u], low[v]); n       if (low[v] - k <= dep[u]) dp[u] += dp[v]; n     } n   } n } n int dfs(int u, int fa) { n   if (head[u] == -1) return 1; n   int ans = 0; n   for (int i = head[u]; i + 1; i = E[i].next) { n     int v = E[i].to; n     int temp = dp[u]; n     if (low[v] - k <= dep[u]) temp -= dp[v]; n     ans = max(ans, temp + dfs(v, u)); n   } n   return ans; n } n int main() { n   int x; n   scanf( %d%d , &n, &k); n   memset(head, -1, sizeof head); n   for (int i = 2; i <= n; i++) { n     scanf( %d , &x); n     add(x, i); n   } n   dfs1(1, 0); n   printf( %d n , dfs(1, 0)); n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n int m, n, a[100009], b[100009]; n long long sa, sb; n long long proca() { n   long long ans = 0; n   ans += sa; n   for (int(x) = (1); (x) < (n); ++(x)) { n     ans += min(sa, (long long)b[x]); n   } n   return ans; n } n long long procb() { n   long long ans = 0; n   ans += sb; n   for (int(x) = (1); (x) < (m); ++(x)) ans += min(sb, (long long)a[x]); n   return ans; n } n int main() { n   while (scanf( %d%d , &m, &n) == 2) { n     sa = sb = 0; n     for (int(x) = (0); (x) < (m); ++(x)) { n       scanf( %d , &a[x]); n       sa += a[x]; n     } n     for (int(x) = (0); (x) < (n); ++(x)) { n       scanf( %d , &b[x]); n       sb += b[x]; n     } n     sort(a, a + m); n     reverse(a, a + m); n     sort(b, b + n); n     reverse(b, b + n); n     cout << min(proca(), procb()) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Edge { n   int t, v, next; n   Edge() {} n   Edge(int a, int b, int c) : t(a), v(b), next(c) {} n }; n Edge e[600005]; n int head[300005]; n long long dis[300005]; n int p[300005]; n priority_queue<pair<long long, int>, vector<pair<long long, int> >, n                greater<pair<long long, int> > > n     q; n void dijkstra(int k) { n   memset(dis, 0x3f, sizeof(dis)); n   dis[1] = 0; n   q.push(pair<long long, int>(0, 1)); n   int cnt = 0; n   while (!q.empty()) { n     pair<long long, int> t = q.top(); n     q.pop(); n     if (dis[t.second] < t.first) continue; n     int a = t.second; n     long long b = t.first; n     if (p[a] && cnt < k) { n       printf( %d  , p[a]); n       cnt++; n     } n     for (int i = head[a]; i; i = e[i].next) n       if (b + e[i].v < dis[e[i].t]) { n         int u = e[i].t; n         dis[u] = b + e[i].v; n         p[u] = ((i + 1) >> 1); n         q.push(pair<long long, int>(dis[u], u)); n       } n   } n   printf(  n ); n } n int main() { n   int n, m, k; n   scanf( %d%d%d , &n, &m, &k); n   k = min(k, n - 1); n   for (int i = 1; i <= m; i++) { n     int x, y, z; n     scanf( %d%d%d , &x, &y, &z); n     e[2 * i - 1] = Edge(y, z, head[x]); n     head[x] = 2 * i - 1; n     e[2 * i] = Edge(x, z, head[y]); n     head[y] = 2 * i; n   } n   printf( %d n , k); n   dijkstra(k); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, i, j; n   char s[101]; n   cin >> n >> m; n   for (i = 0; i < n; i++) { n     cin >> s; n     for (j = 0; j < m; j++) { n       if (s[j] ==  . ) s[j] = ((i + j) % 2 == 0) ?  B  :  W ; n     } n     cout << s << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   srand(1598743157); n   int v[4]; n   for (int i = 0; i < 4; i++) scanf( %d , &v[i]); n   while (v[0] != 1 || v[1] != 1 || v[2] != 1 || v[3] != 1) { n     bool ok = true; n     bool was = false; n     while (ok) { n       ok = false; n       for (int i = 0; i < 4; i++) n         if (v[i] % 2 == 0 && v[(i + 1) % 4] % 2 == 0) { n           printf( /%d n , i + 1); n           v[i] /= 2; n           v[(i + 1) % 4] /= 2; n           ok = true; n           was = true; n         } n     } n     if (was) continue; n     int i = rand() % 4; n     printf( +%d n , i + 1); n     v[i]++; n     v[(i + 1) % 4]++; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[2][100010]; n long long MOD = 1000000007; n int main() { n   string s1, s2; n   int k; n   while (cin >> s1 >> s2 >> k) { n     s1 = s1 + s1; n     int cntS = 0, cntD = 0; n     for (int i = 0; i < s2.size(); ++i) n       if (s1.substr(i, s2.size()) == s2) n         cntS++; n       else n         cntD++; n     if (s1.substr(0, s2.size()) == s2) n       dp[0][0] = 1, dp[1][0] = 0; n     else n       dp[0][0] = 0, dp[1][0] = 1; n     for (int i = 1; i <= k; ++i) { n       dp[0][i] = dp[0][i - 1] * (cntS - 1) + dp[1][i - 1] * (cntS); n       dp[1][i] = dp[0][i - 1] * (cntD) + dp[1][i - 1] * (cntD - 1); n       dp[0][i] %= MOD; n       dp[1][i] %= MOD; n     } n     cout << dp[0][k] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1e5 + 10; n int main() { n   ios::sync_with_stdio(0); n   int n; n   cin >> n; n   string s; n   cin >> s; n   s +=  _ ; n   int ln = 0, lng = 0, imin = 0; n   for (int i = 0; i < s.length(); i++) { n     if (s[i] ==  ( ) { n       imin = 1; n       ln = 0; n     } else if (s[i] ==  ) ) { n       imin = 0; n       ln = 0; n     } else if (!imin) { n       if (s[i] !=  _ ) { n         ln++; n         lng = max(lng, ln); n       } else n         ln = 0; n     } else n       ln = 0; n   } n   cout << lng <<    ; n   imin = 0, ln = 0; n   int cnt = 0; n   for (int i = 0; i < s.length(); i++) { n     if (s[i] ==  ( ) { n       imin = 1; n       ln = 0; n     } else if (s[i] ==  ) ) { n       imin = 0; n       if (ln) cnt++; n       ln = 0; n     } else if (imin) { n       if (s[i] !=  _ ) n         ln++; n       else { n         if (ln) cnt++; n         ln = 0; n       } n     } else n       ln = 0; n   } n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N, K; n   cin >> N >> K; n   using arr2 = array<int, 2>; n   vector<arr2> binTrie; n   const int w = 30; n   binTrie.push_back({-1, -1}); n   vector<int> sz(1, 0); n   auto add = [&](int x) { n     int v = 0; n     for (int i = w - 1; i >= 0; --i) { n       int b = (x >> i) & 1; n       ++sz[v]; n       if (binTrie[v][b] >= 0) { n         v = binTrie[v][b]; n         continue; n       } n       int v_ = binTrie.size(); n       binTrie[v][b] = v_; n       binTrie.push_back({-1, -1}); n       sz.push_back(0); n       v = v_; n     } n     ++sz[v]; n   }; n   auto xor_lower_bound_size = [&](int x, int k) { n     int v = 0, ret = 0; n     for (int i = w - 1; i >= 0; --i) { n       int b = ((x ^ k) >> i) & 1; n       int t = (k >> i) & 1; n       if ((!t) and binTrie[v][b ^ 1] >= 0) ret += sz[binTrie[v][b ^ 1]]; n       if (binTrie[v][b] < 0) { n         v = -1; n         break; n       } n       v = binTrie[v][b]; n     } n     if (v >= 0) ret += sz[v]; n     return ret; n   }; n   vector<int> A(N + 1); n   for (int i = 0; i < N; ++i) { n     cin >> A[i + 1]; n     A[i + 1] ^= A[i]; n   } n   long long ans = 0; n   for (int i = 0; i <= N; ++i) { n     ans += xor_lower_bound_size(A[i], K); n     add(A[i]); n   } n   cout << ans << endl; 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   ; n   long long t = 1; n   cin >> t; n   while (t--) { n     long long n, k; n     cin >> n >> k; n     if (k > n) { n       cout <<  NO n ; n       continue; n     } n     if (n % 2 == 0 && k % 2 == 1 && k > n / 2) { n       cout <<  NO n ; n       continue; n     } n     if (n % 2 != 0 && k % 2 == 0) { n       cout <<  NO n ; n       continue; n     } n     if (k % 2 != n % 2) { n       cout <<  YES n ; n       cout << n - 2 * (k - 1) <<    ; n       for (long long i = 0; i < k - 1; i++) cout << 2 <<    ; n       cout << endl; n       continue; n     } n     if (n % 2 == k % 2) { n       cout <<  YES n ; n       cout << n - (k - 1) <<    ; n       for (long long i = 0; i < k - 1; i++) cout << 1 <<    ; n       cout << endl; n       continue; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (a < b) swap(a, b); n   if (b == 0) return a; n   while ((a = a % b) != 0) { n     swap(a, b); n   } n   return b; n } n long long mpow(long long x, long long y, long long m) { n   long long res = 1; n   while (y > 0) { n     if (y & 1) res = res * x % m; n     y = y >> 1; n     x = x * x % m; n   } n   return res; n } n long long ncr(long long n, long long r) { n   if (r > n) return 0; n   long long res = 1; n   for (long long i = (1); i < (r + 1); i++) res = res * (n - r + i) / i; n   return res; n } n long long *sieve(long long n) { n   long long *lpf = new long long[n + 1]; n   for (long long i = 1; i <= n; i++) lpf[i] = i; n   long long rt = (long long)floor(sqrt(n)) + 1; n   for (long long i = (2); i < (rt); i++) { n     if (lpf[i] != i) continue; n     for (long long j = i * i; j <= n; j += i) { n       if (lpf[j] == j) lpf[j] = i; n     } n   } n   return lpf; n } n void solve() { n   long long n; n   cin >> n; n   long long a[n]; n   for (long long i = (0); i < (n); i++) cin >> a[i]; n   long long mx = *max_element(a, a + n), mn = *min_element(a, a + n); n   if (mx == mn) { n     if (mx != 0) n       cout <<  NO ; n     else { n       cout <<  YES n ; n       for (long long i = (0); i < (n); i++) cout <<  1  ; n     } n     return; n   } n   long long k; n   for (long long i = (0); i < (n); i++) { n     long long prev = i - 1; n     if (prev == -1) prev = n - 1; n     if (a[i] == mx && a[prev] != mx) { n       k = i; n       break; n     } n   } n   long long pt = k; n   long long b[n]; n   b[pt] = mx; n   pt--; n   if (pt == -1) pt = n - 1; n   if (a[pt] == 0) n     b[pt] = mx * 2; n   else n     b[pt] = mx + a[pt]; n   pt--; n   if (pt == -1) pt = n - 1; n   while (pt != k) { n     b[pt] = b[(pt + 1) % n] + a[pt]; n     pt--; n     if (pt == -1) pt = n - 1; n   } n   cout <<  YES n ; n   for (long long i = (0); i < (n); i++) cout << b[i] <<    ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t = 1; n   for (long long i = (1); i < (t + 1); i++) { n     solve(); n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int m, n; n   cin >> m >> n; n   vector<pair<int, int>> a, b; n   a.resize(n); n   for (int i = 0; i < n; ++i) { n     a[i].second = i; n     cin >> a[i].first; n   } n   sort(a.begin(), a.end()); n   b.resize(n); n   for (int i = 0; i < n; ++i) { n     b[i].second = i; n     cin >> b[i].first; n   } n   sort(b.begin(), b.end()); n   vector<long long> sum; n   sum.resize(n * 2 + 1); n   for (int i = 0; i < n * 2 + 1; ++i) { n     sum[i] = 0; n   } n   for (int i = 0, p0 = 0, p1 = 0, p2 = 0; i < n; ++i) { n     while (p0 < n && b[p0].first < a[i].first - 1.0 * m / 2) { n       ++p0; n     } n     while (p1 < n && b[p1].first < a[i].first) { n       ++p1; n     } n     while (p2 < n && b[p2].first < a[i].first + 1.0 * m / 2) { n       ++p2; n     } n     int beg = n - i; n     sum[beg + 0] += m - a[i].first; n     sum[beg + p0] -= m - a[i].first; n     sum[beg + p0] += a[i].first; n     sum[beg + p1] -= a[i].first; n     sum[beg + p1] += -a[i].first; n     sum[beg + p2] -= -a[i].first; n     sum[beg + p2] += m + a[i].first; n     sum[beg + n] -= m + a[i].first; n   } n   for (int i = 1; i < n * 2; ++i) { n     sum[i] += sum[i - 1]; n   } n   for (int i = 0; i < n; ++i) { n     sum[i] += sum[i + n]; n   } n   vector<long long> rsum; n   rsum.resize(n * 2 + 1); n   for (int i = 0; i < n * 2 + 1; ++i) { n     rsum[i] = 0; n   } n   for (int i = 0, p0 = 0, p1 = 0, p2 = 0; i < n; ++i) { n     while (p0 < n && a[p0].first <= b[i].first - 1.0 * m / 2) { n       ++p0; n     } n     while (p1 < n && a[p1].first <= b[i].first) { n       ++p1; n     } n     while (p2 < n && a[p2].first <= b[i].first + 1.0 * m / 2) { n       ++p2; n     } n     int beg = n - i - 1; n     rsum[beg + 0] += -b[i].first; n     rsum[beg + p0] -= -b[i].first; n     rsum[beg + p0] += b[i].first; n     rsum[beg + p1] -= b[i].first; n     rsum[beg + p1] += -b[i].first; n     rsum[beg + p2] -= -b[i].first; n     rsum[beg + p2] += b[i].first; n     rsum[beg + n] -= b[i].first; n   } n   for (int i = 1; i < n * 2; ++i) { n     rsum[i] += rsum[i - 1]; n   } n   for (int i = 0; i < n; ++i) { n     rsum[i] += rsum[i + n]; n   } n   vector<long long> cost; n   cost.resize(n); n   for (int i = 0; i < n; ++i) { n     cost[i] = sum[i] + rsum[n - 1 - i]; n   } n   long long cur = (long long)(1e18); n   int pos; n   for (int i = 0; i < n; ++i) { n     if (cost[i] < cur) { n       cur = cost[i]; n       pos = i; n     } n   } n   cout << cur << endl; n   vector<int> at; n   at.resize(n); n   for (int i = 0; i < n; ++i) { n     at[a[i].second] = b[(i + pos) % n].second; n   } n   for (int i = 0; i < n; ++i) { n     cout << at[i] + 1 <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int getFldsFromLine(char *line, std::vector<char *> &res, n                     const char *sep =    t n v f r ); n int main(int argc, char **argv) { n   FILE *infile = stdin; n   char *line; n   line = (char *)calloc(5000000, sizeof(char)); n   if (line == NULL) { n     fprintf(stderr,  Couldn t allocate space for  %s  nBye! n ,  line ); n     exit(-1); n   }; n   std::vector<char *> flds; n   int numFlds, numCases, caseNum = 0; n   fgets(line, 5000000, infile); n   numFlds = getFldsFromLine(line, flds); n   numCases = atoi(flds[0]); n   bool isGood = false; n   for (caseNum = 1; caseNum <= numCases; ++caseNum) { n     fgets(line, 5000000, infile); n     numFlds = getFldsFromLine(line, flds); n     int num1 = atoi(flds[1]), num2 = atoi(flds[2]); n     if ((num1 >= 2400) && (num2 > num1)) isGood = true; n   } n   if (isGood) n     printf( YES n ); n   else n     printf( NO n ); n   return (0); n } n int getFldsFromLine(char *line, std::vector<char *> &res, const char *sep) { n   res.clear(); n   char *tok = strtok(line, sep); n   while (tok) { n     res.push_back(tok); n     tok = strtok(0, sep); n   } n   return res.size(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, j, i, l, r, ans = 0, x, y, minimum; n   scanf( %d , &n); n   scanf( %d , &k); n   int count_a[n], count_b[n]; n   char str[n + 1], ch; n   scanf( %s , str); n   for (i = 0; i < n; i++) { n     count_a[i] = 0; n     count_b[i] = 0; n   } n   for (i = 0; i < n; i++) { n     if (str[i] ==  a ) count_a[i]++; n     if (str[i] ==  b ) count_b[i]++; n   } n   for (i = 1; i < n; i++) { n     count_a[i] += count_a[i - 1]; n     count_b[i] += count_b[i - 1]; n   } n   l = 0; n   r = 0; n   while (r < n) { n     if (l == 0) { n       x = count_a[r]; n       y = count_b[r]; n     } else { n       x = count_a[r] - count_a[l - 1]; n       y = count_b[r] - count_b[l - 1]; n     } n     minimum = min(x, y); n     if (k >= minimum) { n       ans = max(ans, r - l + 1); n       r++; n     } else n       l++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC target( avx2 ) n #pragma GCC optimize( O3 ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC optimize( Ofast ) n using namespace std; n const long long INF64 = 1e18; n const long long INF32 = 1e9; n const double EPS = static_cast<double>(1e-10); n const double PI = 2.0 * acos(0.0); n const long long MOD = (long long)1e9 + 7; n long long powMod(long long a, long long b) { n   long long res = 1; n   a %= MOD; n   assert(b >= 0); n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % MOD; n     a = a * a % MOD; n   } n   return res; n } n long long invMod(long long a) { n   long long b = MOD, u = 1, v = 0; n   while (b) { n     long long t = a / b; n     a -= t * b; n     swap(a, b); n     u -= t * v; n     swap(u, v); n   } n   return (MOD + u) % MOD; n } n const long long day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n mt19937 rnd(239); n vector<long long> prefixSum(vector<long long> &a) { n   vector<long long> b; n   std::partial_sum(a.begin(), a.end(), b.begin()); n   return b; n } n long long mostSignDigit(long long n) { n   double k = log10(n); n   return (long long)pow(10, k - floor(k)); n } n long long numOfDigits(long long n) { return (long long)floor(log10(n)) + 1; } n bool isPowerOfTwo(long long x) { return x && (!(x & (x - 1))); } n bool allPositive(vector<long long> &a) { n   return all_of(begin(a), end(a), [](long long x) { return x > 0; }); n } n bool anyPositive(vector<long long> &a) { n   return any_of(begin(a), end(a), [](long long x) { return x > 0; }); n } n bool nonePositive(vector<long long> &a) { n   return none_of(begin(a), end(a), [](long long x) { return x > 0; }); n } n void FILEIO() { n   freopen( in.txt ,  r , stdin); n   freopen( out.txt ,  w , stdout); n } n void __print(long long x) { cerr << x; } n void __print(long x) { cerr << x; } n void __print(unsigned x) { cerr << x; } n void __print(unsigned long x) { cerr << x; } n void __print(unsigned long long x) { cerr << x; } n void __print(float x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(char x) { cerr <<      << x <<     ; } n void __print(const char *x) { cerr <<     << x <<    ; } n void __print(const string &x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  T  :  F ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  , ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &x) { n   long long f = 0; n   cerr <<  { ; n   for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n vector<vector<long long>> adj; n vector<long long> lvl; n vector<long long> par; n void dfs(long long vertex) { n   for (long long nxt : adj[vertex]) { n     if (lvl[nxt] == 0) { n       par[nxt] = vertex; n       lvl[nxt] = lvl[vertex] + 1; n       dfs(nxt); n     } n   } n } n void init(long long n) { n   adj.clear(); n   adj.assign(n, vector<long long>()); n   lvl.clear(); n   lvl.resize(n); n   par.clear(); n   par.resize(n); n } n void DFS() { n   lvl[0] = 1; n   dfs(0); n } n void solve() { n   long long n, m; n   cin >> n >> m; n   init(n); n   for (long long i = (0); i < (m); i++) { n     long long u, v; n     cin >> u >> v; n     u--, v--; n     adj[u].push_back(v); n     adj[v].push_back(u); n   }; n   DFS(); n   ; n   ; n   long long mx = -1, umx = 0; n   for (long long u = (0); u < (n); u++) { n     if (lvl[u] > mx) { n       umx = u; n       mx = lvl[u]; n     } n   } n   if (mx >= (n + 1) / 2) { n     vector<long long> ans; n     long long u = umx; n     for (long long i = (0); i < (mx); i++) { n       if (((long long)(ans).size()) >= (n + 1) / 2) break; n       ans.push_back(u); n       u = par[u]; n     } n     reverse(begin(ans), end(ans)); n     cout <<  PATH  << endl; n     cout << ((long long)(ans).size()) << endl; n     for (long long i = (0); i < (((long long)(ans).size())); i++) n       cout << ans[i] + 1 <<    ; n     cout << endl; n   } else { n     vector<vector<long long>> lvlvec(mx); n     for (long long u = (0); u < (n); u++) lvlvec[lvl[u] - 1].push_back(u); n     ; n     vector<pair<long long, long long>> ans; n     for (long long i = (0); i < (mx); i++) { n       vector<long long> vc = lvlvec[i]; n       for (long long j = (0); j < (((long long)(vc).size()) / 2); j++) { n         long long a = vc[2 * j], b = vc[2 * j + 1]; n         ans.push_back(make_pair(a, b)); n       } n       if (((long long)(ans).size()) * 2 >= (n + 1) / 2) break; n     } n     cout <<  PAIRING  << endl; n     cout << ((long long)(ans).size()) << endl; n     for (long long i = (0); i < (((long long)(ans).size())); i++) { n       cout << ans[i].first + 1 <<     << ans[i].second + 1 <<   n ; n     } n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(); n   long long t; n   cin >> t; n   for (long long i = (1); i <= (t); i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 10; n int n, q, a, b, blo, arr[N]; n struct node { n   int a, b, id; n } qq[N]; n long long ans[N], dp[N]; n bool cmp(node a, node b) { n   if (a.b != b.b) return a.b > b.b; n   return a.a < b.a; n } n int main() { n   scanf( %d , &n); n   blo = sqrt(n); n   for (int i = 1; i <= n; i++) scanf( %d , &arr[i]); n   scanf( %d , &q); n   for (int i = 1; i <= q; i++) { n     scanf( %d%d , &qq[i].a, &qq[i].b); n     qq[i].id = i; n   } n   sort(qq + 1, qq + q + 1, cmp); n   int n_b = -1; n   for (int i = 1; i <= q; i++) { n     node nn = qq[i]; n     if (nn.b >= blo) { n       for (int i = nn.a; i <= n; i += nn.b) ans[nn.id] += arr[i]; n     } else { n       if (nn.b != n_b || dp[nn.a] == 0) { n         for (int j = n; j >= 1; j--) { n           if (j + nn.b > n) n             dp[j] = arr[j]; n           else n             dp[j] = dp[j + nn.b] + arr[j]; n         } n         n_b = nn.b; n       } n       ans[nn.id] = dp[nn.a]; n     } n   } n   for (int i = 1; i <= q; i++) { n     printf( %I64d n , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int stones, counter = 0; n   string colours; n   cin >> stones; n   cin >> colours; n   for (int i = 0; i < stones - 1; i++) { n     if (colours[i] == colours[i + 1]) { n       counter++; n     } n   } n   cout << counter << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool possible(int smaller, int bigger) { return (bigger <= smaller * 3 + 1); } n int main() { n   ios::sync_with_stdio(false); n   int q; n   cin >> q; n   for (int i = 0; i < q; ++i) { n     int b, w; n     cin >> b >> w; n     if ((b < w && !possible(b, w)) || (w < b && !possible(w, b))) { n       cout <<  NO  << endl; n       continue; n     } n     cout <<  YES  << endl; n     int offset = (b < w) ? 1 : 0; n     if (b < w) swap(b, w); n     int column = 1; n     while (w + 1 < b) { n       cout << column <<     << (2 + offset) << endl; n       column++; n       cout << column <<     << (1 + offset) << endl; n       cout << column <<     << (2 + offset) << endl; n       cout << column <<     << (3 + offset) << endl; n       column++; n       w -= 1; n       b -= 3; n     } n     if (w < b) { n       cout << column <<     << (2 + offset) << endl; n       b--; n       column++; n     } n     for (; 0 < b; b--) { n       cout << column <<     << (1 + offset) << endl; n       cout << column <<     << (2 + offset) << endl; n       column++; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   int man = 0, ans = 0, n, m; n   int a[1000]; n   while (cin >> n >> m) { n     ans = 0; n     if (n > m) { n       printf( %d n , n - m); n     } else { n       while (n < m) { n         if (m % 2 != 0) { n           m++; n           ans++; n         } n         m /= 2; n         ans++; n       } n       ans += n - m; n       printf( %d n , ans); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-9; n const double pi = acos(-1); n const long long int INF = 1e18; n const int inf = 1e9; n const int MOD = 1e9 + 7; n const int nax = 1000000 + 10; n int n, arr[nax]; n int main() { n   ios::sync_with_stdio(0); n   cin >> n; n   if (n % 10 == 0) { n     cout << n; n     return 0; n   } n   if (n <= 5) n     cout << 0; n   else { n     int div = (n + 10 - 1) / 10; n     int ans = 10 * div; n     int rem = (n % 10); n     if (rem <= 5) ans -= 10; n     cout << ans; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   scanf( %d %d , &n, &k); n   if (k > (n * n + 1) / 2) { n     printf( NO ); n     return 0; n   } n   int i, j, count = 0; n   printf( YES n ); n   for (i = 0; i < n; i++) { n     for (j = 0; j < n; j++) { n       if ((i + j) % 2 == 0 and count < k) { n         count++; n         printf( L ); n       } else n         printf( S ); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 36; n long long dp[maxn][maxn]; n int main() { n   long long n, h; n   cin >> n >> h; n   for (long long i = 0; i <= n; i++) dp[0][i] = 1; n   for (long long i = 1; i <= n; i++) n     for (long long j = 1; j <= n; j++) n       for (long long k = 0; k < i; k++) { n         dp[i][j] += dp[k][j - 1] * dp[i - 1 - k][j - 1]; n       } n   cout << dp[n][n] - dp[n][h - 1] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string ans1, ans2; n   string s1, s2, ss1, ss2; n   int a, b; n   cin >> s1 >> s2; n   a = atoi(s1.c_str()) + atoi(s2.c_str()); n   stringstream ss; n   ss << a; n   ss >> ans1; n   for (int i = 0; i < s1.length(); ++i) { n     if (s1[i] !=  0 ) { n       ss1 += s1[i]; n     } n   } n   for (int i = 0; i < ans1.length(); ++i) { n     if (ans1[i] !=  0 ) { n       ans2 += ans1[i]; n     } n   } n   for (int i = 0; i < s2.length(); ++i) { n     if (s2[i] !=  0 ) { n       ss2 += s2[i]; n     } n   } n   b = atoi(ss1.c_str()) + atoi(ss2.c_str()); n   stringstream sss; n   sss << b; n   sss >> ans1; n   if (ans1 == ans2) { n     cout <<  YES ; n   } else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long mod, dp[18][250005]; n unsigned long long fac[250005], inv[250005]; n unsigned long long n, k; n inline unsigned long long qpow(unsigned long long a, unsigned long long b) { n   unsigned long long res = 1ll, tp = a; n   while (b) { n     if (b & 1ll) { n       res *= tp; n       res %= mod; n     } n     tp *= tp; n     tp %= mod; n     b >>= 1ll; n   } n   return res; n } n inline unsigned long long cal(int a, int b) { n   return (((fac[a] * inv[b]) % mod) * inv[a - b]) % mod; n } n inline unsigned long long calc() { n   int i, j, l; n   fac[0] = 1ll; n   for (i = 1; i <= n; i++) n     fac[i] = fac[i - 1] * ((unsigned long long)(i)) % mod; n   inv[n] = qpow(fac[n], mod - 2ll); n   for (i = n - 1; i >= 0; i--) n     inv[i] = inv[i + 1] * ((unsigned long long)(i + 1)) % mod; n   for (i = 1; i <= n; i++) { n     dp[1][i] = inv[i]; n   } n   unsigned long long ans = dp[1][n] * cal(k, 1) % mod; n   int t = min(17, ((int)(k))); n   for (i = 2; i <= t; i++) { n     for (j = 1; j <= n; j++) { n       if ((j & n) != j) { n         continue; n       } n       int cnt = 0; n       for (l = j; l; l = (l - 1) & j) { n         dp[i][j] += dp[i - 1][j ^ l] * inv[l]; n         cnt++; n         if (cnt == 18) { n           cnt = 0; n           dp[i][j] %= mod; n         } n       } n       dp[i][j] %= mod; n     } n     ans += dp[i][n] * cal(k, i); n     ans %= mod; n   } n   ans *= fac[n]; n   ans %= mod; n   return ans; n } n int main() { n   cin >> n >> k >> mod; n   unsigned long long ans = qpow(k, n); n   if (n & 1) { n     cout << ans << endl; n     return 0; n   } n   ans = (ans - calc() + mod) % mod; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n static const long long maxn = 2005; n long long M[maxn][maxn]; n long long MyM[maxn][maxn]; n vector<vector<long long>*> graph; n long long dsu[maxn]; n bool PreCheck() { n   for (long long i = 0; i < n; i++) { n     if (M[i][i] != 0) return false; n   } n   for (long long i = 0; i < n; i++) n     for (long long j = 0; j < n; j++) { n       if (M[i][j] != M[j][i]) return false; n     } n   return true; n } n long long edge_count = 0; n struct edge { n   long long u; n   long long v; n   long long d; n   bool operator<(const edge& rhs) const { return d < rhs.d; } n }; n long long dsu_root(long long i) { n   while (dsu[i] != i) { n     dsu[i] = dsu[dsu[i]]; n     i = dsu[i]; n   } n   return i; n } n bool dsu_connected(long long i, long long j) { n   return dsu_root(i) == dsu_root(j); n } n void dsu_join(long long i, long long j) { n   long long r = rand() % 2; n   if (r) n     dsu[dsu_root(i)] = j; n   else n     dsu[dsu_root(j)] = i; n } n void init() { n   for (long long i = 0; i < n; i++) { n     dsu[i] = i; n     vector<long long>* v = new vector<long long>(); n     graph.push_back(v); n   } n } n bool dfs_visited[maxn]; n void dfs(long long i, long long root, long long curr) { n   if (dfs_visited[i]) return; n   dfs_visited[i] = true; n   for (auto it : *graph[i]) { n     if (dfs_visited[it]) n       continue; n     else { n       long long d = curr + M[i][it]; n       MyM[root][it] = d; n       MyM[it][root] = d; n       dfs(it, root, d); n     } n   } n } n bool FinalCheck() { n   for (long long i = 1; i < n; i++) { n     if (!dsu_connected(i, 0)) return false; n   } n   for (long long i = 0; i < n; i++) n     for (long long j = 0; j < i; j++) { n       if (M[i][j] != MyM[i][j]) return false; n     } n   return true; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   init(); n   for (long long i = 0; i < n; i++) n     for (long long j = 0; j < n; j++) { n       cin >> M[i][j]; n       MyM[i][j] = 0; n     } n   if (!PreCheck()) { n     cout <<  NO  << endl; n     return 0; n   } n   vector<edge> edges; n   for (long long i = 0; i < n; i++) n     for (long long j = 0; j < i; j++) { n       edge curr; n       curr.u = i; n       curr.v = j; n       curr.d = M[i][j]; n       if (curr.d > 0) edges.push_back(curr); n     } n   sort(edges.begin(), edges.end()); n   for (auto it : edges) { n     if (!dsu_connected(it.u, it.v)) { n       edge_count++; n       graph[it.u]->push_back(it.v); n       graph[it.v]->push_back(it.u); n       dsu_join(it.u, it.v); n     } n   } n   if (edge_count != n - 1) { n     cout <<  NO  << endl; n     return 0; n   } n   for (long long i = 0; i < n; i++) { n     memset(dfs_visited, 0, sizeof(dfs_visited)); n     dfs(i, i, 0); n   } n   if (FinalCheck()) { 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 int MAX_N = 200005; n int l[MAX_N], r[MAX_N]; n int a[MAX_N]; n int n; n int F[MAX_N]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= n; i++) { n     l[i] = i; n     while (a[l[i] - 1] >= a[i]) l[i] = l[l[i] - 1]; n   } n   for (int i = n; i >= 1; i--) { n     r[i] = i; n     while (a[r[i] + 1] >= a[i]) r[i] = r[r[i] + 1]; n   } n   for (int i = 1; i <= n; i++) n     F[r[i] - l[i] + 1] = max(F[r[i] - l[i] + 1], a[i]); n   for (int i = n; i >= 1; i--) F[i] = max(F[i], F[i + 1]); n   for (int i = 1; i <= n; i++) printf( %d  , F[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace FFT { n const int N = 20; n const int MAXN = (1 << N); n class cmplx { n  private: n   double x, y; n  n  public: n   cmplx() : x(0.0), y(0.0) {} n   cmplx(double a) : x(a), y(0.0) {} n   cmplx(double a, double b) : x(a), y(b) {} n   double get_real() { return this->x; } n   double get_img() { return this->y; } n   cmplx conj() { return cmplx(this->x, -(this->y)); } n   cmplx operator=(const cmplx& a) { n     this->x = a.x; n     this->y = a.y; n     return *this; n   } n   cmplx operator+(const cmplx& b) { n     return cmplx(this->x + b.x, this->y + b.y); n   } n   cmplx operator-(const cmplx& b) { n     return cmplx(this->x - b.x, this->y - b.y); n   } n   cmplx operator*(const double& num) { n     return cmplx(this->x * num, this->y * num); n   } n   cmplx operator/(const double& num) { n     return cmplx(this->x / num, this->y / num); n   } n   cmplx operator*(const cmplx& b) { n     return cmplx(this->x * b.x - this->y * b.y, this->y * b.x + this->x * b.y); n   } n   cmplx operator/(const cmplx& b) { n     cmplx temp(b.x, -b.y); n     cmplx n = (*this) * temp; n     return n / (b.x * b.x + b.y * b.y); n   } n }; n cmplx w[MAXN]; n cmplx f1[MAXN]; n int rev[MAXN]; n void ReserveBits(int k) { n   static int rk = -1, lim; n   if (k == rk) return; n   rk = k, lim = 1 << k; n   for (int i = 1; i <= lim; ++i) { n     int j = rev[i - 1], t = k - 1; n     while (t >= 0 && ((j >> t) & 1)) { n       j ^= 1 << t; n       --t; n     } n     if (t >= 0) { n       j ^= 1 << t; n       --t; n     } n     rev[i] = j; n   } n } n void fft(cmplx* a, int k) { n   ReserveBits(k); n   int n = 1 << k; n   for (int i = 0; i < n; ++i) n     if (rev[i] > i) swap(a[i], a[rev[i]]); n   for (int l = 2, m = 1; l <= n; l += l, m += m) { n     if (w[m].get_real() == 0 && w[m].get_img() == 0) { n       double angle = M_PI / m; n       cmplx ww(cos(angle), sin(angle)); n       if (m > 1) { n         for (int j = 0; j < m; ++j) { n           if (j & 1) n             w[m + j] = w[(m + j) / 2] * ww; n           else n             w[m + j] = w[(m + j) / 2]; n         } n       } else n         w[m] = cmplx(1, 0); n     } n     for (int i = 0; i < n; i += l) { n       for (int j = 0; j < m; ++j) { n         cmplx v = a[i + j], u = a[i + j + m] * w[m + j]; n         a[i + j] = v + u; n         a[i + j + m] = v - u; n       } n     } n   } n } n vector<long long> mul(vector<long long> a, vector<long long> b) { n   int k = 1; n   while ((1 << k) < (a.size() + b.size())) ++k; n   int n = (1 << k); n   for (int i = 0; i < n; ++i) f1[i] = cmplx(0, 0); n   for (int i = 0; i < a.size(); ++i) f1[i] = f1[i] + cmplx(a[i], 0); n   for (int i = 0; i < b.size(); ++i) f1[i] = f1[i] + cmplx(0, b[i]); n   fft(f1, k); n   for (int i = 0; i <= n / 2; ++i) { n     cmplx p = f1[i] + f1[(n - i) % n].conj(); n     cmplx _q = f1[(n - i) % n] - f1[i].conj(); n     cmplx q(_q.get_img(), _q.get_real()); n     f1[i] = (p * q) * 0.25; n     if (i > 0) f1[(n - i)] = f1[i].conj(); n   } n   for (int i = 0; i < n; ++i) f1[i] = f1[i].conj(); n   fft(f1, k); n   vector<long long> ans(a.size() + b.size() - 1); n   for (int i = 0; i < ans.size(); ++i) { n     ans[i] = (long long)(f1[i].get_real() / n + 0.5); n   } n   return ans; n } n };  // namespace FFT n int buff = 2e5 + 5; n int main() { n   ios_base::sync_with_stdio(false); n   long long int n, x; n   long long int an[200005]; n   cin >> n >> x; n   long long int nn = 2 * buff + 5; n   vector<long long int> v(nn, 0); n   vector<long long int> v1(nn, 0); n   v[0] = 1; n   v1[buff] = 1; n   long long int cool = 0; n   for (int i = 0; i <= n - 1; i++) { n     long long int xx; n     cin >> xx; n     if (xx < x) cool++; n     an[0] += v[cool]; n     v[cool]++; n     v1[buff - cool]++; n   } n   vector<long long int> res = FFT::mul(v, v1); n   int sz = res.size(); n   an[0] = (n * (n + 1)) / 2; n   for (int i = buff + 1; i <= buff + n; i++) { n     an[i - buff] = res[i]; n     an[0] -= res[i]; n   } n   for (int i = 0; i <= n; i++) { n     cout << an[i]; n     cout <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int h; n   long long n, l = 1, r, rpta = 0, aux2; n   cin >> h >> n; n   r = 1LL << h; n   aux2 = r; n   int aux = 1; n   while (r > l) { n     rpta++; n     long long m = (l + r) / 2; n     if (n <= m && aux == 0) { n       rpta += aux2 - 1; n     } else if (n > m && aux == 1) { n       rpta += aux2 - 1; n     } else n       aux ^= 1; n     if (n <= m) n       r = m; n     else n       l = m + 1; n     aux2 >>= 1; n   } n   cout << rpta << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = (int)1e3 + 10; n int a[MaxN][MaxN]; n int main() { n   int n, i, m, k, x, y, j; n   char c; n   scanf( %d%d%d n , &n, &m, &k); n   for (i = 1; i <= n; i++) n     for (j = 1; j <= m; j++) scanf( %d , &a[i][j]); n   scanf(  n ); n   for (i = 1; i <= m; i++) a[0][i] = i; n   for (i = 1; i <= n; i++) a[i][0] = i; n   for (i = 1; i <= k; i++) { n     scanf( %c , &c); n     if (c ==  c ) { n       scanf( %d%d n , &x, &y); n       swap(a[0][x], a[0][y]); n     } n     if (c ==  r ) { n       scanf( %d%d n , &x, &y); n       swap(a[x][0], a[y][0]); n     } n     if (c ==  g ) { n       scanf( %d%d n , &x, &y); n       printf( %d n , a[a[x][0]][a[0][y]]); n     } n   } n   cin.get(); n   cin.get(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n const int M = 1e4 + 10; n const int MOD = 1e9 + 7; n const int inf = 1e9; n const double pi = acos(-1.0); n const double eps = 1e-6; n int dx[] = {0, -1, 0, 1}; n int dy[] = {1, 0, -1, 0}; n int n, m; n int a[101]; n int sum[102]; n int l, r; n int res; n int main() { n   cin >> n >> m; n   sum[0] = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     sum[i + 1] += sum[i] + a[i]; n   } n   for (int i = 0; i < m; i++) { n     cin >> l >> r; n     int v = sum[r] - sum[l - 1]; n     if (v > 0) res += v; n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void read(T& s) { n   s = 0; n   int f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     s = s * 10 + c -  0 ; n     c = getchar(); n   } n   s *= f; n } n void exkmp(char* s, char* t, int n, int m, int* f, int* g) { n   int mx = 0; n   f[1] = n; n   for (int i = 2; i <= m; i++) { n     f[i] = max(0, min(mx + f[mx] - i, f[i - mx + 1])); n     while (i + f[i] <= m && t[i + f[i]] == t[f[i] + 1]) f[i]++; n     if (i + f[i] > mx + f[mx]) mx = i; n   } n   mx = 0; n   for (int i = 1; i <= n; i++) { n     g[i] = max(0, min(f[i - mx + 1], mx + g[mx] - i)); n     while (i + g[i] <= n && s[i + g[i]] == t[g[i] + 1]) g[i]++; n     if (i + g[i] > mx + g[mx]) mx = i; n   } n } n int n, m; n char a[1000005], b[1000005], at[1000005]; n int f[1000005], g1[1000005], g2[1000005]; n int mxw[1000005]; n int main() { n   n = m = 1; n   char c = getchar(); n   while (c !=   n ) { n     a[n++] = c; n     c = getchar(); n   } n   c = getchar(); n   while (c !=   n ) { n     b[m++] = c; n     c = getchar(); n   } n   n--; n   m--; n   if (n != m) { n     printf( -1 -1 n ); n     return 0; n   } n   if (a[1] != b[n]) { n     printf( -1 -1 n ); n     return 0; n   } n   int mxi = 1; n   for (mxi; mxi < n && a[mxi + 1] == b[n - mxi]; mxi++) n     ; n   for (int i = 1; i <= m; i++) at[i] = a[m + 1 - i]; n   exkmp(a, b, n, m, f, g1); n   exkmp(b, at, n, m, f, g2); n   memset(mxw, 23, sizeof(mxw)); n   for (int i = 1; i <= n; i++) n     mxw[1 + n - g2[i] - i] = min(mxw[1 + n - g2[i] - i], i); n   for (int i = 1; i <= n; i++) mxw[i] = min(mxw[i], mxw[i - 1]); n   for (int i = mxi; i >= 1; i--) { n     int mx = min(g1[i + 1] + 1, n - i); n     int gg = mxw[i]; n     if (gg > mx) continue; n     printf( %d %d n , i - 1, gg + i - 1); n     return 0; n   } n   printf( -1 -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void smin(T &a, U b) { n   if (a > b) a = b; n } n template <typename T, typename U> n inline void smax(T &a, U b) { n   if (a < b) a = b; n } n int n, i, j, o, ans, cnt, num; n double a, b, c, d, first[1010], second[1010], p, q, r; n pair<pair<double, double>, double> m[1010 * 1010]; n bool cmp(pair<pair<double, double>, double> a, n          pair<pair<double, double>, double> b) { n   if (fabs(a.first.first - b.first.first) > 1e-8) n     return a.first.first < b.first.first; n   if (fabs(a.first.second - b.first.second) > 1e-8) n     return a.first.second < b.first.second; n   return a.second < b.second; n } n int main() { n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %lf %lf %lf %lf , &a, &b, &c, &d); n     p = a / b, q = c / d; n     r = p * p + q * q; n     first[i] = p / r, second[i] = q / r; n   } n   for (i = 0; i < n; i++) { n     for (j = i + 1; j < n; j++) { n       p = first[i] + first[j], q = second[i] + second[j]; n       m[o++] = pair<pair<double, double>, double>( n           pair<double, double>(p, q), n           fabs(first[i] - first[j]) < 1e-8 n               ? 0x3f3f3f3f n               : (second[i] - second[j]) / (first[i] - first[j])); n     } n   } n   sort(m, m + o, cmp); n   for (i = 0; i < o; i = j) { n     cnt = num = 1; n     for (j = i + 1; j < o; j++) { n       if (fabs(m[j].first.first - m[j - 1].first.first) > 1e-8) break; n       if (fabs(m[j].first.second - m[j - 1].first.second) > 1e-8) break; n       if (fabs(m[j].second - m[j - 1].second) > 1e-8) { n         cnt = (long long)cnt * (num + 1) % 1000000007; n         num = 1; n       } else n         num++; n     } n     cnt = (long long)cnt * (num + 1) % 1000000007; n     ans = (ans + cnt) % 1000000007; n     ans = (ans + 1000000007 - (j - i + 1)) % 1000000007; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long n; n   scanf( %ld , &n); n   std::vector<long> a(n); n   for (long p = 0; p < n; p++) { n     scanf( %ld , &a[p]); n   } n   sort(a.begin(), a.end()); n   long cur(0); n   for (long p = 0; p < n; p++) { n     if (a[p] >= cur + 1) { n       ++cur; n     } n   } n   printf( %ld n , cur); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double eps = 1e-9; n const int inf = (1 << 30) - 1; n const long long inf64 = ((long long)1 << 62) - 1; n const long double pi = acos(-1); n template <class T> n T sqr(T x) { n   return x * x; n } n template <class T> n T abs(T x) { n   return x < 0 ? -x : x; n } n template <class T> n vector<T> ReadVector(int n) { n   vector<T> a(n); n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   return a; n } n template <class T> n void WriteVector(const vector<T>& a) { n   for (int i = 0; i < ((int)(a).size()); ++i) { n     cout << a[i] << (i + 1 < ((int)(a).size()) ?     :   n ); n   } n } n void Solve() { n   long long x; n   cin >> x; n   vector<long long> a; n   a.push_back(x); n   vector<string> res; n   while (a[0] != 1) { n     long long val = -1; n     for (int i = 0; i < ((int)(a).size()); ++i) { n       for (int j = 0; j <= i; ++j) { n         long long tmp = a[i] + a[j]; n         val = tmp; n         for (int k = ((int)(a).size()) - 1; k >= 0; --k) { n           tmp = min(tmp, tmp ^ a[k]); n         } n         if (tmp > 0) { n           res.push_back(to_string(a[i]) +   +   + to_string(a[j])); n           break; n         } n         val = -1; n       } n       if (val > 0) { n         break; n       } n     } n     for (int i = ((int)(a).size()) - 1; i >= 0; --i) { n       if ((val ^ a[i]) < val) { n         res.push_back(to_string(a[i]) +   ^   + to_string(val)); n         val ^= a[i]; n       } n     } n     for (int i = 0; i < ((int)(a).size()); ++i) { n       if ((a[i] ^ val) < a[i]) { n         res.push_back(to_string(a[i]) +   ^   + to_string(val)); n         a[i] ^= val; n       } n     } n     a.push_back(val); n     sort((a).begin(), (a).end()); n   } n   cout << ((int)(res).size()) <<   n ; n   for (int i = 0; i < ((int)(res).size()); ++i) { n     cout << res[i] <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   std::cin.tie(nullptr); n   int tc = 1; n   for (int ti = 0; ti < tc; ++ti) { n     Solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char ara[100005]; n map<int, int> mp; n int main() { n   int t, test; n   long long a, b, c, n; n   char ch; n   double d; n   gets(ara); n   t = strlen(ara); n   int aa = ara[t - 1] -  0 ; n   int bb = ara[t - 2] -  0 ; n   aa += (bb * 10); n   if (aa % 4 == 0) { n     cout <<  4 n ; n   } else { n     cout <<  0 n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int ans; n int a[2002], b[2002]; n int le; n int p; n int ssearch(int x) { n   int maxd = 0; n   for (int i = 1; i <= n; i++) { n     int z = abs(a[i] - b[x - le + i]) + abs(b[x - le + i] - p); n     if (z > maxd) maxd = z; n   } n   return maxd; n } n int main() { n   int k; n   scanf( %d%d%d , &n, &k, &p); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   for (int i = 1; i <= k; i++) { n     scanf( %d , &b[i]); n   } n   sort(a + 1, a + 1 + n); n   sort(b + 1, b + 1 + k); n   le = n; n   for (int i = 1; i <= n; i++) n     if (a[i] >= p) { n       le = i - 1; n       break; n     } n   ans = 2147483647; n   for (int i = le; i <= k + le - n; i++) { n     int y = ssearch(i); n     if (y < ans) ans = y; n   } n   printf( %d n , ans); n   return 0; n } n