#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000005; n map<long long, pair<int, int> > mm; n int a[maxn]; n int b[maxn]; n long long s[maxn], s1[maxn]; n int main() { n   int i, j, n; n   cin >> n; n   for (i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     s[i] = s[i - 1] + a[i]; n   } n   for (i = 1; i <= n; i++) { n     scanf( %d , &b[i]); n     s1[i] = s1[i - 1] + b[i]; n   } n   int p = n; n   if (s[n] <= s1[n]) { n     i = n; n     while (i >= 0) { n       if (s[i] < s1[p]) { n         p--; n         continue; n       } n       if (mm.find(s[i] - s1[p]) != mm.end()) { n         pair<int, int> p2 = mm[s[i] - s1[p]]; n         printf( %d n , p2.first - i); n         for (j = i + 1; j <= p2.first; j++) printf( %d  , j); n         printf(  n ); n         printf( %d n , p2.second - p); n         for (j = p + 1; j <= p2.second; j++) printf( %d  , j); n         printf(  n ); n         return 0; n       } n       mm[s[i] - s1[p]] = make_pair(i, p); n       i--; n     } n   } else { n     i = n; n     while (i >= 0) { n       if (s1[i] < s[p]) { n         p--; n         continue; n       } n       if (mm.find(s1[i] - s[p]) != mm.end()) { n         pair<int, int> p2 = mm[s1[i] - s[p]]; n         printf( %d n , p2.second - p); n         for (j = p + 1; j <= p2.second; j++) printf( %d  , j); n         printf(  n ); n         printf( %d n , p2.first - i); n         for (j = i + 1; j <= p2.first; j++) printf( %d  , j); n         printf(  n ); n         return 0; n       } n       mm[s1[i] - s[p]] = make_pair(i, p); n       i--; n     } n   } n   printf( -1 n ); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4e5 + 5; n int n; n int fa[N], r[N]; n int rt[N], ls[N * 30], rs[N * 30], siz[N * 30], tot; n long long sum[N * 30], ans; n void update(int &rt, int l, int r, int p) { n   if (!rt) rt = ++tot; n   if (l == r) { n     siz[rt] = 1, sum[rt] = p; n     return; n   } n   int mid = (l + r) >> 1; n   if (p <= mid) n     update(ls[rt], l, mid, p); n   else n     update(rs[rt], mid + 1, r, p); n   siz[rt] = siz[ls[rt]] + siz[rs[rt]]; n   sum[rt] = sum[ls[rt]] + sum[rs[rt]]; n } n int merge(int x, int y) { n   if (!x || !y) return x | y; n   ans -= sum[ls[x]] * siz[rs[x]] + sum[ls[y]] * siz[rs[y]]; n   ls[x] = merge(ls[x], ls[y]); n   rs[x] = merge(rs[x], rs[y]); n   ans += sum[ls[x]] * siz[rs[x]]; n   siz[x] += siz[y]; n   sum[x] += sum[y]; n   return x; n } n inline int findf(int x) { return x == fa[x] ? x : fa[x] = findf(fa[x]); } n void unify(int x, int y) { n   x = findf(x), y = findf(y); n   fa[y] = x; n   ans -= sum[rt[x]] * x + sum[rt[y]] * y; n   rt[x] = merge(rt[x], rt[y]); n   ans += sum[rt[x]] * x; n   r[x] = r[y]; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= 400000; i++) fa[i] = i, r[i] = i; n   for (int i = 1, a, b; i <= n; i++) { n     scanf( %d%d , &a, &b); n     int p = rt[a] ? r[findf(a)] + 1 : a; n     ans -= 1ll * a * b; n     ans += 1ll * p * b; n     update(rt[p], 1, n, b); n     if (rt[p - 1]) unify(p - 1, p); n     if (rt[p + 1]) unify(p, p + 1); n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, d; n   scanf( %d%d%d%d , &a, &b, &c, &d); n   double r = (1 - 1.0 * a / b) * (1 - 1.0 * c / d); n   r = 1.0 / (1 - r); n   printf( %.10f n , r * a / b); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, p, v, t, no = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> p >> v >> t; n     if (p + v + t >= 2) { n       no++; n     } n   } n   cout << no <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, t, a, b, da, db; n   scanf( %d%d%d%d%d%d , &x, &t, &a, &b, &da, &db); n   if (x == 0 || (x <= a && x > a - da * t && (a - x) % da == 0) || n       (x <= b && x > b - db * t && (b - x) % db == 0)) { n     cout <<  YES  << endl; n     return 0; n   } n   for (int i = 0; i < t; i++) { n     for (int j = 0; j < t; j++) { n       if ((a - j * da) + (b - i * db) == x) { n         printf( YES ); n         return 0; n       } n     } n   } n   printf( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n const int NMAX = 100005; n int n, k; n char s[NMAX]; n int sum[NMAX]; n int get(int l, int r) { n   l = max(1, l); n   r = min(r, n); n   return sum[r] - sum[l - 1]; n } n int main() { n   cin.sync_with_stdio(false); n   cin >> n >> k; n   cin >> (s + 1); n   for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + (s[i] ==  0 ); n   int ans = 1 << 30; n   for (int i = 1; i <= n; i++) n     if (s[i] ==  0 ) { n       int l = 1; n       int r = n; n       int sol = -1; n       while (l <= r) { n         int mi = (l + r) / 2; n         if (get(i - mi, i - 1) + get(i + 1, i + mi) >= k) { n           sol = mi; n           r = mi - 1; n         } else n           l = mi + 1; n       } n       if (sol != -1) ans = min(ans, sol); n     } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   register int x = 0, f = 1; n   register char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) x = x * 10 + (ch ^ 48), ch = getchar(); n   return x * f; n } n const int P = 998244353; n const int N = 1e7 + 5; n long long fsp(long long x, int y = P - 2) { n   long long ans = 1; n   while (y) { n     if (y & 1) ans = ans * x % P; n     x = x * x % P, y >>= 1; n   } n   return ans; n } n long long inv[N]; n int n, m, a[N], b[N], f[N]; n void Add(int &x, int y) { n   x += y; n   if (x < 0) x += P; n   if (x >= P) x -= P; n } n int main() { n   n = read(); n   for (int i = 1; i <= n; i++) a[i] = read(), m += a[i]; n   b[n] = m; n   inv[1] = 1; n   for (int i = 2; i <= m; i++) inv[i] = (P - P / i) * inv[P % i] % P; n   long long im = fsp(m), in = fsp(n - 1); n   for (int i = 1; i < m; i++) { n     long long inv1 = inv[m - i] * m % P * (n - 1) % P; n     long long inv2 = (1 - im * in % P * (m - i) % P * (n - 2) % P + P) % P; n     f[i + 1] = n         inv1 * (inv2 * f[i] % P - i * im % P * (f[i - 1] + 1) % P + P) % P; n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) Add(ans, f[a[i]]); n   for (int i = 1; i <= n; i++) Add(ans, P - f[b[i]]); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char ans[300][300]; n string s; n int cnt[300]; n int bal[300]; n int n, m; n void f(int l, int r, int ll, int rr) { n   if (l + 1 == r) { n     for (int i = 0; i < n / 2 - bal[r]; ++i) { n       ans[n / 2 + i][ll] =  | ; n       ans[n / 2 - i][ll] =  | ; n       ans[n / 2 + i][rr] =  | ; n       ans[n / 2 - i][rr] =  | ; n     } n     ans[bal[l] - 1][ll] =  + ; n     ans[n - bal[l]][ll] =  + ; n     ans[bal[l] - 1][rr] =  + ; n     ans[n - bal[l]][rr] =  + ; n     ans[bal[l] - 1][ll + 1] =  - ; n     ans[n - bal[l]][ll + 1] =  - ; n     ans[bal[l] - 1][rr - 1] =  - ; n     ans[n - bal[l]][rr - 1] =  - ; n     if (ll + 4 != rr) { n       throw 1; n     } n     return; n   } n   int q = l; n   int bl = 0; n   for (q = l; q <= r; ++q) { n     if (s[q] ==  [ ) { n       ++bl; n     } else { n       --bl; n     } n     if (bl == 0) { n       break; n     } n   } n   if (q == r) { n     for (int i = 0; i < n / 2 - bal[r]; ++i) { n       ans[n / 2 + i][ll] =  | ; n       ans[n / 2 - i][ll] =  | ; n       ans[n / 2 + i][rr] =  | ; n       ans[n / 2 - i][rr] =  | ; n     } n     ans[bal[l] - 1][ll] =  + ; n     ans[n - bal[l]][ll] =  + ; n     ans[bal[l] - 1][rr] =  + ; n     ans[n - bal[l]][rr] =  + ; n     ans[bal[l] - 1][ll + 1] =  - ; n     ans[n - bal[l]][ll + 1] =  - ; n     ans[bal[l] - 1][rr - 1] =  - ; n     ans[n - bal[l]][rr - 1] =  - ; n     f(l + 1, r - 1, ll + 1, rr - 1); n     return; n   } n   int qq = ll + q - l + (cnt[q] - cnt[l]) * 3; n   f(l, q, ll, qq); n   f(q + 1, r, qq + 1, rr); n } n int main() { n   memset(ans,    , sizeof(ans)); n   int qwe; n   cin >> qwe; n   cin >> s; n   int bl = 0; n   int mx = 0; n   for (int i = 0; i < s.size(); ++i) { n     if (s[i] ==  [ ) { n       ++bl; n       mx = max(mx, bl); n     } else { n       if (s[i - 1] ==  [ ) { n         cnt[i] = 1; n       } n       --bl; n     } n     bal[i] = bl; n     cnt[i] += i > 0 ? cnt[i - 1] : 0; n   } n   n = mx * 2 + 1; n   m = s.size() + cnt[s.size() - 1] * 3; n   f(0, s.size() - 1, 0, m - 1); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       cout << ans[i][j]; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rnd(time(0)); n bool isPrime(long long a) { n   long long i; n   for (i = 2; i * i <= a; i++) { n     if (a % i == 0) { n       return 0; n     } n   } n   return (a != 1); n } n bool isPalindrom(const string &s, int l, int r) { n   for (int i = 0; i < (r - l + 1) / 2; i++) { n     if (s[l + i] != s[r - i]) { n       return 0; n     } n   } n   return 1; n } n template <class type1> n type1 gcd(type1 a, type1 b) { n   type1 t; n   while (b) { n     t = b; n     b = a % b; n     a = t; n   } n   return a; n } n long long positiveMod(long long v, long long md) { n   v = v % md; n   if (v < 0) { n     v += md; n   } n   return v; n } n class information { n  public: n   vector<pair<long long, long long> > dat; n   vector<long long> *stL, *stR; n   long long iq, n; n   void build() { n     n = dat.size(); n     sort(dat.begin(), dat.end()); n     stL = new vector<long long>[4 * n]; n     stR = new vector<long long>[4 * n]; n     buildSt(1, 0, n - 1); n   } n   void buildSt(long long v, long long l, long long r) { n     if (l == r) { n       stL[v].push_back(dat[l].first - dat[l].second); n       stR[v].push_back(dat[l].first + dat[l].second); n     } else { n       long long mid = (l + r) / 2; n       buildSt(v * 2, l, mid); n       buildSt(v * 2 + 1, mid + 1, r); n       int i1, i2; n       i1 = i2 = 0; n       while (i1 != stL[v * 2].size() || i2 != stL[v * 2 + 1].size()) { n         if (i1 == stL[v * 2].size()) { n           stL[v].push_back(stL[v * 2 + 1][i2]); n           i2++; n         } else if (i2 == stL[v * 2 + 1].size()) { n           stL[v].push_back(stL[v * 2][i1]); n           i1++; n         } else { n           if (stL[v * 2][i1] < stL[v * 2 + 1][i2]) { n             stL[v].push_back(stL[v * 2][i1]); n             i1++; n           } else { n             stL[v].push_back(stL[v * 2 + 1][i2]); n             i2++; n           } n         } n       } n       i1 = i2 = 0; n       while (i1 != stR[v * 2].size() || i2 != stR[v * 2 + 1].size()) { n         if (i1 == stR[v * 2].size()) { n           stR[v].push_back(stR[v * 2 + 1][i2]); n           i2++; n         } else if (i2 == stR[v * 2 + 1].size()) { n           stR[v].push_back(stR[v * 2][i1]); n           i1++; n         } else { n           if (stR[v * 2][i1] > stR[v * 2 + 1][i2]) { n             stR[v].push_back(stR[v * 2][i1]); n             i1++; n           } else { n             stR[v].push_back(stR[v * 2 + 1][i2]); n             i2++; n           } n         } n       } n     } n   } n   long long getSum(long long v, long long l, long long r, long long tl, n                    long long tr, long long val) { n     if (l == tl && tr == r) { n       long long mid, res = 0; n       l = 0; n       r = stL[v].size(); n       while (l != r) { n         mid = (l + r) / 2; n         if (stL[v][mid] > val) { n           r = mid; n         } else { n           l = mid + 1; n         } n       } n       res += stL[v].size() - l; n       l = 0; n       r = stR[v].size(); n       while (l != r) { n         mid = (l + r) / 2; n         if (stR[v][mid] >= val) { n           l = mid + 1; n         } else { n           r = mid; n         } n       } n       res += stR[v].size() - l; n       return res; n     } else { n       long long mid = (l + r) / 2; n       if (tr <= mid) { n         return getSum(v * 2, l, mid, tl, tr, val); n       } else if (tl > mid) { n         return getSum(v * 2 + 1, mid + 1, r, tl, tr, val); n       } else { n         return getSum(v * 2, l, mid, tl, mid, val) + n                getSum(v * 2 + 1, mid + 1, r, mid + 1, tr, val); n       } n     } n   } n   long long getCount(long long x, long long rad) { n     long long l, r, i, j, st, en, mid; n     l = 0; n     r = n; n     while (l != r) { n       mid = (l + r) / 2; n       if (dat[mid].first >= x - rad) { n         r = mid; n       } else { n         l = mid + 1; n       } n     } n     st = l; n     l = -1; n     r = n - 1; n     while (l != r) { n       mid = (l + r + 1) / 2; n       if (dat[mid].first <= x + rad) { n         l = mid; n       } else { n         r = mid - 1; n       } n     } n     en = r; n     if (en < st) { n       return 0; n     } else { n       return (en - st + 1) - getSum(1, 0, n - 1, st, en, x); n     } n   } n }; n vector<information> inf; n vector<pair<long long, pair<long long, long long> > > inp; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n, k, i, j, a, b, c, l, r, res = 0; n   cin >> n >> k; n   for (i = 0; i < n; i++) { n     cin >> a >> b >> c; n     inp.push_back({c, {a, b}}); n   } n   sort(inp.begin(), inp.end()); n   for (i = 0; i < n;) { n     l = i; n     r = i; n     while (r < n) { n       if (inp[r].first == inp[l].first) { n         r++; n       } else { n         break; n       } n     } n     information t; n     t.iq = inp[l].first; n     for (j = l; j < r; j++) { n       t.dat.push_back(inp[j].second); n     } n     inf.push_back(t); n     inf.back().build(); n     i = r; n   } n   for (i = 0; i < inf.size(); i++) { n     a = 0; n     for (auto it : inf[i].dat) { n       a += inf[i].getCount(it.first, it.second); n       for (j = i - 1; j >= 0; j--) { n         if ((inf[i].iq - inf[j].iq) > k) { n           break; n         } else { n           res += inf[j].getCount(it.first, it.second); n         } n       } n     } n     res += (a - inf[i].n) / 2; n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, m, k, t, sum = 0, a, b, p; n   cin >> t; n   while (t--) { n     bool fg = 0; n     p = 0; n     k = 0; n     sum = 0; n     cin >> a >> b; n     string s; n     cin >> s; n     n = s.size(); n     for (long long int i = 0; i <= n - 1; i++) { n       if (s[i] ==  1 ) { n         if (fg == 0) { n           p += a; n           fg = 1; n           k = 0; n         } else if (k > 0) { n           p += min(k * b, a); n           k = 0; n           continue; n         } n       } else n         k++; n     } n     cout << p << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a; n int main() { n   scanf( %d%d , &n, &m); n   if (n == 1) n     printf( 1 n ); n   else { n     if (m > n / 2) n       printf( %d n , m - 1); n     else n       printf( %d n , m + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a, b, res; n int main() { n   cin >> n >> a >> b; n   for (int i = 1; i <= min(a, b); i++) { n     if (a / i + b / i >= n) { n       res = i; n     } n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const string ALICE =  Alice , BOB =  Bob ; n void solve_case() { n   int N, K; n   cin >> N >> K; n   if (K % 3 != 0) { n     cout << (N % 3 != 0 ? ALICE : BOB) <<   n ; n     return; n   } n   N %= K + 1; n   cout << (N % 3 == 0 && N < K ? BOB : ALICE) <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int T; n   cin >> T; n   while (T-- > 0) solve_case(); n } n


