#include <bits/stdc++.h> n using namespace std; n int n, m, S[2005]; n bool mark[2005][2005]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   char c; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       cin >> c; n       mark[i][j] = c -  0 ; n       if (mark[i][j]) S[j]++; n     } n   bool flag; n   for (int i = 1; i <= n; i++) { n     flag = 0; n     for (int j = 1; j <= m; j++) n       if (mark[i][j]) S[j]--; n     for (int j = 1; j <= m; j++) n       if (S[j] == 0) { n         flag = 1; n         break; n       } n     if (!flag) { n       cout <<  YES ; n       return 0; n     } n     for (int j = 1; j <= m; j++) n       if (mark[i][j]) S[j]++; n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 400500; n long long A[N]; n long long B[N]; n inline long long mymod2(long long x) { n   if (x % 2 == 0) n     return 0; n   else n     return 1; n } n inline long long mydiv2(long long x) { n   if (x >= 0) n     return x / 2; n   else { n     if (x % 2 == 0) n       return x / 2; n     else n       return x / 2 - 1; n   } n } n void buildB() { n   for (int i = 0; i < N; i++) B[i] = A[i]; n   for (int i = 0; i < N - 1; i++) { n     B[i + 1] += mydiv2(B[i]); n     B[i] = mymod2(B[i]); n   } n } n int main() { n   int n, k; n   scanf( %d %d , &n, &k); n   for (int i = 0; i <= n; i++) { n     int x; n     scanf( %d , &x); n     A[i] = x; n   } n   buildB(); n   if (B[N - 1]) { n     for (int i = 0; i <= n; i++) A[i] = -A[i]; n     buildB(); n   } n   int fst = 0; n   while (fst != N && B[fst] == 0) { n     fst++; n   } n   assert(fst != N); n   long long cur = 0; n   bool bad = false; n   for (int i = fst; i < N; i++) { n     if (B[i]) { n       int p = i - fst; n       if (p > 40) { n         bad = true; n         break; n       } n       cur += 1ll << p; n     } n   } n   int cnt = 0; n   if (!bad) { n     for (int i = fst; i >= 0; i--) { n       if (cur > (1ll << 32)) break; n       long long nA = A[i] - cur; n       if (abs(nA) <= k && (i != n || nA != 0) && (i <= n)) cnt++; n       cur <<= 1; n     } n   } n   printf( %d n , cnt); n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long int n; n   cin >> n; n   vector<long long int> v(n); n   for (int i = 0; i < n; i++) cin >> v[i]; n   sort(v.begin(), v.end()); n   if (n == 1) { n     int q; n     cin >> q; n     for (int i = 0; i < q; i++) { n       long long int l, r; n       cin >> l >> r; n       cout << r - l + 1 <<    ; n     } n     return 0; n   } n   vector<long long int> use(n - 1); n   for (int i = 1; i < n; i++) { n     use[i - 1] = v[i] - v[i - 1]; n   } n   sort(use.begin(), use.end()); n   vector<long long int> pref(n - 1); n   pref[0] = use[0]; n   for (int i = 1; i < n - 1; i++) { n     pref[i] = pref[i - 1] + use[i]; n   } n   int q; n   cin >> q; n   for (int i = 0; i < q; i++) { n     long long int l, r; n     cin >> l >> r; n     long long int ans = 0; n     long long int val = r - l + 1; n     long long int where = n         upper_bound(use.begin(), use.end(), val) - use.begin(); n     if (where) ans = ans + pref[where - 1]; n     long long int extra = (val) * (n - 1 - where); n     ans = ans + extra; n     ans = ans + val; n     cout << ans <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   char tt =  a ; n   int k = s.size(), c = 0; n   for (int i = 0; i < k; i++) { n     int x = int(tt); n     int y = int(s[i]); n     int a = abs(x - y); n     int b = 26 - a; n     b = min(b, a); n     c += b; n     tt = s[i]; n   } n   cout << c << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void err(istream_iterator<string> it) { cerr << endl; } n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cerr << *it <<   =   << a <<   ,  ; n   err(++it, args...); n } n namespace fastIO { n bool IOerror = 0; n inline char nc() { n   static char buf[100000], *p1 = buf + 100000, *pend = buf + 100000; n   if (p1 == pend) { n     p1 = buf; n     pend = buf + fread(buf, 1, 100000, stdin); n     if (pend == p1) { n       IOerror = 1; n       return -1; n     } n   } n   return *p1++; n } n inline bool blank(char ch) { n   return ch ==     || ch ==   n  || ch ==   r  || ch ==   t ; n } n inline void read(int &x) { n   char ch; n   while (blank(ch = nc())) n     ; n   if (IOerror) return; n   for (x = ch -  0 ; (ch = nc()) >=  0  && ch <=  9 ; x = x * 10 + ch -  0 ) n     ; n } n }  // namespace fastIO n using namespace fastIO; n const double PI = acos(-1.0); n const double eps = 1e-8; n const int mod = 1e9 + 7; n long long qp(long long b, long long n) { n   long long r = 1; n   if (n < 0) n += mod - 1; n   b %= mod; n   for (; n; n >>= 1, b = b * b % mod) n     if (n & 1) r = r * b % mod; n   return r; n } n int sgn(double x) { n   if (x < -eps) return -1; n   return x > eps ? 1 : 0; n } n const int maxn = 3e5 + 7; n const int N = 1e5 + 10; n int n; n int sum[N], dif[N], a[N]; n pair<int, int> p[N]; n int main() { n   scanf( %d , &n); n   long long ans = 0; n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     sum[a[i]]++; n     ans += (1LL * a[i] * (n - a[i] + 1)); n   } n   for (int i = 1; i <= n; ++i) sum[i] += sum[i - 1]; n   for (int i = 1; i < n; ++i) { n     p[i].first = a[i]; n     p[i].second = a[i + 1]; n     if (p[i].first > p[i].second) swap(p[i].first, p[i].second); n     ans -= 1LL * p[i].first * (n - p[i].second + 1); n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void checkmin(T &a, T b) { n   if (b < a) a = b; n } n template <class T> n inline void checkmax(T &a, T b) { n   if (b > a) a = b; n } n int N, K; n char cs[100002]; n int main() { n   scanf( %d%d , &N, &K); n   for (int i = (0); i < (int)(N); i++) cs[i] =  1 ; n   for (int i = (0); i < (int)(K); i++) cs[i] =  0 ; n   cs[N] = 0; n   if (K > 1 && K != N) { n     int cnt = (N - K) / 2; n     for (int i = 0; i < N; i += cnt + 1) { n       for (int j = 0; j < cnt + 1 && i + j < N; j++) { n         if (j == 0) n           cs[i + j] =  0 ; n         else n           cs[i + j] =  1 ; n       } n     } n   } n   printf( %s n , cs); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2025; n const int MOD = 998244353; n int n, k; n namespace Task1 { n int ans; n int f[MAXN][MAXN]; n int sum[MAXN][MAXN]; n void solve() { n   f[0][n] = 1; n   for (int i = n; i >= 1; i--) sum[0][i] = 1; n   for (int i = 1; i <= n; i++) n     for (int j = i; j >= 1; j--) { n       f[i][j] = sum[i - j][j]; n       (ans += f[i][j]) %= MOD; n       sum[i][j] = (sum[i][j + 1] + f[i][j]) % MOD; n     } n   printf( %d n , ans); n } n }  // namespace Task1 n namespace Task2 { n int ans, N; n vector<int> f[MAXN][70]; n vector<int> sum[MAXN][70]; n void solve() { n   N = min(n, 65); n   for (int i = 0; i <= n; i++) { n     f[i][0].resize(MAXN); n     sum[i][0].resize(MAXN); n     for (int j = 1; j <= N; j++) { n       f[i][j].resize(i / j + 2); n       sum[i][j].resize(i / j + 2); n     } n   } n   f[0][0][n] = 1; n   for (int i = n; i >= 1; i--) sum[0][0][i] = 1; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= N; j++) n       for (int k = i / j; k >= 1; k--) { n         if (sum[i - j * k][j - 1].size() > k) n           f[i][j][k] = sum[i - j * k][j - 1][k]; n         (ans += f[i][j][k]) %= MOD; n         sum[i][j][k] = (sum[i][j][k + 1] + f[i][j][k]) % MOD; n       } n   printf( %d n , ans); n } n }  // namespace Task2 n namespace Task3 { n int ans; n vector<int> a; n bool chk(vector<int> t) { n   int K = k; n   while (K--) { n     vector<int> tt; n     int sum = 0; n     for (int i = 0; i < t.size(); i++) sum += t[i]; n     if (sum > n) return 0; n     tt.resize(sum); n     int tot = 0; n     for (int i = t.size() - 1; i >= 0; i--) n       for (int j = 1; j <= t[i]; j++) tt[tot++] = i + 1; n     swap(tt, t); n   } n   return 1; n } n void dfs(int d) { n   if (d > 1 && !chk(a)) return; n   if (d > 1) ans++; n   int t = d > 1 ? a.back() : n; n   for (int i = 1; i <= t; i++) { n     a.push_back(i); n     int tmp = ans; n     dfs(d + 1); n     a.pop_back(); n     if (ans == tmp) return; n   } n } n void solve() { n   dfs(1); n   printf( %d n , ans); n } n }  // namespace Task3 n int main() { n   scanf( %d%d , &n, &k); n   if (k == 1) Task1::solve(); n   if (k == 2) Task2::solve(); n   if (k >= 3) Task3::solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n const int inf = 0x3f3f3f3f; n int n; n vector<int> vec[N]; n int dis(int s, int t) { n   if (t >= s) return t - s; n   return t - s + n; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int m; n   cin >> n >> m; n   int num = 0; n   for (int i = 1; i <= m; i++) { n     int a, b; n     cin >> a >> b; n     vec[a].push_back(dis(a, b)); n     num = max(num, (int)vec[a].size()); n   } n   for (int i = 1; i <= n; i++) { n     sort(vec[i].begin(), vec[i].end()); n   } n   for (int st = 1; st <= n; st++) { n     int ans = 0; n     for (int i = 1; i <= n; i++) { n       if (vec[i].size() > 0) n         ans = max(ans, dis(st, i) + ((int)vec[i].size() - 1) * n + vec[i][0]); n     } n     cout << ans <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   vector<int> silver, gold; n   int g = 0, mxg = -1; n   ; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  S ) { n       gold.push_back(g); n       if (g > mxg) mxg = g; n       g = 0; n       silver.push_back(i); n     } else n       g++; n   } n   if (g) gold.push_back(g); n   if (g > mxg) mxg = g; n   for (int i = 0; i < silver.size(); i++) { n     if (i == gold.size() - 1) break; n     if (gold[i] > 0 && gold[i + 1]) { n       int g = gold[i] + gold[i + 1]; n       bool found = 0; n       for (int p = 0; p < gold.size(); p++) { n         if (p != i && p != i + 1 && gold[p] > 0) { n           found = true; n           break; n         } n       } n       if (found) g++; n       if (g > mxg) mxg = g; n     } else if (gold[i] > 0) { n       int g = gold[i]; n       bool found = 0; n       for (int p = 0; p < gold.size(); p++) { n         if (p != i && gold[p] > 0) { n           found = true; n           break; n         } n       } n       if (found) g++; n       if (g > mxg) mxg = g; n     } else if (gold[i + 1] > 0) { n       int g = gold[i + 1]; n       bool found = 0; n       for (int p = 0; p < gold.size(); p++) { n         if (p != i + 1 && gold[p] > 0) { n           found = true; n           break; n         } n       } n       if (found) g++; n       if (g > mxg) mxg = g; n     } n   } n   cout << mxg << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 1e5 + 10; n const long long MAXL = 20; n long long n, m; n long long v[MAXN], s[MAXN]; n long long rmq[MAXL][MAXN]; n long long resp[MAXN]; n long long best[MAXN]; n void buildRMQ() { n   for (long long k = 0; k < n; k++) rmq[0][k] = s[k]; n   for (long long i = 1; i < MAXL; i++) n     for (long long k = 0; k < MAXN; k++) n       if (k + (1 << i) <= n) n         rmq[i][k] = max(rmq[i - 1][k], rmq[i - 1][k + (1 << i - 1)]); n } n long long RMQ(long long a, long long b) { n   long long size = b - a + 1; n   long long log = 31 - __builtin_clz(size); n   return max(rmq[log][a], rmq[log][b - (1 << log) + 1]); n } n long long bs(long long ini, long long fim, long long pos) { n   while (ini < fim) { n     long long mid = (ini + fim + 1) / 2; n     if (RMQ(mid, pos - 1) >= s[pos]) n       ini = mid; n     else n       fim = mid - 1; n   } n   return ini; n } n int main() { n   scanf( %lld %lld , &n, &m); n   for (long long i = 0; i < n; i++) scanf( %lld , &v[i]); n   s[0] = 0; n   for (long long i = 1; i < n; i++) s[i] = abs(v[i] - v[i - 1]); n   buildRMQ(); n   while (m--) { n     long long p, q; n     scanf( %lld %lld , &p, &q); n     p--; n     q--; n     long long ans = 0; n     stack<pair<long long, long long> > st; n     for (long long i = p; i <= q; i++) { n       while (st.size() && st.top().first <= s[i]) st.pop(); n       if (st.size()) n         best[i] = st.top().second; n       else n         best[i] = 0; n       st.push(pair<long long, long long>(s[i], i)); n     } n     for (long long i = p; i <= q; i++) { n       resp[i] = 0; n       long long bt = best[i]; n       if (s[bt] >= s[i]) { n         resp[i] = s[i] * (i - bt) + resp[bt]; n       } else { n         resp[i] = s[i] * (i - p); n       } n       ans += resp[i]; n     } n     printf( %lld n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 201; n const long long K = 201; n const long long X = 201; n long long dp[N][X]; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n, k, x; n   cin >> n >> k >> x; n   vector<long long> picBeauty; n   picBeauty.push_back(-1); n   for (long long i = 1; i <= n; i++) { n     long long x; n     cin >> x; n     picBeauty.push_back(x); n   } n   long long minimumPosts = n / k; n   if (x < minimumPosts) { n     cout << -1 << endl; n     return 0; n   } n   for (long long i = 0; i <= n; i++) { n     for (long long j = 0; j <= x; j++) { n       dp[i][j] = 0; n     } n   } n   for (long long i = 1; i <= k; i++) { n     dp[i][1] = picBeauty[i]; n   } n   long long flag = 0; n   for (long long i = 1; i <= n; i++) { n     for (long long j = 2; j <= i && j <= x; j++) { n       long long tempFlag = 0; n       long long backAllowed = i - k; n       for (long long y = i - 1; y >= 1 && y >= backAllowed; y--) { n         if (dp[y][j - 1] > 0) n           dp[i][j] = max(dp[i][j], dp[y][j - 1] + picBeauty[i]); n       } n     } n   } n   long long ans = 0; n   for (long long i = n - k + 1; i <= n; i++) { n     ans = max(ans, dp[i][x]); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Q { n   int l, r, id; n   bool operator<(const Q a) const { n     if (l == a.l) return r < a.r; n     return l < a.l; n   } n } q[400003]; n int a[400003], b[400003]; n int pos[400003]; n int m, sum[400003]; n int lb(int k) { return k & -k; } n void add(int k, int v) { n   while (k <= m) { n     sum[k] += v; n     k += lb(k); n   } n } n int s(int k) { n   int re = 0; n   while (k) { n     re += sum[k]; n     k -= lb(k); n   } n   return re; n } n int L[400003]; n int main() { n   int i, j, n; n   while (~scanf( %d , &n)) { n     memset(sum, 0, sizeof(sum)); n     m = 0; n     for (i = 0; i < n; i++) { n       scanf( %d%d , &q[i].l, &q[i].r); n       q[i].id = i; n       b[m++] = q[i].l, b[m++] = q[i].r; n     } n     sort(b, b + m); n     m = unique(b, b + m) - b; n     for (i = 0; i < n; i++) { n       int id = lower_bound(b, b + m, q[i].r) - b + 1; n       add(id, 1); n     } n     sort(q, q + n); n     int ans = 0; n     for (i = 0; i < n; i++) { n       int l = lower_bound(b, b + m, q[i].l) - b + 1; n       int r = lower_bound(b, b + m, q[i].r) - b + 1; n       add(r, -1); n       ans = s(r) - s(l - 1); n       L[q[i].id] = ans; n     } n     for (i = 0; i < n; i++) { n       printf( %d n , L[i]); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     string s; n     cin >> s; n     string w; n     for (int i = 0; i <= 2 * n - 2; i = i + 2) { n       w = w + s[i]; n     } n     cout << w << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > p; n int main() { n   int tc; n   cin >> tc; n   while (tc--) { n     p.clear(); n     int n, a, b; n     cin >> n; n     bool value = true; n     for (int i = 0; i < n; ++i) { n       cin >> a >> b; n       if (a < b) value = false; n       if (p.empty()) n         p.push_back({a, b}); n       else { n         if (a < p.back().first || b < p.back().second || n             a - p.back().first < b - p.back().second) n           value = false; n         p.push_back({a, b}); n       } n     } n     if (value) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e3 + 5; n const long long MOD = 1e9 + 7; n long long nCr[N][N]; n void precompute() { n   nCr[0][0] = 1; n   for (long long i = 1; i < N; i++) { n     nCr[i][0] = 1; n     for (long long j = 1; j <= i; j++) { n       nCr[i][j] = nCr[i - 1][j - 1] + nCr[i - 1][j]; n       nCr[i][j] %= MOD; n     } n   } n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   precompute(); n   long long n, m, k; n   cin >> n >> m >> k; n   long long ans = nCr[n - 1][2 * k] * nCr[m - 1][2 * k]; n   ans %= MOD; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, a[110][110], i, j, maxi, f, maxid[110], c = 0, poi; n   scanf( %d%d , &n, &m); n   for (i = 0; i < m; i++) { n     for (j = 0; j < n; j++) scanf( %d , &a[i][j]); n   } n   for (i = 0; i < m; i++) { n     maxi = a[i][0]; n     maxid[i] = 0; n     for (j = 0; j < n; j++) { n       if (maxi < a[i][j]) { n         maxi = a[i][j]; n         maxid[i] = j; n       } n     } n   } n   f = 0; n   sort(maxid, maxid + m); n   poi = maxid[0]; n   for (i = 1; i < m; i++) { n     if (maxid[i] != maxid[i - 1]) { n       if (f < c) { n         f = c; n         poi = maxid[i - 1]; n       } n       c = 0; n     } else n       c++; n   } n   printf( %d n , poi + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const double eps = 1e-11; n template <class T> n T sqr(const T &x) { n   return x * x; n } n template <class T> n T lowbit(const T &x) { n   return (x ^ (x - 1)) & x; n } n inline long long mlog2(long long n) { n   long long c = 0; n   c += 64 - __builtin_clzll(n); n   return c - 1; n } n void fun(int a, int b) { n   for (b = 0; b < 5; b++) a++; n } n bool arr[15][15] = {0}; n int k; n bool check(int a, int b, int c, int d) { n   int i, j, tp = 0; n   for (i = a; i <= b; i++) n     for (j = c; j <= d; j++) { n       if (arr[i][j] == 1) tp++; n       if (tp >= k) return 1; n     } n   return 0; n } n int main() { n   int r, c, n, i, j, p, q, cou = 0, x, y; n   cin >> r >> c >> n >> k; n   for (i = 0; i < n; i++) { n     cin >> x >> y; n     x--; n     y--; n     arr[x][y] = 1; n   } n   for (i = 0; i < r; i++) { n     for (j = i; j < r; j++) { n       for (p = 0; p < c; p++) { n         for (q = p; q < c; q++) { n           if (check(i, j, p, q)) cou++; n         } n       } n     } n   } n   cout << cou; n } n


