#include <bits/stdc++.h> n using namespace std; n void PLAY() { n   cout << fixed << setprecision(10); n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n int max(int a, int b) { n   if (a > b) return a; n   return b; n } n int min(int a, int b) { n   if (a < b) return a; n   return b; n } n long long fx4[] = {-1, 1, 0, 0}; n long long fy4[] = {0, 0, -1, 1}; n long long fx8[] = {-1, -1, -1, 0, 1, 1, 1, 0}; n long long fy8[] = {-1, 0, 1, 1, 1, 0, -1, -1}; n const double PI = 3.141592653589793; n const long long N = 2e5 + 5; n const long long mod = 1000000007; n long long m, n; n long long ar[N]; n vector<long long> arr; n long long dp[5005][5005]; n long long func(long long l, long long r) { n   if (l == r) n     return 0; n   else if (l + 1 == r) n     return 1; n   long long &ans = dp[l][r]; n   if (ans != -1) return ans; n   ans = 0; n   long long op1 = INT_MAX; n   long long op2 = INT_MAX; n   long long op3 = INT_MAX; n   if (arr[l] == arr[r]) n     op1 = 1 + func(l + 1, r - 1); n   else { n     op2 = 1 + func(l, r - 1); n     op3 = 1 + func(l + 1, r); n   } n   ans = min(op1, min(op2, op3)); n   return ans; n } n int32_t main() { n   PLAY(); n   cin >> m; n   for (long long i = 0; i < m; i++) cin >> ar[i]; n   arr.push_back(ar[0]); n   for (long long i = 1; i < m;) { n     while (ar[i] == ar[i - 1]) i++; n     if (i < m) arr.push_back(ar[i]); n     i++; n   } n   memset(dp, -1, sizeof(dp)); n   n = arr.size(); n   cout << func(0, n - 1) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n void _dbg(string) { cout << endl; } n template <class H, class... T> n void _dbg(string s, H h, T... t) { n   int l = s.find( , ); n   cout << s.substr(0, l) <<   =   << h <<  ,  ; n   _dbg(s.substr(l + 1), t...); n } n template <class T, class U> n ostream& operator<<(ostream& o, const pair<T, U>& p) { n   o <<  (  << p.first <<  ,  << p.second <<  ) ; n   return o; n } n template <class T> n ostream& operator<<(ostream& o, const vector<T>& v) { n   o <<  [ ; n   for (T t : v) { n     o << t <<  , ; n   } n   o <<  ] ; n   return o; n } n const int N = 100000; n int n, m; n bool used[N + 5]; n char s[N + 5]; n int hoge(char c) { n   int crnt = 0; n   int last = -1; n   int ret = 0; n   for (int i = (int)(0); i < (int)(n); i++) { n     crnt++; n     if (s[i] == c) last = i; n     if (used[i]) crnt = 0; n     if (crnt == m) { n       if (i - last >= m) return -1; n       crnt = i - last; n       used[last] = true; n       ret++; n     } n   } n   return ret; n } n int main() { n   cin >> m >> s; n   n = strlen(s); n   for (int i = (int)(0); i < (int)(26); i++) { n     char c = i +  a ; n     int r = hoge(c); n     if (r == -1) { n       r = 0; n       for (int j = (int)(0); j < (int)(n); j++) n         if (s[j] == c) { n           r++; n           used[j] = true; n         } n     } else { n       i = 100; n     } n     for (int j = (int)(0); j < (int)(r); j++) printf( %c , c); n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void c_p_c() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n bool isPrime(long long n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (long long i = 5; i * i <= n; i += 6) { n     if (n % i == 0 || n % (i + 2) == 0) return false; n   } n   return true; n } n vector<bool> Sieve(long long n) { n   bool prime[n + 1]; n   memset(prime, true, sizeof(prime)); n   for (long long i = 2; i * i <= n; i++) { n     if (prime[i]) { n       for (long long j = i * i; j <= n; j += i) { n         prime[j] = false; n       } n     } n   } n   vector<bool> primes(n + 1); n   primes[0] = false; n   primes[1] = false; n   for (long long i = 2; i <= n; i++) { n     if (prime[i]) primes[i] = true; n   } n   return primes; n } n set<long long> primeFactors(long long n) { n   set<long long> factors; n   while (n % 2 == 0) { n     n /= 2; n     factors.insert(2); n   } n   for (long long i = 3; i <= sqrt(n); i += 2) { n     while (n % i == 0) { n       factors.insert(i); n       n /= i; n     } n   } n   if (n > 2) factors.insert(n); n   return factors; n } n bool cmp(string a, string b) { return a.length() < b.length(); } n int32_t main() { n   c_p_c(); n   long long n, k; n   cin >> n >> k; n   for (long long i = n; i >= n - k + 1; i--) cout << i <<    ; n   for (long long i = 1; i <= n - k; i++) cout << i <<    ; n   cout <<   n ; n } n


