#include <bits/stdc++.h> n using namespace std; n const long long int MOD = 1e6 + 3; n const long double PI = 2 * acos(0.0); n const vector<long long int> dx = {1, -1, 0, 0}; n const vector<long long int> dy = {0, 0, 1, -1}; n template <typename T = long long int> n vector<T> ga(long long int n, bool oneIndexed = false) { n   vector<T> a = vector<T>(n + oneIndexed); n   for (long long int i = 0; i < n; i++) { n     T p; n     cin >> p; n     a[i + oneIndexed] = p; n   } n   return move(a); n } n template <typename T, typename A> n void pa(vector<T, A> const &a, long long int begin = 0, n         long long int end = -1) { n   if (end == -1) end = (long long int)a.size() - 1; n   for (long long int i = begin; i <= end; i++) { n     cout << a[i] <<    ; n   } n   cout <<   n ; n } n template <typename T> n T po(T base, T exp) { n   T result = 1; n   for (;;) { n     if (exp & 1) result *= base; n     exp >>= 1; n     if (!exp) break; n     base *= base; n   } n   return result; n } n template <typename T> n T pomod(T x, T y, T mod) { n   T res = 1; n   x = x % mod; n   while (y > 0) { n     if (y & 1) res = (res * x) % mod; n     y = y >> 1; n     x = (x * x) % mod; n   } n   return res; n } n void solve() { n   long long int n; n   cin >> n; n   if (n == 0) n     cout << 1 <<   n ; n   else n     cout << pomod(3ll, n - 1, MOD); n } n int main(void) { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char **argv) { n   int n; n   scanf( %d , &n); n   vector<int> f(n + 1, -1); n   vector<int> g(n + 1, -1); n   vector<int> h(1); n   int m = 0; n   vector<bool> b(n + 1, false); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &f[i]); n     if (f[i] == i) { n       m++; n       h.push_back(i); n       g[i] = m; n       b[i] = true; n     } n   } n   for (int i = 1; i <= n; i++) { n     if (f[i] != i) { n       int to = g[f[i]]; n       if (b[f[i]] == false || to == -1) { n         puts( -1 ); n         return 0; n       } n       g[i] = to; n     } n   } n   printf( %d n , m); n   for (int i = 1; i <= n; i++) { n     if (i > 1) { n       putchar(   ); n     } n     printf( %d , g[i]); n   } n   puts(  ); n   for (int i = 1; i <= m; i++) { n     if (i > 1) { n       putchar(   ); n     } n     printf( %d , h[i]); n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1000000007; n const long long int mymax = 1e18; n const long long int mymin = -1e18; n const double PIE = 3.1415926536; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t, n, m, d, k, ti, lef, rit; n   bool ans; n   vector<pair<int, int> > cent; n   cin >> t; n   while (t--) { n     cin >> n >> m >> k; n     char a[n][m]; n     int vis[n][m]; n     cent.clear(); n     ans = true; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         cin >> a[i][j]; n         vis[i][j] = 0; n       } n     } n     for (int i = n - 1; i >= 1; i--) { n       for (int j = 1; j <= (m - 2); j++) { n         if ((a[i][j] ==  * ) && (a[i - 1][j - 1] ==  * ) && n             (a[i - 1][j + 1] ==  * )) { n           d = 0; n           ti = i - 1; n           lef = j - 1; n           rit = j + 1; n           while ((ti >= 0) && (lef >= 0) && (rit < m) && (a[ti][lef] ==  * ) && n                  (a[ti][rit] ==  * )) { n             d++; n             ti--; n             lef--; n             rit++; n           } n           if (d >= k) { n             cent.push_back({i, j}); n           } n         } n       } n     } n     for (auto it : cent) { n       vis[it.first][it.second] = 1; n       ti = it.first - 1; n       lef = it.second - 1; n       rit = it.second + 1; n       while ((ti >= 0) && (lef >= 0) && (rit < m) && (a[ti][lef] ==  * ) && n              (a[ti][rit] ==  * )) { n         vis[ti][lef] = vis[ti][rit] = 1; n         ti--; n         lef--; n         rit++; n       } n     } n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         if ((a[i][j] ==  * ) && !vis[i][j]) { n           ans = false; n           break; n         } n       } n     } n     cout << ((ans) ?  YES  :  NO ) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n const double eps = 1e-8; n const int nm = 1100005; n int n, k, m, t; n long long res; n int cnt[nm], f[nm][23]; n int POW(int x, int z) { n   if (z == 0) return 1; n   if (z == 1) return x % MOD; n   int u = POW(x, z >> 1); n   u = ((long long)u * u) % MOD; n   if (z % 2) u = ((long long)u * x) % MOD; n   return u; n } n int main() { n   int i, j, x, y, z, w; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d , &x); n     cnt[x]++; n   } n   for (i = (1 << 20) - 1; i >= 0; i--) { n     if (i & 1) n       f[i][0] = cnt[i]; n     else n       f[i][0] = cnt[i] + cnt[i + 1]; n     for (j = 1; j < 20; j++) { n       if (i & (1 << j)) n         f[i][j] = f[i][j - 1]; n       else n         f[i][j] = f[i][j - 1] + f[i + (1 << j)][j - 1]; n     } n   } n   for (i = (1 << 20) - 1; i >= 0; i--) { n     x = __builtin_popcount(i); n     if (x & 1) n       res -= POW(2, f[i][19]); n     else n       res += POW(2, f[i][19]); n     res = (res + MOD) % MOD; n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   int u, v; n }; n int n, m, k, f[1000000 + 1], ans[1000000 + 1], q[1000000 + 1], lq = 0, rq = -1, n                                                                cur; n edge b[1000000 + 1]; n vector<int> a[1000000 + 1], e[1000000 + 1]; n bool remoed[1000000 + 1], removeti[1000000 + 1]; n void removequeue() { n   int x, y; n   while (lq <= rq) { n     x = q[lq]; n     lq++; n     cur--; n     for (int i = 0; i < a[x].size(); i++) { n       y = a[x][i]; n       if (remoed[e[x][i]] == false) { n         remoed[e[x][i]] = true; n         f[y]--; n         if (f[y] < k && removeti[y] == false) { n           rq++; n           q[rq] = y; n           removeti[y] = true; n         } n       } n     } n   } n } n void removemany() { n   for (int i = 1; i <= n; i++) { n     if (f[i] < k) { n       rq++; n       q[rq] = i; n       removeti[i] = true; n     } n   } n   removequeue(); n } n void solve() { n   removemany(); n   for (int i = m - 1; i >= 0; i--) { n     ans[i] = cur; n     if (remoed[i] == false) { n       remoed[i] = true; n       f[b[i].u]--; n       f[b[i].v]--; n       if (f[b[i].u] < k && removeti[b[i].u] == false) { n         rq++; n         q[rq] = b[i].u; n         removeti[b[i].u] = true; n       } n       if (f[b[i].v] < k && removeti[b[i].v] == false) { n         rq++; n         q[rq] = b[i].v; n         removeti[b[i].v] = true; n       } n     } n     removequeue(); n   } n   for (int i = 0; i < m; i++) cout << ans[i] <<   n ; n } n void readit() { n   cin >> n >> m >> k; n   for (int i = 0; i < m; i++) { n     cin >> b[i].u >> b[i].v; n     a[b[i].u].push_back(b[i].v); n     a[b[i].v].push_back(b[i].u); n     e[b[i].u].push_back(i); n     e[b[i].v].push_back(i); n     f[b[i].u]++; n     f[b[i].v]++; n   } n   cur = n; n } n int main() { n   readit(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   for (int k = 0; k < n - 1; k++) { n     if (s[k] > s[k + 1]) { n       cout <<  YES  << endl; n       cout << k + 1 <<     << k + 2; n       return 0; n     } n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000 + 10; n int main() { n   int c, d, n, m, k; n   while (scanf( %d %d , &c, &d) != EOF) { n     scanf( %d %d , &n, &m); n     scanf( %d , &k); n     int t = n * m - k; n     if (t <= 0) { n       printf( 0 n ); n       continue; n     } n     int res = t / n; n     int ans = min(res * n * d, res * c); n     ans += min(c, (t - res * n) * d); n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n long long a[100005], k; n map<long long, long long> tr; n long long getans(const long long k) { n   long long t1 = 1, t2 = 0, t3 = 0, t4 = 1, ans = 0; n   while (t2 < n) { n     ans += (a[t1] - a[t2]) * t3; n     ++t3; n     t2 = t1; n     t1 += t4 * k; n     t4 *= k; n     if (t1 > n) t1 = n; n   } n   return ans; n } n int main() { n   tr.clear(); n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %I64d , &a[i]); n     a[i] = -a[i]; n   } n   sort(a + 1, a + n + 1); n   a[0] = 0; n   for (int i = 1; i <= n; ++i) { n     a[i] = -a[i]; n     a[i] += a[i - 1]; n   } n   scanf( %d , &m); n   for (int i = 1; i <= m; ++i) { n     scanf( %I64d , &k); n     if (i != 1) printf(   ); n     if (!tr.count(k)) tr[k] = getans(k); n     printf( %I64d , tr[k]); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> cities[100000]; n vector<bool> visited(100000); n vector<int> t(100000), low(100000); n map<pair<int, int>, int> b; n int c = 0, ans = 0; n void dfs(int v, int p = -1) { n   visited[v] = true; n   t[v] = low[v] = c++; n   for (int dest : cities[v]) { n     if (dest == p) continue; n     if (visited[dest]) { n       low[v] = min(low[v], t[dest]); n     } else { n       dfs(dest, v); n       low[v] = min(low[v], low[dest]); n       if (low[dest] > t[v]) { n         b[{v, dest}] = 1; n         b[{dest, v}] = 1; n       } else { n         b[{v, dest}] = 0; n         b[{dest, v}] = 0; n       } n     } n   } n } n vector<long long> dist(100001); n void Dijkstra(int r, int e) { n   set<pair<int, int> > s; n   dist[r] = 0; n   s.insert({0, r}); n   while (!s.empty()) { n     pair<int, int> p = *s.begin(); n     s.erase(s.begin()); n     int x = p.first, y = p.second; n     if (x > dist[y]) { n       continue; n     } else if (dist[e] != INT_MAX) { n       return; n     } n     for (int j = 0; j < cities[y].size(); j++) { n       if (dist[y] + b[{y, cities[y][j]}] < dist[cities[y][j]]) { n         dist[cities[y][j]] = dist[y] + b[{y, cities[y][j]}]; n         s.insert({dist[cities[y][j]], cities[y][j]}); n       } n     } n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m, k; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     cities[x - 1].push_back(y - 1); n     cities[y - 1].push_back(x - 1); n   } n   c = 0; n   t.assign(n, -1); n   low.assign(n, -1); n   visited.assign(n, false); n   for (int i = 0; i < n; ++i) { n     if (!visited[i]) { n       dfs(i); n     } n   } n   cin >> k; n   for (int i = 0; i < k; i++) { n     ans = 0; n     dist.assign(n, INT_MAX); n     int root, shop; n     cin >> root >> shop; n     Dijkstra(root - 1, shop - 1); n     cout << dist[shop - 1] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int y, w, x, counter = 0; n   cin >> y >> w; n   x = max(y, w); n   for (int i = x; i <= 6; i++) { n     counter++; n   } n   if (counter == 0) n     cout <<  0/1 ; n   else if (counter == 1) n     cout <<  1/6 ; n   else if (counter == 2) n     cout <<  1/3 ; n   else if (counter == 3) n     cout <<  1/2 ; n   else if (counter == 4) n     cout <<  2/3 ; n   else if (counter == 5) n     cout <<  5/6 ; n   else n     cout <<  1/1 ; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long x = 0, f = 1; n   char c = getchar(); n   while ((c <  0  || c >  9 ) && (c !=  - )) c = getchar(); n   if (c ==  - ) f = -1, c = getchar(); n   while (c >=  0  && c <=  9 ) x = x * 10 + c -  0 , c = getchar(); n   return x * f; n } n const int N = 2e5 + 10; n int n, m, Ans[N]; n pair<int, int> a[N], b[N]; n inline long long Query(int x) { n   long long ret = 0; n   if (x < 0) { n     x = -x; n     for (register int i = (1); i <= (x); i++) n       ret += m - a[n - x + i].first + b[i].first, n           Ans[a[n - i + 1].second] = b[i].second; n     for (register int i = (x + 1); i <= (n); i++) n       ret += abs(b[i].first - a[i - x].first), n           Ans[a[i - x].second] = b[i].second; n   } else { n     for (register int i = (1); i <= (x); i++) n       ret += a[i].first + m - b[n - x + i].first, n           Ans[a[i].second] = b[n - x + i].second; n     for (register int i = (x + 1); i <= (n); i++) n       ret += abs(a[i].first - b[i - x].first), n           Ans[a[i].second] = b[i - x].second; n   } n   return ret; n } n int main() { n   m = read(), n = read(); n   for (register int i = (1); i <= (n); i++) a[i] = make_pair(read(), i); n   for (register int i = (1); i <= (n); i++) b[i] = make_pair(read(), i); n   sort(a + 1, a + 1 + n), sort(b + 1, b + 1 + n); n   int l = -n, r = n; n   while (l < r) { n     int mid = l + r >> 1; n     if (Query(mid) < Query(mid + 1)) n       r = mid; n     else n       l = mid + 1; n   } n   printf( %lld n , Query(l)); n   for (register int i = (1); i <= (n); i++) printf( %d  , Ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n long long ma, n, i, j, temp; n long long mi = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL; n int main() { n   cin >> n; n   for (i = 1; i * i * i <= n; i++) n     if (n % i == 0) n       for (j = i; j * j <= n / i; j++) n         if ((n / i) % j == 0) { n           temp = (n / i) / j; n           ma = max(ma, (i + 2) * (j + 2) * (temp + 1)); n           ma = max(ma, (i + 2) * (j + 1) * (temp + 2)); n           ma = max(ma, (i + 1) * (j + 2) * (temp + 2)); n           mi = min(mi, (i + 2) * (j + 2) * (temp + 1)); n           mi = min(mi, (i + 2) * (j + 1) * (temp + 2)); n           mi = min(mi, (i + 1) * (j + 2) * (temp + 2)); n         } n   cout << mi - n <<     << ma - n << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   scanf( %d , &n); n   int i = sqrt(n); n   for (int i = 0; i <= 10; i++) { n     if (pow(2, i) == n) { n       printf( YES ); n       return 0; n     } n   } n   printf( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using vi = vector<int>; n using ii = pair<int, int>; n template <typename A, typename B> n ostream& operator<<(ostream& os, pair<A, B> p) { n   return os <<  {  << p.first <<  ,   << p.second <<  } ; n } n const int oo = 0x3f3f3f3f; n const ll OO = ll(oo) * oo; n const ll M = 1e9 + 9; n ll p2[100005]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   int m; n   cin >> m; n   ll k = 1; n   for (int i = 0; i < m + 1; i++) { n     p2[i] = k; n     k <<= 1ll; n     k %= M; n   } n   ll ans = 1; n   for (int i = 0; i < n; i++) { n     if (p2[m] - i - 1 <= 0) { n       cout << 0 << endl; n       return 0; n     } n     ans *= (p2[m] - i - 1) % M; n     ans %= M; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n int a[5005]; n int solve(int i, int j) { n   int k, vans, hans, mini, s, t; n   vans = j - i + 1; n   mini = a[i]; n   for (k = i + 1; k <= j; k++) n     if (mini > a[k]) mini = a[k]; n   hans = mini; n   for (k = i; k <= j; k++) a[k] -= mini; n   hans = mini; n   for (k = i; k <= j;) { n     if (a[k] == 0) { n       k++; n     } else { n       s = k; n       t = s; n       while (t <= j && a[t] != 0) t++; n       hans += solve(s, t - 1); n       k = t; n     } n   } n   if (vans < hans) n     return vans; n   else n     return hans; n } n int main() { n   int n, i; n   int vans, hans; n   int ans; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) scanf( %d , &a[i]); n   ans = solve(1, n); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( unroll-loops ) n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( O3 ) n const long long inf = 1e17; n void bfs(int a, vector<vector<int> > &g, vector<int> &da) { n   int n = g.size(); n   queue<int> q; n   q.push(a); n   vector<bool> u(n, false); n   u[a] = true; n   da[a] = 0; n   while (!q.empty()) { n     int v = q.front(); n     q.pop(); n     for (int i = 0; i < g[v].size(); i++) { n       int x = g[v][i]; n       if (!u[x]) { n         u[x] = true; n         da[x] = da[v] + 1; n         q.push(x); n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int q; n   cin >> q; n   while (q--) { n     int n, m, a, b, c; n     cin >> n >> m >> a >> b >> c; n     a--; n     b--; n     c--; n     vector<vector<int> > g(n, vector<int>(0)); n     vector<long long> cost(m); n     for (int i = 0; i < m; i++) cin >> cost[i]; n     sort(cost.begin(), cost.end()); n     for (int i = 1; i < m; i++) cost[i] += cost[i - 1]; n     vector<int> da(n, 0), db(n, 0), dc(n, 0); n     for (int i = 0; i < m; i++) { n       int fr, to; n       cin >> fr >> to; n       fr--; n       to--; n       g[fr].push_back(to); n       g[to].push_back(fr); n     } n     bfs(a, g, da); n     bfs(b, g, db); n     bfs(c, g, dc); n     long long ans = inf; n     for (int i = 0; i < n; i++) { n       if (da[i] + db[i] + dc[i] - 1 > m - 1) continue; n       long long cur = 0; n       if (da[i] + db[i] + dc[i] != 0) cur += cost[da[i] + db[i] + dc[i] - 1]; n       if (db[i] > 0) cur += cost[db[i] - 1]; n       ans = min(ans, cur); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cache[1005][20][2]; n int ret[1005]; n int n; n vector<int> ans(1005, INT_MAX); n void query(vector<int> &arr) { n   if (!arr.empty()) { n     printf( %lu n , arr.size()); n     for (auto &x : arr) printf( %i  , x); n     printf(  n ); n     fflush(stdout); n     for (int x = 1; x <= n; x++) scanf( %i , &ret[x]); n   } else n     memset(ret, 0, sizeof ret); n } n int main() { n   scanf( %i , &n); n   for (int x = 2, y = 1; x < 2 * n; x *= 2, y++) { n     vector<int> w; n     for (int z = 1; z <= n; z += x) n       for (int a = 0; a < x / 2 && z + a <= n; a++) w.push_back(z + a); n     query(w); n     for (int z = 1; z <= n; z++) cache[z][y][0] = ret[z]; n     w.clear(); n     for (int z = 1 + x / 2; z <= n; z += x) n       for (int a = 0; a < x / 2 && z + a <= n; a++) w.push_back(z + a); n     query(w); n     for (int z = 1; z <= n; z++) cache[z][y][1] = ret[z]; n   } n   printf( -1 n ); n   for (int z = 1; z <= n; z++) { n     for (int x = 2, y = 1; x < 2 * n; x *= 2, y++) { n       for (int a = 0; a <= 1; a++) n         if (((z - 1) >> (y - 1) & 1) != a) ans[z] = min(ans[z], cache[z][y][a]); n     } n     printf( %i  , ans[z]); n   } n   printf(  n ); n   fflush(stdout); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 110000; n int a[N], wei[N], r1, r2; n bool can[N], ans[N]; n int n, m; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     scanf( %d%d , &n, &m); n     n--; n     for (int i = (1); i <= (int)m; i++) { n       scanf( %d , &a[i]); n       wei[i] = a[i]; n       ans[i] = 0; n       can[i] = 1; n     } n     r1 = r2 = 0; n     int shiwang = 0; n     for (int i = (1); i <= (int)n; i++) { n       int x, y; n       scanf( %d%d , &x, &y); n       if (y == 1) { n         if (!shiwang) shiwang = i; n       } n       if (!shiwang && x == 0) r1++; n       if (x == 0) r2++; n       if (shiwang) can[x] = 0; n       a[x]--; n       if (!shiwang) wei[x]--; n     } n     if (!shiwang) { n       for (int i = (1); i <= (int)m; i++) n         if (a[i] <= r2) n           printf( Y ); n         else n           printf( N ); n       printf(  n ); n     } else { n       int lu = 0; n       for (int i = (1); i <= (int)m; i++) n         if (can[i] && wei[i] <= r1) { n           ans[i] = 1; n           if (r2 - wei[i] > lu) lu = r2 - wei[i]; n         } n       for (int i = (1); i <= (int)m; i++) { n         if (a[i] <= lu) ans[i] = 1; n         if (ans[i]) n           printf( Y ); n         else n           printf( N ); n       } n       printf(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<long long, long long> > A, B; n void getDivisors(long long x, vector<pair<long long, long long> > &X) { n   for (long long i = 1; i * i <= x; i++) { n     if (x % i) continue; n     X.push_back({i, x / i}); n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(NULL), cout.tie(NULL); n   long long a, b; n   cin >> a >> b; n   long long area = a + b; n   getDivisors(a, A); n   getDivisors(b, B); n   long long lim = 0; n   while (lim * lim <= area) lim++; n   for (long long i = lim - 1; i >= 1; i--) { n     if (area % i) continue; n     int f = 0; n     long long x = i; n     long long y = area / i; n     int ptr = n         lower_bound(A.begin(), A.end(), make_pair(x + 1, 0LL)) - A.begin(); n     if (ptr > 0 && A[ptr - 1].second <= y) f = 1; n     ptr = lower_bound(B.begin(), B.end(), make_pair(x + 1, 0LL)) - B.begin(); n     if (ptr > 0 && B[ptr - 1].second <= y) f = 1; n     if (f == 1) { n       cout << (x + y) * 2 <<   n ; n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 60; n vector<int> adj[maxn]; n int n, m; n void read() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     a--, b--; n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n } n bool vis[maxn]; n vector<int> ch; n void dfs(int v) { n   vis[v] = 1; n   ch.push_back(v); n   for (int i = 0; i < int(adj[v].size()); i++) { n     int nxt = adj[v][i]; n     if (!vis[nxt]) dfs(nxt); n   } n } n void solve() { n   memset(vis, 0, sizeof(vis)); n   if (m > n) { n     puts( NO ); n     return; n   } else if (m == n) { n     dfs(0); n     for (int i = 0; i < n; i++) { n       if (!vis[i] || int(adj[i].size()) != 2) { n         puts( NO ); n         return; n       } n     } n     puts( YES n0 ); n     return; n   } else { n     for (int i = 0; i < n; i++) { n       if (int(adj[i].size()) > 2) { n         puts( NO ); n         return; n       } n     } n     set<pair<int, int> > line; n     for (int i = 0; i < n; i++) { n       if (vis[i]) continue; n       if (int(adj[i].size()) == 0) { n         line.insert(make_pair(i, i)); n         vis[i] = 1; n       } n       if (int(adj[i].size()) == 1) { n         ch.clear(); n         dfs(i); n         line.insert(make_pair(ch.front(), ch.back())); n       } n     } n     for (int i = 0; i < n; i++) { n       if (!vis[i]) { n         puts( NO ); n         return; n       } n     } n     vector<pair<int, int> > ans; n     while (int(line.size())) { n       if (int(line.size()) == 1) { n         ans.push_back(*line.begin()); n         line.erase(line.begin()); n       } else { n         pair<int, int> a = *line.begin(); n         line.erase(line.begin()); n         pair<int, int> b = *line.begin(); n         line.erase(line.begin()); n         ans.push_back(make_pair(a.first, b.first)); n         line.insert( n             make_pair(min(a.second, b.second), max(a.second, b.second))); n       } n     } n     puts( YES ); n     printf( %d n , int(ans.size())); n     for (int i = 0; i < int(ans.size()); i++) { n       printf( %d %d n , ans[i].first + 1, ans[i].second + 1); n     } n   } n } n int main() { n   read(); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> ed[2000]; n int sho[1200][1200]; n int col[1200]; n int n, m; n int makk[1200]; n int sp[1200]; n void dfs(int v, int ccc) { n   sp[v] = ccc; n   for (int a = 0; a < ed[v].size(); a++) { n     if (sp[ed[v][a]] == -1) dfs(ed[v][a], ccc); n   } n } n void getsho(int v) { n   int q[3000]; n   int dp[3000]; n   int c[3000]; n   for (int a = 0; a < n; a++) c[a] = -1; n   q[0] = v; n   dp[0] = 0; n   c[v] = 1; n   int bb = 0; n   int ee = 0; n   while (bb <= ee) { n     int vx = q[bb]; n     sho[v][vx] = dp[bb]; n     for (int a = 0; a < ed[vx].size(); a++) { n       if (c[ed[vx][a]] == -1) { n         int vu = ed[vx][a]; n         c[vu] = 1; n         ee++; n         q[ee] = vu; n         dp[ee] = dp[bb] + 1; n       } n     } n     bb++; n   } n } n int isok; n void kolo(int v, int c) { n   col[v] = c; n   for (int a = 0; a < ed[v].size(); a++) { n     if (col[ed[v][a]] == c) n       isok = 0; n     else if (col[ed[v][a]] == -1) n       kolo(ed[v][a], 1 - c); n   } n } n int main() { n   cin >> n >> m; n   for (int a = 0; a < 1200; a++) makk[a] = 0; n   isok = 1; n   int x, y; n   for (int a = 0; a < n; a++) n     for (int b = 0; b < n; b++) sho[a][b] = 1000000; n   for (int a = 0; a < m; a++) { n     cin >> x >> y; n     ed[x - 1].push_back(y - 1); n     ed[y - 1].push_back(x - 1); n   } n   for (int a = 0; a < 1100; a++) col[a] = -1; n   kolo(0, 0); n   if (!isok) { n     cout << -1; n     return 0; n   } n   for (int a = 0; a < n; a++) sp[a] = -1; n   int ccc = -1; n   for (int a = 0; a < n; a++) { n     if (sp[a] == -1) { n       ccc++; n       dfs(a, ccc); n     } n   } n   for (int a = 0; a < n; a++) getsho(a); n   int mini = 0; n   for (int a = 0; a < n; a++) { n     for (int b = a + 1; b < n; b++) { n       if (sp[a] == sp[b] && sho[a][b] > makk[sp[a]] && sho[a][b] < 100000) n         makk[sp[a]] = sho[a][b]; n     } n   } n   int ss = 0; n   for (int a = 0; a < 1200; a++) ss += makk[a]; n   cout << ss; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300 * 1000 + 20; n int n, a[N]; n multiset<int> e; n long long ans; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) { n     if (e.size()) n       if (*e.begin() < a[i]) { n         ans += a[i] - *e.begin(); n         e.erase(e.begin()); n         e.insert(a[i]); n       } n     e.insert(a[i]); n   } n   return cout << ans << endl, 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long max2(long long a, long long int b) { n   return (a) > (b) ? (a) : (b); n } n inline long long max3(long long a, long long b, long long c) { n   return (a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c)); n } n inline long long min2(long long a, long long b) { n   return (a) < (b) ? (a) : (b); n } n inline long long min3(long long a, long long b, long long c) { n   return (a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c)); n } n using namespace std; n const long long modu = 998244353; n long long dr8[] = {0, 0, 1, -1, 1, 1, -1, -1}; n long long dc8[] = {1, -1, 0, 0, -1, 1, -1, 1}; n long long dr4[] = {0, 1, -1, 0}; n long long dc4[] = {1, 0, 0, -1}; n inline void deb_vi(vector<long long> a) { n   for (long long i = 0; i < a.size(); i++) cout << a[i] <<    ; n } n inline void deb_vll(vector<long long> a) { n   for (long long i = 0; i < a.size(); i++) cout << a[i] <<    ; n } n inline bool is_palindrome(const string& s) { n   return std::equal(s.begin(), s.end(), s.rbegin()); n } n using namespace std::chrono; n long long power(long long a, long long b) { n   long long result = 1; n   while (b) { n     if (b & 1) result = result * a % modu; n     a = (a * a) % modu; n     b /= 2; n     result %= modu; n   } n   return result; n } n long long fun1(string s) { n   long long ans = 0; n   long long l = s.length() - 1; n   long long exp = 0; n   for (long long i = l; i >= 0; i--) { n     ans = (s[i] -  0 ) % modu * power(10, exp) + ans; n     ans %= modu; n     exp += 2; n   } n   return ans % modu; n } n long long fun2(string s) { n   long long ans = 0; n   long long l = s.length() - 1; n   long long exp = 1; n   for (long long i = l; i >= 0; i--) { n     ans = (s[i] -  0 ) % modu * power(10, exp) + ans; n     ans %= modu; n     exp += 2; n   } n   return ans % modu; n } n int32_t main() { n   auto start = high_resolution_clock::now(); n   { n     long long n; n     cin >> n; n     string a[n]; n     long long ans = 0; n     for (long long i = 0; i < n; i++) { n       cin >> a[i]; n       long long t1 = fun1(a[i]) * n; n       t1 %= modu; n       long long t2 = fun2(a[i]) * n; n       t2 %= modu; n       ans += (t1 + t2); n       ans %= modu; n     } n     cout << (ans + modu) % modu; n   } n   auto stop = high_resolution_clock::now(); n   auto duration = duration_cast<microseconds>(stop - start); n   return 0; n } n