#include <bits/stdc++.h> n using namespace std; n string st; n long ans, tans, n, ttl, qpos; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   cin >> st; n   for (int i = 0; i < st.size(); i++) n     if (st[i] ==  H ) ++ttl; n   ans = 1000000; n   for (int stp = 0; stp < n; stp++) { n     tans = 0; n     qpos = stp; n     for (int i = 1; i <= ttl; i++) { n       if (st[qpos] ==  T ) ++tans; n       ++qpos; n       if (qpos == n) qpos = 0; n     } n     ans = min(ans, tans); n   } n   cout << ans << endl; n   cin.get(); n   cin.get(); n   return 0; n } n


#include <bits/stdc++.h> n const int N = 60; n const int M = 11; n const int MOD = 1e9 + 7; n int binom[N + 1][N + 1], n, a[N], parent[N], inDegree[N], v0[N], v1[N], msk1[N], n     dp[N + 1][1 << M]; n int findRoot(int u) { n   if (parent[u] != u) { n     parent[u] = findRoot(parent[u]); n   } n   return parent[u]; n } n void add(int &x, int a) { n   x += a; n   if (x >= MOD) { n     x -= MOD; n   } n } n int solve(int n0, int n1) { n   assert(n0 <= M); n   for (int j = 0; j < n1; ++j) { n     msk1[j] = 0; n   } n   for (int i = 0; i < n0; ++i) { n     for (int j = 0; j < n1; ++j) { n       if (v1[j] % v0[i] == 0) { n         msk1[j] |= 1 << i; n       } n     } n   } n   for (int i = 1; i <= n1; ++i) { n     for (int msk = 0; msk < 1 << n0; ++msk) { n       dp[i][msk] = 0; n     } n   } n   for (int i = 0; i < n1; ++i) { n     add(dp[1][msk1[i]], 1); n   } n   for (int i = 1; i < n1; ++i) { n     for (int msk = 0; msk < 1 << n0; ++msk) { n       if (dp[i][msk]) { n         int cnt = 0; n         for (int j = 0; j < n1; ++j) { n           if ((msk1[j] & msk) == msk1[j]) { n             cnt++; n           } else if (msk1[j] & msk) { n             add(dp[i + 1][msk | msk1[j]], dp[i][msk]); n           } n         } n         if (i < cnt) { n           add(dp[i + 1][msk], 1LL * (cnt - i) * dp[i][msk] % MOD); n         } n       } n     } n   } n   return dp[n1][(1 << n0) - 1]; n } n int main() { n   for (int i = 0; i <= N; ++i) { n     binom[i][0] = 1; n     for (int j = 1; j <= i; ++j) { n       binom[i][j] = (binom[i - 1][j - 1] + binom[i - 1][j]) % MOD; n     } n   } n   while (scanf( %d , &n) == 1) { n     for (int i = 0; i < n; ++i) { n       scanf( %d , a + i); n       parent[i] = i; n       inDegree[i] = 0; n     } n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < n; ++j) { n         if (i != j && a[j] % a[i] == 0) { n           if (findRoot(i) != findRoot(j)) { n             parent[findRoot(i)] = findRoot(j); n           } n           inDegree[j]++; n         } n       } n     } n     int result = 1, size = 0; n     for (int group = 0; group < n; ++group) { n       if (parent[group] == group) { n         int n0 = 0, n1 = 0; n         for (int i = 0; i < n; ++i) { n           if (findRoot(i) == group) { n             if (inDegree[i]) { n               v1[n1++] = a[i]; n             } else { n               v0[n0++] = a[i]; n             } n           } n         } n         if (n1) { n           size += n1 - 1; n           result = n               1LL * result * binom[size][n1 - 1] % MOD * solve(n0, n1) % MOD; n         } n       } n     } n     printf( %d n , result); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 2010; n const long long mod = 998244353; n long long pw(long long a, long long b) { n   if (b == 0) return 1; n   long long k = pw(a, b / 2); n   k = (k * k) % mod; n   if (b & 1) k = (k * a) % mod; n   return k; n } n long long n, m; n long long dp[maxn][maxn], cnt[maxn], f[maxn][maxn]; n string s; n signed main() { n   s +=  . ; n   string ss; n   cin >> ss; n   n = ss.size(); n   s += ss; n   for (long long i = 1; i <= n; i++) cnt[i] = cnt[i - 1] + (s[i] ==  ? ); n   for (long long t = 2; t <= n; t++) n     for (long long l = 1; l + t - 1 <= n; l++) { n       long long r = l + t - 1; n       if (t == 2) f[l][r] = (s[l] !=  ) ); n       if (s[l] ==  ) ) { n         dp[l][r] = dp[l + 1][r]; n         f[l][r] = f[l + 1][r]; n         continue; n       } n       if (s[r] ==  ( ) { n         dp[l][r] = dp[l][r - 1]; n         continue; n       } n       if (t == 2) { n         dp[l][r] = 1; n         continue; n       } n       long long K = pw(2, cnt[r - 1] - cnt[l]); n       f[l][r] = K + dp[l + 1][r - 1]; n       if (s[l] ==  ? ) f[l][r] += f[l + 1][r]; n       f[l][r] %= mod; n       if (s[l] ==  (  && s[r] ==  ) ) dp[l][r] = (dp[l + 1][r - 1] + K) % mod; n       if (s[l] ==  ?  && s[r] ==  ) ) n         dp[l][r] = (dp[l + 1][r] + dp[l + 1][r - 1] + K) % mod; n       if (s[l] ==  (  && s[r] ==  ? ) n         dp[l][r] = (dp[l][r - 1] + dp[l + 1][r - 1] + K) % mod; n       if (s[l] ==  ?  && s[r] ==  ? ) n         dp[l][r] = (dp[l][r - 1] + dp[l + 1][r] + K) % mod; n     } n   cout << dp[1][n]; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 1e5 + 10; n const int MaxL = 18; n const int INF = 1e9; n const int MOD = 1e9 + 7; n int up[MaxN][MaxL], ds[MaxN]; n vector<int> graph[MaxN]; n int n, m, sz[MaxN]; n long long sum[MaxN], ups[MaxN]; n void dfsLca(int v, int p) { n   up[v][0] = p; n   for (int i = 1; i < MaxL; ++i) { n     up[v][i] = up[up[v][i - 1]][i - 1]; n   } n   sz[v] = 1; n   for (int i = 0; i < (int)graph[v].size(); ++i) { n     int to = graph[v][i]; n     if (to == p) { n       continue; n     } n     ds[to] = ds[v] + 1; n     dfsLca(to, v); n     sz[v] += sz[to]; n     sum[v] += sz[to] + sum[to]; n   } n } n void dfsUp(int v, int p) { n   if (p != -1) { n     ups[v] = (sum[p] - (sum[v] + sz[v]) + ups[p]) + (n - sz[v]); n   } n   for (int i = 0; i < (int)graph[v].size(); ++i) { n     int to = graph[v][i]; n     if (to == p) { n       continue; n     } n     dfsUp(to, v); n   } n } n int getLca(int x, int y) { n   if (ds[x] > ds[y]) { n     swap(x, y); n   } n   for (int i = MaxL - 1; i >= 0; --i) { n     if (ds[up[y][i]] >= ds[x]) { n       y = up[y][i]; n     } n   } n   if (x == y) { n     return x; n   } n   for (int i = MaxL - 1; i >= 0; --i) { n     if (up[x][i] != up[y][i]) { n       x = up[x][i]; n       y = up[y][i]; n     } n   } n   return up[x][0]; n } n int getKth(int x, int k) { n   for (int i = MaxL - 1; i >= 0; --i) { n     if (k & (1 << i)) { n       x = up[x][i]; n     } n   } n   return x; n } n double solve(int x, int y) { n   int l = getLca(x, y); n   int len = ds[x] + ds[y] - 2 * ds[l]; n   double ans = 0; n   if (x != l && y != l) { n     long long total = 1LL * sz[x] * sz[y]; n     long long cnt = n         1LL * sz[x] * sum[y] + 1LL * sz[y] * sum[x] + 1LL * total * len + total; n     ans = 1.0 * cnt / total; n   } else { n     if (ds[x] > ds[y]) { n       swap(x, y); n     } n     int xy = getKth(y, len - 1); n     long long total = 1LL * (n - sz[xy]) * sz[y]; n     long long cnt = 1LL * (n - sz[xy]) * sum[y] + n                     1LL * (ups[xy] - (n - sz[xy])) * sz[y] + 1LL * total * len + n                     total; n     ans = 1.0 * cnt / total; n   } n   return ans; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n - 1; ++i) { n     int x, y; n     scanf( %d%d , &x, &y); n     x--; n     y--; n     graph[x].push_back(y); n     graph[y].push_back(x); n   } n   dfsLca(0, 0); n   dfsUp(0, -1); n   for (int i = 0; i < m; ++i) { n     int x, y; n     scanf( %d%d , &x, &y); n     x--; n     y--; n     printf( %.9lf n , solve(x, y)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> len(n); n   vector<string> s(n); n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n     len[i] = s[i].length(); n   } n   const int all = n - 1 + accumulate(len.begin(), len.end(), 0); n   vector<vector<bool>> eq(n, vector<bool>(n, false)); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (s[i] == s[j]) eq[i][j] = true; n     } n   } n   vector<vector<int>> dp(n, vector<int>(n, 0)); n   for (int i = n - 1; i >= 0; i--) { n     for (int j = n - 1; j >= 0; j--) n       if (eq[i][j]) { n         if (i + 1 < n and j + 1 < n) n           dp[i][j] = dp[i + 1][j + 1] + 1; n         else n           dp[i][j] = 1; n       } n   } n   int ans = all; n   for (int i = 0; i < n; i++) { n     int sum = 0; n     for (int d = 0; i + d < n; d++) { n       int cnt = 1; n       sum += len[i + d]; n       for (int pos = i + d + 1; pos < n; pos++) { n         if (dp[i][pos] > d) { n           cnt++; n           pos += d; n         } n       } n       int res = all - sum * cnt + cnt; n       if (cnt > 1 and ans > res) ans = res; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n long long max(long long a, long long b) { n   if (a > b) { n     return a; n   } else { n     return b; n   } n } n long long min(long long a, long long b) { n   if (a < b) { n     return a; n   } else { n     return b; n   } n } n long long sz(vector<long long> v) { n   if (v.empty()) { n     return 0; n   } else { n     return v.size(); n   } n } n long long sz(set<long long> v) { n   if (v.empty()) { n     return 0; n   } else { n     return v.size(); n   } n } n long long sz(vector<pair<long long, long long> > v) { n   if (v.empty()) { n     return 0; n   } else { n     return v.size(); n   } n } n long long power(long long b, long long e) { n   if (e == 0) return 1; n   if (e % 2) n     return ((b * power((b) * (b), (e - 1) / 2))); n   else n     return power((b) * (b), e / 2); n } n long long modpower(long long b, long long e, long long q) { n   long long MOD = q; n   if (e == 0) return 1; n   if (e % 2) n     return ((b % MOD) * modpower((b % MOD) * (b % MOD), (e - 1) / 2, q)) % MOD; n   else n     return modpower((b % MOD) * (b % MOD), e / 2, q) % MOD; n } n void dpv(vector<long long> v) { n   for (long long i = 0; i < v.size(); i++) { n     cout << v[i] <<    ; n   } n   cout <<   n ; n } n void dpv(vector<pair<long long, long long> > v) { n   for (long long i = 0; i < v.size(); i++) { n     cout << v[i].first <<     << v[i].second <<   n ; n   } n } n void dpv(set<long long> v) { n   for (auto i : v) { n     cout << i <<    ; n   } n   cout <<   n ; n } n void oblivious() { n   long long n; n   cin >> n; n   string s[n]; n   char c[n][n]; n   for (long long i = 0; i < n; i++) { n     cin >> s[i]; n     for (long long j = 0; j < s[i].length(); j++) { n       c[i][j] = s[i][j]; n     } n   } n   long long r[n], co[n]; n   memset(r, 0, sizeof(r)); n   memset(co, 0, sizeof(co)); n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < n; j++) { n       if (c[i][j] ==  . ) { n         r[i] = 1; n         co[j] = 1; n       } n     } n   } n   long long f1 = 1; n   long long f2 = 1; n   for (long long i = 0; i < n; i++) { n     if (r[i] == 0) { n       f1 = 0; n       break; n     } n   } n   for (long long i = 0; i < n; i++) { n     if (co[i] == 0) { n       f2 = 0; n       break; n     } n   } n   if (f1 || f2) { n     if (f1 && f2) { n       for (long long i = 0; i < n; i++) { n         for (long long j = 0; j < n; j++) { n           if (c[i][j] ==  . ) { n             cout << i + 1 <<     << j + 1 <<   n ; n             break; n           } n         } n       } n     } else if (f1) { n       for (long long i = 0; i < n; i++) { n         for (long long j = 0; j < n; j++) { n           if (c[i][j] ==  . ) { n             cout << i + 1 <<     << j + 1 <<   n ; n             break; n           } n         } n       } n     } else if (f2) { n       for (long long i = 0; i < n; i++) { n         for (long long j = 0; j < n; j++) { n           if (c[j][i] ==  . ) { n             cout << j + 1 <<     << i + 1 <<   n ; n             break; n           } n         } n       } n     } n   } else { n     cout <<  -1  <<   n ; n   } n } n signed main() { n   long long t = 1; n   while (t--) { n     oblivious(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 10, N = 2000; n long long m, n, shroo, I[maxn], tmm, id[maxn], J[maxn], W[maxn], Su[maxn], n     dp1[maxn], dp2[maxn]; n vector<pair<long long, long long> > hel1; n vector<int> tmp, in[maxn], out[maxn]; n bool amx(int i, int j) { return id[I[i]] > id[I[j]]; } n void dfs_1(int i) { n   id[i] = 1; n   for (auto A : out[i]) n     if (!id[A]) dfs_1(A); n   tmp.push_back(i); n } n void dfs_2(int i) { n   id[i] = tmm; n   for (auto A : in[i]) n     if (!id[A]) dfs_2(A); n } n void scc() { n   for (int y = 1; y <= n; y++) n     if (!id[y]) dfs_1(y); n   memset(id, 0, sizeof id); n   reverse(tmp.begin(), tmp.end()); n   for (auto A : tmp) n     if (!id[A]) tmm++, dfs_2(A); n } n long long gett(long long x) { n   int st = 0, en = 2e4 + 5, md, i; n   while (st <= en) { n     md = st + ((en - st) >> 1); n     if (2 * x >= md * (md + 1)) n       st = md + 1, i = md; n     else n       en = md - 1; n   } n   return (i + 1) * x - 1LL * i * (i + 1) * (i + 2) / 6; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int y = 1; y <= m; y++) { n     scanf( %d%d%d , &I[y], &J[y], &W[y]); n     in[J[y]].push_back(I[y]); n     out[I[y]].push_back(J[y]); n     Su[y] = gett(W[y]); n   } n   scc(); n   tmp.clear(); n   cin >> shroo; n   for (int y = 1; y <= m; y++) n     if (id[I[y]] == id[J[y]]) dp2[id[I[y]]] += Su[y]; n   for (int y = 1; y <= m; y++) n     if (id[I[y]] != id[J[y]]) tmp.push_back(y); n   sort(tmp.begin(), tmp.end(), amx); n   for (int y = 1; y <= n; y++) dp1[y] = dp2[y]; n   for (auto A : tmp) n     dp1[id[I[A]]] = max(dp1[id[I[A]]], dp2[id[I[A]]] + W[A] + dp1[id[J[A]]]); n   printf( %lld , dp1[id[shroo]]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100500; n struct matrix { n   long double a[3][3]; n   void clear() { memset(a, 0, sizeof(a)); } n   matrix operator*(const matrix &y) { n     matrix ans; n     ans.clear(); n     for (int i = (0), iend = (2); i <= iend; i++) n       for (int j = (0), jend = (2); j <= jend; j++) n         for (int k = (0), kend = (2); k <= kend; k++) n           ans.a[i][j] += a[i][k] * y.a[k][j]; n     return ans; n   } n } basis; n struct segment_tree { n   matrix sum[maxn << 2]; n   void push_up(int rt) { sum[rt] = sum[(rt << 1)] * sum[(rt << 1 | 1)]; } n   void upd(int l, int r, int rt, int tar, long double val) { n     if (l == r) { n       sum[rt].clear(); n       sum[rt].a[1][0] = 1; n       sum[rt].a[2][1] = 1; n       sum[rt].a[1][2] = (val - 1) / val; n       sum[rt].a[2][2] = 1 / val; n       return; n     } n     int mid = (l + r) >> 1; n     if (tar <= mid) n       upd(l, mid, (rt << 1), tar, val); n     else n       upd(mid + 1, r, (rt << 1 | 1), tar, val); n     push_up(rt); n   } n   matrix query(int l, int r, int rt, int tl, int tr) { n     if (tl <= l && r <= tr) return sum[rt]; n     int mid = (l + r) >> 1; n     matrix ans = basis; n     if (tl <= mid) ans = ans * query(l, mid, (rt << 1), tl, tr); n     if (tr >= mid + 1) ans = ans * query(mid + 1, r, (rt << 1 | 1), tl, tr); n     return ans; n   } n } t; n int main() { n   basis.clear(); n   for (int i = (0), iend = (2); i <= iend; i++) basis.a[i][i] = 1; n   int n, q; n   scanf( %d%d , &n, &q); n   for (int i = (1), iend = (n); i <= iend; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     t.upd(1, n, 1, i, (long double)(a) / b); n   } n   for (int i = (1), iend = (q); i <= iend; i++) { n     int ty; n     scanf( %d , &ty); n     if (ty == 1) { n       int idx, a, b; n       scanf( %d%d%d , &idx, &a, &b); n       t.upd(1, n, 1, idx, (long double)(a) / b); n     } else { n       int l, r; n       scanf( %d%d , &l, &r); n       matrix ans; n       ans.clear(); n       ans.a[0][2] = 1; n       ans = ans * t.query(1, n, 1, l, r); n       long double out = (long double)(1) / ans.a[0][2]; n       if (isnan(out)) out = 0; n       printf( %.7lf n , (double)(out)); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mn = 101000; n vector<int> e[mn]; n int c[mn], s[mn], sc[mn], sv[mn]; n int R[mn], vis[mn]; n int n, m, i, j, k, nc, tt; n long long ans; n bool cmpc(int i, int j) { return c[i] > c[j]; } n bool cmps(int i, int j) { return s[i] < s[j]; } n void add(int i, int d, int l) { n   s[0] = l; n   int j = lower_bound(sv + 1, sv + 1 + n, 0, cmps) - sv; n   if (j == n + 1) return; n   j = sv[j]; n   if (s[j] != l || c[j] > d) return; n   e[j].push_back(i); n } n inline bool dfs(int r) { n   int p, i; n   vis[r] = nc; n   for (p = 0; p < e[r].size(); ++p) { n     i = e[r][p]; n     if (R[i] == 0 || (vis[R[i]] != nc && dfs(R[i]))) { n       R[i] = r; n       return 1; n     } n   } n   return 0; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   for (i = 1; i <= n; ++i) cin >> c[i] >> s[i]; n   for (i = 1; i <= n; ++i) sc[i] = sv[i] = i; n   sort(sc + 1, sc + 1 + n, cmpc); n   sort(sv + 1, sv + 1 + n, cmps); n   cin >> m; n   for (i = 1; i <= m; ++i) { n     cin >> j >> k; n     add(i, j, k); n     add(i, j, k + 1); n   } n   for (i = 1; i <= n; ++i) { n     ++nc; n     if (dfs(sc[i])) ++tt, ans += c[sc[i]]; n   } n   cout << ans << endl; n   cout << tt << endl; n   for (i = 1; i <= m; ++i) n     if (R[i]) cout << i <<     << R[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MX = 2e5 + 3; n long long n, k, arr[MX], sum; n multiset<long long> st; n string s; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   for (long long i = 0; i < n; i++) cin >> arr[i]; n   cin >> s; n   st.insert(-arr[0]); n   for (long long i = 1; i < n; i++) { n     if (s[i] == s[i - 1]) n       st.insert(-arr[i]); n     else { n       long long a = 0; n       for (auto u : st) { n         if (a == k) break; n         sum -= u; n         a++; n       } n       st.clear(); n       st.insert(-arr[i]); n     } n   } n   long long a = 0; n   for (auto u : st) { n     if (a == k) break; n     sum -= u; n     a++; n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int n, root; n vector<int> adj[N]; n int dp[N][2], deg[N]; n bool mark[N]; n void dfs(int u, int pre) { n   for (int v : adj[u]) { n     if (v == pre) continue; n     dfs(v, u); n   } n   for (int p = 0, _n = (2); p < _n; p++) { n     int d = (deg[u] + ((p == 0 && pre != -1) ? -1 : 0)); n     vector<int> ban; n     for (int v : adj[u]) n       if (v != pre) { n         if (!dp[v][0]) ban.push_back(v); n       } n     bool flag = false; n     for (int v : ban) { n       if (!dp[v][1]) { n         flag = true; n         break; n       } n     } n     if (flag) continue; n     if ((d - ban.size()) % 2 == 1) { n       flag = true; n       for (int v : adj[u]) n         if (v != pre && dp[v][0]) { n           if (dp[v][1]) flag = false; n         } n       if (flag) continue; n     } n     dp[u][p] = 1; n   } n } n void print_ans(int u, int pre, int p) { n   int d = (deg[u] + ((p == 0 && pre != -1) ? -1 : 0)), cnt = 0; n   for (int v : adj[u]) n     if (v != pre) { n       if (!dp[v][0]) { n         cnt++; n         print_ans(v, u, 1); n         mark[v] = 1; n       } n     } n   if ((d - cnt) % 2 == 1) { n     for (int v : adj[u]) n       if (v != pre && dp[v][0] && dp[v][1]) { n         print_ans(v, u, 1); n         mark[v] = 1; n         break; n       } n   } n   cout << u <<   n ; n   for (int v : adj[u]) n     if (v != pre && !mark[v]) print_ans(v, u, 0); n } n int main(int argc, char const *argv[]) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   clock_t tStart = clock(); n   cin >> n; n   for (int i = (1), _b = (n); i <= _b; i++) { n     int j; n     cin >> j; n     if (j == 0) n       root = i; n     else { n       adj[i].push_back(j); n       adj[j].push_back(i); n       deg[i]++; n       deg[j]++; n     } n   } n   dfs(root, -1); n   if (!dp[root][0]) { n     cout <<  NO  <<   n ; n     return 0; n   } else { n     cout <<  YES  <<   n ; n     print_ans(root, -1, 0); n   } n   fprintf(stderr,  Time taken: %.2fs n , n           (double)(clock() - tStart) / CLOCKS_PER_SEC); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double weight = 0; n   long n, m; n   cin >> n >> m; n   long long a[n]; n   long long b[n]; n   bool flag = 0; n   for (long i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] == 1) { n       flag = 1; n     } n   } n   for (long i = 0; i < n; i++) { n     cin >> b[i]; n     if (b[i] == 1) { n       flag = 1; n     } n   } n   if (flag == 1) { n     cout << -1 << endl; n     exit(0); n   } n   weight = m; n   weight += double(weight) / double(b[0] - 1); n   for (long i = n - 1; i > 0; i--) { n     weight += double(weight) / double(a[i] - 1); n     weight += double(weight) / double(b[i] - 1); n   } n   weight += double(weight) / double(a[0] - 1); n   cout << setprecision(30) << weight - m << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 10050; n const int inf = 0x7fffffff; n string str; n int buf[maxn]; n vector<int> sans[10]; n int save[300] = {}; n string ansstr; n string bk; n int bK; n int main(void) { n   int n, K; n   cin >> n >> K >> str; n   int ans = inf; n   for (int k = ( 0 ); k <= ( 9 ); ++k) { n     for (int i = 0; i < (n); ++i) buf[i] = abs(k - str[i]); n     sort(buf, buf + n); n     int res = accumulate(buf, buf + K, 0); n     if (ans > res) ans = res; n     save[k] = res; n   } n   cout << ans << endl; n   for (int i = 0; i < (n); ++i) ansstr.push_back( 9 ); n   bk = str; n   bK = K; n   for (int no = ( 0 ); no <= ( 9 ); ++no) n     if (save[no] == ans) { n       K = bK; n       str = bk; n       for (int i = 0; i < (10); ++i) sans[i].clear(); n       for (int i = 0; i < (n); ++i) { n         if (str[i] >= no) n           sans[abs(no - str[i])].push_back(i); n         else n           sans[abs(no - str[i])].push_back(1000000 - i); n       } n       for (int i = 0; i < (10); ++i) sort(sans[i].begin(), sans[i].end()); n       for (int i = 0; i < (10); ++i) n         for (int j = 0; j < (sans[i].size()); ++j) { n           if (K <= 0) break; n           K--; n           int cnt = sans[i][j]; n           if (cnt > 500000) cnt -= 1000000; n           str[abs(cnt)] = no; n         } n       if (str < ansstr) ansstr = str; n     } n   cout << ansstr << endl; n } n


