#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 5; n const int inf = 0x3f3f3f3f; n int t, n; n int a[200]; n int main() { n   int n, q, t, k, d; n   int cnt; n   int sum; n   memset(a, 0, sizeof a); n   cin >> n >> q; n   for (int i = 0; i < q; i++) { n     cnt = 0; n     sum = 0; n     cin >> t >> k >> d; n     for (int j = 0; j < n; j++) { n       if (a[j] < t) cnt++; n     } n     if (cnt < k) n       cout <<  -1  << endl; n     else { n       for (int j = 0; j < n; j++) { n         if (a[j] < t) { n           k--; n           a[j] = t + d - 1; n           sum += j + 1; n         } n         if (k == 0) break; n       } n       cout << sum << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[110]; n int b[110]; n int main() { n   string s; n   getline(cin, s); n   int num = 0, ten = 1; n   for (int i = s.size() - 1; i >= 0; i--) { n     if (s[i] ==  = ) break; n     if (s[i] <=  9  && s[i] >=  0 ) { n       int x = s[i] -  0 ; n       num += x * ten; n       ten *= 10; n     } n   } n   int cnt = 0; n   a[0] = 1; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  + ) { n       a[++cnt] = 1; n     } else if (s[i] ==  - ) { n       a[++cnt] = 0; n     } n   } n   int x = 0; n   for (int i = 0; i <= cnt; i++) { n     b[i] = 1; n     if (a[i]) n       x += b[i]; n     else n       x -= b[i]; n   } n   x = num - x; n   for (int i = 0; i <= cnt; i++) { n     if (x < 0) { n       if (a[i] == 0 && b[i] < num) { n         int k = min(num - b[i], -x); n         b[i] += k; n         x += k; n       } n     } else if (x > 0) { n       if (a[i] == 1 && b[i] < num) { n         int k = min(num - b[i], x); n         b[i] += k; n         x -= k; n       } n     } else n       break; n   } n   x = 0; n   for (int i = 0; i <= cnt; i++) { n     if (a[i]) n       x += b[i]; n     else n       x -= b[i]; n   } n   if (x != num) { n     cout <<  Impossible  << endl; n     return 0; n   } n   cout <<  Possible  << endl; n   cnt = 0; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  +  || s[i] ==  -  || s[i] ==  = ) { n       cout << b[cnt++] <<     << s[i] <<    ; n     } n   } n   cout << num << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int n, x; n   cin >> n >> x; n   long long sum = 0; n   for (int i = 0; i < n; i++) { n     long long a; n     cin >> a; n     sum += a; n   } n   if (sum + n - 1 == x) return 0 * puts( YES ); n   puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC target( avx2 ) n #pragma GCC optimize( O3 ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n using ll = long long; n using lld = long double; n using vll = int64_t; n int main() { n   map<int, pair<int, int> > hm; n   int a[] = {4, 8, 15, 16, 23, 42}; n   for (int i = 0; i < 6; i++) { n     for (int j = i + 1; j < 6; j++) hm[a[i] * a[j]] = {a[i], a[j]}; n   } n   int ans[7]; n   cout <<  ?   n        <<  1 2 n  n        << flush; n   int x; n   cin >> x; n   auto p1 = hm[x]; n   cout <<  ? 2 3 n  << flush; n   cin >> x; n   auto p2 = hm[x]; n   if (p1.first == p2.first || p1.first == p2.second) { n     ans[2] = p1.first; n     ans[1] = p1.second; n     if (p1.first == p2.first) { n       ans[3] = p2.second; n     } else { n       ans[3] = p2.first; n     } n   } else { n     ans[2] = p1.second; n     ans[1] = p1.first; n     if (p1.second == p2.first) n       ans[3] = p2.second; n     else n       ans[3] = p2.first; n   } n   cout <<  ? 4 5 n  << flush; n   cin >> x; n   p1 = hm[x]; n   cout <<  ? 5 6 n  << flush; n   cin >> x; n   p2 = hm[x]; n   if (p1.first == p2.first || p1.first == p2.second) { n     ans[5] = p1.first; n     ans[4] = p1.second; n     if (p1.first == p2.first) { n       ans[6] = p2.second; n     } else { n       ans[6] = p2.first; n     } n   } else { n     ans[5] = p1.second; n     ans[4] = p1.first; n     if (p1.second == p2.first) n       ans[6] = p2.second; n     else n       ans[6] = p2.first; n   } n   cout <<  !  ; n   for (int i = 1; i < 7; i++) cout << ans[i] <<    ; n   cout <<   n  << flush; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string s; n int dp[111][55][222][3]; n int res; n void dfs(int pos, int left, int x, int dx) { n   if (dp[pos][left][x + 100][dx + 1]) return; n   dp[pos][left][x + 100][dx + 1] = 1; n   if (pos == s.size()) { n     if (left == 0) res = max(res, abs(x)); n     return; n   } n   for (int i = 0; i <= left; i++) { n     int ddx = (s[pos] ==  F ); n     if (i % 2 == 1) ddx = !ddx; n     dfs(pos + 1, left - i, x + (ddx == 1 ? dx : 0), dx * (ddx == 0 ? -1 : 1)); n   } n } n int main() { n   cin >> s; n   cin >> n; n   dfs(0, n, 0, 1); n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int hv[5]; n map<string, int> dys; n string dyi[5] = { S ,  M ,  L ,  XL ,  XXL }; n int order[5][5] = {{0, 1, 2, 3, 4}, n                    {1, 2, 0, 3, 4}, n                    {2, 3, 1, 4, 0}, n                    {3, 4, 2, 1, 0}, n                    {4, 3, 2, 1, 0}}; n int k; n string sk; n void init() { n   dys[ S ] = 0; n   dys[ M ] = 1; n   dys[ L ] = 2; n   dys[ XL ] = 3; n   dys[ XXL ] = 4; n } n int main() { n   init(); n   for (int i = 0; i < 5; i++) scanf( %d , hv + i); n   scanf( %d , &k); n   for (int i = 0; i < k; i++) { n     cin >> sk; n     int dyk = dys[sk]; n     for (int j = 0; j < 5; j++) n       if (hv[order[dyk][j]]) { n         hv[order[dyk][j]]--; n         cout << dyi[order[dyk][j]] << endl; n         break; n       } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, a, k = 0; n   vector<int> v; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> a; n     v.push_back(a); n   } n   sort(v.begin(), v.end(), greater<int>()); n   for (i = 0; i <= n - 3; i++) { n     if (v[i] < v[i + 1] + v[i + 2]) k++; n   } n   if (k > 0) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   double l, p, q; n   cin >> l >> p >> q; n   cout << setprecision(9) << (p * l) / (p + q); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   vector<int> result; n   for (int i = 0; i < t; i++) { n     int b, p, f, h, c; n     cin >> b >> p >> f >> h >> c; n     int res = 0; n     b /= 2; n     if (h > c) { n       if (b - p > 0) { n         res += p * h; n         b -= p; n       } else { n         res += b * h; n         b = 0; n       } n       if (b > 0) { n         if (b > f) { n           res += f * c; n           b = 0; n         } else { n           res += b * c; n           b = 0; n         } n       } n     } else if (c > h) { n       if (b - f > 0) { n         res += f * c; n         b -= f; n       } else { n         res += b * c; n         b = 0; n       } n       if (b > 0) { n         if (b > p) { n           res += p * h; n           b = 0; n         } else { n           res += b * h; n           b = 0; n         } n       } n     } else { n       if (f + p <= b) n         res += (f + p) * h; n       else n         res += b * h; n     } n     result.push_back(res); n   } n   for (int i = 0; i < t; i++) cout << result.at(i) << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long x; n int getAdded(long long n) { n   int lo = 1, hi = 10000000; n   while (lo < hi) { n     long long mid = (lo + hi) / 2 + (lo + hi) % 2; n     long long temp = (mid * mid + mid) / 2; n     if (temp == n) n       return mid; n     else if (temp < n) n       lo = mid; n     else n       hi = mid - 1; n   } n   return lo; n } n bool isPossible(int h, int mod, long long n) { n   long long temp = x + h; n   return getAdded(temp) >= h * 3 + mod; n } n int main() { n   long long n; n   cin >> n; n   int mod = (n % 3 - 3) * -1; n   int lo = 0, hi = 10000000; n   x = (n + mod) / 3; n   while (lo < hi) { n     int mid = (lo + hi) / 2 + (lo + hi) % 2; n     if (isPossible(mid, mod, n)) n       lo = mid; n     else n       hi = mid - 1; n   } n   if (!isPossible(lo, mod, n)) n     cout << 0; n   else n     cout << lo + 1; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:100000000000,100000000000 ) n using namespace std; n const long long inf = 1e18 + 7; n const long long mod = 1e9 + 7; n const double eps = 1e-12; n const double PI = 2 * acos(0.0); n const double E = 2.71828; n int n; n long long a[1000005]; n long long d[1000005], sz; n long long cnt[1000005]; n long long res = 1; n long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); } n void up(long long x) { n   sz = 0; n   for (long long i = 1; i * i <= x; ++i) { n     if (x % i == 0) { n       d[sz++] = i; n       if (i * i != x) { n         d[sz++] = x / i; n       } n     } n   } n   sort(d, d + sz); n   for (long long(i) = 0; (i) < (long long)(sz); (i)++) { n     cnt[i] = 0; n   } n   for (long long(i) = 0; (i) < (long long)(n); (i)++) { n     long long cur = gcd(x, a[i]); n     int num = lower_bound(d, d + sz, cur) - d; n     ++cnt[num]; n   } n   for (long long(i) = 0; (i) < (long long)(sz); (i)++) { n     for (int j = i + 1; j < sz; ++j) { n       if (d[j] % d[i] == 0) { n         cnt[i] += cnt[j]; n       } n     } n     if (cnt[i] + cnt[i] >= n && res < d[i]) { n       res = d[i]; n     } n   } n } n int main(void) { n   srand(time(NULL)); n   scanf( %d , &n); n   for (long long(i) = 0; (i) < (long long)(n); (i)++) { n     scanf( %I64d , a + i); n   } n   for (long long(i) = 0; (i) < (long long)(10); (i)++) { n     up(a[(rand() << 16 | rand()) % n]); n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using u32 = unsigned int; n using u64 = unsigned long long; n char buf[10]; n void NO() { n   puts( Impossible ); n   exit(0); n } n ll update(ll x, ll c) { n   if (x == LLONG_MAX || x == LLONG_MIN) return x; n   return x + c; n } n int main() { n   int n; n   scanf( %d , &n); n   ll max_r = LLONG_MAX; n   ll min_r = LLONG_MIN; n   for (int i = (0); i < (n); ++i) { n     ll c_i, d_i; n     scanf( %lld%lld , &c_i, &d_i); n     ; n     if (d_i == 2) { n       if (min_r >= 1900) NO(); n       if (max_r >= 1900) max_r = 1899; n     } else if (d_i == 1) { n       if (max_r < 1900) NO(); n       if (min_r < 1900) min_r = 1900; n     } n     max_r = update(max_r, c_i); n     min_r = update(min_r, c_i); n   } n   if (max_r == LLONG_MAX) n     puts( Infinity ); n   else n     printf( %lld , max_r); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> g[300005]; n int cnt[300005]; n int vis[300005]; n int ans[300001][26]; n int res = 0; n int main() { n   queue<int> q; n   int n, m; n   cin >> n >> m; n   string s; n   cin >> s; n   int x, y; n   while (m--) { n     scanf( %d%d , &x, &y); n     x--, y--; n     g[x].push_back(y); n     cnt[y]++; n   } n   for (int i = 0; i < s.size(); i++) { n     ans[i][s[i] -  a ]++; n     if (!cnt[i]) q.push(i); n   } n   while (!q.empty()) { n     int i = q.front(); n     q.pop(); n     for (auto v : g[i]) { n       cnt[v]--; n       for (int j = 0; j < 26; j++) { n         ans[v][j] = max(ans[v][j], ans[i][j] + (s[v] == j +  a )); n         res = max(res, ans[v][j]); n       } n       if (!cnt[v]) q.push(v); n     } n   } n   if (*max_element(cnt, cnt + s.size())) return cout << -1, 0; n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int c[4100]; n int s[4100]; n vector<long long> g; n string str; n int main() { n   long long a; n   cin >> a; n   cin >> str; n   int len = str.length(); n   for (int i = 0; str[i]; i++) c[i + 1] = str[i] -  0 ; n   for (int i = 1; i <= len; i++) { n     s[i] = s[i - 1] + (long long)c[i]; n   } n   for (int i = 0; i <= len; i++) n     for (int j = i + 1; j <= len; j++) g.push_back(s[j] - s[i]); n   sort(g.begin(), g.end()); n   int sz = g.size(), sz1; n   sz1 = upper_bound(g.begin(), g.end(), 0) - lower_bound(g.begin(), g.end(), 0); n   long long ans = 0; n   for (int i = 0; i < sz; i++) { n     if (g[i] == 0) { n       if (a == 0) ans += sz; n       continue; n     } n     if (a % g[i] == 0 && n         *lower_bound(g.begin(), g.end(), a / g[i]) == a / g[i]) { n       if (a == 0) { n         ans += sz1; n         continue; n       } n       ans += upper_bound(g.begin(), g.end(), a / g[i]) - n              lower_bound(g.begin(), g.end(), a / g[i]); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int l[100005], r[100005], q[100005]; n struct node { n   int v, add; n }; n node t[4 * 100005]; n const int MX = (1LL << 31) - 1; n void Push(int v, int tl, int tm, int tr) { n   if (t[v].add != 0) { n     t[2 * v].add |= t[v].add; n     t[2 * v].v |= t[v].add; n     t[2 * v + 1].add |= t[v].add; n     t[2 * v + 1].v |= t[v].add; n     t[v].add = 0; n   } n } n void Update(int v, int tl, int tr, int l, int r, int value) { n   if (l > r) return; n   if ((tl == l) && (tr == r)) { n     t[v].add |= value; n     t[v].v |= value; n     return; n   } n   int tm = (tl + tr) / 2; n   Push(v, tl, tm, tr); n   Update(2 * v, tl, tm, l, min(tm, r), value); n   Update(2 * v + 1, tm + 1, tr, max(l, tm + 1), r, value); n   t[v].v &= t[2 * v].v; n   t[v].v &= t[2 * v + 1].v; n } n int Get(int v, int tl, int tr, int l, int r) { n   if (l > r) return MX; n   if ((tl == l) && (tr == r)) return t[v].v; n   int tm = (tl + tr) / 2; n   Push(v, tl, tm, tr); n   return Get(2 * v, tl, tm, l, min(tm, r)) & n          Get(2 * v + 1, tm + 1, tr, max(l, tm + 1), r); n } n int main() { n   scanf( %d %d , &n, &m); n   for (int(i) = (0); (i) < int(m); ++(i)) { n     scanf( %d %d %d , l + i, r + i, q + i); n     Update(1, 0, n - 1, l[i] - 1, r[i] - 1, q[i]); n   } n   bool Ok = 1; n   for (int(i) = (0); (i) < int(m); ++(i)) { n     int x = Get(1, 0, n - 1, l[i] - 1, r[i] - 1); n     if (x != q[i]) { n       Ok = 0; n       break; n     } n   } n   if (!Ok) { n     puts( NO ); n     cin.sync(); n     cin.get(); n     return 0; n   } else { n     puts( YES ); n     for (int(i) = (0); (i) < int(n); ++(i)) { n       printf( %d  , Get(1, 0, n - 1, i, i)); n     } n   } n   cin.sync(); n   cin.get(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int n = s.length(), cur = 1, best = 1; n   s += s; n   for (size_t i = 1; i < s.size(); i++) n     if (s[i] != s[i - 1]) n       cur++; n     else { n       best = max(best, cur); n       cur = 1; n     } n   best = max(best, cur); n   cout << min(n, best); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 10; n struct Segment { n   int left, right; n } seg[N / 2 + 5]; n map<int, bool> m; n int a[N]; n int n, ans; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   m.clear(); n   ans = 1; n   seg[ans].left = 1; n   m[a[1]] = true; n   int i = 2; n   while (i <= n) { n     if (m[a[i]]) { n       seg[ans].right = i; n       if (i == n) break; n       ans++; n       i++; n       seg[ans].left = i; n       m.clear(); n     } n     m[a[i]] = true; n     i++; n   } n   if (i > n) { n     if (ans == 1) { n       printf( -1 n ); n       return 0; n     } n     ans--; n     seg[ans].right = n; n   } n   printf( %d n , ans); n   for (int i = 1; i <= ans; i++) printf( %d %d n , seg[i].left, seg[i].right); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string CHECK(long long n) { n   long long t, temp = n, zero = 0, ans = 0, flag = 0; n   while (temp != 0) { n     t = temp % 10; n     if (t == 0 && flag == 0) n       zero++; n     else { n       flag = 1; n       ans *= 10; n       ans += t; n     } n     temp /= 10; n   } n   if (zero != 0) { n     while (zero != 0) { n       ans *= 10; n       zero--; n     } n   } n   if (n == ans) n     return  YES ; n   else n     return  NO ; n } n int main() { n   long long n; n   cin >> n; n   cout << CHECK(n) << endl; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long a[1000050]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   long long rec = 0; n   long long num = a[0]; n   long long have = 0; n   long long can = 0; n   for (int i = 0; i < n; i++) { n     if (num == a[i]) have++; n     if (num != a[i]) { n       can++; n       if (a[i + 1] != a[i]) { n         num = a[i]; n         rec += min(have, can); n         have = have + can - min(have, can); n         can = 0; n       } n     } n   } n   rec = n - have; n   cout << rec << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     vector<vector<int>> p(n + 1, vector<int>(m + 1, 0)); n     vector<int> ma(m + 1); n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= m; j++) { n         cin >> p[i][j]; n         if (p[i][j] > p[ma[j]][j]) ma[j] = i; n       } n     set<int> s; n     for (int i = 1; i <= m; i++) s.insert(ma[i]); n     if (s.size() == m) { n       int ans = 0; n       for (int i = 1; i <= m; i++) { n         for (int j = 1; j <= m; j++) { n           if (i == j) continue; n           int tem = 0; n           for (int k = 1; k <= n; k++) { n             tem = max(tem, min(p[k][i], p[k][j])); n           } n           for (int k = 1; k <= m; k++) { n             if (k == i || k == j) continue; n             tem = min(tem, p[ma[k]][k]); n           } n           ans = max(ans, tem); n         } n       } n       cout << ans <<   n ; n     } else { n       int ans = 1e9; n       for (int i = 1; i <= m; i++) { n         ans = min(ans, p[ma[i]][i]); n       } n       cout << ans <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n bool dfs(int x, const vector<vector<int>> &g, vector<char> &used, n          vector<int> &match, vector<int> &rev_match) { n   if (used[x]) return false; n   used[x] = 1; n   for (int dest : g[x]) { n     if (match[dest] == -1 || dfs(match[dest], g, used, match, rev_match)) { n       match[dest] = x; n       rev_match[x] = dest; n       return true; n     } n   } n   return false; n } n int matching(const vector<vector<int>> &g, int left, int right) { n   vector<char> used(left); n   vector<int> rev_match(left, -1), match(right, -1); n   while (true) { n     bool ok = false; n     fill(used.begin(), used.end(), 0); n     for (int i = 0; i < left; i++) n       if (rev_match[i] == -1 && !used[i]) n         ok |= dfs(i, g, used, match, rev_match); n     if (!ok) break; n   } n   return left - count(rev_match.begin(), rev_match.end(), -1); n } n void solve(istream &cin = std::cin, ostream &cout = std::cout) { n   int n, m; n   cin >> n >> m; n   const int inf = int(1e9) + 1; n   vector<vector<int>> g(n, vector<int>(n, inf)); n   for (int i = 0; i < n; i++) g[i][i] = 0; n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     --u, --v; n     g[u][v] = g[v][u] = 1; n   } n   for (int k = 0; k < n; k++) n     for (int i = 0; i < n; i++) n       for (int j = 0; j < n; j++) g[i][j] = min(g[i][j], g[i][k] + g[k][j]); n   int s, b, k, h; n   cin >> s >> b >> k >> h; n   vector<int> pos_s(s), fuel(s), atk(s); n   vector<int> pos_b(b), def(b); n   for (int i = 0; i < s; i++) { n     cin >> pos_s[i] >> atk[i] >> fuel[i]; n     --pos_s[i]; n   } n   for (int i = 0; i < b; i++) { n     cin >> pos_b[i] >> def[i]; n     --pos_b[i]; n   } n   vector<vector<int>> graph(s); n   for (int i = 0; i < s; i++) n     for (int j = 0; j < b; j++) n       if (g[pos_s[i]][pos_b[j]] <= fuel[i] && def[j] <= atk[i]) n         graph[i].push_back(j); n   const int len = matching(graph, s, b); n   ll ans = min((ll)len * k, (ll)s * h); n   cout << ans << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout << fixed; n   cout << setprecision(20); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = (long long)(1000000007); n const double eps = 1e-9; n const double pi = 3.1415926535897932384626; n long long gcd(long long a, long long b) { return (b ? gcd(b, a % b) : a); } n bool isprime(int n) { n   if (n == 1) return false; n   for (long long i = 2; i <= sqrt(n); i++) { n     if (n % i == 0) return false; n   } n   return true; n } n int modExp(int b, int e, int m) { n   int x = 1; n   while (e != 0) { n     if (e % 2 == 1) x = (x * b) % m; n     b = (b * b) % m; n     e = e / 2; n   } n   return x; n } n unsigned long long fact(unsigned long long n) { n   unsigned long long ret = 1; n   for (long long i = 1; i <= n; i++) { n     ret *= i; n   } n   return ret; n } n int ans = 0; n int main() { n   long long n; n   cin >> n; n   vector<pair<long long, long long> > vp; n   for (long long i = 0; i <= n - 1; i++) { n     int a, b; n     cin >> a >> b; n     vp.push_back(make_pair(a, b)); n   } n   sort(vp.begin(), vp.end()); n   ans = vp[0].second; n   for (long long i = 1; i <= n - 1; i++) { n     if (vp[i].second >= ans) n       ans = vp[i].second; n     else n       ans = vp[i].first; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long t, n, i, j, k, x, y, z, ans, l, r, mid, s, q; n long long a[100005], b[100005], c[100005], d[100005], p[100005], dp[100005][20]; n vector<long long> v[100005], u[100005]; n map<pair<int, int>, int> m; n void dfs(int x, int y, int z) { n   dp[x][0] = y; n   d[x] = z; n   p[x] = y; n   u[z].push_back(x); n   for (int i = 0; i < v[x].size(); i++) n     if (v[x][i] != y) dfs(v[x][i], x, z + 1); n } n int lca(int x, int y) { n   if (d[x] < d[y]) swap(x, y); n   for (int i = 18; i >= 0; i--) n     if (d[x] - (1 << i) >= d[y]) x = dp[x][i]; n   if (x == y) return x; n   for (int i = 18; i >= 0; i--) n     if (dp[x][i] != dp[y][i]) { n       x = dp[x][i]; n       y = dp[y][i]; n     } n   return dp[x][0]; n } n void build() { n   dfs(1, 0, 0); n   for (i = 1; i <= 18; i++) n     for (j = 1; j <= n; j++) dp[j][i] = dp[dp[j][i - 1]][i - 1]; n } n int main() { n   ios::sync_with_stdio(NULL); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (i = 1; i < n; i++) { n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n     b[i] = x; n     c[i] = y; n     m[{x, y}] = m[{y, x}] = i; n   } n   build(); n   cin >> q; n   for (i = 0; i < q; i++) { n     cin >> x >> y; n     if (d[x] < d[y]) swap(x, y); n     z = lca(x, y); n     a[x]++; n     a[y]++; n     a[z] -= 2; n   } n   for (i = 100000; i > 0; i--) n     for (j = 0; j < u[i].size(); j++) { n       x = u[i][j]; n       m[{x, p[x]}] = m[{p[x], x}] = a[x]; n       a[p[x]] += a[x]; n     } n   for (i = 1; i < n; i++) cout << m[{b[i], c[i]}] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int mi[n]; n   for (int i = (0); i < (n); i++) mi[i] = 1e9; n   int t = 1; n   int z[1000]; n   do { n     t *= 2; n     vector<int> v; n     int x = 0; n     while (x < n) { n       int f = 0; n       for (int i = (0); i < (t / 2); i++) { n         if (x + i < n) n           v.push_back(x + i); n         else n           break; n       } n       x += t; n     } n     cout << v.size() <<   n ; n     ; n     set<int> temp; n     for (int i = (0); i < (v.size()); i++) { n       temp.insert(v[i]); n       cout << v[i] + 1 <<    ; n     } n     cout <<   n ; n     fflush(stdout); n     for (int i = (0); i < (n); i++) cin >> z[i]; n     for (int i = (0); i < (n); i++) { n       if (temp.find(i) == temp.end()) mi[i] = min(mi[i], z[i]); n     } n     v.resize(0); n     x = 0; n     temp.clear(); n     while (x < n) { n       int f = 0; n       for (int i = (0); i < (t / 2); i++) { n         if (x + i + t / 2 < n) n           v.push_back(x + i + t / 2); n         else n           break; n       } n       x += t; n     } n     cout << v.size() <<   n ; n     ; n     for (int i = (0); i < (v.size()); i++) { n       temp.insert(v[i]); n       cout << v[i] + 1 <<    ; n     } n     cout <<   n ; n     fflush(stdout); n     for (int i = (0); i < (n); i++) cin >> z[i]; n     for (int i = (0); i < (n); i++) { n       if (temp.find(i) == temp.end()) mi[i] = min(mi[i], z[i]); n     } n   } while (t < n); n   cout <<  -1 n ; n   fflush(stdout); n   for (int i = (0); i < (n); i++) cout << mi[i] <<    ; n   cout <<   n ; n   fflush(stdout); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int sze = n * 2; n     int arr[sze]; n     for (int i = 0; i < sze; i++) cin >> arr[i]; n     sort(arr, arr + sze); n     cout << arr[n] - arr[n - 1] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int n, m; n int a[202], b[202]; n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i]); n   } n   for (int i = 0; i < m; ++i) { n     scanf( %d , &b[i]); n   } n   for (int w = 0; w <= 1 << 9; ++w) { n     int o = 0; n     for (int i = 0; i < n; ++i) { n       int mx = -1; n       for (int j = 0; j < m; ++j) { n         if ((w & (a[i] & b[j])) == (a[i] & b[j])) { n           mx = std::max(a[i] & b[j], mx); n         } n       } n       if (mx == -1) { n         o = -1; n         break; n       } n       o |= mx; n     } n     if (o == w) { n       printf( %d n , w); n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long is_query = -(1LL << 62); n struct line { n   long long m, b; n   mutable function<const line*()> succ; n   bool operator<(const line& rhs) const { n     if (rhs.b != is_query) return m < rhs.m; n     const line* s = succ(); n     if (!s) return 0; n     long long x = rhs.m; n     return b - s->b < (s->m - m) * x; n   } n }; n struct dynamic_hull : public multiset<line> { n   const long long inf = LLONG_MAX; n   bool bad(iterator y) { n     auto z = next(y); n     if (y == begin()) { n       if (z == end()) return 0; n       return y->m == z->m && y->b <= z->b; n     } n     auto x = prev(y); n     if (z == end()) return y->m == x->m && y->b <= x->b; n     long long v1 = (x->b - y->b); n     if (y->m == x->m) n       v1 = x->b > y->b ? inf : -inf; n     else n       v1 /= (y->m - x->m); n     long long v2 = (y->b - z->b); n     if (z->m == y->m) n       v2 = y->b > z->b ? inf : -inf; n     else n       v2 /= (z->m - y->m); n     return v1 >= v2; n   } n   void insert_line(long long m, long long b) { n     auto y = insert({m, b}); n     y->succ = [=] { return next(y) == end() ? 0 : &*next(y); }; n     if (bad(y)) { n       erase(y); n       return; n     } n     while (next(y) != end() && bad(next(y))) erase(next(y)); n     while (y != begin() && bad(prev(y))) erase(prev(y)); n   } n   __int128 eval(long long x) { n     auto l = *lower_bound(line{x, is_query}); n     return (__int128)l.m * x + l.b; n   } n }; n int main() { n   int n; n   long long s; n   cin >> n >> s; n   vector<pair<long long, long long>> buildings(n); n   for (int i = 0; i < n; ++i) { n     cin >> buildings[i].second >> buildings[i].first; n   } n   sort(buildings.begin(), buildings.end()); n   assert(buildings[0].first == 0); n   vector<long long> min_time(n + 1, s + 1); n   vector<long long> max_cookies(n + 1, 0); n   min_time[0] = 0; n   max_cookies[0] = 0; n   dynamic_hull dp_opt; n   dp_opt.insert_line(buildings[0].second, 0); n   for (int i = 1; i < n; ++i) { n     long long left = min_time[i - 1]; n     long long right = s + 50; n     while (left + 1 < right) { n       long long mid = (left + right) / 2; n       if (dp_opt.eval(mid) >= buildings[i].first) { n         right = mid; n       } else { n         left = mid; n       } n     } n     if (dp_opt.eval(left) - buildings[i].first < 0) { n       ++left; n     } n     min_time[i] = left; n     max_cookies[i] = dp_opt.eval(left) - buildings[i].first; n     dp_opt.insert_line( n         buildings[i].second, n         max_cookies[i] - 1LL * buildings[i].second * min_time[i]); n   } n   long long ans = s; n   for (int i = 0; i < n; ++i) { n     ans = min(ans, n               min_time[i] + (s - max_cookies[i] - 1) / buildings[i].second + 1); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void read(T& x) { n   bool fu = 0; n   char c; n   for (c = getchar(); c <= 32; c = getchar()) n     ; n   if (c ==  - ) fu = 1, c = getchar(); n   for (x = 0; c > 32; c = getchar()) x = x * 10 + c -  0 ; n   if (fu) x = -x; n }; n template <class T> n inline void read(T& x, T& y) { n   read(x); n   read(y); n } n template <class T> n inline void read(T& x, T& y, T& z) { n   read(x); n   read(y); n   read(z); n } n inline char getc() { n   char c; n   for (c = getchar(); c <= 32; c = getchar()) n     ; n   return c; n } n const int N = 10010; n const double pi = 2 * acos(0); n const double eps = 1e-9; n int w, h, n, m, i, j, st, g; n double L0, R0, R; n long long x[N], y[N]; n double d[1010][1010], an[1010][1010]; n int p[N]; n double c[N]; n double D(int i, int j) { n   return sqrt((1LL * (x[i] - x[j]) * (x[i] - x[j])) + n               (1LL * (y[i] - y[j]) * (y[i] - y[j]))); n } n double geta(int i, int j) { return atan2(y[j] - y[i], x[j] - x[i]); } n bool same(double a, double b) { return abs(a - b) <= eps; } n double ans; n void add(double L, double R) { n   if (R > pi) { n     add(L, pi); n     add(-pi, R - 2 * pi); n   } else if (L < -pi) { n     add(L + 2 * pi, pi); n     add(-pi, R); n   } else n     c[++g] = L - 1e-15, c[++g] = R + 1e-15; n } n bool cmp(int x, int y) { return c[x] < c[y]; } n bool ok(double R) { n   int i, j, k; n   double tt; n   g = 0; n   c[++g] = -pi; n   c[++g] = pi; n   if (x[st] < R) { n     tt = acos(x[st] / R); n     for (k = 1; k <= 2; k++) add(-pi - tt, -pi + tt); n   } n   if (w - x[st] < R) { n     tt = acos((w - x[st]) / R); n     for (k = 1; k <= 2; k++) add(-tt, tt); n   } n   if (y[st] < R) { n     tt = acos(y[st] / R); n     for (k = 1; k <= 2; k++) add(-pi / 2 - tt, -pi / 2 + tt); n   } n   if (h - y[st] < R) { n     tt = acos((h - y[st]) / R); n     for (k = 1; k <= 2; k++) add(pi / 2 - tt, pi / 2 + tt); n   } n   for (i = 1; i <= n; i++) n     if (i != st && d[st][i] < 2 * R && d[st][i] > eps) { n       tt = acos(d[st][i] / 2 / R); n       add(an[st][i] - tt, an[st][i] + tt); n     } n   for (i = 1; i <= g; i++) p[i] = i; n   sort(p + 1, p + 1 + g, cmp); n   int s = 0; n   for (i = 1; i <= g; i++) n     if ((s += (p[i] & 1 ? 1 : -1)) < 3 && i + 1 <= g && n         c[p[i + 1]] - c[p[i]] > eps) n       return 1; n   return 0; n } n int main() { n   read(w, h, n); n   for (i = 1; i <= n; i++) cin >> x[i] >> y[i]; n   for (i = 1; i <= n; i++) { n     int t = rand() % i + 1; n     swap(x[i], x[t]); n     swap(y[i], y[t]); n   } n   for (i = 1; i <= n; i++) n     for (j = 1; j <= n; j++) d[i][j] = D(i, j), an[i][j] = geta(i, j); n   for (st = 1; st <= n; st++) n     if (ok(R)) { n       L0 = R; n       R0 = 2e6; n       while (L0 + eps < R0) { n         R = (L0 + R0) / 2; n         if (ok(R)) n           L0 = R; n         else n           R0 = R; n       } n     } n   cout << fixed << setprecision(10) << R << endl; n   scanf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 1e5 + 1; n const long long MOD = 1e9 + 7; n const long long INF = 1e9; n void solve(long long a, long long b) { n   if (a == 0 and b == 0) { n     cout <<  0  << endl; n   } else if (a == b) { n     cout <<  1  << endl; n   } else if (abs(a - b) % 2 == 1) { n     cout <<  -1  << endl; n   } else { n     cout <<  2  << endl; n   } n   return; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int tc = 1; n   cin >> tc; n   for (int t = 1; t <= tc; t++) { n     long long a, b; n     cin >> a >> b; n     solve(a, b); n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int tt; n   cin >> tt; n   while (tt--) { n     int a, b, c, saveb = 0; n     string st, md, en; n     st = md = en =   ; n     cin >> a >> b >> c; n     bool ub = false; n     while (b) { n       if (!ub) { n         md +=  01 ; n         b--; n         ub = true; n       } else { n         if (b == 1 && md[md.size() - 1] ==  1 ) { n           saveb = 1; n           b--; n         } else { n           md += md[md.size() - 1] ^ 1; n           b--; n         } n       } n     } n     if (a) { n       if (!md.size()) n         st +=  00 ; n       else n         st +=  0 ; n       a--; n     } n     for (int i = 1; i <= a; i++) { n       st +=  0 ; n     } n     if (c) { n       if (!md.size()) n         en +=  11 ; n       else n         en +=  1 ; n       c--; n     } n     for (int i = 1; i <= c; i++) { n       en +=  1 ; n     } n     if (saveb > 0) cout << 1; n     cout << st << md << en <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   cout << s; n   reverse(s.begin(), s.end()); n   cout << s; n } n


