#include <bits/stdc++.h> n using namespace std; n const int maxn = 2005; n const int maxz = 16005; n int l[maxn], r[maxn], n, l1, l2; n int dp[maxn][maxz], q[maxz]; n bool lt[maxn][maxz]; n char s[maxn]; n struct ac_machine { n   int ch[maxz][10], cnt; n   int fail[maxz]; n   int tp[maxz][maxn]; n   void init() { n     int x = 0, y = 0; n     if (l1 == l2) { n       for (int i = 1; i <= l1; ++i) { n         if (x == y) { n           for (int j = l[i] + 1; j < r[i]; ++j) { n             if (!ch[x][j]) ch[x][j] = ++cnt; n             tp[ch[x][j]][l1 - i]++; n           } n         } else { n           for (int j = l[i] + 1; j <= 9; ++j) { n             if (!ch[x][j]) ch[x][j] = ++cnt; n             tp[ch[x][j]][l1 - i]++; n           } n           for (int j = (i == 1); j < r[i]; ++j) { n             if (!ch[y][j]) ch[y][j] = ++cnt; n             tp[ch[y][j]][l2 - i]++; n           } n         } n         if (!ch[x][l[i]]) ch[x][l[i]] = ++cnt; n         x = ch[x][l[i]]; n         if (!ch[y][r[i]]) ch[y][r[i]] = ++cnt; n         y = ch[y][r[i]]; n       } n       tp[x][0]++; n       if (x != y) tp[y][0]++; n     } else { n       for (int i = 1; i <= l1; ++i) { n         for (int j = l[i] + 1; j <= 9; ++j) { n           if (!ch[x][j]) ch[x][j] = ++cnt; n           tp[ch[x][j]][l1 - i]++; n         } n         if (!ch[x][l[i]]) ch[x][l[i]] = ++cnt; n         x = ch[x][l[i]]; n       } n       for (int i = 1; i <= l2; ++i) { n         for (int j = (i == 1); j < r[i]; ++j) { n           if (!ch[y][j]) ch[y][j] = ++cnt; n           tp[ch[y][j]][l2 - i]++; n         } n         if (!ch[y][r[i]]) ch[y][r[i]] = ++cnt; n         y = ch[y][r[i]]; n       } n       for (int i = l1 + 1; i < l2; ++i) n         for (int j = 1; j <= 9; ++j) { n           if (!ch[0][j]) ch[0][j] = ++cnt; n           tp[ch[0][j]][i - 1]++; n         } n       tp[x][0]++; n       tp[y][0]++; n     } n   } n   void build() { n     int h = 1, t = 0; n     for (int i = 0; i <= 9; ++i) n       if (ch[0][i]) q[++t] = ch[0][i]; n     int x; n     while (h <= t) { n       x = q[h++]; n       for (int i = 0; i <= 9; ++i) n         if (!ch[x][i]) n           ch[x][i] = ch[fail[x]][i]; n         else n           fail[ch[x][i]] = ch[fail[x]][i], q[++t] = ch[x][i]; n     } n     for (int i = 1; i <= t; ++i) { n       x = q[i]; n       for (int j = 0; j <= n; ++j) tp[x][j] += tp[fail[x]][j]; n     } n     for (int i = 1; i <= cnt; ++i) n       for (int j = 1; j <= n; ++j) tp[i][j] += tp[i][j - 1]; n   } n } t; n int main() { n   t.cnt = 0; n   scanf( %s , s + 1); n   l1 = strlen(s + 1); n   for (int i = 1; i <= l1; i++) l[i] = s[i] -  0 ; n   scanf( %s , s + 1); n   l2 = strlen(s + 1); n   for (int i = 1; i <= l2; i++) r[i] = s[i] -  0 ; n   scanf( %d , &n); n   memset(dp, -1, sizeof(dp)); n   dp[0][0] = 0; n   t.init(); n   t.build(); n   for (int i = 0; i < n; ++i) n     for (int j = 0; j <= t.cnt; ++j) { n       if (dp[i][j] == -1) continue; n       for (int k = 0; k <= 9; ++k) { n         int x = t.ch[j][k]; n         dp[i + 1][x] = max(dp[i + 1][x], dp[i][j] + t.tp[x][n - i - 1]); n       } n     } n   int ans = 0; n   for (int i = 0; i <= t.cnt; ++i) ans = max(ans, dp[n][i]); n   printf( %d n , ans); n   for (int i = 0; i <= t.cnt; ++i) n     if (dp[n][i] == ans) lt[n][i] = 1; n   for (int i = n - 1; i >= 0; --i) { n     for (int j = 0; j <= t.cnt; ++j) { n       if (dp[i][j] == -1) continue; n       for (int k = 0; k <= 9; ++k) { n         int x = t.ch[j][k]; n         lt[i][j] |= n             lt[i + 1][x] && (dp[i + 1][x] == dp[i][j] + t.tp[x][n - i - 1]); n       } n     } n   } n   int x = 0; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j <= 9; ++j) { n       int y = t.ch[x][j]; n       if (lt[i + 1][y] && (dp[i + 1][y] == dp[i][x] + t.tp[y][n - i - 1])) { n         putchar(j +  0 ); n         x = y; n         break; n       } n     } n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int ans = 0, fh = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) fh = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) n     ans = (ans << 1) + (ans << 3) + ch -  0 , ch = getchar(); n   return ans * fh; n } n const int maxn = 2e5 + 100; n const long long inf = 1e18; n int n, m, head[maxn], nex[maxn], v[maxn], num = 1; n long long p[maxn << 2], la[maxn << 2]; n long long a[maxn], b[maxn], w[maxn]; n inline void updata(int o) { p[o] = min(p[o << 1], p[o << 1 | 1]); } n inline void pushdown(int o) { n   if (!la[o]) return; n   p[o << 1] += la[o], p[o << 1 | 1] += la[o]; n   la[o << 1] += la[o], la[o << 1 | 1] += la[o]; n   la[o] = 0; n } n void revise(int o, int l, int r, int ql, int qr, long long qz) { n   if (ql == l && qr == r) { n     p[o] += qz, la[o] += qz; n     return; n   } n   int mid = l + r >> 1; n   pushdown(o); n   if (ql <= mid) revise(o << 1, l, mid, ql, min(qr, mid), qz); n   if (qr > mid) revise(o << 1 | 1, mid + 1, r, max(ql, mid + 1), qr, qz); n   updata(o); n } n long long query(int o, int l, int r, int ql, int qr) { n   if (ql == l && qr == r) return p[o]; n   int mid = l + r >> 1; n   pushdown(o); n   long long ans = inf; n   if (ql <= mid) ans = query(o << 1, l, mid, ql, min(qr, mid)); n   if (qr > mid) n     ans = min(ans, query(o << 1 | 1, mid + 1, r, max(ql, mid + 1), qr)); n   return ans; n } n void build(int o, int l, int r) { n   la[o] = 0; n   if (l == r) { n     p[o] = b[l]; n     return; n   } n   int mid = l + r >> 1; n   build(o << 1, l, mid), build(o << 1 | 1, mid + 1, r); n   updata(o); n } n inline void add(int x, int y, long long z) { n   v[++num] = y; n   w[num] = z; n   nex[num] = head[x]; n   head[x] = num; n } n int main() { n   n = read(), m = read(); n   int T = read(); n   for (int i = 1; i < n; i++) a[i] = read(), b[i] = read(); n   for (int i = 1; i <= m; i++) { n     int x = read(), y = read(); n     add(x, y, read()); n   } n   build(1, 0, n - 1); n   for (int x = 1; x <= n; x++) { n     for (int i = head[x]; i; i = nex[i]) revise(1, 0, n - 1, 0, v[i] - 1, w[i]); n     b[x] = a[x] + p[1]; n   } n   build(1, 1, n); n   printf( %I64d n , p[1]); n   while (T--) { n     int x = read(), y = read(); n     revise(1, 1, n, x, x, y - a[x]), a[x] = y; n     printf( %I64d n , p[1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ReadInt() { n   int t; n   cin >> t; n   return t; n } n long long ReadLong() { n   long long t; n   cin >> t; n   return t; n } n int Vis[101][101]; n vector<pair<int, int> > Road; n int main() { n   int n, m; n   cin >> n >> m; n   if (m % 2 == 0 || n % 2 == 0) { n     if ((n % 2 == 0 && m % 2 != 0 && m != 1) || (n == 2 && m == 1)) { n       cout << 0 <<   n ; n       int x = 1, y = 1; n       Road.push_back({x, y}); n       Vis[x][y] = 1; n       while (1) { n         if (x + 1 <= n && !Vis[x + 1][y]) { n           x++; n           Road.push_back({x, y}); n           Vis[x][y] = 1; n         } else if (y + 1 <= m && !Vis[x][y + 1]) { n           y++; n           Road.push_back({x, y}); n           Vis[x][y] = 1; n         } else if (y - 1 > 0 && !Vis[x][y - 1]) { n           y--; n           Road.push_back({x, y}); n           Vis[x][y] = 1; n         } else if (x - 1 > 0 && !Vis[x - 1][y]) { n           x--; n           Road.push_back({x, y}); n           Vis[x][y] = 1; n         } else { n           Road.push_back({1, 1}); n           break; n         } n       } n       for (int i = 0; i < Road.size(); i++) n         cout << Road[i].first <<     << Road[i].second <<   n ; n     } else if ((m % 2 == 0 && n != 1) || (n == 1 && m == 2)) { n       cout << 0 <<   n ; n       int x = 1, y = 1; n       Road.push_back({x, y}); n       Vis[x][y] = 1; n       while (1) { n         if (x + 1 <= n && !Vis[x + 1][y]) { n           x++; n           Road.push_back({x, y}); n           Vis[x][y] = 1; n         } else if (y + 1 <= m && !Vis[x][y + 1]) { n           y++; n           Road.push_back({x, y}); n           Vis[x][y] = 1; n         } else if (x - 1 > 0 && !Vis[x - 1][y]) { n           x--; n           Road.push_back({x, y}); n           Vis[x][y] = 1; n         } else if (y - 1 > 0 && !Vis[x][y - 1]) { n           y--; n           Road.push_back({x, y}); n           Vis[x][y] = 1; n         } else { n           Road.push_back({1, 1}); n           break; n         } n       } n       for (int i = 0; i < Road.size(); i++) n         cout << Road[i].first <<     << Road[i].second <<   n ; n     } else { n       cout << 1 <<   n ; n       int x = 1, y = 1; n       Road.push_back({x, y}); n       Vis[x][y] = 1; n       while (1) { n         if (x + 1 <= n && !Vis[x + 1][y]) { n           x++; n           Road.push_back({x, y}); n           Vis[x][y] = 1; n         } else if (y + 1 <= m && !Vis[x][y + 1]) { n           y++; n           Road.push_back({x, y}); n           Vis[x][y] = 1; n         } else if (x - 1 > 0 && !Vis[x - 1][y]) { n           x--; n           Road.push_back({x, y}); n           Vis[x][y] = 1; n         } else if (y - 1 > 0 && !Vis[x][y - 1]) { n           y--; n           Road.push_back({x, y}); n           Vis[x][y] = 1; n         } else { n           cout << x <<     << y <<     << 1 <<     << 1 <<   n ; n           break; n         } n       } n       for (int i = 0; i < Road.size(); i++) n         cout << Road[i].first <<     << Road[i].second <<   n ; n       cout << 1 <<     << 1 <<   n ; n     } n   } else { n     cout << 1 <<   n ; n     int x = 1, y = 1; n     Road.push_back({x, y}); n     Vis[x][y] = 1; n     while (1) { n       if (x + 1 <= n && !Vis[x + 1][y]) { n         x++; n         Road.push_back({x, y}); n         Vis[x][y] = 1; n       } else if (y + 1 <= m && !Vis[x][y + 1]) { n         y++; n         Road.push_back({x, y}); n         Vis[x][y] = 1; n       } else if (x - 1 > 0 && !Vis[x - 1][y]) { n         x--; n         Road.push_back({x, y}); n         Vis[x][y] = 1; n       } else if (y - 1 > 0 && !Vis[x][y - 1]) { n         y--; n         Road.push_back({x, y}); n         Vis[x][y] = 1; n       } else { n         cout << x <<     << y <<     << 1 <<     << 1 <<   n ; n         break; n       } n     } n     for (int i = 0; i < Road.size(); i++) n       cout << Road[i].first <<     << Road[i].second <<   n ; n     cout << 1 <<     << 1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int num = pow(10, 9) + 7; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int n, k; n   cin >> n >> k; n   long long int c[n]; n   for (int i = 0; i < n; ++i) { n     cin >> c[i]; n   } n   long long int max_diff = -1; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; j++) { n       if (abs(c[i] - c[j]) > max_diff) max_diff = abs(c[i] - c[j]); n     } n   } n   if (max_diff > k) { n     cout <<  NO ; n   } else { n     cout <<  YES  << endl; n     for (int i = 0; i < n; ++i) { n       for (int j = 1; j <= c[i]; j++) { n         if (j % k == 0) n           cout << k <<    ; n         else n           cout << j % k <<    ; n       } n       cout << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)3e5 + 100; n vector<int> g[N]; n vector<pair<int, int> > G[N]; n map<int, long long> vToMinCost[N]; n multiset<long long> costs[N]; n long long addToAll[N]; n int ansAt[N]; n void upd(int id, int v, long long x) { n   x -= addToAll[id]; n   if (vToMinCost[id].count(v) == 0 || vToMinCost[id][v] > x) { n     if (vToMinCost[id].count(v) != 0) n       costs[id].erase(costs[id].find(vToMinCost[id][v])); n     vToMinCost[id][v] = x; n     costs[id].insert(x); n   } n } n void del(int id, int v) { n   if (vToMinCost[id].count(v)) { n     costs[id].erase(costs[id].find(vToMinCost[id][v])); n     vToMinCost[id].erase(v); n   } n } n void solve(int v, int par) { n   ansAt[v] = v; n   for (auto p : G[v]) upd(v, p.first, p.second); n   for (int to : g[v]) { n     if (to == par) continue; n     solve(to, v); n     int a = ansAt[v], b = ansAt[to]; n     if (vToMinCost[a].size() < vToMinCost[b].size()) swap(a, b); n     del(a, to); n     del(b, to); n     if (vToMinCost[a].empty() || vToMinCost[b].empty()) { n       printf( -1 n ); n       exit(0); n     } n     long long mn = *costs[b].begin() + addToAll[b]; n     long long mna = *costs[a].begin() + addToAll[a]; n     addToAll[a] += mn; n     addToAll[b] += mna; n     for (auto p : vToMinCost[b]) upd(a, p.first, p.second + addToAll[b]); n     ansAt[v] = a; n   } n } n int main(int, char **) { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     a--; n     b--; n     g[a].push_back(b); n     g[b].push_back(a); n   } n   for (int i = 0; i < n; i++) G[i].push_back(make_pair(i, 0)); n   for (int i = 0; i < m; i++) { n     int a, b, c; n     scanf( %d%d%d , &a, &b, &c); n     a--; n     b--; n     G[a].push_back(make_pair(b, c)); n   } n   solve(0, -1); n   int id = ansAt[0]; n   long long ans = *costs[id].begin() + addToAll[id]; n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, qu, f[100005], v[100005 + 100005], o[100005 + 100005], p, s[100005], n     r, fr[100005], to[100005], si[100005], curm, grp[100005], gr, rt[100005]; n bool u[100005], vis[100005]; n vector<int> b[100005], c[100005], q[100005], ans[100005]; n void bd(int a, int b) { n   v[++p] = b; n   o[p] = f[a]; n   f[a] = p; n } n void merge(vector<int> &a, vector<int> b) { n   vector<int> c; n   int l = 0, r = 0; n   while (l < a.size() || r < b.size()) { n     if (l == a.size()) n       c.push_back(b[r++]); n     else if (r == b.size()) n       c.push_back(a[l++]); n     else if (a[l] < b[r]) n       c.push_back(a[l++]); n     else n       c.push_back(b[r++]); n   } n   a = c; n   if (a.size() > 11) a.resize(11); n } n void size(int x, int y) { n   s[x] = vis[x] = 1; n   for (int i = (f[x]); i; i = o[i]) n     if (v[i] != y && !u[v[i]]) { n       size(v[i], x); n       s[x] += s[v[i]]; n     } n } n void pick(int x, int y, int z) { n   int m = s[z] - s[x]; n   for (int i = (f[x]); i; i = o[i]) n     if (v[i] != y && !u[v[i]]) { n       pick(v[i], x, z); n       m = max(m, s[v[i]]); n     } n   if (m < curm) { n     curm = m; n     r = x; n   } n } n void group(int x, int y, int z) { n   grp[x] = z; n   for (int i = (f[x]); i; i = o[i]) n     if (v[i] != y && !u[v[i]]) group(v[i], x, z); n } n void dfs(int x, int y) { n   c[x] = c[y]; n   merge(c[x], b[x]); n   for (int i = (0); i < (q[x].size()); i++) { n     int d = q[x][i]; n     if (vis[fr[d]] && vis[to[d]] && grp[fr[d]] != grp[to[d]]) { n       merge(ans[d], c[x]); n       rt[d] = r; n     } n   } n   for (int i = (f[x]); i; i = o[i]) n     if (v[i] != y && !u[v[i]]) dfs(v[i], x); n } n void reset(int x, int y) { n   vis[x] = 0; n   for (int i = (f[x]); i; i = o[i]) n     if (v[i] != y && !u[v[i]]) reset(v[i], x); n } n void root(int x) { n   size(x, 0); n   curm = 100005; n   pick(x, 0, x); n   grp[r] = 0; n   gr = 1; n   u[r] = 1; n   for (int i = (f[r]); i; i = o[i]) n     if (!u[v[i]]) group(v[i], r, gr++); n   for (int i = (0); i < (q[r].size()); i++) { n     int d = q[r][i]; n     if (vis[fr[d]] && vis[to[d]]) rt[d] = r; n   } n   for (int i = (f[r]); i; i = o[i]) n     if (!u[v[i]]) dfs(v[i], 0); n   reset(r, 0); n   for (int i = (f[r]); i; i = o[i]) n     if (!u[v[i]]) root(v[i]); n } n int main() { n   scanf( %d %d %d , &n, &m, &qu); n   for (int i = (1); i < (n); i++) { n     int a, b; n     scanf( %d %d , &a, &b); n     bd(a, b); n     bd(b, a); n   } n   for (int i = (1); i <= (m); i++) { n     int t; n     scanf( %d , &t); n     b[t].push_back(i); n   } n   for (int i = (1); i <= (n); i++) n     if (b[i].size() > 11) b[i].resize(11); n   for (int i = (1); i <= (qu); i++) { n     scanf( %d %d %d , &fr[i], &to[i], &si[i]); n     q[fr[i]].push_back(i); n     q[to[i]].push_back(i); n   } n   root(1); n   for (int i = (1); i <= (qu); i++) { n     merge(ans[i], b[rt[i]]); n     if (ans[i].size() > si[i]) ans[i].resize(si[i]); n     printf( %d , ans[i].size()); n     for (int j = (0); j < (ans[i].size()); j++) printf(  %d , ans[i][j]); n     puts(  ); n   } n   scanf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k, l, r, s, t; n long long modpow(long long x, long long y) { n   if (y == 0) return 1 % 1000000009; n   long long u = modpow(x, y / 2); n   u = u * u % 1000000009; n   if (y % 2) u = u * x % 1000000009; n   return u; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m >> k; n   l = (n / k) * (k - 1) + n % k; n   r = m - l; n   if (r <= 0) { n     cout << m << endl; n     return 0; n   } n   s = m - r * (k); n   if (s <= 0) s = 0; n   t = modpow(2, r + 1); n   t = (t + 1000000009 - 2) % 1000000009; n   t = t * k % 1000000009; n   s = s + t; n   cout << s % 1000000009 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool is_prime(long long n) { n   if (n == 2) return true; n   if (n < 2) return false; n   for (long long i = 3; i * i <= n; ++i) n     if (!(n % i)) return false; n   return true; n } n vector<long long> divison(long long n) { n   vector<long long> v; n   for (long long i = 1; i * i <= n; ++i) n     if (!(n % i)) v.push_back(i), v.push_back(n / i); n   return v; n } n void printnumberinbinary(unsigned long long t) { n   if (!t) return; n   printnumberinbinary(t >> 1); n   cout << (t & 1); n } n int numberofdiv(int t) { n   int ans = 0, x; n   for (x = 1; x * x < t; x++) { n     if (!(t % x)) ans += 2; n   } n   if (x * x == t) ans++; n   return ans; n } n vector<set<int>> adj(1e5 + 1); n vector<bool> vis(1e5 + 1); n void dfs(int t) { n   cout << t <<    ; n   vis[t] = true; n   for (auto f : adj[t]) n     if (!vis[f]) dfs(f); n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     int a, y = 0; n     cin >> a; n     vector<int> v(a + 1, 0); n     for (int x = 0; x < a; x++) { n       int s; n       cin >> s; n       int flag = 0; n       while (s--) { n         int y; n         cin >> y; n         if (!v[y] && !flag) { n           v[y]++; n           flag++; n         } n       } n       if (!flag) y = x + 1; n     } n     if (!y) n       cout <<  OPTIMAL ; n     else { n       cout <<  IMPROVE n ; n       for (int x = 1; x < v.size(); x++) { n         if (!v[x]) { n           cout << y <<     << x; n           break; n         } n       } n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100500; n bool w, w1; n int main() { n   srand(time(NULL)); n   int n, m, mn, mx; n   cin >> n >> m >> mn >> mx; n   for (int i = 1; i <= m; i++) { n     int x; n     cin >> x; n     if (mn > x || mx < x) cout <<  Incorrect , exit(0); n     if (x == mn) w = true; n     if (x == mx) w1 = true; n   } n   if (w && w1) cout <<  Correct , exit(0); n   if (w || w1) n     if (n - m > 0) n       cout <<  Correct , exit(0); n     else n       cout <<  Incorrect , exit(0); n   if (n - m > 1) n     cout <<  Correct , exit(0); n   else n     cout <<  Incorrect , exit(0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e3 + 5; n int n, a[N][N]; n char s[N]; n bool vis[N]; n void dfs(int x, int fa) { n   vis[x] = 1; n   for (int i = 1; i <= n; i++) { n     if (!a[x][i] || i == fa || i == x) continue; n     if (a[i][fa]) printf( %d %d %d n , x, i, fa), exit(0); n     if (!vis[i]) dfs(i, x); n   } n } n signed main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %s , s + 1); n     for (int j = 1; j <= n; j++) a[i][j] = s[j] -  0 ; n   } n   for (int i = 1; i <= n; i++) n     if (!vis[i]) dfs(i, 0); n   puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int arr[n]; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   sort(arr, arr + n); n   if (arr[n - 1] < arr[n - 2] + arr[n - 3]) { n     cout <<  YES  << endl; n     for (int i = 0; i < n - 2; i++) { n       cout << arr[i] <<    ; n     } n     cout << arr[n - 1] <<     << arr[n - 2]; n   } else { n     cout <<  NO ; n   } n } n