#include <bits/stdc++.h> n int n, m, a, b, mna = 101, mnb = 101, mxa, mxb; n void No() { printf( -1 ); } n void Yes(int n) { printf( %d , n); } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; n        i++, scanf( %d , &a), mna = std::min(mna, a), mxa = std::max(mxa, a)) n     ; n   for (int i = 0; i < m; i++, scanf( %d , &b), mnb = std::min(mnb, b)) n     ; n   mxa = std::max(mxa, 2 * mna); n   if (mxa >= mnb) n     No(); n   else n     Yes(mxa); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T bigmod(T p, T e, T M) { n   long long int ret = 1; n   for (; e > 0; e >>= 1) { n     if (e & 1) ret = (ret * p) % M; n     p = (p * p) % M; n   } n   return (T)ret; n } n template <class T> n inline T gcd(T a, T b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n template <class T> n inline T modinverse(T a, T M) { n   return bigmod(a, M - 2, M); n } n template <class T> n inline T bpow(T p, T e) { n   long long int ret = 1; n   for (; e > 0; e >>= 1) { n     if (e & 1) ret = (ret * p); n     p = (p * p); n   } n   return (T)ret; n } n int toInt(string s) { n   int sm; n   stringstream ss(s); n   ss >> sm; n   return sm; n } n int toLlint(string s) { n   long long int sm; n   stringstream ss(s); n   ss >> sm; n   return sm; n } n int ts, kk = 1; n int n, a[1000006]; n bool bl[1000006]; n char s[1000006]; n int p[2][2][1000006]; n int dp(bool st, bool c, int i) { n   if (i == n) { n     if (c && st) return -1; n     return 0; n   } n   int &pr = p[st][c][i]; n   if (pr != -1) return pr; n   pr = 0; n   pr = max(pr, dp(st, 0, i + 1)); n   if (bl[i] == 0) { n     if (c == 0) pr = max(dp((i == 0) || st, 1, i + 1) + 1, pr); n     if (a[i] > 1) pr = max(dp(st, 1, i + 1) + 1, pr); n   } else { n     if (c == 0) pr = max(dp((i == 0) || st, 1, i + 1) + 1, pr); n     if (a[i] > 1) pr = max(dp(st, 1, i + 1) + 1, pr); n   } n   return pr; n } n void prnt(bool st, bool c, int i) { n   if (i == n) { n     puts(  ); n     return; n   } n   int &pr = p[st][c][i]; n   if (dp(st, 0, i + 1) == pr) { n     prnt(st, 0, i + 1); n     return; n   } n   if (bl[i] == 0) { n     if (c == 0 && dp((i == 0), 1, i + 1) + 1 == pr) { n       printf( %d 0 0 n , i); n       prnt((i == 0), 1, i + 1); n       return; n     } n     if (a[i] > 1 && pr == dp(st, 1, i + 1) + 1) { n       printf( %d 0 1 n , i); n       prnt(st, 1, i + 1); n       return; n     } n   } else { n     if (c == 0 && dp((i == 0), 1, i + 1) + 1 == pr) { n       printf( %d 1 0 n , i); n       prnt((i == 0), 1, i + 1); n       return; n     } n     if (a[i] > 1 && pr == dp(st, 1, i + 1) + 1) { n       printf( %d 1 1 n , i); n       prnt(st, 1, i + 1); n       return; n     } n   } n } n int main() { n   int t, i, j, k; n   memset(p, -1, sizeof(p)); n   scanf(  %s , s); n   k = strlen(s); n   bl[0] = s[0] -  0 ; n   n = 1; n   for (i = 0; i < k; i++) { n     if ((bool)(s[i] -  0 ) == bl[n - 1]) n       a[n - 1]++; n     else { n       bl[n] = (s[i] -  0 ); n       a[n++] = 1; n     } n   } n   if (n > 1 && bl[n - 1] == bl[0]) { n     a[0] += a[n - 1]; n     n--; n   } n   k = dp(0, 0, 0); n   printf( %d n , k); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<pair<int, pair<int, int> > > l, u; n set<pair<int, pair<int, int> > >::iterator itu, itl; n const int INF = 1000 * 1000 * 1000LL + 10; n int main() { n   int n, q, a, b, ll, lm, lt, uu, um, ut; n   char c; n   long long ans; n   cin >> n >> q; n   for (int i = 0; i < q; i++) { n     scanf( %d %d %c , &a, &b, &c); n     ans = 0; n     if (c ==  U ) { n       l.insert(make_pair(b, make_pair(INF, INF))); n       u.insert(make_pair(a, make_pair(0, 0))); n       itl = l.find(make_pair(b, make_pair(INF, INF))); n       itu = u.find(make_pair(a, make_pair(0, 0))); n       itu++; n       if (itl == l.begin()) { n         ll = 0; n         lm = 0; n         lt = q; n       } else { n         itl--; n         ll = (*itl).first; n         lm = (*itl).second.first; n         lt = (*itl).second.second; n         itl++; n       } n       if (itu == u.end()) { n         uu = 0; n         um = 0; n         ut = q; n       } else { n         uu = n - (*itu).first + 1; n         um = (*itu).second.first; n         ut = (*itu).second.second; n       } n       itu--; n       l.erase(itl); n       u.erase(itu); n       if (uu > ll || (ll == uu && ut < lt)) { n         if (uu != b) ans += (um + b - uu); n         u.insert(make_pair(a, make_pair(um + b - uu, i + 1))); n       } else { n         ans += (b - ll); n         u.insert(make_pair(a, make_pair(b - ll, i + 1))); n       } n     } else { n       l.insert(make_pair(b, make_pair(0, 0))); n       u.insert(make_pair(a, make_pair(INF, INF))); n       itl = l.find(make_pair(b, make_pair(0, 0))); n       itu = u.find(make_pair(a, make_pair(INF, INF))); n       itl++; n       if (itl == l.end()) { n         ll = n + 1; n         lm = 0; n         lt = q; n       } else { n         ll = (*itl).first; n         lm = (*itl).second.first; n         lt = (*itl).second.second; n       } n       if (itu == u.begin()) { n         uu = n + 1; n         um = 0; n         ut = q; n       } else { n         itu--; n         uu = n - (*itu).first + 1; n         um = (*itu).second.first; n         ut = (*itu).second.second; n         itu++; n       } n       itl--; n       l.erase(itl); n       u.erase(itu); n       if (uu > ll || (ll == uu && ut > lt)) { n         if (ll != b) ans += (lm + ll - b); n         l.insert(make_pair(b, make_pair(lm + ll - b, i + 1))); n       } else { n         ans += (uu - b); n         l.insert(make_pair(b, make_pair(uu - b, i + 1))); n       } n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 4040; n map<int, int> cnt1; n map<int, int> cnt2; n pair<int, int> a[200020]; n int main() { n   int ans = 0; n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i].first >> a[i].second; n     cnt1[a[i].first + a[i].second]++; n     cnt2[a[i].first - a[i].second]++; n   } n   for (int i = 0; i < n; i++) { n     int curx = a[i].first; n     int cury = a[i].second; n     ans += cnt2[curx - cury] - 1 + cnt1[curx + cury] - 1; n   } n   cout << ans / 2 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int a[2000]; n int n; n inline bool check() { n   int c = 0; n   for (int i = 0; i < n; ++i) n     if (a[i] + 1 == a[i + 1]) ++c; n   if (c == n) n     return true; n   else n     return false; n } n inline void push() { n   for (int i = 0; i <= n; ++i) { n     if (i % 2 == 0) { n       if (a[i] == n) n         a[i] = 0; n       else n         a[i] = a[i] + 1; n     } else { n       if (a[i] == 0) n         a[i] = n; n       else n         a[i] = a[i] - 1; n     } n   } n } n int main() { n   scanf( %d , &n); n   --n; n   for (int i = 0; i <= n; ++i) { n     scanf( %d , &a[i]); n     if (i % 2 == 0) { n       if (a[i] == n) n         a[i] = 0; n       else n         a[i] = a[i] + 1; n     } else { n       if (a[i] == 0) n         a[i] = n; n       else n         a[i] = a[i] - 1; n     } n   } n   if (check()) { n     puts( Yes ); n     return 0; n   } else { n     for (int i = 0; i < n; ++i) { n       push(); n       if (check()) { n         puts( Yes ); n         return 0; n       } n     } n   } n   puts( No ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 55; n int n, l; n int a[maxn], b[maxn]; n bool check(int d, int x, int y) { n   int i = x + 1, j = y + 1; n   i %= n; n   j %= n; n   while (i != x && j != y) { n     if ((a[i] - b[j] + l) % l == d) { n       i++; n       i %= n; n       j++; n       j %= n; n     } else { n       return false; n     } n   } n   return true; n } n int main() { n   cin >> n >> l; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) cin >> b[i]; n   bool flag = false; n   for (int i = 0; i < n && !flag; i++) { n     for (int j = 0; j < n && !flag; j++) { n       int d = (a[i] - b[j] + l) % l; n       if (check(d, i, j)) { n         flag = true; n       } n     } n   } n   if (flag) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 100000 + 10; n int main() { n   long long n, a[maxN], c = 0, min = 1000000001, index; n   cin >> n; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] < min) { n       min = a[i]; n       c = 0; n       index = i; n     } else if (a[i] == min) { n       c++; n     } n   } n   if (c == 0) n     cout << index + 1; n   else n     cout <<  Still Rozdil ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c = 0; n   cin >> a >> b; n   int x[10] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6}; n   for (int i = a; i <= b; i++) { n     int y = i; n     while (y != 0) { n       c += x[y % 10]; n       y = y / 10; n     } n   } n   cout << c; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n using ld = long double; n const int MN = 2e5 + 3, MOD = 1e9 + 7; n void solve() { n   int n, a, b; n   scanf( %d %d %d , &n, &a, &b); n   if (a > b) swap(a, b); n   printf( %d %d n , min(n, max(1, a + b + 1 - n)), min(n, a + b - 1)); n } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {1, 0, -1, 0}; n int dy[] = {0, 1, 0, -1}; n const int N = (int)2e5 + 7; n const int MOD = (int)1e9 + 7; n const int INF = (int)2e7; n const long long BIG = 1e18; n int a[N], b[N], c[N], d[N]; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   ; n   int n, ans = 0; n   cin >> n; n   unordered_map<int, int> cnt; n   for (int i = 0; i < n; i++) cin >> a[i], cnt[a[i]]++; n   set<pair<int, int> > q; n   for (auto x : cnt) q.insert(make_pair(x.second, x.first)); n   while (int(q.size()) >= 3) { n     vector<pair<int, int> > del; n     auto it = q.end(); n     --it; n     b[ans] = it->second; n     del.push_back(*it); n     --it; n     c[ans] = it->second; n     del.push_back(*it); n     --it; n     d[ans] = it->second; n     del.push_back(*it); n     ++ans; n     for (auto x : del) { n       pair<int, int> w = x; n       q.erase(w); n       if (w.first != 1) q.insert(make_pair(w.first - 1, w.second)); n     } n   } n   cout << ans <<   n ; n   for (int i = 0; i < ans; i++) { n     vector<int> p = {b[i], c[i], d[i]}; n     sort(p.begin(), p.end()); n     cout << p[2] <<     << p[1] <<     << p[0] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 151; n int m, n; n char gd[N][N]; n inline int fab(int x) { return x > 0 ? x : -x; } n int main() { n   while (scanf( %d%d , &m, &n) != -1) { n     for (int i = 0; i < m; i++) scanf( %s , gd[i]); n     int ans = 0, x = 0, y = 0; n     for (int i = 0; i < m; i++) n       for (int j = (i & 1 ? n - 1 : 0); 0 <= j && j < n; j += (i & 1 ? -1 : 1)) n         if (gd[i][j] ==  W ) { n           ans += fab(i - x) + fab(j - y); n           x = i; n           y = j; n         } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long g, r; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, m; n   cin >> n >> m; n   vector<int> d; n   for (int i = 0; i < m; i++) { n     long long dt; n     cin >> dt; n     d.push_back(dt); n   } n   sort(d.begin(), d.end()); n   cin >> g >> r; n   vector<vector<bool>> vis(m, vector<bool>(g + 1)); n   vector<vector<int>> dis(m, vector<int>(g + 1, 0)); n   deque<tuple<int, int>> dq; n   dq.push_back(make_tuple(0, 0)); n   long long ans = 1e9; n   vis[0][0] = 1; n   while (!dq.empty()) { n     int i, t; n     tie(i, t) = dq.front(); n     dq.pop_front(); n     if (t == 0) { n       int ttn = n - d[i]; n       if (ttn <= g) { n         ans = min(ans, dis[i][t] * (r + g) + ttn); n       } n     } n     if (t == g) { n       if (!vis[i][0]) { n         dq.push_back(make_tuple(i, 0)); n         dis[i][0] = dis[i][t] + 1; n         vis[i][0] = 1; n       } n       continue; n     } n     if (i) { n       int rs = t + d[i] - d[i - 1]; n       if (rs <= g && !vis[i - 1][rs]) { n         dis[i - 1][rs] = dis[i][t]; n         dq.push_front(make_tuple(i - 1, rs)); n         vis[i - 1][rs] = 1; n       } n     } n     if (i < m - 1) { n       int rs = t + d[i + 1] - d[i]; n       if (rs <= g && !vis[i + 1][rs]) { n         dis[i + 1][rs] = dis[i][t]; n         dq.push_front(make_tuple(i + 1, rs)); n         vis[i + 1][rs] = 1; n       } n     } n   } n   if (ans == 1e9) { n     cout << -1 <<   n ; n   } else { n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void no(int ex = -1) { cout << ex <<   n ; } n void win(bool bl) { n   if (bl) n     cout <<  YES ; n   else n     cout <<  NO ; n   cout <<   n ; n } n const int N = 2 * 100000; n const int mod = 998244353; n double T, n, m, k, p, res, pr; n bool bw; n string str; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> T; n   while (T--) { n     cin >> n; n     cin >> pr; n     bw = false; n     for (int i = 1; i < n; i++) { n       cin >> k; n       if (abs(k - pr) >= 2) { n         bw = true; n         p = i; n       } n       pr = k; n     } n     if (bw) n       cout <<  YES  n            <<   n  n            << p <<     << p + 1 <<   n ; n     else n       cout <<  NO  n            <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1000000007; n const int MM = 998244353; n const long double PI = acos(-1); n long long power(long long b, long long e, long long m) { n   if (e == 0) return 1; n   if (e & 1) return b * power(b * b % m, e / 2, m) % m; n   return power(b * b % m, e / 2, m); n } n long long power(long long b, long long e) { n   if (e == 0) return 1; n   if (e & 1) return b * power(b * b, e / 2); n   return power(b * b, e / 2); n } n template <typename T, typename U> n static inline void amin(T &x, U y) { n   if (y < x) x = y; n } n template <typename T, typename U> n static inline void amax(T &x, U y) { n   if (x < y) x = y; n } n template <typename T, typename U> n ostream &operator<<(ostream &os, const pair<T, U> &p) { n   return os <<  (  << p.first <<  ,  << p.second <<  ) ; n } n template <typename T> n struct fenwick { n   vector<T> bit; n   int n; n   void build(int n_) { n     n = n_; n     bit.resize(n + 1); n     for (int i = 1; i <= n; ++i) bit[i] = T(0); n   } n   void update(int pos, T val) { n     for (; pos <= n; pos += pos & -pos) bit[pos] = (bit[pos] + val) % M; n   } n   T get(int r) { n     T x{}; n     for (; r; r -= r & -r) x = (x + bit[r]) % M; n     return x; n   } n   T query(int l, int r) { return (get(r) - get(l - 1) + M) % M; } n }; n fenwick<long long> fenw1, fenw2; n const int N = 5e5 + 5; n int b[N]; n long long a[N]; n int _runtimeTerror_() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> a[i], b[i] = i; n   sort(b + 1, b + 1 + n, [&](int x, int y) { return a[x] < a[y]; }); n   long long ans = 0; n   fenw1.build(n); n   fenw2.build(n); n   for (int i = 1; i <= n; ++i) { n     fenw1.update(b[i], b[i]); n     fenw2.update(b[i], n - b[i] + 1); n     long long u = 1LL * (fenw1.get(b[i])) * (n - b[i] + 1) % M; n     u += 1LL * b[i] * (fenw2.query(b[i] + 1, n)) % M; n     u %= M; n     ans += a[b[i]] * 1LL * u % M; n     ans %= M; n   } n   cout << ans <<   n ; n   return 0; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int TESTS = 1; n   while (TESTS--) _runtimeTerror_(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const int M = 1e9 + 7; n const int INF = 2e9 + 5; n const int rx[8] = {-1, 0, 1, 0, 1, 1, -1, -1}; n const int ry[8] = {0, -1, 0, 1, 1, -1, 1, -1}; n mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count()); n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   vector<pair<long long, long long> > a(n); n   long long sumx = 0; n   long long sumy = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i].first >> a[i].second; n     sumx += a[i].first; n     sumy += a[i].second; n   } n   long long res = 0; n   for (int i = 0; i < n; i++) { n     res += (n - 1) * a[i].first * a[i].first - a[i].first * (sumx - a[i].first); n     res += (n - 1) * a[i].second * a[i].second - n            a[i].second * (sumy - a[i].second); n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int>> g; n vector<int> a; n int n; n vector<map<int, int>> dp; n int ans = 0; n int gcd(int a, int b) { n   while (b) { n     a %= b; n     swap(a, b); n   } n   return a; n } n void dfs(int v, int p = -1) { n   for (auto to : g[v]) { n     if (to != p) { n       dfs(to, v); n       if (gcd(a[v], a[to]) != 1) { n         for (auto i : dp[to]) { n           int y = gcd(a[v], i.first); n           if (y != 1) { n             for (auto c : dp[v]) n               if (gcd(c.first, i.first) != 1) n                 ans = max(ans, i.second + c.second); n           } n         } n         for (auto i : dp[to]) { n           int y = gcd(a[v], i.first); n           if (y != 1) { n             dp[v][y] = max(dp[v][y], i.second + 1); n             ans = max(ans, i.second + 1); n           } n         } n       } n     } n   } n } n signed main() { n   std::ios_base::sync_with_stdio(0), cin.tie(0); n   cin >> n; n   g.resize(n); n   a.resize(n); n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   dp.resize(n); n   for (int i = 0; i < n - 1; ++i) { n     int x, y; n     cin >> x >> y; n     x--, y--; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   for (int i = 0; i < n; ++i) { n     if (a[i] != 1) { n       ans = 1; n       dp[i][a[i]] = 1; n     } n   } n   if (!ans) { n     cout << 0; n     return 0; n   } n   dfs(0); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000010; n const int md = 1e9 + 7; n int mul(int a, int b) { return (long long)a * b % md; } n char s[N]; n int n, m, nn, x[N], z[N]; n bool start[N]; n int main() { n   scanf( %d %d %s , &n, &m, s); n   nn = strlen(s); n   for (int i = 0; i < m; i++) { n     scanf( %d , &x[i]); n     x[i]--; n     start[x[i]] = true; n   } n   for (int i = 1, l = -1, r = -1; i < nn; i++) { n     int &k = z[i]; n     if (i < r) { n       k = min(r - i, z[i - l]); n     } n     for (; i + k < nn && s[k] == s[i + k]; k++) n       ; n     if (i + k > r) { n       l = i; n       r = i + k; n     } n   } n   for (int i = 0; i < m - 1; i++) { n     int p = x[i + 1] - x[i]; n     if (p + z[p] < nn) { n       puts( 0 ); n       return 0; n     } n   } n   int ans = 1; n   int r = -1; n   for (int i = 0; i < n; i++) { n     if (start[i]) { n       r = i + nn; n     } n     if (i >= r) { n       ans = mul(ans, 26); n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int s; n   cin >> s; n   long long int tot = 0; n   while (s >= 10) { n     tot += s / 10 * 10; n     s = s / 10 + s % 10; n   } n   cout << tot + s << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 998244353; n const long long EPS = 1e-9; n long long mpow(long long a, long long b, long long p = MOD) { n   a = a % p; n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = (res * a) % p; n     a = (a * a) % p; n     b = b >> 1; n   } n   return res % p; n } n const long long N = 1e6 + 5; n long long dp[N]; n long long fac[N]; n long long ncm(long long n, long long m) { n   if (m > n) return 0; n   long long ans = fac[n]; n   ans = (ans * mpow(fac[m], MOD - 2)) % MOD; n   ans = (ans * mpow(fac[n - m], MOD - 2)) % MOD; n   return ans; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   fac[0] = 1; n   for (long long i = 1; i < N; i++) { n     fac[i] = (i * fac[i - 1]) % MOD; n   } 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   dp[n] = 1; n   long long ans = 0; n   for (long long i = n - 1; i >= 0; i--) { n     dp[i] = dp[i + 1]; n     if (a[i] <= 0) continue; n     for (long long j = i + 1; j < n; j++) { n       if (j - i < a[i]) continue; n       long long rem = a[i] - 1; n       long long oo = j - i - 1; n       long long c = ncm(oo, rem) * dp[j + 1]; n       ans += c; n       ans %= MOD; n       dp[i] += c; n       dp[i] %= MOD; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool ckmin(T& a, const T& b) { n   return b < a ? a = b, 1 : 0; n } n template <class T> n bool ckmax(T& a, const T& b) { n   return a < b ? a = b, 1 : 0; n } n const long long MOD = (long long)(1e9) + 7; n long long getVal(pair<pair<int, int>, long long> p) { n   return (p.second * (p.first.second - p.first.first + 1)) % MOD; n } n void pr(stack<pair<pair<int, int>, long long>> st) { n   cout << st.top().first.first <<     << st.top().first.second <<     n        << st.top().second <<   n ; n   st.pop(); n   if (!st.empty()) pr(st); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   string second; n   cin >> second; n   vector<int> v; n   int cur = 0; n   for (int i = 0; i < (int)second.size(); i++) { n     if (second[i] ==  1 ) { n       v.push_back(cur); n       cur = 0; n     } else { n       cur++; n     } n   } n   if ((int)v.size() == 0) { n     return cout << (int)second.size() <<   n , 0; n     ; n   } n   long long factor = ((long long)(v[0] + 1) * (cur + 1)) % MOD; n   v.erase(v.begin()); n   stack<pair<pair<int, int>, long long>> st; n   long long cursum = 0; n   for (int i = 0; i < (int)v.size(); i++) { n     long long ori = cursum; n     while (!st.empty()) { n       if (v[i] >= st.top().first.first) { n         cursum -= getVal(st.top()); n         cursum %= MOD; n         cursum += MOD; n         cursum %= MOD; n         if (v[i] < st.top().first.second) { n           st.top().first.first = v[i] + 1; n           cursum += getVal(st.top()); n           cursum %= MOD; n           break; n         } else { n           st.pop(); n         } n       } else { n         break; n       } n     } n     st.push(make_pair(make_pair(0, v[i]), (ori + 1) % MOD)); n     cursum += getVal(st.top()); n     cursum %= MOD; n   } n   cursum++; n   cursum %= MOD; n   long long res = (factor * cursum) % MOD; n   return cout << res <<   n , 0; n   ; n } n