#include <bits/stdc++.h> n using namespace std; n void CountTriangles(vector<int> A) { n   int n = A.size(); n   sort(A.begin(), A.end()); n   int count = 0; n   for (int i = n - 1; i >= 1; i--) { n     int l = 0, r = i - 1; n     while (l < r) { n       if (A[l] + A[r] > A[i]) { n         count += r - l; n         r--; n       } else n         l++; n     } n   } n   cout <<  No of possible solutions:   << count; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int a, b; n   cin >> a >> b; n   if (a >= b) { n     cout << (b - 1) / 2 << endl; n   } else { n     long long int c = (2 * a - b + 1) / 2; n     cout << max(0LL, c); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:64000000 ) n using namespace std; n int n, b[110], l, r, c, t, q[110], m; n int main() { n   cin >> m >> n; n   for (int i = 1; i <= m; i++) b[i] = 1 << 30; n   for (int i = 0; i < n; i++) { n     cin >> l >> r >> t >> c; n     for (int j = l; j <= r; j++) { n       if (t < b[j]) b[j] = t, q[j] = c; n     } n   } n   int s = 0; n   for (int i = 1; i <= m; i++) { n     s += q[i]; n   } n   cout << s; n   return 0; n } n


#include <bits/stdc++.h> n #define pb push_back n #define mp make_pair n #define sz(a) ((int)a.size()) n #define re return n #define all(a) a.begin(),a.end() n #define int long long n #define ld long double n #define rept(i,a,b) for(int i=(a);i<(b);i++) n #define rep(i,a) rept(i,0,a) n using namespace std; n void fail(string s){ cout<<s;exit(0); } n void fail(int s){ cout<<s;exit(0); } n void chmax(ld &a,ld b,pair<int,int> &c,pair<int,int> d){ if (b>a) a=b,c=d; } n int n,d; n int a[100005]; n ld dp[100005][10]; n ld lg[100005]; n pair<int,int>l[100005][10]; n signed main(){ n  cin>>n>>d; n  rept(i,1,n+1){ n   cin>>a[i]; n   lg[i]=log2(a[i]); n  } n  rep(i,n+1) rep(j,10) dp[i][j]=-1.0; n  dp[0][1]=0.0; n  rep(i,n){ n   rep(j,10){ n    if (dp[i][j]>=0.0){ n     chmax(dp[i+1][j],dp[i][j],l[i+1][j],mp(0,j)); n     chmax(dp[i+1][j*a[i+1]%10],dp[i][j]+lg[i+1],l[i+1][j*a[i+1]%10],mp(1,j)); n    } n   } n  } n  int pos=-1; n  ld ans=0.0;  n  rept(i,1,n+1){ n   if (dp[i][d]>=ans){ n    ans=dp[i][d]; n    pos=i; n   } n  } n  vector<int>v; n  int x=l[pos][d].first,y=l[pos][d].second; n  for (int i=pos-1;i>=0;i--){ n   if (x) n    v.pb(i+1); n   x=l[i][y].first,y=l[i][y].second; n  } n  if (!sz(v)) n   fail(-1); n  cout<<sz(v)<<  n ; n  rep(i,sz(v)) n   cout<<a[v[i]]<<   ; n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void read(int& x) { n   bool fu = 0; n   char c; n   for (c = getchar(); c <= 32; c = getchar()) n     ; n   if (c ==  - ) fu = 1, c = getchar(); n   for (x = 0; c > 32; c = getchar()) x = x * 10 + c -  0 ; n   if (fu) x = -x; n }; n char getc() { n   char c; n   for (c = getchar(); c <= 32; c = getchar()) n     ; n   return c; n } n const int N = 100010; n int n, i, j, k, l, p, x, a[N], s0, s1; n int b[N]; n int main() { n   read(n); n   for (i = 1; i <= n; i++) read(a[i]); n   for (i = 1; i <= n && a[i] == 0; i++) printf( 0 n ); n   for (; i <= n; i = j + 1) { n     for (j = i; j <= n && a[j] > 0; j++) n       ; n     j--; n     if (j + 1 > n) { n       for (k = i; k <= n; k++) printf( pushStack n ); n       break; n     } n     b[0] = 0; n     for (k = i; k <= j; k++) b[++b[0]] = a[k]; n     sort(b + 1, b + 1 + b[0]); n     if (b[0] == 0) n       printf( 0 n ); n     else if (b[0] == 1) n       printf( pushStack n1 popStack n ); n     else if (b[0] == 2) n       printf( pushStack npushQueue n2 popStack popQueue n ); n     else { n       p = b[b[0] - 2]; n       for (k = i; k <= j; k++) n         if (a[k] >= p) break; n       s0 = k; n       for (k = j; k >= i; k--) n         if (a[k] >= p) break; n       s1 = k; n       if (s1 <= s0) printf( - - n ); n       for (k = i; k <= j; k++) n         if (k == s1) n           printf( pushStack n ); n         else if (k == s0) n           printf( pushQueue n ); n         else if (a[k] >= p) n           printf( pushFront n ); n         else if (k < s1) n           printf( pushStack n ); n         else n           printf( pushQueue n ); n       printf( 3 popStack popQueue popFront n ); n     } n     j++; n   } n   scanf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct com { n   int l, r; n   long long c; n   friend bool operator<(const com &a, const com &b) { return a.r < b.r; } n } a[100010]; n int n, m, K; n long long cost[310][310], dp[310][310]; n const long long inf = 0x3F3F3F3F3F3F3F3FLL; n int main() { n   scanf( %d%d%d , &n, &m, &K); n   memset(cost, 0x3F, sizeof(cost)); n   for (int i = 0; i < m; i++) n     scanf( %d%d%I64d , &a[i].l, &a[i].r, &a[i].c), n         cost[a[i].l][a[i].r] = min(cost[a[i].l][a[i].r], a[i].c); n   for (int l = n; l >= 1; l--) { n     for (int i = 1; i + l - 1 <= n; i++) { n       int st = i, ed = i + l - 1; n       if (st > 1) cost[st][ed] = min(cost[st][ed], cost[st - 1][ed]); n       if (ed < n) cost[st][ed] = min(cost[st][ed], cost[st][ed + 1]); n     } n   } n   memset(dp, 0x3F, sizeof(dp)); n   dp[0][0] = 0; n   for (int i = 1; i <= n; i++) n     for (int j = 0; j <= i; j++) { n       dp[i][j] = min(dp[i][j], dp[i - 1][j]); n       for (int k = 0; k <= j; k++) n         dp[i][j] = min(dp[i - k][j - k] + cost[i - k + 1][i], dp[i][j]); n     } n   if (dp[n][K] == inf) n     cout << -1 << endl; n   else n     cout << dp[n][K] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[107], b[107]; n int ans[10000007]; n int n, nMax, nMaxid, times, nMin, aol, aMin, aMax, aL; n void init() { n   memset(a, 0, sizeof(a)); n   memset(ans, 0, sizeof(ans)); n   memset(b, 0, sizeof(b)); n   aol = 0; n   aL = 0; n } n int check(int x) { n   nMax = 0; n   times = 0; n   for (int i = 1; i <= n; i++) { n     if (a[i] > nMax) { n       nMax = a[i]; n       nMaxid = i; n       times = 0; n     } n     if (a[i] == nMax) times++; n   } n   if (times < x) return 1; n   return 0; n } n void makeans(int x) { n   times -= x; n   for (int i = 1; i <= n; i++) n     if (a[i] == nMax) { n       x--; n       a[i]--; n       ans[++aol] = i; n       if (x == 0) break; n     } n   ++aol; n   aL++; n } n void printans(int x) { n   if (ans[x] == 0) { n     for (int i = 1; i <= n; i++) printf( %d , b[i]); n     printf(  n ); n     memset(b, 0, sizeof(b)); n   } else n     b[ans[x]] = 1; n } n int main() { n   while (scanf( %d , &n) != EOF) { n     init(); n     for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n     while (check(2)) { n       int yMax = -1, yID = 0; n       for (int i = 1; i <= n; i++) n         if (a[i] > yMax) { n           if (i == nMaxid) continue; n           yMax = a[i]; n           yID = i; n         } n       ans[++aol] = yID; n       ans[++aol] = nMaxid; n       ++aol; n       aL++; n       if (a[yID]) a[yID]--; n       a[nMaxid]--; n     } n     aMin = 107; n     aMax = 0; n     for (int i = 1; i <= n; i++) n       if (a[i] < aMin) aMin = a[i]; n     while (check(n)) { n       if (times % 2 == 1) makeans(3); n       while (times) makeans(2); n     } n     printf( %d n%d n , aMin, aL); n     for (int i = 1; i <= aol; i++) printans(i); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n namespace Bigcat { n template <typename T> n inline void max_s(T& a, const T& b) { n   a = max(a, b); n } n template <typename T> n inline void min_s(T& a, const T& b) { n   a = min(a, b); n } n template <typename T> n void sort_v(T& x) { n   sort(x.begin(), x.end()); n } n template <typename T> n void erase_dup(T& x) { n   x.erase(unique(x.begin(), x.end()), x.end()); n } n template <typename T, typename V> n int pos_l(const T& x, const V& v) { n   return lower_bound(x.begin(), x.end(), v) - x.begin(); n } n template <typename T, typename V> n int pos_u(const T& x, const V& v) { n   return upper_bound(x.begin(), x.end(), v) - x.begin(); n } n inline unsigned rand_g() { n   static mt19937 mt_rand((int)time(0)); n   return mt_rand(); n } n using LL = long long; n using VI = vector<int>; n using VVI = vector<VI>; n using VL = vector<LL>; n using PII = pair<int, int>; n using VPII = vector<PII>; n }  // namespace Bigcat n using namespace Bigcat; n const LL base = LL(1e9 + 7); n const LL INFL = 1LL << 62; n const int INF = 1 << 30; n const int MAX = int(1e5 + 100); n struct Node { n   int x, y, w; n }; n bool operator<(const Node& n0, const Node& n1) { return n0.x < n1.y; } n LL le0[300], ri0[300], th[300]; n string zs[MAX]; n void solve1() { n   LL result = 0; n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> zs[i]; n   } n   string s = zs[n - 1]; n   char c0 =    ; n   LL g = 0; n   for (auto x : s) { n     if (x == c0) n       g++; n     else { n       c0 = x; n       result = max(result, g); n       g = 1; n     } n   } n   result = max(result, g); n   char lastone; n   auto work = [&]() { n     int bb = n; n     for (int tt = n - 1; tt >= 0; --tt) { n       string s = zs[tt]; n       LL g = 0; n       for (auto x : s) { n         if (x == lastone) n           g++; n         else { n           break; n         } n       } n       if (g == s.size()) { n         continue; n       } n       bb = tt + 1; n       break; n     } n     auto func = [&](const string& s) { n       LL t = 0, a = 0; n       for (auto c : s) { n         if (c == lastone) { n           t++; n           a = max(a, t); n         } else { n           t = 0; n         } n       } n       return a; n     }; n     LL mc = 0; n     for (int i = 0; i < bb; ++i) { n       string s = zs[i]; n       if (mc == 0) { n         mc = func(s); n         continue; n       } n       if (i != bb - 1) continue; n       LL t = 1; n       for (int i = 0; i < s.size(); ++i) { n         if (s[i] != lastone) break; n         t++; n       } n       for (int i = s.size() - 1; i >= 0; --i) { n         if (s[i] != lastone) break; n         t++; n       } n       mc = t; n       mc = max(mc, func(s)); n     } n     for (int i = bb; i < n; ++i) { n       mc = (mc + 1) * zs[i].size() + mc; n     } n     result = max(result, mc); n   }; n   lastone = zs[n - 1][0]; n   work(); n   lastone = zs[n - 1].back(); n   work(); n   cout << result << endl; n } n void solve() { n   int t = 1; n   for (int i = 0; i < t; ++i) { n     solve1(); n   } n   int x = 1; n } n void io_init() { n   const int inId = 0; n   const int outId = 0; n   if (inId > 0) { n     cerr <<  redirect stdin to input   << inId << endl; n     string s =  input ; n     s += to_string(inId); n     s +=  .txt ; n     auto r = freopen(s.c_str(),  r , stdin); n     if (r == nullptr) { n       cerr <<  invalid input file   << s << endl; n       ; n       r = freopen( CON ,  r , stdin); n     } n   } n   if (outId > 0) { n     cerr <<  redirect stdout to stdout   << outId << endl; n     string s =  stdout ; n     s += to_string(outId); n     s +=  .txt ; n     auto r = freopen(s.c_str(),  w , stdout); n     if (r == nullptr) { n       cerr <<  invalid output file   << s << endl; n       ; n       r = freopen( CON ,  w , stdout); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout.precision(11); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, s1; n   int count1 = 0, count2 = 0; n   cin >> s >> s1; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] != s1[i]) { n       if (s[i] ==  4 ) n         count1++; n       else n         count2++; n     } n   } n   if (count1 <= count2) n     cout << count2; n   else n     cout << count1; n   cin.get(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   for (int i = 0; i < t; i++) { n     int n; n     cin >> n; n     map<int, int> m; n     long long int a[n]; n     for (int j = 0; j < n; j++) { n       cin >> a[j]; n       m[a[j] % 3]++; n     } n     long long int res = m[0]; n     if (m[1] >= m[2]) { n       res += m[2]; n       m[1] = m[1] - m[2]; n       res += m[1] / 3; n     } else { n       res += m[1]; n       m[2] = m[2] - m[1]; n       res += m[2] / 3; n     } n     cout << res <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, ans, flag; n int a[100005], b[100005], xb[100005]; n void solve() { n   int i, j, ma; n   ans = -1; n   b[1] = xb[1] = 0; n   for (i = 2; i <= n; i++) { n     ma = 0; n     flag = 0; n     j = i - 1; n     while (a[j] < a[i] && j) { n       if (!b[j]) { n         flag = 1; n         break; n       } n       if (b[j] > ma) ma = b[j]; n       j = xb[j]; n     } n     if (!flag) n       b[i] = ma + 1; n     else n       b[i] = 0; n     xb[i] = j; n   } n   for (i = 1; i <= n; i++) { n     if (ans < b[i]) ans = b[i]; n   } n } n int main() { n   int i, j; n   while (~scanf( %d , &n)) { n     for (i = 1; i <= n; i++) { n       scanf( %d , &a[i]); n     } n     solve(); n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int SIZE = 1 << 20; n int INF = 1000000001; n int a[SIZE], ops[SIZE]; n int h, g; n int qq[24], qn; n int pull(int id) { n   int tmp = a[id]; n   a[id] = 0; n   qn = 0; n   qq[qn++] = id; n   while (id * 2 < (1 << h) && a[id] < max(a[id << 1], a[(id << 1) | 1])) { n     if (a[id << 1] > a[(id << 1) | 1]) { n       swap(a[id << 1], a[id]); n       id <<= 1; n     } else { n       swap(a[(id << 1) | 1], a[id]); n       id = (id << 1) | 1; n     } n     qq[qn++] = id; n   } n   if (id < (1 << g)) { n     for (int i = qn - 1; i > 0; i--) { n       a[qq[i]] = a[qq[i - 1]]; n     } n     a[qq[0]] = tmp; n     return 0; n   } n   return tmp; n } n void solve() { n   scanf( %d%d , &h, &g); n   long long an = 0; n   for (int i = 1; i < (1 << h); i++) { n     scanf( %d , &a[i]); n     an += a[i]; n   } n   int need = 0; n   for (int i = 1; i < (1 << g); i++) { n     while (1) { n       int v = pull(i); n       if (v) { n         an -= v; n         ops[need++] = i; n       } else n         break; n     } n   } n   printf( %lld n , an); n   for (int i = 0; i < need; i++) printf( %d%c , ops[i],    n [i == need - 1]); n } n int main() { n   int T; n   scanf( %d , &T); n   while (T--) solve(); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const long long mox = 1e6 + 9; n long long ha[mox]; n long long mod = 1e9 + 7; n long long d[mox]; n bool visit[mox]; n long long ma[mox]; n bool visit2[mox]; n long long ta(long long a, long long b) { n   long long ans = 1; n   long long c = a; n   while (b) { n     if (b % 2) { n       ans = (ans * c) % mod; n     } n     c = (c * c) % mod; n     b /= 2; n   } n   return ans; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, m; n   cin >> n >> m; n   string t; n   cin >> t; n   d[0] = 1; n   for (long long i = 0; i < t.size(); i++) { n     long long x = (d[i] * (t[i] -  a  + 1)) % mod; n     ha[i] = (ha[i - 1] + x) % mod; n     d[i + 1] = (d[i] * 27) % mod; n   } n   long long ans = 0; n   for (long long i = t.size() - 1; i >= 1; i--) { n     long long x = (ha[t.size() - 1] - ha[i - 1] + mod) % mod; n     long long y = ta(d[i], mod - 2); n     x = (x * y) % mod; n     if (x == ha[t.size() - 1 - i]) { n       visit[i] = 1; n     } n   } n   for (long long i = 0; i < m; i++) { n     cin >> ma[i]; n     visit2[ma[i]] = 1; n   } n   for (long long i = 1; i < m; i++) { n     if (ma[i] - ma[i - 1] < t.size()) { n       if (visit[ma[i] - ma[i - 1]] == 0) { n         cout << 0; n         exit(0); n       } n     } n   } n   long long x = 0; n   long long o = 0; n   for (long long i = 1; i <= n; i++) { n     if (visit2[i] == 1) { n       x = i + t.size() - 1; n     } n     if (i > x) { n       o += 1; n     } n   } n   long long w = 1; n   for (long long i = 0; i < o; i++) { n     w = (w * 26) % mod; n   } n   cout << w; n } n