#include <bits/stdc++.h> n int main() { n   char s[105]; n   int i, n, j; n   scanf( %d , &n); n   getchar(); n   for (i = 0; i < n; i++) { n     scanf( %c , &s[i]); n   } n   for (i = 0; i < n; i++) { n     if (s[i] ==  o  && s[i + 1] ==  g  && s[i + 2] ==  o ) { n       printf( *** ); n       for (j = i + 3; j < n; j += 2) { n         if (s[j] ==  g  && s[j + 1] ==  o ) { n           continue; n         } else { n           break; n         } n       } n       i = j - 1; n     } else n       printf( %c , s[i]); n   } n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long K; n vector<int> E[200000 + 5]; n void addEdge(int u, int v) { n   E[u].push_back(v); n   E[v].push_back(u); n } n int sz[200000 + 5], deep[200000 + 5], fa[200000 + 5]; n int msz[200000 + 5], root; n void dfs1(int x, int f) { n   sz[x] = 1; n   msz[x] = 0; n   for (int y : E[x]) { n     if (y != f) { n       dfs1(y, x); n       sz[x] += sz[y]; n       msz[x] = max(msz[x], sz[y]); n     } n   } n   msz[x] = max(msz[x], n - sz[x]); n   if (root == 0 || msz[x] < msz[root]) root = x; n } n set<pair<int, int> > S[200000 + 5]; n long long minans = 0, maxans = 0; n int sons[200000 + 5]; n void dfs2(int x, int f, int rt) { n   sz[x] = 1; n   fa[x] = f; n   deep[x] = deep[f] + 1; n   for (int y : E[x]) { n     if (y != f) { n       sons[x]++; n       dfs2(y, x, rt); n       sz[x] += sz[y]; n     } n   } n   if (sz[x] > 1) S[rt].insert(make_pair(deep[x], x)); n   minans += sz[x] % 2; n   maxans += sz[x]; n } n bool vis[200000 + 5]; n int getSon(int x) { n   for (int t : E[x]) { n     if (t != fa[x] && !vis[t]) { n       vis[t] = 1; n       return t; n     } n   } n   return x; n } n void del(int x, int rt) { n   vis[x] = 1; n   sons[fa[x]]--; n   if (sons[fa[x]] == 0) S[rt].erase(make_pair(deep[fa[x]], fa[x])); n } n priority_queue<pair<int, int> > q; n vector<int> ans; n void dfs3(int x) { n   if (!vis[x]) ans.push_back(x); n   for (int y : E[x]) n     if (y != fa[x]) dfs3(y); n } n int main() { n   scanf( %d %lld , &n, &K); n   for (int i = 1, u, v; i < n; i++) { n     scanf( %d %d , &u, &v); n     addEdge(u, v); n   } n   dfs1(1, 0); n   for (int y : E[root]) { n     dfs2(y, root, y); n     q.push(make_pair(sz[y], y)); n   } n   if (K >= minans && K <= maxans && (maxans - K) % 2 == 0) { n     puts( YES ); n     K = (maxans - K) / 2; n     while (K) { n       int rt = q.top().second; n       q.pop(); n       int lc = S[rt].rbegin()->second; n       int d = deep[lc]; n       if (K >= d) { n         K -= d; n         int u = getSon(lc), v = getSon(lc); n         del(u, rt); n         del(v, rt); n         printf( %d %d n , u, v); n       } else { n         int u = S[rt].lower_bound(make_pair(K, 0))->second; n         int v = getSon(u); n         del(u, rt); n         del(v, rt); n         printf( %d %d n , u, v); n         break; n       } n       sz[rt] -= 2; n       if (sz[rt] > 1) q.push(make_pair(sz[rt], rt)); n     } n     dfs3(root); n     for (int i = 0; i < (int)ans.size() / 2; i++) n       printf( %d %d n , ans[i], ans[i + (int)ans.size() / 2]); n   } else n     puts( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200000 + 5, inf = 2e9; n const long double EPS = 1e-9L; n vector<pair<int, int> > pairs[26]; n vector<int> mn[26]; n int leftSub[N], rightSub[N]; n void computeSub(string& a, string& b, int ans[]) { n   for (int i = 0; i < (int)(a).size(); ++i) { n     int prv = 0; n     if (i != 0) prv = ans[i - 1]; n     if (prv == (int)(b).size() || a[i] != b[prv]) n       ans[i] = ans[i - 1]; n     else n       ans[i] = 1 + ans[i - 1]; n   } n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   string s, t; n   cin >> s >> t; n   for (int i = 0; i < (int)(t).size(); ++i) { n     int l = i, r = (int)(t).size() - 1 - i; n     pairs[t[i] -  a ].push_back({l, r}); n   } n   for (int c = 0; c < 26; ++c) { n     sort(pairs[c].begin(), pairs[c].end()); n     for (int i = 0; i < (int)(pairs[c]).size(); ++i) { n       if (i == 0) n         mn[c].push_back(pairs[c][i].second); n       else n         mn[c].push_back(min(pairs[c][i].second, mn[c][i - 1])); n     } n   } n   computeSub(s, t, leftSub); n   reverse(s.begin(), s.end()), reverse(t.begin(), t.end()); n   computeSub(s, t, rightSub); n   reverse(s.begin(), s.end()), reverse(rightSub, rightSub + (int)(s).size()); n   for (int i = 0; i < (int)(s).size(); ++i) { n     int a = 0, b = 0; n     if (i != 0) a = leftSub[i - 1]; n     if (i != (int)(s).size() - 1) b = rightSub[i + 1]; n     pair<int, int> p = {a, inf}; n     if (pairs[s[i] -  a ].empty() || pairs[s[i] -  a ][0] > p) { n       cout <<  No n ; n       return 0; n     } n     int ind = n         upper_bound(pairs[s[i] -  a ].begin(), pairs[s[i] -  a ].end(), p) - n         begin(pairs[s[i] -  a ]); n     if (mn[s[i] -  a ][ind - 1] > b) { n       cout <<  No n ; n       return 0; n     } n   } n   cout <<  Yes n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n int T, n, k; n long long ans, p; n int main() { n   scanf( %d , &T); n   while (T--) { n     scanf( %d%d , &n, &k); n     ans = 0; n     p = 1; n     for (int i = 0; i <= 31; i++) { n       if (((long long)(1 << i) & k) != 0) ans = (ans + p) % MOD; n       p = p * n % MOD; n     } n     printf( %lld n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T gcd(T a, T b) { n   return b ? gcd(b, a % b) : a; n } n template <typename T> n inline T lowbit(T x) { n   return x & -x; n } n const int N = 2e5 + 10; n int n, m; n int q; n int res; n int les[N], more[N]; n bool vis[N]; n void solve() { n   cin >> n >> m; n   while (m--) { n     int u, v; n     scanf( %d%d , &u, &v); n     if (u > v) n       les[u]++, more[v]++; n     else n       les[v]++, more[u]++; n   } n   for (int i = 1; i <= n; ++i) { n     if (!more[i]) res++, vis[i] = 1; n   } n   cin >> q; n   while (q--) { n     int x; n     scanf( %d , &x); n     int a, b; n     if (x == 1) { n       scanf( %d%d , &a, &b); n       if (a > b) n         les[a]++, more[b]++; n       else n         les[b]++, more[a]++; n       if (vis[a] && more[a]) vis[a] = 0, res--; n       if (vis[b] && more[b]) vis[b] = 0, res--; n     } n     if (x == 2) { n       scanf( %d%d , &a, &b); n       if (a > b) n         les[a]--, more[b]--; n       else n         les[b]--, more[a]--; n       if (!vis[a] && !more[a]) res++, vis[a] = 1; n       if (!vis[b] && !more[b]) res++, vis[b] = 1; n     } n     if (x == 3) { n       cout << res << endl; n     } n   } n } n signed main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int nmax = 100005; n const long long inf = 1e9; n const double eps = 1e-6; n long long dp[1005][1005]; n long long gp[1005][1005]; n long long a[1005][1005]; n long long rdp[1005][1005]; n long long rgp[1005][1005]; n int main() { n   ios_base::sync_with_stdio(0); n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       cin >> a[i][j]; n     } n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + a[i][j]; n     } n   } n   for (int i = n; i; --i) { n     for (int j = m; j; --j) { n       rdp[i][j] = max(rdp[i][j + 1], rdp[i + 1][j]) + a[i][j]; n     } n   } n   for (int i = n; i; --i) { n     for (int j = 1; j <= m; ++j) { n       gp[i][j] = max(gp[i + 1][j], gp[i][j - 1]) + a[i][j]; n     } n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = m; j; --j) { n       rgp[i][j] = max(rgp[i - 1][j], rgp[i][j + 1]) + a[i][j]; n     } n   } n   long long ans = 0; n   for (int i = 2; i < n; ++i) { n     for (int j = 2; j < m; ++j) { n       ans = n           max(ans, dp[i][j - 1] + rdp[i][j + 1] + gp[i + 1][j] + rgp[i - 1][j]); n       ans = n           max(ans, dp[i - 1][j] + rdp[i + 1][j] + gp[i][j - 1] + rgp[i][j + 1]); n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 5e5 + 5; n template <typename T> n void chkmax(T& x, T y) { n   if (y > x) x = y; n } n template <typename T> n void chkmin(T& x, T y) { n   if (y < x) x = y; n } n int N, c[MAXN]; n long long ans; n int main() { n   scanf( %d , &N); n   for (int i = 1; i <= N; ++i) scanf( %d , &c[i]); n   for (int i = 1; i <= N; i += 2) { n     long long minv = 0, sum = 0; n     for (int j = i + 1; j <= N; ++j) { n       long long l = -sum - c[i], r = -sum - max(1ll, -minv); n       if (j % 2 == 1) n         chkmax(l, 1ll), chkmin(r, (long long)c[j]); n       else n         chkmax(l, (long long)-c[j]), chkmin(r, -1ll); n       ans += max(0ll, r - l + 1); n       sum += j % 2 == 1 ? c[j] : -c[j]; n       chkmin(minv, sum); n     } n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n const int B = 250; n int p1 = 5000000, p2 = 0; n char buf[5000005], wbuf[5000005]; n int gc() { n   if (p1 >= 5000000) fread(buf, 1, 5000000, stdin), p1 = 0; n   return buf[p1++]; n } n int rd() { n   int x = 0, ch = gc(); n   while (!isdigit(ch)) ch = gc(); n   while (isdigit(ch)) x = x * 10 + ch -  0 , ch = gc(); n   return x; n } n struct T { n   int x, id; n } tmp[100005]; n int bx[100005], bid[100005], cx[100005], cid[100005], nowans[100005]; n int a[100005], pre[100005], bel[100005], S[100005], E[100005], dfn[100005], n     tag[100005]; n int size[100005], son[100005], n, m, fa[100005], is[100005], top[100005], sign, n     sum = 1, ts = 0; n vector<int> g[100005]; n const bool operator<(const T i, const T j) { return i.x < j.x; } n void dfs(int x) { n   size[x] = 1; n   for (int y : g[x]) { n     dfs(y), size[x] += size[y]; n     if (size[y] > size[son[x]]) son[x] = y; n   } n } n void dfs2(int x, int tp) { n   top[x] = tp, dfn[x] = ++sign; n   if (son[x]) n     dfs2(son[x], tp); n   else { n     int l = dfn[top[x]], r = dfn[x], t = 100; n     if (r - l < 10) { n       for (int i = l; i <= r; i++) bel[i] = sum; n       if (r - ts >= 250) sum++, ts = r; n       return; n     } n     if (sqrt(r - l + 1) >= t) t = sqrt(r - l + 1), t = min(t, 250); n     for (int i = l; i <= r; i++) bel[i] = (i - l) / t + sum; n     sum = bel[r] + 1, ts = r; n   } n   for (int y : g[x]) n     if (y ^ son[x]) dfs2(y, y); n } n void Make(int x) { n   for (int s = S[bel[x]], e = E[bel[x]], i = s; i <= e; i++) { n     if (bid[i] == x) { n       int dlt = 1 - 2 * is[x]; n       while (i <= e) pre[i] += dlt, i++; n     } n   } n   is[x] ^= 1; n } n void Addb(int p, int l, int r, int x) { n   int tc = 0, s = S[p], e = E[p]; n   for (int i = s; i <= e; i++) bx[i] += tag[p]; n   tag[p] = 0, nowans[p] = -1; n   int L = s, R = e; n   while (bid[L] < l || bid[L] > r) L++; n   while (bid[R] < l || bid[R] > r) R--; n   while (L > s && bx[L - 1] == bx[L]) L--; n   while (R < e && bx[R + 1] == bx[R]) R++; n   for (int i = L, j; i <= R; i = j + 1) { n     j = i; n     while (j < R && bx[j + 1] == bx[i]) j++; n     if (x == 1) { n       for (int k = i; k <= j; k++) n         if (bid[k] < l || bid[k] > r) cid[++tc] = bid[k], cx[tc] = bx[k]; n       for (int k = i; k <= j; k++) n         if (bid[k] >= l && bid[k] <= r) cid[++tc] = bid[k], cx[tc] = bx[k] + 1; n     } else { n       for (int k = i; k <= j; k++) n         if (bid[k] >= l && bid[k] <= r) cid[++tc] = bid[k], cx[tc] = bx[k] - 1; n       for (int k = i; k <= j; k++) n         if (bid[k] < l || bid[k] > r) cid[++tc] = bid[k], cx[tc] = bx[k]; n     } n   } n   for (int i = L; i <= R; i++) bx[i] = cx[i - L + 1], bid[i] = cid[i - L + 1]; n   for (int i = L; i <= e; i++) pre[i] = (i == s ? 0 : pre[i - 1]) + is[bid[i]]; n } n void Add(int l, int r, int x) { n   Addb(bel[l], l, r, x); n   if (bel[l] == bel[r]) return; n   Addb(bel[r], l, r, x); n   for (int i = bel[l] + 1; i < bel[r]; i++) tag[i] += x, nowans[i] = -1; n } n int Query() { n   int ans = 0; n   for (int i = 1; i <= bel[n]; i++) { n     if (nowans[i] != -1) { n       ans += nowans[i]; n       continue; n     } n     int pos = lower_bound(bx + S[i], bx + E[i] + 1, -tag[i]) - bx - 1; n     if (pos >= S[i]) n       ans += pre[pos], nowans[i] = pre[pos]; n     else n       nowans[i] = 0; n   } n   return ans; n } n void Upd(int x) { n   int p = dfn[x]; n   while (x) Add(dfn[top[x]], dfn[x], 1 - 2 * is[p]), x = fa[top[x]]; n   Make(p); n } n int main() { n   n = rd(), m = rd(); n   for (int i = 2, x; i <= n; i++) x = rd(), fa[i] = x, g[x].push_back(i); n   dfs(1), dfs2(1, 1); n   for (int i = 1; i <= n; i++) n     a[i] = rd(), is[i] = 1, tmp[dfn[i]] = {a[i], dfn[i]}; n   for (int i = 1; i <= n; i++) E[bel[i]] = i; n   for (int i = n; i >= 1; i--) S[bel[i]] = i; n   for (int i = 1; i <= bel[n]; i++) { n     nowans[i] = -1; n     int s = S[i], e = E[i]; n     sort(tmp + s, tmp + e + 1); n     for (int j = s; j <= e; j++) n       pre[j] = j - s + 1, bx[j] = tmp[j].x, bid[j] = tmp[j].id; n   } n   while (m--) { n     int x = rd(); n     Upd(x); n     printf( %d  , Query()); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10; n char s1[N], s2[N], s[N]; n int n, lst, nod, len[N], fail[N], diff[N], anc[N], ch[N][26]; n int f[N], pos[N], pre[N]; n void init() { n   fail[0] = fail[1] = nod = 1; n   len[1] = -1; n } n void extend(int c, int i) { n   int p = lst; n   while (s[i - len[p] - 1] != s[i]) p = fail[p]; n   if (!ch[p][c]) { n     int x = ++nod, k = fail[p]; n     while (s[i - len[k] - 1] != s[i]) k = fail[k]; n     fail[x] = ch[k][c]; n     ch[p][c] = x; n     len[x] = len[p] + 2; n     diff[x] = len[x] - len[fail[x]]; n     anc[x] = (diff[x] == diff[fail[x]]) ? anc[fail[x]] : fail[x]; n   } n   lst = ch[p][c]; n } n int main() { n   scanf( %s%s , s1 + 1, s2 + 1); n   n = strlen(s1 + 1); n   init(); n   for (int i = 1; i <= n * 2; i++) s[i] = (i & 1) ? s1[(i + 1) / 2] : s2[i / 2]; n   for (int i = 1; i <= n * 2; i++) f[i] = 1e9; n   for (int i = 1; i <= n * 2; i++) { n     extend(s[i] -  a , i); n     for (int k = lst; k; k = anc[k]) { n       pos[k] = i - len[anc[k]] - diff[k]; n       if (anc[k] != fail[k] && f[pos[fail[k]]] < f[pos[k]]) n         pos[k] = pos[fail[k]]; n       if (!(i & 1) && f[i] > f[pos[k]] + 1) n         f[i] = f[pos[k]] + 1, pre[i] = pos[k]; n     } n     if (!(i & 1) && s[i] == s[i - 1] && f[i - 2] <= f[i]) n       f[i] = f[i - 2], pre[i] = i - 2; n   } n   if (f[n * 2] >= 1e9) return puts( -1 ), 0; n   printf( %d n , f[n * 2]); n   for (int i = n * 2; i; i = pre[i]) n     if (i - pre[i] > 2) printf( %d %d n , pre[i] / 2 + 1, i >> 1); n } n