#include <bits/stdc++.h> n using namespace std; n long long M = 1e15 + 37; n const long long N = 1000 + 10; n const long long IM = 1e18; n const long long PI = 3.14159265; n void PV(vector<long long> v) { n   for (long long i = 0; i < v.size(); i++) cout << v[i] <<    ; n   cout <<   n ; n } n void PA(long long v[], long long n, long long x = 0) { n   for (long long i = x; i < n + x; i++) cout << v[i] <<    ; n   cout <<   n ; n } n void IN(long long a[], long long n, long long x = 0) { n   for (long long i = x; i < n + x; i++) cin >> a[i]; n } n inline void PTN(long long n = 1, long long a = 0, long long b = 0, n                 long long c = 0) { n   if (n--) cout << a <<    ; n   if (n--) cout << b <<    ; n   if (n--) cout << c <<    ; n   cout <<   n ; n } n inline void op() {} n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   op(); n   long long i, j, x, y, u, l; n   long long tt = 1; n   { n     i = j = x = y = u = l = 0; n     string s; n     cin >> s; n     l = s.size(); n     for (i = 0; i < l; i++) { n       char c; n       if (i && s[i] == s[i - 1]) { n         for (c =  a ; c <=  z ; c++) { n           if (c != s[i]) { n             if (i < l) { n               if (c != s[i + 1]) { n                 s[i] = c; n                 break; n               } n             } n           } n         } n       } n     } n     cout << s <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n static inline void amn(T &x, U y) { n   if (y < x) x = y; n } n template <typename T, typename U> n static inline void amx(T &x, U y) { n   if (x < y) x = y; n } n const long long MOD = 1e9 + 7; n const long long INF = 1e18; n const long long N = 5e5 + 5; n long long top(long long x, long long y) { n   long long f = x / y; n   if (x % y) f++; n   return f; n } n bool cmp(pair<long long, long long> &a, pair<long long, long long> &b) { n   return a.first < b.first; n } n void solve() { n   long long n, m, x; n   cin >> n >> m >> x; n   vector<pair<long long, long long> > v; n   for (long long i = 1; i <= n; i++) { n     v.push_back({0, i}); n   } n   long long sa = top(n, m), sb = n / m; n   long long A = (n % m) * sa; n   for (long long i = 1; i <= x; i++) { n     for (long long j = 0; j < A; j++) { n       v[j].first++; n     } n     for (long long j = 0; j < A; j += sa) { n       cout << sa; n       for (long long k = j; k < j + sa; k++) { n         cout <<     << v[k].second; n       } n       cout << endl; n     } n     for (long long j = A; j < n; j += sb) { n       cout << sb; n       for (long long k = j; k < j + sb; k++) { n         cout <<     << v[k].second; n       } n       cout << endl; n     } n     sort(v.begin(), v.end(), cmp); n   } n   cout << endl; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long test_case = 1; n   cin >> test_case; n   while (test_case--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int binarySearch(int a[], int l, int r, int x); n int main() { n   int n, m, i, temp; n   vector<int> v1; n   vector<int> v2; n   scanf( %d %d , &n, &m); n   for (i = 0; i < n; i++) { n     scanf( %d , &temp); n     v1.push_back(temp); n   } n   for (i = 0; i < m; i++) { n     scanf( %d , &temp); n     v2.push_back(temp); n   } n   for (i = 0; i < n; i++) { n     if (find(v2.begin(), v2.end(), v1[i]) != v2.end()) { n       printf( %d  , v1[i]); n     } n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 2e4 + 3; n const long long MOD = 998244353; n int a[MAX]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, k; n   cin >> n >> k; n   int s = 2 * n, mid = n, one = 0, left = 0; n   for (int i = 1; i <= k; i++) { n     cin >> a[i]; n     if (a[i] % 2) { n       a[i]--; n       left++; n     } n   } n   for (int i = 1; i <= k; i++) { n     if (a[i] >= 4) { n       int need = a[i] / 4; n       if (mid <= need) { n         a[i] -= mid * 4; n         mid = 0; n       } else { n         a[i] %= 4; n         mid -= need; n       } n     } n   } n   if (mid == 0) { n     for (int i = 1; i <= k; i++) s -= a[i] / 2; n     s -= left; n     if (s >= 0) n       cout <<  YES ; n     else n       cout <<  NO ; n     return 0; n   } n   for (int i = 1; i <= k; i++) { n     if (a[i] == 2) { n       if (s) { n         s--; n         continue; n       } n       if (mid) { n         mid--; n         left--; n         continue; n       } n       left += 2; n     } n   } n   if (s + mid * 2 >= left) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a[200005]; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   int s, t, m = 0; n   for (s = 0; s < n;) { n     t = s + 1; n     while (a[t] <= a[t - 1] * 2 && t < n) t++; n     int res = t - s; n     m = max(m, res); n     s = t; n   } n   printf( %d , m); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cmp(string s1, string s2) { n   if (s1 ==  rock  && s2 ==  scissors ) return 1; n   if (s1 ==  scissors  && s2 ==  paper ) return 1; n   if (s1 ==  paper  && s2 ==  rock ) return 1; n   return 0; n } n int main() { n   string s1, s2, s3; n   cin >> s1 >> s2 >> s3; n   if (cmp(s1, s2) && cmp(s1, s3)) { n     putchar( F ); n     return 0; n   } n   if (cmp(s2, s1) && cmp(s2, s3)) { n     putchar( M ); n     return 0; n   } n   if (cmp(s3, s1) && cmp(s3, s2)) { n     putchar( S ); n     return 0; n   } n   putchar( ? ); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 7; n const long long mod = 1e9 + 7; n long long mpow(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b % 2 == 1) { n       res = res * a; n     } n     b = b / 2; n     a = a * a; n   } n   return res; n } n long long pow1(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b % 2 == 1) res = res * a % mod; n     b = b / 2; n     a = a * a % mod; n   } n   return res; n } n int main() { n   long long k; n   cin >> k; n   long long b = 1; n   b = mpow(2LL, k - 1) - 1 + mpow(2, k) - 1 - (mpow(2, k - 1)); n   long long ans = pow1(4, b); n   ans = ans * 6 % mod; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n long long ToInt(const T &x) { n   stringstream s; n   s << x; n   long long r; n   s >> r; n   return r; n } n template <class T> n string ToString(const T &x) { n   stringstream s; n   s << x; n   return s.str(); n } n int main() { n   string str, str2; n   cin >> str; n   if (str.length() == 1) return cout << 1 << endl, 0; n   long long N = str[0] -  0 ; n   N++; n   for (int i = 1; i < str.length(); i++) str2 +=  0 ; n   string temp = ToString(N); n   str2 = temp + str2; n   printf( %lld n , ToInt(str2) - ToInt(str)); n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, c, d, e, f, g, h, i, j, k; n int father[100005][20]; n vector<int> tree[100005]; n int deep[100005]; n int que[100005], num[100005]; n int K; n int an; n void soso(int x) { n   int ii, jj, kk; n   que[++que[0]] = x; n   deep[x] = deep[father[x][0]] + 1; n   for (ii = 0; ii < tree[x].size(); ii++) { n     jj = tree[x][ii]; n     if (jj == father[x][0]) continue; n     father[jj][0] = x; n     soso(jj); n   } n } n void Build_LCA() { n   int ii, jj, kk; n   for (jj = 1; jj <= 19; jj++) n     for (ii = 1; ii <= a; ii++) n       father[ii][jj] = father[father[ii][jj - 1]][jj - 1]; n } n int LCA(int x, int y) { n   int ii, jj, kk; n   if (deep[x] < deep[y]) swap(x, y); n   for (ii = 18; ii >= 0; ii--) n     if (deep[father[x][ii]] >= deep[y]) x = father[x][ii]; n   if (x == y) return x; n   for (ii = 18; ii >= 0; ii--) n     if (father[x][ii] != father[y][ii]) x = father[x][ii], y = father[y][ii]; n   return father[x][0]; n } n int Dis(int x, int y) { n   int u = LCA(x, y); n   return deep[x] + deep[y] - 2 * deep[u]; n } n int Add(set<pair<int, int> >& E, int x, int vv) { n   int R = 0; n   set<pair<int, int> >::iterator now; n   if (vv == 1) E.insert(make_pair(num[x], x)); n   now = E.find(make_pair(num[x], x)); n   set<pair<int, int> >::iterator now1 = now; n   set<pair<int, int> >::iterator pre, next; n   if (now != E.begin()) { n     pre = --now; n     R += Dis((*pre).second, x); n   } n   now = now1; n   next = ++now; n   if (next != E.end()) { n     R += Dis((*next).second, x); n   } n   now = now1; n   if (now != E.begin() && next != E.end()) n     R -= Dis((*pre).second, (*next).second); n   if (vv == -1) E.erase(now); n   return R * vv; n } n bool Check(int x) { n   int ii, jj, kk; n   int now = 0; n   set<pair<int, int> > GG; n   for (ii = 1; ii <= x - 1; ii++) now += Add(GG, ii, 1); n   for (ii = x; ii <= a; ii++) { n     now += Add(GG, ii, 1); n     set<pair<int, int> >::iterator in = GG.end(); n     in--; n     int tmp = Dis((*GG.begin()).second, (*in).second); n     if (now + tmp <= (K << 1)) return true; n     now += Add(GG, ii - x + 1, -1); n   } n   return false; n } n int main() { n   cin >> a >> K; n   K--; n   for (i = 1; i < a; i++) { n     cin >> c >> d; n     tree[c].push_back(d); n     tree[d].push_back(c); n   } n   soso(1); n   Build_LCA(); n   for (i = 1; i <= a; i++) num[que[i]] = i; n   int low = 1, high = a, mid; n   while (low <= high) { n     mid = (low + high) >> 1; n     if (Check(mid)) { n       an = max(an, mid); n       low = mid + 1; n     } else n       high = mid - 1; n   } n   cout << an << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int a[505], b[505]; n void solve() { n   long long int n; n   cin >> n; n   for (long long int i = 1; i <= n; i++) cin >> a[i]; n   for (long long int i = 1; i <= n; i++) cin >> b[i]; n   long long int x = (n + 1) / 2; n   if (n % 2 == 1 && a[x] != b[x]) { n     cout <<  No n ; n     return; n   } n   multiset<pair<long long int, long long int> > m; n   if (n % 2) { n     for (long long int i = 1; i < x; i++) m.insert({a[x + i], a[x - i]}); n     for (long long int i = 1; i < x; i++) { n       pair<long long int, long long int> p = {b[x + i], b[x - i]}; n       if (m.find(p) != m.end()) { n         m.erase(m.find(p)); n         continue; n       } n       swap(p.first, p.second); n       if (m.find(p) != m.end()) { n         m.erase(m.find(p)); n         continue; n       } n       cout <<  No n ; n       return; n     } n   } else { n     for (long long int i = 0; i < x; i++) m.insert({a[x - i], a[x + i + 1]}); n     for (long long int i = 0; i < x; i++) { n       pair<long long int, long long int> p = {b[x - i], b[x + i + 1]}; n       if (m.find(p) != m.end()) { n         m.erase(m.find(p)); n         continue; n       } n       swap(p.first, p.second); n       if (m.find(p) != m.end()) { n         m.erase(m.find(p)); n         continue; n       } n       cout <<  No n ; n       return; n     } n   } n   cout <<  Yes n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int qc; n   cin >> qc; n   for (long long int i = 1; i <= qc; i++) solve(); n } n