#include <bits/stdc++.h> n using namespace std; n void solve_test() { n   string s; n   cin >> s; n   int n = int(s.length()); n   int total = 0; n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  + ) n       total++; n     else n       total--; n     if (total < 0) { n       total++; n       ans += i + 1; n     } n   } n   cout << ans + n <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int tests = 1; n   cin >> tests; n   while (tests--) solve_test(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 10; n int sum[maxn], op[maxn]; n int main() { n   int n, l = 0, r = 0, ans = 0, minn = 100; n   string s; n   cin >> n >> s; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  ( ) { n       op[i + 1] = 1; n       sum[i + 1] = sum[i] + 1; n       l++; n     } else { n       op[i + 1] = 0; n       sum[i + 1] = sum[i] - 1; n       r++; n     } n   } n   if (n & 1 == 1 || (abs(l - r) > 2) || (sum[n] == 0) || n       (*min_element(sum + 1, sum + 1 + n) < -2) || n       (*min_element(sum + 1, sum + 1 + n) < 0 && sum[n] != -2)) { n     printf( 0 n ); n     exit(0); n   } n   if (sum[n] == -2) { n     for (int i = 1; i <= n; i++) { n       if (op[i] == 0 && sum[i] < 0) { n         ans++; n         break; n       } else if (op[i] == 0 && sum[i] >= 0) n         ans++; n     } n   } n   if (sum[n] == 2) { n     for (int i = n; i >= 1; i--) { n       if (sum[i] >= 2) n         minn = i; n       else n         break; n     } n     minn = max(minn, 2); n     for (int i = minn; i <= n; i++) { n       if (op[i] == 1) ans++; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int fr[200002]; n int arr[200002]; n bool check(int cati) { n   int qq = 0; n   for (int i = 1; i <= 200000; ++i) qq += fr[i] / cati; n   if (qq >= k) return 1; n   return 0; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> k; n   for (int i = 1; i <= n; ++i) cin >> arr[i], fr[arr[i]]++; n   int b = 1; n   int e = n; n   int ans = 1; n   while (b <= e) { n     int mid = (b + e) / 2; n     if (check(mid)) n       ans = mid, b = mid + 1; n     else n       e = mid - 1; n   } n   int qa = 0; n   for (int i = 1; i <= 200000; ++i) { n     if (qa == k) return 0; n     while (fr[i] >= ans && qa < k) cout << i <<    , ++qa, fr[i] -= ans; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v; n map<int, long long> dp; n long long power(int a, long long b) { n   long long res = 1, base = a; n   while (b > 0) { n     if (b % 2) { n       res *= base; n       res %= (1000 * 1000 * 1000 + 7); n     } n     base *= base; n     base %= (1000 * 1000 * 1000 + 7); n     b /= 2; n   } n   return res; n } n long long compute(int g) { n   if (g == 1) { n     return 1; n   } n   if (dp.find(g) != dp.end()) { n     return dp[g]; n   } n   long long val = power(2, g - 1); n   int i; n   for (i = 0; i < v.size(); i++) { n     if (v[i] >= g) { n       break; n     } n     if (g % v[i] == 0) val -= compute(v[i]); n   } n   dp[g] = val; n   dp[g] %= (1000 * 1000 * 1000 + 7); n   dp[g] += (1000 * 1000 * 1000 + 7); n   dp[g] %= (1000 * 1000 * 1000 + 7); n   return dp[g]; n } n int main() { n   std::ios::sync_with_stdio(false); n   int x, y, val1, i; n   cin >> x >> y; n   if (y % x) { n     cout << 0 << endl; n   } else { n     y /= x; n     for (i = 1; i * i <= y; i++) { n       if (y % i == 0) { n         v.push_back(i); n         if (y / i != i) v.push_back(y / i); n       } n     } n     sort(v.begin(), v.end()); n     cout << compute(y); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   long long r; n   while (b != 0) { n     r = a % b; n     a = b; n     b = r; n   } n   return a; n } n long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n int n, m; n vector<int> v[4000000]; n char g[4000000]; n int inline make(int i, int j) { return i * m + j; } n void solve() { n   cin >> n >> m; n   int cnt = 0; n   for (int i = 0; i < n; i++) { n     char buf[2001]; n     cin >> buf; n     for (int j = 0; j < m; j++) { n       g[make(i, j)] = buf[j]; n       if (g[make(i, j)] ==  . ) { n         cnt++; n         if (i > 0 && g[make(i - 1, j)] ==  . ) { n           v[make(i, j)].push_back(make(i - 1, j)); n           v[make(i - 1, j)].push_back(make(i, j)); n         } n         if (j > 0 && g[make(i, j - 1)] ==  . ) { n           v[make(i, j)].push_back(make(i, j - 1)); n           v[make(i, j - 1)].push_back(make(i, j)); n         } n       } n     } n   } n   queue<int> q; n   for (int i = 0; i < n * m; i++) n     if (int((v[i]).size()) == 1) q.push(i); n   while (!q.empty()) { n     int i = q.front(); n     q.pop(); n     if (int((v[i]).size()) == 1) { n       int j = v[i][0], x = ((i) > (j) ? (j) : (i)), y = ((i) > (j) ? (i) : (j)); n       if (x / m == y / m) { n         g[x] =  < ; n         g[y] =  > ; n       } else { n         g[x] =  ^ ; n         g[y] =  v ; n       } n       for (int k = 0; k < int((v[j]).size()); k++) { n         int l = v[j][k]; n         for (int h = 0; h < int((v[l]).size()); h++) { n           if (v[l][h] == j) { n             v[l].erase(v[l].begin() + h); n             break; n           } n         } n         if (int((v[l]).size()) == 1) q.push(l); n       } n       v[i].clear(); n       v[j].clear(); n       cnt -= 2; n     } n   } n   if (cnt) { n     cout <<  Not unique ; n     return; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) cout << g[make(i, j)]; n     cout <<   n ; n   } n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<int> v = {1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683}; n   vector<int> vis; n   for (int i = 0; i < (1 << 10); i++) { n     int sum = 0; n     for (int j = 0; j < 10; j++) { n       if (i & (1 << j)) { n         sum += v[j]; n       } n     } n     vis.push_back(sum); n   } n   sort(vis.begin(), vis.end()); n   int q; n   cin >> q; n   while (q--) { n     int x; n     cin >> x; n     cout << *lower_bound(vis.begin(), vis.end(), x) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 7; n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     int l, r; n     scanf( %d%d , &l, &r); n     if (l * 2 > r) { n       printf( YES n ); n     } else { n       printf( NO n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k, a[100000], b[1000000], i, s, j, x, y; n int main() { n   cin >> n >> m; n   if (n == m) { n     cout <<  0  << endl; n     return 0; n   } n   while (n > 0) { n     a[i] = n % 2; n     n = n / 2; n     i++; n   } n   while (m > 0) { n     b[x] = m % 2; n     m = m / 2; n     x++; n   } n   while (a[x] == b[x]) { n     x--; n   } n   y = 1; n   for (j = 0; j <= x; j++) { n     y *= 2; n   } n   cout << y - 1 << endl; n } n


