#include <bits/stdc++.h> n using namespace std; n vector<vector<int> > c(6, vector<int>(4, 0)); n bool full[6]; n bool polu[6]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(nullptr); n   for (int i = 0; i < 6; ++i) { n     bool was[7] = {}; n     for (int j = 0; j < 4; ++j) { n       cin >> c[i][j]; n       was[c[i][j]] = 1; n     } n     int cnt = 0; n     for (int j = 1; j <= 6; ++j) cnt += was[j]; n     if (cnt == 1) full[i] = 1; n     if (cnt == 2) polu[i] = 1; n   } n   int cnt1 = 0, cnt2 = 0; n   for (int i = 0; i < 6; ++i) cnt1 += full[i], cnt2 += polu[i]; n   if (!(cnt1 == 2 && cnt2 == 4)) { n     cout <<  NO ; n     return 0; n   } n   if (full[0] && full[2]) { n     int path[5] = {3, 1, 4, 5, 3}; n     bool ok = true; n     for (int i = 0; i < 4; ++i) { n       if (!(c[path[i]][0] == c[path[i]][1] && n             c[path[i + 1]][2] == c[path[i + 1]][3] && n             c[path[i]][0] == c[path[i + 1]][2])) { n         ok = false; n         break; n       } n     } n     for (int i = 4; i > 0; --i) { n       if (!(c[path[i]][2] == c[path[i]][3] && n             c[path[i - 1]][0] == c[path[i - 1]][1] && n             c[path[i]][2] == c[path[i - 1]][0])) { n         ok = false; n         break; n       } n     } n     if (ok) { n       cout <<  YES ; n       return 0; n     } n     ok = true; n     for (int i = 0; i < 4; ++i) { n       if (!(c[path[i]][2] == c[path[i]][3] && n             c[path[i + 1]][0] == c[path[i + 1]][1] && n             c[path[i]][2] == c[path[i + 1]][0])) { n         ok = false; n         break; n       } n     } n     for (int i = 4; i > 0; --i) { n       if (!(c[path[i]][0] == c[path[i]][1] && n             c[path[i - 1]][2] == c[path[i - 1]][3] && n             c[path[i]][0] == c[path[i - 1]][2])) { n         ok = false; n         break; n       } n     } n     if (ok) { n       cout <<  YES ; n       return 0; n     } n     cout <<  NO ; n   } else if (full[3] && full[4]) { n     int path[5] = {0, 1, 2, 5, 0}; n     swap(c[5][0], c[5][1]); n     swap(c[5][2], c[5][3]); n     bool ok = true; n     for (int i = 0; i < 4; ++i) { n       if (!(c[path[i]][1] == c[path[i]][3] && n             c[path[i + 1]][0] == c[path[i + 1]][2] && n             c[path[i]][1] == c[path[i + 1]][0])) { n         ok = false; n         break; n       } n     } n     for (int i = 4; i > 0; --i) { n       if (!(c[path[i]][0] == c[path[i]][2] && n             c[path[i - 1]][1] == c[path[i - 1]][3] && n             c[path[i]][0] == c[path[i - 1]][1])) { n         ok = false; n         break; n       } n     } n     if (ok) { n       cout <<  YES ; n       return 0; n     } n     ok = true; n     for (int i = 0; i < 4; ++i) { n       if (!(c[path[i]][0] == c[path[i]][2] && n             c[path[i + 1]][1] == c[path[i + 1]][3] && n             c[path[i]][0] == c[path[i + 1]][1])) { n         ok = false; n         break; n       } n     } n     for (int i = 4; i > 0; --i) { n       if (!(c[path[i]][1] == c[path[i]][3] && n             c[path[i - 1]][0] == c[path[i - 1]][2] && n             c[path[i]][1] == c[path[i - 1]][0])) { n         ok = false; n         break; n       } n     } n     if (ok) { n       cout <<  YES ; n       return 0; n     } n     cout <<  NO ; n   } else if (full[1] && full[5]) { n     int path[5] = {0, 4, 2, 3, 0}; n     swap(c[4][1], c[4][2]); n     swap(c[2][0], c[2][3]); n     swap(c[2][1], c[2][2]); n     swap(c[3][0], c[3][3]); n     bool ok = true; n     for (int i = 0; i < 4; ++i) { n       if (i % 2 == 0) { n         if (!(c[path[i]][0] == c[path[i]][1] && n               c[path[i + 1]][0] == c[path[i + 1]][1] && n               c[path[i]][0] == c[path[i + 1]][0])) { n           ok = false; n           break; n         } n       } else { n         if (!(c[path[i]][2] == c[path[i]][3] && n               c[path[i + 1]][2] == c[path[i + 1]][3] && n               c[path[i]][2] == c[path[i + 1]][2])) { n           ok = false; n           break; n         } n       } n     } n     for (int i = 4; i > 0; --i) { n       if (i % 2 == 0) { n         if (!(c[path[i]][2] == c[path[i]][3] && n               c[path[i - 1]][2] == c[path[i - 1]][3] && n               c[path[i]][2] == c[path[i - 1]][2])) { n           ok = false; n           break; n         } n       } else { n         if (!(c[path[i]][0] == c[path[i]][1] && n               c[path[i - 1]][0] == c[path[i - 1]][1] && n               c[path[i]][0] == c[path[i - 1]][0])) { n           ok = false; n           break; n         } n       } n     } n     if (ok) { n       cout <<  YES ; n       return 0; n     } n     ok = true; n     for (int i = 0; i < 4; ++i) { n       if (i % 2 == 1) { n         if (!(c[path[i]][0] == c[path[i]][1] && n               c[path[i + 1]][0] == c[path[i + 1]][1] && n               c[path[i]][0] == c[path[i + 1]][0])) { n           ok = false; n           break; n         } n       } else { n         if (!(c[path[i]][2] == c[path[i]][3] && n               c[path[i + 1]][2] == c[path[i + 1]][3] && n               c[path[i]][2] == c[path[i + 1]][2])) { n           ok = false; n           break; n         } n       } n     } n     for (int i = 4; i > 0; --i) { n       if (i % 2 == 1) { n         if (!(c[path[i]][2] == c[path[i]][3] && n               c[path[i - 1]][2] == c[path[i - 1]][3] && n               c[path[i]][2] == c[path[i - 1]][2])) { n           ok = false; n           break; n         } n       } else { n         if (!(c[path[i]][0] == c[path[i]][1] && n               c[path[i - 1]][0] == c[path[i - 1]][1] && n               c[path[i]][0] == c[path[i - 1]][0])) { n           ok = false; n           break; n         } n       } n     } n     if (ok) { n       cout <<  YES ; n       return 0; n     } n     cout <<  NO ; n   } else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n using ll = long long; n struct TNode { n   int l, r; n   ll v; n } t[N << 2]; n inline int lc(int pos) { return pos << 1; } n inline int rc(int pos) { return pos << 1 | 1; } n int a[N], n, m; n ll sum[N], lazy[N << 2]; n inline void pushup(int pos) { t[pos].v = max(t[lc(pos)].v, t[rc(pos)].v); } n void build(int pos, int l, int r) { n   t[pos].l = l; n   t[pos].r = r; n   if (l == r) { n     t[pos].v = a[l] - sum[l - 1]; n     return; n   } n   int mid = (l + r) >> 1; n   build(lc(pos), l, mid); n   build(rc(pos), mid + 1, r); n   pushup(pos); n } n inline void pushdown(int pos) { n   t[lc(pos)].v += lazy[pos]; n   t[rc(pos)].v += lazy[pos]; n   lazy[lc(pos)] += lazy[pos]; n   lazy[rc(pos)] += lazy[pos]; n   lazy[pos] = 0; n } n void modify(int pos, int l, int r, int v) { n   if (t[pos].l == l && t[pos].r == r) { n     t[pos].v += v; n     lazy[pos] += v; n     return; n   } n   int mid = (t[pos].l + t[pos].r) >> 1; n   pushdown(pos); n   if (r <= mid) n     modify(lc(pos), l, r, v); n   else if (l > mid) n     modify(rc(pos), l, r, v); n   else n     modify(lc(pos), l, mid, v), modify(rc(pos), mid + 1, r, v); n   pushup(pos); n } n int query(int pos) { n   if (t[pos].v < 0) return -1; n   if (t[pos].l == t[pos].r) return t[pos].v ? -1 : t[pos].l; n   pushdown(pos); n   int ret = query(lc(pos)); n   if (ret == -1) ret = query(rc(pos)); n   return ret; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> a[i], sum[i] = sum[i - 1] + a[i]; n   build(1, 1, n); n   int t1, t2; n   for (int i = 1; i <= m; i++) { n     cin >> t1 >> t2; n     t2 = t2 - a[t1]; n     a[t1] += t2; n     modify(1, t1, t1, t2); n     if (t1 < n) modify(1, t1 + 1, n, -t2); n     cout << query(1) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[5010], b[5010], d[110][110]; n double mat[110][110], res[110], flow[110][110], c[5010], ans; n bool used[110]; n void input() { n   memset(flow, 0x7f, sizeof(flow)); n   cin >> n >> m; n   for (int i = 0; i < m; ++i) { n     cin >> a[i] >> b[i] >> c[i]; n     ++d[a[i]][b[i]], ++d[b[i]][a[i]]; n     flow[a[i]][b[i]] = flow[b[i]][a[i]] = min(flow[a[i]][b[i]], c[i]); n   } n } n bool achievable(int x) { n   bool flg = (x == n); n   used[x] = 1; n   for (int i = 1; i <= n; ++i) n     if (!used[i] && d[x][i]) flg |= achievable(i); n   return flg; n } n void check() { n   if (!achievable(1)) { n     for (int i = 0; i <= m; puts( 0.00000 ), ++i) n       ; n     exit(0); n   } n } n void potential() { n   res[1] = 1.0, mat[n][n] = 1.0; n   for (int i = 1; i < n; ++i) { n     if (!used[i]) { n       mat[i][i] = 1.0; n       continue; n     } n     for (int j = 1; j <= n; ++j) n       if (j != i) mat[i][i] += 1.0 * d[i][j], mat[i][j] -= 1.0 * d[i][j]; n   } n } n void Gauss() { n   for (int i = 1; i <= n; ++i) { n     int ind = i; n     for (int j = i + 1; j <= n; ++j) n       if (((mat[j][i]) > 0 ? (mat[j][i]) : (-mat[j][i])) > n           ((mat[ind][i]) > 0 ? (mat[ind][i]) : (-mat[ind][i]))) n         ind = j; n     if (ind != i) n       for (int j = i; j <= n; ++j) swap(mat[ind][j], mat[i][j]); n     swap(res[ind], res[i]); n     for (int j = 1; j <= n; ++j) { n       if (i == j) continue; n       double ratio = -mat[j][i] / mat[i][i]; n       for (int k = i; k <= n; ++k) mat[j][k] += ratio * mat[i][k]; n       res[j] += ratio * res[i]; n     } n   } n   for (int i = 1; i <= n; ++i) n     if (((mat[i][i]) > 0 ? (mat[i][i]) : (-mat[i][i])) > 1e-8) n       res[i] /= mat[i][i]; n } n void output() { n   ans = 1.0e9; n   for (int i = 0; i < m; ++i) n     if ((((res[a[i]] - res[b[i]])) > 0 ? ((res[a[i]] - res[b[i]])) n                                        : (-(res[a[i]] - res[b[i]]))) > 1e-8) n       ans = min(ans, flow[a[i]][b[i]] / (((res[a[i]] - res[b[i]])) > 0 n                                              ? ((res[a[i]] - res[b[i]])) n                                              : (-(res[a[i]] - res[b[i]])))); n   cout << fixed << setprecision(9) << ans << endl; n   for (int i = 0; i < m; ++i) n     cout << fixed << setprecision(9) << (res[a[i]] - res[b[i]]) * ans << endl; n } n int main() { n   input(); n   check(); n   potential(); n   Gauss(); n   output(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, d[40005], v[4005], p[4005], cur, val, use[4005], cnt; n int main() { n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> v[i] >> d[i] >> p[i]; n   for (int i = 1; i <= n; ++i) n     if (!use[i]) { n       cur = v[i]; n       for (int j = i + 1; j <= n; ++j) n         if (!use[j]) { n           if (cur == 0) break; n           p[j] -= cur; n           cur--; n         } n       for (int j = i + 1; j <= n; ++j) n         if (p[j] < 0 && !use[j]) { n           use[j] = 1; n           val = d[j]; n           for (int k = j + 1; k <= n; ++k) n             if (!use[k]) { n               p[k] -= val; n               if (p[k] < 0) { n                 use[k] = 1; n                 val += d[k]; n               } n             } n           break; n         } n     } n   for (int i = 1; i <= n; ++i) n     if (p[i] >= 0) cnt++; n   cout << cnt << endl; n   for (int i = 1; i <= n; ++i) n     if (p[i] >= 0) cout << i <<    ; n } n