#include <bits/stdc++.h> n #define ll long long n #define f(n) for(ll i=0;i<n;i++) n #define mod 998244353 n #define vl vector<ll> n #define pb push_back n #define mk make_pair n #define forn(i, n) for (int i = 0; i < int(n); i++) n #define nl   n  n #define IOS ios_base::sync_with_stdio(false); n   n #include <iostream> n using namespace std; n   n  n void solve() n { n         ll n; n         cin>>n; n         ll a[n]; n         for(ll i=0;i<n;i++) n         { n             cin>>a[i]; n         } n         sort(a,a+n); n         ll v=0,s=0; n   n         for(int i=2;i<n;i++) n         { n             s+=a[i-2]; n             v-=(a[i]*(i-1)); n             v+=s; n         } n         cout<<v<<endl; n } n   n  n void init_code() n { n     #ifndef ONLINE_JUDGE n     freopen( input.txt ,  r , stdin); n     freopen( output.txt ,  w , stdout); n     #endif  n } n   n   n int main() n { n     init_code(); n    IOS n    cin.tie(NULL); n    cout.tie(NULL); n    ll t; n    cin>>t; n    //t=1; n    while(t--) n    { n       solve(); n    } n    return 0; n }


#include <bits/stdc++.h> n using namespace std; n  n using ll = long long int; n typedef vector<ll> vi; n typedef vector<vi> vvi; n typedef pair<ll,ll> ii; n typedef vector<ii> vii; n typedef vector<vii> vvii; n #define F first n #define S second n  n ll min(ll x, ll y){ n     return ((x < y) ? x : y); n } n  n ll max(ll x, ll y){ n     return ((x > y) ? x : y); n } n const ll INF = 1e18; n  n double findavg (ll x, ll itr){ n     ll sum = 0; n     for (ll i = 0; itr > i; i++){ n         ll mvs = 0; n         ll cur = 0; n         while (x > cur){ n             ll rd = rand()%2; n             if (rd){ n                 cur++; n             }else cur = 0; n             mvs++; n         } n         cout << mvs << endl; n         sum += mvs; n     } n     double ans = (double)sum/(double)itr; n     cout <<  x:   << x <<     <<  itr:   << itr << endl; n     cout << ans << endl; n     return ans; n } n  n int main(){ n     ll t; cin >> t; n     while (t--){ n         ll k; cin >> k; n         if (k%2 == 1){ n             cout <<  -1 n ; n             continue; n         } n         vi v; n         for (ll i = 1; 63 > i; i++){ n             ll bm = 1; n             bm <<= i; n             if (k & bm){ n                 v.push_back(1); n                 for (ll j = 0; i > j+1; j++){ n                     if (j) v.push_back(0); n                     else v.push_back(1); n                 } n             } n  n         } n         cout << v.size() << endl; n         for (auto i: v) cout << i <<    ; n         cout << endl; n     } n  n } n  n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, a, b, c; n   cin >> n >> a >> b >> c; n   long long x = n % 4; n   long long y = 4 - x; n   if (n % 4 == 0) n     cout << 0 <<   n ; n   else { n     if (y == 1) { n       cout << min(min(a, b + c), 3 * c) <<   n ; n     } else if (y == 2) { n       cout << min(min(2 * a, 2 * c), b) <<   n ; n     } else if (y == 3) { n       cout << min(min(c, 3 * a), b + a) <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct debugger { n   template <typename T> n   debugger& operator,(const T& v) { n     cerr << v <<    ; n     return *this; n   } n } dbg; n inline long long gcd(long long a, long long b) { n   a = ((a) < 0 ? -(a) : (a)); n   b = ((b) < 0 ? -(b) : (b)); n   while (b) { n     a = a % b; n     swap(a, b); n   } n   return a; n } n long long ext_gcd(long long A, long long B, long long* X, long long* Y) { n   long long x2, y2, x1, y1, x, y, r2, r1, q, r; n   x2 = 1; n   y2 = 0; n   x1 = 0; n   y1 = 1; n   for (r2 = A, r1 = B; r1 != 0; n        r2 = r1, r1 = r, x2 = x1, y2 = y1, x1 = x, y1 = y) { n     q = r2 / r1; n     r = r2 % r1; n     x = x2 - (q * x1); n     y = y2 - (q * y1); n   } n   *X = x2; n   *Y = y2; n   return r2; n } n inline long long modInv(long long a, long long m) { n   long long x, y; n   ext_gcd(a, m, &x, &y); n   x %= m; n   if (x < 0) x += m; n   return x; n } n inline long long bigmod(long long a, long long p, long long m) { n   long long res = 1 % m, x = a % m; n   while (p) { n     if (p & 1) res = (res * x) % m; n     x = (x * x) % m; n     p >>= 1; n   } n   return res; n } n int dir4[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; n const long long inf = 100000000000000LL; n const long long mod = 1000000007; n const double pi = 2 * acos(0.0); n const double eps = 1e-11; n const int Size = 300505; n int N; n int Map[105]; n int A[105]; n vector<int> POS[105]; n vector<int> G1, GX, G2; n vector<int> mset1, mset2; n char res[105]; n bool solve() { n   int cnt1 = 0, cntx = 0; n   for (long long s = (1); s <= (100); ++s) { n     if (Map[s] == 1) { n       cnt1++; n       G1.push_back(s); n     } else if (Map[s] == 2) { n       G2.push_back(s); n     } else if (Map[s] > 2) { n       cntx++; n       GX.push_back(s); n     } n   } n   for (long long i = (0); i <= (cnt1 / 2 - 1); ++i) { n     int v = G1.back(); n     mset1.push_back(POS[v][0]); n     G1.pop_back(); n   } n   for (long long i = (0); i <= (cnt1 / 2 - 1); ++i) { n     int v = G1.back(); n     mset2.push_back(POS[v][0]); n     G1.pop_back(); n   } n   if (cnt1 % 2 == 1) { n     int v = G1.back(); n     mset1.push_back(POS[v][0]); n     G1.pop_back(); n     if (cntx == 0) return false; n     int xx = GX.back(); n     GX.pop_back(); n     mset2.push_back(POS[xx][0]); n     for (long long i = (1); i <= (((long long)(POS[xx]).size()) - 1); ++i) { n       mset1.push_back(POS[xx][i]); n     } n   } n   for (long long i = (0); i <= (((long long)(G2).size()) - 1); ++i) { n     int v = G2[i]; n     for (long long j = (0); j <= (((long long)(POS[v]).size()) - 1); ++j) { n       mset1.push_back(POS[v][j]); n     } n   } n   for (long long i = (0); i <= (((long long)(GX).size()) - 1); ++i) { n     int v = GX[i]; n     for (long long j = (0); j <= (((long long)(POS[v]).size()) - 1); ++j) { n       mset1.push_back(POS[v][j]); n     } n   } n   for (long long i = (0); i <= (((long long)(mset1).size()) - 1); ++i) { n     int p = mset1[i]; n     res[p] =  A ; n   } n   for (long long i = (0); i <= (((long long)(mset2).size()) - 1); ++i) { n     int p = mset2[i]; n     res[p] =  B ; n   } n   if (((long long)(mset1).size()) + ((long long)(mset2).size()) != N) n     return false; n   res[N] =   0 ; n   printf( YES n ); n   printf( %s n , res); n   return true; n } n int main() { n   scanf( %d , &N); n   for (long long i = (0); i <= (N - 1); ++i) { n     scanf( %d , &A[i]); n     Map[A[i]]++; n     POS[A[i]].push_back(i); n   } n   if (!solve()) { n     printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1000000007; n long long fin(long long a, long long b) { n   long long ans = 1; n   a %= mod; n   while (b) { n     if (b % 2 == 1) { n       ans = (ans * a) % mod; n     } n     a = (a * a) % mod; n     b = b / 2; n   } n   return ans; n } n int main() { n   long long x, k; n   cin >> x >> k; n   if (x == 0) n     cout <<  0 n ; n   else { n     x %= mod; n     cout << (((2 * x - 1) * fin(2, k) + mod) % mod + 1 + mod) % mod <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(){ n  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n     //freopen( input.txt , r ,stdin); n     //freopen( output.txt , w ,stdout); n  int T; n  cin>>T; n  for(int t=0;t<T;t++){ n   int n,k; n   cin>>n>>k; n   if(n==1&&k==1){cout<<0<<endl;continue;} n   if(k<=3){ n    if(k==2){ n     int m=n-1; n     cout<<m<<endl; n     cout<<1<<   ; n     for(int i=3;i<=n;i++) n      cout<<i<<   ; n     cout<<endl; n   }else{ n    if(k==1){ n     cout<<n-1<<endl; n     for(int i=2;i<=n;i++) n       cout<<i<<   ; n      cout<<endl; n    } n    else{ n     cout<<n-2<<endl; n     cout<<1<<   ; n     for(int i=4;i<=n;i++) n       cout<<i<<   ; n      cout<<endl; n      n    } n   } n   }else{ n    if(k%2==0){ n     int m=n-k/2; n     cout<<m<<endl; n     for(int i=k/2;i<=n;i++) n      if(i!=k)cout<<i<<   ; n     cout<<endl; n    }else{ n     int m=n-k/2-1; n     cout<<m<<endl; n     for(int i=k/2+1;i<=n;i++) n      if(i!=k)cout<<i<<   ; n     cout<<endl; n    } n   } n  } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, A; n long long b[200005]; n long long beki[200005]; n long long pow(long long a, long long n) { n   if (n == 0) return 1; n   if (n % 2) { n     return ((a % 998244353) * (pow(a, n - 1) % 998244353)) % 998244353; n   } else { n     return pow((a * a) % 998244353, n / 2) % 998244353; n   } n } n int main(void) { n   cin >> n >> m >> A; n   for (int i = 1; i <= m; i++) cin >> b[i]; n   long long inv2 = pow(2, 998244353 - 2); n   long long ans = 1; n   for (int i = 1; i <= m; i++) { n     long long l = b[i] - b[i - 1]; n     long long tmp = (pow(A, 2 * l) + 998244353 - pow(A, l)) % 998244353; n     tmp *= inv2, tmp %= 998244353; n     tmp += pow(A, l), tmp %= 998244353; n     ans *= tmp, ans %= 998244353; n   } n   ans *= pow(A, n - 2 * b[m]), ans %= 998244353; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void init_code() {} n long long int c[207]; n bool adj[207][207]; n long long int n; n void dfs(long long int u, long long int cu) { n   if (~c[u]) { n     if (c[u] ^ cu) { n       cout <<  NO  n            <<   n ; n       exit(0); n     } n     return; n   } n   c[u] = cu; n   for (long long int v = 0; v < n; v++) n     if (adj[u][v]) dfs(v, cu ^ 1); n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   init_code(); n   cin >> n; n   string str; n   cin >> str; n   for (long long int i = 0; i < n; i++) { n     for (long long int j = i + 1; j < n; j++) { n       if (str[j] < str[i]) adj[i][j] = adj[j][i] = 1; n     } n   } n   memset(c, -1, sizeof(c)); n   for (long long int i = 0; i < n; i++) { n     if (c[i] < 0) dfs(i, 0); n   } n   cout <<  YES  n        <<   n ; n   for (long long int i = 0; i < n; i++) cout << c[i]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A> n using VE = vector<A>; n template <typename A> n using USET = unordered_set<A>; n template <typename A> n using HEAP = priority_queue<A>; n template <typename A, typename B> n using PA = pair<A, B>; n template <typename A, typename B> n using UMAP = unordered_map<A, B>; n template <typename A> n using RHEAP = priority_queue<A, vector<A>, greater<A> >; n template <typename A> n A MAX(const A &a) { n   return a; n } n template <typename A> n A MIN(const A &a) { n   return a; n } n template <typename A> n A MAX(const A *a, const A *b) { n   return *max_element(a, b); n } n template <typename A> n A MIN(const A *a, const A *b) { n   return *min_element(a, b); n } n template <typename A, typename... B> n A MAX(const A &a, const B &...b) { n   return max(a, MAX(b...)); n } n template <typename A, typename... B> n A MIN(const A &a, const B &...b) { n   return min(a, MIN(b...)); n } n template <typename A, typename B = typename std::iterator_traits<A>::value_type> n B MAX(A a, A b) { n   return *max_element(a, b); n } n template <typename A, typename B = typename std::iterator_traits<A>::value_type> n B MIN(A a, A b) { n   return *min_element(a, b); n } n mt19937 rng( n     (unsigned int)chrono::steady_clock::now().time_since_epoch().count()); n const int N = 100010, M = 1010; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout << fixed << setprecision(10); n   int T; n   cin >> T; n   while (T--) { n     int n; n     cin >> n; n     int idx = 0; n     map<int, int> mp; n     vector<int> a(n); n     for (auto &u : a) { n       cin >> u; n       if (!mp.count(u)) mp[u] = ++idx; n       u = mp[u]; n     } n     int p = -1; n     for (int i = 0; i < n; i++) n       if (a[i] == a[(i + 1) % n]) { n         p = i; n         break; n       } n     vector<int> c(n); n     if (p == -1) { n       for (int i = 0, j = 1; i + 1 < n; i += 2) { n         c[i] = j; n         j = 3 - j; n         c[i + 1] = j; n         j = 3 - j; n       } n       if (c.back() == 0) { n         if (c[n - 2] != c[0]) n           c.back() = 3; n         else n           c.back() = 3 - c[0]; n       } n     } else { n       rotate(a.begin(), a.begin() + p + 1, a.end()); n       c[0] = 1; n       for (int i = 1; i < n; i++) n         if (a[i] != a[i - 1]) n           c[i] = 3 - c[i - 1]; n         else n           c[i] = 1; n     } n     cout << MAX(c.begin(), c.end()) <<   n ; n     int j; n     if (p == -1) n       j = 0; n     else n       j = n - p - 1; n     for (int i = 0; i < n; i++, j++) cout << c[j % n] <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[27], c[27], d[27]; n int main() { n   int count, i, n, max, co, t, index, k; n   char str[1010], ch, b[1010]; n   ch = getchar(); n   while (ch !=   n ) { n     a[ch -  a  + 1]++; n     ch = getchar(); n   } n   count = 0; n   for (i = 1; i <= 26; i++) n     if (a[i] != 0) count++; n   scanf( %d , &n); n   if (n < count) n     printf( -1 n ); n   else if (n == count) { n     max = 0; n     for (i = 1; i <= 26; i++) n       if (a[i] > max) max = a[i]; n     printf( %d n , max); n     for (i = 1; i <= 26; i++) n       if (a[i] != 0) printf( %c , i +  a  - 1); n     printf(  n ); n   } else { n     t = 0; n     for (i = 1; i <= 26; i++) { n       if (a[i] != 0) b[t++] = i +  a  - 1; n       c[i] = 1; n     } n     copy(a, a + 27, d); n     for (k = n - count; k > 0; k--) { n       max = 0; n       index = 0; n       for (i = 1; i <= 26; i++) n         if (d[i] > max) { n           max = d[i]; n           index = i; n         } n       c[index]++; n       b[t++] = index +  a  - 1; n       if (a[index] % c[index] != 0) n         d[index] = a[index] / c[index] + 1; n       else n         d[index] = a[index] / c[index]; n     } n     max = 0; n     for (i = 1; i <= 26; i++) { n       if (d[i] > max) max = d[i]; n     } n     printf( %d n , max); n     b[t] = 0; n     puts(b); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 2e5 + 5, mod = 1e9 + 7, inf = 1e18; n long long n, m, ans[maxn], h[maxn], par[20][maxn], mx[20][maxn], pw2[20], n     p[maxn], sz[maxn], ip[maxn]; n vector<pair<long long, long long>> v1[maxn], v2[maxn]; n vector<pair<long long, pair<long long, long long>>> adj[maxn], g[maxn]; n vector<pair<pair<long long, long long>, pair<long long, long long>>> vec; n set<pair<long long, long long>> s[maxn]; n long long get_par(long long u) { n   if (p[u] == 0) return u; n   p[u] = get_par(p[u]); n   return p[u]; n } n bool merge(long long u, long long v) { n   u = get_par(u); n   v = get_par(v); n   if (u == v) return 0; n   if (sz[u] < sz[v]) swap(u, v); n   p[v] = u; n   sz[u] += sz[v]; n   return 1; n } n void dfs(long long u) { n   for (long long i = 1; i < 20; i++) { n     long long v = par[i - 1][u]; n     if ((v == 0) || (par[i - 1][v] == 0)) break; n     par[i][u] = par[i - 1][v]; n     mx[i][u] = max(mx[i - 1][u], mx[i - 1][v]); n   } n   for (auto j : g[u]) { n     long long v = j.first, w = j.second.first; n     if (j.first == par[0][u]) continue; n     ip[v] = j.second.second; n     mx[0][v] = w; n     par[0][v] = u; n     h[v] = h[u] + 1ll; n     dfs(v); n   } n } n void build_mst() { n   sort(vec.begin(), vec.end()); n   for (auto i : vec) { n     long long u = i.second.first, v = i.second.second, w = i.first.first; n     if (merge(u, v)) { n       g[u].push_back({v, {w, i.first.second}}); n       g[v].push_back({u, {w, i.first.second}}); n     } n   } n   dfs(1); n } n long long get_pr(long long v, long long d) { n   for (long long i = 0; i < 20; i++) { n     if ((pw2[i] & d) == 0) continue; n     v = par[i][v]; n   } n   return v; n } n long long get_mx(long long v, long long d) { n   long long MX = 0; n   for (long long i = 0; i < 20; i++) { n     if ((pw2[i] & d) == 0) continue; n     MX = max(MX, mx[i][v]); n     v = par[i][v]; n   } n   return MX; n } n long long get_lca(long long u, long long v) { n   if (h[u] > h[v]) swap(u, v); n   if (h[u] != h[v]) v = get_pr(v, h[v] - h[u]); n   if (u == v) return u; n   for (long long i = 19; i >= 0; i--) { n     if (par[i][v] == par[i][u]) continue; n     u = par[i][u]; n     v = par[i][v]; n   } n   return par[0][u]; n } n void DFS(long long u) { n   for (pair<long long, long long> i : v1[u]) s[u].insert(i); n   for (auto j : g[u]) { n     if (j.first == par[0][u]) continue; n     DFS(j.first); n     if (s[j.first].size() == 0) continue; n     if (s[j.first].size() > s[u].size()) s[u].swap(s[j.first]); n     for (auto j2 : s[j.first]) s[u].insert(j2); n     s[j.first].clear(); n   } n   for (pair<long long, long long> i : v2[u]) s[u].erase(i); n   if (u == 1) return; n   if (s[u].size() == 0) { n     ans[ip[u]] = -1; n     return; n   } n   ans[ip[u]] = (*s[u].begin()).first - 1ll; n } n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   pw2[0] = 1; n   for (long long i = 1; i < 20; i++) pw2[i] = (pw2[i - 1] * 2ll); n   cin >> n >> m; n   for (long long i = 0; i < m; i++) { n     long long u, v, w; n     cin >> u >> v >> w; n     adj[u].push_back({v, {w, i}}); n     adj[v].push_back({u, {w, i}}); n     vec.push_back({{w, i}, {u, v}}); n   } n   build_mst(); n   for (auto j : vec) { n     long long u = j.second.first, v = j.second.second, w = j.first.first, n               i = j.first.second; n     if (h[u] > h[v]) swap(u, v); n     if (par[0][v] == u) continue; n     long long lca = get_lca(u, v); n     v1[u].push_back({w, v}); n     v1[v].push_back({w, u}); n     v2[lca].push_back({w, v}); n     v2[lca].push_back({w, u}); n     ans[i] = max(get_mx(u, h[u] - h[lca]), get_mx(v, h[v] - h[lca])) - 1; n   } n   DFS(1); n   for (long long i = 0; i < m; i++) cout << ans[i] <<    ; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { cout <<  INTERCAL  << endl; } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   set<int> s; n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     s.insert(x); n   } n   set<int>::iterator it, it1, it2; n   it = s.begin(); n   it1 = it; n   it1++; n   it2 = s.end(); n   it2--; n   if (s.size() == 1) n     cout << 0 << endl; n   else if (s.size() == 2) { n     int e = *it1 - (*it); n     if (e % 2 == 0) e /= 2; n     cout << e; n   } else if (s.size() == 3) { n     int a = (*it); n     int b = (*it1); n     int c = (*it2); n     if (b - a == c - b) n       cout << b - a << endl; n     else n       cout << -1 << endl; n   } else n     cout << -1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2000010; n int n, ans, q[N], x[N], y[N]; n struct node { n   int a, b, m, id; n }; n vector<node> a[N]; n bool cmp(node x, node y) { return x.a < y.a; } n void Work() { n   scanf( %d , &n); n   for (int i = 1, x, y, z; i <= n; i++) { n     scanf( %d%d%d , &x, &y, &z); n     a[x + y - z].push_back((node){x, y, z, i}); n     q[i] = x + y - z; n   } n   ans = 0; n   for (int i = 1; i <= n; i++) n     if (a[q[i]].size()) { n       sort(a[q[i]].begin(), a[q[i]].end(), cmp); n       for (int j = 0, px = -1e9; j < (int)a[q[i]].size(); j++) { n         node b = a[q[i]][j]; n         if (b.b < q[i] - px) px = min(b.a, q[i]), ans++; n         x[b.id] = b.a - px; n         y[b.id] = b.m - x[b.id]; n       } n       a[q[i]].clear(); n     } n   cout << ans <<   n ; n   for (int i = 1; i <= n; i++) cout << x[i] <<     << y[i] <<   n ; n } n int main() { n   int T = 1; n   scanf( %d , &T); n   while (T--) Work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int d8x[8] = {1, -1, 0, 0, 1, 1, -1, -1}; n int d8y[8] = {0, 0, 1, -1, 1, -1, 1, -1}; n const int M = 1e9 + 7; n const int N = 1e3, Nx = 500; n int a[N + 1], c[N + 1]; n bool isPrime[N + 1]; n void sieve(int x) { n   for (int i = 0; i <= x; i++) isPrime[i] = true; n   isPrime[0] = false; n   isPrime[1] = false; n   for (int i = 2; i * i <= x; i++) { n     if (isPrime[i]) { n       for (int j = i * i; j <= x; j += i) isPrime[j] = false; n     } n   } n } n template <class type> n type gcd(type a, type b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n template <class type> n type MODEXPO(type a, type b, int mod) { n   if (b == 0) n     return 1; n   else if (b % 2 == 0) n     return MODEXPO((a * a) % mod, b / 2, mod); n   else n     return (a * MODEXPO((a * a) % mod, (b - 1) / 2, mod)) % mod; n } n void fast() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n } n int main() { n   fast(); n   int n; n   cin >> n; n   int ans = 1; n   for (int i = 0; i <= n - 1; i++) cin >> a[i], a[i]--; n   for (int i = 0; i <= n - 1; i++) { n     memset(c, 0, sizeof c); n     int l = 0, x = i; n     while (!c[x]) c[x] = 1, l++, x = a[x]; n     if (x != i) { n       ans = -1; n       break; n     } n     if (l % 2 == 0) l /= 2; n     ans *= l / gcd(l, ans); n   } n   cout << ans; n   return 0; n } n