#include <bits/stdc++.h> n using namespace std; n int a[105][105], u[105], v[105]; n int main() { n   int n, m, k; n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) scanf( %d , &a[i][j]); n   for (int j = 0; j < m; j++) { n     int cnt[105] = {}; n     for (int i = 0; i < n; i++) n       if (!u[i]) ++cnt[a[i][j]]; n     for (int i = 0; i < n; i++) n       if (!u[i] && a[i][j]) { n         if (v[a[i][j]]) n           u[i] = j + 1; n         else if (cnt[a[i][j]] >= 2) n           u[i] = v[a[i][j]] = j + 1; n       } n   } n   for (int i = 0; i < n; i++) printf( %d n , u[i]); n } n


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


#include <bits/stdc++.h> n using namespace std; n const int P = 1e9 + 7, INF = 0x3f3f3f3f; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n long long qpow(long long a, long long n) { n   long long r = 1 % P; n   for (a %= P; n; a = a * a % P, n >>= 1) n     if (n & 1) r = r * a % P; n   return r; n } n long long inv(long long first) { n   return first <= 1 ? 1 : inv(P % first) * (P - P / first) % P; n } n inline int rd() { n   int first = 0; n   char p = getchar(); n   while (p <  0  || p >  9 ) p = getchar(); n   while (p >=  0  && p <=  9 ) first = first * 10 + p -  0 , p = getchar(); n   return first; n } n const int N = 1e6 + 10; n int n, m, k, a[N], deg1[N], deg2[N]; n int L[N], R[N], vis[N]; n int first[N], second[N], b[N]; n vector<int> g1[N], g2[N]; n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , a + i); n     if (a[i]) n       L[i] = R[i] = a[i]; n     else n       L[i] = 1, R[i] = k; n   } n   for (int i = 1; i <= m; ++i) { n     scanf( %d%d , first + i, second + i); n     g2[first[i]].push_back(second[i]), ++deg2[second[i]]; n     g1[second[i]].push_back(first[i]), ++deg1[first[i]]; n   } n   queue<int> q; n   for (int i = 1; i <= n; ++i) n     if (!deg1[i]) q.push(i); n   int cnt = 0; n   while (q.size()) { n     int u = q.front(); n     q.pop(); n     if (L[u] > R[u]) return puts( -1 ), 0; n     ++cnt; n     for (auto &v : g1[u]) { n       L[v] = max(L[v], L[u] + 1); n       if (!--deg1[v]) q.push(v); n     } n   } n   if (cnt != n) return puts( -1 ), 0; n   for (int i = 1; i <= n; ++i) n     if (!deg2[i]) q.push(i); n   while (q.size()) { n     int u = q.front(); n     q.pop(); n     if (L[u] > R[u]) return puts( -1 ), 0; n     for (auto &v : g2[u]) { n       R[v] = min(R[v], R[u] - 1); n       if (!--deg2[v]) q.push(v); n     } n   } n   for (int i = 1; i <= n; ++i) b[i] = i; n   sort(b + 1, b + 1 + n, [](int a, int b) { return L[a] < L[b]; }); n   int now = 1; n   set<pair<int, int> > s; n   for (int i = 1; i <= k; ++i) { n     while (now <= n && L[b[now]] <= i) { n       s.insert(pair<int, int>(R[b[now]], b[now])); n       ++now; n     } n     int ok = 0; n     while (s.size()) { n       int id = s.begin()->second; n       if (ok && R[id] > i) break; n       ok = 1; n       s.erase(s.begin()); n       a[id] = i; n     } n     if (!ok) return puts( -1 ), 0; n   } n   for (int i = 1; i <= n; ++i) printf( %d  , a[i] ? a[i] : L[i]); n   putchar(10); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const int M = 22; n int n, a[N]; n long long f[N][M][2]; n vector<int> v[N]; n long long ans = 0; n void dfs(int x, int tr) { n   ans += a[x]; n   for (int i = 0; i < M; i++) { n     f[x][i][(a[x] >> i) & 1]++; n   } n   for (auto u : v[x]) n     if (u != tr) { n       dfs(u, x); n       for (int i = 0; i < M; i++) { n         ans += (1 << i) * (f[x][i][0] * f[u][i][1] + f[x][i][1] * f[u][i][0]); n         f[x][i][((a[x] >> i) & 1) ^ 0] += f[u][i][0]; n         f[x][i][((a[x] >> i) & 1) ^ 1] += f[u][i][1]; n       } n     } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (int i = 1; i < n; i++) { n     int x, y; n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   dfs(1, 0); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int DIM = 1005; n bitset<DIM> oki; n int val[DIM], nxt[DIM]; n int main(void) { n   ios ::sync_with_stdio(false); n   cin.tie(); n   cout.tie(); n   int n, m, ans = 0; n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) cin >> val[i], nxt[i] = -1; n   for (int i = 1; i <= m; ++i) { n     int x; n     cin >> x; n     for (int j = 1; j <= n; ++j) n       if (nxt[x] < nxt[j]) ans += val[j]; n     nxt[x] = i; n   } n   cout << ans << endl; n   return 0; n } n


// Author: wlzhouzhuan n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx ) n #include <bits/stdc++.h> n using namespace std; n  n #define ull unsigned long long n   n namespace IO { n   const int SIZE = 1 << 23; n   char ibuf[SIZE], *iS, *iT; n   char obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1; n   bool flush() { n     fwrite(obuf, 1, oS - obuf, stdout); n     oS = obuf; n     return true; n   } n   #define gc() (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++) n   #define pc(x) (*oS++ = x)  n   int read() { n     int x = 0, f = 0; n     char c = gc(); n     while (!isdigit(c)) f |= c ==  - , c = gc(); n     while (isdigit(c)) x = 10 * x + c -  0 , c = gc(); n     return f ? -x : x; n   } n   char qu[55]; int qlen; n   template <class T> void print(T x) { n     if (!x) pc( 0 ); n     if (x < 0) pc( - ), x = -x; n     while (x) qu[++qlen] = x % 10 +  0 , x /= 10; n     while (qlen) pc(qu[qlen--]); n   } n   template <class T> void print(T x, char charset) { n     print(x), pc(charset); n   } n   struct Flusher { n     ~Flusher() { n       flush(); n     } n   } flusher; n } n using namespace IO; n  n const int N = 1005; n const int mod = 998244353; n  n int qpow(int a, int b = mod - 2) { n   int res = 1; n   while (b > 0) { n     if (b & 1) res = 1ll * res * a % mod; n     a = 1ll * a * a % mod; n     b >>= 1; n   } n   return res; n } n  n ull mul(ull a, ull b) { n   return (static_cast<unsigned long long>(a) * b) % mod; n } n int a[N], m, n; n ull fz = 1, sumfz = 0, fm = 1, ans = 0; n int j = 0; n void run(int t) { n   for (; j < t; j++) { n     fz = mul(fz, 2 * n - j); n     sumfz = mul(sumfz, (n - j)) + fz; n     fm = mul(fm, n - j); n   } n } n int main() { n   m = read(); n   for (int i = 1; i <= m; i++) { n     a[i] = read(), n += a[i]; n   } n   sort(a + 1, a + m + 1); n   for (int i = 1; i <= m; i++) { n     run(a[i]); n     ans = (ans + mul(sumfz, qpow(fm))) % mod; n   } n   run(n); n   ans = (ans + mod - mul(sumfz, qpow(fm))) % mod; n   print((mod - ans) % mod,   n ); n   return 0; n }


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   char c[101]; n   int count = 0; n   int sum = 0; n   while (cin.getline(c, 101)) { n     if (c[0] ==  + ) { n       count++; n       continue; n     } n     if (c[0] ==  - ) { n       count--; n       continue; n     } n     int x = 0; n     for (; c[x] !=  : ; x++) { n     } n     sum += (strlen(c) - (x + 1)) * count; n   } n   cout << sum; n } n