#include <bits/stdc++.h> n using namespace std; n int grid[33][33]; n int main() { n   int test; n   scanf( %d , &test); n   for (int _T = 1; _T <= test; _T++) { n     int n, m; n     cin >> n >> m; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         cin >> grid[i][j]; n       } n     } n     int ans = 0; n     for (int dist = 0; dist < (n + m - 1) / 2; dist++) { n       int cnt1 = 0, cnt2 = 0; n       for (int i = 0; i < n; i++) { n         for (int j = 0; j < m; j++) { n           if (i + j == dist) { n             cnt1 += (grid[i][j] == 0); n             cnt2 += (grid[i][j] == 1); n           } n         } n       } n       for (int i = 0; i < n; i++) { n         for (int j = 0; j < m; j++) { n           if ((n - 1 - i) + (m - 1 - j) == dist) { n             cnt1 += (grid[i][j] == 0); n             cnt2 += (grid[i][j] == 1); n           } n         } n       } n       ans += min(cnt1, cnt2); n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, ave1, ave2, ans1, ans2; n int a[567890], sum; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]), sum += a[i]; n   sort(a + 1, a + n + 1); n   int ave1 = sum / n; n   if (ave1 * n == sum) n     ave2 = ave1; n   else n     ave2 = ave1 + 1; n   for (int i = 1; i <= n; i++) { n     if (a[i] < ave1) ans1 += (ave1 - a[i]); n     if (a[i] > ave2) ans2 += (a[i] - ave2); n   } n   printf( %d , ans1 > ans2 ? ans1 : ans2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int GCD(int a, int b) { n   if (b == 0) return a; n   return (a % b == 0 ? b : GCD(b, a % b)); n } n long long int POW(long long int base, long long int exp) { n   long long int val; n   val = 1; n   while (exp > 0) { n     if (exp % 2 == 1) { n       val = (val * base) % 1000000007; n     } n     base = (base * base) % 1000000007; n     exp = exp / 2; n   } n   return val; n } n vector<int> fib; n vector<int> ans; n int main() { n   int s, k, i, n; n   long long till; n   scanf( %d%d , &s, &k); n   fib.push_back(1); n   till = 1; n   for (i = 1; fib[i - 1] <= s; i++) { n     fib.push_back(till); n     if (i >= k) { n       till = till + fib[i] - fib[i - k]; n     } else { n       till = till + fib[i]; n     } n   } n   n = fib.size(); n   ans.push_back(0); n   for (i = n - 1; i >= 1; i--) { n     if (s >= fib[i]) { n       s = s - fib[i]; n       ans.push_back(fib[i]); n     } n   } n   n = ans.size(); n   printf( %d n , n); n   for (i = 0; i < n; i++) { n     printf( %d  , ans[i]); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   int c[n], a[m]; n   int i; n   for (i = 0; i < n; i++) { n     scanf( %d , &c[i]); n   } n   for (i = 0; i < m; i++) { n     scanf( %d , &a[i]); n   } n   int j = 0, count = 0; n   int t = 0; n   for (i = 0; i < m;) { n     if (j >= n) break; n     for (j = t; j < n; j++) { n       if (i >= m) { n         break; n       } n       if (a[i] >= c[j]) { n         i = i + 1; n         t = j + 1; n         count = count + 1; n       } n     } n   } n   printf( %d n , count); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010; n int c[maxn], ans[maxn]; n int k, n, maxb, t; n int b[maxn]; n void update(int x, int d) { n   for (; x <= maxb; x += x & -x) { n     if (d > c[x]) c[x] = d; n   } n } n int get(int x) { n   int ans = 0; n   for (; x > 0; x -= x & -x) n     if (c[x] > ans) ans = c[x]; n   return ans; n } n void solve() { n   if (t >= maxb) { n     sort(b, b + n); n     cout << unique(b, b + n) - b << endl; n   } else { n     int x; n     memset(c, 0, sizeof(c)); n     memset(ans, 0, sizeof(ans)); n     for (int i = 0; i < t; i++) { n       for (int j = 0; j < n; j++) { n         x = get(b[j] - 1) + 1; n         if (x > ans[j]) { n           ans[j] = x; n           update(b[j], x); n         } n       } n     } n     cout << *max_element(ans, ans + n) << endl; n   } n } n int main() { n   cin >> k >> n >> maxb >> t; n   for (int i = 0; i < k; i++) { n     for (int j = 0; j < n; j++) scanf( %d , b + j); n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 10; n int e[maxn], ne[maxn], h[maxn], idx = 0; n int sz[maxn]; n int ans = 0; n void add(int a, int b) { n   e[idx] = b, ne[idx] = h[a]; n   h[a] = idx++; n } n void dfs(int now, int fa) { n   sz[now] = 1; n   for (int i = h[now]; i != -1; i = ne[i]) { n     int j = e[i]; n     if (j == fa) continue; n     dfs(j, now); n     sz[now]++; n   } n } n int dfs2(int now, int fa) { n   int d1 = 0, d2 = 0; n   for (int i = h[now]; i != -1; i = ne[i]) { n     int j = e[i]; n     if (j == fa) continue; n     int d = dfs2(j, now); n     if (d > d1) n       d2 = d1, d1 = d; n     else if (d > d2) n       d2 = d; n   } n   if (d1 >= 1 && d2 >= 1) { n     if (now != 1) n       ans = max(ans, d1 + d2 + sz[now] - 1); n     else n       ans = max(ans, d1 + d2 + sz[now] - 2); n   } else if (d2 == 0 && d1 >= 1) { n     if (now != 1) n       ans = max(ans, d1 + sz[now]); n     else n       ans = max(ans, d1 + sz[now] - 1); n   } n   return (d1 == 0 ? 1 : d1 + sz[now] - 1); n } n int main() { n   int t; n   scanf( %d , &t); n   memset(h, -1, sizeof h); n   while (t--) { n     int n; n     scanf( %d , &n); n     idx = 0; n     ans = 0; n     for (int i = 1; i <= n - 1; i++) { n       int a, b; n       scanf( %d%d , &a, &b); n       add(a, b); n       add(b, a); n     } n     dfs(1, -1); n     dfs2(1, -1); n     cout << ans << endl; n     for (int i = 1; i <= n; i++) h[i] = -1, sz[i] = 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int sum = 0, mx = 0; n   node *left = nullptr, *right = nullptr; n   node *getLeft() { n     if (left == nullptr) left = new node; n     return left; n   } n   node *getRight() { n     if (right == nullptr) right = new node; n     return right; n   } n   void setValue(int k, int v, int a = 0, int b = 1 << 30) { n     if (b == a + 1) n       mx = sum = v; n     else if (k < (a + b) / 2) { n       getLeft()->setValue(k, v, a, (a + b) / 2); n       mx = ::max(mx, left->mx + sum); n     } else { n       getRight()->setValue(k, v, (a + b) / 2, b); n       mx = ::max(mx, right->mx + sum); n     } n   } n   void insert(int pa, int pb, int a = 0, int b = 1 << 30) { n     if (a >= pb || b <= pa) return; n     if (a >= pa && b <= pb) { n       sum++; n       mx++; n     } else { n       mx = 0; n       if (left) { n         left->insert(pa, pb, a, (a + b) / 2); n         mx = ::max(mx, left->mx); n       } n       if (right) { n         right->insert(pa, pb, (a + b) / 2, b); n         mx = ::max(mx, right->mx); n       } n       mx += sum; n     } n   } n   int max(int pa, int pb, int a = 0, int b = 1 << 30) { n     if (a >= pb || b <= pa) return 0; n     if (a >= pa && b <= pb) return mx; n     int ans = 0; n     if (left) ans = ::max(ans, left->max(pa, pb, a, (a + b) / 2)); n     if (right) ans = ::max(ans, right->max(pa, pb, (a + b) / 2, b)); n     return ans + sum; n   } n }; n int main() { n   int n, r, h, t; n   node *tree = new node; n   priority_queue<pair<int, int> > pq; n   scanf( %d %d , &n, &r); n   for (int i = 0; i < n; i++) { n     scanf( %d %d , &h, &t); n     if (t < 0) continue; n     h = max(h, 0); n     pq.push(make_pair(-t, -h)); n     pq.push(make_pair(-h, 2)); n     pq.push(make_pair(-(t + 1), 2)); n   } n   int prev = -1; n   while (!pq.empty()) { n     int a = -pq.top().second, b = -pq.top().first; n     pq.pop(); n     if (a < 0) { n       if (b == prev) continue; n       int v = tree->max(0, b - r + 1); n       if (a == -2 || v > tree->max(prev, prev + 1)) { n         tree->setValue(b, v); n         prev = b; n         if (b + r <= 1000000000) pq.push(make_pair(-(b + r), 1)); n       } n     } else n       tree->insert(a, b + 1); n     if (tree->mx > n) puts( wtf ); n   } n   printf( %d n , tree->mx); n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000000; n const double eps = 0.01; n void solution() { n   int n; n   cin >> n; n   vector<int> h(n + 1); n   map<pair<int, int>, int> data; n   map<pair<int, int>, int>::iterator iter; n   for (int i = 1; i <= n; i++) { n     cin >> h[i]; n     data[make_pair(h[i], i)]++; n   } n   vector<pair<int, int> > coll; n   int place = 0; n   for (iter = data.begin(); iter != data.end(); iter++) { n     for (int j = 0; j < iter->second; j++) { n       place++; n       coll.push_back(make_pair(place, iter->first.second)); n     } n   } n   for (int i = 0; i < n; i++) { n     if (coll[i].first > coll[i].second) { n       swap(coll[i].first, coll[i].second); n     } n   } n   sort(coll.begin(), coll.end()); n   int L = coll[0].first; n   int R = coll[0].second; n   int answer = 0; n   for (int i = 1; i < n; i++) { n     int l2 = coll[i].first; n     int r2 = coll[i].second; n     if (l2 <= R) { n       R = max(R, r2); n     } else { n       L = l2; n       R = r2; n       answer++; n     } n   } n   cout << answer + 1 <<   n ; n } n void include_file() { n   freopen( input.txt ,  r , stdin); n   freopen( output.txt ,  w , stdout); n } n int main() { n   solution(); n   return 0; n } n


#include <bits/stdc++.h> n int grid[1001][1001]; n int start[100000]; n int main(void) { n   int n, m, k; n   scanf( %d %d %d , &n, &m, &k); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       scanf( %d , &grid[i][j]); n     } n   } n   for (int i = 0; i < k; i++) { n     scanf( %d , &start[i]); n   } n   for (int i = 0; i < k; i++) { n     int r = 1; n     int c = start[i]; n     while (r <= n) { n       int rr, cc; n       switch (grid[r][c]) { n         case 1: n           rr = r; n           cc = c + 1; n           break; n         case 2: n           rr = r + 1; n           cc = c; n           break; n         case 3: n           rr = r; n           cc = c - 1; n           break; n       } n       grid[r][c] = 2; n       r = rr; n       c = cc; n     } n     printf( %d  , c); n   } n } n


