#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1 << std::endl; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n template <typename T1, typename T2> n istream& operator>>(istream& in, pair<T1, T2>& a) { n   in >> a.first >> a.second; n   return in; n } n template <typename T1, typename T2> n ostream& operator<<(ostream& out, pair<T1, T2> a) { n   out << a.first <<     << a.second; n   return out; n } n template <typename T, typename T1> n T amax(T& a, T1 b) { n   if (b > a) a = b; n   return a; n } n template <typename T, typename T1> n T amin(T& a, T1 b) { n   if (b < a) a = b; n   return a; n } n const long long INF = 1e18; n const int32_t M = 1e9 + 7; n const int32_t MM = 998244353; n const long long N = 2 * 100005; n struct node { n   long long mn, sum; n   bool lazyset; n   long long lazyval; n   void assign(long long val) { n     mn = val; n     sum = val; n   } n   void merge(node& a, node& b) { n     mn = min(a.mn, b.mn); n     sum = a.sum + b.sum; n   } n }; n struct segtree { n   node t[4 * N]; n   void pushdown(long long v, long long tl, long long tr) { n     long long tm = (tl + tr) / 2; n     if (t[v].lazyset) { n       apply(v * 2, tl, tm, t[v].lazyval); n       apply(v * 2 + 1, tm + 1, tr, t[v].lazyval); n       t[v].lazyset = 0; n     } n   } n   void apply(long long v, long long tl, long long tr, long long val) { n     t[v].mn = val; n     t[v].sum = val * (tr - tl + 1); n     t[v].lazyset = 1; n     t[v].lazyval = val; n   } n   void build(long long a[], long long v, long long tl, long long tr) { n     if (tl == tr) { n       t[v].assign(a[tl]); n     } else { n       long long tm = (tl + tr) / 2; n       build(a, v * 2, tl, tm); n       build(a, v * 2 + 1, tm + 1, tr); n       t[v].merge(t[v * 2], t[v * 2 + 1]); n     } n     t[v].lazyset = 0; n   } n   long long query(long long v, long long tl, long long tr, long long l, n                   long long r) { n     if (tl > r || tr < l) return 0; n     if (l <= tl && tr <= r) { n       return t[v].sum; n     } n     pushdown(v, tl, tr); n     long long tm = (tl + tr) / 2; n     return query(v * 2, tl, tm, l, r) + query(v * 2 + 1, tm + 1, tr, l, r); n   } n   long long sumdescent(long long v, long long tl, long long tr, long long val) { n     if (tl == tr) return t[v].sum > val ? tl : N; n     pushdown(v, tl, tr); n     long long tm = (tl + tr) / 2; n     if (t[v * 2].sum > val) return sumdescent(v * 2, tl, tm, val); n     return sumdescent(v * 2 + 1, tm + 1, tr, val - t[v * 2].sum); n   } n   long long descent(long long v, long long tl, long long tr, long long val) { n     if (t[v].mn >= val) return tr + 1; n     if (tl == tr) return tl; n     pushdown(v, tl, tr); n     long long tm = (tl + tr) / 2; n     if (t[v * 2].mn < val) return descent(v * 2, tl, tm, val); n     return descent(v * 2 + 1, tm + 1, tr, val); n   } n   void rupd(long long v, long long tl, long long tr, long long l, long long r, n             long long val) { n     if (tl > r || tr < l) return; n     if (l <= tl && tr <= r) { n       apply(v, tl, tr, val); n       return; n     } n     pushdown(v, tl, tr); n     long long tm = (tl + tr) / 2; n     rupd(v * 2, tl, tm, l, r, val); n     rupd(v * 2 + 1, tm + 1, tr, l, r, val); n     t[v].merge(t[v * 2], t[v * 2 + 1]); n   } n } st; n void solve() { n   long long n, q; n   cin >> n >> q; n   long long a[n]; n   for (long long i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   st.build(a, 1, 0, n - 1); n   for (long long i = 0; i < q; ++i) { n     long long t, x, y; n     cin >> t >> x >> y; n     if (t == 1) { n       long long l = st.descent(1, 0, n - 1, y); n       long long r = x - 1; n       if (l <= r) st.rupd(1, 0, n - 1, l, r, y); n     } else { n       long long cur = y; n       long long idx = x - 1; n       long long ans = 0; n       while (idx < n) { n         long long nxtidx = st.descent(1, 0, n - 1, cur + 1); n         amax(idx, nxtidx); n         long long sumidx = n             st.sumdescent(1, 0, n - 1, n                           cur + st.query(1, 0, n - 1, 0, idx - 1)) - n             1; n         amin(sumidx, n - 1); n         ans += sumidx - idx + 1; n         cur -= st.query(1, 0, n - 1, idx, sumidx); n         idx = sumidx + 1; n       } n       cout << ans <<   n ; n     } n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   unsigned long long k, res = 0; n   cin >> n >> k; n   unsigned long long *arr = new unsigned long long[n + 1]; n   arr[0] = 1; n   for (int i = 1; i <= n; i++) arr[i] = arr[i - 1] * 2; n   bool left = true; n   while (n > 1) { n     if (k > arr[n - 1]) { n       if (left) { n         res += arr[n]; n       } else { n         res++; n         left = true; n       } n       k -= arr[n - 1]; n     } else { n       if (left) { n         res++; n       } else { n         res += arr[n]; n       } n       left = false; n     } n     n--; n   } n   if (k == 1 && left) n     res += 2; n   else if (k == 2 && left) n     res += 3; n   else if (k == 1) n     res += 3; n   else n     res += 2; n   cout << res - 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long int n; n   cin >> n; n   multiset<long long int> s; n   for (long long int i = 0; i < n; i++) { n     long long int x; n     cin >> x; n     s.insert(x); n   } n   long long int ans = 0; n   for (long long int i = 1; i <= n; i++) { n     while (s.size()) { n       long long int x = *(s.begin()); n       if (x >= i) break; n       s.erase(s.begin()); n     } n     if (!s.size()) break; n     ans++; n     s.erase(s.begin()); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( O2 ) n const int P = 998244353; n int n, k, ans; n int dp[2][510][510]; n int cnt[510], pre[510]; n template <typename T> n inline void read(T &x) { n   T f = 1; n   x = 0; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -f; n   for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c -  0 ; n   x *= f; n } n inline int exp_mod(int a, int n) { n   int b = a, res = 1; n   while (n) { n     if (n & 1) res = 1ll * res * b % P; n     b = 1ll * b * b % P; n     n >>= 1; n   } n   return res; n } n int main() { n   read(n); n   read(k); n   if (n == 1) { n     printf( 0 n ); n     return 0; n   } n   dp[0][0][0] = 1; n   for (int i = 0; i <= n; i++) { n     int now = i & 1, nxt = now ^ 1; n     for (int j = 0; j <= n; j++) { n       for (int x = 0; x <= n; x++) { n         dp[nxt][j][x] = 0; n       } n     } n     for (int j = 0; j <= i; j++) { n       for (int x = 0; x <= n; x++) { n         dp[nxt][j + 1][max(x, j + 1)] = n             (1ll * dp[nxt][j + 1][max(x, j + 1)] + dp[now][j][x]) % P; n         dp[nxt][1][max(x, 1)] = n             (1ll * dp[nxt][1][max(x, 1)] + dp[now][j][x]) % P; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       cnt[i] = (1ll * cnt[i] + dp[n & 1][j][i]) % P; n     } n   } n   for (int i = 1; i <= n; i++) pre[i] = (pre[i - 1] + cnt[i]) % P; n   for (int i = 1; i <= n; i++) n     ans = (ans + 1ll * cnt[i] * 1ll * pre[min(n, (k - 1) / i)]) % P; n   printf( %d n , 1ll * ans * exp_mod(2, P - 2) % P); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (1 << 22) + 20; n bool vis[2][N]; n int a[N], n, m, ex[N]; n int get_not(int x) { return (1 << n) - 1 - x; } n void dfs(int type, int msk) { n   vis[type][msk] = true; n   if (type == 1) { n     if (!vis[0][get_not(msk)]) dfs(0, get_not(msk)); n     for (int i = 0; i < n; i++) n       if ((!(msk & (1 << i))) && (!vis[1][msk ^ (1 << i)])) n         dfs(1, msk ^ (1 << i)); n   } else if (!vis[1][msk] && ex[msk]) n     dfs(1, msk); n } n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n >> m; n   for (int i = 0; i < m; i++) cin >> a[i], ex[a[i]] = 1; n   int ans = 0; n   for (int i = 0; i < m; i++) { n     if (!vis[0][a[i]]) dfs(0, a[i]), ans++; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, sum = 0; n   cin >> n; n   n++; n   for (int i = 1; i < n - 2; i++) { n     for (int j = i; j < n - 1; j++) { n       int k = (int)(i ^ j); n       if (k < n && k > j) { n         if (!(i ^ j ^ k)) { n           double p = (i + j + k) / 2; n           double s = sqrt(p * (p - i) * (p - j) * (p - k)); n           if (s > 0) { n             sum++; n           } n         } n       } n     } n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mul(long long a, long long b, long long p) { n   long long s = 0; n   while (b) { n     if (b & 1) s = (s + a) % p; n     a = (a + a) % p; n     b >>= 1; n   } n   return s; n } n long long ksm(long long a, long long b, long long p) { n   long long s = 1; n   while (b) { n     if (b & 1) s = mul(s, a, p); n     a = mul(a, a, p); n     b >>= 1; n   } n   return s; n } n int main() { n   long long n; n   cin >> n; n   while (n--) { n     long long a; n     cin >> a; n     long long n = to_string(a).length(); n     for (long long m = 0;; m++) { n       long long b = ((1 << n + m) - a % (1 << n + m)) % (1 << n + m); n       if ((a + b) % 5 == 0) b += 1 << n + m; n       if (b < pow(10, m)) { n         long long x = (a + b) >> n + m, d = -1; n         for (long long i = 0; i < 5; i++) n           if ((1 << i) % 5 == x % 5) { n             d = i; n             break; n           } n         for (long long i = 2, p = 5; i <= n + m; i++) { n           long long phi = p / 5 * 4; n           p *= 5; n           for (long long k = 0; k < 5; k++) n             if (ksm(2, d + k * phi, p) == x % p) { n               d += k * phi; n               break; n             } n         } n         cout << d + n + m <<   n ; n         break; n       } n       a *= 10; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000005; n int n, m, _m, cnt, d[maxn], fa[maxn]; n bool vis[maxn]; n struct Edge { n   int x, y; n } Es[maxn]; n long long ans; n int getfa(int x) { return fa[x] == x ? x : fa[x] = getfa(fa[x]); } n void merge(int x, int y) { n   x = getfa(x); n   y = getfa(y); n   if (x != y) fa[x] = y; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) fa[i] = i; n   for (int i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     if (x != y) { n       Es[++_m].x = x, Es[_m].y = y; n       d[x]++; n       d[y]++; n       vis[x] = vis[y] = true; n       merge(x, y); n     } else n       cnt++, vis[x] = true; n   } n   int rt = 0; n   for (int i = 1; i <= n; i++) n     if (vis[i]) rt = i; n   if (!rt) return printf( 0 n ), 0; n   for (int i = 1; i <= n; i++) n     if (vis[i] && getfa(rt) != getfa(i)) return printf( 0 n ), 0; n   for (int i = 1; i <= _m; i++) ans += (d[Es[i].x] - 1) + (d[Es[i].y] - 1); n   ans = ans / 2 + (long long)_m * cnt + (long long)cnt * (cnt - 1) / 2; n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, a, b; n   cin >> n >> a >> b; n   long long ma, mi; n   ma = max(a, b); n   mi = min(a, b); n   long long seated = 0; n   for (long long i = (long long)(0); i < (long long)(n); i++) { n     char t; n     cin >> t; n     while (t ==  .  and i < n) { n       if (ma != 0) { n         ma--; n         seated++; n       } n       a = ma; n       ma = mi; n       mi = a; n       i++; n       if (i < n) cin >> t; n     } n     a = ma; n     ma = max(mi, ma); n     mi = min(mi, a); n   } n   cout << seated << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1 << 30; n int num[222222]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> num[i]; n   int sum = 1; n   int ans = 1; n   for (int i = 1; i < n; i++) { n     if (num[i] <= num[i - 1] * 2) { n       sum++; n     } else { n       ans = max(ans, sum); n       sum = 1; n     } n   } n   ans = max(ans, sum); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   double df, de; n   void init(double _df, double _de) { n     df = _df; n     de = _de; n   } n } V[100]; n int nf, ne, ns, rf, re, rs, df, de; n int n, top, place[13]; n double ans = 0, dp[21][21], lf, le, ls; n bool cmp(const point &a, const point &b) { return a.df > b.df; } n double cross(double l1, double r1, double l2, double r2) { n   if (l2 >= r1 || l1 >= r2) return 0; n   return min(r1, r2) - max(l1, l2); n } n void init_V() { n   top = 0; n   for (int i = 0; i < n; i++) n     if (place[i] < 2) { n       double t1 = lf * df, t2 = le * de; n       for (int j = 0; j < n; j++) n         if (place[j]) { n           t1 += df * place[j] * n                 cross(j - ls / 2, j + ls / 2, i - lf / 2, i + lf / 2); n           t2 += de * place[j] * n                 cross(j - ls / 2, j + ls / 2, i - le / 2, i + le / 2); n         } n       for (int j = 1; j + place[i] <= 2; j++) V[top++].init(t1, t2); n     } n   sort(V, V + top, cmp); n } n void init_dp() { n   memset(dp, 0, sizeof(dp)); n   for (int i = 0; i <= nf; i++) n     for (int j = 0; j <= ne; j++) { n       if (i) dp[i][j] = max(dp[i][j], dp[i - 1][j] + V[i + j - 1].df); n       if (j) dp[i][j] = max(dp[i][j], dp[i][j - 1] + V[i + j - 1].de); n     } n   ans = max(ans, dp[nf][ne]); n } n void dfs(int p, int left) { n   if ((n - p) * 2 < left) return; n   if (p == n) { n     init_V(); n     init_dp(); n     return; n   } n   for (int i = 0; i <= 2 && left >= i; i++) { n     place[p] = i; n     dfs(p + 1, left - i); n   } n } n int main() { n   cin >> nf >> ne >> ns >> rf >> re >> rs >> df >> de; n   n = (nf + ne + ns) / 3 * 2 + 1; n   lf = 2 * sqrt(rf * rf - 1.0); n   le = 2 * sqrt(re * re - 1.0); n   ls = 2 * sqrt(rs * rs - 1.0); n   dfs(0, ns); n   printf( %.10lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 10003; n int32_t main() { n   int n, a, b, c; n   cin >> n >> a >> b >> c; n   if (n == 1) { n     cout <<  0 ; n     return 0; n   } n   n--; n   long long ans = 0; n   ans += min(a, b); n   n--; n   if (min(a, b) > c) { n     ans += n * c; n     cout << ans; n     return 0; n   } n   ans += n * min(a, b); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<int> vec1, vec2, vec3; n   int n, sz; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     int x; n     cin >> x; n     if (x == 1) n       vec1.push_back(i); n     else if (x == 2) n       vec2.push_back(i); n     else n       vec3.push_back(i); n   } n   sz = min(min(vec1.size(), vec2.size()), vec3.size()); n   cout << sz << endl; n   for (int i = 0; i < sz; ++i) n     cout << vec1[i] <<     << vec2[i] <<     << vec3[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, arr[100000], r, arr1[100000], z; n bool boo[100000]; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> r; n     arr[i] = r; n   } n   for (int i = 0; i < n; i++) arr1[i] = arr[i]; n   reverse(arr1, arr1 + n); n   z = arr1[0]; n   boo[0] = 1; n   for (int i = 0; i < n; i++) { n     if (arr1[i] > z) boo[i] = 1; n     if (arr1[i] >= z) n       z = arr1[i]; n     else n       arr1[i] = z; n   } n   reverse(arr1, arr1 + n); n   reverse(boo, boo + n); n   for (int i = 0; i < n; i++) { n     int k = arr1[i] - arr[i]; n     if (boo[i] == 1) n       cout << 0 << endl; n     else n       cout << k + 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a[222222]; n   scanf( %d n , &n); n   pair<int, int> p; n   vector<pair<int, int> > v, v1; n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     p.first = x; n     p.second = 0; n     v.push_back(p); n   } n   reverse(v.begin(), v.end()); n   int maxi = v[0].first, pos = 0; n   ; n   for (int i = 0; i < v.size(); i++) { n     if (pos != i) { n       if (maxi >= v[i].first) v[i].second = maxi - v[i].first + 1; n       if (maxi < v[i].first) { n         maxi = v[i].first; n         pos = i; n       } n     } n   } n   reverse(v.begin(), v.end()); n   for (int i = 0; i < v.size(); i++) printf( %d  , v[i].second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int n = s.length(); n   s += s; n   int ans = -1; n   int len = 1; n   for (int i = 1; i < 2 * n; i++) { n     if (s[i] != s[i - 1]) n       len++; n     else { n       if (len <= n) ans = max(len, ans); n       len = 1; n     } n   } n   if (len <= n) ans = max(len, ans); n   if (len > n) ans = n; n   cout << ans << endl; n } n


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


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n long long dp[255][255]; n long long p[255], q[255], C[255][255]; n int N; n long long K; n void init() { n   cin >> N >> K; n   p[0] = q[0] = C[0][0] = 1; n   for (int i = 1; i <= 250; i++) { n     p[i] = p[i - 1] * K % MOD; n     q[i] = q[i - 1] * (K - 1) % MOD; n     C[i][0] = 1; n     for (int j = 1; j <= i; j++) { n       C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD; n     } n   } n } n void solve() { n   dp[0][0] = 1; n   for (int r = 1; r <= N; r++) { n     for (int c = 1; c <= N; c++) { n       for (int a = 0; a <= c; a++) { n         if (a == c) { n           dp[r][c] = (dp[r][c] + ((p[c] + MOD - q[c]) * q[N - c] % MOD * n                                   dp[r - 1][c] % MOD)) % n                      MOD; n         } else { n           dp[r][c] = (dp[r][c] + (C[N - a][c - a] * p[a] % MOD * q[N - c] % n                                   MOD * dp[r - 1][a] % MOD)) % n                      MOD; n         } n       } n     } n   } n   cout << dp[N][N] <<   n ; n } n int main() { n   init(); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1E5 + 10; n int n, tot; n int a[N], vis[N], twom[N], threem[N]; n vector<int> x, y; n stack<int> one, two, three; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   for (int i = n; i >= 1; --i) { n     if (a[i] == 1) { n       one.push(i); n     } else if (a[i] == 2) { n       if (!one.size()) { n         cout << -1 << endl; n         return 0; n       } else { n         twom[i] = one.top(); n         one.pop(); n       } n       two.push(i); n     } else if (a[i] == 3) { n       if (three.size()) { n         threem[i] = three.top(); n         three.pop(); n       } else if (two.size()) { n         threem[i] = two.top(); n       } else if (one.size()) { n         threem[i] = one.top(); n         one.pop(); n       } else { n         cout << -1 << endl; n         return 0; n       } n       three.push(i); n     } n   } n   int currow = 1; n   for (int i = 1; i <= n; ++i) { n     if (a[i] == 0 || vis[i] == 1) continue; n     int curcol = i; n     while (curcol) { n       vis[curcol] = 1; n       if (a[curcol] == 1) { n         x.push_back(currow); n         y.push_back(curcol); n         curcol = 0; n         ++currow; n       } else if (a[curcol] == 2) { n         x.push_back(currow); n         y.push_back(curcol); n         curcol = twom[curcol]; n       } else if (a[curcol] == 3) { n         x.push_back(currow); n         y.push_back(curcol); n         curcol = threem[curcol]; n         x.push_back(currow); n         y.push_back(curcol); n         ++currow; n       } n     } n   } n   cout << x.size() << endl; n   for (int i = 0; i <= (int)x.size() - 1; ++i) printf( %d %d n , x[i], y[i]); n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n vector<pair<long long, int> > v; n int main() { n   long long a, b, d; n   cin >> a; n   cin >> b; n   d = a * b / gcd(a, b); n   if (a < b) { n     for (int i = 0; i * a <= d; i++) { n       v.push_back(make_pair(i * a, 1)); n     } n     for (int i = 0; i * b <= d; i++) { n       v.push_back(make_pair(i * b, 0)); n     } n   } else { n     for (int i = 0; i * a <= d; i++) { n       v.push_back(make_pair(i * a, 0)); n     } n     for (int i = 0; i * b <= d; i++) { n       v.push_back(make_pair(i * b, 1)); n     } n   } n   sort(v.begin(), v.end()); n   long long s1, s2; n   s1 = 0; n   s2 = 0; n   for (int i = 0; i < (int)v.size() - 1; i++) { n     if (v[i + 1].second == 0) { n       s1 += v[i + 1].first - v[i].first; n     } else n       s2 += v[i + 1].first - v[i].first; n   } n   if (a > b) { n     if (s1 > s2) printf( Dasha n ); n     if (s1 < s2) printf( Masha n ); n   } else { n     if (s1 < s2) printf( Dasha n ); n     if (s1 > s2) printf( Masha n ); n   } n   if (s1 == s2) printf( Equal n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, long long> mp; n long long ar[200005]; n long long br[200005]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   t = 1; n   while (t--) { n     int n; n     cin >> n; n     for (int i = 1; i <= n; i++) { n       cin >> ar[i]; n     } n     long long prv = 1; n     mp[0] = 0; n     long long ans = 0; n     for (int i = 1; i <= n; i++) { n       br[i] = br[i - 1] + ar[i]; n       if (mp.count(br[i])) { n         prv = max(prv, mp[br[i]] + 2); n       } n       ans += i - prv + 1; n       mp[br[i]] = i; n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> a(n + 1); n   vector<int> b(n + 1); n   for (int i = 1; i <= n; i++) cin >> a[i]; n   sort(a.begin() + 1, a.end()); n   for (int i = 1; i <= n / 2; i++) b[2 * i] = a[i]; n   for (int i = n / 2 + 1; i <= n; i++) b[(i - n / 2) * 2 - 1] = a[i]; n   int ans = 0; n   for (int i = 2; i < n; i++) ans += (b[i] < b[i - 1]) * (b[i] < b[i + 1]); n   cout << ans << endl; n   for (auto i = b.begin() + 1; i != b.end(); i++) cout << *i <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   cout << (n * n) / 4 << endl; n   for (int i = 1; i <= n / 2; i++) cout << n <<     << i << endl; n   for (int i = 1; i <= n / 2; i++) n     for (int j = n / 2 + 1; j < n; j++) cout << i <<     << j << endl; n } n


#include <bits/stdc++.h> n using namespace std; n short memo[6][40][1 << 6][1 << 6][1 << 6]; n int n, m; n short roll(int row, int col, int a, int b, int c) { n   if (col == m) { n     return (n - __builtin_popcount(a)); n   } n   if (row == n) { n     return roll(0, col + 1, b, c, 0) + n            (col > 0 ? (n - __builtin_popcount(a)) : 0); n   } n   short &ans = memo[row][col][a][b][c]; n   if (~ans) { n     return ans; n   } n   ans = roll(row + 1, col, a, b | (1 << row), c); n   if (col > 0) { n     ans = max(ans, roll(row + 1, col, a | (1 << row), b, c)); n   } n   if (col < m - 1) { n     ans = max(ans, roll(row + 1, col, a, b, c | (1 << row))); n   } n   if (row > 0) { n     ans = max(ans, roll(row + 1, col, a, b | (1 << (row - 1)), c)); n   } n   if (row < n - 1) { n     ans = max(ans, roll(row + 1, col, a, b | (1 << (row + 1)), c)); n   } n   return ans; n } n int main() { n   scanf( %d %d , &n, &m); n   if (n > m) { n     swap(n, m); n   } n   memset(memo, -1, sizeof memo); n   cout << roll(0, 0, 0, 0, 0) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int modulo = 1e9 + 7; n const int max_n = 111; n const int max_k = max_n * max_n; n int bin_pow(int base, long long pw) { n   if (pw == 0) return 1; n   if (pw & 1) return (1LL * base * bin_pow(base, pw - 1)) % modulo; n   return bin_pow((1LL * base * base) % modulo, pw >> 1); n } n int a[max_n][max_k], C[max_n][max_n], d[max_n][max_n]; n int n, k; n long long m; n int main() { n   ios_base::sync_with_stdio(false); n   C[0][0] = 1; n   for (int i = 1; i < max_n; ++i) { n     C[0][i] = 0; n     C[i][0] = 1; n   } n   for (int i = 1; i < max_n; ++i) n     for (int j = 1; j < max_n; ++j) n       C[i][j] = (0LL + C[i][j] + C[i - 1][j] + C[i - 1][j - 1]) % modulo; n   cin >> n >> m >> k; n   for (int i = 1; i <= n; ++i) n     for (int j = 0; j <= n; ++j) { n       long long pw = 1 + (m - i) / n; n       d[i][j] = bin_pow(C[n][j], pw); n     } n   a[0][0] = 1; n   for (int i = 1; i <= n; ++i) n     for (int j = 0; j <= min(k, i * n); ++j) n       for (int t = 0; t <= min(j, n); ++t) n         if (a[i - 1][j - t] != 0) n           a[i][j] = (a[i][j] + 1LL * a[i - 1][j - t] * d[i][t]) % modulo; n   cout << a[n][k] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cube[8][3]; n long long len[28]; n long long dist(int a, int b) { n   long long ret = 0; n   for (int i = 0; i < 3; i++) n     ret += (long long)(cube[a][i] - cube[b][i]) * (cube[a][i] - cube[b][i]); n   return ret; n } n bool check() { n   int idx = 0; n   for (int i = 0; i < 8; i++) n     for (int j = i + 1; j < 8; j++) len[idx++] = dist(i, j); n   sort(len, len + 28); n   for (int i = 1; i < 12; i++) n     if (len[i] != len[i - 1] || len[i] == 0) return false; n   for (int i = 13; i < 24; i++) n     if (len[i] != len[i - 1]) return false; n   for (int i = 25; i < 28; i++) n     if (len[i] != len[i - 1]) return false; n   if (len[0] * 2 != len[12] || len[0] * 3 != len[24]) return false; n   return true; n } n bool go(int idx) { n   if (idx == 8) return check(); n   do { n     if (go(idx + 1)) return true; n   } while (next_permutation(cube[idx], cube[idx] + 3)); n   return false; n } n int main() { n   for (int i = 0; i < 8; i++) cin >> cube[i][0] >> cube[i][1] >> cube[i][2]; n   for (int i = 0; i < 8; i++) sort(cube[i], cube[i] + 3); n   if (go(1)) { n     cout <<  YES n ; n     for (int i = 0; i < 8; i++) n       cout << cube[i][0] <<     << cube[i][1] <<     << cube[i][2] <<   n ; n     return 0; n   } n   cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i; n   cin >> n; n   bool t[2 * n], s[2 * n]; n   char tmp; n   for (i = 0; i < 2 * n; i++) { n     cin >> tmp; n     t[i] = (tmp ==  1 ); n   } n   for (i = 0; i < 2 * n; i++) { n     cin >> tmp; n     s[i] = (tmp ==  1 ); n   } n   int t_sum = 0, s_sum = 0, and_sum = 0, t_sum1 = 0, s_sum1 = 0; n   for (i = 0; i < 2 * n; i++) { n     t_sum += int(t[i]); n     s_sum += int(s[i]); n     and_sum += int(t[i] & s[i]); n     t_sum1 += int(!t[i] & s[i]); n     s_sum1 += int(t[i] & !s[i]); n   } n   if (t_sum > s_sum) { n     if (and_sum % 2 == 0) n       t_sum -= min(t_sum - s_sum, s_sum1) / 2; n     else n       t_sum -= min(t_sum - s_sum, s_sum1) / 2 + min(t_sum - s_sum, s_sum1) % 2; n   } else if (t_sum < s_sum) { n     if (and_sum % 2 == 0) n       s_sum -= min(s_sum - t_sum, t_sum1) / 2 + min(s_sum - t_sum, t_sum1) % 2; n     else n       s_sum -= min(s_sum - t_sum, t_sum1) / 2; n   } n   t_sum -= and_sum / 2; n   s_sum -= and_sum / 2 + and_sum % 2; n   if (t_sum == s_sum) n     cout <<  Draw ; n   else if (t_sum > s_sum) n     cout <<  First ; n   else n     cout <<  Second ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 105; n int v[N], ans[N]; n int main() { n   cin.tie(0), ios_base::sync_with_stdio(false); n   int n, k, s = 0; n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> v[i], s += v[i], ans[i % k] += v[i]; n   int tans = 0; n   for (int i = 0; i < k; i++) tans = max(tans, abs(s - ans[i])); n   cout << tans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const long long int MXN = 1e18; n struct node { n   double x, y; n } p, p1, p2, p3, p4; n double ji(node a, node b, node c) { n   return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y); n } n bool jug(node a) { n   if ((ji(p1, p2, a) * ji(p3, p4, a) >= 0) && n       (ji(p2, p3, a) * ji(p4, p1, a) >= 0)) n     return true; n   return false; n } n int main() { n   int n, d, m; n   cin >> n >> d >> m; n   p1.x = 0, p1.y = d; n   p2.x = d, p2.y = 0; n   p3.x = n, p3.y = n - d; n   p4.x = n - d, p4.y = n; n   int ans = 0; n   double a, b; n   for (int i = 0; i < m; i++) { n     cin >> p.x >> p.y; n     if (jug(p)) { n       cout <<  YES  << endl; n     } else n       cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct pl { n   int num, t1, t2, h1, h2; n }; n vector<pl> h[(1 << 16) + 5]; n int n, k, su; n int s[22], sum[(1 << 16) + 5], si[(1 << 16) + 5]; n bitset<200000000> fl, ft; n int work(int a) { n   if (a % k == 0) return work(a / k); n   return a; n } n int change(int a, int b) { return a * 2001 + b; } n void find(int a, int b) { n   int c = b; n   for (int i = 0; i < h[a].size(); i++) { n     if (h[a][i].num == c) { n       b = i; n       break; n     } n   } n   if (h[a][b].t1 != -1) find(h[a][b].t1, h[a][b].h1); n   if (h[a][b].t2 != -1) find(h[a][b].t2, h[a][b].h2); n   if (h[a][b].t1 != -1) printf( %d %d n , h[a][b].h1, h[a][b].h2); n } n bool dfs(int a, int b) { n   if (si[a] < b) return 0; n   if (fl[change(a, b)] == 1) return 0; n   if (ft[change(a, b)] == 1) return 1; n   for (int i = 0; i < h[a].size(); i++) { n     if (h[a][i].num == b) { n       ft[change(a, b)] = 1; n       return 1; n     } n   } n   if (sum[a] == 1) { n     fl[change(a, b)] = 1; n     return 0; n   } n   int tmp = a; n   pl ti; n   ti.num = b; n   while (tmp != 0) { n     if (sum[tmp] * 2 <= sum[a]) { n       for (int i = b; i <= si[a]; i *= k) { n         for (int j = 1; j < i; j++) { n           if (dfs(tmp, j) && dfs((a ^ tmp), i - j)) { n             ti.t1 = tmp; n             ti.t2 = (a ^ tmp); n             ti.h1 = j; n             ti.h2 = i - j; n             h[a].push_back(ti); n             ft[change(a, b)] = 1; n             return 1; n           } n         } n       } n     } n     tmp = (a & (tmp - 1)); n   } n   fl[change(a, b)] = 1; n   return 0; n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &s[i]); n     su += s[i]; n     pl tmp; n     tmp.t1 = tmp.t2 = -1; n     tmp.num = s[i]; n     h[1 << (i - 1)].push_back(tmp); n   } n   for (int i = 1; i < (1 << n); i++) { n     int tmp = i, ti = 1; n     while (tmp != 0) { n       if (tmp & 1) si[i] += s[ti]; n       sum[i] += (tmp & 1), tmp = (tmp >> 1); n       ti++; n     } n   } n   if (k >= 50) { n     int an = change((1 << n) - 1, 1); n     for (int i = 1; i < (1 << n); i++) { n       for (int j = 0; j < h[i].size(); j++) { n         int tmp = (((1 << n) - 1) ^ i), ti = tmp; n         while (tmp != 0) { n           for (int k = 0; k < h[tmp].size(); k++) { n             int tip = change((i | tmp), work(h[i][j].num + h[tmp][k].num)); n             if (fl[tip] == 1) continue; n             fl[tip] = 1; n             pl tp; n             tp.num = work(h[i][j].num + h[tmp][k].num); n             tp.t1 = i; n             tp.t2 = tmp; n             tp.h1 = h[i][j].num; n             tp.h2 = h[tmp][k].num; n             h[i | tmp].push_back(tp); n             if (fl[an] == 1) break; n           } n           if (fl[an] == 1) break; n           tmp = (ti & (tmp - 1)); n         } n         if (fl[an] == 1) break; n       } n       if (fl[an] == 1) break; n     } n   } else n     dfs((1 << n) - 1, 1); n   for (int i = 0; i < h[(1 << n) - 1].size(); i++) { n     if (h[(1 << n) - 1][i].num == 1) { n       puts( YES ); n       find((1 << n) - 1, 1); n       return 0; n     } n   } n   puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int a, b, c; n     cin >> a >> b >> c; n     int totmake = (a + b + c) / 3; n     if (a == 0 || b == 0 || totmake == 0) { n       cout << 0 << endl; n     } else if (a <= b && a <= totmake) { n       cout << a << endl; n     } else if (b <= a && b <= totmake) { n       cout << b << endl; n     } else { n       cout << totmake << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, t; n   cin >> s >> t; n   map<char, char> m; n   bool ans = true; n   vector<pair<char, char>> v; n   int count = 0; n   for (int i = 0; i < s.size(); i++) { n     if (m.find(s[i]) != m.end()) { n       if (m[s[i]] != t[i]) { n         ans = false; n         break; n       } else { n         continue; n       } n     } else { n       if (m.find(t[i]) != m.end()) { n         ans = false; n         break; n       } else { n         m[s[i]] = t[i]; n         m[t[i]] = s[i]; n         if (s[i] != t[i]) { n           v.push_back({s[i], t[i]}); n           count++; n         } n       } n     } n   } n   if (ans) { n     cout << count << endl; n     for (int i = 0; i < count; i++) n       cout << v[i].first <<     << v[i].second << endl; n   } else n     cout <<  -1 ; n   return 0; n } n


# include <bits/stdc++.h> n using namespace std; n const int N=1e3+10; n int n,m,a[N][N],sum1[N],sum2[N],c1[N],c2[N],c3[N]; n map<int,int>mp,tt; n signed main() n { n  scanf( %d%d ,&n,&m); n  for (int i=1;i<=m;i++)  n   for (int j=1;j<=n;j++) { n    scanf( %d ,&a[i][j]); n    sum1[i]+=a[i][j]; n    sum2[i]+=a[i][j]*a[i][j]; n   }  n  for (int i=1;i<=m;i++) { n   c1[i]=sum1[i]-sum1[i-1]; n   c2[i]=sum2[i]-sum2[i-1]; n   if (i>1) mp[c1[i]]++,tt[c1[i]]=i; n  } n  int id=m; n  for (map<int,int>::iterator it=mp.begin();it!=mp.end();it++) { n   if ((it->second)==1) id=min(id,tt[it->first]); n  } n  for (int i=1;i<=m;i++) c3[i]=c2[i]-c2[i-1]; n  int res; n  if (id>=4) res=c3[3]; else res=c3[7];  n   n  if (id!=2) { n   for (int i=3;i<=m;i++) c3[i]=res; n   for (int i=1;i<=m;i++) c3[i]=c3[i-1]+c3[i]; n   for (int i=1;i<=m;i++) c3[i]=c3[i-1]+c3[i]; n  } else { n   int d=sum2[m]-sum2[m-1]-sum2[m-1]+sum2[m-2]; n   c3[m]=sum2[m]-sum2[m-1]; n   for (int i=m-1;i>=2;i--) c3[i]=c3[i+1]-d; n   for (int i=1;i<=m;i++) c3[i]=c3[i-1]+c3[i]; n  } n  if (id>=4) res=c1[3]; else res=c1[7]; n  if (id!=2) { n   for (int i=3;i<=m;i++) c1[i]=res; n   for (int i=1;i<=m;i++) c1[i]=c1[i-1]+c1[i]; n  } else { n   int d=sum1[m]-sum1[m-1]-sum1[m-1]+sum1[m-2]; n   c1[m]=sum1[m]-sum1[m-1]; n   for (int i=m-1;i>=2;i--) c1[i]=c1[i+1]-d; n   for (int i=1;i<=m;i++) c1[i]=c1[i-1]+c1[i]; n  } n  int ans1=id-1,ans2; n  for (int i=1;i<=n;i++) { n   int t=c1[id]-(sum1[id]-a[id][i]); n   if (t*t+sum2[id]-a[id][i]*a[id][i]==c3[id]) { n    ans2=t; break; n   } n  } n  printf( %d %d n ,ans1,ans2); n  fflush(stdout); n  return 0; n } n  n /* n by maystern n */


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, s1, s2; n   long long a, b, c = 0; n   cin >> s; n   b = stoi(s); n   while (b > 0) { n     s1 = s; n     sort(s1.begin(), s1.end()); n     int d = stoi(s1) % 10; n     b = b - d; n     c++; n     s = to_string(b); n   } n   cout << c <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a0, a1, a2, b0, b1, b2, n; n   cin >> a0 >> a1 >> a2; n   int cnt = (a0 + a1 + a2) / 5; n   if ((a0 + a1 + a2) % 5) cnt++; n   cin >> b0 >> b1 >> b2; n   cnt += ((b0 + b1 + b2) / 10); n   if ((b0 + b1 + b2) % 10) cnt++; n   cin >> n; n   if (cnt <= n) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long x, y; n string tt(long long a) { n   string ans =   ; n   while (a != 0) ans += (a % 10) +  0 , a /= 10; n   int p = ans.length(); n   for (int i = 0; i * 2 < p; ++i) swap(ans[i], ans[p - i - 1]); n   return ans; n } n int main() { n   cin >> x >> y; n   string s; n   while (x != 1 || y != 1) { n     if (x < y) { n       s += tt((y - 1) / x) +  B ; n       y = (y - 1) % x + 1; n     } else if (x > y) { n       s += tt((x - 1) / y) +  A ; n       x = (x - 1) % y + 1; n     } else { n       cout <<  Impossible n ; n       return 0; n     } n   } n   cout << s <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double Cnk(int n, int k) { n   double result = 1; n   for (int i = 1; i <= k; ++i) { n     result *= n - k + i; n     result /= i; n   } n   return result; n } n double Power(int n, int power) { n   double result = 1; n   for (int i = 0; i < power; ++i) { n     result *= n; n   } n   return result; n } n int main() { n   int n, m; n   cin >> n >> m; n   vector<int> a(m); n   for (int i = 0; i < m; ++i) { n     cin >> a[i]; n   } n   vector<vector<vector<double> > > p(n + 1); n   for (int i = 0; i <= n; ++i) { n     p[i].resize(m + 1); n     for (int j = 0; j <= m; ++j) { n       p[i][j].assign(n + 1, 0); n     } n   } n   for (int j = 0; j <= m; ++j) { n     p[0][j][0] = 1; n     for (int i = 1; i <= n; ++i) { n       p[0][j][i] = 0; n       p[i][j][0] = 0; n     } n   } n   for (int i = 0; i <= n; ++i) { n     for (int k = 0; k <= n; ++k) { n       if (k == (i + a[0] - 1) / a[0]) { n         p[i][1][k] = 1; n       } else { n         p[i][1][k] = 0; n       } n     } n   } n   for (int j = 2; j <= m; ++j) { n     for (int i = 1; i <= n; ++i) { n       for (int k = 1; k <= n; ++k) { n         p[i][j][k] = 0; n         for (int h = a[j - 1] * k - a[j - 1] + 1; n              (h <= i) && (h < a[j - 1] * (k + 1) - a[j - 1] + 1); ++h) { n           double sum = 0; n           for (int l = 0; l <= k; ++l) { n             sum += p[i - h][j - 1][l]; n           } n           p[i][j][k] += Cnk(i, h) * Power(j - 1, i - h) * sum; n         } n         for (int h = 0; (h <= i) && (h < k * a[j - 1] - a[j - 1] + 1); ++h) { n           p[i][j][k] += Cnk(i, h) * Power(j - 1, i - h) * p[i - h][j - 1][k]; n         } n         p[i][j][k] /= Power(j, i); n       } n     } n   } n   double E = 0; n   for (int i = 0; i <= n; ++i) { n     E += i * p[n][m][i]; n   } n   printf( %.10lf , E); n   getchar(); n   getchar(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ull = unsigned long long; n int main() { n   int t; n   cin >> t; n   while (t--) { n     ull n, k, x; n     cin >> n >> k >> x; n     cin.ignore(); n     string arr; n     getline(cin, arr); n     vector<pair<ull, ull>> values; n     for (int i = arr.size(); i-- > 0;) { n       if (arr[i] ==  a ) { n         if (i == arr.size() - 1 || arr[i + 1] !=  a ) n           values.emplace_back(0, 1); n         else n           values.back().second++; n         continue; n       } n       if (i == arr.size() - 1) n         values.emplace_back(1, k); n       else if (arr[i + 1] ==  * ) n         values.back().second += k; n       else n         values.emplace_back(1, k); n     } n     x--; n     for (int i = values.size(); i-- > 0;) { n       if (values[i].first == 0) { n         for (int j = 0; j < values[i].second; j++) cout <<  a ; n         continue; n       } n       ull weight = 1; n       for (int j = 0; j < values.size(); j++) { n         if (j == i) break; n         if (!values[j].first) continue; n         ull temp = weight * (values[j].second + 1); n         if (temp < weight) { n           weight = 0; n           break; n         } n         weight = temp; n       } n       if (!weight) continue; n       int total_b = x / weight; n       for (int j = 0; j < total_b; j++) cout <<  b ; n       x -= total_b * weight; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1000000007; n const int MM = 998244353; 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 int _runtimeTerror_() { n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   sort(a.begin(), a.end()); n   string s; n   cin >> s; n   vector<int> ans(n + 1); n   vector<char> dir(n); n   dir[0] = s[0]; n   ans[0] = 0; n   char heavy = s[0]; n   for (int i = 1; i < n; ++i) { n     if (s[i] == heavy) { n       if (i & 1) { n         dir[i] =  L  ^  R  ^ heavy; n         ans[i] += 0; n         ++ans[0]; n         --ans[i]; n       } else { n         dir[i] = heavy; n         ans[i] += 0; n         ++ans[0]; n         --ans[i]; n       } n     } else { n       ans[i] += i; n       dir[i] = s[i]; n       ans[i + 1] -= i; n     } n     heavy = s[i]; n   } n   for (int i = 1; i < n; ++i) { n     ans[i] += ans[i - 1]; n   } n   for (int i = 0; i < n; ++i) { n     cout << a[ans[i]] <<     << dir[i] <<   n ; 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 int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, i, j, k, l, m, b, t, sum, sum1, flag, max1, min1; n   cin >> n >> m; n   long long a[n + 1]; n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   sum = 0; n   long long pre[100009]; n   pre[1] = a[1]; n   for (i = 2; i <= n; i++) { n     pre[i] = pre[i - 1] + a[i]; n   } n   max1 = INT_MIN; n   for (i = 1; i <= n; i++) { n     k = pre[i]; n     l = pre[n] - pre[i]; n     k = k % m; n     l = l % m; n     max1 = max(k + l, max1); n   } n   cout << max1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n template <class T> n void out(T x) { n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   if (x > 9) out(x / 10); n   putchar(x % 10 +  0 ); n } n long long dp[2005]; n struct node { n   int x, y; n } a[2005]; n bool cmp(node a, node b) { n   if (a.x == b.x) return a.y < b.y; n   return a.x < b.x; n } n long long fact[200010], inv[200010]; n inline long long MOD(long long x) { n   while (x < 0) x += mod; n   return x % mod; n } n long long binpow(long long a, int k) { n   if (k == 0) return 1; n   long long ans = binpow(a, k / 2); n   ans = 1ll * ans * ans % mod; n   if (k % 2) ans = 1ll * ans * a % mod; n   return ans; n } n long long C(long long n, long long k) { n   if (k > n) return 0; n   long long ans = fact[n] * 1ll * inv[k] % mod; n   ans = 1ll * ans * inv[n - k] % mod; n   return ans % mod; n } n void pre() { n   fact[0] = inv[0] = 1; n   for (int i = 1; i < 200010; i++) { n     fact[i] = 1ll * i * fact[i - 1] % mod; n     inv[i] = binpow(fact[i], mod - 2); n   } n } n int main() { n   pre(); n   int h, w, n; n   scanf( %d%d%d , &h, &w, &n); n   for (int i = 0; i < n; i++) scanf( %d%d , &a[i].x, &a[i].y); n   sort(a, a + n, cmp); n   long long ans = C(h + w - 2, h - 1); n   for (int i = 0; i < n; i++) { n     dp[i] = C(a[i].x + a[i].y - 2, a[i].y - 1); n     for (int j = 0; j < i; j++) n       if (a[j].x <= a[i].x && a[j].y <= a[i].y) { n         dp[i] = (dp[i] - n                  dp[j] * C(a[i].x - a[j].x + a[i].y - a[j].y, a[i].x - a[j].x) + n                  mod) % n                 mod; n       } n     ans = (ans - dp[i] * C(h - a[i].x + w - a[i].y, h - a[i].x) + mod) % mod; n   } n   out((ans + mod) % mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int>> adj[101]; n int vis[101][101]; n int id; n bool dfs(const int &idx, int cur, int to) { n   if (cur == to) return true; n   bool ret = false; n   for (int i = 0; i < (int)adj[idx][cur].size(); i++) { n     if (vis[idx][adj[idx][cur][i]] == id) continue; n     vis[idx][adj[idx][cur][i]] = id; n     ret |= dfs(idx, adj[idx][cur][i], to); n   } n   return ret; n } n int main() { n   int n, m, q; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < 101; i++) adj[i].resize(n); n   for (int i = 0, u, v, c; i < m; i++) { n     scanf( %d%d%d , &u, &v, &c); n     adj[c - 1][u - 1].push_back(v - 1); n     adj[c - 1][v - 1].push_back(u - 1); n   } n   int u, v; n   scanf( %d , &q); n   while (q--) { n     id++; n     scanf( %d%d , &u, &v); n     int ans = 0; n     for (int i = 0; i < m; i++) ans += dfs(i, u - 1, v - 1); n     printf( %d n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 1e5 + 7; n int H, ans; n int cnt[10007][444], a[Maxn], flag[Maxn]; n int mark[Maxn]; n vector<int> luck; n void lucky(int p) { n   if (p > 10000) return; n   luck.push_back(p); n   mark[p] = true; n   p *= 10; n   lucky(p + 4); n   lucky(p + 7); n } n void add(int l, int r, int d) { n   int p1 = l / H; n   int p2 = r / H; n   for (int i = p1 + 1; i < p2; i++) flag[i] += d; n   if (p1 == p2) { n     for (int i = l; i <= r; i++) cnt[a[i]][p1]--, a[i] += d, cnt[a[i]][p1]++; n     return; n   } n   int L = p2 * H; n   int R = p1 * H + H - 1; n   for (int i = L; i <= r; i++) cnt[a[i]][p2]--, a[i] += d, cnt[a[i]][p2]++; n   for (int i = l; i <= R; i++) cnt[a[i]][p1]--, a[i] += d, cnt[a[i]][p1]++; n } n void Find(int l, int r) { n   int p1 = l / H; n   int p2 = r / H; n   for (int i = p1 + 1; i < p2; i++) n     for (int j = 0; j < luck.size(); j++) n       if (luck[j] - flag[i] > 0) ans += cnt[luck[j] - flag[i]][i]; n   if (p1 == p2) { n     for (int i = l; i <= r; i++) ans += mark[a[i] + flag[p1]]; n     return; n   } n   int L = p2 * H; n   int R = p1 * H + H - 1; n   for (int i = L; i <= r; i++) ans += mark[a[i] + flag[p2]]; n   for (int i = l; i <= R; i++) ans += mark[a[i] + flag[p1]]; n } n int main() { n   int n, q; n   scanf( %d%d , &n, &q); n   H = sqrt(n); n   for (int i = 0; i < n; i++) scanf( %d , a + i), cnt[a[i]][i / H]++; n   lucky(4); n   lucky(7); n   for (int i = 0; i < q; i++) { n     char s[10]; n     scanf( %s  , s); n     int l, r, x; n     if (s[0] ==  c ) { n       ans = 0; n       scanf( %d%d , &l, &r); n       l--, r--; n       Find(l, r); n       printf( %d  , ans); n     } else { n       scanf( %d%d%d , &l, &r, &x); n       l--, r--; n       add(l, r, x); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-8; n char img[2007][2007]; n int c[2007], r[2007]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) scanf( %s , img[i]); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) img[i][j] -=  0 ; n   int ans = 0; n   for (int i = 0; i < n; i++) n     for (int j = n - 1; j > i; --j) n       if (c[j] ^ r[i] ^ img[i][j]) { n         ++ans; n         c[j] ^= 1, r[i] ^= 1; n       } n   for (int i = 0; i < n; i++) img[i][i] ^= c[i] ^ r[i]; n   memset(c, 0, sizeof(c)), memset(r, 0, sizeof(r)); n   for (int i = n - 1; i >= 0; --i) n     for (int j = 0; j < i + 1; j++) n       if (c[j] ^ r[i] ^ img[i][j]) { n         ++ans; n         c[j] ^= 1, r[i] ^= 1; n       } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:102400000,102400000 ) n using namespace std; n const int maxn = 1e6 + 3000; n const long long mod = 1e9 + 7; n long long k, p; n int f[maxn]; n bool vis[maxn]; n long long cnt[maxn]; n long long qpow(long long x, long long p) { n   long long ans = 1; n   while (p) { n     if (p & 1) ans = ans * x % mod; n     x = x * x % mod; n     p >>= 1; n   } n   return ans; n } n int main() { n   cin >> p >> k; n   if (k == 0) { n     cout << qpow(p, p - 1) << endl; n     return 0; n   } n   if (k == 1) { n     cout << qpow(p, p) << endl; n     return 0; n   } n   for (int i = 1; i < p; i++) { n     f[i] = i * k % p; n   } n   long long ans = 1; n   for (int i = 1; i < p; i++) { n     int c = 0; n     if (vis[i]) continue; n     int x = i; n     while (!vis[x]) { n       vis[x] = 1; n       x = f[x]; n       c++; n     } n     cnt[c]++; n   } n   for (int i = 2; i < maxn; i++) { n     if (cnt[i]) { n       ans = ans * qpow(i * cnt[i] + 1, cnt[i]) % mod; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void reset(int& n, int first) { n = n & ~(1 << first); } n bool check(int n, int first) { return (bool)(n & (1 << first)); } n const int mx = 55; n long long inf = 1e18; n unsigned long long f[mx], v; n int a[mx]; n int main() { n   f[0] = f[1] = 1; n   int i, j, k, n; n   for (i = 2; i < mx; i++) f[i] = f[i - 1] + f[i - 2]; n   while (cin >> n >> v) { n     int cur = n; n     for (i = 1; i <= n; i++) a[i] = i; n     for (i = 1; i <= n; i++) { n       if (v > f[cur - 1]) { n         swap(a[i], a[i + 1]); n         i++; n         cur -= 2; n         v -= f[cur + 1]; n       } else n         cur--; n     } n     for (i = 1; i <= n; i++) printf( %d  , a[i]); n     puts(  ); n   } n } n


#include <bits/stdc++.h> n using ll = long long; n int main() { n   std::ios_base::sync_with_stdio(0); n   std::cin.tie(0); n   std::string s; n   std::cin >> s; n   int n = s.length(); n   std::vector<std::set<int>> adj(26); n   for (int i = 0; i < n; ++i) { n     int d = s[i] -  a ; n     adj[d].insert(i); n   } n   int q; n   std::cin >> q; n   while (q--) { n     int op; n     std::cin >> op; n     if (op == 1) { n       int pos; n       char c; n       std::cin >> pos >> c; n       --pos; n       int d = s[pos] -  a ; n       s[pos] = c; n       adj[d].erase(pos); n       adj[c -  a ].insert(pos); n     } else { n       int l, r; n       std::cin >> l >> r; n       --l, --r; n       int ans = 0; n       for (int i = 0; i < 26; ++i) { n         if (adj[i].empty()) { n           continue; n         } n         auto it = adj[i].lower_bound(l); n         if (it == adj[i].end()) { n           continue; n         } n         int index = *it; n         if (index <= r) { n           ++ans; n         } n       } n       std::cout << ans <<   n ; n     } n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n using INT = long long; n int n; n int a[200010]; n vector<int> B[200010]; n void add(int u, int val) { n   for (; u <= n; u += u & -u) B[u].push_back(val); n } n int calc(int u, int val) { n   int ans = 0; n   for (; u; u -= u & -u) { n     int id = lower_bound(B[u].begin(), B[u].end(), val) - B[u].begin(); n     ans += B[u].size() - id; n   } n   return ans; n } n int calc(int l, int r, int val) { n   if (l > r) return 0; n   return calc(r, val) - calc(l - 1, val); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) add(i, min(a[i], n)); n   for (int i = 1; i <= n; i++) sort(B[i].begin(), B[i].end()); n   INT ans = 0; n   for (int i = 1; i < n; i++) { n     ans += calc(i + 1, min(n, a[i]), i); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 10; n bool a[maxn]; n int main() { n   int n, k; n   while (~scanf( %d%d , &n, &k)) { n     int p = (n - k) / 2 + 1; n     int t = n / p; n     while (t--) { n       for (int i = 0; i < p; i++) { n         if (i == p - 1) n           printf( 0 ); n         else n           printf( 1 ); n       } n     } n     for (int i = 0; i < n % p; i++) printf( 1 ); n     printf(  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     int o = 0; n     int z = 0; n     for (int i = 0; i < n; i++) { n       int y; n       cin >> y; n       if (y == 1) n         o++; n       else n         z++; n     } n     if (z == o) { n       cout << n / 2 <<   n ; n       for (int i = 1; i <= n / 2; i++) cout <<  0  ; n     } else if (z > o) { n       cout << n / 2 <<   n ; n       for (int i = 1; i <= n / 2; i++) cout <<  0  ; n     } else { n       int y = n / 2; n       if (y % 2 == 0) { n         cout << y <<   n ; n         for (int i = 0; i < y; i++) cout <<  1  ; n       } else { n         cout << (y + 1) <<   n ; n         for (int i = 0; i < y + 1; i++) n           cout <<  1  n                <<    ; n       } n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, x, i, w[100005], sum; n int main() { n   cin >> n >> x; n   for (i = 1; i <= n; i++) { n     cin >> w[i]; n     sum = sum + w[i]; n   } n   if (sum + n - 1 == x) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, A, B, a[200006]; n long long ask(int l, int r) { n   long long ans; n   int mid = (l + r) / 2, n       num = upper_bound(a + 1, a + k + 1, r) - lower_bound(a + 1, a + k + 1, l); n   if (num == 0) n     return A; n   else n     ans = 1LL * B * num * (r - l + 1); n   return r - l ? min(ans, ask(l, mid) + ask(mid + 1, r)) : ans; n } n int main() { n   scanf( %d%d%d%d , &n, &k, &A, &B); n   for (int i = 1; i <= k; i++) scanf( %d , &a[i]); n   sort(a + 1, a + k + 1); n   printf( %lld n , ask(1, 1 << n)); n } n


#include <bits/stdc++.h> n const int M = 1e5 + 5; n int len; n long long lsh[M * 3]; n int n, m, k, p, t[M], v[M], nxt[M], pre[M]; n long long CB[M * 3]; n long long now, val[M], ans[M]; n struct Query { n   int L, R, p, id; n   inline bool operator<(const Query& it) const { n     return p == it.p ? p & 1 ? R < it.R : R > it.R : L < it.L; n   } n } q[M]; n inline void Add(const int& id, const int& to) { n   now += CB[to]; n   ++CB[v[id]]; n } n inline void Del(const int& id, const int& to) { n   --CB[v[id]]; n   now -= CB[to]; n } n signed main() { n   register int i, L = 1, R = 0; n   scanf( %d%d , &n, &k); n   lsh[++len] = 0; n   lsh[++len] = k; n   lsh[++len] = -k; n   for (i = 1; i <= n; ++i) scanf( %d , t + i); n   for (i = 1; i <= n; ++i) { n     scanf( %lld , val + i); n     if (t[i] == 1) n       val[i] = val[i - 1] + val[i]; n     else n       val[i] = val[i - 1] - val[i]; n     lsh[++len] = val[i]; n     lsh[++len] = val[i] - k; n     lsh[++len] = val[i] + k; n   } n   std::sort(lsh + 1, lsh + len + 1); n   len = std::unique(lsh + 1, lsh + len + 1) - lsh - 1; n   for (i = 0; i <= n; ++i) { n     v[i] = std::lower_bound(lsh + 1, lsh + len + 1, val[i]) - lsh; n     pre[i] = std::lower_bound(lsh + 1, lsh + len + 1, val[i] - k) - lsh; n     nxt[i] = std::lower_bound(lsh + 1, lsh + len + 1, val[i] + k) - lsh; n   } n   scanf( %d , &m); n   p = ceil(n / sqrt(m * 2.0 / 3)); n   for (i = 1; i <= m; ++i) { n     scanf( %d%d , &q[i].L, &q[i].R); n     --q[i].L; n     q[i].id = i; n     q[i].p = q[i].L / p; n   } n   std::sort(q + 1, q + m + 1); n   for (i = 1; i <= m; ++i) { n     const int &QL = q[i].L, &QR = q[i].R; n     while (R < QR) ++R, Add(R, pre[R]); n     while (L > QL) --L, Add(L, nxt[L]); n     while (L < QL) Del(L, nxt[L]), ++L; n     while (R > QR) Del(R, pre[R]), --R; n     ans[q[i].id] = now; n   } n   for (i = 1; i <= m; ++i) printf( %lld n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, d[200], g[200][200], dp[200][200], ans[200]; n int cnt, st[200], ed[200]; n bool flag[200]; n void dfs1(int u) { n   flag[u] = true; n   st[u] = ++cnt; n   for (int i = 1; i <= n; i++) n     if (g[u][i] == 1 && !flag[i]) dfs1(i); n   ed[u] = ++cnt; n } n void dfs2(int u) { n   flag[u] = true; n   for (int i = 1; i <= n; i++) n     if (i ^ u) dp[u][i] = d[g[u][i]]; n   dp[u][u] = m; n   for (int i = 1; i <= n; i++) n     if (g[u][i] == 1 && !flag[i]) { n       dfs2(i); n       for (int j = 1; j <= n; j++) n         if (st[i] <= st[j] && ed[j] <= ed[i]) n           dp[u][j] += dp[i][j]; n         else n           dp[u][j] += min(dp[i][j], dp[i][dp[i][0]]); n     } n   dp[u][0] = u; n   for (int i = 1; i <= n; i++) n     if (st[u] <= st[i] && ed[i] <= ed[u] && dp[u][i] < dp[u][dp[u][0]]) n       dp[u][0] = i; n } n void dfs3(int u) { n   flag[u] = true; n   for (int i = 1; i <= n; i++) n     if (g[u][i] == 1 && !flag[i]) { n       if (st[i] <= st[ans[u]] && ed[ans[u]] <= ed[i]) n         ans[i] = ans[u]; n       else if (dp[i][ans[u]] < dp[i][dp[i][0]]) n         ans[i] = ans[u]; n       else n         ans[i] = dp[i][0]; n       dfs3(i); n     } n } n void solve() { n   for (int k = 1; k <= n; k++) n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= n; j++) g[i][j] = min(g[i][j], g[i][k] + g[k][j]); n   memset(flag, false, sizeof(flag)); n   cnt = 0; n   dfs1(1); n   memset(flag, false, sizeof(flag)); n   dfs2(1); n   ans[1] = dp[1][0]; n   memset(flag, false, sizeof(flag)); n   dfs3(1); n   printf( %d n , dp[1][dp[1][0]]); n   for (int i = 1; i <= n; i++) printf( %d%c , ans[i], i < n ?     :   n ); n } n int main() { n   int u, v; n   while (scanf( %d%d , &n, &m) == 2) { n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= n; j++) g[i][j] = 1000000000; n     for (int i = 1; i < n; i++) scanf( %d , d + i); n     for (int i = 1; i < n; i++) { n       scanf( %d%d , &u, &v); n       g[u][v] = g[v][u] = 1; n     } n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NMax = 101000; n int N; n vector<pair<int, int> > A, B, C, D; n bool cmp1(pair<int, int> a, pair<int, int> b) { n   if (a.first < b.first) return 1; n   if (a.first > b.first) return 0; n   if (a.second < b.second) return 1; n   return 0; n } n bool cmp2(pair<int, int> a, pair<int, int> b) { n   if (a.first < b.first) return 1; n   if (a.first > b.first) return 0; n   if (a.second > b.second) return 1; n   return 0; n } n bool cmp3(pair<int, int> a, pair<int, int> b) { n   if (a.first > b.first) return 1; n   if (a.first < b.first) return 0; n   if (a.second > b.second) return 1; n   return 0; n } n bool cmp4(pair<int, int> a, pair<int, int> b) { n   if (a.first > b.first) return 1; n   if (a.first < b.first) return 0; n   if (a.second < b.second) return 1; n   return 0; n } n int main() { n   scanf( %d , &N); n   for (int i = 1; i <= N; i++) { n     int first, second; n     scanf( %d%d , &first, &second); n     if (first >= 0 && second >= 0) n       A.push_back(make_pair(first, second)); n     else if (first >= 0 && second <= 0) n       B.push_back(make_pair(first, second)); n     else if (first <= 0 && second <= 0) n       C.push_back(make_pair(first, second)); n     else n       D.push_back(make_pair(first, second)); n   } n   int ret = 0; n   for (int i = 0; i < A.size(); i++) { n     if (A[i].first) ret += 2; n     if (A[i].second) ret += 2; n     ret += 2; n   } n   for (int i = 0; i < B.size(); i++) { n     if (B[i].first) ret += 2; n     if (B[i].second) ret += 2; n     ret += 2; n   } n   for (int i = 0; i < C.size(); i++) { n     if (C[i].first) ret += 2; n     if (C[i].second) ret += 2; n     ret += 2; n   } n   for (int i = 0; i < D.size(); i++) { n     if (D[i].first) ret += 2; n     if (D[i].second) ret += 2; n     ret += 2; n   } n   printf( %d n , ret); n   sort(A.begin(), A.end(), cmp1); n   for (int i = 0; i < A.size(); i++) { n     if (A[i].first) printf( 1 %d R n , A[i].first); n     if (A[i].second) printf( 1 %d U n , A[i].second); n     printf( 2 n ); n     if (A[i].second) printf( 1 %d D n , A[i].second); n     if (A[i].first) printf( 1 %d L n , A[i].first); n     printf( 3 n ); n   } n   sort(B.begin(), B.end(), cmp2); n   for (int i = 0; i < B.size(); i++) { n     if (B[i].first) printf( 1 %d R n , B[i].first); n     if (B[i].second) printf( 1 %d D n , -B[i].second); n     printf( 2 n ); n     if (B[i].second) printf( 1 %d U n , -B[i].second); n     if (B[i].first) printf( 1 %d L n , B[i].first); n     printf( 3 n ); n   } n   sort(C.begin(), C.end(), cmp3); n   for (int i = 0; i < C.size(); i++) { n     if (C[i].first) printf( 1 %d L n , -C[i].first); n     if (C[i].second) printf( 1 %d D n , -C[i].second); n     printf( 2 n ); n     if (C[i].second) printf( 1 %d U n , -C[i].second); n     if (C[i].first) printf( 1 %d R n , -C[i].first); n     printf( 3 n ); n   } n   sort(D.begin(), D.end(), cmp4); n   for (int i = 0; i < D.size(); i++) { n     if (D[i].first) printf( 1 %d L n , -D[i].first); n     if (D[i].second) printf( 1 %d U n , D[i].second); n     printf( 2 n ); n     if (D[i].second) printf( 1 %d D n , D[i].second); n     if (D[i].first) printf( 1 %d R n , -D[i].first); n     printf( 3 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   char a[110]; n   cin >> n >> a; n   for (int i = 2; i <= n; i++) { n     if (n % i == 0) reverse(a, a + i); n   } n   cout << a << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m, k, minn = 10000, a[1000], temp = 0; n   cin >> n >> m >> k; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] <= k && a[i] != 0) { n       temp = abs(i + 1 - m) * 10; n       if (temp < minn) minn = temp; n     } n   } n   cout << minn; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gi() { n   int a; n   scanf( %d , &a); n   return a; n } n long long gli() { n   long long a; n   scanf( %I64d , &a); n   return a; n } n long long gcd(long long a, long long b) { n   if (a == 0) return b; n   if (b == 0) return a; n   if (a > b) return gcd(a % b, b); n   return gcd(b % a, a); n } n int pr[1000000]; n int main() { n   unordered_map<long long, vector<pair<long long, long long> > > m; n   unordered_map<long long, vector<pair<long long, long long> > >::iterator it; n   int n = gi(); n   for (int i = 0; i < n; i++) { n     long long x = gli(); n     long long y = gli(); n     long long c = gli(); n     m[x].push_back(make_pair(y, c)); n   } n   vector<pair<long long, long long> > main; n   int L = 0; n   long long gg = 0; n   int first = 1; n   for (it = m.begin(); it != m.end(); it++) { n     vector<pair<long long, long long> > &v = it->second; n     sort(v.begin(), v.end()); n     if (first) { n       first = 0; n       L = v.size(); n       long long g = 0; n       for (int i = 0; i < L; i++) g = gcd(g, v[i].second); n       for (int i = 0; i < L; i++) n         main.push_back(make_pair(v[i].first, v[i].second / g)); n     } else { n       if (int(v.size()) != L || v[0].first != main[0].first || n           v[0].second % main[0].second != 0) { n         printf( 0 n ); n         return 0; n       } n       long long f = v[0].second / main[0].second; n       for (int i = 1; i < L; i++) n         if (v[i].first != main[i].first || v[i].second % main[i].second != 0 || n             v[i].second / main[i].second != f) { n           printf( 0 n ); n           return 0; n         } n     } n     gg = gcd(gg, v[0].second); n   } n   m.clear(); n   long long g = 0; n   for (int i = 0; i < L; i++) g = gcd(g, main[i].second); n   g *= gg / main[0].second; n   memset(pr, -1, sizeof(pr)); n   long long r = 1; n   long long c = 1; n   while (g % 2 == 0) { n     c++; n     g /= 2; n   } n   r *= c; n   for (int i = 3; i < 1000000; i += 2) n     if (pr[i]) { n       for (int j = i * 3; j < 1000000; j += i * 2) pr[i] = 0; n       long long c = 1; n       while (g % i == 0) { n         c++; n         g /= i; n       } n       r *= c; n     } n   if (g > 1LL) r *= 2; n   cout << r << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 55; n const unsigned long long M = 1000000009LL; n const int K = 3; n const int LIT = 2500; n const int INF = 1 << 30; n const double eps = 1e-6; n const unsigned long long MOD = 1000000007LL; n const int dir[][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; n unsigned long long n, m; n unsigned long long pw2[N] = {0}; n void init() { n   pw2[0] = 1; n   for (int i = 1; i < N; i++) { n     pw2[i] = pw2[i - 1] * 2; n   } n } n int getCP(int n, int m) { n   if (n == 3 && m == 1) return -1; n   if (n == 3 && m == 2) return -2; n   unsigned long long tot = pw2[n - 2]; n   unsigned long long mid = pw2[n - 3]; n   if (m > mid) { n     return getCP(n, m - mid) - 1; n   } else { n     return getCP(n - 1, m); n   } n } n vector<unsigned long long> getP(unsigned long long n, unsigned long long m) { n   vector<unsigned long long> ans; n   if (n == 1) { n     ans.push_back(1); n     return ans; n   } n   if (n == 2) { n     if (m == 1) { n       ans.push_back(1); n       ans.push_back(2); n     } n     if (m == 2) { n       ans.push_back(2); n       ans.push_back(1); n     } n     return ans; n   } n   unsigned long long tot = pw2[n - 1]; n   unsigned long long mid = pw2[n - 2]; n   if (m > mid) { n     ans = getP(n, tot + 1 - m); n     reverse(ans.begin(), ans.end()); n     return ans; n   } n   vector<unsigned long long> pre = getP(n - 1, (m + 1) / 2); n   int cp = n + getCP(n, m); n   for (int i = 0; i < cp; i++) { n     ans.push_back(pre[i]); n   } n   ans.push_back(n); n   for (int i = cp; i < n - 1; i++) { n     ans.push_back(pre[i]); n   } n   return ans; n } n void solve() { n   vector<unsigned long long> ans = getP(n, m); n   for (int i = 0; i < ans.size(); i++) cout << ans[i] <<    ; n   cout << endl; n } n int main() { n   int T; n   while (cin >> n >> m) { n     init(); n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int th[100080], bl[100080]; n int main() { n   int i, j, n, k, mx = 0, sum, fl, fval; n   scanf( %d %d , &n, &k); n   for (i = 0; i < n; i++) { n     scanf( %d , &th[i]); n   } n   for (i = 0; i < n; i++) { n     scanf( %d , &bl[i]); n   } n   j = 0; n   i = 0; n   sum = 0; n   fl = 0; n   fval = th[0]; n   while (j < n) { n     while (i < k && j < n) { n       if (bl[j] == 0) sum += th[j]; n       i++; n       j++; n     } n     if (i == k && j <= n) { n       if (sum > mx) mx = sum; n       fl++; n       if (bl[fl - 1] == 0) sum -= fval; n       fval = th[fl]; n       i--; n     } n   } n   for (i = 0; i < n; i++) { n     if (bl[i] == 1) mx += th[i]; n   } n   printf( %d , mx); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long Solve(long long n, long long k) { n   long long low = 1; n   long long high = ((long long)1 << n) - 1; n   long long mid = low + (high - low) / 2; n   if (mid == k) n     return n; n   else if (mid < k) n     return Solve(n - 1, k - mid); n   else n     return Solve(n - 1, k); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, k; n   cin >> n >> k; n   cout << Solve(n, k) << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 200; n long long n, num[N], tmp; n int main() { n   cin >> n; n   for (int i = 0, v; i < n; i++) cin >> v, num[v]++; n   for (int i = 1; i <= 100; i++) { n     for (int j = 1; j < i; j++) n       tmp = max(0ll, min(num[0] - num[j], num[i])), num[j] += tmp, n       num[i] -= tmp; n     tmp = num[i] - num[0]; n     int k = tmp % (i + 1); n     tmp = tmp / (i + 1); n     if (tmp > 0 || k > 0) n       for (int j = 0; j <= i; j++) { n         num[j] += tmp, num[i] -= tmp; n         if (j < k) num[j]++, num[i]--; n       } n   } n   cout << num[0] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int main() { n   cin >> n; n   cout << n / 2 - !(n % 2) - (n % 4 == 2) <<     << n / 2 + 1 + (n % 4 == 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n bool doubleEquals(double a, double b) { return abs(a - b) < eps; } n bool db_less(double a, double b) { return a < b - eps; } n bool db_equal(double a, double b) { return abs(a - b) < eps; } n bool db_less_equal(double a, double b) { return a < b + eps; } n struct Point { n   double x, y; n   Point() {} n   Point(double x, double y) : x(x), y(y) {} n   Point operator-(Point p) { return Point(x - p.x, y - p.y); } n   Point operator+(Point p) { return Point(x + p.x, y + p.y); } n   Point operator/(double e) { return Point(x / e, y / e); } n   Point operator*(double e) { return Point(x * e, y * e); } n   bool operator<(const Point &p) const { n     return x < p.x || (x == p.x && y < p.y); n   } n   bool operator==(Point p) { n     return doubleEquals(x, p.x) && doubleEquals(y, p.y); n   } n }; n ostream &operator<<(ostream &os, Point p) { n   return os <<  (  << p.x <<  ,  << p.y <<  ) ; n } n double cross(Point O, Point A, Point B) { n   return (A - O).x * (B - O).y - (A - O).y * (B - O).x; n } n double dot(Point A, Point B) { return A.x * B.x + A.y * B.y; } n double dist(Point A, Point B) { return sqrtl(dot(A - B, A - B)); } n double distLine(Point A, Point B, Point P) { n   return abs(cross(A, B, P) / dist(A, B)); n } n double angle(Point O, Point A, Point B) { n   return acos(dot(A - O, B - O) / (dist(O, A) * dist(O, B))); n } n vector<Point> convexHull(vector<Point> &points) { n   vector<Point> hull(points.size() + 1); n   int k = 0; n   sort(points.begin(), points.end()); n   for (int i = 0; i < points.size(); i++) { n     while (k >= 2 && (cross(hull[k - 2], hull[k - 1], points[i]) + eps > 0)) n       k--; n     hull[k++] = points[i]; n   } n   for (int i = points.size() - 2, t = k + 1; i >= 0; i--) { n     while (k >= t && cross(hull[k - 2], hull[k - 1], points[i]) + eps > 0) k--; n     hull[k++] = points[i]; n   } n   k--; n   hull.resize(k); n   return hull; n } n bool pnpoly(Point p, vector<Point> &points) { n   bool c = 0; n   for (int i = 0, j = points.size() - 1; i < points.size(); j = i++) { n     if (((points[i].y > p.y) != (points[j].y > p.y)) && n         (p.x < (double)(points[j].x - points[i].x) * (p.y - points[i].y) / n                        (points[j].y - points[i].y) + n                    points[i].x)) n       c = !c; n   } n   return c; n } n struct Line { n   double A, B, C; n   Line(double A, double B, double C) : A(A), B(B), C(C){}; n   Line(Point p1, Point p2) { n     A = p1.y - p2.y; n     B = p2.x - p1.x; n     C = p1.x * p2.y - p1.y * p2.x; n   } n   bool parallel(Line l) { return doubleEquals(B * l.A - A * l.B, 0); } n   bool equals(Line l) { n     if (!parallel(l)) return false; n     return parallel(l) && doubleEquals(distLine(l), 0); n   } n   Point inter(Line l) { n     assert(!parallel(l)); n     Point ret; n     ret.x = (l.C * B - C * l.B) / (l.B * A - B * l.A); n     ret.y = (l.C * A - C * l.A) / (B * l.A - l.B * A); n     return ret; n   } n   double distLine(Line l) { n     if (l.A == 0) return distPoint({0, -l.C / l.B}); n     return distPoint({-l.C / l.A, 0}); n   } n   double distPoint(Point p) { n     return abs(A * p.x + B * p.y + C) / sqrt(A * A + B * B); n   } n   Line getPerpendicular(Point p) { n     Line l(B, -A, 0); n     l.C = -l.A * p.x - l.B * p.y; n     return l; n   } n }; n bool pointInSegment(pair<Point, Point> s, Point p) { n   if (doubleEquals(cross(s.first, s.second, p), 0)) { n     double xa = min(s.first.x, s.second.x), xb = max(s.first.x, s.second.x); n     double ya = min(s.first.y, s.second.y), yb = max(s.first.y, s.second.y); n     return p.x + eps > xa && p.x - eps < xb && p.y + eps > ya && p.y - eps < yb; n   } n   return false; n } n bool segmentInter(pair<Point, Point> A, pair<Point, Point> B) { n   Line la(A.first, A.second), lb(B.first, B.second); n   if (la.parallel(lb) && !la.equals(lb)) return false; n   if (la.equals(lb)) { n     if (max(A.first.x, A.second.x) + eps < min(B.first.x, B.second.x)) n       return false; n     if (min(A.first.x, A.second.x) - eps > max(B.first.x, B.second.x)) n       return false; n     if (max(A.first.y, A.second.y) + eps < min(B.first.y, B.second.y)) n       return false; n     if (min(A.first.y, A.second.y) - eps > max(B.first.y, B.second.y)) n       return false; n     return true; n   } n   Point i = la.inter(lb); n   return pointInSegment(A, i) && pointInSegment(B, i); n } n double distSegmentToPoint(pair<Point, Point> s, Point P) { n   if (dot(s.first - s.second, P - s.second) < 0) return dist(s.second, P); n   if (dot(s.second - s.first, P - s.first) < 0) return dist(s.first, P); n   return distLine(s.first, s.second, P); n } n double distSegmentToSegment(pair<Point, Point> sa, pair<Point, Point> sb) { n   if (sa.first == sa.second && sb.first == sb.second) n     return dist(sa.first, sb.first); n   if (sa.first == sa.second) return distSegmentToPoint(sb, sa.first); n   if (sb.first == sb.second) return distSegmentToPoint(sa, sb.first); n   if (segmentInter(sa, sb)) return 0; n   return min( n       {distSegmentToPoint(sa, sb.first), distSegmentToPoint(sa, sb.second), n        distSegmentToPoint(sb, sa.first), distSegmentToPoint(sb, sa.second)}); n } n double polygonArea(vector<Point> &points) { n   double area = 0; n   for (int i = 0; i < points.size(); i++) n     area += cross({0, 0}, points[i], points[(i + 1) % points.size()]); n   return abs(area) / 2.0; n } n Point rotate(Point O, Point p, double rad) { n   p = p - O; n   double x = p.x * cos(rad) + p.y * -sin(rad); n   double y = p.x * sin(rad) + p.y * cos(rad); n   return Point(x, y) + O; n } n bool insideTriangle(Point A, Point B, Point C, Point p) { n   return abs(cross(p, A, B)) + abs(cross(p, B, C)) + abs(cross(p, C, A)) == n          abs(cross(p, A, B) + cross(p, B, C) + cross(p, C, A)); n } n pair<Point, Point> centerCircle(Point a, Point b, double r) { n   Point c = (a + b) / 2; n   double d = dist(a, b); n   Point v = (b - a) / d; n   swap(v.x, v.y); n   v.x *= -1; n   assert(d < 2 * r + eps); n   double q; n   if (doubleEquals(r * r, (d / 2) * (d / 2))) n     q = 0; n   else n     q = sqrt(r * r - (d / 2) * (d / 2)); n   return {c + (v * q), c - (v * q)}; n } n double getAxisAngle(Point c, Point p) { n   double ang = atan2((p - c).y, (p - c).x); n   if (ang < 0) ang += 2 * acos(-1); n   return ang; n } n struct Circle { n   Point p; n   double r; n   Circle(Point p, double r) : p(p), r(r) {} n   bool touch(Circle other) { n     double d = dist(p, other.p); n     return d < r + other.r + eps; n   } n   bool inside(Circle other) { n     double d = dist(p, other.p); n     return d + r < other.r - eps; n   } n   bool cover(Point point) { return dist(point, p) < r + eps; } n   bool equal(Circle other) { n     double d = dist(p, other.p); n     return doubleEquals(d, 0) && doubleEquals(r, other.r); n   } n }; n pair<Point, Point> circleIntersection(Circle c1, Circle c2) { n   assert(c1.touch(c2)); n   assert(!c1.inside(c2) && !c2.inside(c1)); n   assert(!c1.equal(c2)); n   double d = dist(c1.p, c2.p); n   double a = (c1.r * c1.r - c2.r * c2.r + d * d) / (2 * d); n   double h = sqrtl(c1.r * c1.r - a * a); n   Point u = (c2.p - c1.p) / d; n   Point pu = Point(-u.y, u.x); n   u = u * a; n   pu = pu * h; n   return {u + pu + c1.p, u - pu + c1.p}; n } n int main() { n   vector<Point> A; n   vector<Point> B; n   for (int i = 0; i < 4; i++) { n     int a, b; n     scanf( %d %d , &a, &b); n     A.push_back({1. * a, 1. * b}); n   } n   for (int i = 0; i < 4; i++) { n     int a, b; n     scanf( %d %d , &a, &b); n     B.push_back({1. * a, 1. * b}); n   } n   for (int i = 0; i < 4; i++) { n     int a = i; n     int b = (i + 1) % 4; n     for (int j = 0; j < 4; j++) { n       int c = j; n       int d = (j + 1) % 4; n       if (segmentInter({A[a], A[b]}, {B[c], B[d]})) { n         printf( YES ); n         return 0; n       } n     } n   } n   for (int i = 0; i < 4; i++) { n     vector<Point> v; n     v.push_back(B[0]); n     v.push_back(B[1]); n     v.push_back(B[2]); n     v.push_back(B[3]); n     if (pnpoly(A[i], v)) { n       printf( YES ); n       return 0; n     } n   } n   for (int i = 0; i < 4; i++) { n     vector<Point> v; n     v.push_back(A[0]); n     v.push_back(A[1]); n     v.push_back(A[2]); n     v.push_back(A[3]); n     if (pnpoly(B[i], v)) { n       printf( YES ); n       return 0; n     } n   } n   printf( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n]; n   a[0] = 1; n   a[1] = 5; n   a[2] = 13; n   a[3] = 25; n   for (int i = 4; i < n; i++) { n     a[i] = a[i - 1] + 4 * (i); n   } n   cout << a[n - 1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n class Matrix { n  public: n   long long m[4][4]; n   Matrix(){}; n   Matrix(long long k[4][4]) { n     for (int i = 0; i < 4; i++) n       for (int j = 0; j < 4; j++) m[i][j] = k[i][j]; n   } n }; n Matrix operator*(const Matrix &a, const Matrix &b) { n   Matrix t; n   for (int i = 0; i < 4; i++) { n     for (int j = 0; j < 4; j++) { n       t.m[i][j] = 0; n       for (int c = 0; c < 4; c++) { n         t.m[i][j] += (b.m[i][c]) * (a.m[c][j]); n         t.m[i][j] %= MOD; n       } n     } n   } n   return t; n } n Matrix pow(Matrix m, int p) { n   if (p == 1) return m; n   Matrix ret = pow(m, p / 2); n   if (p & 1) { n     return (ret * ret) * m; n   } else n     return (ret * ret); n } n int main() { n   int n; n   long long arr[4][4]; n   scanf( %d , &n); n   for (int i = 0; i < 4; i++) n     for (int j = 0; j < 4; j++) n       if (i == j) n         arr[i][j] = 0; n       else n         arr[i][j] = 1; n   Matrix g(arr); n   Matrix ret = pow(g, n); n   cout << ret.m[3][3] % MOD << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int n, deg[N], fa[N]; n int root = 0; n vector<pair<pair<int, int>, int> > ans; n struct edge { n   int to, val; n   edge() {} n   edge(int to, int val) : to(to), val(val) {} n }; n vector<edge> G[N]; n vector<int> leaf[N]; n void add_edge(int x, int y, int z) { G[x].push_back(edge(y, z)); } n void init() { n   scanf( %d , &n); n   int x, y, z; n   if (n == 2) { n     scanf( %d%d%d , &x, &y, &z); n     printf( YES n1 n1 2 %d n , z); n     exit(0); n   } n   for (int i = 1; i < n; ++i) { n     scanf( %d%d%d , &x, &y, &z); n     add_edge(x, y, z); n     add_edge(y, x, z); n     ++deg[x], ++deg[y]; n   } n } n void pre(int x, int f) { n   fa[x] = f; n   for (int i = 0; i < (int)G[x].size(); ++i) { n     int to = G[x][i].to, w = G[x][i].val; n     if (to == f) continue; n     pre(to, x); n     leaf[x].push_back(leaf[to][0]); n   } n   if (G[x].size() == 1) { n     leaf[x].push_back(x); n   } n } n void add_to_root(int x, int val) { n   if (leaf[x].size() >= 2) { n     int f1 = leaf[x][0], f2 = leaf[x][1]; n     ans.push_back(make_pair(make_pair(root, f1), val / 2)); n     ans.push_back(make_pair(make_pair(root, f2), val / 2)); n     ans.push_back(make_pair(make_pair(f1, f2), -val / 2)); n   } else { n     ans.push_back(make_pair(make_pair(root, x), val)); n   } n } n void dfs(int x, int f) { n   for (int i = 0; i < (int)G[x].size(); ++i) { n     int to = G[x][i].to, w = G[x][i].val; n     if (to == f) continue; n     dfs(to, x); n     add_to_root(to, w); n     if (x != root) add_to_root(x, -w); n   } n } n void solve() { n   int flag = 1; n   for (int i = 1; i <= n; ++i) { n     if (deg[i] == 2) { n       puts( NO ); n       return; n     } n   } n   puts( YES ); n   for (int i = 1; i <= n; ++i) { n     if (G[i].size() == 1) { n       root = i; n       break; n     } n   } n   pre(root, 0); n   dfs(root, 0); n   printf( %d n , ans.size()); n   for (int i = 0; i < (int)ans.size(); ++i) { n     printf( %d %d %d n , ans[i].first.first, ans[i].first.second, n            ans[i].second); n   } n } n int main() { n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   int mc = 0, i = 1, j = n; n   while (1) { n     cout << i <<    ; n     mc++; n     if (mc == n) break; n     cout << j <<    ; n     i++; n     j--; n     mc++; n     if (mc == n) break; n   } n   cout << endl; n   return 0; n } n


#include <iostream> n #include <sstream> n #include <cstdio> n #include <cmath> n #include <cstring> n #include <cctype> n #include <string> n #include <vector> n #include <set> n #include <unordered_set> n #include <map> n #include <unordered_map> n #include <queue> n #include <stack> n #include <algorithm> n #include <functional> n #include <climits> n #include <iterator> n #include <numeric> n #include <memory> n  n #define IOS  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0) n  n using namespace std; n typedef long long int ll; n typedef pair<int,int> pii; n typedef vector<int> vi; n  n int main() n { n     IOS; n     int t, n; n     cin>>t; n     while(t--) { n         cin>>n; n         vector<int> p(n), maxP(n); n         cin>>p[0]; n         maxP[0]=p[0]; n         for(int i=1; i<n; i++) { n             cin>>p[i]; n             maxP[i]=max(maxP[i-1], p[i]); n         } n         int r=n-1, l=n-1; n         while(l>=0) { n             while(l>=0 && maxP[r]!=p[l]) { n                 l--; n             } n             for(int i=l; i<=r; i++) { n                 cout<<p[i]<<   ; n             } n             l--; n             r=l; n         } n         cout<<  n ; n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n; n   cin >> n; n   vector<long long> a(n); n   long long p1 = 0; n   for (long long i = 0; i < n; i++) { n     long long x; n     cin >> x; n     a[i] = x; n     p1 = max(p1, x); n   } n   long long m; n   cin >> m; n   vector<pair<long long, long long> > b(m); n   long long p2 = 0; n   for (long long i = 0; i < m; i++) { n     long long x, y; n     cin >> x >> y; n     b[i].first = x; n     b[i].second = y; n     p2 = max(p2, x); n   } n   if (p2 < p1) { n     cout << -1 <<   n ; n     return; n   } n   sort(b.begin(), b.end()); n   for (long long i = m - 2; i >= 0; i--) { n     b[i].second = max(b[i].second, b[i + 1].second); n   } n   long long mx = 0; n   long long cnt = -1; n   long long ans = 0; n   long long q = -1; n   for (long long i = 0; i < n; i++) { n     mx = max(mx, a[i]); n     long long j = lower_bound(b.begin(), b.end(), make_pair(mx, q)) - b.begin(); n     if (b[j].second < i - cnt) { n       ans++; n       mx = 0; n       i--; n       cnt = i; n     } n   } n   ans++; n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   long long TESTS = 1; n   cin >> TESTS; n   while (TESTS--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = (1 << 30) - 1; n const long long linf = (1ll << 62) - 1; n const int N = 1e6; n int n; n int p[N]; n int division[2][N]; n int dln[2]; n bool u[N]; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     scanf( %d , &p[i]); n     p[i]--; n   } n   int index1 = -1, index2 = -1; n   for (int i = 0; i < n; i++) { n     if (p[i] == i) index1 = i; n     if (p[p[i]] == i) index2 = i; n   } n   if (index1 != -1) { n     puts( YES ); n     for (int i = 0; i < n; i++) n       if (index1 != i) printf( %d %d n , index1 + 1, i + 1); n     return 0; n   } n   if (index2 != -1) { n     bool flag = true; n     dln[0] = dln[1] = 0; n     fill_n(u, n, 0); n     u[index2] = u[p[index2]] = 1; n     for (int i = 0; i < n; i++) n       if (!u[i]) { n         u[i] = 1; n         division[0][dln[0]++] = i; n         int curI = p[i], step = 1; n         while (curI != i) { n           u[curI] = 1; n           division[step][dln[step]++] = curI; n           step ^= 1; n           curI = p[curI]; n         } n         if (step == 1) { n           flag = false; n           break; n         } n       } n     if (!flag) { n       puts( NO ); n       return 0; n     } n     puts( YES ); n     printf( %d %d n , index2 + 1, p[index2] + 1); n     for (int i = 0; i < dln[0]; i++) n       printf( %d %d n , index2 + 1, division[0][i] + 1); n     for (int i = 0; i < dln[1]; i++) n       printf( %d %d n , p[index2] + 1, division[1][i] + 1); n     return 0; n   } n   puts( NO ); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 5000 + 7; n const int inf = 0x3f3f3f3f; n const long long INF = 0x3f3f3f3f3f3f3f3f; n const int mod = 998244353; n const double eps = 1e-8; n const double PI = acos(-1); n template <class T, class S> n inline void add(T& a, S b) { n   a += b; n   if (a >= mod) a -= mod; n } n template <class T, class S> n inline void sub(T& a, S b) { n   a -= b; n   if (a < 0) a += mod; n } n template <class T, class S> n inline bool chkmax(T& a, S b) { n   return a < b ? a = b, true : false; n } n template <class T, class S> n inline bool chkmin(T& a, S b) { n   return a > b ? a = b, true : false; n } n int n, k, v, sum, now1, now2, a[N]; n bool dp[N][N]; n bool f[N][N]; n vector<int> vc[2]; n int main() { n   scanf( %d%d%d , &n, &k, &v); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     sum += a[i]; n   } n   if (sum < v) return puts( NO ), 0; n   dp[0][0] = true; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < k; j++) { n       if (!dp[i][j]) continue; n       dp[i + 1][(j + a[i + 1]) % k] = true; n       f[i + 1][(j + a[i + 1]) % k] = true; n       dp[i + 1][j] = true; n       f[i + 1][j] = false; n     } n   } n   if (!dp[n][v % k]) return puts( NO ), 0; n   int tmp = v % k; n   for (int i = n; i >= 1; i--) { n     if (f[i][tmp]) { n       vc[0].push_back(i); n       tmp = ((tmp - a[i]) % k + k) % k; n     } else { n       vc[1].push_back(i); n     } n   } n   sort((vc[0]).begin(), (vc[0]).end()); n   sort((vc[1]).begin(), (vc[1]).end()); n   for (auto& t : vc[0]) now1 += a[t]; n   now2 = sum - now1; n   puts( YES ); n   for (int i = 1; i < ((int)vc[0].size()); i++) n     printf( 100000 %d %d n , vc[0][i], vc[0][0]); n   for (int i = 1; i < ((int)vc[1].size()); i++) n     printf( 100000 %d %d n , vc[1][i], vc[1][0]); n   if (!((int)vc[1].size())) vc[1].push_back(vc[0].back()); n   if (!((int)vc[0].size())) vc[0].push_back(vc[1].back()); n   if (now1 < v) printf( %d %d %d n , (v - now1) / k, vc[1][0], vc[0][0]); n   if (now1 > v) printf( %d %d %d n , (now1 - v) / k, vc[0][0], vc[1][0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int main() { n   getline(cin, s); n   int n, flag = 0; n   n = s.length(); n   for (int i = n; i >= 0; i--) { n     if ((s[i] >=  a  && s[i] <=  z ) || (s[i] >=  A  && s[i] <=  Z )) { n       if (s[i] ==  a  || s[i] ==  e  || s[i] ==  i  || s[i] ==  o  || n           s[i] ==  u  || s[i] ==  y  || s[i] ==  A  || s[i] ==  E  || n           s[i] ==  I  || s[i] ==  O  || s[i] ==  U  || s[i] ==  Y ) { n         flag = 1; n       } n       break; n     } n   } n   if (flag) { n     cout <<  YES n ; n   } else { n     cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> arr; n pair<int, long long> doit(long long n) { n   if (n <= 7) return make_pair(n, n); n   vector<long long>::iterator ite = upper_bound(arr.begin(), arr.end(), n); n   int ind = (ite - arr.begin()); n   ind--; n   pair<int, long long> te1 = doit(n - arr[ind]); n   pair<int, long long> te2 = doit(arr[ind] - 1 - arr[ind - 1]); n   if (te1.first > te2.first) n     return make_pair(te1.first + 1, te1.second + arr[ind]); n   if (te1.first < te2.first) n     return make_pair(te2.first + 1, te2.second + arr[ind - 1]); n   if (te1.second + arr[ind] > te2.second + arr[ind - 1]) n     return make_pair(te1.first + 1, te1.second + arr[ind]); n   return make_pair(te2.first + 1, te2.second + arr[ind - 1]); n } n int main() { n   for (long long i = 0; i <= 100001; i++) { n     arr.push_back(i * i * i); n   } n   long long m; n   cin >> m; n   pair<int, long long> temp = doit(m); n   cout << temp.first <<     << temp.second; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double xp, yp, vp; n double x, y, v, r; n const double pi = acos(-1); n double cal(double x0, double y0, double x1, double y1) { n   double ji = x0 * x1 + y0 * y1; n   double L0 = sqrt(x0 * x0 + y0 * y0); n   double L1 = sqrt(x1 * x1 + y1 * y1); n   double cosA = ji / L0 / L1; n   double A = acos(cosA); n   double s = L1 * L0 * sin(A) / (double)2; n   double l = sqrt((x0 - x1) * (x0 - x1) + (y0 - y1) * (y0 - y1)); n   double dis = s * (double)2 / l; n   if (dis >= r || (L0 * L0 + l * l - L1 * L1) < 0 || n       (L1 * L1 + l * l - L0 * L0) < 0) n     return l; n   double a0 = acos(r / L0); n   double a1 = acos(r / L1); n   double Lsum = r * (A - a0 - a1) + L0 * sin(a0) + L1 * sin(a1); n   return Lsum; n } n bool s(double t) { n   double R = sqrt(xp * xp + yp * yp); n   double a = acos(xp / R); n   if (yp < 0) a = -a + 2 * pi; n   a = a + t * vp / R; n   double x0 = R * cos(a); n   double y0 = R * sin(a); n   double t0 = cal(x0, y0, x, y) / v; n   if (t0 <= t) n     return true; n   else n     return false; n } n int main() { n   cin >> xp >> yp >> vp; n   cin >> x >> y >> v >> r; n   double l = 0, r = 1e4, mid; n   while (r - l > 1e-9) { n     mid = (r + l) / (double)2; n     if (s(mid)) n       r = mid; n     else n       l = mid; n   } n   printf( %.10lf , mid); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   std::ios::sync_with_stdio(false); n   cin.tie(0); n   int t, i, n, j, e; n   char grid[8][8]; n   map<int, bool> black, white; n   for (i = 0; i < 8; i++) { n     cin >> grid[i]; n   } n   int min = INT_MAX; n   int flag = 0; n   for (i = 0; i < 8; i++) { n     for (j = 0; j < 8; j++) { n       if (grid[i][j] ==  W ) { n         int x = i; n         int y = j; n         int f = 1; n         while (x >= 0) { n           if (grid[x][j] ==  B ) { n             f = 0; n             break; n           } n           x--; n         } n         if (f == 1) { n           if (min >= i) { n             min = i; n             flag = 0; n           } n         } n       } n       if (grid[i][j] ==  B ) { n         int x = i; n         int y = j; n         int f = 1; n         while (x < 8) { n           if (grid[x][j] ==  W ) { n             f = 0; n             break; n           } n           x++; n         } n         if (f == 1) { n           if (min > (7 - i)) { n             min = 7 - i; n             flag = 1; n           } n         } n       } n     } n   } n   if (flag == 0) n     cout <<  A ; n   else n     cout <<  B ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n void coder_RB6() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n bool comp(long long int a, long long int b) { return (a > b); } n int32_t main() { n   coder_RB6(); n   long long int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     vector<long long int> a; n     for (long long int i = 0; i < n; i++) { n       long long int x; n       cin >> x; n       a.push_back(x); n     } n     long long int flag = 0; n     long long int index; n     for (long long int i = 1; i < n - 1; i++) { n       if (a[i] > a[i - 1] && a[i] > a[i + 1]) { n         flag = 1; n         index = i; n         break; n       } n     } n     if (flag == 1) { n       cout <<  YES  << endl; n       cout << index <<     << index + 1 <<     << index + 2 << endl; n     } else { n       cout <<  NO  << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, f, t; n const int N = 1e3 + 5; n vector<int> adj[N]; n int col[N], vis[N], row[N]; n int dj[N][N]; n int mn = -1e9, id = 1; n void dfs(int u, int ret) { n   if (u == n + 1) { n     ret = 0; n     for (int i = 1; i <= 6; i++) { n       for (int j = i; j <= 6; j++) { n         dj[i][j] = 0; n       } n     } n     for (int i = 1; i <= n; i++) { n       for (auto j : adj[i]) { n         int c = col[i]; n         int k = col[j]; n         if (!dj[c][k] && c <= k) { n           ret++; n           dj[c][k] = 1; n         } n       } n     } n     mn = max(mn, ret); n     return; n   } n   for (int i = 1; i <= 6; i++) { n     if (!col[u]) { n       col[u] = i; n       dfs(u + 1, 0); n       col[u] = 0; n     } n   } n } n int main() { n   cin >> n >> m; n   while (m--) { n     cin >> f >> t; n     adj[f].push_back(t); n     adj[t].push_back(f); n   } n   dfs(1, 1); n   cout << mn <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int dot[510]; n inline long double max(long double a, long double b) { return a > b ? a : b; } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , dot + i); n   } n   double ans = 0.0; n   for (int i = 1; i <= m; i++) { n     int x, y, z; n     scanf( %d%d%d , &x, &y, &z); n     if (z > 0) { n       ans = max(ans, (long double)(dot[x] + dot[y]) / z); n     } n   } n   cout << setiosflags(ios::fixed) << setprecision(10) << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n const long long int BOUND = 1.5e6; n const long long int SQ_BOUND = BOUND * BOUND; n vector<complex<long long int> > arr; n long long int s[N]; n inline long long int norm_sq(const complex<long long int> &z) { n   return (z * conj(z)).real(); n } n long long int greedy(long long int sgn) { n   complex<long long int> o{0, 0}; n   reverse((arr).begin(), (arr).end()); n   o = (s[0] = sgn) * arr[0]; n   for (int i = 1; i < (int)arr.size(); ++i) { n     complex<long long int> po = o + arr[i]; n     complex<long long int> ne = o - arr[i]; n     if (norm_sq(po) <= norm_sq(ne)) { n       s[i] = 1; n       o = po; n     } else { n       s[i] = -1; n       o = ne; n     } n   } n   reverse((arr).begin(), (arr).end()); n   return norm_sq(o); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   srand(time(NULL)); n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     int x, y; n     cin >> x >> y; n     arr.emplace_back(x, y); n   } n   long long int cur, sgn = 1; n   do { n     cur = greedy(sgn); n     if (!~sgn) random_shuffle((arr).begin(), (arr).end()); n     sgn = -sgn; n   } while (cur > SQ_BOUND); n   for (int i = n - 1; i >= 0; --i) cout << s[i] <<    n [i + 1 == n]; n   return 0; n } n


n //  Say:He is the Most Merciful,We have believed in him and upon him we have relied  [67:29] n   n //#pragma GCC optimize ( Ofast ) n //#pragma GCC target ( avx2 ) n //#pragma GCC optimize( unroll-loops ) n   n #include<bits/stdc++.h> n using namespace std; n                                                       n /* n #include <ext/pb_ds/assoc_container.hpp> n #include <ext/pb_ds/tree_policy.hpp> n using namespace __gnu_pbds; n template<typename T> n using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; n  */ n   n typedef long long ll; n typedef long double ld; n typedef pair<ll,ll> pll; n typedef pair<int,int> pii; n typedef vector<ll> vll; n typedef vector<int> vii; n typedef map<int,int> mpi; n typedef map<ll,ll> mpl; n typedef unordered_map<int,int> umpi; n typedef unordered_map<ll,ll> umpl; n   n #define ump unordered_map n #define mod 1000000007 n #define inf 1000000000000000006 n #define infi 2000000009 n #define ff first n #define ss second n #define pb push_back n #define all(v) v.begin(), v.end() n #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) n #define endl   n  n #define pi acos(-1.0) n #define dec(n) fixed << setprecision(n) n #define N 300005 n //#define int long long n  n inline void IO(string name) { // Usaco n      freopen((name +  .in ).c_str(),  r , stdin); n      freopen((name +  .out ).c_str(),  w , stdout); n } n  n ll n,a[N],t[N],k,d[N]; n vector<pii> v; n void solve(){ n  cin >> n >> k; n  for(int i=0;i<k;i++) cin >> a[i]; n  for(int i=0;i<k;i++) cin >> t[i]; n  priority_queue<pii> pq; n  for(int i=1;i<=n;i++) d[i]=infi; n  for(int i=0;i<k;i++){ n   pq.push({-t[i],a[i]}); n   d[a[i]]=t[i]; n  } n  //int cc=0; n  while(!pq.empty()){ n   //cc++; n   int u=pq.top().ss,c=-pq.top().ff; n   pq.pop(); n   if(u-1!=0 && d[u-1]>c+1){ n    d[u-1]=c+1; n    pq.push({-d[u-1],u-1}); n   } n   if(u+1!=n+1 && d[u+1]>c+1){ n    d[u+1]=c+1; n    pq.push({-d[u+1],u+1}); n   } n  } n  for(int i=1;i<=n;i++) cout << d[i] <<    ; n  cout << endl; n  //cout << cc << endl; n   n } n  n signed main(){ n     fastio; n     //srand(chrono::steady_clock::now().time_since_epoch().count()); n     int T=1,cs=0; n     cin >> T; n     while(T--){ n         //cout <<  Case   << ++cs <<  :  <<     ;  n      solve(); n     }  n } n


#include <bits/stdc++.h> n using namespace std; n int num[100 + 5][100 + 5], ans[100 + 5][100 + 5]; n int row[100 + 5], col[100 + 5]; n int main() { n   int n, m; n   while (scanf( %d%d , &m, &n) != EOF) { n     int i, j, k; n     int ii, jj, kk; n     memset(row, 0, sizeof(row)); n     memset(col, 0, sizeof(col)); n     for (i = 0; i < m; i++) { n       for (j = 0; j < n; j++) { n         scanf( %d , &num[i][j]); n         row[i] += num[i][j]; n         col[j] += num[i][j]; n       } n     } n     for (i = 0; i < m; i++) { n       for (j = 0; j < n; j++) { n         ans[i][j] = 1; n       } n     } n     for (i = 0; i < m; i++) { n       for (j = 0; j < n; j++) { n         if (num[i][j] == 0) { n           for (ii = 0; ii < m; ii++) ans[ii][j] = 0; n           for (jj = 0; jj < n; jj++) ans[i][jj] = 0; n         } n       } n     } n     bool ok = true; n     for (i = 0; i < m; i++) { n       for (j = 0; j < n; j++) { n         int tp = 0; n         for (ii = 0; ii < m; ii++) tp |= ans[ii][j]; n         for (jj = 0; jj < n; jj++) tp |= ans[i][jj]; n         if (tp != num[i][j]) { n           ok = false; n           break; n         } n       } n       if (!ok) break; n     } n     if (ok) { n       printf( YES n ); n       for (i = 0; i < m; i++) { n         for (j = 0; j < n; j++) { n           if (j) printf(   ); n           printf( %d , ans[i][j]); n         } n         printf(  n ); n       } n     } else n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 501; n int n, m, a[N]; n bool dp[2][N][N]; n vector<int> sol; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; ++i) scanf( %d , &a[i]); n   dp[0][0][0] = dp[0][a[0]][0] = dp[0][a[0]][a[0]] = true; n   int at = 0; n   for (int i = 1; i < n; ++i) { n     at ^= 1; n     for (int j = 0; j <= m; ++j) n       for (int k = 0; k <= m; ++k) { n         dp[at][j][k] = dp[at ^ 1][j][k]; n         if (a[i] <= j) dp[at][j][k] |= dp[at ^ 1][j - a[i]][k]; n         if (a[i] <= j && a[i] <= k && k <= j) n           dp[at][j][k] |= dp[at ^ 1][j - a[i]][k - a[i]]; n       } n   } n   for (int i = 0; i <= m; ++i) n     if (dp[at][m][i]) sol.push_back(i); n   printf( %d n , (int)sol.size()); n   for (int i = 0; i < (int)sol.size(); ++i) n     printf( %s%d , i ?     :   , sol[i]); n   puts(  ); n   return 0; n } n