#include<iostream> n #include<algorithm> n #include<vector> n #include<bitset> n #include<time.h> n using namespace std; n #define R register int n bitset<200000>S[60]; n bool c[60]; n int main(){ n  srand(time(0)); n  int n,m,p,q; n  cin>>n>>m>>p; n  q=n-1>>1; n  string s; n  for(R i=0;i!=n;i++){ n   cin>>s; n   for(R j=0;j!=m;j++){ n    if(s[j]== 1 ){ n     S[j][i]=1; n    } n   } n  } n  vector<int>A,E; n  for(int i=0;i!=m;i++){ n   c[i]=false; n   if(S[i].count()>q){ n    A.push_back(i); n   } n  } n  if(A.empty()==true){ n   for(R i=0;i!=m;i++){ n    cout<<0; n   } n   return 0; n  } n  p=0; n  while(clock()<2950){ n   bitset<200000>B=S[A[p]]; n   vector<int>C,D; n   D.push_back(A[p]); n   for(R i=A.size()-1;i!=-1;i--){ n    if(i!=p){ n     C.push_back(A[i]); n    } n   } n   random_shuffle(C.begin(),C.end()); n   for(vector<int>::iterator T=C.begin();T!=C.end();T++){ n    if((B&S[*T]).count()>q){ n     B&=S[*T]; n     D.push_back(*T); n    } n   } n   if(D.size()>E.size()){ n    E=D; n   } n   p++; n   if(p==A.size()){ n    p=0; n   } n  } n  for(vector<int>::iterator T=E.begin();T!=E.end();T++){ n   c[*T]=true; n  } n  for(R i=0;i!=m;i++){ n   if(c[i]==true){ n    cout<<1; n   }else{ n    cout<<0; n   } n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 998244353; n const long long INF = 1000000000; n void solve() { n   long long n; n   string s; n   cin >> n; n   vector<long long> p(n); n   vector<long long> ans(n); n   for (int i = 0; i < n; i++) { n     long long x; n     cin >> x; n     p[--x] = i; n   } n   cin >> s; n   long long ones = 1; n   long long nuls = 1; n   for (long long i = 0; i < n; i++) n     if (s[i] ==  0 ) ++nuls; n   for (long long i = 0; i < n; i++) { n     if (s[p[i]] ==  0 ) { n       ans[p[i]] = ones; n       ++ones; n     } else { n       ans[p[i]] = nuls; n       ++nuls; n     } n   } n   for (long long i = 0; i < n; i++) cout << ans[i] <<    ; n   cout << endl; n } n int main(void) { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int t; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n   n #define send {ios_base::sync_with_stdio(false);} n #define help {cin.tie(NULL);} n #define f first n #define s second n #define pb push_back n #define vi vector<long long> n #define vii vector<pair<long long,long long>> n #define dict unordered_map<long long, long long> n #define contains(d,x) (d.find(x)!=d.end()) n  n typedef long long ll; n typedef long double lld; n typedef unsigned long long ull; n  n const lld pi = 3.14159265358979323846; n const ll mod = 1000000007; n // const ll mod = 998244353; n   n ll n, m, k, l, p, q, r, x, y, z, sz; n const ll template_array_size = 1e6 + 5; n ll a[template_array_size]; n ll b[template_array_size]; n ll c[template_array_size]; n string s, t; n ll ans = 0; n ll ts[5000000]; n  n ll power(int x, int y){ n  if(!y) return 1; n  ll temp = power(x, y/2); n  temp = (temp*temp)%mod; n  if(y&1){ n   return (temp*x)%mod; n  } n  return temp; n } n  n int find(int i, ll rep[]) { n  int t = i; n  while(rep[i]!=i){ n   i = rep[i]; n  } n  while(rep[t]!=t){ n   t = rep[t]; n   rep[t] = i; n  } n  return i; n } n  n int onion(int i, int j, ll rep[]){ n  i = find(i,rep); n  j = find(j,rep); n  rep[j] = i; n  return i!=j; n } n  n void solve(int tc = 0) { n  cin >> n >> m; n  vector<vii> adj(m+1); n  vi chosen; n  for(int i=0; i<m+1; i++) a[i] = i; n  for(int i=0; i<n; i++) { n   cin >> x; n   if (x==1) { n    cin >> y; n    x = 0; n   } else { n    cin >> x >> y; n   } n   if(onion(x,y,a)){ n    chosen.pb(i+1); n   } n  } n  p = chosen.size(); n  ans = power(2,p); n  cout << ans <<     << p << endl; n  for(int i : chosen) cout << i <<   ; n  cout << endl; n } n   n int main() { n   n     send help n  n  // int tc = 1; n  // cin >> tc; n  // for (int t = 0; t < tc; t++) solve(t); n  solve(); n }