#include <bits/stdc++.h> n  n using namespace std; n  n #define inf          1e15 n #define mod          1000000007 n #define N            100001 n #define ll           long long int n #define vint         vector<int> n #define vll          vector<ll> n #define vstr         vector<string> n #define vvint        vector<vector<int> > n #define vvll         vector<vector<ll> > n #define vint_pair    vector<pair<int,int> > n #define input(arr)   for(auto &x:arr) cin>>x; n #define all(x)       (x).begin(), (x).end() n #define rall(x)      (x).rbegin(), (x).rend() n  n int main() { n     ios_base::sync_with_stdio(false); n     cin.tie(0); n     cout.tie(0); n     int t=1; n     cin>>t; n     while(t--){ n         int n; n         cin>>n; n         set<int>num; n         vint arr(n); n         for(int i=0;i<n;++i){ n             cin>>arr[i]; n             num.insert(arr[i]); n         } n         int vis[n+1]; n         memset(vis, 0, sizeof(vis)); n         int l = 1; n         for(int i=0;i<n;++i){ n             if(vis[arr[i]]){ n                 cout<<l<<   ; n                 vis[l] = 1; n             } n             else{ n                 cout<<arr[i]<<   ; n                 vis[arr[i]] = 1; n             } n             while(vis[l]){ n                 ++l; n             } n         } n         cout<<  n ; n         set<int>st; n         memset(vis, 0, sizeof(vis)); n         for(int i=1;i<=n;++i){ n             if(num.find(i)==num.end()){ n                 st.insert(i); n             } n         } n         for(int i=0;i<n;++i){ n             if(!vis[arr[i]]){ n                 cout<<arr[i]<<   ; n                 vis[arr[i]] = 1; n             } n             else{ n                 auto it = st.lower_bound(arr[i]); n                 --it; n                 int tmp = *it; n                 cout<<tmp<<   ; n                 vis[tmp] = 1; n                 st.erase(tmp); n             } n         } n         cout<<  n ; n     } n } n


#include <bits/stdc++.h> n using namespace std; n int sumdigs(int x) { n   int c = 0; n   while (x) { n     c += x % 10; n     x /= 10; n   } n   return c; n } n int main() { n   int a; n   cin >> a; n   if (sumdigs(a) % 4 == 0) n     cout << a; n   else { n     for (int i = a; i < 1000000; i++) { n       if (sumdigs(i) % 4 == 0) { n         cout << i; n         return 0; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e3 + 5; n int n, m, k; n int a[maxn], c[maxn]; n int ans, tmp, cnt, rt, sum, ma; n int flag; n char s[maxn]; n bool ok[maxn]; n vector<int> vc[maxn], vv[maxn]; n template <typename T> n inline void read(T &X) { n   X = 0; n   int w = 0; n   char ch = 0; n   while (!isdigit(ch)) { n     w |= ch ==  - ; n     ch = getchar(); n   } n   while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar(); n   if (w) X = -X; n } n pair<int, int> query(int id) { n   printf( ? %d , vv[id].size()); n   for (int i = 0; i < vv[id].size(); i++) { n     printf(  %d , vv[id][i]); n   } n   puts(  ); n   fflush(stdout); n   int x, y; n   read(x); n   read(y); n   return make_pair(x, y); n } n void dfs(int u, int fa, int dep) { n   ma = max(ma, dep); n   vv[dep].push_back(u); n   for (int i = 0; i < vc[u].size(); i++) { n     int v = vc[u][i]; n     if (v == fa) continue; n     dfs(v, u, dep + 1); n   } n } n int jud(int as) { n   pair<int, int> k = query(as); n   if (k.second > sum) return 1; n   rt = k.first; n   return 0; n } n int main() { n   int T, cas = 1; n   cin >> T; n   while (T--) { n     read(n); n     ans = 0; n     for (int i = (0); i <= (n + 1); i++) { n       vc[i].clear(); n       vv[i].clear(); n     } n     for (int i = (1); i <= (n - 1); i++) { n       int x, y; n       read(x); n       read(y); n       vc[x].push_back(y); n       vc[y].push_back(x); n       vv[n + 1].push_back(i); n     } n     vv[n + 1].push_back(n); n     pair<int, int> tp = query(n + 1); n     rt = tp.first; n     sum = tp.second; n     ma = 0; n     dfs(rt, -1, 0); n     ma = min(ma, sum); n     int l = (sum + 1) / 2, r = ma + 1, tmp; n     while (l < r) { n       int mid = (l + r) >> 1; n       if (jud(mid)) { n         r = mid; n       } else { n         l = mid + 1; n         tmp = mid; n       } n     } n     for (int i = (0); i <= (n); i++) vv[i].clear(); n     dfs(rt, -1, 0); n     tp = query(sum); n     printf( ! %d %d n , rt, tp.first); n     fflush(stdout); n     scanf( %s , s); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void si(T &x) { n   register int c = getchar(); n   x = 0; n   int neg = 0; n   for (; ((c<48 | c> 57) && c !=  - ); c = getchar()) n     ; n   if (c ==  - ) { n     neg = 1; n     c = getchar(); n   } n   for (; c > 47 && c < 58; c = getchar()) { n     x = (x << 1) + (x << 3) + c - 48; n   } n   if (neg) x = -x; n } n long long bigmod(long long p, long long e, long long M) { n   long long ret = 1; n   for (; e > 0; e >>= 1) { n     if (e & 1) ret = (ret * p) % M; n     p = (p * p) % M; n   } n   return ret; n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long modinverse(long long a, long long M) { return bigmod(a, M - 2, M); } n void io() { freopen( /Users/MyMac/Desktop/in.txt ,  r , stdin); } n const int N = 1e6 + 6; n long long nxt[N]; n const long long MOD = 1e9 + 7; n bool vis[N]; n void dfs(int u) { n   if (vis[u]) return; n   vis[u] = 1; n   dfs(nxt[u]); n } n int main() { n   long long p, k; n   cin >> p >> k; n   if (k == 0) { n     long long ans = 1; n     for (int i = 0; i < p - 1; i++) { n       ans *= p; n       ans %= MOD; n     } n     cout << ans << endl; n     return 0; n   } n   if (k == 1) { n     long long ans = 1; n     for (int i = 0; i < p; i++) { n       ans *= p; n       ans %= MOD; n     } n     cout << ans << endl; n     return 0; n   } n   for (long long i = 0; i < p; i++) { n     nxt[i * k % p] = i; n   } n   long long ans = 1; n   for (int i = 1; i < p; i++) { n     if (vis[i] == 0) { n       dfs(i); n       ans *= p; n       ans %= MOD; n     } n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3F3F3F3F; n const long long INF_LL = 0x3F3F3F3F3F3F3F3FLL; n const int N = 4e5 + 50; n bool check(const string &s, const string &t) { n   int match = 0; n   for (int i = 0; i < s.size() && match < t.size(); ++i) { n     if (s[i] == t[match]) ++match; n   } n   return match == t.size(); n } n int main() { n   string s, t; n   cin >> s >> t; n   int ans = 0; n   for (int i = 0; i < int(s.size()); ++i) { n     for (int j = i; j < int(s.size()); ++j) { n       string tmp = s; n       tmp.erase(tmp.begin() + i, tmp.begin() + j + 1); n       if (check(tmp, t)) ans = max(ans, j - i + 1); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4e5 + 10, LOG = 20; n int n, q, F[N], T[N], P[N], D[N], Par[LOG][N]; n pair<int, int> Q[N]; n vector<int> Adj[N], Ap[N], V[N << 2]; n void Pre(int v) { n   for (int i = 1; i < LOG; i++) Par[i][v] = Par[i - 1][Par[i - 1][v]]; n   for (auto X : Adj[v]) n     if (X != Par[0][v]) Par[0][X] = v, D[X] = D[v] + 1, Pre(X); n } n int LCA(int v, int u) { n   if (D[v] < D[u]) return (LCA(u, v)); n   for (int i = 0; i < LOG; i++) n     if ((D[v] - D[u]) & (1 << i)) v = Par[i][v]; n   if (v == u) return (v); n   for (int i = LOG - 1; ~i; i--) n     if (Par[i][v] != Par[i][u]) v = Par[i][v], u = Par[i][u]; n   return (Par[0][v]); n } n int Dist(int v, int u) { return (D[v] + D[u] - 2 * D[LCA(v, u)]); } n int Find(int v) { n   if (P[v] < 0) return (v); n   return (Find(P[v])); n } n void Add(int i, int le, int ri, int id = 1, int l = 0, int r = q + 2) { n   if (ri <= l || r <= le) return; n   if (le <= l && r <= ri) { n     V[id].push_back(i); n     return; n   } n   Add(i, le, ri, id * 2, l, (l + r) >> 1); n   Add(i, le, ri, id * 2 + 1, (l + r) >> 1, r); n } n void DFS(int id, int l = 0, int r = q + 2) { n   vector<pair<int, pair<int, int> > > Op; n   for (auto i : V[id]) n     if (Find(F[i]) != Find(T[i])) { n       int v = Find(F[i]), u = Find(T[i]); n       if (P[v] > P[u]) swap(v, u); n       Op.push_back({u, {v, P[u]}}); n       P[v] += P[u]; n       P[u] = v; n     } n   if (r - l == 1) n     if (Q[l].first > 0) { n       if (Find(Q[l].first) == Find(Q[l].second)) n         printf( %d n , Dist(Q[l].first, Q[l].second)); n       else n         puts( -1 ); n     } n   if (r - l > 1) DFS(id * 2, l, (l + r) >> 1), DFS(id * 2 + 1, (l + r) >> 1, r); n   reverse(Op.begin(), Op.end()); n   for (auto X : Op) n     P[X.first] = X.second.second, P[X.second.first] -= P[X.first]; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) n     scanf( %d%d , &F[i], &T[i]), Ap[i].push_back(0), Adj[F[i]].push_back(T[i]), n         Adj[T[i]].push_back(F[i]); n   scanf( %d , &q); n   int tp, v, u; n   for (int i = 1; i <= q; i++) { n     scanf( %d%d , &tp, &v); n     if (tp < 3) n       Ap[v].push_back(i); n     else n       scanf( %d , &u), Q[i] = {v, u}; n   } n   for (int i = 1; i < n; i++) { n     Ap[i].push_back(q + 1); n     for (int j = 1; j < Ap[i].size(); j += 2) Add(i, Ap[i][j - 1], Ap[i][j]); n   } n   memset(P, -1, sizeof(P)); n   Pre(1); n   DFS(1); n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n template <class T> n void chkmin(T &x, const T &y) { n   if (y < x) { n     x = y; n   } n } n template <class T> n void chkmax(T &x, const T &y) { n   if (y > x) { n     x = y; n   } n } n template <class T> n void sort(vector<T> &a) { n   sort(a.begin(), a.end()); n } n template <class T, class Cmp> n void sort(vector<T> &a, Cmp cmp) { n   sort(a.begin(), a.end(), cmp); n } n template <class T> n void unique(vector<T> &a) { n   a.resize(unique(a.begin(), a.end()) - a.begin()); n } n int readInt() { n   int x = 0; n   char c = getchar(); n   while (!isdigit(c)) { n     c = getchar(); n   } n   while (isdigit(c)) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   return x; n } n string readString() { n   string s; n   char c = getchar(); n   while (isspace(c)) { n     c = getchar(); n   } n   while (!isspace(c)) { n     s += c; n     c = getchar(); n   } n   return s; n } n void solve_single_test(int test_id = 1) { n   int a = readInt(); n   int b = readInt(); n   int n = a + b; n   int ans = 0; n   for (int l = 1, r; l <= n; l = r + 1) { n     int t = n / l; n     r = n / t; n     if ((a + t) / (t + 1) > a / t || (b + t) / (t + 1) > b / t) { n       continue; n     } n     int L = max(l, (a + t) / (t + 1) + (b + t) / (t + 1)); n     int R = min(r, a / t + b / t) + 1; n     if (L < R) { n       ans += R - L; n     } n   } n   printf( %d n , ans); n } n void solve_multiple_tests() { n   int n_tests; n   scanf( %d , &n_tests); n   for (int i = 1; i <= n_tests; ++i) { n     solve_single_test(i); n   } n } n int main() { n   solve_single_test(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, i, k, u, v, cnt; n int head[300005], a[300005]; n long long f[300005], ans; n struct ff { n   int to, nxt; n } e[2 * 300005]; n int fast() { n   int res = 0, fl = 1; n   char c = getchar(); n   while (c < 48 || c > 57) { n     if (c ==  - ) fl = -1; n     c = getchar(); n   } n   while (c >= 48 && c <= 57) res = res * 10 + c - 48, c = getchar(); n   return res * fl; n } n void write(long long x) { n   if (x > 9) write(x / 10); n   putchar(x % 10 + 48); n } n void add(int u, int v) { n   e[++cnt] = (ff){v, head[u]}; n   head[u] = cnt; n } n void dfs(int now, int fa, int fl) { n   f[now] = a[now]; n   for (int i = head[now]; i; i = e[i].nxt) { n     int v = e[i].to; n     if (v == fa) continue; n     dfs(v, now, fl); n     if (f[v] > 0) f[now] += f[v]; n   } n   if (fl) n     ans = max(ans, f[now]); n   else if (ans == f[now]) n     k++, f[now] = -1; n } n int main() { n   n = fast(); n   for (i = 1; i <= n; i++) a[i] = fast(); n   for (i = 1; i < n; i++) { n     u = fast(), v = fast(); n     add(u, v), add(v, u); n   } n   ans = -(long long)1e16; n   dfs(1, 0, 1); n   dfs(1, 0, 0); n   printf( %lld %d n , ans * k, k); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   bool flag = true; n   while (cin >> n >> m) { n     for (int i = 0; i < n; i++) { n       for (int j = 1; j <= m; j++) { n         if (i % 2 && j < m && flag) n           cout <<  . ; n         else if (i % 2 && j == m && flag) n           cout <<  # ; n         else if (i % 2 && j == 1 && !flag) n           cout <<  # ; n         else if (i % 2 && j > 1 && !flag) n           cout <<  . ; n         else n           cout <<  # ; n       } n       if (i % 2) flag = !flag; n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int max_x = 1000, inf = 1000111222; n const int max_bal = 2222; n const int sd = max_bal / 2; n int a; n pair<int, pair<int, int>> par[max_x][max_bal]; n bool dp[max_x][max_bal]; n int get_sum(int x) { n   int sum = 0; n   while (x) { n     sum += x % 10; n     x /= 10; n   } n   return sum; n } n int main() { n   cin >> a; n   queue<pair<int, int>> q; n   dp[0][sd] = 1; n   q.push({0, sd}); n   while (!q.empty()) { n     pair<int, int> p = q.front(); n     q.pop(); n     for (int d = 0; d <= 9; ++d) { n       int y = p.first + a * d; n       int to = y / 10; n       int w = d - a * (y % 10); n       if (p.second + w >= 0 && p.second + w < max_bal && n           !dp[to][p.second + w]) { n         dp[to][p.second + w] = 1; n         par[to][p.second + w] = {d, p}; n         q.push({to, p.second + w}); n       } n     } n   } n   for (int i = 1; i < max_x; ++i) { n     int need = get_sum(i) * a + sd; n     if (need < max_bal && dp[i][need]) { n       string ans; n       int x = i, bal = need; n       while (x != 0 || bal != sd) { n         ans +=  0  + par[x][bal].first; n         if (ans ==  0 ) { n           ans =   ; n         } n         int nx = par[x][bal].second.first; n         int nbal = par[x][bal].second.second; n         x = nx; n         bal = nbal; n       } n       cout << ans << endl; n       return 0; n     } n   } n   cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int INF = 1000000000; n vector<vector<int> > v; n int* dist; n void bfs(int ver, int n) { n   bool vis[n]; n   for (int i = 0; i < n; i++) vis[i] = false; n   vis[ver] = true; n   queue<pair<int, int> > q; n   q.push(make_pair(ver, 0)); n   while (!q.empty()) { n     pair<int, int> p = q.front(); n     q.pop(); n     int a = p.first, b = p.second; n     dist[a] = b; n     for (int i = 0; i < v[a].size(); i++) { n       if (vis[v[a][i]] == false) { n         vis[v[a][i]] = true; n         q.push(make_pair(v[a][i], b + 1)); n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int n, m; n   cin >> n >> m; n   vector<vector<int> > vt(n); n   v = vt; n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     x -= 1; n     y -= 1; n     v[y].push_back(x); n     vt[x].push_back(y); n   } n   int k; n   cin >> k; n   vector<int> path(k); n   for (int i = 0; i < k; i++) { n     int x; n     cin >> x; n     x -= 1; n     path[i] = x; n   } n   dist = new int[n]; n   bfs(path[k - 1], n); n   vector<pair<int, int> > pos(k, make_pair(0, 0)); n   for (int i = 1; i < k; i++) { n     int ver = path[i - 1], pver = path[i]; n     vector<pair<int, int> > t; n     for (int j = 0; j < vt[ver].size(); j++) n       t.push_back(make_pair(dist[vt[ver][j]], vt[ver][j])); n     int md = t[0].first; n     for (int j = 1; j < t.size(); j++) md = min(md, t[j].first); n     for (int j = 0; j < t.size(); j++) { n       if (t[j].first == md and t[j].second == pver) pos[i].first = 1; n       if (t[j].first == md and t[j].second != pver) pos[i].second = 1; n     } n   } n   int mans = 0, Mans = 0; n   for (int i = 1; i < k; i++) { n     if (pos[i].first != 1) mans += 1; n     if (pos[i].second == 1) Mans += 1; n   } n   cout << mans <<     << Mans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int arr[100005]; n int ans[100005][3], alen; n struct node { n   int n, cnt; n   bool friend operator<(node a, node b) { return a.cnt < b.cnt; } n } n1, n2, n3; n priority_queue<node> qu; n int blen = 0; n int main() { n   scanf( %d , &n); n   int i, j; n   for (i = 0; i < n; i++) scanf( %d , arr + i); n   sort(arr, arr + n); n   n1.n = arr[0]; n   n1.cnt = 1; n   for (i = 1; i < n; i++) { n     if (arr[i] == arr[i - 1]) { n       n1.cnt++; n     } else { n       qu.push(n1); n       n1.cnt = 1; n       n1.n = arr[i]; n     } n   } n   qu.push(n1); n   while (qu.size() > 2) { n     n1 = qu.top(); n     qu.pop(); n     n2 = qu.top(); n     qu.pop(); n     n3 = qu.top(); n     qu.pop(); n     ans[alen][0] = n1.n; n     ans[alen][1] = n2.n; n     ans[alen][2] = n3.n; n     sort(ans[alen], ans[alen] + 3); n     alen++; n     n1.cnt--; n     n2.cnt--; n     n3.cnt--; n     if (n1.cnt != 0) qu.push(n1); n     if (n2.cnt != 0) qu.push(n2); n     if (n3.cnt != 0) qu.push(n3); n   } n   printf( %d n , alen); n   for (i = 0; i < alen; i++) { n     for (j = 2; j > 0; j--) { n       printf( %d  , ans[i][j]); n     } n     printf( %d n , ans[i][0]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n = 6; n   int x, f[12] = {0}; n   for (int i = 0; i < n; i++) { n     cin >> x; n     f[x]++; n   } n   bool res = 0; n   for (int i = 0; i < 10; i++) { n     if (f[i] >= 4) { n       res = 1; n       f[i] -= 4; n       for (int j = 0; j < 10; j++) { n         if (f[j] == 2) { n           cout <<  Elephant n ; n           return 0; n         } else if (f[j]) { n           cout <<  Bear n ; n           return 0; n         } n       } n     } n   } n   cout <<  Alien n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m = 0, m1 = 0, l, q[1000], t; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> q[i]; n   } n   cin >> t; n   sort(q, q + n); n   for (int i = 0; i < n; i++) { n     l = 1; n     for (int j = i + 1; j < n; j++) { n       if (q[j] - q[i] <= t) l++; n     } n     if (l > m) m = l; n   } n   cout << m; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void debug_out() { cerr <<   n ; } n template <typename H, typename... T> n void debug_out(H h, T... t) { n   cerr <<     << (h); n   debug_out(t...); n } n int n; n string a, b; n char flip(char ch) { return (char)( 0  + ( 1  - ch)); } n char ch(int i, int cnt) { return (char)( 0  + ((a[i] -  0  + cnt) % 2)); } n void solve() { n   cin >> n >> a >> b; n   vector<int> res; n   int first = 0; n   int last = n - 1; n   int cnt = 0; n   for (int i = n - 1; i >= 0; i--) { n     if (ch(last, cnt) != b[i]) { n       if (ch(first, cnt) == b[i]) { n         res.push_back(1); n         a[first] = flip(a[first]); n       } n       if (i or ch(first, cnt) != b[i]) { n         res.push_back(i + 1); n         cnt++; n         swap(first, last); n       } n     } n     if (cnt % 2) { n       last++; n     } else { n       last--; n     } n   } n   cout << res.size(); n   for (auto i : res) cout <<     << i; n   cout <<   n ; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   if (n < 998) n     cout << n + 2; n   else n     cout << n - 2; n } n