#include <bits/stdc++.h> n using namespace std; n static const double EPS = 1e-10; n const int MODULO = 1000000007; n typedef struct { n   int i, m, v; n } P; n vector<P> mv; n bool mLt(const P& x, const P& y) { n   return (x.m != y.m) ? less<int>()(x.m, y.m) : less<int>()(x.v, y.v); n } n int n, k, h; n bool C(double t) { n   int i = 1; n   for (__typeof__((mv).begin()) it = (mv).begin(); it != (mv).end(); ++it) { n     if ((double)i * h <= (*it).v * t) i++; n     if (i > k) break; n   } n   return i > k; n } n int main() { n   cin >> n >> k >> h; n   mv.resize(n); n   for (int i = 0; i < n; i++) mv[i].i = i + 1; n   for (int i = 0; i < n; i++) cin >> mv[i].m; n   for (int i = 0; i < n; i++) cin >> mv[i].v; n   sort(mv.begin(), mv.end(), mLt); n   double l = 0, r = 1e10; n   double t; n   for (int i = 0; i < 100; i++) { n     t = (l + r) / 2; n     if (C(t)) n       r = t; n     else n       l = t; n   } n   int cnt = 1; n   for (__typeof__((mv).begin()) it = (mv).begin(); it != (mv).end(); ++it) { n     if ((double)cnt * h <= (*it).v * t * (1 + EPS)) { n       printf( %d  , (*it).i); n       cnt++; n     } n     if (cnt > k) break; n   } n   puts(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int s[10]; n struct node { n   int l, r, now, h, a; n } f[10][10]; n int i, j, k, l, n, m, x, y, h, a, ans = 100000, ans1; n inline void work(int o) { n   if (o != n && s[n] > ans) return; n   int gt = 0; n   if (o == 1) { n     if (s[o] + s[n] == 0 && s[o] <= 0) { n       int gtw = 0; n       if (s[n] < ans) { n         for (int i = 1; i <= n; i++) n           for (int j = i + 1; j <= n; j++) n             gtw = n                 gtw + f[i][j].now * f[i][j].now + (f[i][j].now > 0) * f[i][j].a; n         ans1 = gtw; n         ans = s[n]; n       } else if (s[n] == ans) { n         for (int i = 1; i <= n; i++) n           for (int j = i + 1; j <= n; j++) n             gtw = n                 gtw + f[i][j].now * f[i][j].now + (f[i][j].now > 0) * f[i][j].a; n         ans1 = max(ans1, gtw); n       } n     } n     return; n   } n   for (int i = 1; i < o; i++) { n     if (s[o] > 0) { n       if (o == 2) { n         for (int gt = 0; gt <= s[o]; gt++) { n           if (gt > f[1][o].now - f[1][o].l) break; n           s[o] = s[o] - gt; n           s[1] = s[1] + gt; n           f[1][o].now -= gt; n           if (s[o] == 0 || o == n) work(o - 1); n           s[o] += gt; n           f[1][o].now += gt; n           s[1] -= gt; n         } n       } else if (o == 3) { n         for (int j = 0; j <= s[o]; j++) { n           if (j > f[1][o].now - f[1][o].l) break; n           for (int gt = 0; gt <= s[o] - j; gt++) { n             if (gt > f[2][o].now - f[2][o].l) break; n             s[o] = s[o] - j - gt; n             s[1] = s[1] + j; n             s[2] = s[2] + gt; n             f[1][o].now -= j; n             f[2][o].now -= gt; n             if (s[o] == 0 || o == n) work(o - 1); n             s[o] = s[o] + gt + j; n             s[1] -= j; n             s[2] -= gt; n             f[1][o].now += j; n             f[2][o].now += gt; n           } n         } n       } else if (o == 4) { n         for (int j = 0; j <= s[o]; j++) { n           if (j > f[1][o].now - f[1][o].l) break; n           for (int k = 0; k <= s[o] - j; k++) { n             if (k > f[2][o].now - f[2][o].l) break; n             for (int gt = 0; gt <= s[o] - j - k; gt++) { n               if (gt > f[3][o].now - f[3][o].l) break; n               s[o] = s[o] - j - k - gt; n               s[1] += j; n               s[2] += k; n               s[3] += gt; n               f[1][o].now -= j; n               f[2][o].now -= k; n               f[3][o].now -= gt; n               if (s[o] == 0 || o == n) work(o - 1); n               s[o] = s[o] + j + k + gt; n               s[1] -= j; n               s[2] -= k; n               s[3] -= gt; n               f[1][o].now += j; n               f[2][o].now += k; n               f[3][o].now += gt; n             } n           } n         } n       } n       if (o == 5) { n         for (int j = 0; j <= s[o]; j++) { n           if (j > f[1][o].now - f[1][o].l) break; n           for (int k = 0; k <= s[o] - j; k++) { n             if (k > f[2][o].now - f[2][o].l) break; n             for (int l = 0; l <= s[o] - j - k; l++) { n               for (int gt = 0; gt <= s[o] - j - k - l; gt++) { n                 if (l > f[3][o].now - f[3][o].l) break; n                 if (gt > f[4][o].now - f[4][o].l) break; n                 s[o] = s[o] - j - k - gt - l; n                 s[1] += j; n                 s[2] += k; n                 s[3] += l; n                 s[4] += gt; n                 f[1][o].now -= j; n                 f[2][o].now -= k; n                 f[3][o].now -= l; n                 f[4][o].now -= gt; n                 if (s[o] == 0 || o == n) work(o - 1); n                 s[o] = s[o] + j + k + gt + l; n                 s[1] -= j; n                 s[2] -= k; n                 s[3] -= l; n                 s[4] -= gt; n                 f[1][o].now += j; n                 f[2][o].now += k; n                 f[3][o].now += l; n                 f[4][o].now += gt; n               } n             } n           } n         } n       } n       if (o == 6) { n         for (int j = 0; j <= s[o]; j++) { n           if (j > f[1][o].now - f[1][o].l) break; n           for (int k = 0; k <= s[o] - j; k++) { n             if (k > f[2][o].now - f[2][o].l) break; n             for (int l = 0; l <= s[o] - j - k; l++) { n               if (l > f[3][o].now - f[3][o].l) break; n               for (int q = 0; q <= s[o] - j - k - l; q++) { n                 if (q > f[4][o].now - f[4][o].l) break; n                 for (int gt = 0; gt <= s[o] - j - k - l - q; gt++) { n                   if (gt > f[5][o].now - f[5][o].l) break; n                   s[o] = s[o] - j - k - gt - l - q; n                   s[1] += j; n                   s[2] += k; n                   s[3] += l; n                   s[4] += q; n                   s[5] += gt; n                   f[1][o].now -= j; n                   f[2][o].now -= k; n                   f[3][o].now -= l; n                   f[4][o].now -= q; n                   f[5][o].now -= gt; n                   if (s[o] == 0 || o == n) work(o - 1); n                   s[o] = s[o] + j + k + gt + l + q; n                   s[1] -= j; n                   s[2] -= k; n                   s[3] -= l; n                   s[4] -= q; n                   s[5] -= gt; n                   f[1][o].now += j; n                   f[2][o].now += k; n                   f[3][o].now += l; n                   f[4][o].now += q; n                   f[5][o].now += gt; n                 } n               } n             } n           } n         } n       } n     } else { n       if (o == 2) { n         for (int gt = 0; gt <= s[o]; gt++) { n           if (gt > f[1][o].r - f[1][o].now) break; n           s[o] = s[o] + gt; n           s[1] = s[1] - gt; n           f[1][o].now += gt; n           if (s[o] == 0 || o == n) work(o - 1); n           s[o] -= gt; n           s[1] += gt; n           f[1][o].now -= gt; n         } n       } else if (o == 3) { n         for (int j = 0; j <= s[o]; j++) { n           if (j > f[1][o].r - f[1][o].now) break; n           for (int gt = 0; gt <= s[o] - j; gt++) { n             if (gt > f[2][o].r - f[2][o].now) break; n             s[o] = s[o] + j + gt; n             s[1] = s[1] - j; n             s[2] = s[2] - gt; n             f[1][o].now += j; n             f[2][o].now += gt; n             if (s[o] == 0 || o == n) work(o - 1); n             s[o] = s[o] - gt - j; n             s[1] += j; n             s[2] += gt; n             f[1][o].now -= j; n             f[2][o].now -= gt; n           } n         } n       } else if (o == 4) { n         for (int j = 0; j <= s[o]; j++) { n           if (j > f[1][o].r - f[1][o].now) break; n           for (int k = 0; k <= s[o] - j; k++) { n             if (k > f[2][o].r - f[2][o].now) break; n             for (int gt = 0; gt <= s[o] - j - k; gt++) { n               if (gt > f[3][o].r - f[3][o].now) break; n               s[o] = s[o] + j + k + gt; n               s[1] -= j; n               s[2] -= k; n               s[3] -= gt; n               f[1][o].now += j; n               f[2][o].now += k; n               f[3][o].now += gt; n               if (s[o] == 0 || o == n) work(o - 1); n               s[o] = s[o] - j - k - gt; n               s[1] += j; n               s[2] += k; n               s[3] += gt; n               f[1][o].now -= j; n               f[2][o].now -= k; n               f[3][o].now -= gt; n             } n           } n         } n       } n       if (o == 5) { n         for (int j = 0; j <= s[o]; j++) { n           if (j > f[1][o].r - f[1][o].now) break; n           for (int k = 0; k <= s[o] - j; k++) { n             if (k > f[2][o].r - f[2][o].now) break; n             for (int l = 0; l <= s[o] - j - k; l++) { n               if (l > f[3][o].r - f[3][o].now) break; n               for (int gt = 0; gt <= s[o] - j - k - l; gt++) { n                 if (gt > f[4][o].r - f[4][o].now) break; n                 s[o] = s[o] + j + k + gt + l; n                 s[1] -= j; n                 s[2] -= k; n                 s[3] -= l; n                 s[4] -= gt; n                 f[1][o].now += j; n                 f[2][o].now += k; n                 f[3][o].now += l; n                 f[4][o].now += gt; n                 if (s[o] == 0 || o == n) work(o - 1); n                 s[o] = s[o] - j - k - gt - l; n                 s[1] += j; n                 s[2] += k; n                 s[3] += l; n                 s[4] += gt; n                 f[1][o].now -= j; n                 f[2][o].now -= k; n                 f[3][o].now -= l; n                 f[4][o].now -= gt; n               } n             } n           } n         } n       } n       if (o == 6) { n         for (int j = 0; j <= s[o]; j++) { n           if (j > f[1][o].r - f[1][o].now) break; n           for (int k = 0; k <= s[o] - j; k++) { n             if (k > f[2][o].r - f[2][o].now) break; n             for (int l = 0; l <= s[o] - j - k; l++) { n               if (l > f[3][o].r - f[3][o].now) break; n               for (int q = 0; q <= s[o] - j - k - l; q++) { n                 if (q > f[4][o].r - f[4][o].now) break; n                 for (int gt = 0; gt <= s[o] - j - k - l - q; gt++) { n                   if (gt > f[5][o].r - f[5][o].now) break; n                   s[o] = s[o] + j + k + gt + l + q; n                   s[1] -= j; n                   s[2] -= k; n                   s[3] -= l; n                   s[4] -= q; n                   s[5] -= gt; n                   f[1][o].now += j; n                   f[2][o].now += k; n                   f[3][o].now += l; n                   f[4][o].now += q; n                   f[5][o].now += gt; n                   if (s[o] == 0 || o == n) work(o - 1); n                   s[o] = s[o] - j - k - gt - l - q; n                   s[1] += j; n                   s[2] += k; n                   s[3] += l; n                   s[4] += q; n                   s[5] += gt; n                   f[1][o].now -= j; n                   f[2][o].now -= k; n                   f[3][o].now -= l; n                   f[4][o].now -= q; n                   f[5][o].now -= gt; n                 } n               } n             } n           } n         } n       } n     } n   } n } n int main() { n   scanf( %d , &n); n   m = n * (n - 1) / 2; n   for (i = 1; i <= m; i++) { n     scanf( %d%d%d%d%d , &x, &y, &l, &h, &a); n     f[x][y].l = l; n     f[x][y].r = h; n     f[x][y].a = a; n     f[x][y].now = h; n     s[y] += h; n     s[x] -= h; n   } n   work(n); n   if (ans == 100000) n     printf( -1 -1 ); n   else n     printf( %d %d n , ans, ans1); n } n