#include <bits/stdc++.h> n int main() { n   int n, k, s = 0; n   scanf( %d%d , &n, &k); n   int a[n]; n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n   } n   for (int j = 0; j < n; j++) { n     if (n == k && a[j] >= a[k - 1] && a[j] > 0) n       s++; n     else if (a[j] >= a[k - 1] && a[j] > 0) n       s++; n   } n   printf( %d , s); n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, m[1000]; n int s = 0; n int main() { n   int n; n   cin >> n; n   int z = n; n   while (n > 0) { n     s = 0; n     cin >> a >> b; n     while (a > 0 && b > 0) { n       if (a >= b) { n         int t = a / b; n         a = a % b; n         s += t; n       } else { n         int t = b / a; n         b = b % a; n         s += t; n       } n     } n     m[z - n + 1] = s; n     --n; n   } n   int j; n   for (j = 1; j <= z; ++j) cout << m[j] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void doRoutine() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   srand(322179); n } n const int C = 5010; n int tr[C * 8]; n bool cross(pair<pair<int, int>, int> hor, pair<int, pair<int, int> > vert) { n   return (vert.second.first <= hor.second && n           hor.second <= vert.second.second) && n          (hor.first.first <= vert.first && vert.first <= hor.first.second); n } n void init() { n   for (int i = 0; i < C * 8; ++i) { n     tr[i] = 0; n   } n } n int get(int l, int r, int lb = -C, int rb = C, int v = 0) { n   if (l == lb && r == rb) { n     return tr[v]; n   } else { n     int mb = (lb + rb) / 2; n     if (r <= mb) { n       return get(l, r, lb, mb, v * 2 + 1); n     } else if (l >= mb) { n       return get(l, r, mb, rb, v * 2 + 2); n     } else { n       return get(l, mb, lb, mb, v * 2 + 1) + get(mb, r, mb, rb, v * 2 + 2); n     } n   } n } n void add(int i, int x, int lb = -C, int rb = C, int v = 0) { n   if (lb + 1 == rb) { n     tr[v] += x; n   } else { n     int mb = (lb + rb) / 2; n     if (i < mb) { n       add(i, x, lb, mb, v * 2 + 1); n     } else if (i >= mb) { n       add(i, x, mb, rb, v * 2 + 2); n     } n     tr[v] = tr[v * 2 + 1] + tr[v * 2 + 2]; n   } n } n signed main() { n   doRoutine(); n   int n; n   cin >> n; n   vector<pair<int, pair<int, int> > > verts; n   vector<pair<pair<int, int>, int> > hors; n   for (int i = 0; i < n; ++i) { n     int x1, y1, x2, y2; n     cin >> x1 >> y1 >> x2 >> y2; n     if (x1 > x2) { n       swap(x1, x2); n     } n     if (y1 > y2) { n       swap(y1, y2); n     } n     if (x1 == x2) { n       verts.push_back({x1, {y1, y2}}); n     } else { n       hors.push_back({{x1, x2}, y1}); n     } n   } n   sort(verts.begin(), verts.end()); n   long long ans = 0; n   for (int i = 0; i < verts.size(); ++i) { n     vector<pair<int, int> > events; n     for (auto seg : hors) { n       if (cross(seg, verts[i])) { n         events.push_back({seg.first.second, seg.second}); n       } n     } n     if (!events.empty()) { n       sort(events.begin(), events.end()); n       reverse(events.begin(), events.end()); n       int ptr = 0; n       init(); n       for (int j = (int)verts.size() - 1; j > i; --j) { n         while ((ptr < events.size() && events[ptr].first >= verts[j].first)) { n           add(events[ptr].second, 1); n           ++ptr; n         } n         int cnt = get(verts[j].second.first, verts[j].second.second + 1); n         ans += cnt * (cnt - 1) / 2; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( O3 ) n #pragma GCC target( avx ) n const long long INF = numeric_limits<long long>::max(); n const long double PI = 3.1415926535898; n const long long MOD = 1000000007; n const long long LIM = 100005; n long long fpow(long long x, long long y) { n   long long temp; n   if (y == 0) return 1; n   temp = fpow(x, y / 2); n   if (y % 2 == 0) n     return temp * temp; n   else n     return x * temp * temp; n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); 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 = i + 6) n     if (n % i == 0 || n % (i + 2) == 0) return false; n   return true; n } n long long modInverse(long long n, long long p) { return fpow(n, p - 2); } n map<long long, long long> primeFactors(long long n) { n   map<long long, long long> mp; n   while (n % 2 == 0) { n     if (mp.count(2) == 1) n       mp[2]++; n     else n       mp.insert({2, 1}); n     n = n / 2; n   } n   for (long long i = 3; i <= sqrt(n); i = i + 2) { n     while (n % i == 0) { n       if (mp.count(i) == 1) n         mp[i]++; n       else n         mp.insert({i, 1}); n       n = n / i; n     } n   } n   if (n > 2) { n     if (mp.count(n) == 1) n       mp[n]++; n     else n       mp.insert({n, 1}); n   } n   return mp; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   for (long long tt = 1; tt <= t; ++tt) { n     long long n, ans = 0; n     cin >> n; n     vector<long long> t(n + 1); n     for (long long i = 1; i <= n; i++) cin >> t[i]; n     t[0] = 0; n     sort(t.begin(), t.end()); n     ; n     long long dp[n + 1][2 * n + 1]; n     for (long long i = 0; i <= n; i++) n       for (long long j = 0; j <= 2 * n; j++) dp[i][j] = INF; n     for (long long i = 0; i <= 2 * n; i++) dp[0][i] = 0; n     for (long long i = 1; i <= n; i++) { n       for (long long j = i; j <= 2 * n; j++) { n         dp[i][j] = min(dp[i][j - 1], dp[i - 1][j - 1] + abs(t[i] - j)); n       } n     } n     ans = dp[n][2 * n]; n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char str[11][11]; n int f[8][20][20]; n int dx[] = {0, 1, -1, -1, 1, 1, 0, -1}, dy[] = {-1, -1, -1, 0, 0, 1, 1, 1}; n bool check() { n   memset(f, 0, sizeof f); n   for (int i = 1; i <= 10; i++) n     for (int j = 1; j <= 10; j++) { n       if (str[i][j] !=  X ) continue; n       for (int k = 0; k < 4; k++) f[k][i][j] = f[k][i + dy[k]][j + dx[k]] + 1; n     } n   for (int i = 10; i >= 1; i--) n     for (int j = 10; j >= 1; j--) { n       if (str[i][j] !=  X ) continue; n       for (int k = 4; k < 8; k++) f[k][i][j] = f[k][i + dy[k]][j + dx[k]] + 1; n     } n   for (int i = 1; i <= 10; i++) n     for (int j = 1; j <= 10; j++) n       for (int k = 0; k < 8; k++) n         if (f[k][i][j] >= 5) return 1; n   return 0; n } n int main() { n   for (int i = 1; i <= 10; i++) scanf( %s , str[i] + 1); n   for (int i = 1; i <= 10; i++) n     for (int j = 1; j <= 10; j++) { n       if (str[i][j] !=  . ) continue; n       str[i][j] =  X ; n       if (check()) { n         puts( YES ); n         return 0; n       } n       str[i][j] =  . ; n     } n   puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, l, r, mid, a; n int main() { n   cin >> n >> k; n   a = 240 - k; n   l = 0; n   r = n + 1; n   while (r - l > 1) { n     mid = (r + l) / 2; n     if (5 * (mid * (mid + 1) / 2) > a) { n       r = mid; n     } else { n       l = mid; n     } n   } n   cout << l << endl; n   return 0; n } n