#include <bits/stdc++.h> n using namespace std; n void __print(int x) { cerr << x; } n void __print(long x) { cerr << x; } n void __print(long long x) { cerr << x; } n void __print(unsigned x) { cerr << x; } n void __print(unsigned long x) { cerr << x; } n void __print(unsigned long long x) { cerr << x; } n void __print(float x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(char x) { cerr <<      << x <<     ; } n void __print(const char *x) { cerr <<     << x <<    ; } n void __print(const string &x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  , ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &x) { n   int f = 0; n   cerr <<  { ; n   for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n const int SIZE = 2e5 + 5; n const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; n const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; n void fastio() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n } n void solveTest() { n   long long n, m; n   cin >> n >> m; n   --m; n   vector<long long> a(n); n   long long mn = INT_MAX; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     mn = min(mn, a[i]); n   } n   for (int i = 0; i < n; i++) { n     a[i] -= mn; n   } n   long long cnt = (1ll * mn) * n; n   while (a[m] != 0) { n     a[m]--; n     cnt++; n     if (m == 0) n       m = n - 1; n     else n       m--; n   } n   a[m] = cnt; n   for (int i = 0; i < n; i++) { n     cout << a[i] <<    ; n   } n   return; n } n int main() { n   fastio(); n   int t = 1; n   while (t--) { n     solveTest(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k; n int log2(long long x) { n   int ret = 0; n   while (x > 0) { n     x /= 2; n     ret++; n   } n   return ret; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   clock_t ck; n   ck = clock(); n   cin >> n >> k; n   if (k == 1) { n     cout << n <<   n ; n     return 0; n   } n   int exp = log2(n); n   cout << (1ll << exp) - 1 <<   n ; n   ck = clock() - ck; n   cerr <<  It took   << 1.0 * ck / CLOCKS_PER_SEC <<   sec n ; n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n  n int n; n inline int lst(int i){return i==1?n:i-1;} n inline int nxt(int i){return i==n?1:i+1;} n inline int lst(int i,int d){return (((i-d-1)%n+n)%n+1);} n inline int nxt(int i,int d){return ((i+d-1)%n+1);} n  n inline int getval(){ n  fflush(stdout); n  int val; n  scanf( %d ,&val); n  return val; n } n inline int curval(int i){ n  printf( ? %d n n ,i); n  return getval(); n } n inline int lstval(int i){ n  printf( ? %d n n ,lst(i)); n  return getval(); n } n inline int nxtval(int i){ n  printf( ? %d n n ,nxt(i)); n  return getval(); n } n  n int k; n int a[200],b[200]; n  n inline void ansis(int ans) n { n  printf( ! %d n ,ans); n  exit(0); n } n inline void findit(int pos,int val,int len) n { n  if (val>k){ n   int lef=1,rig=len,mid; n   while (lef<=rig){ n    mid=((lef+rig)>>1); n    int curpos=lst(pos,mid); n    int cur=curval(curpos); n    if (cur==k) ansis(curpos); n    if (cur>k) lef=mid+1; else rig=mid-1; n   } n  }else{ n   int lef=1,rig=len,mid; n   while (lef<=rig){ n    mid=((lef+rig)>>1); n    int curpos=nxt(pos,mid); n    int cur=curval(curpos); n    if (cur==k) ansis(curpos); n    if (cur<k) lef=mid+1; else rig=mid-1; n   } n  } n } n int main() n { n  scanf( %d%d ,&n,&k); n  curval(1); n  int pos=1; n  for (int t=1;t<=950;t+=2){ n   int val1=curval(pos); n   if (val1!=k) { n    findit(pos,val1,t); n    return 0; n   } n   int val2=nxtval(pos); n   if (val2!=k) { n    findit(nxt(pos),val2,t+1); n    return 0; n   } n   pos=nxt(pos,t*2+1); n  } n }  n


#include <bits/stdc++.h> n using namespace std; n const int N = 100000 + 77; n int n, q; n long long a[N]; n vector<long long> V, Ps; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %lld , a + i); n   if (n == 1) { n     int q; n     scanf( %d , &q); n     long long x, y; n     while (q--) { n       scanf( %lld %lld , &x, &y); n       x = y - x + 1; n       printf( %lld  , x); n     } n     return 0; n   } n   sort(a + 1, a + 1 + n); n   for (int i = 2; i <= n; ++i) V.push_back(a[i] - a[i - 1]); n   sort(V.begin(), V.end()); n   Ps.push_back(V[0]); n   for (int i = 3; i <= n; ++i) Ps.push_back(Ps.back() + V[i - 2]); n   int sz = V.size(); n   scanf( %d , &q); n   while (q--) { n     long long x, y; n     scanf( %lld %lld , &x, &y); n     x = y - x + 1; n     int id = upper_bound(V.begin(), V.end(), x - 1) - V.begin() - 1; n     long long A = 0; n     if (id == -1) { n       A = n * x; n     } else { n       A = (n - id - 1) * x + Ps[id]; n     } n     printf( %lld  , A); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> prefix_function(string s) { n   int n = (int)s.length(); n   vector<int> pi(n); n   for (int i = 1; i < n; ++i) { n     int j = pi[i - 1]; n     while (j > 0 && s[i] != s[j]) j = pi[j - 1]; n     if (s[i] == s[j]) ++j; n     pi[i] = j; n   } n   return pi; n } n int main() { n   int t, n, x = 0; n   cin >> t >> n; n   string s; n   cin >> s; n   cout << s; n   vector<int> v = prefix_function(s); n   s = s.substr(v[v.size() - 1], s.size() - v[v.size() - 1]); n   for (int i = 1; i < n; i++) { n     cout << s; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b; n   cin >> a >> b; n   long long d = abs(a - b); n   long long p1 = (d + 1) / 2, p2 = d / 2; n   long long s1 = (p1 * (p1 + 1)) / 2, s2 = (p2 * (p2 + 1)) / 2; n   cout << s1 + s2; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q; n int a[200010]; n int last[200010]; n int x = -1, y = -1, z = -1; n stack<int> t; n int main() { n   scanf( %d%d , &n, &q); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     if (a[i] == 0) { n       z = i; n       if (i) a[i] = a[i - 1]; n     } else { n       if (x == -1) x = i; n       if (a[i] == q) y = i; n     } n   } n   if (x == -1) { n     cout <<  YES  << endl; n     for (int i = 0; i < n; i++) cout << q <<    ; n     return 0; n   } n   for (int i = x - 1; i >= 0; i--) a[i] = a[i + 1]; n   if (y == -1) { n     if (z == -1) { n       cout <<  NO  << endl; n       return 0; n     } n     a[z] = q; n   } n   for (int i = 0; i < n; i++) last[a[i]] = i; n   for (int i = 0; i < n; i++) { n     if (!t.empty() && t.top() > a[i]) { n       cout <<  NO  << endl; n       return 0; n     } else if (t.empty() || t.top() < a[i]) n       t.push(a[i]); n     if (last[a[i]] == i) t.pop(); n   } n   cout <<  YES  << endl; n   for (int i = 0; i < n; i++) cout << a[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n char c[99]; n int i, x; n int main() { n   for (;;) { n     scanf( %s , c); n     fflush(stdout); n     ; n     if (*c ==  e ) break; n     for (i = 2; i <= 1 << 30; i <<= 1) { n       printf( ? %d %d n , i, i >> 1); n       fflush(stdout); n       ; n       scanf( %s , c); n       fflush(stdout); n       ; n       if (*c ==  y  || i == 1 << 30) break; n     } n     if (*c ==  x ) { n       puts( ! 1 ); n       fflush(stdout); n       ; n       continue; n     } n     int l = i >> 1, r = i, m; n     for (; l + 1 < r;) { n       m = (l + r) >> 1; n       printf( ? %d %d n , l, m); n       fflush(stdout); n       ; n       scanf( %s , c); n       fflush(stdout); n       ; n       if (*c ==  y ) n         l = m; n       else n         r = m; n     } n     printf( ! %d n , r); n     fflush(stdout); n     ; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 1e5 + 500; n const int mod = 1e9 + 7; n const int inf = 123456789; n using namespace std; n int n, t, a, b, c; n vector<int> v[maxn], ans2[maxn]; n vector<int> ans; n set<int> st[maxn]; n int vis[maxn]; n map<tuple<int, int>, int> mp; n void add(int a, int b, int i) { n   if (mp.find({a, b}) != mp.end()) { n     int x = mp[{a, b}]; n     ans2[x].push_back(i); n     ans2[i].push_back(x); n     mp.erase({a, b}); n   } else { n     mp[{a, b}] = i; n   } n } n void ins(int a, int b) { n   if (st[a].count(b)) { n     st[a].erase(b); n   } else { n     st[a].insert(b); n   } n   if (st[b].count(a)) { n     st[b].erase(a); n   } else { n     st[b].insert(a); n   } n } n void dfs(int x, int fa) { n   vis[x] = 1; n   ans.push_back(x); n   for (int i : v[x]) { n     if (i == fa || vis[i]) continue; n     dfs(i, x); n     if (x == 1) break; n   } n } n void dfs1(int x, int fa) { n   for (int i : ans2[x]) { n     if (i != fa) { n       dfs1(i, x); n     } n   } n   cout << x <<    ; n } n void init(int x) { n   ans.clear(); n   mp.clear(); n   memset(vis, 0, sizeof(vis)); n   for (int i = 1; i <= x; i++) { n     ans2[i].clear(); n     v[i].clear(); n     st[i].clear(); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     cin >> n; n     init(n); n     for (int i = 1; i < n - 1; i++) { n       cin >> a >> b >> c; n       if (a > b) swap(a, b); n       if (a > c) swap(a, c); n       if (b > c) swap(b, c); n       ins(a, b); n       ins(a, c); n       ins(b, c); n       add(a, b, i); n       add(a, c, i); n       add(b, c, i); n     } n     for (int i = 1; i <= n; i++) { n       for (int it : st[i]) { n         v[i].push_back(it); n       } n     } n     dfs(1, 0); n     for (int i : ans) { n       cout << i <<    ; n     } n     cout <<   n ; n     dfs1(1, 0); n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int Tn; n   cin >> Tn; n   while (Tn--) { n     int n, x; n     cin >> n >> x; n     int mx = 0, flag = 0; n     while (n--) { n       int t; n       cin >> t; n       if (t == x) flag = 1; n       mx = max(mx, t); n     } n     if (!flag) n       printf( %d n , max((x - 1) / mx + 1, 2)); n     else n       printf( %d n , 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1010; n long long c[2][2][MAXN][MAXN] = {0}; n int n, m; n long long val; n int lowbit(int x) { return x & -x; } n void add(int a, int b) { n   for (int i = a; i <= n; i += lowbit(i)) n     for (int j = b; j <= n; j += lowbit(j)) c[a & 1][b & 1][i][j] ^= val; n } n long long sum(int a, int b) { n   long long ans = 0; n   for (int i = a; i >= 1; i -= lowbit(i)) n     for (int j = b; j >= 1; j -= lowbit(j)) ans ^= c[a & 1][b & 1][i][j]; n   return ans; n } n int main() { n   scanf( %d%d , &n, &m); n   int x1, x2, y1, y2, op; n   long long ans; n   for (int i = 1; i <= m; i++) { n     scanf( %d , &op); n     if (op == 1) { n       scanf( %d%d%d%d , &x1, &y1, &x2, &y2); n       ans = n           sum(x2, y2) ^ sum(x2, y1 - 1) ^ sum(x1 - 1, y2) ^ sum(x1 - 1, y1 - 1); n       printf( %I64d n , ans); n     } else { n       scanf( %d%d%d%d%I64d , &x1, &y1, &x2, &y2, &val); n       add(x1, y1); n       add(x2 + 1, y1); n       add(x1, y2 + 1); n       add(x2 + 1, y2 + 1); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n constexpr int maxn = 1010; n constexpr int dx[5] = {0, 0, 1, -1}; n constexpr int dy[5] = {1, -1, 0, 0}; n using Node = struct { int x, y, v; }; n char G[maxn][maxn]; n bool vis[maxn][maxn]; n int n, m; n vector<Node> p, ret; n inline bool ok(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; } n void gao(int x, int y) { n   int tot = 1; n   bool flag = true; n   bool yes = false; n   while (flag) { n     flag = false; n     int cnt = 0; n     for (int i = 0; i < 4; i++) { n       int tx = x + dx[i] * tot; n       int ty = y + dy[i] * tot; n       if (ok(tx, ty) && G[tx][ty] ==  * ) cnt++; n     } n     if (cnt != 4) break; n     for (int i = 0; i < 4; i++) { n       int tx = x + dx[i] * tot; n       int ty = y + dy[i] * tot; n       vis[tx][ty] = 1; n     } n     flag = true; n     yes = true; n     tot++; n   } n   if (yes) { n     vis[x][y] = 1; n     ret.push_back({x + 1, y + 1, tot - 1}); n   } n } n signed main() { n   while (~scanf( %d%d , &n, &m)) { n     p.clear(); n     ret.clear(); n     memset(vis, 0, sizeof(vis)); n     for (int i = 0; i < n; i++) scanf( %s , G[i]); n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         if (G[i][j] ==  * ) p.push_back({i, j, 0}); n       } n     } n     if (p.size() == 0) { n       printf( 0 n ); n       continue; n     } n     for (auto t : p) gao(t.x, t.y); n     if (ret.size() == 0) { n       printf( -1 n ); n       continue; n     } n     bool flag = 0; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         if (!vis[i][j] && G[i][j] ==  * ) { n           flag = 1; n         } n       } n     } n     if (flag) { n       printf( -1 n ); n       continue; n     } n     printf( %d n , ret.size()); n     for (auto t : ret) { n       printf( %d %d %d n , t.x, t.y, t.v); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[1003][1003][11][2]; n string s, t; n int n, m; n int k; n int rec(int i, int j, int how_k, int op) { n   if (how_k > k) return -1e9; n   if (i >= n || j >= m) { n     if (how_k == k && !op) return 0; n     return -1e9; n   } n   if (s[i] != t[j] && op) return -1e9; n   int &ret = dp[i][j][how_k][op]; n   if (ret != -1) return ret; n   ret = -1e9; n   if (s[i] == t[j]) { n     if (op) { n       ret = max(ret, rec(i + 1, j + 1, how_k + 1, 0) + 1); n       ret = max(ret, rec(i + 1, j + 1, how_k, op) + 1); n     } else { n       ret = max(ret, rec(i + 1, j + 1, how_k + 1, op) + 1); n       ret = max(ret, rec(i + 1, j + 1, how_k, 1) + 1); n     } n   } n   ret = max(ret, rec(i + 1, j, how_k + (op == 1), 0)); n   ret = max(ret, rec(i, j + 1, how_k + (op == 1), 0)); n   return ret; n } n int main() { n   cin >> n >> m >> k; n   memset(dp, -1, sizeof dp); n   cin >> s >> t; n   cout << rec(0, 0, 0, 0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200500; n int mapp[maxn], num[maxn], ans[maxn]; n int main() { n   int n, a, b, k; n   while (cin >> n >> a >> b >> k) { n     memset(ans, 0, sizeof(ans)); n     char c; n     for (int i = 0; i < n; i++) { n       cin >> c; n       mapp[i] = c -  0 ; n     } n     int len = 0, pos = 0, tk = a; n     int sum, sum2 = 0; n     for (int i = 0; i < n; i++) { n       if (0 == mapp[i]) len++; n       if (1 == mapp[i] || i == n - 1) { n         num[pos++] = len; n         int kk = len / b; n         if (kk <= tk) n           tk -= kk; n         else { n           if (i == n - 1 && !mapp[i]) i++; n           for (int j = tk + 1; j <= kk; j++) { n             ans[i - len - 1 + j * b] = 1; n             sum2++; n           } n           tk = 0; n         } n         len = 0; n       } n     } n     sum = 1; n     len = 0; n     for (int i = 0; i < n; i++) { n       if (0 == mapp[i]) len++; n       if ((1 == mapp[i] || i == n - 1) && sum > 0) { n         if (i == n - 1 && !mapp[i]) i++; n         int kk = len / b; n         for (int j = 1; j <= kk; j++) { n           sum--; n           if (!ans[i - len - 1 + j * b]) sum2++; n           ans[i - len - 1 + j * b] = 1; n           if (sum <= 0) break; n         } n         len = 0; n       } n     } n     cout << sum2 << endl; n     int flag = 0; n     for (int i = 0; i < n; i++) { n       if (!ans[i]) continue; n       if (!flag) n         flag = 1; n       else n         cout <<    ; n       cout << i + 1; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, n1, n2, a[111111], sum1 = 0, sum2 = 0; n int main() { n   cin >> n >> n1 >> n2; n   for (int i = 0; i < n; i++) cin >> a[i]; n   if (n1 > n2) swap(n1, n2); n   sort(a, a + n); n   reverse(a, a + n); n   int i = 0; n   int m1 = n1; n   int m2 = n2; n   while (m1 != 0) { n     sum1 += a[i]; n     i++; n     m1--; n   } n   while (m2 != 0) { n     sum2 += a[i]; n     i++; n     m2--; n   } n   cout << fixed << setprecision(7) << (sum1 * 1.0 / n1) + (sum2 * 1.0 / n2); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 110000; n int s1, s2; n int n, a[N]; n int mi[17][N * 2], ma[17][N * 2]; n int BB[N * 2]; n int getmi(int l, int r) { n   return min(mi[BB[r - l + 1]][l], n              mi[BB[r - l + 1]][r - (1 << BB[r - l + 1]) + 1]); n } n int getma(int l, int r) { n   return max(ma[BB[r - l + 1]][l], n              ma[BB[r - l + 1]][r - (1 << BB[r - l + 1]) + 1]); n } n void init() { n   for (int j = (int)0; j <= (int)16; j++) n     for (int i = (int)(1 << j); i <= (int)(1 << (j + 1)) - 1; i++) BB[i] = j; n   for (int i = (int)1; i <= (int)n; i++) { n     mi[0][i] = ma[0][i] = a[i]; n   } n   for (int j = (int)1; j <= (int)16; j++) n     for (int i = (int)1; i <= (int)n - (1 << j) + 1; i++) { n       mi[j][i] = min(mi[j - 1][i], mi[j - 1][i + (1 << (j - 1))]); n       ma[j][i] = max(ma[j - 1][i], ma[j - 1][i + (1 << (j - 1))]); n     } n } n bool f[N], g[N]; n bool check(int S) { n   for (int i = (int)0; i <= (int)n; i++) f[i] = g[i] = 0; n   f[0] = g[0] = 1; n   int maf, mag; n   maf = mag = 0; n   for (int i = (int)0; i <= (int)n - 1; i++) { n     bool f1 = (i <= maf); n     bool f2 = (i <= mag); n     if (f1) { n       int now = a[i]; n       if (i == 0) now = s1; n       int l = i + 1; n       int r = n; n       int rt = 0; n       while (l < r) { n         int mid = (l + r) >> 1; n         if ((abs(getma(i + 1, mid) - now) > S) || n             (abs(getmi(i + 1, mid) - now) > S)) n           r = mid; n         else n           l = mid + 1, rt = mid; n       } n       if (!(abs((getma(i + 1, l) - now) > S) || n             (abs(getmi(i + 1, l) - now) > S))) n         rt = l; n       mag = max(mag, rt); n     } n     if (f2) { n       int now = a[i]; n       if (i == 0) now = s2; n       int l = i + 1; n       int r = n; n       int rt = 0; n       while (l < r) { n         int mid = (l + r) >> 1; n         if ((abs(getma(i + 1, mid) - now) > S) || n             (abs(getmi(i + 1, mid) - now) > S)) n           r = mid; n         else n           l = mid + 1, rt = mid; n       } n       if (!((abs(getma(i + 1, l) - now) > S) || n             (abs(getmi(i + 1, l) - now) > S))) n         rt = l; n       maf = max(maf, rt); n     } n   } n   return (maf == n) || (mag == n); n } n int main() { n   scanf( %d%d%d , &n, &s1, &s2); n   for (int i = (int)1; i <= (int)n; i++) scanf( %d , &a[i]); n   init(); n   int L = abs(s1 - s2); n   int R = 1000000000; n   int ans = 0; n   while (L < R) { n     int mid = (L + R) >> 1; n     if (check(mid)) n       ans = mid, R = mid; n     else n       L = mid + 1; n   } n   if (check(L)) ans = L; n   cout << ans << endl; n   return 0; n } n