#include <bits/stdc++.h> n using namespace std; n struct Edge { n   int u, v, w; n }; n const int N = 500005; n int n, m, q; n Edge edge[N]; n vector<int> sorted_edge[N]; n vector<pair<int, vector<int>>> query[N]; n bool npos[N]; n int par[N], rnk[N]; n vector<int> rnk_roll, par_roll; n void init() { n   for (int i = 1; i <= n; i++) par[i] = i, rnk[i] = 1; n } n int get(int u, bool roll = false) { n   if (u == par[u]) return u; n   if (roll) return get(par[u], roll); n   return par[u] = get(par[u], roll); n } n bool merge(int u, int v, bool roll = false) { n   u = get(u, roll), v = get(v, roll); n   if (u == v) return false; n   if (rnk[u] < rnk[v]) swap(u, v); n   if (roll) par_roll.push_back(v); n   par[v] = u; n   if (rnk[u] == rnk[v]) { n     rnk[u]++; n     rnk_roll.push_back(u); n   } n   return true; n } n void rollback() { n   for (auto u : rnk_roll) rnk[u]--; n   for (auto u : par_roll) par[u] = u; n   rnk_roll.clear(); n   par_roll.clear(); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d%d , &edge[i].u, &edge[i].v, &edge[i].w); n     sorted_edge[edge[i].w].push_back(i); n   } n   scanf( %d , &q); n   for (int i = 0, k; i < q; i++) { n     scanf( %d , &k); n     vector<int> elt(k); n     for (int i = 0; i < k; i++) scanf( %d , &elt[i]); n     sort(elt.begin(), elt.end(), n          [](int i, int j) { return edge[i].w < edge[j].w; }); n     int it = 0, j; n     do { n       vector<int> ei; n       for (j = it; j < k && edge[elt[j]].w == edge[elt[it]].w; j++) n         ei.push_back(elt[j]); n       query[edge[elt[it]].w].push_back({i, ei}); n       it = j; n     } while (it < k); n   } n   init(); n   for (int i = 1; i < N; i++) { n     for (auto qq : query[i]) { n       int qid = qq.first; n       for (auto e : qq.second) { n         if (!merge(edge[e].u, edge[e].v, true)) { n           npos[qid] = true; n           break; n         } n       } n       rollback(); n     } n     for (auto e : sorted_edge[i]) merge(edge[e].u, edge[e].v); n   } n   for (int i = 0; i < q; i++) { n     printf( %s n , npos[i] ?  NO  :  YES ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long points[1002][2]; n long long circle[1002][3]; n vector<long long> tree[1002]; n bool mark[1002]; n vector<long long> paths[1002]; n long long inside[1002]; n long long dist2(long long a, long long b, long long c, long long d) { n   return (a - c) * (a - c) + (b - d) * (b - d); n } n void dfs(long long v, long long parent) { n   mark[v] = 1; n   if (parent != -1) paths[v] = paths[parent]; n   paths[v].push_back(v); n   for (long long i = 0; i < tree[v].size(); i++) n     if (!mark[tree[v][i]]) dfs(tree[v][i], v); n } n int main() { n   long long n, m, k, a, b, p; n   cin >> n >> m >> k; n   for (long long i = 0; i < n; i++) cin >> points[i][0] >> points[i][1]; n   for (long long i = 0; i < m; i++) n     cin >> circle[i][0] >> circle[i][1] >> circle[i][2]; n   for (long long i = 0; i < m; i++) { n     long long parent = m; n     long long min_radius = 2000000000; n     for (long long j = 0; j < m; j++) { n       if (circle[j][0] <= circle[i][0]) continue; n       if (dist2(circle[i][1], circle[i][2], circle[j][1], circle[j][2]) > n           circle[j][0] * circle[j][0]) n         continue; n       if (min_radius > circle[j][0]) { n         parent = j; n         min_radius = circle[j][0]; n       } n     } n     tree[parent].push_back(i); n   } n   for (long long i = 0; i < n; i++) { n     long long parent = m; n     long long min_radius = 2000000000; n     for (long long j = 0; j < m; j++) { n       if (dist2(points[i][0], points[i][1], circle[j][1], circle[j][2]) > n           circle[j][0] * circle[j][0]) n         continue; n       if (min_radius > circle[j][0]) { n         parent = j; n         min_radius = circle[j][0]; n       } n     } n     inside[i] = parent; n   } n   dfs(m, -1); n   for (long long i = 0; i < k; i++) { n     cin >> a >> b; n     a = inside[a - 1]; n     b = inside[b - 1]; n     p = 0; n     while (p < min(paths[a].size(), paths[b].size()) && n            paths[a][p] == paths[b][p]) n       p++; n     cout << (long long)paths[a].size() + (long long)paths[b].size() - p - p n          << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod(long long x, long long MOD) { n   if (x > MOD) x -= MOD; n   return x; n } n long long _ceil(long long x, long long y) { return x / y + (x % y != 0); } n void wrt() { n   cout <<   n ; n   return; n } n template <typename T1, typename... T2> n void wrt(T1 x, T2... args) { n   ((cout << x <<    ), wrt(args...)); n } n template <typename T> n ostream& operator<<(ostream& os, const vector<T>& v) { n   for (auto& i : v) os << i <<    ; n   return os; n } n template <typename T> n istream& operator>>(istream& in, vector<T>& v) { n   for (auto& e : v) in >> e; n   return in; n } n const long long MOD = 998244353; n const long long N = 1e6 + 5, inf = 1e18 + 2; n long long n, m, x, u, v, k, p, q, i, j; n string s, t; n long long ans; n long long a[N]; n vector<long long> arr(7, 0); n bool f(long long i, long long j, long long k) { n   long long x; n   vector<long long> res; n   res.push_back(arr[i]); n   res.push_back(arr[j]); n   res.push_back(arr[k]); n   res.push_back(arr[i] + arr[j]); n   res.push_back(arr[j] + arr[k]); n   res.push_back(arr[i] + arr[k]); n   res.push_back(arr[i] + arr[j] + arr[k]); n   sort((res).begin(), (res).end()); n   for (long long i = 0; i < (((long long)(res).size())); ++i) { n     if (res[i] != arr[i]) return false; n   } n   return true; n } n void solve() { n   for (long long i = 0; i < (7); ++i) arr[i] = 0; n   cin >> arr; n   sort((arr).begin(), (arr).end()); n   for (long long i = 0; i < (7); ++i) { n     for (long long j = (i + 1); j < (7); ++j) { n       for (long long k = (j + 1); k < (7); ++k) { n         if (f(i, j, k)) { n           wrt(arr[i], arr[j], arr[k]); n           return; n         } n       } n     } n   } n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T INT_LEN(T v) { n   int len = 1; n   while (v /= 10) ++len; n   return len; n } n int t1, t2, x1, x2, t0; n int less_than(double va, double vb) { return va <= vb + 1e-10; } n int binary_found(int lt, int rt, int y2, double &pre) { n   int y1; n   bool flag = false; n   double t; n   while (lt <= rt) { n     y1 = (lt + rt) >> 1; n     t = ((double)t1 * y1 + (double)t2 * y2) / (y1 + y2); n     if (t >= t0) n       lt = -~y1; n     else n       rt = ~-y1; n   } n   t = ((double)t1 * rt + (double)t2 * y2) / (rt + y2); n   if (less_than(fabs(t - t0), fabs(pre - t0))) { n     flag = true; n     pre = t; n   } n   return flag ? rt : -1; n } n void solve() { n   int i, j, mx = 0; n   int y1, y2; n   if (t1 == t2) n     printf( %d %d n , x1, x2); n   else if (t0 == t1) n     printf( %d %d n , x1, 0); n   else if (t0 == t2) n     printf( %d %d n , 0, x2); n   else { n     double pre = t2; n     y2 = 0; n     for (i = 1; i <= x2; i++) { n       j = binary_found(0, x1, i, pre); n       if (~j && i + j > mx) { n         mx = i + j; n         y1 = j; n         y2 = i; n       } n     } n     printf( %d %d n , y1, y2); n   } n } n int main(void) { n   while (5 == scanf( %d%d%d%d%d , &t1, &t2, &x1, &x2, &t0)) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void smin(T &a, U b) { n   if (a > b) a = b; n } n template <typename T, typename U> n inline void smax(T &a, U b) { n   if (a < b) a = b; n } n inline void gn(long long &first) { n   int sg = 1; n   char c; n   while (((c = getchar()) <  0  || c >  9 ) && c !=  - ) n     ; n   c ==  -  ? (sg = -1, first = 0) : (first = c -  0 ); n   while ((c = getchar()) >=  0  && c <=  9 ) first = first * 10 + c -  0 ; n   first *= sg; n } n inline void gn(int &first) { n   long long t; n   gn(t); n   first = t; n } n inline void gn(unsigned long long &first) { n   long long t; n   gn(t); n   first = t; n } n inline void gn(double &first) { n   double t; n   scanf( %lf , &t); n   first = t; n } n inline void gn(long double &first) { n   double t; n   scanf( %lf , &t); n   first = t; n } n int power(int a, int b, int m, int ans = 1) { n   for (; b; b >>= 1, a = 1LL * a * a % m) n     if (b & 1) ans = 1LL * ans * a % m; n   return ans; n } n int main() { n   long long k, b, n, t, m; n   scanf( %I64d%I64d%I64d%I64d , &k, &b, &n, &t); n   if (k == 1) { n     m = n - (t - 1) / b; n     if (m < 0) m = 0; n     cout << m << endl; n     return 0; n   } n   double u = 1.0 * (t - t * k - b) / (1 - k - b); n   double v = log(u) / log(1.0 * k); n   m = n - floor(v); n   if (m < 0) m = 0; n   printf( %I64d n , m); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5 + 5; n const long long MOD = 998244353; n int n, k; n vector<int> a, b; n long long ans[MAXN][3][5]; n bool found[MAXN][3][5]; n long long mul(long long a, long long b) { return (a * b) % MOD; } n long long dp(int pos, int last, int var) { n   if (pos == 1) return (last == 1) ? 1LL : 0LL; n   if (found[pos][last][var]) return ans[pos][last][var]; n   found[pos][last][var] = true; n   ans[pos][last][var] = 0LL; n   for (int i = 0; i < var; ++i) n     if (last == 0 || i != last) { n       long long koef = 1; n       if (i == 0) koef = k - var + 1; n       if (last == 0 && i == 0) koef--; n       koef = max(koef, 0LL); n       ans[pos][last][var] = n           (ans[pos][last][var] + mul(dp(pos - 1, i, var), koef)) % MOD; n     } n   return ans[pos][last][var]; n } n long long calc(const vector<int> &A) { n   vector<pair<int, int> > fixed; n   fixed.push_back(pair<int, int>(0, -1)); n   for (int i = 0; i < A.size(); ++i) { n     if (A[i] != -1) { n       fixed.push_back(pair<int, int>(A[i], i)); n     } n   } n   fixed.push_back(pair<int, int>(0, A.size())); n   long long tmpAns = 1LL; n   for (int i = 0; i < fixed.size() - 1; ++i) { n     int l = fixed[i].second, r = fixed[i + 1].second; n     if (fixed[i].first == 0 && fixed[i + 1].first == 0) { n       tmpAns = mul(tmpAns, k); n       for (int j = l + 2; j < r; ++j) tmpAns = mul(tmpAns, k - 1); n     } else if (fixed[i].first == 0 || fixed[i + 1].first == 0) { n       for (int j = l + 1; j < r; ++j) tmpAns = mul(tmpAns, k - 1); n     } else { n       if (A[l] == A[r]) { n         tmpAns = mul(tmpAns, dp(r - l + 1, 1, 2)); n       } else { n         tmpAns = mul(tmpAns, dp(r - l + 1, 2, 3)); n       } n     } n   } n   return tmpAns; n } n int main() { n   scanf( %d %d , &n, &k); n   for (int i = 0; i < n; ++i) { n     int tmp; n     scanf( %d , &tmp); n     if (i % 2) n       a.push_back(tmp); n     else n       b.push_back(tmp); n   } n   printf( %lld n , mul(calc(a), calc(b))); n } n