#include <bits/stdc++.h> n int bus_bin(int[], int, int, int); n int main() { n   int n, k, q, i, j, l, y, aux, existeB; n   scanf( %d%d , &n, &k); n   int monedas[n]; n   for (i = 0; i < n; i++) scanf( %d , &monedas[i]); n   scanf( %d , &q); n   int cant[q]; n   for (i = 0; i < q; i++) scanf( %d , &cant[i]); n   for (i = 0; i < q; i++) { n     aux = cant[i]; n     existeB = bus_bin(monedas, 0, n - 1, aux); n     if (existeB) { n       cant[i] = 1; n     } else { n       cant[i] = k + 1; n       for (y = 0; y < n && monedas[y] <= aux; y++) { n         for (j = 1; j <= k && j * monedas[y] <= aux; j++) { n           if (aux - j * monedas[y] == 0) { n             if (j < cant[i]) cant[i] = j; n           } else { n             for (l = 1; l + j <= k; l++) { n               existeB = bus_bin(monedas, 0, n - 1, (aux - j * monedas[y]) / l); n               if ((aux - j * monedas[y]) % l == 0 && existeB) { n                 if (j + l < cant[i]) cant[i] = j + l; n                 break; n               } n             } n           } n         } n       } n     } n   } n   for (i = 0; i < q; i++) { n     printf( %d n , cant[i] > k ? -1 : cant[i]); n   } n   return 0; n } n int bus_bin(int v[], int ini, int fin, int x) { n   int medio; n   medio = (ini + fin) / 2; n   if (v[medio] == x) n     return 1; n   else if (ini > fin) n     return 0; n   else if (x > v[medio]) n     return bus_bin(v, medio + 1, fin, x); n   else if (x < v[medio]) n     return bus_bin(v, ini, medio - 1, x); n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1e9 + 7; n bool IsPowerOfTwo(long long int x) { return (x != 0) && ((x & (x - 1)) == 0); } n long long int poer(long long int a, long long int b) { n   long long int rs = 1; n   while (b > 0) { n     if (b & 1) rs *= a; n     a *= a; n     b /= 2; n   } n   return rs; n } n long long int mod_poer(long long int a, long long int b, long long int mo) { n   long long int rs = 1; n   while (b > 0) { n     if (b & 1) rs = (rs * a) % mo; n     a = (a * a) % mo; n     b /= 2; n   } n   return rs % mo; n } n long long int d, x, y; n void ext_euclid(long long int a, long long int b) { n   if (b == 0) { n     d = a; n     x = 1; n     y = 0; n   } else { n     ext_euclid(b, a % b); n     long long int temp = x; n     x = y; n     y = temp - (a / b) * y; n   } n } n long long int mod_inverse1(long long int a, long long int m) { n   ext_euclid(a, m); n   return (x % m + m) % m; n } n long long int mod_inverse2(long long int a, long long int m) { n   return mod_poer(a, m - 2, m); n } n long long int gcd(long long int a, long long int b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n long long int fact[200005]; n void fac() { n   fact[1] = fact[0] = 1; n   for (long long int i = 2; i <= 200005; ++i) { n     fact[i] = ((fact[i - 1] % mod) * i) % mod; n   } n } n long long int combi(long long int a, long long int b) { n   if (b > a) return 0; n   if (b == a) n     return 1; n   else { n     long long int x = mod_poer(fact[b], mod - 2, mod) % mod; n     long long int y = mod_poer(fact[a - b], mod - 2, mod) % mod; n     return ((((fact[a]) * x) % mod) * y) % mod; n   } n } n long long int len[54]; n string str1 =  What are you doing while sending   ; n string str2 =   ? Are you busy? Will you send   ; n string str3 =   ? ; n long long int len1 = str1.size(); n long long int len2 = str2.size(); n long long int len3 = str3.size(); n string f0 = n      What are you doing at the end of the world? Are you busy? Will you save   n      us? ; n char get(long long int n, long long int k) { n   if (n > 54) { n     if (k <= len1) return str1[k - 1]; n     return get(n - 1, k - len1); n   } n   if (n < 54 && k > len[n]) return  . ; n   if (n == 0) return f0[k - 1]; n   if (k <= len1) return str1[k - 1]; n   if (k <= len1 + len[n - 1]) return get(n - 1, k - len1); n   if (k <= len1 + len[n - 1] + len2) return str2[k - len1 - len[n - 1] - 1]; n   if (k <= len1 + len[n - 1] * 2 + len2) n     return get(n - 1, k - len1 - len[n - 1] - len2); n   return str3[k - len1 - len[n - 1] * 2 - len2 - 1]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   len[0] = f0.size(); n   for (long long int i = 1; i < 54; ++i) { n     len[i] = (len[i - 1] * 2) + len1 + len2 + len3; n   } n   long long int t; n   cin >> t; n   while (t--) { n     long long int n, k; n     cin >> n >> k; n     cout << get(n, k); n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void getint(int &v) { n   char ch, fu = 0; n   for (ch =  * ; (ch <  0  || ch >  9 ) && ch !=  - ; ch = getchar()) n     ; n   if (ch ==  - ) fu = 1, ch = getchar(); n   for (v = 0; ch >=  0  && ch <=  9 ; ch = getchar()) v = v * 10 + ch -  0 ; n   if (fu) v = -v; n } n int n, k, a[1000010], ans; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n   sort(a + 1, a + n + 1); n   for (int i = 1; i <= k; ++i) ans += a[i]; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[28 * 2]; n bitset<1250005> bleh[28][28 * 2]; n int sm; n vector<int> in[2]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   for (int i = 0; i < (signed)(n); i++) { n     cin >> a[i]; n     sm += a[i]; n   } n   for (int i = 0; i < (signed)(n); i++) { n     cin >> a[i + n]; n     sm += a[i + n]; n   } n   sort(a, a + 2 * n); n   bleh[0][1][0] = true; n   for (int i = (2); i <= (signed)(2 * n - 1); i++) { n     bleh[0][i] = bleh[0][i - 1]; n     for (int j = (n - 1); j >= 1; j--) { n       bleh[j][i] = bleh[j][i - 1] | (bleh[j - 1][i - 1] << a[i]); n       for (int k = (0); k <= (signed)(400); k++) { n         if (bleh[j][i][k]) { n         } n       } n     } n   } n   int tot = sm - a[0] - a[1]; n   int wtmoo = tot / 2; n   while (!bleh[n - 1][2 * n - 1][wtmoo]) { n     --wtmoo; n   } n   int cur = wtmoo; n   int cnt = n - 1; n   for (int i = (2 * n - 2); i >= 1; i--) { n     if (bleh[cnt][i][cur]) { n       in[0].push_back(a[i + 1]); n     } else { n       in[1].push_back(a[i + 1]); n       --cnt; n       cur -= a[i + 1]; n     } n   } n   in[0].push_back(a[0]); n   in[1].push_back(a[1]); n   sort(in[0].begin(), in[0].end()); n   sort(in[1].begin(), in[1].end(), greater<int>()); n   for (int i = 0; i < (signed)(2); i++) { n     for (int j = 0; j < (signed)(n); j++) { n       cout << in[i][j] <<    ; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = 3.14159265358979323846; n const double eps = (1e-5); n long long isGreater(long double x, long double y) { n   if (abs(x - y) < eps) return 0; n   if (x > y) return 1; n   return -1; n } n long long n; n bool valid(long long i) { return i >= 0 && i < n; } n long long getSum(const vector<long long> &cum, long long l, long long r) { n   long long ret = 0; n   if (valid(r)) ret += cum[r]; n   if (valid(l - 1)) ret -= cum[l - 1]; n   return ret; n } n long long bestAns; n long long solve(vector<long long> a, long long d) { n   long long sum = 0; n   for (long long i = 0; i < (long long)(a).size(); ++i) { n     a[i] = a[i] % d; n     sum += a[i]; n     sum %= d; n   } n   if (sum != 0) return LLONG_MAX; n   sum = 0; n   long long size = 0; n   long long ans = 0; n   for (long long i = 0; i < (long long)(a).size(); ++i) { n     sum += a[i]; n     size++; n     if (sum >= d) { n       long long extra = sum - d; n       a[i] -= extra; n       long long st = i - size + 1, en = i; n       long long minSum = LLONG_MAX, temp = 0, inc = 0, dec = d; n       for (long long j = st; j <= en; ++j) temp += 1LL * a[j] * (j - st + 1); n       for (long long mid = st; mid <= en; ++mid) { n         temp += inc; n         temp -= dec; n         dec -= a[mid]; n         inc += a[mid]; n         minSum = min(minSum, temp); n       } n       ans += minSum; n       if (ans > bestAns) break; n       a[i] = extra; n       size = 0; n       sum = 0; n       --i; n     } n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   vector<long long> a(n); n   long long sum = 0; n   for (long long i = 0; i < n; ++i) { n     cin >> a[i]; n     sum += a[i]; n   } n   if (sum == 1) { n     cout << -1; n     return 0; n   } n   bestAns = solve(a, sum); n   for (long long i = 2; i <= sum / i; ++i) { n     if (sum % i == 0) { n       bestAns = min(bestAns, solve(a, i)); n       while (sum % i == 0) { n         sum /= i; n       } n     } n   } n   if (sum > 1) bestAns = min(bestAns, solve(a, sum)); n   cout << bestAns; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long read() { n   long long x = 0, f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) n     x = (x << 3) + (x << 1) + f * (c -  0 ), c = getchar(); n   return x; n } n const long long MAXN = 1e5 + 10; n long long n, m; n double f[MAXN]; n double ans; n void ycl() { n   long long x = n; n   double a[MAXN], b[MAXN]; n   for (long long i = 1; i <= m; i++) a[i] = (double)i / m, b[i] = 1; n   while (x > 0) { n     if (x % 2 == 1) { n       for (long long i = 1; i <= m; i++) b[i] *= a[i]; n     } n     for (long long i = 1; i <= m; i++) a[i] *= a[i]; n     x /= 2; n   } n   for (long long i = 1; i <= m; i++) f[i] = b[i]; n } n signed main() { n   m = read(); n   n = read(); n   ycl(); n   for (long long i = 1; i <= m; i++) ans += (double)(f[i] - f[i - 1]) * i; n   printf( %lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v(110, 0); n int main() { n   int n, a, flag = 0, b; n   vector<int> m; n   cin >> n; n   b = n; n   while (n--) { n     cin >> a; n     m.push_back(a); n     v[a]++; n   } n   sort(m.begin(), m.end()); n   for (int i = m[0]; i <= m[b - 1]; i++) { n     for (int j = m[0]; j <= m[b - 1]; j++) { n       if (i != j && v[i] == v[j] && (v[i] + v[j]) == b) { n         flag = 1; n         cout <<  YES  << endl << i <<     << j << endl; n         break; n       } n     } n     if (flag) break; n   } n   if (!flag) cout <<  NO  << endl; 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   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   int time = INT_MAX, check = 0; n   for (int i = 0; i < n; i++) { n     check = 0; n     int no = a[i]; n     for (int j = 0; j < no; j++) { n       int item; n       cin >> item; n       check += item * 5; n     } n     check += no * 15; n     time = min(time, check); n   } n   cout << time; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = (1 << 12); n int cnt[maxn]; n pair<int, int> res[maxn][maxn]; n int pr[maxn][maxn]; n int w[13]; n int len; n int bsearch(int i, int k) { n   int l = 0, r = len; n   while (r - l > 1) { n     int mid = (r + l) / 2; n     if (res[i][mid].first <= k) n       l = mid; n     else n       r = mid; n   } n   return l; n } n bool cmp(pair<int, int> a, pair<int, int> b) { return a.first < b.first; } n void counting_sort(int i) { n   vector<int> cnts[1205]; n   for (int j = 0; j < len; j++) { n     cnts[res[i][j].first].push_back(res[i][j].second); n   } n   int j = 0; n   for (int k = 0; k < 1204; k++) { n     while (!cnts[k].empty()) { n       res[i][j] = {k, cnts[k].back()}; n       cnts[k].pop_back(); n       j++; n     } n   } n } n int main() { n   int n, m, q; n   scanf( %d %d %d , &n, &m, &q); n   len = (1 << n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &w[n - i - 1]); n   } n   for (int i = 0; i < m; i++) { n     int num = 0; n     for (int j = 0; j < n; j++) { n       char c = getchar(); n       while (c !=  0  && c !=  1 ) { n         c = getchar(); n       } n       num *= 2; n       num += (c -  0 ); n     } n     cnt[num]++; n   } n   for (int i = 0; i < (1 << n); i++) { n     for (int j = 0; j < (1 << n); j++) { n       int ans = 0; n       for (int k = 0; k < n; k++) { n         if (((1 << k) & i) == ((1 << k) & j)) ans += w[k]; n       } n       res[i][j] = {ans, cnt[j]}; n     } n     counting_sort(i); n     pr[i][0] = res[i][0].second; n     for (int j = 1; j < (1 << n); j++) { n       pr[i][j] = pr[i][j - 1] + (res[i][j].second); n     } n   } n   for (int i = 0; i < q; i++) { n     int num = 0; n     for (int j = 0; j < n; j++) { n       char c = getchar(); n       while (c !=  0  && c !=  1 ) { n         c = getchar(); n       } n       num *= 2; n       num += (c -  0 ); n     } n     int k; n     scanf( %d , &k); n     int ind = bsearch(num, k); n     printf( %d n , pr[num][ind]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int m, n, i; n long long int a[1000000], ans, sum[1000000] = {0}, x; n int check(long long int k) { n   x = 0; n   for (i = 1; i <= n; i++) { n     if (a[i] - (i - 1) / k > 0) x = x + a[i] - (i - 1) / k; n   } n   if (x >= m) n     return 1; n   else n     return 0; n } n int binsearch(long long int low, long long int high) { n   while (low < high) { n     long long int mid = (low + high) / 2; n     if (check(mid)) n       high = mid; n     else n       low = mid + 1; n   } n   if (!check(low)) low = -1; n   return low; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   sort(a + 1, a + 1 + n, greater<int>()); n   for (i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i]; n   ans = binsearch(1, n); n   cout << ans; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const long long N = 3e5 + 5, INF = 1e9; n string s2, s; n struct data { n   int next[30]; n   int cnt; n   bool least; n }; n data t[N]; n int sz = 1, ans; n string p; n inline void add(string s) { n   int v = 0; n   t[v].cnt++; n   for (int i = 0; i < s.size(); ++i) { n     int x = s[i] -  a ; n     if (t[v].next[x] == -1) { n       t[v].next[x] = sz; n       sz++; n     } n     v = t[v].next[x]; n     t[v].cnt++; n   } n   t[v].least = true; n } n map<string, bool> usd; n bool flag; n inline void f(int &pos) { n   int v = 0; n   int c = -1; n   int k = 0; n   if (t[v].next[s[pos] -  a ] == -1) { n     pos--; n     return; n   } n   while (s[pos] >=  a  && s[pos] <=  z ) { n     int x = s[pos] -  a ; n     if (t[v].next[x] == -1) break; n     k++; n     if (k >= 2 && t[v].cnt == 1) c++; n     p += s[pos]; n     v = t[v].next[x]; n     pos++; n   } n   pos--; n   ans += k; n   if (c != -1 && t[v].least == 1) ans -= c; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   for (int i = 0; i <= 300000; ++i) { n     for (int j = 0; j <= 26; ++j) { n       t[i].next[j] = -1; n     } n     t[i].cnt = 0; n     t[i].least = false; n   } n   while (getline(cin, s2)) { n     s += s2; n     s +=    ; n   } n   for (int i = 0; i < s.size(); ++i) { n     if (!flag && s[i] >=  a  && s[i] <=  z ) n       flag = true, f(i); n     else { n       ans++; n       if (s[i] >=  a  && s[i] <=  z ) n         p += s[i]; n       else if (p !=   ) { n         if (!usd[p]) add(p), usd[p] = true; n         p =   ; n       } n       if (s[i] <  a  || s[i] >  z ) flag = false; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx ) n using namespace std; n const int MAXN = 1e5 + 9; n const int MOD = (int)(1e9 + 7); n const int LOG2MAXN = 17; n const int INF = 1e9; n const long double eps = 1e-9; n int di[] = {0, 1, 0, -1}; n int dj[] = {1, 0, -1, 0}; n char ddir[] = { R ,  U ,  L ,  D }; n mt19937_64 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); n int n, m; n vector<vector<int> > gg; n bool used[MAXN]; n void cry() { n   cout <<  NO ; n   exit(0); n } n int ans[111][111]; n char cc[111][111]; n void solve() { n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) cin >> cc[i][j]; n   int mx = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       bool ok = 1; n       for (int tmp = 0; tmp < k; tmp++) n         if (i + tmp >= n || cc[i + tmp][j] ==  # ) ok = 0; n       if (ok) n         for (int tmp = 0; tmp < k; tmp++) ans[i + tmp][j]++; n       ok = 1; n       for (int tmp = 0; tmp < k; tmp++) n         if (j + tmp >= n || cc[i][j + tmp] ==  # ) ok = 0; n       if (ok) n         for (int tmp = 0; tmp < k; tmp++) ans[i][j + tmp]++; n       mx = max(mx, ans[i][j]); n     } n   } n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) n       if (ans[i][j] == mx) { n         cout << i + 1 <<     << j + 1 << endl; n         return; n       } n } n void input() {} n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   input(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, f[105], s[105], ans; n int Getf(int x) { return (f[x] == x ? x : Getf(f[x])); } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) f[i] = i, s[i] = 1; n   int x, y; n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &x, &y); n     int tx = Getf(x), ty = Getf(y); n     if (tx == ty && (s[ty] & 1)) ans++; n     f[tx] = ty; n     s[ty] += s[tx]; n   } n   if ((n - ans) % 2 != 0) ans++; n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m, i, left, right, p[52]; n   vector<long long> bin; n   cin >> n >> m; n   m--; n   while (m) { n     if (m & 1) n       bin.push_back(1); n     else n       bin.push_back(0); n     m /= 2; n   } n   for (i = bin.size(); i < n - 1; i++) bin.push_back(0); n   reverse(bin.begin(), bin.end()); n   left = 0; n   right = n - 1; n   for (i = 0; i < n - 1; i++) { n     if (bin[i]) { n       p[right] = i + 1; n       right--; n     } else { n       p[left] = i + 1; n       left++; n     } n   } n   p[left] = i + 1; n   for (i = 0; i < n - 1; i++) cout << p[i] <<    ; n   cout << p[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, l, r; n   cin >> n >> l >> r; n   long long mn[l], t1 = 1, mx[r], sum1 = 0, sum2 = 0; n   for (int i = 0; i < l; i++) { n     mn[i] = t1; n     sum1 += mn[i]; n     t1 *= 2; n   } n   for (int i = 0; i < n - l; i++) { n     sum1 += mn[0]; n   } n   t1 = 1; n   for (int i = 0; i < r; i++) { n     mx[i] = t1; n     t1 *= 2; n     sum2 += mx[i]; n   } n   for (int i = 0; i < n - r; i++) { n     sum2 += mx[r - 1]; n   } n   cout << sum1 <<     << sum2; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, m; n void solve() { n   cin >> n >> m; n   vector<vector<long long int>> v(m, vector<long long int>(n)); n   vector<long long int> sum(n); n   for (long long int i = 0; i < (m); i++) { n     for (long long int j = 0; j < (n); j++) { n       cin >> v[i][j]; n     } n   } n   vector<long long int> ans(m); n   for (long long int i = 1; i < (m + 1); i++) ans[i - 1] = i; n   for (long long int i = 0; i < (n - 1); i++) { n     vector<pair<long long int, long long int>> p(m); n     long long int diff = 0; n     for (long long int j = 0; j < (m); j++) { n       p[j] = make_pair(v[j][i] - v[j][n - 1], j); n       diff += p[j].first; n     } n     sort((p).begin(), (p).end()); n     vector<long long int> newans; n     for (long long int w = 0; w < (m); w++) { n       if (diff >= 0) break; n       newans.push_back(p[w].second + 1); n       diff -= p[w].first; n     } n     if (newans.size() < ans.size()) { n       ans = newans; n     } n   } n   cout << ans.size() << endl; n   for (long long int x : ans) { n     cout << x <<    ; n   } n   cout << endl; n } n int main() { n   long long int t; n   t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long A, B, C, D, X, Y; n inline long long gcd(long long a, long long b, long long &x, long long &y) { n   if (b == 0) { n     x = 1; n     y = 0; n     return a; n   } n   long long res = gcd(b, a % b, x, y); n   long long t = x; n   x = y; n   y = t - (a / b) * y; n   return res; n } n int main() { n   scanf( %lld %lld %lld , &A, &B, &C); n   C = -C; n   D = gcd(A, B, X, Y); n   if (C % D) n     puts( -1 ); n   else { n     C /= D; n     printf( %lld %lld n , X * C, Y * C); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n set<pair<int, int> > z[200010], f[200010]; n map<int, int> use[200010]; n int n, m, k, dir; n int sx, sy; n inline void insert(int x, int y) { n   z[x - y + m + 1].insert(make_pair(x, y)); n   f[x + y].insert(make_pair(x, y)); n   use[x][y] = 1; n } n void init() { n   char s[3]; n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1, x, y; i <= k; i++) { n     scanf( %d%d , &x, &y); n     insert(x, y); n   } n   scanf( %d%d%s , &sx, &sy, s); n   dir = (s[1] ==  E ) | ((s[0] ==  S ) << 1); n   for (int i = 0; i <= n + 1; i++) insert(i, 0), insert(i, m + 1); n   for (int i = 0; i <= m + 1; i++) insert(0, i), insert(n + 1, i); n } n void go(int x, int y, int d, int &x1, int &y1, int &d1, int &px, int &py) { n   int a, b; n   if (d == 0) { n     set<pair<int, int> >::iterator p = n         z[x - y + m + 1].lower_bound(make_pair(x, y)); n     p--; n     px = p->first; n     py = p->second; n     a = use[px + 1].count(py); n     b = use[px].count(py + 1); n     if (!a && b) { n       x1 = px + 1; n       y1 = py; n       d1 = 2; n     } else if (a && !b) { n       x1 = px; n       y1 = py + 1; n       d1 = 1; n     } else { n       x1 = px + 1; n       y1 = py + 1; n       d1 = 3; n     } n   } else if (d == 1) { n     set<pair<int, int> >::iterator p = f[x + y].lower_bound(make_pair(x, y)); n     p--; n     px = p->first; n     py = p->second; n     a = use[px + 1].count(py); n     b = use[px].count(py - 1); n     if (!a && b) { n       x1 = px + 1; n       y1 = py; n       d1 = 3; n     } else if (a && !b) { n       x1 = px; n       y1 = py - 1; n       d1 = 0; n     } else { n       x1 = px + 1; n       y1 = py - 1; n       d1 = 2; n     } n   } else if (d == 2) { n     set<pair<int, int> >::iterator p = f[x + y].upper_bound(make_pair(x, y)); n     px = p->first; n     py = p->second; n     a = use[px - 1].count(py); n     b = use[px].count(py + 1); n     if (!a && b) { n       x1 = px - 1; n       y1 = py; n       d1 = 0; n     } else if (a && !b) { n       x1 = px; n       y1 = py + 1; n       d1 = 3; n     } else { n       x1 = px - 1; n       y1 = py + 1; n       d1 = 1; n     } n   } else { n     set<pair<int, int> >::iterator p = n         z[x - y + m + 1].upper_bound(make_pair(x, y)); n     px = p->first; n     py = p->second; n     a = use[px - 1].count(py); n     b = use[px].count(py - 1); n     if (!a && b) { n       x1 = px - 1; n       y1 = py; n       d1 = 1; n     } else if (a && !b) { n       x1 = px; n       y1 = py - 1; n       d1 = 2; n     } else { n       x1 = px - 1; n       y1 = py - 1; n       d1 = 0; n     } n   } n } n void solve() { n   long long ans = 0; n   int rep = 0; n   int xx, yy, dd, px, py; n   go(sx, sy, dir, sx, sy, dir, px, py); n   for (int x = sx, y = sy, d = dir;; x = xx, y = yy, d = dd) { n     go(x, y, d, xx, yy, dd, px, py); n     ans += abs(x - px); n     if (xx == sx && yy == sy && dd == dir) break; n     if (dd + d == 3) rep = 1; n   } n   if (rep) ans >>= 1; n   printf( %I64d n , ans); n } n int main() { n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 5, inf = 1e9 + 7; n long long n, k; n string s, t; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long i, j, a, b, ans = 0; n   cin >> n >> k >> s >> t; n   ans = n; n   i = 0; n   while (i < n && s[i] == t[i]) i++; n   long long t1 = 0, t2 = 0, a1 = 1, a2 = 1; n   k--; n   if (i < n && k) { n     ans += n - i; n     k--; n     i++; n   } n   for (; i < n; i++) { n     if (s[i] ==  a ) { n       ans += min(k, t1 + 1) * (n - i); n       k -= min(k, t1 + 1); n       t1 = t1 * 2 + 1; n     } else { n       ans += min(k, t1) * (n - i); n       k -= min(k, t1); n       t1 *= 2; n     } n     if (t[i] ==  a ) { n       ans += min(k, t2) * (n - i); n       k -= min(k, t2); n       t2 *= 2; n     } else { n       ans += min(k, t2 + 1) * (n - i); n       k -= min(k, t2 + 1); n       t2 = t2 * 2 + 1; n     } n     if (!k) break; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n int main() { puts( Karen ); } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k; n vector<vector<pair<int, int> > > v2; n long long costs[(int)1e5 + 5]; n int parent[(int)1e5 + 5]; n vector<int> answer; n void dikstra() { n   priority_queue<pair<long long, int>, vector<pair<long long, int> >, n                  greater<pair<long long, int> > > n       pq; n   pq.push({0, 1}); n   parent[1] = -1; n   costs[1] = 0; n   while (!pq.empty()) { n     int node = pq.top().second; n     long long cost = pq.top().first; n     pq.pop(); n     for (auto I : v2[node]) { n       if (costs[I.second] > cost + I.first) { n         costs[I.second] = cost + I.first; n         parent[I.second] = node; n         pq.push({costs[I.second], I.second}); n       } n     } n   } n } n int main() { n   string s; n   cin >> s; n   cin >> n; n   for (int i = 0; i < n; i++) { n     s.push_back( ! ); n   } n   int mx = 0; n   for (int i = 0; i < s.size(); i++) { n     for (int j = i + 1; j < s.size(); j++) { n       int l = 0; n       for (int k = i; k < i + ((j - i + 1) / 2); k++) { n         if (s[k] ==  !  || s[k + (j - i + 1) / 2] ==  ! ) { n           l++; n         } else if (s[k] == s[k + (j - i + 1) / 2]) { n           l++; n         } else n           break; n       } n       mx = max(mx, l * 2); n     } n   } n   cout << mx << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long mpow(long long a, long long b) { n   long long c = 1; n   while (b) { n     if (b & 1) c = c * a % 998244353; n     a = a * a % 998244353; n     b /= 2; n   } n   return c; n } n long long minv(long long a) { return mpow(a, 998244353 - 2); } n int n; n long long ans; n vector<int> g[500010]; n long long f(int u, int p) { n   long long prob = 1; n   for (int v : g[u]) n     if (v != p) { n       prob *= 1 - f(v, u) * ((998244353 + 1) / 2) % 998244353; n       prob %= 998244353; n     } n   ans += 1 - prob; n   ans %= 998244353; n   return prob; n } n int main() { n   scanf( %d , &n); n   for (int i = 1, u, v; i < n; i++) { n     scanf( %d %d , &u, &v); n     g[u].push_back(v); n     g[v].push_back(u); n   } n   f(1, 0); n   printf( %I64d n , (ans + 998244353) * mpow(2, n) % 998244353); n } n