#pragma GCC diagnostic ignored  -Wunused-parameter  n #define _USE_MATH_DEFINES n #include <bits/stdc++.h> n  n using namespace std; n  n #define INP  solve  n #define OUT  solve  n  n const long long INF_LL = 8e18; n const int INF = 1e9 + 100; n const int MOD = 1e9 + 7; n //const int Base = 311;9 n const long double EPS = 1e-10; n const int BLOCK = 1000; n const int dx[4] = {0, 0, 1, -1}; n const int dy[4] = {1, -1, 0, 0}; n mt19937 rnd(chrono::system_clock::now().time_since_epoch().count()); n  n void open_file() { n     #ifdef THEMIS n         freopen(INP  .inp , r ,stdin); n         freopen(OUT  .out , w ,stdout); n     #endif // THEMIS n } n  n const int maxN = 3e5 + 100; n  n void sol(){ n     int n; n     cin >> n; n     vector<vector<int> > s(n); n     map<int, int> id; n     int M = 0; n     int N = 0; n     for (int i = 0; i < n; i++) { n         int k; n         cin >> k; n         M += k; n         s[i].resize(k); n         for (int j = 0; j < k; j++) { n             int x; n             cin >> x; n             if (!id.count(x)) id[x] = N++; n             s[i][j] = id[x]; n         } n         sort(s[i].begin(), s[i].end()); n     } n     int block = (int)sqrt(M) + 1; n     vector<int> large, small; n     for (int i = 0; i < n; i++) { n         if ((int)s[i].size() >= block) large.push_back(i); n         else small.push_back(i); n     } n  n     int time = 0; n     vector<int> visited(N, 0); n     for (int u : large) { n         time++; n         for (int x : s[u]) visited[x] = time; n         for (int v = 0; v < n; v++) { n             if (u == v) continue; n             int d = 0; n             for (int val : s[v]) { n                 d += visited[val] == time; n                 if (d == 2) { n                     cout << u + 1 <<     << v + 1 <<   n ; n                     return; n                 } n             } n         } n     } n  n     vector<vector<int> > adj(N); n     for (int i = 0; i < n; i++) { n         for (int x : s[i]) adj[x].push_back(i); n     } n     vector<int> last(N, -1), pos(N, -1); n     for (int u = 0; u < N; u++) { n         for (int i : adj[u]) { n             if ((int)s[i].size() < block) { n                 for (int j = 0; j < (int)s[i].size() && s[i][j] < u; j++) { n                     int v = s[i][j]; n                     if (last[v] == u) { n                         cout << pos[v] + 1 <<     << i + 1 <<   n ; n                         return; n                     } n                     last[v] = u; n                     pos[v] = i; n                 } n             } n         } n     } n  n     cout << -1 <<   n ; n } n  n void solve() { n     clock_t begin = clock();     n     int T; n     cin >> T; n     //T = 1; n     int TestCase = 0; n     while (T--) { n         TestCase += 1; n         //cout <<  Case #  << TestCase <<  :  <<    ; n         //cout <<  Test   << TestCase <<  :  ; n         sol(); n     } n     clock_t end = clock(); n     cerr <<  Time:   << fixed << setprecision(10) << (double)(end - begin) / (double)CLOCKS_PER_SEC <<   n ; n } n  n int main(int argc,char *argv[]) { n     ///srand(time(nullptr)); n     ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); n     open_file(); n     solve(); n }