#include <bits/stdc++.h> n using namespace std; n void FastIO() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n } n stack<pair<int, int> > stk; n string ip; n int length; n int main() { n   FastIO(); n   cin >> ip; n   length = ip.length(); n   int prev[length + 10], open[length + 10]; n   pair<int, int> ppp; n   memset(open, 0, sizeof(open)); n   if (ip[0] ==  [ ) open[0]++; n   for (int i = 1; i < length; i++) { n     open[i] = open[i - 1]; n     if (ip[i] ==  [ ) open[i]++; n   } n   for (int i = 0; i < length; i++) { n     prev[i] = -1; n     if (ip[i] ==  ( ) n       stk.push(make_pair(1, i)); n     else if (ip[i] ==  [ ) n       stk.push(make_pair(2, i)); n     else if (ip[i] ==  ]  && !stk.empty()) { n       ppp = stk.top(); n       if (ppp.first != 2) { n         stk.push(make_pair(-1, -1)); n       } n       if (ppp.first == 2) { n         stk.pop(); n         prev[i] = ppp.second; n       } n     } else if (ip[i] ==  )  && !stk.empty()) { n       ppp = stk.top(); n       if (ppp.first != 1) { n         stk.push(make_pair(-1, -1)); n       } n       if (ppp.first == 1) { n         stk.pop(); n         prev[i] = ppp.second; n       } n     } n   } n   string anssubsr; n   int ans = 0, t; n   for (int i = 0; i < length; i++) { n     if (prev[i] != -1) { n       t = prev[i]; n       while (prev[t - 1] != -1 && t > 0) { n         t = prev[t - 1]; n         if (t <= 0) break; n       } n       prev[i] = t; n     } n   } n   int at, from; n   for (int i = 0; i < length; i++) { n     if (prev[i] != -1) { n       t = prev[i]; n       prev[i]--; n       if (prev[i] >= 0) open[i] -= open[prev[i]]; n       if (open[i] > ans) { n         ans = open[i]; n         from = t; n         at = i; n       } n     } n   } n   if (ans != 0) { n     cout << ans << endl; n     for (int k = from; k <= at; k++) cout << ip[k]; n     cout << endl; n   } else n     cout << 0 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, i; n   cin >> t; n   while (t--) { n     int flag = 0; n     string s, t, p; n     cin >> s >> t >> p; n     int a[26] = {0}; n     if (s.length() > t.length()) { n       flag = 0; n     } else { n       for (i = 0; i < min(t.length(), s.length()) + 1; i++) { n         if (s[i] != t[i]) { n           for (int j = 0; j < p.length(); j++) { n             if (p[j] == t[i]) { n               string d =   ; n               d = d + p[j]; n               s.insert(i, d); n               p.erase(p.begin() + j); n               flag = 0; n               break; n             } n           } n           if (flag == 1) { n             cout <<  NO n ; n             break; n           } n         } n       } n     } n     if (s == t && flag == 0) n       cout <<  YES n ; n     else if (flag == 0) n       cout <<  NO n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct Maxflow { n   static const int MAXV = 20010; n   long long INF = 1000000000000000ll; n   struct Edge { n     int v, r; n     long long c; n     Edge(int _v, long long _c, int _r) : v(_v), c(_c), r(_r) {} n   }; n   int s, t; n   vector<Edge> G[MAXV * 2]; n   int iter[MAXV * 2], d[MAXV * 2], gap[MAXV * 2], tot; n   void init(int x) { n     tot = x + 2; n     s = x + 1, t = x + 2; n     for (int i = 0; i <= tot; i++) { n       G[i].clear(); n       iter[i] = d[i] = gap[i] = 0; n     } n   } n   void addEdge(int u, int v, long long c) { n     G[u].push_back(Edge(v, c, ((int)(G[v]).size()))); n     G[v].push_back(Edge(u, 0, ((int)(G[u]).size()) - 1)); n   } n   long long dfs(int p, long long flow) { n     if (p == t) return flow; n     for (int &i = iter[p]; i < ((int)(G[p]).size()); i++) { n       Edge &e = G[p][i]; n       if (e.c > 0 && d[p] == d[e.v] + 1) { n         long long f = dfs(e.v, min(flow, e.c)); n         if (f) { n           e.c -= f; n           G[e.v][e.r].c += f; n           return f; n         } n       } n     } n     if ((--gap[d[p]]) == 0) n       d[s] = tot; n     else { n       d[p]++; n       iter[p] = 0; n       ++gap[d[p]]; n     } n     return 0; n   } n   long long solve() { n     long long res = 0; n     gap[0] = tot; n     for (res = 0; d[s] < tot; res += dfs(s, INF)) n       ; n     return res; n   } n } flow; n int n, m; n int main() { n   cin >> n >> m; n   flow.init(n + m + 5); n   for (int i = 1; i <= n; i++) { n     long long f; n     scanf( %lld , &f); n     flow.addEdge(m + i, flow.t, f); n   } n   long long tot = 0; n   for (int i = 1; i <= m; i++) { n     int x, y; n     long long f; n     scanf( %d%d%lld , &x, &y, &f); n     flow.addEdge(flow.s, i, f); n     flow.addEdge(i, m + x, flow.INF); n     flow.addEdge(i, m + y, flow.INF); n     tot += f; n   } n   printf( %lld n , tot - flow.solve()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long M = 1e8; n int main() { n   long long n, i, j; n   cin >> n; n   vector<long long> ara; n   for (i = 0; i < n; i++) { n     cin >> j; n     ara.push_back(j); n   } n   long long pre1[n + 1]; n   long long pre2[n + 1]; n   pre1[0] = 0; n   pre2[0] = 0; n   for (i = 1; i <= n; i++) pre1[i] = pre1[i - 1] + ara[i - 1]; n   sort(ara.begin(), ara.end()); n   for (i = 1; i <= n; i++) pre2[i] = pre2[i - 1] + ara[i - 1]; n   long long t; n   cin >> t; n   while (t--) { n     long long l, h, type, ans = 0; n     cin >> type >> l >> h; n     if (type == 1) { n       ans = pre1[h] - pre1[l - 1]; n     } else n       ans = pre2[h] - pre2[l - 1]; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A, typename B> n string to_string(pair<A, B> p); n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p); n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p); n string to_string(const string& s) { return     + s +    ; } n string to_string(const char* s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n string to_string(vector<bool> v) { n   bool first = true; n   string res =  { ; n   for (int i = 0; i < static_cast<int>(v.size()); i++) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(v[i]); n   } n   res +=  } ; n   return res; n } n template <size_t N> n string to_string(bitset<N> v) { n   string res =   ; n   for (size_t i = 0; i < N; i++) { n     res += static_cast<char>( 0  + v[i]); n   } n   return res; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto& x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ) ; n } n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ,   + to_string(get<3>(p)) +  ) ; n } n void debug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   debug_out(T...); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int h, w; n   cin >> h >> w; n   vector<vector<int>> a(h, vector<int>(w)); n   vector<pair<int, int>> pos(h * w); n   for (int i = 0; i < h; i++) { n     for (int j = 0; j < w; j++) { n       cin >> a[i][j]; n       --a[i][j]; n       pos[a[i][j]] = make_pair(i, j); n     } n   } n   vector<vector<int>> b(h, vector<int>(w, -1)); n   int pi = -1; n   int pj = -1; n   vector<pair<int, int>> cells; n   int ptr = 0; n   for (int val = h * w - 1; val >= 0; val--) { n     for (int i = 0; i < h; i++) 42; n     42; n     auto& at = pos[val]; n     int vi = at.first; n     int vj = at.second; n     bool row_max = true; n     for (int j = 0; j < w; j++) { n       row_max &= (a[vi][j] <= val); n     } n     bool col_max = true; n     for (int i = 0; i < h; i++) { n       col_max &= (a[i][vj] <= val); n     } n     42; n     if (row_max && col_max) { n       b[++pi][++pj] = val; n       for (int j = pj - 1; j >= 0; j--) { n         if (b[pi][j] == -1) { n           cells.emplace_back(pi, j); n         } n       } n       for (int i = pi - 1; i >= 0; i--) { n         if (b[i][pj] == -1) { n           cells.emplace_back(i, pj); n         } n       } n       continue; n     } n     if (row_max) { n       b[++pi][pj] = val; n       for (int j = pj - 1; j >= 0; j--) { n         if (b[pi][j] == -1) { n           cells.emplace_back(pi, j); n         } n       } n       continue; n     } n     if (col_max) { n       b[pi][++pj] = val; n       for (int i = pi - 1; i >= 0; i--) { n         if (b[i][pj] == -1) { n           cells.emplace_back(i, pj); n         } n       } n       continue; n     } n     assert(ptr < (int)cells.size()); n     b[cells[ptr].first][cells[ptr].second] = val; n     ++ptr; n   } n   for (int i = 0; i < h; i++) { n     for (int j = 0; j < w; j++) { n       if (j > 0) { n         cout <<    ; n       } n       cout << b[i][j] + 1; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10, mod = 1e9 + 7; n int k, n, sz, last; n char s[N]; n string ret; n long long cnt[2 * N], dp[2 * N]; n char inp[N]; n struct state { n   int link, len, next[26]; n }; n state st[2 * N]; n bool vis[2 * N]; n void sa_init() { n   st[0].len = 0; n   st[0].link = -1; n   for (int i = 0; i < 26; i++) st[0].next[i] = -1; n   sz = 1; n   last = 0; n } n void sa_extend(char ch) { n   int cur = sz++; n   int c = ch -  a ; n   st[cur].len = st[last].len + 1; n   for (int i = 0; i < 26; i++) st[cur].next[i] = -1; n   int p = last; n   while (p != -1 && st[p].next[c] == -1) { n     st[p].next[c] = cur; n     p = st[p].link; n   } n   if (p == -1) n     st[cur].link = 0; n   else { n     int q = st[p].next[c]; n     if (st[q].len == st[p].len + 1) n       st[cur].link = q; n     else { n       int clone = sz++; n       for (int i = 0; i < 26; i++) st[clone].next[i] = -1; n       st[clone] = st[q]; n       st[clone].len = st[p].len + 1; n       while (p != -1 && st[p].next[c] == q) { n         st[p].next[c] = clone; n         p = st[p].link; n       } n       st[cur].link = st[q].link = clone; n     } n   } n   last = cur; n   cnt[cur] = 1; n } n bool cmp(int x, int y) { return st[x].len > st[y].len; } n void dfs(int u) { n   vis[u] = 1; n   dp[u] = cnt[u]; n   for (int j = 0; j < 26; j++) { n     int v = st[u].next[j]; n     if (v == -1) continue; n     if (!vis[v]) dfs(v); n     dp[u] += dp[v]; n   } n } n void solve(int u) { n   for (int j = 0; j < 26; j++) { n     int v = st[u].next[j]; n     if (v == -1) continue; n     if (dp[v] >= k) { n       k -= cnt[v]; n       ret.push_back(j +  a ); n       if (k > 0) n         solve(v); n       else n         k = 0; n       break; n     } n     k -= dp[v]; n   } n } n int main() { n   scanf( %s%d , &s, &k); n   n = strlen(s); n   sa_init(); n   for (int i = 0; i < n; i++) sa_extend(s[i]); n   vector<int> v(sz); n   iota(v.begin(), v.end(), 0); n   sort(v.begin(), v.end(), cmp); n   for (int i = 0; i < sz; i++) { n     cnt[st[v[i]].link] += cnt[v[i]]; n   } n   dfs(0); n   solve(0); n   if (!k) n     cout << ret <<   n ; n   else n     cout <<  No such line. n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100010], b[100010]; n map<string, bool> mark; n bool word(char a) { n   if (a >=  a  and a <=  z ) return true; n   if (a >=  A  and a <=  Z ) return true; n   return false; n } n int main() { n   int n; n   cin >> n; n   string a; n   cin >> a; n   int ind = 0; n   int inside = 0; n   int ans1 = 0; n   int ans2 = 0; n   while (ind < n) { n     if (a[ind] ==  _ ) { n       ind++; n       continue; n     } n     if (a[ind] ==  ( ) { n       inside++; n       ind++; n       continue; n     } n     if (a[ind] ==  ) ) { n       inside--; n       ind++; n       continue; n     } n     int leng = 0; n     string cur =   ; n     while (word(a[ind]) == true) { n       if (ind == n - 1) { n         cur += a[ind]; n         if (inside > 0) { n           ans2++; n         } n         if (inside == 0) { n           ans1 = max(ans1, int(cur.size())); n         } n         ind++; n         break; n       } n       cur += a[ind]; n       ind++; n     } n     if (ind == n) { n       break; n     } n     if (inside > 0) { n       ans2++; n     } n     if (inside == 0) { n       ans1 = max(ans1, int(cur.size())); n     } n   } n   cout << ans1 <<     << ans2 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18; n const long long MOD = 1e9 + 7; n template <class T> n inline void chmax(T &a, const T &b) { n   if ((a) < (b)) (a) = (b); n } n template <class T> n inline void chmin(T &a, const T &b) { n   if ((a) > (b)) (a) = (b); n } n template <typename T> n using PQ = priority_queue<T, vector<T>, greater<T>>; n struct Fast { n   Fast() { n     std::cin.tie(0); n     ios::sync_with_stdio(false); n   } n } fast; n long long query() { n   long long n; n   cin >> n; n   vector<long long> x(n); n   vector<long long> y(n); n   vector<long long> L(n); n   vector<long long> U(n); n   vector<long long> R(n); n   vector<long long> D(n); n   long long l = -1e5; n   long long r = 1e5; n   long long d = -1e5; n   long long u = 1e5; n   for (long long i = (long long)(0); i < (long long)(n); ++i) { n     cin >> x[i] >> y[i] >> L[i] >> U[i] >> R[i] >> D[i]; n     if (L[i] == 1 && R[i] == 1) { n     } n     if (L[i] == 0 && R[i] == 1) { n       chmax(l, x[i]); n     } n     if (L[i] == 1 && R[i] == 0) { n       chmin(r, x[i]); n     } n     if (L[i] == 0 && R[i] == 0) { n       chmax(l, x[i]); n       chmin(r, x[i]); n     } n     if (D[i] == 1 && U[i] == 1) { n     } n     if (D[i] == 0 && U[i] == 1) { n       chmax(d, y[i]); n     } n     if (D[i] == 1 && U[i] == 0) { n       chmin(u, y[i]); n     } n     if (D[i] == 0 && U[i] == 0) { n       chmax(d, y[i]); n       chmin(u, y[i]); n     } n   } n   42; n   42; n   if (l <= r && d <= u) { n     42; n     printf( 1 %lld %lld n , l, d); n   } else { n     printf( 0 n ); n   } n   return 0; n } n long long solve() { n   long long Q; n   cin >> Q; n   for (long long q = (long long)(0); q < (long long)(Q); ++q) { n     query(); n   } n   return 0; n } n signed main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void smin(T &a, const U &b) { n   if (a > b) a = b; n } n template <typename T, typename U> n inline void smax(T &a, const U &b) { n   if (a < b) a = b; n } n template <class T> n inline void gn(T &first) { n   char c, sg = 0; n   while (c = getchar(), (c >  9  || c <  0 ) && c !=  - ) n     ; n   for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ; n        c = getchar()) n     first = (first << 1) + (first << 3) + c -  0 ; n   if (sg) first = -first; n } n template <class T1, class T2> n inline void gn(T1 &x1, T2 &x2) { n   gn(x1), gn(x2); n } n template <class T1, class T2, class T3> n inline void gn(T1 &x1, T2 &x2, T3 &x3) { n   gn(x1, x2), gn(x3); n } n template <class T> n inline void print(T first) { n   if (first < 0) { n     putchar( - ); n     return print(-first); n   } n   if (first < 10) { n     putchar( 0  + first); n     return; n   } n   print(first / 10); n   putchar(first % 10 +  0 ); n } n template <class T> n inline void println(T first) { n   print(first); n   putchar(  n ); n } n template <class T> n inline void printsp(T first) { n   print(first); n   putchar(   ); n } n template <class T1, class T2> n inline void print(T1 x1, T2 x2) { n   printsp(x1), println(x2); n } n template <class T1, class T2, class T3> n inline void print(T1 x1, T2 x2, T3 x3) { n   printsp(x1), printsp(x2), println(x3); n } n int power(int a, int b, int m, int ans = 1) { n   for (; b; b >>= 1, a = (long long)a * a % m) n     if (b & 1) ans = (long long)ans * a % m; n   return ans; n } n int size(int n) { n   int ans = 0; n   while (n) { n     n /= 7; n     ans++; n   } n   smax(ans, 1); n   return ans; n } n int f[11], p[11]; n int calc(int st, int ed) { n   int ans = 0; n   int nn = 0; n   for (int i = ed; i >= st; i--) { n     ans += p[i] * f[nn]; n     nn++; n   } n   return ans; n } n int main() { n   int n, m; n   gn(n, m); n   int N = size(n - 1), M = size(m - 1); n   if (N + M > 7) return puts( 0 ); n   f[0] = 1; n   for (int i = 1; i < 11; i++) f[i] = f[i - 1] * 7; n   for (int i = 0; i < 7; i++) p[i] = i; n   int ans = 0; n   do { n     int a = calc(0, N - 1), b = calc(N, N + M - 1); n     if (a < n && b < m) ans++; n   } while (next_permutation(p, p + 7)); n   int num = 1; n   for (int i = 2; i <= 7 - N - M; i++) num *= i; n   println(ans / num); n } n