//Saptak_Roy_Turja n #include<bits/stdc++.h> n #define ll long long int n using namespace std; n int main() n { n     int t; n     cin>>t; n     while(t--){ n         ll n,a,b,c=1,i; n         string s; n         cin>>n>>a>>b>>s; n         for(i=1;i<n;i++){ n             if(s[i]!=s[i-1]){ n                 c++; n             } n         } n         if(b>=0){ n             cout<<n*(a+b)<<  n ; n         } n         else { n             cout<<n*a+b*(c/2+1)<<  n ; n         } n     } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, l, r, mid, ans; n long long a[200100]; n bool check(long long x) { n   long long p, q; n   double sum = 0, s; n   for (long long i = 1; i <= n; i++) { n     if (a[i] == 0) continue; n     s = a[i]; n     if (a[i] >= k) return 1; n     p = x - 1; n     q = n - i + x - 1; n     p = min(p, q - p); n     while (p) { n       s = s * q / p; n       p--; n       q--; n       if (s >= k) return 1; n     } n     sum += s; n     if (sum >= k) return 1; n   } n   return 0; n } n int main() { n   scanf( %I64d%I64d , &n, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %I64d , &a[i]); n     if (a[i] >= k) { n       puts( 0 ); n       return 0; n     } n   } n   l = 1; n   r = k; n   while (l <= r) { n     mid = (l + r) >> 1; n     if (check(mid)) { n       ans = mid; n       r = mid - 1; n     } else n       l = mid + 1; n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const string yno[2] = { NO n ,  YES n }; n int dem(vector<int> &a, int k) { n   return upper_bound(a.begin(), a.end(), k) - n          lower_bound(a.begin(), a.end(), k); n } n int dem(long long k) { n   int ans = 0; n   while (k) { n     ans += k & 1; n     k >>= 1; n   } n   return ans; n } n void process() { n   int n, m; n   cin >> n >> m; n   vector<int> a(n), b(m); n   for (int &i : a) cin >> i; n   for (int &i : b) cin >> i; n   vector<pair<long long, long long> > c; n   map<int, pair<long long, long long> > M; n   for (int i = 0; i <= n - 1; i++) { n     for (int j = 0; j <= m - 1; j++) { n       M[a[i] + b[j]].first |= (1ll << i); n       M[a[i] + b[j]].second |= (1ll << j); n     } n   } n   for (auto it = M.begin(); it != M.end(); it++) { n     c.push_back(it->second); n   } n   int ans = 0; n   for (int i = 0; i < c.size(); i++) { n     for (int j = i; j < c.size(); j++) { n       ans = max(ans, __builtin_popcountll(c[i].first | c[j].first) + n                          __builtin_popcountll(c[i].second | c[j].second)); n     } n   } n   cout << ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   process(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 252; n int dis(int x1, int y1, int x2, int y2) { return abs(x1 - x2) + abs(y1 - y2); } n bool Gao(int x1, int y1, int x2, int y2) { n   if (dis(0, 0, x1, y1) <= max(x2, y2)) return true; n   for (int i = 1; i <= min(x2, y2); ++i) { n     if (dis(x2 - i, y2 - i, x1, y1) <= i) return true; n   } n   return false; n } n int main() { n   int x1, x2, y1, y2; n   while (cin >> x1 >> y1 >> x2 >> y2) { n     if (Gao(x1, y1, x2, y2)) n       cout <<  Polycarp  << endl; n     else n       cout <<  Vasiliy  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, p[1005]; n vector<int> T; n bool ok; n inline void enter() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> p[i]; n } n void dfs(int u) { n   while (T[u] != 1) { n     T[u]++; n     u = p[u]; n   } n   cout << u <<    ; n } n inline void solve() { n   for (int i = 1; i <= n; i++) { n     T.assign(n + 4, 0); n     dfs(i); n   } n } n int main() { n   enter(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, t; n string s; n int puc(int k, int t) { n   vector<int> v(n + 1, 0); n   v[n - 1] = (s[n - 1] ==  H ); n   for (int i = n - 2; i >= 0; --i) v[i] = v[i + 1] + (s[i] ==  H ); n   int nh = 0, ns = 0; n   for (int i = 0; i < int(n); ++i) { n     if (s[i] ==  H ) n       ++nh; n     else if (s[i] ==  S ) n       ++ns; n   } n   if (k + ns < nh) return 0; n   int zk = k; n   int ret = 0; n   int qui = -1, q = 0; n   for (int i = 0; i < n; ++i) { n     if (s[i] ==  S ) { n       ++k; n       --ns; n     } else if (s[i] ==  H ) { n       --nh; n       if (k > 0) n         --k; n       else if (qui == -1) { n         qui = i; n         q = 1; n       } else n         ++q; n     } n     if (nh == 0 and k >= q) { n       if (qui == -1 and ret < t) return true; n       if (ret + (i - qui) < t) return true; n       break; n     } n     ++ret; n   } n   k = zk; n   int ult = -1; n   int r = 0; n   int ans = 0; n   for (int i = 0; i < int(n); ++i) { n     if (s[i] ==  S ) ++k; n     if (k >= r and ult != -1) { n       ans += i - ult; n       if (v[i] > 0) ans += i - ult; n       k -= r; n       ult = -1; n       r = 0; n     } n     if (s[i] ==  H ) { n       if (k > 0) n         --k; n       else if (ult == -1) { n         ult = i; n         r = 1; n       } else n         ++r; n     } n     if (ult == -1 and v[i + 1] == 0) break; n     ++ans; n   } n   return (ult == -1 and ans < t); n } n int main() { n   cin >> n >> t; n   cin >> s; n   int lo = 0, hi = n, m; n   while (hi - lo > 5) { n     int m = (lo + hi) / 2; n     if (puc(m, t)) n       hi = m; n     else n       lo = m; n   } n   bool trobat = 0; n   if (puc(0, t)) { n     cout << 0 << endl; n     trobat = true; n   } n   for (int k = lo; !trobat and k <= hi; ++k) { n     if (puc(k, t)) { n       cout << k << endl; n       trobat = true; n     } n   } n   if (!trobat) cout << -1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const string YES =  Yes ; n const string NO =  No ; n string solve() { n   int N; n   cin >> N; n   vector<int> p(N); n   for (int i = 0; i < (N); ++i) { n     cin >> p[i]; n   } n   for (int i = 0; i < (N - 1); ++i) { n     if (p[i] + 1 == p[i + 1]) { n       continue; n     } n     if (p[i] > p[i + 1]) { n       continue; n     } n     return NO; n   } n   return YES; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int T; n   cin >> T; n   for (int t = 0; t < (T); ++t) { n     cout << solve() << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int arr[6], i, sum = 0; n   for (i = 0; i < 5; i++) { n     scanf( %d , &arr[i]); n     sum = sum + arr[i]; n   } n   if (sum == 0) { n     printf( -1 n ); n   } else if (sum % 5 == 0) { n     printf( %d , sum / 5); n   } else { n     printf( -1 ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, a, b, c, ans, boby, apu; n   cin >> n; n   while (n--) { n     cin >> a >> b >> c >> ans; n     boby = b - a; n     apu = c + ans; n     if (boby % apu == 0) { n       cout << boby / apu << endl; n     } else n       cout <<  -1  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = 1e9; n const int N = 5001; n const int maxxor = (1 << 13); n int f[2][maxxor]; n vector<int> a[N]; n int cnp(int l, int r, int pos, int id) { n   if (l > r) return oo; n   int mid = (l + r) / 2; n   if (a[id][mid] > pos) return min(a[id][mid], cnp(l, mid - 1, pos, id)); n   return cnp(mid + 1, r, pos, id); n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     a[x].push_back(i); n   } n   for (int i = 0; i <= 1; i++) n     for (int j = 0; j < maxxor; j++) f[i][j] = oo; n   f[0][0] = 0; n   int id; n   vector<int> res; n   for (int i = 1; i < N; i++) { n     int now = i & 1; n     int back = now ^ 1; n     for (int j = 0; j < maxxor; j++) { n       f[now][j] = f[back][j]; n       if (a[i].size() == 0) { n         if (i == N - 1 && f[now][j] != oo) res.push_back(j); n         continue; n       } n       int pos = f[back][j ^ i]; n       id = cnp(0, a[i].size() - 1, pos, i); n       if (id != oo) f[now][j] = min(f[now][j], id); n       if (i == N - 1 && f[now][j] != oo) res.push_back(j); n     } n   } n   cout << res.size() << endl; n   for (int xnew : res) cout << xnew <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int add(int a, int b) { n   return (a + b >= 1000000007) ? a + b - 1000000007 : a + b; n } n inline void inc(int &a, int b) { a = add(a, b); } n inline int sub(int a, int b) { n   return (a - b < 0) ? a - b + 1000000007 : a - b; n } n inline void dec(int &a, int b) { a = sub(a, b); } n inline int mul(int a, int b) { return 1ll * a * b % 1000000007; } n inline void grow(int &a, int b) { a = mul(a, b); } n const int INV = 205555557; n int phi[1000005]; n int pre[1000005]; n int f[1000005]; n int onepre[1000005]; n int twopre[1000005]; n void phiCalc() { n   phi[0] = 0; n   phi[1] = 1; n   for (int i = 2; i <= 1000005 - 1; i++) phi[i] = i; n   for (int i = 2; i <= 1000005 - 1; i++) { n     if (phi[i] == i) { n       for (int j = i; j <= 1000005 - 1; j += i) phi[j] -= phi[j] / i; n     } n   } n } n int func(int n, int m) { n   int res = sub(0, INV); n   grow(res, n); n   grow(res, n + 1); n   grow(res, n + 2); n   int tres = 15; n   inc(tres, mul(4, mul(n, mul(n, n)))); n   dec(tres, mul(12, mul(n, n))); n   dec(tres, mul(7, n)); n   int ttres = mul(n, n); n   dec(ttres, mul(3, n)); n   dec(ttres, 3); n   grow(ttres, sub(0, 6)); n   grow(ttres, m); n   return mul(res, add(tres, ttres)); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   phiCalc(); n   for (int i = 1; i < 1000005; i++) { n     for (int j = i; j < 1000005; j += i) inc(f[j], mul(i, phi[j / i])); n   } n   pre[0] = f[0]; n   onepre[0] = twopre[0] = 0; n   for (int i = 1; i < 1000005; i++) { n     pre[i] = add(pre[i - 1], f[i]); n     onepre[i] = add(onepre[i - 1], mul(i, f[i])); n     twopre[i] = add(twopre[i - 1], mul(mul(i, i), f[i])); n   } n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     if (n > m) swap(n, m); n     int ans = 0; n     inc(ans, pre[n]); n     inc(ans, twopre[n]); n     dec(ans, mul(2, onepre[n])); n     inc(ans, mul(m, mul(n, pre[n]))); n     dec(ans, mul(m, onepre[n])); n     inc(ans, mul(m, pre[n])); n     dec(ans, mul(n, onepre[n])); n     inc(ans, mul(n, pre[n])); n     grow(ans, 2); n     inc(ans, func(n, m)); n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int f = 1, x = 0; n   char s = getchar(); n   while (s <  0  || s >  9 ) { n     if (s ==  - ) f = -1; n     s = getchar(); n   } n   while (s >=  0  && s <=  9 ) { n     x = x * 10 + s -  0 ; n     s = getchar(); n   } n   return x * f; n } n int len[300 + 5]; n string str[300 + 5]; n bool vis[300 + 5][300 + 5]; n int main() { n   int n = read(), Ans; n   for (int i = 1; i <= n; i++) { n     cin >> str[i]; n     len[i] = int(str[i].length()) + len[i - 1]; n   } n   Ans = len[n] + n - 1; n   for (int i = 1; i <= n; i++) { n     for (int j = i; j <= n; j++) { n       int tmp = 0; n       if (vis[i][j]) continue; n       vis[i][j] = 1; n       for (int t = j + 1; t <= n - j + i; t++) { n         bool flag = 0; n         for (int s = 0; s <= j - i; s++) n           if (str[i + s] != str[t + s]) { n             flag++; n             break; n           } n         if (!flag) tmp++, t += j - i, vis[t][t + j - i] = 1; n       } n       if (tmp != 0) { n         int tot = len[n] - (tmp + 1) * (len[j] - len[i - 1]) + tmp + n; n         Ans = min(Ans, tot); n       } n     } n   } n   printf( %d n , Ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18, MAXN = 1e5 + 128, MOD = 998244353; n long long binpow(long long a, long long n) { n   long long res = 1; n   while (n) { n     if (n & 1) res *= a; n     a *= a; n     n >>= 1; n   } n   return res; n } n long long NOD(long long NOD1, long long NOD2) { n   while (NOD2) { n     NOD1 %= NOD2; n     swap(NOD1, NOD2); n   } n   return NOD1; n } n long long NOK(long long NOK1, long long NOK2) { n   return NOK1 * NOK2 / NOD(NOK1, NOK2); n } n long long n, i1, i2; n string s; n char a, b; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> s; n   a = s[0]; n   for (int i = 0; i < n; i++) n     if (a == s[i]) n       i1 = i; n     else n       break; n   b = s[n - 1]; n   for (int i = n - 1; i >= 0; i--) n     if (b == s[i]) n       i2 = i; n     else n       break; n   i2 = n - i2; n   if (b == a) n     cout << ((i1 + 2) * (i2 + 1)) % MOD; n   else n     cout << (i2 + i1 + 2) % MOD; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxint = 0x7fffffff; n const int minint = 0x80000000; n const double eps = 1e-6; n int gcd(int a, int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int max(int a, int b) { return a > b ? a : b; } n int main() { n   int sum, d, n, a; n   scanf( %d , &d); n   scanf( %d , &n); n   sum = 0; n   while (--n) { n     scanf( %d , &a); n     sum += d - a; n   } n   scanf( %d , &a); n   printf( %d n , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t, n; n   cin >> t; n   while (t--) { n     string A, B; n     cin >> n; n     cin >> A >> B; n     int f = 1; n     for (int i = 0; i < n; ++i) { n       if (A[i] > B[i]) f = 0; n     } n     if (!f) { n       cout << -1 <<   n ; n       continue; n     } n     int ans = 0; n     for (int i = 0; i < 26; ++i) { n       bitset<27> good; n       for (int j = 0; j < n; ++j) { n         if (A[j] == B[j]) continue; n         if (B[j] == char( a  + i)) { n           good[A[j] -  a ] = 1; n         } n       } n       ans += good.count(); n       for (int j = 0; j < n; ++j) { n         if (A[j] == B[j]) continue; n         if (good[A[j] -  a ] == 1) A[j] = char( a  + i); n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1e5 + 5; n int n, k; n vector<pair<int, int> > g[M]; n int ans; n int dp[M][2]; n void dfs(int u, int Pa) { n   vector<pair<int, int> > ch; n   for (auto p : g[u]) { n     int v = p.first, w = p.second; n     if (v == Pa) continue; n     dfs(v, u); n     ch.push_back(make_pair(dp[v][0] + w, dp[v][1] + w)); n   } n   bool flag = ch.size() > 0; n   if (flag) { n     sort(ch.begin(), ch.end(), [&](pair<int, int> a, pair<int, int> b) { n       return a.second > b.second; n     }); n   } n   for (int i = 0; i < k - 1 && i < ch.size(); i++) { n     dp[u][1] += ch[i].second; n   } n   if (ch.size() <= k) { n     for (auto p : ch) { n       dp[u][0] += p.second; n     } n     if (flag) { n       sort(ch.begin(), ch.end(), [&](pair<int, int> a, pair<int, int> b) { n         return a.first - a.second > b.first - b.second; n       }); n       dp[u][0] += ch[0].first - ch[0].second; n     } n   } else { n     int max_val = -1e9; n     for (int i = k; i < ch.size(); i++) { n       max_val = max(max_val, ch[i].first); n     } n     int sum = 0; n     for (int i = 0; i < k; i++) { n       sum += ch[i].second; n     } n     for (int i = 0; i < k; i++) { n       dp[u][0] = max(dp[u][0], sum - ch[i].second + max(max_val, ch[i].first)); n     } n   } n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i < n; i++) { n     int u, v, c; n     scanf( %d%d%d , &u, &v, &c); n     u++, v++; n     g[u].push_back(make_pair(v, c)); n     g[v].push_back(make_pair(u, c)); n   } n   dfs(1, 0); n   printf( %d n , dp[1][0]); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100002; n int arr[N], n, inc[N], decr[N]; n int tree[4 * N]; n int update(int pos, int l, int r, int x, int v) { n   if (l > x || r < x) return tree[pos]; n   if (l == r) return tree[pos] = max(tree[pos], v); n   int mid = (l + r) / 2; n   return tree[pos] = max(update(pos * 2, l, mid, x, v), n                          update(pos * 2 + 1, mid + 1, r, x, v)); n } n int get(int pos, int l, int r, int qLow, int qHigh) { n   if (l > qHigh || r < qLow) return 0; n   if (l >= qLow && r <= qHigh) return tree[pos]; n   int mid = (l + r) / 2; n   return max(get(pos * 2, l, mid, qLow, qHigh), n              get(pos * 2 + 1, mid + 1, r, qLow, qHigh)); n } n int bitonic() { n   int i, max; n   inc[1] = 1; n   update(1, 0, n, arr[1], 1); n   for (i = 2; i <= n; i++) { n     int x = get(1, 0, n, 0, arr[i]); n     inc[i] = x + 1; n     update(1, 0, n, arr[i], inc[i]); n   } n   memset(tree, 0, sizeof tree); n   decr[n] = 1; n   update(1, 0, n, arr[n], 1); n   for (i = n - 1; i > 0; i--) { n     int x = get(1, 0, n, 0, arr[i]); n     decr[i] = x + 1; n     update(1, 0, n, arr[i], decr[i]); n   } n   max = 0; n   for (i = 0; i <= n; i++) n     if (inc[i] + decr[i] - 1 > max) max = inc[i] + decr[i] - 1; n   return max; n } n int main() { n   int a, b; n   scanf( %d%d , &a, &n); n   for (int i = 0; i < a; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     arr[x]++, arr[y + 1]--; n   } n   for (int i = 1; i <= n; i++) arr[i] += arr[i - 1]; n   printf( %d , bitonic()); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   char s[50]; n   scanf( %s , s); n   int i, l = strlen(s), flag = 0; n   for (i = 0; i < l; i++) { n     if (s[i] != s[l - i - 1]) flag++; n   } n   if (flag == 2) n     printf( YES ); n   else if (flag == 0 && strlen(s) % 2 == 1) n     printf( YES ); n   else n     printf( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long l, r, f[100]; n bool check(string s, int x, int y) { n   if (s.size() % x != 0) return false; n   for (int i = 0; i + x < s.size() and i + x < y + 1; i++) n     if (s[i] != s[i + x]) return false; n   return true; n } n long long p(int x) { n   long long ans = 0; n   for (int i = 1; i <= x; i++) n     if (x % i == 0) { n       f[i] = pow(2, i - 1); n     } n   for (int j = 1; j < x; j++) { n     if (x % j != 0) continue; n     ans += f[j]; n     for (int u = j * 2; u < x; u += j) f[u] -= f[j]; n   } n   return ans; n } n long long solve(long long a) { n   long long ans = 0; n   string s =   ; n   while (a) { n     s += (a % (long long)2) +  0 ; n     a /= (long long)2; n   } n   reverse(s.begin(), s.end()); n   bool no = 0; n   for (int i = 1; i < s.size(); i++) { n     if (check(s, i, 100000)) { n       no = 1; n     } n   } n   for (int i = 1; i < s.size(); i++) { n     if (s[i] ==  0 ) continue; n     s[i] =  0 ; n     for (int j = 1; j <= i; j++) { n       f[j] = check(s, j, i); n     } n     for (int j = i + 1; j < s.size(); j++) f[j] = pow(2, j - i - 1); n     s[i] =  1 ; n     for (int j = 1; j < s.size(); j++) { n       if (s.size() % j != 0) continue; n       ans += f[j]; n       for (int u = j * 2; u < s.size(); u += j) f[u] -= f[j]; n     } n   } n   for (int i = s.size() - 1; i >= 1; i--) ans += p(i); n   ans += no; n   return ans; n } n int main() { n   scanf( %lld%lld , &l, &r); n   printf( %lld , solve(r) - solve(l - 1)); n } n