#include <bits/stdc++.h> n int main() { n   int n, m; n   int r = 0, a = 0, v = 0; n   double es = 0.0; n   scanf( %d%d , &n, &m); n   r = 2 * n; n   v = 5 * n; n   a = 8 * n; n   es = (double)r / m; n   r = r / m; n   if (es - r != 0.00) { n     r++; n   } n   es = (double)v / m; n   v = v / m; n   if (es - v != 0.00) { n     v++; n   } n   es = (double)a / m; n   a = a / m; n   if (es - a != 0.00) { n     a++; n   } n   printf( %d , a + v + r); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int c[n]; n   for (int i = 0; i < n; i++) c[i] = a[i]; n   int b[n]; n   b[0] = a[0]; n   for (int i = 0; i < n - 1; i++) { n     if (a[i] + a[i + 1] >= k) n       b[i + 1] = a[i + 1]; n     else { n       b[i + 1] = k - a[i]; n       a[i + 1] = b[i + 1]; n     } n   } n   int sum = 0; n   for (int i = 0; i < n; i++) { n     sum = sum + b[i] - c[i]; n   } n   cout << sum << endl; n   for (int i = 0; i < n; i++) cout << b[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N6 = 1e6 + 6, N3 = 1e3 + 6, oo = 1e9 + 9; n int n, a[N6], ans; n bool ok; n int main() { n   ios_base ::sync_with_stdio(0); n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n   } n   for (int i = 1; i <= n; ++i) { n     if (!a[i]) { n       int cnt = 0; n       while (!a[i] && i <= n) { n         ++i; n         ++cnt; n       } n       if ((cnt == 1 && !ok) || (cnt > 1) || (i > n && cnt == 1)) ans += cnt; n     } n     if (a[i]) ok = 1; n   } n   cout << abs(n - ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     int k; n     cin >> n >> k; n     int octr = 0; n     int a[n]; n     int sum = 0; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       if (a[i] % 2 != 0) octr++; n       sum = sum + a[i]; n     } n     if (octr >= k) { n       if ((sum % 2 != 0 && k % 2 != 0) || (sum % 2 == 0 && k % 2 == 0)) { n         cout <<  YES  << endl; n         int f = 0; n         while (k > 1) { n           if (a[f] % 2 != 0) { n             cout << f + 1 <<    ; n             k--; n           } n           f++; n         } n         cout << n <<    ; n         cout << endl; n       } else n         cout <<  NO  << endl; n     } else n       cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long int llINF = (long long)(1e18) + 100; n const long long int MAXN = 1e6 + 10; n int lp[MAXN]; n vector<int> pr; n void crivo() { n   for (long long int i = 2; i < MAXN; i++) { n     if (lp[i] == 0) { n       lp[i] = i; n       pr.push_back(i); n     } n     for (long long int j = 0; n          j < (int)pr.size() && pr[j] <= lp[i] && i * pr[j] < MAXN; ++j) n       lp[i * pr[j]] = pr[j]; n   } n } n int n, dist[MAXN]; n vector<int> adj[MAXN], dcp[MAXN]; n vector<pair<int, int> > edge; n int other(int idx, int u) { n   return edge[idx].first == u ? edge[idx].second : edge[idx].first; n } n int bfs(int p) { n   memset(dist, 0, sizeof(dist)); n   queue<pair<int, int> > fila; n   for (int x : adj[p]) { n     fila.push({x, other(x, p)}); n     dist[other(x, p)] = 1; n   } n   int u, v, ans = INF; n   pair<int, int> aux; n   while (!fila.empty()) { n     aux = fila.front(); n     fila.pop(); n     u = aux.second; n     for (int w : adj[u]) { n       if (w != aux.first) { n         v = other(w, u); n         if (dist[v] == 0 and v != p) { n           fila.push({w, v}); n           dist[v] = dist[u] + 1; n         } else n           ans = min(ans, dist[u] + dist[v] + 1); n       } n     } n   } n   return ans; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   crivo(); n   lp[1] = 1; n   cin >> n; n   for (int i = 2; i < MAXN; i++) { n     dcp[i] = dcp[i / lp[i]]; n     auto it = find(dcp[i].begin(), dcp[i].end(), lp[i]); n     if (it != dcp[i].end()) n       dcp[i].erase(it); n     else n       dcp[i].push_back(lp[i]); n   } n   int a; n   for (int i = 0; i < n; i++) { n     cin >> a; n     if (dcp[a].empty()) { n       cout << 1 <<   n ; n       return 0; n     } n     if (dcp[a].size() == 1) dcp[a].push_back(1); n     edge.push_back({dcp[a][0], dcp[a][1]}); n     adj[dcp[a][0]].push_back(i); n     adj[dcp[a][1]].push_back(i); n   } n   int ans = INF; n   pr.insert(pr.begin(), 1); n   for (int p : pr) { n     if (p * p > MAXN) break; n     ans = min(ans, bfs(p)); n   } n   cout << (ans == INF ? -1 : ans) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n struct obj { n   int id; n   int f, t; n   obj(int a, int b, int c) { n     id = a; n     f = b; n     t = c; n   } n }; n bool cmpup(obj a, obj b) { n   if (a.f == b.f) { n     return a.id > b.id; n   } n   return a.f < b.f; n } n bool cmpdown(obj a, obj b) { n   if (a.f == b.f) { n     return a.id < b.id; n   } n   return a.f > b.f; n } n int n; n vector<obj> up; n vector<obj> down; n int a[105], d[105]; n int now[105]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d%d , &a[i], &d[i]); n     now[i] = a[i]; n     if (d[i] > 0) n       up.push_back(obj(i, a[i], a[i] + d[i])); n     else if (d[i] < 0) n       down.push_back(obj(i, a[i], a[i] + d[i])); n   } n   sort(up.begin(), up.end(), cmpup); n   sort(down.begin(), down.end(), cmpdown); n   int ans = 0; n   for (int i = 0; i < up.size(); i++) { n     for (int j = 0; j < n; j++) { n       if (j == up[i].id) continue; n       if (now[j] < up[i].f || now[j] > up[i].t) continue; n       if (now[j] == up[i].f) { n         if (j < up[i].id) ans++; n       } else if (now[j] == up[i].t) { n         if (j > up[i].id) ans++; n       } else { n         ans++; n       } n     } n     now[up[i].id] = up[i].t; n   } n   for (int i = 0; i < down.size(); i++) { n     for (int j = 0; j < n; j++) { n       if (j == down[i].id) continue; n       if (now[j] < down[i].t || now[j] > down[i].f) continue; n       if (now[j] == down[i].f) { n         if (j > down[i].id) ans++; n       } else if (now[j] == down[i].t) { n         if (j < down[i].id) ans++; n       } else { n         ans++; n       } n     } n     now[down[i].id] = down[i].t; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double pi = 3.1415926535897932384626433832795l; n template <typename T> n inline auto sqr(T x) -> decltype(x * x) { n   return x * x; n } n template <typename T1, typename T2> n inline bool umx(T1& a, T2 b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <typename T1, typename T2> n inline bool umn(T1& a, T2 b) { n   if (b < a) { n     a = b; n     return 1; n   } n   return 0; n } n struct Input { n   int n, k; n   bool read() { n     if (!(cin >> n >> k)) { n       return false; n     } n     return true; n   } n   void init(const Input& input) { *this = input; } n }; n struct Data : Input { n   long long ans; n   void write() { cout << ans <<   n ; } n   virtual void solve() {} n   virtual void clear() { *this = Data(); } n }; n struct Solution : Data { n   void solve() { n     ans = 0; n     while (n > 1 && k > 0) { n       ans += 2 * (n - 1) - 1; n       n -= 2; n       --k; n     } n   } n   void clear() { *this = Solution(); } n }; n Solution sol; n int main() { n   cout.setf(ios::showpoint | ios::fixed); n   cout.precision(20); n   sol.read(); n   sol.solve(); n   sol.write(); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long int find(vector<long long int> &p, long long int i) { n   if (p[i] == -1) return i; n   return find(p, p[i]); n } n void solve() { n   string st; n   cin >> st; n   long long int c = 0; n   for (long long int i = 0; i < st.size(); i++) { n     if (st[i] ==  >  or st[i] ==  < ) { n       continue; n     } n     if (st[i] ==  / ) { n       c--; n       for (long long int j = 0; j < 2 * c; j++) { n         cout <<    ; n       } n       cout <<  </  << st[i + 1] <<  >  << endl; n     } else if (st[i - 1] !=  / ) { n       for (long long int j = 0; j < 2 * c; j++) { n         cout <<    ; n       } n       cout <<  <  << st[i] <<  >  << endl; n       c++; n     } n   } n } n int main() { n   { solve(); } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int xx[] = {0, 0, -1, 1}, yy[] = {-1, 1, 0, 0}; n using ll = long long; n using ppi = pair<int, int>; n using ppl = pair<ll, ll>; n ll d[3001][3001]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int T; n   cin >> T; n   while (T--) { n     int N; n     cin >> N; n     vector<int> v(N); n     for (long long i = (0); i < (N); ++i) { n       cin >> v[i]; n     } n     memset(d, 0, sizeof(d)); n     for (int i = N - 1; 0 <= i; --i) { n       d[i][v[i]] = 1; n       if (i + 1 < N) { n         for (long long x = (1); x <= (N); ++x) { n           d[i][x] += d[i + 1][x]; n         } n       } n     } n     ll ans = 0; n     for (long long j = (1); j < (N - 2); ++j) { n       for (long long k = (j + 1); k < (N - 1); ++k) { n         ans += (d[0][v[k]] - d[j][v[k]]) * (d[k + 1][v[j]]); n       } n     } n     cout << ans << ((char)10); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n long long a[300100], sum[300100]; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; i++) scanf( %lld , &a[i]); n   for (int i = n - 1; i >= 0; i--) sum[i] = sum[i + 1] + a[i]; n   long long ans = sum[0]; n   sort(sum + 1, sum + n); n   for (int i = 0; i < k - 1; i++) ans += sum[n - 1 - i]; n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char alphz[27] = { a ,  b ,  c ,  d ,  e ,  f ,  g ,  h ,  i , n                    j ,  k ,  l ,  m ,  n ,  o ,  p ,  q ,  r , n                    s ,  t ,  u ,  v ,  w ,  x ,  y ,  z }; n float Euclidean(long long x1, long long x2, long long y1, long long y2) { n   return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2)); n } n long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); } n long long LCM(long long a, long long b) { return (a * b) / gcd(a, b); } n void PrimeFactor(long long n) { n   while (n % 2 == 0) { n     n /= 2; n   } n   for (int i = 3; i <= sqrt(n); i += 2) { n     if (n % i == 0) { n       n /= i; n     } n   } n   if (n > 2) { n   }; n } n bool is_square(long long x) { n   long long l = 0, r = x; n   while (l <= r) { n     long long mid = l + (r - l) / 2; n     if (mid * mid == x) return true; n     if (mid * mid > x) n       r = mid - 1; n     else n       l = mid + 1; n   } n   return false; n } n long long power(long long x, long long y) { n   long long temp; n   if (y == 0) return 1; n   temp = power(x, y / 2); n   if (y % 2 == 0) n     return temp * temp; n   else n     return x * temp * temp; n } n bool is_Prime(int x) { n   if (x == 2) n     return 1; n   else if (x % 2 == 0 || x < 2) n     return 0; n   for (int i = 3; i * i <= x; i += 2) n     if (x % i == 0) return 0; n   return 1; n } n const int N = 1e+5; n int aseg[4], seg[4]; n void SEGBuild(int pos, int low, int hig) { n   if (low == hig) n     seg[pos] = aseg[low]; n   else { n     int mid = low + (hig - low) / 2; n     SEGBuild(2 * pos, low, mid); n     SEGBuild(2 * pos + 1, mid + 1, hig); n     if (seg[2 * pos] < seg[2 * pos + 1]) n       seg[pos] = seg[2 * pos]; n     else n       seg[pos] = seg[2 * pos + 1]; n   } n } n void SEGUpdate(int pos, int idx, int low, int hig, int val) { n   if (low == hig) { n     aseg[idx] = val; n     seg[pos] = val; n   } else { n     int mid = low + (hig - low) / 2; n     if (idx >= low && idx <= mid) n       SEGUpdate(2 * pos, idx, low, mid, val); n     else n       SEGUpdate(2 * pos + 1, idx, mid + 1, hig, val); n     seg[pos] = min(seg[2 * pos + 1], seg[2 * pos]); n   } n } n int ANSQuery(int pos, int low, int hig, int l, int r) { n   if (l > hig || low > r) return 1e+5; n   if (l <= low && r >= hig) n     return seg[pos]; n   else { n     int p1, p2; n     int mid = low + (hig - low) / 2; n     p1 = ANSQuery(2 * pos, low, mid, l, r); n     p2 = ANSQuery(2 * pos + 1, mid + 1, hig, l, r); n     return min(p1, p2); n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int k; n   string s; n   cin >> k >> s; n   vector<int> a(k, 0); n   int len = (int)s.size(); n   if (len == 1) { n     if (k > 1) return cout <<  IMPOSSIBLE n , 0; n     return cout <<  a  <<   n , 0; n   } n   int i = 0, j = len - 1; n   while (i < j) { n     if (s[i] !=  ?  && s[j] !=  ?  && s[i] != s[j]) n       return cout <<  IMPOSSIBLE n , 0; n     if (s[i] !=  ? ) a[s[i] -  a ]++; n     if (s[j] !=  ? ) a[s[j] -  a ]++; n     i++, j--; n   } n   int mid = len / 2; n   if (len % 2 == 0) n     i = mid - 1, j = mid; n   else n     i = mid, j = mid; n   while (i >= 0) { n     if (s[i] !=  ?  && s[j] !=  ? ) n       a[s[i] -  a ] += 2; n     else if (s[i] ==  ?  && s[j] !=  ? ) n       s[i] = s[j], a[s[j] -  a ] += 2; n     else if (s[i] !=  ?  && s[j] ==  ? ) n       s[j] = s[i], a[s[i] -  a ] += 2; n     else { n       int idx = 0; n       for (int i = k - 1; i >= 0; i--) { n         if (a[i] == 0) { n           idx = i; n           break; n         } n       } n       s[i] = s[j] = idx +  a ; n       a[idx] += 2; n     } n     i--, j++; n   } n   for (int i = 0; i < k; i++) n     if (!a[i]) return cout <<  IMPOSSIBLE n , 0; n   cout << s <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int rev(int ans) { n   if (ans == 1) n     return 0; n   else n     return 1; n } n int main() { n   int arr[3][3], ans[3][3]; n   for (int i = 0; i < 3; i++) { n     for (int j = 0; j < 3; j++) { n       cin >> arr[i][j]; n       ans[i][j] = 1; n     } n   } n   for (int i = 0; i < 3; i++) { n     for (int j = 0; j < 3; j++) { n       if (arr[i][j] % 2 != 0) { n         if (i - 1 >= 0) { n           ans[i - 1][j] = rev(ans[i - 1][j]); n         } n         if (i + 1 < 3) { n           ans[i + 1][j] = rev(ans[i + 1][j]); n         } n         if (j + 1 < 3) { n           ans[i][j + 1] = rev(ans[i][j + 1]); n         } n         if (j - 1 >= 0) { n           ans[i][j - 1] = rev(ans[i][j - 1]); n         } n         ans[i][j] = rev(ans[i][j]); n       } n     } n   } n   for (int i = 0; i < 3; i++) { n     for (int j = 0; j < 3; j++) { n       cout << ans[i][j]; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1, c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = (x << 1) + (x << 3) + (c ^ 48); n     c = getchar(); n   } n   return f == 1 ? x : -x; n } n const int N = 1e5 + 4; n int n, a[N], c[40]; n inline void solve() { n   memset(c, 0, sizeof(c)); n   n = read(); n   for (int i = 1; i <= n; i++) { n     a[i] = read(); n     for (int j = 0; j < 30; j++) n       if ((a[i] >> j) & 1) c[j]++; n   } n   for (int i = 29; i >= 0; i--) n     if (c[i] & 1) { n       if (c[i] % 4 == 1) { n         puts( WIN ); n       } else { n         puts((n & 1) ?  LOSE  :  WIN ); n       } n       return; n     } n   puts( DRAW ); n } n int main() { n   for (int T = read(); T--;) solve(); n   return (0 - 0); n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000001; n const double EPS = 10e-9; n int const S = 1 << 19; n int n, a[S], leftlink[S], rightlink[S], decrease; n pair<int, int> tree[2 * S]; n pair<int, int> Min() { return tree[1]; } n void Init() { n   tree[S] = make_pair(INF, 0); n   for (int i = 1; i <= (n); ++i) tree[i + S] = make_pair(a[i], i); n   for (int i = n + 1; i <= (S - 1); ++i) tree[S + i] = make_pair(INF, i); n   for (int i = S - 1; i >= (1); --i) n     tree[i] = min(tree[2 * i], tree[2 * i + 1]); n } n void Delete(int p) { n   for (tree[p + S] = make_pair(INF, p), p += S, p >>= 1; p; p >>= 1) n     tree[p] = min(tree[p * 2], tree[p * 2 + 1]); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i <= (n); ++i) { n     cin >> a[i]; n     leftlink[i] = i - 1; n     rightlink[i] = i + 1; n   } n   rightlink[n] = 0; n   Init(); n   pair<int, int> greedy; n   long long ans = 0; n   while (n) { n     if (n <= 2) break; n     greedy = Min(); n     Delete(greedy.second); n     if (!leftlink[greedy.second]) { n       ans += ((long long)(n - 2)) * (greedy.first - decrease); n       leftlink[rightlink[greedy.second]] = 0; n       decrease = greedy.first; n       --n; n       continue; n     } n     if (!rightlink[greedy.second]) { n       ans += ((long long)(n - 2)) * (greedy.first - decrease); n       decrease = greedy.first; n       rightlink[leftlink[greedy.second]] = 0; n       --n; n       continue; n     } n     leftlink[rightlink[greedy.second]] = leftlink[greedy.second]; n     rightlink[leftlink[greedy.second]] = rightlink[greedy.second]; n     ans += n         min(a[leftlink[greedy.second]], a[rightlink[greedy.second]]) - decrease; n     --n; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n = 0, m = 0, temp = 0; n   cin >> n >> m; n   int arr[n + 1]; n   fill(arr, arr + n + 1, -1); n   for (int i = 1; i <= m; i++) { n     cin >> temp; n     int k = temp; n     while (k <= n && arr[k] == -1) { n       arr[k] = temp; n       k++; n     } n   } n   for (int j = 1; j <= n; j++) cout << arr[j] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, cost; n int di[4] = {0, -1, 0, 1}; n int dj[4] = {-1, 0, 1, 0}; n char grid[51][51]; n int vis[51][51]; n pair<int, int> pi; n pair<int, int> pf; n vector<pair<int, int>> ili; n vector<pair<int, int>> ilf; n void dfs(int r, int c, vector<pair<int, int>>* island) { n   vis[r][c] = 1; n   (*island).push_back({r, c}); n   for (int v = 0; v < 4; v++) { n     int i = r + di[v]; n     int j = c + dj[v]; n     if (i > 0 && i <= n && j > 0 && j <= n) { n       if (grid[i][j] ==  0  && !vis[i][j]) { n         dfs(i, j, island); n       } n     } n   } n } n int main() { n   cin >> n; n   cin >> pi.first >> pi.second; n   cin >> pf.first >> pf.second; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       cin >> grid[i][j]; n     } n   } n   dfs(pi.first, pi.second, &ili); n   if (vis[pf.first][pf.second]) { n     cout <<  0  << endl; n   } else { n     dfs(pf.first, pf.second, &ilf); n     int less = (int)INFINITY; n     for (int i = 0; i < (int)ili.size(); i++) { n       for (int j = 0; j < (int)ilf.size(); j++) { n         cost = pow((ili[i].first - ilf[j].first), 2) + n                pow((ili[i].second - ilf[j].second), 2); n         if (cost < less && cost) { n           less = cost; n         } n       } n     } n     cout << less << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000007; n const long long LLINF = 1000000000000000007LL; n const double EPS = 1e-9; n const int sz = 200100; n int ar[600][600]; n int p[600][600]; n int suf_sum_2d(int i1, int j1, int i2, int j2) { n   return p[i2 + 1][j2 + 1] - p[i2 + 1][j1] - p[i1][j2 + 1] + p[i1][j1]; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       cin >> ar[i][j]; n       p[i + 1][j + 1] = p[i][j + 1] + p[i + 1][j] - p[i][j] + ar[i][j]; n     } n   } n   int mx = -INF; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       int r = min(min(i + 1, j + 1), min(n - i, m - j)); n       int res = ar[i][j]; n       for (int k = 1; k < r; ++k) { n         res = n             suf_sum_2d(i - k, j - k, i + k, j + k) - res - ar[i - k + 1][j - k]; n         mx = max(mx, res); n       } n     } n   } n   cout << mx; n } n