#include <bits/stdc++.h> n using namespace std; n int n; n struct hh { n   int t, nxt; n } edge[1000020 << 1]; n int head[1000020], ecnt; n void make_edge(int f, int t) { n   edge[++ecnt] = (hh){t, head[f]}; n   head[f] = ecnt; n   edge[++ecnt] = (hh){f, head[t]}; n   head[t] = ecnt; n } n int dep[1000020], size[1000020], son[1000020], fa[1000020]; n void dfs1(int x, int fa) { n   dep[x] = dep[fa] + 1; n   size[x] = 1; n   for (int i = head[x]; i; i = edge[i].nxt) n     if (edge[i].t != fa) { n       dfs1(edge[i].t, x); n       size[x] += size[edge[i].t]; n       if (size[edge[i].t] > size[son[x]]) son[x] = edge[i].t; n     } n } n map<string, int> S[1000020]; n int sum[1000020]; n string a[1000020]; n bool s[1000020]; n void add(string s, int d, int t) { n   if (!S[d][s]) ++sum[d]; n   S[d][s] += t; n   if (!S[d][s]) --sum[d]; n } n void add(int x, int fa, int t) { n   add(a[x], dep[x], t); n   for (int i = head[x]; i; i = edge[i].nxt) n     if (edge[i].t != fa && !s[edge[i].t]) add(edge[i].t, x, t); n } n vector<pair<int, int> > q[1000020]; n int ans[1000020]; n void dfs2(int x, int fa, bool keep) { n   for (int i = head[x]; i; i = edge[i].nxt) n     if (edge[i].t != fa && edge[i].t != son[x]) dfs2(edge[i].t, x, 0); n   s[son[x]] = 1; n   if (son[x]) dfs2(son[x], x, 1); n   add(x, fa, 1); n   for (int i = 0; i < (int)q[x].size(); i++) n     ans[q[x][i].first] = sum[q[x][i].second]; n   s[son[x]] = 0; n   if (!keep) add(x, fa, -1); n } n int main() { n   ios::sync_with_stdio(false); n   int i, x, y, m; n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> a[i] >> fa[i]; n     if (fa[i]) make_edge(fa[i], i); n   } n   for (i = 1; i <= n; i++) n     if (!fa[i]) dfs1(i, 0); n   cin >> m; n   for (i = 1; i <= m; i++) { n     cin >> x >> y; n     q[x].push_back(make_pair(i, dep[x] + y)); n   } n   for (i = 1; i <= n; i++) n     if (!fa[i]) dfs2(i, 0, 0); n   for (i = 1; i <= m; i++) printf( %d n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)5e5 + 5; n int n, m, k, dp[1 << 15][110], nxt[N][16]; n vector<int> a; n map<string, int> mp; n void update(int &x, int y) { n   if (x == -1 || x > y) x = y; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     string s; n     cin >> s; n     mp[s] = i; n   } n   cin >> m; n   int idx = 0, cnt = 0, lmt = n * (n - 1) / 2; n   for (int i = 1; i <= m; ++i) { n     cin >> k; n     string s; n     a.clear(); n     for (int j = 1; j <= k; ++j) { n       cin >> s; n       if (mp.find(s) != mp.end()) n         a.push_back(mp[s]); n       else n         a.push_back(0); n     } n     for (int j = 1; j <= n; ++j) nxt[k + 1][j] = k + 1; n     for (int j = k; j >= 1; --j) { n       for (int x = 1; x <= n; ++x) { n         if (a[j - 1] == x) n           nxt[j][x] = j; n         else n           nxt[j][x] = nxt[j + 1][x]; n       } n     } n     memset(dp, -1, sizeof(dp)); n     dp[0][0] = 0; n     for (int x = 0; x < (1 << n) - 1; ++x) { n       for (int y = 0; y <= lmt; ++y) n         if (dp[x][y] != -1) { n           int tmp = 0; n           for (int z = n - 1; z >= 0; --z) { n             if ((x >> z) & 1) n               ++tmp; n             else { n               int pos = nxt[dp[x][y] + 1][z + 1]; n               if (pos <= k && y + tmp <= lmt) n                 update(dp[x | (1 << z)][y + tmp], pos); n             } n           } n         } n     } n     for (int y = 0; y <= lmt; ++y) n       if (dp[(1 << n) - 1][y] != -1) { n         if (lmt - y + 1 > cnt) idx = i, cnt = lmt - y + 1; n         break; n       } n   } n   if (!idx) return puts( Brand new problem! ), 0; n   printf( %d n[: , idx); n   for (int i = 1; i <= cnt; ++i) printf( | ); n   printf( :] ); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   char old_h[1001][21], new_h[1001][21]; n   int q, i, j; n   char temp[21]; n   scanf( %d , &q); n   for (i = 0; i < q; i++) { n     scanf( %s %s , old_h[i], new_h[i]); n   } n   for (i = 0; i < q; i++) { n     for (j = i + 1; j < q; j++) { n       if (strcmp(new_h[i], old_h[j]) == 0) { n         strcpy(temp, new_h[i]); n         strcpy(new_h[i], new_h[j]); n         strcpy(new_h[j], temp); n       } n     } n   } n   int count = 0; n   for (i = 0; i < q; i++) { n     if (strcmp(old_h[i], new_h[i]) != 0) count++; n   } n   printf( %d n , count); n   for (i = 0; i < q; i++) { n     if (strcmp(old_h[i], new_h[i]) != 0) { n       printf( %s %s n , old_h[i], new_h[i]); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, d, ans = 0; n vector<long long> a; n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> d; n   while (n--) { n     long long temp; n     cin >> temp; n     a.push_back(temp); n   } n   for (long long i = 0; i < a.size(); i++) { n     long long idx = upper_bound(a.begin(), a.end(), a[i] + d) - a.begin(); n     idx--; n     if (idx > i + 1) ans += ((idx - i) * (idx - i - 1)) / 2; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(0.0) * 2.0; n const double eps = 1e-12; n const int step[8][2] = {{1, 0}, {-1, 0}, {0, 1},  {0, -1}, n                         {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; n template <class T> n inline T abs1(T a) { n   return a < 0 ? -a : a; n } n template <class T> n inline T max1(T a, T b) { n   return a > b ? a : b; n } n template <class T> n inline T max1(T a, T b, T c) { n   return max1(max1(a, b), c); n } n template <class T> n inline T max1(T a, T b, T c, T d) { n   return max1(max1(a, b, c), d); n } n template <class T> n inline T max1(T a, T b, T c, T d, T e) { n   return max1(max1(a, b, c, d), e); n } n template <class T> n inline T min1(T a, T b) { n   return a > b ? a : b; n } n template <class T> n inline T min1(T a, T b, T c) { n   return min1(min1(a, b), c); n } n template <class T> n inline T min1(T a, T b, T c, T d) { n   return min1(min1(a, b, c), d); n } n template <class T> n inline T min1(T a, T b, T c, T d, T e) { n   return min1(min1(a, b, c, d), e); n } n template <class T> n inline T lowb(T num) { n   return num & (-num); n } n inline int jud(double a, double b) { n   if (abs(a) < eps && abs(b) < eps) n     return 0; n   else if (abs1(a - b) / abs1(a) < eps) n     return 0; n   if (a < b) return -1; n   return 1; n } n template <typename t> n inline int jud(t a, t b) { n   if (a < b) return -1; n   if (a == b) return 0; n   return 1; n } n template <typename it, typename t1> n inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1) { n   int be = 0, en = na - 1; n   if (*a <= *(a + na - 1)) { n     if (f_lb == 0) n       while (be < en) { n         int mid = (be + en + 1) / 2; n         if (jud(*(a + mid), val) != 1) n           be = mid; n         else n           en = mid - 1; n       } n     else n       while (be < en) { n         int mid = (be + en) / 2; n         if (jud(*(a + mid), val) != -1) n           en = mid; n         else n           be = mid + 1; n       } n     if (f_small && jud(*(a + be), val) == 1) be--; n     if (!f_small && jud(*(a + be), val) == -1) be++; n   } else { n     if (f_lb) n       while (be < en) { n         int mid = (be + en + 1) / 2; n         if (jud(*(a + mid), val) != -1) n           be = mid; n         else n           en = mid - 1; n       } n     else n       while (be < en) { n         int mid = (be + en) / 2; n         if (jud(*(a + mid), val) != 1) n           en = mid; n         else n           be = mid + 1; n       } n     if (!f_small && jud(*(a + be), val) == -1) be--; n     if (f_small && jud(*(a + be), val) == 1) be++; n   } n   return be; n } n inline int bitnum(unsigned int nValue) { return __builtin_popcount(nValue); } n inline int bitnum(int nValue) { return __builtin_popcount(nValue); } n inline int bitnum(unsigned long long nValue) { n   return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32); n } n inline int bitnum(long long nValue) { n   return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32); n } n inline int bitmaxl(unsigned int a) { n   if (a) return 0; n   return 32 - __builtin_clz(a); n } n inline int bitmaxl(int a) { n   if (a) return 0; n   return 32 - __builtin_clz(a); n } n inline int bitmaxl(unsigned long long a) { n   int temp = a >> 32; n   if (temp) return 32 - __builtin_clz(temp) + 32; n   return bitmaxl(int(a)); n } n inline int bitmaxl(long long a) { n   int temp = a >> 32; n   if (temp) return 32 - __builtin_clz(temp) + 32; n   return bitmaxl(int(a)); n } n long long pow(long long n, long long m, long long mod = 0) { n   if (m < 0) return 0; n   long long ans = 1; n   long long k = n; n   while (m) { n     if (m & 1) { n       ans *= k; n       if (mod) ans %= mod; n     } n     k *= k; n     if (mod) k %= mod; n     m >>= 1; n   } n   return ans; n } n const int maxn = (1 << 24) + 10; n int cnt[maxn], n; n int arr[10010]; n int main() { n   ios_base::sync_with_stdio(0); n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     char str[10]; n     scanf( %s , str); n     int rmask = 0; n     for (int j = 0; j < 3; j++) rmask |= 1 << (str[j] -  a ); n     rmask = (1 << 24) - 1 - rmask; n     arr[i] = rmask; n     cnt[rmask]++; n   } n   long long ans = 0; n   for (int i = 23; i >= 0; i--) { n     int mask = 1 << 24; n     mask -= 1 + (1 << i); n     for (int j = mask; j >= 0; j = (j - 1) & mask) { n       cnt[j] += cnt[j + (1 << i)]; n       if (j == 0) break; n     } n   } n   for (int i = (1 << 24) - 1; i >= 0; i--) { n     ans ^= (n - cnt[i]) * (n - cnt[i]); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n int getbit(T s, int i) { n   return (s >> i) & 1; n } n template <class T> n T onbit(T s, int i) { n   return s | (T(1) << i); n } n template <class T> n T offbit(T s, int i) { n   return s & (~(T(1) << i)); n } n template <class T> n int cntbit(T s) { n   return __builtin_popcount(s); n } n template <class T> n T gcd(T a, T b) { n   T r; n   while (b != 0) { n     r = a % b; n     a = b; n     b = r; n   } n   return a; n } n template <class T> n T lcm(T a, T b) { n   return a / gcd(a, b) * b; n } n int n; n string s; n long long u[2000005], d[2000005]; n long long nu[2000005], nd[2000005]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> s; n   memset(nu, 0, sizeof(nu)); n   memset(nd, 0, sizeof(nd)); n   memset(u, 0, sizeof(u)); n   memset(d, 0, sizeof(d)); n   for (int i = (1); i <= (n); ++i) { n     char ch = s[i - 1]; n     nu[i] = nu[i - 1]; n     nd[i] = nd[i - 1]; n     if (ch ==  U ) { n       nu[i]++; n       u[nu[i]] = u[nu[i] - 1] + i; n     } else { n       nd[i]++; n       d[nd[i]] = d[nd[i] - 1] + i; n     } n   } n   for (int i = (1); i <= (n); ++i) { n     char ch = s[i - 1]; n     long long res = 0; n     if (ch ==  U ) { n       long long numd = nd[n] - nd[i]; n       long long numu = nu[i]; n       if (numd < numu) { n         res += (d[nd[i] + numd] - d[nd[i]]) * 2 + n + 1; n         res -= (u[nu[i]] - u[nu[i] - numd - 1]) * 2 - i; n       } else { n         res += (d[nd[i] + numu] - d[nd[i]]) * 2; n         res -= (u[nu[i]]) * 2 - i; n       } n     } else { n       long long numd = nd[n] - nd[i - 1]; n       long long numu = nu[i]; n       if (numd <= numu) { n         res += (d[nd[i - 1] + numd] - d[nd[i - 1]]) * 2 + n + 1 - i; n         res -= (u[nu[i]] - u[nu[i] - numd]) * 2; n       } else { n         res += (d[nd[i] + numu] - d[nd[i - 1]]) * 2 - i; n         res -= (u[nu[i]]) * 2; n       } n     } n     cout << res <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {1, 0, -1, 0, -1, 1, -1, 1}; n int dy[] = {0, 1, 0, -1, -1, 1, 1, -1}; n const double eps = 1e-6; n const int N = 1e9; n void moha() {} n bool vis[1505][1505]; n int comp[1505][1505][2]; n char x[1505][1505]; n int n, m; n int row(int x) { return (x + n * 1500) % n; } n int col(int y) { return (y + m * 1500) % m; } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   int a, b; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       cin >> x[i][j]; n       if (x[i][j] ==  S ) { n         a = i; n         b = j; n       } n     } n   queue<pair<int, int> > q; n   q.push({a, b}); n   vis[a][b] = 1; n   comp[row(a)][col(b)][0] = a; n   comp[row(a)][col(b)][1] = b; n   while (!q.empty()) { n     pair<int, int> p = q.front(); n     q.pop(); n     for (int i = 0; i < 4; i++) { n       int xx = p.first + dx[i]; n       int yy = p.second + dy[i]; n       if (x[row(xx)][col(yy)] ==  # ) continue; n       if (vis[row(xx)][col(yy)]) { n         if (comp[row(xx)][col(yy)][0] != xx || n             comp[row(xx)][col(yy)][1] != yy) { n           cout <<  Yes  << endl; n           return 0; n         } n         continue; n       } n       q.push({xx, yy}); n       vis[row(xx)][col(yy)] = 1; n       comp[row(xx)][col(yy)][0] = xx; n       comp[row(xx)][col(yy)][1] = yy; n     } n   } n   cout <<  No  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int a[101]; n signed main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   sort(a + 1, a + 1 + n); n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     if (!a[i]) continue; n     ans++; n     for (int j = i + 1; j <= n; j++) { n       if (a[j]) { n         if (a[j] % a[i] == 0) a[j] = 0; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n template <typename T> n inline void read(T &x) { n   char c; n   bool nega = 0; n   while ((!isdigit(c = getchar())) && (c !=  - )) n     ; n   if (c ==  - ) { n     nega = 1; n     c = getchar(); n   } n   x = c - 48; n   while (isdigit(c = getchar())) x = x * 10 + c - 48; n   if (nega) x = -x; n } n template <typename T> n inline void writep(T x) { n   if (x > 9) writep(x / 10); n   putchar(x % 10 + 48); n } n template <typename T> n inline void write(T x) { n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   writep(x); n } n template <typename T> n inline void writeln(T x) { n   write(x); n   putchar(  n ); n } n int n, s; n int a[2000]; n deque<int> pos[2001]; n map<int, int> m; n int f[2001]; n int trace[2001]; n int g[2001]; n int dist_inc(int u, int v) { n   if (v >= u) return v - u; n   return v + n - u; n } n int dist_dec(int u, int v) { n   if (v <= u) return u - v; n   return u + n - v; n } n int dist(int u, int v) { return min(abs(u - v), n - abs(u - v)); } n void output(int x) { n   putchar( +- [x < 0]); n   writeln(abs(x)); n } n void rebuild(int u) { n   if (a[u] > 1) rebuild(trace[u]); n   if ((a[u] == 1) && (a[s] == 1)) { n     output(0); n     return; n   } n   int x = a[u]; n   int sz = pos[x].size(); n   int i = trace[u]; n   if (pos[x].size() == 1) { n     if (dist(i, u) == dist_inc(i, u)) { n       output(dist(i, u)); n     } else { n       output(-dist(trace[u], u)); n     } n   } else { n     while (dist_inc(pos[x].back(), i) + dist_inc(i, pos[x].front()) != n            dist_inc(pos[x].back(), pos[x].front())) { n       pos[x].push_back(pos[x].front()); n       pos[x].pop_front(); n     } n     if (i == s) { n       if (a[s] == 0) n         g[s] = 0; n       else n         g[s] = f[s]; n     } n     int maxs = dist_inc(i, pos[x].front()); n     int tmax = pos[x].front(); n     for (int j = (0); j < (sz); j++) { n       if (j) { n         if (maxs < dist_inc(pos[x][j - 1], pos[x][j])) { n           maxs = dist_inc(pos[x][j - 1], pos[x][j]); n           tmax = pos[x][j]; n         } n       } n       if (pos[x][j] == u) break; n     } n     if (f[u] == g[i] + n + dist_inc(i, u) - maxs * 2) { n       int last = i; n       for (int j = (0); j < (sz); j++) { n         if (pos[x][j] == tmax) break; n         last = pos[x][j]; n         output(dist_inc((j == 0) ? i : pos[x][j - 1], pos[x][j])); n       } n       for (int j = (sz - 1); j >= (0); j--) { n         output(-dist_dec((j == sz - 1) ? last : pos[x][j + 1], pos[x][j])); n         if (pos[x][j] == tmax) { n           if (tmax == u) break; n           output(dist_inc(tmax, u)); n           break; n         } n       } n       return; n     } n     maxs = dist_dec(i, pos[x].back()); n     tmax = pos[x].back(); n     for (int j = (sz - 1); j >= (0); j--) { n       if (j < sz - 1) { n         if (maxs < dist_dec(pos[x][j + 1], pos[x][j])) { n           maxs = dist_dec(pos[x][j + 1], pos[x][j]); n           tmax = pos[x][j]; n         } n       } n       if (pos[x][j] == u) break; n     } n     if (f[u] == g[i] + n + dist_dec(i, u) - maxs * 2) { n       int last = i; n       for (int j = (sz - 1); j >= (0); j--) { n         if (pos[x][j] == tmax) break; n         last = pos[x][j]; n         output(-dist_dec((j == sz - 1) ? i : pos[x][j + 1], pos[x][j])); n       } n       for (int j = (0); j < (sz); j++) { n         output(dist_inc((j == 0) ? last : pos[x][j - 1], pos[x][j])); n         if (pos[x][j] == tmax) { n           if (tmax == u) break; n           output(-dist_dec(tmax, u)); n           break; n         } n       } n       return; n     } n   } n } n int main() { n   read(n); n   read(s); n   s--; n   for (int i = (0); i < (n); i++) read(a[i]); n   if (a[s] == *min_element(a, a + n)) { n     a[s]--; n   } n   for (int i = (0); i < (n); i++) m[a[i]] = 0; n   { n     int cnt = 0; n     for (auto &t : m) t.second = ++cnt; n   } n   for (int i = (0); i < (n); i++) { n     a[i] = m[a[i]]; n     pos[a[i]].push_back(i); n   } n   if (a[s] != 1) pos[0].push_back(s); n   for (int x = (1 + (a[s] == 1)); x <= (m.size()); x++) { n     for (int i : pos[x - 1]) g[i] = f[i]; n     for (int i : pos[x]) f[i] = 1e9; n     int sz = pos[x].size(); n     for (int i : pos[x - 1]) { n       if (pos[x].size() == 1) { n         if (f[pos[x][0]] > g[i] + dist(i, pos[x][0])) { n           f[pos[x][0]] = g[i] + dist(i, pos[x][0]); n           trace[pos[x][0]] = i; n         } n       } else { n         while (dist_inc(pos[x].back(), i) + dist_inc(i, pos[x].front()) != n                dist_inc(pos[x].back(), pos[x].front())) { n           pos[x].push_back(pos[x].front()); n           pos[x].pop_front(); n         } n         int maxs = dist_inc(i, pos[x].front()); n         for (int j = (0); j < (sz); j++) { n           if (j) maxs = max(maxs, dist_inc(pos[x][j - 1], pos[x][j])); n           if (f[pos[x][j]] > g[i] + n + dist_inc(i, pos[x][j]) - maxs * 2) { n             f[pos[x][j]] = g[i] + n + dist_inc(i, pos[x][j]) - maxs * 2; n             trace[pos[x][j]] = i; n           } n         } n         maxs = dist_dec(i, pos[x].back()); n         for (int j = (sz - 1); j >= (0); j--) { n           if (j + 1 < sz) maxs = max(maxs, dist_dec(pos[x][j + 1], pos[x][j])); n           if (f[pos[x][j]] > g[i] + n + dist_dec(i, pos[x][j]) - maxs * 2) { n             f[pos[x][j]] = g[i] + n + dist_dec(i, pos[x][j]) - maxs * 2; n             trace[pos[x][j]] = i; n           } n         } n       } n     } n   } n   int best = pos[m.size()][0]; n   for (int i : pos[m.size()]) n     if (f[i] < f[best]) best = i; n   writeln(f[best]); n   rebuild(best); n } n