#include <bits/stdc++.h> n #define int long long n #define boost ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) n  n using namespace std; n  n #define MOD         1000000007 n #define nline         n          n #define pb          push_back n #define mp          make_pair n #define fi          first n #define se          second n #define yes         cout<< YES n  n #define no          cout<< NO n  n #define vi          vector<int> n #define pii         pair<int,int> n #define sz(v)       v.size() n #define lcm(a,b)    (a*b)/(__gcd(a,b)) n #define deb(x)      cout<<#x<<  =  <<x<<endl; n #define deb2(x,y)   cout<<#x<<  =  <<x<<   <<#y<<  =  <<y<<endl; n #define deb3(x,y,z) cout<<#x<<  =  <<x<<   <<#y<<  =  <<y<<   <<#z<<  =  <<z<<endl; n #define PR(a,n)     cout<<#a<<  =  ;for(int _=0;_<n;_++)cout<<a[_]<<   ;cout<<endl; n #define all(v)      v.begin(),v.end() n #define sortv(v)    sort(all(v)); n #define sortrev(v)  sort(all(v),greater<int>()) n #define maxHeap     priority_queue<int> n #define minHeap     priority_queue<int, vector<int>, greater<int>> n  n int pow(int a, int b, int m){ n     int ans = 1; n     while(b){ n         if (b&1) ans = (ans*a) % m; n         b /= 2; n         a = (a*a) % m; n     } n     return ans; n } n  n int SumOfDigits(int x) n { n     if (x < 0)return -1; n     int s = 0; n     while(x) n     { n         s += x%10; n         x /= 10; n     } n   n     return s; n } n  n //lower_bound = first element >= val, end otherwise n //upper_bound = first element > val, end otherwise n //remember 2 pointer , binary search , bits method n //always look for the edges i.e max value ad min value possible n  n inline void solve() n { n     int k,n,m; n     cin>>k>>n>>m; n     int a[n],b[m]; n     int cnt1 = 0,cnt2=0; n     vi x,y; n     for(int i=0;i<n;i++) n     { n         cin>>a[i]; n     } n     for(int i=0;i<m;i++) n     { n         cin>>b[i]; n     } n     vi ans; n     int i=0,j=0; n     int total = k; n     while(1) n     { n         if(sz(ans)==n+m) n         break; n         if(a[i]==0 && i<n) n         { n             ans.pb(0); n             total++; n             i++; n             continue; n         } n         else if(b[j]==0 && j<m) n         { n             ans.pb(0); n             total++; n             j++; n             continue; n         } n         if(( a[i]<b[j] && (i<n && j<m) ) || (i<n && j>=m)) n         { n             if(total>=a[i]) n             { n                 ans.pb(a[i]); n                 i++; n             } n             else  n             { n                 cout<<-1<<nline; n                 return; n             } n         } n         else if( (a[i]>=b[j] && (j<m && i<n)) || (j<m && i>=n) ) n         { n             if(total>=b[j]) n             { n                 ans.pb(b[j]); n                 j++; n             } n             else  n             { n                 cout<<-1<<nline; n                 return; n             } n         } n     } n     if(sz(ans)==n+m) n     { n         for(auto x:ans) n         cout<<x<<   ; n         cout<<nline; n     } n     else  n     cout<<-1<<nline; n  n     return; n } n  n signed main() n { n     boost; n     int t=1; n     cin>>t; n     for(int tt=1;tt<=t;tt++) n     { n         // cout<< Case # <<tt<< :  ; n         solve(); n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 503; n int m, n, vis[N][N]; n vector<int> adj[N * N]; n void init() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n int id(int x, int y) { return (x - 1) * n + y; } n struct ds { n   char c; n   int x, y; n }; n vector<ds> V; n void print(int u, int cc, int type = -1) { n   int x = (u - 1) / n + 1, y = u - (x - 1) * n; n   if (adj[u].size() == 0 && !type) { n     V.push_back((ds){ R , x, y}); n     return; n   } n   V.push_back((ds){ B , x, y}); n   for (int v : adj[u]) print(v, cc, 0); n   if (!type) V.push_back((ds){ D , x, y}), V.push_back((ds){ R , x, y}); n } n int cc = 0; n queue<pair<int, int> > Q; n int hang[4] = {0, 0, 1, -1}; n int cot[4] = {1, -1, 0, 0}; n void build(int orgx, int orgy, int cc) { n   vis[orgx][orgy] = cc; n   Q.push({orgx, orgy}); n   while (Q.size()) { n     int x = Q.front().first, y = Q.front().second; n     Q.pop(); n     for (int t = 0; t <= (int)3; ++t) { n       int nx = x + hang[t], ny = y + cot[t]; n       if (nx < 1 || nx > m || ny < 1 || ny > n || vis[nx][ny]) continue; n       adj[id(x, y)].push_back(id(nx, ny)); n       vis[nx][ny] = cc; n       Q.push({nx, ny}); n     } n   } n   print(id(orgx, orgy), cc); n } n void solve() { n   cin >> m >> n; n   for (int i = 1; i <= (int)m; ++i) n     for (int j = 1; j <= (int)n; ++j) { n       char c; n       cin >> c; n       if (c ==  # ) vis[i][j] = -1; n     } n   for (int i = 1; i <= (int)m; ++i) n     for (int j = 1; j <= (int)n; ++j) n       if (!vis[i][j]) build(i, j, ++cc); n   cout << V.size() <<   n ; n   for (ds it : V) cout << it.c <<     << it.x <<     << it.y <<   n ; n } n int main() { n   init(); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n queue<int> q; n const int maxn = 40007; n const int maxm = 800007; n const int inf = 0x7f7f7f7f; n struct E { n   int u, v, w, cf; n } e[maxm]; n int first[maxn], nt[maxm], ES = 1; n bool acc[maxm]; n inline void addE(int u, int v, int w, int cf) { n   e[++ES] = (E){u, v, w, cf}; n   nt[ES] = first[u]; n   first[u] = ES; n   return; n } n inline void add(int u, int v, int w) { n   addE(u, v, w, 1); n   addE(v, u, w, 0); n   return; n } n int tot; n int N, M; n int cur[maxn]; n int dep[maxn]; n int S, T; n bool BFS() { n   memset(dep, 0, sizeof(dep)); n   dep[S] = 1; n   q.push(S); n   int u, v; n   while (!q.empty()) { n     u = q.front(); n     q.pop(); n     for (int i = first[u]; i; i = nt[i]) { n       v = e[i].v; n       if (e[i].cf > 0 && acc[i] && !dep[v]) { n         dep[v] = dep[u] + 1; n         q.push(v); n       } n     } n   } n   return dep[T] != 0; n } n int dfs(int u, int f) { n   if (u == T) return f; n   int v, d, sum = 0; n   for (int &i = cur[u]; i; i = nt[i]) { n     v = e[i].v; n     if (dep[v] == dep[u] + 1 && e[i].cf > 0 && acc[i]) { n       d = dfs(v, min(f, e[i].cf)); n       if (d > 0) { n         sum += d; n         f -= d; n         e[i].cf -= d; n         e[i ^ 1].cf += d; n         if (f <= 0) return sum; n       } n     } n   } n   return sum; n } n int solve() { n   int re = 0; n   while (BFS()) { n     memcpy(cur, first, sizeof(first)); n     re += dfs(S, inf); n   } n   return re; n } n bool check(int p) { n   for (int i = 1; i <= ES; i++) { n     if (e[i].w <= p) n       acc[i] = true; n     else n       acc[i] = false; n     e[i].cf = (i & 1) ^ 1; n   } n   return solve() == N; n } n int main() { n   scanf( %d%d , &N, &M); n   int u, v, w; n   int L = 0, R = 0, mid, ans = -1; n   for (int i = 1; i <= M; i++) scanf( %d%d%d , &u, &v, &w), add(u, v + N, w); n   S = 0; n   T = 2 * N + 1; n   for (int i = 1; i <= N; i++) add(S, i, -1), add(i + N, T, -1); n   R = 1e9; n   while (L <= R) { n     mid = L + R >> 1; n     if (check(mid)) n       ans = mid, R = mid - 1; n     else n       L = mid + 1; n   } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6; n int Left[N], Right[N]; n void pre(int node) { n   if (node == -1) return; n   pre(Left[node]); n   printf( %d n , node + 1); n   pre(Right[node]); n } n int main() { n   memset(Left, -1, sizeof Left); n   memset(Right, -1, sizeof Right); n   string s; n   cin >> s; n   for (int i = 0; i + 1 < (int)s.size(); ++i) n     if (s[i] ==  l ) n       Left[i] = i + 1; n     else n       Right[i] = i + 1; n   pre(0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Edge { n   int v, next; n } edge[2050]; n int e, ft[2050]; n int a, n, m; n bool rain[2050]; n int x[2050], p[2050]; n long long dp[2050][2050]; n long long opt[2050]; n int main() { n   int l, r; n   while (cin >> a >> n >> m) { n     e = 0; n     for (int i = 0; i <= a; i++) { n       rain[i] = false; n       for (int j = 0; j <= m; j++) dp[i][j] = (1ll << 60); n       ft[i] = -1; n     } n     for (int i = 0; i < n; i++) { n       scanf( %d%d , &l, &r); n       while (l < r) { n         rain[l++] = true; n       } n     } n     for (int i = 0; i < m; i++) { n       scanf( %d%d , x + i, p + i); n       edge[e].v = i; n       edge[e].next = ft[x[i]]; n       ft[x[i]] = e++; n       if (x[i] == 0) dp[0][i] = 0; n     } n     dp[0][m] = 0; n     p[m] = 0; n     opt[0] = 0; n     for (int i = 0; i < a; i++) { n       for (int j = 0; j <= m; j++) { n         if (dp[i][j] >= (1ll << 60)) continue; n         if (j < m || !rain[i]) n           dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + p[j]); n       } n       opt[i + 1] = (1ll << 60); n       for (int j = 0; j <= m; j++) opt[i + 1] = min(opt[i + 1], dp[i + 1][j]); n       for (int j = ft[i + 1]; j != -1; j = edge[j].next) { n         int idx = edge[j].v; n         dp[i + 1][idx] = min(dp[i + 1][idx], opt[i + 1]); n       } n       dp[i + 1][m] = min(dp[i + 1][m], opt[i + 1]); n     } n     long long ans = -1; n     if (opt[a] < (1ll << 60)) ans = opt[a]; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fastIO(void); n void decimal(int); n long long int modmulti(long long int, long long int, long long int); n long long int modadd(long long int, long long int, long long int); n long long int modpower(long long int, long long int, long long int); n long long int gcd(long long int, long long int); n long long int modinv(long long int, long long int); n int main() { n   fastIO(); n   long long int n; n   cin >> n; n   long long int t1, t2; n   vector<long long int> a(n); n   multiset<long long int> b; n   for (long long int i = 0; i < n; i++) cin >> a[i]; n   for (long long int i = 0; i < n; i++) { n     cin >> t1; n     b.insert(t1); n   } n   long long int c[n]; n   for (long long int i = 0; i < n; i++) { n     t2 = n - a[i]; n     auto itr = b.lower_bound(n - a[i]); n     if (itr == b.end()) itr = b.begin(); n     c[i] = (a[i] + *itr) % n; n     b.erase(itr); n   } n   for (long long int i = 0; i < n; i++) cout << c[i] <<    ; n   cout <<   n ; n } n void fastIO(void) { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n } n void decimal(int n) { n   cout << fixed; n   cout << setprecision(n); n } n long long int modmulti(long long int a, long long int b, long long int m) { n   return ((a % m) * (b % m)) % m; n } n long long int modadd(long long int a, long long int b, long long int m) { n   return ((a % m) + (b % m) + m) % m; n } n long long int modpower(long long int a, long long int n, long long int m) { n   if (n == 0) return 1; n   long long int result = 1; n   while (n > 0) { n     if (n % 2 == 1) result = modmulti(result, a, m); n     a = modmulti(a, a, m); n     n = n / 2; n   } n   return result; n } n long long int gcd(long long int a, long long int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long int modinv(long long int a, long long int m) { n   return modpower(a, m - 2, m); n } n