#include <bits/stdc++.h> n using namespace std; n unordered_map<int, int> mp; n bool f(int mid) { n   if (mp.size() < mid) return false; n   int a = 0, b = 0; n   bool fl = 0; n   for (auto it = mp.begin(); it != mp.end(); it++) { n     if (it->second > mid) { n       a = mid; n       if (fl == 0) { n         b++; n       } else { n         b += 2; n         fl = 0; n       } n     } else if (it->second == mid) { n       if (a == 0) { n         a = mid; n         fl = 1; n       } else n         b++; n     } else if (it->second < mid) n       b++; n   } n   return a >= mid && b >= mid; n } n int main() { n   int test; n   cin >> test; n   for (int t = 1; t <= test; t++) { n     int n, ans = 0, imax = 0; n     cin >> n; n     mp.clear(); n     for (int i = 0; i < n; i++) { n       int temp; n       cin >> temp; n       mp[temp]++; n       imax = max(imax, mp[temp]); n     } n     int low = 1, high = imax; n     while (low <= high) { n       int mid = low + (high - low) / 2; n       if (f(mid)) { n         ans = mid; n         low = mid + 1; n       } else n         high = mid - 1; n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int INT_MAX_VAL = (int)0x3F3F3F3F; n int INT_MIN_VAL = (int)-0x3F3F3F3F; n long long LONG_MAX_VAL = (long long)0x3F3F3F3F3F3F3F3F; n long long LONG_MIN_VAL = (long long)-0x3F3F3F3F3F3F3F3F; n vector<long long> G[5005]; n long long n; n int W; n int cnt[5005]; n int dp[2][5005][5005]; n int dfs2(int v, int p) { n   if (G[v].size() == 1) { n     cnt[v] = 1; n     return 1; n   } n   int res = 0; n   for (auto &w : G[v]) n     if (w != p) res += dfs2(w, v); n   cnt[v] = res; n   return res; n } n void dfs(int v, int p) { n   dp[1][v][0] = 0; n   if (G[v].size() == 1) { n     dp[0][v][1] = 0; n     return; n   } n   dp[0][v][0] = 0; n   dp[1][v][0] = 0; n   for (auto &w : G[v]) { n     if (w == p) continue; n     dfs(w, v); n     for (int i = W; i >= 0; --i) { n       int d0 = INT_MAX_VAL; n       int d1 = INT_MAX_VAL; n       for (int j = min(i, cnt[w]); j >= 0; --j) { n         d0 = min(d0, dp[0][w][j] + dp[0][v][i - j]); n         d0 = min(d0, dp[1][w][j] + dp[0][v][i - j] + 1); n         d1 = min(d1, dp[1][w][j] + dp[1][v][i - j]); n         d1 = min(d1, dp[0][w][j] + dp[1][v][i - j] + 1); n       } n       dp[0][v][i] = d0; n       dp[1][v][i] = d1; n     } n   } n } n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   memset(dp, 0x3F, 2 * 5005 * 5005 * sizeof(int)); n   cin >> n; n   for (long long(i) = 0LL; (i) < (n - 1); (i)++) { n     long long a, b; n     cin >> a >> b; n     --a, --b; n     G[a].push_back(b); n     G[b].push_back(a); n   } n   if (n == 2) { n     cout << 1 <<   n ; n     return 0; n   } n   int i = 0; n   while (G[i].size() == 1) ++i; n   W = dfs2(i, -1) / 2; n   dfs(i, -1); n   cout << dp[0][i][W] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n struct ban { n   long long s, a, b; n }; n bool operator<(const ban& a, const ban& b) { return (a.a - a.b) > (b.a - b.b); } n int n; n long long s; n ban a[N]; n long long sum; n long long ans; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> s; n   for (int i = 0; i < n; ++i) { n     cin >> a[i].s >> a[i].a >> a[i].b; n     sum += a[i].s; n   } n   sort(a, a + n); n   long long t = 0; n   for (int i = 0; i < n; ++i) { n     if (a[i].a - a[i].b > 0) t += a[i].s; n   } n   long long yans = 0; n   for (int i = 0; i < n; ++i) { n     if (a[i].a - a[i].b > 0) yans += (a[i].a * a[i].s); n   } n   long long tt = (((sum / s) + !!(sum % s)) - ((t / s) + !!(t % s))) * s; n   for (int i = n - 1; i >= 0; --i) { n     if (a[i].a - a[i].b <= 0) { n       if (tt > a[i].s) { n         tt -= a[i].s; n         yans += (a[i].b * a[i].s); n       } else { n         yans += (a[i].b * tt); n         yans += (a[i].a * (a[i].s - tt)); n         tt = 0; n       } n     } n   } n   ans = yans; n   t = 0; n   for (int i = n - 1; i >= 0; --i) { n     if (a[i].b - a[i].a > 0) t += a[i].s; n   } n   yans = 0; n   for (int i = n - 1; i >= 0; --i) { n     if (a[i].b - a[i].a > 0) yans += (a[i].b * a[i].s); n   } n   tt = (((sum / s) + !!(sum % s)) - ((t / s) + !!(t % s))) * s; n   for (int i = 0; i < n; ++i) { n     if (a[i].b - a[i].a <= 0) { n       if (tt > a[i].s) { n         tt -= a[i].s; n         yans += (a[i].a * a[i].s); n       } else { n         yans += (a[i].a * tt); n         yans += (a[i].b * (a[i].s - tt)); n         tt = 0; n       } n     } n   } n   ans = max(ans, yans); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace ::std; n int NEGINF = -1e9; n int main() { n   ifstream fin( input.txt ); n   int n, l, r; n   cin >> n >> l >> r; n   vector<int> a(n), b(n), p(n), c(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   vector<int> ppos(n); n   for (int i = 0; i < n; i++) { n     cin >> p[i]; n     ppos[p[i] - 1] = i; n   } n   int loc, csmall = NEGINF; n   for (int i = 0; i < n; i++) { n     loc = ppos[i]; n     if (csmall + 1 + a[loc] < l) { n       b[loc] = l; n       csmall = b[loc] - a[loc]; n     } else if (csmall + 1 + a[loc] > r) { n       cout <<  -1 n ; n       return 0; n     } else { n       csmall++; n       b[loc] = a[loc] + csmall; n     } n   } n   for (int i = 0; i < n; i++) { n     cout << b[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T abs(T x) { n   return x > 0 ? x : -x; n } n char a[2005][2005]; n bool used[2005][2005]; n queue<pair<int, int> > q; n int nu() { n   puts( Not unique ); n   return 0; n } n bool impasse(int ic, int jc) { n   int cnt = 0; n   cnt += (a[ic][jc - 1] ==  . ); n   cnt += (a[ic][jc + 1] ==  . ); n   cnt += (a[ic - 1][jc] ==  . ); n   cnt += (a[ic + 1][jc] ==  . ); n   return (cnt < 2); n } n void add(int ic, int jc) { n   if (impasse(ic - 1, jc) && !used[ic - 1][jc]) { n     q.push(make_pair(ic - 1, jc)); n     used[ic - 1][jc] = true; n   } n   if (impasse(ic + 1, jc) && !used[ic + 1][jc]) { n     q.push(make_pair(ic + 1, jc)); n     used[ic + 1][jc] = true; n   } n   if (impasse(ic, jc - 1) && !used[ic][jc - 1]) { n     q.push(make_pair(ic, jc - 1)); n     used[ic][jc - 1] = true; n   } n   if (impasse(ic, jc + 1) && !used[ic][jc + 1]) { n     q.push(make_pair(ic, jc + 1)); n     used[ic][jc + 1] = true; n   } n } n int main() { n   int n, m; n   scanf( %d %d n , &n, &m); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       scanf( %c , &a[i][j]); n     } n     scanf( %c , &a[0][0]); n   } n   for (int i = 0; i <= n + 1; i++) { n     a[i][0] =  * ; n     a[i][m + 1] =  * ; n   } n   for (int i = 0; i <= m + 1; i++) { n     a[0][i] =  * ; n     a[n + 1][i] =  * ; n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       if (a[i][j] ==  * ) continue; n       int cnt = 0; n       cnt += (a[i][j - 1] ==  . ); n       cnt += (a[i][j + 1] ==  . ); n       cnt += (a[i - 1][j] ==  . ); n       cnt += (a[i + 1][j] ==  . ); n       if (cnt == 0) return nu(); n       if (cnt == 1) { n         used[i][j] = true; n         q.push(make_pair(i, j)); n       } n     } n   while (!q.empty()) { n     int ic = q.front().first; n     int jc = q.front().second; n     q.pop(); n     if (a[ic][jc] !=  . ) continue; n     if (a[ic + 1][jc] ==  . ) { n       a[ic][jc] =  ^ ; n       a[ic + 1][jc] =  v ; n       add(ic + 1, jc); n     } n     if (a[ic - 1][jc] ==  . ) { n       a[ic][jc] =  v ; n       a[ic - 1][jc] =  ^ ; n       add(ic - 1, jc); n     } n     if (a[ic][jc + 1] ==  . ) { n       a[ic][jc] =  < ; n       a[ic][jc + 1] =  > ; n       add(ic, jc + 1); n     } n     if (a[ic][jc - 1] ==  . ) { n       a[ic][jc] =  > ; n       a[ic][jc - 1] =  < ; n       add(ic, jc - 1); n     } n     if (a[ic][jc] ==  . ) return nu(); n     add(ic, jc); n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (a[i][j] ==  . ) return nu(); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       putchar(a[i][j]); n     } n     putchar(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long v[100100]; n long long h[100100]; n int main() { n   long long n, m; n   cin >> n >> m; n   long long vidx = 0; n   for (; vidx < n; ++vidx) { n     long long x; n     cin >> x; n     v[vidx] = x; n   } n   long long hidx = 0; n   for (long long loop = 0; loop < m; ++loop) { n     long long x1, x2, y; n     cin >> x1 >> x2 >> y; n     if (x1 > 1) continue; n     h[hidx] = x2; n     ++hidx; n   } n   sort(v, v + vidx); n   sort(h, h + hidx); n   v[vidx] = 1e9; n   ++vidx; n   long long res = n + m; n   for (long long idx = 0; idx < vidx; ++idx) { n     long long lim = v[idx]; n     auto it = lower_bound(h, h + hidx, lim) - h; n     res = min(res, hidx - it + idx); n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long przodek(long long n, long long m) { n   if (n == m) return n; n   if (n > m) return przodek(n / 2, m); n   return przodek(n, m / 2); n } n int main() { n   long long q; n   cin >> q; n   set<pair<long long, long long> > path_fee; n   for (int i = 0; i < q; i++) { n     long long op, v, u, w; n     cin >> op; n     if (op == 1) { n       cin >> u >> v >> w; n       long long prz = przodek(u, v); n       while (u != prz) { n         pair<long long, long long> act_fee = *path_fee.lower_bound({u, 0}); n         if (act_fee.first == u) n           path_fee.erase(act_fee); n         else n           act_fee.second = 0; n         path_fee.insert({u, act_fee.second + w}); n         u /= 2; n       } n       while (v != prz) { n         pair<long long, long long> act_fee = *path_fee.lower_bound({v, 0}); n         if (act_fee.first == v) n           path_fee.erase(act_fee); n         else n           act_fee.second = 0; n         path_fee.insert({v, act_fee.second + w}); n         v /= 2; n       } n       continue; n     } n     cin >> u >> v; n     long long cost = 0; n     long long prz = przodek(u, v); n     while (u != prz) { n       pair<long long, long long> act_fee = *path_fee.lower_bound({u, 0}); n       if (act_fee.first == u) { n         cost += act_fee.second; n       } n       u /= 2; n     } n     while (v != prz) { n       pair<long long, long long> act_fee = *path_fee.lower_bound({v, 0}); n       if (act_fee.first == v) { n         cost += act_fee.second; n       } n       v /= 2; n     } n     cout << cost << endl; n   } n } n