#include <bits/stdc++.h> n long long tt, x, y, z, sum, n, i, j, h, k, t, d, m, l, ll, rr, r, a[1000006], n     b[1000006], f[1000006]; n using namespace std; n long long xl(long long i) { n   long long res = 0; n   for (j = 1; j <= d; j++) b[j] = a[j]; n   l = 1; n   r = i; n   tt = 1; n   z = b[1]; n   x = 1; n   while (r <= n) { n     if (b[x] > i) { n       l += b[x] - b[x] % i - i; n       r += b[x] - b[x] % i - i; n       b[x] -= (b[x] - b[x] % i - i); n     } n     y = 0; n     t = (l + r) / 2; n     while (z < t) tt++, z += a[tt]; n     while (y < i) { n       if (b[x] > i - y) { n         res += (i - y) * abs(f[x] - f[tt]); n         b[x] -= (i - y); n         y = i; n       } else { n         res += b[x] * abs(f[x] - f[tt]); n         y += b[x]; n         x++; n       } n     } n     l += i; n     r += i; n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> j; n     sum += j; n     if (j > 0) d++, a[d] = j, f[d] = i; n   } n   if (sum == 1) n     cout << -1; n   else { n     n = sum; n     i = 2; n     m = 1e18; n     while (i <= sqrt(sum)) { n       if (sum % i == 0) m = min(m, xl(i)); n       while (sum % i == 0) sum /= i; n       i++; n     } n     if (sum > 1) m = min(m, xl(sum)); n     cout << m; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = LLONG_MAX; n const int inf = INT_MAX; n const int mod = 1e9 + 7; n const int pi = acos(-1); n const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}; n const int dy[8] = {1, -1, 0, 0, 1, -1, 1, -1}; n const int N = 2e5 + 5; n const int MAXN = 1e5 + 1; n int a[N], dp[N][6], p[N][6]; n vector<int> v; n void ans(int n, int val) { n   v.push_back(val); n   if (n == 1) { n     reverse(v.begin(), v.end()); n     for (int i = 0; i < (int)v.size(); i++) { n       cout << v[i] <<    ; n     } n     exit(0); n   } n   int to = p[n][val]; n   ans(n - 1, to); n } n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (int i = 1; i <= 5; i++) { n     dp[1][i] = 1; n   } n   for (int i = 2; i <= n; i++) { n     for (int j = 1; j <= 5; j++) { n       int w = INT_MIN; n       for (int k = 1; k <= 5; k++) { n         if (k == j) continue; n         if (a[i] > a[i - 1]) { n           if (j > k) w = max(w, dp[i - 1][k]); n         } n         if (a[i] < a[i - 1]) { n           if (j < k) w = max(w, dp[i - 1][k]); n         } n         if (a[i] == a[i - 1]) { n           w = max(w, dp[i - 1][k]); n         } n       } n       if (w == 1) { n         dp[i][j] = 1; n         int ans; n         for (int k = 1; k <= 5; k++) { n           if (k == j) continue; n           if (a[i] > a[i - 1]) { n             if (j > k) { n               if (dp[i - 1][k] == 1) { n                 ans = k; n                 break; n               } n             } n           } else if (a[i] < a[i - 1]) { n             if (j < k) { n               if (dp[i - 1][k] == 1) { n                 ans = k; n                 break; n               } n             } n           } else { n             if (dp[i - 1][k] == 1) { n               ans = k; n               break; n             } n           } n         } n         p[i][j] = ans; n       } else { n         dp[i][j] = INT_MIN; n       } n     } n   } n   for (int i = 1; i <= 5; i++) { n     if (dp[n][i] == 1) { n       ans(n, i); n       return 0; n     } n   } n   cout << -1; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 8; n const int mod = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     long long n, m; n     cin >> n >> m; n     long long arr[n][m]; n     long long x = 0; n     for (long long i = 0; i < (n); i++) { n       for (long long j = 0; j < (m); j++) { n         cin >> arr[i][j]; n         if (arr[i][j] < 0) { n           x++; n         } n       } n     } n     long long ans = 0; n     long long minval = 101; n     for (long long i = 0; i < (n); i++) { n       for (long long j = 0; j < (m); j++) { n         ans += abs(arr[i][j]); n         minval = min(minval, abs(arr[i][j])); n       } n     } n     if (x % 2 != 0) { n       ans -= 2 * minval; n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, colour[105], p[105]; n   memset(colour, -1, sizeof colour); n   bool flag = 0; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> p[i]; n     colour[p[i]] = 2; n   } n   for (int i = 0; i < 101; i++) { n     if (colour[i] == 2) { n       colour[i] = flag; n       flag = !flag; n     } n   } n   for (int i = 0; i < n; i++) { n     if (i) cout <<    ; n     cout << colour[p[i]]; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   vector<vector<long long int>> grid(51, vector<long long int>(51, 0)); n   vector<pair<long long int, long long int>> a; n   int t1 = 4 * n + 1; n   while (t1--) { n     int u, v; n     cin >> u >> v; n     a.push_back({u, v}); n     grid[u][v] = 1; n   } n   t1 = 4 * n + 1; n   for (int stx = 0; stx < 50; stx++) { n     for (int enx = stx + 1; enx < 51; enx++) { n       for (int sty = 0; sty < 50; sty++) { n         for (int eny = sty + 1; eny < 51; eny++) { n           long long int cnt = 0, c1 = 0, c2 = 0, c3 = 0, c4 = 0; n           for (int j = sty; j <= eny; j++) { n             if (grid[stx][j] == 1) { n               cnt++; n               c1++; n               if (j == sty) { n                 c2++; n               } n               if (j == eny) { n                 c4++; n               } n             } n             if (grid[enx][j] == 1) { n               cnt++; n               c3++; n               if (j == sty) { n                 c2++; n               } n               if (j == eny) { n                 c4++; n               } n             } n           } n           for (int j = stx + 1; j <= enx - 1; j++) { n             if (grid[j][sty] == 1) { n               cnt++; n               c2++; n             } n             if (grid[j][eny] == 1) { n               cnt++; n               c4++; n             } n           } n           if (cnt == 4 * n && c1 >= n && c2 >= n && c3 >= n && c4 >= n) { n             for (int i = 0; i < a.size(); i++) { n               if (a[i].first != stx && a[i].first != enx && n                   a[i].second != sty && a[i].second != eny) { n                 cout << a[i].first <<     << a[i].second; n                 return 0; n               } n               if (a[i].first == stx || a[i].first == enx) { n                 if (a[i].second < sty || a[i].second > eny) { n                   cout << a[i].first <<     << a[i].second; n                   return 0; n                 } n               } n               if (a[i].second == sty || a[i].second == eny) { n                 if (a[i].first < stx || a[i].first > enx) { n                   cout << a[i].first <<     << a[i].second; n                   return 0; n                 } n               } n             } n           } n         } n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, dp[3005][3005], a[1000005]; n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; ++i) scanf( %d , a + i); n   if (n <= m) { n     dp[1][a[1] % m] = 1; n     for (int i = 2; i <= n; ++i) { n       for (int j = 0; j < m; ++j) { n         a[i] = (a[i] % m); n         dp[i][j] = (dp[i - 1][j] | dp[i - 1][(j - a[i] + m) % m]); n       } n       dp[i][a[i] % m] = 1; n     } n     if (dp[n][0]) n       return !printf( YES n ); n     else n       return !printf( NO n ); n   } else n     printf( YES n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m; n   cin >> n >> m; n   if (m == 0) { n     cout << 1 << endl; n     return 0; n   } n   if (m <= n / 2) { n     cout << m << endl; n   } else { n     long long res = n - m; n     cout << res << endl; n   } n   return 0; n } n