#include <bits/stdc++.h> n using namespace std; n const static int maxN = 1000; n struct { n   int r, c; n } tooth[maxN]; n int toothrow[maxN + 1]; n int main() { n   int n, m, k; n   int i; n   int ans; n   while (scanf( %d%d%d , &n, &m, &k) != EOF) { n     for (i = 0; i < n; i++) { n       scanf( %d%d , &tooth[i].r, &tooth[i].c); n     } n     memset(toothrow, -1, sizeof(int) * (m + 1)); n     ans = 0; n     for (i = 0; i < n; i++) { n       if (toothrow[tooth[i].r] != -1) { n         if (tooth[i].c < toothrow[tooth[i].r]) { n           ans -= toothrow[tooth[i].r]; n           ans += tooth[i].c; n           toothrow[tooth[i].r] = tooth[i].c; n         } n       } else { n         ans += tooth[i].c; n         toothrow[tooth[i].r] = tooth[i].c; n       } n     } n     printf( %d n , min(ans, k)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace { n using namespace std; n template <typename flow_t = long long> n struct PushRelabel { n   struct Edge { n     int to, rev; n     flow_t f, c; n   }; n   vector<vector<Edge>> g; n   vector<flow_t> ec; n   vector<Edge*> cur; n   vector<vector<int>> hs; n   vector<int> H; n   PushRelabel(int n) : g(n), ec(n), cur(n), hs(2 * n), H(n) {} n   void add_edge(int s, int t, flow_t cap, flow_t rcap = 0) { n     if (s == t) return; n     Edge a = {t, (int)g[t].size(), 0, cap}; n     Edge b = {s, (int)g[s].size(), 0, rcap}; n     g[s].push_back(a); n     g[t].push_back(b); n   } n   void add_flow(Edge& e, flow_t f) { n     Edge& back = g[e.to][e.rev]; n     if (!ec[e.to] && f) hs[H[e.to]].push_back(e.to); n     e.f += f; n     e.c -= f; n     ec[e.to] += f; n     back.f -= f; n     back.c += f; n     ec[back.to] -= f; n   } n   flow_t max_flow(int s, int t) { n     const int n = g.size(); n     ec.assign(n, 0); n     cur.assign(n, nullptr); n     hs.assign(2 * n, {}); n     H.assign(n, 0); n     int v = g.size(); n     H[s] = v; n     ec[t] = 1; n     vector<int> co(2 * v); n     co[0] = v - 1; n     for (int i = 0; i < v; ++i) cur[i] = g[i].data(); n     for (auto& e : g[s]) add_flow(e, e.c); n     if (hs[0].size()) n       for (int hi = 0; hi >= 0;) { n         int u = hs[hi].back(); n         hs[hi].pop_back(); n         while (ec[u] > 0) n           if (cur[u] == g[u].data() + g[u].size()) { n             H[u] = 1e9; n             for (auto& e : g[u]) n               if (e.c && H[u] > H[e.to] + 1) H[u] = H[e.to] + 1, cur[u] = &e; n             if (++co[H[u]], !--co[hi] && hi < v) n               for (int i = 0; i < v; ++i) n                 if (hi < H[i] && H[i] < v) { n                   --co[H[i]]; n                   H[i] = v + 1; n                 } n             hi = H[u]; n           } else if (cur[u]->c && H[u] == H[cur[u]->to] + 1) n             add_flow(*cur[u], min(ec[u], cur[u]->c)); n           else n             ++cur[u]; n         while (hi >= 0 && hs[hi].empty()) --hi; n       } n     return -ec[s]; n   } n }; n template <typename flow_t = long long> n struct MaxFlow { n   static constexpr flow_t INF = numeric_limits<flow_t>::max(); n   PushRelabel<flow_t> flow; n   MaxFlow(const int n) : flow(n) {} n   inline void add_arc(const int u, const int v, const flow_t capacity) { n     flow.add_edge(u, v, capacity); n   } n   inline void add_edge(const int u, const int v, const flow_t capacity) { n     flow.add_edge(u, v, capacity, capacity); n   } n   inline void add_infinite_arc(const int u, const int v) { n     flow.add_edge(u, v, INF); n   } n   inline void add_infinite_edge(const int u, const int v) { n     flow.add_edge(u, v, INF, INF); n   } n   flow_t solve(const int s, const int t) { return flow.max_flow(s, t); } n }; n template <typename flow_t> n class MaxFlowLR { n  private: n   static constexpr flow_t INF = numeric_limits<flow_t>::max(); n   int n; n   vector<flow_t> from_source, to_sink, outgoing_capacity; n   vector<tuple<int, int, flow_t>> demands; n   flow_t total_demand; n   bool invalid; n   PushRelabel<flow_t> flow; n  n  public: n   MaxFlowLR(const int n) n       : n(n), n         from_source(n), n         to_sink(n), n         outgoing_capacity(n), n         total_demand(0), n         invalid(false), n         flow(n + 2) {} n   void add_arc(const int u, const int v, const flow_t demand, n                const flow_t capacity) { n     if (demand > capacity) { n       invalid = true; n       return; n     } n     if (u != v) { n       from_source[v] += demand; n       to_sink[u] += demand; n       flow.add_edge(u, v, capacity - demand); n       demands.push_back({u, v, demand}); n       total_demand += demand; n       outgoing_capacity[u] += capacity; n     } n   } n   flow_t solve(const int s, const int t) { n     if (invalid) { n       return -1; n     } n     const int source = n; n     const int sink = n + 1; n     for (int v = 0; v < n; ++v) { n       if (from_source[v] != 0) { n         flow.add_edge(source, v, from_source[v]); n       } n       if (to_sink[v] != 0) { n         flow.add_edge(v, sink, to_sink[v]); n       } n     } n     flow.add_edge(t, s, INF); n     const flow_t flow_aux = flow.max_flow(source, sink); n     if (s != t) { n       flow.g[s].pop_back(); n       flow.g[t].pop_back(); n     } n     for (int v = n - 1; v >= 0; --v) { n       if (to_sink[v] != 0) { n         flow.g[sink].pop_back(); n         flow.g[v].pop_back(); n       } n       if (from_source[v] != 0) { n         flow.g[source].pop_back(); n         flow.g[v].pop_back(); n       } n     } n     if (flow_aux != total_demand) return -1; n     const flow_t flow_new = flow.max_flow(s, t); n     flow_t total_flow = outgoing_capacity[s]; n     for (const auto& e : flow.g[s]) total_flow -= e.c; n     return total_flow; n   } n   vector<map<int, flow_t>> retrieve() { n     vector<map<int, flow_t>> ans(n); n     for (int u = 0; u < n; ++u) n       for (const auto& e : flow.g[u]) n         if (e.f > 0) ans[u][e.to] += e.f, ans[e.to][u] -= e.f; n     for (auto t : demands) n       if (get<2>(t) > 0) n         ans[get<0>(t)][get<1>(t)] += get<2>(t), n             ans[get<1>(t)][get<0>(t)] -= get<2>(t); n     return ans; n   } n }; n }  // namespace n inline int divceil(const int x, const int y) { n   const int q = x / y; n   return q * y == x ? q : q + 1; n } n inline long long divceil(const long long x, const long long y) { n   const long long q = x / y; n   return q * y == x ? q : q + 1; n } n map<int, int> coordinate_compression(vector<int> v) { n   sort(v.begin(), v.end()); n   const int n = unique(v.begin(), v.end()) - v.begin(); n   map<int, int> compr; n   for (int i = 0; i < n; ++i) compr[v[i]] = i; n   return compr; n } n map<long long, int> coordinate_compression(vector<long long> v) { n   sort(v.begin(), v.end()); n   const int n = unique(v.begin(), v.end()) - v.begin(); n   map<long long, int> compr; n   for (int i = 0; i < n; ++i) compr[v[i]] = i; n   return compr; n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   int r, b; n   scanf( %d%d , &r, &b); n   bool flip = false; n   if (r >= b) { n     flip = true; n     swap(r, b); n   } n   vector<std::pair<int, int>> original_points; n   vector<int> allx, ally; n   for (int i = 0; i < n; ++i) { n     int x, y; n     scanf( %d%d , &x, &y); n     original_points.emplace_back(x, y); n     allx.push_back(x); n     ally.push_back(y); n   } n   map<int, int> comprx = coordinate_compression(allx), n                 compry = coordinate_compression(ally); n   const int xlines = comprx.rbegin()->second + 1; n   const int ylines = compry.rbegin()->second + 1; n   vector<int> xdeg(xlines), ydeg(ylines); n   vector<int> xcons(xlines), ycons(ylines); n   vector<std::pair<int, int>> points; n   for (const std::pair<int, int> p : original_points) { n     const int xx = comprx[p.first]; n     const int yy = compry[p.second]; n     points.emplace_back(xx, yy); n     ++xdeg[xx]; n     ++ydeg[yy]; n     ++xcons[xx]; n     ++ycons[yy]; n   } n   while (m--) { n     int t, l, d; n     scanf( %d%d%d , &t, &l, &d); n     if (t == 1) { n       const auto it = comprx.find(l); n       if (it != comprx.end()) xcons[it->second] = min(xcons[it->second], d); n     } else { n       const auto it = compry.find(l); n       if (it != compry.end()) ycons[it->second] = min(ycons[it->second], d); n     } n   } n   MaxFlowLR<long long> dinic(2 + xlines + ylines); n   const int source = 0; n   const int sink = 1; n   const auto get_xvert = [&](const int i) { return 2 + i; }; n   const auto get_yvert = [&](const int i) { return 2 + xlines + i; }; n   for (int i = 0; i < xlines; ++i) { n     const int L = divceil(xdeg[i] - xcons[i], 2); n     const int R = (xdeg[i] + xcons[i]) / 2; n     dinic.add_arc(source, get_xvert(i), L, R); n   } n   for (int i = 0; i < ylines; ++i) { n     const int L = divceil(ydeg[i] - ycons[i], 2); n     const int R = (ydeg[i] + ycons[i]) / 2; n     dinic.add_arc(get_yvert(i), sink, L, R); n   } n   for (const std::pair<int, int> p : points) n     dinic.add_arc(get_xvert(p.first), get_yvert(p.second), 0, 1); n   long long flow_value = dinic.solve(source, sink); n   if (flow_value == -1) { n     puts( -1 ); n     return 0; n   } n   long long ans = flow_value * r + (n - flow_value) * b; n   cout << ans << endl; n   map<std::pair<int, int>, int> red; n   auto flow = dinic.retrieve(); n   for (int i = 0; i < xlines; ++i) { n     const int x = get_xvert(i); n     for (const auto p : flow[x]) { n       const int j = p.first - 2 - xlines; n       const int f = p.second; n       if (j >= 0) red[std::pair<int, int>(i, j)] += f; n     } n   } n   for (const std::pair<int, int> p : points) { n     int& v = red[p]; n     if (v) { n       v--; n       putchar(flip ?  b  :  r ); n     } else { n       putchar(flip ?  r  :  b ); n     } n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 5; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n void solve() { n   vector<long long> v(14); n   long long curr_score = 0; n   long long o = 0; n   for (int i = 0; i < 14; i++) { n     cin >> v[i]; n   } n   for (int i = 0; i < 14; i++) { n     vector<long long> t(14); n     for (int k = 0; k < 14; k++) { n       t[k] = v[k]; n     } n     long long p = v[i] / 14, q = v[i] % 14; n     t[i] = 0; n     for (int j = 0; j < 14; j++) { n       t[j] += p; n     } n     long long j = i + 1; n     while (q--) { n       if (j == 14) { n         j = 0; n       } n       t[j++]++; n     } n     long long cnt = 0; n     for (int i = 0; i < 14; i++) { n       if (t[i] % 2 == 0) { n         cnt += t[i]; n       } n     } n     curr_score = max(curr_score, cnt); n   } n   cout << curr_score <<   n ; n } n int main() { n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   long long a, b, cost; n   edge(long long x, long long y, long long z) : a(x), b(y), cost(z){}; n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, k, p; n   cin >> n >> k >> p; n   vector<long long> v(n), a(k); n   for (long long i = 0; i < (long long)(n); i++) cin >> v[i]; n   for (long long i = 0; i < (long long)(k); i++) cin >> a[i]; n   sort((v).begin(), (v).end()); n   sort((a).begin(), (a).end()); n   long long ans = INT_MAX; n   for (long long i = (long long)(0); i <= (long long)(k - n); i++) { n     long long res = INT_MIN; n     for (long long j = (long long)(0); j <= (long long)(n - 1); j++) { n       res = max(res, abs(v[j] - a[i + j]) + abs(a[i + j] - p)); n     } n     ans = min(ans, res); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long ans; n int n, i, j, k, u, v; n int size[80011], fa[80011]; n vector<int> e[80011]; n void dfs(int now) { n   unsigned long long o = 0; n   size[now] = 1; n   for (int i = 0; i < e[now].size(); i++) n     if (e[now][i] != fa[now]) { n       fa[e[now][i]] = now; n       dfs(e[now][i]); n       o += (unsigned long long)size[now] * size[e[now][i]]; n       size[now] += size[e[now][i]]; n     } n   ans -= o * (o + 2LL * size[now] * (n - size[now])); n } n int main() { n   scanf( %d , &n); n   for (i = 1; i < n; i++) { n     scanf( %d%d , &u, &v); n     e[u].push_back(v); n     e[v].push_back(u); n   } n   ans = (n - 1LL) * n / 2; n   ans *= ans; n   dfs(1); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ans = 0; n string str; n int n; n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   cin >> str; n   for (int i = 0; i < n; ++i) ans += (str[i] -  0 ) & 1 ? 0 : i + 1; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } n inline void normal(long long int &a) { n   a %= 1000000007; n   (a < 0) && (a += 1000000007); n } n inline long long int modMul(long long int a, long long int b) { n   a %= 1000000007, b %= 1000000007; n   normal(a), normal(b); n   return (a * b) % 1000000007; n } n inline long long int modAdd(long long int a, long long int b) { n   a %= 1000000007, b %= 1000000007; n   normal(a), normal(b); n   return (a + b) % 1000000007; n } n inline long long int modSub(long long int a, long long int b) { n   a %= 1000000007, b %= 1000000007; n   normal(a), normal(b); n   a -= b; n   normal(a); n   return a; n } n inline long long int modPow(long long int b, long long int p) { n   long long int r = 1; n   while (p) { n     if (p & 1) r = modMul(r, b); n     b = modMul(b, b); n     p >>= 1; n   } n   return r; n } n inline long long int modInverse(long long int a) { n   return modPow(a, 1000000007 - 2); n } n inline long long int modDiv(long long int a, long long int b) { n   return modMul(a, modInverse(b)); n } n inline bool isInside(pair<int, int> p, long long int n, long long int m) { n   return (p.first >= 0 && p.first < n && p.second >= 0 && p.second < m); n } n inline bool isInside(pair<int, int> p, long long int n) { n   return (p.first >= 0 && p.first < n && p.second >= 0 && p.second < n); n } n inline bool isSquare(long long int x) { n   long long int s = sqrt(x); n   return (s * s == x); n } n inline bool isFib(long long int x) { n   return isSquare(5 * x * x + 4) || isSquare(5 * x * x - 4); n } n inline bool isPowerOfTwo(long long int x) { n   return ((1LL << (long long int)log2(x)) == x); n } n template <typename first, typename second> n ostream &operator<<(ostream &os, const pair<first, second> &p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   os <<  { ; n   for (auto it = v.begin(); it != v.end(); ++it) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   return os <<  } ; n } n template <typename T> n ostream &operator<<(ostream &os, const set<T> &v) { n   os <<  [ ; n   for (auto it = v.begin(); it != v.end(); ++it) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   return os <<  ] ; n } n template <typename first, typename second> n ostream &operator<<(ostream &os, const map<first, second> &v) { n   os <<  [ ; n   for (auto it = v.begin(); it != v.end(); ++it) { n     if (it != v.begin()) os <<  ,  ; n     os << it->first <<   =   << it->second; n   } n   return os <<  ] ; n } n clock_t tStart = clock(); n void faltu() { cerr << endl; } n template <typename T> n void faltu(T a[], int n) { n   for (int i = 0; i < n; ++i) cerr << a[i] <<    ; n   cerr << endl; n } n template <typename T, typename... hello> n void faltu(T arg, const hello &...rest) { n   cerr << arg <<    ; n   faltu(rest...); n } n const long long int INF = 0x3f3f3f3f3f3f3f3f; n const long double EPS = 1e-9; n const int inf = 0x3f3f3f3f; n const int mx = (int)2e5 + 9; n long long int n, m, a, b, u, t, i, j, d, cs = 0, counT = 0, k, ans = 0, l = 0, n                                          sum1 = 0, sum = 0, Max, Min, num; n int _time, idx; n set<long long int> vc[mx]; n int st[mx], en[mx], pos[mx], arr[mx]; n void dfs(int node, int par = 0) { n   st[node] = ++_time; n   arr[node] = ++idx; n   pos[idx] = node; n   for (auto next : vc[node]) { n     if (next == par) continue; n     dfs(next, node); n   } n   en[node] = _time; n } n int main() { n   cin >> n >> m; n   for (i = 2; i <= n; i++) { n     cin >> a; n     vc[i].insert(a); n     vc[a].insert(i); n   } n   dfs(1); n   while (m--) { n     cin >> u >> k; n     if (arr[u] + k - 1 > n) { n       cout << -1 <<   n ; n       continue; n     } n     int v = pos[arr[u] + k - 1]; n     if (st[u] <= st[v] && en[u] >= en[v]) n       cout << v <<   n ; n     else n       cout << -1 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long test(vector<long long>& h, int l, int r) { n   long long ret = 0; n   for (int i = l; i < r; i++) { n     long long tmp = 0; n     for (int j = i + 1; j < r; j++) { n       tmp = max(tmp, abs(h[j] - h[j - 1])); n       ret += tmp; n     } n   } n   return ret; n } n int main() { n   int n, q; n   cin >> n >> q; n   vector<long long> h(n); n   for (int i = 0; i < n; i++) { n     scanf( %I64d , &h[i]); n   } n   while (q--) { n     int l, r; n     scanf( %d%d , &l, &r); n     l--; n     long long ans = 0; n     long long last = 0; n     deque<pair<long long, int>> dq; n     for (int i = l + 1; i < r; i++) { n       long long tmp = abs(h[i] - h[i - 1]); n       long long len = 1; n       while (dq.size() && dq.back().first <= tmp) { n         auto val = dq.back(); n         dq.pop_back(); n         len += val.second; n         last -= val.first * val.second; n       } n       dq.push_back({tmp, len}); n       last += tmp * len; n       ans += last; n     } n     printf( %I64d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long cont = 0; n template <typename T, typename F> n struct AHO_CORASICK { n   const long long ALPHA = 26; n   long long cv(F c) { return c -  0 ; } n   vector<map<long long, long long>> tgo; n   vector<long long> link; n   vector<long long> ch; n   vector<long long> p; n   vector<bool> flag; n   long long sz = 0; n   long long size() { return sz; } n   void new_node(long long tp, long long tch) { n     sz++; n     tgo.emplace_back(); n     link.push_back(0); n     ch.push_back(tch); n     p.push_back(tp); n     flag.push_back(false); n   } n   AHO_CORASICK() { n     new_node(0, 0); n     new_node(1, 0); n   } n   void add_string(T cad) { n     long long pt = 1; n     for (long long i = 0; i < cad.size(); i++) { n       long long tch = cv(cad[i]); n       if (tgo[pt][tch] == 0) { n         tgo[pt][tch] = sz; n         new_node(pt, tch); n       } n       pt = tgo[pt][tch]; n     } n     flag[pt] = true; n   } n   long long get_link(long long nodo) { n     if (link[nodo] == 0) { n       if (nodo == 1 || p[nodo] == 1) n         link[nodo] = 1; n       else n         link[nodo] = go(get_link(p[nodo]), ch[nodo]); n       flag[nodo] = flag[nodo] | flag[link[nodo]]; n     } n     return link[nodo]; n   } n   long long go(long long nodo, F tch) { return go(nodo, cv(tch)); } n   long long go(long long nodo, long long tch) { n     if (tgo[nodo][tch] == 0) { n       tgo[nodo][tch] = (nodo == 1) ? 1 : go(get_link(nodo), tch); n     } n     return tgo[nodo][tch]; n   } n }; n long long X; n vector<string> bf; n string temp; n void fff(long long sum) { n   if (sum > X) return; n   for (long long i = 0; i < temp.size(); i++) { n     long long s = 0; n     for (long long j = i; j < temp.size(); j++) { n       s += temp[j] -  0 ; n       if (s != X && X % s == 0) return; n     } n   } n   if (sum == X) { n     bf.push_back(temp); n     return; n   } n   for (long long i = 1; i <= 9; i++) { n     temp += (i +  0 ); n     fff(sum + i); n     temp.pop_back(); n   } n } n const long long MAXN = 1010; n long long dp[MAXN][MAXN * 5]; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   for (long long i = 0; i < MAXN; i++) { n     for (long long j = 0; j < 5 * MAXN; j++) { n       dp[i][j] = 1e9; n     } n   } n   string cad; n   cin >> cad; n   cin >> X; n   long long N = cad.size(); n   fff(0); n   AHO_CORASICK<string, char> ac; n   for (auto x : bf) { n     ac.add_string(x); n   } n   dp[0][1] = 0; n   for (long long i = 0; i < N; i++) { n     for (long long j = 1; j < ac.size(); j++) { n       long long pt = ac.go(j, cad[i]); n       if (ac.flag[pt] == false) dp[i + 1][pt] = min(dp[i + 1][pt], dp[i][j]); n       dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + 1); n     } n   } n   long long res = 1e9; n   for (long long i = 0; i < ac.sz; i++) { n     res = min(res, dp[N][i]); n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char name[5][10] = { Sheldon ,  Leonard ,  Penny ,  Rajesh ,  Howard }; n   int n, c = 5; n   scanf( %d , &n); n   while (n > c) { n     n -= c; n     c *= 2; n   } n   printf( %s n , name[(n - 1) / (c / 5)]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   map<string, pair<string, long long>> best; n   long long n; n   cin >> n; n   for (long long i = 0; i < n; i++) { n     string name; n     long long score; n     cin >> name >> score; n     best[name].second = max(best[name].second, score); n   } n   for (auto itr = best.begin(); itr != best.end(); itr++) { n     long long count = 0; n     for (auto i = best.begin(); i != best.end(); i++) { n       if (itr->second.second >= i->second.second) count++; n     } n     double percent = ((double)count / best.size() * 1.0) * 100.0; n     if (percent < 50.0) n       itr->second.first =  noob ; n     else if (percent >= 50.0 && percent < 80.0) n       itr->second.first =  random ; n     else if (percent >= 80.0 && percent < 90.0) n       itr->second.first =  average ; n     else if (percent >= 90.0 && percent < 99.0) n       itr->second.first =  hardcore ; n     else n       itr->second.first =  pro ; n   } n   cout << best.size() << endl; n   for (auto x : best) { n     cout << x.first <<     << x.second.first << endl; n   } n } n int32_t main() { n   long long testcases = 1; n   while (testcases--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void run_case() { n   string A, B, C, D; n   cin >> A; n   cin >> B; n   C = (A + B); n   sort(C.begin(), C.end()); n   cin >> D; n   sort(D.begin(), D.end()); n   if (C == D) n     cout <<  YES n ; n   else n     cout <<  NO n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long T = 1; n   while (T--) run_case(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:200000000 ) n const double EPS = 1E-9; n const int INF = 1000000000; n const long long INF64 = (long long)1E18; n const double PI = 3.1415926535897932384626433832795; n char s[2100][2100]; n int ans[2100]; n int main() { n   int n, m, k; n   scanf( %d%d%d n , &n, &m, &k); n   for (int i = 0; i < (int)(n); i++) gets(s[i]); n   for (int i = 0; i < (int)(n); i++) n     for (int j = 0; j < (int)(m); j++) { n       if (s[i][j] ==  .  || s[i][j] ==  D ) continue; n       if (s[i][j] ==  U ) { n         if (i % 2 == 1) continue; n         ans[j]++; n         continue; n       } n       int nj; n       if (s[i][j] ==  L ) n         nj = j - i; n       else n         nj = j + i; n       if (0 <= nj && nj < m) ans[nj]++; n     } n   for (int i = 0; i < (int)(m); i++) printf(i ?   %d  :  %d , ans[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int in() { n   int32_t x; n   scanf( %d , &x); n   return x; n } n inline long long lin() { n   long long x; n   scanf( %lld , &x); n   return x; n } n inline string get() { n   char ch[2000010]; n   scanf( %s , ch); n   return ch; n } n const int MAX_LG = 21; n const int maxn = 2e3 + 10; n const int maxm = 1e5 + 10; n const int base = 29; n const int mod = 1e9 + 7; n const int INF = 1e9 + 100; n const int SQ = 317; n int Cmp; n int mark[maxn]; n int v[maxm], u[maxm], deg[maxn], has[maxn][maxn], e[maxm]; n int vc[5]; n inline void dfs(int node, int id) { n   int edgeid = has[node][vc[id]] - 1; n   if (edgeid < 0) return; n   int nxt = v[edgeid] == node ? u[edgeid] : v[edgeid]; n   has[node][vc[id]] = 0; n   has[nxt][vc[id]] = 0; n   dfs(nxt, id ^ 1); n   has[node][vc[id ^ 1]] = edgeid + 1; n   has[nxt][vc[id ^ 1]] = edgeid + 1; n   e[edgeid] = vc[id ^ 1]; n } n int res; n int32_t main() { n   int a = in(), b = in(), m = in(); n   for (int i = 0; i < m; i++) { n     v[i] = in(), u[i] = in() + a; n     deg[v[i]]++; n     deg[u[i]]++; n     res = max(res, max(deg[v[i]], deg[u[i]])); n   } n   for (int i = 0; i < m; i++) { n     bool fl = false; n     for (int col = 1; col <= res; col++) { n       if (has[v[i]][col] == 0 && has[u[i]][col] == 0 && col <= res) { n         has[v[i]][col] = has[u[i]][col] = i + 1; n         e[i] = col; n         fl = true; n         break; n       } n     } n     if (!fl) { n       Cmp++; n       for (int col = 1; col <= res; col++) { n         if (has[v[i]][col] && !has[u[i]][col]) vc[0] = col; n         if (has[v[i]][col] == 0 && has[u[i]][col]) vc[1] = col; n       } n       dfs(v[i], 0); n       has[v[i]][vc[0]] = has[u[i]][vc[0]] = i + 1; n       e[i] = vc[0]; n     } n   } n   cout << res <<   n ; n   for (int i = 0; i < m; i++) cout << e[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int f[100005], b[100005], fn[100005], bn[100005], fm, bm; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; ++i) { n     scanf( %d , f + i); n     ++fn[f[i]]; n     fm = max(fm, f[i]); n   } n   for (int i = 0; i < m; ++i) { n     scanf( %d , b + i); n     ++bn[b[i]]; n     bm = max(bm, b[i]); n   } n   if (fm < bm) { n     printf( Impossible n ); n   } else { n     bool ans = true; n     bool chong = false; n     for (int i = 1; i <= bm; ++i) { n       if (bn[i] > 0) { n         if (fn[i] == 0) { n           ans = false; n         } else if (fn[i] > 1) { n           chong = true; n         } n       } n     } n     if (!ans) { n       printf( Impossible n ); n     } else { n       if (chong) { n         printf( Ambiguity n ); n       } else { n         printf( Possible n ); n         for (int i = 0; i < n; ++i) { n           fn[f[i]] = i + 1; n         } n         for (int i = 0; i < m; ++i) { n           printf( %d  , fn[b[i]]); n         } n         printf(  n ); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int k; n int n[3], t[3]; n multiset<int> f[3]; n int main() { n   cin >> k; n   cin >> n[0] >> n[1] >> n[2]; n   cin >> t[0] >> t[1] >> t[2]; n   for (int i = 0; i < 3; i++) n     for (int j = 0; j < n[i]; j++) f[i].insert(0); n   int ans = 0; n   for (int i = 0; i < k; i++) { n     int go[3]; n     for (int j = 0; j < 3; j++) { n       go[j] = *f[j].begin(); n       f[j].erase(f[j].begin()); n     } n     go[1] -= t[0]; n     go[2] -= t[0] + t[1]; n     int w = max(go[0], max(go[1], go[2])); n     ans = w + t[0] + t[1] + t[2]; n     f[0].insert(w + t[0]); n     f[1].insert(w + t[0] + t[1]); n     f[2].insert(ans); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n inline void read(int &x) { n   x = 0; n   char ch = getchar(), flag = 0; n   while ((ch >  9  || ch <  0 ) && ch !=  - ) ch = getchar(); n   if (ch ==  - ) flag = 1, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   if (flag) x = -x; n } n inline void read(int &x, int &y) { read(x), read(y); } n inline void read(int &x, int &y, int &z) { read(x), read(y), read(z); } n const int N = 20005, inf = 1e9 + 7, mod = 20000; n int n, m, up[N], cup[N], cnt, si[N], num[N], dep[N], use[N]; n vector<int> g[N], f[N], s[N]; n bool vis[N]; n struct node { n   int to, next, v; n } p[N << 4]; n int S, T, head[N], st[N], tot, ans; n inline void add(int x, int y, int z) { n   p[tot].to = y; n   p[tot].next = head[x]; n   p[tot].v = z; n   head[x] = tot++; n   p[tot].to = x; n   p[tot].next = head[y]; n   p[tot].v = 0; n   head[y] = tot++; n } n queue<int> q; n inline bool bfs(int x) { n   memset(st, 0, sizeof(st)); n   st[x] = 1; n   q.push(x); n   while (!q.empty()) { n     int x = q.front(); n     q.pop(); n     for (int i = head[x]; ~i; i = p[i].next) { n       int y = p[i].to, v = p[i].v; n       if (!v || st[y]) continue; n       st[y] = st[x] + 1; n       q.push(y); n     } n   } n   return st[T] > 0; n } n inline int dfs(int x, int f) { n   int r = 0; n   if (x == T) return f; n   for (int i = head[x]; ~i; i = p[i].next) { n     int y = p[i].to, v = p[i].v, tmp; n     if (!v || st[y] != st[x] + 1) continue; n     tmp = dfs(y, min(f - r, v)); n     r += tmp; n     p[i].v -= tmp; n     p[i ^ 1].v += tmp; n     if (r == f) break; n   } n   if (!r) st[x] = -1; n   return r; n } n inline void dinic() { n   int t; n   while (bfs(S)) n     while (t = dfs(S, inf)) ans += t; n   printf( %d n , ans); n } n inline void Add(int x, int y, int z) { n   g[x].push_back(y); n   f[x].push_back(z); n   g[y].push_back(x); n   f[y].push_back(z); n   vis[x] = vis[y] = 1; n } n inline void Dfs(int x, int fa) { n   vis[x] = 1; n   for (int i = 0; i < g[x].size(); i++) { n     int y = g[x][i], z = f[x][i]; n     if (y == fa) continue; n     if (vis[y] && dep[y] < dep[x]) { n       cnt++; n       s[cnt].push_back(z); n       si[z]--; n       int k = x, o = 0; n       while (k != y) { n         si[cup[k]]--; n         s[cnt].push_back(cup[k]); n         k = up[k]; n       } n       continue; n     } n     if (vis[y]) continue; n     cup[y] = z; n     up[y] = x; n     dep[y] = dep[x] + 1; n     Dfs(y, x); n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     int x, y, z; n     scanf( %d%d%d , &x, &y, &z); n     Add(x, y, z); n     si[z]++; n     use[z]++; n   } n   for (int i = 1; i <= n; i++) n     if (vis[i]) { n       memset(vis, 0, sizeof(vis)); n       Dfs(i, -1); n       break; n     } n   memset(head, -1, sizeof(head)); n   S = 0; n   T = cnt + 1; n   for (int i = 1; i <= m; i++) n     if (!si[i] && use[i]) n       T++; n     else if (use[i]) n       ans++; n   int CNT = cnt; n   for (int i = 1; i <= m; i++) n     if (!si[i] && use[i]) add(num[i] = ++CNT, T, 1); n   for (int i = 1; i <= cnt; i++) { n     bool isok = 1; n     for (int j = 0; j < s[i].size(); j++) n       if (si[s[i][j]]) isok = 0; n     add(S, i, s[i].size() - 1); n     for (int j = 0; j < s[i].size(); j++) n       if (num[s[i][j]]) add(i, num[s[i][j]], 1); n   } n   dinic(); 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 < 3) { n     cout << -1; n     return 0; n   } n   if (n == 3) { n     cout << 210; n     return 0; n   } n   int A[] = {50, 80, 170, 20, 200, 110}; n   int nA[] = {2, 2, 3, 2, 3, 3}; n   int j = 0; n   for (int i = 4; i <= n; i++) { n     if (j == 6) j = 0; n     if (i == n) { n       cout << 1; n       for (int k = 1; k < n - nA[j]; k++) { n         cout <<  0 ; n       } n       cout << A[j]; n     } n     j++; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[100100], sir[100100]; n int viz[100100], mi, ma, t, n, freq[100100], fre[100100]; n int prim(int x) { n   for (int i = 2; i <= x; ++i) { n     if (i * i > x) return 1; n     if (x % i == 0) return 0; n   } n   return 1; n } n int main() { n   cin >> (s + 1); n   n = strlen(s + 1); n   for (int i = 2; i <= n; ++i) { n     if (prim(i)) { n       mi = ++t; n       for (int j = 1; j <= n; ++j) { n         if (i * j > n) break; n         if (viz[i * j]) mi = min(mi, viz[i * j]); n       } n       if (mi != t) --t; n       for (int j = 1; j <= n; ++j) { n         if (i * j > n) break; n         viz[i * j] = mi; n       } n     } n   } n   for (int i = 1; i <= n; ++i) freq[viz[i]]++; n   for (int i = 1; i <= n; ++i) fre[s[i]]++; n   for (int i = 1; i <= t; ++i) { n     ma = 0; n     for (int j =  a ; j <=  z ; ++j) n       if (fre[j] > fre[ma]) { n         ma = j; n       } n     if (fre[ma] >= freq[i]) { n       fre[ma] -= freq[i]; n       for (int j = 1; j <= n; ++j) n         if (viz[j] == i) s[j] = ma; n     } else { n       cout <<  NO ; n       return 0; n     } n   } n   t = 0; n   for (int j =  a ; j <=  z ; ++j) n     while (fre[j]) { n       --fre[j]; n       sir[++t] = j; n     } n   for (int i = 1; i <= n; ++i) n     if (!viz[i]) { n       s[i] = sir[t--]; n     } n   cout <<  YES n ; n   cout << (s + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int n; n   cin >> n; n   long long int a[n]; n   long long int q = 0, p = 0, z = 0; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     if (a[i] < 0) n       q++; n     else if (a[i] > 0) n       p++; n     else n       z++; n   } n   long long int m1, m2, m3, m4, m5; n   sort(a, a + n); n   m1 = a[n - 1] * a[n - 2] * a[n - 3] * a[n - 4] * a[n - 5]; n   m2 = a[n - 1] * a[n - 2] * a[n - 3] * a[0] * a[1]; n   m3 = a[n - 1] * a[0] * a[1] * a[2] * a[3]; n   long long int ans = max(m1, max(m2, m3)); n   cout << ans <<   n ; n } n int main() { n   ; n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   int t; n   cin >> t; n   for (int i = 1; i <= t; ++i) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e4 + 10, inf = 1e9; n int r[N], c[N], dist[N]; n bool seen[N] = {0}; n int main() { n   int row, col, k; n   cin >> row >> col >> k; n   for (int i = 1; i <= k; i++) { n     cin >> r[i] >> c[i]; n     dist[i] = inf; n     if (r[i] == 1 and c[i] == 1) dist[i] = 0; n   } n   dist[0] = inf; n   while (1) { n     int cur = 0; n     for (int i = 1; i <= k; i++) { n       if (!seen[i] and dist[cur] > dist[i]) { n         cur = i; n       } n     } n     if (!cur) break; n     seen[cur] = 1; n     for (int i = 1; i <= k; i++) { n       if (seen[i]) continue; n       if (abs(r[i] - r[cur]) > 2 and abs(c[i] - c[cur]) > 2) continue; n       int temp = 1; n       if ((abs(r[i] - r[cur]) + abs(c[i] - c[cur])) == 1) temp = 0; n       if (dist[cur] + temp < dist[i]) dist[i] = temp + dist[cur]; n     } n   } n   int ans = inf; n   for (int i = 1; i <= k; i++) { n     if (r[i] == row and c[i] == col) n       ans = min(ans, dist[i]); n     else if (r[i] >= row - 1 or c[i] >= col - 1) n       ans = min(ans, dist[i] + 1); n   } n   if (ans == inf) n     cout << -1; n   else n     cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n long long int _sieve_size; n bitset<10000010> bs; n vector<int> primes; n void sieve(long long int upperbound) { n   _sieve_size = upperbound + 1; n   bs.set(); n   bs[0] = bs[1] = 0; n   for (long long int i = 2; i <= _sieve_size; i++) n     if (bs[i]) { n       for (long long int j = i * i; j <= _sieve_size; j += i) bs[j] = 0; n       primes.push_back((int)i); n     } n } n bool isPrime(long long int N) { n   if (N <= _sieve_size) return bs[N]; n   for (int i = 0; i < (int)primes.size(); i++) n     if (N % primes[i] == 0) return false; n   return true; n } n int main() { n   int h1, m1, h2, m2, oh, om; n   char tmp; n   cin >> h1 >> tmp >> m1; n   cin >> h2 >> tmp >> m2; n   om = m1 - m2; n   if (om < 0) { n     om += 60; n     h1 -= 1; n   } n   oh = h1 - h2; n   if (oh < 0) { n     oh += 24; n   } n   if (oh < 10) { n     cout << 0 << oh; n   } else n     cout << oh; n   cout << tmp; n   if (om < 10) { n     cout << 0 << om; n   } else n     cout << om; n   return 0; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4786) n #pragma comment(linker,  /STACK:16777216 ) n using namespace std; n char str[1000005]; n int Z[1000005], n; n void Zfunction() { n   int L, R, r, i, j, k; n   L = 0; n   R = 0; n   Z[0] = 0; n   for (i = 1; i < n; i++) { n     if (i > R) { n       L = i; n       R = i; n       while (R < n && str[R - L] == str[R]) R++; n       Z[i] = R - L; n       R--; n     } else { n       r = i - L; n       if (Z[r] < R - i + 1) n         Z[i] = Z[r]; n       else { n         L = i; n         while (R < n && str[R - L] == str[R]) R++; n         Z[i] = R - L; n         R--; n       } n     } n   } n } n int max(int a, int b) { n   if (a > b) return a; n   return b; n } n int main() { n   int i, j, k, maxr, res; n   while (scanf( %s , str) != EOF) { n     n = strlen(str); n     maxr = 0; n     res = 0; n     Zfunction(); n     for (i = 0; i < n; i++) { n       if (Z[i] == n - i) { n         if (maxr >= n - i) { n           res = n - i; n           break; n         } n       } n       maxr = max(maxr, Z[i]); n     } n     if (res == 0 || n <= 2) n       printf( Just a legend n ); n     else { n       for (i = 0; i < res; i++) printf( %c , str[i]); n       printf(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e4 + 1, M = 100; n int n, q; n bitset<N> sq[M]; n vector<int> on[N]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> q; n   for (int x = 0, qwerty = M; x < qwerty; x++) sq[x][0] = 1; n   for (int x = 0, qwerty = q; x < qwerty; x++) { n     int l, r, v; n     cin >> l >> r >> v; n     l--; n     r--; n     if (l / M == r / M) { n       for (int i = l; i <= r; i++) on[i].push_back(v); n       continue; n     } n     for (int i = (l / M) + 1; i < r / M; i++) { n       sq[i] |= (sq[i] << v); n     } n     for (int i = l; i < M * ((l / M) + 1); i++) on[i].push_back(v); n     for (int i = M * (r / M); i <= r; i++) on[i].push_back(v); n   } n   bitset<N> ans; n   for (int x = 0, qwerty = n; x < qwerty; x++) { n     int pos = x / M; n     bitset<N> tmp = sq[pos]; n     for (int v : on[x]) tmp |= tmp << v; n     ans |= tmp; n   } n   vector<int> aa; n   for (int x = 1, qwerty = n + 1; x < qwerty; x++) n     if (ans[x]) aa.push_back(x); n   cout << ((int)(aa).size()) <<   n ; n   for (int x = 0, qwerty = ((int)(aa).size()); x < qwerty; x++) n     cout << aa[x] <<    n [x + 1 == ((int)(aa).size())]; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<string> v; n   string a; n   while (n--) { n     cin >> a; n     v.push_back(a); n   } n   int cot = 0; n   bool p = 1; n   int ans = 1000000; n   string c; n   for (int j = 0; j < v.size(); j++) { n     c = v[j]; n     cot = 0; n     for (int i = 0; i < v.size(); i++) { n       if (c == v[i] || i == j) n         continue; n       else { n         int ncot = 0; n         string b = v[i]; n         while (c != b) { n           ncot++; n           char d = b[0]; n           b.erase(b.begin()); n           b.push_back(d); n           if (ncot > b.length()) { n             p = 0; n             break; n           } n         } n         if (p == 0) n           break; n         else n           cot += ncot; n       } n     } n     if (p == 0) break; n     if (cot < ans) { n       ans = cot; n     } n   } n   if (p) n     cout << ans << endl; n   else n     cout << -1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline bool equal(double _x, double _y) { n   return _x > _y - 1e-9 && _x < _y + 1e-9; n } n char s[300]; n int mx, cnt, sum, ans; n bool OK(char c) { n   if (c >=  a  && c <=  z  || c >=  A  && c <=  Z ) return 1; n   return 0; n } n int main() { n   int n; n   scanf( %d , &n); n   scanf( %s , s); n   for (int l = 0, r = 0; l < n; l = r) { n     if (s[l] ==  ( ) { n       while (s[r] !=  ) ) r++; n       for (int i = l + 1; i < r; i++) { n         if (OK(s[i]) && !OK(s[i - 1])) { n           cnt++; n         } n       } n       for (int i = l + 1; i < r; i++) s[i] =  _ ; n     } n     r++; n   } n   for (int i = 0; i < n; i++) { n     if (OK(s[i])) n       sum++; n     else n       sum = 0; n     ans = max(ans, sum); n   } n   printf( %d %d n , ans, cnt); n } n