#include <bits/stdc++.h> n int arr[1100000]; n using namespace std; n int main() { n   memset(arr, 0, sizeof arr); n   int n; n   cin >> n; n   int x = 1, pre = 1; n   for (int i = 1; i <= n; i++) { n     int c; n     cin >> c; n     arr[x] = i; n     x += c; n   } n   x = arr[1]; n   for (int i = 1; i <= 1000000; i++) { n     if (!arr[i]) n       arr[i] = x; n     else n       x = arr[i]; n   } n   int q; n   cin >> q; n   while (q--) { n     cin >> x; n     cout << arr[x] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, x, l, r, m; n int main() { n   cin >> n; n   int endtime = 0; n   while (n--) { n     cin >> x; n     endtime += x; n   } n   cin >> m; n   while (m--) { n     cin >> l >> r; n     if (endtime <= l) { n       cout << l << endl; n       return 0; n     } else if (endtime > r) { n       continue; n     } else { n       cout << endtime << endl; n       return 0; n     } n   } n   cout <<  -1  << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:64000000 ) n using namespace std; n const int N6 = 1e6 + 6; n const int N9 = 1e9 + 7; n const long long INFLL = (long long)1e18 + 100; n const int INF = 1e9 + 100; n const long long mod = 1e9 + 9; n const long long P = 31; n int n, k, cur, ans; n std::vector<int> g[1111]; n map<pair<int, int>, bool> cnt; n void solve() { n   cin >> n >> k; n   int cur; n   for (int i = 1; i <= n; ++i) { n     cur = 0; n     for (int j = 1; j <= n; ++j) { n       if (!cnt.count(make_pair(i, j)) && i != j) { n         ++cur; n         cnt[make_pair(i, j)]; n         cnt[make_pair(j, i)]; n         g[i].push_back(j); n         ++ans; n       } n       if (cur == k) { n         break; n       } n     } n     if (cur != k) { n       cout << -1; n       exit(0); n     } n   } n   cout << ans <<   n ; n   for (int i = 1; i <= n; ++i) { n     for (int j = 0; j < g[i].size(); ++j) { n       cout << i <<     << g[i][j] <<   n ; n     } n   } n } n int main() { n   int tt = 1; n   while (tt--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int a = 0; n   char c = getchar(); n   bool f = 0; n   while (!isdigit(c)) { n     if (c ==  - ) f = 1; n     c = getchar(); n   } n   while (isdigit(c)) { n     a = (a << 3) + (a << 1) + (c ^  0 ); n     c = getchar(); n   } n   return f ? -a : a; n } n int point[51][3], N; n void solve(int l, int r, int dir) { n   if (l == r) n     printf( (%01d*abs(((1-abs((t-%d)))+abs((abs((t-%d))-1))))) , n            point[l][dir] / 2, l, l); n   else { n     putchar( ( ); n     solve(l, l + r >> 1, dir); n     putchar( + ); n     solve((l + r >> 1) + 1, r, dir); n     putchar( ) ); n   } n } n int main() { n   N = read(); n   for (int i = 1; i <= N; ++i) { n     point[i][0] = read(); n     point[i][1] = read(); n     point[i][2] = read(); n   } n   solve(1, N, 0); n   putchar(  n ); n   solve(1, N, 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int max_n = 100111, inf = 1000111222; n const int max_lev = 18; n int n, m, q, parent[max_lev][max_n], best_h[max_n], used[max_n], h[max_n]; n int real_p[max_lev][max_n]; n int tin[max_n], tout[max_n], cur; n vector<int> g[max_n]; n void dfs(int v, int p) { n   tin[v] = ++cur; n   parent[0][v] = p; n   real_p[0][v] = p; n   for (int to : g[v]) { n     if (to == p) { n       continue; n     } n     if (tin[to]) { n       if (tin[to] < tin[parent[0][v]]) { n         parent[0][v] = to; n       } n     } else { n       dfs(to, v); n       if (tin[parent[0][to]] < tin[parent[0][v]]) { n         parent[0][v] = parent[0][to]; n       } n     } n   } n   tout[v] = cur; n } n void get_all_p(int parent[max_lev][max_n]) { n   for (int lev = 1; lev < max_lev; ++lev) { n     for (int i = 0; i < n; ++i) { n       parent[lev][i] = parent[lev - 1][parent[lev - 1][i]]; n     } n   } n } n bool is_ancestor(int v1, int v2) { n   return tin[v1] <= tin[v2] && tout[v1] >= tout[v2]; n } n int lca(int v1, int v2) { n   if (is_ancestor(v1, v2)) { n     return v1; n   } n   for (int i = max_lev - 1; i >= 0; --i) { n     if (!is_ancestor(real_p[i][v1], v2)) { n       v1 = real_p[i][v1]; n     } n   } n   return real_p[0][v1]; n } n int get_d(int v, int up) { n   if (v == up) { n     return 0; n   } n   int res = 1; n   for (int i = max_lev - 1; i >= 0; --i) { n     if (!is_ancestor(parent[i][v], up)) { n       res += 1 << i; n       v = parent[i][v]; n     } n   } n   return res; n } n int main() { n   scanf( %d%d%d , &n, &m, &q); n   while (m--) { n     int u, v; n     scanf( %d%d , &u, &v); n     --u; n     --v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   dfs(0, 0); n   get_all_p(parent); n   get_all_p(real_p); n   while (q--) { n     int u, v; n     scanf( %d%d , &u, &v); n     --u; n     --v; n     int lc = lca(u, v); n     int ans = get_d(u, lc) + get_d(v, lc); n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 6e5 + 8; n const int T = 2e5 + 2; n int n, b[N], t[N]; n template <typename _Tp> n inline void IN(_Tp& x) { n   char ch; n   bool flag = 0; n   x = 0; n   while (ch = getchar(), !isdigit(ch)) n     if (ch ==  - ) flag = 1; n   while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar(); n   if (flag) x = -x; n } n struct Segment_Tree { n   long long sum[N << 2]; n   void update(int x, int l, int r, int pos, int val) { n     if (l == r) { n       sum[x] += val; n       return void(); n     } n     (pos <= ((l + r) >> 1)) n         ? update(((x) << 1), l, ((l + r) >> 1), pos, val) n         : update(((x) << 1 | 1), ((l + r) >> 1) + 1, r, pos, val); n     sum[x] = max(sum[((x) << 1)], sum[((x) << 1 | 1)]); n   } n } Tree; n int main() { n   IN(n); n   for (int i = 1; i <= n; ++i) IN(b[i]), t[i] = b[i] - i + T; n   for (int i = 1; i <= n; ++i) Tree.update(1, 1, N - 1, t[i], b[i]); n   printf( %lld n , Tree.sum[1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, int> mp; n vector<string> vr; n int n, m; n int a[105][105], b[105][105]; n int xx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; n int yy[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; n bool check(int i, int j) { n   if (i >= 0 && i < n && j >= 0 && j < m && a[i][j] != -2 && a[i][j] != -6) n     return true; n   return false; n } n bool Check(int i, int j) { n   if (i >= 0 && i < n && j >= 0 && j < m && b[i][j] == -6) return true; n   return false; n } n void dfs(int x, int y) { n   int X, Y, i; n   for (i = 0; i < 8; i++) { n     X = x + xx[i]; n     Y = y + yy[i]; n     if (check(X, Y)) b[X][Y]++; n   } n } n int main() { n   char ma[105][105]; n   while (~scanf( %d%d , &n, &m)) { n     int f = 0; n     memset(a, 0, sizeof(a)); n     memset(b, 0, sizeof(b)); n     for (int i = 0; i < n; i++) { n       cin >> ma[i]; n     } n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         a[i][j] = ma[i][j] -  0 ; n       } n     } n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         if (a[i][j] == -6) { n           b[i][j] = -6; n           dfs(i, j); n         } n         if (a[i][j] == -2) b[i][j] = -2; n       } n     } n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         if (b[i][j] == -2) { n           for (int k = 0; k < 8; k++) { n             int X = xx[k] + i; n             int Y = yy[k] + j; n             if (Check(X, Y)) { n               f = 1; n               break; n             } n           } n         } n         if (a[i][j] != b[i][j]) { n           f = 1; n           break; n         } n         if (f == 1) break; n       } n       if (f == 1) break; n     } n     if (f) n       printf( NO n ); n     else n       printf( YES n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     long long int a, b, c, d; n     cin >> a >> b >> c >> d; n     string w, x, y, z, k =  Ya , l =  Tidak ; n     if (a == 0 && d == 0) { n       if (b % 2 == 1) { n         cout <<  Tidak Ya Tidak Tidak n ; n       } else { n         cout <<  Tidak Tidak Ya Tidak n ; n       } n     } else if (b == 0 && c == 0) { n       if (a % 2 == 1) { n         cout <<  Ya Tidak Tidak Tidak n ; n       } else { n         cout <<  Tidak Tidak Tidak Ya n ; n       } n     } else { n       if ((a + b) % 2 == 1) { n         cout <<  Ya Ya Tidak Tidak n ; n       } else { n         cout <<  Tidak Tidak Ya Ya n ; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n pair<long long, int> arr[N]; n long long t[4 * N] = {}; n inline void update(int v, int vl, int vr, int pos, long long val) { n   if (vl == vr) { n     t[v] = val; n     return; n   } n   int vm = vl + (vr - vl) / 2; n   if (pos <= vm) n     update(2 * v + 1, vl, vm, pos, val); n   else n     update(2 * v + 2, vm + 1, vr, pos, val); n   t[v] = max(t[2 * v + 1], t[2 * v + 2]); n } n inline long long get(int v, int vl, int vr, int l, int r) { n   if (r < vl || vr < l) return 0; n   if (l <= vl && vr <= r) return t[v]; n   int vm = vl + (vr - vl) / 2; n   long long ql = get(2 * v + 1, vl, vm, l, r); n   long long qr = get(2 * v + 2, vm + 1, vr, l, r); n   return max(ql, qr); n } n inline bool c(pair<long long, int> x, pair<long long, int> y) { n   if (x.first != y.first) n     return x.first < y.first; n   else n     return x.second > y.second; n } n int main() { n   ios_base ::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     long long r, h; n     cin >> r >> h; n     arr[i] = {1ll * r * r * h, i}; n   } n   sort(arr, arr + n, c); n   for (int i = 0; i < n; i++) { n     long long res = get(0, 0, n - 1, 0, max(0, arr[i].second - 1)); n     update(0, 0, n - 1, arr[i].second, res + arr[i].first); n   } n   cout << fixed << setprecision(6) << 3.14159265 * get(0, 0, n - 1, 0, n - 1) n        <<   n ; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9; n int n; n char s[20]; n int ans[300100]; n vector<int> dep[300100]; n int G[300100][26], siz[300100], son[300100]; n vector<int *> a; n void dfs(int o, int depth) { n   dep[depth].push_back(o); n   siz[o] = 1; n   for (int i = 0; i < 26; ++i) { n     if (G[o][i] == -1) continue; n     dfs(G[o][i], depth + 1); n     if (son[o] == -1 || siz[son[o]] < siz[G[o][i]]) { n       son[o] = G[o][i]; n     } n     siz[o] += siz[G[o][i]]; n   } n } n int DFS(int u, int v) { n   int ret = 0; n   for (int i = 0; i < 26; ++i) { n     if (G[u][i] == -1) continue; n     if (G[v][i] == -1) { n       G[v][i] = G[u][i]; n       ret += siz[G[u][i]]; n       a.push_back(&G[v][i]); n     } else { n       ret += DFS(G[u][i], G[v][i]); n     } n   } n   return ret; n } n int main() { n   memset(G, -1, sizeof(G)); n   memset(son, -1, sizeof(son)); n   scanf( %d , &n); n   for (int i = 1; i < n; ++i) { n     int u, v; n     scanf( %d%d%s , &u, &v, s); n     G[u][s[0] -  a ] = v; n   } n   dfs(1, 0); n   int tmp = 0; n   for (int i = 0; i <= n; ++i) { n     a.clear(); n     ans[i] += tmp; n     for (auto &j : dep[i]) { n       if (son[j] != -1) ans[i + 1] += siz[son[j]] - 1; n       for (int k = 0; k < 26; ++k) { n         if (G[j][k] == -1 || son[j] == G[j][k]) continue; n         ans[i + 1] += DFS(G[j][k], son[j]); n       } n     } n     for (auto &i : a) { n       *i = -1; n     } n     tmp += (int)dep[i].size(); n   } n   tmp = 1; n   for (int i = 2; i <= n; ++i) { n     if (ans[tmp] > ans[i]) { n       tmp = i; n     } n   } n   printf( %d n%d n , ans[tmp], tmp); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   string cad; n   cin >> n >> cad; n   int res = 0; n   for (int i = 0; i < n; ++i) { n     if (cad[i] ==  0 ) { n       res++; n       break; n     } else { n       res++; n     } n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s, ss; n int m, l, r, k, i, j, n, p; n int main() { n   cin >> s; n   cin >> m; n   ss = s; n   for (i = 1; i <= m; i++) { n     cin >> l >> r >> k; n     r--; n     l--; n     for (j = l; j <= r; j++) { n       p = r - l + 1; n       ss[j] = s[l + ((j - l - k) % p + p) % p]; n     } n     s = ss; n   } n   cout << ss << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2000000000; n const int MAX = 10000000; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   while (cin >> n) { n     map<string, int> m; n     for (int i = 0; i < n; i++) { n       string s; n       cin >> s; n       m[s]++; n     } n     int mx = 0; n     for (map<string, int>::iterator it = m.begin(); it != m.end(); it++) n       mx = max(mx, it->second); n     cout << mx <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, i = 0, last = 0, current = 0; n int main() { n   int x, y, z = 0, a = 0, c = 1; n   long long b; n   cin >> x; n   for (int i = 0; i < x; i++) { n     cin >> y >> z; n     a = z * 2; n     c = y * 2; n     if (a > c) { n       if (z > c) n         cout << z * z << endl; n       else n         cout << c * c << endl; n     } else { n       if (y > a) n         cout << y * y << endl; n       else n         cout << a * a << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int testnum; n struct Point3 { n   double x, y, z; n   Point3(double _x = 0, double _y = 0, double _z = 0) { n     x = _x; n     y = _y; n     z = _z; n   } n   bool operator==(Point3 other) { n     return abs(x - other.x) < 1e-9 && abs(y - other.y) < 1e-9 && n            abs(z - other.z) < 1e-9; n   } n   bool operator!=(Point3 other) { n     return abs(x - other.x) > 1e-9 || abs(y - other.y) > 1e-9 || n            abs(z - other.z) > 1e-9; n   } n   double length() { return sqrt(x * x + y * y + z * z); } n   Point3 operator*(double k) { return Point3(k * x, k * y, k * z); } n   Point3 normalize() { return (*this) * (1 / length()); } n   Point3 operator+(Point3 other) { n     return Point3(x + other.x, y + other.y, z + other.z); n   } n   Point3 operator-(Point3 other) { n     return Point3(x - other.x, y - other.y, z - other.z); n   } n   double dist(Point3 other) { return (*this - other).length(); } n   double operator^(Point3 other) { n     return x * other.x + y * other.y + z * other.z; n   } n   Point3 operator*(Point3 other) { n     return Point3(y * other.z - z * other.y, z * other.x - x * other.z, n                   x * other.y - y * other.x); n   } n   void display() { printf( Point3 (%lf , %lf , %lf) n , x, y, z); } n }; n Point3 operator*(double k, Point3 p) { n   return Point3(k * p.x, k * p.y, k * p.z); n } n struct Point2 { n   double x, y; n   Point2(double _x = 0, double _y = 0) { n     x = _x; n     y = _y; n   } n   double length() { return sqrt(x * x + y * y); } n   Point2 operator*(double k) { return Point2(k * x, k * y); } n   Point2 normalize() { return (*this) * (1 / length()); } n   Point2 operator+(Point2 other) { return Point2(x + other.x, y + other.y); } n   Point2 operator-(Point2 other) { return Point2(x - other.x, y - other.y); } n   double dist(Point2 other) { return (*this - other).length(); } n   double operator^(Point2 other) { return x * other.x + y * other.y; } n   double operator*(Point2 other) { return x * other.y - y * other.x; } n   void display() { printf( Point2 (%lf , %lf) n , x, y); } n }; n Point2 operator*(double k, Point2 p) { return Point2(k * p.x, k * p.y); } n struct Plane { n   double a, b, c, d; n   Point3 A, B, C; n   Point3 N, O, X, Y; n   Plane() {} n   Plane(Point3 _AA, Point3 _BB, Point3 _CC) { n     A = _AA; n     B = _BB; n     C = _CC; n     Point3 normal = (B - A) * (C - A); n     N = normal.normalize(); n     a = N.x; n     b = N.y; n     c = N.z; n     d = -(A ^ N); n     O = Point3(-d * a, -d * b, -d * c); n     if (A != O) n       X = (A - O).normalize(); n     else n       X = (B - O).normalize(); n     Y = normal * X; n   } n   Plane(double _a, double _b, double _c, double _d) { n     double len = sqrt(_a * _a + _b * _b + _c * _c); n     a = _a / len; n     b = _b / len; n     c = _c / len; n     d = _d / len; n     N = Point3(a, b, c); n     if (a != 0) { n       A = Point3(-(d + 0 * b + 0 * c) / a, 0, 0); n       B = Point3(-(d + 1 * b + 0 * c) / a, 1, 0); n       C = Point3(-(d + 0 * b + 1 * c) / a, 0, 1); n     } else if (b != 0) { n       A = Point3(0, -(d + 0 * a + 0 * c) / b, 0); n       B = Point3(1, -(d + 1 * a + 0 * c) / b, 0); n       C = Point3(0, -(d + 0 * a + 1 * c) / b, 1); n     } else { n       A = Point3(0, 0, -(d + 0 * a + 0 * b) / c); n       B = Point3(1, 0, -(d + 1 * a + 0 * b) / c); n       C = Point3(0, 1, -(d + 0 * a + 1 * b) / c); n     } n     O = Point3(-d * a, -d * b, -d * c); n     if (A != O) n       X = (A - O).normalize(); n     else n       X = (B - O).normalize(); n     Y = N * X; n   } n   Point3 project(Point3 P) { return (P - (P ^ N) * N) + O; } n   Point2 project2D(Point3 P) { n     P = project(P); n     return Point2(P ^ X, P ^ Y); n   } n   double distance(Point3 p) { return abs(a * p.x + b * p.y + c * p.z + d); } n   void display() { n     printf( Plane (%lf , %lf , %lf , %lf) n , a, b, c, d); n     printf( A :  ); n     A.display(); n     printf( B :  ); n     B.display(); n     printf( C :  ); n     C.display(); n     printf( N :  ); n     N.display(); n     printf( O :  ); n     O.display(); n     printf( X :  ); n     X.display(); n     printf( Y :  ); n     Y.display(); n   } n }; n void preprocess() {} n int n, m; n vector<Point3> p3; n vector<Point2> p2; n vector<Plane> plns; n double eval(double x, double y) { n   double ans = 0; n   double dx, dy; n   for (int i = 0; i < n; i++) { n     dx = x - p2[i].x; n     dy = y - p2[i].y; n     ans = max(ans, dx * dx + dy * dy); n   } n   return ans; n } n pair<double, double> doTernaryY(double x) { n   double lo = 1e4; n   double hi = 0; n   for (int i = 0; i < n; i++) { n     lo = min(lo, p2[i].y); n     hi = max(hi, p2[i].y); n   } n   for (int iter = 0; iter < 35; iter++) { n     double y1 = (lo + lo + hi) / 3; n     double y2 = (lo + hi + hi) / 3; n     double val1 = eval(x, y1); n     double val2 = eval(x, y2); n     if (val1 < val2) n       hi = y2; n     else n       lo = y1; n   } n   double ans = (lo + hi) / 2; n   return make_pair(ans, eval(x, ans)); n } n pair<double, double> doTernaryX() { n   double lo = 1e4; n   double hi = 0; n   for (int i = 0; i < n; i++) { n     lo = min(lo, p2[i].x); n     hi = max(hi, p2[i].x); n   } n   for (int iter = 0; iter < 35; iter++) { n     double x1 = (lo + lo + hi) / 3; n     double x2 = (lo + hi + hi) / 3; n     double val1 = doTernaryY(x1).second; n     double val2 = doTernaryY(x2).second; n     if (val1 < val2) n       hi = x2; n     else n       lo = x1; n   } n   double ans = (lo + hi) / 2; n   return make_pair(ans, doTernaryY(ans).first); n } n void solve() { n   p2.resize(n); n   for (int i = 0; i < m; i++) { n     for (int j = 0; j < n; j++) p2[j] = plns[i].project2D(p3[j]); n     pair<double, double> centre = doTernaryX(); n     double rad = sqrt(eval(centre.first, centre.second)); n     printf( %.9lf n , rad); n   } n } n bool input() { n   scanf( %d , &n); n   scanf( %d , &m); n   p3.resize(n); n   plns.resize(m); n   int a, b, c; n   for (int i = 0; i < n; i++) { n     scanf( %d , &a); n     scanf( %d , &b); n     scanf( %d , &c); n     p3[i] = Point3(a, b, c); n   } n   for (int i = 0; i < m; i++) { n     scanf( %d , &a); n     scanf( %d , &b); n     scanf( %d , &c); n     plns[i] = Plane(a, b, c, 0); n   } n   return true; n } n int main() { n   preprocess(); n   int T = 1; n   for (testnum = 1; testnum <= T; testnum++) { n     if (!input()) break; n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, m, a[1000005]; n multiset<long long int> st; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   for (long long int i = 1; i <= n; i++) cin >> a[i]; n   long long int ans = 0; n   for (long long int i = 1; i <= n; i++) { n     st.insert(a[i]); n     auto it = st.begin(); n     if (*it < a[i]) { n       ans += a[i] - *it; n       st.erase(it); n       st.insert(a[i]); n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 10; n int main() { n   ios_base ::sync_with_stdio(false), cout.tie(0), cin.tie(0); n   int n, cnt = 0; n   cin >> n; n   int sv = n; n   while (n--) { n     int x, y; n     cin >> x >> y; n     if (x < 0) ++cnt; n   } n   cout << (min(cnt, sv - cnt) <= 1 ?  Yes n  :  No n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[200010]; n int main() { n   long long n, k; n   cin >> n >> k; n   n -= k * (k + 1) / 2; n   if (n < 0) { n     cout <<  NO  << endl; n     return 0; n   } n   for (int i = 0; i < k; i++) { n     a[i] = i + 1 + n / k + (i >= (k - n % k)); n   } n   bool ok = 1; n   for (int i = 1; i < k; i++) { n     if (a[i] > a[i - 1] * 2) { n       if (i == k - 1) ok = 0; n       a[k - 1]++; n       a[i]--; n     } n   } n   if (ok) { n     cout <<  YES  << endl; n     for (int i = 0; i < k; i++) cout << a[i] << endl; n   } else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   int arr[n]; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   int s1 = count(arr, arr + n, 1); n   int s2 = count(arr, arr + n, -1); n   for (int i = 0; i < m; i++) { n     int a, b; n     scanf( %d %d , &a, &b); n     if ((b - a + 1) % 2 != 0) { n       printf( %d n , 0); n     } else { n       int c = (b - a + 1) / 2; n       if (s1 >= c && s2 >= c) { n         printf( %d n , 1); n       } else { n         printf( %d n , 0); n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, long long> mp; n int main() { n   long long k, i; n   cin >> k; n   string s; n   cin >> s; n   long long ans = 0, cnt = 0; n   long long n = s.length(); n   mp[0] = 1; n   for (i = 0; i < n; i++) { n     if (s[i] ==  1 ) { n       cnt++; n     } n     if (cnt >= k) { n       ans += mp[cnt - k]; n     } n     mp[cnt]++; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = (long long)1e5 + 5; n long long a[N]; n inline void solve() { n   long long n; n   cin >> n; n   queue<pair<long long, long long> > negative, positive; n   long long pos_sum = 0; n   for (long long i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n     if (a[i] == 0) { n       continue; n     } n     if (a[i] > 0) { n       pos_sum += a[i]; n       positive.push({i, a[i]}); n     } else { n       negative.push({i, a[i]}); n     } n   } n   while (!positive.empty()) { n     long long p = positive.front().first; n     long long x = positive.front().second; n     while (!negative.empty() && negative.front().first < p) { n       negative.pop(); n     } n     if (negative.empty()) { n       break; n     } n     long long relax = min(x, -negative.front().second); n     positive.front().second -= relax; n     pos_sum -= relax; n     if (positive.front().second == 0) { n       positive.pop(); n     } n     if (relax >= -negative.front().second) { n       negative.pop(); n     } else { n       negative.front().second += relax; n     } n   } n   cout << pos_sum << endl; n } n signed main() { n   long long Q; n   cin >> Q; n   while (Q--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int lst[200005], nxt[400005], to[400005], tol; n int sz[200005], f[200005]; n long long dp[200005]; n int vis[200005]; n int qu[200005]; n int N, K; n void adde(int u, int v) { n   nxt[tol] = lst[u]; n   to[tol] = v; n   lst[u] = tol; n   ++tol; n } n void solve() { n   cin >> N >> K; n   memset(lst, 0xff, sizeof(lst)); n   tol = 0; n   memset(sz, 0, sizeof(sz)); n   for (int i = 0; i < (K * 2); ++i) { n     int u; n     scanf( %d , &u); n     sz[u] = 1; n   } n   for (int i = 0; i < (N - 1); ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     adde(u, v); n     adde(v, u); n   } n   memset(vis, 0, sizeof(vis)); n   memset(dp, 0, sizeof(dp)); n   int tail = 0; n   f[1] = 0; n   vis[1] = 1; n   qu[tail++] = 1; n   for (int i = 0; i < (tail); ++i) { n     int u = qu[i]; n     for (int p = lst[u]; p != -1; p = nxt[p]) { n       int v = to[p]; n       if (!vis[v]) { n         vis[v] = 1; n         f[v] = u; n         qu[tail++] = v; n       } n     } n   } n   for (int i = tail - 1; i >= 0; --i) { n     int u = qu[i]; n     dp[u] += min(sz[u], 2 * K - sz[u]); n     sz[f[u]] += sz[u]; n     dp[f[u]] += dp[u]; n   } n   cout << dp[1] << endl; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, ans, now; n int v[505][505]; n int main() { n   scanf( %d%d , &n, &k); n   now = n * n; n   for (int i = n; i >= 1; i--) n     for (int h = n; h >= k; h--) v[i][h] = now--; n   now = 1; n   for (int i = 1; i <= n; i++) n     for (int h = 1; h < k; h++) v[i][h] = now++; n   for (int i = 1; i <= n; i++) ans += v[i][k]; n   printf( %d n , ans); n   for (int i = 1; i <= n; i++) { n     for (int h = 1; h <= n; h++) cout << v[i][h] <<    ; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[31][31], used[31][31]; n long long n, m; n vector<pair<int, int> > zz = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; n int check(long long x, long long y) { n   if (x >= 1 && x <= n && y >= 1 && y <= m && used[x][y] == 0) return 1; n   return 0; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   for (; t > 0; t--) { n     long long ans = 0; n     cin >> n >> m; n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= m; j++) { n         cin >> a[i][j]; n         used[i][j] = 0; n       } n     if (n == 1 && m == 1) { n       cout << 0 <<   n ; n       continue; n     } n     queue<int> x, y, dep; n     int k = 0; n     x.push(1); n     y.push(1); n     dep.push(1); n     x.push(n); n     y.push(m); n     dep.push(1); n     used[1][1] = 1; n     used[n][m] = 1; n     long long z = n + m - 1; n     z /= 2; n     while (k + 1 <= z) { n       k++; n       long long ed = 0; n       long long nul = 0; n       while (!dep.empty() && dep.front() == k) { n         long long xx = x.front(); n         x.pop(); n         long long yy = y.front(); n         y.pop(); n         long long d = dep.front(); n         dep.pop(); n         if (a[xx][yy] == 1) n           ed++; n         else n           nul++; n         for (int i = 0; i < 4; i++) { n           xx += zz[i].first; n           yy += zz[i].second; n           if (check(xx, yy)) { n             x.push(xx); n             y.push(yy); n             dep.push(d + 1); n             used[xx][yy] = 1; n           } n           xx -= zz[i].first; n           yy -= zz[i].second; n         } n       } n       ans += min(ed, nul); n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[100010]; n inline int read() { n   char c = getchar(); n   int x = 0, flag = 1; n   while (!isdigit(c)) { n     if (c ==  - ) flag = -1; n     c = getchar(); n   } n   while (isdigit(c)) x = x * 10 + c -  0 , c = getchar(); n   return x * flag; n } n int main() { n   int n = read(), k = read(), lst = 1, ans = 0; n   for (int i = 1; i <= n; i++) { n     a[i] = read(); n     if (a[i] == a[i - 1]) lst = i; n     ans = max(ans, i - lst + 1); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n struct lazy_seg { n  public: n   vector<T> st, lazy; n   lazy_seg(int n) { st.resize(4 * n + 1, 0), lazy.resize(4 * n + 1, 0); } n   lazy_seg() = default; n   void push(int l, int r, int curr) { n     if (lazy[curr] != 0) { n       st[curr] += lazy[curr] * (r - l + 1); n       if (l != r) n         lazy[2 * curr] += lazy[curr], lazy[2 * curr + 1] += lazy[curr]; n       lazy[curr] = 0; n     } n   } n   void pull(int l, int r, int curr) { n     push(l, r, curr); n     int mid = l + (r - l) / 2; n     if (l != r) push(l, mid, 2 * curr), push(mid + 1, r, 2 * curr + 1); n     st[curr] = (st[2 * curr] + st[2 * curr + 1]); n   } n   void upd(int x, int y, T v, int l, int r, int curr) { n     int mid = (l + r) / 2; n     push(l, r, curr); n     if (l == x && r == y) { n       lazy[curr] += v; n       push(l, r, curr); n       return; n     } n     if (y <= mid) n       upd(x, y, v, l, mid, 2 * curr); n     else if (x > mid) n       upd(x, y, v, mid + 1, r, 2 * curr + 1); n     else n       upd(x, mid, v, l, mid, 2 * curr), n           upd(mid + 1, y, v, mid + 1, r, 2 * curr + 1); n     pull(l, r, curr); n   } n   T qry(int x, int y, int l, int r, int curr) { n     int mid = (l + r) / 2; n     push(l, r, curr); n     if (l == x && r == y) return st[curr]; n     if (y <= mid) n       return qry(x, y, l, mid, 2 * curr); n     else if (x > mid) n       return qry(x, y, mid + 1, r, 2 * curr + 1); n     return (qry(x, mid, l, mid, 2 * curr) + n             qry(mid + 1, y, mid + 1, r, 2 * curr + 1)); n   } n   void build(int l, int r, int curr) { n     int mid = (l + r) / 2; n     if (l == r) { n       st[curr] = 0; n       return; n     } n     build(l, mid, 2 * curr); n     build(mid + 1, r, 2 * curr + 1); n     st[curr] = (st[2 * curr] + st[2 * curr + 1]); n   } n }; n const int N = 500005; n int n; n string s; n lazy_seg<long long> f(N); n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   cin >> n >> s; n   long long ans = 0; n   int curl = 0; n   bool has = false; n   stack<pair<int, int> > opt_range; n   opt_range.push({-1000000000, -1}); n   for (int i = 0; i < n; i++) { n     long long fa = ans; n     if (s[i] ==  0 ) { n       if (has) { n         opt_range.push({curl, i - 1}); n       } n       ans += f.qry(0, i, 0, n - 1, 1); n       has = false; n     } else { n       if (!has) { n         has = 1; n         curl = i; n       } n       if (opt_range.top().second != -1) { n         auto u = opt_range.top(); n         f.upd(u.second + 1, i, +1, 0, n - 1, 1); n         u.second--; n         if (u.first > u.second) { n           opt_range.pop(); n         } else { n           opt_range.pop(); n           opt_range.push(u); n         } n       } else { n         f.upd(0, i, 1, 0, n - 1, 1); n       } n       ans += f.qry(0, i, 0, n - 1, 1); n     } n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)1e3 + 5; n const int INF = (int)1e9 + 7; n int x[N], y[N]; n int n; n map<pair<int, int>, int> id; n vector<pair<int, int> > g[2][2]; n int dist(int i, int j) { n   return (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]); n } n void calc(vector<pair<int, int> > g[2][2]) { n   bool flag = 0; n   int sz00 = (int)g[0][0].size(); n   int sz11 = (int)g[1][1].size(); n   int sz01 = (int)g[0][1].size(); n   int sz10 = (int)g[1][0].size(); n   int fx, fy; n   if (sz00 == n) { n     flag = 1, fx = 0, fy = 0; n   } n   if (sz11 == n) { n     flag = 1, fx = 1, fy = 1; n   } n   if (sz01 == n) { n     flag = 1, fx = 0, fy = 1; n   } n   if (sz10 == n) { n     flag = 1, fx = 1, fy = 0; n   } n   if (flag) { n     vector<pair<int, int> > nw[2][2]; n     for (auto x : g[fx][fy]) { n       int a = x.first, b = x.second; n       a /= 2, b /= 2; n       id[{a, b}] = id[{x.first, x.second}]; n       nw[abs(a) % 2][abs(b) % 2].push_back({a, b}); n     } n     calc(nw); n   } n   if (sz00 + sz11 && sz01 + sz10) { n     cout << sz00 + sz11 << endl; n     for (pair<int, int> c : g[0][0]) { n       cout << id[c] <<    ; n     } n     for (pair<int, int> c : g[1][1]) { n       cout << id[c] <<    ; n     } n     exit(0); n   } n   if (sz00 && sz11) { n     cout << sz00 << endl; n     for (pair<int, int> c : g[0][0]) { n       cout << id[c] <<    ; n     } n     exit(0); n   } n   if (sz01 && sz10) { n     cout << sz01 << endl; n     for (pair<int, int> c : g[0][1]) { n       cout << id[c] <<    ; n     } n     exit(0); n   } n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &x[i], &y[i]); n     x[i] += 1e6, y[i] += 1e6; n     g[abs(x[i]) % 2][abs(y[i]) % 2].push_back({x[i], y[i]}); n     id[{x[i], y[i]}] = i; n   } n   calc(g); n } n