#include<bits/stdc++.h> n #define LL long long n using namespace std; n int n,m,edge_t=0,sum=0,rt_t=0; n  n bool tyl_jyy=0; n  n LL t,ans=0; n int rt[200002]={}; n vector<int> vec[200002]; n set<int> s; n struct aaa n { n  int x,y,val,u; n }e[200002]; n template<class T>void read(T &x) n { n  x=0;int f=0;char ch=getchar(); n  while(ch< 0  || ch> 9 )f|=(ch== - ),ch=getchar(); n  while(ch>= 0  && ch<= 9 )x=(x<<3)+(x<<1)+(ch^48),ch=getchar(); n  x=f? -x:x;return ; n } n inline bool cmp(aaa a,aaa b) n { n  return a.val<b.val? 1:0; n } n inline void add_edge(int x,int y,int z,int i) n { n  e[i]=(aaa){x,y,z,0},vec[x].push_back(y),vec[y].push_back(x); n } n inline void dfs(int x,int y) n { n  int i,j;s.erase(x),rt[x]=y; n  set<int>::iterator it; n  for(int i=1;;++i) n  { n   it=s.lower_bound(i); n   if(it==s.end())return ; n   i=(*it),j=lower_bound(vec[x].begin(),vec[x].end(),i)-vec[x].begin(); n   if(j==vec[x].size() || vec[x][j]>i)dfs(i,y); n  } n  /*for(set<int>::iterator it=s.begin();;) n  { n   v=(*it),ok=((++it)==s.end()),i=lower_bound(vec[x].begin(),vec[x].end(),v)-vec[x].begin(); n   if(i==vec[x].size() || vec[x][i]>v)dfs(v,y);if(ok)return ; n  }*/ n } n inline void getroot(int x) n { n  if(x==rt[x])return ; n  getroot(rt[x]),rt[x]=rt[rt[x]]; n } n int main() n { n  read(n),read(m),t=(1LL*n*(n-1))/2-m; n   n  //if(n==200000 && m==200000)tyl_jyy=1; n   n  for(int i=1,x,y,z;i<=m;++i)read(x),read(y),read(z),sum^=z,add_edge(x,y,z,i); n  for(int i=1;i<=n;++i)s.insert(i),sort(vec[i].begin(),vec[i].end()); n   n  //if(tyl_jyy)return 0&puts( ok ); n   n  for(int i=1;i<=n;++i)if(!rt[i])++rt_t,dfs(i,i); n  sort(e+1,e+m+1,cmp); n  for(int i=1,x,y;i<=m;++i) n  { n   x=e[i].x,y=e[i].y,getroot(x),getroot(y); n   if(rt[x]==rt[y])continue; n   rt[rt[x]]=rt[y],ans+=e[i].val,e[i].u=1; n  } n  if(n-rt_t==t) n  { n   for(int i=1;i<=n;++i)rt[i]=i; n   for(int i=1,x,y;i<=m;++i)if(e[i].u)x=e[i].x,y=e[i].y,getroot(x),getroot(y),rt[rt[x]]=rt[y]; n   for(int i=1,x,y;i<=m;++i) n    if(!e[i].u) n    { n     x=e[i].x,y=e[i].y,getroot(x),getroot(y); n     if(rt[x]==rt[y])continue; n     sum=min(sum,e[i].val);break; n    } n  } n  else sum=0; n  printf( %lld ,ans+sum); n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n long int a[5001], b[5001]; n long long ans[5001]; n int main(void) { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     a[i] -= i; n     b[i] = a[i]; n   } n   sort(b + 1, b + n + 1); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) { n       ans[j] += abs(a[i] - b[j]); n       if (j > 1) ans[j] = min(ans[j], ans[j - 1]); n     } n   cout << ans[n]; n } n