#include <bits/stdc++.h> n long long z; n int l, r, i, j, k, ans; n short p; n std::vector<short> prm; n std::vector<int> to; n short dp[3000111]; n bool done[3000111]; n void dfs(long long now, char cnt) { n   to.push_back(now); n   for (; cnt < prm.size() && now * prm[cnt] <= r; ++cnt) n     dfs(now * prm[cnt], cnt); n } n int main() { n   prm.reserve(29); n   to.reserve(3000111); n   scanf( %d %d %hd , &l, &r, &p); n   for (i = 2; i < p; ++i) { n     for (j = 2; j * j <= i; ++j) n       if (i % j == 0) break; n     if (j * j > i) prm.push_back(i); n   } n   dfs(1, 0); n   std::sort(to.begin(), to.end()); n   memset(dp, 0x3f, sizeof(dp)); n   dp[0] = 0; n   for (i = 2; i < p; ++i) n     for (j = 0, k = 1; j < to.size(); ++j) { n       for (z = to[j] * i; k < to.size() && to[k] < z; ++k) n         ; n       if (to[k] == z && dp[k] > dp[j] + 1) { n         dp[k] = dp[j] + 1; n         if (i + dp[k] <= p) done[k] = true; n       } n     } n   r = std::min(r, to.back()); n   for (i = 0; i < to.size() && to[i] < l; ++i) n     ; n   for (; i < to.size() && to[i] <= r; ++i) n     if (done[i]) ++ans; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n long long N; n long long fac(long long x) { n   long long res = 1; n   for (int i = 1; i <= x; ++i) res = res * i % mod; n   return res; n } n long long qpow(long long x, long long y) { n   long long res = 1; n   for (; y; x = x * x % mod, y >>= 1) n     if (y & 1) res = res * x % mod; n   return res; n } n int main() { n   scanf( %lld , &N); n   long long fz = fac(2 * N - 1), fm = fac(N - 1) * fac(N) % mod; n   printf( %lld n , (2 * fz % mod * qpow(fm, mod - 2) % mod - N + mod) % mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<long long int> > dp; n vector<long long int> a; n long long int check(long long int l, long long int r) { n   if (l >= r) return 0; n   if (dp[l][r]) return dp[l][r]; n   if (l + 1 == r) { n     dp[l][r] = a[r]; n     return a[r]; n   } n   dp[l][r] = -1; n   for (int m = l + 1; m < r; ++m) { n     long long int lf = check(l, m), rt = check(m, r); n     if (lf > 0 && lf == rt) { n       dp[l][r] = lf + 1; n       return lf + 1; n     } n   } n   return dp[l][r]; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int n; n   cin >> n; n   a.resize(n + 1); n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   dp.assign(n + 1, vector<long long int>(n + 1, 0)); n   vector<long long int> ans(n + 1, 1e9); n   ans[0] = 0; n   for (int i = 0; i < n; ++i) { n     for (int j = i + 1; j <= n; ++j) { n       if (check(i, j) > 0) ans[j] = min(ans[j], ans[i] + 1); n     } n   } n   cout << ans[n]; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k, B, c; n   cin >> n >> k >> B >> c; n   long long a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   long long x = *min_element(a, a + n); n   for (int i = 0; i < n; i++) a[i] = a[i] - x; n   B = min(B, 5 * c); n   long long ans = 1e18; n   for (int t = 0; t < 5; t++) { n     pair<long long, long long> b[n]; n     for (int i = 0; i < n; i++) { n       b[i] = {a[i], 0}; n       while (b[i].first % 5 != t) { n         b[i].first++; n         b[i].second += c; n       } n       b[i].first /= 5; n     } n     sort(b, b + n); n     priority_queue<long long> G; n     long long suma = 0; n     for (int i = 0; i < n; i++) { n       if (G.size() >= k - 1) { n         ans = min(ans, b[i].second + b[i].first * B * (k - 1) + suma); n       } n       G.push(b[i].second - b[i].first * B); n       suma += b[i].second - b[i].first * B; n       while (G.size() >= k) { n         suma -= G.top(); n         G.pop(); n       } n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC optimize( Ofast,no-stack-protector ) n using namespace std; n function<void(void)> ____ = []() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n }; n template <typename T> n vector<T> &operator<<(vector<T> &__container, T x) { n   __container.push_back(x); n   return __container; n } n template <typename T> n ostream &operator<<(ostream &out, vector<T> &__container) { n   for (T _ : __container) out << _ <<    ; n   return out; n } n const int MAXN = 1e5 + 7; n struct EDGE { n   int to, cap, rev; n   EDGE(){}; n   EDGE(int to, int cap, int rev) : to(to), cap(cap), rev(rev){}; n }; n vector<EDGE> G[MAXN]; n int iter[MAXN], rk[MAXN]; n void ADDEDGE(int u, int v, int cap) { n   G[u].push_back(EDGE(v, cap, (int)G[v].size())); n   G[v].push_back(EDGE(u, 0, (int)G[u].size() - 1)); n } n bool bfs() { n   memset(rk, 0, sizeof(rk)); n   memset(iter, 0, sizeof(iter)); n   rk[0] = 1; n   queue<int> que; n   que.push(0); n   while (!que.empty()) { n     int u = que.front(); n     que.pop(); n     for (auto e : G[u]) { n       if (!e.cap or rk[e.to]) continue; n       rk[e.to] = rk[u] + 1; n       que.push(e.to); n     } n   } n   return rk[MAXN - 1] != 0; n } n int dfs(int u, int flow) { n   if (u == MAXN - 1) return flow; n   for (int &i = iter[u]; i < (int)G[u].size(); i++) { n     auto &e = G[u][i]; n     if (!e.cap or rk[e.to] != rk[u] + 1) continue; n     int d = dfs(e.to, min(e.cap, flow)); n     if (d) { n       e.cap -= d; n       G[e.to][e.rev].cap += d; n       return d; n     } n   } n   return 0; n } n int Dinic() { n   int flow = 0; n   while (bfs()) { n     int d = dfs(0, 0x3f3f3f3f); n     while (d) { n       flow += d; n       d = dfs(0, 0x3f3f3f3f); n     } n   } n   return flow; n } n int n, m, idud[222][222], idlr[MAXN][222]; n char g[222][222]; n void solve() { n   scanf( %d , &n); n   scanf( %d , &m); n   int cur = 1, cnt = 0, tot = 0; n   for (int i = 1; i <= n; i++) scanf( %s , g[i] + 1); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (g[i][j] ==  # ) tot++; n   for (int i = 1; i < n; i++) n     for (int j = 1; j <= m; j++) idud[i][j] = cur++; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j < m; j++) idlr[i][j] = cur++; n   for (int i = 1; i < n; i++) n     for (int j = 1; j <= m; j++) n       if (g[i][j] ==  #  and g[i + 1][j] ==  # ) n         ADDEDGE(0, idud[i][j], 1), cnt++; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j < m; j++) n       if (g[i][j] ==  #  and g[i][j + 1] ==  # ) n         ADDEDGE(idlr[i][j], MAXN - 1, 1), cnt++; n   for (int i = 1; i < n; i++) n     for (int j = 1; j <= m; j++) { n       if (g[i][j] ==  #  and g[i + 1][j] ==  # ) { n         if (g[i][j - 1] ==  # ) ADDEDGE(idud[i][j], idlr[i][j - 1], 0x3f3f3f3f); n         if (g[i + 1][j - 1] ==  # ) n           ADDEDGE(idud[i][j], idlr[i + 1][j - 1], 0x3f3f3f3f); n         if (g[i][j + 1] ==  # ) ADDEDGE(idud[i][j], idlr[i][j], 0x3f3f3f3f); n         if (g[i + 1][j + 1] ==  # ) n           ADDEDGE(idud[i][j], idlr[i + 1][j], 0x3f3f3f3f); n       } n     } n   cout << tot - (cnt - Dinic()) <<   n ; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int in() { n   int x, y; n   y = scanf( %d , &x); n   return x; n } n const int N = -1; n int main() { n   int X = in(), d = in(); n   vector<long long> v; n   v.push_back(0); n   int b = 0; n   while (X) { n     if (X % 2) { n       v.push_back(v.back() + d); n       long long back = v.back(); n       for (int j = 0; j < b; j++) v.push_back(back + d); n     } n     X /= 2; n     b++; n   } n   cout << (int)v.size() - 1 << endl; n   for (int i = 1; i < v.size(); i++) cout << v[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int maxN = 2000; n const unsigned int mod = 998244353; n constexpr int inf = INT_MAX; n class Array { n  public: n   inline const int *begin() const { return data; } n   inline const int *end() const { return endPtr; } n   inline void push_back(const int x) { *(endPtr++) = x; } n   inline size_t size() const { return endPtr - data; } n   template <class T> n   inline void foreach (T fun) const { n     std::for_each<const int *>(data, endPtr, fun); n   } n  n  private: n   int data[maxN + 10], *endPtr = data; n }; n Array edgeIn[maxN + 10], edgeOut[maxN + 10]; n int originDegree[maxN + 10], degree[maxN + 10]; n inline void reset(const int n) { n   std::memcpy(degree, originDegree, sizeof(degree[0]) * n); n } n void readGraph(const int n) { n   for (int i = 0; i < n; ++i) { n     static char str[maxN + 10]; n     std::cin >> str; n     for (int j = 0; j < n; ++j) n       if (str[j] ==  1 ) { n         edgeIn[j].push_back(i); n         edgeOut[i].push_back(j); n         ++originDegree[i]; n       } n   } n } n bool isStrongConnect(const int n) { n   std::sort(degree, degree + n); n   int sum = 0; n   for (int i = 0; i + 1 < n; ++i) { n     sum += degree[i]; n     if (i * (i + 1) / 2 == sum) return false; n   } n   return true; n } n void reverseSet(const int n, const unsigned int set) { n   for (int i = 0; i < n; ++i) n     for (auto to : edgeOut[i]) n       if ((((set) >> (i)) & 0x01u) ^ (((set) >> (to)) & 0x01u)) { n         --degree[i]; n         ++degree[to]; n       } n } n inline bool checkSet(const int n, const unsigned int set) { n   reset(n); n   reverseSet(n, set); n   return isStrongConnect(n); n } n void reverseSingle(const int vertex) { n   edgeOut[vertex].foreach ([](int p) { ++degree[p]; }); n   edgeIn[vertex].foreach ([](int p) { --degree[p]; }); n   degree[vertex] = edgeIn[vertex].size(); n } n inline bool checkSingle(const int n, const int vertex) { n   reset(n); n   reverseSingle(vertex); n   return isStrongConnect(n); n } n unsigned int factor(const unsigned int n) { n   unsigned int ret = 1; n   for (unsigned int i = 2; i <= n; ++i) ret = (ret * i) % mod; n   return ret; n } n std::pair<int, int> enumSet(const int n) { n   int count = inf, solution = 0; n   const unsigned int msk = 1u << n; n   for (unsigned int i = 1; i < msk; ++i) { n     const int cnt = __builtin_popcount(i); n     if (cnt <= count && checkSet(n, i)) { n       if (cnt == count) n         ++solution; n       else { n         solution = 1; n         count = cnt; n       } n     } n   } n   return std::make_pair(count, n                         count != inf ? (solution * factor(count)) % mod : 0); n } n std::pair<int, int> enumVertex(const int n) { n   int solution = 0; n   for (int i = 0; i < n; ++i) solution += checkSingle(n, i); n   return std::make_pair(1, solution); n } n int main() { n   std::ios::sync_with_stdio(false); n   int n; n   std::cin >> n; n   readGraph(n); n   reset(n); n   if (isStrongConnect(n)) { n     std::cout <<  0 1 n ; n     return 0; n   } n   const auto [count, solution] = n <= 6 ? enumSet(n) : enumVertex(n); n   if (count == inf) n     std::cout <<  -1 n ; n   else n     std::cout << count <<     << solution <<   n ; n   return 0; n } n


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


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