#pragma comment(linker,  /STACK:256777216 ) n #define _USE_MATH_DEFINES n #define _CRT_SECURE_NO_WARNINGS n #include <iostream> n #include <string> n #include <cstring> n #include <vector> n #include <queue> n #include <bitset> n #include <stack> n #include <deque> n #include <set> n #include <unordered_set> n #include <map> n #include <unordered_map> n #include <algorithm> n #include <math.h> n #include <chrono> n #include <cmath> n #include <climits> n #include <ctime> n #include <random> n #include <iostream> n #include <cstdio> n #include <iomanip> n using namespace std; n  n #define forx(_name,_from, to, value) for (int name = from; name < to; name += value) n #define rforx(_name, from, to, value) for (int name = from; name > to; name -= _value) n #define all(_STL_NAME) _STL_NAME.begin(), _STL_NAME.end() n #define rall(_STL_NAME) _STL_NAME.rbegin(), _STL_NAME.rend() n #define mp(_FIRST,_SECOND) make_pair(_FIRST,_SECOND) n  n typedef long long ll; n typedef unsigned long long llu; n typedef long double ld; n const ld eps = 1e-9; n mt19937 rndm; n  n void start() { n  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); n  cout << setprecision(5); n  cout.setf(ios::fixed); n  string FILENAME =  dice ; n  rndm.seed(time(0)); n #ifdef _DEBUG n  freopen( input.txt ,  rt , stdin); n  freopen( output.txt ,  wt , stdout); n #else n  /*freopen((FILENAME +  .in ).c_str(),  rt , stdin); n  freopen((FILENAME +  .out ).c_str(),  wt , stdout);*/ n #endif n } n  n const int MAX_N = 5e3 + 5; n const int INF = 1e9 + 7; n const int MOD = 1e9 + 7; n  n int a[MAX_N]; n ll cnt[MAX_N]; n ll dp1[MAX_N][MAX_N]; n ll dp2[MAX_N][MAX_N]; n  n void CalcCnt(int n, int k) { n  for (int i = 0; i < n; ++i) { n   dp1[i][0] = 1; n  } n  n  for (int j = 1; j <= k; ++j) { n   for (int i = 0; i < n; ++i) { n    if (i > 0) { n     dp1[i][j] = (dp1[i][j] + dp1[i - 1][j - 1]) % MOD; n    } n    if (i < n - 1) { n     dp1[i][j] = (dp1[i][j] + dp1[i + 1][j - 1]) % MOD; n    } n   } n  } n  n  for (int i = 0; i < n; ++i) { n   for (int j = 0; j <= k; ++j) { n    dp2[i][j] = (dp1[i][j] * dp1[i][k - j]) % MOD; n   } n  } n  n  for (int i = 0; i < n; ++i) { n   for (int j = 0; j <= k; ++j) { n    cnt[i] = (cnt[i] + dp2[i][j]) % MOD; n   } n  } n } n  n void solve() { n  int n, k, q; n  cin >> n >> k >> q; n  CalcCnt(n, k); n  for (int i = 0; i < n; ++i) { n   cin >> a[i]; n  } n  n  ll ans = 0; n  for (int i = 0; i < n; ++i) { n   ans = (ans + (a[i] * cnt[i]) % MOD) % MOD; n  } n  n  for (int i = 0; i < q; ++i) { n   int ii, x; n   cin >> ii >> x; n   --ii; n   ans = (ans - (a[ii] * cnt[ii]) % MOD + MOD) % MOD; n   a[ii] = x; n   ans = (ans + (a[ii] * cnt[ii]) % MOD) % MOD; n   cout << ans <<   n ; n  } n } n  n void multitest() { n  int t; n  cin >> t; n  for (int i = 0; i < t; ++i) { n   solve(); n  } n } n  n int main() { n  start(); n  //multitest(); n  solve(); n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e6 + 2; n vector<int> g[MAXN]; n int d[MAXN], low[MAXN], scc[MAXN]; n bool stacked[MAXN]; n stack<int> s; n int ticks, current_scc; n void tarjan(int u) { n   d[u] = low[u] = ticks++; n   s.push(u); n   stacked[u] = true; n   const vector<int> &out = g[u]; n   for (int k = 0, m = out.size(); k < m; ++k) { n     const int &v = out[k]; n     if (d[v] == -1) { n       tarjan(v); n       low[u] = min(low[u], low[v]); n     } else if (stacked[v]) { n       low[u] = min(low[u], low[v]); n     } n   } n   if (d[u] == low[u]) { n     int v; n     do { n       v = s.top(); n       s.pop(); n       stacked[v] = false; n       scc[v] = current_scc; n     } while (u != v); n     current_scc++; n   } n } n vector<pair<long long, long long> > adj[2 * MAXN]; n vector<pair<long long, pair<long long, long long> > > reg; n long long peso[2 * MAXN]; n long long dp[2 * MAXN]; n long long getval(long long x) { n   long long r = sqrt(2 * x); n   if (r * r + r > 2 * x) r--; n   return (r + 1) * x - (r * (r + 1) * (r + 2) / 6); n } n long long solve(long long u) { n   long long high = 0; n   if (dp[u] != -1) return dp[u]; n   for (auto d : adj[u]) { n     long long v = d.first, w = d.second; n     high = max(high, solve(v) + w); n   } n   return dp[u] = high; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   ; n   memset(d, -1, sizeof(d)); n   memset(scc, -1, sizeof(scc)); n   long long n, m; n   cin >> n >> m; n   for (long long(i) = (0); (i) < (m); (i)++) { n     long long u, v, w; n     cin >> u >> v >> w; n     g[u].push_back(v); n     reg.push_back(make_pair((u), make_pair((v), (w)))); n   } n   long long x; n   cin >> x; n   tarjan(x); n   long long tot = current_scc; n   for (auto t : reg) { n     long long u = t.first, v = t.second.first, w = t.second.second; n     if (scc[u] != -1 && scc[u] == scc[v]) { n       peso[scc[u]] += getval(w); n     } else if (scc[u] == -1 || scc[v] == -1) n       continue; n     else if (scc[u] != scc[v]) { n       adj[scc[u] + tot].push_back(make_pair(scc[v], w)); n     } n   } n   for (long long(i) = (0); (i) < (tot); (i)++) { n     adj[i].push_back(make_pair(i + tot, peso[i])); n   } n   memset(dp, -1, sizeof(dp)); n   cout << solve(scc[x]) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cap[200005]; n int empty[200005]; n int main() { n   int n, i, m, z, p, k, x; n   cin >> n; n   set<int> unfilled; n   for (i = 1; i <= n; i++) { n     cin >> cap[i]; n     unfilled.insert(i); n     empty[i] = cap[i]; n   } n   cin >> m; n   for (i = 1; i <= m; i++) { n     cin >> z; n     if (z == 1) { n       cin >> p >> x; n       set<int>::iterator it = unfilled.upper_bound(p - 1); n       int flag = 0; n       while (it != unfilled.end() && flag == 0) { n         int l = *it; n         if (x >= empty[l]) { n           x = x - empty[l]; n           empty[l] = 0; n           it++; n           unfilled.erase(l); n         } else { n           empty[l] = empty[l] - x; n           it++; n           flag = 1; n         } n       } n     } else { n       cin >> k; n       int l = cap[k] - empty[k]; n       cout << l <<   n ; n     } n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n long long dp[1001][1001]; n long long mem(int sz, int rem) { n   if (rem == 0) return 1; n   if (dp[sz][rem] != -1) return dp[sz][rem]; n   long long& ref = dp[sz][rem]; n   ref = 0; n   for (int i = 1; i <= sz - 2; i++) { n     long long t = (sz - 1 - i) * mem(i, rem - 1); n     ref = (ref + t) % 1000000007LL; n   } n   return ref; n } n int n, m, k; n int main() { n   cin >> n >> m >> k; n   memset(dp, -1, sizeof dp); n   long long t1 = mem(n, k); n   long long t2 = mem(m, k); n   long long res = (t1 * t2) % 1000000007LL; n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   pair<int, int> u, d, r, l, c; n   u.first = 0; n   u.second = 1; n   d.first = 0; n   d.second = -1; n   r.first = 1; n   r.second = 0; n   l.first = -1; n   l.second = 0; n   int ans = 0, n; n   string s; n   cin >> n >> s; n   for (int i = 0; i < n - 1; i++) { n     if (s[i] ==  U ) { n       c.first = u.first; n       c.second = u.second; n     } else if (s[i] ==  D ) { n       c.first = d.first; n       c.second = d.second; n     } else if (s[i] ==  L ) { n       c.first = l.first; n       c.second = l.second; n     } else { n       c.first = r.first; n       c.second = r.second; n     } n     for (int j = i + 1; j < n; j++) { n       if (s[j] ==  U ) { n         c.first += u.first; n         c.second += u.second; n       } else if (s[j] ==  D ) { n         c.first += d.first; n         c.second += d.second; n       } else if (s[j] ==  L ) { n         c.first += l.first; n         c.second += l.second; n       } else { n         c.first += r.first; n         c.second += r.second; n       } n       if (c.first == 0 && c.second == 0) ans++; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   int a[k]; n   int count = 0; n   for (int i = 0; i < k; i++) { n     cin >> a[i]; n   } n   while (n--) { n     int b; n     for (int i = 0; i < m; i++) { n       cin >> b; n       int *p = find(a, a + k, b); n       count = count + p - a + 1; n       for (int j = p - a; j > 0; j--) { n         int t = a[j]; n         a[j] = a[j - 1]; n         a[j - 1] = t; n       } n     } n   } n   cout << count << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long H; n   int n; n   cin >> H >> n; n   vector<long long> a(n); n   long long sum = 0; n   long long gap = 0; n   long long h = H; n   int f = 0; n   int pos; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     sum -= a[i]; n     h += a[i]; n     if (h <= 0 && !f) { n       f = 1; n       pos = i + 1; n     } n     gap = max(gap, sum); n   } n   if (f) { n     cout << pos << endl; n     return 0; n   } n   if (sum <= 0) { n     cout << -1 << endl; n     return 0; n   } n   long long whole = (H - gap) / sum; n   H -= whole * sum; n   long long result = whole * n; n   for (int i = 0;; i++) { n     H += a[i % n]; n     result++; n     if (H <= 0) { n       cout << result << endl; n       break; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (int)1e9; n const int len = (int)2e5; n int a[len]; n int main() { n   int k, n; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   string s; n   cin >> s; n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     int j = i; n     vector<int> v; n     while (j < n && s[j] == s[i]) { n       v.push_back(a[j]); n       j++; n     } n     sort(v.begin(), v.end(), greater<int>()); n     for (int l = 0; l < min(k, (int)v.size()); l++) { n       ans += v[l]; n     } n     i = j - 1; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( -O3 ) n using namespace std; n void _main(); n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   _main(); n } n const int inf = INT_MAX / 2; n const long long infl = 1LL << 60; n template <class T> n bool chmax(T &a, const T &b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n bool chmin(T &a, const T &b) { n   if (b < a) { n     a = b; n     return 1; n   } n   return 0; n } n string E; n int P, M; n struct Node { n   Node *left, *right; n   int value, id, cnt; n }; n int i = 0, idx = 0; n Node *parse() { n   Node *n = new Node; n   n->id = idx; n   idx++; n   if (E[i] ==  ( ) { n     i++; n     n->left = parse(); n     i++; n     n->right = parse(); n     i++; n     n->value = -1; n     n->cnt = n->left->cnt + n->right->cnt + 1; n   } else { n     n->value = E[i] -  0 ; n     i++; n     n->cnt = 0; n   } n   return n; n } n int vis[4101][105]; n pair<int, int> memo[4101][105]; n pair<int, int> f(Node *n, int p) { n   int m = n->cnt - p; n   if (m < 0) return {inf, inf}; n   int id = n->id; n   if (vis[id][p]) return memo[id][p]; n   if (n->cnt == 0) return memo[id][p] = {n->value, n->value}; n   int mi = inf, ma = -inf; n   for (int pls = 0; pls < p; pls++) { n     auto le = f(n->left, pls); n     if (le.first == inf) continue; n     auto ri = f(n->right, p - pls - 1); n     if (ri.first == inf) continue; n     vector<int> a = {le.first, le.second}; n     vector<int> b = {ri.first, ri.second}; n     for (int i = 0; i < 2; i++) n       for (int j = 0; j < 2; j++) { n         int res = a[i] + b[j]; n         chmin(mi, res); n         chmax(ma, res); n       } n   } n   for (int pls = 0; pls < p + 1; pls++) { n     auto le = f(n->left, pls); n     if (le.first == inf) continue; n     auto ri = f(n->right, p - pls); n     if (ri.first == inf) continue; n     vector<int> a = {le.first, le.second}; n     vector<int> b = {ri.first, ri.second}; n     for (int i = 0; i < 2; i++) n       for (int j = 0; j < 2; j++) { n         int res = a[i] - b[j]; n         chmin(mi, res); n         chmax(ma, res); n       } n   } n   assert(mi != inf); n   vis[id][p] = 1; n   return memo[id][p] = {mi, ma}; n } n pair<int, int> g(Node *n, int p) { n   int m = n->cnt - p; n   if (m < 0) return {inf, inf}; n   int id = n->id; n   if (vis[id][p]) return memo[id][p]; n   if (n->cnt == 0) return memo[id][p] = {n->value, n->value}; n   int mi = inf, ma = -inf; n   for (int pls = 0; pls < p; pls++) { n     auto le = g(n->left, pls); n     if (le.first == inf) continue; n     auto ri = g(n->right, p - pls - 1); n     if (ri.first == inf) continue; n     vector<int> a = {le.first, le.second}; n     vector<int> b = {ri.first, ri.second}; n     for (int i = 0; i < 2; i++) n       for (int j = 0; j < 2; j++) { n         int res = a[i] - b[j]; n         chmin(mi, res); n         chmax(ma, res); n       } n   } n   for (int pls = 0; pls < p + 1; pls++) { n     auto le = g(n->left, pls); n     if (le.first == inf) continue; n     auto ri = g(n->right, p - pls); n     if (ri.first == inf) continue; n     vector<int> a = {le.first, le.second}; n     vector<int> b = {ri.first, ri.second}; n     for (int i = 0; i < 2; i++) n       for (int j = 0; j < 2; j++) { n         int res = a[i] + b[j]; n         chmin(mi, res); n         chmax(ma, res); n       } n   } n   assert(mi != inf); n   vis[id][p] = 1; n   return memo[id][p] = {mi, ma}; n } n void _main() { n   cin >> E >> P >> M; n   E +=  = ; n   Node *root = parse(); n   if (P <= M) { n     auto res = f(root, P); n     int ans = res.second; n     cout << ans << endl; n   } else { n     auto res = g(root, M); n     int ans = res.second; n     cout << ans << endl; n   } n } n