#include <bits/stdc++.h> n int main() { n   long n, a, b; n   scanf( %ld %ld %ld , &n, &a, &b); n   std::vector<long> v(n, 1); n   long current(1), sum(1); n   for (long p = 1; p < n; p++) { n     if (b > 0) { n       current = sum + 1; n       --b; n     } else if ((p > 1) && (a > 0)) { n       ++current, --a; n     } n     v[p] = current; n     sum += current; n   } n   if ((a > 0) || (b > 0)) { n     puts( -1 ); n   } else { n     for (long p = 0; p < n; p++) { n       printf( %ld  , v[p]); n     }; n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool debug = false; n int n, m, k; n int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; n long long ln, lk, lm; n long long dp[35][1005], tmp[1005]; n int cnt = 0; n void init() { n   dp[0][0] = 1; n   tmp[0] = 1; n   for (int i = 1; i <= 30; i++) { n     dp[i][0] = 1; n     for (int j = 1; j <= 1000; j++) { n       for (int k = 0; k < j; k++) { n         dp[i][j] += (tmp[k] * tmp[j - k - 1]) % 7340033; n         dp[i][j] %= 7340033; n       } n     } n     for (int j = 1; j <= 1000; j++) tmp[j] = 0; n     tmp[0] = 1; n     for (int j = 1; j <= 1000; j++) { n       for (int k = 0; k <= j; k++) { n         tmp[j] += (dp[i][k] * dp[i][j - k]) % 7340033; n         tmp[j] %= 7340033; n       } n     } n   } n } n int main() { n   init(); n   int q; n   scanf( %d , &q); n   while (q--) { n     scanf( %d%d , &n, &k); n     if (k == 0) n       puts( 1 ); n     else { n       int id = 0; n       while ((n & 1) && n > 1) { n         n >>= 1; n         id++; n       } n       printf( %lld n , dp[id][k]); n     } n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 100; n void ask(int r1, int c1, int r2, int c2) { n   cout <<  ?   << r1 <<     << c1 <<     << r2 <<     << c2 <<   n ; n   cout.flush(); n } n bool ans() { n   string ans; n   cin >> ans; n   return ans ==  YES ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   int steps = 0; n   int rx = 1, ry = 1; n   string pre =   ; n   while (steps < n - 1) { n     ask(rx + 1, ry, n, n); n     bool sol = ans(); n     if (sol) { n       pre +=  D ; n       rx++; n     } else { n       pre +=  R ; n       ry++; n     } n     steps++; n   } n   string suf =   ; n   steps = 0; n   rx = n, ry = n; n   while (steps < n - 1) { n     ask(1, 1, rx, ry - 1); n     bool sol = ans(); n     if (sol) { n       suf +=  R ; n       ry--; n     } else { n       suf +=  D ; n       rx--; n     } n     steps++; n   } n   reverse(suf.begin(), suf.end()); n   cout <<  !   << (pre + suf) <<   n ; n   cout.flush(); n } n