#include <bits/stdc++.h> n using namespace std; n inline long long add(long long a, long long b) { n   return ((a % 1000000007LL) + (b % 1000000007LL)) % 1000000007LL; n } n inline long long mul(long long a, long long b) { n   return ((a % 1000000007LL) * (b % 1000000007LL)) % 1000000007LL; n } n inline long long sub(long long a, long long b) { n   return ((a % 1000000007LL) - (b % 1000000007LL) + 1000000007LL) % n          1000000007LL; n } n void prefixInversionAndSwap(string &ss, long long x) { n   for (long long i = 0; i <= x; i++) { n     if (ss[i] ==  0 ) { n       ss[i] =  1 ; n     } else { n       ss[i] =  0 ; n     } n   } n   for (long long i = 0; i <= (x / 2); i++) { n     swap(ss[i], ss[x - i]); n   } n   return; n } n void Flash() { n   long long n; n   cin >> n; n   string a, b; n   cin >> a >> b; n   vector<long long> ans; n   long long swapped = 0; n   long long ind = 0, nai, bbit; n   long long firstBit = a[0] -  0 ; n   for (long long i = (n - 1); i > 0; i--) { n     if (swapped) { n       nai = (a[ind - i] -  0 ); n       nai ^= 1; n     } else { n       nai = (a[ind + i] -  0 ); n     } n     bbit = b[i] -  0 ; n     if (nai != bbit) { n       if (nai == 1) { n         if (firstBit == 0) { n           ans.push_back(1); n         } n         ans.push_back(i + 1); n         if (swapped) { n           ind -= i; n         } else { n           ind += i; n         } n         swapped ^= 1; n         firstBit = (nai ^ 1); n       } else { n         if (firstBit == 1) { n           ans.push_back(1); n         } n         ans.push_back(i + 1); n         if (swapped) { n           ind -= i; n         } else { n           ind += i; n         } n         swapped ^= 1; n         firstBit = (nai ^ 1); n       } n     } n   } n   if (firstBit != (b[0] -  0 )) { n     ans.push_back(1); n   } n   cout << ans.size() <<    ; n   for (auto it : ans) { n     cout << it <<    ; n   } n   cout <<   n ; n } n int main(int argc, char const *argv[]) { n   ios::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL); n   ; n   long long t = 1; n   cin >> t; n   for (long long i = 1; i <= t; i++) { n     Flash(); n   } n   return 0; n } n