#include <bits/stdc++.h> n using namespace std; n struct operacion { n   string name; n   int x; n   operacion() {} n   operacion(string name, int x) : name(name), x(x) {} n }; n int n; n priority_queue<int, vector<int>, greater<int> > q; n vector<operacion> v; n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int cas = 0; cas < n; cas++) { n     string name; n     cin >> name; n     if (name ==  insert ) { n       int x; n       cin >> x; n       v.push_back(operacion(name, x)); n       q.push(x); n     } else if (name ==  getMin ) { n       int x; n       cin >> x; n       while (int(q.size()) > 0 and q.top() < x) { n         v.push_back(operacion( removeMin , 0)); n         q.pop(); n       } n       if (int(q.size()) == 0 or q.top() != x) { n         v.push_back(operacion( insert , x)); n         q.push(x); n       } n       v.push_back(operacion( getMin , x)); n     } else if (name ==  removeMin ) { n       if (int(q.size()) == 0) { n         v.push_back(operacion( insert , 1)); n         q.push(1); n       } n       v.push_back(operacion( removeMin , 0)); n       q.pop(); n     } n   } n   cout << int(v.size()) << endl; n   for (int i = 0; i < int(v.size()); i++) { n     cout << v[i].name; n     if (v[i].name !=  removeMin ) cout <<     << v[i].x; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 155; n const int INF = 123456789; n struct edge { n   int to, lim, pri, rev; n   edge() : to(0), lim(0), pri(0), rev(0){}; n   edge(int to, int lim, int pri, int rev) n       : to(to), lim(lim), pri(pri), rev(rev){}; n }; n vector<edge> nei[N]; n char ss[105]; n int hv[26]; n int n, sl, s, t, cst, flw; n int d[N], f[N], p[N], v[N]; n queue<int> q; n void add_edge(int x, int y, int l, int p) { n   nei[x].push_back(edge(y, l, p, int(nei[y].size()))); n   nei[y].push_back(edge(x, 0, -p, int(nei[x].size()) - 1)); n } n int spfa() { n   for (int i = s; i <= t; i++) { n     v[i] = 0; n     d[i] = f[i] = INF; n   } n   d[s] = 0, v[s] = 1; n   while (!q.empty()) q.pop(); n   q.push(s); n   while (!q.empty()) { n     int x = q.front(); n     q.pop(); n     v[x] = 0; n     for (int i = 0; i < nei[x].size(); i++) { n       int to = nei[x][i].to; n       int lim = nei[x][i].lim; n       int pri = nei[x][i].pri; n       int rev = nei[x][i].rev; n       if (lim == 0) continue; n       if (d[to] > d[x] + pri) { n         p[to] = rev; n         d[to] = d[x] + pri; n         f[to] = min(f[x], lim); n         if (!v[to]) { n           v[to] = 1; n           q.push(to); n         } n       } n     } n   } n   return d[t] != INF; n } n void mcmf() { n   while (spfa()) { n     flw += f[t]; n     cst += d[t] * f[t]; n     int x = t; n     while (x != s) { n       int r = p[x]; n       int to = nei[x][r].to; n       int rev = nei[x][r].rev; n       nei[x][r].lim += f[t]; n       nei[to][rev].lim -= f[t]; n       x = to; n     } n   } n } n int main() { n   scanf( %s , &ss); n   sl = strlen(ss); n   int Sl = sl; n   for (int i = 0; i < sl; i++) hv[ss[i] -  a ]++; n   scanf( %d , &n); n   s = 1; n   t = n + 28; n   for (int i = 0; i < 26; i++) n     if (hv[i]) { n       add_edge(n + 2 + i, t, hv[i], 0); n       hv[i] = 0; n     } n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %s%d , ss, &x); n     sl = strlen(ss); n     for (int j = 0; j < sl; j++) hv[ss[j] -  a ]++; n     add_edge(s, i + 1, x, 0); n     for (int j = 0; j < 26; j++) n       if (hv[j]) { n         add_edge(i + 1, n + 2 + j, hv[j], i); n         hv[j] = 0; n       } n   } n   mcmf(); n   if (Sl == flw) n     printf( %d n , cst); n   else n     puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1000000000000000; n bool sortCct(pair<long long, long long> a, pair<long long, long long> b) { n   return a.first > b.first; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, i, t, g, c = 0; n   cin >> n; n   vector<long long> ct(n); n   vector<pair<long long, long long> > ctt(n); n   map<long long, long long> fr; n   for (i = 0; i < n; i++) cin >> ct[i]; n   for (i = 0; i < n; i++) { n     cin >> t; n     ctt[i] = pair<long long, long long>(t, ct[i]); n   } n   sort(ctt.begin(), ctt.end(), sortCct); n   fr[INF] = 1; n   for (i = 0; i < n; i++) { n     pair<long long, long long> k = ctt[i]; n     auto it = fr.lower_bound(k.second); n     g = max(k.second, it->second); n     c += (g - k.second) * k.first; n     if (g == it->second and g == it->first) n       fr.erase(it); n     else if (g == it->second) n       it->second++; n     else if (g == it->first) { n       fr[it->first - 1] = it->second; n       fr.erase(it); n     } else { n       fr[g - 1] = it->second; n       it->second = g + 1; n     } n   } n   cout << c <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   char ch[105][105], ss[105][105]; n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> ch[i]; n   } n   for (int i = 0; i < n; i++) { n     string s = ch[i], gu; n     if (i % 2 == 0) { n       for (int j = 0; j < m; j++) { n         if (j % 2 == 0 && ch[i][j] !=  - ) { n           gu +=  B ; n         } else if (ch[i][j] !=  - ) { n           gu +=  W ; n         } else { n           gu +=  - ; n         } n       } n     } else { n       for (int j = 0; j < m; j++) { n         if (j % 2 == 0 && ch[i][j] !=  - ) { n           gu +=  W ; n         } else if (ch[i][j] !=  - ) { n           gu +=  B ; n         } else { n           gu +=  - ; n         } n       } n     } n     cout << gu <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e9 + 7, N1 = 1e7 + 5, N2 = 1e6 + 3, N3 = 1e5 + 3; n long long binpow(long long a, long long b) { n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = res * a; n     a = a * a; n     b >>= 1; n   } n   return res; n } n void solve() { n   long long x, y; n   cin >> x >> y; n   if (y < x * 2) n     cout <<  YES ; n   else n     cout <<  NO ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t1 = 1; n   cin >> t1; n   while (t1--) { n     solve(); n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1 << 12 | 5, Q = 1 << 16 | 5; n int n, q, B, a[N], p[N], cnt, id[Q]; n vector<pair<int, int>> opt; n struct node { n   int n; n   vector<int> pos; n   vector<vector<int>> id; n   node(int _n = 0) { n     n = _n; n     pos.resize(n); n     id.resize(n); n     for (auto &v : id) { n       v.resize(n); n     } n   } n   inline int get(int l, int r) { n     l = lower_bound(pos.begin(), pos.end(), l) - pos.begin(); n     r = upper_bound(pos.begin(), pos.end(), r) - pos.begin() - 1; n     return l <= r ? id[l][r] : 0; n   } n } sqr[N]; n inline int merge(int x, int y) { n   int res; n   if (x && y) { n     opt.push_back({x, y}); n     res = ++cnt; n   } else { n     res = x | y; n   } n   return res; n } n node solve(int l, int r, int s) { n   if (l == r) { n     node res(1); n     res.pos = {p[s + l]}; n     res.id = {{p[s + l]}}; n     return res; n   } n   int mid = l + r >> 1; n   node x = solve(l, mid, s); n   node y = solve(mid + 1, r, s); n   node z(x.n + y.n); n   merge(x.pos.begin(), x.pos.end(), y.pos.begin(), y.pos.end(), z.pos.begin()); n   for (int i = 0; i < z.n; ++i) { n     for (int j = i; j < z.n; ++j) { n       int l = z.pos[i], r = z.pos[j]; n       z.id[i][j] = merge(x.get(l, r), y.get(l, r)); n     } n   } n   return z; n } n int main() { n   scanf( %d%d , &n, &q); n   B = sqrt(q); n   cnt = n; n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     p[a[i]] = i; n   } n   for (int i = 1, j = 1; i <= n; i += B, ++j) { n     sqr[j] = solve(0, min(i + B - 1, n) - i, i); n   } n   for (int t = 1; t <= q; ++t) { n     int l, r; n     scanf( %d%d , &l, &r); n     for (int i = 1, j = 1; i <= n; i += B, ++j) { n       id[t] = merge(id[t], sqr[j].get(l, r)); n     } n   } n   printf( %d n , cnt); n   for (auto x : opt) { n     printf( %d %d n , x.first, x.second); n   } n   for (int t = 1; t <= q; ++t) { n     printf( %d%c , id[t],    n [t == q]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n, o = 0, e = 0; n   cin >> n; n   vector<long long> a(n); n   for (long long i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   for (long long i = 0; i < n; ++i) { n     if (a[i] % 2 != i % 2) { n       if (a[i] % 2 == 0) n         e++; n       else n         o++; n     } n   } n   if (o == e) { n     cout << o <<   n ; n   } else { n     cout << -1 <<   n ; n   } n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1 << 18; n bool a[N]; n vector<int> res; n int x, n; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n >> x; n   res.push_back(0); n   a[0] = true; n   for (int i = 1; i <= int((1 << n) - 1); i++) { n     if (a[i ^ x]) continue; n     res.push_back(i); n     a[i] = true; n   } n   cout << res.size() - 1 <<   n ; n   for (int i = 1; i <= int(res.size() - 1); i++) { n     int t = res[i] ^ res[i - 1]; n     cout << t <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> adj[100005]; n long long sn[100005], an[100005], level[100005]; n long long got = 1; n void dfs(long long u, long long par, long long carry) { n   if (sn[u] != -1 && sn[u] < carry) got = 0; n   level[u] = level[par] + 1; n   if (sn[u] != -1) { n     an[u] = sn[u] - carry; n     carry = sn[u]; n   } else n     an[u] = 0; n   for (auto it : adj[u]) { n     if (it != par) dfs(it, u, carry); n   } n } n void dfs2(long long u, long long par, long long carry) { n   if (level[u] & 1) { n     if (u != 1) { n       an[u] = sn[u] - carry; n       carry = sn[u]; n     } n   } else { n     long long mn = 1e15 + 7; n     for (auto it : adj[u]) n       if (it != par) mn = min(mn, sn[it]); n     if (mn != 1e15 + 7) { n       an[u] = mn - carry; n       carry = mn; n     } else n       an[u] = 0; n   } n   for (auto it : adj[u]) n     if (it != par) dfs2(it, u, carry); n } n int main() { n   long long i, j, k; n   long long n, m; n   cin >> n; n   for (i = 2; i <= n; i++) { n     cin >> m; n     adj[m].push_back(i); n     adj[i].push_back(m); n   } n   for (i = 1; i <= n; i++) cin >> sn[i]; n   dfs(1, 0, sn[1]); n   if (!got) { n     cout << -1 << endl; n     return 0; n   } n   dfs2(1, 0, sn[1]); n   an[1] = sn[1]; n   long long ans = 0; n   for (i = 1; i <= n; i++) ans += an[i]; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class Graph { n   vector<int>* adjList; n   vector<bool> init; n   vector<bool> goal; n   bool* visited; n   int n; n   vector<int> operate; n  n  public: n   Graph(int n) { n     adjList = new vector<int>[n]; n     visited = new bool[n](); n     this->n = n; n   } n   void initialise(bool value) { init.push_back(value); } n   void goals(bool value) { goal.push_back(value); } n   void edge(int u, int v) { n     adjList[u].push_back(v); n     adjList[v].push_back(u); n   } n   void print() { n     for (int j = 0; j < operate.size(); ++j) { n       cout << operate[j] << endl; n     } n   } n   int achieveGoal(int root, int op1, int op2) { n     int total = 0; n     visited[root] = true; n     int operations = op1; n     if (operations & 1 == 1) { n       init[root] = !init[root]; n     } n     if (init[root] != goal[root]) { n       init[root] = !init[root]; n       ++operations; n       total += 1; n       operate.push_back(root + 1); n     } n     for (int i = 0; i < adjList[root].size(); ++i) { n       if (!visited[adjList[root][i]]) n         total += achieveGoal(adjList[root][i], op2, operations); n     } n     return total; n   } n }; n int main() { n   int n, u, v; n   cin >> n; n   Graph g(n); n   for (int i = 1; i < n; ++i) { n     cin >> u >> v; n     g.edge(u - 1, v - 1); n   } n   for (int i = 0; i < n; ++i) { n     cin >> u; n     g.initialise(u); n   } n   for (int i = 0; i < n; ++i) { n     cin >> v; n     g.goals(v); n   } n   cout << g.achieveGoal(0, 0, 0) << endl; n   g.print(); n } n