#include <bits/stdc++.h> n using namespace std; n template <class A> n void read(vector<A>& v); n template <class A, size_t S> n void read(array<A, S>& a); n template <class T> n void read(T& x) { n   cin >> x; n } n void read(double& d) { n   string t; n   read(t); n   d = stod(t); n } n void read(long double& d) { n   string t; n   read(t); n   d = stold(t); n } n template <class H, class... T> n void read(H& h, T&... t) { n   read(h); n   read(t...); n } n template <class A> n void read(vector<A>& x) { n   for (auto& a : x) read(a); n } n template <class A, size_t S> n void read(array<A, S>& x) { n   for (auto& a : x) read(a); n } n const int mod = 1e9 + 7; n long long int primeLimit = 1e5; n const double PI = acos(-1); n bool prime[100000]; n string LongestPalindromicPrefix(string str) { n   string temp = str +  ? ; n   reverse(str.begin(), str.end()); n   temp += str; n   long long int n = temp.length(); n   long long int lps[n]; n   fill(lps, lps + n, 0); n   for (long long int i = 1; i < n; i++) { n     long long int len = lps[i - 1]; n     while (len > 0 && temp[len] != temp[i]) { n       len = lps[len - 1]; n     } n     if (temp[i] == temp[len]) { n       len++; n     } n     lps[i] = len; n   } n   return temp.substr(0, lps[n - 1]); n } n bool binarySearchFloat(float lower, float higher, float maxErrorAllowed, n                        long long int numberOfTimesWanttoRunCode) { n   float l = lower, r = higher; n   float eps = maxErrorAllowed; n   long long int iteration_count = numberOfTimesWanttoRunCode; n   for (long long int i = 0; i < iteration_count && l + eps < r; ++i) { n   } n } n void primefinder() { n   memset(prime, true, sizeof(prime)); n   prime[1] = false; n   prime[0] = false; n   for (long long int i = 2; i < primeLimit; i++) { n     if (prime[i]) { n       for (long long int j = i * i; j < primeLimit; j += i) { n         prime[j] = false; n       } n     } n   } n } n long long fast_pow(long long int x, long long int y) { n   long long res = 1; n   while (y > 0) { n     if (y & 1) res = res * x; n     y = y >> 1; n     x = x * x; n   } n   return res; n } n long long Fast_pow(long long int x, long long int y, long long int p) { n   long long res = 1; n   x = x % p; n   if (x == 0) return 0; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n long long int bs(vector<long long int> arr, long long int low, n                  long long int cur) { n   long long int high = (long long int)arr.size() - 1; n   long long int index = -1; n   while (low <= high) { n     long long int mid = (low + high) / 2; n     if (arr[mid] <= cur) { n       index = mid; n       low = mid + 1; n     } else { n       high = mid - 1; n     } n   } n   return index; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int n, k; n   cin >> n >> k; n   vector<long long int> arr(n); n   read(arr); n   sort((arr).begin(), (arr).end()); n   long long int ans = 0; n   for (long long int i = 0; i < n; i++) { n     long long int index = bs(arr, i, 2 * k); n     if (index >= (n - 1)) break; n     if (index == -1) { n       while ((k * 2) < arr[i]) { n         k *= 2; n         ans++; n       } n       k = std::max(k, arr[i]); n     } else { n       k = std::max(k, arr[index]); n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, c, d, e, n, m, t, kk, tt = 0, l[5007], r[5007], fa[5007], type[5007], n                                 ind[5007]; n int y[5007] = {0}, qw[5007], tree[1000007], a1[5007], a2[5007], a3[5007], n     a4[5007]; n vector<int> q[5007]; n vector<short> w[5007]; n void get(int x1, int l, int r, int tl, int tr, int k) { n   if (tree[k] == tt) return; n   if (l >= tl && r <= tr) { n     tree[k] = tt; n     for (int i = l; i <= r; i++) w[x1].push_back(i); n     return; n   } n   int m = (l + r) / 2; n   if (tl <= m) get(x1, l, m, tl, tr, k << 1); n   if (tr > m) get(x1, m + 1, r, tl, tr, (k << 1) + 1); n } n int sou(int x1) { n   t = 0; n   if (type[x1] == 0) n     for (int j = 1; j <= q[x1].size(); j++) w[x1].push_back(q[x1][j - 1]); n   else n     get(x1, 1, m, q[x1][0], q[x1][1], 1); n   for (int j = 1; j <= w[x1].size(); j++) n     if (!qw[fa[w[x1][j - 1]]]) { n       qw[fa[w[x1][j - 1]]] = 1; n       if (y[fa[w[x1][j - 1]]] == 0 || sou(y[fa[w[x1][j - 1]]])) { n         y[fa[w[x1][j - 1]]] = x1; n         w[x1].clear(); n         return 1; n       } n     } n   w[x1].clear(); n   return 0; n } n int main() { n   scanf( %d%d , &n, &m); n   kk = 1; n   for (int i = 1; i <= m; i++) fa[i] = i; n   int ans = 0, t3 = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &type[kk]); n     if (type[kk] == 0) { n       scanf( %d , &b); n       for (int j = 1; j <= b; j++) { n         scanf( %d , &c); n         q[kk].push_back(c); n       } n       ind[kk] = i; n       kk++; n     } else if (type[kk] == 1) { n       scanf( %d%d , &b, &c); n       ind[kk] = i; n       q[kk].push_back(b); n       q[kk++].push_back(c); n     } else { n       scanf( %d%d%d , &b, &c, &d); n       fa[c] = b; n       fa[d] = b; n       ans += 2; n       a1[++t3] = b, a2[t3] = c, a3[t3] = d, a4[t3] = i; n     } n   } n   for (int i = 1; i <= kk - 1; i++) { n     tt++; n     for (int j = 1; j <= m; j++) qw[j] = 0; n     if (sou(i)) ans++; n   } n   printf( %d n , ans); n   for (int i = 1; i <= t3; i++) { n     fa[a1[i]] = 0; n     fa[a2[i]] = 0; n     fa[a3[i]] = 0; n     int g = 0; n     if (y[a1[i]] == 0) { n       printf( %d %d n , a4[i], a1[i]); n       printf( %d %d n , a4[i], a2[i]); n       continue; n     } else if (type[y[a1[i]]] == 0) n       for (int j = 1; j <= q[y[a1[i]]].size(); j++) { n         if (q[y[a1[i]]][j - 1] == a1[i]) g = 1; n         if (q[y[a1[i]]][j - 1] == a2[i]) g = 2; n         if (q[y[a1[i]]][j - 1] == a3[i]) g = 3; n       } n     else n       for (int j = q[y[a1[i]]][0]; j <= q[y[a1[i]]][1]; j++) { n         if (j == a1[i]) g = 1; n         if (j == a2[i]) g = 2; n         if (j == a3[i]) g = 3; n       } n     if (g == 1) n       printf( %d %d n , ind[y[a1[i]]], a1[i]); n     else n       printf( %d %d n , a4[i], a1[i]); n     if (g == 2) n       printf( %d %d n , ind[y[a1[i]]], a2[i]); n     else n       printf( %d %d n , a4[i], a2[i]); n     if (g == 3) n       printf( %d %d n , ind[y[a1[i]]], a3[i]); n     else n       printf( %d %d n , a4[i], a3[i]); n   } n   for (int i = 1; i <= m; i++) n     if (y[i] && fa[i] == i) printf( %d %d n , ind[y[i]], i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char x[15]; n int main() { n   scanf( %s , x); n   int lenx = strlen(x); n   for (int i = lenx - 1; i >= 0; i--) { n     int dig = x[i] -  0 ; n     if (dig == 0) n       printf( O-|-OOOO n ); n     else if (dig == 1) n       printf( O-|O-OOO n ); n     else if (dig == 2) n       printf( O-|OO-OO n ); n     else if (dig == 3) n       printf( O-|OOO-O n ); n     else if (dig == 4) n       printf( O-|OOOO- n ); n     else if (dig == 5) n       printf( -O|-OOOO n ); n     else if (dig == 6) n       printf( -O|O-OOO n ); n     else if (dig == 7) n       printf( -O|OO-OO n ); n     else if (dig == 8) n       printf( -O|OOO-O n ); n     else n       printf( -O|OOOO- n ); n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-6, pi = acos(-1); n const int N = 1e2 + 5, M = 1e9 + 7, SEGM = 4 * N, OO = 0x3f3f3f3f; n int t, n, m, v1, v2; n int mem[105][2005]; n int main() { n   scanf( %d%d%d%d , &v1, &v2, &n, &m); n   if (v1 > v2) swap(v1, v2); n   memset(mem, -OO, sizeof mem); n   mem[0][v1] = v1; n   for (int i = 1; i < n; ++i) { n     for (int cur = v1; cur < 2005; ++cur) { n       for (int j = 0; j <= m; ++j) { n         if (cur + j < 2005) n           mem[i][cur + j] = max(mem[i][cur + j], cur + j + mem[i - 1][cur]); n         if (cur - j > -1) n           mem[i][cur - j] = max(mem[i][cur - j], cur - j + mem[i - 1][cur]); n       } n     } n   } n   cout << mem[n - 1][v2]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void flanther_fakadi() { n   string s; n   cin >> s; n   long long n = s.length(); n   long long ct = 0; n   for (long long i = 0; i < s.length(); i++) { n     if (ct && s[i] ==  B ) { n       n -= 2; n       ct--; n     } else { n       ct++; n     } n   } n   cout << n << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(NULL); n   int t = 0; n   cin >> t; n   while (t--) { n     flanther_fakadi(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   vector<pair<long long int, long long int>> ans; n   string des =   ; n   for (long long int i = 1; i < k; i++) { n     des +=  () ; n   } n   long long int maxx = (n / 2); n   for (long long int i = 0; i < (maxx - k + 1); i++) { n     des +=  ( ; n   } n   for (long long int i = 0; i < (maxx - k + 1); i++) { n     des +=  ) ; n   } n   for (long long int i = 0; i < n; i++) { n     if (s[i] != des[i]) { n       long long int j = i + 1; n       while (s[j] != des[i]) j++; n       string temp = s.substr(i, (j - i + 1)); n       reverse(temp.begin(), temp.end()); n       for (long long int k = i; k <= j; k++) { n         s[k] = temp[k - i]; n       } n       ans.push_back({i + 1, j + 1}); n     } n   } n   cout << ans.size() <<   n ; n   for (long long int i = 0; i < ans.size(); i++) { n     cout << ans[i].first <<     << ans[i].second <<   n ; n   } n } n signed main() { n   long long int t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxv = 1.5e7; n const int bit = 29; n int cnt[maxv] = {}; n int go[maxv][2] = {}; n int tot = 1; n void add(int v, int d, int val) { n   cnt[v]++; n   if (d == -1) return; n   if (!go[v][(val >> d) & 1]) go[v][(val >> d) & 1] = ++tot; n   add(go[v][(val >> d) & 1], d - 1, val); n } n void get(int v, int d, int val, int& t_a) { n   assert(cnt[v]); n   cnt[v]--; n   if (d == -1) return; n   for (int k = ((val >> d) & 1), l = 0; l < 2; k = 1 - k, ++l) n     if (go[v][k]) { n       get(go[v][k], d - 1, val, t_a); n       if (!cnt[go[v][k]]) go[v][k] = 0; n       t_a += (k << d); n       return; n     } n } n int main() { n   int n; n   cin >> n; n   vector<int> msg(n); n   for (int& i : msg) cin >> i; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     add(1, bit, x); n   } n   for (int i : msg) { n     int out = 0; n     get(1, bit, i, out); n     assert((i ^ out) >= 0); n     cout << (i ^ out) <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 7; n int head[N], nxt[N << 1], ver[N << 1], tot; n int n, d[N], out[N], f[N], ji[N], ou[N]; n vector<int> v[N]; n bool flag; n void add(int x, int y) { n   ver[++tot] = y; n   nxt[tot] = head[x], head[x] = tot; n   out[x]++; n } n void dfs(int x, int fa) { n   d[x] = d[fa] + 1; n   f[x] = fa; n   for (int i = head[x]; i; i = nxt[i]) { n     int y = ver[i]; n     if (y == fa) continue; n     dfs(y, x); n   } n } n void dfs2(int x, int fa) { n   for (int i = head[x]; i; i = nxt[i]) { n     int y = ver[i]; n     if (y == fa) continue; n     dfs2(y, x); n     if (ou[y]) ji[x] = 1; n     if (ji[y]) ou[x] = 1; n   } n   if (ou[x] && ji[x]) flag = true; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     add(x, y), add(y, x); n   } n   int root = 0, minn = 0, maxx = n - 1; n   for (int i = 1; i <= n; i++) n     if (out[i] >= 2) { n       root = i; n       break; n     } n   for (int i = 1; i <= n; i++) n     if (out[i] == 1) ou[i] = 1; n   dfs2(root, 0); n   minn = (flag ? 3 : 1); n   dfs(root, 0); n   for (int i = 1; i <= n; i++) v[f[i]].push_back(i); n   for (int i = 1; i <= n; i++) { n     int siz = v[i].size(), cnt = 0; n     if (!siz) continue; n     for (int j = 0; j < siz; j++) { n       int tt = v[i][j]; n       if (out[tt] == 1) cnt++; n     } n     if (cnt) maxx -= cnt - 1; n   } n   printf( %d %d n , minn, maxx); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cnt[3], ans; n vector<int> c; n vector<vector<int>> sum; n vector<vector<int>> arr; n void dfs(int x, int p) { n   for (auto i : arr[x]) { n     if (i != p) { n       dfs(i, x); n       sum[x][0] += sum[i][0]; n       sum[x][1] += sum[i][1]; n       sum[x][2] += sum[i][2]; n     } n   } n   sum[x][c[x]]++; n   if (sum[x][1] == 0 && sum[x][2] == cnt[2] || n       sum[x][1] == cnt[1] && sum[x][2] == 0) n     ans++; n } n int main() { n   cin.tie(0), ios::sync_with_stdio(0); n   int n; n   cin >> n; n   c.resize(n); n   arr.resize(n); n   for (int i = 0; i < n; i++) cin >> c[i], cnt[c[i]]++; n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     cin >> a >> b; n     arr[a - 1].push_back(b - 1); n     arr[b - 1].push_back(a - 1); n   } n   sum.resize(n, vector<int>(3, 0)); n   dfs(0, 0); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, i, j, k, res[200500], st1[200500][20], st2[200500][20]; n int l2(int n) { n   int r = 0; n   while (n - 1) { n     r++; n     n >>= 1; n   } n   return r; n } n int chk1(int l, int r) { n   int k = l2(r - l + 1); n   return max(st1[l][k], st1[r - (1 << k) + 1][k]); n } n int chk2(int l, int r) { n   int k = l2(r - l + 1); n   return min(st2[l][k], st2[r - (1 << k) + 1][k]); n } n struct sb { n   int id, l, r; n   bool operator<(const sb x) const { return r > x.r; } n } tmp, s[200500]; n vector<sb> v[200500]; n priority_queue<sb> q; n void ans() { n   for (int i = 1; i <= n; i++) { n     printf( %d  , res[i]); n   } n   puts(  ); n } n int main() { n   memset(st2, 0x3f, sizeof(st2)); n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d%d , &j, &k); n     s[i].id = i; n     s[i].l = j; n     s[i].r = k; n     v[j].push_back(s[i]); n   } n   for (i = 1; i <= n; i++) { n     for (auto j : v[i]) { n       q.push(j); n     } n     s[i] = q.top(); n     q.pop(); n     res[s[i].id] = i; n     st1[i][0] = s[i].r; n     st2[i][0] = s[i].l; n   } n   for (j = 1; j <= 18; j++) { n     for (i = 1; i + (1 << j) - 1 <= n; i++) { n       st1[i][j] = max(st1[i][j - 1], st1[i + (1 << (j - 1))][j - 1]); n       st2[i][j] = min(st2[i][j - 1], st2[i + (1 << (j - 1))][j - 1]); n     } n   } n   for (i = 1; i <= n; i++) { n     if (s[i].l < i && chk1(s[i].l, i - 1) >= i) { n       puts( NO ); n       ans(); n       for (j = i - 1;; j--) { n         if (s[j].r >= i) { n           swap(res[s[i].id], res[s[j].id]); n           break; n         } n       } n       ans(); n       return 0; n     } n     if (s[i].r > i && chk2(i + 1, s[i].r) <= i) { n       puts( NO ); n       ans(); n       for (j = i + 1;; j++) { n         if (s[j].l <= i) { n           swap(res[s[i].id], res[s[j].id]); n           break; n         } n       } n       ans(); n       return 0; n     } n   } n   puts( YES ); n   ans(); n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1e3 + 5; n long long int dp[M][12][M], inv[M], n, d, mod; n long long int pw(long long int a, long long int b) { n   if (b == 0) return 1ll; n   long long int p = pw(a, b / 2ll); n   if (b % 2ll) n     return p * p % mod * a % mod; n   else n     return p * p % mod; n } n int main() { n   cin >> n >> d >> mod; n   for (long long int i = 1; i <= n; i++) inv[i] = pw(i, mod - 2ll); n   dp[1][0][0] = 1; n   for (int i = 2; i <= n; i++) n     for (int j = 1; j <= d; j++) n       for (int k = (i - 1) / j; k <= i - 1; k++) { n         dp[i][j][k] = dp[i][j][k - 1]; n         long long int cur = 1, buf = dp[k][d - 1][k - 1]; n         for (int t = 1; t <= j && t * k <= i - 1; t++) { n           cur = cur * buf++ % mod * inv[t] % mod; n           (dp[i][j][k] += dp[i - t * k][j - t][min(k - 1, i - t * k - 1)] * n                           (cur + (k == 1))) %= mod; n         } n       } n   long long int ans = dp[n][d][(n - 1) / 2]; n   if (n % 2 == 0) n     (ans += dp[n / 2][d - 1][n / 2 - 1] * (dp[n / 2][d - 1][n / 2 - 1] + 1) % n             mod * inv[2]) %= mod; n   cout << (n <= 2 ? 1 : ans) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   long long n, m; n   cin >> n >> m; n   vector<vector<long long>> dist((size_t)n, n                                  vector<long long>((size_t)n, (long long)1e16)); n   for (long long i = 0; i < n; i++) { n     dist[i][i] = (long long)0; n   } n   for (long long i = 0; i < m; i++) { n     long long u, v; n     cin >> u >> v; n     u--, v--; n     dist[u][v] = dist[v][u] = 0; n   } n   for (long long k = 0; k < n; k++) { n     for (long long i = 0; i < n; i++) { n       for (long long j = 0; j < n; j++) { n         dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); n       } n     } n   } n   long long s, b, k, h; n   cin >> s >> b >> k >> h; n   vector<pair<long long, pair<long long, long long>>> x((size_t)s); n   for (long long i = 0; i < s; i++) { n     cin >> x[i].first >> x[i].second.first >> x[i].second.second; n     x[i].first--; n   } n   vector<pair<long long, long long>> y((size_t)b); n   for (long long i = 0; i < b; i++) { n     cin >> y[i].first >> y[i].second; n     y[i].first--; n   } n   vector<vector<long long>> adj((size_t)s); n   for (long long i = 0; i < s; i++) { n     for (long long j = 0; j < b; j++) { n       if (x[i].second.first >= y[j].second && n           x[i].second.second >= dist[x[i].first][y[j].first]) { n         adj[i].push_back(j); n       } n     } n   } n   vector<bool> visited; n   vector<long long> matching((size_t)b, -1); n   function<bool(long long)> visit = [&](long long i) { n     if (visited[i]) { n       return false; n     } n     visited[i] = true; n     for (long long j : adj[i]) { n       if (matching[j] == -1 || visit(matching[j])) { n         matching[j] = i; n         return true; n       } n     } n     return false; n   }; n   long long f = 0; n   for (long long i = 0; i < s; i++) { n     visited.assign((size_t)s, false); n     if (visit(i)) { n       f++; n     } n   } n   cout << min(f * k, h * s) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int n; n   cin >> n; n   int x, y; n   cin >> x >> y; n   string s; n   cin >> s; n   bool flag = false; n   int cnt = 0; n   for (int i = 0; i < n; ++i) { n     if (s[i] ==  0 ) { n       flag = true; n     } else if (flag) { n       ++cnt; n       flag = false; n     } n   } n   if (flag) ++cnt; n   long long ans = 1LL * (cnt - 1) * min(x, y) + y; n   if (cnt == 0) ans = 0; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string cad; n   int cnt = 0, ans = 0; n   cin >> cnt >> cad; n   cnt = 0; n   for (int i = 0; i < cad.size(); i++) { n     if (cad[i] ==  x ) { n       cnt++; n     } else { n       ans += max(0, cnt - 2); n       cnt = 0; n     } n   } n   ans += max(0, cnt - 2); n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int x, y, a, b, c; n   cin >> x >> y >> a >> b >> c; n   long long int t = a * 2; n   long long int r = c * 3; n   cout << max(t + b - x, 0ll) + max(r + b - y, 0ll); n   return 0; n } n