#include <bits/stdc++.h> n #define int long long n using namespace std; n  n pair<int, int> extgcd(int a, int b) { n     if(!b) return {1, 0}; n     pair<int, int> val = extgcd(b, a % b); n     return {val.second, val.first - (a / b) * val.second}; n } n  n signed main(){ n     ios_base::sync_with_stdio(false); n     cin.tie(0); n     int t; cin >> t; n     while(t--){ n         int n; cin >> n; n         string s; cin >> s; n         int cnt = 0, cnt2 = 0; n         for(int i = 0; i < n; ++i){ n             if(s[i] ==  1  && s[i + 1] ==  1 ){ n                 ++cnt; ++i; n             } n             else if(s[i] ==  1 ){ n                 ++cnt2; n             } n         } n         int ans = 1, mod = 998244353, can = n - 1 - cnt2; n         for(int i = n - cnt - cnt2; i > cnt; --i){ n             ans *= i; ans %= mod; n         } n         for(int i = n - cnt - cnt - cnt2; i >= 1; --i){ n             ans *= (extgcd(i, mod).first + mod) % mod; ans %= mod; n         } n         cout << ans <<   n ; n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n int countbit(int n) { return (n == 0) ? 0 : (1 + countbit(n & (n - 1))); } n int lowbit(int n) { return (n ^ (n - 1)) & n; } n const double pi = acos(-1.0); n const double eps = 1e-11; n template <class T> n T sqr(T x) { n   return x * x; n } n int n, m, k; n int c[101], clen; n bool Nim(int k) { n   for (int i = 0; i < 8; ++i) { n     int sum = 0; n     for (int j = 0; j < clen; ++j) sum += (c[j] >> i) & 1; n     if (sum % (k + 1)) return true; n   } n   return false; n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   int aw = 0, bw = 0; n   for (int i = 0; i < n; ++i) { n     char str[101]; n     char *pos, *pos1, *pos2; n     scanf( %s , str); n     pos = strchr(str,  - ); n     pos2 = strchr(str,  R ); n     pos1 = strchr(str,  G ); n     if (pos && !pos1 && pos2) bw = 1; n     if (pos && !pos2 && pos1) aw = 1; n     if (pos1 && pos2) c[clen++] = abs(pos1 - pos2) - 1; n   } n   if (aw && bw) { n     printf( Draw n ); n     return 0; n   } else if (aw) { n     printf( First n ); n     return 0; n   } else if (bw) { n     printf( Second n ); n     return 0; n   } n   if (Nim(k)) n     printf( First n ); n   else n     printf( Second n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class C> n inline void mini(C& a4, C b4) { n   a4 = min(a4, b4); n } n template <class C> n inline void maxi(C& a4, C b4) { n   a4 = max(a4, b4); n } n const int INF = 1e9 + 7; n const int base = 1e9; n const int SIGMA = 26; n const int N = 1e5 + 7; n int n; n string s; n vector<int> ans; n void rec(int v) { n   if (v == n) return; n   if (s[v] ==  l ) { n     ans.push_back(v); n     rec(v + 1); n   } else { n     rec(v + 1); n     ans.push_back(v); n   } n } n int main() { n   std::ios_base::sync_with_stdio(0); n   cout << setprecision(15) << fixed; n   cin >> s; n   n = ((int)(s).size()); n   rec(0); n   reverse((ans).begin(), (ans).end()); n   for (__typeof((ans).begin()) i = (ans).begin(); i != (ans).end(); ++i) n     printf( %d n , *i + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n bool cmax(int &a, int b) { return (a < b) ? a = b, 1 : 0; } n bool cmin(int &a, int b) { return (a > b) ? a = b, 1 : 0; } n template <typename T> n T read() { n   T ans = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch) && ch !=  - ) ch = getchar(); n   if (ch ==  - ) f = -1, ch = getchar(); n   while (isdigit(ch)) n     ans = (ans << 3) + (ans << 1) + (ch -  0 ), ch = getchar(); n   return ans * f; n } n void file() {} n int n, dep[N]; n int same; n void input() { n   n = read<int>(); n   for (register int i = (int)1; i <= (int)n + 1; ++i) { n     dep[i] = read<int>(); n     if (dep[i] > 1 && dep[i - 1] > 1) same = i; n   } n } n void work() { n   int old = 0, now = 0; n   if (!same) { n     puts( perfect ); n     return; n   } n   puts( ambiguous ); n   for (register int i = (int)1; i <= (int)n + 1; ++i) { n     for (register int j = (int)1; j <= (int)dep[i]; ++j) { n       now++; n       printf( %d  , old); n     } n     old = now; n   } n   printf(  n ); n   old = now = 0; n   for (register int i = (int)1; i <= (int)n + 1; ++i) { n     for (register int j = (int)1; j <= (int)dep[i]; ++j) { n       now++; n       if (i == same) { n         printf( %d  , old - 1); n         same = 0; n       } else n         printf( %d  , old); n     } n     old = now; n   } n } n int main() { n   file(); n   input(); n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using pii = pair<int, int>; n constexpr int N = 2e5 + 5; n int t, a, b, p; n string s; n void Solve() { n   cin >> t; n   while (t--) { n     cin >> a >> b >> p >> s; n     s.pop_back(); n     vector<ll> suf; n     suf.resize(s.size()); n     char last =  0 ; n     for (int i = s.size() - 1; i >= 0; i--) { n       if (s[i] != last) suf[i] = (s[i] ==  A  ? a : b); n       last = s[i]; n     } n     for (int i = suf.size() - 2; i >= 0; i--) suf[i] += suf[i + 1]; n     int id = 0; n     while (id < suf.size() && suf[id] > p) id++; n     cout << id + 1 <<   n ; n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   Solve(); n   return 0; n } n


#include <bits/stdc++.h> n const int N = 1e5 + 1e4; n struct edg { n   int now, to, val; n } ed[N]; n struct edge { n   int to, next; n } e[N]; n int ehead[N], ecnt; n inline void add_edge(int now, int to) { n   ecnt++; n   e[ecnt].to = to; n   e[ecnt].next = ehead[now]; n   ehead[now] = ecnt; n } n int n, m, left, rig, mid; n int pos[N], ans[N], acnt, pcnt; n void dfs(int now) { n   pos[now] = 1; n   for (int i = ehead[now]; i; i = e[i].next) n     if (pos[e[i].to] == 0) dfs(e[i].to); n   pos[now] = ++pcnt; n } n inline bool check(int lim) { n   ecnt = acnt = 0; n   memset(ehead, 0, sizeof(ehead)); n   memset(pos, 0, sizeof(pos)); n   for (int i = 1; i <= m; i++) n     if (ed[i].val > lim) add_edge(ed[i].now, ed[i].to); n   for (int i = 1; i <= n; i++) n     if (!pos[i]) dfs(i); n   for (int i = 1; i <= m; i++) n     if (pos[ed[i].now] < pos[ed[i].to]) n       if (ed[i].val > lim) n         return false; n       else n         ans[++acnt] = i; n   return true; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) n     scanf( %d%d%d , &ed[i].now, &ed[i].to, &ed[i].val); n   rig = 1e9; n   while (left != rig) { n     mid = (left + rig) >> 1; n     if (check(mid)) n       rig = mid; n     else n       left = mid + 1; n   } n   check(left); n   printf( %d %d n , left, acnt); n   for (int i = 1; i <= acnt; i++) printf( %d  , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int N = 2e5 + 10; n int beg[N]; n int nxt[N]; n int to[N]; n int tot; n int a[N]; n void add(int x, int y) { n   ++tot; n   nxt[tot] = beg[x]; n   to[tot] = y; n   beg[x] = tot; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, m, k; n   cin >> n >> m >> k; n   for (int i = 1; i <= n; ++i) { n     int x; n     cin >> x; n     add(x, i); n   } n   int ans = 0; n   for (int i = 1; i <= m; ++i) { n     int siz = 0; n     for (int j = beg[i]; j; j = nxt[j]) a[++siz] = to[j]; n     if (siz == 0) continue; n     reverse(a + 1, a + 1 + siz); n     for (int r = 1, l = 1; r <= siz; ++r) { n       for (; l < r && a[r] - a[l] + 1 > k + r - l + 1; ++l) n         ; n       ans = max(ans, r - l + 1); n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, k, p, x; n   cin >> n >> k >> p; n   std::vector<int> even, odd; n   for (int i = 0; i < n; ++i) { n     cin >> x; n     if (x % 2) n       odd.emplace_back(x); n     else n       even.emplace_back(x); n   } n   if (odd.size() < k - p || (odd.size() - k - p) % 2 != 0) { n     cout <<  NO ; n     return 0; n   } n   if ((even.size() + (odd.size() - (k - p)) / 2) < p) { n     cout <<  NO  <<   n ; n     return 0; n   } n   cout <<  YES  <<   n ; n   long long cnt = 0; n   if (k - p) { n     while (even.size() > 0 && cnt != p) { n       cout << 1 <<     << even.back() <<   n ; n       even.pop_back(); n       cnt++; n     } n     while (odd.size() > 0 && cnt != p) { n       cout << 2 <<     << odd.back() <<    ; n       odd.pop_back(); n       cout << odd.back() <<   n ; n       odd.pop_back(); n       cnt++; n     } n     for (int i = 0; i < k - p - 1; i++) { n       vector<int> ans; n       while (even.size() > 0) { n         ans.emplace_back(even.back()); n         even.pop_back(); n       } n       ans.emplace_back(odd.back()); n       odd.pop_back(); n       cout << ans.size() <<    ; n       for (auto i : ans) { n         cout << i <<    ; n       } n       cout <<   n ; n     } n     vector<int> ans; n     while (odd.size() > 0) { n       ans.emplace_back(odd.back()); n       odd.pop_back(); n     } n     while (even.size() > 0) { n       ans.emplace_back(even.back()); n       even.pop_back(); n     } n     cout << ans.size() <<    ; n     for (auto i : ans) { n       cout << i <<    ; n     } n     cout <<   n ; n   } else { n     vector<int> ans; n     for (int i = 0; i < p - 1; i++) { n       if (even.size() > 0) { n         cout << 1 <<     << even.back() <<   n ; n         even.pop_back(); n       } else { n         cout << 2 <<     << odd.back() <<    ; n         odd.pop_back(); n         cout << odd.back() <<    ; n         odd.pop_back(); n       } n     } n     while (even.size() > 0) { n       ans.emplace_back(even.back()); n       even.pop_back(); n     } n     while (odd.size()) { n       ans.emplace_back(odd.back()); n       odd.pop_back(); n     } n     cout << ans.size() <<    ; n     for (auto I : ans) { n       cout << I <<    ; n     } n   } n } n