#include <bits/stdc++.h> n using namespace std; n int countdigits(int n) { n   int i, b = 0; n   while (n) { n     b++; n     n = n / 10; n   } n   return b; n } n int main() { n   int t, i, n, x, cnt = 0; n   cin >> t; n   for (i = 0; i < t; i++) { n     cin >> n; n     cnt = countdigits(n); n     n = n % 10; n     if (cnt == 1) { n       x = (n - 1) * 10 + 1; n     } n     if (cnt == 2) { n       x = (n - 1) * 10 + 3; n     } n     if (cnt == 3) { n       x = (n - 1) * 10 + 6; n     } n     if (cnt == 4) { n       x = (n - 1) * 10 + 10; n     } n     cout << x << endl; n     x = 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void read(T &x) { n   static char _c; n   static bool _f; n   x = 0; n   _f = 0; n   _c = getchar(); n   while (_c <  0  ||  9  < _c) { n     if (_c ==  - ) _f = true; n     _c = getchar(); n   } n   while ( 0  <= _c && _c <=  9 ) { n     x = (x << 1) + (x << 3) + (_c & 15); n     _c = getchar(); n   } n   if (_f) x = -x; n } n template <typename T> n inline void Min(T &x, T y) { n   if (y < x) x = y; n } n template <typename T> n inline void Max(T &x, T y) { n   if (x < y) x = y; n } n const int INF = 0x3f3f3f3f; n const double pi = (double)acos(-1.0); n const double eps = (double)1e-8; n const int e5 = (int)1e5 + 5; n const int MOD = (int)1e9 + 7; n inline int sig(double x) { return x < -eps ? -1 : eps < x; } n long long fp(long long a, long long n, long long mod = MOD) { n   if (n < 0) a = fp(a, mod - 2, mod), n = -n; n   long long res = 1; n   for (; n; n >>= 1, a = a * a % mod) n     if (n & 1) res = res * a % mod; n   return res; n } n struct Mint { n   int x; n   Mint() { x = 0; } n   Mint(int _x) : x(_x) { n     if (x < 0 || x >= MOD) x = (x % MOD + MOD) % MOD; n   } n   Mint(long long _x) : x(_x) { n     if (x < 0 || x >= MOD) x = (x % MOD + MOD) % MOD; n   } n   Mint operator-() const { return Mint(MOD - x); } n   Mint operator+(const Mint &rhs) const { n     return Mint(x + rhs.x >= MOD ? x + rhs.x - MOD : x + rhs.x); n   } n   Mint operator-(const Mint &rhs) const { n     return Mint(x - rhs.x < 0 ? x - rhs.x + MOD : x - rhs.x); n   } n   Mint operator*(const Mint &rhs) const { n     return Mint((long long)x * rhs.x % MOD); n   } n   Mint operator/(const Mint &rhs) const { n     return Mint(x * fp(rhs.x, -1) % MOD); n   } n   Mint &operator+=(const Mint &rhs) { n     x += rhs.x; n     if (x >= MOD) x -= MOD; n     return *this; n   } n   Mint &operator*=(const Mint &rhs) { n     x = ((long long)x * rhs.x) % MOD; n     return *this; n   } n   bool operator==(const Mint &rhs) const { return x == rhs.x; } n   bool operator!=(const Mint &rhs) const { return x != rhs.x; } n   friend ostream &operator<<(ostream &out, const Mint &rhs) { n     return out << rhs.x; n   } n   friend istream &operator>>(istream &in, Mint &rhs) { return in >> rhs.x; } n }; n const int maxn = (int)2e5 + 20; n const int maxm = (int)1e6 + 20; n const int N = 500 + 5; n struct PQ_tree { n   int n, tot; n   int fail; n   vector<int> G[N << 2]; n   int ty[N << 2]; n   int sz[N << 2]; n   int szc[N << 2]; n   bool s[N]; n   inline int getstate(int u) { n     if (szc[u] == 0) return 0; n     if (szc[u] == sz[u]) return 2; n     return 1; n   } n   void addson(int x, int y) { n     if (y) G[x].push_back(y); n   } n   void join(int x, int y) { n     for (auto v : G[y]) G[x].push_back(v); n   } n   int mergeP(vector<int> &vec) { n     if (vec.size() == 0) return 0; n     if (vec.size() == 1) return vec[0]; n     G[++tot] = vec; n     return tot; n   } n   void init(int _n) { n     n = _n; n     tot = n + 1; n     memset(ty, 0, sizeof(ty)); n     for (int i = 1; i <= n; i++) G[n + 1].push_back(i); n     fail = 0; n   } n   void dfs(int u) { n     sz[u] = u <= n; n     szc[u] = u <= n && s[u]; n     for (auto v : G[u]) { n       dfs(v); n       sz[u] += sz[v]; n       szc[u] += szc[v]; n     } n   } n   int check(int u, int t) { n     if (fail) return 0; n     vector<int> vec[3]; n     for (auto v : G[u]) vec[getstate(v)].push_back(v); n     if (vec[1].size() > 2 || (t && vec[1].size() > 1)) return fail = 1, 0; n     if (t == 0 && vec[1].size() == 1 && vec[2].size() == 0) n       return check(vec[1][0], 0); n     if (ty[u] == 0) { n       int p2 = mergeP(vec[2]); n       if (t == 0) { n         G[u] = vec[0]; n         if (vec[1].size() == 0) n           addson(u, p2); n         else { n           int tmp1 = check(vec[1][0], 2); n           addson(tmp1, p2); n           if (vec[1].size() == 2) join(tmp1, check(vec[1][1], 1)); n           addson(u, tmp1); n         } n         return u; n       } else { n         ty[u] = 1; n         G[u].clear(); n         addson(u, p2); n         if (vec[1].size() == 1) join(u, check(vec[1][0], 1)); n         addson(u, mergeP(vec[0])); n         if (t == 2) reverse(G[u].begin(), G[u].end()); n       } n       return u; n     } else { n       if (getstate(G[u].front()) > getstate(G[u].back())) n         reverse(G[u].begin(), G[u].end()); n       int flag = 0; n       vector<int> tG; n       for (auto v : G[u]) { n         int sta = getstate(v); n         if (sta == 0) { n           if (flag == 1) flag = 2; n           tG.push_back(v); n         } else if (sta == 2) { n           if (flag == 0) n             flag = 1; n           else if (flag == 2) n             return fail = 2, 0; n           tG.push_back(v); n         } else { n           int p1; n           if (flag == 0) n             flag = 1, p1 = check(v, 2); n           else if (flag == 1) n             flag = 2, p1 = check(v, 1); n           else n             return fail = 3, 0; n           for (auto x : G[v]) tG.push_back(x); n         } n       } n       if (t && flag == 2) return fail = 4, 0; n       if (t == 1) reverse(tG.begin(), tG.end()); n       G[u] = tG; n       return u; n     } n   } n   void dfs_permutation(int u, vector<int> &per) { n     if (u <= n) per.push_back(u); n     for (auto v : G[u]) dfs_permutation(v, per); n   } n   vector<int> get_permutation() { n     vector<int> res; n     dfs_permutation(n + 1, res); n     return res; n   } n   void restrict(vector<int> res) { n     for (int i = 1; i <= n; i++) s[i] = 0; n     for (auto x : res) s[x] = 1; n     dfs(n + 1); n     check(n + 1, 0); n   } n }; n void work() { n   int n; n   cin >> n; n   PQ_tree *pq = new PQ_tree; n   pq->init(n); n   vector<string> s(n); n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n     vector<int> res; n     for (int j = 0; j < n; j++) n       if (s[i][j] ==  1 ) res.push_back(j + 1); n     pq->restrict(res); n   } n   if (pq->fail) { n     cout <<  NO  << endl; n   } else { n     cout <<  YES  << endl; n     vector<int> perm = pq->get_permutation(); n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < n; j++) cout << s[i][perm[j] - 1]; n       cout << endl; n     } n   } n   delete pq; n } n int main(int argc, char **argv) { n   int tc = 1; n   for (int ca = 1; ca <= tc; ca++) { n     work(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a, b; n int used[100005], d[100005]; n vector<int> g[100005]; n void bfs(int node) { n   memset(used, 0, sizeof(used)); n   memset(d, 0, sizeof(d)); n   queue<int> q; n   q.push(node); n   used[node] = 1; n   while (!q.empty()) { n     int cur = q.front(); n     q.pop(); n     for (int i = 0; i < g[cur].size(); i++) { n       int to = g[cur][i]; n       if (!used[to]) { n         q.push(to); n         d[to] = d[cur] + 1; n         used[to] = 1; n       } n     } n   } n } n int main() { n   scanf( %d , &n); n   scanf( %d , &m); n   while (m--) { n     scanf( %d , &a); n     scanf( %d , &b); n     g[a].push_back(b); n     g[b].push_back(a); n   } n   bfs(1); n   int start = 1, dis = 0; n   for (int i = 2; i <= n; i++) { n     if (dis < d[i]) { n       dis = d[i]; n       start = i; n     } n   } n   bfs(start); n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     ans = max(ans, d[i]); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct targ { n   long long x, r; n   int id, s; n } tar[10004]; n bool _x(targ a, targ b) { return a.x < b.x; } n bool _id(targ a, targ b) { return a.id < b.id; } n int fd(int l, int r, long long x) { n   if (l == r) return l; n   int m = l + r >> 1; n   if (tar[m].x < x) n     return fd(m + 1, r, x); n   else n     return fd(l, m, x); n } n int cnt = 0; n void sl(int now, int p, long long x, long long y) { n   if ((x - tar[p].x) * (x - tar[p].x) + y * y <= tar[p].r * tar[p].r) n     tar[p].s = now, cnt++; n } n int main() { n   int n, m; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %lld%lld , &tar[i].x, &tar[i].r); n     tar[i].id = i; n     tar[i].s = -1; n   } n   sort(tar + 1, tar + n + 1, _x); n   scanf( %d , &m); n   long long x, y; n   for (int i = 1; i <= m; i++) { n     scanf( %lld%lld , &x, &y); n     int p = fd(1, n, x); n     if (tar[p].s == -1) sl(i, p, x, y); n     if (p - 1 && tar[p - 1].s == -1) sl(i, p - 1, x, y); n   } n   sort(tar + 1, tar + n + 1, _id); n   cout << cnt << endl; n   for (int i = 1; i <= n; i++) { n     printf( %d%c , tar[i].s,    n [i == n]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10, mod = 998244353; n int add(int a, int b) { n   a += b; n   if (a >= mod) { n     a -= mod; n   } n   return a; n } n int sub(int a, int b) { n   a -= b; n   if (a < 0) { n     a += mod; n   } n   return a; n } n int mul(long long a, int b) { return (a * b) % mod; } n int pwr(int x, int e) { n   int res = 1; n   while (e) { n     if (e & 1) { n       res = mul(res, x); n     } n     x = mul(x, x); n     e /= 2; n   } n   return res; n } n struct tournament { n   vector<int> p; n   int h = 0, i; n }; n vector<tournament> get_pairs(int a, int l, int r); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int k, a, h; n   cin >> k >> a >> h; n   vector<tournament> v1, v2; n   v1 = get_pairs(a, 1, (1 << (k - 1))); n   v2 = get_pairs(a, (1 << (k - 1)) + 1, (1 << k)); n   for (int tc = 0; tc < 2; ++tc) { n     int sz = v1.size(); n     map<int, int> mp; n     for (int i = 0; i < sz; ++i) { n       mp[sub(h, add(v1[i].h, mul(v1[i].i, a)))] = i; n     } n     for (int i = 0; i < sz; ++i) { n       int val = add(v2[i].h, mul(v2[i].i, mul(a, a))); n       if (mp.find(val) != mp.end()) { n         int ti = mp[val], tj = i; n         int winner = v1[ti].i; n         int second = v2[tj].i; n         if (tc == 1) { n           swap(ti, tj); n         } n         vector<int> ans = v1[ti].p; n         for (auto& pos : v2[tj].p) { n           ans.push_back(pos); n         } n         ans[winner - 1] = 1; n         ans[second - 1] = 2; n         for (auto& pos : ans) { n           cout << pos <<    ; n         } n         cout <<   n ; n         return 0; n       } n     } n     swap(v1, v2); n   } n   cout << -1 <<   n ; n } n vector<tournament> get_pairs(int a, int l, int r) { n   int n = (r - l + 1), tot = (1 << (n - 1)); n   vector<int> ids(n); n   vector<tournament> ans; n   iota(ids.begin(), ids.end(), l); n   for (int ch = 0; ch < tot; ++ch) { n     int mask = ch; n     tournament t; n     t.p.resize(n, 0); n     queue<int> q; n     for (int i = 0; i < n; ++i) { n       q.push(i); n     } n     int cur_rank = n + 1, rank_cnt = n / 2; n     while (q.size() > 1) { n       int p1 = q.front(); n       q.pop(); n       int p2 = q.front(); n       q.pop(); n       if (mask & 1) { n         t.p[p2] = cur_rank; n         t.h = add(t.h, mul(ids[p2], pwr(a, cur_rank))); n         q.push(p1); n       } else { n         t.p[p1] = cur_rank; n         t.h = add(t.h, mul(ids[p1], pwr(a, cur_rank))); n         q.push(p2); n       } n       mask /= 2; n       --rank_cnt; n       if (rank_cnt == 0) { n         cur_rank = ((cur_rank - 1) / 2) + 1; n         rank_cnt = (cur_rank - 1) / 2; n       } n     } n     t.i = ids[q.front()]; n     ans.push_back(t); n   } n   return ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int sz = 1e5 + 10, mod = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, i, j; n   long long k, cost = 0; n   string s; n   cin >> n >> k >> s; n   int last[n + 1][26]; n   long long dp[n][n + 1], f[n + 1]; n   memset(last, -1, sizeof(last)); n   memset(dp, 0, sizeof(dp)); n   memset(f, 0, sizeof(f)); n   for (i = 0; i <= n; i++) { n     for (j = 0; j < i; j++) { n       last[i][s[j] -  a ] = j; n     } n   } n   dp[0][1] = 1; n   for (i = 1; i < n; i++) { n     for (int len = 1; len <= n; len++) { n       if (i + 1 < len) break; n       if (len == 1) { n         dp[i][len] = 1; n         continue; n       } n       dp[i][len] = 0; n       for (j = 0; j < 26; j++) { n         int x = last[i][j]; n         if (x != -1) { n           dp[i][len] += dp[x][len - 1]; n         } n       } n     } n   } n   f[0] = 1; n   for (i = 0; i < 26; i++) { n     if (last[n][i] != -1) { n       for (int len = 1; len <= n; len++) { n         f[len] += dp[last[n][i]][len]; n       } n     } n   } n   bool foo = false; n   for (int len = n; len >= 0; len--) { n     if (k - f[len] <= 0) { n       cost += 1LL * k * (n - len); n       foo = true; n       break; n     } else { n       cost += 1LL * f[len] * (n - len); n       k -= f[len]; n     } n   } n   if (foo) n     cout << cost <<   n ; n   else n     cout <<  -1 n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, a, b; n   cin >> n >> m >> a >> b; n   if (m * a <= b) n     cout << n * a <<   n ; n   else n     cout << (n / m) * b + min((n % m) * a, b) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, d, m, A[101]; n   while (cin >> n >> d) { n     int sum = 0; n     for (int i = 0; i < n; i++) { n       cin >> A[i]; n     } n     cin >> m; n     sort(A, A + n); n     if (m > n) { n       for (int i = 0; i < n; i++) { n         sum += A[i]; n       } n       sum -= ((m - n) * d); n     } else { n       for (int i = 0; i < m; i++) { n         sum += A[i]; n       } n     } n     cout << sum << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   int res[100000]; n   memset(res, -1, sizeof(res)); n   cin >> n; n   vector<pair<pair<long long, long long>, int> > v; n   for (int(i) = 0; (i) < (n); (i)++) { n     long long x, r; n     cin >> x >> r; n     v.push_back(pair<pair<long long, long long>, int>( n         pair<long long, long long>(x, r), i)); n   } n   sort(v.begin(), v.end()); n   int m; n   cin >> m; n   int ret = 0; n   for (int(i) = 0; (i) < (m); (i)++) { n     long long x, y; n     cin >> x >> y; n     int l = 0, r = n - 1; n     while (l <= r) { n       int mid = (l + r) / 2; n       if (x < v[mid].first.first - v[mid].first.second) { n         r = mid - 1; n         continue; n       } n       if (x > v[mid].first.first + v[mid].first.second) { n         l = mid + 1; n         continue; n       } n       if ((x - v[mid].first.first) * (x - v[mid].first.first) + y * y <= n           v[mid].first.second * v[mid].first.second) { n         if (res[v[mid].second] == -1) { n           ret++; n           res[v[mid].second] = i + 1; n         } n       } n       if (mid > 0) { n         if ((x - v[mid - 1].first.first) * (x - v[mid - 1].first.first) + n                 y * y <= n             v[mid - 1].first.second * v[mid - 1].first.second) { n           if (res[v[mid - 1].second] == -1) { n             ret++; n             res[v[mid - 1].second] = i + 1; n           } n         } n       } n       if (mid < n - 1) { n         if ((x - v[mid + 1].first.first) * (x - v[mid + 1].first.first) + n                 y * y <= n             v[mid + 1].first.second * v[mid + 1].first.second) { n           if (res[v[mid + 1].second] == -1) { n             ret++; n             res[v[mid + 1].second] = i + 1; n           } n         } n       } n       break; n     } n   } n   cout << ret << endl; n   for (int(i) = 0; (i) < (n); (i)++) { n     cout << res[i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = 3.141592653589793238463; n int x[1009], y[1009], t[1009]; n double p[1009]; n double dp[1009][1009]; n int n; n double sol(int i, int prev) { n   if (i == n) return 0; n   if (dp[i][prev] == dp[i][prev]) return dp[i][prev]; n   bool can = true; n   if (prev == n) n     can = true; n   else { n     int dt = t[i] - t[prev]; n     int dx = abs(x[i] - x[prev]); n     int dy = abs(y[i] - y[prev]); n     can = (dt >= hypot(dx, dy)); n   } n   if (!can) return dp[i][prev] = sol(i + 1, prev); n   return dp[i][prev] = max(sol(i + 1, prev), p[i] + sol(i + 1, i)); n } n struct target { n   int x, y, t; n   double p; n   bool operator<(const target &other) const { return t < other.t; } n }; n int main() { n   cin.tie(0); n   cin.sync_with_stdio(0); n   cin >> n; n   vector<target> v; n   for (int i = 0; i < (n); i++) { n     int x, y, t; n     cin >> x >> y >> t; n     double p; n     cin >> p; n     v.push_back({x, y, t, p}); n   } n   sort(v.begin(), v.end()); n   for (int i = 0; i < (n); i++) n     x[i] = v[i].x, y[i] = v[i].y, t[i] = v[i].t, p[i] = v[i].p; n   memset((dp), (-1), sizeof(dp)); n   printf( %.10f n , sol(0, n)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 100005; n int cnt[MAX_N]; n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   vector<int> vec(n); n   for (int i = 0; i < (int)(n); ++i) { n     cin >> vec[i]; n   } n   int m1 = -1, m2 = -2; n   int id1 = -1, id2 = -1; n   for (int i = 0; i < (int)(n); ++i) { n     if (vec[i] > m1) { n       cnt[i] -= 1; n       m2 = m1, id2 = id1; n       m1 = vec[i], id1 = i; n     } else if (vec[i] > m2) { n       cnt[id1] += 1; n       m2 = vec[i], id2 = i; n     } n   } n   vector<pair<int, int> > res; n   for (int i = 0; i < (int)(n); ++i) { n     res.push_back(pair<int, int>(cnt[i], -vec[i])); n   } n   sort((res).begin(), (res).end(), greater<pair<int, int> >()); n   cout << -res[0].second <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct data1 { n   long long sum, index, position; n }; n bool comp(data1 a, data1 b) { return a.sum < b.sum; } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(0); n   long long k; n   cin >> k; n   vector<data1> arr; n   long long sz = 0; n   for (long long i = 0; i < k; i++) { n     long long n; n     cin >> n; n     std::vector<long long> v(n); n     for (auto &x : v) cin >> x; n     long long sum = accumulate(v.begin(), v.end(), 0ll); n     for (long long j = 0; j < n; j++) { n       data1 d; n       d.sum = sum - v[j]; n       d.index = i; n       d.position = j; n       arr.push_back(d); n       sz++; n     } n   } n   sort(arr.begin(), arr.end(), &comp); n   bool flag = false; n   for (long long i = 0; i < sz - 1; i++) { n     if (arr[i].sum == arr[i + 1].sum and arr[i].index != arr[i + 1].index) { n       cout <<  YES n ; n       cout << arr[i].index + 1 <<     << arr[i].position + 1 <<   n ; n       cout << arr[i + 1].index + 1 <<     << arr[i + 1].position + 1 <<   n ; n       flag = true; n       break; n     } n   } n   if (flag) return 0; n   cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool f[200000] = {0}; n string ans, s; n multiset<int> g[66000]; n int n, in[66000] = {0}, out[66000] = {0}, kol1 = 0, kol2 = 0, start = 0; n void dfs(int v) { n   while (!g[v].empty()) { n     int tmp = *g[v].begin(); n     g[v].erase(g[v].begin()); n     dfs(tmp); n   } n   ans += char(v % 255); n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> s; n     int v = int(s[0]) * 255 + int(s[1]); n     int u = int(s[1] * 255) + int(s[2]); n     start = v; n     g[v].insert(u); n     out[v]++; n     in[u]++; n   } n   for (int i = 1; i <= 65025; i++) { n     int d = out[i] - in[i]; n     if (d == 1) { n       kol1++; n       start = i; n     } else { n       if (d == -1) n         kol2++; n       else if (d != 0) { n         cout <<  NO ; n         return 0; n       } n     } n     if (kol1 > 1 || kol2 > 1) { n       cout <<  NO ; n       return 0; n     } n   } n   dfs(start); n   ans += char(start / 255); n   reverse(ans.begin(), ans.end()); n   if (ans.length() == n + 2) { n     cout <<  YES  n          <<   n ; n     cout << ans; n   } else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   string s, a =   , b; n   cin >> s; n   unordered_map<string, int> m; n   m[s]++; n   for (int i = s.length() - 1; i >= 0; i--) { n     a += s[i]; n     b = a; n     reverse(b.begin(), b.end()); n     m[b + s.substr(0, i)]++; n   } n   cout << m.size() << endl; n } n int main() { n   { solve(); } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int main() { n   cin >> n; n   int k = 0; n   if (n == 1 || n == 2) n     cout << -1; n   else if (n == 3) n     cout << 210; n   else n     for (int i = 4; i <= n; i++) { n       k++; n       if (k == 7) k = 1; n     } n   if (k == 1) { n     cout << 1; n     for (int i = 2; i <= n - 2; i++) cout << 0; n     cout << 50; n   } else if (k == 2) { n     cout << 1; n     for (int i = 2; i <= n - 2; i++) cout << 0; n     cout << 80; n   } else if (k == 3) { n     cout << 1; n     for (int i = 2; i <= n - 3; i++) cout << 0; n     cout << 170; n   } else if (k == 4) { n     cout << 1; n     for (int i = 2; i <= n - 2; i++) cout << 0; n     cout << 20; n   } else if (k == 5) { n     cout << 1; n     for (int i = 2; i <= n - 3; i++) cout << 0; n     cout << 200; n   } else if (k == 6) { n     cout << 1; n     for (int i = 2; i <= n - 3; i++) cout << 0; n     cout << 110; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 5; n int minn[MAX], maxx[MAX], h[MAX]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> h[i]; n   int block = 1; n   minn[1] = h[1]; n   maxx[1] = h[1]; n   for (int i = 2; i <= n; ++i) { n     if (maxx[block] <= h[i]) { n       block++; n       minn[block] = h[i]; n       maxx[block] = h[i]; n     } else { n       minn[block] = min(minn[block], h[i]); n       maxx[block] = max(maxx[block], h[i]); n       while (block > 1 && maxx[block - 1] > minn[block]) { n         --block; n         minn[block] = min(minn[block], minn[block + 1]); n         maxx[block] = max(maxx[block], maxx[block + 1]); n       } n     } n   } n   cout << block; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005, SQRT = 550, Q = 100005; n struct Query { n   int l, r, id; n   Query() {} n   Query(int l, int r, int id) : l(l), r(r), id(id) {} n   bool operator<(const Query& q) const { n     if (l / SQRT != q.l / SQRT) return l / SQRT < q.l / SQRT; n     return r > q.r; n   } n } queries[Q]; n int arr[N], n, k; n int freq[2000005], answer[Q], mo_distinct, mo_has_special; n deque<int> nums[N]; n map<int, int> freq_ratio[N]; n inline void add_elem(int x) { n   if (++freq[x] == 1) ++mo_distinct; n } n inline void rem_elem(int x) { n   if (freq[x]-- == 1) --mo_distinct; n } n inline void add_freq_ratio(int x, int diff) { n   bool before = freq_ratio[x].size() <= 1; n   ++freq_ratio[x][diff]; n   bool after = freq_ratio[x].size() == 1; n   if (!before && after) ++mo_has_special; n   if (before && !after) --mo_has_special; n } n inline void rem_freq_ratio(int x, int diff) { n   bool before = freq_ratio[x].size() == 1; n   --freq_ratio[x][diff]; n   if (freq_ratio[x][diff] == 0) freq_ratio[x].erase(diff); n   bool after = freq_ratio[x].size() <= 1; n   if (!before && after) ++mo_has_special; n   if (before && !after) --mo_has_special; n } n void add_begin(int idx, int x) { n   add_elem(x); n   if (!nums[x].empty()) { n     int diff = nums[x].front() - idx; n     add_freq_ratio(x, diff); n   } else { n     ++mo_has_special; n   } n   nums[x].push_front(idx); n } n void add_end(int idx, int x) { n   add_elem(x); n   if (!nums[x].empty()) { n     int diff = idx - nums[x].back(); n     add_freq_ratio(x, diff); n   } else { n     ++mo_has_special; n   } n   nums[x].push_back(idx); n } n void rem_begin(int x) { n   rem_elem(x); n   int front = nums[x].front(); n   nums[x].pop_front(); n   if (!nums[x].empty()) { n     int diff = nums[x].front() - front; n     rem_freq_ratio(x, diff); n   } else { n     --mo_has_special; n   } n } n void rem_end(int x) { n   rem_elem(x); n   int back = nums[x].back(); n   nums[x].pop_back(); n   if (!nums[x].empty()) { n     int diff = back - nums[x].back(); n     rem_freq_ratio(x, diff); n   } else { n     --mo_has_special; n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %d , &arr[i]); n   int q; n   scanf( %d , &q); n   for (int i = 0; i < q; ++i) { n     int l, r; n     scanf( %d %d , &l, &r); n     --l; n     --r; n     queries[i] = Query(l, r, i); n   } n   sort(queries, queries + q); n   mo_has_special = mo_distinct = 0; n   int l = 0, r = 0; n   add_end(0, arr[0]); n   for (int i = 0; i < q; ++i) { n     while (r < queries[i].r) { n       ++r; n       add_end(r, arr[r]); n     } n     while (l > queries[i].l) { n       --l; n       add_begin(l, arr[l]); n     } n     while (l < queries[i].l) rem_begin(arr[l++]); n     while (r > queries[i].r) rem_end(arr[r--]); n     if (mo_has_special != 0) { n       answer[queries[i].id] = mo_distinct; n     } else { n       answer[queries[i].id] = mo_distinct + 1; n     } n   } n   for (int i = 0; i < q; ++i) printf( %d n , answer[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string n; n   cin >> n; n   reverse(n.begin(), n.end()); n   int k = n[0] + 10 * n[1]; n   if (k % 4 == 0) { n     cout << 4 << endl; n   } else { n     cout << 0 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   ; n   vector<int> v(n); n   long long x = 0; n   for (int i = 0; i < n; i++) { n     char x; n     cin >> x; n     if ((x -  0 ) % 2 == 0) { n       v.push_back(i + 1); n     } n   } n   for (int i = 0; i < v.size(); i++) { n     x += v[i]; n   } n   cout << x << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 10; n int P[N], ar[N]; n long long c[N]; n vector<int> v[N]; n bool used[N]; n void init() { n   P[1] = 1; n   int i, j; n   for (i = 1; i < N; ++i) n     for (j = (i * 2); j < N; j += i) P[j] -= P[i]; n } n int main() { n   int tt, q, x, n, i, j; n   scanf( %d%d , &n, &q); n   long long ret = 0; n   init(); n   for (i = 1; i <= n; ++i) { n     scanf( %d , &ar[i]); n     x = ar[i]; n     for (j = 1; j * j <= x; ++j) n       if (x % j == 0) { n         v[i].push_back(j); n         if ((x / j) != j) v[i].push_back(x / j); n       } n   } n   for (i = 0; i < q; ++i) { n     scanf( %d , &x); n     for (j = 0; j < v[x].size(); ++j) { n       tt = v[x][j]; n       ret -= c[tt] * (c[tt] - 1) / 2 * P[tt]; n       if (used[x]) n         --c[tt]; n       else n         ++c[tt]; n       ret += c[tt] * (c[tt] - 1) / 2 * P[tt]; n     } n     used[x] ^= true; n     cout << ret << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void debug() {} n int const N = 1e3 + 9, OO = 1e9; n int main() { n   int n, q, type, a; n   char b; n   bool prd = 0; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       scanf(  %c , &b); n       if (j == i && b ==  1 ) prd = !prd; n     } n   } n   scanf( %d , &q); n   for (int i = 0; i < q; ++i) { n     scanf( %d , &type); n     if (type != 3) scanf( %d , &a); n     if (type == 1 || type == 2) n       prd = !prd; n     else n       printf((prd) ?  1  :  0 ); n   } n } n