#include <bits/stdc++.h> n const int kInf = 0x3f3f3f3f; n int len, n; n int msk[200010]; n int mem[21][1 << 20]; n int f[200010], s[200010]; n int conv(char *s) { n   int res = 0; n   for (int i = 0; i < len; i++) res = (res << 1) + (s[i] -  0 ); n   return res; n } n int g(int i, int j) { n   int p = msk[i], q = msk[j]; n   for (int i = len; i >= 0; i--) n     if ((p & ((1 << i) - 1)) == (q >> (len - i))) return i; n   assert(0); n } n inline void upd(int &x, int y) { n   if (y < x) x = y; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     static char ts[22]; n     scanf( %s , ts); n     len = strlen(ts); n     msk[i] = conv(ts); n   } n   for (int i = 2; i <= n; i++) s[i] = s[i - 1] + g(i - 1, i); n   memset(mem, 0x3f, sizeof(mem)); n   mem[0][0] = 0; n   for (int i = 1; i <= n; i++) { n     f[i] = kInf; n     if (i < n) { n       for (int l = 0; l <= len; l++) { n         int tmp = mem[l][msk[i + 1] >> (len - l)]; n         if (tmp != kInf) upd(f[i], tmp + i * len - s[i] - l); n       } n       for (int l = 0; l <= len; l++) n         upd(mem[l][msk[i] & ((1 << l) - 1)], f[i] + s[i + 1] - i * len); n     } else { n       f[i] = mem[0][0] + i * len - s[i]; n     } n   } n   printf( %d n , f[n]); n } n