#include <bits/stdc++.h> n template <class T> n int gmax(T &a, T b) { n   if (b > a) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n int gmin(T &a, T b) { n   if (b < a) { n     a = b; n     return 1; n   } n   return 0; n } n using namespace std; n string to_string(string s) { return     + s +    ; } n string to_string(const char *s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto &x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n void debug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   debug_out(T...); n } n const int MAXN = 2e5 + 5; n const int MOD = 998244353; n int a[MAXN]; n int b[MAXN]; n int id[MAXN]; n long long cnt[MAXN]; n int cmp(int x, int y) { return cnt[x] > cnt[y]; } n int main() { n   int n; n   while (~scanf( %d , &n)) { n     for (int i = 0; i < n; ++i) scanf( %d , &a[i]); n     for (int i = 0; i < n; ++i) scanf( %d , &b[i]); n     sort(b, b + n); n     for (int i = 0; i < n; ++i) { n       id[i] = i; n       cnt[i] = 1ll * (i + 1) * (n - i) * a[i]; n     } n     sort(id, id + n, cmp); n     long long result = 0; n     for (int i = 0; i < n; ++i) { n       result = (result + cnt[id[i]] % MOD * b[i] % MOD) % MOD; n     } n     printf( %lld n , result); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5 + 10; n const int MOD = 998244353; n int n, q; n long long seg[4 * MAXN], mul[4 * MAXN], plu[4 * MAXN]; n set<pair<int, int>> number[MAXN]; n void shift(int l, int r, int id) { n   int mid = l + r >> 1; n   seg[id << 1] = (seg[id << 1] * mul[id]) % MOD; n   seg[id << 1] = (seg[id << 1] + plu[id] * (mid - l)) % MOD; n   seg[id << 1 | 1] = (seg[id << 1 | 1] * mul[id]) % MOD; n   seg[id << 1 | 1] = (seg[id << 1 | 1] + plu[id] * (r - mid)) % MOD; n   mul[id << 1] = (mul[id << 1] * mul[id]) % MOD; n   plu[id << 1] = (plu[id << 1] * mul[id]) % MOD; n   mul[id << 1 | 1] = (mul[id << 1 | 1] * mul[id]) % MOD; n   plu[id << 1 | 1] = (plu[id << 1 | 1] * mul[id]) % MOD; n   plu[id << 1] = (plu[id << 1] + plu[id]) % MOD; n   plu[id << 1 | 1] = (plu[id << 1 | 1] + plu[id]) % MOD; n   mul[id] = 1; n   plu[id] = 0; n } n long long seg_get(int s, int e, int l = 0, int r = n, int id = 1) { n   if (l >= s && r <= e) return seg[id]; n   if (l >= e || r <= s) return 0; n   shift(l, r, id); n   int mid = l + r >> 1; n   return (seg_get(s, e, l, mid, id << 1) + seg_get(s, e, mid, r, id << 1 | 1)) % n          MOD; n } n void seg_do(int s, int e, int l = 0, int r = n, int id = 1) { n   if (l >= s && r <= e) { n     seg[id] = (seg[id] + (r - l)) % MOD; n     plu[id] = (plu[id] + 1) % MOD; n     return; n   } n   if (l >= e || r <= s) return; n   int mid = l + r >> 1; n   shift(l, r, id); n   seg_do(s, e, l, mid, id << 1); n   seg_do(s, e, mid, r, id << 1 | 1); n   seg[id] = (seg[id << 1] + seg[id << 1 | 1]) % MOD; n   return; n } n void seg_mul(int s, int e, int l = 0, int r = n, int id = 1) { n   if (s == e) return; n   if (l >= s && r <= e) { n     seg[id] = (seg[id] * 2) % MOD; n     plu[id] = (plu[id] * 2) % MOD; n     mul[id] = (mul[id] * 2) % MOD; n     return; n   } n   if (l >= e || r <= s) return; n   int mid = l + r >> 1; n   shift(l, r, id); n   seg_mul(s, e, l, mid, id << 1); n   seg_mul(s, e, mid, r, id << 1 | 1); n   seg[id] = (seg[id << 1] + seg[id << 1 | 1]) % MOD; n   return; n } n void handle(int l, int r, int x) { n   auto itl = number[x].upper_bound({l, -2}); n   itl--; n   if ((*itl).second <= l) itl++; n   auto itr = number[x].lower_bound({r, -2}); n   if ((*itl).first > l) { n     seg_do(l, min(r, (*itl).first)); n   } n   for (auto it = itl; it != itr; it++) { n     int L = max(l, (*it).first); n     int R = min(r, (*it).second); n     seg_mul(L, R); n     if ((*it).second >= r) continue; n     auto it2 = it; n     it2++; n     L = (*it).second; n     R = r; n     R = min(R, (*it2).first); n     seg_do(L, R); n   } n   l = min(l, (*itl).first); n   itr--; n   r = max(r, (*itr).second); n   itr++; n   number[x].erase(itl, itr); n   number[x].insert({l, r}); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> q; n   for (int i = 0; i < 4 * MAXN; i++) mul[i] = 1; n   for (int i = 0; i <= n; i++) { n     number[i].insert({-1, -1}); n     number[i].insert({n, n}); n   } n   while (q--) { n     int tp; n     cin >> tp; n     if (tp == 1) { n       int l, r, x; n       cin >> l >> r >> x; n       handle(l - 1, r, x); n     } else { n       int l, r; n       cin >> l >> r; n       cout << seg_get(l - 1, r) <<   n ; n     } n   } n   cin >> n; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[2210], b[2210]; n long long inf = 1e13; n int main() { n   memset(a, 0, sizeof(a)); n   memset(b, 0, sizeof(b)); n   int n = 0, k = 0; n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; ++i) { n     scanf( %I64d , &a[i]); n   } n   for (int i = 1; i <= n; ++i) { n     scanf( %I64d , &b[i]); n   } n   long long l = 0, r = inf, res = 0; n   while (l <= r) { n     long long mid = (l + r) >> 1; n     priority_queue<long long, vector<long long>, greater<long long>> qa; n     priority_queue<long long> qb; n     long long sz = 0, cur = 0; n     for (int i = 1; i <= n; ++i) { n       qa.push(a[i]); n       long long tmp1 = qa.top() + b[i] - mid; n       long long tmp2 = qb.empty() ? inf : (b[i] - qb.top()); n       if (tmp1 <= tmp2 && tmp1 <= 0) { n         cur += tmp1; n         ++sz; n         qb.push(b[i]); n         qa.pop(); n       } else if (tmp2 < tmp1 && tmp2 < 0) { n         cur += tmp2; n         qb.pop(); n         qb.push(b[i]); n       } n     } n     if (sz >= k) { n       res = cur + (long long)k * mid; n       r = mid - 1; n     } else { n       l = mid + 1; n     } n   } n   printf( %I64d , res); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-8; n const int iinf = INT_MAX; n const long long linf = 2e18; n const double dinf = 1e30; n const int MOD = 1000000007; n inline int read() { n   int X = 0, w = 0; n   char ch = 0; n   while (!isdigit(ch)) { n     w |= ch ==  - ; n     ch = getchar(); n   } n   while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar(); n   return w ? -X : X; n } n void write(int x) { n   if (x < 0) putchar( - ), x = -x; n   if (x > 9) write(x / 10); n   putchar(x % 10 +  0 ); n } n void print(int x) { n   cout << x <<   n ; n   exit(0); n } n void PRINT(string x) { n   cout << x <<   n ; n   exit(0); n } n void douout(double x) { printf( %lf n , x + 0.0000000001); } n long long ans = 1e18, a, b, mx = 1e18; n int main() { n   cin >> a >> b; n   for (long long i = 1; i * i <= (a + b); i++) { n     if (a % i == 0) mx = min(mx, a / i); n     if (b % i == 0) mx = min(mx, b / i); n     if ((a + b) % i == 0 && (a + b) / i >= mx) n       ans = min(ans, 2 * i + 2 * ((a + b) / i)); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, p; n     cin >> n >> p; n     int cnt = 0; n     for (int i = 1; i < n; ++i) { n       for (int j = i + 1; j <= n; ++j) { n         if (cnt == 2 * n + p) break; n         cout << i <<     << j <<   n ; n         cnt++; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double getangle(pair<int, int> a, pair<int, int> b) {} n int main() { n   int n; n   cin >> n; n   double arr[n + 1]; n   for (int i = 0; i < n; ++i) { n     int x, y; n     cin >> x >> y; n     arr[i] = atan2(x, y); n     if (arr[i] < 0) arr[i] += 2.0 * M_PI; n     arr[i] *= 180.0 / M_PI; n   } n   sort(arr, arr + n); n   double a = 0.0; n   if (n <= 2) n     a = min(360.0 - (arr[0] - arr[n - 1] + 360.0), n             360.0 - (arr[n - 1] - arr[0])); n   else { n     a = 360.0 - (arr[0] - arr[n - 1] + 360.0); n     for (int i = 0; i < n - 1; ++i) a = min(a, 360.0 - (arr[i + 1] - arr[i])); n   } n   printf( %.8lf n , a); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string aux; n   cin >> aux; n   int flag_Invalid_Input = 0; n   int memo[10] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512}; n   int counter = 0; n   int len = aux.size(); n   int i, j; n   for (i = 0; i < len; i++) { n     if (aux[i] >  1 ) { n       for (j = 0; j < len - i; j++) { n         counter += memo[j]; n       } n       break; n     } n     if (aux[i] ==  1 ) { n       counter += memo[len - i - 1]; n     } n   } n   cout << counter; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 210010; n const int mod = 1e9 + 7; n int dp[110][maxn], sum[maxn]; n int a, b, k, t; n int main() { n   scanf( %d%d%d%d , &a, &b, &k, &t); n   dp[0][0] = 1; n   for (int i = 1; i <= t; i++) { n     for (int j = 0; j < maxn; j++) n       sum[j] = ((j > 0 ? sum[j - 1] : 0) + dp[i - 1][j]) % mod; n     for (int j = 0; j < maxn; j++) n       dp[i][j] = n           (sum[j] - (j - 2 * k - 1 >= 0 ? sum[j - 2 * k - 1] : 0) + mod) % mod; n   } n   for (int i = 0; i < maxn; i++) { n     sum[i] = ((i > 0 ? sum[i - 1] : 0) + dp[t][i]) % mod; n   } n   int ans = 0; n   for (int i = 0; i <= 2 * k * t; i++) { n     (ans += (1LL) * dp[t][i] * (i + a - b - 1 < 0 ? 0 : sum[i + a - b - 1]) % n             mod) %= mod; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void dfs(long long n, long long i, n          unordered_map<long long, vector<long long>> &adj, vector<bool> &vis, n          vector<long long> &a1, vector<long long> &a2) { n   if (vis[n]) return; n   vis[n] = true; n   if (i == 0) n     a1.push_back(n); n   else n     a2.push_back(n); n   for (auto &it : adj[n]) { n     if (!vis[it]) dfs(it, i ^ 1, adj, vis, a1, a2); n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     long long n, m; n     cin >> n >> m; n     unordered_map<long long, vector<long long>> adj; n     vector<bool> vis(n + 1, false); n     vector<long long> a1, a2; n     for (long long i = 0; i < m; i++) { n       long long x, y; n       cin >> x >> y; n       adj[x].push_back(y); n       adj[y].push_back(x); n     } n     queue<pair<long long, long long>> q; n     q.push({1, 0}); n     while (q.size() > 0) { n       auto it = q.front(); n       q.pop(); n       long long x, y; n       tie(x, y) = it; n       if (vis[x]) continue; n       vis[x] = true; n       if (y == 0) n         a1.push_back(x); n       else n         a2.push_back(x); n       for (auto &it : adj[x]) { n         if (!vis[it]) q.push({it, y ^ 1}); n       } n     } n     if (a1.size() <= a2.size()) { n       cout << a1.size() <<   n ; n       for (auto x : a1) cout << x <<    ; n       cout << endl; n       ; n     } else { n       cout << a2.size() <<   n ; n       for (auto x : a2) cout << x <<    ; n       cout << endl; n       ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI = acos(-1); n struct Point { n   long long x, y, r; n   Point() {} n   Point(long long a, long long b, long long c) { n     x = a; n     y = b; n     r = c; n   } n }; n long double rad; n long double cor(long double angle) { n   if (angle < 0) { n     return angle + 2 * PI; n   } n   return angle; n } n pair<long double, long double> fnd(Point& p) { n   long double kor = sqrt(p.x * p.x + p.y * p.y); n   if (kor > rad + p.r) { n     return {20, 20}; n   } n   if (rad - p.r > kor) { n     return {20, 20}; n   } else { n     long double an1 = n         acos((rad * rad + p.x * p.x + p.y * p.y - p.r * p.r) / (2 * rad * kor)); n     long double tmp = sqrt(p.x * p.x + p.y * p.y - p.r * p.r); n     long double l = cor(atan2(p.y, p.x)); n     return {l - an1, l + an1}; n   } n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long double d; n   long long n; n   cin >> n; n   cin >> d; n   rad = 0; n   vector<Point> sp(n); n   long long x, y, r; n   for (long long i = 0; i < n; ++i) { n     cin >> x >> y >> r; n     sp[i].x = x; n     sp[i].y = y; n     sp[i].r = r; n   } n   vector<pair<long double, long long> > al(0); n   for (long long kol = 0; kol < 2850; ++kol) { n     rad += d; n     for (long long i = 0; i < n; ++i) { n       pair<long double, long double> now = fnd(sp[i]); n       if (now.first == 20) { n         continue; n       } else if (now.first == 30) { n         continue; n       } else if (now.first < 0 && now.second > 0) { n         al.push_back({cor(now.first), 0}); n         al.push_back({2 * PI, 1}); n         al.push_back({0, 0}); n         al.push_back({now.second, 1}); n       } else if (now.second > 2 * PI) { n         al.push_back({now.first, 0}); n         al.push_back({2 * PI, 1}); n         al.push_back({0, 0}); n         al.push_back({now.second - 2 * PI, 1}); n       } else { n         al.push_back({now.first, 0}); n         al.push_back({now.second, 1}); n       } n     } n   } n   sort(al.begin(), al.end()); n   long long now = 0; n   long long mx = 0; n   for (auto p : al) { n     if (p.second == 0) { n       ++now; n     } else { n       --now; n     } n     mx = max(mx, now); n   } n   cout << mx << 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 maxn = n * n - 1; n   int c = 0; n   for (int i = 0; i <= maxn; i++) { n     if (i % 2 == 0) { n       cout << i <<    ; n       c++; n     } n     if (c == n) { n       cout << endl; n       c = 0; n     } n   } n   for (int i = 0; i <= maxn; i++) { n     if (i % 2 == 1) { n       cout << i <<    ; n       c++; n     } n     if (c == n) { n       cout << endl; n       c = 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k, x, i, sum, j, r = 1, B[20][20]; n char A[20][20]; n int main() { n   cin >> n >> m; n   for (i = 0; i < n; i++) { n     cin >> x; n     sum += x; n   } n   if (sum == 0) n     cout << 0; n   else { n     if (abs(sum) <= m) n       cout <<  1 ; n     else n       cout << (abs(sum) / m) + (abs(sum) % m != 0); n   } n   return 0; n } n