#include <bits/stdc++.h> n using namespace std; n long long power(long long a, long long b) { n   if (b == 0) return 1; n   if (b % 2 == 0) return power(a * a % 1000000007, b / 2); n   return a * power(a, b - 1) % 1000000007; n } n long long gcd(long long a, long long b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n long long a[100000]; n int main() { n   long long n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   long long ans = 0; n   for (int i = 0; i < n / 2; i++) { n     ans += a[i] * a[n - i - 1] * 2; n     ans %= 10007; n   } n   if (n % 2) ans += a[n / 2] * a[n / 2]; n   ans %= 10007; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 101; n short dp[N][N][N * N], a[N], ans; n int n; n short calc(int u, int k, int sum) { n   if (u == N) return !sum && !k; n   short &ref = dp[u][k][sum]; n   if (ref != -1) return ref; n   ref = 0; n   for (short i = 0; i <= a[u] && sum - u * i >= 0 && i <= k; i++) n     ref += calc(u + 1, k - i, sum - u * i); n   return ref = min(ref, (short)2); n } n int main() { n   scanf( %d , &n); n   memset(dp, -1, sizeof dp); n   int freq = 0; n   for (int i = 0, a; i < n; i++) { n     scanf( %d , &a); n     if (!::a[a]) freq++; n     ::a[a]++; n   } n   for (short i = 1; i < N; i++) n     for (short j = 1; j <= a[i]; j++) n       if (calc(1, j, i * j) == 1) { n         ans = max(ans, j); n         if (j == a[i] && freq == 2) ans = n; n       } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300 * 1000 + 5, SQ = 373; n int a[N]; n long long dp[2 * N]; n pair<pair<int, int>, int> t[N]; n long long finans[N]; n int main() { n   ios ::sync_with_stdio(false); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   int q; n   cin >> q; n   for (int i = 1; i <= q; i++) { n     cin >> t[i].first.second >> t[i].first.first; n     t[i].second = i; n   } n   sort(t + 1, t + q + 1); n   for (int i = 1; i <= q; i++) { n     int x = t[i].first.first, y = t[i].first.second; n     if (x < SQ) { n       if (x != t[i - 1].first.first) { n         for (int j = n; j > 0; j--) { n           dp[j] = dp[j + x] + a[j]; n         } n       } n       finans[t[i].second] = dp[y]; n     } else { n       long long ans = 0; n       for (int j = y; j <= n; j += x) { n         ans += a[j]; n       } n       finans[t[i].second] = ans; n     } n   } n   for (int i = 1; i <= q; i++) { n     cout << finans[i] <<   n ; n   } n   return 0; n } n