#include <bits/stdc++.h> n using namespace std; n long long int n; n long long int a[100001]; n vector<pair<long long int, long long int> > g[100001]; n long long int dis[100001]; n long long int rem[100001]; n long long int dp[100001]; n long long int child[100001]; n long long int ct = 1; n void simpledfs(long long int x) { n   rem[x] = 1; n   for (long long int i = 0; i < g[x].size(); i++) { n     simpledfs(g[x][i].first); n   } n } n void dfs1(long long int x, long long int distance) { n   dis[x] = distance; n   long long int temp = 0; n   long long int mi = 1e9; n   long long int pos = -1; n   for (long long int i = 0; i < g[x].size(); i++) { n     dfs1(g[x][i].first, distance + g[x][i].second); n     temp = temp + child[g[x][i].first] + 1; n     if (dis[g[x][i].first] < mi) { n       mi = dis[g[x][i].first]; n       pos = g[x][i].first; n     } n   } n   dp[x] = pos; n   child[x] = temp; n } n void dfs2(long long int x, long long int minv) { n   for (long long int i = 0; i < g[x].size(); i++) { n     long long int cur = g[x][i].first; n     if ((dis[cur] - minv) > a[cur]) n       continue; n     else { n       ct++; n       dfs2(cur, min(minv, dis[cur])); n     } n   } n } n int main() { n   cin >> n; n   for (long long int i = 1; i <= n; i++) cin >> a[i]; n   for (long long int i = 2; i <= n; i++) { n     long long int p, c; n     cin >> p >> c; n     g[p].push_back(make_pair(i, c)); n   } n   dfs1(1, 0); n   dfs2(1, 0); n   cout << n - ct; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e4 + 5, mod = 1e9 + 9, bit = 61, sq = 1000; n const int T = (100001 + sq - 1) / sq; n vector<int> lucky; n bool isl[N] = {0}; n int bid[100005], a[100005], beg[T], en[T], lazy[T] = {0}; n int fq[T][N]; n void create() { n   for (int d = 1; d <= 4; d++) { n     for (int mask = 0; mask < (1ll << d); mask++) { n       int curr = 0; n       for (int j = 0; j <= d - 1; j++) { n         curr = (curr * 10); n         if (mask & (1ll << j)) { n           curr += 4; n         } else { n           curr += 7; n         } n       } n       isl[curr] = 1; n       lucky.push_back(curr); n     } n   } n } n signed main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   int t = 1; n   memset(fq, 0, sizeof(fq)); n   create(); n   while (t--) { n     int n, q; n     cin >> n >> q; n     int j = -1; n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n       if (i % sq == 1) { n         bid[i] = ++j; n         beg[j] = i; n         en[j] = min(i + sq - 1, n); n       } else { n         bid[i] = j; n       } n       fq[j][a[i]]++; n     } n     while (q--) { n       string tp; n       int l, r; n       cin >> tp >> l >> r; n       if (tp[0] ==  c ) { n         int ans = 0; n         if (bid[l] == bid[r]) { n           for (int i = l; i <= r; i++) { n             ans += isl[a[i] + lazy[bid[l]]]; n           } n         } else { n           for (int i = l; i <= en[bid[l]]; i++) { n             ans += isl[a[i] + lazy[bid[l]]]; n           } n           for (int i = bid[l] + 1; i <= bid[r] - 1; i++) { n             for (auto x : lucky) { n               if (x >= lazy[i]) { n                 ans += fq[i][x - lazy[i]]; n               } n             } n           } n           for (int i = beg[bid[r]]; i <= r; i++) { n             ans += isl[a[i] + lazy[bid[r]]]; n           } n         } n         cout << ans <<   n ; n       } else { n         int d; n         cin >> d; n         if (bid[l] == bid[r]) { n           for (int i = l; i <= r; i++) { n             fq[bid[i]][a[i]]--; n             fq[bid[i]][a[i] + d]++; n             a[i] += d; n           } n         } else { n           for (int i = l; i <= en[bid[l]]; i++) { n             fq[bid[i]][a[i]]--; n             fq[bid[i]][a[i] + d]++; n             a[i] += d; n           } n           for (int i = bid[l] + 1; i <= bid[r] - 1; i++) { n             lazy[i] += d; n           } n           for (int i = beg[bid[r]]; i <= r; i++) { n             fq[bid[i]][a[i]]--; n             fq[bid[i]][a[i] + d]++; n             a[i] += d; n           } n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int inf = 1e9 + 7; n int main() { n   long long int n; n   cin >> n; n   vector<pair<long long int, long long int> > arr(n + 1); n   for (long long int i = 1; i < n + 1; i++) { n     long long int a, b; n     cin >> a >> b; n     arr[i].first = a; n     arr[i].second = b; n   } n   vector<long long int> ans(n + 1); n   ans[1] = 1; n   long long int x, y; n   x = arr[1].first; n   y = arr[1].second; n   if (arr[x].first == y or arr[x].second == y) { n     ans[2] = x; n     ans[3] = y; n     long long int i = 3; n     while (i != n) { n       i++; n       long long int a = arr[ans[i - 2]].first; n       long long int b = arr[ans[i - 2]].second; n       ans[i] = (a == ans[i - 1] ? b : a); n     } n     for (int i = 1; i < ans.size(); i++) { n       cout << ans[i] <<    ; n     } n     return 0; n   } else { n     ans[2] = y; n     ans[3] = x; n     long long int i = 3; n     while (i != n) { n       i++; n       long long int a = arr[ans[i - 2]].first; n       long long int b = arr[ans[i - 2]].second; n       ans[i] = (a == ans[i - 1] ? b : a); n     } n     for (int i = 1; i < ans.size(); i++) { n       cout << ans[i] <<    ; n     } n     return 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100020], b[100020]; n int main() { n   int m, n; n   scanf( %d%d , &m, &n); n   long long sum1 = 0, sum2 = 0, s1 = 0, s2 = 0, Max1 = 0, Max2 = 0; n   for (int i = 1; i <= m; i++) { n     scanf( %I64d , &a[i]); n     sum1 += a[i]; n     if (a[i] > Max1) { n       Max1 = a[i]; n       s1 = i; n     } n   } n   for (int i = 1; i <= n; i++) { n     scanf( %I64d , &b[i]); n     sum2 += b[i]; n     if (b[i] > Max2) { n       Max2 = b[i]; n       s2 = i; n     } n   } n   long long ans1 = sum1 * n, ans2 = sum2 * m; n   for (int i = 1; i <= m; i++) { n     if (i != s1 && a[i] < sum2) ans2 = ans2 - sum2 + a[i]; n   } n   for (int i = 1; i <= n; i++) { n     if (i != s2 && b[i] < sum1) ans1 = ans1 - sum1 + b[i]; n   } n   if (ans1 > ans2) n     printf( %I64d n , ans2); n   else n     printf( %I64d n , ans1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2005; n char mpt[2005][2005]; n int dp[maxn][maxn]; n int col[maxn]; n int main() { n   int n, m, k; n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= n; i++) scanf( %s , mpt[i] + 1); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (mpt[i][j] ==  L ) { n         int w = i - 1; n         if (j - w >= 1) dp[i][j - w] += 1; n       } else if (mpt[i][j] ==  R ) { n         int w = i - 1; n         if (j + w <= m) dp[i][j + w] += 1; n       } else if (mpt[i][j] ==  U  && i % 2 == 1) { n         col[j] += 1; n       } else if (mpt[i][j] ==  D ) { n         continue; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       col[j] += dp[i][j]; n     } n   } n   for (int i = 1; i <= m; i++) printf( %d  , col[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, c, x, y, rem; n char a[501][501]; n int v[501][501]; n void dfs(int x, int y) { n   if (rem == 0 || a[x][y] ==  # ) return; n   if (x < 0 || x >= n || y < 0 || y >= m || v[x][y] == 1) return; n   v[x][y] = 1; n   rem--; n   dfs(x + 1, y); n   dfs(x, y + 1); n   dfs(x, y - 1); n   dfs(x - 1, y); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       cin >> a[i][j]; n       if (a[i][j] ==  . ) { n         c++; n         if (c == 1) { n           x = i; n           y = j; n         } n       } n     } n   rem = c - k; n   dfs(x, y); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (v[i][j] == 0 && a[i][j] ==  . ) n         cout <<  X ; n       else n         cout << a[i][j]; n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> a[15]; n vector<pair<int, int>> all; n int rp[75000]; n long long sum[15]; n vector<pair<int, int>> loops; n int path[16]; n int plen = 0; n bool vis[1 << 15] = {0}; n int finstate; n bool dfs(int state, int ps) { n   if (state == finstate) return true; n   if (vis[state]) return false; n   vis[state] = true; n   for (int i = ps; i < loops.size(); i++) { n     int m = loops[i].first; n     if ((state & m) == 0 && !vis[state | m] && dfs(state | m, i + 1)) { n       path[plen++] = i; n       return true; n     } n   } n   return false; n } n int main() { n   ios::sync_with_stdio(false); n   int k; n   long long total = 0; n   cin >> k; n   for (int i = 0; i < k; i++) { n     int ni; n     cin >> ni; n     a[i].reserve(ni); n     long long s = 0; n     for (int j = 0; j < ni; ++j) { n       int x; n       cin >> x; n       s += x; n       a[i].push_back(x); n       all.push_back(make_pair(x, i)); n     } n     sum[i] = s; n     total += s; n   } n   if (total % k != 0) { n     cout <<  No  << endl; n     return 0; n   } n   long long final = total / k; n   sort(all.begin(), all.end()); n   int okmask = 0; n   for (int i = 0; i < k; i++) n     if (sum[i] == final) okmask |= 1 << i; n   int n = all.size(); n   for (int i = 0; i < n; i++) { n     long long v = all[i].first; n     long long delta = final - sum[all[i].second]; n     long long rep = v + delta; n     rp[i] = -1; n     if (abs(rep) <= 1000000000 && delta != 0) { n       auto pos = n           lower_bound(all.begin(), all.end(), make_pair<int, int>((int)rep, 0)); n       if (pos != all.end() && pos->first == rep && pos->second != all[i].second) n         rp[i] = pos - all.begin(); n     } n   } n   for (int i = 0; i < n; ++i) { n     if (rp[i] != -1) { n       int mask = (1 << all[i].second); n       int p = rp[i]; n       while (p >= 0 && (mask & (1 << all[p].second)) == 0) { n         mask |= (1 << all[p].second); n         p = rp[p]; n       } n       if (p == i) { n         loops.push_back(make_pair(mask, i)); n       } n     } n   } n   sort(loops.begin(), loops.end()); n   loops.erase(unique(loops.begin(), loops.end()), loops.end()); n   finstate = (1 << k) - 1; n   if (dfs(okmask, 0)) { n     int moveTo[15]; n     int val[15]; n     for (int i = 0; i < k; i++) { n       moveTo[i] = i; n       val[i] = a[i][0]; n     } n     if (a[0][0] == -174529428) { n       cout <<  okmask =   << okmask << endl; n       for (int i = 0; i < plen; i++) { n         cout <<  mask =   << loops[path[i]].first << endl; n       } n     } n     for (int i = 0; i < plen; i++) { n       int p0 = loops[path[i]].second; n       int from = p0; n       int p = rp[from]; n       while (p != p0) { n         int grp = all[p].second; n         moveTo[grp] = all[from].second; n         val[grp] = all[p].first; n         from = p; n         p = rp[from]; n       } n       int sg = all[p].second; n       moveTo[sg] = all[from].second; n       val[sg] = all[p].first; n     } n     cout <<  Yes  << endl; n     for (int i = 0; i < k; i++) { n       cout << val[i] <<     << (moveTo[i] + 1) << endl; n     } n   } else { n     cout <<  No  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n const int mod = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   long long ans = 0; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     ans += ((i % 5) + m) / 5; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct opt { n   vector<set<int>> s; n   vector<int> l; n   vector<int> r; n   int n; n   opt(string t) { n     n = t.size(); n     t =  $  + t; n     int c = 1; n     s.resize(n + 1); n     l.assign(n + 1, 1); n     r.assign(n + 1, n); n     s[1].insert(n); n     for (int i = n - 1; i >= 1; i--) { n       if (t[i] == t[i + 1]) n         c++; n       else n         c = 1; n       s[c].insert(i); n     } n     char val = t[n]; n     for (int i = n - 1; i >= 1; i--) { n       if (t[i] == val) n         r[i] = r[i + 1]; n       else { n         val = t[i]; n         r[i] = i; n       } n     } n     val = t[1]; n     for (int i = 2; i <= n; i++) { n       if (t[i] == val) n         l[i] = l[i - 1]; n       else { n         val = t[i]; n         l[i] = i; n       } n     } n   } n   int nxt(int pos, int sz) { n     auto it = s[sz].lower_bound(pos); n     if (it == s[sz].end()) return -1; n     int x = *it; n     int a = max(pos, l[x]); n     return a + sz; n   } n }; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   string s; n   cin >> s; n   string t = s; n   for (int i = 0; i < n; i++) n     if (t[i] ==  ? ) t[i] =  0 ; n   opt a(t); n   t = s; n   for (int i = 0; i < n; i++) n     if (t[i] ==  ? ) t[i] =  1 ; n   opt b(t); n   for (int i = 1; i <= n; i++) { n     int now = 1; n     int res = 0; n     while (1) { n       int one = a.nxt(now, i); n       int two = b.nxt(now, i); n       if (one == -1 && two == -1) n         break; n       else if (one == -1) { n         res++; n         now = two; n       } else if (two == -1) { n         res++; n         now = one; n       } else { n         res++; n         now = min(one, two); n       } n     } n     cout << res <<    n [i == n]; n   } n   return 0; n } n


#include<cstdio> n #include<cstring> n #include<algorithm> n using namespace std; n #define N 100500 n int n,is[N],mn=1e9,f1=1,f2=1,as[N],ct,s2[N]; n char st[N]; n int main() n { n  scanf( %s ,st+1);n=strlen(st+1); n  st[n+2]= # ; n  int s1=0; n  for(int i=0;i<=n+2;i++)if(st[i]== # ) n  { n   if(!s1)continue; n   mn=min(mn,s1),is[s1-1]=1; n   if(s1&1)f2=0; n   if(s1>1)f1=0; n   s1=0; n  } n  else s1++; n  for(int i=1;i<=mn;i++) n  { n   if(i==1&&!f1)continue; n   if(i==2&&!f2)continue; n   if(is[i])continue; n   int ls=0; n   for(int j=0;j<=n+2;j++)if(st[j]== # ) n   { n    int l1=ls,r1=j-1;ls=j+1; n    if(l1>=j)continue; n    r1-=i-1;s2[r1]=1; n    while(l1<r1) n    { n     s2[l1]=1; n     int v1=l1+i; n     if(v1>r1)v1=r1; n     if(v1==r1-1)v1--; n     l1=v1; n    } n   } n   int s3=0; n   for(int j=1;j<=n-i+1;j++)if(s2[j]){if(s3)as[++ct]=s3,s3=0;}else s3++; n   if(s3)as[++ct]=s3; n   printf( %d n ,ct);for(int i=1;i<=ct;i++)printf( %d  ,as[i]); n   return 0; n  } n  printf( -1 n ); n }


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n int a[10], n; n char s1, s2; n int main() { n   a[0] = 118, a[1] = 60, a[2] = 94, a[3] = 62; n   while (cin >> s1 >> s2) { n     scanf( %d , &n); n     n %= 4; n     int k; n     for (int i = 0; i < 4; i++) n       if ((int)s1 == a[i]) { n         k = i; n         break; n       } n     int kk = (k + n) % 4, kkk = ((k - n) % 4 + 4) % 4; n     if (a[kk] == (int)s2 && kkk == kk) n       printf( undefined n ); n     else if (a[kk] == (int)s2) n       printf( cw n ); n     else n       printf( ccw n ); n   } n   return 0; n } n